blob: 14d04c848465a68f2b107c87b22b90f69062e9c5 [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
19static struct socket *afs_socket; /* my RxRPC socket */
20static 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) {
210 rxrpc_kernel_end_call(call->rxcall);
211 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;
328 ret = rxrpc_kernel_send_data(call->rxcall, msg,
329 to - offset);
330 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 Howells08e0e7c2007-04-26 15:55:03 -0700409 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
410 if (ret < 0)
411 goto error_do_abort;
412
David Howells31143d52007-05-09 02:33:46 -0700413 if (call->send_pages) {
414 ret = afs_send_pages(call, &msg, iov);
415 if (ret < 0)
416 goto error_do_abort;
417 }
418
David Howells08e0e7c2007-04-26 15:55:03 -0700419 /* at this point, an async call may no longer exist as it may have
420 * already completed */
421 return wait_mode->wait(call);
422
423error_do_abort:
424 rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
Anton Blanchardc0173862012-03-16 10:28:19 +0000425 while ((skb = skb_dequeue(&call->rx_queue)))
426 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700427error_kill_call:
David Howells6c67c7c2014-05-21 14:48:05 +0100428 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700429 _leave(" = %d", ret);
430 return ret;
431}
432
433/*
David Howellsdc44b3a2016-04-07 17:23:30 +0100434 * Handles intercepted messages that were arriving in the socket's Rx queue.
435 *
436 * Called from the AF_RXRPC call processor in waitqueue process context. For
437 * each call, it is guaranteed this will be called in order of packet to be
438 * delivered.
David Howells08e0e7c2007-04-26 15:55:03 -0700439 */
440static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
441 struct sk_buff *skb)
442{
443 struct afs_call *call = (struct afs_call *) user_call_ID;
444
445 _enter("%p,,%u", call, skb->mark);
446
David Howells00d3b7a2007-04-26 15:57:07 -0700447 _debug("ICPT %p{%u} [%d]",
448 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
449
David Howells08e0e7c2007-04-26 15:55:03 -0700450 ASSERTCMP(sk, ==, afs_socket->sk);
David Howells00d3b7a2007-04-26 15:57:07 -0700451 atomic_inc(&afs_outstanding_skbs);
David Howells08e0e7c2007-04-26 15:55:03 -0700452
453 if (!call) {
454 /* its an incoming call for our callback service */
David Howells00d3b7a2007-04-26 15:57:07 -0700455 skb_queue_tail(&afs_incoming_calls, skb);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000456 queue_work(afs_wq, &afs_collect_incoming_call_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700457 } else {
458 /* route the messages directly to the appropriate call */
David Howells00d3b7a2007-04-26 15:57:07 -0700459 skb_queue_tail(&call->rx_queue, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700460 call->wait_mode->rx_wakeup(call);
461 }
462
463 _leave("");
464}
465
466/*
467 * deliver messages to a call
468 */
469static void afs_deliver_to_call(struct afs_call *call)
470{
471 struct sk_buff *skb;
472 bool last;
473 u32 abort_code;
474 int ret;
475
476 _enter("");
477
478 while ((call->state == AFS_CALL_AWAIT_REPLY ||
479 call->state == AFS_CALL_AWAIT_OP_ID ||
480 call->state == AFS_CALL_AWAIT_REQUEST ||
481 call->state == AFS_CALL_AWAIT_ACK) &&
482 (skb = skb_dequeue(&call->rx_queue))) {
483 switch (skb->mark) {
484 case RXRPC_SKB_MARK_DATA:
485 _debug("Rcv DATA");
486 last = rxrpc_kernel_is_data_last(skb);
487 ret = call->type->deliver(call, skb, last);
488 switch (ret) {
David Howells372ee162016-08-03 14:11:40 +0100489 case -EAGAIN:
490 if (last) {
491 _debug("short data");
492 goto unmarshal_error;
493 }
494 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700495 case 0:
David Howells372ee162016-08-03 14:11:40 +0100496 ASSERT(last);
497 if (call->state == AFS_CALL_AWAIT_REPLY)
David Howells08e0e7c2007-04-26 15:55:03 -0700498 call->state = AFS_CALL_COMPLETE;
499 break;
500 case -ENOTCONN:
501 abort_code = RX_CALL_DEAD;
502 goto do_abort;
503 case -ENOTSUPP:
504 abort_code = RX_INVALID_OPERATION;
505 goto do_abort;
506 default:
David Howells372ee162016-08-03 14:11:40 +0100507 unmarshal_error:
David Howells08e0e7c2007-04-26 15:55:03 -0700508 abort_code = RXGEN_CC_UNMARSHAL;
509 if (call->state != AFS_CALL_AWAIT_REPLY)
510 abort_code = RXGEN_SS_UNMARSHAL;
511 do_abort:
512 rxrpc_kernel_abort_call(call->rxcall,
513 abort_code);
514 call->error = ret;
515 call->state = AFS_CALL_ERROR;
516 break;
517 }
David Howells372ee162016-08-03 14:11:40 +0100518 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700519 case RXRPC_SKB_MARK_FINAL_ACK:
520 _debug("Rcv ACK");
521 call->state = AFS_CALL_COMPLETE;
522 break;
523 case RXRPC_SKB_MARK_BUSY:
524 _debug("Rcv BUSY");
525 call->error = -EBUSY;
526 call->state = AFS_CALL_BUSY;
527 break;
528 case RXRPC_SKB_MARK_REMOTE_ABORT:
529 abort_code = rxrpc_kernel_get_abort_code(skb);
530 call->error = call->type->abort_to_error(abort_code);
531 call->state = AFS_CALL_ABORTED;
532 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
533 break;
David Howellsdc44b3a2016-04-07 17:23:30 +0100534 case RXRPC_SKB_MARK_LOCAL_ABORT:
535 abort_code = rxrpc_kernel_get_abort_code(skb);
536 call->error = call->type->abort_to_error(abort_code);
537 call->state = AFS_CALL_ABORTED;
538 _debug("Loc ABORT %u -> %d", abort_code, call->error);
539 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700540 case RXRPC_SKB_MARK_NET_ERROR:
541 call->error = -rxrpc_kernel_get_error_number(skb);
542 call->state = AFS_CALL_ERROR;
543 _debug("Rcv NET ERROR %d", call->error);
544 break;
545 case RXRPC_SKB_MARK_LOCAL_ERROR:
546 call->error = -rxrpc_kernel_get_error_number(skb);
547 call->state = AFS_CALL_ERROR;
548 _debug("Rcv LOCAL ERROR %d", call->error);
549 break;
550 default:
551 BUG();
552 break;
553 }
554
David Howells00d3b7a2007-04-26 15:57:07 -0700555 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700556 }
557
558 /* make sure the queue is empty if the call is done with (we might have
559 * aborted the call early because of an unmarshalling error) */
560 if (call->state >= AFS_CALL_COMPLETE) {
561 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700562 afs_free_skb(skb);
David Howells6c67c7c2014-05-21 14:48:05 +0100563 if (call->incoming)
564 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700565 }
566
567 _leave("");
568}
569
570/*
571 * wait synchronously for a call to complete
572 */
573static int afs_wait_for_call_to_complete(struct afs_call *call)
574{
575 struct sk_buff *skb;
576 int ret;
577
578 DECLARE_WAITQUEUE(myself, current);
579
580 _enter("");
581
582 add_wait_queue(&call->waitq, &myself);
583 for (;;) {
584 set_current_state(TASK_INTERRUPTIBLE);
585
586 /* deliver any messages that are in the queue */
587 if (!skb_queue_empty(&call->rx_queue)) {
588 __set_current_state(TASK_RUNNING);
589 afs_deliver_to_call(call);
590 continue;
591 }
592
593 ret = call->error;
594 if (call->state >= AFS_CALL_COMPLETE)
595 break;
596 ret = -EINTR;
597 if (signal_pending(current))
598 break;
599 schedule();
600 }
601
602 remove_wait_queue(&call->waitq, &myself);
603 __set_current_state(TASK_RUNNING);
604
605 /* kill the call */
606 if (call->state < AFS_CALL_COMPLETE) {
607 _debug("call incomplete");
608 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
609 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700610 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700611 }
612
613 _debug("call complete");
David Howells6c67c7c2014-05-21 14:48:05 +0100614 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700615 _leave(" = %d", ret);
616 return ret;
617}
618
619/*
620 * wake up a waiting call
621 */
622static void afs_wake_up_call_waiter(struct afs_call *call)
623{
624 wake_up(&call->waitq);
625}
626
627/*
628 * wake up an asynchronous call
629 */
630static void afs_wake_up_async_call(struct afs_call *call)
631{
632 _enter("");
633 queue_work(afs_async_calls, &call->async_work);
634}
635
636/*
637 * put a call into asynchronous mode
638 * - mustn't touch the call descriptor as the call my have completed by the
639 * time we get here
640 */
641static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
642{
643 _enter("");
644 return -EINPROGRESS;
645}
646
647/*
648 * delete an asynchronous call
649 */
David Howells656f88d2014-05-21 15:55:26 +0100650static void afs_delete_async_call(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700651{
David Howells08e0e7c2007-04-26 15:55:03 -0700652 _enter("");
653
David Howells00d3b7a2007-04-26 15:57:07 -0700654 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700655
656 _leave("");
657}
658
659/*
660 * perform processing on an asynchronous call
661 * - on a multiple-thread workqueue this work item may try to run on several
662 * CPUs at the same time
663 */
David Howells656f88d2014-05-21 15:55:26 +0100664static void afs_process_async_call(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700665{
David Howells08e0e7c2007-04-26 15:55:03 -0700666 _enter("");
667
668 if (!skb_queue_empty(&call->rx_queue))
669 afs_deliver_to_call(call);
670
671 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
672 if (call->wait_mode->async_complete)
673 call->wait_mode->async_complete(call->reply,
674 call->error);
675 call->reply = NULL;
676
677 /* kill the call */
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100678 afs_end_call_nofree(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700679
680 /* we can't just delete the call because the work item may be
681 * queued */
Tejun Heo05949942014-03-07 10:24:50 -0500682 call->async_workfn = afs_delete_async_call;
David Howells08e0e7c2007-04-26 15:55:03 -0700683 queue_work(afs_async_calls, &call->async_work);
684 }
685
686 _leave("");
687}
688
689/*
David Howells372ee162016-08-03 14:11:40 +0100690 * Empty a socket buffer into a flat reply buffer.
David Howells08e0e7c2007-04-26 15:55:03 -0700691 */
David Howells372ee162016-08-03 14:11:40 +0100692int afs_transfer_reply(struct afs_call *call, struct sk_buff *skb, bool last)
David Howells08e0e7c2007-04-26 15:55:03 -0700693{
694 size_t len = skb->len;
695
David Howells372ee162016-08-03 14:11:40 +0100696 if (len > call->reply_max - call->reply_size) {
697 _leave(" = -EBADMSG [%zu > %u]",
698 len, call->reply_max - call->reply_size);
699 return -EBADMSG;
700 }
701
702 if (len > 0) {
703 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size,
704 len) < 0)
705 BUG();
706 call->reply_size += len;
707 }
708
709 afs_data_consumed(call, skb);
710 if (!last)
711 return -EAGAIN;
712
713 if (call->reply_size != call->reply_max) {
714 _leave(" = -EBADMSG [%u != %u]",
715 call->reply_size, call->reply_max);
716 return -EBADMSG;
717 }
718 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700719}
720
721/*
722 * accept the backlog of incoming calls
723 */
724static void afs_collect_incoming_call(struct work_struct *work)
725{
726 struct rxrpc_call *rxcall;
727 struct afs_call *call = NULL;
728 struct sk_buff *skb;
729
730 while ((skb = skb_dequeue(&afs_incoming_calls))) {
731 _debug("new call");
732
733 /* don't need the notification */
David Howells00d3b7a2007-04-26 15:57:07 -0700734 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700735
736 if (!call) {
737 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
738 if (!call) {
739 rxrpc_kernel_reject_call(afs_socket);
740 return;
741 }
742
Tejun Heo05949942014-03-07 10:24:50 -0500743 call->async_workfn = afs_process_async_call;
744 INIT_WORK(&call->async_work, afs_async_workfn);
David Howells08e0e7c2007-04-26 15:55:03 -0700745 call->wait_mode = &afs_async_incoming_call;
746 call->type = &afs_RXCMxxxx;
747 init_waitqueue_head(&call->waitq);
748 skb_queue_head_init(&call->rx_queue);
749 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700750
751 _debug("CALL %p{%s} [%d]",
752 call, call->type->name,
753 atomic_read(&afs_outstanding_calls));
754 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700755 }
756
757 rxcall = rxrpc_kernel_accept_call(afs_socket,
758 (unsigned long) call);
759 if (!IS_ERR(rxcall)) {
760 call->rxcall = rxcall;
761 call = NULL;
762 }
763 }
764
David Howells00d3b7a2007-04-26 15:57:07 -0700765 if (call)
766 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700767}
768
769/*
David Howells372ee162016-08-03 14:11:40 +0100770 * Grab the operation ID from an incoming cache manager call. The socket
771 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700772 */
773static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
774 bool last)
775{
776 size_t len = skb->len;
777 void *oibuf = (void *) &call->operation_ID;
778
779 _enter("{%u},{%zu},%d", call->offset, len, last);
780
781 ASSERTCMP(call->offset, <, 4);
782
783 /* the operation ID forms the first four bytes of the request data */
784 len = min_t(size_t, len, 4 - call->offset);
785 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
786 BUG();
787 if (!pskb_pull(skb, len))
788 BUG();
789 call->offset += len;
790
791 if (call->offset < 4) {
David Howells372ee162016-08-03 14:11:40 +0100792 afs_data_consumed(call, skb);
793 _leave(" = -EAGAIN");
794 return -EAGAIN;
David Howells08e0e7c2007-04-26 15:55:03 -0700795 }
796
797 call->state = AFS_CALL_AWAIT_REQUEST;
798
799 /* ask the cache manager to route the call (it'll change the call type
800 * if successful) */
801 if (!afs_cm_incoming_call(call))
802 return -ENOTSUPP;
803
804 /* pass responsibility for the remainer of this message off to the
805 * cache manager op */
806 return call->type->deliver(call, skb, last);
807}
808
809/*
810 * send an empty reply
811 */
812void afs_send_empty_reply(struct afs_call *call)
813{
814 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700815
816 _enter("");
817
David Howells08e0e7c2007-04-26 15:55:03 -0700818 msg.msg_name = NULL;
819 msg.msg_namelen = 0;
David Howellsbfd4e952015-04-01 16:03:46 +0100820 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700821 msg.msg_control = NULL;
822 msg.msg_controllen = 0;
823 msg.msg_flags = 0;
824
825 call->state = AFS_CALL_AWAIT_ACK;
826 switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
827 case 0:
828 _leave(" [replied]");
829 return;
830
831 case -ENOMEM:
832 _debug("oom");
833 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
834 default:
David Howells6c67c7c2014-05-21 14:48:05 +0100835 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700836 _leave(" [error]");
837 return;
838 }
839}
840
841/*
David Howellsb908fe62007-04-26 15:58:17 -0700842 * send a simple reply
843 */
844void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
845{
846 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500847 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100848 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700849
850 _enter("");
851
852 iov[0].iov_base = (void *) buf;
853 iov[0].iov_len = len;
854 msg.msg_name = NULL;
855 msg.msg_namelen = 0;
Al Viro2e90b1c2014-11-27 21:50:31 -0500856 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700857 msg.msg_control = NULL;
858 msg.msg_controllen = 0;
859 msg.msg_flags = 0;
860
861 call->state = AFS_CALL_AWAIT_ACK;
David Howellsbd6dc742007-07-20 10:59:41 +0100862 n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
863 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100864 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700865 _leave(" [replied]");
866 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100867 }
David Howells6c67c7c2014-05-21 14:48:05 +0100868
David Howellsbd6dc742007-07-20 10:59:41 +0100869 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700870 _debug("oom");
871 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
David Howellsb908fe62007-04-26 15:58:17 -0700872 }
David Howells6c67c7c2014-05-21 14:48:05 +0100873 afs_end_call(call);
David Howellsbd6dc742007-07-20 10:59:41 +0100874 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700875}
876
877/*
David Howells372ee162016-08-03 14:11:40 +0100878 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700879 */
880int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
881 bool last, void *buf, size_t count)
882{
883 size_t len = skb->len;
884
885 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
886
887 ASSERTCMP(call->offset, <, count);
888
889 len = min_t(size_t, len, count - call->offset);
890 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
891 !pskb_pull(skb, len))
892 BUG();
893 call->offset += len;
894
895 if (call->offset < count) {
David Howells372ee162016-08-03 14:11:40 +0100896 afs_data_consumed(call, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700897 _leave(" = -EAGAIN");
898 return -EAGAIN;
899 }
900 return 0;
901}