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 atomic_t afs_outstanding_calls;
23 static void afs_free_call(struct afs_call *);
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 int afs_dont_wait_for_call_to_complete(struct afs_call *);
28 static void afs_process_async_call(struct work_struct *);
29 static void afs_rx_new_call(struct sock *);
30 static int afs_deliver_cm_op_id(struct afs_call *);
32 /* synchronous call management */
33 const struct afs_wait_mode afs_sync_call = {
34 .notify_rx = afs_wake_up_call_waiter,
35 .wait = afs_wait_for_call_to_complete,
38 /* asynchronous call management */
39 const struct afs_wait_mode afs_async_call = {
40 .notify_rx = afs_wake_up_async_call,
41 .wait = afs_dont_wait_for_call_to_complete,
44 /* asynchronous incoming call management */
45 static const struct afs_wait_mode afs_async_incoming_call = {
46 .notify_rx = afs_wake_up_async_call,
49 /* asynchronous incoming call initial processing */
50 static const struct afs_call_type afs_RXCMxxxx = {
52 .deliver = afs_deliver_cm_op_id,
53 .abort_to_error = afs_abort_to_error,
56 static void afs_collect_incoming_call(struct work_struct *);
58 static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
60 static int afs_wait_atomic_t(atomic_t *p)
67 * open an RxRPC socket and bind it to be a server for callback notifications
68 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
70 int afs_open_socket(void)
72 struct sockaddr_rxrpc srx;
73 struct socket *socket;
79 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
83 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
87 socket->sk->sk_allocation = GFP_NOFS;
89 /* bind the callback manager's address to make this a server socket */
90 srx.srx_family = AF_RXRPC;
91 srx.srx_service = CM_SERVICE;
92 srx.transport_type = SOCK_DGRAM;
93 srx.transport_len = sizeof(srx.transport.sin);
94 srx.transport.sin.sin_family = AF_INET;
95 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
96 memset(&srx.transport.sin.sin_addr, 0,
97 sizeof(srx.transport.sin.sin_addr));
99 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
103 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call);
105 ret = kernel_listen(socket, INT_MAX);
114 sock_release(socket);
116 destroy_workqueue(afs_async_calls);
118 _leave(" = %d", ret);
123 * close the RxRPC socket AFS was using
125 void afs_close_socket(void)
129 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
130 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
131 TASK_UNINTERRUPTIBLE);
132 _debug("no outstanding calls");
134 flush_workqueue(afs_async_calls);
135 sock_release(afs_socket);
138 destroy_workqueue(afs_async_calls);
145 static void afs_free_call(struct afs_call *call)
147 _debug("DONE %p{%s} [%d]",
148 call, call->type->name, atomic_read(&afs_outstanding_calls));
150 ASSERTCMP(call->rxcall, ==, NULL);
151 ASSERT(!work_pending(&call->async_work));
152 ASSERT(call->type->name != NULL);
154 kfree(call->request);
157 if (atomic_dec_and_test(&afs_outstanding_calls))
158 wake_up_atomic_t(&afs_outstanding_calls);
162 * End a call but do not free it
164 static void afs_end_call_nofree(struct afs_call *call)
167 rxrpc_kernel_end_call(afs_socket, call->rxcall);
170 if (call->type->destructor)
171 call->type->destructor(call);
175 * End a call and free it
177 static void afs_end_call(struct afs_call *call)
179 afs_end_call_nofree(call);
184 * allocate a call with flat request and reply buffers
186 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
187 size_t request_size, size_t reply_max)
189 struct afs_call *call;
191 call = kzalloc(sizeof(*call), GFP_NOFS);
195 _debug("CALL %p{%s} [%d]",
196 call, type->name, atomic_read(&afs_outstanding_calls));
197 atomic_inc(&afs_outstanding_calls);
200 call->request_size = request_size;
201 call->reply_max = reply_max;
204 call->request = kmalloc(request_size, GFP_NOFS);
210 call->buffer = kmalloc(reply_max, GFP_NOFS);
215 init_waitqueue_head(&call->waitq);
225 * clean up a call with flat buffer
227 void afs_flat_call_destructor(struct afs_call *call)
231 kfree(call->request);
232 call->request = NULL;
238 * attach the data from a bunch of pages on an inode to a call
240 static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
243 struct page *pages[8];
244 unsigned count, n, loop, offset, to;
245 pgoff_t first = call->first, last = call->last;
250 offset = call->first_offset;
251 call->first_offset = 0;
254 _debug("attach %lx-%lx", first, last);
256 count = last - first + 1;
257 if (count > ARRAY_SIZE(pages))
258 count = ARRAY_SIZE(pages);
259 n = find_get_pages_contig(call->mapping, first, count, pages);
260 ASSERTCMP(n, ==, count);
266 if (first + loop >= last)
269 msg->msg_flags = MSG_MORE;
270 iov->iov_base = kmap(pages[loop]) + offset;
271 iov->iov_len = to - offset;
274 _debug("- range %u-%u%s",
275 offset, to, msg->msg_flags ? " [more]" : "");
276 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
277 iov, 1, to - offset);
279 /* have to change the state *before* sending the last
280 * packet as RxRPC might give us the reply before it
281 * returns from sending the request */
282 if (first + loop >= last)
283 call->state = AFS_CALL_AWAIT_REPLY;
284 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
289 } while (++loop < count);
292 for (loop = 0; loop < count; loop++)
293 put_page(pages[loop]);
296 } while (first <= last);
298 _leave(" = %d", ret);
305 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
306 const struct afs_wait_mode *wait_mode)
308 struct sockaddr_rxrpc srx;
309 struct rxrpc_call *rxcall;
314 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
316 ASSERT(call->type != NULL);
317 ASSERT(call->type->name != NULL);
319 _debug("____MAKE %p{%s,%x} [%d]____",
320 call, call->type->name, key_serial(call->key),
321 atomic_read(&afs_outstanding_calls));
323 call->wait_mode = wait_mode;
324 INIT_WORK(&call->async_work, afs_process_async_call);
326 memset(&srx, 0, sizeof(srx));
327 srx.srx_family = AF_RXRPC;
328 srx.srx_service = call->service_id;
329 srx.transport_type = SOCK_DGRAM;
330 srx.transport_len = sizeof(srx.transport.sin);
331 srx.transport.sin.sin_family = AF_INET;
332 srx.transport.sin.sin_port = call->port;
333 memcpy(&srx.transport.sin.sin_addr, addr, 4);
336 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
337 (unsigned long) call, gfp,
338 wait_mode->notify_rx);
340 if (IS_ERR(rxcall)) {
341 ret = PTR_ERR(rxcall);
342 goto error_kill_call;
345 call->rxcall = rxcall;
347 /* send the request */
348 iov[0].iov_base = call->request;
349 iov[0].iov_len = call->request_size;
353 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
355 msg.msg_control = NULL;
356 msg.msg_controllen = 0;
357 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
359 /* have to change the state *before* sending the last packet as RxRPC
360 * might give us the reply before it returns from sending the
362 if (!call->send_pages)
363 call->state = AFS_CALL_AWAIT_REPLY;
364 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
365 &msg, call->request_size);
369 if (call->send_pages) {
370 ret = afs_send_pages(call, &msg, iov);
375 /* at this point, an async call may no longer exist as it may have
376 * already completed */
377 return wait_mode->wait(call);
380 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT);
383 _leave(" = %d", ret);
388 * deliver messages to a call
390 static void afs_deliver_to_call(struct afs_call *call)
395 _enter("%s", call->type->name);
397 while (call->state == AFS_CALL_AWAIT_REPLY ||
398 call->state == AFS_CALL_AWAIT_OP_ID ||
399 call->state == AFS_CALL_AWAIT_REQUEST ||
400 call->state == AFS_CALL_AWAIT_ACK
402 if (call->state == AFS_CALL_AWAIT_ACK) {
404 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
405 NULL, 0, &offset, false,
407 if (ret == -EINPROGRESS || ret == -EAGAIN)
410 call->state = AFS_CALL_COMPLETE;
416 ret = call->type->deliver(call);
419 if (call->state == AFS_CALL_AWAIT_REPLY)
420 call->state = AFS_CALL_COMPLETE;
426 abort_code = RX_CALL_DEAD;
427 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
431 abort_code = RX_INVALID_OPERATION;
432 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
439 abort_code = RXGEN_CC_UNMARSHAL;
440 if (call->state != AFS_CALL_AWAIT_REPLY)
441 abort_code = RXGEN_SS_UNMARSHAL;
442 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
449 if (call->state == AFS_CALL_COMPLETE && call->incoming)
457 call->state = AFS_CALL_COMPLETE;
462 * wait synchronously for a call to complete
464 static int afs_wait_for_call_to_complete(struct afs_call *call)
468 DECLARE_WAITQUEUE(myself, current);
472 add_wait_queue(&call->waitq, &myself);
474 set_current_state(TASK_INTERRUPTIBLE);
476 /* deliver any messages that are in the queue */
477 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
478 call->need_attention = false;
479 __set_current_state(TASK_RUNNING);
480 afs_deliver_to_call(call);
485 if (call->state == AFS_CALL_COMPLETE)
488 if (signal_pending(current))
493 remove_wait_queue(&call->waitq, &myself);
494 __set_current_state(TASK_RUNNING);
497 if (call->state < AFS_CALL_COMPLETE) {
498 _debug("call incomplete");
499 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
503 _debug("call complete");
505 _leave(" = %d", ret);
510 * wake up a waiting call
512 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
513 unsigned long call_user_ID)
515 struct afs_call *call = (struct afs_call *)call_user_ID;
517 call->need_attention = true;
518 wake_up(&call->waitq);
522 * wake up an asynchronous call
524 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
525 unsigned long call_user_ID)
527 struct afs_call *call = (struct afs_call *)call_user_ID;
529 call->need_attention = true;
530 queue_work(afs_async_calls, &call->async_work);
534 * put a call into asynchronous mode
535 * - mustn't touch the call descriptor as the call my have completed by the
538 static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
545 * delete an asynchronous call
547 static void afs_delete_async_call(struct work_struct *work)
549 struct afs_call *call = container_of(work, struct afs_call, async_work);
559 * perform processing on an asynchronous call
561 static void afs_process_async_call(struct work_struct *work)
563 struct afs_call *call = container_of(work, struct afs_call, async_work);
567 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
568 call->need_attention = false;
569 afs_deliver_to_call(call);
572 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
573 if (call->wait_mode->async_complete)
574 call->wait_mode->async_complete(call->reply,
579 afs_end_call_nofree(call);
581 /* we can't just delete the call because the work item may be
583 call->async_work.func = afs_delete_async_call;
584 queue_work(afs_async_calls, &call->async_work);
591 * accept the backlog of incoming calls
593 static void afs_collect_incoming_call(struct work_struct *work)
595 struct rxrpc_call *rxcall;
596 struct afs_call *call = NULL;
602 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
604 rxrpc_kernel_reject_call(afs_socket);
608 INIT_WORK(&call->async_work, afs_process_async_call);
609 call->wait_mode = &afs_async_incoming_call;
610 call->type = &afs_RXCMxxxx;
611 init_waitqueue_head(&call->waitq);
612 call->state = AFS_CALL_AWAIT_OP_ID;
614 _debug("CALL %p{%s} [%d]",
615 call, call->type->name,
616 atomic_read(&afs_outstanding_calls));
617 atomic_inc(&afs_outstanding_calls);
620 rxcall = rxrpc_kernel_accept_call(afs_socket,
622 afs_wake_up_async_call);
623 if (!IS_ERR(rxcall)) {
624 call->rxcall = rxcall;
625 call->need_attention = true;
626 queue_work(afs_async_calls, &call->async_work);
636 * Notification of an incoming call.
638 static void afs_rx_new_call(struct sock *sk)
640 queue_work(afs_wq, &afs_collect_incoming_call_work);
644 * Grab the operation ID from an incoming cache manager call. The socket
645 * buffer is discarded on error or if we don't yet have sufficient data.
647 static int afs_deliver_cm_op_id(struct afs_call *call)
651 _enter("{%zu}", call->offset);
653 ASSERTCMP(call->offset, <, 4);
655 /* the operation ID forms the first four bytes of the request data */
656 ret = afs_extract_data(call, &call->operation_ID, 4, true);
660 call->state = AFS_CALL_AWAIT_REQUEST;
663 /* ask the cache manager to route the call (it'll change the call type
665 if (!afs_cm_incoming_call(call))
668 /* pass responsibility for the remainer of this message off to the
669 * cache manager op */
670 return call->type->deliver(call);
674 * send an empty reply
676 void afs_send_empty_reply(struct afs_call *call)
684 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
685 msg.msg_control = NULL;
686 msg.msg_controllen = 0;
689 call->state = AFS_CALL_AWAIT_ACK;
690 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
692 _leave(" [replied]");
697 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
707 * send a simple reply
709 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
717 iov[0].iov_base = (void *) buf;
718 iov[0].iov_len = len;
721 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
722 msg.msg_control = NULL;
723 msg.msg_controllen = 0;
726 call->state = AFS_CALL_AWAIT_ACK;
727 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
730 _leave(" [replied]");
736 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
744 * Extract a piece of data from the received data socket buffers.
746 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
751 _enter("{%s,%zu},,%zu,%d",
752 call->type->name, call->offset, count, want_more);
754 ASSERTCMP(call->offset, <=, count);
756 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
757 buf, count, &call->offset,
758 want_more, &call->abort_code);
759 if (ret == 0 || ret == -EAGAIN)
763 switch (call->state) {
764 case AFS_CALL_AWAIT_REPLY:
765 call->state = AFS_CALL_COMPLETE;
767 case AFS_CALL_AWAIT_REQUEST:
768 call->state = AFS_CALL_REPLYING;
776 if (ret == -ECONNABORTED)
777 call->error = call->type->abort_to_error(call->abort_code);
780 call->state = AFS_CALL_COMPLETE;