blob: 720ef05a24fe370205d10cd37ba768b3cc01e6d4 [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>
David Howells08e0e7c2007-04-26 15:55:03 -070013#include <net/sock.h>
14#include <net/af_rxrpc.h>
15#include <rxrpc/packet.h>
16#include "internal.h"
17#include "afs_cm.h"
18
David Howells8324f0b2016-08-30 09:49:29 +010019struct socket *afs_socket; /* my RxRPC socket */
David Howells08e0e7c2007-04-26 15:55:03 -070020static struct workqueue_struct *afs_async_calls;
David Howells00e90712016-09-08 11:10:12 +010021static struct afs_call *afs_spare_incoming_call;
David Howells00d3b7a2007-04-26 15:57:07 -070022static atomic_t afs_outstanding_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070023
David Howellsd0016482016-08-30 20:42:14 +010024static void afs_free_call(struct afs_call *);
25static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howells08e0e7c2007-04-26 15:55:03 -070026static int afs_wait_for_call_to_complete(struct afs_call *);
David Howellsd0016482016-08-30 20:42:14 +010027static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howells08e0e7c2007-04-26 15:55:03 -070028static int afs_dont_wait_for_call_to_complete(struct afs_call *);
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
34/* synchronous call management */
35const struct afs_wait_mode afs_sync_call = {
David Howellsd0016482016-08-30 20:42:14 +010036 .notify_rx = afs_wake_up_call_waiter,
David Howells08e0e7c2007-04-26 15:55:03 -070037 .wait = afs_wait_for_call_to_complete,
38};
39
40/* asynchronous call management */
41const struct afs_wait_mode afs_async_call = {
David Howellsd0016482016-08-30 20:42:14 +010042 .notify_rx = afs_wake_up_async_call,
David Howells08e0e7c2007-04-26 15:55:03 -070043 .wait = afs_dont_wait_for_call_to_complete,
44};
45
46/* asynchronous incoming call management */
47static const struct afs_wait_mode afs_async_incoming_call = {
David Howellsd0016482016-08-30 20:42:14 +010048 .notify_rx = afs_wake_up_async_call,
David Howells08e0e7c2007-04-26 15:55:03 -070049};
50
51/* asynchronous incoming call initial processing */
52static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070053 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070054 .deliver = afs_deliver_cm_op_id,
55 .abort_to_error = afs_abort_to_error,
56};
57
58static void afs_collect_incoming_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010059static void afs_charge_preallocation(struct work_struct *);
David Howells08e0e7c2007-04-26 15:55:03 -070060
David Howells08e0e7c2007-04-26 15:55:03 -070061static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
David Howells00e90712016-09-08 11:10:12 +010062static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
David Howells08e0e7c2007-04-26 15:55:03 -070063
David Howells2f02f7a2016-04-07 17:23:03 +010064static int afs_wait_atomic_t(atomic_t *p)
65{
66 schedule();
67 return 0;
68}
69
David Howells08e0e7c2007-04-26 15:55:03 -070070/*
71 * open an RxRPC socket and bind it to be a server for callback notifications
72 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
73 */
74int afs_open_socket(void)
75{
76 struct sockaddr_rxrpc srx;
77 struct socket *socket;
78 int ret;
79
80 _enter("");
81
David Howells0e119b42016-06-10 22:30:37 +010082 ret = -ENOMEM;
Bhaktipriya Shridhar69ad0522016-09-04 20:53:42 +053083 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
David Howells0e119b42016-06-10 22:30:37 +010084 if (!afs_async_calls)
85 goto error_0;
David Howells08e0e7c2007-04-26 15:55:03 -070086
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -050087 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
David Howells0e119b42016-06-10 22:30:37 +010088 if (ret < 0)
89 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070090
91 socket->sk->sk_allocation = GFP_NOFS;
92
93 /* bind the callback manager's address to make this a server socket */
94 srx.srx_family = AF_RXRPC;
95 srx.srx_service = CM_SERVICE;
96 srx.transport_type = SOCK_DGRAM;
97 srx.transport_len = sizeof(srx.transport.sin);
98 srx.transport.sin.sin_family = AF_INET;
99 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
100 memset(&srx.transport.sin.sin_addr, 0,
101 sizeof(srx.transport.sin.sin_addr));
102
103 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
David Howells0e119b42016-06-10 22:30:37 +0100104 if (ret < 0)
105 goto error_2;
106
David Howells00e90712016-09-08 11:10:12 +0100107 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
108 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +0100109
David Howells0e119b42016-06-10 22:30:37 +0100110 ret = kernel_listen(socket, INT_MAX);
111 if (ret < 0)
112 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -0700113
David Howells08e0e7c2007-04-26 15:55:03 -0700114 afs_socket = socket;
David Howells00e90712016-09-08 11:10:12 +0100115 afs_charge_preallocation(NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700116 _leave(" = 0");
117 return 0;
David Howells0e119b42016-06-10 22:30:37 +0100118
119error_2:
120 sock_release(socket);
121error_1:
122 destroy_workqueue(afs_async_calls);
123error_0:
124 _leave(" = %d", ret);
125 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700126}
127
128/*
129 * close the RxRPC socket AFS was using
130 */
131void afs_close_socket(void)
132{
133 _enter("");
134
David Howells00e90712016-09-08 11:10:12 +0100135 if (afs_spare_incoming_call) {
136 atomic_inc(&afs_outstanding_calls);
137 afs_free_call(afs_spare_incoming_call);
138 afs_spare_incoming_call = NULL;
139 }
140
David Howellsd0016482016-08-30 20:42:14 +0100141 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100142 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
143 TASK_UNINTERRUPTIBLE);
144 _debug("no outstanding calls");
145
David Howellsd0016482016-08-30 20:42:14 +0100146 flush_workqueue(afs_async_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700147 sock_release(afs_socket);
148
149 _debug("dework");
150 destroy_workqueue(afs_async_calls);
151 _leave("");
152}
153
154/*
David Howells00d3b7a2007-04-26 15:57:07 -0700155 * free a call
156 */
157static void afs_free_call(struct afs_call *call)
158{
159 _debug("DONE %p{%s} [%d]",
160 call, call->type->name, atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700161
162 ASSERTCMP(call->rxcall, ==, NULL);
163 ASSERT(!work_pending(&call->async_work));
David Howells00d3b7a2007-04-26 15:57:07 -0700164 ASSERT(call->type->name != NULL);
165
166 kfree(call->request);
167 kfree(call);
David Howells2f02f7a2016-04-07 17:23:03 +0100168
169 if (atomic_dec_and_test(&afs_outstanding_calls))
170 wake_up_atomic_t(&afs_outstanding_calls);
David Howells00d3b7a2007-04-26 15:57:07 -0700171}
172
173/*
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100174 * End a call but do not free it
David Howells6c67c7c2014-05-21 14:48:05 +0100175 */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100176static void afs_end_call_nofree(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100177{
178 if (call->rxcall) {
David Howells4de48af2016-08-30 12:00:48 +0100179 rxrpc_kernel_end_call(afs_socket, call->rxcall);
David Howells6c67c7c2014-05-21 14:48:05 +0100180 call->rxcall = NULL;
181 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100182 if (call->type->destructor)
183 call->type->destructor(call);
184}
185
186/*
187 * End a call and free it
188 */
189static void afs_end_call(struct afs_call *call)
190{
191 afs_end_call_nofree(call);
David Howells6c67c7c2014-05-21 14:48:05 +0100192 afs_free_call(call);
193}
194
195/*
David Howells08e0e7c2007-04-26 15:55:03 -0700196 * allocate a call with flat request and reply buffers
197 */
198struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100199 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700200{
201 struct afs_call *call;
202
203 call = kzalloc(sizeof(*call), GFP_NOFS);
204 if (!call)
205 goto nomem_call;
206
David Howells00d3b7a2007-04-26 15:57:07 -0700207 _debug("CALL %p{%s} [%d]",
208 call, type->name, atomic_read(&afs_outstanding_calls));
209 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700210
211 call->type = type;
212 call->request_size = request_size;
David Howellsd0016482016-08-30 20:42:14 +0100213 call->reply_max = reply_max;
David Howells08e0e7c2007-04-26 15:55:03 -0700214
David Howells00d3b7a2007-04-26 15:57:07 -0700215 if (request_size) {
216 call->request = kmalloc(request_size, GFP_NOFS);
217 if (!call->request)
218 goto nomem_free;
219 }
220
David Howellsd0016482016-08-30 20:42:14 +0100221 if (reply_max) {
222 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 Howells08e0e7c2007-04-26 15:55:03 -0700227 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700228 return call;
229
David Howells00d3b7a2007-04-26 15:57:07 -0700230nomem_free:
231 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700232nomem_call:
233 return NULL;
234}
235
236/*
237 * clean up a call with flat buffer
238 */
239void afs_flat_call_destructor(struct afs_call *call)
240{
241 _enter("");
242
243 kfree(call->request);
244 call->request = NULL;
245 kfree(call->buffer);
246 call->buffer = NULL;
247}
248
249/*
David Howells31143d52007-05-09 02:33:46 -0700250 * attach the data from a bunch of pages on an inode to a call
251 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700252static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
253 struct kvec *iov)
David Howells31143d52007-05-09 02:33:46 -0700254{
255 struct page *pages[8];
256 unsigned count, n, loop, offset, to;
257 pgoff_t first = call->first, last = call->last;
258 int ret;
259
260 _enter("");
261
262 offset = call->first_offset;
263 call->first_offset = 0;
264
265 do {
266 _debug("attach %lx-%lx", first, last);
267
268 count = last - first + 1;
269 if (count > ARRAY_SIZE(pages))
270 count = ARRAY_SIZE(pages);
271 n = find_get_pages_contig(call->mapping, first, count, pages);
272 ASSERTCMP(n, ==, count);
273
274 loop = 0;
275 do {
276 msg->msg_flags = 0;
277 to = PAGE_SIZE;
278 if (first + loop >= last)
279 to = call->last_to;
280 else
281 msg->msg_flags = MSG_MORE;
282 iov->iov_base = kmap(pages[loop]) + offset;
283 iov->iov_len = to - offset;
284 offset = 0;
285
286 _debug("- range %u-%u%s",
287 offset, to, msg->msg_flags ? " [more]" : "");
Al Viro2e90b1c2014-11-27 21:50:31 -0500288 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
289 iov, 1, to - offset);
David Howells31143d52007-05-09 02:33:46 -0700290
291 /* have to change the state *before* sending the last
292 * packet as RxRPC might give us the reply before it
293 * returns from sending the request */
294 if (first + loop >= last)
295 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100296 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
297 msg, to - offset);
David Howells31143d52007-05-09 02:33:46 -0700298 kunmap(pages[loop]);
299 if (ret < 0)
300 break;
301 } while (++loop < count);
302 first += count;
303
304 for (loop = 0; loop < count; loop++)
305 put_page(pages[loop]);
306 if (ret < 0)
307 break;
David Howells5bbf5d32007-05-10 03:15:23 -0700308 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700309
310 _leave(" = %d", ret);
311 return ret;
312}
313
314/*
David Howells08e0e7c2007-04-26 15:55:03 -0700315 * initiate a call
316 */
317int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
318 const struct afs_wait_mode *wait_mode)
319{
320 struct sockaddr_rxrpc srx;
321 struct rxrpc_call *rxcall;
322 struct msghdr msg;
323 struct kvec iov[1];
324 int ret;
325
326 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
327
David Howells00d3b7a2007-04-26 15:57:07 -0700328 ASSERT(call->type != NULL);
329 ASSERT(call->type->name != NULL);
330
David Howells31143d52007-05-09 02:33:46 -0700331 _debug("____MAKE %p{%s,%x} [%d]____",
332 call, call->type->name, key_serial(call->key),
333 atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700334
David Howells08e0e7c2007-04-26 15:55:03 -0700335 call->wait_mode = wait_mode;
David Howellsd0016482016-08-30 20:42:14 +0100336 INIT_WORK(&call->async_work, afs_process_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700337
338 memset(&srx, 0, sizeof(srx));
339 srx.srx_family = AF_RXRPC;
340 srx.srx_service = call->service_id;
341 srx.transport_type = SOCK_DGRAM;
342 srx.transport_len = sizeof(srx.transport.sin);
343 srx.transport.sin.sin_family = AF_INET;
344 srx.transport.sin.sin_port = call->port;
345 memcpy(&srx.transport.sin.sin_addr, addr, 4);
346
347 /* create a call */
348 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
David Howellsd0016482016-08-30 20:42:14 +0100349 (unsigned long) call, gfp,
350 wait_mode->notify_rx);
David Howells00d3b7a2007-04-26 15:57:07 -0700351 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700352 if (IS_ERR(rxcall)) {
353 ret = PTR_ERR(rxcall);
354 goto error_kill_call;
355 }
356
357 call->rxcall = rxcall;
358
359 /* send the request */
360 iov[0].iov_base = call->request;
361 iov[0].iov_len = call->request_size;
362
363 msg.msg_name = NULL;
364 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500365 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500366 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700367 msg.msg_control = NULL;
368 msg.msg_controllen = 0;
David Howells31143d52007-05-09 02:33:46 -0700369 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700370
371 /* have to change the state *before* sending the last packet as RxRPC
372 * might give us the reply before it returns from sending the
373 * request */
David Howells31143d52007-05-09 02:33:46 -0700374 if (!call->send_pages)
375 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100376 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
377 &msg, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700378 if (ret < 0)
379 goto error_do_abort;
380
David Howells31143d52007-05-09 02:33:46 -0700381 if (call->send_pages) {
382 ret = afs_send_pages(call, &msg, iov);
383 if (ret < 0)
384 goto error_do_abort;
385 }
386
David Howells08e0e7c2007-04-26 15:55:03 -0700387 /* at this point, an async call may no longer exist as it may have
388 * already completed */
389 return wait_mode->wait(call);
390
391error_do_abort:
David Howells5a429762016-09-06 22:19:51 +0100392 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
David Howells08e0e7c2007-04-26 15:55:03 -0700393error_kill_call:
David Howells6c67c7c2014-05-21 14:48:05 +0100394 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700395 _leave(" = %d", ret);
396 return ret;
397}
398
399/*
David Howells08e0e7c2007-04-26 15:55:03 -0700400 * deliver messages to a call
401 */
402static void afs_deliver_to_call(struct afs_call *call)
403{
David Howells08e0e7c2007-04-26 15:55:03 -0700404 u32 abort_code;
405 int ret;
406
David Howellsd0016482016-08-30 20:42:14 +0100407 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700408
David Howellsd0016482016-08-30 20:42:14 +0100409 while (call->state == AFS_CALL_AWAIT_REPLY ||
410 call->state == AFS_CALL_AWAIT_OP_ID ||
411 call->state == AFS_CALL_AWAIT_REQUEST ||
412 call->state == AFS_CALL_AWAIT_ACK
413 ) {
414 if (call->state == AFS_CALL_AWAIT_ACK) {
415 size_t offset = 0;
416 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
417 NULL, 0, &offset, false,
418 &call->abort_code);
419 if (ret == -EINPROGRESS || ret == -EAGAIN)
420 return;
421 if (ret == 1) {
422 call->state = AFS_CALL_COMPLETE;
423 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700424 }
David Howellsd0016482016-08-30 20:42:14 +0100425 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700426 }
427
David Howellsd0016482016-08-30 20:42:14 +0100428 ret = call->type->deliver(call);
429 switch (ret) {
430 case 0:
431 if (call->state == AFS_CALL_AWAIT_REPLY)
432 call->state = AFS_CALL_COMPLETE;
433 goto done;
434 case -EINPROGRESS:
435 case -EAGAIN:
436 goto out;
437 case -ENOTCONN:
438 abort_code = RX_CALL_DEAD;
439 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100440 abort_code, -ret, "KNC");
David Howellsd0016482016-08-30 20:42:14 +0100441 goto do_abort;
442 case -ENOTSUPP:
443 abort_code = RX_INVALID_OPERATION;
444 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100445 abort_code, -ret, "KIV");
David Howellsd0016482016-08-30 20:42:14 +0100446 goto do_abort;
447 case -ENODATA:
448 case -EBADMSG:
449 case -EMSGSIZE:
450 default:
451 abort_code = RXGEN_CC_UNMARSHAL;
452 if (call->state != AFS_CALL_AWAIT_REPLY)
453 abort_code = RXGEN_SS_UNMARSHAL;
454 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100455 abort_code, EBADMSG, "KUM");
David Howellsd0016482016-08-30 20:42:14 +0100456 goto do_abort;
457 }
David Howells08e0e7c2007-04-26 15:55:03 -0700458 }
459
David Howellsd0016482016-08-30 20:42:14 +0100460done:
461 if (call->state == AFS_CALL_COMPLETE && call->incoming)
462 afs_end_call(call);
463out:
David Howells08e0e7c2007-04-26 15:55:03 -0700464 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100465 return;
466
467do_abort:
468 call->error = ret;
469 call->state = AFS_CALL_COMPLETE;
470 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700471}
472
473/*
474 * wait synchronously for a call to complete
475 */
476static int afs_wait_for_call_to_complete(struct afs_call *call)
477{
David Howells5a429762016-09-06 22:19:51 +0100478 const char *abort_why;
David Howells08e0e7c2007-04-26 15:55:03 -0700479 int ret;
480
481 DECLARE_WAITQUEUE(myself, current);
482
483 _enter("");
484
485 add_wait_queue(&call->waitq, &myself);
486 for (;;) {
487 set_current_state(TASK_INTERRUPTIBLE);
488
489 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100490 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
491 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700492 __set_current_state(TASK_RUNNING);
493 afs_deliver_to_call(call);
494 continue;
495 }
496
David Howells5a429762016-09-06 22:19:51 +0100497 abort_why = "KWC";
David Howells08e0e7c2007-04-26 15:55:03 -0700498 ret = call->error;
David Howellsd0016482016-08-30 20:42:14 +0100499 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700500 break;
David Howells5a429762016-09-06 22:19:51 +0100501 abort_why = "KWI";
David Howells08e0e7c2007-04-26 15:55:03 -0700502 ret = -EINTR;
503 if (signal_pending(current))
504 break;
505 schedule();
506 }
507
508 remove_wait_queue(&call->waitq, &myself);
509 __set_current_state(TASK_RUNNING);
510
511 /* kill the call */
512 if (call->state < AFS_CALL_COMPLETE) {
513 _debug("call incomplete");
David Howellsd0016482016-08-30 20:42:14 +0100514 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100515 RX_CALL_DEAD, -ret, abort_why);
David Howells08e0e7c2007-04-26 15:55:03 -0700516 }
517
518 _debug("call complete");
David Howells6c67c7c2014-05-21 14:48:05 +0100519 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700520 _leave(" = %d", ret);
521 return ret;
522}
523
524/*
525 * wake up a waiting call
526 */
David Howellsd0016482016-08-30 20:42:14 +0100527static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
528 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700529{
David Howellsd0016482016-08-30 20:42:14 +0100530 struct afs_call *call = (struct afs_call *)call_user_ID;
531
532 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700533 wake_up(&call->waitq);
534}
535
536/*
537 * wake up an asynchronous call
538 */
David Howellsd0016482016-08-30 20:42:14 +0100539static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
540 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700541{
David Howellsd0016482016-08-30 20:42:14 +0100542 struct afs_call *call = (struct afs_call *)call_user_ID;
543
544 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700545 queue_work(afs_async_calls, &call->async_work);
546}
547
548/*
549 * put a call into asynchronous mode
550 * - mustn't touch the call descriptor as the call my have completed by the
551 * time we get here
552 */
553static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
554{
555 _enter("");
556 return -EINPROGRESS;
557}
558
559/*
560 * delete an asynchronous call
561 */
David Howellsd0016482016-08-30 20:42:14 +0100562static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700563{
David Howellsd0016482016-08-30 20:42:14 +0100564 struct afs_call *call = container_of(work, struct afs_call, async_work);
565
David Howells08e0e7c2007-04-26 15:55:03 -0700566 _enter("");
567
David Howells00d3b7a2007-04-26 15:57:07 -0700568 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700569
570 _leave("");
571}
572
573/*
574 * perform processing on an asynchronous call
David Howells08e0e7c2007-04-26 15:55:03 -0700575 */
David Howellsd0016482016-08-30 20:42:14 +0100576static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700577{
David Howellsd0016482016-08-30 20:42:14 +0100578 struct afs_call *call = container_of(work, struct afs_call, async_work);
579
David Howells08e0e7c2007-04-26 15:55:03 -0700580 _enter("");
581
David Howellsd0016482016-08-30 20:42:14 +0100582 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
583 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700584 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100585 }
David Howells08e0e7c2007-04-26 15:55:03 -0700586
David Howellsd0016482016-08-30 20:42:14 +0100587 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
David Howells08e0e7c2007-04-26 15:55:03 -0700588 if (call->wait_mode->async_complete)
589 call->wait_mode->async_complete(call->reply,
590 call->error);
591 call->reply = NULL;
592
593 /* kill the call */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100594 afs_end_call_nofree(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700595
596 /* we can't just delete the call because the work item may be
597 * queued */
David Howellsd0016482016-08-30 20:42:14 +0100598 call->async_work.func = afs_delete_async_call;
David Howells08e0e7c2007-04-26 15:55:03 -0700599 queue_work(afs_async_calls, &call->async_work);
600 }
601
602 _leave("");
603}
604
605/*
David Howells08e0e7c2007-04-26 15:55:03 -0700606 * accept the backlog of incoming calls
607 */
608static void afs_collect_incoming_call(struct work_struct *work)
609{
610 struct rxrpc_call *rxcall;
611 struct afs_call *call = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700612
David Howellsd0016482016-08-30 20:42:14 +0100613 _enter("");
David Howells08e0e7c2007-04-26 15:55:03 -0700614
David Howellsd0016482016-08-30 20:42:14 +0100615 do {
David Howells08e0e7c2007-04-26 15:55:03 -0700616 if (!call) {
617 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
618 if (!call) {
619 rxrpc_kernel_reject_call(afs_socket);
620 return;
621 }
622
David Howellsd0016482016-08-30 20:42:14 +0100623 INIT_WORK(&call->async_work, afs_process_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700624 call->wait_mode = &afs_async_incoming_call;
625 call->type = &afs_RXCMxxxx;
626 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700627 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700628
629 _debug("CALL %p{%s} [%d]",
630 call, call->type->name,
631 atomic_read(&afs_outstanding_calls));
632 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700633 }
634
635 rxcall = rxrpc_kernel_accept_call(afs_socket,
David Howellsd0016482016-08-30 20:42:14 +0100636 (unsigned long)call,
637 afs_wake_up_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700638 if (!IS_ERR(rxcall)) {
639 call->rxcall = rxcall;
David Howellsd0016482016-08-30 20:42:14 +0100640 call->need_attention = true;
641 queue_work(afs_async_calls, &call->async_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700642 call = NULL;
643 }
David Howellsd0016482016-08-30 20:42:14 +0100644 } while (!call);
David Howells08e0e7c2007-04-26 15:55:03 -0700645
David Howells00d3b7a2007-04-26 15:57:07 -0700646 if (call)
647 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700648}
649
David Howells00e90712016-09-08 11:10:12 +0100650static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
651{
652 struct afs_call *call = (struct afs_call *)user_call_ID;
653
654 call->rxcall = rxcall;
655}
656
657/*
658 * Charge the incoming call preallocation.
659 */
660static void afs_charge_preallocation(struct work_struct *work)
661{
662 struct afs_call *call = afs_spare_incoming_call;
663
664 for (;;) {
665 if (!call) {
666 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
667 if (!call)
668 break;
669
670 INIT_WORK(&call->async_work, afs_process_async_call);
671 call->wait_mode = &afs_async_incoming_call;
672 call->type = &afs_RXCMxxxx;
673 init_waitqueue_head(&call->waitq);
674 call->state = AFS_CALL_AWAIT_OP_ID;
675 }
676
677 if (rxrpc_kernel_charge_accept(afs_socket,
678 afs_wake_up_async_call,
679 afs_rx_attach,
680 (unsigned long)call,
681 GFP_KERNEL) < 0)
682 break;
683 call = NULL;
684 }
685 afs_spare_incoming_call = call;
686}
687
688/*
689 * Discard a preallocated call when a socket is shut down.
690 */
691static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
692 unsigned long user_call_ID)
693{
694 struct afs_call *call = (struct afs_call *)user_call_ID;
695
696 atomic_inc(&afs_outstanding_calls);
697 call->rxcall = NULL;
698 afs_free_call(call);
699}
700
David Howells08e0e7c2007-04-26 15:55:03 -0700701/*
David Howellsd0016482016-08-30 20:42:14 +0100702 * Notification of an incoming call.
703 */
David Howells00e90712016-09-08 11:10:12 +0100704static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
705 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100706{
707 queue_work(afs_wq, &afs_collect_incoming_call_work);
David Howells00e90712016-09-08 11:10:12 +0100708 queue_work(afs_wq, &afs_charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100709}
710
711/*
David Howells372ee162016-08-03 14:11:40 +0100712 * Grab the operation ID from an incoming cache manager call. The socket
713 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700714 */
David Howellsd0016482016-08-30 20:42:14 +0100715static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700716{
David Howellsd0016482016-08-30 20:42:14 +0100717 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700718
David Howellsd0016482016-08-30 20:42:14 +0100719 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700720
721 ASSERTCMP(call->offset, <, 4);
722
723 /* the operation ID forms the first four bytes of the request data */
David Howellsd0016482016-08-30 20:42:14 +0100724 ret = afs_extract_data(call, &call->operation_ID, 4, true);
725 if (ret < 0)
726 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700727
728 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100729 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700730
731 /* ask the cache manager to route the call (it'll change the call type
732 * if successful) */
733 if (!afs_cm_incoming_call(call))
734 return -ENOTSUPP;
735
736 /* pass responsibility for the remainer of this message off to the
737 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100738 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700739}
740
741/*
742 * send an empty reply
743 */
744void afs_send_empty_reply(struct afs_call *call)
745{
746 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700747
748 _enter("");
749
David Howells08e0e7c2007-04-26 15:55:03 -0700750 msg.msg_name = NULL;
751 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100752 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700753 msg.msg_control = NULL;
754 msg.msg_controllen = 0;
755 msg.msg_flags = 0;
756
757 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100758 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700759 case 0:
760 _leave(" [replied]");
761 return;
762
763 case -ENOMEM:
764 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100765 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100766 RX_USER_ABORT, ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700767 default:
David Howells6c67c7c2014-05-21 14:48:05 +0100768 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700769 _leave(" [error]");
770 return;
771 }
772}
773
774/*
David Howellsb908fe62007-04-26 15:58:17 -0700775 * send a simple reply
776 */
777void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
778{
779 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500780 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100781 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700782
783 _enter("");
784
785 iov[0].iov_base = (void *) buf;
786 iov[0].iov_len = len;
787 msg.msg_name = NULL;
788 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500789 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700790 msg.msg_control = NULL;
791 msg.msg_controllen = 0;
792 msg.msg_flags = 0;
793
794 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100795 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
David Howellsbd6dc742007-07-20 10:59:41 +0100796 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100797 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700798 _leave(" [replied]");
799 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100800 }
David Howells6c67c7c2014-05-21 14:48:05 +0100801
David Howellsbd6dc742007-07-20 10:59:41 +0100802 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700803 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100804 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100805 RX_USER_ABORT, ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700806 }
David Howells6c67c7c2014-05-21 14:48:05 +0100807 afs_end_call(call);
David Howellsbd6dc742007-07-20 10:59:41 +0100808 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700809}
810
811/*
David Howells372ee162016-08-03 14:11:40 +0100812 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700813 */
David Howellsd0016482016-08-30 20:42:14 +0100814int afs_extract_data(struct afs_call *call, void *buf, size_t count,
815 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700816{
David Howellsd0016482016-08-30 20:42:14 +0100817 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700818
David Howellsd0016482016-08-30 20:42:14 +0100819 _enter("{%s,%zu},,%zu,%d",
820 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700821
David Howellsd0016482016-08-30 20:42:14 +0100822 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700823
David Howellsd0016482016-08-30 20:42:14 +0100824 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
825 buf, count, &call->offset,
826 want_more, &call->abort_code);
827 if (ret == 0 || ret == -EAGAIN)
828 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700829
David Howellsd0016482016-08-30 20:42:14 +0100830 if (ret == 1) {
831 switch (call->state) {
832 case AFS_CALL_AWAIT_REPLY:
833 call->state = AFS_CALL_COMPLETE;
834 break;
835 case AFS_CALL_AWAIT_REQUEST:
836 call->state = AFS_CALL_REPLYING;
837 break;
838 default:
839 break;
840 }
841 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700842 }
David Howellsd0016482016-08-30 20:42:14 +0100843
844 if (ret == -ECONNABORTED)
845 call->error = call->type->abort_to_error(call->abort_code);
846 else
847 call->error = ret;
848 call->state = AFS_CALL_COMPLETE;
849 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700850}