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 struct socket *afs_socket; /* my RxRPC socket */
20 static struct workqueue_struct *afs_async_calls;
21 static struct afs_call *afs_spare_incoming_call;
22 atomic_t afs_outstanding_calls;
24 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
25 static int afs_wait_for_call_to_complete(struct afs_call *);
26 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
27 static void afs_process_async_call(struct work_struct *);
28 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
29 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
30 static int afs_deliver_cm_op_id(struct afs_call *);
32 /* asynchronous incoming call initial processing */
33 static const struct afs_call_type afs_RXCMxxxx = {
35 .deliver = afs_deliver_cm_op_id,
36 .abort_to_error = afs_abort_to_error,
39 static void afs_charge_preallocation(struct work_struct *);
41 static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
43 static int afs_wait_atomic_t(atomic_t *p)
50 * open an RxRPC socket and bind it to be a server for callback notifications
51 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
53 int afs_open_socket(void)
55 struct sockaddr_rxrpc srx;
56 struct socket *socket;
62 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
66 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
70 socket->sk->sk_allocation = GFP_NOFS;
72 /* bind the callback manager's address to make this a server socket */
73 srx.srx_family = AF_RXRPC;
74 srx.srx_service = CM_SERVICE;
75 srx.transport_type = SOCK_DGRAM;
76 srx.transport_len = sizeof(srx.transport.sin);
77 srx.transport.sin.sin_family = AF_INET;
78 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
79 memset(&srx.transport.sin.sin_addr, 0,
80 sizeof(srx.transport.sin.sin_addr));
82 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
86 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
87 afs_rx_discard_new_call);
89 ret = kernel_listen(socket, INT_MAX);
94 afs_charge_preallocation(NULL);
101 destroy_workqueue(afs_async_calls);
103 _leave(" = %d", ret);
108 * close the RxRPC socket AFS was using
110 void afs_close_socket(void)
114 kernel_listen(afs_socket, 0);
115 flush_workqueue(afs_async_calls);
117 if (afs_spare_incoming_call) {
118 afs_put_call(afs_spare_incoming_call);
119 afs_spare_incoming_call = NULL;
122 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
123 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
124 TASK_UNINTERRUPTIBLE);
125 _debug("no outstanding calls");
127 kernel_sock_shutdown(afs_socket, SHUT_RDWR);
128 flush_workqueue(afs_async_calls);
129 sock_release(afs_socket);
132 destroy_workqueue(afs_async_calls);
139 static struct afs_call *afs_alloc_call(const struct afs_call_type *type,
142 struct afs_call *call;
145 call = kzalloc(sizeof(*call), gfp);
150 atomic_set(&call->usage, 1);
151 INIT_WORK(&call->async_work, afs_process_async_call);
152 init_waitqueue_head(&call->waitq);
154 o = atomic_inc_return(&afs_outstanding_calls);
155 trace_afs_call(call, afs_call_trace_alloc, 1, o,
156 __builtin_return_address(0));
161 * Dispose of a reference on a call.
163 void afs_put_call(struct afs_call *call)
165 int n = atomic_dec_return(&call->usage);
166 int o = atomic_read(&afs_outstanding_calls);
168 trace_afs_call(call, afs_call_trace_put, n + 1, o,
169 __builtin_return_address(0));
173 ASSERT(!work_pending(&call->async_work));
174 ASSERT(call->type->name != NULL);
177 rxrpc_kernel_end_call(afs_socket, call->rxcall);
180 if (call->type->destructor)
181 call->type->destructor(call);
183 kfree(call->request);
186 o = atomic_dec_return(&afs_outstanding_calls);
187 trace_afs_call(call, afs_call_trace_free, 0, o,
188 __builtin_return_address(0));
190 wake_up_atomic_t(&afs_outstanding_calls);
195 * Queue the call for actual work. Returns 0 unconditionally for convenience.
197 int afs_queue_call_work(struct afs_call *call)
199 int u = atomic_inc_return(&call->usage);
201 trace_afs_call(call, afs_call_trace_work, u,
202 atomic_read(&afs_outstanding_calls),
203 __builtin_return_address(0));
205 INIT_WORK(&call->work, call->type->work);
207 if (!queue_work(afs_wq, &call->work))
213 * allocate a call with flat request and reply buffers
215 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
216 size_t request_size, size_t reply_max)
218 struct afs_call *call;
220 call = afs_alloc_call(type, GFP_NOFS);
225 call->request_size = request_size;
226 call->request = kmalloc(request_size, GFP_NOFS);
232 call->reply_max = reply_max;
233 call->buffer = kmalloc(reply_max, GFP_NOFS);
238 init_waitqueue_head(&call->waitq);
248 * clean up a call with flat buffer
250 void afs_flat_call_destructor(struct afs_call *call)
254 kfree(call->request);
255 call->request = NULL;
261 * attach the data from a bunch of pages on an inode to a call
263 static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
266 struct page *pages[8];
267 unsigned count, n, loop, offset, to;
268 pgoff_t first = call->first, last = call->last;
273 offset = call->first_offset;
274 call->first_offset = 0;
277 _debug("attach %lx-%lx", first, last);
279 count = last - first + 1;
280 if (count > ARRAY_SIZE(pages))
281 count = ARRAY_SIZE(pages);
282 n = find_get_pages_contig(call->mapping, first, count, pages);
283 ASSERTCMP(n, ==, count);
289 if (first + loop >= last)
292 msg->msg_flags = MSG_MORE;
293 iov->iov_base = kmap(pages[loop]) + offset;
294 iov->iov_len = to - offset;
297 _debug("- range %u-%u%s",
298 offset, to, msg->msg_flags ? " [more]" : "");
299 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
300 iov, 1, to - offset);
302 /* have to change the state *before* sending the last
303 * packet as RxRPC might give us the reply before it
304 * returns from sending the request */
305 if (first + loop >= last)
306 call->state = AFS_CALL_AWAIT_REPLY;
307 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
312 } while (++loop < count);
315 for (loop = 0; loop < count; loop++)
316 put_page(pages[loop]);
319 } while (first <= last);
321 _leave(" = %d", ret);
328 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
331 struct sockaddr_rxrpc srx;
332 struct rxrpc_call *rxcall;
337 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
339 ASSERT(call->type != NULL);
340 ASSERT(call->type->name != NULL);
342 _debug("____MAKE %p{%s,%x} [%d]____",
343 call, call->type->name, key_serial(call->key),
344 atomic_read(&afs_outstanding_calls));
348 memset(&srx, 0, sizeof(srx));
349 srx.srx_family = AF_RXRPC;
350 srx.srx_service = call->service_id;
351 srx.transport_type = SOCK_DGRAM;
352 srx.transport_len = sizeof(srx.transport.sin);
353 srx.transport.sin.sin_family = AF_INET;
354 srx.transport.sin.sin_port = call->port;
355 memcpy(&srx.transport.sin.sin_addr, addr, 4);
358 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
359 (unsigned long) call, gfp,
361 afs_wake_up_async_call :
362 afs_wake_up_call_waiter));
364 if (IS_ERR(rxcall)) {
365 ret = PTR_ERR(rxcall);
366 goto error_kill_call;
369 call->rxcall = rxcall;
371 /* send the request */
372 iov[0].iov_base = call->request;
373 iov[0].iov_len = call->request_size;
377 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
379 msg.msg_control = NULL;
380 msg.msg_controllen = 0;
381 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
383 /* have to change the state *before* sending the last packet as RxRPC
384 * might give us the reply before it returns from sending the
386 if (!call->send_pages)
387 call->state = AFS_CALL_AWAIT_REPLY;
388 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
389 &msg, call->request_size);
393 if (call->send_pages) {
394 ret = afs_send_pages(call, &msg, iov);
399 /* at this point, an async call may no longer exist as it may have
400 * already completed */
404 return afs_wait_for_call_to_complete(call);
407 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
410 _leave(" = %d", ret);
415 * deliver messages to a call
417 static void afs_deliver_to_call(struct afs_call *call)
422 _enter("%s", call->type->name);
424 while (call->state == AFS_CALL_AWAIT_REPLY ||
425 call->state == AFS_CALL_AWAIT_OP_ID ||
426 call->state == AFS_CALL_AWAIT_REQUEST ||
427 call->state == AFS_CALL_AWAIT_ACK
429 if (call->state == AFS_CALL_AWAIT_ACK) {
431 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
432 NULL, 0, &offset, false,
434 trace_afs_recv_data(call, 0, offset, false, ret);
436 if (ret == -EINPROGRESS || ret == -EAGAIN)
438 if (ret == 1 || ret < 0) {
439 call->state = AFS_CALL_COMPLETE;
445 ret = call->type->deliver(call);
448 if (call->state == AFS_CALL_AWAIT_REPLY)
449 call->state = AFS_CALL_COMPLETE;
455 abort_code = RX_CALL_DEAD;
456 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
457 abort_code, -ret, "KNC");
460 abort_code = RX_INVALID_OPERATION;
461 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
462 abort_code, -ret, "KIV");
468 abort_code = RXGEN_CC_UNMARSHAL;
469 if (call->state != AFS_CALL_AWAIT_REPLY)
470 abort_code = RXGEN_SS_UNMARSHAL;
471 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
472 abort_code, EBADMSG, "KUM");
478 if (call->state == AFS_CALL_COMPLETE && call->incoming)
486 call->state = AFS_CALL_COMPLETE;
491 * wait synchronously for a call to complete
493 static int afs_wait_for_call_to_complete(struct afs_call *call)
495 const char *abort_why;
498 DECLARE_WAITQUEUE(myself, current);
502 add_wait_queue(&call->waitq, &myself);
504 set_current_state(TASK_INTERRUPTIBLE);
506 /* deliver any messages that are in the queue */
507 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
508 call->need_attention = false;
509 __set_current_state(TASK_RUNNING);
510 afs_deliver_to_call(call);
516 if (call->state == AFS_CALL_COMPLETE)
520 if (signal_pending(current))
525 remove_wait_queue(&call->waitq, &myself);
526 __set_current_state(TASK_RUNNING);
529 if (call->state < AFS_CALL_COMPLETE) {
530 _debug("call incomplete");
531 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
532 RX_CALL_DEAD, -ret, abort_why);
535 _debug("call complete");
537 _leave(" = %d", ret);
542 * wake up a waiting call
544 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
545 unsigned long call_user_ID)
547 struct afs_call *call = (struct afs_call *)call_user_ID;
549 call->need_attention = true;
550 wake_up(&call->waitq);
554 * wake up an asynchronous call
556 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
557 unsigned long call_user_ID)
559 struct afs_call *call = (struct afs_call *)call_user_ID;
562 trace_afs_notify_call(rxcall, call);
563 call->need_attention = true;
565 u = __atomic_add_unless(&call->usage, 1, 0);
567 trace_afs_call(call, afs_call_trace_wake, u,
568 atomic_read(&afs_outstanding_calls),
569 __builtin_return_address(0));
571 if (!queue_work(afs_async_calls, &call->async_work))
577 * Delete an asynchronous call. The work item carries a ref to the call struct
578 * that we need to release.
580 static void afs_delete_async_call(struct work_struct *work)
582 struct afs_call *call = container_of(work, struct afs_call, async_work);
592 * Perform I/O processing on an asynchronous call. The work item carries a ref
593 * to the call struct that we either need to release or to pass on.
595 static void afs_process_async_call(struct work_struct *work)
597 struct afs_call *call = container_of(work, struct afs_call, async_work);
601 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
602 call->need_attention = false;
603 afs_deliver_to_call(call);
606 if (call->state == AFS_CALL_COMPLETE) {
609 /* We have two refs to release - one from the alloc and one
610 * queued with the work item - and we can't just deallocate the
611 * call because the work item may be queued again.
613 call->async_work.func = afs_delete_async_call;
614 if (!queue_work(afs_async_calls, &call->async_work))
622 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
624 struct afs_call *call = (struct afs_call *)user_call_ID;
626 call->rxcall = rxcall;
630 * Charge the incoming call preallocation.
632 static void afs_charge_preallocation(struct work_struct *work)
634 struct afs_call *call = afs_spare_incoming_call;
638 call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL);
643 call->state = AFS_CALL_AWAIT_OP_ID;
644 init_waitqueue_head(&call->waitq);
647 if (rxrpc_kernel_charge_accept(afs_socket,
648 afs_wake_up_async_call,
655 afs_spare_incoming_call = call;
659 * Discard a preallocated call when a socket is shut down.
661 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
662 unsigned long user_call_ID)
664 struct afs_call *call = (struct afs_call *)user_call_ID;
671 * Notification of an incoming call.
673 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
674 unsigned long user_call_ID)
676 queue_work(afs_wq, &afs_charge_preallocation_work);
680 * Grab the operation ID from an incoming cache manager call. The socket
681 * buffer is discarded on error or if we don't yet have sufficient data.
683 static int afs_deliver_cm_op_id(struct afs_call *call)
687 _enter("{%zu}", call->offset);
689 ASSERTCMP(call->offset, <, 4);
691 /* the operation ID forms the first four bytes of the request data */
692 ret = afs_extract_data(call, &call->tmp, 4, true);
696 call->operation_ID = ntohl(call->tmp);
697 call->state = AFS_CALL_AWAIT_REQUEST;
700 /* ask the cache manager to route the call (it'll change the call type
702 if (!afs_cm_incoming_call(call))
705 trace_afs_cb_call(call);
707 /* pass responsibility for the remainer of this message off to the
708 * cache manager op */
709 return call->type->deliver(call);
713 * send an empty reply
715 void afs_send_empty_reply(struct afs_call *call)
723 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
724 msg.msg_control = NULL;
725 msg.msg_controllen = 0;
728 call->state = AFS_CALL_AWAIT_ACK;
729 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
731 _leave(" [replied]");
736 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
737 RX_USER_ABORT, ENOMEM, "KOO");
745 * send a simple reply
747 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
755 iov[0].iov_base = (void *) buf;
756 iov[0].iov_len = len;
759 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
760 msg.msg_control = NULL;
761 msg.msg_controllen = 0;
764 call->state = AFS_CALL_AWAIT_ACK;
765 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
768 _leave(" [replied]");
774 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
775 RX_USER_ABORT, ENOMEM, "KOO");
781 * Extract a piece of data from the received data socket buffers.
783 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
788 _enter("{%s,%zu},,%zu,%d",
789 call->type->name, call->offset, count, want_more);
791 ASSERTCMP(call->offset, <=, count);
793 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
794 buf, count, &call->offset,
795 want_more, &call->abort_code);
796 trace_afs_recv_data(call, count, call->offset, want_more, ret);
797 if (ret == 0 || ret == -EAGAIN)
801 switch (call->state) {
802 case AFS_CALL_AWAIT_REPLY:
803 call->state = AFS_CALL_COMPLETE;
805 case AFS_CALL_AWAIT_REQUEST:
806 call->state = AFS_CALL_REPLYING;
814 if (ret == -ECONNABORTED)
815 call->error = call->type->abort_to_error(call->abort_code);
818 call->state = AFS_CALL_COMPLETE;