blob: 8f76b13d55494bddec9e81203c0734a0f6d811d7 [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>
17#include <rxrpc/packet.h>
18#include "internal.h"
19#include "afs_cm.h"
20
David Howells8324f0b2016-08-30 09:49:29 +010021struct socket *afs_socket; /* my RxRPC socket */
David Howells08e0e7c2007-04-26 15:55:03 -070022static struct workqueue_struct *afs_async_calls;
David Howells00e90712016-09-08 11:10:12 +010023static struct afs_call *afs_spare_incoming_call;
David Howells341f7412017-01-05 10:38:36 +000024atomic_t afs_outstanding_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070025
David Howellsd0016482016-08-30 20:42:14 +010026static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howells08e0e7c2007-04-26 15:55:03 -070027static int afs_wait_for_call_to_complete(struct afs_call *);
David Howellsd0016482016-08-30 20:42:14 +010028static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010029static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010030static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
31static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010032static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070033
David Howells08e0e7c2007-04-26 15:55:03 -070034/* asynchronous incoming call initial processing */
35static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070036 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070037 .deliver = afs_deliver_cm_op_id,
38 .abort_to_error = afs_abort_to_error,
39};
40
David Howells00e90712016-09-08 11:10:12 +010041static void afs_charge_preallocation(struct work_struct *);
David Howells08e0e7c2007-04-26 15:55:03 -070042
David Howells00e90712016-09-08 11:10:12 +010043static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
David Howells08e0e7c2007-04-26 15:55:03 -070044
David Howells2f02f7a2016-04-07 17:23:03 +010045static int afs_wait_atomic_t(atomic_t *p)
46{
47 schedule();
48 return 0;
49}
50
David Howells08e0e7c2007-04-26 15:55:03 -070051/*
52 * open an RxRPC socket and bind it to be a server for callback notifications
53 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
54 */
55int afs_open_socket(void)
56{
57 struct sockaddr_rxrpc srx;
58 struct socket *socket;
59 int ret;
60
61 _enter("");
62
David Howells0e119b42016-06-10 22:30:37 +010063 ret = -ENOMEM;
Bhaktipriya Shridhar69ad0522016-09-04 20:53:42 +053064 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
David Howells0e119b42016-06-10 22:30:37 +010065 if (!afs_async_calls)
66 goto error_0;
David Howells08e0e7c2007-04-26 15:55:03 -070067
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -050068 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
David Howells0e119b42016-06-10 22:30:37 +010069 if (ret < 0)
70 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070071
72 socket->sk->sk_allocation = GFP_NOFS;
73
74 /* bind the callback manager's address to make this a server socket */
75 srx.srx_family = AF_RXRPC;
76 srx.srx_service = CM_SERVICE;
77 srx.transport_type = SOCK_DGRAM;
78 srx.transport_len = sizeof(srx.transport.sin);
79 srx.transport.sin.sin_family = AF_INET;
80 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
81 memset(&srx.transport.sin.sin_addr, 0,
82 sizeof(srx.transport.sin.sin_addr));
83
84 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
David Howells0e119b42016-06-10 22:30:37 +010085 if (ret < 0)
86 goto error_2;
87
David Howells00e90712016-09-08 11:10:12 +010088 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
89 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010090
David Howells0e119b42016-06-10 22:30:37 +010091 ret = kernel_listen(socket, INT_MAX);
92 if (ret < 0)
93 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070094
David Howells08e0e7c2007-04-26 15:55:03 -070095 afs_socket = socket;
David Howells00e90712016-09-08 11:10:12 +010096 afs_charge_preallocation(NULL);
David Howells08e0e7c2007-04-26 15:55:03 -070097 _leave(" = 0");
98 return 0;
David Howells0e119b42016-06-10 22:30:37 +010099
100error_2:
101 sock_release(socket);
102error_1:
103 destroy_workqueue(afs_async_calls);
104error_0:
105 _leave(" = %d", ret);
106 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700107}
108
109/*
110 * close the RxRPC socket AFS was using
111 */
112void afs_close_socket(void)
113{
114 _enter("");
115
David Howells341f7412017-01-05 10:38:36 +0000116 kernel_listen(afs_socket, 0);
117 flush_workqueue(afs_async_calls);
118
David Howells00e90712016-09-08 11:10:12 +0100119 if (afs_spare_incoming_call) {
David Howells341f7412017-01-05 10:38:36 +0000120 afs_put_call(afs_spare_incoming_call);
David Howells00e90712016-09-08 11:10:12 +0100121 afs_spare_incoming_call = NULL;
122 }
123
David Howellsd0016482016-08-30 20:42:14 +0100124 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100125 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
126 TASK_UNINTERRUPTIBLE);
127 _debug("no outstanding calls");
128
David Howells248f2192016-09-08 11:10:12 +0100129 kernel_sock_shutdown(afs_socket, SHUT_RDWR);
130 flush_workqueue(afs_async_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700131 sock_release(afs_socket);
132
133 _debug("dework");
134 destroy_workqueue(afs_async_calls);
135 _leave("");
136}
137
138/*
David Howells341f7412017-01-05 10:38:36 +0000139 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700140 */
David Howells341f7412017-01-05 10:38:36 +0000141static struct afs_call *afs_alloc_call(const struct afs_call_type *type,
142 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700143{
David Howells341f7412017-01-05 10:38:36 +0000144 struct afs_call *call;
145 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700146
David Howells341f7412017-01-05 10:38:36 +0000147 call = kzalloc(sizeof(*call), gfp);
148 if (!call)
149 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700150
David Howells341f7412017-01-05 10:38:36 +0000151 call->type = type;
152 atomic_set(&call->usage, 1);
153 INIT_WORK(&call->async_work, afs_process_async_call);
154 init_waitqueue_head(&call->waitq);
David Howells2f02f7a2016-04-07 17:23:03 +0100155
David Howells341f7412017-01-05 10:38:36 +0000156 o = atomic_inc_return(&afs_outstanding_calls);
157 trace_afs_call(call, afs_call_trace_alloc, 1, o,
158 __builtin_return_address(0));
159 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700160}
161
162/*
David Howells341f7412017-01-05 10:38:36 +0000163 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100164 */
David Howells341f7412017-01-05 10:38:36 +0000165void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100166{
David Howells341f7412017-01-05 10:38:36 +0000167 int n = atomic_dec_return(&call->usage);
168 int o = atomic_read(&afs_outstanding_calls);
169
170 trace_afs_call(call, afs_call_trace_put, n + 1, o,
171 __builtin_return_address(0));
172
173 ASSERTCMP(n, >=, 0);
174 if (n == 0) {
175 ASSERT(!work_pending(&call->async_work));
176 ASSERT(call->type->name != NULL);
177
178 if (call->rxcall) {
179 rxrpc_kernel_end_call(afs_socket, call->rxcall);
180 call->rxcall = NULL;
181 }
182 if (call->type->destructor)
183 call->type->destructor(call);
184
185 kfree(call->request);
186 kfree(call);
187
188 o = atomic_dec_return(&afs_outstanding_calls);
189 trace_afs_call(call, afs_call_trace_free, 0, o,
190 __builtin_return_address(0));
191 if (o == 0)
192 wake_up_atomic_t(&afs_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100193 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100194}
195
196/*
David Howells341f7412017-01-05 10:38:36 +0000197 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100198 */
David Howells341f7412017-01-05 10:38:36 +0000199int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100200{
David Howells341f7412017-01-05 10:38:36 +0000201 int u = atomic_inc_return(&call->usage);
202
203 trace_afs_call(call, afs_call_trace_work, u,
204 atomic_read(&afs_outstanding_calls),
205 __builtin_return_address(0));
206
207 INIT_WORK(&call->work, call->type->work);
208
209 if (!queue_work(afs_wq, &call->work))
210 afs_put_call(call);
211 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100212}
213
214/*
David Howells08e0e7c2007-04-26 15:55:03 -0700215 * allocate a call with flat request and reply buffers
216 */
217struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100218 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700219{
220 struct afs_call *call;
221
David Howells341f7412017-01-05 10:38:36 +0000222 call = afs_alloc_call(type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700223 if (!call)
224 goto nomem_call;
225
David Howells00d3b7a2007-04-26 15:57:07 -0700226 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000227 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700228 call->request = kmalloc(request_size, GFP_NOFS);
229 if (!call->request)
230 goto nomem_free;
231 }
232
David Howellsd0016482016-08-30 20:42:14 +0100233 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000234 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100235 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700236 if (!call->buffer)
237 goto nomem_free;
238 }
239
David Howells08e0e7c2007-04-26 15:55:03 -0700240 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700241 return call;
242
David Howells00d3b7a2007-04-26 15:57:07 -0700243nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000244 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700245nomem_call:
246 return NULL;
247}
248
249/*
250 * clean up a call with flat buffer
251 */
252void afs_flat_call_destructor(struct afs_call *call)
253{
254 _enter("");
255
256 kfree(call->request);
257 call->request = NULL;
258 kfree(call->buffer);
259 call->buffer = NULL;
260}
261
David Howells2f5705a2017-03-16 16:27:46 +0000262#define AFS_BVEC_MAX 8
263
264/*
265 * Load the given bvec with the next few pages.
266 */
267static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
268 struct bio_vec *bv, pgoff_t first, pgoff_t last,
269 unsigned offset)
270{
271 struct page *pages[AFS_BVEC_MAX];
272 unsigned int nr, n, i, to, bytes = 0;
273
274 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
275 n = find_get_pages_contig(call->mapping, first, nr, pages);
276 ASSERTCMP(n, ==, nr);
277
278 msg->msg_flags |= MSG_MORE;
279 for (i = 0; i < nr; i++) {
280 to = PAGE_SIZE;
281 if (first + i >= last) {
282 to = call->last_to;
283 msg->msg_flags &= ~MSG_MORE;
284 }
285 bv[i].bv_page = pages[i];
286 bv[i].bv_len = to - offset;
287 bv[i].bv_offset = offset;
288 bytes += to - offset;
289 offset = 0;
290 }
291
292 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
293}
294
David Howells08e0e7c2007-04-26 15:55:03 -0700295/*
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);
310 offset = 0;
311 bytes = msg->msg_iter.count;
312 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700313
David Howells2f5705a2017-03-16 16:27:46 +0000314 /* Have to change the state *before* sending the last
315 * packet as RxRPC might give us the reply before it
316 * returns from sending the request.
317 */
David Howells445783d2017-03-16 16:27:48 +0000318 if (first + nr - 1 >= last)
David Howells2f5705a2017-03-16 16:27:46 +0000319 call->state = AFS_CALL_AWAIT_REPLY;
320 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
321 msg, bytes);
322 for (loop = 0; loop < nr; loop++)
323 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700324 if (ret < 0)
325 break;
David Howells2f5705a2017-03-16 16:27:46 +0000326
327 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700328 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700329
David Howells31143d52007-05-09 02:33:46 -0700330 return ret;
331}
332
333/*
David Howells08e0e7c2007-04-26 15:55:03 -0700334 * initiate a call
335 */
336int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000337 bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700338{
339 struct sockaddr_rxrpc srx;
340 struct rxrpc_call *rxcall;
341 struct msghdr msg;
342 struct kvec iov[1];
David Howells70af0e32017-03-16 16:27:47 +0000343 size_t offset;
344 u32 abort_code;
David Howells08e0e7c2007-04-26 15:55:03 -0700345 int ret;
346
347 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
348
David Howells00d3b7a2007-04-26 15:57:07 -0700349 ASSERT(call->type != NULL);
350 ASSERT(call->type->name != NULL);
351
David Howells31143d52007-05-09 02:33:46 -0700352 _debug("____MAKE %p{%s,%x} [%d]____",
353 call, call->type->name, key_serial(call->key),
354 atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700355
David Howells56ff9c82017-01-05 10:38:36 +0000356 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700357
358 memset(&srx, 0, sizeof(srx));
359 srx.srx_family = AF_RXRPC;
360 srx.srx_service = call->service_id;
361 srx.transport_type = SOCK_DGRAM;
362 srx.transport_len = sizeof(srx.transport.sin);
363 srx.transport.sin.sin_family = AF_INET;
364 srx.transport.sin.sin_port = call->port;
365 memcpy(&srx.transport.sin.sin_addr, addr, 4);
366
367 /* create a call */
368 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
David Howellsd0016482016-08-30 20:42:14 +0100369 (unsigned long) call, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000370 (async ?
371 afs_wake_up_async_call :
372 afs_wake_up_call_waiter));
David Howells00d3b7a2007-04-26 15:57:07 -0700373 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700374 if (IS_ERR(rxcall)) {
375 ret = PTR_ERR(rxcall);
376 goto error_kill_call;
377 }
378
379 call->rxcall = rxcall;
380
381 /* send the request */
382 iov[0].iov_base = call->request;
383 iov[0].iov_len = call->request_size;
384
385 msg.msg_name = NULL;
386 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500387 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500388 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700389 msg.msg_control = NULL;
390 msg.msg_controllen = 0;
David Howells31143d52007-05-09 02:33:46 -0700391 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700392
David Howells70af0e32017-03-16 16:27:47 +0000393 /* We have to change the state *before* sending the last packet as
394 * rxrpc might give us the reply before it returns from sending the
395 * request. Further, if the send fails, we may already have been given
396 * a notification and may have collected it.
397 */
David Howells31143d52007-05-09 02:33:46 -0700398 if (!call->send_pages)
399 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100400 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
401 &msg, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700402 if (ret < 0)
403 goto error_do_abort;
404
David Howells31143d52007-05-09 02:33:46 -0700405 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500406 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700407 if (ret < 0)
408 goto error_do_abort;
409 }
410
David Howells08e0e7c2007-04-26 15:55:03 -0700411 /* at this point, an async call may no longer exist as it may have
412 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000413 if (call->async)
414 return -EINPROGRESS;
415
416 return afs_wait_for_call_to_complete(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700417
418error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000419 call->state = AFS_CALL_COMPLETE;
420 if (ret != -ECONNABORTED) {
421 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT,
422 -ret, "KSD");
423 } else {
424 abort_code = 0;
425 offset = 0;
426 rxrpc_kernel_recv_data(afs_socket, rxcall, NULL, 0, &offset,
427 false, &abort_code);
428 ret = call->type->abort_to_error(abort_code);
429 }
David Howells08e0e7c2007-04-26 15:55:03 -0700430error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000431 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700432 _leave(" = %d", ret);
433 return ret;
434}
435
436/*
David Howells08e0e7c2007-04-26 15:55:03 -0700437 * deliver messages to a call
438 */
439static void afs_deliver_to_call(struct afs_call *call)
440{
David Howells08e0e7c2007-04-26 15:55:03 -0700441 u32 abort_code;
442 int ret;
443
David Howellsd0016482016-08-30 20:42:14 +0100444 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700445
David Howellsd0016482016-08-30 20:42:14 +0100446 while (call->state == AFS_CALL_AWAIT_REPLY ||
447 call->state == AFS_CALL_AWAIT_OP_ID ||
448 call->state == AFS_CALL_AWAIT_REQUEST ||
449 call->state == AFS_CALL_AWAIT_ACK
450 ) {
451 if (call->state == AFS_CALL_AWAIT_ACK) {
452 size_t offset = 0;
453 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
454 NULL, 0, &offset, false,
455 &call->abort_code);
David Howells8e8d7f12017-01-05 10:38:34 +0000456 trace_afs_recv_data(call, 0, offset, false, ret);
457
David Howellsd0016482016-08-30 20:42:14 +0100458 if (ret == -EINPROGRESS || ret == -EAGAIN)
459 return;
David Howells9008f992016-10-06 08:11:50 +0100460 if (ret == 1 || ret < 0) {
David Howellsd0016482016-08-30 20:42:14 +0100461 call->state = AFS_CALL_COMPLETE;
462 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700463 }
David Howellsd0016482016-08-30 20:42:14 +0100464 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700465 }
466
David Howellsd0016482016-08-30 20:42:14 +0100467 ret = call->type->deliver(call);
468 switch (ret) {
469 case 0:
470 if (call->state == AFS_CALL_AWAIT_REPLY)
471 call->state = AFS_CALL_COMPLETE;
472 goto done;
473 case -EINPROGRESS:
474 case -EAGAIN:
475 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000476 case -ECONNABORTED:
477 goto call_complete;
David Howellsd0016482016-08-30 20:42:14 +0100478 case -ENOTCONN:
479 abort_code = RX_CALL_DEAD;
480 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100481 abort_code, -ret, "KNC");
David Howells70af0e32017-03-16 16:27:47 +0000482 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100483 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000484 abort_code = RXGEN_OPCODE;
David Howellsd0016482016-08-30 20:42:14 +0100485 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100486 abort_code, -ret, "KIV");
David Howells70af0e32017-03-16 16:27:47 +0000487 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100488 case -ENODATA:
489 case -EBADMSG:
490 case -EMSGSIZE:
491 default:
492 abort_code = RXGEN_CC_UNMARSHAL;
493 if (call->state != AFS_CALL_AWAIT_REPLY)
494 abort_code = RXGEN_SS_UNMARSHAL;
495 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100496 abort_code, EBADMSG, "KUM");
David Howells70af0e32017-03-16 16:27:47 +0000497 goto save_error;
David Howellsd0016482016-08-30 20:42:14 +0100498 }
David Howells08e0e7c2007-04-26 15:55:03 -0700499 }
500
David Howellsd0016482016-08-30 20:42:14 +0100501done:
502 if (call->state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000503 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100504out:
David Howells08e0e7c2007-04-26 15:55:03 -0700505 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100506 return;
507
David Howells70af0e32017-03-16 16:27:47 +0000508save_error:
David Howellsd0016482016-08-30 20:42:14 +0100509 call->error = ret;
David Howells70af0e32017-03-16 16:27:47 +0000510call_complete:
David Howellsd0016482016-08-30 20:42:14 +0100511 call->state = AFS_CALL_COMPLETE;
512 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700513}
514
515/*
516 * wait synchronously for a call to complete
517 */
518static int afs_wait_for_call_to_complete(struct afs_call *call)
519{
David Howells08e0e7c2007-04-26 15:55:03 -0700520 int ret;
521
522 DECLARE_WAITQUEUE(myself, current);
523
524 _enter("");
525
526 add_wait_queue(&call->waitq, &myself);
527 for (;;) {
528 set_current_state(TASK_INTERRUPTIBLE);
529
530 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100531 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
532 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700533 __set_current_state(TASK_RUNNING);
534 afs_deliver_to_call(call);
535 continue;
536 }
537
David Howells954cd6d2017-03-16 16:27:49 +0000538 if (call->state == AFS_CALL_COMPLETE ||
539 signal_pending(current))
David Howells08e0e7c2007-04-26 15:55:03 -0700540 break;
541 schedule();
542 }
543
544 remove_wait_queue(&call->waitq, &myself);
545 __set_current_state(TASK_RUNNING);
546
David Howells954cd6d2017-03-16 16:27:49 +0000547 /* Kill off the call if it's still live. */
David Howells08e0e7c2007-04-26 15:55:03 -0700548 if (call->state < AFS_CALL_COMPLETE) {
David Howells954cd6d2017-03-16 16:27:49 +0000549 _debug("call interrupted");
David Howellsd0016482016-08-30 20:42:14 +0100550 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells954cd6d2017-03-16 16:27:49 +0000551 RX_USER_ABORT, -EINTR, "KWI");
David Howells08e0e7c2007-04-26 15:55:03 -0700552 }
553
David Howells954cd6d2017-03-16 16:27:49 +0000554 ret = call->error;
David Howells08e0e7c2007-04-26 15:55:03 -0700555 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000556 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700557 _leave(" = %d", ret);
558 return ret;
559}
560
561/*
562 * wake up a waiting call
563 */
David Howellsd0016482016-08-30 20:42:14 +0100564static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
565 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700566{
David Howellsd0016482016-08-30 20:42:14 +0100567 struct afs_call *call = (struct afs_call *)call_user_ID;
568
569 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700570 wake_up(&call->waitq);
571}
572
573/*
574 * wake up an asynchronous call
575 */
David Howellsd0016482016-08-30 20:42:14 +0100576static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
577 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700578{
David Howellsd0016482016-08-30 20:42:14 +0100579 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000580 int u;
David Howellsd0016482016-08-30 20:42:14 +0100581
David Howells8e8d7f12017-01-05 10:38:34 +0000582 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100583 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000584
585 u = __atomic_add_unless(&call->usage, 1, 0);
586 if (u != 0) {
587 trace_afs_call(call, afs_call_trace_wake, u,
588 atomic_read(&afs_outstanding_calls),
589 __builtin_return_address(0));
590
591 if (!queue_work(afs_async_calls, &call->async_work))
592 afs_put_call(call);
593 }
David Howells08e0e7c2007-04-26 15:55:03 -0700594}
595
596/*
David Howells341f7412017-01-05 10:38:36 +0000597 * Delete an asynchronous call. The work item carries a ref to the call struct
598 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700599 */
David Howellsd0016482016-08-30 20:42:14 +0100600static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700601{
David Howellsd0016482016-08-30 20:42:14 +0100602 struct afs_call *call = container_of(work, struct afs_call, async_work);
603
David Howells08e0e7c2007-04-26 15:55:03 -0700604 _enter("");
605
David Howells341f7412017-01-05 10:38:36 +0000606 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700607
608 _leave("");
609}
610
611/*
David Howells341f7412017-01-05 10:38:36 +0000612 * Perform I/O processing on an asynchronous call. The work item carries a ref
613 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700614 */
David Howellsd0016482016-08-30 20:42:14 +0100615static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700616{
David Howellsd0016482016-08-30 20:42:14 +0100617 struct afs_call *call = container_of(work, struct afs_call, async_work);
618
David Howells08e0e7c2007-04-26 15:55:03 -0700619 _enter("");
620
David Howellsd0016482016-08-30 20:42:14 +0100621 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
622 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700623 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100624 }
David Howells08e0e7c2007-04-26 15:55:03 -0700625
David Howells56ff9c82017-01-05 10:38:36 +0000626 if (call->state == AFS_CALL_COMPLETE) {
David Howells08e0e7c2007-04-26 15:55:03 -0700627 call->reply = NULL;
628
David Howells341f7412017-01-05 10:38:36 +0000629 /* We have two refs to release - one from the alloc and one
630 * queued with the work item - and we can't just deallocate the
631 * call because the work item may be queued again.
632 */
David Howellsd0016482016-08-30 20:42:14 +0100633 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000634 if (!queue_work(afs_async_calls, &call->async_work))
635 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700636 }
637
David Howells341f7412017-01-05 10:38:36 +0000638 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700639 _leave("");
640}
641
David Howells00e90712016-09-08 11:10:12 +0100642static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
643{
644 struct afs_call *call = (struct afs_call *)user_call_ID;
645
646 call->rxcall = rxcall;
647}
648
649/*
650 * Charge the incoming call preallocation.
651 */
652static void afs_charge_preallocation(struct work_struct *work)
653{
654 struct afs_call *call = afs_spare_incoming_call;
655
656 for (;;) {
657 if (!call) {
David Howells341f7412017-01-05 10:38:36 +0000658 call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100659 if (!call)
660 break;
661
David Howells56ff9c82017-01-05 10:38:36 +0000662 call->async = true;
David Howells00e90712016-09-08 11:10:12 +0100663 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000664 init_waitqueue_head(&call->waitq);
David Howells00e90712016-09-08 11:10:12 +0100665 }
666
667 if (rxrpc_kernel_charge_accept(afs_socket,
668 afs_wake_up_async_call,
669 afs_rx_attach,
670 (unsigned long)call,
671 GFP_KERNEL) < 0)
672 break;
673 call = NULL;
674 }
675 afs_spare_incoming_call = call;
676}
677
678/*
679 * Discard a preallocated call when a socket is shut down.
680 */
681static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
682 unsigned long user_call_ID)
683{
684 struct afs_call *call = (struct afs_call *)user_call_ID;
685
David Howells00e90712016-09-08 11:10:12 +0100686 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000687 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100688}
689
David Howells08e0e7c2007-04-26 15:55:03 -0700690/*
David Howellsd0016482016-08-30 20:42:14 +0100691 * Notification of an incoming call.
692 */
David Howells00e90712016-09-08 11:10:12 +0100693static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
694 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100695{
David Howells00e90712016-09-08 11:10:12 +0100696 queue_work(afs_wq, &afs_charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100697}
698
699/*
David Howells372ee162016-08-03 14:11:40 +0100700 * Grab the operation ID from an incoming cache manager call. The socket
701 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700702 */
David Howellsd0016482016-08-30 20:42:14 +0100703static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700704{
David Howellsd0016482016-08-30 20:42:14 +0100705 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700706
David Howellsd0016482016-08-30 20:42:14 +0100707 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700708
709 ASSERTCMP(call->offset, <, 4);
710
711 /* the operation ID forms the first four bytes of the request data */
David Howells50a2c952016-10-13 08:27:10 +0100712 ret = afs_extract_data(call, &call->tmp, 4, true);
David Howellsd0016482016-08-30 20:42:14 +0100713 if (ret < 0)
714 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700715
David Howells50a2c952016-10-13 08:27:10 +0100716 call->operation_ID = ntohl(call->tmp);
David Howells08e0e7c2007-04-26 15:55:03 -0700717 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100718 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700719
720 /* ask the cache manager to route the call (it'll change the call type
721 * if successful) */
722 if (!afs_cm_incoming_call(call))
723 return -ENOTSUPP;
724
David Howells8e8d7f12017-01-05 10:38:34 +0000725 trace_afs_cb_call(call);
726
David Howells08e0e7c2007-04-26 15:55:03 -0700727 /* pass responsibility for the remainer of this message off to the
728 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100729 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700730}
731
732/*
733 * send an empty reply
734 */
735void afs_send_empty_reply(struct afs_call *call)
736{
737 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700738
739 _enter("");
740
David Howells08e0e7c2007-04-26 15:55:03 -0700741 msg.msg_name = NULL;
742 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100743 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700744 msg.msg_control = NULL;
745 msg.msg_controllen = 0;
746 msg.msg_flags = 0;
747
748 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100749 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700750 case 0:
751 _leave(" [replied]");
752 return;
753
754 case -ENOMEM:
755 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100756 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100757 RX_USER_ABORT, ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700758 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700759 _leave(" [error]");
760 return;
761 }
762}
763
764/*
David Howellsb908fe62007-04-26 15:58:17 -0700765 * send a simple reply
766 */
767void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
768{
769 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500770 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100771 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700772
773 _enter("");
774
775 iov[0].iov_base = (void *) buf;
776 iov[0].iov_len = len;
777 msg.msg_name = NULL;
778 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500779 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700780 msg.msg_control = NULL;
781 msg.msg_controllen = 0;
782 msg.msg_flags = 0;
783
784 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100785 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
David Howellsbd6dc742007-07-20 10:59:41 +0100786 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100787 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700788 _leave(" [replied]");
789 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100790 }
David Howells6c67c7c2014-05-21 14:48:05 +0100791
David Howellsbd6dc742007-07-20 10:59:41 +0100792 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700793 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100794 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100795 RX_USER_ABORT, ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700796 }
David Howellsbd6dc742007-07-20 10:59:41 +0100797 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700798}
799
800/*
David Howells372ee162016-08-03 14:11:40 +0100801 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700802 */
David Howellsd0016482016-08-30 20:42:14 +0100803int afs_extract_data(struct afs_call *call, void *buf, size_t count,
804 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700805{
David Howellsd0016482016-08-30 20:42:14 +0100806 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700807
David Howellsd0016482016-08-30 20:42:14 +0100808 _enter("{%s,%zu},,%zu,%d",
809 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700810
David Howellsd0016482016-08-30 20:42:14 +0100811 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700812
David Howellsd0016482016-08-30 20:42:14 +0100813 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
814 buf, count, &call->offset,
815 want_more, &call->abort_code);
David Howells8e8d7f12017-01-05 10:38:34 +0000816 trace_afs_recv_data(call, count, call->offset, want_more, ret);
David Howellsd0016482016-08-30 20:42:14 +0100817 if (ret == 0 || ret == -EAGAIN)
818 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700819
David Howellsd0016482016-08-30 20:42:14 +0100820 if (ret == 1) {
821 switch (call->state) {
822 case AFS_CALL_AWAIT_REPLY:
823 call->state = AFS_CALL_COMPLETE;
824 break;
825 case AFS_CALL_AWAIT_REQUEST:
826 call->state = AFS_CALL_REPLYING;
827 break;
828 default:
829 break;
830 }
831 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700832 }
David Howellsd0016482016-08-30 20:42:14 +0100833
834 if (ret == -ECONNABORTED)
835 call->error = call->type->abort_to_error(call->abort_code);
836 else
837 call->error = ret;
838 call->state = AFS_CALL_COMPLETE;
839 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700840}