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