blob: 9226a6674d7f87a49af67dab3e8f63ae9ea31c86 [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 *);
28static void afs_process_async_call(struct work_struct *);
29static 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
61/*
62 * open an RxRPC socket and bind it to be a server for callback notifications
63 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
64 */
65int afs_open_socket(void)
66{
67 struct sockaddr_rxrpc srx;
68 struct socket *socket;
69 int ret;
70
71 _enter("");
72
73 skb_queue_head_init(&afs_incoming_calls);
74
75 afs_async_calls = create_singlethread_workqueue("kafsd");
76 if (!afs_async_calls) {
77 _leave(" = -ENOMEM [wq]");
78 return -ENOMEM;
79 }
80
81 ret = sock_create_kern(AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
82 if (ret < 0) {
83 destroy_workqueue(afs_async_calls);
84 _leave(" = %d [socket]", ret);
85 return ret;
86 }
87
88 socket->sk->sk_allocation = GFP_NOFS;
89
90 /* bind the callback manager's address to make this a server socket */
91 srx.srx_family = AF_RXRPC;
92 srx.srx_service = CM_SERVICE;
93 srx.transport_type = SOCK_DGRAM;
94 srx.transport_len = sizeof(srx.transport.sin);
95 srx.transport.sin.sin_family = AF_INET;
96 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
97 memset(&srx.transport.sin.sin_addr, 0,
98 sizeof(srx.transport.sin.sin_addr));
99
100 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
101 if (ret < 0) {
102 sock_release(socket);
Dan Carpenterbebf8cf2010-08-10 18:03:27 -0700103 destroy_workqueue(afs_async_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700104 _leave(" = %d [bind]", ret);
105 return ret;
106 }
107
108 rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
109
110 afs_socket = socket;
111 _leave(" = 0");
112 return 0;
113}
114
115/*
116 * close the RxRPC socket AFS was using
117 */
118void afs_close_socket(void)
119{
120 _enter("");
121
122 sock_release(afs_socket);
123
124 _debug("dework");
125 destroy_workqueue(afs_async_calls);
David Howells00d3b7a2007-04-26 15:57:07 -0700126
127 ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
128 ASSERTCMP(atomic_read(&afs_outstanding_calls), ==, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700129 _leave("");
130}
131
132/*
David Howells00d3b7a2007-04-26 15:57:07 -0700133 * note that the data in a socket buffer is now delivered and that the buffer
134 * should be freed
135 */
136static void afs_data_delivered(struct sk_buff *skb)
137{
138 if (!skb) {
139 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs));
140 dump_stack();
141 } else {
142 _debug("DLVR %p{%u} [%d]",
143 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
144 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
145 BUG();
146 rxrpc_kernel_data_delivered(skb);
147 }
148}
149
150/*
151 * free a socket buffer
152 */
153static void afs_free_skb(struct sk_buff *skb)
154{
155 if (!skb) {
156 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs));
157 dump_stack();
158 } else {
159 _debug("FREE %p{%u} [%d]",
160 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
161 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
162 BUG();
163 rxrpc_kernel_free_skb(skb);
164 }
165}
166
167/*
168 * free a call
169 */
170static void afs_free_call(struct afs_call *call)
171{
172 _debug("DONE %p{%s} [%d]",
173 call, call->type->name, atomic_read(&afs_outstanding_calls));
174 if (atomic_dec_return(&afs_outstanding_calls) == -1)
175 BUG();
176
177 ASSERTCMP(call->rxcall, ==, NULL);
178 ASSERT(!work_pending(&call->async_work));
179 ASSERT(skb_queue_empty(&call->rx_queue));
180 ASSERT(call->type->name != NULL);
181
182 kfree(call->request);
183 kfree(call);
184}
185
186/*
David Howells6c67c7c2014-05-21 14:48:05 +0100187 * End a call
188 */
189static void afs_end_call(struct afs_call *call)
190{
191 if (call->rxcall) {
192 rxrpc_kernel_end_call(call->rxcall);
193 call->rxcall = NULL;
194 }
195 call->type->destructor(call);
196 afs_free_call(call);
197}
198
199/*
David Howells08e0e7c2007-04-26 15:55:03 -0700200 * allocate a call with flat request and reply buffers
201 */
202struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
203 size_t request_size, size_t reply_size)
204{
205 struct afs_call *call;
206
207 call = kzalloc(sizeof(*call), GFP_NOFS);
208 if (!call)
209 goto nomem_call;
210
David Howells00d3b7a2007-04-26 15:57:07 -0700211 _debug("CALL %p{%s} [%d]",
212 call, type->name, atomic_read(&afs_outstanding_calls));
213 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700214
215 call->type = type;
216 call->request_size = request_size;
217 call->reply_max = reply_size;
218
David Howells00d3b7a2007-04-26 15:57:07 -0700219 if (request_size) {
220 call->request = kmalloc(request_size, GFP_NOFS);
221 if (!call->request)
222 goto nomem_free;
223 }
224
225 if (reply_size) {
226 call->buffer = kmalloc(reply_size, GFP_NOFS);
227 if (!call->buffer)
228 goto nomem_free;
229 }
230
David Howells08e0e7c2007-04-26 15:55:03 -0700231 init_waitqueue_head(&call->waitq);
232 skb_queue_head_init(&call->rx_queue);
233 return call;
234
David Howells00d3b7a2007-04-26 15:57:07 -0700235nomem_free:
236 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700237nomem_call:
238 return NULL;
239}
240
241/*
242 * clean up a call with flat buffer
243 */
244void afs_flat_call_destructor(struct afs_call *call)
245{
246 _enter("");
247
248 kfree(call->request);
249 call->request = NULL;
250 kfree(call->buffer);
251 call->buffer = NULL;
252}
253
254/*
David Howells31143d52007-05-09 02:33:46 -0700255 * attach the data from a bunch of pages on an inode to a call
256 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700257static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
258 struct kvec *iov)
David Howells31143d52007-05-09 02:33:46 -0700259{
260 struct page *pages[8];
261 unsigned count, n, loop, offset, to;
262 pgoff_t first = call->first, last = call->last;
263 int ret;
264
265 _enter("");
266
267 offset = call->first_offset;
268 call->first_offset = 0;
269
270 do {
271 _debug("attach %lx-%lx", first, last);
272
273 count = last - first + 1;
274 if (count > ARRAY_SIZE(pages))
275 count = ARRAY_SIZE(pages);
276 n = find_get_pages_contig(call->mapping, first, count, pages);
277 ASSERTCMP(n, ==, count);
278
279 loop = 0;
280 do {
281 msg->msg_flags = 0;
282 to = PAGE_SIZE;
283 if (first + loop >= last)
284 to = call->last_to;
285 else
286 msg->msg_flags = MSG_MORE;
287 iov->iov_base = kmap(pages[loop]) + offset;
288 iov->iov_len = to - offset;
289 offset = 0;
290
291 _debug("- range %u-%u%s",
292 offset, to, msg->msg_flags ? " [more]" : "");
293 msg->msg_iov = (struct iovec *) iov;
294 msg->msg_iovlen = 1;
295
296 /* have to change the state *before* sending the last
297 * packet as RxRPC might give us the reply before it
298 * returns from sending the request */
299 if (first + loop >= last)
300 call->state = AFS_CALL_AWAIT_REPLY;
301 ret = rxrpc_kernel_send_data(call->rxcall, msg,
302 to - offset);
303 kunmap(pages[loop]);
304 if (ret < 0)
305 break;
306 } while (++loop < count);
307 first += count;
308
309 for (loop = 0; loop < count; loop++)
310 put_page(pages[loop]);
311 if (ret < 0)
312 break;
David Howells5bbf5d32007-05-10 03:15:23 -0700313 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700314
315 _leave(" = %d", ret);
316 return ret;
317}
318
319/*
David Howells08e0e7c2007-04-26 15:55:03 -0700320 * initiate a call
321 */
322int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
323 const struct afs_wait_mode *wait_mode)
324{
325 struct sockaddr_rxrpc srx;
326 struct rxrpc_call *rxcall;
327 struct msghdr msg;
328 struct kvec iov[1];
329 int ret;
Anton Blanchardc0173862012-03-16 10:28:19 +0000330 struct sk_buff *skb;
David Howells08e0e7c2007-04-26 15:55:03 -0700331
332 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
333
David Howells00d3b7a2007-04-26 15:57:07 -0700334 ASSERT(call->type != NULL);
335 ASSERT(call->type->name != NULL);
336
David Howells31143d52007-05-09 02:33:46 -0700337 _debug("____MAKE %p{%s,%x} [%d]____",
338 call, call->type->name, key_serial(call->key),
339 atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700340
David Howells08e0e7c2007-04-26 15:55:03 -0700341 call->wait_mode = wait_mode;
342 INIT_WORK(&call->async_work, afs_process_async_call);
343
344 memset(&srx, 0, sizeof(srx));
345 srx.srx_family = AF_RXRPC;
346 srx.srx_service = call->service_id;
347 srx.transport_type = SOCK_DGRAM;
348 srx.transport_len = sizeof(srx.transport.sin);
349 srx.transport.sin.sin_family = AF_INET;
350 srx.transport.sin.sin_port = call->port;
351 memcpy(&srx.transport.sin.sin_addr, addr, 4);
352
353 /* create a call */
354 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
355 (unsigned long) call, gfp);
David Howells00d3b7a2007-04-26 15:57:07 -0700356 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700357 if (IS_ERR(rxcall)) {
358 ret = PTR_ERR(rxcall);
359 goto error_kill_call;
360 }
361
362 call->rxcall = rxcall;
363
364 /* send the request */
365 iov[0].iov_base = call->request;
366 iov[0].iov_len = call->request_size;
367
368 msg.msg_name = NULL;
369 msg.msg_namelen = 0;
370 msg.msg_iov = (struct iovec *) iov;
371 msg.msg_iovlen = 1;
372 msg.msg_control = NULL;
373 msg.msg_controllen = 0;
David Howells31143d52007-05-09 02:33:46 -0700374 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700375
376 /* have to change the state *before* sending the last packet as RxRPC
377 * might give us the reply before it returns from sending the
378 * request */
David Howells31143d52007-05-09 02:33:46 -0700379 if (!call->send_pages)
380 call->state = AFS_CALL_AWAIT_REPLY;
David Howells08e0e7c2007-04-26 15:55:03 -0700381 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
382 if (ret < 0)
383 goto error_do_abort;
384
David Howells31143d52007-05-09 02:33:46 -0700385 if (call->send_pages) {
386 ret = afs_send_pages(call, &msg, iov);
387 if (ret < 0)
388 goto error_do_abort;
389 }
390
David Howells08e0e7c2007-04-26 15:55:03 -0700391 /* at this point, an async call may no longer exist as it may have
392 * already completed */
393 return wait_mode->wait(call);
394
395error_do_abort:
396 rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
Anton Blanchardc0173862012-03-16 10:28:19 +0000397 while ((skb = skb_dequeue(&call->rx_queue)))
398 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700399error_kill_call:
David Howells6c67c7c2014-05-21 14:48:05 +0100400 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700401 _leave(" = %d", ret);
402 return ret;
403}
404
405/*
406 * handles intercepted messages that were arriving in the socket's Rx queue
407 * - called with the socket receive queue lock held to ensure message ordering
408 * - called with softirqs disabled
409 */
410static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
411 struct sk_buff *skb)
412{
413 struct afs_call *call = (struct afs_call *) user_call_ID;
414
415 _enter("%p,,%u", call, skb->mark);
416
David Howells00d3b7a2007-04-26 15:57:07 -0700417 _debug("ICPT %p{%u} [%d]",
418 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
419
David Howells08e0e7c2007-04-26 15:55:03 -0700420 ASSERTCMP(sk, ==, afs_socket->sk);
David Howells00d3b7a2007-04-26 15:57:07 -0700421 atomic_inc(&afs_outstanding_skbs);
David Howells08e0e7c2007-04-26 15:55:03 -0700422
423 if (!call) {
424 /* its an incoming call for our callback service */
David Howells00d3b7a2007-04-26 15:57:07 -0700425 skb_queue_tail(&afs_incoming_calls, skb);
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000426 queue_work(afs_wq, &afs_collect_incoming_call_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700427 } else {
428 /* route the messages directly to the appropriate call */
David Howells00d3b7a2007-04-26 15:57:07 -0700429 skb_queue_tail(&call->rx_queue, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700430 call->wait_mode->rx_wakeup(call);
431 }
432
433 _leave("");
434}
435
436/*
437 * deliver messages to a call
438 */
439static void afs_deliver_to_call(struct afs_call *call)
440{
441 struct sk_buff *skb;
442 bool last;
443 u32 abort_code;
444 int ret;
445
446 _enter("");
447
448 while ((call->state == AFS_CALL_AWAIT_REPLY ||
449 call->state == AFS_CALL_AWAIT_OP_ID ||
450 call->state == AFS_CALL_AWAIT_REQUEST ||
451 call->state == AFS_CALL_AWAIT_ACK) &&
452 (skb = skb_dequeue(&call->rx_queue))) {
453 switch (skb->mark) {
454 case RXRPC_SKB_MARK_DATA:
455 _debug("Rcv DATA");
456 last = rxrpc_kernel_is_data_last(skb);
457 ret = call->type->deliver(call, skb, last);
458 switch (ret) {
459 case 0:
460 if (last &&
461 call->state == AFS_CALL_AWAIT_REPLY)
462 call->state = AFS_CALL_COMPLETE;
463 break;
464 case -ENOTCONN:
465 abort_code = RX_CALL_DEAD;
466 goto do_abort;
467 case -ENOTSUPP:
468 abort_code = RX_INVALID_OPERATION;
469 goto do_abort;
470 default:
471 abort_code = RXGEN_CC_UNMARSHAL;
472 if (call->state != AFS_CALL_AWAIT_REPLY)
473 abort_code = RXGEN_SS_UNMARSHAL;
474 do_abort:
475 rxrpc_kernel_abort_call(call->rxcall,
476 abort_code);
477 call->error = ret;
478 call->state = AFS_CALL_ERROR;
479 break;
480 }
David Howells00d3b7a2007-04-26 15:57:07 -0700481 afs_data_delivered(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700482 skb = NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700483 continue;
David Howells08e0e7c2007-04-26 15:55:03 -0700484 case RXRPC_SKB_MARK_FINAL_ACK:
485 _debug("Rcv ACK");
486 call->state = AFS_CALL_COMPLETE;
487 break;
488 case RXRPC_SKB_MARK_BUSY:
489 _debug("Rcv BUSY");
490 call->error = -EBUSY;
491 call->state = AFS_CALL_BUSY;
492 break;
493 case RXRPC_SKB_MARK_REMOTE_ABORT:
494 abort_code = rxrpc_kernel_get_abort_code(skb);
495 call->error = call->type->abort_to_error(abort_code);
496 call->state = AFS_CALL_ABORTED;
497 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
498 break;
499 case RXRPC_SKB_MARK_NET_ERROR:
500 call->error = -rxrpc_kernel_get_error_number(skb);
501 call->state = AFS_CALL_ERROR;
502 _debug("Rcv NET ERROR %d", call->error);
503 break;
504 case RXRPC_SKB_MARK_LOCAL_ERROR:
505 call->error = -rxrpc_kernel_get_error_number(skb);
506 call->state = AFS_CALL_ERROR;
507 _debug("Rcv LOCAL ERROR %d", call->error);
508 break;
509 default:
510 BUG();
511 break;
512 }
513
David Howells00d3b7a2007-04-26 15:57:07 -0700514 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700515 }
516
517 /* make sure the queue is empty if the call is done with (we might have
518 * aborted the call early because of an unmarshalling error) */
519 if (call->state >= AFS_CALL_COMPLETE) {
520 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700521 afs_free_skb(skb);
David Howells6c67c7c2014-05-21 14:48:05 +0100522 if (call->incoming)
523 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700524 }
525
526 _leave("");
527}
528
529/*
530 * wait synchronously for a call to complete
531 */
532static int afs_wait_for_call_to_complete(struct afs_call *call)
533{
534 struct sk_buff *skb;
535 int ret;
536
537 DECLARE_WAITQUEUE(myself, current);
538
539 _enter("");
540
541 add_wait_queue(&call->waitq, &myself);
542 for (;;) {
543 set_current_state(TASK_INTERRUPTIBLE);
544
545 /* deliver any messages that are in the queue */
546 if (!skb_queue_empty(&call->rx_queue)) {
547 __set_current_state(TASK_RUNNING);
548 afs_deliver_to_call(call);
549 continue;
550 }
551
552 ret = call->error;
553 if (call->state >= AFS_CALL_COMPLETE)
554 break;
555 ret = -EINTR;
556 if (signal_pending(current))
557 break;
558 schedule();
559 }
560
561 remove_wait_queue(&call->waitq, &myself);
562 __set_current_state(TASK_RUNNING);
563
564 /* kill the call */
565 if (call->state < AFS_CALL_COMPLETE) {
566 _debug("call incomplete");
567 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
568 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700569 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700570 }
571
572 _debug("call complete");
David Howells6c67c7c2014-05-21 14:48:05 +0100573 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700574 _leave(" = %d", ret);
575 return ret;
576}
577
578/*
579 * wake up a waiting call
580 */
581static void afs_wake_up_call_waiter(struct afs_call *call)
582{
583 wake_up(&call->waitq);
584}
585
586/*
587 * wake up an asynchronous call
588 */
589static void afs_wake_up_async_call(struct afs_call *call)
590{
591 _enter("");
592 queue_work(afs_async_calls, &call->async_work);
593}
594
595/*
596 * put a call into asynchronous mode
597 * - mustn't touch the call descriptor as the call my have completed by the
598 * time we get here
599 */
600static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
601{
602 _enter("");
603 return -EINPROGRESS;
604}
605
606/*
607 * delete an asynchronous call
608 */
609static void afs_delete_async_call(struct work_struct *work)
610{
611 struct afs_call *call =
612 container_of(work, struct afs_call, async_work);
613
614 _enter("");
615
David Howells00d3b7a2007-04-26 15:57:07 -0700616 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700617
618 _leave("");
619}
620
621/*
622 * perform processing on an asynchronous call
623 * - on a multiple-thread workqueue this work item may try to run on several
624 * CPUs at the same time
625 */
626static void afs_process_async_call(struct work_struct *work)
627{
628 struct afs_call *call =
629 container_of(work, struct afs_call, async_work);
630
631 _enter("");
632
633 if (!skb_queue_empty(&call->rx_queue))
634 afs_deliver_to_call(call);
635
636 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
637 if (call->wait_mode->async_complete)
638 call->wait_mode->async_complete(call->reply,
639 call->error);
640 call->reply = NULL;
641
642 /* kill the call */
643 rxrpc_kernel_end_call(call->rxcall);
David Howells00d3b7a2007-04-26 15:57:07 -0700644 call->rxcall = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700645 if (call->type->destructor)
646 call->type->destructor(call);
647
648 /* we can't just delete the call because the work item may be
649 * queued */
Tejun Heo05949942014-03-07 10:24:50 -0500650 call->async_workfn = afs_delete_async_call;
David Howells08e0e7c2007-04-26 15:55:03 -0700651 queue_work(afs_async_calls, &call->async_work);
652 }
653
654 _leave("");
655}
656
657/*
658 * empty a socket buffer into a flat reply buffer
659 */
660void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
661{
662 size_t len = skb->len;
663
664 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
665 BUG();
666 call->reply_size += len;
667}
668
Tejun Heo05949942014-03-07 10:24:50 -0500669static void afs_async_workfn(struct work_struct *work)
670{
671 struct afs_call *call = container_of(work, struct afs_call, async_work);
672
673 call->async_workfn(work);
674}
675
David Howells08e0e7c2007-04-26 15:55:03 -0700676/*
677 * accept the backlog of incoming calls
678 */
679static void afs_collect_incoming_call(struct work_struct *work)
680{
681 struct rxrpc_call *rxcall;
682 struct afs_call *call = NULL;
683 struct sk_buff *skb;
684
685 while ((skb = skb_dequeue(&afs_incoming_calls))) {
686 _debug("new call");
687
688 /* don't need the notification */
David Howells00d3b7a2007-04-26 15:57:07 -0700689 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700690
691 if (!call) {
692 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
693 if (!call) {
694 rxrpc_kernel_reject_call(afs_socket);
695 return;
696 }
697
Tejun Heo05949942014-03-07 10:24:50 -0500698 call->async_workfn = afs_process_async_call;
699 INIT_WORK(&call->async_work, afs_async_workfn);
David Howells08e0e7c2007-04-26 15:55:03 -0700700 call->wait_mode = &afs_async_incoming_call;
701 call->type = &afs_RXCMxxxx;
702 init_waitqueue_head(&call->waitq);
703 skb_queue_head_init(&call->rx_queue);
704 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700705
706 _debug("CALL %p{%s} [%d]",
707 call, call->type->name,
708 atomic_read(&afs_outstanding_calls));
709 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700710 }
711
712 rxcall = rxrpc_kernel_accept_call(afs_socket,
713 (unsigned long) call);
714 if (!IS_ERR(rxcall)) {
715 call->rxcall = rxcall;
716 call = NULL;
717 }
718 }
719
David Howells00d3b7a2007-04-26 15:57:07 -0700720 if (call)
721 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700722}
723
724/*
725 * grab the operation ID from an incoming cache manager call
726 */
727static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
728 bool last)
729{
730 size_t len = skb->len;
731 void *oibuf = (void *) &call->operation_ID;
732
733 _enter("{%u},{%zu},%d", call->offset, len, last);
734
735 ASSERTCMP(call->offset, <, 4);
736
737 /* the operation ID forms the first four bytes of the request data */
738 len = min_t(size_t, len, 4 - call->offset);
739 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
740 BUG();
741 if (!pskb_pull(skb, len))
742 BUG();
743 call->offset += len;
744
745 if (call->offset < 4) {
746 if (last) {
747 _leave(" = -EBADMSG [op ID short]");
748 return -EBADMSG;
749 }
750 _leave(" = 0 [incomplete]");
751 return 0;
752 }
753
754 call->state = AFS_CALL_AWAIT_REQUEST;
755
756 /* ask the cache manager to route the call (it'll change the call type
757 * if successful) */
758 if (!afs_cm_incoming_call(call))
759 return -ENOTSUPP;
760
761 /* pass responsibility for the remainer of this message off to the
762 * cache manager op */
763 return call->type->deliver(call, skb, last);
764}
765
766/*
767 * send an empty reply
768 */
769void afs_send_empty_reply(struct afs_call *call)
770{
771 struct msghdr msg;
772 struct iovec iov[1];
773
774 _enter("");
775
776 iov[0].iov_base = NULL;
777 iov[0].iov_len = 0;
778 msg.msg_name = NULL;
779 msg.msg_namelen = 0;
780 msg.msg_iov = iov;
781 msg.msg_iovlen = 0;
782 msg.msg_control = NULL;
783 msg.msg_controllen = 0;
784 msg.msg_flags = 0;
785
786 call->state = AFS_CALL_AWAIT_ACK;
787 switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
788 case 0:
789 _leave(" [replied]");
790 return;
791
792 case -ENOMEM:
793 _debug("oom");
794 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
795 default:
David Howells6c67c7c2014-05-21 14:48:05 +0100796 afs_end_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700797 _leave(" [error]");
798 return;
799 }
800}
801
802/*
David Howellsb908fe62007-04-26 15:58:17 -0700803 * send a simple reply
804 */
805void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
806{
807 struct msghdr msg;
808 struct iovec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100809 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700810
811 _enter("");
812
813 iov[0].iov_base = (void *) buf;
814 iov[0].iov_len = len;
815 msg.msg_name = NULL;
816 msg.msg_namelen = 0;
817 msg.msg_iov = iov;
818 msg.msg_iovlen = 1;
819 msg.msg_control = NULL;
820 msg.msg_controllen = 0;
821 msg.msg_flags = 0;
822
823 call->state = AFS_CALL_AWAIT_ACK;
David Howellsbd6dc742007-07-20 10:59:41 +0100824 n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
825 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100826 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700827 _leave(" [replied]");
828 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100829 }
David Howells6c67c7c2014-05-21 14:48:05 +0100830
David Howellsbd6dc742007-07-20 10:59:41 +0100831 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700832 _debug("oom");
833 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
David Howellsb908fe62007-04-26 15:58:17 -0700834 }
David Howells6c67c7c2014-05-21 14:48:05 +0100835 afs_end_call(call);
David Howellsbd6dc742007-07-20 10:59:41 +0100836 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700837}
838
839/*
David Howells08e0e7c2007-04-26 15:55:03 -0700840 * extract a piece of data from the received data socket buffers
841 */
842int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
843 bool last, void *buf, size_t count)
844{
845 size_t len = skb->len;
846
847 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
848
849 ASSERTCMP(call->offset, <, count);
850
851 len = min_t(size_t, len, count - call->offset);
852 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
853 !pskb_pull(skb, len))
854 BUG();
855 call->offset += len;
856
857 if (call->offset < count) {
858 if (last) {
David Howellsb1bdb692007-04-27 15:28:45 -0700859 _leave(" = -EBADMSG [%d < %zu]", call->offset, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700860 return -EBADMSG;
861 }
862 _leave(" = -EAGAIN");
863 return -EAGAIN;
864 }
865 return 0;
866}