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