arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / afs / rxrpc.c
blob523b1d3ca2c68437566d920ea5332735774be43c
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 <net/sock.h>
14 #include <net/af_rxrpc.h>
15 #include <rxrpc/packet.h>
16 #include "internal.h"
17 #include "afs_cm.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 static atomic_t afs_outstanding_calls;
24 static void afs_free_call(struct afs_call *);
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 int afs_dont_wait_for_call_to_complete(struct afs_call *);
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 /* synchronous call management */
35 const struct afs_wait_mode afs_sync_call = {
36 .notify_rx = afs_wake_up_call_waiter,
37 .wait = afs_wait_for_call_to_complete,
40 /* asynchronous call management */
41 const struct afs_wait_mode afs_async_call = {
42 .notify_rx = afs_wake_up_async_call,
43 .wait = afs_dont_wait_for_call_to_complete,
46 /* asynchronous incoming call management */
47 static const struct afs_wait_mode afs_async_incoming_call = {
48 .notify_rx = afs_wake_up_async_call,
51 /* asynchronous incoming call initial processing */
52 static const struct afs_call_type afs_RXCMxxxx = {
53 .name = "CB.xxxx",
54 .deliver = afs_deliver_cm_op_id,
55 .abort_to_error = afs_abort_to_error,
58 static void afs_charge_preallocation(struct work_struct *);
60 static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
62 static int afs_wait_atomic_t(atomic_t *p)
64 schedule();
65 return 0;
69 * open an RxRPC socket and bind it to be a server for callback notifications
70 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
72 int afs_open_socket(void)
74 struct sockaddr_rxrpc srx;
75 struct socket *socket;
76 int ret;
78 _enter("");
80 ret = -ENOMEM;
81 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
82 if (!afs_async_calls)
83 goto error_0;
85 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
86 if (ret < 0)
87 goto error_1;
89 socket->sk->sk_allocation = GFP_NOFS;
91 /* bind the callback manager's address to make this a server socket */
92 srx.srx_family = AF_RXRPC;
93 srx.srx_service = CM_SERVICE;
94 srx.transport_type = SOCK_DGRAM;
95 srx.transport_len = sizeof(srx.transport.sin);
96 srx.transport.sin.sin_family = AF_INET;
97 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
98 memset(&srx.transport.sin.sin_addr, 0,
99 sizeof(srx.transport.sin.sin_addr));
101 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
102 if (ret < 0)
103 goto error_2;
105 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
106 afs_rx_discard_new_call);
108 ret = kernel_listen(socket, INT_MAX);
109 if (ret < 0)
110 goto error_2;
112 afs_socket = socket;
113 afs_charge_preallocation(NULL);
114 _leave(" = 0");
115 return 0;
117 error_2:
118 sock_release(socket);
119 error_1:
120 destroy_workqueue(afs_async_calls);
121 error_0:
122 _leave(" = %d", ret);
123 return ret;
127 * close the RxRPC socket AFS was using
129 void afs_close_socket(void)
131 _enter("");
133 if (afs_spare_incoming_call) {
134 atomic_inc(&afs_outstanding_calls);
135 afs_free_call(afs_spare_incoming_call);
136 afs_spare_incoming_call = NULL;
139 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
140 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
141 TASK_UNINTERRUPTIBLE);
142 _debug("no outstanding calls");
144 flush_workqueue(afs_async_calls);
145 kernel_sock_shutdown(afs_socket, SHUT_RDWR);
146 flush_workqueue(afs_async_calls);
147 sock_release(afs_socket);
149 _debug("dework");
150 destroy_workqueue(afs_async_calls);
151 _leave("");
155 * free a call
157 static void afs_free_call(struct afs_call *call)
159 _debug("DONE %p{%s} [%d]",
160 call, call->type->name, atomic_read(&afs_outstanding_calls));
162 ASSERTCMP(call->rxcall, ==, NULL);
163 ASSERT(!work_pending(&call->async_work));
164 ASSERT(call->type->name != NULL);
166 kfree(call->request);
167 kfree(call);
169 if (atomic_dec_and_test(&afs_outstanding_calls))
170 wake_up_atomic_t(&afs_outstanding_calls);
174 * End a call but do not free it
176 static void afs_end_call_nofree(struct afs_call *call)
178 if (call->rxcall) {
179 rxrpc_kernel_end_call(afs_socket, call->rxcall);
180 call->rxcall = NULL;
182 if (call->type->destructor)
183 call->type->destructor(call);
187 * End a call and free it
189 static void afs_end_call(struct afs_call *call)
191 afs_end_call_nofree(call);
192 afs_free_call(call);
196 * allocate a call with flat request and reply buffers
198 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
199 size_t request_size, size_t reply_max)
201 struct afs_call *call;
203 call = kzalloc(sizeof(*call), GFP_NOFS);
204 if (!call)
205 goto nomem_call;
207 _debug("CALL %p{%s} [%d]",
208 call, type->name, atomic_read(&afs_outstanding_calls));
209 atomic_inc(&afs_outstanding_calls);
211 call->type = type;
212 call->request_size = request_size;
213 call->reply_max = reply_max;
215 if (request_size) {
216 call->request = kmalloc(request_size, GFP_NOFS);
217 if (!call->request)
218 goto nomem_free;
221 if (reply_max) {
222 call->buffer = kmalloc(reply_max, GFP_NOFS);
223 if (!call->buffer)
224 goto nomem_free;
227 init_waitqueue_head(&call->waitq);
228 return call;
230 nomem_free:
231 afs_free_call(call);
232 nomem_call:
233 return NULL;
237 * clean up a call with flat buffer
239 void afs_flat_call_destructor(struct afs_call *call)
241 _enter("");
243 kfree(call->request);
244 call->request = NULL;
245 kfree(call->buffer);
246 call->buffer = NULL;
250 * attach the data from a bunch of pages on an inode to a call
252 static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
253 struct kvec *iov)
255 struct page *pages[8];
256 unsigned count, n, loop, offset, to;
257 pgoff_t first = call->first, last = call->last;
258 int ret;
260 _enter("");
262 offset = call->first_offset;
263 call->first_offset = 0;
265 do {
266 _debug("attach %lx-%lx", first, last);
268 count = last - first + 1;
269 if (count > ARRAY_SIZE(pages))
270 count = ARRAY_SIZE(pages);
271 n = find_get_pages_contig(call->mapping, first, count, pages);
272 ASSERTCMP(n, ==, count);
274 loop = 0;
275 do {
276 msg->msg_flags = 0;
277 to = PAGE_SIZE;
278 if (first + loop >= last)
279 to = call->last_to;
280 else
281 msg->msg_flags = MSG_MORE;
282 iov->iov_base = kmap(pages[loop]) + offset;
283 iov->iov_len = to - offset;
284 offset = 0;
286 _debug("- range %u-%u%s",
287 offset, to, msg->msg_flags ? " [more]" : "");
288 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
289 iov, 1, to - offset);
291 /* have to change the state *before* sending the last
292 * packet as RxRPC might give us the reply before it
293 * returns from sending the request */
294 if (first + loop >= last)
295 call->state = AFS_CALL_AWAIT_REPLY;
296 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
297 msg, to - offset);
298 kunmap(pages[loop]);
299 if (ret < 0)
300 break;
301 } while (++loop < count);
302 first += count;
304 for (loop = 0; loop < count; loop++)
305 put_page(pages[loop]);
306 if (ret < 0)
307 break;
308 } while (first <= last);
310 _leave(" = %d", ret);
311 return ret;
315 * initiate a call
317 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
318 const struct afs_wait_mode *wait_mode)
320 struct sockaddr_rxrpc srx;
321 struct rxrpc_call *rxcall;
322 struct msghdr msg;
323 struct kvec iov[1];
324 size_t offset;
325 u32 abort_code;
326 int ret;
328 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
330 ASSERT(call->type != NULL);
331 ASSERT(call->type->name != NULL);
333 _debug("____MAKE %p{%s,%x} [%d]____",
334 call, call->type->name, key_serial(call->key),
335 atomic_read(&afs_outstanding_calls));
337 call->wait_mode = wait_mode;
338 INIT_WORK(&call->async_work, afs_process_async_call);
340 memset(&srx, 0, sizeof(srx));
341 srx.srx_family = AF_RXRPC;
342 srx.srx_service = call->service_id;
343 srx.transport_type = SOCK_DGRAM;
344 srx.transport_len = sizeof(srx.transport.sin);
345 srx.transport.sin.sin_family = AF_INET;
346 srx.transport.sin.sin_port = call->port;
347 memcpy(&srx.transport.sin.sin_addr, addr, 4);
349 /* create a call */
350 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
351 (unsigned long) call, gfp,
352 wait_mode->notify_rx);
353 call->key = NULL;
354 if (IS_ERR(rxcall)) {
355 ret = PTR_ERR(rxcall);
356 goto error_kill_call;
359 call->rxcall = rxcall;
361 /* send the request */
362 iov[0].iov_base = call->request;
363 iov[0].iov_len = call->request_size;
365 msg.msg_name = NULL;
366 msg.msg_namelen = 0;
367 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
368 call->request_size);
369 msg.msg_control = NULL;
370 msg.msg_controllen = 0;
371 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
373 /* We have to change the state *before* sending the last packet as
374 * rxrpc might give us the reply before it returns from sending the
375 * request. Further, if the send fails, we may already have been given
376 * a notification and may have collected it.
378 if (!call->send_pages)
379 call->state = AFS_CALL_AWAIT_REPLY;
380 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
381 &msg, call->request_size);
382 if (ret < 0)
383 goto error_do_abort;
385 if (call->send_pages) {
386 ret = afs_send_pages(call, &msg, iov);
387 if (ret < 0)
388 goto error_do_abort;
391 /* at this point, an async call may no longer exist as it may have
392 * already completed */
393 return wait_mode->wait(call);
395 error_do_abort:
396 call->state = AFS_CALL_COMPLETE;
397 if (ret != -ECONNABORTED) {
398 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT,
399 -ret, "KSD");
400 } else {
401 abort_code = 0;
402 offset = 0;
403 rxrpc_kernel_recv_data(afs_socket, rxcall, NULL, 0, &offset,
404 false, &abort_code);
405 ret = call->type->abort_to_error(abort_code);
407 error_kill_call:
408 afs_end_call(call);
409 _leave(" = %d", ret);
410 return ret;
414 * deliver messages to a call
416 static void afs_deliver_to_call(struct afs_call *call)
418 u32 abort_code;
419 int ret;
421 _enter("%s", call->type->name);
423 while (call->state == AFS_CALL_AWAIT_REPLY ||
424 call->state == AFS_CALL_AWAIT_OP_ID ||
425 call->state == AFS_CALL_AWAIT_REQUEST ||
426 call->state == AFS_CALL_AWAIT_ACK
428 if (call->state == AFS_CALL_AWAIT_ACK) {
429 size_t offset = 0;
430 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
431 NULL, 0, &offset, false,
432 &call->abort_code);
433 if (ret == -EINPROGRESS || ret == -EAGAIN)
434 return;
435 if (ret == 1 || ret < 0) {
436 call->state = AFS_CALL_COMPLETE;
437 goto done;
439 return;
442 ret = call->type->deliver(call);
443 switch (ret) {
444 case 0:
445 if (call->state == AFS_CALL_AWAIT_REPLY)
446 call->state = AFS_CALL_COMPLETE;
447 goto done;
448 case -EINPROGRESS:
449 case -EAGAIN:
450 goto out;
451 case -ECONNABORTED:
452 goto call_complete;
453 case -ENOTCONN:
454 abort_code = RX_CALL_DEAD;
455 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
456 abort_code, -ret, "KNC");
457 goto save_error;
458 case -ENOTSUPP:
459 abort_code = RXGEN_OPCODE;
460 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
461 abort_code, -ret, "KIV");
462 goto save_error;
463 case -ENODATA:
464 case -EBADMSG:
465 case -EMSGSIZE:
466 default:
467 abort_code = RXGEN_CC_UNMARSHAL;
468 if (call->state != AFS_CALL_AWAIT_REPLY)
469 abort_code = RXGEN_SS_UNMARSHAL;
470 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
471 abort_code, EBADMSG, "KUM");
472 goto save_error;
476 done:
477 if (call->state == AFS_CALL_COMPLETE && call->incoming)
478 afs_end_call(call);
479 out:
480 _leave("");
481 return;
483 save_error:
484 call->error = ret;
485 call_complete:
486 call->state = AFS_CALL_COMPLETE;
487 goto done;
491 * wait synchronously for a call to complete
493 static int afs_wait_for_call_to_complete(struct afs_call *call)
495 int ret;
497 DECLARE_WAITQUEUE(myself, current);
499 _enter("");
501 add_wait_queue(&call->waitq, &myself);
502 for (;;) {
503 set_current_state(TASK_INTERRUPTIBLE);
505 /* deliver any messages that are in the queue */
506 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
507 call->need_attention = false;
508 __set_current_state(TASK_RUNNING);
509 afs_deliver_to_call(call);
510 continue;
513 if (call->state == AFS_CALL_COMPLETE ||
514 signal_pending(current))
515 break;
516 schedule();
519 remove_wait_queue(&call->waitq, &myself);
520 __set_current_state(TASK_RUNNING);
522 /* Kill off the call if it's still live. */
523 if (call->state < AFS_CALL_COMPLETE) {
524 _debug("call interrupted");
525 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
526 RX_USER_ABORT, -EINTR, "KWI");
529 ret = call->error;
530 _debug("call complete");
531 afs_end_call(call);
532 _leave(" = %d", ret);
533 return ret;
537 * wake up a waiting call
539 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
540 unsigned long call_user_ID)
542 struct afs_call *call = (struct afs_call *)call_user_ID;
544 call->need_attention = true;
545 wake_up(&call->waitq);
549 * wake up an asynchronous call
551 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
552 unsigned long call_user_ID)
554 struct afs_call *call = (struct afs_call *)call_user_ID;
556 call->need_attention = true;
557 queue_work(afs_async_calls, &call->async_work);
561 * put a call into asynchronous mode
562 * - mustn't touch the call descriptor as the call my have completed by the
563 * time we get here
565 static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
567 _enter("");
568 return -EINPROGRESS;
572 * delete an asynchronous call
574 static void afs_delete_async_call(struct work_struct *work)
576 struct afs_call *call = container_of(work, struct afs_call, async_work);
578 _enter("");
580 afs_free_call(call);
582 _leave("");
586 * perform processing on an asynchronous call
588 static void afs_process_async_call(struct work_struct *work)
590 struct afs_call *call = container_of(work, struct afs_call, async_work);
592 _enter("");
594 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
595 call->need_attention = false;
596 afs_deliver_to_call(call);
599 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
600 if (call->wait_mode->async_complete)
601 call->wait_mode->async_complete(call->reply,
602 call->error);
603 call->reply = NULL;
605 /* kill the call */
606 afs_end_call_nofree(call);
608 /* we can't just delete the call because the work item may be
609 * queued */
610 call->async_work.func = afs_delete_async_call;
611 queue_work(afs_async_calls, &call->async_work);
614 _leave("");
617 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
619 struct afs_call *call = (struct afs_call *)user_call_ID;
621 call->rxcall = rxcall;
625 * Charge the incoming call preallocation.
627 static void afs_charge_preallocation(struct work_struct *work)
629 struct afs_call *call = afs_spare_incoming_call;
631 for (;;) {
632 if (!call) {
633 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
634 if (!call)
635 break;
637 INIT_WORK(&call->async_work, afs_process_async_call);
638 call->wait_mode = &afs_async_incoming_call;
639 call->type = &afs_RXCMxxxx;
640 init_waitqueue_head(&call->waitq);
641 call->state = AFS_CALL_AWAIT_OP_ID;
644 if (rxrpc_kernel_charge_accept(afs_socket,
645 afs_wake_up_async_call,
646 afs_rx_attach,
647 (unsigned long)call,
648 GFP_KERNEL) < 0)
649 break;
650 call = NULL;
652 afs_spare_incoming_call = call;
656 * Discard a preallocated call when a socket is shut down.
658 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
659 unsigned long user_call_ID)
661 struct afs_call *call = (struct afs_call *)user_call_ID;
663 atomic_inc(&afs_outstanding_calls);
664 call->rxcall = NULL;
665 afs_free_call(call);
669 * Notification of an incoming call.
671 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
672 unsigned long user_call_ID)
674 atomic_inc(&afs_outstanding_calls);
675 queue_work(afs_wq, &afs_charge_preallocation_work);
679 * Grab the operation ID from an incoming cache manager call. The socket
680 * buffer is discarded on error or if we don't yet have sufficient data.
682 static int afs_deliver_cm_op_id(struct afs_call *call)
684 int ret;
686 _enter("{%zu}", call->offset);
688 ASSERTCMP(call->offset, <, 4);
690 /* the operation ID forms the first four bytes of the request data */
691 ret = afs_extract_data(call, &call->tmp, 4, true);
692 if (ret < 0)
693 return ret;
695 call->operation_ID = ntohl(call->tmp);
696 call->state = AFS_CALL_AWAIT_REQUEST;
697 call->offset = 0;
699 /* ask the cache manager to route the call (it'll change the call type
700 * if successful) */
701 if (!afs_cm_incoming_call(call))
702 return -ENOTSUPP;
704 /* pass responsibility for the remainer of this message off to the
705 * cache manager op */
706 return call->type->deliver(call);
710 * send an empty reply
712 void afs_send_empty_reply(struct afs_call *call)
714 struct msghdr msg;
716 _enter("");
718 msg.msg_name = NULL;
719 msg.msg_namelen = 0;
720 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
721 msg.msg_control = NULL;
722 msg.msg_controllen = 0;
723 msg.msg_flags = 0;
725 call->state = AFS_CALL_AWAIT_ACK;
726 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
727 case 0:
728 _leave(" [replied]");
729 return;
731 case -ENOMEM:
732 _debug("oom");
733 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
734 RX_USER_ABORT, ENOMEM, "KOO");
735 default:
736 afs_end_call(call);
737 _leave(" [error]");
738 return;
743 * send a simple reply
745 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
747 struct msghdr msg;
748 struct kvec iov[1];
749 int n;
751 _enter("");
753 iov[0].iov_base = (void *) buf;
754 iov[0].iov_len = len;
755 msg.msg_name = NULL;
756 msg.msg_namelen = 0;
757 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
758 msg.msg_control = NULL;
759 msg.msg_controllen = 0;
760 msg.msg_flags = 0;
762 call->state = AFS_CALL_AWAIT_ACK;
763 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
764 if (n >= 0) {
765 /* Success */
766 _leave(" [replied]");
767 return;
770 if (n == -ENOMEM) {
771 _debug("oom");
772 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
773 RX_USER_ABORT, ENOMEM, "KOO");
775 afs_end_call(call);
776 _leave(" [error]");
780 * Extract a piece of data from the received data socket buffers.
782 int afs_extract_data(struct afs_call *call, void *buf, size_t count,
783 bool want_more)
785 int ret;
787 _enter("{%s,%zu},,%zu,%d",
788 call->type->name, call->offset, count, want_more);
790 ASSERTCMP(call->offset, <=, count);
792 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
793 buf, count, &call->offset,
794 want_more, &call->abort_code);
795 if (ret == 0 || ret == -EAGAIN)
796 return ret;
798 if (ret == 1) {
799 switch (call->state) {
800 case AFS_CALL_AWAIT_REPLY:
801 call->state = AFS_CALL_COMPLETE;
802 break;
803 case AFS_CALL_AWAIT_REQUEST:
804 call->state = AFS_CALL_REPLYING;
805 break;
806 default:
807 break;
809 return 0;
812 if (ret == -ECONNABORTED)
813 call->error = call->type->abort_to_error(call->abort_code);
814 else
815 call->error = ret;
816 call->state = AFS_CALL_COMPLETE;
817 return ret;