blob: 5ddfb7c4cf78152affcdf95d2aac4be96a3b2e6a [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 Howells8b2a4642017-11-02 15:27:50 +0000324long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000325 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700326{
David Howells8b2a4642017-11-02 15:27:50 +0000327 struct sockaddr_rxrpc *srx = ac->addr;
David Howells08e0e7c2007-04-26 15:55:03 -0700328 struct rxrpc_call *rxcall;
329 struct msghdr msg;
330 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000331 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100332 s64 tx_total_len;
David Howells70af0e32017-03-16 16:27:47 +0000333 u32 abort_code;
David Howells08e0e7c2007-04-26 15:55:03 -0700334 int ret;
335
David Howells4d9df982017-11-02 15:27:47 +0000336 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700337
David Howells00d3b7a2007-04-26 15:57:07 -0700338 ASSERT(call->type != NULL);
339 ASSERT(call->type->name != NULL);
340
David Howells31143d52007-05-09 02:33:46 -0700341 _debug("____MAKE %p{%s,%x} [%d]____",
342 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000343 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700344
David Howells56ff9c82017-01-05 10:38:36 +0000345 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700346
David Howellse754eba2017-06-07 12:40:03 +0100347 /* Work out the length we're going to transmit. This is awkward for
348 * calls such as FS.StoreData where there's an extra injection of data
349 * after the initial fixed part.
350 */
351 tx_total_len = call->request_size;
352 if (call->send_pages) {
353 tx_total_len += call->last_to - call->first_offset;
354 tx_total_len += (call->last - call->first) * PAGE_SIZE;
355 }
356
David Howells08e0e7c2007-04-26 15:55:03 -0700357 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000358 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100359 (unsigned long)call,
360 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000361 (async ?
362 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100363 afs_wake_up_call_waiter),
364 call->upgrade);
David Howells00d3b7a2007-04-26 15:57:07 -0700365 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700366 if (IS_ERR(rxcall)) {
367 ret = PTR_ERR(rxcall);
368 goto error_kill_call;
369 }
370
371 call->rxcall = rxcall;
372
373 /* send the request */
374 iov[0].iov_base = call->request;
375 iov[0].iov_len = call->request_size;
376
377 msg.msg_name = NULL;
378 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500379 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500380 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700381 msg.msg_control = NULL;
382 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100383 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700384
David Howells70af0e32017-03-16 16:27:47 +0000385 /* We have to change the state *before* sending the last packet as
386 * rxrpc might give us the reply before it returns from sending the
387 * request. Further, if the send fails, we may already have been given
388 * a notification and may have collected it.
389 */
David Howells31143d52007-05-09 02:33:46 -0700390 if (!call->send_pages)
391 call->state = AFS_CALL_AWAIT_REPLY;
David Howellsf044c882017-11-02 15:27:45 +0000392 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100393 &msg, call->request_size,
394 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700395 if (ret < 0)
396 goto error_do_abort;
397
David Howells31143d52007-05-09 02:33:46 -0700398 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500399 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700400 if (ret < 0)
401 goto error_do_abort;
402 }
403
David Howells08e0e7c2007-04-26 15:55:03 -0700404 /* at this point, an async call may no longer exist as it may have
405 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000406 if (call->async)
407 return -EINPROGRESS;
408
409 return afs_wait_for_call_to_complete(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700410
411error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000412 call->state = AFS_CALL_COMPLETE;
413 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000414 rxrpc_kernel_abort_call(call->net->socket, rxcall,
415 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000416 } else {
417 abort_code = 0;
418 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000419 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000420 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000421 &call->service_id);
David Howells33cd7f22017-11-02 15:27:48 +0000422 ret = afs_abort_to_error(call->abort_code);
David Howells70af0e32017-03-16 16:27:47 +0000423 }
David Howells08e0e7c2007-04-26 15:55:03 -0700424error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000425 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700426 _leave(" = %d", ret);
427 return ret;
428}
429
430/*
David Howells08e0e7c2007-04-26 15:55:03 -0700431 * deliver messages to a call
432 */
433static void afs_deliver_to_call(struct afs_call *call)
434{
David Howells08e0e7c2007-04-26 15:55:03 -0700435 u32 abort_code;
436 int ret;
437
David Howellsd0016482016-08-30 20:42:14 +0100438 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700439
David Howellsd0016482016-08-30 20:42:14 +0100440 while (call->state == AFS_CALL_AWAIT_REPLY ||
441 call->state == AFS_CALL_AWAIT_OP_ID ||
442 call->state == AFS_CALL_AWAIT_REQUEST ||
443 call->state == AFS_CALL_AWAIT_ACK
444 ) {
445 if (call->state == AFS_CALL_AWAIT_ACK) {
446 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000447 ret = rxrpc_kernel_recv_data(call->net->socket,
448 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100449 NULL, 0, &offset, false,
David Howellsa68f4a22017-10-18 11:36:39 +0100450 &call->abort_code,
451 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000452 trace_afs_recv_data(call, 0, offset, false, ret);
453
David Howellsd0016482016-08-30 20:42:14 +0100454 if (ret == -EINPROGRESS || ret == -EAGAIN)
455 return;
David Howells9008f992016-10-06 08:11:50 +0100456 if (ret == 1 || ret < 0) {
David Howellsd0016482016-08-30 20:42:14 +0100457 call->state = AFS_CALL_COMPLETE;
458 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700459 }
David Howellsd0016482016-08-30 20:42:14 +0100460 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700461 }
462
David Howellsd0016482016-08-30 20:42:14 +0100463 ret = call->type->deliver(call);
464 switch (ret) {
465 case 0:
466 if (call->state == AFS_CALL_AWAIT_REPLY)
467 call->state = AFS_CALL_COMPLETE;
468 goto done;
469 case -EINPROGRESS:
470 case -EAGAIN:
471 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000472 case -ECONNABORTED:
David Howells33cd7f22017-11-02 15:27:48 +0000473 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100474 case -ENOTCONN:
475 abort_code = RX_CALL_DEAD;
David Howellsf044c882017-11-02 15:27:45 +0000476 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100477 abort_code, ret, "KNC");
David Howells70af0e32017-03-16 16:27:47 +0000478 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100479 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000480 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000481 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100482 abort_code, ret, "KIV");
David Howells70af0e32017-03-16 16:27:47 +0000483 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100484 case -ENODATA:
485 case -EBADMSG:
486 case -EMSGSIZE:
487 default:
488 abort_code = RXGEN_CC_UNMARSHAL;
489 if (call->state != AFS_CALL_AWAIT_REPLY)
490 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000491 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100492 abort_code, -EBADMSG, "KUM");
David Howells70af0e32017-03-16 16:27:47 +0000493 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100494 }
David Howells08e0e7c2007-04-26 15:55:03 -0700495 }
496
David Howellsd0016482016-08-30 20:42:14 +0100497done:
498 if (call->state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000499 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100500out:
David Howells08e0e7c2007-04-26 15:55:03 -0700501 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100502 return;
503
David Howells70af0e32017-03-16 16:27:47 +0000504save_error:
David Howellsd0016482016-08-30 20:42:14 +0100505 call->error = ret;
506 call->state = AFS_CALL_COMPLETE;
507 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700508}
509
510/*
511 * wait synchronously for a call to complete
512 */
David Howells33cd7f22017-11-02 15:27:48 +0000513static long afs_wait_for_call_to_complete(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700514{
David Howellsbc5e3a52017-10-18 11:07:31 +0100515 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000516 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100517 u64 rtt;
518 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700519
520 DECLARE_WAITQUEUE(myself, current);
521
522 _enter("");
523
David Howellsf044c882017-11-02 15:27:45 +0000524 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100525 rtt2 = nsecs_to_jiffies64(rtt) * 2;
526 if (rtt2 < 2)
527 rtt2 = 2;
528
529 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000530 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100531
David Howells08e0e7c2007-04-26 15:55:03 -0700532 add_wait_queue(&call->waitq, &myself);
533 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100534 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700535
536 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100537 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
538 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700539 __set_current_state(TASK_RUNNING);
540 afs_deliver_to_call(call);
541 continue;
542 }
543
David Howellsbc5e3a52017-10-18 11:07:31 +0100544 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700545 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100546
David Howellsf044c882017-11-02 15:27:45 +0000547 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100548 if (timeout == 0 &&
549 life == last_life && signal_pending(current))
550 break;
551
552 if (life != last_life) {
553 timeout = rtt2;
554 last_life = life;
555 }
556
557 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700558 }
559
560 remove_wait_queue(&call->waitq, &myself);
561 __set_current_state(TASK_RUNNING);
562
David Howells954cd6d2017-03-16 16:27:49 +0000563 /* Kill off the call if it's still live. */
David Howells08e0e7c2007-04-26 15:55:03 -0700564 if (call->state < AFS_CALL_COMPLETE) {
David Howells954cd6d2017-03-16 16:27:49 +0000565 _debug("call interrupted");
David Howellsf044c882017-11-02 15:27:45 +0000566 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells954cd6d2017-03-16 16:27:49 +0000567 RX_USER_ABORT, -EINTR, "KWI");
David Howells08e0e7c2007-04-26 15:55:03 -0700568 }
569
David Howells954cd6d2017-03-16 16:27:49 +0000570 ret = call->error;
David Howells33cd7f22017-11-02 15:27:48 +0000571 if (ret < 0) {
572 ret = afs_abort_to_error(call->abort_code);
573 } else if (ret == 0 && call->ret_reply0) {
574 ret = (long)call->reply[0];
575 call->reply[0] = NULL;
576 }
577
David Howells08e0e7c2007-04-26 15:55:03 -0700578 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000579 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000580 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700581 return ret;
582}
583
584/*
585 * wake up a waiting call
586 */
David Howellsd0016482016-08-30 20:42:14 +0100587static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
588 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700589{
David Howellsd0016482016-08-30 20:42:14 +0100590 struct afs_call *call = (struct afs_call *)call_user_ID;
591
592 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700593 wake_up(&call->waitq);
594}
595
596/*
597 * wake up an asynchronous call
598 */
David Howellsd0016482016-08-30 20:42:14 +0100599static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
600 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700601{
David Howellsd0016482016-08-30 20:42:14 +0100602 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000603 int u;
David Howellsd0016482016-08-30 20:42:14 +0100604
David Howells8e8d7f12017-01-05 10:38:34 +0000605 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100606 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000607
608 u = __atomic_add_unless(&call->usage, 1, 0);
609 if (u != 0) {
610 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000611 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000612 __builtin_return_address(0));
613
614 if (!queue_work(afs_async_calls, &call->async_work))
615 afs_put_call(call);
616 }
David Howells08e0e7c2007-04-26 15:55:03 -0700617}
618
619/*
David Howells341f7412017-01-05 10:38:36 +0000620 * Delete an asynchronous call. The work item carries a ref to the call struct
621 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700622 */
David Howellsd0016482016-08-30 20:42:14 +0100623static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700624{
David Howellsd0016482016-08-30 20:42:14 +0100625 struct afs_call *call = container_of(work, struct afs_call, async_work);
626
David Howells08e0e7c2007-04-26 15:55:03 -0700627 _enter("");
628
David Howells341f7412017-01-05 10:38:36 +0000629 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700630
631 _leave("");
632}
633
634/*
David Howells341f7412017-01-05 10:38:36 +0000635 * Perform I/O processing on an asynchronous call. The work item carries a ref
636 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700637 */
David Howellsd0016482016-08-30 20:42:14 +0100638static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700639{
David Howellsd0016482016-08-30 20:42:14 +0100640 struct afs_call *call = container_of(work, struct afs_call, async_work);
641
David Howells08e0e7c2007-04-26 15:55:03 -0700642 _enter("");
643
David Howellsd0016482016-08-30 20:42:14 +0100644 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
645 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700646 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100647 }
David Howells08e0e7c2007-04-26 15:55:03 -0700648
David Howells56ff9c82017-01-05 10:38:36 +0000649 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000650 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700651
David Howells341f7412017-01-05 10:38:36 +0000652 /* We have two refs to release - one from the alloc and one
653 * queued with the work item - and we can't just deallocate the
654 * call because the work item may be queued again.
655 */
David Howellsd0016482016-08-30 20:42:14 +0100656 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000657 if (!queue_work(afs_async_calls, &call->async_work))
658 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700659 }
660
David Howells341f7412017-01-05 10:38:36 +0000661 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700662 _leave("");
663}
664
David Howells00e90712016-09-08 11:10:12 +0100665static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
666{
667 struct afs_call *call = (struct afs_call *)user_call_ID;
668
669 call->rxcall = rxcall;
670}
671
672/*
673 * Charge the incoming call preallocation.
674 */
David Howellsf044c882017-11-02 15:27:45 +0000675void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100676{
David Howellsf044c882017-11-02 15:27:45 +0000677 struct afs_net *net =
678 container_of(work, struct afs_net, charge_preallocation_work);
679 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100680
681 for (;;) {
682 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000683 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100684 if (!call)
685 break;
686
David Howells56ff9c82017-01-05 10:38:36 +0000687 call->async = true;
David Howells00e90712016-09-08 11:10:12 +0100688 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000689 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100690 }
691
David Howellsf044c882017-11-02 15:27:45 +0000692 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100693 afs_wake_up_async_call,
694 afs_rx_attach,
695 (unsigned long)call,
696 GFP_KERNEL) < 0)
697 break;
698 call = NULL;
699 }
David Howellsf044c882017-11-02 15:27:45 +0000700 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100701}
702
703/*
704 * Discard a preallocated call when a socket is shut down.
705 */
706static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
707 unsigned long user_call_ID)
708{
709 struct afs_call *call = (struct afs_call *)user_call_ID;
710
David Howells00e90712016-09-08 11:10:12 +0100711 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000712 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100713}
714
David Howells08e0e7c2007-04-26 15:55:03 -0700715/*
David Howellsd0016482016-08-30 20:42:14 +0100716 * Notification of an incoming call.
717 */
David Howells00e90712016-09-08 11:10:12 +0100718static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
719 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100720{
David Howellsf044c882017-11-02 15:27:45 +0000721 struct afs_net *net = afs_sock2net(sk);
722
723 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100724}
725
726/*
David Howells372ee162016-08-03 14:11:40 +0100727 * Grab the operation ID from an incoming cache manager call. The socket
728 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700729 */
David Howellsd0016482016-08-30 20:42:14 +0100730static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700731{
David Howellsd0016482016-08-30 20:42:14 +0100732 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700733
David Howellsd0016482016-08-30 20:42:14 +0100734 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700735
736 ASSERTCMP(call->offset, <, 4);
737
738 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100739 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100740 if (ret < 0)
741 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700742
David Howells50a2c952016-10-13 08:27:10 +0100743 call->operation_ID = ntohl(call->tmp);
David Howells08e0e7c2007-04-26 15:55:03 -0700744 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100745 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700746
747 /* ask the cache manager to route the call (it'll change the call type
748 * if successful) */
749 if (!afs_cm_incoming_call(call))
750 return -ENOTSUPP;
751
David Howells8e8d7f12017-01-05 10:38:34 +0000752 trace_afs_cb_call(call);
753
David Howells08e0e7c2007-04-26 15:55:03 -0700754 /* pass responsibility for the remainer of this message off to the
755 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100756 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700757}
758
759/*
David Howellse8332512017-08-29 10:18:56 +0100760 * Advance the AFS call state when an RxRPC service call ends the transmit
761 * phase.
762 */
763static void afs_notify_end_reply_tx(struct sock *sock,
764 struct rxrpc_call *rxcall,
765 unsigned long call_user_ID)
766{
767 struct afs_call *call = (struct afs_call *)call_user_ID;
768
769 if (call->state == AFS_CALL_REPLYING)
770 call->state = AFS_CALL_AWAIT_ACK;
771}
772
773/*
David Howells08e0e7c2007-04-26 15:55:03 -0700774 * send an empty reply
775 */
776void afs_send_empty_reply(struct afs_call *call)
777{
David Howellsf044c882017-11-02 15:27:45 +0000778 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700779 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700780
781 _enter("");
782
David Howellsf044c882017-11-02 15:27:45 +0000783 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100784
David Howells08e0e7c2007-04-26 15:55:03 -0700785 msg.msg_name = NULL;
786 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100787 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700788 msg.msg_control = NULL;
789 msg.msg_controllen = 0;
790 msg.msg_flags = 0;
791
792 call->state = AFS_CALL_AWAIT_ACK;
David Howellsf044c882017-11-02 15:27:45 +0000793 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100794 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700795 case 0:
796 _leave(" [replied]");
797 return;
798
799 case -ENOMEM:
800 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000801 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100802 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700803 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700804 _leave(" [error]");
805 return;
806 }
807}
808
809/*
David Howellsb908fe62007-04-26 15:58:17 -0700810 * send a simple reply
811 */
812void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
813{
David Howellsf044c882017-11-02 15:27:45 +0000814 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700815 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500816 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100817 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700818
819 _enter("");
820
David Howellsf044c882017-11-02 15:27:45 +0000821 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100822
David Howellsb908fe62007-04-26 15:58:17 -0700823 iov[0].iov_base = (void *) buf;
824 iov[0].iov_len = len;
825 msg.msg_name = NULL;
826 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500827 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700828 msg.msg_control = NULL;
829 msg.msg_controllen = 0;
830 msg.msg_flags = 0;
831
832 call->state = AFS_CALL_AWAIT_ACK;
David Howellsf044c882017-11-02 15:27:45 +0000833 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100834 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100835 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100836 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700837 _leave(" [replied]");
838 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100839 }
David Howells6c67c7c2014-05-21 14:48:05 +0100840
David Howellsbd6dc742007-07-20 10:59:41 +0100841 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700842 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000843 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100844 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700845 }
David Howellsbd6dc742007-07-20 10:59:41 +0100846 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700847}
848
849/*
David Howells372ee162016-08-03 14:11:40 +0100850 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700851 */
David Howellsd0016482016-08-30 20:42:14 +0100852int afs_extract_data(struct afs_call *call, void *buf, size_t count,
853 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700854{
David Howellsf044c882017-11-02 15:27:45 +0000855 struct afs_net *net = call->net;
David Howellsd0016482016-08-30 20:42:14 +0100856 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700857
David Howellsd0016482016-08-30 20:42:14 +0100858 _enter("{%s,%zu},,%zu,%d",
859 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700860
David Howellsd0016482016-08-30 20:42:14 +0100861 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700862
David Howellsf044c882017-11-02 15:27:45 +0000863 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100864 buf, count, &call->offset,
David Howellsa68f4a22017-10-18 11:36:39 +0100865 want_more, &call->abort_code,
866 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000867 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100868 if (ret == 0 || ret == -EAGAIN)
869 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700870
David Howellsd0016482016-08-30 20:42:14 +0100871 if (ret == 1) {
872 switch (call->state) {
873 case AFS_CALL_AWAIT_REPLY:
874 call->state = AFS_CALL_COMPLETE;
875 break;
876 case AFS_CALL_AWAIT_REQUEST:
877 call->state = AFS_CALL_REPLYING;
878 break;
879 default:
880 break;
881 }
882 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700883 }
David Howellsd0016482016-08-30 20:42:14 +0100884
885 if (ret == -ECONNABORTED)
David Howellsf780c8e2017-11-02 15:27:48 +0000886 call->error = afs_abort_to_error(call->abort_code);
David Howellsd0016482016-08-30 20:42:14 +0100887 else
888 call->error = ret;
889 call->state = AFS_CALL_COMPLETE;
890 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700891}