1
2
3
4
5
6
7
8
9
10
11
12#include <linux/slab.h>
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
19struct socket *afs_socket;
20static struct workqueue_struct *afs_async_calls;
21static struct afs_call *afs_spare_incoming_call;
22static atomic_t afs_outstanding_calls;
23
24static void afs_free_call(struct afs_call *);
25static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
26static int afs_wait_for_call_to_complete(struct afs_call *);
27static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
28static int afs_dont_wait_for_call_to_complete(struct afs_call *);
29static void afs_process_async_call(struct work_struct *);
30static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
31static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
32static int afs_deliver_cm_op_id(struct afs_call *);
33
34
35const struct afs_wait_mode afs_sync_call = {
36 .notify_rx = afs_wake_up_call_waiter,
37 .wait = afs_wait_for_call_to_complete,
38};
39
40
41const struct afs_wait_mode afs_async_call = {
42 .notify_rx = afs_wake_up_async_call,
43 .wait = afs_dont_wait_for_call_to_complete,
44};
45
46
47static const struct afs_wait_mode afs_async_incoming_call = {
48 .notify_rx = afs_wake_up_async_call,
49};
50
51
52static const struct afs_call_type afs_RXCMxxxx = {
53 .name = "CB.xxxx",
54 .deliver = afs_deliver_cm_op_id,
55 .abort_to_error = afs_abort_to_error,
56};
57
58static void afs_charge_preallocation(struct work_struct *);
59
60static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
61
62static int afs_wait_atomic_t(atomic_t *p)
63{
64 schedule();
65 return 0;
66}
67
68
69
70
71
72int afs_open_socket(void)
73{
74 struct sockaddr_rxrpc srx;
75 struct socket *socket;
76 int ret;
77
78 _enter("");
79
80 ret = -ENOMEM;
81 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
82 if (!afs_async_calls)
83 goto error_0;
84
85 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
86 if (ret < 0)
87 goto error_1;
88
89 socket->sk->sk_allocation = GFP_NOFS;
90
91
92 srx.srx_family = AF_RXRPC;
93 srx.srx_service = CM_SERVICE;
94 srx.transport_type = SOCK_DGRAM;
95 srx.transport_len = sizeof(srx.transport.sin);
96 srx.transport.sin.sin_family = AF_INET;
97 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
98 memset(&srx.transport.sin.sin_addr, 0,
99 sizeof(srx.transport.sin.sin_addr));
100
101 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
102 if (ret < 0)
103 goto error_2;
104
105 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
106 afs_rx_discard_new_call);
107
108 ret = kernel_listen(socket, INT_MAX);
109 if (ret < 0)
110 goto error_2;
111
112 afs_socket = socket;
113 afs_charge_preallocation(NULL);
114 _leave(" = 0");
115 return 0;
116
117error_2:
118 sock_release(socket);
119error_1:
120 destroy_workqueue(afs_async_calls);
121error_0:
122 _leave(" = %d", ret);
123 return ret;
124}
125
126
127
128
129void afs_close_socket(void)
130{
131 _enter("");
132
133 if (afs_spare_incoming_call) {
134 atomic_inc(&afs_outstanding_calls);
135 afs_free_call(afs_spare_incoming_call);
136 afs_spare_incoming_call = NULL;
137 }
138
139 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
140 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
141 TASK_UNINTERRUPTIBLE);
142 _debug("no outstanding calls");
143
144 flush_workqueue(afs_async_calls);
145 kernel_sock_shutdown(afs_socket, SHUT_RDWR);
146 flush_workqueue(afs_async_calls);
147 sock_release(afs_socket);
148
149 _debug("dework");
150 destroy_workqueue(afs_async_calls);
151 _leave("");
152}
153
154
155
156
157static void afs_free_call(struct afs_call *call)
158{
159 _debug("DONE %p{%s} [%d]",
160 call, call->type->name, atomic_read(&afs_outstanding_calls));
161
162 ASSERTCMP(call->rxcall, ==, NULL);
163 ASSERT(!work_pending(&call->async_work));
164 ASSERT(call->type->name != NULL);
165
166 kfree(call->request);
167 kfree(call);
168
169 if (atomic_dec_and_test(&afs_outstanding_calls))
170 wake_up_atomic_t(&afs_outstanding_calls);
171}
172
173
174
175
176static void afs_end_call_nofree(struct afs_call *call)
177{
178 if (call->rxcall) {
179 rxrpc_kernel_end_call(afs_socket, call->rxcall);
180 call->rxcall = NULL;
181 }
182 if (call->type->destructor)
183 call->type->destructor(call);
184}
185
186
187
188
189static void afs_end_call(struct afs_call *call)
190{
191 afs_end_call_nofree(call);
192 afs_free_call(call);
193}
194
195
196
197
198struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
199 size_t request_size, size_t reply_max)
200{
201 struct afs_call *call;
202
203 call = kzalloc(sizeof(*call), GFP_NOFS);
204 if (!call)
205 goto nomem_call;
206
207 _debug("CALL %p{%s} [%d]",
208 call, type->name, atomic_read(&afs_outstanding_calls));
209 atomic_inc(&afs_outstanding_calls);
210
211 call->type = type;
212 call->request_size = request_size;
213 call->reply_max = reply_max;
214
215 if (request_size) {
216 call->request = kmalloc(request_size, GFP_NOFS);
217 if (!call->request)
218 goto nomem_free;
219 }
220
221 if (reply_max) {
222 call->buffer = kmalloc(reply_max, GFP_NOFS);
223 if (!call->buffer)
224 goto nomem_free;
225 }
226
227 init_waitqueue_head(&call->waitq);
228 return call;
229
230nomem_free:
231 afs_free_call(call);
232nomem_call:
233 return NULL;
234}
235
236
237
238
239void afs_flat_call_destructor(struct afs_call *call)
240{
241 _enter("");
242
243 kfree(call->request);
244 call->request = NULL;
245 kfree(call->buffer);
246 call->buffer = NULL;
247}
248
249
250
251
252static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
253 struct kvec *iov)
254{
255 struct page *pages[8];
256 unsigned count, n, loop, offset, to;
257 pgoff_t first = call->first, last = call->last;
258 int ret;
259
260 _enter("");
261
262 offset = call->first_offset;
263 call->first_offset = 0;
264
265 do {
266 _debug("attach %lx-%lx", first, last);
267
268 count = last - first + 1;
269 if (count > ARRAY_SIZE(pages))
270 count = ARRAY_SIZE(pages);
271 n = find_get_pages_contig(call->mapping, first, count, pages);
272 ASSERTCMP(n, ==, count);
273
274 loop = 0;
275 do {
276 msg->msg_flags = 0;
277 to = PAGE_SIZE;
278 if (first + loop >= last)
279 to = call->last_to;
280 else
281 msg->msg_flags = MSG_MORE;
282 iov->iov_base = kmap(pages[loop]) + offset;
283 iov->iov_len = to - offset;
284 offset = 0;
285
286 _debug("- range %u-%u%s",
287 offset, to, msg->msg_flags ? " [more]" : "");
288 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
289 iov, 1, to - offset);
290
291
292
293
294 if (first + loop >= last)
295 call->state = AFS_CALL_AWAIT_REPLY;
296 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
297 msg, to - offset);
298 kunmap(pages[loop]);
299 if (ret < 0)
300 break;
301 } while (++loop < count);
302 first += count;
303
304 for (loop = 0; loop < count; loop++)
305 put_page(pages[loop]);
306 if (ret < 0)
307 break;
308 } while (first <= last);
309
310 _leave(" = %d", ret);
311 return ret;
312}
313
314
315
316
317int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
318 const struct afs_wait_mode *wait_mode)
319{
320 struct sockaddr_rxrpc srx;
321 struct rxrpc_call *rxcall;
322 struct msghdr msg;
323 struct kvec iov[1];
324 int ret;
325
326 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
327
328 ASSERT(call->type != NULL);
329 ASSERT(call->type->name != NULL);
330
331 _debug("____MAKE %p{%s,%x} [%d]____",
332 call, call->type->name, key_serial(call->key),
333 atomic_read(&afs_outstanding_calls));
334
335 call->wait_mode = wait_mode;
336 INIT_WORK(&call->async_work, afs_process_async_call);
337
338 memset(&srx, 0, sizeof(srx));
339 srx.srx_family = AF_RXRPC;
340 srx.srx_service = call->service_id;
341 srx.transport_type = SOCK_DGRAM;
342 srx.transport_len = sizeof(srx.transport.sin);
343 srx.transport.sin.sin_family = AF_INET;
344 srx.transport.sin.sin_port = call->port;
345 memcpy(&srx.transport.sin.sin_addr, addr, 4);
346
347
348 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
349 (unsigned long) call, gfp,
350 wait_mode->notify_rx);
351 call->key = NULL;
352 if (IS_ERR(rxcall)) {
353 ret = PTR_ERR(rxcall);
354 goto error_kill_call;
355 }
356
357 call->rxcall = rxcall;
358
359
360 iov[0].iov_base = call->request;
361 iov[0].iov_len = call->request_size;
362
363 msg.msg_name = NULL;
364 msg.msg_namelen = 0;
365 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
366 call->request_size);
367 msg.msg_control = NULL;
368 msg.msg_controllen = 0;
369 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
370
371
372
373
374 if (!call->send_pages)
375 call->state = AFS_CALL_AWAIT_REPLY;
376 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
377 &msg, call->request_size);
378 if (ret < 0)
379 goto error_do_abort;
380
381 if (call->send_pages) {
382 ret = afs_send_pages(call, &msg, iov);
383 if (ret < 0)
384 goto error_do_abort;
385 }
386
387
388
389 return wait_mode->wait(call);
390
391error_do_abort:
392 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
393error_kill_call:
394 afs_end_call(call);
395 _leave(" = %d", ret);
396 return ret;
397}
398
399
400
401
402static void afs_deliver_to_call(struct afs_call *call)
403{
404 u32 abort_code;
405 int ret;
406
407 _enter("%s", call->type->name);
408
409 while (call->state == AFS_CALL_AWAIT_REPLY ||
410 call->state == AFS_CALL_AWAIT_OP_ID ||
411 call->state == AFS_CALL_AWAIT_REQUEST ||
412 call->state == AFS_CALL_AWAIT_ACK
413 ) {
414 if (call->state == AFS_CALL_AWAIT_ACK) {
415 size_t offset = 0;
416 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
417 NULL, 0, &offset, false,
418 &call->abort_code);
419 if (ret == -EINPROGRESS || ret == -EAGAIN)
420 return;
421 if (ret == 1 || ret < 0) {
422 call->state = AFS_CALL_COMPLETE;
423 goto done;
424 }
425 return;
426 }
427
428 ret = call->type->deliver(call);
429 switch (ret) {
430 case 0:
431 if (call->state == AFS_CALL_AWAIT_REPLY)
432 call->state = AFS_CALL_COMPLETE;
433 goto done;
434 case -EINPROGRESS:
435 case -EAGAIN:
436 goto out;
437 case -ENOTCONN:
438 abort_code = RX_CALL_DEAD;
439 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
440 abort_code, -ret, "KNC");
441 goto do_abort;
442 case -ENOTSUPP:
443 abort_code = RX_INVALID_OPERATION;
444 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
445 abort_code, -ret, "KIV");
446 goto do_abort;
447 case -ENODATA:
448 case -EBADMSG:
449 case -EMSGSIZE:
450 default:
451 abort_code = RXGEN_CC_UNMARSHAL;
452 if (call->state != AFS_CALL_AWAIT_REPLY)
453 abort_code = RXGEN_SS_UNMARSHAL;
454 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
455 abort_code, EBADMSG, "KUM");
456 goto do_abort;
457 }
458 }
459
460done:
461 if (call->state == AFS_CALL_COMPLETE && call->incoming)
462 afs_end_call(call);
463out:
464 _leave("");
465 return;
466
467do_abort:
468 call->error = ret;
469 call->state = AFS_CALL_COMPLETE;
470 goto done;
471}
472
473
474
475
476static int afs_wait_for_call_to_complete(struct afs_call *call)
477{
478 const char *abort_why;
479 int ret;
480
481 DECLARE_WAITQUEUE(myself, current);
482
483 _enter("");
484
485 add_wait_queue(&call->waitq, &myself);
486 for (;;) {
487 set_current_state(TASK_INTERRUPTIBLE);
488
489
490 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
491 call->need_attention = false;
492 __set_current_state(TASK_RUNNING);
493 afs_deliver_to_call(call);
494 continue;
495 }
496
497 abort_why = "KWC";
498 ret = call->error;
499 if (call->state == AFS_CALL_COMPLETE)
500 break;
501 abort_why = "KWI";
502 ret = -EINTR;
503 if (signal_pending(current))
504 break;
505 schedule();
506 }
507
508 remove_wait_queue(&call->waitq, &myself);
509 __set_current_state(TASK_RUNNING);
510
511
512 if (call->state < AFS_CALL_COMPLETE) {
513 _debug("call incomplete");
514 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
515 RX_CALL_DEAD, -ret, abort_why);
516 }
517
518 _debug("call complete");
519 afs_end_call(call);
520 _leave(" = %d", ret);
521 return ret;
522}
523
524
525
526
527static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
528 unsigned long call_user_ID)
529{
530 struct afs_call *call = (struct afs_call *)call_user_ID;
531
532 call->need_attention = true;
533 wake_up(&call->waitq);
534}
535
536
537
538
539static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
540 unsigned long call_user_ID)
541{
542 struct afs_call *call = (struct afs_call *)call_user_ID;
543
544 call->need_attention = true;
545 queue_work(afs_async_calls, &call->async_work);
546}
547
548
549
550
551
552
553static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
554{
555 _enter("");
556 return -EINPROGRESS;
557}
558
559
560
561
562static void afs_delete_async_call(struct work_struct *work)
563{
564 struct afs_call *call = container_of(work, struct afs_call, async_work);
565
566 _enter("");
567
568 afs_free_call(call);
569
570 _leave("");
571}
572
573
574
575
576static void afs_process_async_call(struct work_struct *work)
577{
578 struct afs_call *call = container_of(work, struct afs_call, async_work);
579
580 _enter("");
581
582 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
583 call->need_attention = false;
584 afs_deliver_to_call(call);
585 }
586
587 if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
588 if (call->wait_mode->async_complete)
589 call->wait_mode->async_complete(call->reply,
590 call->error);
591 call->reply = NULL;
592
593
594 afs_end_call_nofree(call);
595
596
597
598 call->async_work.func = afs_delete_async_call;
599 queue_work(afs_async_calls, &call->async_work);
600 }
601
602 _leave("");
603}
604
605static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
606{
607 struct afs_call *call = (struct afs_call *)user_call_ID;
608
609 call->rxcall = rxcall;
610}
611
612
613
614
615static void afs_charge_preallocation(struct work_struct *work)
616{
617 struct afs_call *call = afs_spare_incoming_call;
618
619 for (;;) {
620 if (!call) {
621 call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
622 if (!call)
623 break;
624
625 INIT_WORK(&call->async_work, afs_process_async_call);
626 call->wait_mode = &afs_async_incoming_call;
627 call->type = &afs_RXCMxxxx;
628 init_waitqueue_head(&call->waitq);
629 call->state = AFS_CALL_AWAIT_OP_ID;
630 }
631
632 if (rxrpc_kernel_charge_accept(afs_socket,
633 afs_wake_up_async_call,
634 afs_rx_attach,
635 (unsigned long)call,
636 GFP_KERNEL) < 0)
637 break;
638 call = NULL;
639 }
640 afs_spare_incoming_call = call;
641}
642
643
644
645
646static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
647 unsigned long user_call_ID)
648{
649 struct afs_call *call = (struct afs_call *)user_call_ID;
650
651 atomic_inc(&afs_outstanding_calls);
652 call->rxcall = NULL;
653 afs_free_call(call);
654}
655
656
657
658
659static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
660 unsigned long user_call_ID)
661{
662 atomic_inc(&afs_outstanding_calls);
663 queue_work(afs_wq, &afs_charge_preallocation_work);
664}
665
666
667
668
669
670static int afs_deliver_cm_op_id(struct afs_call *call)
671{
672 int ret;
673
674 _enter("{%zu}", call->offset);
675
676 ASSERTCMP(call->offset, <, 4);
677
678
679 ret = afs_extract_data(call, &call->tmp, 4, true);
680 if (ret < 0)
681 return ret;
682
683 call->operation_ID = ntohl(call->tmp);
684 call->state = AFS_CALL_AWAIT_REQUEST;
685 call->offset = 0;
686
687
688
689 if (!afs_cm_incoming_call(call))
690 return -ENOTSUPP;
691
692
693
694 return call->type->deliver(call);
695}
696
697
698
699
700void afs_send_empty_reply(struct afs_call *call)
701{
702 struct msghdr msg;
703
704 _enter("");
705
706 msg.msg_name = NULL;
707 msg.msg_namelen = 0;
708 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
709 msg.msg_control = NULL;
710 msg.msg_controllen = 0;
711 msg.msg_flags = 0;
712
713 call->state = AFS_CALL_AWAIT_ACK;
714 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
715 case 0:
716 _leave(" [replied]");
717 return;
718
719 case -ENOMEM:
720 _debug("oom");
721 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
722 RX_USER_ABORT, ENOMEM, "KOO");
723 default:
724 afs_end_call(call);
725 _leave(" [error]");
726 return;
727 }
728}
729
730
731
732
733void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
734{
735 struct msghdr msg;
736 struct kvec iov[1];
737 int n;
738
739 _enter("");
740
741 iov[0].iov_base = (void *) buf;
742 iov[0].iov_len = len;
743 msg.msg_name = NULL;
744 msg.msg_namelen = 0;
745 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
746 msg.msg_control = NULL;
747 msg.msg_controllen = 0;
748 msg.msg_flags = 0;
749
750 call->state = AFS_CALL_AWAIT_ACK;
751 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
752 if (n >= 0) {
753
754 _leave(" [replied]");
755 return;
756 }
757
758 if (n == -ENOMEM) {
759 _debug("oom");
760 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
761 RX_USER_ABORT, ENOMEM, "KOO");
762 }
763 afs_end_call(call);
764 _leave(" [error]");
765}
766
767
768
769
770int afs_extract_data(struct afs_call *call, void *buf, size_t count,
771 bool want_more)
772{
773 int ret;
774
775 _enter("{%s,%zu},,%zu,%d",
776 call->type->name, call->offset, count, want_more);
777
778 ASSERTCMP(call->offset, <=, count);
779
780 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
781 buf, count, &call->offset,
782 want_more, &call->abort_code);
783 if (ret == 0 || ret == -EAGAIN)
784 return ret;
785
786 if (ret == 1) {
787 switch (call->state) {
788 case AFS_CALL_AWAIT_REPLY:
789 call->state = AFS_CALL_COMPLETE;
790 break;
791 case AFS_CALL_AWAIT_REQUEST:
792 call->state = AFS_CALL_REPLYING;
793 break;
794 default:
795 break;
796 }
797 return 0;
798 }
799
800 if (ret == -ECONNABORTED)
801 call->error = call->type->abort_to_error(call->abort_code);
802 else
803 call->error = ret;
804 call->state = AFS_CALL_COMPLETE;
805 return ret;
806}
807