blob: 06a51d70b82b6e7d3028fc02a85c5ebda3004663 [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* Maintain an RxRPC server socket to do AFS communications through
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
14
David Howells08e0e7c2007-04-26 15:55:03 -070015#include <net/sock.h>
16#include <net/af_rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070017#include "internal.h"
18#include "afs_cm.h"
19
David Howellsf044c882017-11-02 15:27:45 +000020struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070021
David Howellsd0016482016-08-30 20:42:14 +010022static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd2ddc772017-11-02 15:27:50 +000023static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
David Howellsd0016482016-08-30 20:42:14 +010024static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010025static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010026static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
27static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010028static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070029
David Howells08e0e7c2007-04-26 15:55:03 -070030/* asynchronous incoming call initial processing */
31static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070032 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070033 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070034};
35
David Howells08e0e7c2007-04-26 15:55:03 -070036/*
37 * open an RxRPC socket and bind it to be a server for callback notifications
38 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
39 */
David Howellsf044c882017-11-02 15:27:45 +000040int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070041{
42 struct sockaddr_rxrpc srx;
43 struct socket *socket;
44 int ret;
45
46 _enter("");
47
David Howells3838d3e2017-11-02 15:27:47 +000048 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010049 if (ret < 0)
50 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070051
52 socket->sk->sk_allocation = GFP_NOFS;
53
54 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000055 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070056 srx.srx_family = AF_RXRPC;
57 srx.srx_service = CM_SERVICE;
58 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000059 srx.transport_len = sizeof(srx.transport.sin6);
60 srx.transport.sin6.sin6_family = AF_INET6;
61 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
David Howells08e0e7c2007-04-26 15:55:03 -070062
63 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
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 Howellsd2ddc772017-11-02 15:27:50 +0000165 afs_put_cb_interest(call->net, call->cbi);
David Howells341f7412017-01-05 10:38:36 +0000166 kfree(call->request);
167 kfree(call);
168
David Howellsf044c882017-11-02 15:27:45 +0000169 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000170 trace_afs_call(call, afs_call_trace_free, 0, o,
171 __builtin_return_address(0));
172 if (o == 0)
David Howellsf044c882017-11-02 15:27:45 +0000173 wake_up_atomic_t(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100174 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100175}
176
177/*
David Howells341f7412017-01-05 10:38:36 +0000178 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100179 */
David Howells341f7412017-01-05 10:38:36 +0000180int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100181{
David Howells341f7412017-01-05 10:38:36 +0000182 int u = atomic_inc_return(&call->usage);
183
184 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000185 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000186 __builtin_return_address(0));
187
188 INIT_WORK(&call->work, call->type->work);
189
190 if (!queue_work(afs_wq, &call->work))
191 afs_put_call(call);
192 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100193}
194
195/*
David Howells08e0e7c2007-04-26 15:55:03 -0700196 * allocate a call with flat request and reply buffers
197 */
David Howellsf044c882017-11-02 15:27:45 +0000198struct afs_call *afs_alloc_flat_call(struct afs_net *net,
199 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100200 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700201{
202 struct afs_call *call;
203
David Howellsf044c882017-11-02 15:27:45 +0000204 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700205 if (!call)
206 goto nomem_call;
207
David Howells00d3b7a2007-04-26 15:57:07 -0700208 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000209 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700210 call->request = kmalloc(request_size, GFP_NOFS);
211 if (!call->request)
212 goto nomem_free;
213 }
214
David Howellsd0016482016-08-30 20:42:14 +0100215 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000216 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100217 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700218 if (!call->buffer)
219 goto nomem_free;
220 }
221
David Howells025db802017-11-02 15:27:51 +0000222 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700223 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700224 return call;
225
David Howells00d3b7a2007-04-26 15:57:07 -0700226nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000227 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700228nomem_call:
229 return NULL;
230}
231
232/*
233 * clean up a call with flat buffer
234 */
235void afs_flat_call_destructor(struct afs_call *call)
236{
237 _enter("");
238
239 kfree(call->request);
240 call->request = NULL;
241 kfree(call->buffer);
242 call->buffer = NULL;
243}
244
David Howells2f5705a2017-03-16 16:27:46 +0000245#define AFS_BVEC_MAX 8
246
247/*
248 * Load the given bvec with the next few pages.
249 */
250static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
251 struct bio_vec *bv, pgoff_t first, pgoff_t last,
252 unsigned offset)
253{
254 struct page *pages[AFS_BVEC_MAX];
255 unsigned int nr, n, i, to, bytes = 0;
256
257 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
258 n = find_get_pages_contig(call->mapping, first, nr, pages);
259 ASSERTCMP(n, ==, nr);
260
261 msg->msg_flags |= MSG_MORE;
262 for (i = 0; i < nr; i++) {
263 to = PAGE_SIZE;
264 if (first + i >= last) {
265 to = call->last_to;
266 msg->msg_flags &= ~MSG_MORE;
267 }
268 bv[i].bv_page = pages[i];
269 bv[i].bv_len = to - offset;
270 bv[i].bv_offset = offset;
271 bytes += to - offset;
272 offset = 0;
273 }
274
275 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
276}
277
David Howells08e0e7c2007-04-26 15:55:03 -0700278/*
David Howellse8332512017-08-29 10:18:56 +0100279 * Advance the AFS call state when the RxRPC call ends the transmit phase.
280 */
281static void afs_notify_end_request_tx(struct sock *sock,
282 struct rxrpc_call *rxcall,
283 unsigned long call_user_ID)
284{
285 struct afs_call *call = (struct afs_call *)call_user_ID;
286
287 if (call->state == AFS_CALL_REQUESTING)
288 call->state = AFS_CALL_AWAIT_REPLY;
289}
290
291/*
David Howells31143d52007-05-09 02:33:46 -0700292 * attach the data from a bunch of pages on an inode to a call
293 */
Al Viro39c6ace2016-01-09 20:36:51 -0500294static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700295{
David Howells2f5705a2017-03-16 16:27:46 +0000296 struct bio_vec bv[AFS_BVEC_MAX];
297 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700298 pgoff_t first = call->first, last = call->last;
299 int ret;
300
David Howells31143d52007-05-09 02:33:46 -0700301 offset = call->first_offset;
302 call->first_offset = 0;
303
304 do {
David Howells2f5705a2017-03-16 16:27:46 +0000305 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000306 trace_afs_send_pages(call, msg, first, last, offset);
307
David Howells2f5705a2017-03-16 16:27:46 +0000308 offset = 0;
309 bytes = msg->msg_iter.count;
310 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700311
David Howellsf044c882017-11-02 15:27:45 +0000312 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100313 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000314 for (loop = 0; loop < nr; loop++)
315 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700316 if (ret < 0)
317 break;
David Howells2f5705a2017-03-16 16:27:46 +0000318
319 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700320 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700321
David Howells2c099012017-11-02 15:27:51 +0000322 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700323 return ret;
324}
325
326/*
David Howells08e0e7c2007-04-26 15:55:03 -0700327 * initiate a call
328 */
David Howells8b2a4642017-11-02 15:27:50 +0000329long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000330 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700331{
David Howells8b2a4642017-11-02 15:27:50 +0000332 struct sockaddr_rxrpc *srx = ac->addr;
David Howells08e0e7c2007-04-26 15:55:03 -0700333 struct rxrpc_call *rxcall;
334 struct msghdr msg;
335 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000336 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100337 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700338 int ret;
339
David Howells4d9df982017-11-02 15:27:47 +0000340 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700341
David Howells00d3b7a2007-04-26 15:57:07 -0700342 ASSERT(call->type != NULL);
343 ASSERT(call->type->name != NULL);
344
David Howells31143d52007-05-09 02:33:46 -0700345 _debug("____MAKE %p{%s,%x} [%d]____",
346 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000347 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700348
David Howells56ff9c82017-01-05 10:38:36 +0000349 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700350
David Howellse754eba2017-06-07 12:40:03 +0100351 /* Work out the length we're going to transmit. This is awkward for
352 * calls such as FS.StoreData where there's an extra injection of data
353 * after the initial fixed part.
354 */
355 tx_total_len = call->request_size;
356 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000357 if (call->last == call->first) {
358 tx_total_len += call->last_to - call->first_offset;
359 } else {
360 /* It looks mathematically like you should be able to
361 * combine the following lines with the ones above, but
362 * unsigned arithmetic is fun when it wraps...
363 */
364 tx_total_len += PAGE_SIZE - call->first_offset;
365 tx_total_len += call->last_to;
366 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
367 }
David Howellse754eba2017-06-07 12:40:03 +0100368 }
369
David Howells08e0e7c2007-04-26 15:55:03 -0700370 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000371 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100372 (unsigned long)call,
373 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000374 (async ?
375 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100376 afs_wake_up_call_waiter),
377 call->upgrade);
David Howells08e0e7c2007-04-26 15:55:03 -0700378 if (IS_ERR(rxcall)) {
379 ret = PTR_ERR(rxcall);
380 goto error_kill_call;
381 }
382
383 call->rxcall = rxcall;
384
385 /* send the request */
386 iov[0].iov_base = call->request;
387 iov[0].iov_len = call->request_size;
388
389 msg.msg_name = NULL;
390 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500391 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500392 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700393 msg.msg_control = NULL;
394 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100395 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700396
David Howellsf044c882017-11-02 15:27:45 +0000397 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100398 &msg, call->request_size,
399 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700400 if (ret < 0)
401 goto error_do_abort;
402
David Howells31143d52007-05-09 02:33:46 -0700403 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500404 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700405 if (ret < 0)
406 goto error_do_abort;
407 }
408
David Howells08e0e7c2007-04-26 15:55:03 -0700409 /* at this point, an async call may no longer exist as it may have
410 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000411 if (call->async)
412 return -EINPROGRESS;
413
David Howellsd2ddc772017-11-02 15:27:50 +0000414 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700415
416error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000417 call->state = AFS_CALL_COMPLETE;
418 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000419 rxrpc_kernel_abort_call(call->net->socket, rxcall,
420 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000421 } else {
David Howells70af0e32017-03-16 16:27:47 +0000422 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000423 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000424 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000425 &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000426 ac->abort_code = call->abort_code;
427 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000428 }
David Howells025db802017-11-02 15:27:51 +0000429 call->error = ret;
430 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700431error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000432 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000433 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700434 _leave(" = %d", ret);
435 return ret;
436}
437
438/*
David Howells08e0e7c2007-04-26 15:55:03 -0700439 * deliver messages to a call
440 */
441static void afs_deliver_to_call(struct afs_call *call)
442{
David Howells08e0e7c2007-04-26 15:55:03 -0700443 u32 abort_code;
444 int ret;
445
David Howellsd0016482016-08-30 20:42:14 +0100446 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700447
David Howellsd0016482016-08-30 20:42:14 +0100448 while (call->state == AFS_CALL_AWAIT_REPLY ||
449 call->state == AFS_CALL_AWAIT_OP_ID ||
450 call->state == AFS_CALL_AWAIT_REQUEST ||
451 call->state == AFS_CALL_AWAIT_ACK
452 ) {
453 if (call->state == AFS_CALL_AWAIT_ACK) {
454 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000455 ret = rxrpc_kernel_recv_data(call->net->socket,
456 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100457 NULL, 0, &offset, false,
David Howellsa68f4a22017-10-18 11:36:39 +0100458 &call->abort_code,
459 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000460 trace_afs_recv_data(call, 0, offset, false, ret);
461
David Howellsd0016482016-08-30 20:42:14 +0100462 if (ret == -EINPROGRESS || ret == -EAGAIN)
463 return;
David Howells025db802017-11-02 15:27:51 +0000464 if (ret < 0)
465 call->error = ret;
466 if (ret < 0 || ret == 1)
467 goto call_complete;
David Howellsd0016482016-08-30 20:42:14 +0100468 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700469 }
470
David Howellsd0016482016-08-30 20:42:14 +0100471 ret = call->type->deliver(call);
472 switch (ret) {
473 case 0:
474 if (call->state == AFS_CALL_AWAIT_REPLY)
David Howells025db802017-11-02 15:27:51 +0000475 goto call_complete;
David Howellsd0016482016-08-30 20:42:14 +0100476 goto done;
477 case -EINPROGRESS:
478 case -EAGAIN:
479 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000480 case -ECONNABORTED:
David Howells33cd7f22017-11-02 15:27:48 +0000481 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100482 case -ENOTCONN:
483 abort_code = RX_CALL_DEAD;
David Howellsf044c882017-11-02 15:27:45 +0000484 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100485 abort_code, ret, "KNC");
David Howells70af0e32017-03-16 16:27:47 +0000486 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100487 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000488 abort_code = RXGEN_OPCODE;
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, ret, "KIV");
David Howells70af0e32017-03-16 16:27:47 +0000491 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100492 case -ENODATA:
493 case -EBADMSG:
494 case -EMSGSIZE:
495 default:
496 abort_code = RXGEN_CC_UNMARSHAL;
497 if (call->state != AFS_CALL_AWAIT_REPLY)
498 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000499 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100500 abort_code, -EBADMSG, "KUM");
David Howells70af0e32017-03-16 16:27:47 +0000501 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100502 }
David Howells08e0e7c2007-04-26 15:55:03 -0700503 }
504
David Howellsd0016482016-08-30 20:42:14 +0100505done:
506 if (call->state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000507 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100508out:
David Howells08e0e7c2007-04-26 15:55:03 -0700509 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100510 return;
511
David Howells70af0e32017-03-16 16:27:47 +0000512save_error:
David Howellsd0016482016-08-30 20:42:14 +0100513 call->error = ret;
David Howells025db802017-11-02 15:27:51 +0000514call_complete:
515 if (call->state != AFS_CALL_COMPLETE) {
516 call->state = AFS_CALL_COMPLETE;
517 trace_afs_call_done(call);
518 }
David Howellsd0016482016-08-30 20:42:14 +0100519 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700520}
521
522/*
523 * wait synchronously for a call to complete
524 */
David Howellsd2ddc772017-11-02 15:27:50 +0000525static long afs_wait_for_call_to_complete(struct afs_call *call,
526 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700527{
David Howellsbc5e3a52017-10-18 11:07:31 +0100528 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000529 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100530 u64 rtt;
531 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700532
533 DECLARE_WAITQUEUE(myself, current);
534
535 _enter("");
536
David Howellsf044c882017-11-02 15:27:45 +0000537 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100538 rtt2 = nsecs_to_jiffies64(rtt) * 2;
539 if (rtt2 < 2)
540 rtt2 = 2;
541
542 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000543 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100544
David Howells08e0e7c2007-04-26 15:55:03 -0700545 add_wait_queue(&call->waitq, &myself);
546 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100547 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700548
549 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100550 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
551 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700552 __set_current_state(TASK_RUNNING);
553 afs_deliver_to_call(call);
554 continue;
555 }
556
David Howellsbc5e3a52017-10-18 11:07:31 +0100557 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700558 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100559
David Howellsf044c882017-11-02 15:27:45 +0000560 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100561 if (timeout == 0 &&
562 life == last_life && signal_pending(current))
563 break;
564
565 if (life != last_life) {
566 timeout = rtt2;
567 last_life = life;
568 }
569
570 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700571 }
572
573 remove_wait_queue(&call->waitq, &myself);
574 __set_current_state(TASK_RUNNING);
575
David Howells954cd6d2017-03-16 16:27:49 +0000576 /* Kill off the call if it's still live. */
David Howells08e0e7c2007-04-26 15:55:03 -0700577 if (call->state < AFS_CALL_COMPLETE) {
David Howells954cd6d2017-03-16 16:27:49 +0000578 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000579 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells025db802017-11-02 15:27:51 +0000580 RX_USER_ABORT, -EINTR, "KWI")) {
David Howellsd2ddc772017-11-02 15:27:50 +0000581 call->error = -ERESTARTSYS;
David Howells025db802017-11-02 15:27:51 +0000582 trace_afs_call_done(call);
583 }
David Howells08e0e7c2007-04-26 15:55:03 -0700584 }
585
David Howellsd2ddc772017-11-02 15:27:50 +0000586 ac->abort_code = call->abort_code;
587 ac->error = call->error;
588
589 ret = ac->error;
590 switch (ret) {
591 case 0:
592 if (call->ret_reply0) {
593 ret = (long)call->reply[0];
594 call->reply[0] = NULL;
595 }
596 /* Fall through */
597 case -ECONNABORTED:
598 ac->responded = true;
599 break;
David Howells33cd7f22017-11-02 15:27:48 +0000600 }
601
David Howells08e0e7c2007-04-26 15:55:03 -0700602 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000603 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000604 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700605 return ret;
606}
607
608/*
609 * wake up a waiting call
610 */
David Howellsd0016482016-08-30 20:42:14 +0100611static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
612 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700613{
David Howellsd0016482016-08-30 20:42:14 +0100614 struct afs_call *call = (struct afs_call *)call_user_ID;
615
616 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700617 wake_up(&call->waitq);
618}
619
620/*
621 * wake up an asynchronous call
622 */
David Howellsd0016482016-08-30 20:42:14 +0100623static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
624 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700625{
David Howellsd0016482016-08-30 20:42:14 +0100626 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000627 int u;
David Howellsd0016482016-08-30 20:42:14 +0100628
David Howells8e8d7f12017-01-05 10:38:34 +0000629 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100630 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000631
632 u = __atomic_add_unless(&call->usage, 1, 0);
633 if (u != 0) {
634 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000635 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000636 __builtin_return_address(0));
637
638 if (!queue_work(afs_async_calls, &call->async_work))
639 afs_put_call(call);
640 }
David Howells08e0e7c2007-04-26 15:55:03 -0700641}
642
643/*
David Howells341f7412017-01-05 10:38:36 +0000644 * Delete an asynchronous call. The work item carries a ref to the call struct
645 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700646 */
David Howellsd0016482016-08-30 20:42:14 +0100647static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700648{
David Howellsd0016482016-08-30 20:42:14 +0100649 struct afs_call *call = container_of(work, struct afs_call, async_work);
650
David Howells08e0e7c2007-04-26 15:55:03 -0700651 _enter("");
652
David Howells341f7412017-01-05 10:38:36 +0000653 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700654
655 _leave("");
656}
657
658/*
David Howells341f7412017-01-05 10:38:36 +0000659 * Perform I/O processing on an asynchronous call. The work item carries a ref
660 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700661 */
David Howellsd0016482016-08-30 20:42:14 +0100662static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700663{
David Howellsd0016482016-08-30 20:42:14 +0100664 struct afs_call *call = container_of(work, struct afs_call, async_work);
665
David Howells08e0e7c2007-04-26 15:55:03 -0700666 _enter("");
667
David Howellsd0016482016-08-30 20:42:14 +0100668 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
669 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700670 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100671 }
David Howells08e0e7c2007-04-26 15:55:03 -0700672
David Howells56ff9c82017-01-05 10:38:36 +0000673 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000674 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700675
David Howells341f7412017-01-05 10:38:36 +0000676 /* We have two refs to release - one from the alloc and one
677 * queued with the work item - and we can't just deallocate the
678 * call because the work item may be queued again.
679 */
David Howellsd0016482016-08-30 20:42:14 +0100680 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000681 if (!queue_work(afs_async_calls, &call->async_work))
682 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700683 }
684
David Howells341f7412017-01-05 10:38:36 +0000685 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700686 _leave("");
687}
688
David Howells00e90712016-09-08 11:10:12 +0100689static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
690{
691 struct afs_call *call = (struct afs_call *)user_call_ID;
692
693 call->rxcall = rxcall;
694}
695
696/*
697 * Charge the incoming call preallocation.
698 */
David Howellsf044c882017-11-02 15:27:45 +0000699void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100700{
David Howellsf044c882017-11-02 15:27:45 +0000701 struct afs_net *net =
702 container_of(work, struct afs_net, charge_preallocation_work);
703 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100704
705 for (;;) {
706 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000707 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100708 if (!call)
709 break;
710
David Howells56ff9c82017-01-05 10:38:36 +0000711 call->async = true;
David Howells00e90712016-09-08 11:10:12 +0100712 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000713 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100714 }
715
David Howellsf044c882017-11-02 15:27:45 +0000716 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100717 afs_wake_up_async_call,
718 afs_rx_attach,
719 (unsigned long)call,
720 GFP_KERNEL) < 0)
721 break;
722 call = NULL;
723 }
David Howellsf044c882017-11-02 15:27:45 +0000724 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100725}
726
727/*
728 * Discard a preallocated call when a socket is shut down.
729 */
730static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
731 unsigned long user_call_ID)
732{
733 struct afs_call *call = (struct afs_call *)user_call_ID;
734
David Howells00e90712016-09-08 11:10:12 +0100735 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000736 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100737}
738
David Howells08e0e7c2007-04-26 15:55:03 -0700739/*
David Howellsd0016482016-08-30 20:42:14 +0100740 * Notification of an incoming call.
741 */
David Howells00e90712016-09-08 11:10:12 +0100742static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
743 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100744{
David Howellsf044c882017-11-02 15:27:45 +0000745 struct afs_net *net = afs_sock2net(sk);
746
747 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100748}
749
750/*
David Howells372ee162016-08-03 14:11:40 +0100751 * Grab the operation ID from an incoming cache manager call. The socket
752 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700753 */
David Howellsd0016482016-08-30 20:42:14 +0100754static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700755{
David Howellsd0016482016-08-30 20:42:14 +0100756 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700757
David Howellsd0016482016-08-30 20:42:14 +0100758 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700759
760 ASSERTCMP(call->offset, <, 4);
761
762 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100763 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100764 if (ret < 0)
765 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700766
David Howells50a2c952016-10-13 08:27:10 +0100767 call->operation_ID = ntohl(call->tmp);
David Howells08e0e7c2007-04-26 15:55:03 -0700768 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100769 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700770
771 /* ask the cache manager to route the call (it'll change the call type
772 * if successful) */
773 if (!afs_cm_incoming_call(call))
774 return -ENOTSUPP;
775
David Howells8e8d7f12017-01-05 10:38:34 +0000776 trace_afs_cb_call(call);
777
David Howells08e0e7c2007-04-26 15:55:03 -0700778 /* pass responsibility for the remainer of this message off to the
779 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100780 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700781}
782
783/*
David Howellse8332512017-08-29 10:18:56 +0100784 * Advance the AFS call state when an RxRPC service call ends the transmit
785 * phase.
786 */
787static void afs_notify_end_reply_tx(struct sock *sock,
788 struct rxrpc_call *rxcall,
789 unsigned long call_user_ID)
790{
791 struct afs_call *call = (struct afs_call *)call_user_ID;
792
793 if (call->state == AFS_CALL_REPLYING)
794 call->state = AFS_CALL_AWAIT_ACK;
795}
796
797/*
David Howells08e0e7c2007-04-26 15:55:03 -0700798 * send an empty reply
799 */
800void afs_send_empty_reply(struct afs_call *call)
801{
David Howellsf044c882017-11-02 15:27:45 +0000802 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700803 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700804
805 _enter("");
806
David Howellsf044c882017-11-02 15:27:45 +0000807 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100808
David Howells08e0e7c2007-04-26 15:55:03 -0700809 msg.msg_name = NULL;
810 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100811 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700812 msg.msg_control = NULL;
813 msg.msg_controllen = 0;
814 msg.msg_flags = 0;
815
David Howellsf044c882017-11-02 15:27:45 +0000816 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100817 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700818 case 0:
819 _leave(" [replied]");
820 return;
821
822 case -ENOMEM:
823 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000824 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100825 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700826 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700827 _leave(" [error]");
828 return;
829 }
830}
831
832/*
David Howellsb908fe62007-04-26 15:58:17 -0700833 * send a simple reply
834 */
835void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
836{
David Howellsf044c882017-11-02 15:27:45 +0000837 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700838 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500839 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100840 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700841
842 _enter("");
843
David Howellsf044c882017-11-02 15:27:45 +0000844 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100845
David Howellsb908fe62007-04-26 15:58:17 -0700846 iov[0].iov_base = (void *) buf;
847 iov[0].iov_len = len;
848 msg.msg_name = NULL;
849 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500850 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700851 msg.msg_control = NULL;
852 msg.msg_controllen = 0;
853 msg.msg_flags = 0;
854
David Howellsf044c882017-11-02 15:27:45 +0000855 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100856 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100857 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100858 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700859 _leave(" [replied]");
860 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100861 }
David Howells6c67c7c2014-05-21 14:48:05 +0100862
David Howellsbd6dc742007-07-20 10:59:41 +0100863 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700864 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000865 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100866 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700867 }
David Howellsbd6dc742007-07-20 10:59:41 +0100868 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700869}
870
871/*
David Howells372ee162016-08-03 14:11:40 +0100872 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700873 */
David Howellsd0016482016-08-30 20:42:14 +0100874int afs_extract_data(struct afs_call *call, void *buf, size_t count,
875 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700876{
David Howellsf044c882017-11-02 15:27:45 +0000877 struct afs_net *net = call->net;
David Howellsd0016482016-08-30 20:42:14 +0100878 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700879
David Howellsd0016482016-08-30 20:42:14 +0100880 _enter("{%s,%zu},,%zu,%d",
881 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700882
David Howellsd0016482016-08-30 20:42:14 +0100883 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700884
David Howellsf044c882017-11-02 15:27:45 +0000885 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100886 buf, count, &call->offset,
David Howellsa68f4a22017-10-18 11:36:39 +0100887 want_more, &call->abort_code,
888 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000889 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100890 if (ret == 0 || ret == -EAGAIN)
891 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700892
David Howellsd0016482016-08-30 20:42:14 +0100893 if (ret == 1) {
894 switch (call->state) {
895 case AFS_CALL_AWAIT_REPLY:
896 call->state = AFS_CALL_COMPLETE;
David Howells025db802017-11-02 15:27:51 +0000897 trace_afs_call_done(call);
David Howellsd0016482016-08-30 20:42:14 +0100898 break;
899 case AFS_CALL_AWAIT_REQUEST:
900 call->state = AFS_CALL_REPLYING;
901 break;
902 default:
903 break;
904 }
905 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700906 }
David Howellsd0016482016-08-30 20:42:14 +0100907
David Howellsd2ddc772017-11-02 15:27:50 +0000908 call->error = ret;
David Howellsd0016482016-08-30 20:42:14 +0100909 call->state = AFS_CALL_COMPLETE;
David Howells025db802017-11-02 15:27:51 +0000910 trace_afs_call_done(call);
David Howellsd0016482016-08-30 20:42:14 +0100911 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700912}