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