blob: 67cf810e0fd6e6cbadde116cf3916555d96226bb [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);
103 _leave(" = %d [bind]", ret);
104 return ret;
105 }
106
107 rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
108
109 afs_socket = socket;
110 _leave(" = 0");
111 return 0;
112}
113
114/*
115 * close the RxRPC socket AFS was using
116 */
117void afs_close_socket(void)
118{
119 _enter("");
120
121 sock_release(afs_socket);
122
123 _debug("dework");
124 destroy_workqueue(afs_async_calls);
David Howells00d3b7a2007-04-26 15:57:07 -0700125
126 ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
127 ASSERTCMP(atomic_read(&afs_outstanding_calls), ==, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700128 _leave("");
129}
130
131/*
David Howells00d3b7a2007-04-26 15:57:07 -0700132 * note that the data in a socket buffer is now delivered and that the buffer
133 * should be freed
134 */
135static void afs_data_delivered(struct sk_buff *skb)
136{
137 if (!skb) {
138 _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs));
139 dump_stack();
140 } else {
141 _debug("DLVR %p{%u} [%d]",
142 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
143 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
144 BUG();
145 rxrpc_kernel_data_delivered(skb);
146 }
147}
148
149/*
150 * free a socket buffer
151 */
152static void afs_free_skb(struct sk_buff *skb)
153{
154 if (!skb) {
155 _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs));
156 dump_stack();
157 } else {
158 _debug("FREE %p{%u} [%d]",
159 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
160 if (atomic_dec_return(&afs_outstanding_skbs) == -1)
161 BUG();
162 rxrpc_kernel_free_skb(skb);
163 }
164}
165
166/*
167 * free a call
168 */
169static void afs_free_call(struct afs_call *call)
170{
171 _debug("DONE %p{%s} [%d]",
172 call, call->type->name, atomic_read(&afs_outstanding_calls));
173 if (atomic_dec_return(&afs_outstanding_calls) == -1)
174 BUG();
175
176 ASSERTCMP(call->rxcall, ==, NULL);
177 ASSERT(!work_pending(&call->async_work));
178 ASSERT(skb_queue_empty(&call->rx_queue));
179 ASSERT(call->type->name != NULL);
180
181 kfree(call->request);
182 kfree(call);
183}
184
185/*
David Howells08e0e7c2007-04-26 15:55:03 -0700186 * allocate a call with flat request and reply buffers
187 */
188struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
189 size_t request_size, size_t reply_size)
190{
191 struct afs_call *call;
192
193 call = kzalloc(sizeof(*call), GFP_NOFS);
194 if (!call)
195 goto nomem_call;
196
David Howells00d3b7a2007-04-26 15:57:07 -0700197 _debug("CALL %p{%s} [%d]",
198 call, type->name, atomic_read(&afs_outstanding_calls));
199 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700200
201 call->type = type;
202 call->request_size = request_size;
203 call->reply_max = reply_size;
204
David Howells00d3b7a2007-04-26 15:57:07 -0700205 if (request_size) {
206 call->request = kmalloc(request_size, GFP_NOFS);
207 if (!call->request)
208 goto nomem_free;
209 }
210
211 if (reply_size) {
212 call->buffer = kmalloc(reply_size, GFP_NOFS);
213 if (!call->buffer)
214 goto nomem_free;
215 }
216
David Howells08e0e7c2007-04-26 15:55:03 -0700217 init_waitqueue_head(&call->waitq);
218 skb_queue_head_init(&call->rx_queue);
219 return call;
220
David Howells00d3b7a2007-04-26 15:57:07 -0700221nomem_free:
222 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700223nomem_call:
224 return NULL;
225}
226
227/*
228 * clean up a call with flat buffer
229 */
230void afs_flat_call_destructor(struct afs_call *call)
231{
232 _enter("");
233
234 kfree(call->request);
235 call->request = NULL;
236 kfree(call->buffer);
237 call->buffer = NULL;
238}
239
240/*
David Howells31143d52007-05-09 02:33:46 -0700241 * attach the data from a bunch of pages on an inode to a call
242 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700243static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
244 struct kvec *iov)
David Howells31143d52007-05-09 02:33:46 -0700245{
246 struct page *pages[8];
247 unsigned count, n, loop, offset, to;
248 pgoff_t first = call->first, last = call->last;
249 int ret;
250
251 _enter("");
252
253 offset = call->first_offset;
254 call->first_offset = 0;
255
256 do {
257 _debug("attach %lx-%lx", first, last);
258
259 count = last - first + 1;
260 if (count > ARRAY_SIZE(pages))
261 count = ARRAY_SIZE(pages);
262 n = find_get_pages_contig(call->mapping, first, count, pages);
263 ASSERTCMP(n, ==, count);
264
265 loop = 0;
266 do {
267 msg->msg_flags = 0;
268 to = PAGE_SIZE;
269 if (first + loop >= last)
270 to = call->last_to;
271 else
272 msg->msg_flags = MSG_MORE;
273 iov->iov_base = kmap(pages[loop]) + offset;
274 iov->iov_len = to - offset;
275 offset = 0;
276
277 _debug("- range %u-%u%s",
278 offset, to, msg->msg_flags ? " [more]" : "");
279 msg->msg_iov = (struct iovec *) iov;
280 msg->msg_iovlen = 1;
281
282 /* have to change the state *before* sending the last
283 * packet as RxRPC might give us the reply before it
284 * returns from sending the request */
285 if (first + loop >= last)
286 call->state = AFS_CALL_AWAIT_REPLY;
287 ret = rxrpc_kernel_send_data(call->rxcall, msg,
288 to - offset);
289 kunmap(pages[loop]);
290 if (ret < 0)
291 break;
292 } while (++loop < count);
293 first += count;
294
295 for (loop = 0; loop < count; loop++)
296 put_page(pages[loop]);
297 if (ret < 0)
298 break;
David Howells5bbf5d32007-05-10 03:15:23 -0700299 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700300
301 _leave(" = %d", ret);
302 return ret;
303}
304
305/*
David Howells08e0e7c2007-04-26 15:55:03 -0700306 * initiate a call
307 */
308int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
309 const struct afs_wait_mode *wait_mode)
310{
311 struct sockaddr_rxrpc srx;
312 struct rxrpc_call *rxcall;
313 struct msghdr msg;
314 struct kvec iov[1];
315 int ret;
316
317 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
318
David Howells00d3b7a2007-04-26 15:57:07 -0700319 ASSERT(call->type != NULL);
320 ASSERT(call->type->name != NULL);
321
David Howells31143d52007-05-09 02:33:46 -0700322 _debug("____MAKE %p{%s,%x} [%d]____",
323 call, call->type->name, key_serial(call->key),
324 atomic_read(&afs_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700325
David Howells08e0e7c2007-04-26 15:55:03 -0700326 call->wait_mode = wait_mode;
327 INIT_WORK(&call->async_work, afs_process_async_call);
328
329 memset(&srx, 0, sizeof(srx));
330 srx.srx_family = AF_RXRPC;
331 srx.srx_service = call->service_id;
332 srx.transport_type = SOCK_DGRAM;
333 srx.transport_len = sizeof(srx.transport.sin);
334 srx.transport.sin.sin_family = AF_INET;
335 srx.transport.sin.sin_port = call->port;
336 memcpy(&srx.transport.sin.sin_addr, addr, 4);
337
338 /* create a call */
339 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
340 (unsigned long) call, gfp);
David Howells00d3b7a2007-04-26 15:57:07 -0700341 call->key = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700342 if (IS_ERR(rxcall)) {
343 ret = PTR_ERR(rxcall);
344 goto error_kill_call;
345 }
346
347 call->rxcall = rxcall;
348
349 /* send the request */
350 iov[0].iov_base = call->request;
351 iov[0].iov_len = call->request_size;
352
353 msg.msg_name = NULL;
354 msg.msg_namelen = 0;
355 msg.msg_iov = (struct iovec *) iov;
356 msg.msg_iovlen = 1;
357 msg.msg_control = NULL;
358 msg.msg_controllen = 0;
David Howells31143d52007-05-09 02:33:46 -0700359 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700360
361 /* have to change the state *before* sending the last packet as RxRPC
362 * might give us the reply before it returns from sending the
363 * request */
David Howells31143d52007-05-09 02:33:46 -0700364 if (!call->send_pages)
365 call->state = AFS_CALL_AWAIT_REPLY;
David Howells08e0e7c2007-04-26 15:55:03 -0700366 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
367 if (ret < 0)
368 goto error_do_abort;
369
David Howells31143d52007-05-09 02:33:46 -0700370 if (call->send_pages) {
371 ret = afs_send_pages(call, &msg, iov);
372 if (ret < 0)
373 goto error_do_abort;
374 }
375
David Howells08e0e7c2007-04-26 15:55:03 -0700376 /* at this point, an async call may no longer exist as it may have
377 * already completed */
378 return wait_mode->wait(call);
379
380error_do_abort:
381 rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
382 rxrpc_kernel_end_call(rxcall);
David Howells00d3b7a2007-04-26 15:57:07 -0700383 call->rxcall = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700384error_kill_call:
385 call->type->destructor(call);
David Howells00d3b7a2007-04-26 15:57:07 -0700386 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700387 _leave(" = %d", ret);
388 return ret;
389}
390
391/*
392 * handles intercepted messages that were arriving in the socket's Rx queue
393 * - called with the socket receive queue lock held to ensure message ordering
394 * - called with softirqs disabled
395 */
396static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
397 struct sk_buff *skb)
398{
399 struct afs_call *call = (struct afs_call *) user_call_ID;
400
401 _enter("%p,,%u", call, skb->mark);
402
David Howells00d3b7a2007-04-26 15:57:07 -0700403 _debug("ICPT %p{%u} [%d]",
404 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
405
David Howells08e0e7c2007-04-26 15:55:03 -0700406 ASSERTCMP(sk, ==, afs_socket->sk);
David Howells00d3b7a2007-04-26 15:57:07 -0700407 atomic_inc(&afs_outstanding_skbs);
David Howells08e0e7c2007-04-26 15:55:03 -0700408
409 if (!call) {
410 /* its an incoming call for our callback service */
David Howells00d3b7a2007-04-26 15:57:07 -0700411 skb_queue_tail(&afs_incoming_calls, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700412 schedule_work(&afs_collect_incoming_call_work);
413 } else {
414 /* route the messages directly to the appropriate call */
David Howells00d3b7a2007-04-26 15:57:07 -0700415 skb_queue_tail(&call->rx_queue, skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700416 call->wait_mode->rx_wakeup(call);
417 }
418
419 _leave("");
420}
421
422/*
423 * deliver messages to a call
424 */
425static void afs_deliver_to_call(struct afs_call *call)
426{
427 struct sk_buff *skb;
428 bool last;
429 u32 abort_code;
430 int ret;
431
432 _enter("");
433
434 while ((call->state == AFS_CALL_AWAIT_REPLY ||
435 call->state == AFS_CALL_AWAIT_OP_ID ||
436 call->state == AFS_CALL_AWAIT_REQUEST ||
437 call->state == AFS_CALL_AWAIT_ACK) &&
438 (skb = skb_dequeue(&call->rx_queue))) {
439 switch (skb->mark) {
440 case RXRPC_SKB_MARK_DATA:
441 _debug("Rcv DATA");
442 last = rxrpc_kernel_is_data_last(skb);
443 ret = call->type->deliver(call, skb, last);
444 switch (ret) {
445 case 0:
446 if (last &&
447 call->state == AFS_CALL_AWAIT_REPLY)
448 call->state = AFS_CALL_COMPLETE;
449 break;
450 case -ENOTCONN:
451 abort_code = RX_CALL_DEAD;
452 goto do_abort;
453 case -ENOTSUPP:
454 abort_code = RX_INVALID_OPERATION;
455 goto do_abort;
456 default:
457 abort_code = RXGEN_CC_UNMARSHAL;
458 if (call->state != AFS_CALL_AWAIT_REPLY)
459 abort_code = RXGEN_SS_UNMARSHAL;
460 do_abort:
461 rxrpc_kernel_abort_call(call->rxcall,
462 abort_code);
463 call->error = ret;
464 call->state = AFS_CALL_ERROR;
465 break;
466 }
David Howells00d3b7a2007-04-26 15:57:07 -0700467 afs_data_delivered(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700468 skb = NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700469 continue;
David Howells08e0e7c2007-04-26 15:55:03 -0700470 case RXRPC_SKB_MARK_FINAL_ACK:
471 _debug("Rcv ACK");
472 call->state = AFS_CALL_COMPLETE;
473 break;
474 case RXRPC_SKB_MARK_BUSY:
475 _debug("Rcv BUSY");
476 call->error = -EBUSY;
477 call->state = AFS_CALL_BUSY;
478 break;
479 case RXRPC_SKB_MARK_REMOTE_ABORT:
480 abort_code = rxrpc_kernel_get_abort_code(skb);
481 call->error = call->type->abort_to_error(abort_code);
482 call->state = AFS_CALL_ABORTED;
483 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
484 break;
485 case RXRPC_SKB_MARK_NET_ERROR:
486 call->error = -rxrpc_kernel_get_error_number(skb);
487 call->state = AFS_CALL_ERROR;
488 _debug("Rcv NET ERROR %d", call->error);
489 break;
490 case RXRPC_SKB_MARK_LOCAL_ERROR:
491 call->error = -rxrpc_kernel_get_error_number(skb);
492 call->state = AFS_CALL_ERROR;
493 _debug("Rcv LOCAL ERROR %d", call->error);
494 break;
495 default:
496 BUG();
497 break;
498 }
499
David Howells00d3b7a2007-04-26 15:57:07 -0700500 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700501 }
502
503 /* make sure the queue is empty if the call is done with (we might have
504 * aborted the call early because of an unmarshalling error) */
505 if (call->state >= AFS_CALL_COMPLETE) {
506 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700507 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700508 if (call->incoming) {
509 rxrpc_kernel_end_call(call->rxcall);
David Howells00d3b7a2007-04-26 15:57:07 -0700510 call->rxcall = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700511 call->type->destructor(call);
David Howells00d3b7a2007-04-26 15:57:07 -0700512 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700513 }
514 }
515
516 _leave("");
517}
518
519/*
520 * wait synchronously for a call to complete
521 */
522static int afs_wait_for_call_to_complete(struct afs_call *call)
523{
524 struct sk_buff *skb;
525 int ret;
526
527 DECLARE_WAITQUEUE(myself, current);
528
529 _enter("");
530
531 add_wait_queue(&call->waitq, &myself);
532 for (;;) {
533 set_current_state(TASK_INTERRUPTIBLE);
534
535 /* deliver any messages that are in the queue */
536 if (!skb_queue_empty(&call->rx_queue)) {
537 __set_current_state(TASK_RUNNING);
538 afs_deliver_to_call(call);
539 continue;
540 }
541
542 ret = call->error;
543 if (call->state >= AFS_CALL_COMPLETE)
544 break;
545 ret = -EINTR;
546 if (signal_pending(current))
547 break;
548 schedule();
549 }
550
551 remove_wait_queue(&call->waitq, &myself);
552 __set_current_state(TASK_RUNNING);
553
554 /* kill the call */
555 if (call->state < AFS_CALL_COMPLETE) {
556 _debug("call incomplete");
557 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
558 while ((skb = skb_dequeue(&call->rx_queue)))
David Howells00d3b7a2007-04-26 15:57:07 -0700559 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700560 }
561
562 _debug("call complete");
563 rxrpc_kernel_end_call(call->rxcall);
David Howells00d3b7a2007-04-26 15:57:07 -0700564 call->rxcall = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700565 call->type->destructor(call);
David Howells00d3b7a2007-04-26 15:57:07 -0700566 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700567 _leave(" = %d", ret);
568 return ret;
569}
570
571/*
572 * wake up a waiting call
573 */
574static void afs_wake_up_call_waiter(struct afs_call *call)
575{
576 wake_up(&call->waitq);
577}
578
579/*
580 * wake up an asynchronous call
581 */
582static void afs_wake_up_async_call(struct afs_call *call)
583{
584 _enter("");
585 queue_work(afs_async_calls, &call->async_work);
586}
587
588/*
589 * put a call into asynchronous mode
590 * - mustn't touch the call descriptor as the call my have completed by the
591 * time we get here
592 */
593static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
594{
595 _enter("");
596 return -EINPROGRESS;
597}
598
599/*
600 * delete an asynchronous call
601 */
602static void afs_delete_async_call(struct work_struct *work)
603{
604 struct afs_call *call =
605 container_of(work, struct afs_call, async_work);
606
607 _enter("");
608
David Howells00d3b7a2007-04-26 15:57:07 -0700609 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700610
611 _leave("");
612}
613
614/*
615 * perform processing on an asynchronous call
616 * - on a multiple-thread workqueue this work item may try to run on several
617 * CPUs at the same time
618 */
619static void afs_process_async_call(struct work_struct *work)
620{
621 struct afs_call *call =
622 container_of(work, struct afs_call, async_work);
623
624 _enter("");
625
626 if (!skb_queue_empty(&call->rx_queue))
627 afs_deliver_to_call(call);
628
629 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
630 if (call->wait_mode->async_complete)
631 call->wait_mode->async_complete(call->reply,
632 call->error);
633 call->reply = NULL;
634
635 /* kill the call */
636 rxrpc_kernel_end_call(call->rxcall);
David Howells00d3b7a2007-04-26 15:57:07 -0700637 call->rxcall = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700638 if (call->type->destructor)
639 call->type->destructor(call);
640
641 /* we can't just delete the call because the work item may be
642 * queued */
643 PREPARE_WORK(&call->async_work, afs_delete_async_call);
644 queue_work(afs_async_calls, &call->async_work);
645 }
646
647 _leave("");
648}
649
650/*
651 * empty a socket buffer into a flat reply buffer
652 */
653void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
654{
655 size_t len = skb->len;
656
657 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
658 BUG();
659 call->reply_size += len;
660}
661
662/*
663 * accept the backlog of incoming calls
664 */
665static void afs_collect_incoming_call(struct work_struct *work)
666{
667 struct rxrpc_call *rxcall;
668 struct afs_call *call = NULL;
669 struct sk_buff *skb;
670
671 while ((skb = skb_dequeue(&afs_incoming_calls))) {
672 _debug("new call");
673
674 /* don't need the notification */
David Howells00d3b7a2007-04-26 15:57:07 -0700675 afs_free_skb(skb);
David Howells08e0e7c2007-04-26 15:55:03 -0700676
677 if (!call) {
678 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
679 if (!call) {
680 rxrpc_kernel_reject_call(afs_socket);
681 return;
682 }
683
684 INIT_WORK(&call->async_work, afs_process_async_call);
685 call->wait_mode = &afs_async_incoming_call;
686 call->type = &afs_RXCMxxxx;
687 init_waitqueue_head(&call->waitq);
688 skb_queue_head_init(&call->rx_queue);
689 call->state = AFS_CALL_AWAIT_OP_ID;
David Howells00d3b7a2007-04-26 15:57:07 -0700690
691 _debug("CALL %p{%s} [%d]",
692 call, call->type->name,
693 atomic_read(&afs_outstanding_calls));
694 atomic_inc(&afs_outstanding_calls);
David Howells08e0e7c2007-04-26 15:55:03 -0700695 }
696
697 rxcall = rxrpc_kernel_accept_call(afs_socket,
698 (unsigned long) call);
699 if (!IS_ERR(rxcall)) {
700 call->rxcall = rxcall;
701 call = NULL;
702 }
703 }
704
David Howells00d3b7a2007-04-26 15:57:07 -0700705 if (call)
706 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700707}
708
709/*
710 * grab the operation ID from an incoming cache manager call
711 */
712static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
713 bool last)
714{
715 size_t len = skb->len;
716 void *oibuf = (void *) &call->operation_ID;
717
718 _enter("{%u},{%zu},%d", call->offset, len, last);
719
720 ASSERTCMP(call->offset, <, 4);
721
722 /* the operation ID forms the first four bytes of the request data */
723 len = min_t(size_t, len, 4 - call->offset);
724 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
725 BUG();
726 if (!pskb_pull(skb, len))
727 BUG();
728 call->offset += len;
729
730 if (call->offset < 4) {
731 if (last) {
732 _leave(" = -EBADMSG [op ID short]");
733 return -EBADMSG;
734 }
735 _leave(" = 0 [incomplete]");
736 return 0;
737 }
738
739 call->state = AFS_CALL_AWAIT_REQUEST;
740
741 /* ask the cache manager to route the call (it'll change the call type
742 * if successful) */
743 if (!afs_cm_incoming_call(call))
744 return -ENOTSUPP;
745
746 /* pass responsibility for the remainer of this message off to the
747 * cache manager op */
748 return call->type->deliver(call, skb, last);
749}
750
751/*
752 * send an empty reply
753 */
754void afs_send_empty_reply(struct afs_call *call)
755{
756 struct msghdr msg;
757 struct iovec iov[1];
758
759 _enter("");
760
761 iov[0].iov_base = NULL;
762 iov[0].iov_len = 0;
763 msg.msg_name = NULL;
764 msg.msg_namelen = 0;
765 msg.msg_iov = iov;
766 msg.msg_iovlen = 0;
767 msg.msg_control = NULL;
768 msg.msg_controllen = 0;
769 msg.msg_flags = 0;
770
771 call->state = AFS_CALL_AWAIT_ACK;
772 switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
773 case 0:
774 _leave(" [replied]");
775 return;
776
777 case -ENOMEM:
778 _debug("oom");
779 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
780 default:
781 rxrpc_kernel_end_call(call->rxcall);
782 call->rxcall = NULL;
783 call->type->destructor(call);
David Howells00d3b7a2007-04-26 15:57:07 -0700784 afs_free_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700785 _leave(" [error]");
786 return;
787 }
788}
789
790/*
David Howellsb908fe62007-04-26 15:58:17 -0700791 * send a simple reply
792 */
793void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
794{
795 struct msghdr msg;
796 struct iovec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100797 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700798
799 _enter("");
800
801 iov[0].iov_base = (void *) buf;
802 iov[0].iov_len = len;
803 msg.msg_name = NULL;
804 msg.msg_namelen = 0;
805 msg.msg_iov = iov;
806 msg.msg_iovlen = 1;
807 msg.msg_control = NULL;
808 msg.msg_controllen = 0;
809 msg.msg_flags = 0;
810
811 call->state = AFS_CALL_AWAIT_ACK;
David Howellsbd6dc742007-07-20 10:59:41 +0100812 n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
813 if (n >= 0) {
David Howellsb908fe62007-04-26 15:58:17 -0700814 _leave(" [replied]");
815 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100816 }
817 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700818 _debug("oom");
819 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
David Howellsb908fe62007-04-26 15:58:17 -0700820 }
David Howellsbd6dc742007-07-20 10:59:41 +0100821 rxrpc_kernel_end_call(call->rxcall);
822 call->rxcall = NULL;
823 call->type->destructor(call);
824 afs_free_call(call);
825 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700826}
827
828/*
David Howells08e0e7c2007-04-26 15:55:03 -0700829 * extract a piece of data from the received data socket buffers
830 */
831int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
832 bool last, void *buf, size_t count)
833{
834 size_t len = skb->len;
835
836 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
837
838 ASSERTCMP(call->offset, <, count);
839
840 len = min_t(size_t, len, count - call->offset);
841 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
842 !pskb_pull(skb, len))
843 BUG();
844 call->offset += len;
845
846 if (call->offset < count) {
847 if (last) {
David Howellsb1bdb692007-04-27 15:28:45 -0700848 _leave(" = -EBADMSG [%d < %zu]", call->offset, count);
David Howells08e0e7c2007-04-26 15:55:03 -0700849 return -EBADMSG;
850 }
851 _leave(" = -EAGAIN");
852 return -EAGAIN;
853 }
854 return 0;
855}