blob: 183cc5418722602b61b87cfc6ca73c5d6dcbb5d8 [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* 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 Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
14
David Howells08e0e7c2007-04-26 15:55:03 -070015#include <net/sock.h>
16#include <net/af_rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070017#include "internal.h"
18#include "afs_cm.h"
19
David Howellsf044c882017-11-02 15:27:45 +000020struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070021
David Howellsd0016482016-08-30 20:42:14 +010022static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd2ddc772017-11-02 15:27:50 +000023static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
David Howellsd0016482016-08-30 20:42:14 +010024static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010025static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010026static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
27static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010028static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070029
David Howells08e0e7c2007-04-26 15:55:03 -070030/* asynchronous incoming call initial processing */
31static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070032 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070033 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070034};
35
David Howells08e0e7c2007-04-26 15:55:03 -070036/*
37 * open an RxRPC socket and bind it to be a server for callback notifications
38 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
39 */
David Howellsf044c882017-11-02 15:27:45 +000040int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070041{
42 struct sockaddr_rxrpc srx;
43 struct socket *socket;
David Howells4776cab2018-05-10 23:10:40 +010044 unsigned int min_level;
David Howells08e0e7c2007-04-26 15:55:03 -070045 int ret;
46
47 _enter("");
48
David Howells5b86d4f2018-05-18 11:46:15 +010049 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010050 if (ret < 0)
51 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070052
53 socket->sk->sk_allocation = GFP_NOFS;
54
55 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000056 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070057 srx.srx_family = AF_RXRPC;
58 srx.srx_service = CM_SERVICE;
59 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000060 srx.transport_len = sizeof(srx.transport.sin6);
61 srx.transport.sin6.sin6_family = AF_INET6;
62 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
David Howells08e0e7c2007-04-26 15:55:03 -070063
David Howells4776cab2018-05-10 23:10:40 +010064 min_level = RXRPC_SECURITY_ENCRYPT;
65 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
66 (void *)&min_level, sizeof(min_level));
67 if (ret < 0)
68 goto error_2;
69
David Howells08e0e7c2007-04-26 15:55:03 -070070 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000071 if (ret == -EADDRINUSE) {
72 srx.transport.sin6.sin6_port = 0;
73 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
74 }
David Howells0e119b42016-06-10 22:30:37 +010075 if (ret < 0)
76 goto error_2;
77
David Howells00e90712016-09-08 11:10:12 +010078 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
79 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010080
David Howells0e119b42016-06-10 22:30:37 +010081 ret = kernel_listen(socket, INT_MAX);
82 if (ret < 0)
83 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070084
David Howellsf044c882017-11-02 15:27:45 +000085 net->socket = socket;
86 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -070087 _leave(" = 0");
88 return 0;
David Howells0e119b42016-06-10 22:30:37 +010089
90error_2:
91 sock_release(socket);
92error_1:
David Howells0e119b42016-06-10 22:30:37 +010093 _leave(" = %d", ret);
94 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -070095}
96
97/*
98 * close the RxRPC socket AFS was using
99 */
David Howellsf044c882017-11-02 15:27:45 +0000100void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -0700101{
102 _enter("");
103
David Howellsf044c882017-11-02 15:27:45 +0000104 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +0000105 flush_workqueue(afs_async_calls);
106
David Howellsf044c882017-11-02 15:27:45 +0000107 if (net->spare_incoming_call) {
108 afs_put_call(net->spare_incoming_call);
109 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100110 }
111
David Howellsf044c882017-11-02 15:27:45 +0000112 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100113 wait_var_event(&net->nr_outstanding_calls,
114 !atomic_read(&net->nr_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100115 _debug("no outstanding calls");
116
David Howellsf044c882017-11-02 15:27:45 +0000117 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100118 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000119 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700120
121 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700122 _leave("");
123}
124
125/*
David Howells341f7412017-01-05 10:38:36 +0000126 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700127 */
David Howellsf044c882017-11-02 15:27:45 +0000128static struct afs_call *afs_alloc_call(struct afs_net *net,
129 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000130 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700131{
David Howells341f7412017-01-05 10:38:36 +0000132 struct afs_call *call;
133 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700134
David Howells341f7412017-01-05 10:38:36 +0000135 call = kzalloc(sizeof(*call), gfp);
136 if (!call)
137 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700138
David Howells341f7412017-01-05 10:38:36 +0000139 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000140 call->net = net;
David Howellsa25e21f2018-03-27 23:03:00 +0100141 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells341f7412017-01-05 10:38:36 +0000142 atomic_set(&call->usage, 1);
143 INIT_WORK(&call->async_work, afs_process_async_call);
144 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000145 spin_lock_init(&call->state_lock);
David Howells2f02f7a2016-04-07 17:23:03 +0100146
David Howellsf044c882017-11-02 15:27:45 +0000147 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000148 trace_afs_call(call, afs_call_trace_alloc, 1, o,
149 __builtin_return_address(0));
150 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700151}
152
153/*
David Howells341f7412017-01-05 10:38:36 +0000154 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100155 */
David Howells341f7412017-01-05 10:38:36 +0000156void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100157{
David Howellsf044c882017-11-02 15:27:45 +0000158 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000159 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000160 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000161
162 trace_afs_call(call, afs_call_trace_put, n + 1, o,
163 __builtin_return_address(0));
164
165 ASSERTCMP(n, >=, 0);
166 if (n == 0) {
167 ASSERT(!work_pending(&call->async_work));
168 ASSERT(call->type->name != NULL);
169
170 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000171 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000172 call->rxcall = NULL;
173 }
174 if (call->type->destructor)
175 call->type->destructor(call);
176
David Howellsd0676a12017-11-02 15:27:49 +0000177 afs_put_server(call->net, call->cm_server);
David Howellsd2ddc772017-11-02 15:27:50 +0000178 afs_put_cb_interest(call->net, call->cbi);
David Howells341f7412017-01-05 10:38:36 +0000179 kfree(call->request);
David Howellsa25e21f2018-03-27 23:03:00 +0100180
181 trace_afs_call(call, afs_call_trace_free, 0, o,
182 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000183 kfree(call);
184
David Howellsf044c882017-11-02 15:27:45 +0000185 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000186 if (o == 0)
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100187 wake_up_var(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100188 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100189}
190
191/*
David Howells341f7412017-01-05 10:38:36 +0000192 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100193 */
David Howells341f7412017-01-05 10:38:36 +0000194int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100195{
David Howells341f7412017-01-05 10:38:36 +0000196 int u = atomic_inc_return(&call->usage);
197
198 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000199 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000200 __builtin_return_address(0));
201
202 INIT_WORK(&call->work, call->type->work);
203
204 if (!queue_work(afs_wq, &call->work))
205 afs_put_call(call);
206 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100207}
208
209/*
David Howells08e0e7c2007-04-26 15:55:03 -0700210 * allocate a call with flat request and reply buffers
211 */
David Howellsf044c882017-11-02 15:27:45 +0000212struct afs_call *afs_alloc_flat_call(struct afs_net *net,
213 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100214 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700215{
216 struct afs_call *call;
217
David Howellsf044c882017-11-02 15:27:45 +0000218 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700219 if (!call)
220 goto nomem_call;
221
David Howells00d3b7a2007-04-26 15:57:07 -0700222 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000223 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700224 call->request = kmalloc(request_size, GFP_NOFS);
225 if (!call->request)
226 goto nomem_free;
227 }
228
David Howellsd0016482016-08-30 20:42:14 +0100229 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000230 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100231 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700232 if (!call->buffer)
233 goto nomem_free;
234 }
235
David Howells025db802017-11-02 15:27:51 +0000236 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700237 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700238 return call;
239
David Howells00d3b7a2007-04-26 15:57:07 -0700240nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000241 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700242nomem_call:
243 return NULL;
244}
245
246/*
247 * clean up a call with flat buffer
248 */
249void afs_flat_call_destructor(struct afs_call *call)
250{
251 _enter("");
252
253 kfree(call->request);
254 call->request = NULL;
255 kfree(call->buffer);
256 call->buffer = NULL;
257}
258
David Howells2f5705a2017-03-16 16:27:46 +0000259#define AFS_BVEC_MAX 8
260
261/*
262 * Load the given bvec with the next few pages.
263 */
264static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
265 struct bio_vec *bv, pgoff_t first, pgoff_t last,
266 unsigned offset)
267{
268 struct page *pages[AFS_BVEC_MAX];
269 unsigned int nr, n, i, to, bytes = 0;
270
271 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
272 n = find_get_pages_contig(call->mapping, first, nr, pages);
273 ASSERTCMP(n, ==, nr);
274
275 msg->msg_flags |= MSG_MORE;
276 for (i = 0; i < nr; i++) {
277 to = PAGE_SIZE;
278 if (first + i >= last) {
279 to = call->last_to;
280 msg->msg_flags &= ~MSG_MORE;
281 }
282 bv[i].bv_page = pages[i];
283 bv[i].bv_len = to - offset;
284 bv[i].bv_offset = offset;
285 bytes += to - offset;
286 offset = 0;
287 }
288
289 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
290}
291
David Howells08e0e7c2007-04-26 15:55:03 -0700292/*
David Howellse8332512017-08-29 10:18:56 +0100293 * Advance the AFS call state when the RxRPC call ends the transmit phase.
294 */
295static void afs_notify_end_request_tx(struct sock *sock,
296 struct rxrpc_call *rxcall,
297 unsigned long call_user_ID)
298{
299 struct afs_call *call = (struct afs_call *)call_user_ID;
300
David Howells98bf40c2017-11-02 15:27:53 +0000301 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100302}
303
304/*
David Howells31143d52007-05-09 02:33:46 -0700305 * attach the data from a bunch of pages on an inode to a call
306 */
Al Viro39c6ace2016-01-09 20:36:51 -0500307static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700308{
David Howells2f5705a2017-03-16 16:27:46 +0000309 struct bio_vec bv[AFS_BVEC_MAX];
310 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700311 pgoff_t first = call->first, last = call->last;
312 int ret;
313
David Howells31143d52007-05-09 02:33:46 -0700314 offset = call->first_offset;
315 call->first_offset = 0;
316
317 do {
David Howells2f5705a2017-03-16 16:27:46 +0000318 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000319 trace_afs_send_pages(call, msg, first, last, offset);
320
David Howells2f5705a2017-03-16 16:27:46 +0000321 offset = 0;
322 bytes = msg->msg_iter.count;
323 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700324
David Howellsf044c882017-11-02 15:27:45 +0000325 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100326 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000327 for (loop = 0; loop < nr; loop++)
328 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700329 if (ret < 0)
330 break;
David Howells2f5705a2017-03-16 16:27:46 +0000331
332 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700333 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700334
David Howells2c099012017-11-02 15:27:51 +0000335 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700336 return ret;
337}
338
339/*
David Howells08e0e7c2007-04-26 15:55:03 -0700340 * initiate a call
341 */
David Howells8b2a4642017-11-02 15:27:50 +0000342long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000343 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700344{
David Howells8b2a4642017-11-02 15:27:50 +0000345 struct sockaddr_rxrpc *srx = ac->addr;
David Howells08e0e7c2007-04-26 15:55:03 -0700346 struct rxrpc_call *rxcall;
347 struct msghdr msg;
348 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000349 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100350 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700351 int ret;
352
David Howells4d9df982017-11-02 15:27:47 +0000353 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700354
David Howells00d3b7a2007-04-26 15:57:07 -0700355 ASSERT(call->type != NULL);
356 ASSERT(call->type->name != NULL);
357
David Howells31143d52007-05-09 02:33:46 -0700358 _debug("____MAKE %p{%s,%x} [%d]____",
359 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000360 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700361
David Howells56ff9c82017-01-05 10:38:36 +0000362 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700363
David Howellse754eba2017-06-07 12:40:03 +0100364 /* Work out the length we're going to transmit. This is awkward for
365 * calls such as FS.StoreData where there's an extra injection of data
366 * after the initial fixed part.
367 */
368 tx_total_len = call->request_size;
369 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000370 if (call->last == call->first) {
371 tx_total_len += call->last_to - call->first_offset;
372 } else {
373 /* It looks mathematically like you should be able to
374 * combine the following lines with the ones above, but
375 * unsigned arithmetic is fun when it wraps...
376 */
377 tx_total_len += PAGE_SIZE - call->first_offset;
378 tx_total_len += call->last_to;
379 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
380 }
David Howellse754eba2017-06-07 12:40:03 +0100381 }
382
David Howells08e0e7c2007-04-26 15:55:03 -0700383 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000384 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100385 (unsigned long)call,
386 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000387 (async ?
388 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100389 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100390 call->upgrade,
391 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700392 if (IS_ERR(rxcall)) {
393 ret = PTR_ERR(rxcall);
394 goto error_kill_call;
395 }
396
397 call->rxcall = rxcall;
398
399 /* send the request */
400 iov[0].iov_base = call->request;
401 iov[0].iov_len = call->request_size;
402
403 msg.msg_name = NULL;
404 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500405 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500406 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700407 msg.msg_control = NULL;
408 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100409 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700410
David Howellsf044c882017-11-02 15:27:45 +0000411 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100412 &msg, call->request_size,
413 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700414 if (ret < 0)
415 goto error_do_abort;
416
David Howells31143d52007-05-09 02:33:46 -0700417 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500418 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700419 if (ret < 0)
420 goto error_do_abort;
421 }
422
David Howells08e0e7c2007-04-26 15:55:03 -0700423 /* at this point, an async call may no longer exist as it may have
424 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000425 if (call->async)
426 return -EINPROGRESS;
427
David Howellsd2ddc772017-11-02 15:27:50 +0000428 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700429
430error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000431 call->state = AFS_CALL_COMPLETE;
432 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000433 rxrpc_kernel_abort_call(call->net->socket, rxcall,
434 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000435 } else {
David Howells70af0e32017-03-16 16:27:47 +0000436 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000437 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000438 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000439 &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000440 ac->abort_code = call->abort_code;
441 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000442 }
David Howells025db802017-11-02 15:27:51 +0000443 call->error = ret;
444 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700445error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000446 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000447 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700448 _leave(" = %d", ret);
449 return ret;
450}
451
452/*
David Howells08e0e7c2007-04-26 15:55:03 -0700453 * deliver messages to a call
454 */
455static void afs_deliver_to_call(struct afs_call *call)
456{
David Howells98bf40c2017-11-02 15:27:53 +0000457 enum afs_call_state state;
458 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700459 int ret;
460
David Howellsd0016482016-08-30 20:42:14 +0100461 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700462
David Howells98bf40c2017-11-02 15:27:53 +0000463 while (state = READ_ONCE(call->state),
464 state == AFS_CALL_CL_AWAIT_REPLY ||
465 state == AFS_CALL_SV_AWAIT_OP_ID ||
466 state == AFS_CALL_SV_AWAIT_REQUEST ||
467 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100468 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000469 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howellsd0016482016-08-30 20:42:14 +0100470 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000471 ret = rxrpc_kernel_recv_data(call->net->socket,
472 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100473 NULL, 0, &offset, false,
David Howells98bf40c2017-11-02 15:27:53 +0000474 &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100475 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000476 trace_afs_recv_data(call, 0, offset, false, ret);
477
David Howellsd0016482016-08-30 20:42:14 +0100478 if (ret == -EINPROGRESS || ret == -EAGAIN)
479 return;
David Howells98bf40c2017-11-02 15:27:53 +0000480 if (ret < 0 || ret == 1) {
481 if (ret == 1)
482 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000483 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000484 }
David Howellsd0016482016-08-30 20:42:14 +0100485 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700486 }
487
David Howellsd0016482016-08-30 20:42:14 +0100488 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000489 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100490 switch (ret) {
491 case 0:
David Howellsf2686b02018-05-10 14:12:50 +0100492 if (state == AFS_CALL_CL_PROC_REPLY) {
493 if (call->cbi)
494 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
495 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000496 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100497 }
David Howells98bf40c2017-11-02 15:27:53 +0000498 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100499 goto done;
500 case -EINPROGRESS:
501 case -EAGAIN:
502 goto out;
David Howells98bf40c2017-11-02 15:27:53 +0000503 case -EIO:
David Howells70af0e32017-03-16 16:27:47 +0000504 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000505 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
506 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100507 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000508 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000509 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100510 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000511 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100512 case -ENODATA:
513 case -EBADMSG:
514 case -EMSGSIZE:
515 default:
516 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000517 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100518 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000519 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100520 abort_code, -EBADMSG, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000521 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100522 }
David Howells08e0e7c2007-04-26 15:55:03 -0700523 }
524
David Howellsd0016482016-08-30 20:42:14 +0100525done:
David Howells98bf40c2017-11-02 15:27:53 +0000526 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000527 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100528out:
David Howells08e0e7c2007-04-26 15:55:03 -0700529 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100530 return;
531
David Howells98bf40c2017-11-02 15:27:53 +0000532local_abort:
533 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000534call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000535 afs_set_call_complete(call, ret, remote_abort);
536 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100537 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700538}
539
540/*
541 * wait synchronously for a call to complete
542 */
David Howellsd2ddc772017-11-02 15:27:50 +0000543static long afs_wait_for_call_to_complete(struct afs_call *call,
544 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700545{
David Howellsbc5e3a52017-10-18 11:07:31 +0100546 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000547 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100548 u64 rtt;
549 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700550
551 DECLARE_WAITQUEUE(myself, current);
552
553 _enter("");
554
David Howellsf044c882017-11-02 15:27:45 +0000555 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100556 rtt2 = nsecs_to_jiffies64(rtt) * 2;
557 if (rtt2 < 2)
558 rtt2 = 2;
559
560 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000561 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100562
David Howells08e0e7c2007-04-26 15:55:03 -0700563 add_wait_queue(&call->waitq, &myself);
564 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100565 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700566
567 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000568 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
569 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100570 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700571 __set_current_state(TASK_RUNNING);
572 afs_deliver_to_call(call);
573 continue;
574 }
575
David Howells98bf40c2017-11-02 15:27:53 +0000576 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700577 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100578
David Howellsf044c882017-11-02 15:27:45 +0000579 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100580 if (timeout == 0 &&
581 life == last_life && signal_pending(current))
582 break;
583
584 if (life != last_life) {
585 timeout = rtt2;
586 last_life = life;
587 }
588
589 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700590 }
591
592 remove_wait_queue(&call->waitq, &myself);
593 __set_current_state(TASK_RUNNING);
594
David Howells954cd6d2017-03-16 16:27:49 +0000595 /* Kill off the call if it's still live. */
David Howells98bf40c2017-11-02 15:27:53 +0000596 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Howells954cd6d2017-03-16 16:27:49 +0000597 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000598 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells98bf40c2017-11-02 15:27:53 +0000599 RX_USER_ABORT, -EINTR, "KWI"))
600 afs_set_call_complete(call, -EINTR, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700601 }
602
David Howells98bf40c2017-11-02 15:27:53 +0000603 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000604 ac->abort_code = call->abort_code;
605 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000606 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000607
608 ret = ac->error;
609 switch (ret) {
610 case 0:
611 if (call->ret_reply0) {
612 ret = (long)call->reply[0];
613 call->reply[0] = NULL;
614 }
615 /* Fall through */
616 case -ECONNABORTED:
617 ac->responded = true;
618 break;
David Howells33cd7f22017-11-02 15:27:48 +0000619 }
620
David Howells08e0e7c2007-04-26 15:55:03 -0700621 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000622 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000623 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700624 return ret;
625}
626
627/*
628 * wake up a waiting call
629 */
David Howellsd0016482016-08-30 20:42:14 +0100630static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
631 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700632{
David Howellsd0016482016-08-30 20:42:14 +0100633 struct afs_call *call = (struct afs_call *)call_user_ID;
634
635 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700636 wake_up(&call->waitq);
637}
638
639/*
640 * wake up an asynchronous call
641 */
David Howellsd0016482016-08-30 20:42:14 +0100642static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
643 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700644{
David Howellsd0016482016-08-30 20:42:14 +0100645 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000646 int u;
David Howellsd0016482016-08-30 20:42:14 +0100647
David Howells8e8d7f12017-01-05 10:38:34 +0000648 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100649 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000650
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100651 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000652 if (u != 0) {
653 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000654 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000655 __builtin_return_address(0));
656
657 if (!queue_work(afs_async_calls, &call->async_work))
658 afs_put_call(call);
659 }
David Howells08e0e7c2007-04-26 15:55:03 -0700660}
661
662/*
David Howells341f7412017-01-05 10:38:36 +0000663 * Delete an asynchronous call. The work item carries a ref to the call struct
664 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700665 */
David Howellsd0016482016-08-30 20:42:14 +0100666static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700667{
David Howellsd0016482016-08-30 20:42:14 +0100668 struct afs_call *call = container_of(work, struct afs_call, async_work);
669
David Howells08e0e7c2007-04-26 15:55:03 -0700670 _enter("");
671
David Howells341f7412017-01-05 10:38:36 +0000672 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700673
674 _leave("");
675}
676
677/*
David Howells341f7412017-01-05 10:38:36 +0000678 * Perform I/O processing on an asynchronous call. The work item carries a ref
679 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700680 */
David Howellsd0016482016-08-30 20:42:14 +0100681static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700682{
David Howellsd0016482016-08-30 20:42:14 +0100683 struct afs_call *call = container_of(work, struct afs_call, async_work);
684
David Howells08e0e7c2007-04-26 15:55:03 -0700685 _enter("");
686
David Howellsd0016482016-08-30 20:42:14 +0100687 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
688 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700689 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100690 }
David Howells08e0e7c2007-04-26 15:55:03 -0700691
David Howells56ff9c82017-01-05 10:38:36 +0000692 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000693 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700694
David Howells341f7412017-01-05 10:38:36 +0000695 /* We have two refs to release - one from the alloc and one
696 * queued with the work item - and we can't just deallocate the
697 * call because the work item may be queued again.
698 */
David Howellsd0016482016-08-30 20:42:14 +0100699 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000700 if (!queue_work(afs_async_calls, &call->async_work))
701 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700702 }
703
David Howells341f7412017-01-05 10:38:36 +0000704 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700705 _leave("");
706}
707
David Howells00e90712016-09-08 11:10:12 +0100708static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
709{
710 struct afs_call *call = (struct afs_call *)user_call_ID;
711
712 call->rxcall = rxcall;
713}
714
715/*
716 * Charge the incoming call preallocation.
717 */
David Howellsf044c882017-11-02 15:27:45 +0000718void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100719{
David Howellsf044c882017-11-02 15:27:45 +0000720 struct afs_net *net =
721 container_of(work, struct afs_net, charge_preallocation_work);
722 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100723
724 for (;;) {
725 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000726 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100727 if (!call)
728 break;
729
David Howells56ff9c82017-01-05 10:38:36 +0000730 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000731 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000732 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100733 }
734
David Howellsf044c882017-11-02 15:27:45 +0000735 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100736 afs_wake_up_async_call,
737 afs_rx_attach,
738 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100739 GFP_KERNEL,
740 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100741 break;
742 call = NULL;
743 }
David Howellsf044c882017-11-02 15:27:45 +0000744 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100745}
746
747/*
748 * Discard a preallocated call when a socket is shut down.
749 */
750static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
751 unsigned long user_call_ID)
752{
753 struct afs_call *call = (struct afs_call *)user_call_ID;
754
David Howells00e90712016-09-08 11:10:12 +0100755 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000756 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100757}
758
David Howells08e0e7c2007-04-26 15:55:03 -0700759/*
David Howellsd0016482016-08-30 20:42:14 +0100760 * Notification of an incoming call.
761 */
David Howells00e90712016-09-08 11:10:12 +0100762static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
763 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100764{
David Howellsf044c882017-11-02 15:27:45 +0000765 struct afs_net *net = afs_sock2net(sk);
766
767 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100768}
769
770/*
David Howells372ee162016-08-03 14:11:40 +0100771 * Grab the operation ID from an incoming cache manager call. The socket
772 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700773 */
David Howellsd0016482016-08-30 20:42:14 +0100774static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700775{
David Howellsd0016482016-08-30 20:42:14 +0100776 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700777
David Howellsd0016482016-08-30 20:42:14 +0100778 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700779
780 ASSERTCMP(call->offset, <, 4);
781
782 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100783 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100784 if (ret < 0)
785 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700786
David Howells50a2c952016-10-13 08:27:10 +0100787 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000788 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howellsd0016482016-08-30 20:42:14 +0100789 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700790
791 /* ask the cache manager to route the call (it'll change the call type
792 * if successful) */
793 if (!afs_cm_incoming_call(call))
794 return -ENOTSUPP;
795
David Howells8e8d7f12017-01-05 10:38:34 +0000796 trace_afs_cb_call(call);
797
David Howells08e0e7c2007-04-26 15:55:03 -0700798 /* pass responsibility for the remainer of this message off to the
799 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100800 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700801}
802
803/*
David Howellse8332512017-08-29 10:18:56 +0100804 * Advance the AFS call state when an RxRPC service call ends the transmit
805 * phase.
806 */
807static void afs_notify_end_reply_tx(struct sock *sock,
808 struct rxrpc_call *rxcall,
809 unsigned long call_user_ID)
810{
811 struct afs_call *call = (struct afs_call *)call_user_ID;
812
David Howells98bf40c2017-11-02 15:27:53 +0000813 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100814}
815
816/*
David Howells08e0e7c2007-04-26 15:55:03 -0700817 * send an empty reply
818 */
819void afs_send_empty_reply(struct afs_call *call)
820{
David Howellsf044c882017-11-02 15:27:45 +0000821 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700822 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700823
824 _enter("");
825
David Howellsf044c882017-11-02 15:27:45 +0000826 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100827
David Howells08e0e7c2007-04-26 15:55:03 -0700828 msg.msg_name = NULL;
829 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100830 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700831 msg.msg_control = NULL;
832 msg.msg_controllen = 0;
833 msg.msg_flags = 0;
834
David Howellsf044c882017-11-02 15:27:45 +0000835 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100836 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700837 case 0:
838 _leave(" [replied]");
839 return;
840
841 case -ENOMEM:
842 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000843 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100844 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700845 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700846 _leave(" [error]");
847 return;
848 }
849}
850
851/*
David Howellsb908fe62007-04-26 15:58:17 -0700852 * send a simple reply
853 */
854void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
855{
David Howellsf044c882017-11-02 15:27:45 +0000856 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700857 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500858 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100859 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700860
861 _enter("");
862
David Howellsf044c882017-11-02 15:27:45 +0000863 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100864
David Howellsb908fe62007-04-26 15:58:17 -0700865 iov[0].iov_base = (void *) buf;
866 iov[0].iov_len = len;
867 msg.msg_name = NULL;
868 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500869 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700870 msg.msg_control = NULL;
871 msg.msg_controllen = 0;
872 msg.msg_flags = 0;
873
David Howellsf044c882017-11-02 15:27:45 +0000874 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100875 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100876 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100877 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700878 _leave(" [replied]");
879 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100880 }
David Howells6c67c7c2014-05-21 14:48:05 +0100881
David Howellsbd6dc742007-07-20 10:59:41 +0100882 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700883 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000884 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100885 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700886 }
David Howellsbd6dc742007-07-20 10:59:41 +0100887 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700888}
889
890/*
David Howells372ee162016-08-03 14:11:40 +0100891 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700892 */
David Howellsd0016482016-08-30 20:42:14 +0100893int afs_extract_data(struct afs_call *call, void *buf, size_t count,
894 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700895{
David Howellsf044c882017-11-02 15:27:45 +0000896 struct afs_net *net = call->net;
David Howells98bf40c2017-11-02 15:27:53 +0000897 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000898 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100899 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700900
David Howellsd0016482016-08-30 20:42:14 +0100901 _enter("{%s,%zu},,%zu,%d",
902 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700903
David Howellsd0016482016-08-30 20:42:14 +0100904 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700905
David Howellsf044c882017-11-02 15:27:45 +0000906 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100907 buf, count, &call->offset,
David Howells98bf40c2017-11-02 15:27:53 +0000908 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100909 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000910 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100911 if (ret == 0 || ret == -EAGAIN)
912 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700913
David Howells98bf40c2017-11-02 15:27:53 +0000914 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100915 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000916 switch (state) {
917 case AFS_CALL_CL_AWAIT_REPLY:
918 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100919 break;
David Howells98bf40c2017-11-02 15:27:53 +0000920 case AFS_CALL_SV_AWAIT_REQUEST:
921 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100922 break;
David Howells98bf40c2017-11-02 15:27:53 +0000923 case AFS_CALL_COMPLETE:
924 kdebug("prem complete %d", call->error);
925 return -EIO;
David Howellsd0016482016-08-30 20:42:14 +0100926 default:
927 break;
928 }
929 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700930 }
David Howellsd0016482016-08-30 20:42:14 +0100931
David Howells98bf40c2017-11-02 15:27:53 +0000932 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100933 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700934}
David Howells5f702c82018-04-06 14:17:25 +0100935
936/*
937 * Log protocol error production.
938 */
939noinline int afs_protocol_error(struct afs_call *call, int error)
940{
941 trace_afs_protocol_error(call, error, __builtin_return_address(0));
942 return error;
943}