blob: ac1e25f957b1cc4ade1be1590ba9c945b28c2ef8 [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
David Howellsd0676a12017-11-02 15:27:49 +0000164 afs_put_server(call->net, call->cm_server);
David Howells341f7412017-01-05 10:38:36 +0000165 kfree(call->request);
166 kfree(call);
167
David Howellsf044c882017-11-02 15:27:45 +0000168 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000169 trace_afs_call(call, afs_call_trace_free, 0, o,
170 __builtin_return_address(0));
171 if (o == 0)
David Howellsf044c882017-11-02 15:27:45 +0000172 wake_up_atomic_t(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100173 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100174}
175
176/*
David Howells341f7412017-01-05 10:38:36 +0000177 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100178 */
David Howells341f7412017-01-05 10:38:36 +0000179int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100180{
David Howells341f7412017-01-05 10:38:36 +0000181 int u = atomic_inc_return(&call->usage);
182
183 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000184 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000185 __builtin_return_address(0));
186
187 INIT_WORK(&call->work, call->type->work);
188
189 if (!queue_work(afs_wq, &call->work))
190 afs_put_call(call);
191 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100192}
193
194/*
David Howells08e0e7c2007-04-26 15:55:03 -0700195 * allocate a call with flat request and reply buffers
196 */
David Howellsf044c882017-11-02 15:27:45 +0000197struct afs_call *afs_alloc_flat_call(struct afs_net *net,
198 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100199 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700200{
201 struct afs_call *call;
202
David Howellsf044c882017-11-02 15:27:45 +0000203 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700204 if (!call)
205 goto nomem_call;
206
David Howells00d3b7a2007-04-26 15:57:07 -0700207 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000208 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700209 call->request = kmalloc(request_size, GFP_NOFS);
210 if (!call->request)
211 goto nomem_free;
212 }
213
David Howellsd0016482016-08-30 20:42:14 +0100214 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000215 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100216 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700217 if (!call->buffer)
218 goto nomem_free;
219 }
220
David Howells08e0e7c2007-04-26 15:55:03 -0700221 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700222 return call;
223
David Howells00d3b7a2007-04-26 15:57:07 -0700224nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000225 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700226nomem_call:
227 return NULL;
228}
229
230/*
231 * clean up a call with flat buffer
232 */
233void afs_flat_call_destructor(struct afs_call *call)
234{
235 _enter("");
236
237 kfree(call->request);
238 call->request = NULL;
239 kfree(call->buffer);
240 call->buffer = NULL;
241}
242
David Howells2f5705a2017-03-16 16:27:46 +0000243#define AFS_BVEC_MAX 8
244
245/*
246 * Load the given bvec with the next few pages.
247 */
248static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
249 struct bio_vec *bv, pgoff_t first, pgoff_t last,
250 unsigned offset)
251{
252 struct page *pages[AFS_BVEC_MAX];
253 unsigned int nr, n, i, to, bytes = 0;
254
255 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
256 n = find_get_pages_contig(call->mapping, first, nr, pages);
257 ASSERTCMP(n, ==, nr);
258
259 msg->msg_flags |= MSG_MORE;
260 for (i = 0; i < nr; i++) {
261 to = PAGE_SIZE;
262 if (first + i >= last) {
263 to = call->last_to;
264 msg->msg_flags &= ~MSG_MORE;
265 }
266 bv[i].bv_page = pages[i];
267 bv[i].bv_len = to - offset;
268 bv[i].bv_offset = offset;
269 bytes += to - offset;
270 offset = 0;
271 }
272
273 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
274}
275
David Howells08e0e7c2007-04-26 15:55:03 -0700276/*
David Howellse8332512017-08-29 10:18:56 +0100277 * Advance the AFS call state when the RxRPC call ends the transmit phase.
278 */
279static void afs_notify_end_request_tx(struct sock *sock,
280 struct rxrpc_call *rxcall,
281 unsigned long call_user_ID)
282{
283 struct afs_call *call = (struct afs_call *)call_user_ID;
284
285 if (call->state == AFS_CALL_REQUESTING)
286 call->state = AFS_CALL_AWAIT_REPLY;
287}
288
289/*
David Howells31143d52007-05-09 02:33:46 -0700290 * attach the data from a bunch of pages on an inode to a call
291 */
Al Viro39c6ace2016-01-09 20:36:51 -0500292static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700293{
David Howells2f5705a2017-03-16 16:27:46 +0000294 struct bio_vec bv[AFS_BVEC_MAX];
295 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700296 pgoff_t first = call->first, last = call->last;
297 int ret;
298
David Howells31143d52007-05-09 02:33:46 -0700299 offset = call->first_offset;
300 call->first_offset = 0;
301
302 do {
David Howells2f5705a2017-03-16 16:27:46 +0000303 afs_load_bvec(call, msg, bv, first, last, offset);
304 offset = 0;
305 bytes = msg->msg_iter.count;
306 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700307
David Howellsf044c882017-11-02 15:27:45 +0000308 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100309 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000310 for (loop = 0; loop < nr; loop++)
311 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700312 if (ret < 0)
313 break;
David Howells2f5705a2017-03-16 16:27:46 +0000314
315 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700316 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700317
David Howells31143d52007-05-09 02:33:46 -0700318 return ret;
319}
320
321/*
David Howells08e0e7c2007-04-26 15:55:03 -0700322 * initiate a call
323 */
David Howells33cd7f22017-11-02 15:27:48 +0000324long afs_make_call(struct sockaddr_rxrpc *srx, struct afs_call *call,
325 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700326{
David Howells08e0e7c2007-04-26 15:55:03 -0700327 struct rxrpc_call *rxcall;
328 struct msghdr msg;
329 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000330 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100331 s64 tx_total_len;
David Howells70af0e32017-03-16 16:27:47 +0000332 u32 abort_code;
David Howells08e0e7c2007-04-26 15:55:03 -0700333 int ret;
334
David Howells4d9df982017-11-02 15:27:47 +0000335 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700336
David Howells00d3b7a2007-04-26 15:57:07 -0700337 ASSERT(call->type != NULL);
338 ASSERT(call->type->name != NULL);
339
David Howells31143d52007-05-09 02:33:46 -0700340 _debug("____MAKE %p{%s,%x} [%d]____",
341 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000342 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700343
David Howells56ff9c82017-01-05 10:38:36 +0000344 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700345
David Howellse754eba2017-06-07 12:40:03 +0100346 /* Work out the length we're going to transmit. This is awkward for
347 * calls such as FS.StoreData where there's an extra injection of data
348 * after the initial fixed part.
349 */
350 tx_total_len = call->request_size;
351 if (call->send_pages) {
352 tx_total_len += call->last_to - call->first_offset;
353 tx_total_len += (call->last - call->first) * PAGE_SIZE;
354 }
355
David Howells08e0e7c2007-04-26 15:55:03 -0700356 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000357 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100358 (unsigned long)call,
359 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000360 (async ?
361 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100362 afs_wake_up_call_waiter),
363 call->upgrade);
David Howells00d3b7a2007-04-26 15:57:07 -0700364 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700365 if (IS_ERR(rxcall)) {
366 ret = PTR_ERR(rxcall);
367 goto error_kill_call;
368 }
369
370 call->rxcall = rxcall;
371
372 /* send the request */
373 iov[0].iov_base = call->request;
374 iov[0].iov_len = call->request_size;
375
376 msg.msg_name = NULL;
377 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500378 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500379 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700380 msg.msg_control = NULL;
381 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100382 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700383
David Howells70af0e32017-03-16 16:27:47 +0000384 /* We have to change the state *before* sending the last packet as
385 * rxrpc might give us the reply before it returns from sending the
386 * request. Further, if the send fails, we may already have been given
387 * a notification and may have collected it.
388 */
David Howells31143d52007-05-09 02:33:46 -0700389 if (!call->send_pages)
390 call->state = AFS_CALL_AWAIT_REPLY;
David Howellsf044c882017-11-02 15:27:45 +0000391 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100392 &msg, call->request_size,
393 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700394 if (ret < 0)
395 goto error_do_abort;
396
David Howells31143d52007-05-09 02:33:46 -0700397 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500398 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700399 if (ret < 0)
400 goto error_do_abort;
401 }
402
David Howells08e0e7c2007-04-26 15:55:03 -0700403 /* at this point, an async call may no longer exist as it may have
404 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000405 if (call->async)
406 return -EINPROGRESS;
407
408 return afs_wait_for_call_to_complete(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700409
410error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000411 call->state = AFS_CALL_COMPLETE;
412 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000413 rxrpc_kernel_abort_call(call->net->socket, rxcall,
414 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000415 } else {
416 abort_code = 0;
417 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000418 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000419 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000420 &call->service_id);
David Howells33cd7f22017-11-02 15:27:48 +0000421 ret = afs_abort_to_error(call->abort_code);
David Howells70af0e32017-03-16 16:27:47 +0000422 }
David Howells08e0e7c2007-04-26 15:55:03 -0700423error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000424 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700425 _leave(" = %d", ret);
426 return ret;
427}
428
429/*
David Howells08e0e7c2007-04-26 15:55:03 -0700430 * deliver messages to a call
431 */
432static void afs_deliver_to_call(struct afs_call *call)
433{
David Howells08e0e7c2007-04-26 15:55:03 -0700434 u32 abort_code;
435 int ret;
436
David Howellsd0016482016-08-30 20:42:14 +0100437 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700438
David Howellsd0016482016-08-30 20:42:14 +0100439 while (call->state == AFS_CALL_AWAIT_REPLY ||
440 call->state == AFS_CALL_AWAIT_OP_ID ||
441 call->state == AFS_CALL_AWAIT_REQUEST ||
442 call->state == AFS_CALL_AWAIT_ACK
443 ) {
444 if (call->state == AFS_CALL_AWAIT_ACK) {
445 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000446 ret = rxrpc_kernel_recv_data(call->net->socket,
447 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100448 NULL, 0, &offset, false,
David Howellsa68f4a22017-10-18 11:36:39 +0100449 &call->abort_code,
450 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000451 trace_afs_recv_data(call, 0, offset, false, ret);
452
David Howellsd0016482016-08-30 20:42:14 +0100453 if (ret == -EINPROGRESS || ret == -EAGAIN)
454 return;
David Howells9008f992016-10-06 08:11:50 +0100455 if (ret == 1 || ret < 0) {
David Howellsd0016482016-08-30 20:42:14 +0100456 call->state = AFS_CALL_COMPLETE;
457 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700458 }
David Howellsd0016482016-08-30 20:42:14 +0100459 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700460 }
461
David Howellsd0016482016-08-30 20:42:14 +0100462 ret = call->type->deliver(call);
463 switch (ret) {
464 case 0:
465 if (call->state == AFS_CALL_AWAIT_REPLY)
466 call->state = AFS_CALL_COMPLETE;
467 goto done;
468 case -EINPROGRESS:
469 case -EAGAIN:
470 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000471 case -ECONNABORTED:
David Howells33cd7f22017-11-02 15:27:48 +0000472 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100473 case -ENOTCONN:
474 abort_code = RX_CALL_DEAD;
David Howellsf044c882017-11-02 15:27:45 +0000475 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100476 abort_code, ret, "KNC");
David Howells70af0e32017-03-16 16:27:47 +0000477 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100478 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000479 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000480 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100481 abort_code, ret, "KIV");
David Howells70af0e32017-03-16 16:27:47 +0000482 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100483 case -ENODATA:
484 case -EBADMSG:
485 case -EMSGSIZE:
486 default:
487 abort_code = RXGEN_CC_UNMARSHAL;
488 if (call->state != AFS_CALL_AWAIT_REPLY)
489 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000490 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100491 abort_code, -EBADMSG, "KUM");
David Howells70af0e32017-03-16 16:27:47 +0000492 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100493 }
David Howells08e0e7c2007-04-26 15:55:03 -0700494 }
495
David Howellsd0016482016-08-30 20:42:14 +0100496done:
497 if (call->state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000498 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100499out:
David Howells08e0e7c2007-04-26 15:55:03 -0700500 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100501 return;
502
David Howells70af0e32017-03-16 16:27:47 +0000503save_error:
David Howellsd0016482016-08-30 20:42:14 +0100504 call->error = ret;
505 call->state = AFS_CALL_COMPLETE;
506 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700507}
508
509/*
510 * wait synchronously for a call to complete
511 */
David Howells33cd7f22017-11-02 15:27:48 +0000512static long afs_wait_for_call_to_complete(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700513{
David Howellsbc5e3a52017-10-18 11:07:31 +0100514 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000515 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100516 u64 rtt;
517 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700518
519 DECLARE_WAITQUEUE(myself, current);
520
521 _enter("");
522
David Howellsf044c882017-11-02 15:27:45 +0000523 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100524 rtt2 = nsecs_to_jiffies64(rtt) * 2;
525 if (rtt2 < 2)
526 rtt2 = 2;
527
528 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000529 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100530
David Howells08e0e7c2007-04-26 15:55:03 -0700531 add_wait_queue(&call->waitq, &myself);
532 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100533 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700534
535 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100536 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
537 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700538 __set_current_state(TASK_RUNNING);
539 afs_deliver_to_call(call);
540 continue;
541 }
542
David Howellsbc5e3a52017-10-18 11:07:31 +0100543 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700544 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100545
David Howellsf044c882017-11-02 15:27:45 +0000546 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100547 if (timeout == 0 &&
548 life == last_life && signal_pending(current))
549 break;
550
551 if (life != last_life) {
552 timeout = rtt2;
553 last_life = life;
554 }
555
556 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700557 }
558
559 remove_wait_queue(&call->waitq, &myself);
560 __set_current_state(TASK_RUNNING);
561
David Howells954cd6d2017-03-16 16:27:49 +0000562 /* Kill off the call if it's still live. */
David Howells08e0e7c2007-04-26 15:55:03 -0700563 if (call->state < AFS_CALL_COMPLETE) {
David Howells954cd6d2017-03-16 16:27:49 +0000564 _debug("call interrupted");
David Howellsf044c882017-11-02 15:27:45 +0000565 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells954cd6d2017-03-16 16:27:49 +0000566 RX_USER_ABORT, -EINTR, "KWI");
David Howells08e0e7c2007-04-26 15:55:03 -0700567 }
568
David Howells954cd6d2017-03-16 16:27:49 +0000569 ret = call->error;
David Howells33cd7f22017-11-02 15:27:48 +0000570 if (ret < 0) {
571 ret = afs_abort_to_error(call->abort_code);
572 } else if (ret == 0 && call->ret_reply0) {
573 ret = (long)call->reply[0];
574 call->reply[0] = NULL;
575 }
576
David Howells08e0e7c2007-04-26 15:55:03 -0700577 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000578 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000579 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700580 return ret;
581}
582
583/*
584 * wake up a waiting call
585 */
David Howellsd0016482016-08-30 20:42:14 +0100586static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
587 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700588{
David Howellsd0016482016-08-30 20:42:14 +0100589 struct afs_call *call = (struct afs_call *)call_user_ID;
590
591 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700592 wake_up(&call->waitq);
593}
594
595/*
596 * wake up an asynchronous call
597 */
David Howellsd0016482016-08-30 20:42:14 +0100598static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
599 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700600{
David Howellsd0016482016-08-30 20:42:14 +0100601 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000602 int u;
David Howellsd0016482016-08-30 20:42:14 +0100603
David Howells8e8d7f12017-01-05 10:38:34 +0000604 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100605 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000606
607 u = __atomic_add_unless(&call->usage, 1, 0);
608 if (u != 0) {
609 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000610 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000611 __builtin_return_address(0));
612
613 if (!queue_work(afs_async_calls, &call->async_work))
614 afs_put_call(call);
615 }
David Howells08e0e7c2007-04-26 15:55:03 -0700616}
617
618/*
David Howells341f7412017-01-05 10:38:36 +0000619 * Delete an asynchronous call. The work item carries a ref to the call struct
620 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700621 */
David Howellsd0016482016-08-30 20:42:14 +0100622static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700623{
David Howellsd0016482016-08-30 20:42:14 +0100624 struct afs_call *call = container_of(work, struct afs_call, async_work);
625
David Howells08e0e7c2007-04-26 15:55:03 -0700626 _enter("");
627
David Howells341f7412017-01-05 10:38:36 +0000628 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700629
630 _leave("");
631}
632
633/*
David Howells341f7412017-01-05 10:38:36 +0000634 * Perform I/O processing on an asynchronous call. The work item carries a ref
635 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700636 */
David Howellsd0016482016-08-30 20:42:14 +0100637static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700638{
David Howellsd0016482016-08-30 20:42:14 +0100639 struct afs_call *call = container_of(work, struct afs_call, async_work);
640
David Howells08e0e7c2007-04-26 15:55:03 -0700641 _enter("");
642
David Howellsd0016482016-08-30 20:42:14 +0100643 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
644 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700645 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100646 }
David Howells08e0e7c2007-04-26 15:55:03 -0700647
David Howells56ff9c82017-01-05 10:38:36 +0000648 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000649 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700650
David Howells341f7412017-01-05 10:38:36 +0000651 /* We have two refs to release - one from the alloc and one
652 * queued with the work item - and we can't just deallocate the
653 * call because the work item may be queued again.
654 */
David Howellsd0016482016-08-30 20:42:14 +0100655 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000656 if (!queue_work(afs_async_calls, &call->async_work))
657 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700658 }
659
David Howells341f7412017-01-05 10:38:36 +0000660 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700661 _leave("");
662}
663
David Howells00e90712016-09-08 11:10:12 +0100664static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
665{
666 struct afs_call *call = (struct afs_call *)user_call_ID;
667
668 call->rxcall = rxcall;
669}
670
671/*
672 * Charge the incoming call preallocation.
673 */
David Howellsf044c882017-11-02 15:27:45 +0000674void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100675{
David Howellsf044c882017-11-02 15:27:45 +0000676 struct afs_net *net =
677 container_of(work, struct afs_net, charge_preallocation_work);
678 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100679
680 for (;;) {
681 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000682 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100683 if (!call)
684 break;
685
David Howells56ff9c82017-01-05 10:38:36 +0000686 call->async = true;
David Howells00e90712016-09-08 11:10:12 +0100687 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000688 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100689 }
690
David Howellsf044c882017-11-02 15:27:45 +0000691 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100692 afs_wake_up_async_call,
693 afs_rx_attach,
694 (unsigned long)call,
695 GFP_KERNEL) < 0)
696 break;
697 call = NULL;
698 }
David Howellsf044c882017-11-02 15:27:45 +0000699 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100700}
701
702/*
703 * Discard a preallocated call when a socket is shut down.
704 */
705static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
706 unsigned long user_call_ID)
707{
708 struct afs_call *call = (struct afs_call *)user_call_ID;
709
David Howells00e90712016-09-08 11:10:12 +0100710 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000711 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100712}
713
David Howells08e0e7c2007-04-26 15:55:03 -0700714/*
David Howellsd0016482016-08-30 20:42:14 +0100715 * Notification of an incoming call.
716 */
David Howells00e90712016-09-08 11:10:12 +0100717static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
718 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100719{
David Howellsf044c882017-11-02 15:27:45 +0000720 struct afs_net *net = afs_sock2net(sk);
721
722 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100723}
724
725/*
David Howells372ee162016-08-03 14:11:40 +0100726 * Grab the operation ID from an incoming cache manager call. The socket
727 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700728 */
David Howellsd0016482016-08-30 20:42:14 +0100729static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700730{
David Howellsd0016482016-08-30 20:42:14 +0100731 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700732
David Howellsd0016482016-08-30 20:42:14 +0100733 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700734
735 ASSERTCMP(call->offset, <, 4);
736
737 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100738 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100739 if (ret < 0)
740 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700741
David Howells50a2c952016-10-13 08:27:10 +0100742 call->operation_ID = ntohl(call->tmp);
David Howells08e0e7c2007-04-26 15:55:03 -0700743 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100744 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700745
746 /* ask the cache manager to route the call (it'll change the call type
747 * if successful) */
748 if (!afs_cm_incoming_call(call))
749 return -ENOTSUPP;
750
David Howells8e8d7f12017-01-05 10:38:34 +0000751 trace_afs_cb_call(call);
752
David Howells08e0e7c2007-04-26 15:55:03 -0700753 /* pass responsibility for the remainer of this message off to the
754 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100755 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700756}
757
758/*
David Howellse8332512017-08-29 10:18:56 +0100759 * Advance the AFS call state when an RxRPC service call ends the transmit
760 * phase.
761 */
762static void afs_notify_end_reply_tx(struct sock *sock,
763 struct rxrpc_call *rxcall,
764 unsigned long call_user_ID)
765{
766 struct afs_call *call = (struct afs_call *)call_user_ID;
767
768 if (call->state == AFS_CALL_REPLYING)
769 call->state = AFS_CALL_AWAIT_ACK;
770}
771
772/*
David Howells08e0e7c2007-04-26 15:55:03 -0700773 * send an empty reply
774 */
775void afs_send_empty_reply(struct afs_call *call)
776{
David Howellsf044c882017-11-02 15:27:45 +0000777 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700778 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700779
780 _enter("");
781
David Howellsf044c882017-11-02 15:27:45 +0000782 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100783
David Howells08e0e7c2007-04-26 15:55:03 -0700784 msg.msg_name = NULL;
785 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100786 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700787 msg.msg_control = NULL;
788 msg.msg_controllen = 0;
789 msg.msg_flags = 0;
790
791 call->state = AFS_CALL_AWAIT_ACK;
David Howellsf044c882017-11-02 15:27:45 +0000792 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100793 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700794 case 0:
795 _leave(" [replied]");
796 return;
797
798 case -ENOMEM:
799 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000800 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100801 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700802 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700803 _leave(" [error]");
804 return;
805 }
806}
807
808/*
David Howellsb908fe62007-04-26 15:58:17 -0700809 * send a simple reply
810 */
811void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
812{
David Howellsf044c882017-11-02 15:27:45 +0000813 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700814 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500815 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100816 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700817
818 _enter("");
819
David Howellsf044c882017-11-02 15:27:45 +0000820 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100821
David Howellsb908fe62007-04-26 15:58:17 -0700822 iov[0].iov_base = (void *) buf;
823 iov[0].iov_len = len;
824 msg.msg_name = NULL;
825 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500826 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700827 msg.msg_control = NULL;
828 msg.msg_controllen = 0;
829 msg.msg_flags = 0;
830
831 call->state = AFS_CALL_AWAIT_ACK;
David Howellsf044c882017-11-02 15:27:45 +0000832 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100833 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100834 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100835 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700836 _leave(" [replied]");
837 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100838 }
David Howells6c67c7c2014-05-21 14:48:05 +0100839
David Howellsbd6dc742007-07-20 10:59:41 +0100840 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700841 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000842 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100843 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700844 }
David Howellsbd6dc742007-07-20 10:59:41 +0100845 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700846}
847
848/*
David Howells372ee162016-08-03 14:11:40 +0100849 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700850 */
David Howellsd0016482016-08-30 20:42:14 +0100851int afs_extract_data(struct afs_call *call, void *buf, size_t count,
852 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700853{
David Howellsf044c882017-11-02 15:27:45 +0000854 struct afs_net *net = call->net;
David Howellsd0016482016-08-30 20:42:14 +0100855 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700856
David Howellsd0016482016-08-30 20:42:14 +0100857 _enter("{%s,%zu},,%zu,%d",
858 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700859
David Howellsd0016482016-08-30 20:42:14 +0100860 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700861
David Howellsf044c882017-11-02 15:27:45 +0000862 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100863 buf, count, &call->offset,
David Howellsa68f4a22017-10-18 11:36:39 +0100864 want_more, &call->abort_code,
865 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000866 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100867 if (ret == 0 || ret == -EAGAIN)
868 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700869
David Howellsd0016482016-08-30 20:42:14 +0100870 if (ret == 1) {
871 switch (call->state) {
872 case AFS_CALL_AWAIT_REPLY:
873 call->state = AFS_CALL_COMPLETE;
874 break;
875 case AFS_CALL_AWAIT_REQUEST:
876 call->state = AFS_CALL_REPLYING;
877 break;
878 default:
879 break;
880 }
881 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700882 }
David Howellsd0016482016-08-30 20:42:14 +0100883
884 if (ret == -ECONNABORTED)
David Howellsf780c8e2017-11-02 15:27:48 +0000885 call->error = afs_abort_to_error(call->abort_code);
David Howellsd0016482016-08-30 20:42:14 +0100886 else
887 call->error = ret;
888 call->state = AFS_CALL_COMPLETE;
889 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700890}