blob: 244896baf24102f43d15acf3634c1bec2d97aeb1 [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 Howells00d3b7a2007-04-26 15:57:07 -070021static atomic_t afs_outstanding_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070022
David Howellsd0016482016-08-30 20:42:14 +010023static void afs_free_call(struct afs_call *);
24static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howells08e0e7c2007-04-26 15:55:03 -070025static int afs_wait_for_call_to_complete(struct afs_call *);
David Howellsd0016482016-08-30 20:42:14 +010026static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howells08e0e7c2007-04-26 15:55:03 -070027static int afs_dont_wait_for_call_to_complete(struct afs_call *);
David Howellsd0016482016-08-30 20:42:14 +010028static void afs_process_async_call(struct work_struct *);
29static void afs_rx_new_call(struct sock *);
30static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070031
32/* synchronous call management */
33const struct afs_wait_mode afs_sync_call = {
David Howellsd0016482016-08-30 20:42:14 +010034 .notify_rx = afs_wake_up_call_waiter,
David Howells08e0e7c2007-04-26 15:55:03 -070035 .wait = afs_wait_for_call_to_complete,
36};
37
38/* asynchronous call management */
39const struct afs_wait_mode afs_async_call = {
David Howellsd0016482016-08-30 20:42:14 +010040 .notify_rx = afs_wake_up_async_call,
David Howells08e0e7c2007-04-26 15:55:03 -070041 .wait = afs_dont_wait_for_call_to_complete,
42};
43
44/* asynchronous incoming call management */
45static const struct afs_wait_mode afs_async_incoming_call = {
David Howellsd0016482016-08-30 20:42:14 +010046 .notify_rx = afs_wake_up_async_call,
David Howells08e0e7c2007-04-26 15:55:03 -070047};
48
49/* asynchronous incoming call initial processing */
50static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070051 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070052 .deliver = afs_deliver_cm_op_id,
53 .abort_to_error = afs_abort_to_error,
54};
55
56static void afs_collect_incoming_call(struct work_struct *);
57
David Howells08e0e7c2007-04-26 15:55:03 -070058static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
59
David Howells2f02f7a2016-04-07 17:23:03 +010060static int afs_wait_atomic_t(atomic_t *p)
61{
62 schedule();
63 return 0;
64}
65
David Howells08e0e7c2007-04-26 15:55:03 -070066/*
67 * open an RxRPC socket and bind it to be a server for callback notifications
68 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
69 */
70int afs_open_socket(void)
71{
72 struct sockaddr_rxrpc srx;
73 struct socket *socket;
74 int ret;
75
76 _enter("");
77
David Howells0e119b42016-06-10 22:30:37 +010078 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -070079 afs_async_calls = create_singlethread_workqueue("kafsd");
David Howells0e119b42016-06-10 22:30:37 +010080 if (!afs_async_calls)
81 goto error_0;
David Howells08e0e7c2007-04-26 15:55:03 -070082
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -050083 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
David Howells0e119b42016-06-10 22:30:37 +010084 if (ret < 0)
85 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070086
87 socket->sk->sk_allocation = GFP_NOFS;
88
89 /* bind the callback manager's address to make this a server socket */
90 srx.srx_family = AF_RXRPC;
91 srx.srx_service = CM_SERVICE;
92 srx.transport_type = SOCK_DGRAM;
93 srx.transport_len = sizeof(srx.transport.sin);
94 srx.transport.sin.sin_family = AF_INET;
95 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
96 memset(&srx.transport.sin.sin_addr, 0,
97 sizeof(srx.transport.sin.sin_addr));
98
99 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
David Howells0e119b42016-06-10 22:30:37 +0100100 if (ret < 0)
101 goto error_2;
102
David Howellsd0016482016-08-30 20:42:14 +0100103 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call);
104
David Howells0e119b42016-06-10 22:30:37 +0100105 ret = kernel_listen(socket, INT_MAX);
106 if (ret < 0)
107 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -0700108
David Howells08e0e7c2007-04-26 15:55:03 -0700109 afs_socket = socket;
110 _leave(" = 0");
111 return 0;
David Howells0e119b42016-06-10 22:30:37 +0100112
113error_2:
114 sock_release(socket);
115error_1:
116 destroy_workqueue(afs_async_calls);
117error_0:
118 _leave(" = %d", ret);
119 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700120}
121
122/*
123 * close the RxRPC socket AFS was using
124 */
125void afs_close_socket(void)
126{
127 _enter("");
128
David Howellsd0016482016-08-30 20:42:14 +0100129 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100130 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
131 TASK_UNINTERRUPTIBLE);
132 _debug("no outstanding calls");
133
David Howellsd0016482016-08-30 20:42:14 +0100134 flush_workqueue(afs_async_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700135 sock_release(afs_socket);
136
137 _debug("dework");
138 destroy_workqueue(afs_async_calls);
139 _leave("");
140}
141
142/*
David Howells00d3b7a2007-04-26 15:57:07 -0700143 * free a call
144 */
145static void afs_free_call(struct afs_call *call)
146{
147 _debug("DONE %p{%s} [%d]",
148 call, call->type->name, atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700149
150 ASSERTCMP(call->rxcall, ==, NULL);
151 ASSERT(!work_pending(&call->async_work));
David Howells00d3b7a2007-04-26 15:57:07 -0700152 ASSERT(call->type->name != NULL);
153
154 kfree(call->request);
155 kfree(call);
David Howells2f02f7a2016-04-07 17:23:03 +0100156
157 if (atomic_dec_and_test(&afs_outstanding_calls))
158 wake_up_atomic_t(&afs_outstanding_calls);
David Howells00d3b7a2007-04-26 15:57:07 -0700159}
160
161/*
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100162 * End a call but do not free it
David Howells6c67c7c2014-05-21 14:48:05 +0100163 */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100164static void afs_end_call_nofree(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100165{
166 if (call->rxcall) {
David Howells4de48af2016-08-30 12:00:48 +0100167 rxrpc_kernel_end_call(afs_socket, call->rxcall);
David Howells6c67c7c2014-05-21 14:48:05 +0100168 call->rxcall = NULL;
169 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100170 if (call->type->destructor)
171 call->type->destructor(call);
172}
173
174/*
175 * End a call and free it
176 */
177static void afs_end_call(struct afs_call *call)
178{
179 afs_end_call_nofree(call);
David Howells6c67c7c2014-05-21 14:48:05 +0100180 afs_free_call(call);
181}
182
183/*
David Howells08e0e7c2007-04-26 15:55:03 -0700184 * allocate a call with flat request and reply buffers
185 */
186struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100187 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700188{
189 struct afs_call *call;
190
191 call = kzalloc(sizeof(*call), GFP_NOFS);
192 if (!call)
193 goto nomem_call;
194
David Howells00d3b7a2007-04-26 15:57:07 -0700195 _debug("CALL %p{%s} [%d]",
196 call, type->name, atomic_read(&afs_outstanding_calls));
197 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700198
199 call->type = type;
200 call->request_size = request_size;
David Howellsd0016482016-08-30 20:42:14 +0100201 call->reply_max = reply_max;
David Howells08e0e7c2007-04-26 15:55:03 -0700202
David Howells00d3b7a2007-04-26 15:57:07 -0700203 if (request_size) {
204 call->request = kmalloc(request_size, GFP_NOFS);
205 if (!call->request)
206 goto nomem_free;
207 }
208
David Howellsd0016482016-08-30 20:42:14 +0100209 if (reply_max) {
210 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700211 if (!call->buffer)
212 goto nomem_free;
213 }
214
David Howells08e0e7c2007-04-26 15:55:03 -0700215 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700216 return call;
217
David Howells00d3b7a2007-04-26 15:57:07 -0700218nomem_free:
219 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700220nomem_call:
221 return NULL;
222}
223
224/*
225 * clean up a call with flat buffer
226 */
227void afs_flat_call_destructor(struct afs_call *call)
228{
229 _enter("");
230
231 kfree(call->request);
232 call->request = NULL;
233 kfree(call->buffer);
234 call->buffer = NULL;
235}
236
237/*
David Howells31143d52007-05-09 02:33:46 -0700238 * attach the data from a bunch of pages on an inode to a call
239 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700240static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
241 struct kvec *iov)
David Howells31143d52007-05-09 02:33:46 -0700242{
243 struct page *pages[8];
244 unsigned count, n, loop, offset, to;
245 pgoff_t first = call->first, last = call->last;
246 int ret;
247
248 _enter("");
249
250 offset = call->first_offset;
251 call->first_offset = 0;
252
253 do {
254 _debug("attach %lx-%lx", first, last);
255
256 count = last - first + 1;
257 if (count > ARRAY_SIZE(pages))
258 count = ARRAY_SIZE(pages);
259 n = find_get_pages_contig(call->mapping, first, count, pages);
260 ASSERTCMP(n, ==, count);
261
262 loop = 0;
263 do {
264 msg->msg_flags = 0;
265 to = PAGE_SIZE;
266 if (first + loop >= last)
267 to = call->last_to;
268 else
269 msg->msg_flags = MSG_MORE;
270 iov->iov_base = kmap(pages[loop]) + offset;
271 iov->iov_len = to - offset;
272 offset = 0;
273
274 _debug("- range %u-%u%s",
275 offset, to, msg->msg_flags ? " [more]" : "");
Al Viro2e90b1c2014-11-27 21:50:31 -0500276 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
277 iov, 1, to - offset);
David Howells31143d52007-05-09 02:33:46 -0700278
279 /* have to change the state *before* sending the last
280 * packet as RxRPC might give us the reply before it
281 * returns from sending the request */
282 if (first + loop >= last)
283 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100284 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
285 msg, to - offset);
David Howells31143d52007-05-09 02:33:46 -0700286 kunmap(pages[loop]);
287 if (ret < 0)
288 break;
289 } while (++loop < count);
290 first += count;
291
292 for (loop = 0; loop < count; loop++)
293 put_page(pages[loop]);
294 if (ret < 0)
295 break;
David Howells5bbf5d32007-05-10 03:15:23 -0700296 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700297
298 _leave(" = %d", ret);
299 return ret;
300}
301
302/*
David Howells08e0e7c2007-04-26 15:55:03 -0700303 * initiate a call
304 */
305int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
306 const struct afs_wait_mode *wait_mode)
307{
308 struct sockaddr_rxrpc srx;
309 struct rxrpc_call *rxcall;
310 struct msghdr msg;
311 struct kvec iov[1];
312 int ret;
313
314 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
315
David Howells00d3b7a2007-04-26 15:57:07 -0700316 ASSERT(call->type != NULL);
317 ASSERT(call->type->name != NULL);
318
David Howells31143d52007-05-09 02:33:46 -0700319 _debug("____MAKE %p{%s,%x} [%d]____",
320 call, call->type->name, key_serial(call->key),
321 atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700322
David Howells08e0e7c2007-04-26 15:55:03 -0700323 call->wait_mode = wait_mode;
David Howellsd0016482016-08-30 20:42:14 +0100324 INIT_WORK(&call->async_work, afs_process_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700325
326 memset(&srx, 0, sizeof(srx));
327 srx.srx_family = AF_RXRPC;
328 srx.srx_service = call->service_id;
329 srx.transport_type = SOCK_DGRAM;
330 srx.transport_len = sizeof(srx.transport.sin);
331 srx.transport.sin.sin_family = AF_INET;
332 srx.transport.sin.sin_port = call->port;
333 memcpy(&srx.transport.sin.sin_addr, addr, 4);
334
335 /* create a call */
336 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
David Howellsd0016482016-08-30 20:42:14 +0100337 (unsigned long) call, gfp,
338 wait_mode->notify_rx);
David Howells00d3b7a2007-04-26 15:57:07 -0700339 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700340 if (IS_ERR(rxcall)) {
341 ret = PTR_ERR(rxcall);
342 goto error_kill_call;
343 }
344
345 call->rxcall = rxcall;
346
347 /* send the request */
348 iov[0].iov_base = call->request;
349 iov[0].iov_len = call->request_size;
350
351 msg.msg_name = NULL;
352 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500353 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500354 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700355 msg.msg_control = NULL;
356 msg.msg_controllen = 0;
David Howells31143d52007-05-09 02:33:46 -0700357 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700358
359 /* have to change the state *before* sending the last packet as RxRPC
360 * might give us the reply before it returns from sending the
361 * request */
David Howells31143d52007-05-09 02:33:46 -0700362 if (!call->send_pages)
363 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100364 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
365 &msg, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700366 if (ret < 0)
367 goto error_do_abort;
368
David Howells31143d52007-05-09 02:33:46 -0700369 if (call->send_pages) {
370 ret = afs_send_pages(call, &msg, iov);
371 if (ret < 0)
372 goto error_do_abort;
373 }
374
David Howells08e0e7c2007-04-26 15:55:03 -0700375 /* at this point, an async call may no longer exist as it may have
376 * already completed */
377 return wait_mode->wait(call);
378
379error_do_abort:
David Howells4de48af2016-08-30 12:00:48 +0100380 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT);
David Howells08e0e7c2007-04-26 15:55:03 -0700381error_kill_call:
David Howells6c67c7c2014-05-21 14:48:05 +0100382 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700383 _leave(" = %d", ret);
384 return ret;
385}
386
387/*
David Howells08e0e7c2007-04-26 15:55:03 -0700388 * deliver messages to a call
389 */
390static void afs_deliver_to_call(struct afs_call *call)
391{
David Howells08e0e7c2007-04-26 15:55:03 -0700392 u32 abort_code;
393 int ret;
394
David Howellsd0016482016-08-30 20:42:14 +0100395 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700396
David Howellsd0016482016-08-30 20:42:14 +0100397 while (call->state == AFS_CALL_AWAIT_REPLY ||
398 call->state == AFS_CALL_AWAIT_OP_ID ||
399 call->state == AFS_CALL_AWAIT_REQUEST ||
400 call->state == AFS_CALL_AWAIT_ACK
401 ) {
402 if (call->state == AFS_CALL_AWAIT_ACK) {
403 size_t offset = 0;
404 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
405 NULL, 0, &offset, false,
406 &call->abort_code);
407 if (ret == -EINPROGRESS || ret == -EAGAIN)
408 return;
409 if (ret == 1) {
410 call->state = AFS_CALL_COMPLETE;
411 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700412 }
David Howellsd0016482016-08-30 20:42:14 +0100413 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700414 }
415
David Howellsd0016482016-08-30 20:42:14 +0100416 ret = call->type->deliver(call);
417 switch (ret) {
418 case 0:
419 if (call->state == AFS_CALL_AWAIT_REPLY)
420 call->state = AFS_CALL_COMPLETE;
421 goto done;
422 case -EINPROGRESS:
423 case -EAGAIN:
424 goto out;
425 case -ENOTCONN:
426 abort_code = RX_CALL_DEAD;
427 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
428 abort_code);
429 goto do_abort;
430 case -ENOTSUPP:
431 abort_code = RX_INVALID_OPERATION;
432 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
433 abort_code);
434 goto do_abort;
435 case -ENODATA:
436 case -EBADMSG:
437 case -EMSGSIZE:
438 default:
439 abort_code = RXGEN_CC_UNMARSHAL;
440 if (call->state != AFS_CALL_AWAIT_REPLY)
441 abort_code = RXGEN_SS_UNMARSHAL;
442 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
443 abort_code);
444 goto do_abort;
445 }
David Howells08e0e7c2007-04-26 15:55:03 -0700446 }
447
David Howellsd0016482016-08-30 20:42:14 +0100448done:
449 if (call->state == AFS_CALL_COMPLETE && call->incoming)
450 afs_end_call(call);
451out:
David Howells08e0e7c2007-04-26 15:55:03 -0700452 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100453 return;
454
455do_abort:
456 call->error = ret;
457 call->state = AFS_CALL_COMPLETE;
458 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700459}
460
461/*
462 * wait synchronously for a call to complete
463 */
464static int afs_wait_for_call_to_complete(struct afs_call *call)
465{
David Howells08e0e7c2007-04-26 15:55:03 -0700466 int ret;
467
468 DECLARE_WAITQUEUE(myself, current);
469
470 _enter("");
471
472 add_wait_queue(&call->waitq, &myself);
473 for (;;) {
474 set_current_state(TASK_INTERRUPTIBLE);
475
476 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100477 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
478 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700479 __set_current_state(TASK_RUNNING);
480 afs_deliver_to_call(call);
481 continue;
482 }
483
484 ret = call->error;
David Howellsd0016482016-08-30 20:42:14 +0100485 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700486 break;
487 ret = -EINTR;
488 if (signal_pending(current))
489 break;
490 schedule();
491 }
492
493 remove_wait_queue(&call->waitq, &myself);
494 __set_current_state(TASK_RUNNING);
495
496 /* kill the call */
497 if (call->state < AFS_CALL_COMPLETE) {
498 _debug("call incomplete");
David Howellsd0016482016-08-30 20:42:14 +0100499 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
500 RX_CALL_DEAD);
David Howells08e0e7c2007-04-26 15:55:03 -0700501 }
502
503 _debug("call complete");
David Howells6c67c7c2014-05-21 14:48:05 +0100504 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700505 _leave(" = %d", ret);
506 return ret;
507}
508
509/*
510 * wake up a waiting call
511 */
David Howellsd0016482016-08-30 20:42:14 +0100512static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
513 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700514{
David Howellsd0016482016-08-30 20:42:14 +0100515 struct afs_call *call = (struct afs_call *)call_user_ID;
516
517 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700518 wake_up(&call->waitq);
519}
520
521/*
522 * wake up an asynchronous call
523 */
David Howellsd0016482016-08-30 20:42:14 +0100524static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
525 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700526{
David Howellsd0016482016-08-30 20:42:14 +0100527 struct afs_call *call = (struct afs_call *)call_user_ID;
528
529 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700530 queue_work(afs_async_calls, &call->async_work);
531}
532
533/*
534 * put a call into asynchronous mode
535 * - mustn't touch the call descriptor as the call my have completed by the
536 * time we get here
537 */
538static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
539{
540 _enter("");
541 return -EINPROGRESS;
542}
543
544/*
545 * delete an asynchronous call
546 */
David Howellsd0016482016-08-30 20:42:14 +0100547static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700548{
David Howellsd0016482016-08-30 20:42:14 +0100549 struct afs_call *call = container_of(work, struct afs_call, async_work);
550
David Howells08e0e7c2007-04-26 15:55:03 -0700551 _enter("");
552
David Howells00d3b7a2007-04-26 15:57:07 -0700553 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700554
555 _leave("");
556}
557
558/*
559 * perform processing on an asynchronous call
David Howells08e0e7c2007-04-26 15:55:03 -0700560 */
David Howellsd0016482016-08-30 20:42:14 +0100561static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700562{
David Howellsd0016482016-08-30 20:42:14 +0100563 struct afs_call *call = container_of(work, struct afs_call, async_work);
564
David Howells08e0e7c2007-04-26 15:55:03 -0700565 _enter("");
566
David Howellsd0016482016-08-30 20:42:14 +0100567 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
568 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700569 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100570 }
David Howells08e0e7c2007-04-26 15:55:03 -0700571
David Howellsd0016482016-08-30 20:42:14 +0100572 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
David Howells08e0e7c2007-04-26 15:55:03 -0700573 if (call->wait_mode->async_complete)
574 call->wait_mode->async_complete(call->reply,
575 call->error);
576 call->reply = NULL;
577
578 /* kill the call */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100579 afs_end_call_nofree(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700580
581 /* we can't just delete the call because the work item may be
582 * queued */
David Howellsd0016482016-08-30 20:42:14 +0100583 call->async_work.func = afs_delete_async_call;
David Howells08e0e7c2007-04-26 15:55:03 -0700584 queue_work(afs_async_calls, &call->async_work);
585 }
586
587 _leave("");
588}
589
590/*
David Howells08e0e7c2007-04-26 15:55:03 -0700591 * accept the backlog of incoming calls
592 */
593static void afs_collect_incoming_call(struct work_struct *work)
594{
595 struct rxrpc_call *rxcall;
596 struct afs_call *call = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700597
David Howellsd0016482016-08-30 20:42:14 +0100598 _enter("");
David Howells08e0e7c2007-04-26 15:55:03 -0700599
David Howellsd0016482016-08-30 20:42:14 +0100600 do {
David Howells08e0e7c2007-04-26 15:55:03 -0700601 if (!call) {
602 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
603 if (!call) {
604 rxrpc_kernel_reject_call(afs_socket);
605 return;
606 }
607
David Howellsd0016482016-08-30 20:42:14 +0100608 INIT_WORK(&call->async_work, afs_process_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700609 call->wait_mode = &afs_async_incoming_call;
610 call->type = &afs_RXCMxxxx;
611 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700612 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700613
614 _debug("CALL %p{%s} [%d]",
615 call, call->type->name,
616 atomic_read(&afs_outstanding_calls));
617 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700618 }
619
620 rxcall = rxrpc_kernel_accept_call(afs_socket,
David Howellsd0016482016-08-30 20:42:14 +0100621 (unsigned long)call,
622 afs_wake_up_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700623 if (!IS_ERR(rxcall)) {
624 call->rxcall = rxcall;
David Howellsd0016482016-08-30 20:42:14 +0100625 call->need_attention = true;
626 queue_work(afs_async_calls, &call->async_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700627 call = NULL;
628 }
David Howellsd0016482016-08-30 20:42:14 +0100629 } while (!call);
David Howells08e0e7c2007-04-26 15:55:03 -0700630
David Howells00d3b7a2007-04-26 15:57:07 -0700631 if (call)
632 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700633}
634
635/*
David Howellsd0016482016-08-30 20:42:14 +0100636 * Notification of an incoming call.
637 */
638static void afs_rx_new_call(struct sock *sk)
639{
640 queue_work(afs_wq, &afs_collect_incoming_call_work);
641}
642
643/*
David Howells372ee162016-08-03 14:11:40 +0100644 * Grab the operation ID from an incoming cache manager call. The socket
645 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700646 */
David Howellsd0016482016-08-30 20:42:14 +0100647static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700648{
David Howellsd0016482016-08-30 20:42:14 +0100649 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700650
David Howellsd0016482016-08-30 20:42:14 +0100651 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700652
653 ASSERTCMP(call->offset, <, 4);
654
655 /* the operation ID forms the first four bytes of the request data */
David Howellsd0016482016-08-30 20:42:14 +0100656 ret = afs_extract_data(call, &call->operation_ID, 4, true);
657 if (ret < 0)
658 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700659
660 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100661 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700662
663 /* ask the cache manager to route the call (it'll change the call type
664 * if successful) */
665 if (!afs_cm_incoming_call(call))
666 return -ENOTSUPP;
667
668 /* pass responsibility for the remainer of this message off to the
669 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100670 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700671}
672
673/*
674 * send an empty reply
675 */
676void afs_send_empty_reply(struct afs_call *call)
677{
678 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700679
680 _enter("");
681
David Howells08e0e7c2007-04-26 15:55:03 -0700682 msg.msg_name = NULL;
683 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100684 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700685 msg.msg_control = NULL;
686 msg.msg_controllen = 0;
687 msg.msg_flags = 0;
688
689 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100690 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700691 case 0:
692 _leave(" [replied]");
693 return;
694
695 case -ENOMEM:
696 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100697 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
698 RX_USER_ABORT);
David Howells08e0e7c2007-04-26 15:55:03 -0700699 default:
David Howells6c67c7c2014-05-21 14:48:05 +0100700 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700701 _leave(" [error]");
702 return;
703 }
704}
705
706/*
David Howellsb908fe62007-04-26 15:58:17 -0700707 * send a simple reply
708 */
709void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
710{
711 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500712 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100713 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700714
715 _enter("");
716
717 iov[0].iov_base = (void *) buf;
718 iov[0].iov_len = len;
719 msg.msg_name = NULL;
720 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500721 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700722 msg.msg_control = NULL;
723 msg.msg_controllen = 0;
724 msg.msg_flags = 0;
725
726 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100727 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
David Howellsbd6dc742007-07-20 10:59:41 +0100728 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100729 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700730 _leave(" [replied]");
731 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100732 }
David Howells6c67c7c2014-05-21 14:48:05 +0100733
David Howellsbd6dc742007-07-20 10:59:41 +0100734 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700735 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100736 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
737 RX_USER_ABORT);
David Howellsb908fe62007-04-26 15:58:17 -0700738 }
David Howells6c67c7c2014-05-21 14:48:05 +0100739 afs_end_call(call);
David Howellsbd6dc742007-07-20 10:59:41 +0100740 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700741}
742
743/*
David Howells372ee162016-08-03 14:11:40 +0100744 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700745 */
David Howellsd0016482016-08-30 20:42:14 +0100746int afs_extract_data(struct afs_call *call, void *buf, size_t count,
747 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700748{
David Howellsd0016482016-08-30 20:42:14 +0100749 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700750
David Howellsd0016482016-08-30 20:42:14 +0100751 _enter("{%s,%zu},,%zu,%d",
752 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700753
David Howellsd0016482016-08-30 20:42:14 +0100754 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700755
David Howellsd0016482016-08-30 20:42:14 +0100756 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
757 buf, count, &call->offset,
758 want_more, &call->abort_code);
759 if (ret == 0 || ret == -EAGAIN)
760 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700761
David Howellsd0016482016-08-30 20:42:14 +0100762 if (ret == 1) {
763 switch (call->state) {
764 case AFS_CALL_AWAIT_REPLY:
765 call->state = AFS_CALL_COMPLETE;
766 break;
767 case AFS_CALL_AWAIT_REQUEST:
768 call->state = AFS_CALL_REPLYING;
769 break;
770 default:
771 break;
772 }
773 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700774 }
David Howellsd0016482016-08-30 20:42:14 +0100775
776 if (ret == -ECONNABORTED)
777 call->error = call->type->abort_to_error(call->abort_code);
778 else
779 call->error = ret;
780 call->state = AFS_CALL_COMPLETE;
781 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700782}