David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1 | /* Maintain an RxRPC server socket to do AFS communications through |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 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. |
| 10 | */ |
| 11 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 13 | #include <linux/sched/signal.h> |
| 14 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 15 | #include <net/sock.h> |
| 16 | #include <net/af_rxrpc.h> |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | #include "afs_cm.h" |
David Howells | 35dbfba | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 19 | #include "protocol_yfs.h" |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 20 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 21 | struct workqueue_struct *afs_async_calls; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 22 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 23 | static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 24 | static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long); |
David Howells | 34fa476 | 2019-01-10 15:40:50 +0000 | [diff] [blame] | 25 | static void afs_delete_async_call(struct work_struct *); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 26 | static void afs_process_async_call(struct work_struct *); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 27 | static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long); |
| 28 | static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 29 | static int afs_deliver_cm_op_id(struct afs_call *); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 30 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 31 | /* asynchronous incoming call initial processing */ |
| 32 | static const struct afs_call_type afs_RXCMxxxx = { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 33 | .name = "CB.xxxx", |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 34 | .deliver = afs_deliver_cm_op_id, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 37 | /* |
| 38 | * open an RxRPC socket and bind it to be a server for callback notifications |
| 39 | * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT |
| 40 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 41 | int afs_open_socket(struct afs_net *net) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 42 | { |
| 43 | struct sockaddr_rxrpc srx; |
| 44 | struct socket *socket; |
David Howells | 4776cab | 2018-05-10 23:10:40 +0100 | [diff] [blame] | 45 | unsigned int min_level; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 46 | int ret; |
| 47 | |
| 48 | _enter(""); |
| 49 | |
David Howells | 5b86d4f | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 50 | ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 51 | if (ret < 0) |
| 52 | goto error_1; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 53 | |
| 54 | socket->sk->sk_allocation = GFP_NOFS; |
| 55 | |
| 56 | /* bind the callback manager's address to make this a server socket */ |
David Howells | 3838d3e | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 57 | memset(&srx, 0, sizeof(srx)); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 58 | srx.srx_family = AF_RXRPC; |
| 59 | srx.srx_service = CM_SERVICE; |
| 60 | srx.transport_type = SOCK_DGRAM; |
David Howells | 3838d3e | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 61 | srx.transport_len = sizeof(srx.transport.sin6); |
| 62 | srx.transport.sin6.sin6_family = AF_INET6; |
| 63 | srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 64 | |
David Howells | 4776cab | 2018-05-10 23:10:40 +0100 | [diff] [blame] | 65 | min_level = RXRPC_SECURITY_ENCRYPT; |
| 66 | ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL, |
| 67 | (void *)&min_level, sizeof(min_level)); |
| 68 | if (ret < 0) |
| 69 | goto error_2; |
| 70 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 71 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
Marc Dionne | 83732ec | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 72 | if (ret == -EADDRINUSE) { |
| 73 | srx.transport.sin6.sin6_port = 0; |
| 74 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
| 75 | } |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 76 | if (ret < 0) |
| 77 | goto error_2; |
| 78 | |
David Howells | 35dbfba | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 79 | srx.srx_service = YFS_CM_SERVICE; |
| 80 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
| 81 | if (ret < 0) |
| 82 | goto error_2; |
| 83 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 84 | /* Ideally, we'd turn on service upgrade here, but we can't because |
| 85 | * OpenAFS is buggy and leaks the userStatus field from packet to |
| 86 | * packet and between FS packets and CB packets - so if we try to do an |
| 87 | * upgrade on an FS packet, OpenAFS will leak that into the CB packet |
| 88 | * it sends back to us. |
| 89 | */ |
David Howells | 35dbfba | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 90 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 91 | rxrpc_kernel_new_call_notification(socket, afs_rx_new_call, |
| 92 | afs_rx_discard_new_call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 93 | |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 94 | ret = kernel_listen(socket, INT_MAX); |
| 95 | if (ret < 0) |
| 96 | goto error_2; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 97 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 98 | net->socket = socket; |
| 99 | afs_charge_preallocation(&net->charge_preallocation_work); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 100 | _leave(" = 0"); |
| 101 | return 0; |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 102 | |
| 103 | error_2: |
| 104 | sock_release(socket); |
| 105 | error_1: |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 106 | _leave(" = %d", ret); |
| 107 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * close the RxRPC socket AFS was using |
| 112 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 113 | void afs_close_socket(struct afs_net *net) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 114 | { |
| 115 | _enter(""); |
| 116 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 117 | kernel_listen(net->socket, 0); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 118 | flush_workqueue(afs_async_calls); |
| 119 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 120 | if (net->spare_incoming_call) { |
| 121 | afs_put_call(net->spare_incoming_call); |
| 122 | net->spare_incoming_call = NULL; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 123 | } |
| 124 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 125 | _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls)); |
Peter Zijlstra | ab1fbe3 | 2018-03-15 11:42:28 +0100 | [diff] [blame] | 126 | wait_var_event(&net->nr_outstanding_calls, |
| 127 | !atomic_read(&net->nr_outstanding_calls)); |
David Howells | 2f02f7a | 2016-04-07 17:23:03 +0100 | [diff] [blame] | 128 | _debug("no outstanding calls"); |
| 129 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 130 | kernel_sock_shutdown(net->socket, SHUT_RDWR); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 131 | flush_workqueue(afs_async_calls); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 132 | sock_release(net->socket); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 133 | |
| 134 | _debug("dework"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 135 | _leave(""); |
| 136 | } |
| 137 | |
| 138 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 139 | * Allocate a call. |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 140 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 141 | static struct afs_call *afs_alloc_call(struct afs_net *net, |
| 142 | const struct afs_call_type *type, |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 143 | gfp_t gfp) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 144 | { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 145 | struct afs_call *call; |
| 146 | int o; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 147 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 148 | call = kzalloc(sizeof(*call), gfp); |
| 149 | if (!call) |
| 150 | return NULL; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 151 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 152 | call->type = type; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 153 | call->net = net; |
David Howells | a25e21f | 2018-03-27 23:03:00 +0100 | [diff] [blame] | 154 | call->debug_id = atomic_inc_return(&rxrpc_debug_id); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 155 | atomic_set(&call->usage, 1); |
| 156 | INIT_WORK(&call->async_work, afs_process_async_call); |
| 157 | init_waitqueue_head(&call->waitq); |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 158 | spin_lock_init(&call->state_lock); |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 159 | call->_iter = &call->iter; |
David Howells | 2f02f7a | 2016-04-07 17:23:03 +0100 | [diff] [blame] | 160 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 161 | o = atomic_inc_return(&net->nr_outstanding_calls); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 162 | trace_afs_call(call, afs_call_trace_alloc, 1, o, |
| 163 | __builtin_return_address(0)); |
| 164 | return call; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 168 | * Dispose of a reference on a call. |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 169 | */ |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 170 | void afs_put_call(struct afs_call *call) |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 171 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 172 | struct afs_net *net = call->net; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 173 | int n = atomic_dec_return(&call->usage); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 174 | int o = atomic_read(&net->nr_outstanding_calls); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 175 | |
| 176 | trace_afs_call(call, afs_call_trace_put, n + 1, o, |
| 177 | __builtin_return_address(0)); |
| 178 | |
| 179 | ASSERTCMP(n, >=, 0); |
| 180 | if (n == 0) { |
| 181 | ASSERT(!work_pending(&call->async_work)); |
| 182 | ASSERT(call->type->name != NULL); |
| 183 | |
| 184 | if (call->rxcall) { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 185 | rxrpc_kernel_end_call(net->socket, call->rxcall); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 186 | call->rxcall = NULL; |
| 187 | } |
| 188 | if (call->type->destructor) |
| 189 | call->type->destructor(call); |
| 190 | |
David Howells | d0676a1 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 191 | afs_put_server(call->net, call->cm_server); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 192 | afs_put_cb_interest(call->net, call->cbi); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 193 | afs_put_addrlist(call->alist); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 194 | kfree(call->request); |
David Howells | a25e21f | 2018-03-27 23:03:00 +0100 | [diff] [blame] | 195 | |
| 196 | trace_afs_call(call, afs_call_trace_free, 0, o, |
| 197 | __builtin_return_address(0)); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 198 | kfree(call); |
| 199 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 200 | o = atomic_dec_return(&net->nr_outstanding_calls); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 201 | if (o == 0) |
Peter Zijlstra | ab1fbe3 | 2018-03-15 11:42:28 +0100 | [diff] [blame] | 202 | wake_up_var(&net->nr_outstanding_calls); |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 203 | } |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 204 | } |
| 205 | |
David Howells | 7a75b00 | 2019-01-10 15:14:29 +0000 | [diff] [blame] | 206 | static struct afs_call *afs_get_call(struct afs_call *call, |
| 207 | enum afs_call_trace why) |
| 208 | { |
| 209 | int u = atomic_inc_return(&call->usage); |
| 210 | |
| 211 | trace_afs_call(call, why, u, |
| 212 | atomic_read(&call->net->nr_outstanding_calls), |
| 213 | __builtin_return_address(0)); |
| 214 | return call; |
| 215 | } |
| 216 | |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 217 | /* |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 218 | * Queue the call for actual work. |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 219 | */ |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 220 | static void afs_queue_call_work(struct afs_call *call) |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 221 | { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 222 | if (call->type->work) { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 223 | INIT_WORK(&call->work, call->type->work); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 224 | |
David Howells | 7a75b00 | 2019-01-10 15:14:29 +0000 | [diff] [blame] | 225 | afs_get_call(call, afs_call_trace_work); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 226 | if (!queue_work(afs_wq, &call->work)) |
| 227 | afs_put_call(call); |
| 228 | } |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 232 | * allocate a call with flat request and reply buffers |
| 233 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 234 | struct afs_call *afs_alloc_flat_call(struct afs_net *net, |
| 235 | const struct afs_call_type *type, |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 236 | size_t request_size, size_t reply_max) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 237 | { |
| 238 | struct afs_call *call; |
| 239 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 240 | call = afs_alloc_call(net, type, GFP_NOFS); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 241 | if (!call) |
| 242 | goto nomem_call; |
| 243 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 244 | if (request_size) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 245 | call->request_size = request_size; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 246 | call->request = kmalloc(request_size, GFP_NOFS); |
| 247 | if (!call->request) |
| 248 | goto nomem_free; |
| 249 | } |
| 250 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 251 | if (reply_max) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 252 | call->reply_max = reply_max; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 253 | call->buffer = kmalloc(reply_max, GFP_NOFS); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 254 | if (!call->buffer) |
| 255 | goto nomem_free; |
| 256 | } |
| 257 | |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 258 | afs_extract_to_buf(call, call->reply_max); |
David Howells | 025db80 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 259 | call->operation_ID = type->op; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 260 | init_waitqueue_head(&call->waitq); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 261 | return call; |
| 262 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 263 | nomem_free: |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 264 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 265 | nomem_call: |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * clean up a call with flat buffer |
| 271 | */ |
| 272 | void afs_flat_call_destructor(struct afs_call *call) |
| 273 | { |
| 274 | _enter(""); |
| 275 | |
| 276 | kfree(call->request); |
| 277 | call->request = NULL; |
| 278 | kfree(call->buffer); |
| 279 | call->buffer = NULL; |
| 280 | } |
| 281 | |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 282 | #define AFS_BVEC_MAX 8 |
| 283 | |
| 284 | /* |
| 285 | * Load the given bvec with the next few pages. |
| 286 | */ |
| 287 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, |
| 288 | struct bio_vec *bv, pgoff_t first, pgoff_t last, |
| 289 | unsigned offset) |
| 290 | { |
| 291 | struct page *pages[AFS_BVEC_MAX]; |
| 292 | unsigned int nr, n, i, to, bytes = 0; |
| 293 | |
| 294 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); |
| 295 | n = find_get_pages_contig(call->mapping, first, nr, pages); |
| 296 | ASSERTCMP(n, ==, nr); |
| 297 | |
| 298 | msg->msg_flags |= MSG_MORE; |
| 299 | for (i = 0; i < nr; i++) { |
| 300 | to = PAGE_SIZE; |
| 301 | if (first + i >= last) { |
| 302 | to = call->last_to; |
| 303 | msg->msg_flags &= ~MSG_MORE; |
| 304 | } |
| 305 | bv[i].bv_page = pages[i]; |
| 306 | bv[i].bv_len = to - offset; |
| 307 | bv[i].bv_offset = offset; |
| 308 | bytes += to - offset; |
| 309 | offset = 0; |
| 310 | } |
| 311 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 312 | iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes); |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 313 | } |
| 314 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 315 | /* |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 316 | * Advance the AFS call state when the RxRPC call ends the transmit phase. |
| 317 | */ |
| 318 | static void afs_notify_end_request_tx(struct sock *sock, |
| 319 | struct rxrpc_call *rxcall, |
| 320 | unsigned long call_user_ID) |
| 321 | { |
| 322 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 323 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 324 | afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY); |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 328 | * attach the data from a bunch of pages on an inode to a call |
| 329 | */ |
Al Viro | 39c6ace | 2016-01-09 20:36:51 -0500 | [diff] [blame] | 330 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 331 | { |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 332 | struct bio_vec bv[AFS_BVEC_MAX]; |
| 333 | unsigned int bytes, nr, loop, offset; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 334 | pgoff_t first = call->first, last = call->last; |
| 335 | int ret; |
| 336 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 337 | offset = call->first_offset; |
| 338 | call->first_offset = 0; |
| 339 | |
| 340 | do { |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 341 | afs_load_bvec(call, msg, bv, first, last, offset); |
David Howells | 2c09901 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 342 | trace_afs_send_pages(call, msg, first, last, offset); |
| 343 | |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 344 | offset = 0; |
| 345 | bytes = msg->msg_iter.count; |
| 346 | nr = msg->msg_iter.nr_segs; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 347 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 348 | ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg, |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 349 | bytes, afs_notify_end_request_tx); |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 350 | for (loop = 0; loop < nr; loop++) |
| 351 | put_page(bv[loop].bv_page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 352 | if (ret < 0) |
| 353 | break; |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 354 | |
| 355 | first += nr; |
David Howells | 5bbf5d3 | 2007-05-10 03:15:23 -0700 | [diff] [blame] | 356 | } while (first <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 357 | |
David Howells | 2c09901 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 358 | trace_afs_sent_pages(call, call->first, last, first, ret); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | /* |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 363 | * Initiate a call and synchronously queue up the parameters for dispatch. Any |
| 364 | * error is stored into the call struct, which the caller must check for. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 365 | */ |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 366 | void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 367 | { |
David Howells | 2feeaf8 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 368 | struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index]; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 369 | struct rxrpc_call *rxcall; |
| 370 | struct msghdr msg; |
| 371 | struct kvec iov[1]; |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 372 | s64 tx_total_len; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 373 | int ret; |
| 374 | |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 375 | _enter(",{%pISp},", &srx->transport); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 376 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 377 | ASSERT(call->type != NULL); |
| 378 | ASSERT(call->type->name != NULL); |
| 379 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 380 | _debug("____MAKE %p{%s,%x} [%d]____", |
| 381 | call, call->type->name, key_serial(call->key), |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 382 | atomic_read(&call->net->nr_outstanding_calls)); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 383 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 384 | call->addr_ix = ac->index; |
| 385 | call->alist = afs_get_addrlist(ac->alist); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 386 | |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 387 | /* Work out the length we're going to transmit. This is awkward for |
| 388 | * calls such as FS.StoreData where there's an extra injection of data |
| 389 | * after the initial fixed part. |
| 390 | */ |
| 391 | tx_total_len = call->request_size; |
| 392 | if (call->send_pages) { |
David Howells | 1199db6 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 393 | if (call->last == call->first) { |
| 394 | tx_total_len += call->last_to - call->first_offset; |
| 395 | } else { |
| 396 | /* It looks mathematically like you should be able to |
| 397 | * combine the following lines with the ones above, but |
| 398 | * unsigned arithmetic is fun when it wraps... |
| 399 | */ |
| 400 | tx_total_len += PAGE_SIZE - call->first_offset; |
| 401 | tx_total_len += call->last_to; |
| 402 | tx_total_len += (call->last - call->first - 1) * PAGE_SIZE; |
| 403 | } |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 404 | } |
| 405 | |
David Howells | 34fa476 | 2019-01-10 15:40:50 +0000 | [diff] [blame] | 406 | /* If the call is going to be asynchronous, we need an extra ref for |
| 407 | * the call to hold itself so the caller need not hang on to its ref. |
| 408 | */ |
| 409 | if (call->async) |
| 410 | afs_get_call(call, afs_call_trace_get); |
| 411 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 412 | /* create a call */ |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 413 | rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key, |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 414 | (unsigned long)call, |
| 415 | tx_total_len, gfp, |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 416 | (call->async ? |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 417 | afs_wake_up_async_call : |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 418 | afs_wake_up_call_waiter), |
David Howells | a25e21f | 2018-03-27 23:03:00 +0100 | [diff] [blame] | 419 | call->upgrade, |
| 420 | call->debug_id); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 421 | if (IS_ERR(rxcall)) { |
| 422 | ret = PTR_ERR(rxcall); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 423 | call->error = ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 424 | goto error_kill_call; |
| 425 | } |
| 426 | |
| 427 | call->rxcall = rxcall; |
| 428 | |
David Howells | 94f699c | 2019-05-16 13:21:59 +0100 | [diff] [blame^] | 429 | if (call->max_lifespan) |
| 430 | rxrpc_kernel_set_max_life(call->net->socket, rxcall, |
| 431 | call->max_lifespan); |
| 432 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 433 | /* send the request */ |
| 434 | iov[0].iov_base = call->request; |
| 435 | iov[0].iov_len = call->request_size; |
| 436 | |
| 437 | msg.msg_name = NULL; |
| 438 | msg.msg_namelen = 0; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 439 | iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 440 | msg.msg_control = NULL; |
| 441 | msg.msg_controllen = 0; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 442 | msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 443 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 444 | ret = rxrpc_kernel_send_data(call->net->socket, rxcall, |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 445 | &msg, call->request_size, |
| 446 | afs_notify_end_request_tx); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 447 | if (ret < 0) |
| 448 | goto error_do_abort; |
| 449 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 450 | if (call->send_pages) { |
Al Viro | 39c6ace | 2016-01-09 20:36:51 -0500 | [diff] [blame] | 451 | ret = afs_send_pages(call, &msg); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 452 | if (ret < 0) |
| 453 | goto error_do_abort; |
| 454 | } |
| 455 | |
David Howells | 34fa476 | 2019-01-10 15:40:50 +0000 | [diff] [blame] | 456 | /* Note that at this point, we may have received the reply or an abort |
| 457 | * - and an asynchronous call may already have completed. |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 458 | * |
| 459 | * afs_wait_for_call_to_complete(call, ac) |
| 460 | * must be called to synchronously clean up. |
David Howells | 34fa476 | 2019-01-10 15:40:50 +0000 | [diff] [blame] | 461 | */ |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 462 | return; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 463 | |
| 464 | error_do_abort: |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 465 | if (ret != -ECONNABORTED) { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 466 | rxrpc_kernel_abort_call(call->net->socket, rxcall, |
| 467 | RX_USER_ABORT, ret, "KSD"); |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 468 | } else { |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 469 | iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0); |
David Howells | eb9950e | 2018-08-03 17:06:56 +0100 | [diff] [blame] | 470 | rxrpc_kernel_recv_data(call->net->socket, rxcall, |
| 471 | &msg.msg_iter, false, |
| 472 | &call->abort_code, &call->service_id); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 473 | ac->abort_code = call->abort_code; |
| 474 | ac->responded = true; |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 475 | } |
David Howells | 025db80 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 476 | call->error = ret; |
| 477 | trace_afs_call_done(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 478 | error_kill_call: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 479 | if (call->type->done) |
| 480 | call->type->done(call); |
David Howells | 34fa476 | 2019-01-10 15:40:50 +0000 | [diff] [blame] | 481 | |
| 482 | /* We need to dispose of the extra ref we grabbed for an async call. |
| 483 | * The call, however, might be queued on afs_async_calls and we need to |
| 484 | * make sure we don't get any more notifications that might requeue it. |
| 485 | */ |
| 486 | if (call->rxcall) { |
| 487 | rxrpc_kernel_end_call(call->net->socket, call->rxcall); |
| 488 | call->rxcall = NULL; |
| 489 | } |
| 490 | if (call->async) { |
| 491 | if (cancel_work_sync(&call->async_work)) |
| 492 | afs_put_call(call); |
| 493 | afs_put_call(call); |
| 494 | } |
| 495 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 496 | ac->error = ret; |
David Howells | 34fa476 | 2019-01-10 15:40:50 +0000 | [diff] [blame] | 497 | call->state = AFS_CALL_COMPLETE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 498 | _leave(" = %d", ret); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 502 | * deliver messages to a call |
| 503 | */ |
| 504 | static void afs_deliver_to_call(struct afs_call *call) |
| 505 | { |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 506 | enum afs_call_state state; |
| 507 | u32 abort_code, remote_abort = 0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 508 | int ret; |
| 509 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 510 | _enter("%s", call->type->name); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 511 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 512 | while (state = READ_ONCE(call->state), |
| 513 | state == AFS_CALL_CL_AWAIT_REPLY || |
| 514 | state == AFS_CALL_SV_AWAIT_OP_ID || |
| 515 | state == AFS_CALL_SV_AWAIT_REQUEST || |
| 516 | state == AFS_CALL_SV_AWAIT_ACK |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 517 | ) { |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 518 | if (state == AFS_CALL_SV_AWAIT_ACK) { |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 519 | iov_iter_kvec(&call->iter, READ, NULL, 0, 0); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 520 | ret = rxrpc_kernel_recv_data(call->net->socket, |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 521 | call->rxcall, &call->iter, |
| 522 | false, &remote_abort, |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 523 | &call->service_id); |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 524 | trace_afs_receive_data(call, &call->iter, false, ret); |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 525 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 526 | if (ret == -EINPROGRESS || ret == -EAGAIN) |
| 527 | return; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 528 | if (ret < 0 || ret == 1) { |
| 529 | if (ret == 1) |
| 530 | ret = 0; |
David Howells | 025db80 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 531 | goto call_complete; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 532 | } |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 533 | return; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 534 | } |
| 535 | |
David Howells | 12d8e95 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 536 | if (call->want_reply_time && |
| 537 | rxrpc_kernel_get_reply_time(call->net->socket, |
| 538 | call->rxcall, |
| 539 | &call->reply_time)) |
| 540 | call->want_reply_time = false; |
| 541 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 542 | ret = call->type->deliver(call); |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 543 | state = READ_ONCE(call->state); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 544 | switch (ret) { |
| 545 | case 0: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 546 | afs_queue_call_work(call); |
David Howells | f2686b0 | 2018-05-10 14:12:50 +0100 | [diff] [blame] | 547 | if (state == AFS_CALL_CL_PROC_REPLY) { |
| 548 | if (call->cbi) |
| 549 | set_bit(AFS_SERVER_FL_MAY_HAVE_CB, |
| 550 | &call->cbi->server->flags); |
David Howells | 025db80 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 551 | goto call_complete; |
David Howells | f2686b0 | 2018-05-10 14:12:50 +0100 | [diff] [blame] | 552 | } |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 553 | ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 554 | goto done; |
| 555 | case -EINPROGRESS: |
| 556 | case -EAGAIN: |
| 557 | goto out; |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 558 | case -ECONNABORTED: |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 559 | ASSERTCMP(state, ==, AFS_CALL_COMPLETE); |
| 560 | goto done; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 561 | case -ENOTSUPP: |
David Howells | 1157f15 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 562 | abort_code = RXGEN_OPCODE; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 563 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 564 | abort_code, ret, "KIV"); |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 565 | goto local_abort; |
David Howells | 4ac15ea | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 566 | case -EIO: |
| 567 | pr_err("kAFS: Call %u in bad state %u\n", |
| 568 | call->debug_id, state); |
| 569 | /* Fall through */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 570 | case -ENODATA: |
| 571 | case -EBADMSG: |
| 572 | case -EMSGSIZE: |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 573 | abort_code = RXGEN_CC_UNMARSHAL; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 574 | if (state != AFS_CALL_CL_AWAIT_REPLY) |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 575 | abort_code = RXGEN_SS_UNMARSHAL; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 576 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 577 | abort_code, ret, "KUM"); |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 578 | goto local_abort; |
David Howells | 8022c4b | 2019-04-13 08:37:37 +0100 | [diff] [blame] | 579 | default: |
| 580 | abort_code = RX_USER_ABORT; |
| 581 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
| 582 | abort_code, ret, "KER"); |
| 583 | goto local_abort; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 584 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 585 | } |
| 586 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 587 | done: |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 588 | if (call->type->done) |
| 589 | call->type->done(call); |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 590 | if (state == AFS_CALL_COMPLETE && call->incoming) |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 591 | afs_put_call(call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 592 | out: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 593 | _leave(""); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 594 | return; |
| 595 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 596 | local_abort: |
| 597 | abort_code = 0; |
David Howells | 025db80 | 2017-11-02 15:27:51 +0000 | [diff] [blame] | 598 | call_complete: |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 599 | afs_set_call_complete(call, ret, remote_abort); |
| 600 | state = AFS_CALL_COMPLETE; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 601 | goto done; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 605 | * Wait synchronously for a call to complete and clean up the call struct. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 606 | */ |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 607 | long afs_wait_for_call_to_complete(struct afs_call *call, |
| 608 | struct afs_addr_cursor *ac) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 609 | { |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 610 | signed long rtt2, timeout; |
David Howells | 33cd7f2 | 2017-11-02 15:27:48 +0000 | [diff] [blame] | 611 | long ret; |
David Howells | 7150cea | 2018-11-12 22:33:22 +0000 | [diff] [blame] | 612 | bool stalled = false; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 613 | u64 rtt; |
| 614 | u32 life, last_life; |
Marc Dionne | f7f1dd3 | 2019-04-12 16:34:02 +0100 | [diff] [blame] | 615 | bool rxrpc_complete = false; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 616 | |
| 617 | DECLARE_WAITQUEUE(myself, current); |
| 618 | |
| 619 | _enter(""); |
| 620 | |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 621 | ret = call->error; |
| 622 | if (ret < 0) |
| 623 | goto out; |
| 624 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 625 | rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall); |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 626 | rtt2 = nsecs_to_jiffies64(rtt) * 2; |
| 627 | if (rtt2 < 2) |
| 628 | rtt2 = 2; |
| 629 | |
| 630 | timeout = rtt2; |
Marc Dionne | 4611da3 | 2019-04-12 16:33:47 +0100 | [diff] [blame] | 631 | rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life); |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 632 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 633 | add_wait_queue(&call->waitq, &myself); |
| 634 | for (;;) { |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 635 | set_current_state(TASK_UNINTERRUPTIBLE); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 636 | |
| 637 | /* deliver any messages that are in the queue */ |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 638 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE) && |
| 639 | call->need_attention) { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 640 | call->need_attention = false; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 641 | __set_current_state(TASK_RUNNING); |
| 642 | afs_deliver_to_call(call); |
| 643 | continue; |
| 644 | } |
| 645 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 646 | if (afs_check_call_state(call, AFS_CALL_COMPLETE)) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 647 | break; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 648 | |
Marc Dionne | f7f1dd3 | 2019-04-12 16:34:02 +0100 | [diff] [blame] | 649 | if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) { |
| 650 | /* rxrpc terminated the call. */ |
| 651 | rxrpc_complete = true; |
| 652 | break; |
| 653 | } |
| 654 | |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 655 | if (timeout == 0 && |
David Howells | 7150cea | 2018-11-12 22:33:22 +0000 | [diff] [blame] | 656 | life == last_life && signal_pending(current)) { |
| 657 | if (stalled) |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 658 | break; |
David Howells | 7150cea | 2018-11-12 22:33:22 +0000 | [diff] [blame] | 659 | __set_current_state(TASK_RUNNING); |
| 660 | rxrpc_kernel_probe_life(call->net->socket, call->rxcall); |
| 661 | timeout = rtt2; |
| 662 | stalled = true; |
| 663 | continue; |
| 664 | } |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 665 | |
| 666 | if (life != last_life) { |
| 667 | timeout = rtt2; |
| 668 | last_life = life; |
David Howells | 7150cea | 2018-11-12 22:33:22 +0000 | [diff] [blame] | 669 | stalled = false; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | timeout = schedule_timeout(timeout); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | remove_wait_queue(&call->waitq, &myself); |
| 676 | __set_current_state(TASK_RUNNING); |
| 677 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 678 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { |
Marc Dionne | f7f1dd3 | 2019-04-12 16:34:02 +0100 | [diff] [blame] | 679 | if (rxrpc_complete) { |
| 680 | afs_set_call_complete(call, call->error, call->abort_code); |
| 681 | } else { |
| 682 | /* Kill off the call if it's still live. */ |
| 683 | _debug("call interrupted"); |
| 684 | if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
| 685 | RX_USER_ABORT, -EINTR, "KWI")) |
| 686 | afs_set_call_complete(call, -EINTR, 0); |
| 687 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 688 | } |
| 689 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 690 | spin_lock_bh(&call->state_lock); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 691 | ac->abort_code = call->abort_code; |
| 692 | ac->error = call->error; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 693 | spin_unlock_bh(&call->state_lock); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 694 | |
| 695 | ret = ac->error; |
| 696 | switch (ret) { |
| 697 | case 0: |
| 698 | if (call->ret_reply0) { |
| 699 | ret = (long)call->reply[0]; |
| 700 | call->reply[0] = NULL; |
| 701 | } |
| 702 | /* Fall through */ |
| 703 | case -ECONNABORTED: |
| 704 | ac->responded = true; |
| 705 | break; |
David Howells | 33cd7f2 | 2017-11-02 15:27:48 +0000 | [diff] [blame] | 706 | } |
| 707 | |
David Howells | 0b9bf38 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 708 | out: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 709 | _debug("call complete"); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 710 | afs_put_call(call); |
David Howells | 33cd7f2 | 2017-11-02 15:27:48 +0000 | [diff] [blame] | 711 | _leave(" = %p", (void *)ret); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * wake up a waiting call |
| 717 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 718 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
| 719 | unsigned long call_user_ID) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 720 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 721 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 722 | |
| 723 | call->need_attention = true; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 724 | wake_up(&call->waitq); |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * wake up an asynchronous call |
| 729 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 730 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
| 731 | unsigned long call_user_ID) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 732 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 733 | struct afs_call *call = (struct afs_call *)call_user_ID; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 734 | int u; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 735 | |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 736 | trace_afs_notify_call(rxcall, call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 737 | call->need_attention = true; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 738 | |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 739 | u = atomic_fetch_add_unless(&call->usage, 1, 0); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 740 | if (u != 0) { |
| 741 | trace_afs_call(call, afs_call_trace_wake, u, |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 742 | atomic_read(&call->net->nr_outstanding_calls), |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 743 | __builtin_return_address(0)); |
| 744 | |
| 745 | if (!queue_work(afs_async_calls, &call->async_work)) |
| 746 | afs_put_call(call); |
| 747 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 751 | * Delete an asynchronous call. The work item carries a ref to the call struct |
| 752 | * that we need to release. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 753 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 754 | static void afs_delete_async_call(struct work_struct *work) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 755 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 756 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
| 757 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 758 | _enter(""); |
| 759 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 760 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 761 | |
| 762 | _leave(""); |
| 763 | } |
| 764 | |
| 765 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 766 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
| 767 | * to the call struct that we either need to release or to pass on. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 768 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 769 | static void afs_process_async_call(struct work_struct *work) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 770 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 771 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
| 772 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 773 | _enter(""); |
| 774 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 775 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
| 776 | call->need_attention = false; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 777 | afs_deliver_to_call(call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 778 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 779 | |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 780 | if (call->state == AFS_CALL_COMPLETE) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 781 | /* We have two refs to release - one from the alloc and one |
| 782 | * queued with the work item - and we can't just deallocate the |
| 783 | * call because the work item may be queued again. |
| 784 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 785 | call->async_work.func = afs_delete_async_call; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 786 | if (!queue_work(afs_async_calls, &call->async_work)) |
| 787 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 788 | } |
| 789 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 790 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 791 | _leave(""); |
| 792 | } |
| 793 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 794 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
| 795 | { |
| 796 | struct afs_call *call = (struct afs_call *)user_call_ID; |
| 797 | |
| 798 | call->rxcall = rxcall; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Charge the incoming call preallocation. |
| 803 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 804 | void afs_charge_preallocation(struct work_struct *work) |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 805 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 806 | struct afs_net *net = |
| 807 | container_of(work, struct afs_net, charge_preallocation_work); |
| 808 | struct afs_call *call = net->spare_incoming_call; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 809 | |
| 810 | for (;;) { |
| 811 | if (!call) { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 812 | call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 813 | if (!call) |
| 814 | break; |
| 815 | |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 816 | call->async = true; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 817 | call->state = AFS_CALL_SV_AWAIT_OP_ID; |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 818 | init_waitqueue_head(&call->waitq); |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 819 | afs_extract_to_tmp(call); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 820 | } |
| 821 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 822 | if (rxrpc_kernel_charge_accept(net->socket, |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 823 | afs_wake_up_async_call, |
| 824 | afs_rx_attach, |
| 825 | (unsigned long)call, |
David Howells | a25e21f | 2018-03-27 23:03:00 +0100 | [diff] [blame] | 826 | GFP_KERNEL, |
| 827 | call->debug_id) < 0) |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 828 | break; |
| 829 | call = NULL; |
| 830 | } |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 831 | net->spare_incoming_call = call; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Discard a preallocated call when a socket is shut down. |
| 836 | */ |
| 837 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, |
| 838 | unsigned long user_call_ID) |
| 839 | { |
| 840 | struct afs_call *call = (struct afs_call *)user_call_ID; |
| 841 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 842 | call->rxcall = NULL; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 843 | afs_put_call(call); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 844 | } |
| 845 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 846 | /* |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 847 | * Notification of an incoming call. |
| 848 | */ |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 849 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
| 850 | unsigned long user_call_ID) |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 851 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 852 | struct afs_net *net = afs_sock2net(sk); |
| 853 | |
| 854 | queue_work(afs_wq, &net->charge_preallocation_work); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* |
David Howells | 372ee16 | 2016-08-03 14:11:40 +0100 | [diff] [blame] | 858 | * Grab the operation ID from an incoming cache manager call. The socket |
| 859 | * buffer is discarded on error or if we don't yet have sufficient data. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 860 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 861 | static int afs_deliver_cm_op_id(struct afs_call *call) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 862 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 863 | int ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 864 | |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 865 | _enter("{%zu}", iov_iter_count(call->_iter)); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 866 | |
| 867 | /* the operation ID forms the first four bytes of the request data */ |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 868 | ret = afs_extract_data(call, true); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 869 | if (ret < 0) |
| 870 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 871 | |
David Howells | 50a2c95 | 2016-10-13 08:27:10 +0100 | [diff] [blame] | 872 | call->operation_ID = ntohl(call->tmp); |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 873 | afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 874 | |
| 875 | /* ask the cache manager to route the call (it'll change the call type |
| 876 | * if successful) */ |
| 877 | if (!afs_cm_incoming_call(call)) |
| 878 | return -ENOTSUPP; |
| 879 | |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 880 | trace_afs_cb_call(call); |
| 881 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 882 | /* pass responsibility for the remainer of this message off to the |
| 883 | * cache manager op */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 884 | return call->type->deliver(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 888 | * Advance the AFS call state when an RxRPC service call ends the transmit |
| 889 | * phase. |
| 890 | */ |
| 891 | static void afs_notify_end_reply_tx(struct sock *sock, |
| 892 | struct rxrpc_call *rxcall, |
| 893 | unsigned long call_user_ID) |
| 894 | { |
| 895 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 896 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 897 | afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK); |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 901 | * send an empty reply |
| 902 | */ |
| 903 | void afs_send_empty_reply(struct afs_call *call) |
| 904 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 905 | struct afs_net *net = call->net; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 906 | struct msghdr msg; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 907 | |
| 908 | _enter(""); |
| 909 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 910 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 911 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 912 | msg.msg_name = NULL; |
| 913 | msg.msg_namelen = 0; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 914 | iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 915 | msg.msg_control = NULL; |
| 916 | msg.msg_controllen = 0; |
| 917 | msg.msg_flags = 0; |
| 918 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 919 | switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 920 | afs_notify_end_reply_tx)) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 921 | case 0: |
| 922 | _leave(" [replied]"); |
| 923 | return; |
| 924 | |
| 925 | case -ENOMEM: |
| 926 | _debug("oom"); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 927 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 928 | RX_USER_ABORT, -ENOMEM, "KOO"); |
Gustavo A. R. Silva | e690c9e | 2019-01-10 15:52:25 -0600 | [diff] [blame] | 929 | /* Fall through */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 930 | default: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 931 | _leave(" [error]"); |
| 932 | return; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /* |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 937 | * send a simple reply |
| 938 | */ |
| 939 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) |
| 940 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 941 | struct afs_net *net = call->net; |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 942 | struct msghdr msg; |
Al Viro | 2e90b1c | 2014-11-27 21:50:31 -0500 | [diff] [blame] | 943 | struct kvec iov[1]; |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 944 | int n; |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 945 | |
| 946 | _enter(""); |
| 947 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 948 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 949 | |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 950 | iov[0].iov_base = (void *) buf; |
| 951 | iov[0].iov_len = len; |
| 952 | msg.msg_name = NULL; |
| 953 | msg.msg_namelen = 0; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 954 | iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len); |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 955 | msg.msg_control = NULL; |
| 956 | msg.msg_controllen = 0; |
| 957 | msg.msg_flags = 0; |
| 958 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 959 | n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 960 | afs_notify_end_reply_tx); |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 961 | if (n >= 0) { |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 962 | /* Success */ |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 963 | _leave(" [replied]"); |
| 964 | return; |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 965 | } |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 966 | |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 967 | if (n == -ENOMEM) { |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 968 | _debug("oom"); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 969 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 970 | RX_USER_ABORT, -ENOMEM, "KOO"); |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 971 | } |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 972 | _leave(" [error]"); |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* |
David Howells | 372ee16 | 2016-08-03 14:11:40 +0100 | [diff] [blame] | 976 | * Extract a piece of data from the received data socket buffers. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 977 | */ |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 978 | int afs_extract_data(struct afs_call *call, bool want_more) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 979 | { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 980 | struct afs_net *net = call->net; |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 981 | struct iov_iter *iter = call->_iter; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 982 | enum afs_call_state state; |
Dan Carpenter | 7888da9 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 983 | u32 remote_abort = 0; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 984 | int ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 985 | |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 986 | _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 987 | |
David Howells | 12bdcf3 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 988 | ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter, |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 989 | want_more, &remote_abort, |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 990 | &call->service_id); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 991 | if (ret == 0 || ret == -EAGAIN) |
| 992 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 993 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 994 | state = READ_ONCE(call->state); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 995 | if (ret == 1) { |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 996 | switch (state) { |
| 997 | case AFS_CALL_CL_AWAIT_REPLY: |
| 998 | afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 999 | break; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 1000 | case AFS_CALL_SV_AWAIT_REQUEST: |
| 1001 | afs_set_call_state(call, state, AFS_CALL_SV_REPLYING); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 1002 | break; |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 1003 | case AFS_CALL_COMPLETE: |
| 1004 | kdebug("prem complete %d", call->error); |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1005 | return afs_io_error(call, afs_io_error_extract); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 1006 | default: |
| 1007 | break; |
| 1008 | } |
| 1009 | return 0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1010 | } |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 1011 | |
David Howells | 98bf40c | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 1012 | afs_set_call_complete(call, ret, remote_abort); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 1013 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1014 | } |
David Howells | 5f702c8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1015 | |
| 1016 | /* |
| 1017 | * Log protocol error production. |
| 1018 | */ |
David Howells | 160cb95 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1019 | noinline int afs_protocol_error(struct afs_call *call, int error, |
| 1020 | enum afs_eproto_cause cause) |
David Howells | 5f702c8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1021 | { |
David Howells | 160cb95 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1022 | trace_afs_protocol_error(call, error, cause); |
David Howells | 5f702c8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1023 | return error; |
| 1024 | } |