AFS: Part of afs_end_call() is identical to code elsewhere, so split it
[deliverable/linux.git] / fs / afs / rxrpc.c
CommitLineData
08e0e7c8
DH
1/* 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
5a0e3ad6 12#include <linux/slab.h>
08e0e7c8
DH
13#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;
00d3b7a4
DH
21static atomic_t afs_outstanding_calls;
22static atomic_t afs_outstanding_skbs;
08e0e7c8
DH
23
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 = {
00d3b7a4 51 .name = "CB.xxxx",
08e0e7c8
DH
52 .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);
bebf8cfa 103 destroy_workqueue(afs_async_calls);
08e0e7c8
DH
104 _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);
00d3b7a4
DH
126
127 ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
128 ASSERTCMP(atomic_read(&afs_outstanding_calls), ==, 0);
08e0e7c8
DH
129 _leave("");
130}
131
00d3b7a4
DH
132/*
133 * 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
6c67c7c3 186/*
6cf12869 187 * End a call but do not free it
6c67c7c3 188 */
6cf12869 189static void afs_end_call_nofree(struct afs_call *call)
6c67c7c3
DH
190{
191 if (call->rxcall) {
192 rxrpc_kernel_end_call(call->rxcall);
193 call->rxcall = NULL;
194 }
6cf12869
NWF
195 if (call->type->destructor)
196 call->type->destructor(call);
197}
198
199/*
200 * End a call and free it
201 */
202static void afs_end_call(struct afs_call *call)
203{
204 afs_end_call_nofree(call);
6c67c7c3
DH
205 afs_free_call(call);
206}
207
08e0e7c8
DH
208/*
209 * allocate a call with flat request and reply buffers
210 */
211struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
212 size_t request_size, size_t reply_size)
213{
214 struct afs_call *call;
215
216 call = kzalloc(sizeof(*call), GFP_NOFS);
217 if (!call)
218 goto nomem_call;
219
00d3b7a4
DH
220 _debug("CALL %p{%s} [%d]",
221 call, type->name, atomic_read(&afs_outstanding_calls));
222 atomic_inc(&afs_outstanding_calls);
223
224 call->type = type;
225 call->request_size = request_size;
226 call->reply_max = reply_size;
227
08e0e7c8
DH
228 if (request_size) {
229 call->request = kmalloc(request_size, GFP_NOFS);
230 if (!call->request)
00d3b7a4 231 goto nomem_free;
08e0e7c8
DH
232 }
233
234 if (reply_size) {
235 call->buffer = kmalloc(reply_size, GFP_NOFS);
236 if (!call->buffer)
00d3b7a4 237 goto nomem_free;
08e0e7c8
DH
238 }
239
08e0e7c8
DH
240 init_waitqueue_head(&call->waitq);
241 skb_queue_head_init(&call->rx_queue);
242 return call;
243
00d3b7a4
DH
244nomem_free:
245 afs_free_call(call);
08e0e7c8
DH
246nomem_call:
247 return NULL;
248}
249
250/*
251 * clean up a call with flat buffer
252 */
253void afs_flat_call_destructor(struct afs_call *call)
254{
255 _enter("");
256
257 kfree(call->request);
258 call->request = NULL;
259 kfree(call->buffer);
260 call->buffer = NULL;
261}
262
31143d5d
DH
263/*
264 * attach the data from a bunch of pages on an inode to a call
265 */
c1206a2c
AB
266static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
267 struct kvec *iov)
31143d5d
DH
268{
269 struct page *pages[8];
270 unsigned count, n, loop, offset, to;
271 pgoff_t first = call->first, last = call->last;
272 int ret;
273
274 _enter("");
275
276 offset = call->first_offset;
277 call->first_offset = 0;
278
279 do {
280 _debug("attach %lx-%lx", first, last);
281
282 count = last - first + 1;
283 if (count > ARRAY_SIZE(pages))
284 count = ARRAY_SIZE(pages);
285 n = find_get_pages_contig(call->mapping, first, count, pages);
286 ASSERTCMP(n, ==, count);
287
288 loop = 0;
289 do {
290 msg->msg_flags = 0;
291 to = PAGE_SIZE;
292 if (first + loop >= last)
293 to = call->last_to;
294 else
295 msg->msg_flags = MSG_MORE;
296 iov->iov_base = kmap(pages[loop]) + offset;
297 iov->iov_len = to - offset;
298 offset = 0;
299
300 _debug("- range %u-%u%s",
301 offset, to, msg->msg_flags ? " [more]" : "");
302 msg->msg_iov = (struct iovec *) iov;
303 msg->msg_iovlen = 1;
304
305 /* have to change the state *before* sending the last
306 * packet as RxRPC might give us the reply before it
307 * returns from sending the request */
308 if (first + loop >= last)
309 call->state = AFS_CALL_AWAIT_REPLY;
310 ret = rxrpc_kernel_send_data(call->rxcall, msg,
311 to - offset);
312 kunmap(pages[loop]);
313 if (ret < 0)
314 break;
315 } while (++loop < count);
316 first += count;
317
318 for (loop = 0; loop < count; loop++)
319 put_page(pages[loop]);
320 if (ret < 0)
321 break;
5bbf5d39 322 } while (first <= last);
31143d5d
DH
323
324 _leave(" = %d", ret);
325 return ret;
326}
327
08e0e7c8
DH
328/*
329 * initiate a call
330 */
331int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
332 const struct afs_wait_mode *wait_mode)
333{
334 struct sockaddr_rxrpc srx;
335 struct rxrpc_call *rxcall;
336 struct msghdr msg;
337 struct kvec iov[1];
338 int ret;
c0173863 339 struct sk_buff *skb;
08e0e7c8
DH
340
341 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
342
00d3b7a4
DH
343 ASSERT(call->type != NULL);
344 ASSERT(call->type->name != NULL);
345
31143d5d
DH
346 _debug("____MAKE %p{%s,%x} [%d]____",
347 call, call->type->name, key_serial(call->key),
348 atomic_read(&afs_outstanding_calls));
00d3b7a4 349
08e0e7c8
DH
350 call->wait_mode = wait_mode;
351 INIT_WORK(&call->async_work, afs_process_async_call);
352
353 memset(&srx, 0, sizeof(srx));
354 srx.srx_family = AF_RXRPC;
355 srx.srx_service = call->service_id;
356 srx.transport_type = SOCK_DGRAM;
357 srx.transport_len = sizeof(srx.transport.sin);
358 srx.transport.sin.sin_family = AF_INET;
359 srx.transport.sin.sin_port = call->port;
360 memcpy(&srx.transport.sin.sin_addr, addr, 4);
361
362 /* create a call */
363 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
364 (unsigned long) call, gfp);
00d3b7a4 365 call->key = NULL;
08e0e7c8
DH
366 if (IS_ERR(rxcall)) {
367 ret = PTR_ERR(rxcall);
368 goto error_kill_call;
369 }
370
371 call->rxcall = rxcall;
372
373 /* send the request */
374 iov[0].iov_base = call->request;
375 iov[0].iov_len = call->request_size;
376
377 msg.msg_name = NULL;
378 msg.msg_namelen = 0;
379 msg.msg_iov = (struct iovec *) iov;
380 msg.msg_iovlen = 1;
381 msg.msg_control = NULL;
382 msg.msg_controllen = 0;
31143d5d 383 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
08e0e7c8
DH
384
385 /* have to change the state *before* sending the last packet as RxRPC
386 * might give us the reply before it returns from sending the
387 * request */
31143d5d
DH
388 if (!call->send_pages)
389 call->state = AFS_CALL_AWAIT_REPLY;
08e0e7c8
DH
390 ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
391 if (ret < 0)
392 goto error_do_abort;
393
31143d5d
DH
394 if (call->send_pages) {
395 ret = afs_send_pages(call, &msg, iov);
396 if (ret < 0)
397 goto error_do_abort;
398 }
399
08e0e7c8
DH
400 /* at this point, an async call may no longer exist as it may have
401 * already completed */
402 return wait_mode->wait(call);
403
404error_do_abort:
405 rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
c0173863
AB
406 while ((skb = skb_dequeue(&call->rx_queue)))
407 afs_free_skb(skb);
08e0e7c8 408error_kill_call:
6c67c7c3 409 afs_end_call(call);
08e0e7c8
DH
410 _leave(" = %d", ret);
411 return ret;
412}
413
414/*
415 * handles intercepted messages that were arriving in the socket's Rx queue
416 * - called with the socket receive queue lock held to ensure message ordering
417 * - called with softirqs disabled
418 */
419static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
420 struct sk_buff *skb)
421{
422 struct afs_call *call = (struct afs_call *) user_call_ID;
423
424 _enter("%p,,%u", call, skb->mark);
425
00d3b7a4
DH
426 _debug("ICPT %p{%u} [%d]",
427 skb, skb->mark, atomic_read(&afs_outstanding_skbs));
428
08e0e7c8 429 ASSERTCMP(sk, ==, afs_socket->sk);
00d3b7a4 430 atomic_inc(&afs_outstanding_skbs);
08e0e7c8
DH
431
432 if (!call) {
433 /* its an incoming call for our callback service */
00d3b7a4 434 skb_queue_tail(&afs_incoming_calls, skb);
0ad53eee 435 queue_work(afs_wq, &afs_collect_incoming_call_work);
08e0e7c8
DH
436 } else {
437 /* route the messages directly to the appropriate call */
00d3b7a4 438 skb_queue_tail(&call->rx_queue, skb);
08e0e7c8
DH
439 call->wait_mode->rx_wakeup(call);
440 }
441
442 _leave("");
443}
444
445/*
446 * deliver messages to a call
447 */
448static void afs_deliver_to_call(struct afs_call *call)
449{
450 struct sk_buff *skb;
451 bool last;
452 u32 abort_code;
453 int ret;
454
455 _enter("");
456
457 while ((call->state == AFS_CALL_AWAIT_REPLY ||
458 call->state == AFS_CALL_AWAIT_OP_ID ||
459 call->state == AFS_CALL_AWAIT_REQUEST ||
460 call->state == AFS_CALL_AWAIT_ACK) &&
461 (skb = skb_dequeue(&call->rx_queue))) {
462 switch (skb->mark) {
463 case RXRPC_SKB_MARK_DATA:
464 _debug("Rcv DATA");
465 last = rxrpc_kernel_is_data_last(skb);
466 ret = call->type->deliver(call, skb, last);
467 switch (ret) {
468 case 0:
469 if (last &&
470 call->state == AFS_CALL_AWAIT_REPLY)
471 call->state = AFS_CALL_COMPLETE;
472 break;
473 case -ENOTCONN:
474 abort_code = RX_CALL_DEAD;
475 goto do_abort;
476 case -ENOTSUPP:
477 abort_code = RX_INVALID_OPERATION;
478 goto do_abort;
479 default:
480 abort_code = RXGEN_CC_UNMARSHAL;
481 if (call->state != AFS_CALL_AWAIT_REPLY)
482 abort_code = RXGEN_SS_UNMARSHAL;
483 do_abort:
484 rxrpc_kernel_abort_call(call->rxcall,
485 abort_code);
486 call->error = ret;
487 call->state = AFS_CALL_ERROR;
488 break;
489 }
00d3b7a4 490 afs_data_delivered(skb);
08e0e7c8 491 skb = NULL;
00d3b7a4 492 continue;
08e0e7c8
DH
493 case RXRPC_SKB_MARK_FINAL_ACK:
494 _debug("Rcv ACK");
495 call->state = AFS_CALL_COMPLETE;
496 break;
497 case RXRPC_SKB_MARK_BUSY:
498 _debug("Rcv BUSY");
499 call->error = -EBUSY;
500 call->state = AFS_CALL_BUSY;
501 break;
502 case RXRPC_SKB_MARK_REMOTE_ABORT:
503 abort_code = rxrpc_kernel_get_abort_code(skb);
504 call->error = call->type->abort_to_error(abort_code);
505 call->state = AFS_CALL_ABORTED;
506 _debug("Rcv ABORT %u -> %d", abort_code, call->error);
507 break;
508 case RXRPC_SKB_MARK_NET_ERROR:
509 call->error = -rxrpc_kernel_get_error_number(skb);
510 call->state = AFS_CALL_ERROR;
511 _debug("Rcv NET ERROR %d", call->error);
512 break;
513 case RXRPC_SKB_MARK_LOCAL_ERROR:
514 call->error = -rxrpc_kernel_get_error_number(skb);
515 call->state = AFS_CALL_ERROR;
516 _debug("Rcv LOCAL ERROR %d", call->error);
517 break;
518 default:
519 BUG();
520 break;
521 }
522
00d3b7a4 523 afs_free_skb(skb);
08e0e7c8
DH
524 }
525
526 /* make sure the queue is empty if the call is done with (we might have
527 * aborted the call early because of an unmarshalling error) */
528 if (call->state >= AFS_CALL_COMPLETE) {
529 while ((skb = skb_dequeue(&call->rx_queue)))
00d3b7a4 530 afs_free_skb(skb);
6c67c7c3
DH
531 if (call->incoming)
532 afs_end_call(call);
08e0e7c8
DH
533 }
534
535 _leave("");
536}
537
538/*
539 * wait synchronously for a call to complete
540 */
541static int afs_wait_for_call_to_complete(struct afs_call *call)
542{
543 struct sk_buff *skb;
544 int ret;
545
546 DECLARE_WAITQUEUE(myself, current);
547
548 _enter("");
549
550 add_wait_queue(&call->waitq, &myself);
551 for (;;) {
552 set_current_state(TASK_INTERRUPTIBLE);
553
554 /* deliver any messages that are in the queue */
555 if (!skb_queue_empty(&call->rx_queue)) {
556 __set_current_state(TASK_RUNNING);
557 afs_deliver_to_call(call);
558 continue;
559 }
560
561 ret = call->error;
562 if (call->state >= AFS_CALL_COMPLETE)
563 break;
564 ret = -EINTR;
565 if (signal_pending(current))
566 break;
567 schedule();
568 }
569
570 remove_wait_queue(&call->waitq, &myself);
571 __set_current_state(TASK_RUNNING);
572
573 /* kill the call */
574 if (call->state < AFS_CALL_COMPLETE) {
575 _debug("call incomplete");
576 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
577 while ((skb = skb_dequeue(&call->rx_queue)))
00d3b7a4 578 afs_free_skb(skb);
08e0e7c8
DH
579 }
580
581 _debug("call complete");
6c67c7c3 582 afs_end_call(call);
08e0e7c8
DH
583 _leave(" = %d", ret);
584 return ret;
585}
586
587/*
588 * wake up a waiting call
589 */
590static void afs_wake_up_call_waiter(struct afs_call *call)
591{
592 wake_up(&call->waitq);
593}
594
595/*
596 * wake up an asynchronous call
597 */
598static void afs_wake_up_async_call(struct afs_call *call)
599{
600 _enter("");
601 queue_work(afs_async_calls, &call->async_work);
602}
603
604/*
605 * put a call into asynchronous mode
606 * - mustn't touch the call descriptor as the call my have completed by the
607 * time we get here
608 */
609static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
610{
611 _enter("");
612 return -EINPROGRESS;
613}
614
615/*
616 * delete an asynchronous call
617 */
618static void afs_delete_async_call(struct work_struct *work)
619{
620 struct afs_call *call =
621 container_of(work, struct afs_call, async_work);
622
623 _enter("");
624
00d3b7a4 625 afs_free_call(call);
08e0e7c8
DH
626
627 _leave("");
628}
629
630/*
631 * perform processing on an asynchronous call
632 * - on a multiple-thread workqueue this work item may try to run on several
633 * CPUs at the same time
634 */
635static void afs_process_async_call(struct work_struct *work)
636{
637 struct afs_call *call =
638 container_of(work, struct afs_call, async_work);
639
640 _enter("");
641
642 if (!skb_queue_empty(&call->rx_queue))
643 afs_deliver_to_call(call);
644
645 if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
646 if (call->wait_mode->async_complete)
647 call->wait_mode->async_complete(call->reply,
648 call->error);
649 call->reply = NULL;
650
651 /* kill the call */
6cf12869 652 afs_end_call_nofree(call);
08e0e7c8
DH
653
654 /* we can't just delete the call because the work item may be
655 * queued */
05949945 656 call->async_workfn = afs_delete_async_call;
08e0e7c8
DH
657 queue_work(afs_async_calls, &call->async_work);
658 }
659
660 _leave("");
661}
662
663/*
664 * empty a socket buffer into a flat reply buffer
665 */
666void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
667{
668 size_t len = skb->len;
669
670 if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
671 BUG();
672 call->reply_size += len;
673}
674
05949945
TH
675static void afs_async_workfn(struct work_struct *work)
676{
677 struct afs_call *call = container_of(work, struct afs_call, async_work);
678
679 call->async_workfn(work);
680}
681
08e0e7c8
DH
682/*
683 * accept the backlog of incoming calls
684 */
685static void afs_collect_incoming_call(struct work_struct *work)
686{
687 struct rxrpc_call *rxcall;
688 struct afs_call *call = NULL;
689 struct sk_buff *skb;
690
691 while ((skb = skb_dequeue(&afs_incoming_calls))) {
692 _debug("new call");
693
694 /* don't need the notification */
00d3b7a4 695 afs_free_skb(skb);
08e0e7c8
DH
696
697 if (!call) {
698 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
699 if (!call) {
700 rxrpc_kernel_reject_call(afs_socket);
701 return;
702 }
703
05949945
TH
704 call->async_workfn = afs_process_async_call;
705 INIT_WORK(&call->async_work, afs_async_workfn);
08e0e7c8
DH
706 call->wait_mode = &afs_async_incoming_call;
707 call->type = &afs_RXCMxxxx;
708 init_waitqueue_head(&call->waitq);
709 skb_queue_head_init(&call->rx_queue);
710 call->state = AFS_CALL_AWAIT_OP_ID;
00d3b7a4
DH
711
712 _debug("CALL %p{%s} [%d]",
713 call, call->type->name,
714 atomic_read(&afs_outstanding_calls));
715 atomic_inc(&afs_outstanding_calls);
08e0e7c8
DH
716 }
717
718 rxcall = rxrpc_kernel_accept_call(afs_socket,
719 (unsigned long) call);
720 if (!IS_ERR(rxcall)) {
721 call->rxcall = rxcall;
722 call = NULL;
723 }
724 }
725
00d3b7a4
DH
726 if (call)
727 afs_free_call(call);
08e0e7c8
DH
728}
729
730/*
731 * grab the operation ID from an incoming cache manager call
732 */
733static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
734 bool last)
735{
736 size_t len = skb->len;
737 void *oibuf = (void *) &call->operation_ID;
738
739 _enter("{%u},{%zu},%d", call->offset, len, last);
740
741 ASSERTCMP(call->offset, <, 4);
742
743 /* the operation ID forms the first four bytes of the request data */
744 len = min_t(size_t, len, 4 - call->offset);
745 if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
746 BUG();
747 if (!pskb_pull(skb, len))
748 BUG();
749 call->offset += len;
750
751 if (call->offset < 4) {
752 if (last) {
753 _leave(" = -EBADMSG [op ID short]");
754 return -EBADMSG;
755 }
756 _leave(" = 0 [incomplete]");
757 return 0;
758 }
759
760 call->state = AFS_CALL_AWAIT_REQUEST;
761
762 /* ask the cache manager to route the call (it'll change the call type
763 * if successful) */
764 if (!afs_cm_incoming_call(call))
765 return -ENOTSUPP;
766
767 /* pass responsibility for the remainer of this message off to the
768 * cache manager op */
769 return call->type->deliver(call, skb, last);
770}
771
772/*
773 * send an empty reply
774 */
775void afs_send_empty_reply(struct afs_call *call)
776{
777 struct msghdr msg;
778 struct iovec iov[1];
779
780 _enter("");
781
782 iov[0].iov_base = NULL;
783 iov[0].iov_len = 0;
784 msg.msg_name = NULL;
785 msg.msg_namelen = 0;
786 msg.msg_iov = iov;
787 msg.msg_iovlen = 0;
788 msg.msg_control = NULL;
789 msg.msg_controllen = 0;
790 msg.msg_flags = 0;
791
792 call->state = AFS_CALL_AWAIT_ACK;
793 switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
794 case 0:
795 _leave(" [replied]");
796 return;
797
798 case -ENOMEM:
799 _debug("oom");
800 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
801 default:
6c67c7c3 802 afs_end_call(call);
08e0e7c8
DH
803 _leave(" [error]");
804 return;
805 }
806}
807
b908fe6b
DH
808/*
809 * send a simple reply
810 */
811void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
812{
813 struct msghdr msg;
814 struct iovec iov[1];
bd6dc742 815 int n;
b908fe6b
DH
816
817 _enter("");
818
819 iov[0].iov_base = (void *) buf;
820 iov[0].iov_len = len;
821 msg.msg_name = NULL;
822 msg.msg_namelen = 0;
823 msg.msg_iov = iov;
824 msg.msg_iovlen = 1;
825 msg.msg_control = NULL;
826 msg.msg_controllen = 0;
827 msg.msg_flags = 0;
828
829 call->state = AFS_CALL_AWAIT_ACK;
bd6dc742
DH
830 n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
831 if (n >= 0) {
6c67c7c3 832 /* Success */
b908fe6b
DH
833 _leave(" [replied]");
834 return;
bd6dc742 835 }
6c67c7c3 836
bd6dc742 837 if (n == -ENOMEM) {
b908fe6b
DH
838 _debug("oom");
839 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
b908fe6b 840 }
6c67c7c3 841 afs_end_call(call);
bd6dc742 842 _leave(" [error]");
b908fe6b
DH
843}
844
08e0e7c8
DH
845/*
846 * extract a piece of data from the received data socket buffers
847 */
848int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
849 bool last, void *buf, size_t count)
850{
851 size_t len = skb->len;
852
853 _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
854
855 ASSERTCMP(call->offset, <, count);
856
857 len = min_t(size_t, len, count - call->offset);
858 if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
859 !pskb_pull(skb, len))
860 BUG();
861 call->offset += len;
862
863 if (call->offset < count) {
864 if (last) {
b1bdb691 865 _leave(" = -EBADMSG [%d < %zu]", call->offset, count);
08e0e7c8
DH
866 return -EBADMSG;
867 }
868 _leave(" = -EAGAIN");
869 return -EAGAIN;
870 }
871 return 0;
872}
This page took 0.508738 seconds and 5 git commands to generate.