blob: 1f6235a6e9ae8f1aa61287f7215236316efeb34e [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;
44 int ret;
45
46 _enter("");
47
David Howells3838d3e2017-11-02 15:27:47 +000048 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010049 if (ret < 0)
50 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070051
52 socket->sk->sk_allocation = GFP_NOFS;
53
54 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000055 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070056 srx.srx_family = AF_RXRPC;
57 srx.srx_service = CM_SERVICE;
58 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000059 srx.transport_len = sizeof(srx.transport.sin6);
60 srx.transport.sin6.sin6_family = AF_INET6;
61 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
David Howells08e0e7c2007-04-26 15:55:03 -070062
63 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000064 if (ret == -EADDRINUSE) {
65 srx.transport.sin6.sin6_port = 0;
66 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
67 }
David Howells0e119b42016-06-10 22:30:37 +010068 if (ret < 0)
69 goto error_2;
70
David Howells00e90712016-09-08 11:10:12 +010071 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
72 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010073
David Howells0e119b42016-06-10 22:30:37 +010074 ret = kernel_listen(socket, INT_MAX);
75 if (ret < 0)
76 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070077
David Howellsf044c882017-11-02 15:27:45 +000078 net->socket = socket;
79 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -070080 _leave(" = 0");
81 return 0;
David Howells0e119b42016-06-10 22:30:37 +010082
83error_2:
84 sock_release(socket);
85error_1:
David Howells0e119b42016-06-10 22:30:37 +010086 _leave(" = %d", ret);
87 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -070088}
89
90/*
91 * close the RxRPC socket AFS was using
92 */
David Howellsf044c882017-11-02 15:27:45 +000093void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070094{
95 _enter("");
96
David Howellsf044c882017-11-02 15:27:45 +000097 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +000098 flush_workqueue(afs_async_calls);
99
David Howellsf044c882017-11-02 15:27:45 +0000100 if (net->spare_incoming_call) {
101 afs_put_call(net->spare_incoming_call);
102 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100103 }
104
David Howellsf044c882017-11-02 15:27:45 +0000105 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100106 wait_var_event(&net->nr_outstanding_calls,
107 !atomic_read(&net->nr_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100108 _debug("no outstanding calls");
109
David Howellsf044c882017-11-02 15:27:45 +0000110 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100111 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000112 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700113
114 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700115 _leave("");
116}
117
118/*
David Howells341f7412017-01-05 10:38:36 +0000119 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700120 */
David Howellsf044c882017-11-02 15:27:45 +0000121static struct afs_call *afs_alloc_call(struct afs_net *net,
122 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000123 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700124{
David Howells341f7412017-01-05 10:38:36 +0000125 struct afs_call *call;
126 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700127
David Howells341f7412017-01-05 10:38:36 +0000128 call = kzalloc(sizeof(*call), gfp);
129 if (!call)
130 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700131
David Howells341f7412017-01-05 10:38:36 +0000132 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000133 call->net = net;
David Howellsa25e21f2018-03-27 23:03:00 +0100134 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells341f7412017-01-05 10:38:36 +0000135 atomic_set(&call->usage, 1);
136 INIT_WORK(&call->async_work, afs_process_async_call);
137 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000138 spin_lock_init(&call->state_lock);
David Howells2f02f7a2016-04-07 17:23:03 +0100139
David Howellsf044c882017-11-02 15:27:45 +0000140 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000141 trace_afs_call(call, afs_call_trace_alloc, 1, o,
142 __builtin_return_address(0));
143 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700144}
145
146/*
David Howells341f7412017-01-05 10:38:36 +0000147 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100148 */
David Howells341f7412017-01-05 10:38:36 +0000149void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100150{
David Howellsf044c882017-11-02 15:27:45 +0000151 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000152 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000153 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000154
155 trace_afs_call(call, afs_call_trace_put, n + 1, o,
156 __builtin_return_address(0));
157
158 ASSERTCMP(n, >=, 0);
159 if (n == 0) {
160 ASSERT(!work_pending(&call->async_work));
161 ASSERT(call->type->name != NULL);
162
163 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000164 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000165 call->rxcall = NULL;
166 }
167 if (call->type->destructor)
168 call->type->destructor(call);
169
David Howellsd0676a12017-11-02 15:27:49 +0000170 afs_put_server(call->net, call->cm_server);
David Howellsd2ddc772017-11-02 15:27:50 +0000171 afs_put_cb_interest(call->net, call->cbi);
David Howells341f7412017-01-05 10:38:36 +0000172 kfree(call->request);
David Howellsa25e21f2018-03-27 23:03:00 +0100173
174 trace_afs_call(call, afs_call_trace_free, 0, o,
175 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000176 kfree(call);
177
David Howellsf044c882017-11-02 15:27:45 +0000178 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000179 if (o == 0)
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100180 wake_up_var(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100181 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100182}
183
184/*
David Howells341f7412017-01-05 10:38:36 +0000185 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100186 */
David Howells341f7412017-01-05 10:38:36 +0000187int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100188{
David Howells341f7412017-01-05 10:38:36 +0000189 int u = atomic_inc_return(&call->usage);
190
191 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000192 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000193 __builtin_return_address(0));
194
195 INIT_WORK(&call->work, call->type->work);
196
197 if (!queue_work(afs_wq, &call->work))
198 afs_put_call(call);
199 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100200}
201
202/*
David Howells08e0e7c2007-04-26 15:55:03 -0700203 * allocate a call with flat request and reply buffers
204 */
David Howellsf044c882017-11-02 15:27:45 +0000205struct afs_call *afs_alloc_flat_call(struct afs_net *net,
206 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100207 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700208{
209 struct afs_call *call;
210
David Howellsf044c882017-11-02 15:27:45 +0000211 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700212 if (!call)
213 goto nomem_call;
214
David Howells00d3b7a2007-04-26 15:57:07 -0700215 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000216 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700217 call->request = kmalloc(request_size, GFP_NOFS);
218 if (!call->request)
219 goto nomem_free;
220 }
221
David Howellsd0016482016-08-30 20:42:14 +0100222 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000223 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100224 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700225 if (!call->buffer)
226 goto nomem_free;
227 }
228
David Howells025db802017-11-02 15:27:51 +0000229 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700230 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700231 return call;
232
David Howells00d3b7a2007-04-26 15:57:07 -0700233nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000234 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700235nomem_call:
236 return NULL;
237}
238
239/*
240 * clean up a call with flat buffer
241 */
242void afs_flat_call_destructor(struct afs_call *call)
243{
244 _enter("");
245
246 kfree(call->request);
247 call->request = NULL;
248 kfree(call->buffer);
249 call->buffer = NULL;
250}
251
David Howells2f5705a2017-03-16 16:27:46 +0000252#define AFS_BVEC_MAX 8
253
254/*
255 * Load the given bvec with the next few pages.
256 */
257static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
258 struct bio_vec *bv, pgoff_t first, pgoff_t last,
259 unsigned offset)
260{
261 struct page *pages[AFS_BVEC_MAX];
262 unsigned int nr, n, i, to, bytes = 0;
263
264 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
265 n = find_get_pages_contig(call->mapping, first, nr, pages);
266 ASSERTCMP(n, ==, nr);
267
268 msg->msg_flags |= MSG_MORE;
269 for (i = 0; i < nr; i++) {
270 to = PAGE_SIZE;
271 if (first + i >= last) {
272 to = call->last_to;
273 msg->msg_flags &= ~MSG_MORE;
274 }
275 bv[i].bv_page = pages[i];
276 bv[i].bv_len = to - offset;
277 bv[i].bv_offset = offset;
278 bytes += to - offset;
279 offset = 0;
280 }
281
282 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
283}
284
David Howells08e0e7c2007-04-26 15:55:03 -0700285/*
David Howellse8332512017-08-29 10:18:56 +0100286 * Advance the AFS call state when the RxRPC call ends the transmit phase.
287 */
288static void afs_notify_end_request_tx(struct sock *sock,
289 struct rxrpc_call *rxcall,
290 unsigned long call_user_ID)
291{
292 struct afs_call *call = (struct afs_call *)call_user_ID;
293
David Howells98bf40c2017-11-02 15:27:53 +0000294 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100295}
296
297/*
David Howells31143d52007-05-09 02:33:46 -0700298 * attach the data from a bunch of pages on an inode to a call
299 */
Al Viro39c6ace2016-01-09 20:36:51 -0500300static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700301{
David Howells2f5705a2017-03-16 16:27:46 +0000302 struct bio_vec bv[AFS_BVEC_MAX];
303 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700304 pgoff_t first = call->first, last = call->last;
305 int ret;
306
David Howells31143d52007-05-09 02:33:46 -0700307 offset = call->first_offset;
308 call->first_offset = 0;
309
310 do {
David Howells2f5705a2017-03-16 16:27:46 +0000311 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000312 trace_afs_send_pages(call, msg, first, last, offset);
313
David Howells2f5705a2017-03-16 16:27:46 +0000314 offset = 0;
315 bytes = msg->msg_iter.count;
316 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700317
David Howellsf044c882017-11-02 15:27:45 +0000318 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100319 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000320 for (loop = 0; loop < nr; loop++)
321 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700322 if (ret < 0)
323 break;
David Howells2f5705a2017-03-16 16:27:46 +0000324
325 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700326 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700327
David Howells2c099012017-11-02 15:27:51 +0000328 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700329 return ret;
330}
331
332/*
David Howells08e0e7c2007-04-26 15:55:03 -0700333 * initiate a call
334 */
David Howells8b2a4642017-11-02 15:27:50 +0000335long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000336 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700337{
David Howells8b2a4642017-11-02 15:27:50 +0000338 struct sockaddr_rxrpc *srx = ac->addr;
David Howells08e0e7c2007-04-26 15:55:03 -0700339 struct rxrpc_call *rxcall;
340 struct msghdr msg;
341 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000342 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100343 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700344 int ret;
345
David Howells4d9df982017-11-02 15:27:47 +0000346 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700347
David Howells00d3b7a2007-04-26 15:57:07 -0700348 ASSERT(call->type != NULL);
349 ASSERT(call->type->name != NULL);
350
David Howells31143d52007-05-09 02:33:46 -0700351 _debug("____MAKE %p{%s,%x} [%d]____",
352 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000353 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700354
David Howells56ff9c82017-01-05 10:38:36 +0000355 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700356
David Howellse754eba2017-06-07 12:40:03 +0100357 /* Work out the length we're going to transmit. This is awkward for
358 * calls such as FS.StoreData where there's an extra injection of data
359 * after the initial fixed part.
360 */
361 tx_total_len = call->request_size;
362 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000363 if (call->last == call->first) {
364 tx_total_len += call->last_to - call->first_offset;
365 } else {
366 /* It looks mathematically like you should be able to
367 * combine the following lines with the ones above, but
368 * unsigned arithmetic is fun when it wraps...
369 */
370 tx_total_len += PAGE_SIZE - call->first_offset;
371 tx_total_len += call->last_to;
372 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
373 }
David Howellse754eba2017-06-07 12:40:03 +0100374 }
375
David Howells08e0e7c2007-04-26 15:55:03 -0700376 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000377 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100378 (unsigned long)call,
379 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000380 (async ?
381 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100382 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100383 call->upgrade,
384 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700385 if (IS_ERR(rxcall)) {
386 ret = PTR_ERR(rxcall);
387 goto error_kill_call;
388 }
389
390 call->rxcall = rxcall;
391
392 /* send the request */
393 iov[0].iov_base = call->request;
394 iov[0].iov_len = call->request_size;
395
396 msg.msg_name = NULL;
397 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500398 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500399 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700400 msg.msg_control = NULL;
401 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100402 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700403
David Howellsf044c882017-11-02 15:27:45 +0000404 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100405 &msg, call->request_size,
406 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700407 if (ret < 0)
408 goto error_do_abort;
409
David Howells31143d52007-05-09 02:33:46 -0700410 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500411 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700412 if (ret < 0)
413 goto error_do_abort;
414 }
415
David Howells08e0e7c2007-04-26 15:55:03 -0700416 /* at this point, an async call may no longer exist as it may have
417 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000418 if (call->async)
419 return -EINPROGRESS;
420
David Howellsd2ddc772017-11-02 15:27:50 +0000421 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700422
423error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000424 call->state = AFS_CALL_COMPLETE;
425 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000426 rxrpc_kernel_abort_call(call->net->socket, rxcall,
427 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000428 } else {
David Howells70af0e32017-03-16 16:27:47 +0000429 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000430 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000431 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000432 &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000433 ac->abort_code = call->abort_code;
434 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000435 }
David Howells025db802017-11-02 15:27:51 +0000436 call->error = ret;
437 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700438error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000439 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000440 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700441 _leave(" = %d", ret);
442 return ret;
443}
444
445/*
David Howells08e0e7c2007-04-26 15:55:03 -0700446 * deliver messages to a call
447 */
448static void afs_deliver_to_call(struct afs_call *call)
449{
David Howells98bf40c2017-11-02 15:27:53 +0000450 enum afs_call_state state;
451 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700452 int ret;
453
David Howellsd0016482016-08-30 20:42:14 +0100454 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700455
David Howells98bf40c2017-11-02 15:27:53 +0000456 while (state = READ_ONCE(call->state),
457 state == AFS_CALL_CL_AWAIT_REPLY ||
458 state == AFS_CALL_SV_AWAIT_OP_ID ||
459 state == AFS_CALL_SV_AWAIT_REQUEST ||
460 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100461 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000462 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howellsd0016482016-08-30 20:42:14 +0100463 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000464 ret = rxrpc_kernel_recv_data(call->net->socket,
465 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100466 NULL, 0, &offset, false,
David Howells98bf40c2017-11-02 15:27:53 +0000467 &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100468 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000469 trace_afs_recv_data(call, 0, offset, false, ret);
470
David Howellsd0016482016-08-30 20:42:14 +0100471 if (ret == -EINPROGRESS || ret == -EAGAIN)
472 return;
David Howells98bf40c2017-11-02 15:27:53 +0000473 if (ret < 0 || ret == 1) {
474 if (ret == 1)
475 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000476 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000477 }
David Howellsd0016482016-08-30 20:42:14 +0100478 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700479 }
480
David Howellsd0016482016-08-30 20:42:14 +0100481 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000482 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100483 switch (ret) {
484 case 0:
David Howellsf2686b02018-05-10 14:12:50 +0100485 if (state == AFS_CALL_CL_PROC_REPLY) {
486 if (call->cbi)
487 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
488 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000489 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100490 }
David Howells98bf40c2017-11-02 15:27:53 +0000491 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100492 goto done;
493 case -EINPROGRESS:
494 case -EAGAIN:
495 goto out;
David Howells98bf40c2017-11-02 15:27:53 +0000496 case -EIO:
David Howells70af0e32017-03-16 16:27:47 +0000497 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000498 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
499 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100500 case -ENOTCONN:
501 abort_code = RX_CALL_DEAD;
David Howellsf044c882017-11-02 15:27:45 +0000502 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100503 abort_code, ret, "KNC");
David Howells98bf40c2017-11-02 15:27:53 +0000504 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100505 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000506 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000507 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100508 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000509 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100510 case -ENODATA:
511 case -EBADMSG:
512 case -EMSGSIZE:
513 default:
514 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000515 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100516 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000517 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100518 abort_code, -EBADMSG, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000519 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100520 }
David Howells08e0e7c2007-04-26 15:55:03 -0700521 }
522
David Howellsd0016482016-08-30 20:42:14 +0100523done:
David Howells98bf40c2017-11-02 15:27:53 +0000524 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000525 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100526out:
David Howells08e0e7c2007-04-26 15:55:03 -0700527 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100528 return;
529
David Howells98bf40c2017-11-02 15:27:53 +0000530local_abort:
531 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000532call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000533 afs_set_call_complete(call, ret, remote_abort);
534 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100535 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700536}
537
538/*
539 * wait synchronously for a call to complete
540 */
David Howellsd2ddc772017-11-02 15:27:50 +0000541static long afs_wait_for_call_to_complete(struct afs_call *call,
542 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700543{
David Howellsbc5e3a52017-10-18 11:07:31 +0100544 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000545 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100546 u64 rtt;
547 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700548
549 DECLARE_WAITQUEUE(myself, current);
550
551 _enter("");
552
David Howellsf044c882017-11-02 15:27:45 +0000553 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100554 rtt2 = nsecs_to_jiffies64(rtt) * 2;
555 if (rtt2 < 2)
556 rtt2 = 2;
557
558 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000559 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100560
David Howells08e0e7c2007-04-26 15:55:03 -0700561 add_wait_queue(&call->waitq, &myself);
562 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100563 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700564
565 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000566 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
567 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100568 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700569 __set_current_state(TASK_RUNNING);
570 afs_deliver_to_call(call);
571 continue;
572 }
573
David Howells98bf40c2017-11-02 15:27:53 +0000574 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700575 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100576
David Howellsf044c882017-11-02 15:27:45 +0000577 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100578 if (timeout == 0 &&
579 life == last_life && signal_pending(current))
580 break;
581
582 if (life != last_life) {
583 timeout = rtt2;
584 last_life = life;
585 }
586
587 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700588 }
589
590 remove_wait_queue(&call->waitq, &myself);
591 __set_current_state(TASK_RUNNING);
592
David Howells954cd6d2017-03-16 16:27:49 +0000593 /* Kill off the call if it's still live. */
David Howells98bf40c2017-11-02 15:27:53 +0000594 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Howells954cd6d2017-03-16 16:27:49 +0000595 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000596 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells98bf40c2017-11-02 15:27:53 +0000597 RX_USER_ABORT, -EINTR, "KWI"))
598 afs_set_call_complete(call, -EINTR, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700599 }
600
David Howells98bf40c2017-11-02 15:27:53 +0000601 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000602 ac->abort_code = call->abort_code;
603 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000604 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000605
606 ret = ac->error;
607 switch (ret) {
608 case 0:
609 if (call->ret_reply0) {
610 ret = (long)call->reply[0];
611 call->reply[0] = NULL;
612 }
613 /* Fall through */
614 case -ECONNABORTED:
615 ac->responded = true;
616 break;
David Howells33cd7f22017-11-02 15:27:48 +0000617 }
618
David Howells08e0e7c2007-04-26 15:55:03 -0700619 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000620 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000621 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700622 return ret;
623}
624
625/*
626 * wake up a waiting call
627 */
David Howellsd0016482016-08-30 20:42:14 +0100628static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
629 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700630{
David Howellsd0016482016-08-30 20:42:14 +0100631 struct afs_call *call = (struct afs_call *)call_user_ID;
632
633 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700634 wake_up(&call->waitq);
635}
636
637/*
638 * wake up an asynchronous call
639 */
David Howellsd0016482016-08-30 20:42:14 +0100640static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
641 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700642{
David Howellsd0016482016-08-30 20:42:14 +0100643 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000644 int u;
David Howellsd0016482016-08-30 20:42:14 +0100645
David Howells8e8d7f12017-01-05 10:38:34 +0000646 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100647 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000648
649 u = __atomic_add_unless(&call->usage, 1, 0);
650 if (u != 0) {
651 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000652 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000653 __builtin_return_address(0));
654
655 if (!queue_work(afs_async_calls, &call->async_work))
656 afs_put_call(call);
657 }
David Howells08e0e7c2007-04-26 15:55:03 -0700658}
659
660/*
David Howells341f7412017-01-05 10:38:36 +0000661 * Delete an asynchronous call. The work item carries a ref to the call struct
662 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700663 */
David Howellsd0016482016-08-30 20:42:14 +0100664static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700665{
David Howellsd0016482016-08-30 20:42:14 +0100666 struct afs_call *call = container_of(work, struct afs_call, async_work);
667
David Howells08e0e7c2007-04-26 15:55:03 -0700668 _enter("");
669
David Howells341f7412017-01-05 10:38:36 +0000670 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700671
672 _leave("");
673}
674
675/*
David Howells341f7412017-01-05 10:38:36 +0000676 * Perform I/O processing on an asynchronous call. The work item carries a ref
677 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700678 */
David Howellsd0016482016-08-30 20:42:14 +0100679static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700680{
David Howellsd0016482016-08-30 20:42:14 +0100681 struct afs_call *call = container_of(work, struct afs_call, async_work);
682
David Howells08e0e7c2007-04-26 15:55:03 -0700683 _enter("");
684
David Howellsd0016482016-08-30 20:42:14 +0100685 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
686 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700687 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100688 }
David Howells08e0e7c2007-04-26 15:55:03 -0700689
David Howells56ff9c82017-01-05 10:38:36 +0000690 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000691 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700692
David Howells341f7412017-01-05 10:38:36 +0000693 /* We have two refs to release - one from the alloc and one
694 * queued with the work item - and we can't just deallocate the
695 * call because the work item may be queued again.
696 */
David Howellsd0016482016-08-30 20:42:14 +0100697 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000698 if (!queue_work(afs_async_calls, &call->async_work))
699 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700700 }
701
David Howells341f7412017-01-05 10:38:36 +0000702 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700703 _leave("");
704}
705
David Howells00e90712016-09-08 11:10:12 +0100706static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
707{
708 struct afs_call *call = (struct afs_call *)user_call_ID;
709
710 call->rxcall = rxcall;
711}
712
713/*
714 * Charge the incoming call preallocation.
715 */
David Howellsf044c882017-11-02 15:27:45 +0000716void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100717{
David Howellsf044c882017-11-02 15:27:45 +0000718 struct afs_net *net =
719 container_of(work, struct afs_net, charge_preallocation_work);
720 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100721
722 for (;;) {
723 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000724 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100725 if (!call)
726 break;
727
David Howells56ff9c82017-01-05 10:38:36 +0000728 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000729 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000730 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100731 }
732
David Howellsf044c882017-11-02 15:27:45 +0000733 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100734 afs_wake_up_async_call,
735 afs_rx_attach,
736 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100737 GFP_KERNEL,
738 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100739 break;
740 call = NULL;
741 }
David Howellsf044c882017-11-02 15:27:45 +0000742 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100743}
744
745/*
746 * Discard a preallocated call when a socket is shut down.
747 */
748static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
749 unsigned long user_call_ID)
750{
751 struct afs_call *call = (struct afs_call *)user_call_ID;
752
David Howells00e90712016-09-08 11:10:12 +0100753 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000754 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100755}
756
David Howells08e0e7c2007-04-26 15:55:03 -0700757/*
David Howellsd0016482016-08-30 20:42:14 +0100758 * Notification of an incoming call.
759 */
David Howells00e90712016-09-08 11:10:12 +0100760static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
761 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100762{
David Howellsf044c882017-11-02 15:27:45 +0000763 struct afs_net *net = afs_sock2net(sk);
764
765 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100766}
767
768/*
David Howells372ee162016-08-03 14:11:40 +0100769 * Grab the operation ID from an incoming cache manager call. The socket
770 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700771 */
David Howellsd0016482016-08-30 20:42:14 +0100772static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700773{
David Howellsd0016482016-08-30 20:42:14 +0100774 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700775
David Howellsd0016482016-08-30 20:42:14 +0100776 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700777
778 ASSERTCMP(call->offset, <, 4);
779
780 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100781 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100782 if (ret < 0)
783 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700784
David Howells50a2c952016-10-13 08:27:10 +0100785 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000786 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howellsd0016482016-08-30 20:42:14 +0100787 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700788
789 /* ask the cache manager to route the call (it'll change the call type
790 * if successful) */
791 if (!afs_cm_incoming_call(call))
792 return -ENOTSUPP;
793
David Howells8e8d7f12017-01-05 10:38:34 +0000794 trace_afs_cb_call(call);
795
David Howells08e0e7c2007-04-26 15:55:03 -0700796 /* pass responsibility for the remainer of this message off to the
797 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100798 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700799}
800
801/*
David Howellse8332512017-08-29 10:18:56 +0100802 * Advance the AFS call state when an RxRPC service call ends the transmit
803 * phase.
804 */
805static void afs_notify_end_reply_tx(struct sock *sock,
806 struct rxrpc_call *rxcall,
807 unsigned long call_user_ID)
808{
809 struct afs_call *call = (struct afs_call *)call_user_ID;
810
David Howells98bf40c2017-11-02 15:27:53 +0000811 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100812}
813
814/*
David Howells08e0e7c2007-04-26 15:55:03 -0700815 * send an empty reply
816 */
817void afs_send_empty_reply(struct afs_call *call)
818{
David Howellsf044c882017-11-02 15:27:45 +0000819 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700820 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700821
822 _enter("");
823
David Howellsf044c882017-11-02 15:27:45 +0000824 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100825
David Howells08e0e7c2007-04-26 15:55:03 -0700826 msg.msg_name = NULL;
827 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100828 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700829 msg.msg_control = NULL;
830 msg.msg_controllen = 0;
831 msg.msg_flags = 0;
832
David Howellsf044c882017-11-02 15:27:45 +0000833 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100834 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700835 case 0:
836 _leave(" [replied]");
837 return;
838
839 case -ENOMEM:
840 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000841 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100842 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700843 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700844 _leave(" [error]");
845 return;
846 }
847}
848
849/*
David Howellsb908fe62007-04-26 15:58:17 -0700850 * send a simple reply
851 */
852void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
853{
David Howellsf044c882017-11-02 15:27:45 +0000854 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700855 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500856 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100857 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700858
859 _enter("");
860
David Howellsf044c882017-11-02 15:27:45 +0000861 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100862
David Howellsb908fe62007-04-26 15:58:17 -0700863 iov[0].iov_base = (void *) buf;
864 iov[0].iov_len = len;
865 msg.msg_name = NULL;
866 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500867 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700868 msg.msg_control = NULL;
869 msg.msg_controllen = 0;
870 msg.msg_flags = 0;
871
David Howellsf044c882017-11-02 15:27:45 +0000872 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100873 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100874 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100875 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700876 _leave(" [replied]");
877 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100878 }
David Howells6c67c7c2014-05-21 14:48:05 +0100879
David Howellsbd6dc742007-07-20 10:59:41 +0100880 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700881 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000882 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100883 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700884 }
David Howellsbd6dc742007-07-20 10:59:41 +0100885 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700886}
887
888/*
David Howells372ee162016-08-03 14:11:40 +0100889 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700890 */
David Howellsd0016482016-08-30 20:42:14 +0100891int afs_extract_data(struct afs_call *call, void *buf, size_t count,
892 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700893{
David Howellsf044c882017-11-02 15:27:45 +0000894 struct afs_net *net = call->net;
David Howells98bf40c2017-11-02 15:27:53 +0000895 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000896 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100897 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700898
David Howellsd0016482016-08-30 20:42:14 +0100899 _enter("{%s,%zu},,%zu,%d",
900 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700901
David Howellsd0016482016-08-30 20:42:14 +0100902 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700903
David Howellsf044c882017-11-02 15:27:45 +0000904 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100905 buf, count, &call->offset,
David Howells98bf40c2017-11-02 15:27:53 +0000906 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100907 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000908 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100909 if (ret == 0 || ret == -EAGAIN)
910 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700911
David Howells98bf40c2017-11-02 15:27:53 +0000912 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100913 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000914 switch (state) {
915 case AFS_CALL_CL_AWAIT_REPLY:
916 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100917 break;
David Howells98bf40c2017-11-02 15:27:53 +0000918 case AFS_CALL_SV_AWAIT_REQUEST:
919 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100920 break;
David Howells98bf40c2017-11-02 15:27:53 +0000921 case AFS_CALL_COMPLETE:
922 kdebug("prem complete %d", call->error);
923 return -EIO;
David Howellsd0016482016-08-30 20:42:14 +0100924 default:
925 break;
926 }
927 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700928 }
David Howellsd0016482016-08-30 20:42:14 +0100929
David Howells98bf40c2017-11-02 15:27:53 +0000930 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100931 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700932}
David Howells5f702c82018-04-06 14:17:25 +0100933
934/*
935 * Log protocol error production.
936 */
937noinline int afs_protocol_error(struct afs_call *call, int error)
938{
939 trace_afs_protocol_error(call, error, __builtin_return_address(0));
940 return error;
941}