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