]> Git Repo - linux.git/blame - fs/afs/rxrpc.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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 ([email protected])
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>
174cd4b1
IM
13#include <linux/sched/signal.h>
14
08e0e7c8
DH
15#include <net/sock.h>
16#include <net/af_rxrpc.h>
17#include <rxrpc/packet.h>
18#include "internal.h"
19#include "afs_cm.h"
20
8324f0bc 21struct socket *afs_socket; /* my RxRPC socket */
08e0e7c8 22static struct workqueue_struct *afs_async_calls;
00e90712 23static struct afs_call *afs_spare_incoming_call;
341f741f 24atomic_t afs_outstanding_calls;
08e0e7c8 25
d001648e 26static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
08e0e7c8 27static int afs_wait_for_call_to_complete(struct afs_call *);
d001648e 28static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
d001648e 29static void afs_process_async_call(struct work_struct *);
00e90712
DH
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);
d001648e 32static int afs_deliver_cm_op_id(struct afs_call *);
08e0e7c8 33
08e0e7c8
DH
34/* asynchronous incoming call initial processing */
35static const struct afs_call_type afs_RXCMxxxx = {
00d3b7a4 36 .name = "CB.xxxx",
08e0e7c8
DH
37 .deliver = afs_deliver_cm_op_id,
38 .abort_to_error = afs_abort_to_error,
39};
40
00e90712 41static void afs_charge_preallocation(struct work_struct *);
08e0e7c8 42
00e90712 43static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
08e0e7c8 44
2f02f7ae
DH
45static int afs_wait_atomic_t(atomic_t *p)
46{
47 schedule();
48 return 0;
49}
50
08e0e7c8
DH
51/*
52 * open an RxRPC socket and bind it to be a server for callback notifications
53 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
54 */
55int afs_open_socket(void)
56{
57 struct sockaddr_rxrpc srx;
58 struct socket *socket;
59 int ret;
60
61 _enter("");
62
0e119b41 63 ret = -ENOMEM;
69ad052a 64 afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
0e119b41
DH
65 if (!afs_async_calls)
66 goto error_0;
08e0e7c8 67
eeb1bd5c 68 ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
0e119b41
DH
69 if (ret < 0)
70 goto error_1;
08e0e7c8
DH
71
72 socket->sk->sk_allocation = GFP_NOFS;
73
74 /* bind the callback manager's address to make this a server socket */
75 srx.srx_family = AF_RXRPC;
76 srx.srx_service = CM_SERVICE;
77 srx.transport_type = SOCK_DGRAM;
78 srx.transport_len = sizeof(srx.transport.sin);
79 srx.transport.sin.sin_family = AF_INET;
80 srx.transport.sin.sin_port = htons(AFS_CM_PORT);
81 memset(&srx.transport.sin.sin_addr, 0,
82 sizeof(srx.transport.sin.sin_addr));
83
84 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
0e119b41
DH
85 if (ret < 0)
86 goto error_2;
87
00e90712
DH
88 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
89 afs_rx_discard_new_call);
d001648e 90
0e119b41
DH
91 ret = kernel_listen(socket, INT_MAX);
92 if (ret < 0)
93 goto error_2;
08e0e7c8 94
08e0e7c8 95 afs_socket = socket;
00e90712 96 afs_charge_preallocation(NULL);
08e0e7c8
DH
97 _leave(" = 0");
98 return 0;
0e119b41
DH
99
100error_2:
101 sock_release(socket);
102error_1:
103 destroy_workqueue(afs_async_calls);
104error_0:
105 _leave(" = %d", ret);
106 return ret;
08e0e7c8
DH
107}
108
109/*
110 * close the RxRPC socket AFS was using
111 */
112void afs_close_socket(void)
113{
114 _enter("");
115
341f741f
DH
116 kernel_listen(afs_socket, 0);
117 flush_workqueue(afs_async_calls);
118
00e90712 119 if (afs_spare_incoming_call) {
341f741f 120 afs_put_call(afs_spare_incoming_call);
00e90712
DH
121 afs_spare_incoming_call = NULL;
122 }
123
d001648e 124 _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
2f02f7ae
DH
125 wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
126 TASK_UNINTERRUPTIBLE);
127 _debug("no outstanding calls");
128
248f219c 129 kernel_sock_shutdown(afs_socket, SHUT_RDWR);
d001648e 130 flush_workqueue(afs_async_calls);
08e0e7c8
DH
131 sock_release(afs_socket);
132
133 _debug("dework");
134 destroy_workqueue(afs_async_calls);
135 _leave("");
136}
137
00d3b7a4 138/*
341f741f 139 * Allocate a call.
00d3b7a4 140 */
341f741f
DH
141static struct afs_call *afs_alloc_call(const struct afs_call_type *type,
142 gfp_t gfp)
00d3b7a4 143{
341f741f
DH
144 struct afs_call *call;
145 int o;
00d3b7a4 146
341f741f
DH
147 call = kzalloc(sizeof(*call), gfp);
148 if (!call)
149 return NULL;
00d3b7a4 150
341f741f
DH
151 call->type = type;
152 atomic_set(&call->usage, 1);
153 INIT_WORK(&call->async_work, afs_process_async_call);
154 init_waitqueue_head(&call->waitq);
2f02f7ae 155
341f741f
DH
156 o = atomic_inc_return(&afs_outstanding_calls);
157 trace_afs_call(call, afs_call_trace_alloc, 1, o,
158 __builtin_return_address(0));
159 return call;
00d3b7a4
DH
160}
161
6c67c7c3 162/*
341f741f 163 * Dispose of a reference on a call.
6c67c7c3 164 */
341f741f 165void afs_put_call(struct afs_call *call)
6c67c7c3 166{
341f741f
DH
167 int n = atomic_dec_return(&call->usage);
168 int o = atomic_read(&afs_outstanding_calls);
169
170 trace_afs_call(call, afs_call_trace_put, n + 1, o,
171 __builtin_return_address(0));
172
173 ASSERTCMP(n, >=, 0);
174 if (n == 0) {
175 ASSERT(!work_pending(&call->async_work));
176 ASSERT(call->type->name != NULL);
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 kfree(call->request);
186 kfree(call);
187
188 o = atomic_dec_return(&afs_outstanding_calls);
189 trace_afs_call(call, afs_call_trace_free, 0, o,
190 __builtin_return_address(0));
191 if (o == 0)
192 wake_up_atomic_t(&afs_outstanding_calls);
6c67c7c3 193 }
6cf12869
NWF
194}
195
196/*
341f741f 197 * Queue the call for actual work. Returns 0 unconditionally for convenience.
6cf12869 198 */
341f741f 199int afs_queue_call_work(struct afs_call *call)
6cf12869 200{
341f741f
DH
201 int u = atomic_inc_return(&call->usage);
202
203 trace_afs_call(call, afs_call_trace_work, u,
204 atomic_read(&afs_outstanding_calls),
205 __builtin_return_address(0));
206
207 INIT_WORK(&call->work, call->type->work);
208
209 if (!queue_work(afs_wq, &call->work))
210 afs_put_call(call);
211 return 0;
6c67c7c3
DH
212}
213
08e0e7c8
DH
214/*
215 * allocate a call with flat request and reply buffers
216 */
217struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
d001648e 218 size_t request_size, size_t reply_max)
08e0e7c8
DH
219{
220 struct afs_call *call;
221
341f741f 222 call = afs_alloc_call(type, GFP_NOFS);
08e0e7c8
DH
223 if (!call)
224 goto nomem_call;
225
226 if (request_size) {
341f741f 227 call->request_size = request_size;
08e0e7c8
DH
228 call->request = kmalloc(request_size, GFP_NOFS);
229 if (!call->request)
00d3b7a4 230 goto nomem_free;
08e0e7c8
DH
231 }
232
d001648e 233 if (reply_max) {
341f741f 234 call->reply_max = reply_max;
d001648e 235 call->buffer = kmalloc(reply_max, GFP_NOFS);
08e0e7c8 236 if (!call->buffer)
00d3b7a4 237 goto nomem_free;
08e0e7c8
DH
238 }
239
08e0e7c8 240 init_waitqueue_head(&call->waitq);
08e0e7c8
DH
241 return call;
242
00d3b7a4 243nomem_free:
341f741f 244 afs_put_call(call);
08e0e7c8
DH
245nomem_call:
246 return NULL;
247}
248
249/*
250 * clean up a call with flat buffer
251 */
252void afs_flat_call_destructor(struct afs_call *call)
253{
254 _enter("");
255
256 kfree(call->request);
257 call->request = NULL;
258 kfree(call->buffer);
259 call->buffer = NULL;
260}
261
31143d5d
DH
262/*
263 * attach the data from a bunch of pages on an inode to a call
264 */
39c6acea 265static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
31143d5d
DH
266{
267 struct page *pages[8];
268 unsigned count, n, loop, offset, to;
269 pgoff_t first = call->first, last = call->last;
270 int ret;
271
272 _enter("");
273
274 offset = call->first_offset;
275 call->first_offset = 0;
276
277 do {
278 _debug("attach %lx-%lx", first, last);
279
280 count = last - first + 1;
281 if (count > ARRAY_SIZE(pages))
282 count = ARRAY_SIZE(pages);
283 n = find_get_pages_contig(call->mapping, first, count, pages);
284 ASSERTCMP(n, ==, count);
285
286 loop = 0;
287 do {
39c6acea
AV
288 struct bio_vec bvec = {.bv_page = pages[loop],
289 .bv_offset = offset};
31143d5d
DH
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;
39c6acea 296 bvec.bv_len = to - offset;
31143d5d
DH
297 offset = 0;
298
299 _debug("- range %u-%u%s",
300 offset, to, msg->msg_flags ? " [more]" : "");
39c6acea
AV
301 iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC,
302 &bvec, 1, to - offset);
31143d5d
DH
303
304 /* have to change the state *before* sending the last
305 * packet as RxRPC might give us the reply before it
306 * returns from sending the request */
307 if (first + loop >= last)
308 call->state = AFS_CALL_AWAIT_REPLY;
4de48af6
DH
309 ret = rxrpc_kernel_send_data(afs_socket, call->rxcall,
310 msg, to - offset);
31143d5d
DH
311 if (ret < 0)
312 break;
313 } while (++loop < count);
314 first += count;
315
316 for (loop = 0; loop < count; loop++)
317 put_page(pages[loop]);
318 if (ret < 0)
319 break;
5bbf5d39 320 } while (first <= last);
31143d5d
DH
321
322 _leave(" = %d", ret);
323 return ret;
324}
325
08e0e7c8
DH
326/*
327 * initiate a call
328 */
329int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
56ff9c83 330 bool async)
08e0e7c8
DH
331{
332 struct sockaddr_rxrpc srx;
333 struct rxrpc_call *rxcall;
334 struct msghdr msg;
335 struct kvec iov[1];
336 int ret;
337
338 _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
339
00d3b7a4
DH
340 ASSERT(call->type != NULL);
341 ASSERT(call->type->name != NULL);
342
31143d5d
DH
343 _debug("____MAKE %p{%s,%x} [%d]____",
344 call, call->type->name, key_serial(call->key),
345 atomic_read(&afs_outstanding_calls));
00d3b7a4 346
56ff9c83 347 call->async = async;
08e0e7c8
DH
348
349 memset(&srx, 0, sizeof(srx));
350 srx.srx_family = AF_RXRPC;
351 srx.srx_service = call->service_id;
352 srx.transport_type = SOCK_DGRAM;
353 srx.transport_len = sizeof(srx.transport.sin);
354 srx.transport.sin.sin_family = AF_INET;
355 srx.transport.sin.sin_port = call->port;
356 memcpy(&srx.transport.sin.sin_addr, addr, 4);
357
358 /* create a call */
359 rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
d001648e 360 (unsigned long) call, gfp,
56ff9c83
DH
361 (async ?
362 afs_wake_up_async_call :
363 afs_wake_up_call_waiter));
00d3b7a4 364 call->key = NULL;
08e0e7c8
DH
365 if (IS_ERR(rxcall)) {
366 ret = PTR_ERR(rxcall);
367 goto error_kill_call;
368 }
369
370 call->rxcall = rxcall;
371
372 /* send the request */
373 iov[0].iov_base = call->request;
374 iov[0].iov_len = call->request_size;
375
376 msg.msg_name = NULL;
377 msg.msg_namelen = 0;
2e90b1c4 378 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
c0371da6 379 call->request_size);
08e0e7c8
DH
380 msg.msg_control = NULL;
381 msg.msg_controllen = 0;
31143d5d 382 msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
08e0e7c8
DH
383
384 /* have to change the state *before* sending the last packet as RxRPC
385 * might give us the reply before it returns from sending the
386 * request */
31143d5d
DH
387 if (!call->send_pages)
388 call->state = AFS_CALL_AWAIT_REPLY;
4de48af6
DH
389 ret = rxrpc_kernel_send_data(afs_socket, rxcall,
390 &msg, call->request_size);
08e0e7c8
DH
391 if (ret < 0)
392 goto error_do_abort;
393
31143d5d 394 if (call->send_pages) {
39c6acea 395 ret = afs_send_pages(call, &msg);
31143d5d
DH
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 */
56ff9c83
DH
402 if (call->async)
403 return -EINPROGRESS;
404
405 return afs_wait_for_call_to_complete(call);
08e0e7c8
DH
406
407error_do_abort:
5a42976d 408 rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
08e0e7c8 409error_kill_call:
341f741f 410 afs_put_call(call);
08e0e7c8
DH
411 _leave(" = %d", ret);
412 return ret;
413}
414
08e0e7c8
DH
415/*
416 * deliver messages to a call
417 */
418static void afs_deliver_to_call(struct afs_call *call)
419{
08e0e7c8
DH
420 u32 abort_code;
421 int ret;
422
d001648e
DH
423 _enter("%s", call->type->name);
424
425 while (call->state == AFS_CALL_AWAIT_REPLY ||
426 call->state == AFS_CALL_AWAIT_OP_ID ||
427 call->state == AFS_CALL_AWAIT_REQUEST ||
428 call->state == AFS_CALL_AWAIT_ACK
429 ) {
430 if (call->state == AFS_CALL_AWAIT_ACK) {
431 size_t offset = 0;
432 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
433 NULL, 0, &offset, false,
434 &call->abort_code);
8e8d7f13
DH
435 trace_afs_recv_data(call, 0, offset, false, ret);
436
d001648e
DH
437 if (ret == -EINPROGRESS || ret == -EAGAIN)
438 return;
9008f998 439 if (ret == 1 || ret < 0) {
d001648e
DH
440 call->state = AFS_CALL_COMPLETE;
441 goto done;
08e0e7c8 442 }
d001648e 443 return;
08e0e7c8
DH
444 }
445
d001648e
DH
446 ret = call->type->deliver(call);
447 switch (ret) {
448 case 0:
449 if (call->state == AFS_CALL_AWAIT_REPLY)
450 call->state = AFS_CALL_COMPLETE;
451 goto done;
452 case -EINPROGRESS:
453 case -EAGAIN:
454 goto out;
455 case -ENOTCONN:
456 abort_code = RX_CALL_DEAD;
457 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 458 abort_code, -ret, "KNC");
d001648e
DH
459 goto do_abort;
460 case -ENOTSUPP:
461 abort_code = RX_INVALID_OPERATION;
462 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 463 abort_code, -ret, "KIV");
d001648e
DH
464 goto do_abort;
465 case -ENODATA:
466 case -EBADMSG:
467 case -EMSGSIZE:
468 default:
469 abort_code = RXGEN_CC_UNMARSHAL;
470 if (call->state != AFS_CALL_AWAIT_REPLY)
471 abort_code = RXGEN_SS_UNMARSHAL;
472 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 473 abort_code, EBADMSG, "KUM");
d001648e
DH
474 goto do_abort;
475 }
08e0e7c8
DH
476 }
477
d001648e
DH
478done:
479 if (call->state == AFS_CALL_COMPLETE && call->incoming)
341f741f 480 afs_put_call(call);
d001648e 481out:
08e0e7c8 482 _leave("");
d001648e
DH
483 return;
484
485do_abort:
486 call->error = ret;
487 call->state = AFS_CALL_COMPLETE;
488 goto done;
08e0e7c8
DH
489}
490
491/*
492 * wait synchronously for a call to complete
493 */
494static int afs_wait_for_call_to_complete(struct afs_call *call)
495{
5a42976d 496 const char *abort_why;
08e0e7c8
DH
497 int ret;
498
499 DECLARE_WAITQUEUE(myself, current);
500
501 _enter("");
502
503 add_wait_queue(&call->waitq, &myself);
504 for (;;) {
505 set_current_state(TASK_INTERRUPTIBLE);
506
507 /* deliver any messages that are in the queue */
d001648e
DH
508 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
509 call->need_attention = false;
08e0e7c8
DH
510 __set_current_state(TASK_RUNNING);
511 afs_deliver_to_call(call);
512 continue;
513 }
514
5a42976d 515 abort_why = "KWC";
08e0e7c8 516 ret = call->error;
d001648e 517 if (call->state == AFS_CALL_COMPLETE)
08e0e7c8 518 break;
5a42976d 519 abort_why = "KWI";
08e0e7c8
DH
520 ret = -EINTR;
521 if (signal_pending(current))
522 break;
523 schedule();
524 }
525
526 remove_wait_queue(&call->waitq, &myself);
527 __set_current_state(TASK_RUNNING);
528
529 /* kill the call */
530 if (call->state < AFS_CALL_COMPLETE) {
531 _debug("call incomplete");
d001648e 532 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 533 RX_CALL_DEAD, -ret, abort_why);
08e0e7c8
DH
534 }
535
536 _debug("call complete");
341f741f 537 afs_put_call(call);
08e0e7c8
DH
538 _leave(" = %d", ret);
539 return ret;
540}
541
542/*
543 * wake up a waiting call
544 */
d001648e
DH
545static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
546 unsigned long call_user_ID)
08e0e7c8 547{
d001648e
DH
548 struct afs_call *call = (struct afs_call *)call_user_ID;
549
550 call->need_attention = true;
08e0e7c8
DH
551 wake_up(&call->waitq);
552}
553
554/*
555 * wake up an asynchronous call
556 */
d001648e
DH
557static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
558 unsigned long call_user_ID)
08e0e7c8 559{
d001648e 560 struct afs_call *call = (struct afs_call *)call_user_ID;
341f741f 561 int u;
d001648e 562
8e8d7f13 563 trace_afs_notify_call(rxcall, call);
d001648e 564 call->need_attention = true;
341f741f
DH
565
566 u = __atomic_add_unless(&call->usage, 1, 0);
567 if (u != 0) {
568 trace_afs_call(call, afs_call_trace_wake, u,
569 atomic_read(&afs_outstanding_calls),
570 __builtin_return_address(0));
571
572 if (!queue_work(afs_async_calls, &call->async_work))
573 afs_put_call(call);
574 }
08e0e7c8
DH
575}
576
08e0e7c8 577/*
341f741f
DH
578 * Delete an asynchronous call. The work item carries a ref to the call struct
579 * that we need to release.
08e0e7c8 580 */
d001648e 581static void afs_delete_async_call(struct work_struct *work)
08e0e7c8 582{
d001648e
DH
583 struct afs_call *call = container_of(work, struct afs_call, async_work);
584
08e0e7c8
DH
585 _enter("");
586
341f741f 587 afs_put_call(call);
08e0e7c8
DH
588
589 _leave("");
590}
591
592/*
341f741f
DH
593 * Perform I/O processing on an asynchronous call. The work item carries a ref
594 * to the call struct that we either need to release or to pass on.
08e0e7c8 595 */
d001648e 596static void afs_process_async_call(struct work_struct *work)
08e0e7c8 597{
d001648e
DH
598 struct afs_call *call = container_of(work, struct afs_call, async_work);
599
08e0e7c8
DH
600 _enter("");
601
d001648e
DH
602 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
603 call->need_attention = false;
08e0e7c8 604 afs_deliver_to_call(call);
d001648e 605 }
08e0e7c8 606
56ff9c83 607 if (call->state == AFS_CALL_COMPLETE) {
08e0e7c8
DH
608 call->reply = NULL;
609
341f741f
DH
610 /* We have two refs to release - one from the alloc and one
611 * queued with the work item - and we can't just deallocate the
612 * call because the work item may be queued again.
613 */
d001648e 614 call->async_work.func = afs_delete_async_call;
341f741f
DH
615 if (!queue_work(afs_async_calls, &call->async_work))
616 afs_put_call(call);
08e0e7c8
DH
617 }
618
341f741f 619 afs_put_call(call);
08e0e7c8
DH
620 _leave("");
621}
622
00e90712
DH
623static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
624{
625 struct afs_call *call = (struct afs_call *)user_call_ID;
626
627 call->rxcall = rxcall;
628}
629
630/*
631 * Charge the incoming call preallocation.
632 */
633static void afs_charge_preallocation(struct work_struct *work)
634{
635 struct afs_call *call = afs_spare_incoming_call;
636
637 for (;;) {
638 if (!call) {
341f741f 639 call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL);
00e90712
DH
640 if (!call)
641 break;
642
56ff9c83 643 call->async = true;
00e90712 644 call->state = AFS_CALL_AWAIT_OP_ID;
56ff9c83 645 init_waitqueue_head(&call->waitq);
00e90712
DH
646 }
647
648 if (rxrpc_kernel_charge_accept(afs_socket,
649 afs_wake_up_async_call,
650 afs_rx_attach,
651 (unsigned long)call,
652 GFP_KERNEL) < 0)
653 break;
654 call = NULL;
655 }
656 afs_spare_incoming_call = call;
657}
658
659/*
660 * Discard a preallocated call when a socket is shut down.
661 */
662static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
663 unsigned long user_call_ID)
664{
665 struct afs_call *call = (struct afs_call *)user_call_ID;
666
00e90712 667 call->rxcall = NULL;
341f741f 668 afs_put_call(call);
00e90712
DH
669}
670
d001648e
DH
671/*
672 * Notification of an incoming call.
673 */
00e90712
DH
674static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
675 unsigned long user_call_ID)
d001648e 676{
00e90712 677 queue_work(afs_wq, &afs_charge_preallocation_work);
d001648e
DH
678}
679
08e0e7c8 680/*
372ee163
DH
681 * Grab the operation ID from an incoming cache manager call. The socket
682 * buffer is discarded on error or if we don't yet have sufficient data.
08e0e7c8 683 */
d001648e 684static int afs_deliver_cm_op_id(struct afs_call *call)
08e0e7c8 685{
d001648e 686 int ret;
08e0e7c8 687
d001648e 688 _enter("{%zu}", call->offset);
08e0e7c8
DH
689
690 ASSERTCMP(call->offset, <, 4);
691
692 /* the operation ID forms the first four bytes of the request data */
50a2c953 693 ret = afs_extract_data(call, &call->tmp, 4, true);
d001648e
DH
694 if (ret < 0)
695 return ret;
08e0e7c8 696
50a2c953 697 call->operation_ID = ntohl(call->tmp);
08e0e7c8 698 call->state = AFS_CALL_AWAIT_REQUEST;
d001648e 699 call->offset = 0;
08e0e7c8
DH
700
701 /* ask the cache manager to route the call (it'll change the call type
702 * if successful) */
703 if (!afs_cm_incoming_call(call))
704 return -ENOTSUPP;
705
8e8d7f13
DH
706 trace_afs_cb_call(call);
707
08e0e7c8
DH
708 /* pass responsibility for the remainer of this message off to the
709 * cache manager op */
d001648e 710 return call->type->deliver(call);
08e0e7c8
DH
711}
712
713/*
714 * send an empty reply
715 */
716void afs_send_empty_reply(struct afs_call *call)
717{
718 struct msghdr msg;
08e0e7c8
DH
719
720 _enter("");
721
08e0e7c8
DH
722 msg.msg_name = NULL;
723 msg.msg_namelen = 0;
bfd4e956 724 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
08e0e7c8
DH
725 msg.msg_control = NULL;
726 msg.msg_controllen = 0;
727 msg.msg_flags = 0;
728
729 call->state = AFS_CALL_AWAIT_ACK;
4de48af6 730 switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
08e0e7c8
DH
731 case 0:
732 _leave(" [replied]");
733 return;
734
735 case -ENOMEM:
736 _debug("oom");
4de48af6 737 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 738 RX_USER_ABORT, ENOMEM, "KOO");
08e0e7c8 739 default:
08e0e7c8
DH
740 _leave(" [error]");
741 return;
742 }
743}
744
b908fe6b
DH
745/*
746 * send a simple reply
747 */
748void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
749{
750 struct msghdr msg;
2e90b1c4 751 struct kvec iov[1];
bd6dc742 752 int n;
b908fe6b
DH
753
754 _enter("");
755
756 iov[0].iov_base = (void *) buf;
757 iov[0].iov_len = len;
758 msg.msg_name = NULL;
759 msg.msg_namelen = 0;
2e90b1c4 760 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
b908fe6b
DH
761 msg.msg_control = NULL;
762 msg.msg_controllen = 0;
763 msg.msg_flags = 0;
764
765 call->state = AFS_CALL_AWAIT_ACK;
4de48af6 766 n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
bd6dc742 767 if (n >= 0) {
6c67c7c3 768 /* Success */
b908fe6b
DH
769 _leave(" [replied]");
770 return;
bd6dc742 771 }
6c67c7c3 772
bd6dc742 773 if (n == -ENOMEM) {
b908fe6b 774 _debug("oom");
4de48af6 775 rxrpc_kernel_abort_call(afs_socket, call->rxcall,
5a42976d 776 RX_USER_ABORT, ENOMEM, "KOO");
b908fe6b 777 }
bd6dc742 778 _leave(" [error]");
b908fe6b
DH
779}
780
08e0e7c8 781/*
372ee163 782 * Extract a piece of data from the received data socket buffers.
08e0e7c8 783 */
d001648e
DH
784int afs_extract_data(struct afs_call *call, void *buf, size_t count,
785 bool want_more)
08e0e7c8 786{
d001648e 787 int ret;
08e0e7c8 788
d001648e
DH
789 _enter("{%s,%zu},,%zu,%d",
790 call->type->name, call->offset, count, want_more);
08e0e7c8 791
d001648e 792 ASSERTCMP(call->offset, <=, count);
08e0e7c8 793
d001648e
DH
794 ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
795 buf, count, &call->offset,
796 want_more, &call->abort_code);
8e8d7f13 797 trace_afs_recv_data(call, count, call->offset, want_more, ret);
d001648e
DH
798 if (ret == 0 || ret == -EAGAIN)
799 return ret;
08e0e7c8 800
d001648e
DH
801 if (ret == 1) {
802 switch (call->state) {
803 case AFS_CALL_AWAIT_REPLY:
804 call->state = AFS_CALL_COMPLETE;
805 break;
806 case AFS_CALL_AWAIT_REQUEST:
807 call->state = AFS_CALL_REPLYING;
808 break;
809 default:
810 break;
811 }
812 return 0;
08e0e7c8 813 }
d001648e
DH
814
815 if (ret == -ECONNABORTED)
816 call->error = call->type->abort_to_error(call->abort_code);
817 else
818 call->error = ret;
819 call->state = AFS_CALL_COMPLETE;
820 return ret;
08e0e7c8 821}
This page took 0.826902 seconds and 4 git commands to generate.