blob: 53750dece80e58ad91f7b4b605239b89e5549712 [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;
Bhaktipriya Shridhar69ad0522016-09-04 20:53:42 +053079 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
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 Howells5a429762016-09-06 22:19:51 +0100380 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
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,
David Howells5a429762016-09-06 22:19:51 +0100428 abort_code, -ret, "KNC");
David Howellsd0016482016-08-30 20:42:14 +0100429 goto do_abort;
430 case -ENOTSUPP:
431 abort_code = RX_INVALID_OPERATION;
432 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100433 abort_code, -ret, "KIV");
David Howellsd0016482016-08-30 20:42:14 +0100434 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,
David Howells5a429762016-09-06 22:19:51 +0100443 abort_code, EBADMSG, "KUM");
David Howellsd0016482016-08-30 20:42:14 +0100444 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 Howells5a429762016-09-06 22:19:51 +0100466 const char *abort_why;
David Howells08e0e7c2007-04-26 15:55:03 -0700467 int ret;
468
469 DECLARE_WAITQUEUE(myself, current);
470
471 _enter("");
472
473 add_wait_queue(&call->waitq, &myself);
474 for (;;) {
475 set_current_state(TASK_INTERRUPTIBLE);
476
477 /* deliver any messages that are in the queue */
David Howellsd0016482016-08-30 20:42:14 +0100478 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
479 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700480 __set_current_state(TASK_RUNNING);
481 afs_deliver_to_call(call);
482 continue;
483 }
484
David Howells5a429762016-09-06 22:19:51 +0100485 abort_why = "KWC";
David Howells08e0e7c2007-04-26 15:55:03 -0700486 ret = call->error;
David Howellsd0016482016-08-30 20:42:14 +0100487 if (call->state == AFS_CALL_COMPLETE)
David Howells08e0e7c2007-04-26 15:55:03 -0700488 break;
David Howells5a429762016-09-06 22:19:51 +0100489 abort_why = "KWI";
David Howells08e0e7c2007-04-26 15:55:03 -0700490 ret = -EINTR;
491 if (signal_pending(current))
492 break;
493 schedule();
494 }
495
496 remove_wait_queue(&call->waitq, &myself);
497 __set_current_state(TASK_RUNNING);
498
499 /* kill the call */
500 if (call->state < AFS_CALL_COMPLETE) {
501 _debug("call incomplete");
David Howellsd0016482016-08-30 20:42:14 +0100502 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100503 RX_CALL_DEAD, -ret, abort_why);
David Howells08e0e7c2007-04-26 15:55:03 -0700504 }
505
506 _debug("call complete");
David Howells6c67c7c2014-05-21 14:48:05 +0100507 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700508 _leave(" = %d", ret);
509 return ret;
510}
511
512/*
513 * wake up a waiting call
514 */
David Howellsd0016482016-08-30 20:42:14 +0100515static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
516 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700517{
David Howellsd0016482016-08-30 20:42:14 +0100518 struct afs_call *call = (struct afs_call *)call_user_ID;
519
520 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700521 wake_up(&call->waitq);
522}
523
524/*
525 * wake up an asynchronous call
526 */
David Howellsd0016482016-08-30 20:42:14 +0100527static void afs_wake_up_async_call(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 queue_work(afs_async_calls, &call->async_work);
534}
535
536/*
537 * put a call into asynchronous mode
538 * - mustn't touch the call descriptor as the call my have completed by the
539 * time we get here
540 */
541static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
542{
543 _enter("");
544 return -EINPROGRESS;
545}
546
547/*
548 * delete an asynchronous call
549 */
David Howellsd0016482016-08-30 20:42:14 +0100550static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700551{
David Howellsd0016482016-08-30 20:42:14 +0100552 struct afs_call *call = container_of(work, struct afs_call, async_work);
553
David Howells08e0e7c2007-04-26 15:55:03 -0700554 _enter("");
555
David Howells00d3b7a2007-04-26 15:57:07 -0700556 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700557
558 _leave("");
559}
560
561/*
562 * perform processing on an asynchronous call
David Howells08e0e7c2007-04-26 15:55:03 -0700563 */
David Howellsd0016482016-08-30 20:42:14 +0100564static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700565{
David Howellsd0016482016-08-30 20:42:14 +0100566 struct afs_call *call = container_of(work, struct afs_call, async_work);
567
David Howells08e0e7c2007-04-26 15:55:03 -0700568 _enter("");
569
David Howellsd0016482016-08-30 20:42:14 +0100570 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
571 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700572 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100573 }
David Howells08e0e7c2007-04-26 15:55:03 -0700574
David Howellsd0016482016-08-30 20:42:14 +0100575 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
David Howells08e0e7c2007-04-26 15:55:03 -0700576 if (call->wait_mode->async_complete)
577 call->wait_mode->async_complete(call->reply,
578 call->error);
579 call->reply = NULL;
580
581 /* kill the call */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100582 afs_end_call_nofree(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700583
584 /* we can't just delete the call because the work item may be
585 * queued */
David Howellsd0016482016-08-30 20:42:14 +0100586 call->async_work.func = afs_delete_async_call;
David Howells08e0e7c2007-04-26 15:55:03 -0700587 queue_work(afs_async_calls, &call->async_work);
588 }
589
590 _leave("");
591}
592
593/*
David Howells08e0e7c2007-04-26 15:55:03 -0700594 * accept the backlog of incoming calls
595 */
596static void afs_collect_incoming_call(struct work_struct *work)
597{
598 struct rxrpc_call *rxcall;
599 struct afs_call *call = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700600
David Howellsd0016482016-08-30 20:42:14 +0100601 _enter("");
David Howells08e0e7c2007-04-26 15:55:03 -0700602
David Howellsd0016482016-08-30 20:42:14 +0100603 do {
David Howells08e0e7c2007-04-26 15:55:03 -0700604 if (!call) {
605 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
606 if (!call) {
607 rxrpc_kernel_reject_call(afs_socket);
608 return;
609 }
610
David Howellsd0016482016-08-30 20:42:14 +0100611 INIT_WORK(&call->async_work, afs_process_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700612 call->wait_mode = &afs_async_incoming_call;
613 call->type = &afs_RXCMxxxx;
614 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700615 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700616
617 _debug("CALL %p{%s} [%d]",
618 call, call->type->name,
619 atomic_read(&afs_outstanding_calls));
620 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700621 }
622
623 rxcall = rxrpc_kernel_accept_call(afs_socket,
David Howellsd0016482016-08-30 20:42:14 +0100624 (unsigned long)call,
625 afs_wake_up_async_call);
David Howells08e0e7c2007-04-26 15:55:03 -0700626 if (!IS_ERR(rxcall)) {
627 call->rxcall = rxcall;
David Howellsd0016482016-08-30 20:42:14 +0100628 call->need_attention = true;
629 queue_work(afs_async_calls, &call->async_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700630 call = NULL;
631 }
David Howellsd0016482016-08-30 20:42:14 +0100632 } while (!call);
David Howells08e0e7c2007-04-26 15:55:03 -0700633
David Howells00d3b7a2007-04-26 15:57:07 -0700634 if (call)
635 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700636}
637
638/*
David Howellsd0016482016-08-30 20:42:14 +0100639 * Notification of an incoming call.
640 */
641static void afs_rx_new_call(struct sock *sk)
642{
643 queue_work(afs_wq, &afs_collect_incoming_call_work);
644}
645
646/*
David Howells372ee162016-08-03 14:11:40 +0100647 * Grab the operation ID from an incoming cache manager call. The socket
648 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700649 */
David Howellsd0016482016-08-30 20:42:14 +0100650static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700651{
David Howellsd0016482016-08-30 20:42:14 +0100652 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700653
David Howellsd0016482016-08-30 20:42:14 +0100654 _enter("{%zu}", call->offset);
David Howells08e0e7c2007-04-26 15:55:03 -0700655
656 ASSERTCMP(call->offset, <, 4);
657
658 /* the operation ID forms the first four bytes of the request data */
David Howellsd0016482016-08-30 20:42:14 +0100659 ret = afs_extract_data(call, &call->operation_ID, 4, true);
660 if (ret < 0)
661 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700662
663 call->state = AFS_CALL_AWAIT_REQUEST;
David Howellsd0016482016-08-30 20:42:14 +0100664 call->offset = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700665
666 /* ask the cache manager to route the call (it'll change the call type
667 * if successful) */
668 if (!afs_cm_incoming_call(call))
669 return -ENOTSUPP;
670
671 /* pass responsibility for the remainer of this message off to the
672 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100673 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700674}
675
676/*
677 * send an empty reply
678 */
679void afs_send_empty_reply(struct afs_call *call)
680{
681 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700682
683 _enter("");
684
David Howells08e0e7c2007-04-26 15:55:03 -0700685 msg.msg_name = NULL;
686 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100687 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700688 msg.msg_control = NULL;
689 msg.msg_controllen = 0;
690 msg.msg_flags = 0;
691
692 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100693 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700694 case 0:
695 _leave(" [replied]");
696 return;
697
698 case -ENOMEM:
699 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100700 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100701 RX_USER_ABORT, ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700702 default:
David Howells6c67c7c2014-05-21 14:48:05 +0100703 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700704 _leave(" [error]");
705 return;
706 }
707}
708
709/*
David Howellsb908fe62007-04-26 15:58:17 -0700710 * send a simple reply
711 */
712void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
713{
714 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500715 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100716 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700717
718 _enter("");
719
720 iov[0].iov_base = (void *) buf;
721 iov[0].iov_len = len;
722 msg.msg_name = NULL;
723 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500724 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700725 msg.msg_control = NULL;
726 msg.msg_controllen = 0;
727 msg.msg_flags = 0;
728
729 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100730 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
David Howellsbd6dc742007-07-20 10:59:41 +0100731 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100732 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700733 _leave(" [replied]");
734 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100735 }
David Howells6c67c7c2014-05-21 14:48:05 +0100736
David Howellsbd6dc742007-07-20 10:59:41 +0100737 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700738 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100739 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
David Howells5a429762016-09-06 22:19:51 +0100740 RX_USER_ABORT, ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700741 }
David Howells6c67c7c2014-05-21 14:48:05 +0100742 afs_end_call(call);
David Howellsbd6dc742007-07-20 10:59:41 +0100743 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700744}
745
746/*
David Howells372ee162016-08-03 14:11:40 +0100747 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700748 */
David Howellsd0016482016-08-30 20:42:14 +0100749int afs_extract_data(struct afs_call *call, void *buf, size_t count,
750 bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700751{
David Howellsd0016482016-08-30 20:42:14 +0100752 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700753
David Howellsd0016482016-08-30 20:42:14 +0100754 _enter("{%s,%zu},,%zu,%d",
755 call->type->name, call->offset, count, want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700756
David Howellsd0016482016-08-30 20:42:14 +0100757 ASSERTCMP(call->offset, <=, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700758
David Howellsd0016482016-08-30 20:42:14 +0100759 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
760 buf, count, &call->offset,
761 want_more, &call->abort_code);
762 if (ret == 0 || ret == -EAGAIN)
763 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700764
David Howellsd0016482016-08-30 20:42:14 +0100765 if (ret == 1) {
766 switch (call->state) {
767 case AFS_CALL_AWAIT_REPLY:
768 call->state = AFS_CALL_COMPLETE;
769 break;
770 case AFS_CALL_AWAIT_REQUEST:
771 call->state = AFS_CALL_REPLYING;
772 break;
773 default:
774 break;
775 }
776 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700777 }
David Howellsd0016482016-08-30 20:42:14 +0100778
779 if (ret == -ECONNABORTED)
780 call->error = call->type->abort_to_error(call->abort_code);
781 else
782 call->error = ret;
783 call->state = AFS_CALL_COMPLETE;
784 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700785}