blob: 639c16882e93259f0724af24c3dcac157ecb8156 [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
David Howellsaa563d72018-10-20 00:57:56 +0100289 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
David Howells2f5705a2017-03-16 16:27:46 +0000290}
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 Howellse754eba2017-06-07 12:40:03 +0100349 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700350 int ret;
351
David Howells4d9df982017-11-02 15:27:47 +0000352 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700353
David Howells00d3b7a2007-04-26 15:57:07 -0700354 ASSERT(call->type != NULL);
355 ASSERT(call->type->name != NULL);
356
David Howells31143d52007-05-09 02:33:46 -0700357 _debug("____MAKE %p{%s,%x} [%d]____",
358 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000359 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700360
David Howells56ff9c82017-01-05 10:38:36 +0000361 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700362
David Howellse754eba2017-06-07 12:40:03 +0100363 /* Work out the length we're going to transmit. This is awkward for
364 * calls such as FS.StoreData where there's an extra injection of data
365 * after the initial fixed part.
366 */
367 tx_total_len = call->request_size;
368 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000369 if (call->last == call->first) {
370 tx_total_len += call->last_to - call->first_offset;
371 } else {
372 /* It looks mathematically like you should be able to
373 * combine the following lines with the ones above, but
374 * unsigned arithmetic is fun when it wraps...
375 */
376 tx_total_len += PAGE_SIZE - call->first_offset;
377 tx_total_len += call->last_to;
378 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
379 }
David Howellse754eba2017-06-07 12:40:03 +0100380 }
381
David Howells08e0e7c2007-04-26 15:55:03 -0700382 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000383 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100384 (unsigned long)call,
385 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000386 (async ?
387 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100388 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100389 call->upgrade,
390 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700391 if (IS_ERR(rxcall)) {
392 ret = PTR_ERR(rxcall);
393 goto error_kill_call;
394 }
395
396 call->rxcall = rxcall;
397
398 /* send the request */
399 iov[0].iov_base = call->request;
400 iov[0].iov_len = call->request_size;
401
402 msg.msg_name = NULL;
403 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100404 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700405 msg.msg_control = NULL;
406 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100407 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700408
David Howellsf044c882017-11-02 15:27:45 +0000409 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100410 &msg, call->request_size,
411 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700412 if (ret < 0)
413 goto error_do_abort;
414
David Howells31143d52007-05-09 02:33:46 -0700415 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500416 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700417 if (ret < 0)
418 goto error_do_abort;
419 }
420
David Howells08e0e7c2007-04-26 15:55:03 -0700421 /* at this point, an async call may no longer exist as it may have
422 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000423 if (call->async)
424 return -EINPROGRESS;
425
David Howellsd2ddc772017-11-02 15:27:50 +0000426 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700427
428error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000429 call->state = AFS_CALL_COMPLETE;
430 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000431 rxrpc_kernel_abort_call(call->net->socket, rxcall,
432 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000433 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100434 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100435 rxrpc_kernel_recv_data(call->net->socket, rxcall,
436 &msg.msg_iter, false,
437 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000438 ac->abort_code = call->abort_code;
439 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000440 }
David Howells025db802017-11-02 15:27:51 +0000441 call->error = ret;
442 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700443error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000444 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000445 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700446 _leave(" = %d", ret);
447 return ret;
448}
449
450/*
David Howells08e0e7c2007-04-26 15:55:03 -0700451 * deliver messages to a call
452 */
453static void afs_deliver_to_call(struct afs_call *call)
454{
David Howells98bf40c2017-11-02 15:27:53 +0000455 enum afs_call_state state;
456 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700457 int ret;
458
David Howellsd0016482016-08-30 20:42:14 +0100459 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700460
David Howells98bf40c2017-11-02 15:27:53 +0000461 while (state = READ_ONCE(call->state),
462 state == AFS_CALL_CL_AWAIT_REPLY ||
463 state == AFS_CALL_SV_AWAIT_OP_ID ||
464 state == AFS_CALL_SV_AWAIT_REQUEST ||
465 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100466 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000467 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howellseb9950e2018-08-03 17:06:56 +0100468 struct iov_iter iter;
469
David Howellsaa563d72018-10-20 00:57:56 +0100470 iov_iter_kvec(&iter, READ, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000471 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howellseb9950e2018-08-03 17:06:56 +0100472 call->rxcall, &iter, false,
David Howells98bf40c2017-11-02 15:27:53 +0000473 &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100474 &call->service_id);
David Howellseb9950e2018-08-03 17:06:56 +0100475 trace_afs_recv_data(call, 0, 0, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000476
David Howellsd0016482016-08-30 20:42:14 +0100477 if (ret == -EINPROGRESS || ret == -EAGAIN)
478 return;
David Howells98bf40c2017-11-02 15:27:53 +0000479 if (ret < 0 || ret == 1) {
480 if (ret == 1)
481 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000482 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000483 }
David Howellsd0016482016-08-30 20:42:14 +0100484 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700485 }
486
David Howellsd0016482016-08-30 20:42:14 +0100487 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000488 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100489 switch (ret) {
490 case 0:
David Howellsf2686b02018-05-10 14:12:50 +0100491 if (state == AFS_CALL_CL_PROC_REPLY) {
492 if (call->cbi)
493 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
494 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000495 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100496 }
David Howells98bf40c2017-11-02 15:27:53 +0000497 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100498 goto done;
499 case -EINPROGRESS:
500 case -EAGAIN:
501 goto out;
David Howells98bf40c2017-11-02 15:27:53 +0000502 case -EIO:
David Howells70af0e32017-03-16 16:27:47 +0000503 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000504 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
505 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100506 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000507 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000508 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100509 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000510 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100511 case -ENODATA:
512 case -EBADMSG:
513 case -EMSGSIZE:
514 default:
515 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000516 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100517 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000518 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100519 abort_code, -EBADMSG, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000520 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100521 }
David Howells08e0e7c2007-04-26 15:55:03 -0700522 }
523
David Howellsd0016482016-08-30 20:42:14 +0100524done:
David Howells98bf40c2017-11-02 15:27:53 +0000525 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000526 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100527out:
David Howells08e0e7c2007-04-26 15:55:03 -0700528 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100529 return;
530
David Howells98bf40c2017-11-02 15:27:53 +0000531local_abort:
532 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000533call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000534 afs_set_call_complete(call, ret, remote_abort);
535 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100536 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700537}
538
539/*
540 * wait synchronously for a call to complete
541 */
David Howellsd2ddc772017-11-02 15:27:50 +0000542static long afs_wait_for_call_to_complete(struct afs_call *call,
543 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700544{
David Howellsbc5e3a52017-10-18 11:07:31 +0100545 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000546 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100547 u64 rtt;
548 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700549
550 DECLARE_WAITQUEUE(myself, current);
551
552 _enter("");
553
David Howellsf044c882017-11-02 15:27:45 +0000554 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100555 rtt2 = nsecs_to_jiffies64(rtt) * 2;
556 if (rtt2 < 2)
557 rtt2 = 2;
558
559 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000560 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100561
David Howells08e0e7c2007-04-26 15:55:03 -0700562 add_wait_queue(&call->waitq, &myself);
563 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100564 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700565
566 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000567 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
568 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100569 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700570 __set_current_state(TASK_RUNNING);
571 afs_deliver_to_call(call);
572 continue;
573 }
574
David Howells98bf40c2017-11-02 15:27:53 +0000575 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700576 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100577
David Howellsf044c882017-11-02 15:27:45 +0000578 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100579 if (timeout == 0 &&
580 life == last_life && signal_pending(current))
581 break;
582
583 if (life != last_life) {
584 timeout = rtt2;
585 last_life = life;
586 }
587
588 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700589 }
590
591 remove_wait_queue(&call->waitq, &myself);
592 __set_current_state(TASK_RUNNING);
593
David Howells954cd6d2017-03-16 16:27:49 +0000594 /* Kill off the call if it's still live. */
David Howells98bf40c2017-11-02 15:27:53 +0000595 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Howells954cd6d2017-03-16 16:27:49 +0000596 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000597 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells98bf40c2017-11-02 15:27:53 +0000598 RX_USER_ABORT, -EINTR, "KWI"))
599 afs_set_call_complete(call, -EINTR, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700600 }
601
David Howells98bf40c2017-11-02 15:27:53 +0000602 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000603 ac->abort_code = call->abort_code;
604 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000605 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000606
607 ret = ac->error;
608 switch (ret) {
609 case 0:
610 if (call->ret_reply0) {
611 ret = (long)call->reply[0];
612 call->reply[0] = NULL;
613 }
614 /* Fall through */
615 case -ECONNABORTED:
616 ac->responded = true;
617 break;
David Howells33cd7f22017-11-02 15:27:48 +0000618 }
619
David Howells08e0e7c2007-04-26 15:55:03 -0700620 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000621 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000622 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700623 return ret;
624}
625
626/*
627 * wake up a waiting call
628 */
David Howellsd0016482016-08-30 20:42:14 +0100629static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
630 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700631{
David Howellsd0016482016-08-30 20:42:14 +0100632 struct afs_call *call = (struct afs_call *)call_user_ID;
633
634 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700635 wake_up(&call->waitq);
636}
637
638/*
639 * wake up an asynchronous call
640 */
David Howellsd0016482016-08-30 20:42:14 +0100641static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
642 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700643{
David Howellsd0016482016-08-30 20:42:14 +0100644 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000645 int u;
David Howellsd0016482016-08-30 20:42:14 +0100646
David Howells8e8d7f12017-01-05 10:38:34 +0000647 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100648 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000649
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100650 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000651 if (u != 0) {
652 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000653 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000654 __builtin_return_address(0));
655
656 if (!queue_work(afs_async_calls, &call->async_work))
657 afs_put_call(call);
658 }
David Howells08e0e7c2007-04-26 15:55:03 -0700659}
660
661/*
David Howells341f7412017-01-05 10:38:36 +0000662 * Delete an asynchronous call. The work item carries a ref to the call struct
663 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700664 */
David Howellsd0016482016-08-30 20:42:14 +0100665static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700666{
David Howellsd0016482016-08-30 20:42:14 +0100667 struct afs_call *call = container_of(work, struct afs_call, async_work);
668
David Howells08e0e7c2007-04-26 15:55:03 -0700669 _enter("");
670
David Howells341f7412017-01-05 10:38:36 +0000671 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700672
673 _leave("");
674}
675
676/*
David Howells341f7412017-01-05 10:38:36 +0000677 * Perform I/O processing on an asynchronous call. The work item carries a ref
678 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700679 */
David Howellsd0016482016-08-30 20:42:14 +0100680static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700681{
David Howellsd0016482016-08-30 20:42:14 +0100682 struct afs_call *call = container_of(work, struct afs_call, async_work);
683
David Howells08e0e7c2007-04-26 15:55:03 -0700684 _enter("");
685
David Howellsd0016482016-08-30 20:42:14 +0100686 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
687 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700688 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100689 }
David Howells08e0e7c2007-04-26 15:55:03 -0700690
David Howells56ff9c82017-01-05 10:38:36 +0000691 if (call->state == AFS_CALL_COMPLETE) {
David Howells341f7412017-01-05 10:38:36 +0000692 /* We have two refs to release - one from the alloc and one
693 * queued with the work item - and we can't just deallocate the
694 * call because the work item may be queued again.
695 */
David Howellsd0016482016-08-30 20:42:14 +0100696 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000697 if (!queue_work(afs_async_calls, &call->async_work))
698 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700699 }
700
David Howells341f7412017-01-05 10:38:36 +0000701 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700702 _leave("");
703}
704
David Howells00e90712016-09-08 11:10:12 +0100705static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
706{
707 struct afs_call *call = (struct afs_call *)user_call_ID;
708
709 call->rxcall = rxcall;
710}
711
712/*
713 * Charge the incoming call preallocation.
714 */
David Howellsf044c882017-11-02 15:27:45 +0000715void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100716{
David Howellsf044c882017-11-02 15:27:45 +0000717 struct afs_net *net =
718 container_of(work, struct afs_net, charge_preallocation_work);
719 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100720
721 for (;;) {
722 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000723 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100724 if (!call)
725 break;
726
David Howells56ff9c82017-01-05 10:38:36 +0000727 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000728 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000729 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100730 }
731
David Howellsf044c882017-11-02 15:27:45 +0000732 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100733 afs_wake_up_async_call,
734 afs_rx_attach,
735 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100736 GFP_KERNEL,
737 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100738 break;
739 call = NULL;
740 }
David Howellsf044c882017-11-02 15:27:45 +0000741 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100742}
743
744/*
745 * Discard a preallocated call when a socket is shut down.
746 */
747static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
748 unsigned long user_call_ID)
749{
750 struct afs_call *call = (struct afs_call *)user_call_ID;
751
David Howells00e90712016-09-08 11:10:12 +0100752 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000753 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100754}
755
David Howells08e0e7c2007-04-26 15:55:03 -0700756/*
David Howellsd0016482016-08-30 20:42:14 +0100757 * Notification of an incoming call.
758 */
David Howells00e90712016-09-08 11:10:12 +0100759static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
760 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100761{
David Howellsf044c882017-11-02 15:27:45 +0000762 struct afs_net *net = afs_sock2net(sk);
763
764 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100765}
766
767/*
David Howells372ee162016-08-03 14:11:40 +0100768 * Grab the operation ID from an incoming cache manager call. The socket
769 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700770 */
David Howellsd0016482016-08-30 20:42:14 +0100771static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700772{
David Howellsd0016482016-08-30 20:42:14 +0100773 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700774
David Howellsd0016482016-08-30 20:42:14 +0100775 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700776
777 ASSERTCMP(call->offset, <, 4);
778
779 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100780 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100781 if (ret < 0)
782 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700783
David Howells50a2c952016-10-13 08:27:10 +0100784 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000785 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howellsd0016482016-08-30 20:42:14 +0100786 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700787
788 /* ask the cache manager to route the call (it'll change the call type
789 * if successful) */
790 if (!afs_cm_incoming_call(call))
791 return -ENOTSUPP;
792
David Howells8e8d7f12017-01-05 10:38:34 +0000793 trace_afs_cb_call(call);
794
David Howells08e0e7c2007-04-26 15:55:03 -0700795 /* pass responsibility for the remainer of this message off to the
796 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100797 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700798}
799
800/*
David Howellse8332512017-08-29 10:18:56 +0100801 * Advance the AFS call state when an RxRPC service call ends the transmit
802 * phase.
803 */
804static void afs_notify_end_reply_tx(struct sock *sock,
805 struct rxrpc_call *rxcall,
806 unsigned long call_user_ID)
807{
808 struct afs_call *call = (struct afs_call *)call_user_ID;
809
David Howells98bf40c2017-11-02 15:27:53 +0000810 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100811}
812
813/*
David Howells08e0e7c2007-04-26 15:55:03 -0700814 * send an empty reply
815 */
816void afs_send_empty_reply(struct afs_call *call)
817{
David Howellsf044c882017-11-02 15:27:45 +0000818 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700819 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700820
821 _enter("");
822
David Howellsf044c882017-11-02 15:27:45 +0000823 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100824
David Howells08e0e7c2007-04-26 15:55:03 -0700825 msg.msg_name = NULL;
826 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100827 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700828 msg.msg_control = NULL;
829 msg.msg_controllen = 0;
830 msg.msg_flags = 0;
831
David Howellsf044c882017-11-02 15:27:45 +0000832 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100833 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700834 case 0:
835 _leave(" [replied]");
836 return;
837
838 case -ENOMEM:
839 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000840 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100841 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700842 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700843 _leave(" [error]");
844 return;
845 }
846}
847
848/*
David Howellsb908fe62007-04-26 15:58:17 -0700849 * send a simple reply
850 */
851void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
852{
David Howellsf044c882017-11-02 15:27:45 +0000853 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700854 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500855 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100856 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700857
858 _enter("");
859
David Howellsf044c882017-11-02 15:27:45 +0000860 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100861
David Howellsb908fe62007-04-26 15:58:17 -0700862 iov[0].iov_base = (void *) buf;
863 iov[0].iov_len = len;
864 msg.msg_name = NULL;
865 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100866 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700867 msg.msg_control = NULL;
868 msg.msg_controllen = 0;
869 msg.msg_flags = 0;
870
David Howellsf044c882017-11-02 15:27:45 +0000871 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100872 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100873 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100874 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700875 _leave(" [replied]");
876 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100877 }
David Howells6c67c7c2014-05-21 14:48:05 +0100878
David Howellsbd6dc742007-07-20 10:59:41 +0100879 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700880 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000881 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100882 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700883 }
David Howellsbd6dc742007-07-20 10:59:41 +0100884 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700885}
886
887/*
David Howells372ee162016-08-03 14:11:40 +0100888 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700889 */
David Howellsd0016482016-08-30 20:42:14 +0100890int afs_extract_data(struct afs_call *call, void *buf, size_t count,
891 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700892{
David Howellsf044c882017-11-02 15:27:45 +0000893 struct afs_net *net = call->net;
David Howellseb9950e2018-08-03 17:06:56 +0100894 struct iov_iter iter;
895 struct kvec iov;
David Howells98bf40c2017-11-02 15:27:53 +0000896 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000897 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100898 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700899
David Howellsd0016482016-08-30 20:42:14 +0100900 _enter("{%s,%zu},,%zu,%d",
901 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700902
David Howellsd0016482016-08-30 20:42:14 +0100903 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700904
David Howellseb9950e2018-08-03 17:06:56 +0100905 iov.iov_base = buf + call->offset;
906 iov.iov_len = count - call->offset;
David Howellsaa563d72018-10-20 00:57:56 +0100907 iov_iter_kvec(&iter, READ, &iov, 1, count - call->offset);
David Howellseb9950e2018-08-03 17:06:56 +0100908
909 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, &iter,
David Howells98bf40c2017-11-02 15:27:53 +0000910 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100911 &call->service_id);
David Howellseb9950e2018-08-03 17:06:56 +0100912 call->offset += (count - call->offset) - iov_iter_count(&iter);
David Howells8e8d7f12017-01-05 10:38:34 +0000913 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100914 if (ret == 0 || ret == -EAGAIN)
915 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700916
David Howells98bf40c2017-11-02 15:27:53 +0000917 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100918 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000919 switch (state) {
920 case AFS_CALL_CL_AWAIT_REPLY:
921 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100922 break;
David Howells98bf40c2017-11-02 15:27:53 +0000923 case AFS_CALL_SV_AWAIT_REQUEST:
924 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100925 break;
David Howells98bf40c2017-11-02 15:27:53 +0000926 case AFS_CALL_COMPLETE:
927 kdebug("prem complete %d", call->error);
928 return -EIO;
David Howellsd0016482016-08-30 20:42:14 +0100929 default:
930 break;
931 }
932 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700933 }
David Howellsd0016482016-08-30 20:42:14 +0100934
David Howells98bf40c2017-11-02 15:27:53 +0000935 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100936 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700937}
David Howells5f702c82018-04-06 14:17:25 +0100938
939/*
940 * Log protocol error production.
941 */
942noinline int afs_protocol_error(struct afs_call *call, int error)
943{
944 trace_afs_protocol_error(call, error, __builtin_return_address(0));
945 return error;
946}