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" |
| 19 | |
David Howells | 8324f0b | 2016-08-30 09:49:29 +0100 | [diff] [blame] | 20 | struct socket *afs_socket; /* my RxRPC socket */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 21 | static struct workqueue_struct *afs_async_calls; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 22 | static struct afs_call *afs_spare_incoming_call; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 23 | atomic_t afs_outstanding_calls; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 24 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 25 | static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 26 | static int afs_wait_for_call_to_complete(struct afs_call *); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 27 | static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 28 | static void afs_process_async_call(struct work_struct *); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 29 | static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long); |
| 30 | static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 31 | static int afs_deliver_cm_op_id(struct afs_call *); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 32 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 33 | /* asynchronous incoming call initial processing */ |
| 34 | static const struct afs_call_type afs_RXCMxxxx = { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 35 | .name = "CB.xxxx", |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 36 | .deliver = afs_deliver_cm_op_id, |
| 37 | .abort_to_error = afs_abort_to_error, |
| 38 | }; |
| 39 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 40 | static void afs_charge_preallocation(struct work_struct *); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 41 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 42 | static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 43 | |
David Howells | 2f02f7a | 2016-04-07 17:23:03 +0100 | [diff] [blame] | 44 | static int afs_wait_atomic_t(atomic_t *p) |
| 45 | { |
| 46 | schedule(); |
| 47 | return 0; |
| 48 | } |
| 49 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 50 | /* |
| 51 | * open an RxRPC socket and bind it to be a server for callback notifications |
| 52 | * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT |
| 53 | */ |
| 54 | int afs_open_socket(void) |
| 55 | { |
| 56 | struct sockaddr_rxrpc srx; |
| 57 | struct socket *socket; |
| 58 | int ret; |
| 59 | |
| 60 | _enter(""); |
| 61 | |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 62 | ret = -ENOMEM; |
Bhaktipriya Shridhar | 69ad052 | 2016-09-04 20:53:42 +0530 | [diff] [blame] | 63 | afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0); |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 64 | if (!afs_async_calls) |
| 65 | goto error_0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 66 | |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 67 | ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket); |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 68 | if (ret < 0) |
| 69 | goto error_1; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 70 | |
| 71 | socket->sk->sk_allocation = GFP_NOFS; |
| 72 | |
| 73 | /* bind the callback manager's address to make this a server socket */ |
| 74 | srx.srx_family = AF_RXRPC; |
| 75 | srx.srx_service = CM_SERVICE; |
| 76 | srx.transport_type = SOCK_DGRAM; |
| 77 | srx.transport_len = sizeof(srx.transport.sin); |
| 78 | srx.transport.sin.sin_family = AF_INET; |
| 79 | srx.transport.sin.sin_port = htons(AFS_CM_PORT); |
| 80 | memset(&srx.transport.sin.sin_addr, 0, |
| 81 | sizeof(srx.transport.sin.sin_addr)); |
| 82 | |
| 83 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 84 | if (ret < 0) |
| 85 | goto error_2; |
| 86 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 87 | rxrpc_kernel_new_call_notification(socket, afs_rx_new_call, |
| 88 | afs_rx_discard_new_call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 89 | |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 90 | ret = kernel_listen(socket, INT_MAX); |
| 91 | if (ret < 0) |
| 92 | goto error_2; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 93 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 94 | afs_socket = socket; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 95 | afs_charge_preallocation(NULL); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 96 | _leave(" = 0"); |
| 97 | return 0; |
David Howells | 0e119b4 | 2016-06-10 22:30:37 +0100 | [diff] [blame] | 98 | |
| 99 | error_2: |
| 100 | sock_release(socket); |
| 101 | error_1: |
| 102 | destroy_workqueue(afs_async_calls); |
| 103 | error_0: |
| 104 | _leave(" = %d", ret); |
| 105 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * close the RxRPC socket AFS was using |
| 110 | */ |
| 111 | void afs_close_socket(void) |
| 112 | { |
| 113 | _enter(""); |
| 114 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 115 | kernel_listen(afs_socket, 0); |
| 116 | flush_workqueue(afs_async_calls); |
| 117 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 118 | if (afs_spare_incoming_call) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 119 | afs_put_call(afs_spare_incoming_call); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 120 | afs_spare_incoming_call = NULL; |
| 121 | } |
| 122 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 123 | _debug("outstanding %u", atomic_read(&afs_outstanding_calls)); |
David Howells | 2f02f7a | 2016-04-07 17:23:03 +0100 | [diff] [blame] | 124 | wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t, |
| 125 | TASK_UNINTERRUPTIBLE); |
| 126 | _debug("no outstanding calls"); |
| 127 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 128 | kernel_sock_shutdown(afs_socket, SHUT_RDWR); |
| 129 | flush_workqueue(afs_async_calls); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 130 | sock_release(afs_socket); |
| 131 | |
| 132 | _debug("dework"); |
| 133 | destroy_workqueue(afs_async_calls); |
| 134 | _leave(""); |
| 135 | } |
| 136 | |
| 137 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 138 | * Allocate a call. |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 139 | */ |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 140 | static struct afs_call *afs_alloc_call(const struct afs_call_type *type, |
| 141 | gfp_t gfp) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 142 | { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 143 | struct afs_call *call; |
| 144 | int o; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 145 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 146 | call = kzalloc(sizeof(*call), gfp); |
| 147 | if (!call) |
| 148 | return NULL; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 149 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 150 | call->type = type; |
| 151 | atomic_set(&call->usage, 1); |
| 152 | INIT_WORK(&call->async_work, afs_process_async_call); |
| 153 | init_waitqueue_head(&call->waitq); |
David Howells | 2f02f7a | 2016-04-07 17:23:03 +0100 | [diff] [blame] | 154 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 155 | o = atomic_inc_return(&afs_outstanding_calls); |
| 156 | trace_afs_call(call, afs_call_trace_alloc, 1, o, |
| 157 | __builtin_return_address(0)); |
| 158 | return call; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 162 | * Dispose of a reference on a call. |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 163 | */ |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 164 | void afs_put_call(struct afs_call *call) |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 165 | { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 166 | int n = atomic_dec_return(&call->usage); |
| 167 | int o = atomic_read(&afs_outstanding_calls); |
| 168 | |
| 169 | trace_afs_call(call, afs_call_trace_put, n + 1, o, |
| 170 | __builtin_return_address(0)); |
| 171 | |
| 172 | ASSERTCMP(n, >=, 0); |
| 173 | if (n == 0) { |
| 174 | ASSERT(!work_pending(&call->async_work)); |
| 175 | ASSERT(call->type->name != NULL); |
| 176 | |
| 177 | if (call->rxcall) { |
| 178 | rxrpc_kernel_end_call(afs_socket, call->rxcall); |
| 179 | call->rxcall = NULL; |
| 180 | } |
| 181 | if (call->type->destructor) |
| 182 | call->type->destructor(call); |
| 183 | |
| 184 | kfree(call->request); |
| 185 | kfree(call); |
| 186 | |
| 187 | o = atomic_dec_return(&afs_outstanding_calls); |
| 188 | trace_afs_call(call, afs_call_trace_free, 0, o, |
| 189 | __builtin_return_address(0)); |
| 190 | if (o == 0) |
| 191 | wake_up_atomic_t(&afs_outstanding_calls); |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 192 | } |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 196 | * Queue the call for actual work. Returns 0 unconditionally for convenience. |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 197 | */ |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 198 | int afs_queue_call_work(struct afs_call *call) |
Nathaniel Wesley Filardo | 6cf1286 | 2014-05-21 16:04:11 +0100 | [diff] [blame] | 199 | { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 200 | int u = atomic_inc_return(&call->usage); |
| 201 | |
| 202 | trace_afs_call(call, afs_call_trace_work, u, |
| 203 | atomic_read(&afs_outstanding_calls), |
| 204 | __builtin_return_address(0)); |
| 205 | |
| 206 | INIT_WORK(&call->work, call->type->work); |
| 207 | |
| 208 | if (!queue_work(afs_wq, &call->work)) |
| 209 | afs_put_call(call); |
| 210 | return 0; |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 214 | * allocate a call with flat request and reply buffers |
| 215 | */ |
| 216 | struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type, |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 217 | size_t request_size, size_t reply_max) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 218 | { |
| 219 | struct afs_call *call; |
| 220 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 221 | call = afs_alloc_call(type, GFP_NOFS); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 222 | if (!call) |
| 223 | goto nomem_call; |
| 224 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 225 | if (request_size) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 226 | call->request_size = request_size; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 227 | call->request = kmalloc(request_size, GFP_NOFS); |
| 228 | if (!call->request) |
| 229 | goto nomem_free; |
| 230 | } |
| 231 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 232 | if (reply_max) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 233 | call->reply_max = reply_max; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 234 | call->buffer = kmalloc(reply_max, GFP_NOFS); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 235 | if (!call->buffer) |
| 236 | goto nomem_free; |
| 237 | } |
| 238 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 239 | init_waitqueue_head(&call->waitq); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 240 | return call; |
| 241 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 242 | nomem_free: |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 243 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 244 | nomem_call: |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * clean up a call with flat buffer |
| 250 | */ |
| 251 | void afs_flat_call_destructor(struct afs_call *call) |
| 252 | { |
| 253 | _enter(""); |
| 254 | |
| 255 | kfree(call->request); |
| 256 | call->request = NULL; |
| 257 | kfree(call->buffer); |
| 258 | call->buffer = NULL; |
| 259 | } |
| 260 | |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 261 | #define AFS_BVEC_MAX 8 |
| 262 | |
| 263 | /* |
| 264 | * Load the given bvec with the next few pages. |
| 265 | */ |
| 266 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, |
| 267 | struct bio_vec *bv, pgoff_t first, pgoff_t last, |
| 268 | unsigned offset) |
| 269 | { |
| 270 | struct page *pages[AFS_BVEC_MAX]; |
| 271 | unsigned int nr, n, i, to, bytes = 0; |
| 272 | |
| 273 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); |
| 274 | n = find_get_pages_contig(call->mapping, first, nr, pages); |
| 275 | ASSERTCMP(n, ==, nr); |
| 276 | |
| 277 | msg->msg_flags |= MSG_MORE; |
| 278 | for (i = 0; i < nr; i++) { |
| 279 | to = PAGE_SIZE; |
| 280 | if (first + i >= last) { |
| 281 | to = call->last_to; |
| 282 | msg->msg_flags &= ~MSG_MORE; |
| 283 | } |
| 284 | bv[i].bv_page = pages[i]; |
| 285 | bv[i].bv_len = to - offset; |
| 286 | bv[i].bv_offset = offset; |
| 287 | bytes += to - offset; |
| 288 | offset = 0; |
| 289 | } |
| 290 | |
| 291 | iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes); |
| 292 | } |
| 293 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 294 | /* |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 295 | * Advance the AFS call state when the RxRPC call ends the transmit phase. |
| 296 | */ |
| 297 | static void afs_notify_end_request_tx(struct sock *sock, |
| 298 | struct rxrpc_call *rxcall, |
| 299 | unsigned long call_user_ID) |
| 300 | { |
| 301 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 302 | |
| 303 | if (call->state == AFS_CALL_REQUESTING) |
| 304 | call->state = AFS_CALL_AWAIT_REPLY; |
| 305 | } |
| 306 | |
| 307 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 308 | * attach the data from a bunch of pages on an inode to a call |
| 309 | */ |
Al Viro | 39c6ace | 2016-01-09 20:36:51 -0500 | [diff] [blame] | 310 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 311 | { |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 312 | struct bio_vec bv[AFS_BVEC_MAX]; |
| 313 | unsigned int bytes, nr, loop, offset; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 314 | pgoff_t first = call->first, last = call->last; |
| 315 | int ret; |
| 316 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 317 | offset = call->first_offset; |
| 318 | call->first_offset = 0; |
| 319 | |
| 320 | do { |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 321 | afs_load_bvec(call, msg, bv, first, last, offset); |
| 322 | offset = 0; |
| 323 | bytes = msg->msg_iter.count; |
| 324 | nr = msg->msg_iter.nr_segs; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 325 | |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 326 | ret = rxrpc_kernel_send_data(afs_socket, call->rxcall, msg, |
| 327 | bytes, afs_notify_end_request_tx); |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 328 | for (loop = 0; loop < nr; loop++) |
| 329 | put_page(bv[loop].bv_page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 330 | if (ret < 0) |
| 331 | break; |
David Howells | 2f5705a | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 332 | |
| 333 | first += nr; |
David Howells | 5bbf5d3 | 2007-05-10 03:15:23 -0700 | [diff] [blame] | 334 | } while (first <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 335 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 340 | * initiate a call |
| 341 | */ |
| 342 | int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp, |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 343 | bool async) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 344 | { |
| 345 | struct sockaddr_rxrpc srx; |
| 346 | struct rxrpc_call *rxcall; |
| 347 | struct msghdr msg; |
| 348 | struct kvec iov[1]; |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 349 | size_t offset; |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 350 | s64 tx_total_len; |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 351 | u32 abort_code; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 352 | int ret; |
| 353 | |
| 354 | _enter("%x,{%d},", addr->s_addr, ntohs(call->port)); |
| 355 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 356 | ASSERT(call->type != NULL); |
| 357 | ASSERT(call->type->name != NULL); |
| 358 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 359 | _debug("____MAKE %p{%s,%x} [%d]____", |
| 360 | call, call->type->name, key_serial(call->key), |
| 361 | atomic_read(&afs_outstanding_calls)); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 362 | |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 363 | call->async = async; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 364 | |
| 365 | memset(&srx, 0, sizeof(srx)); |
| 366 | srx.srx_family = AF_RXRPC; |
| 367 | srx.srx_service = call->service_id; |
| 368 | srx.transport_type = SOCK_DGRAM; |
| 369 | srx.transport_len = sizeof(srx.transport.sin); |
| 370 | srx.transport.sin.sin_family = AF_INET; |
| 371 | srx.transport.sin.sin_port = call->port; |
| 372 | memcpy(&srx.transport.sin.sin_addr, addr, 4); |
| 373 | |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 374 | /* Work out the length we're going to transmit. This is awkward for |
| 375 | * calls such as FS.StoreData where there's an extra injection of data |
| 376 | * after the initial fixed part. |
| 377 | */ |
| 378 | tx_total_len = call->request_size; |
| 379 | if (call->send_pages) { |
| 380 | tx_total_len += call->last_to - call->first_offset; |
| 381 | tx_total_len += (call->last - call->first) * PAGE_SIZE; |
| 382 | } |
| 383 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 384 | /* create a call */ |
| 385 | rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key, |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 386 | (unsigned long)call, |
| 387 | tx_total_len, gfp, |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 388 | (async ? |
| 389 | afs_wake_up_async_call : |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 390 | afs_wake_up_call_waiter), |
| 391 | call->upgrade); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 392 | call->key = NULL; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 393 | if (IS_ERR(rxcall)) { |
| 394 | ret = PTR_ERR(rxcall); |
| 395 | goto error_kill_call; |
| 396 | } |
| 397 | |
| 398 | call->rxcall = rxcall; |
| 399 | |
| 400 | /* send the request */ |
| 401 | iov[0].iov_base = call->request; |
| 402 | iov[0].iov_len = call->request_size; |
| 403 | |
| 404 | msg.msg_name = NULL; |
| 405 | msg.msg_namelen = 0; |
Al Viro | 2e90b1c | 2014-11-27 21:50:31 -0500 | [diff] [blame] | 406 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 407 | call->request_size); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 408 | msg.msg_control = NULL; |
| 409 | msg.msg_controllen = 0; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 410 | msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 411 | |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 412 | /* We have to change the state *before* sending the last packet as |
| 413 | * rxrpc might give us the reply before it returns from sending the |
| 414 | * request. Further, if the send fails, we may already have been given |
| 415 | * a notification and may have collected it. |
| 416 | */ |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 417 | if (!call->send_pages) |
| 418 | call->state = AFS_CALL_AWAIT_REPLY; |
David Howells | 4de48af | 2016-08-30 12:00:48 +0100 | [diff] [blame] | 419 | ret = rxrpc_kernel_send_data(afs_socket, rxcall, |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 420 | &msg, call->request_size, |
| 421 | afs_notify_end_request_tx); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 422 | if (ret < 0) |
| 423 | goto error_do_abort; |
| 424 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 425 | if (call->send_pages) { |
Al Viro | 39c6ace | 2016-01-09 20:36:51 -0500 | [diff] [blame] | 426 | ret = afs_send_pages(call, &msg); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 427 | if (ret < 0) |
| 428 | goto error_do_abort; |
| 429 | } |
| 430 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 431 | /* at this point, an async call may no longer exist as it may have |
| 432 | * already completed */ |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 433 | if (call->async) |
| 434 | return -EINPROGRESS; |
| 435 | |
| 436 | return afs_wait_for_call_to_complete(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 437 | |
| 438 | error_do_abort: |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 439 | call->state = AFS_CALL_COMPLETE; |
| 440 | if (ret != -ECONNABORTED) { |
| 441 | rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 442 | ret, "KSD"); |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 443 | } else { |
| 444 | abort_code = 0; |
| 445 | offset = 0; |
| 446 | rxrpc_kernel_recv_data(afs_socket, rxcall, NULL, 0, &offset, |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 447 | false, &abort_code, &call->service_id); |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 448 | ret = call->type->abort_to_error(abort_code); |
| 449 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 450 | error_kill_call: |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 451 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 452 | _leave(" = %d", ret); |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 457 | * deliver messages to a call |
| 458 | */ |
| 459 | static void afs_deliver_to_call(struct afs_call *call) |
| 460 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 461 | u32 abort_code; |
| 462 | int ret; |
| 463 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 464 | _enter("%s", call->type->name); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 465 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 466 | while (call->state == AFS_CALL_AWAIT_REPLY || |
| 467 | call->state == AFS_CALL_AWAIT_OP_ID || |
| 468 | call->state == AFS_CALL_AWAIT_REQUEST || |
| 469 | call->state == AFS_CALL_AWAIT_ACK |
| 470 | ) { |
| 471 | if (call->state == AFS_CALL_AWAIT_ACK) { |
| 472 | size_t offset = 0; |
| 473 | ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall, |
| 474 | NULL, 0, &offset, false, |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 475 | &call->abort_code, |
| 476 | &call->service_id); |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 477 | trace_afs_recv_data(call, 0, offset, false, ret); |
| 478 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 479 | if (ret == -EINPROGRESS || ret == -EAGAIN) |
| 480 | return; |
David Howells | 9008f99 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 481 | if (ret == 1 || ret < 0) { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 482 | call->state = AFS_CALL_COMPLETE; |
| 483 | goto done; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 484 | } |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 485 | return; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 486 | } |
| 487 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 488 | ret = call->type->deliver(call); |
| 489 | switch (ret) { |
| 490 | case 0: |
| 491 | if (call->state == AFS_CALL_AWAIT_REPLY) |
| 492 | call->state = AFS_CALL_COMPLETE; |
| 493 | goto done; |
| 494 | case -EINPROGRESS: |
| 495 | case -EAGAIN: |
| 496 | goto out; |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 497 | case -ECONNABORTED: |
| 498 | goto call_complete; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 499 | case -ENOTCONN: |
| 500 | abort_code = RX_CALL_DEAD; |
| 501 | rxrpc_kernel_abort_call(afs_socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 502 | abort_code, ret, "KNC"); |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 503 | goto save_error; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 504 | case -ENOTSUPP: |
David Howells | 1157f15 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 505 | abort_code = RXGEN_OPCODE; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 506 | rxrpc_kernel_abort_call(afs_socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 507 | abort_code, ret, "KIV"); |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 508 | goto save_error; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 509 | case -ENODATA: |
| 510 | case -EBADMSG: |
| 511 | case -EMSGSIZE: |
| 512 | default: |
| 513 | abort_code = RXGEN_CC_UNMARSHAL; |
| 514 | if (call->state != AFS_CALL_AWAIT_REPLY) |
| 515 | abort_code = RXGEN_SS_UNMARSHAL; |
| 516 | rxrpc_kernel_abort_call(afs_socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 517 | abort_code, -EBADMSG, "KUM"); |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 518 | goto save_error; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 519 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 520 | } |
| 521 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 522 | done: |
| 523 | if (call->state == AFS_CALL_COMPLETE && call->incoming) |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 524 | afs_put_call(call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 525 | out: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 526 | _leave(""); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 527 | return; |
| 528 | |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 529 | save_error: |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 530 | call->error = ret; |
David Howells | 70af0e3 | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 531 | call_complete: |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 532 | call->state = AFS_CALL_COMPLETE; |
| 533 | goto done; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* |
| 537 | * wait synchronously for a call to complete |
| 538 | */ |
| 539 | static int afs_wait_for_call_to_complete(struct afs_call *call) |
| 540 | { |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 541 | signed long rtt2, timeout; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 542 | int ret; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 543 | u64 rtt; |
| 544 | u32 life, last_life; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 545 | |
| 546 | DECLARE_WAITQUEUE(myself, current); |
| 547 | |
| 548 | _enter(""); |
| 549 | |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 550 | rtt = rxrpc_kernel_get_rtt(afs_socket, call->rxcall); |
| 551 | rtt2 = nsecs_to_jiffies64(rtt) * 2; |
| 552 | if (rtt2 < 2) |
| 553 | rtt2 = 2; |
| 554 | |
| 555 | timeout = rtt2; |
| 556 | last_life = rxrpc_kernel_check_life(afs_socket, call->rxcall); |
| 557 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 558 | add_wait_queue(&call->waitq, &myself); |
| 559 | for (;;) { |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 560 | set_current_state(TASK_UNINTERRUPTIBLE); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 561 | |
| 562 | /* deliver any messages that are in the queue */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 563 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
| 564 | call->need_attention = false; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 565 | __set_current_state(TASK_RUNNING); |
| 566 | afs_deliver_to_call(call); |
| 567 | continue; |
| 568 | } |
| 569 | |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 570 | if (call->state == AFS_CALL_COMPLETE) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 571 | break; |
David Howells | bc5e3a5 | 2017-10-18 11:07:31 +0100 | [diff] [blame] | 572 | |
| 573 | life = rxrpc_kernel_check_life(afs_socket, call->rxcall); |
| 574 | if (timeout == 0 && |
| 575 | life == last_life && signal_pending(current)) |
| 576 | break; |
| 577 | |
| 578 | if (life != last_life) { |
| 579 | timeout = rtt2; |
| 580 | last_life = life; |
| 581 | } |
| 582 | |
| 583 | timeout = schedule_timeout(timeout); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | remove_wait_queue(&call->waitq, &myself); |
| 587 | __set_current_state(TASK_RUNNING); |
| 588 | |
David Howells | 954cd6d | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 589 | /* Kill off the call if it's still live. */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 590 | if (call->state < AFS_CALL_COMPLETE) { |
David Howells | 954cd6d | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 591 | _debug("call interrupted"); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 592 | rxrpc_kernel_abort_call(afs_socket, call->rxcall, |
David Howells | 954cd6d | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 593 | RX_USER_ABORT, -EINTR, "KWI"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 594 | } |
| 595 | |
David Howells | 954cd6d | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 596 | ret = call->error; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 597 | _debug("call complete"); |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 598 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 599 | _leave(" = %d", ret); |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * wake up a waiting call |
| 605 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 606 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
| 607 | unsigned long call_user_ID) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 608 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 609 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 610 | |
| 611 | call->need_attention = true; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 612 | wake_up(&call->waitq); |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * wake up an asynchronous call |
| 617 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 618 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
| 619 | unsigned long call_user_ID) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 620 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 621 | struct afs_call *call = (struct afs_call *)call_user_ID; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 622 | int u; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 623 | |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 624 | trace_afs_notify_call(rxcall, call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 625 | call->need_attention = true; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 626 | |
| 627 | u = __atomic_add_unless(&call->usage, 1, 0); |
| 628 | if (u != 0) { |
| 629 | trace_afs_call(call, afs_call_trace_wake, u, |
| 630 | atomic_read(&afs_outstanding_calls), |
| 631 | __builtin_return_address(0)); |
| 632 | |
| 633 | if (!queue_work(afs_async_calls, &call->async_work)) |
| 634 | afs_put_call(call); |
| 635 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 639 | * Delete an asynchronous call. The work item carries a ref to the call struct |
| 640 | * that we need to release. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 641 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 642 | static void afs_delete_async_call(struct work_struct *work) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 643 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 644 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
| 645 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 646 | _enter(""); |
| 647 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 648 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 649 | |
| 650 | _leave(""); |
| 651 | } |
| 652 | |
| 653 | /* |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 654 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
| 655 | * 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] | 656 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 657 | static void afs_process_async_call(struct work_struct *work) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 658 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 659 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
| 660 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 661 | _enter(""); |
| 662 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 663 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
| 664 | call->need_attention = false; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 665 | afs_deliver_to_call(call); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 666 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 667 | |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 668 | if (call->state == AFS_CALL_COMPLETE) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 669 | call->reply = NULL; |
| 670 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 671 | /* We have two refs to release - one from the alloc and one |
| 672 | * queued with the work item - and we can't just deallocate the |
| 673 | * call because the work item may be queued again. |
| 674 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 675 | call->async_work.func = afs_delete_async_call; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 676 | if (!queue_work(afs_async_calls, &call->async_work)) |
| 677 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 678 | } |
| 679 | |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 680 | afs_put_call(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 681 | _leave(""); |
| 682 | } |
| 683 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 684 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
| 685 | { |
| 686 | struct afs_call *call = (struct afs_call *)user_call_ID; |
| 687 | |
| 688 | call->rxcall = rxcall; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Charge the incoming call preallocation. |
| 693 | */ |
| 694 | static void afs_charge_preallocation(struct work_struct *work) |
| 695 | { |
| 696 | struct afs_call *call = afs_spare_incoming_call; |
| 697 | |
| 698 | for (;;) { |
| 699 | if (!call) { |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 700 | call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 701 | if (!call) |
| 702 | break; |
| 703 | |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 704 | call->async = true; |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 705 | call->state = AFS_CALL_AWAIT_OP_ID; |
David Howells | 56ff9c8 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 706 | init_waitqueue_head(&call->waitq); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (rxrpc_kernel_charge_accept(afs_socket, |
| 710 | afs_wake_up_async_call, |
| 711 | afs_rx_attach, |
| 712 | (unsigned long)call, |
| 713 | GFP_KERNEL) < 0) |
| 714 | break; |
| 715 | call = NULL; |
| 716 | } |
| 717 | afs_spare_incoming_call = call; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Discard a preallocated call when a socket is shut down. |
| 722 | */ |
| 723 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, |
| 724 | unsigned long user_call_ID) |
| 725 | { |
| 726 | struct afs_call *call = (struct afs_call *)user_call_ID; |
| 727 | |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 728 | call->rxcall = NULL; |
David Howells | 341f741 | 2017-01-05 10:38:36 +0000 | [diff] [blame] | 729 | afs_put_call(call); |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 730 | } |
| 731 | |
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 | * Notification of an incoming call. |
| 734 | */ |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 735 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
| 736 | unsigned long user_call_ID) |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 737 | { |
David Howells | 00e9071 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 738 | queue_work(afs_wq, &afs_charge_preallocation_work); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* |
David Howells | 372ee16 | 2016-08-03 14:11:40 +0100 | [diff] [blame] | 742 | * Grab the operation ID from an incoming cache manager call. The socket |
| 743 | * 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] | 744 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 745 | static int afs_deliver_cm_op_id(struct afs_call *call) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 746 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 747 | int ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 748 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 749 | _enter("{%zu}", call->offset); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 750 | |
| 751 | ASSERTCMP(call->offset, <, 4); |
| 752 | |
| 753 | /* the operation ID forms the first four bytes of the request data */ |
David Howells | 50a2c95 | 2016-10-13 08:27:10 +0100 | [diff] [blame] | 754 | ret = afs_extract_data(call, &call->tmp, 4, true); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 755 | if (ret < 0) |
| 756 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 757 | |
David Howells | 50a2c95 | 2016-10-13 08:27:10 +0100 | [diff] [blame] | 758 | call->operation_ID = ntohl(call->tmp); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 759 | call->state = AFS_CALL_AWAIT_REQUEST; |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 760 | call->offset = 0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 761 | |
| 762 | /* ask the cache manager to route the call (it'll change the call type |
| 763 | * if successful) */ |
| 764 | if (!afs_cm_incoming_call(call)) |
| 765 | return -ENOTSUPP; |
| 766 | |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 767 | trace_afs_cb_call(call); |
| 768 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 769 | /* pass responsibility for the remainer of this message off to the |
| 770 | * cache manager op */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 771 | return call->type->deliver(call); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /* |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 775 | * Advance the AFS call state when an RxRPC service call ends the transmit |
| 776 | * phase. |
| 777 | */ |
| 778 | static void afs_notify_end_reply_tx(struct sock *sock, |
| 779 | struct rxrpc_call *rxcall, |
| 780 | unsigned long call_user_ID) |
| 781 | { |
| 782 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 783 | |
| 784 | if (call->state == AFS_CALL_REPLYING) |
| 785 | call->state = AFS_CALL_AWAIT_ACK; |
| 786 | } |
| 787 | |
| 788 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 789 | * send an empty reply |
| 790 | */ |
| 791 | void afs_send_empty_reply(struct afs_call *call) |
| 792 | { |
| 793 | struct msghdr msg; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 794 | |
| 795 | _enter(""); |
| 796 | |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 797 | rxrpc_kernel_set_tx_length(afs_socket, call->rxcall, 0); |
| 798 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 799 | msg.msg_name = NULL; |
| 800 | msg.msg_namelen = 0; |
David Howells | bfd4e95 | 2015-04-01 16:03:46 +0100 | [diff] [blame] | 801 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 802 | msg.msg_control = NULL; |
| 803 | msg.msg_controllen = 0; |
| 804 | msg.msg_flags = 0; |
| 805 | |
| 806 | call->state = AFS_CALL_AWAIT_ACK; |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 807 | switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0, |
| 808 | afs_notify_end_reply_tx)) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 809 | case 0: |
| 810 | _leave(" [replied]"); |
| 811 | return; |
| 812 | |
| 813 | case -ENOMEM: |
| 814 | _debug("oom"); |
David Howells | 4de48af | 2016-08-30 12:00:48 +0100 | [diff] [blame] | 815 | rxrpc_kernel_abort_call(afs_socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 816 | RX_USER_ABORT, -ENOMEM, "KOO"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 817 | default: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 818 | _leave(" [error]"); |
| 819 | return; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /* |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 824 | * send a simple reply |
| 825 | */ |
| 826 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) |
| 827 | { |
| 828 | struct msghdr msg; |
Al Viro | 2e90b1c | 2014-11-27 21:50:31 -0500 | [diff] [blame] | 829 | struct kvec iov[1]; |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 830 | int n; |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 831 | |
| 832 | _enter(""); |
| 833 | |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 834 | rxrpc_kernel_set_tx_length(afs_socket, call->rxcall, len); |
| 835 | |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 836 | iov[0].iov_base = (void *) buf; |
| 837 | iov[0].iov_len = len; |
| 838 | msg.msg_name = NULL; |
| 839 | msg.msg_namelen = 0; |
Al Viro | 2e90b1c | 2014-11-27 21:50:31 -0500 | [diff] [blame] | 840 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len); |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 841 | msg.msg_control = NULL; |
| 842 | msg.msg_controllen = 0; |
| 843 | msg.msg_flags = 0; |
| 844 | |
| 845 | call->state = AFS_CALL_AWAIT_ACK; |
David Howells | e833251 | 2017-08-29 10:18:56 +0100 | [diff] [blame] | 846 | n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len, |
| 847 | afs_notify_end_reply_tx); |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 848 | if (n >= 0) { |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 849 | /* Success */ |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 850 | _leave(" [replied]"); |
| 851 | return; |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 852 | } |
David Howells | 6c67c7c | 2014-05-21 14:48:05 +0100 | [diff] [blame] | 853 | |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 854 | if (n == -ENOMEM) { |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 855 | _debug("oom"); |
David Howells | 4de48af | 2016-08-30 12:00:48 +0100 | [diff] [blame] | 856 | rxrpc_kernel_abort_call(afs_socket, call->rxcall, |
David Howells | 3a92789 | 2017-04-06 10:11:56 +0100 | [diff] [blame] | 857 | RX_USER_ABORT, -ENOMEM, "KOO"); |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 858 | } |
David Howells | bd6dc74 | 2007-07-20 10:59:41 +0100 | [diff] [blame] | 859 | _leave(" [error]"); |
David Howells | b908fe6 | 2007-04-26 15:58:17 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* |
David Howells | 372ee16 | 2016-08-03 14:11:40 +0100 | [diff] [blame] | 863 | * Extract a piece of data from the received data socket buffers. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 864 | */ |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 865 | int afs_extract_data(struct afs_call *call, void *buf, size_t count, |
| 866 | bool want_more) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 867 | { |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 868 | int ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 869 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 870 | _enter("{%s,%zu},,%zu,%d", |
| 871 | call->type->name, call->offset, count, want_more); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 872 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 873 | ASSERTCMP(call->offset, <=, count); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 874 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 875 | ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall, |
| 876 | buf, count, &call->offset, |
David Howells | a68f4a2 | 2017-10-18 11:36:39 +0100 | [diff] [blame] | 877 | want_more, &call->abort_code, |
| 878 | &call->service_id); |
David Howells | 8e8d7f1 | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 879 | trace_afs_recv_data(call, count, call->offset, want_more, ret); |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 880 | if (ret == 0 || ret == -EAGAIN) |
| 881 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 882 | |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 883 | if (ret == 1) { |
| 884 | switch (call->state) { |
| 885 | case AFS_CALL_AWAIT_REPLY: |
| 886 | call->state = AFS_CALL_COMPLETE; |
| 887 | break; |
| 888 | case AFS_CALL_AWAIT_REQUEST: |
| 889 | call->state = AFS_CALL_REPLYING; |
| 890 | break; |
| 891 | default: |
| 892 | break; |
| 893 | } |
| 894 | return 0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 895 | } |
David Howells | d001648 | 2016-08-30 20:42:14 +0100 | [diff] [blame] | 896 | |
| 897 | if (ret == -ECONNABORTED) |
| 898 | call->error = call->type->abort_to_error(call->abort_code); |
| 899 | else |
| 900 | call->error = ret; |
| 901 | call->state = AFS_CALL_COMPLETE; |
| 902 | return ret; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 903 | } |