blob: d57f09b5d27b9f0429e67d9723060a37850ba8f9 [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 Howells33cd7f22017-11-02 15:27:48 +000023static long afs_wait_for_call_to_complete(struct afs_call *);
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));
David Howells0e119b42016-06-10 22:30:37 +010064 if (ret < 0)
65 goto error_2;
66
David Howells00e90712016-09-08 11:10:12 +010067 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
68 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010069
David Howells0e119b42016-06-10 22:30:37 +010070 ret = kernel_listen(socket, INT_MAX);
71 if (ret < 0)
72 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070073
David Howellsf044c882017-11-02 15:27:45 +000074 net->socket = socket;
75 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -070076 _leave(" = 0");
77 return 0;
David Howells0e119b42016-06-10 22:30:37 +010078
79error_2:
80 sock_release(socket);
81error_1:
David Howells0e119b42016-06-10 22:30:37 +010082 _leave(" = %d", ret);
83 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -070084}
85
86/*
87 * close the RxRPC socket AFS was using
88 */
David Howellsf044c882017-11-02 15:27:45 +000089void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070090{
91 _enter("");
92
David Howellsf044c882017-11-02 15:27:45 +000093 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +000094 flush_workqueue(afs_async_calls);
95
David Howellsf044c882017-11-02 15:27:45 +000096 if (net->spare_incoming_call) {
97 afs_put_call(net->spare_incoming_call);
98 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +010099 }
100
David Howellsf044c882017-11-02 15:27:45 +0000101 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
102 wait_on_atomic_t(&net->nr_outstanding_calls, atomic_t_wait,
David Howells2f02f7a2016-04-07 17:23:03 +0100103 TASK_UNINTERRUPTIBLE);
104 _debug("no outstanding calls");
105
David Howellsf044c882017-11-02 15:27:45 +0000106 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100107 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000108 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700109
110 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700111 _leave("");
112}
113
114/*
David Howells341f7412017-01-05 10:38:36 +0000115 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700116 */
David Howellsf044c882017-11-02 15:27:45 +0000117static struct afs_call *afs_alloc_call(struct afs_net *net,
118 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000119 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700120{
David Howells341f7412017-01-05 10:38:36 +0000121 struct afs_call *call;
122 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700123
David Howells341f7412017-01-05 10:38:36 +0000124 call = kzalloc(sizeof(*call), gfp);
125 if (!call)
126 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700127
David Howells341f7412017-01-05 10:38:36 +0000128 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000129 call->net = net;
David Howells341f7412017-01-05 10:38:36 +0000130 atomic_set(&call->usage, 1);
131 INIT_WORK(&call->async_work, afs_process_async_call);
132 init_waitqueue_head(&call->waitq);
David Howells2f02f7a2016-04-07 17:23:03 +0100133
David Howellsf044c882017-11-02 15:27:45 +0000134 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000135 trace_afs_call(call, afs_call_trace_alloc, 1, o,
136 __builtin_return_address(0));
137 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700138}
139
140/*
David Howells341f7412017-01-05 10:38:36 +0000141 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100142 */
David Howells341f7412017-01-05 10:38:36 +0000143void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100144{
David Howellsf044c882017-11-02 15:27:45 +0000145 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000146 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000147 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000148
149 trace_afs_call(call, afs_call_trace_put, n + 1, o,
150 __builtin_return_address(0));
151
152 ASSERTCMP(n, >=, 0);
153 if (n == 0) {
154 ASSERT(!work_pending(&call->async_work));
155 ASSERT(call->type->name != NULL);
156
157 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000158 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000159 call->rxcall = NULL;
160 }
161 if (call->type->destructor)
162 call->type->destructor(call);
163
164 kfree(call->request);
165 kfree(call);
166
David Howellsf044c882017-11-02 15:27:45 +0000167 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000168 trace_afs_call(call, afs_call_trace_free, 0, o,
169 __builtin_return_address(0));
170 if (o == 0)
David Howellsf044c882017-11-02 15:27:45 +0000171 wake_up_atomic_t(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100172 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100173}
174
175/*
David Howells341f7412017-01-05 10:38:36 +0000176 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100177 */
David Howells341f7412017-01-05 10:38:36 +0000178int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100179{
David Howells341f7412017-01-05 10:38:36 +0000180 int u = atomic_inc_return(&call->usage);
181
182 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000183 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000184 __builtin_return_address(0));
185
186 INIT_WORK(&call->work, call->type->work);
187
188 if (!queue_work(afs_wq, &call->work))
189 afs_put_call(call);
190 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100191}
192
193/*
David Howells08e0e7c2007-04-26 15:55:03 -0700194 * allocate a call with flat request and reply buffers
195 */
David Howellsf044c882017-11-02 15:27:45 +0000196struct afs_call *afs_alloc_flat_call(struct afs_net *net,
197 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100198 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700199{
200 struct afs_call *call;
201
David Howellsf044c882017-11-02 15:27:45 +0000202 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700203 if (!call)
204 goto nomem_call;
205
David Howells00d3b7a2007-04-26 15:57:07 -0700206 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000207 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700208 call->request = kmalloc(request_size, GFP_NOFS);
209 if (!call->request)
210 goto nomem_free;
211 }
212
David Howellsd0016482016-08-30 20:42:14 +0100213 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000214 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100215 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700216 if (!call->buffer)
217 goto nomem_free;
218 }
219
David Howells08e0e7c2007-04-26 15:55:03 -0700220 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700221 return call;
222
David Howells00d3b7a2007-04-26 15:57:07 -0700223nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000224 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700225nomem_call:
226 return NULL;
227}
228
229/*
230 * clean up a call with flat buffer
231 */
232void afs_flat_call_destructor(struct afs_call *call)
233{
234 _enter("");
235
236 kfree(call->request);
237 call->request = NULL;
238 kfree(call->buffer);
239 call->buffer = NULL;
240}
241
David Howells2f5705a2017-03-16 16:27:46 +0000242#define AFS_BVEC_MAX 8
243
244/*
245 * Load the given bvec with the next few pages.
246 */
247static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
248 struct bio_vec *bv, pgoff_t first, pgoff_t last,
249 unsigned offset)
250{
251 struct page *pages[AFS_BVEC_MAX];
252 unsigned int nr, n, i, to, bytes = 0;
253
254 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
255 n = find_get_pages_contig(call->mapping, first, nr, pages);
256 ASSERTCMP(n, ==, nr);
257
258 msg->msg_flags |= MSG_MORE;
259 for (i = 0; i < nr; i++) {
260 to = PAGE_SIZE;
261 if (first + i >= last) {
262 to = call->last_to;
263 msg->msg_flags &= ~MSG_MORE;
264 }
265 bv[i].bv_page = pages[i];
266 bv[i].bv_len = to - offset;
267 bv[i].bv_offset = offset;
268 bytes += to - offset;
269 offset = 0;
270 }
271
272 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
273}
274
David Howells08e0e7c2007-04-26 15:55:03 -0700275/*
David Howellse8332512017-08-29 10:18:56 +0100276 * Advance the AFS call state when the RxRPC call ends the transmit phase.
277 */
278static void afs_notify_end_request_tx(struct sock *sock,
279 struct rxrpc_call *rxcall,
280 unsigned long call_user_ID)
281{
282 struct afs_call *call = (struct afs_call *)call_user_ID;
283
284 if (call->state == AFS_CALL_REQUESTING)
285 call->state = AFS_CALL_AWAIT_REPLY;
286}
287
288/*
David Howells31143d52007-05-09 02:33:46 -0700289 * attach the data from a bunch of pages on an inode to a call
290 */
Al Viro39c6ace2016-01-09 20:36:51 -0500291static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700292{
David Howells2f5705a2017-03-16 16:27:46 +0000293 struct bio_vec bv[AFS_BVEC_MAX];
294 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700295 pgoff_t first = call->first, last = call->last;
296 int ret;
297
David Howells31143d52007-05-09 02:33:46 -0700298 offset = call->first_offset;
299 call->first_offset = 0;
300
301 do {
David Howells2f5705a2017-03-16 16:27:46 +0000302 afs_load_bvec(call, msg, bv, first, last, offset);
303 offset = 0;
304 bytes = msg->msg_iter.count;
305 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700306
David Howellsf044c882017-11-02 15:27:45 +0000307 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100308 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000309 for (loop = 0; loop < nr; loop++)
310 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700311 if (ret < 0)
312 break;
David Howells2f5705a2017-03-16 16:27:46 +0000313
314 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700315 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700316
David Howells31143d52007-05-09 02:33:46 -0700317 return ret;
318}
319
320/*
David Howells08e0e7c2007-04-26 15:55:03 -0700321 * initiate a call
322 */
David Howells33cd7f22017-11-02 15:27:48 +0000323long afs_make_call(struct sockaddr_rxrpc *srx, struct afs_call *call,
324 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700325{
David Howells08e0e7c2007-04-26 15:55:03 -0700326 struct rxrpc_call *rxcall;
327 struct msghdr msg;
328 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000329 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100330 s64 tx_total_len;
David Howells70af0e32017-03-16 16:27:47 +0000331 u32 abort_code;
David Howells08e0e7c2007-04-26 15:55:03 -0700332 int ret;
333
David Howells4d9df982017-11-02 15:27:47 +0000334 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700335
David Howells00d3b7a2007-04-26 15:57:07 -0700336 ASSERT(call->type != NULL);
337 ASSERT(call->type->name != NULL);
338
David Howells31143d52007-05-09 02:33:46 -0700339 _debug("____MAKE %p{%s,%x} [%d]____",
340 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000341 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700342
David Howells56ff9c82017-01-05 10:38:36 +0000343 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700344
David Howellse754eba2017-06-07 12:40:03 +0100345 /* Work out the length we're going to transmit. This is awkward for
346 * calls such as FS.StoreData where there's an extra injection of data
347 * after the initial fixed part.
348 */
349 tx_total_len = call->request_size;
350 if (call->send_pages) {
351 tx_total_len += call->last_to - call->first_offset;
352 tx_total_len += (call->last - call->first) * PAGE_SIZE;
353 }
354
David Howells08e0e7c2007-04-26 15:55:03 -0700355 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000356 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100357 (unsigned long)call,
358 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000359 (async ?
360 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100361 afs_wake_up_call_waiter),
362 call->upgrade);
David Howells00d3b7a2007-04-26 15:57:07 -0700363 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700364 if (IS_ERR(rxcall)) {
365 ret = PTR_ERR(rxcall);
366 goto error_kill_call;
367 }
368
369 call->rxcall = rxcall;
370
371 /* send the request */
372 iov[0].iov_base = call->request;
373 iov[0].iov_len = call->request_size;
374
375 msg.msg_name = NULL;
376 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500377 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500378 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700379 msg.msg_control = NULL;
380 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100381 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700382
David Howells70af0e32017-03-16 16:27:47 +0000383 /* We have to change the state *before* sending the last packet as
384 * rxrpc might give us the reply before it returns from sending the
385 * request. Further, if the send fails, we may already have been given
386 * a notification and may have collected it.
387 */
David Howells31143d52007-05-09 02:33:46 -0700388 if (!call->send_pages)
389 call->state = AFS_CALL_AWAIT_REPLY;
David Howellsf044c882017-11-02 15:27:45 +0000390 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100391 &msg, call->request_size,
392 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700393 if (ret < 0)
394 goto error_do_abort;
395
David Howells31143d52007-05-09 02:33:46 -0700396 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500397 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700398 if (ret < 0)
399 goto error_do_abort;
400 }
401
David Howells08e0e7c2007-04-26 15:55:03 -0700402 /* at this point, an async call may no longer exist as it may have
403 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000404 if (call->async)
405 return -EINPROGRESS;
406
407 return afs_wait_for_call_to_complete(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700408
409error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000410 call->state = AFS_CALL_COMPLETE;
411 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000412 rxrpc_kernel_abort_call(call->net->socket, rxcall,
413 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000414 } else {
415 abort_code = 0;
416 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000417 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000418 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000419 &call->service_id);
David Howells33cd7f22017-11-02 15:27:48 +0000420 ret = afs_abort_to_error(call->abort_code);
David Howells70af0e32017-03-16 16:27:47 +0000421 }
David Howells08e0e7c2007-04-26 15:55:03 -0700422error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000423 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700424 _leave(" = %d", ret);
425 return ret;
426}
427
428/*
David Howells08e0e7c2007-04-26 15:55:03 -0700429 * deliver messages to a call
430 */
431static void afs_deliver_to_call(struct afs_call *call)
432{
David Howells08e0e7c2007-04-26 15:55:03 -0700433 u32 abort_code;
434 int ret;
435
David Howellsd0016482016-08-30 20:42:14 +0100436 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700437
David Howellsd0016482016-08-30 20:42:14 +0100438 while (call->state == AFS_CALL_AWAIT_REPLY ||
439 call->state == AFS_CALL_AWAIT_OP_ID ||
440 call->state == AFS_CALL_AWAIT_REQUEST ||
441 call->state == AFS_CALL_AWAIT_ACK
442 ) {
443 if (call->state == AFS_CALL_AWAIT_ACK) {
444 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000445 ret = rxrpc_kernel_recv_data(call->net->socket,
446 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100447 NULL, 0, &offset, false,
David Howellsa68f4a22017-10-18 11:36:39 +0100448 &call->abort_code,
449 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000450 trace_afs_recv_data(call, 0, offset, false, ret);
451
David Howellsd0016482016-08-30 20:42:14 +0100452 if (ret == -EINPROGRESS || ret == -EAGAIN)
453 return;
David Howells9008f992016-10-06 08:11:50 +0100454 if (ret == 1 || ret < 0) {
David Howellsd0016482016-08-30 20:42:14 +0100455 call->state = AFS_CALL_COMPLETE;
456 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700457 }
David Howellsd0016482016-08-30 20:42:14 +0100458 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700459 }
460
David Howellsd0016482016-08-30 20:42:14 +0100461 ret = call->type->deliver(call);
462 switch (ret) {
463 case 0:
464 if (call->state == AFS_CALL_AWAIT_REPLY)
465 call->state = AFS_CALL_COMPLETE;
466 goto done;
467 case -EINPROGRESS:
468 case -EAGAIN:
469 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000470 case -ECONNABORTED:
David Howells33cd7f22017-11-02 15:27:48 +0000471 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100472 case -ENOTCONN:
473 abort_code = RX_CALL_DEAD;
David Howellsf044c882017-11-02 15:27:45 +0000474 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100475 abort_code, ret, "KNC");
David Howells70af0e32017-03-16 16:27:47 +0000476 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100477 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000478 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000479 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100480 abort_code, ret, "KIV");
David Howells70af0e32017-03-16 16:27:47 +0000481 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100482 case -ENODATA:
483 case -EBADMSG:
484 case -EMSGSIZE:
485 default:
486 abort_code = RXGEN_CC_UNMARSHAL;
487 if (call->state != AFS_CALL_AWAIT_REPLY)
488 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000489 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100490 abort_code, -EBADMSG, "KUM");
David Howells70af0e32017-03-16 16:27:47 +0000491 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100492 }
David Howells08e0e7c2007-04-26 15:55:03 -0700493 }
494
David Howellsd0016482016-08-30 20:42:14 +0100495done:
496 if (call->state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000497 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100498out:
David Howells08e0e7c2007-04-26 15:55:03 -0700499 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100500 return;
501
David Howells70af0e32017-03-16 16:27:47 +0000502save_error:
David Howellsd0016482016-08-30 20:42:14 +0100503 call->error = ret;
504 call->state = AFS_CALL_COMPLETE;
505 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700506}
507
508/*
509 * wait synchronously for a call to complete
510 */
David Howells33cd7f22017-11-02 15:27:48 +0000511static long afs_wait_for_call_to_complete(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700512{
David Howellsbc5e3a52017-10-18 11:07:31 +0100513 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000514 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100515 u64 rtt;
516 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700517
518 DECLARE_WAITQUEUE(myself, current);
519
520 _enter("");
521
David Howellsf044c882017-11-02 15:27:45 +0000522 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100523 rtt2 = nsecs_to_jiffies64(rtt) * 2;
524 if (rtt2 < 2)
525 rtt2 = 2;
526
527 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000528 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100529
David Howells08e0e7c2007-04-26 15:55:03 -0700530 add_wait_queue(&call->waitq, &myself);
531 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100532 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700533
534 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100535 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
536 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700537 __set_current_state(TASK_RUNNING);
538 afs_deliver_to_call(call);
539 continue;
540 }
541
David Howellsbc5e3a52017-10-18 11:07:31 +0100542 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700543 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100544
David Howellsf044c882017-11-02 15:27:45 +0000545 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100546 if (timeout == 0 &&
547 life == last_life && signal_pending(current))
548 break;
549
550 if (life != last_life) {
551 timeout = rtt2;
552 last_life = life;
553 }
554
555 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700556 }
557
558 remove_wait_queue(&call->waitq, &myself);
559 __set_current_state(TASK_RUNNING);
560
David Howells954cd6d2017-03-16 16:27:49 +0000561 /* Kill off the call if it's still live. */
David Howells08e0e7c2007-04-26 15:55:03 -0700562 if (call->state < AFS_CALL_COMPLETE) {
David Howells954cd6d2017-03-16 16:27:49 +0000563 _debug("call interrupted");
David Howellsf044c882017-11-02 15:27:45 +0000564 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells954cd6d2017-03-16 16:27:49 +0000565 RX_USER_ABORT, -EINTR, "KWI");
David Howells08e0e7c2007-04-26 15:55:03 -0700566 }
567
David Howells954cd6d2017-03-16 16:27:49 +0000568 ret = call->error;
David Howells33cd7f22017-11-02 15:27:48 +0000569 if (ret < 0) {
570 ret = afs_abort_to_error(call->abort_code);
571 } else if (ret == 0 && call->ret_reply0) {
572 ret = (long)call->reply[0];
573 call->reply[0] = NULL;
574 }
575
David Howells08e0e7c2007-04-26 15:55:03 -0700576 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000577 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000578 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700579 return ret;
580}
581
582/*
583 * wake up a waiting call
584 */
David Howellsd0016482016-08-30 20:42:14 +0100585static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
586 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700587{
David Howellsd0016482016-08-30 20:42:14 +0100588 struct afs_call *call = (struct afs_call *)call_user_ID;
589
590 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700591 wake_up(&call->waitq);
592}
593
594/*
595 * wake up an asynchronous call
596 */
David Howellsd0016482016-08-30 20:42:14 +0100597static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
598 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700599{
David Howellsd0016482016-08-30 20:42:14 +0100600 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000601 int u;
David Howellsd0016482016-08-30 20:42:14 +0100602
David Howells8e8d7f12017-01-05 10:38:34 +0000603 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100604 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000605
606 u = __atomic_add_unless(&call->usage, 1, 0);
607 if (u != 0) {
608 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000609 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000610 __builtin_return_address(0));
611
612 if (!queue_work(afs_async_calls, &call->async_work))
613 afs_put_call(call);
614 }
David Howells08e0e7c2007-04-26 15:55:03 -0700615}
616
617/*
David Howells341f7412017-01-05 10:38:36 +0000618 * Delete an asynchronous call. The work item carries a ref to the call struct
619 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700620 */
David Howellsd0016482016-08-30 20:42:14 +0100621static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700622{
David Howellsd0016482016-08-30 20:42:14 +0100623 struct afs_call *call = container_of(work, struct afs_call, async_work);
624
David Howells08e0e7c2007-04-26 15:55:03 -0700625 _enter("");
626
David Howells341f7412017-01-05 10:38:36 +0000627 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700628
629 _leave("");
630}
631
632/*
David Howells341f7412017-01-05 10:38:36 +0000633 * Perform I/O processing on an asynchronous call. The work item carries a ref
634 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700635 */
David Howellsd0016482016-08-30 20:42:14 +0100636static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700637{
David Howellsd0016482016-08-30 20:42:14 +0100638 struct afs_call *call = container_of(work, struct afs_call, async_work);
639
David Howells08e0e7c2007-04-26 15:55:03 -0700640 _enter("");
641
David Howellsd0016482016-08-30 20:42:14 +0100642 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
643 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700644 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100645 }
David Howells08e0e7c2007-04-26 15:55:03 -0700646
David Howells56ff9c82017-01-05 10:38:36 +0000647 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000648 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700649
David Howells341f7412017-01-05 10:38:36 +0000650 /* We have two refs to release - one from the alloc and one
651 * queued with the work item - and we can't just deallocate the
652 * call because the work item may be queued again.
653 */
David Howellsd0016482016-08-30 20:42:14 +0100654 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000655 if (!queue_work(afs_async_calls, &call->async_work))
656 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700657 }
658
David Howells341f7412017-01-05 10:38:36 +0000659 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700660 _leave("");
661}
662
David Howells00e90712016-09-08 11:10:12 +0100663static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
664{
665 struct afs_call *call = (struct afs_call *)user_call_ID;
666
667 call->rxcall = rxcall;
668}
669
670/*
671 * Charge the incoming call preallocation.
672 */
David Howellsf044c882017-11-02 15:27:45 +0000673void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100674{
David Howellsf044c882017-11-02 15:27:45 +0000675 struct afs_net *net =
676 container_of(work, struct afs_net, charge_preallocation_work);
677 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100678
679 for (;;) {
680 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000681 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100682 if (!call)
683 break;
684
David Howells56ff9c82017-01-05 10:38:36 +0000685 call->async = true;
David Howells00e90712016-09-08 11:10:12 +0100686 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000687 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100688 }
689
David Howellsf044c882017-11-02 15:27:45 +0000690 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100691 afs_wake_up_async_call,
692 afs_rx_attach,
693 (unsigned long)call,
694 GFP_KERNEL) < 0)
695 break;
696 call = NULL;
697 }
David Howellsf044c882017-11-02 15:27:45 +0000698 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100699}
700
701/*
702 * Discard a preallocated call when a socket is shut down.
703 */
704static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
705 unsigned long user_call_ID)
706{
707 struct afs_call *call = (struct afs_call *)user_call_ID;
708
David Howells00e90712016-09-08 11:10:12 +0100709 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000710 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100711}
712
David Howells08e0e7c2007-04-26 15:55:03 -0700713/*
David Howellsd0016482016-08-30 20:42:14 +0100714 * Notification of an incoming call.
715 */
David Howells00e90712016-09-08 11:10:12 +0100716static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
717 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100718{
David Howellsf044c882017-11-02 15:27:45 +0000719 struct afs_net *net = afs_sock2net(sk);
720
721 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100722}
723
724/*
David Howells372ee162016-08-03 14:11:40 +0100725 * Grab the operation ID from an incoming cache manager call. The socket
726 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700727 */
David Howellsd0016482016-08-30 20:42:14 +0100728static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700729{
David Howellsd0016482016-08-30 20:42:14 +0100730 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700731
David Howellsd0016482016-08-30 20:42:14 +0100732 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700733
734 ASSERTCMP(call->offset, <, 4);
735
736 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100737 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100738 if (ret < 0)
739 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700740
David Howells50a2c952016-10-13 08:27:10 +0100741 call->operation_ID = ntohl(call->tmp);
David Howells08e0e7c2007-04-26 15:55:03 -0700742 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100743 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700744
745 /* ask the cache manager to route the call (it'll change the call type
746 * if successful) */
747 if (!afs_cm_incoming_call(call))
748 return -ENOTSUPP;
749
David Howells8e8d7f12017-01-05 10:38:34 +0000750 trace_afs_cb_call(call);
751
David Howells08e0e7c2007-04-26 15:55:03 -0700752 /* pass responsibility for the remainer of this message off to the
753 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100754 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700755}
756
757/*
David Howellse8332512017-08-29 10:18:56 +0100758 * Advance the AFS call state when an RxRPC service call ends the transmit
759 * phase.
760 */
761static void afs_notify_end_reply_tx(struct sock *sock,
762 struct rxrpc_call *rxcall,
763 unsigned long call_user_ID)
764{
765 struct afs_call *call = (struct afs_call *)call_user_ID;
766
767 if (call->state == AFS_CALL_REPLYING)
768 call->state = AFS_CALL_AWAIT_ACK;
769}
770
771/*
David Howells08e0e7c2007-04-26 15:55:03 -0700772 * send an empty reply
773 */
774void afs_send_empty_reply(struct afs_call *call)
775{
David Howellsf044c882017-11-02 15:27:45 +0000776 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700777 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700778
779 _enter("");
780
David Howellsf044c882017-11-02 15:27:45 +0000781 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100782
David Howells08e0e7c2007-04-26 15:55:03 -0700783 msg.msg_name = NULL;
784 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100785 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700786 msg.msg_control = NULL;
787 msg.msg_controllen = 0;
788 msg.msg_flags = 0;
789
790 call->state = AFS_CALL_AWAIT_ACK;
David Howellsf044c882017-11-02 15:27:45 +0000791 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100792 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700793 case 0:
794 _leave(" [replied]");
795 return;
796
797 case -ENOMEM:
798 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000799 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100800 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700801 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700802 _leave(" [error]");
803 return;
804 }
805}
806
807/*
David Howellsb908fe62007-04-26 15:58:17 -0700808 * send a simple reply
809 */
810void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
811{
David Howellsf044c882017-11-02 15:27:45 +0000812 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700813 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500814 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100815 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700816
817 _enter("");
818
David Howellsf044c882017-11-02 15:27:45 +0000819 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100820
David Howellsb908fe62007-04-26 15:58:17 -0700821 iov[0].iov_base = (void *) buf;
822 iov[0].iov_len = len;
823 msg.msg_name = NULL;
824 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500825 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700826 msg.msg_control = NULL;
827 msg.msg_controllen = 0;
828 msg.msg_flags = 0;
829
830 call->state = AFS_CALL_AWAIT_ACK;
David Howellsf044c882017-11-02 15:27:45 +0000831 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100832 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100833 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100834 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700835 _leave(" [replied]");
836 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100837 }
David Howells6c67c7c2014-05-21 14:48:05 +0100838
David Howellsbd6dc742007-07-20 10:59:41 +0100839 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700840 _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 Howellsb908fe62007-04-26 15:58:17 -0700843 }
David Howellsbd6dc742007-07-20 10:59:41 +0100844 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700845}
846
847/*
David Howells372ee162016-08-03 14:11:40 +0100848 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700849 */
David Howellsd0016482016-08-30 20:42:14 +0100850int afs_extract_data(struct afs_call *call, void *buf, size_t count,
851 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700852{
David Howellsf044c882017-11-02 15:27:45 +0000853 struct afs_net *net = call->net;
David Howellsd0016482016-08-30 20:42:14 +0100854 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700855
David Howellsd0016482016-08-30 20:42:14 +0100856 _enter("{%s,%zu},,%zu,%d",
857 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700858
David Howellsd0016482016-08-30 20:42:14 +0100859 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700860
David Howellsf044c882017-11-02 15:27:45 +0000861 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100862 buf, count, &call->offset,
David Howellsa68f4a22017-10-18 11:36:39 +0100863 want_more, &call->abort_code,
864 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000865 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100866 if (ret == 0 || ret == -EAGAIN)
867 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700868
David Howellsd0016482016-08-30 20:42:14 +0100869 if (ret == 1) {
870 switch (call->state) {
871 case AFS_CALL_AWAIT_REPLY:
872 call->state = AFS_CALL_COMPLETE;
873 break;
874 case AFS_CALL_AWAIT_REQUEST:
875 call->state = AFS_CALL_REPLYING;
876 break;
877 default:
878 break;
879 }
880 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700881 }
David Howellsd0016482016-08-30 20:42:14 +0100882
883 if (ret == -ECONNABORTED)
David Howellsf780c8e2017-11-02 15:27:48 +0000884 call->error = afs_abort_to_error(call->abort_code);
David Howellsd0016482016-08-30 20:42:14 +0100885 else
886 call->error = ret;
887 call->state = AFS_CALL_COMPLETE;
888 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700889}