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