blob: ea1460b9b71aab5900b342e28641924a0f7b9f04 [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* Maintain an RxRPC server socket to do AFS communications through
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
14
David Howells08e0e7c2007-04-26 15:55:03 -070015#include <net/sock.h>
16#include <net/af_rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070017#include "internal.h"
18#include "afs_cm.h"
19
David Howellsf044c882017-11-02 15:27:45 +000020struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070021
David Howellsd0016482016-08-30 20:42:14 +010022static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd2ddc772017-11-02 15:27:50 +000023static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
David Howellsd0016482016-08-30 20:42:14 +010024static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010025static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010026static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
27static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010028static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070029
David Howells08e0e7c2007-04-26 15:55:03 -070030/* asynchronous incoming call initial processing */
31static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070032 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070033 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070034};
35
David Howells08e0e7c2007-04-26 15:55:03 -070036/*
37 * open an RxRPC socket and bind it to be a server for callback notifications
38 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
39 */
David Howellsf044c882017-11-02 15:27:45 +000040int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070041{
42 struct sockaddr_rxrpc srx;
43 struct socket *socket;
44 int ret;
45
46 _enter("");
47
David Howells3838d3e2017-11-02 15:27:47 +000048 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010049 if (ret < 0)
50 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070051
52 socket->sk->sk_allocation = GFP_NOFS;
53
54 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000055 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070056 srx.srx_family = AF_RXRPC;
57 srx.srx_service = CM_SERVICE;
58 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000059 srx.transport_len = sizeof(srx.transport.sin6);
60 srx.transport.sin6.sin6_family = AF_INET6;
61 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
David Howells08e0e7c2007-04-26 15:55:03 -070062
63 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000064 if (ret == -EADDRINUSE) {
65 srx.transport.sin6.sin6_port = 0;
66 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
67 }
David Howells0e119b42016-06-10 22:30:37 +010068 if (ret < 0)
69 goto error_2;
70
David Howells00e90712016-09-08 11:10:12 +010071 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
72 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010073
David Howells0e119b42016-06-10 22:30:37 +010074 ret = kernel_listen(socket, INT_MAX);
75 if (ret < 0)
76 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070077
David Howellsf044c882017-11-02 15:27:45 +000078 net->socket = socket;
79 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -070080 _leave(" = 0");
81 return 0;
David Howells0e119b42016-06-10 22:30:37 +010082
83error_2:
84 sock_release(socket);
85error_1:
David Howells0e119b42016-06-10 22:30:37 +010086 _leave(" = %d", ret);
87 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -070088}
89
90/*
91 * close the RxRPC socket AFS was using
92 */
David Howellsf044c882017-11-02 15:27:45 +000093void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070094{
95 _enter("");
96
David Howellsf044c882017-11-02 15:27:45 +000097 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +000098 flush_workqueue(afs_async_calls);
99
David Howellsf044c882017-11-02 15:27:45 +0000100 if (net->spare_incoming_call) {
101 afs_put_call(net->spare_incoming_call);
102 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100103 }
104
David Howellsf044c882017-11-02 15:27:45 +0000105 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
106 wait_on_atomic_t(&net->nr_outstanding_calls, atomic_t_wait,
David Howells2f02f7a2016-04-07 17:23:03 +0100107 TASK_UNINTERRUPTIBLE);
108 _debug("no outstanding calls");
109
David Howellsf044c882017-11-02 15:27:45 +0000110 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100111 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000112 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700113
114 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700115 _leave("");
116}
117
118/*
David Howells341f7412017-01-05 10:38:36 +0000119 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700120 */
David Howellsf044c882017-11-02 15:27:45 +0000121static struct afs_call *afs_alloc_call(struct afs_net *net,
122 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000123 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700124{
David Howells341f7412017-01-05 10:38:36 +0000125 struct afs_call *call;
126 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700127
David Howells341f7412017-01-05 10:38:36 +0000128 call = kzalloc(sizeof(*call), gfp);
129 if (!call)
130 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700131
David Howells341f7412017-01-05 10:38:36 +0000132 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000133 call->net = net;
David Howells341f7412017-01-05 10:38:36 +0000134 atomic_set(&call->usage, 1);
135 INIT_WORK(&call->async_work, afs_process_async_call);
136 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000137 spin_lock_init(&call->state_lock);
David Howells2f02f7a2016-04-07 17:23:03 +0100138
David Howellsf044c882017-11-02 15:27:45 +0000139 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000140 trace_afs_call(call, afs_call_trace_alloc, 1, o,
141 __builtin_return_address(0));
142 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700143}
144
145/*
David Howells341f7412017-01-05 10:38:36 +0000146 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100147 */
David Howells341f7412017-01-05 10:38:36 +0000148void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100149{
David Howellsf044c882017-11-02 15:27:45 +0000150 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000151 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000152 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000153
154 trace_afs_call(call, afs_call_trace_put, n + 1, o,
155 __builtin_return_address(0));
156
157 ASSERTCMP(n, >=, 0);
158 if (n == 0) {
159 ASSERT(!work_pending(&call->async_work));
160 ASSERT(call->type->name != NULL);
161
162 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000163 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000164 call->rxcall = NULL;
165 }
166 if (call->type->destructor)
167 call->type->destructor(call);
168
David Howellsd0676a12017-11-02 15:27:49 +0000169 afs_put_server(call->net, call->cm_server);
David Howellsd2ddc772017-11-02 15:27:50 +0000170 afs_put_cb_interest(call->net, call->cbi);
David Howells341f7412017-01-05 10:38:36 +0000171 kfree(call->request);
172 kfree(call);
173
David Howellsf044c882017-11-02 15:27:45 +0000174 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000175 trace_afs_call(call, afs_call_trace_free, 0, o,
176 __builtin_return_address(0));
177 if (o == 0)
David Howellsf044c882017-11-02 15:27:45 +0000178 wake_up_atomic_t(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100179 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100180}
181
182/*
David Howells341f7412017-01-05 10:38:36 +0000183 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100184 */
David Howells341f7412017-01-05 10:38:36 +0000185int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100186{
David Howells341f7412017-01-05 10:38:36 +0000187 int u = atomic_inc_return(&call->usage);
188
189 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000190 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000191 __builtin_return_address(0));
192
193 INIT_WORK(&call->work, call->type->work);
194
195 if (!queue_work(afs_wq, &call->work))
196 afs_put_call(call);
197 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100198}
199
200/*
David Howells08e0e7c2007-04-26 15:55:03 -0700201 * allocate a call with flat request and reply buffers
202 */
David Howellsf044c882017-11-02 15:27:45 +0000203struct afs_call *afs_alloc_flat_call(struct afs_net *net,
204 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100205 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700206{
207 struct afs_call *call;
208
David Howellsf044c882017-11-02 15:27:45 +0000209 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700210 if (!call)
211 goto nomem_call;
212
David Howells00d3b7a2007-04-26 15:57:07 -0700213 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000214 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700215 call->request = kmalloc(request_size, GFP_NOFS);
216 if (!call->request)
217 goto nomem_free;
218 }
219
David Howellsd0016482016-08-30 20:42:14 +0100220 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000221 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100222 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700223 if (!call->buffer)
224 goto nomem_free;
225 }
226
David Howells025db802017-11-02 15:27:51 +0000227 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700228 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700229 return call;
230
David Howells00d3b7a2007-04-26 15:57:07 -0700231nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000232 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700233nomem_call:
234 return NULL;
235}
236
237/*
238 * clean up a call with flat buffer
239 */
240void afs_flat_call_destructor(struct afs_call *call)
241{
242 _enter("");
243
244 kfree(call->request);
245 call->request = NULL;
246 kfree(call->buffer);
247 call->buffer = NULL;
248}
249
David Howells2f5705a2017-03-16 16:27:46 +0000250#define AFS_BVEC_MAX 8
251
252/*
253 * Load the given bvec with the next few pages.
254 */
255static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
256 struct bio_vec *bv, pgoff_t first, pgoff_t last,
257 unsigned offset)
258{
259 struct page *pages[AFS_BVEC_MAX];
260 unsigned int nr, n, i, to, bytes = 0;
261
262 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
263 n = find_get_pages_contig(call->mapping, first, nr, pages);
264 ASSERTCMP(n, ==, nr);
265
266 msg->msg_flags |= MSG_MORE;
267 for (i = 0; i < nr; i++) {
268 to = PAGE_SIZE;
269 if (first + i >= last) {
270 to = call->last_to;
271 msg->msg_flags &= ~MSG_MORE;
272 }
273 bv[i].bv_page = pages[i];
274 bv[i].bv_len = to - offset;
275 bv[i].bv_offset = offset;
276 bytes += to - offset;
277 offset = 0;
278 }
279
280 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
281}
282
David Howells08e0e7c2007-04-26 15:55:03 -0700283/*
David Howellse8332512017-08-29 10:18:56 +0100284 * Advance the AFS call state when the RxRPC call ends the transmit phase.
285 */
286static void afs_notify_end_request_tx(struct sock *sock,
287 struct rxrpc_call *rxcall,
288 unsigned long call_user_ID)
289{
290 struct afs_call *call = (struct afs_call *)call_user_ID;
291
David Howells98bf40c2017-11-02 15:27:53 +0000292 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100293}
294
295/*
David Howells31143d52007-05-09 02:33:46 -0700296 * attach the data from a bunch of pages on an inode to a call
297 */
Al Viro39c6ace2016-01-09 20:36:51 -0500298static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700299{
David Howells2f5705a2017-03-16 16:27:46 +0000300 struct bio_vec bv[AFS_BVEC_MAX];
301 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700302 pgoff_t first = call->first, last = call->last;
303 int ret;
304
David Howells31143d52007-05-09 02:33:46 -0700305 offset = call->first_offset;
306 call->first_offset = 0;
307
308 do {
David Howells2f5705a2017-03-16 16:27:46 +0000309 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000310 trace_afs_send_pages(call, msg, first, last, offset);
311
David Howells2f5705a2017-03-16 16:27:46 +0000312 offset = 0;
313 bytes = msg->msg_iter.count;
314 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700315
David Howellsf044c882017-11-02 15:27:45 +0000316 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100317 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000318 for (loop = 0; loop < nr; loop++)
319 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700320 if (ret < 0)
321 break;
David Howells2f5705a2017-03-16 16:27:46 +0000322
323 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700324 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700325
David Howells2c099012017-11-02 15:27:51 +0000326 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700327 return ret;
328}
329
330/*
David Howells08e0e7c2007-04-26 15:55:03 -0700331 * initiate a call
332 */
David Howells8b2a4642017-11-02 15:27:50 +0000333long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000334 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700335{
David Howells8b2a4642017-11-02 15:27:50 +0000336 struct sockaddr_rxrpc *srx = ac->addr;
David Howells08e0e7c2007-04-26 15:55:03 -0700337 struct rxrpc_call *rxcall;
338 struct msghdr msg;
339 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000340 size_t offset;
David Howellse754eba2017-06-07 12:40:03 +0100341 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700342 int ret;
343
David Howells4d9df982017-11-02 15:27:47 +0000344 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700345
David Howells00d3b7a2007-04-26 15:57:07 -0700346 ASSERT(call->type != NULL);
347 ASSERT(call->type->name != NULL);
348
David Howells31143d52007-05-09 02:33:46 -0700349 _debug("____MAKE %p{%s,%x} [%d]____",
350 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000351 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700352
David Howells56ff9c82017-01-05 10:38:36 +0000353 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700354
David Howellse754eba2017-06-07 12:40:03 +0100355 /* Work out the length we're going to transmit. This is awkward for
356 * calls such as FS.StoreData where there's an extra injection of data
357 * after the initial fixed part.
358 */
359 tx_total_len = call->request_size;
360 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000361 if (call->last == call->first) {
362 tx_total_len += call->last_to - call->first_offset;
363 } else {
364 /* It looks mathematically like you should be able to
365 * combine the following lines with the ones above, but
366 * unsigned arithmetic is fun when it wraps...
367 */
368 tx_total_len += PAGE_SIZE - call->first_offset;
369 tx_total_len += call->last_to;
370 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
371 }
David Howellse754eba2017-06-07 12:40:03 +0100372 }
373
David Howells08e0e7c2007-04-26 15:55:03 -0700374 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000375 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100376 (unsigned long)call,
377 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000378 (async ?
379 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100380 afs_wake_up_call_waiter),
381 call->upgrade);
David Howells08e0e7c2007-04-26 15:55:03 -0700382 if (IS_ERR(rxcall)) {
383 ret = PTR_ERR(rxcall);
384 goto error_kill_call;
385 }
386
387 call->rxcall = rxcall;
388
389 /* send the request */
390 iov[0].iov_base = call->request;
391 iov[0].iov_len = call->request_size;
392
393 msg.msg_name = NULL;
394 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500395 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500396 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700397 msg.msg_control = NULL;
398 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100399 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700400
David Howellsf044c882017-11-02 15:27:45 +0000401 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100402 &msg, call->request_size,
403 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700404 if (ret < 0)
405 goto error_do_abort;
406
David Howells31143d52007-05-09 02:33:46 -0700407 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500408 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700409 if (ret < 0)
410 goto error_do_abort;
411 }
412
David Howells08e0e7c2007-04-26 15:55:03 -0700413 /* at this point, an async call may no longer exist as it may have
414 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000415 if (call->async)
416 return -EINPROGRESS;
417
David Howellsd2ddc772017-11-02 15:27:50 +0000418 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700419
420error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000421 call->state = AFS_CALL_COMPLETE;
422 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000423 rxrpc_kernel_abort_call(call->net->socket, rxcall,
424 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000425 } else {
David Howells70af0e32017-03-16 16:27:47 +0000426 offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000427 rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL,
David Howells33cd7f22017-11-02 15:27:48 +0000428 0, &offset, false, &call->abort_code,
David Howellsf044c882017-11-02 15:27:45 +0000429 &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000430 ac->abort_code = call->abort_code;
431 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000432 }
David Howells025db802017-11-02 15:27:51 +0000433 call->error = ret;
434 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700435error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000436 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000437 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700438 _leave(" = %d", ret);
439 return ret;
440}
441
442/*
David Howells08e0e7c2007-04-26 15:55:03 -0700443 * deliver messages to a call
444 */
445static void afs_deliver_to_call(struct afs_call *call)
446{
David Howells98bf40c2017-11-02 15:27:53 +0000447 enum afs_call_state state;
448 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700449 int ret;
450
David Howellsd0016482016-08-30 20:42:14 +0100451 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700452
David Howells98bf40c2017-11-02 15:27:53 +0000453 while (state = READ_ONCE(call->state),
454 state == AFS_CALL_CL_AWAIT_REPLY ||
455 state == AFS_CALL_SV_AWAIT_OP_ID ||
456 state == AFS_CALL_SV_AWAIT_REQUEST ||
457 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100458 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000459 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howellsd0016482016-08-30 20:42:14 +0100460 size_t offset = 0;
David Howellsf044c882017-11-02 15:27:45 +0000461 ret = rxrpc_kernel_recv_data(call->net->socket,
462 call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100463 NULL, 0, &offset, false,
David Howells98bf40c2017-11-02 15:27:53 +0000464 &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100465 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000466 trace_afs_recv_data(call, 0, offset, false, ret);
467
David Howellsd0016482016-08-30 20:42:14 +0100468 if (ret == -EINPROGRESS || ret == -EAGAIN)
469 return;
David Howells98bf40c2017-11-02 15:27:53 +0000470 if (ret < 0 || ret == 1) {
471 if (ret == 1)
472 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000473 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000474 }
David Howellsd0016482016-08-30 20:42:14 +0100475 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700476 }
477
David Howellsd0016482016-08-30 20:42:14 +0100478 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000479 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100480 switch (ret) {
481 case 0:
David Howells98bf40c2017-11-02 15:27:53 +0000482 if (state == AFS_CALL_CL_PROC_REPLY)
David Howells025db802017-11-02 15:27:51 +0000483 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000484 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100485 goto done;
486 case -EINPROGRESS:
487 case -EAGAIN:
488 goto out;
David Howells98bf40c2017-11-02 15:27:53 +0000489 case -EIO:
David Howells70af0e32017-03-16 16:27:47 +0000490 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000491 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
492 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100493 case -ENOTCONN:
494 abort_code = RX_CALL_DEAD;
David Howellsf044c882017-11-02 15:27:45 +0000495 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100496 abort_code, ret, "KNC");
David Howells98bf40c2017-11-02 15:27:53 +0000497 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100498 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000499 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000500 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100501 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000502 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100503 case -ENODATA:
504 case -EBADMSG:
505 case -EMSGSIZE:
506 default:
507 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000508 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100509 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000510 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100511 abort_code, -EBADMSG, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000512 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100513 }
David Howells08e0e7c2007-04-26 15:55:03 -0700514 }
515
David Howellsd0016482016-08-30 20:42:14 +0100516done:
David Howells98bf40c2017-11-02 15:27:53 +0000517 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000518 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100519out:
David Howells08e0e7c2007-04-26 15:55:03 -0700520 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100521 return;
522
David Howells98bf40c2017-11-02 15:27:53 +0000523local_abort:
524 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000525call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000526 afs_set_call_complete(call, ret, remote_abort);
527 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100528 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700529}
530
531/*
532 * wait synchronously for a call to complete
533 */
David Howellsd2ddc772017-11-02 15:27:50 +0000534static long afs_wait_for_call_to_complete(struct afs_call *call,
535 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700536{
David Howellsbc5e3a52017-10-18 11:07:31 +0100537 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000538 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100539 u64 rtt;
540 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700541
542 DECLARE_WAITQUEUE(myself, current);
543
544 _enter("");
545
David Howellsf044c882017-11-02 15:27:45 +0000546 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100547 rtt2 = nsecs_to_jiffies64(rtt) * 2;
548 if (rtt2 < 2)
549 rtt2 = 2;
550
551 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000552 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100553
David Howells08e0e7c2007-04-26 15:55:03 -0700554 add_wait_queue(&call->waitq, &myself);
555 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100556 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700557
558 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000559 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
560 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100561 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700562 __set_current_state(TASK_RUNNING);
563 afs_deliver_to_call(call);
564 continue;
565 }
566
David Howells98bf40c2017-11-02 15:27:53 +0000567 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700568 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100569
David Howellsf044c882017-11-02 15:27:45 +0000570 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100571 if (timeout == 0 &&
572 life == last_life && signal_pending(current))
573 break;
574
575 if (life != last_life) {
576 timeout = rtt2;
577 last_life = life;
578 }
579
580 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700581 }
582
583 remove_wait_queue(&call->waitq, &myself);
584 __set_current_state(TASK_RUNNING);
585
David Howells954cd6d2017-03-16 16:27:49 +0000586 /* Kill off the call if it's still live. */
David Howells98bf40c2017-11-02 15:27:53 +0000587 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Howells954cd6d2017-03-16 16:27:49 +0000588 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000589 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells98bf40c2017-11-02 15:27:53 +0000590 RX_USER_ABORT, -EINTR, "KWI"))
591 afs_set_call_complete(call, -EINTR, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700592 }
593
David Howells98bf40c2017-11-02 15:27:53 +0000594 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000595 ac->abort_code = call->abort_code;
596 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000597 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000598
599 ret = ac->error;
600 switch (ret) {
601 case 0:
602 if (call->ret_reply0) {
603 ret = (long)call->reply[0];
604 call->reply[0] = NULL;
605 }
606 /* Fall through */
607 case -ECONNABORTED:
608 ac->responded = true;
609 break;
David Howells33cd7f22017-11-02 15:27:48 +0000610 }
611
David Howells08e0e7c2007-04-26 15:55:03 -0700612 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000613 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000614 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700615 return ret;
616}
617
618/*
619 * wake up a waiting call
620 */
David Howellsd0016482016-08-30 20:42:14 +0100621static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
622 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700623{
David Howellsd0016482016-08-30 20:42:14 +0100624 struct afs_call *call = (struct afs_call *)call_user_ID;
625
626 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700627 wake_up(&call->waitq);
628}
629
630/*
631 * wake up an asynchronous call
632 */
David Howellsd0016482016-08-30 20:42:14 +0100633static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
634 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700635{
David Howellsd0016482016-08-30 20:42:14 +0100636 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000637 int u;
David Howellsd0016482016-08-30 20:42:14 +0100638
David Howells8e8d7f12017-01-05 10:38:34 +0000639 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100640 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000641
642 u = __atomic_add_unless(&call->usage, 1, 0);
643 if (u != 0) {
644 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000645 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000646 __builtin_return_address(0));
647
648 if (!queue_work(afs_async_calls, &call->async_work))
649 afs_put_call(call);
650 }
David Howells08e0e7c2007-04-26 15:55:03 -0700651}
652
653/*
David Howells341f7412017-01-05 10:38:36 +0000654 * Delete an asynchronous call. The work item carries a ref to the call struct
655 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700656 */
David Howellsd0016482016-08-30 20:42:14 +0100657static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700658{
David Howellsd0016482016-08-30 20:42:14 +0100659 struct afs_call *call = container_of(work, struct afs_call, async_work);
660
David Howells08e0e7c2007-04-26 15:55:03 -0700661 _enter("");
662
David Howells341f7412017-01-05 10:38:36 +0000663 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700664
665 _leave("");
666}
667
668/*
David Howells341f7412017-01-05 10:38:36 +0000669 * Perform I/O processing on an asynchronous call. The work item carries a ref
670 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700671 */
David Howellsd0016482016-08-30 20:42:14 +0100672static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700673{
David Howellsd0016482016-08-30 20:42:14 +0100674 struct afs_call *call = container_of(work, struct afs_call, async_work);
675
David Howells08e0e7c2007-04-26 15:55:03 -0700676 _enter("");
677
David Howellsd0016482016-08-30 20:42:14 +0100678 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
679 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700680 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100681 }
David Howells08e0e7c2007-04-26 15:55:03 -0700682
David Howells56ff9c82017-01-05 10:38:36 +0000683 if (call->state == AFS_CALL_COMPLETE) {
David Howells97e30432017-11-02 15:27:48 +0000684 call->reply[0] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700685
David Howells341f7412017-01-05 10:38:36 +0000686 /* We have two refs to release - one from the alloc and one
687 * queued with the work item - and we can't just deallocate the
688 * call because the work item may be queued again.
689 */
David Howellsd0016482016-08-30 20:42:14 +0100690 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000691 if (!queue_work(afs_async_calls, &call->async_work))
692 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700693 }
694
David Howells341f7412017-01-05 10:38:36 +0000695 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700696 _leave("");
697}
698
David Howells00e90712016-09-08 11:10:12 +0100699static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
700{
701 struct afs_call *call = (struct afs_call *)user_call_ID;
702
703 call->rxcall = rxcall;
704}
705
706/*
707 * Charge the incoming call preallocation.
708 */
David Howellsf044c882017-11-02 15:27:45 +0000709void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100710{
David Howellsf044c882017-11-02 15:27:45 +0000711 struct afs_net *net =
712 container_of(work, struct afs_net, charge_preallocation_work);
713 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100714
715 for (;;) {
716 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000717 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100718 if (!call)
719 break;
720
David Howells56ff9c82017-01-05 10:38:36 +0000721 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000722 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000723 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100724 }
725
David Howellsf044c882017-11-02 15:27:45 +0000726 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100727 afs_wake_up_async_call,
728 afs_rx_attach,
729 (unsigned long)call,
730 GFP_KERNEL) < 0)
731 break;
732 call = NULL;
733 }
David Howellsf044c882017-11-02 15:27:45 +0000734 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100735}
736
737/*
738 * Discard a preallocated call when a socket is shut down.
739 */
740static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
741 unsigned long user_call_ID)
742{
743 struct afs_call *call = (struct afs_call *)user_call_ID;
744
David Howells00e90712016-09-08 11:10:12 +0100745 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000746 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100747}
748
David Howells08e0e7c2007-04-26 15:55:03 -0700749/*
David Howellsd0016482016-08-30 20:42:14 +0100750 * Notification of an incoming call.
751 */
David Howells00e90712016-09-08 11:10:12 +0100752static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
753 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100754{
David Howellsf044c882017-11-02 15:27:45 +0000755 struct afs_net *net = afs_sock2net(sk);
756
757 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100758}
759
760/*
David Howells372ee162016-08-03 14:11:40 +0100761 * Grab the operation ID from an incoming cache manager call. The socket
762 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700763 */
David Howellsd0016482016-08-30 20:42:14 +0100764static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700765{
David Howellsd0016482016-08-30 20:42:14 +0100766 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700767
David Howellsd0016482016-08-30 20:42:14 +0100768 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700769
770 ASSERTCMP(call->offset, <, 4);
771
772 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100773 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100774 if (ret < 0)
775 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700776
David Howells50a2c952016-10-13 08:27:10 +0100777 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000778 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howellsd0016482016-08-30 20:42:14 +0100779 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700780
781 /* ask the cache manager to route the call (it'll change the call type
782 * if successful) */
783 if (!afs_cm_incoming_call(call))
784 return -ENOTSUPP;
785
David Howells8e8d7f12017-01-05 10:38:34 +0000786 trace_afs_cb_call(call);
787
David Howells08e0e7c2007-04-26 15:55:03 -0700788 /* pass responsibility for the remainer of this message off to the
789 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100790 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700791}
792
793/*
David Howellse8332512017-08-29 10:18:56 +0100794 * Advance the AFS call state when an RxRPC service call ends the transmit
795 * phase.
796 */
797static void afs_notify_end_reply_tx(struct sock *sock,
798 struct rxrpc_call *rxcall,
799 unsigned long call_user_ID)
800{
801 struct afs_call *call = (struct afs_call *)call_user_ID;
802
David Howells98bf40c2017-11-02 15:27:53 +0000803 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100804}
805
806/*
David Howells08e0e7c2007-04-26 15:55:03 -0700807 * send an empty reply
808 */
809void afs_send_empty_reply(struct afs_call *call)
810{
David Howellsf044c882017-11-02 15:27:45 +0000811 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700812 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700813
814 _enter("");
815
David Howellsf044c882017-11-02 15:27:45 +0000816 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100817
David Howells08e0e7c2007-04-26 15:55:03 -0700818 msg.msg_name = NULL;
819 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100820 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700821 msg.msg_control = NULL;
822 msg.msg_controllen = 0;
823 msg.msg_flags = 0;
824
David Howellsf044c882017-11-02 15:27:45 +0000825 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100826 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700827 case 0:
828 _leave(" [replied]");
829 return;
830
831 case -ENOMEM:
832 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000833 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100834 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700835 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700836 _leave(" [error]");
837 return;
838 }
839}
840
841/*
David Howellsb908fe62007-04-26 15:58:17 -0700842 * send a simple reply
843 */
844void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
845{
David Howellsf044c882017-11-02 15:27:45 +0000846 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700847 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500848 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100849 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700850
851 _enter("");
852
David Howellsf044c882017-11-02 15:27:45 +0000853 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100854
David Howellsb908fe62007-04-26 15:58:17 -0700855 iov[0].iov_base = (void *) buf;
856 iov[0].iov_len = len;
857 msg.msg_name = NULL;
858 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500859 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700860 msg.msg_control = NULL;
861 msg.msg_controllen = 0;
862 msg.msg_flags = 0;
863
David Howellsf044c882017-11-02 15:27:45 +0000864 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100865 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100866 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100867 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700868 _leave(" [replied]");
869 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100870 }
David Howells6c67c7c2014-05-21 14:48:05 +0100871
David Howellsbd6dc742007-07-20 10:59:41 +0100872 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700873 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000874 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100875 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700876 }
David Howellsbd6dc742007-07-20 10:59:41 +0100877 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700878}
879
880/*
David Howells372ee162016-08-03 14:11:40 +0100881 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700882 */
David Howellsd0016482016-08-30 20:42:14 +0100883int afs_extract_data(struct afs_call *call, void *buf, size_t count,
884 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700885{
David Howellsf044c882017-11-02 15:27:45 +0000886 struct afs_net *net = call->net;
David Howells98bf40c2017-11-02 15:27:53 +0000887 enum afs_call_state state;
888 u32 remote_abort;
David Howellsd0016482016-08-30 20:42:14 +0100889 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700890
David Howellsd0016482016-08-30 20:42:14 +0100891 _enter("{%s,%zu},,%zu,%d",
892 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700893
David Howellsd0016482016-08-30 20:42:14 +0100894 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700895
David Howellsf044c882017-11-02 15:27:45 +0000896 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall,
David Howellsd0016482016-08-30 20:42:14 +0100897 buf, count, &call->offset,
David Howells98bf40c2017-11-02 15:27:53 +0000898 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100899 &call->service_id);
David Howells8e8d7f12017-01-05 10:38:34 +0000900 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100901 if (ret == 0 || ret == -EAGAIN)
902 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700903
David Howells98bf40c2017-11-02 15:27:53 +0000904 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100905 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000906 switch (state) {
907 case AFS_CALL_CL_AWAIT_REPLY:
908 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100909 break;
David Howells98bf40c2017-11-02 15:27:53 +0000910 case AFS_CALL_SV_AWAIT_REQUEST:
911 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100912 break;
David Howells98bf40c2017-11-02 15:27:53 +0000913 case AFS_CALL_COMPLETE:
914 kdebug("prem complete %d", call->error);
915 return -EIO;
David Howellsd0016482016-08-30 20:42:14 +0100916 default:
917 break;
918 }
919 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700920 }
David Howellsd0016482016-08-30 20:42:14 +0100921
David Howells98bf40c2017-11-02 15:27:53 +0000922 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100923 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700924}