blob: 7b0d18900f5040cc8f5992c7b64bb1d13e1bf6ea [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;
22static atomic_t afs_outstanding_skbs;
David Howells08e0e7c2007-04-26 15:55:03 -070023
24static void afs_wake_up_call_waiter(struct afs_call *);
25static int afs_wait_for_call_to_complete(struct afs_call *);
26static void afs_wake_up_async_call(struct afs_call *);
27static int afs_dont_wait_for_call_to_complete(struct afs_call *);
David Howells656f88d2014-05-21 15:55:26 +010028static void afs_process_async_call(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070029static void afs_rx_interceptor(struct sock *, unsigned long, struct sk_buff *);
30static int afs_deliver_cm_op_id(struct afs_call *, struct sk_buff *, bool);
31
32/* synchronous call management */
33const struct afs_wait_mode afs_sync_call = {
34 .rx_wakeup = afs_wake_up_call_waiter,
35 .wait = afs_wait_for_call_to_complete,
36};
37
38/* asynchronous call management */
39const struct afs_wait_mode afs_async_call = {
40 .rx_wakeup = afs_wake_up_async_call,
41 .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 = {
46 .rx_wakeup = afs_wake_up_async_call,
47};
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
58static struct sk_buff_head afs_incoming_calls;
59static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
60
Nathaniel Wesley Filardo150a6b42014-05-21 14:58:26 +010061static void afs_async_workfn(struct work_struct *work)
62{
63 struct afs_call *call = container_of(work, struct afs_call, async_work);
64
David Howells656f88d2014-05-21 15:55:26 +010065 call->async_workfn(call);
Nathaniel Wesley Filardo150a6b42014-05-21 14:58:26 +010066}
67
David Howells2f02f7a2016-04-07 17:23:03 +010068static int afs_wait_atomic_t(atomic_t *p)
69{
70 schedule();
71 return 0;
72}
73
David Howells08e0e7c2007-04-26 15:55:03 -070074/*
75 * open an RxRPC socket and bind it to be a server for callback notifications
76 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
77 */
78int afs_open_socket(void)
79{
80 struct sockaddr_rxrpc srx;
81 struct socket *socket;
82 int ret;
83
84 _enter("");
85
86 skb_queue_head_init(&afs_incoming_calls);
87
David Howells0e119b42016-06-10 22:30:37 +010088 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -070089 afs_async_calls = create_singlethread_workqueue("kafsd");
David Howells0e119b42016-06-10 22:30:37 +010090 if (!afs_async_calls)
91 goto error_0;
David Howells08e0e7c2007-04-26 15:55:03 -070092
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -050093 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
David Howells0e119b42016-06-10 22:30:37 +010094 if (ret < 0)
95 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070096
97 socket->sk->sk_allocation = GFP_NOFS;
98
99 /* bind the callback manager's address to make this a server socket */
100 srx.srx_family = AF_RXRPC;
101 srx.srx_service = CM_SERVICE;
102 srx.transport_type = SOCK_DGRAM;
103 srx.transport_len = sizeof(srx.transport.sin);
104 srx.transport.sin.sin_family = AF_INET;
105 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
106 memset(&srx.transport.sin.sin_addr, 0,
107 sizeof(srx.transport.sin.sin_addr));
108
109 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
David Howells0e119b42016-06-10 22:30:37 +0100110 if (ret < 0)
111 goto error_2;
112
113 ret = kernel_listen(socket, INT_MAX);
114 if (ret < 0)
115 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -0700116
117 rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
118
119 afs_socket = socket;
120 _leave(" = 0");
121 return 0;
David Howells0e119b42016-06-10 22:30:37 +0100122
123error_2:
124 sock_release(socket);
125error_1:
126 destroy_workqueue(afs_async_calls);
127error_0:
128 _leave(" = %d", ret);
129 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700130}
131
132/*
133 * close the RxRPC socket AFS was using
134 */
135void afs_close_socket(void)
136{
137 _enter("");
138
David Howells2f02f7a2016-04-07 17:23:03 +0100139 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
140 TASK_UNINTERRUPTIBLE);
141 _debug("no outstanding calls");
142
David Howells08e0e7c2007-04-26 15:55:03 -0700143 sock_release(afs_socket);
144
145 _debug("dework");
146 destroy_workqueue(afs_async_calls);
David Howells00d3b7a2007-04-26 15:57:07 -0700147
148 ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700149 _leave("");
150}
151
152/*
David Howells372ee162016-08-03 14:11:40 +0100153 * Note that the data in a socket buffer is now consumed.
David Howells00d3b7a2007-04-26 15:57:07 -0700154 */
David Howells372ee162016-08-03 14:11:40 +0100155void afs_data_consumed(struct afs_call *call, struct sk_buff *skb)
David Howells00d3b7a2007-04-26 15:57:07 -0700156{
157 if (!skb) {
158 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs));
159 dump_stack();
160 } else {
161 _debug("DLVR %p{%u} [%d]",
162 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
David Howells372ee162016-08-03 14:11:40 +0100163 rxrpc_kernel_data_consumed(call->rxcall, skb);
David Howells00d3b7a2007-04-26 15:57:07 -0700164 }
165}
166
167/*
168 * free a socket buffer
169 */
170static void afs_free_skb(struct sk_buff *skb)
171{
172 if (!skb) {
173 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs));
174 dump_stack();
175 } else {
176 _debug("FREE %p{%u} [%d]",
177 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
178 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
179 BUG();
180 rxrpc_kernel_free_skb(skb);
181 }
182}
183
184/*
185 * free a call
186 */
187static void afs_free_call(struct afs_call *call)
188{
189 _debug("DONE %p{%s} [%d]",
190 call, call->type->name, atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700191
192 ASSERTCMP(call->rxcall, ==, NULL);
193 ASSERT(!work_pending(&call->async_work));
194 ASSERT(skb_queue_empty(&call->rx_queue));
195 ASSERT(call->type->name != NULL);
196
197 kfree(call->request);
198 kfree(call);
David Howells2f02f7a2016-04-07 17:23:03 +0100199
200 if (atomic_dec_and_test(&afs_outstanding_calls))
201 wake_up_atomic_t(&afs_outstanding_calls);
David Howells00d3b7a2007-04-26 15:57:07 -0700202}
203
204/*
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100205 * End a call but do not free it
David Howells6c67c7c2014-05-21 14:48:05 +0100206 */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100207static void afs_end_call_nofree(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100208{
209 if (call->rxcall) {
David Howells4de48af2016-08-30 12:00:48 +0100210 rxrpc_kernel_end_call(afs_socket, call->rxcall);
David Howells6c67c7c2014-05-21 14:48:05 +0100211 call->rxcall = NULL;
212 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100213 if (call->type->destructor)
214 call->type->destructor(call);
215}
216
217/*
218 * End a call and free it
219 */
220static void afs_end_call(struct afs_call *call)
221{
222 afs_end_call_nofree(call);
David Howells6c67c7c2014-05-21 14:48:05 +0100223 afs_free_call(call);
224}
225
226/*
David Howells08e0e7c2007-04-26 15:55:03 -0700227 * allocate a call with flat request and reply buffers
228 */
229struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
230 size_t request_size, size_t reply_size)
231{
232 struct afs_call *call;
233
234 call = kzalloc(sizeof(*call), GFP_NOFS);
235 if (!call)
236 goto nomem_call;
237
David Howells00d3b7a2007-04-26 15:57:07 -0700238 _debug("CALL %p{%s} [%d]",
239 call, type->name, atomic_read(&afs_outstanding_calls));
240 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700241
242 call->type = type;
243 call->request_size = request_size;
244 call->reply_max = reply_size;
245
David Howells00d3b7a2007-04-26 15:57:07 -0700246 if (request_size) {
247 call->request = kmalloc(request_size, GFP_NOFS);
248 if (!call->request)
249 goto nomem_free;
250 }
251
252 if (reply_size) {
253 call->buffer = kmalloc(reply_size, GFP_NOFS);
254 if (!call->buffer)
255 goto nomem_free;
256 }
257
David Howells08e0e7c2007-04-26 15:55:03 -0700258 init_waitqueue_head(&call->waitq);
259 skb_queue_head_init(&call->rx_queue);
260 return call;
261
David Howells00d3b7a2007-04-26 15:57:07 -0700262nomem_free:
263 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700264nomem_call:
265 return NULL;
266}
267
268/*
269 * clean up a call with flat buffer
270 */
271void afs_flat_call_destructor(struct afs_call *call)
272{
273 _enter("");
274
275 kfree(call->request);
276 call->request = NULL;
277 kfree(call->buffer);
278 call->buffer = NULL;
279}
280
281/*
David Howells31143d52007-05-09 02:33:46 -0700282 * attach the data from a bunch of pages on an inode to a call
283 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700284static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
285 struct kvec *iov)
David Howells31143d52007-05-09 02:33:46 -0700286{
287 struct page *pages[8];
288 unsigned count, n, loop, offset, to;
289 pgoff_t first = call->first, last = call->last;
290 int ret;
291
292 _enter("");
293
294 offset = call->first_offset;
295 call->first_offset = 0;
296
297 do {
298 _debug("attach %lx-%lx", first, last);
299
300 count = last - first + 1;
301 if (count > ARRAY_SIZE(pages))
302 count = ARRAY_SIZE(pages);
303 n = find_get_pages_contig(call->mapping, first, count, pages);
304 ASSERTCMP(n, ==, count);
305
306 loop = 0;
307 do {
308 msg->msg_flags = 0;
309 to = PAGE_SIZE;
310 if (first + loop >= last)
311 to = call->last_to;
312 else
313 msg->msg_flags = MSG_MORE;
314 iov->iov_base = kmap(pages[loop]) + offset;
315 iov->iov_len = to - offset;
316 offset = 0;
317
318 _debug("- range %u-%u%s",
319 offset, to, msg->msg_flags ? " [more]" : "");
Al Viro2e90b1c2014-11-27 21:50:31 -0500320 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
321 iov, 1, to - offset);
David Howells31143d52007-05-09 02:33:46 -0700322
323 /* have to change the state *before* sending the last
324 * packet as RxRPC might give us the reply before it
325 * returns from sending the request */
326 if (first + loop >= last)
327 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100328 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
329 msg, to - offset);
David Howells31143d52007-05-09 02:33:46 -0700330 kunmap(pages[loop]);
331 if (ret < 0)
332 break;
333 } while (++loop < count);
334 first += count;
335
336 for (loop = 0; loop < count; loop++)
337 put_page(pages[loop]);
338 if (ret < 0)
339 break;
David Howells5bbf5d32007-05-10 03:15:23 -0700340 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700341
342 _leave(" = %d", ret);
343 return ret;
344}
345
346/*
David Howells08e0e7c2007-04-26 15:55:03 -0700347 * initiate a call
348 */
349int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
350 const struct afs_wait_mode *wait_mode)
351{
352 struct sockaddr_rxrpc srx;
353 struct rxrpc_call *rxcall;
354 struct msghdr msg;
355 struct kvec iov[1];
356 int ret;
Anton Blanchardc0173862012-03-16 10:28:19 +0000357 struct sk_buff *skb;
David Howells08e0e7c2007-04-26 15:55:03 -0700358
359 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
360
David Howells00d3b7a2007-04-26 15:57:07 -0700361 ASSERT(call->type != NULL);
362 ASSERT(call->type->name != NULL);
363
David Howells31143d52007-05-09 02:33:46 -0700364 _debug("____MAKE %p{%s,%x} [%d]____",
365 call, call->type->name, key_serial(call->key),
366 atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700367
David Howells08e0e7c2007-04-26 15:55:03 -0700368 call->wait_mode = wait_mode;
Nathaniel Wesley Filardo150a6b42014-05-21 14:58:26 +0100369 call->async_workfn = afs_process_async_call;
370 INIT_WORK(&call->async_work, afs_async_workfn);
David Howells08e0e7c2007-04-26 15:55:03 -0700371
372 memset(&srx, 0, sizeof(srx));
373 srx.srx_family = AF_RXRPC;
374 srx.srx_service = call->service_id;
375 srx.transport_type = SOCK_DGRAM;
376 srx.transport_len = sizeof(srx.transport.sin);
377 srx.transport.sin.sin_family = AF_INET;
378 srx.transport.sin.sin_port = call->port;
379 memcpy(&srx.transport.sin.sin_addr, addr, 4);
380
381 /* create a call */
382 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
383 (unsigned long) call, gfp);
David Howells00d3b7a2007-04-26 15:57:07 -0700384 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700385 if (IS_ERR(rxcall)) {
386 ret = PTR_ERR(rxcall);
387 goto error_kill_call;
388 }
389
390 call->rxcall = rxcall;
391
392 /* send the request */
393 iov[0].iov_base = call->request;
394 iov[0].iov_len = call->request_size;
395
396 msg.msg_name = NULL;
397 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500398 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
Al Viroc0371da2014-11-24 10:42:55 -0500399 call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700400 msg.msg_control = NULL;
401 msg.msg_controllen = 0;
David Howells31143d52007-05-09 02:33:46 -0700402 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700403
404 /* have to change the state *before* sending the last packet as RxRPC
405 * might give us the reply before it returns from sending the
406 * request */
David Howells31143d52007-05-09 02:33:46 -0700407 if (!call->send_pages)
408 call->state = AFS_CALL_AWAIT_REPLY;
David Howells4de48af2016-08-30 12:00:48 +0100409 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
410 &msg, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700411 if (ret < 0)
412 goto error_do_abort;
413
David Howells31143d52007-05-09 02:33:46 -0700414 if (call->send_pages) {
415 ret = afs_send_pages(call, &msg, iov);
416 if (ret < 0)
417 goto error_do_abort;
418 }
419
David Howells08e0e7c2007-04-26 15:55:03 -0700420 /* at this point, an async call may no longer exist as it may have
421 * already completed */
422 return wait_mode->wait(call);
423
424error_do_abort:
David Howells4de48af2016-08-30 12:00:48 +0100425 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT);
Anton Blanchardc0173862012-03-16 10:28:19 +0000426 while ((skb = skb_dequeue(&call->rx_queue)))
427 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700428error_kill_call:
David Howells6c67c7c2014-05-21 14:48:05 +0100429 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700430 _leave(" = %d", ret);
431 return ret;
432}
433
434/*
David Howellsdc44b3a2016-04-07 17:23:30 +0100435 * Handles intercepted messages that were arriving in the socket's Rx queue.
436 *
437 * Called from the AF_RXRPC call processor in waitqueue process context. For
438 * each call, it is guaranteed this will be called in order of packet to be
439 * delivered.
David Howells08e0e7c2007-04-26 15:55:03 -0700440 */
441static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
442 struct sk_buff *skb)
443{
444 struct afs_call *call = (struct afs_call *) user_call_ID;
445
446 _enter("%p,,%u", call, skb->mark);
447
David Howells00d3b7a2007-04-26 15:57:07 -0700448 _debug("ICPT %p{%u} [%d]",
449 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
450
David Howells08e0e7c2007-04-26 15:55:03 -0700451 ASSERTCMP(sk, ==, afs_socket->sk);
David Howells00d3b7a2007-04-26 15:57:07 -0700452 atomic_inc(&afs_outstanding_skbs);
David Howells08e0e7c2007-04-26 15:55:03 -0700453
454 if (!call) {
455 /* its an incoming call for our callback service */
David Howells00d3b7a2007-04-26 15:57:07 -0700456 skb_queue_tail(&afs_incoming_calls, skb);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000457 queue_work(afs_wq, &afs_collect_incoming_call_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700458 } else {
459 /* route the messages directly to the appropriate call */
David Howells00d3b7a2007-04-26 15:57:07 -0700460 skb_queue_tail(&call->rx_queue, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700461 call->wait_mode->rx_wakeup(call);
462 }
463
464 _leave("");
465}
466
467/*
468 * deliver messages to a call
469 */
470static void afs_deliver_to_call(struct afs_call *call)
471{
472 struct sk_buff *skb;
473 bool last;
474 u32 abort_code;
475 int ret;
476
477 _enter("");
478
479 while ((call->state == AFS_CALL_AWAIT_REPLY ||
480 call->state == AFS_CALL_AWAIT_OP_ID ||
481 call->state == AFS_CALL_AWAIT_REQUEST ||
482 call->state == AFS_CALL_AWAIT_ACK) &&
483 (skb = skb_dequeue(&call->rx_queue))) {
484 switch (skb->mark) {
485 case RXRPC_SKB_MARK_DATA:
486 _debug("Rcv DATA");
487 last = rxrpc_kernel_is_data_last(skb);
488 ret = call->type->deliver(call, skb, last);
489 switch (ret) {
David Howells372ee162016-08-03 14:11:40 +0100490 case -EAGAIN:
491 if (last) {
492 _debug("short data");
493 goto unmarshal_error;
494 }
495 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700496 case 0:
David Howells372ee162016-08-03 14:11:40 +0100497 ASSERT(last);
498 if (call->state == AFS_CALL_AWAIT_REPLY)
David Howells08e0e7c2007-04-26 15:55:03 -0700499 call->state = AFS_CALL_COMPLETE;
500 break;
501 case -ENOTCONN:
502 abort_code = RX_CALL_DEAD;
503 goto do_abort;
504 case -ENOTSUPP:
505 abort_code = RX_INVALID_OPERATION;
506 goto do_abort;
507 default:
David Howells372ee162016-08-03 14:11:40 +0100508 unmarshal_error:
David Howells08e0e7c2007-04-26 15:55:03 -0700509 abort_code = RXGEN_CC_UNMARSHAL;
510 if (call->state != AFS_CALL_AWAIT_REPLY)
511 abort_code = RXGEN_SS_UNMARSHAL;
512 do_abort:
David Howells4de48af2016-08-30 12:00:48 +0100513 rxrpc_kernel_abort_call(afs_socket,
514 call->rxcall,
David Howells08e0e7c2007-04-26 15:55:03 -0700515 abort_code);
516 call->error = ret;
517 call->state = AFS_CALL_ERROR;
518 break;
519 }
David Howells372ee162016-08-03 14:11:40 +0100520 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700521 case RXRPC_SKB_MARK_FINAL_ACK:
522 _debug("Rcv ACK");
523 call->state = AFS_CALL_COMPLETE;
524 break;
525 case RXRPC_SKB_MARK_BUSY:
526 _debug("Rcv BUSY");
527 call->error = -EBUSY;
528 call->state = AFS_CALL_BUSY;
529 break;
530 case RXRPC_SKB_MARK_REMOTE_ABORT:
531 abort_code = rxrpc_kernel_get_abort_code(skb);
532 call->error = call->type->abort_to_error(abort_code);
533 call->state = AFS_CALL_ABORTED;
534 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
535 break;
David Howellsdc44b3a2016-04-07 17:23:30 +0100536 case RXRPC_SKB_MARK_LOCAL_ABORT:
537 abort_code = rxrpc_kernel_get_abort_code(skb);
538 call->error = call->type->abort_to_error(abort_code);
539 call->state = AFS_CALL_ABORTED;
540 _debug("Loc ABORT %u -> %d", abort_code, call->error);
541 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700542 case RXRPC_SKB_MARK_NET_ERROR:
543 call->error = -rxrpc_kernel_get_error_number(skb);
544 call->state = AFS_CALL_ERROR;
545 _debug("Rcv NET ERROR %d", call->error);
546 break;
547 case RXRPC_SKB_MARK_LOCAL_ERROR:
548 call->error = -rxrpc_kernel_get_error_number(skb);
549 call->state = AFS_CALL_ERROR;
550 _debug("Rcv LOCAL ERROR %d", call->error);
551 break;
552 default:
553 BUG();
554 break;
555 }
556
David Howells00d3b7a2007-04-26 15:57:07 -0700557 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700558 }
559
560 /* make sure the queue is empty if the call is done with (we might have
561 * aborted the call early because of an unmarshalling error) */
562 if (call->state >= AFS_CALL_COMPLETE) {
563 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700564 afs_free_skb(skb);
David Howells6c67c7c2014-05-21 14:48:05 +0100565 if (call->incoming)
566 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700567 }
568
569 _leave("");
570}
571
572/*
573 * wait synchronously for a call to complete
574 */
575static int afs_wait_for_call_to_complete(struct afs_call *call)
576{
577 struct sk_buff *skb;
578 int ret;
579
580 DECLARE_WAITQUEUE(myself, current);
581
582 _enter("");
583
584 add_wait_queue(&call->waitq, &myself);
585 for (;;) {
586 set_current_state(TASK_INTERRUPTIBLE);
587
588 /* deliver any messages that are in the queue */
589 if (!skb_queue_empty(&call->rx_queue)) {
590 __set_current_state(TASK_RUNNING);
591 afs_deliver_to_call(call);
592 continue;
593 }
594
595 ret = call->error;
596 if (call->state >= AFS_CALL_COMPLETE)
597 break;
598 ret = -EINTR;
599 if (signal_pending(current))
600 break;
601 schedule();
602 }
603
604 remove_wait_queue(&call->waitq, &myself);
605 __set_current_state(TASK_RUNNING);
606
607 /* kill the call */
608 if (call->state < AFS_CALL_COMPLETE) {
609 _debug("call incomplete");
David Howells4de48af2016-08-30 12:00:48 +0100610 rxrpc_kernel_abort_call(afs_socket, call->rxcall, RX_CALL_DEAD);
David Howells08e0e7c2007-04-26 15:55:03 -0700611 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700612 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700613 }
614
615 _debug("call complete");
David Howells6c67c7c2014-05-21 14:48:05 +0100616 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700617 _leave(" = %d", ret);
618 return ret;
619}
620
621/*
622 * wake up a waiting call
623 */
624static void afs_wake_up_call_waiter(struct afs_call *call)
625{
626 wake_up(&call->waitq);
627}
628
629/*
630 * wake up an asynchronous call
631 */
632static void afs_wake_up_async_call(struct afs_call *call)
633{
634 _enter("");
635 queue_work(afs_async_calls, &call->async_work);
636}
637
638/*
639 * put a call into asynchronous mode
640 * - mustn't touch the call descriptor as the call my have completed by the
641 * time we get here
642 */
643static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
644{
645 _enter("");
646 return -EINPROGRESS;
647}
648
649/*
650 * delete an asynchronous call
651 */
David Howells656f88d2014-05-21 15:55:26 +0100652static void afs_delete_async_call(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700653{
David Howells08e0e7c2007-04-26 15:55:03 -0700654 _enter("");
655
David Howells00d3b7a2007-04-26 15:57:07 -0700656 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700657
658 _leave("");
659}
660
661/*
662 * perform processing on an asynchronous call
663 * - on a multiple-thread workqueue this work item may try to run on several
664 * CPUs at the same time
665 */
David Howells656f88d2014-05-21 15:55:26 +0100666static void afs_process_async_call(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700667{
David Howells08e0e7c2007-04-26 15:55:03 -0700668 _enter("");
669
670 if (!skb_queue_empty(&call->rx_queue))
671 afs_deliver_to_call(call);
672
673 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
674 if (call->wait_mode->async_complete)
675 call->wait_mode->async_complete(call->reply,
676 call->error);
677 call->reply = NULL;
678
679 /* kill the call */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100680 afs_end_call_nofree(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700681
682 /* we can't just delete the call because the work item may be
683 * queued */
Tejun Heo05949942014-03-07 10:24:50 -0500684 call->async_workfn = afs_delete_async_call;
David Howells08e0e7c2007-04-26 15:55:03 -0700685 queue_work(afs_async_calls, &call->async_work);
686 }
687
688 _leave("");
689}
690
691/*
David Howells372ee162016-08-03 14:11:40 +0100692 * Empty a socket buffer into a flat reply buffer.
David Howells08e0e7c2007-04-26 15:55:03 -0700693 */
David Howells372ee162016-08-03 14:11:40 +0100694int afs_transfer_reply(struct afs_call *call, struct sk_buff *skb, bool last)
David Howells08e0e7c2007-04-26 15:55:03 -0700695{
696 size_t len = skb->len;
697
David Howells372ee162016-08-03 14:11:40 +0100698 if (len > call->reply_max - call->reply_size) {
699 _leave(" = -EBADMSG [%zu > %u]",
700 len, call->reply_max - call->reply_size);
701 return -EBADMSG;
702 }
703
704 if (len > 0) {
705 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size,
706 len) < 0)
707 BUG();
708 call->reply_size += len;
709 }
710
711 afs_data_consumed(call, skb);
712 if (!last)
713 return -EAGAIN;
714
715 if (call->reply_size != call->reply_max) {
716 _leave(" = -EBADMSG [%u != %u]",
717 call->reply_size, call->reply_max);
718 return -EBADMSG;
719 }
720 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700721}
722
723/*
724 * accept the backlog of incoming calls
725 */
726static void afs_collect_incoming_call(struct work_struct *work)
727{
728 struct rxrpc_call *rxcall;
729 struct afs_call *call = NULL;
730 struct sk_buff *skb;
731
732 while ((skb = skb_dequeue(&afs_incoming_calls))) {
733 _debug("new call");
734
735 /* don't need the notification */
David Howells00d3b7a2007-04-26 15:57:07 -0700736 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700737
738 if (!call) {
739 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
740 if (!call) {
741 rxrpc_kernel_reject_call(afs_socket);
742 return;
743 }
744
Tejun Heo05949942014-03-07 10:24:50 -0500745 call->async_workfn = afs_process_async_call;
746 INIT_WORK(&call->async_work, afs_async_workfn);
David Howells08e0e7c2007-04-26 15:55:03 -0700747 call->wait_mode = &afs_async_incoming_call;
748 call->type = &afs_RXCMxxxx;
749 init_waitqueue_head(&call->waitq);
750 skb_queue_head_init(&call->rx_queue);
751 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700752
753 _debug("CALL %p{%s} [%d]",
754 call, call->type->name,
755 atomic_read(&afs_outstanding_calls));
756 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700757 }
758
759 rxcall = rxrpc_kernel_accept_call(afs_socket,
760 (unsigned long) call);
761 if (!IS_ERR(rxcall)) {
762 call->rxcall = rxcall;
763 call = NULL;
764 }
765 }
766
David Howells00d3b7a2007-04-26 15:57:07 -0700767 if (call)
768 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700769}
770
771/*
David Howells372ee162016-08-03 14:11:40 +0100772 * Grab the operation ID from an incoming cache manager call. The socket
773 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700774 */
775static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
776 bool last)
777{
778 size_t len = skb->len;
779 void *oibuf = (void *) &call->operation_ID;
780
781 _enter("{%u},{%zu},%d", call->offset, len, last);
782
783 ASSERTCMP(call->offset, <, 4);
784
785 /* the operation ID forms the first four bytes of the request data */
786 len = min_t(size_t, len, 4 - call->offset);
787 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
788 BUG();
789 if (!pskb_pull(skb, len))
790 BUG();
791 call->offset += len;
792
793 if (call->offset < 4) {
David Howells372ee162016-08-03 14:11:40 +0100794 afs_data_consumed(call, skb);
795 _leave(" = -EAGAIN");
796 return -EAGAIN;
David Howells08e0e7c2007-04-26 15:55:03 -0700797 }
798
799 call->state = AFS_CALL_AWAIT_REQUEST;
800
801 /* ask the cache manager to route the call (it'll change the call type
802 * if successful) */
803 if (!afs_cm_incoming_call(call))
804 return -ENOTSUPP;
805
806 /* pass responsibility for the remainer of this message off to the
807 * cache manager op */
808 return call->type->deliver(call, skb, last);
809}
810
811/*
812 * send an empty reply
813 */
814void afs_send_empty_reply(struct afs_call *call)
815{
816 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700817
818 _enter("");
819
David Howells08e0e7c2007-04-26 15:55:03 -0700820 msg.msg_name = NULL;
821 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100822 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700823 msg.msg_control = NULL;
824 msg.msg_controllen = 0;
825 msg.msg_flags = 0;
826
827 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100828 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700829 case 0:
830 _leave(" [replied]");
831 return;
832
833 case -ENOMEM:
834 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100835 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
836 RX_USER_ABORT);
David Howells08e0e7c2007-04-26 15:55:03 -0700837 default:
David Howells6c67c7c2014-05-21 14:48:05 +0100838 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700839 _leave(" [error]");
840 return;
841 }
842}
843
844/*
David Howellsb908fe62007-04-26 15:58:17 -0700845 * send a simple reply
846 */
847void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
848{
849 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500850 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100851 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700852
853 _enter("");
854
855 iov[0].iov_base = (void *) buf;
856 iov[0].iov_len = len;
857 msg.msg_name = NULL;
858 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500859 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700860 msg.msg_control = NULL;
861 msg.msg_controllen = 0;
862 msg.msg_flags = 0;
863
864 call->state = AFS_CALL_AWAIT_ACK;
David Howells4de48af2016-08-30 12:00:48 +0100865 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
David Howellsbd6dc742007-07-20 10:59:41 +0100866 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100867 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700868 _leave(" [replied]");
869 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100870 }
David Howells6c67c7c2014-05-21 14:48:05 +0100871
David Howellsbd6dc742007-07-20 10:59:41 +0100872 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700873 _debug("oom");
David Howells4de48af2016-08-30 12:00:48 +0100874 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
875 RX_USER_ABORT);
David Howellsb908fe62007-04-26 15:58:17 -0700876 }
David Howells6c67c7c2014-05-21 14:48:05 +0100877 afs_end_call(call);
David Howellsbd6dc742007-07-20 10:59:41 +0100878 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700879}
880
881/*
David Howells372ee162016-08-03 14:11:40 +0100882 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700883 */
884int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
885 bool last, void *buf, size_t count)
886{
887 size_t len = skb->len;
888
889 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
890
891 ASSERTCMP(call->offset, <, count);
892
893 len = min_t(size_t, len, count - call->offset);
894 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
895 !pskb_pull(skb, len))
896 BUG();
897 call->offset += len;
898
899 if (call->offset < count) {
David Howells372ee162016-08-03 14:11:40 +0100900 afs_data_consumed(call, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700901 _leave(" = -EAGAIN");
902 return -EAGAIN;
903 }
904 return 0;
905}