1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Maintain an RxRPC server socket to do AFS communications through 3 * 4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/sched/signal.h> 10 11 #include <net/sock.h> 12 #include <net/af_rxrpc.h> 13 #include "internal.h" 14 #include "afs_cm.h" 15 #include "protocol_yfs.h" 16 #define RXRPC_TRACE_ONLY_DEFINE_ENUMS 17 #include <trace/events/rxrpc.h> 18 19 struct workqueue_struct *afs_async_calls; 20 21 static void afs_deferred_free_worker(struct work_struct *work); 22 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long); 23 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long); 24 static void afs_process_async_call(struct work_struct *); 25 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long); 26 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long); 27 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID); 28 static void afs_rx_notify_oob(struct sock *sk, struct sk_buff *oob); 29 static int afs_deliver_cm_op_id(struct afs_call *); 30 31 static const struct rxrpc_kernel_ops afs_rxrpc_callback_ops = { 32 .notify_new_call = afs_rx_new_call, 33 .discard_new_call = afs_rx_discard_new_call, 34 .user_attach_call = afs_rx_attach, 35 .notify_oob = afs_rx_notify_oob, 36 }; 37 38 /* asynchronous incoming call initial processing */ 39 static const struct afs_call_type afs_RXCMxxxx = { 40 .name = "CB.xxxx", 41 .deliver = afs_deliver_cm_op_id, 42 }; 43 44 /* 45 * open an RxRPC socket and bind it to be a server for callback notifications 46 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT 47 */ 48 int afs_open_socket(struct afs_net *net) 49 { 50 struct sockaddr_rxrpc srx; 51 struct socket *socket; 52 int ret; 53 54 _enter(""); 55 56 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); 57 if (ret < 0) 58 goto error_1; 59 60 socket->sk->sk_allocation = GFP_NOFS; 61 socket->sk->sk_user_data = net; 62 63 /* bind the callback manager's address to make this a server socket */ 64 memset(&srx, 0, sizeof(srx)); 65 srx.srx_family = AF_RXRPC; 66 srx.srx_service = CM_SERVICE; 67 srx.transport_type = SOCK_DGRAM; 68 srx.transport_len = sizeof(srx.transport.sin6); 69 srx.transport.sin6.sin6_family = AF_INET6; 70 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); 71 72 ret = rxrpc_sock_set_min_security_level(socket->sk, 73 RXRPC_SECURITY_ENCRYPT); 74 if (ret < 0) 75 goto error_2; 76 77 ret = rxrpc_sock_set_manage_response(socket->sk, true); 78 if (ret < 0) 79 goto error_2; 80 81 ret = afs_create_token_key(net, socket); 82 if (ret < 0) 83 pr_err("Couldn't create RxGK CM key: %d\n", ret); 84 85 ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx)); 86 if (ret == -EADDRINUSE) { 87 srx.transport.sin6.sin6_port = 0; 88 ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx)); 89 } 90 if (ret < 0) 91 goto error_2; 92 93 srx.srx_service = YFS_CM_SERVICE; 94 ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx)); 95 if (ret < 0) 96 goto error_2; 97 98 /* Ideally, we'd turn on service upgrade here, but we can't because 99 * OpenAFS is buggy and leaks the userStatus field from packet to 100 * packet and between FS packets and CB packets - so if we try to do an 101 * upgrade on an FS packet, OpenAFS will leak that into the CB packet 102 * it sends back to us. 103 */ 104 105 rxrpc_kernel_set_notifications(socket, &afs_rxrpc_callback_ops); 106 107 ret = kernel_listen(socket, INT_MAX); 108 if (ret < 0) 109 goto error_2; 110 111 net->socket = socket; 112 afs_charge_preallocation(&net->charge_preallocation_work); 113 _leave(" = 0"); 114 return 0; 115 116 error_2: 117 sock_release(socket); 118 error_1: 119 _leave(" = %d", ret); 120 return ret; 121 } 122 123 /* 124 * close the RxRPC socket AFS was using 125 */ 126 void afs_close_socket(struct afs_net *net) 127 { 128 _enter(""); 129 130 cancel_work_sync(&net->charge_preallocation_work); 131 cancel_work_sync(&net->rx_oob_work); 132 /* Future work items should now see ->live is false. */ 133 134 kernel_listen(net->socket, 0); 135 136 /* Make sure work items are no longer running. */ 137 flush_workqueue(afs_async_calls); 138 cancel_work_sync(&net->charge_preallocation_work); 139 140 if (net->spare_incoming_call) { 141 afs_put_call(net->spare_incoming_call); 142 net->spare_incoming_call = NULL; 143 } 144 145 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls)); 146 wait_var_event(&net->nr_outstanding_calls, 147 !atomic_read(&net->nr_outstanding_calls)); 148 _debug("no outstanding calls"); 149 150 kernel_sock_shutdown(net->socket, SHUT_RDWR); 151 flush_workqueue(afs_async_calls); 152 cancel_work_sync(&net->rx_oob_work); 153 net->socket->sk->sk_user_data = NULL; 154 sock_release(net->socket); 155 key_put(net->fs_cm_token_key); 156 157 _debug("dework"); 158 _leave(""); 159 } 160 161 /* 162 * Allocate a call. 163 */ 164 static struct afs_call *afs_alloc_call(struct afs_net *net, 165 const struct afs_call_type *type, 166 gfp_t gfp) 167 { 168 struct afs_call *call; 169 int o; 170 171 call = kzalloc_obj(*call, gfp); 172 if (!call) 173 return NULL; 174 175 call->type = type; 176 call->net = net; 177 call->debug_id = atomic_inc_return(&rxrpc_debug_id); 178 refcount_set(&call->ref, 1); 179 INIT_WORK(&call->async_work, type->async_rx ?: afs_process_async_call); 180 INIT_WORK(&call->work, call->type->work); 181 INIT_WORK(&call->free_work, afs_deferred_free_worker); 182 init_waitqueue_head(&call->waitq); 183 spin_lock_init(&call->state_lock); 184 call->iter = &call->def_iter; 185 186 o = atomic_inc_return(&net->nr_outstanding_calls); 187 trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o, 188 __builtin_return_address(0)); 189 return call; 190 } 191 192 static void afs_free_call(struct afs_call *call) 193 { 194 struct afs_net *net = call->net; 195 int o; 196 197 ASSERT(!work_pending(&call->async_work)); 198 199 rxrpc_kernel_put_peer(call->peer); 200 201 if (call->rxcall) { 202 rxrpc_kernel_shutdown_call(net->socket, call->rxcall); 203 rxrpc_kernel_put_call(net->socket, call->rxcall); 204 call->rxcall = NULL; 205 } 206 if (call->type->destructor) 207 call->type->destructor(call); 208 209 afs_unuse_server_notime(call->net, call->server, afs_server_trace_unuse_call); 210 kfree(call->request); 211 212 o = atomic_read(&net->nr_outstanding_calls); 213 trace_afs_call(call->debug_id, afs_call_trace_free, 0, o, 214 __builtin_return_address(0)); 215 kfree(call); 216 217 o = atomic_dec_return(&net->nr_outstanding_calls); 218 if (o == 0) 219 wake_up_var(&net->nr_outstanding_calls); 220 } 221 222 /* 223 * Dispose of a reference on a call. 224 */ 225 void afs_put_call(struct afs_call *call) 226 { 227 struct afs_net *net = call->net; 228 unsigned int debug_id = call->debug_id; 229 bool zero; 230 int r, o; 231 232 zero = __refcount_dec_and_test(&call->ref, &r); 233 o = atomic_read(&net->nr_outstanding_calls); 234 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o, 235 __builtin_return_address(0)); 236 if (zero) 237 afs_free_call(call); 238 } 239 240 static void afs_deferred_free_worker(struct work_struct *work) 241 { 242 struct afs_call *call = container_of(work, struct afs_call, free_work); 243 244 afs_free_call(call); 245 } 246 247 /* 248 * Dispose of a reference on a call, deferring the cleanup to a workqueue 249 * to avoid lock recursion. 250 */ 251 void afs_deferred_put_call(struct afs_call *call) 252 { 253 struct afs_net *net = call->net; 254 unsigned int debug_id = call->debug_id; 255 bool zero; 256 int r, o; 257 258 zero = __refcount_dec_and_test(&call->ref, &r); 259 o = atomic_read(&net->nr_outstanding_calls); 260 trace_afs_call(debug_id, afs_call_trace_put, r - 1, o, 261 __builtin_return_address(0)); 262 if (zero) 263 schedule_work(&call->free_work); 264 } 265 266 /* 267 * Queue the call for actual work. 268 */ 269 static void afs_queue_call_work(struct afs_call *call) 270 { 271 if (call->type->work) { 272 afs_get_call(call, afs_call_trace_work); 273 if (!queue_work(afs_wq, &call->work)) 274 afs_put_call(call); 275 } 276 } 277 278 /* 279 * allocate a call with flat request and reply buffers 280 */ 281 struct afs_call *afs_alloc_flat_call(struct afs_net *net, 282 const struct afs_call_type *type, 283 size_t request_size, size_t reply_max) 284 { 285 struct afs_call *call; 286 287 call = afs_alloc_call(net, type, GFP_NOFS); 288 if (!call) 289 goto nomem_call; 290 291 if (request_size) { 292 call->request_size = request_size; 293 call->request = kmalloc(request_size, GFP_NOFS); 294 if (!call->request) 295 goto nomem_free; 296 } 297 298 if (reply_max) { 299 call->reply_max = reply_max; 300 call->buffer = kmalloc(reply_max, GFP_NOFS); 301 if (!call->buffer) 302 goto nomem_free; 303 } 304 305 afs_extract_to_buf(call, call->reply_max); 306 call->operation_ID = type->op; 307 init_waitqueue_head(&call->waitq); 308 return call; 309 310 nomem_free: 311 afs_put_call(call); 312 nomem_call: 313 return NULL; 314 } 315 316 /* 317 * clean up a call with flat buffer 318 */ 319 void afs_flat_call_destructor(struct afs_call *call) 320 { 321 _enter(""); 322 323 kfree(call->request); 324 call->request = NULL; 325 kfree(call->buffer); 326 call->buffer = NULL; 327 } 328 329 /* 330 * Advance the AFS call state when the RxRPC call ends the transmit phase. 331 */ 332 static void afs_notify_end_request_tx(struct sock *sock, 333 struct rxrpc_call *rxcall, 334 unsigned long call_user_ID) 335 { 336 struct afs_call *call = (struct afs_call *)call_user_ID; 337 338 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY); 339 } 340 341 /* 342 * Initiate a call and synchronously queue up the parameters for dispatch. Any 343 * error is stored into the call struct, which the caller must check for. 344 */ 345 void afs_make_call(struct afs_call *call, gfp_t gfp) 346 { 347 struct rxrpc_call *rxcall; 348 struct msghdr msg; 349 struct kvec iov[1]; 350 size_t len; 351 s64 tx_total_len; 352 int ret; 353 354 _enter(",{%pISp+%u},", rxrpc_kernel_remote_addr(call->peer), call->service_id); 355 356 ASSERT(call->type != NULL); 357 ASSERT(call->type->name != NULL); 358 359 _debug("____MAKE %p{%s,%x} [%d]____", 360 call, call->type->name, key_serial(call->key), 361 atomic_read(&call->net->nr_outstanding_calls)); 362 363 trace_afs_make_call(call); 364 365 /* Work out the length we're going to transmit. This is awkward for 366 * calls such as FS.StoreData where there's an extra injection of data 367 * after the initial fixed part. 368 */ 369 tx_total_len = call->request_size; 370 if (call->write_iter) 371 tx_total_len += iov_iter_count(call->write_iter); 372 373 /* If the call is going to be asynchronous, we need an extra ref for 374 * the call to hold itself so the caller need not hang on to its ref. 375 */ 376 if (call->async) { 377 afs_get_call(call, afs_call_trace_get); 378 call->drop_ref = true; 379 } 380 381 /* create a call */ 382 rxcall = rxrpc_kernel_begin_call(call->net->socket, call->peer, call->key, 383 (unsigned long)call, 384 tx_total_len, 385 call->max_lifespan, 386 gfp, 387 (call->async ? 388 afs_wake_up_async_call : 389 afs_wake_up_call_waiter), 390 call->service_id, 391 call->upgrade, 392 (call->intr ? RXRPC_PREINTERRUPTIBLE : 393 RXRPC_UNINTERRUPTIBLE), 394 call->debug_id); 395 if (IS_ERR(rxcall)) { 396 ret = PTR_ERR(rxcall); 397 call->error = ret; 398 goto error_kill_call; 399 } 400 401 call->rxcall = rxcall; 402 call->issue_time = ktime_get_real(); 403 404 /* send the request */ 405 iov[0].iov_base = call->request; 406 iov[0].iov_len = call->request_size; 407 408 msg.msg_name = NULL; 409 msg.msg_namelen = 0; 410 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size); 411 msg.msg_control = NULL; 412 msg.msg_controllen = 0; 413 msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0); 414 415 ret = rxrpc_kernel_send_data(call->net->socket, rxcall, 416 &msg, call->request_size, 417 afs_notify_end_request_tx); 418 if (ret < 0) 419 goto error_do_abort; 420 421 if (call->write_iter) { 422 msg.msg_iter = *call->write_iter; 423 msg.msg_flags &= ~MSG_MORE; 424 trace_afs_send_data(call, &msg); 425 426 ret = rxrpc_kernel_send_data(call->net->socket, 427 call->rxcall, &msg, 428 iov_iter_count(&msg.msg_iter), 429 afs_notify_end_request_tx); 430 *call->write_iter = msg.msg_iter; 431 432 trace_afs_sent_data(call, &msg, ret); 433 if (ret < 0) 434 goto error_do_abort; 435 } 436 437 /* Note that at this point, we may have received the reply or an abort 438 * - and an asynchronous call may already have completed. 439 * 440 * afs_wait_for_call_to_complete(call) 441 * must be called to synchronously clean up. 442 */ 443 return; 444 445 error_do_abort: 446 if (ret != -ECONNABORTED) 447 rxrpc_kernel_abort_call(call->net->socket, rxcall, 448 RX_USER_ABORT, ret, 449 afs_abort_send_data_error); 450 if (call->async) { 451 afs_see_call(call, afs_call_trace_async_abort); 452 return; 453 } 454 455 if (ret == -ECONNABORTED) { 456 len = 0; 457 iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0); 458 rxrpc_kernel_recv_data(call->net->socket, rxcall, 459 &msg.msg_iter, &len, false, 460 &call->abort_code, &call->service_id); 461 call->responded = true; 462 } 463 call->error = ret; 464 trace_afs_call_done(call); 465 error_kill_call: 466 if (call->async) 467 afs_see_call(call, afs_call_trace_async_kill); 468 if (call->type->immediate_cancel) 469 call->type->immediate_cancel(call); 470 471 /* We need to dispose of the extra ref we grabbed for an async call. 472 * The call, however, might be queued on afs_async_calls and we need to 473 * make sure we don't get any more notifications that might requeue it. 474 */ 475 if (call->rxcall) 476 rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall); 477 if (call->async) { 478 if (cancel_work_sync(&call->async_work)) 479 afs_put_call(call); 480 afs_set_call_complete(call, ret, 0); 481 } 482 483 call->error = ret; 484 call->state = AFS_CALL_COMPLETE; 485 _leave(" = %d", ret); 486 } 487 488 /* 489 * Log remote abort codes that indicate that we have a protocol disagreement 490 * with the server. 491 */ 492 static void afs_log_error(struct afs_call *call, s32 remote_abort) 493 { 494 static int max = 0; 495 const char *msg; 496 int m; 497 498 switch (remote_abort) { 499 case RX_EOF: msg = "unexpected EOF"; break; 500 case RXGEN_CC_MARSHAL: msg = "client marshalling"; break; 501 case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break; 502 case RXGEN_SS_MARSHAL: msg = "server marshalling"; break; 503 case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break; 504 case RXGEN_DECODE: msg = "opcode decode"; break; 505 case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break; 506 case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break; 507 case -32: msg = "insufficient data"; break; 508 default: 509 return; 510 } 511 512 m = max; 513 if (m < 3) { 514 max = m + 1; 515 pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n", 516 msg, call->type->name, 517 rxrpc_kernel_remote_addr(call->peer)); 518 } 519 } 520 521 /* 522 * deliver messages to a call 523 */ 524 void afs_deliver_to_call(struct afs_call *call) 525 { 526 enum afs_call_state state; 527 size_t len; 528 u32 abort_code, remote_abort = 0; 529 int ret; 530 531 _enter("%s", call->type->name); 532 533 while (state = READ_ONCE(call->state), 534 state == AFS_CALL_CL_AWAIT_REPLY || 535 state == AFS_CALL_SV_AWAIT_OP_ID || 536 state == AFS_CALL_SV_AWAIT_REQUEST || 537 state == AFS_CALL_SV_AWAIT_ACK 538 ) { 539 if (state == AFS_CALL_SV_AWAIT_ACK) { 540 len = 0; 541 iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0); 542 ret = rxrpc_kernel_recv_data(call->net->socket, 543 call->rxcall, &call->def_iter, 544 &len, false, &remote_abort, 545 &call->service_id); 546 trace_afs_receive_data(call, &call->def_iter, false, ret); 547 548 if (ret == -EINPROGRESS || ret == -EAGAIN) 549 return; 550 if (ret < 0 || ret == 1) { 551 if (ret == 1) 552 ret = 0; 553 goto call_complete; 554 } 555 return; 556 } 557 558 ret = call->type->deliver(call); 559 state = READ_ONCE(call->state); 560 if (ret == 0 && call->unmarshalling_error) 561 ret = -EBADMSG; 562 switch (ret) { 563 case 0: 564 call->responded = true; 565 afs_queue_call_work(call); 566 if (state == AFS_CALL_CL_PROC_REPLY) { 567 if (call->op) 568 set_bit(AFS_SERVER_FL_MAY_HAVE_CB, 569 &call->op->server->flags); 570 goto call_complete; 571 } 572 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY); 573 goto done; 574 case -EINPROGRESS: 575 case -EAGAIN: 576 goto out; 577 case -ECONNABORTED: 578 ASSERTCMP(state, ==, AFS_CALL_COMPLETE); 579 call->responded = true; 580 afs_log_error(call, call->abort_code); 581 goto done; 582 case -ENOTSUPP: 583 call->responded = true; 584 abort_code = RXGEN_OPCODE; 585 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 586 abort_code, ret, 587 afs_abort_op_not_supported); 588 goto local_abort; 589 case -EIO: 590 pr_err("kAFS: Call %u in bad state %u\n", 591 call->debug_id, state); 592 fallthrough; 593 case -ENODATA: 594 case -EBADMSG: 595 case -EMSGSIZE: 596 case -ENOMEM: 597 case -EFAULT: 598 abort_code = RXGEN_CC_UNMARSHAL; 599 if (state != AFS_CALL_CL_AWAIT_REPLY) 600 abort_code = RXGEN_SS_UNMARSHAL; 601 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 602 abort_code, ret, 603 afs_abort_unmarshal_error); 604 goto local_abort; 605 default: 606 abort_code = RX_CALL_DEAD; 607 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 608 abort_code, ret, 609 afs_abort_general_error); 610 goto local_abort; 611 } 612 } 613 614 done: 615 if (call->type->done) 616 call->type->done(call); 617 out: 618 _leave(""); 619 return; 620 621 local_abort: 622 abort_code = 0; 623 call_complete: 624 afs_set_call_complete(call, ret, remote_abort); 625 goto done; 626 } 627 628 /* 629 * Wait synchronously for a call to complete. 630 */ 631 void afs_wait_for_call_to_complete(struct afs_call *call) 632 { 633 bool rxrpc_complete = false; 634 635 _enter(""); 636 637 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { 638 DECLARE_WAITQUEUE(myself, current); 639 640 add_wait_queue(&call->waitq, &myself); 641 for (;;) { 642 set_current_state(TASK_UNINTERRUPTIBLE); 643 644 /* deliver any messages that are in the queue */ 645 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) && 646 call->need_attention) { 647 call->need_attention = false; 648 __set_current_state(TASK_RUNNING); 649 afs_deliver_to_call(call); 650 continue; 651 } 652 653 if (afs_check_call_state(call, AFS_CALL_COMPLETE)) 654 break; 655 656 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) { 657 /* rxrpc terminated the call. */ 658 rxrpc_complete = true; 659 break; 660 } 661 662 schedule(); 663 } 664 665 remove_wait_queue(&call->waitq, &myself); 666 __set_current_state(TASK_RUNNING); 667 } 668 669 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { 670 if (rxrpc_complete) { 671 afs_set_call_complete(call, call->error, call->abort_code); 672 } else { 673 /* Kill off the call if it's still live. */ 674 _debug("call interrupted"); 675 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 676 RX_USER_ABORT, -EINTR, 677 afs_abort_interrupted)) 678 afs_set_call_complete(call, -EINTR, 0); 679 } 680 } 681 } 682 683 /* 684 * wake up a waiting call 685 */ 686 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, 687 unsigned long call_user_ID) 688 { 689 struct afs_call *call = (struct afs_call *)call_user_ID; 690 691 call->need_attention = true; 692 wake_up(&call->waitq); 693 } 694 695 /* 696 * Wake up an asynchronous call. The caller is holding the call notify 697 * spinlock around this, so we can't call afs_put_call(). 698 */ 699 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, 700 unsigned long call_user_ID) 701 { 702 struct afs_call *call = (struct afs_call *)call_user_ID; 703 int r; 704 705 trace_afs_notify_call(rxcall, call); 706 call->need_attention = true; 707 708 if (__refcount_inc_not_zero(&call->ref, &r)) { 709 trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1, 710 atomic_read(&call->net->nr_outstanding_calls), 711 __builtin_return_address(0)); 712 713 if (!queue_work(afs_async_calls, &call->async_work)) 714 afs_deferred_put_call(call); 715 } 716 } 717 718 /* 719 * Perform I/O processing on an asynchronous call. The work item carries a ref 720 * to the call struct that we either need to release or to pass on. 721 */ 722 static void afs_process_async_call(struct work_struct *work) 723 { 724 struct afs_call *call = container_of(work, struct afs_call, async_work); 725 726 _enter(""); 727 728 if (call->state < AFS_CALL_COMPLETE && call->need_attention) { 729 call->need_attention = false; 730 afs_deliver_to_call(call); 731 } 732 733 afs_put_call(call); 734 _leave(""); 735 } 736 737 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) 738 { 739 struct afs_call *call = (struct afs_call *)user_call_ID; 740 741 call->rxcall = rxcall; 742 } 743 744 /* 745 * Charge the incoming call preallocation. 746 */ 747 void afs_charge_preallocation(struct work_struct *work) 748 { 749 struct afs_net *net = 750 container_of(work, struct afs_net, charge_preallocation_work); 751 struct afs_call *call = net->spare_incoming_call; 752 753 while (READ_ONCE(net->live)) { 754 if (!call) { 755 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); 756 if (!call) 757 break; 758 759 call->drop_ref = true; 760 call->async = true; 761 call->state = AFS_CALL_SV_AWAIT_OP_ID; 762 init_waitqueue_head(&call->waitq); 763 afs_extract_to_tmp(call); 764 } 765 766 if (rxrpc_kernel_charge_accept(net->socket, 767 afs_wake_up_async_call, 768 (unsigned long)call, 769 GFP_KERNEL, 770 call->debug_id) < 0) 771 break; 772 call = NULL; 773 } 774 net->spare_incoming_call = call; 775 } 776 777 /* 778 * Discard a preallocated call when a socket is shut down. 779 */ 780 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, 781 unsigned long user_call_ID) 782 { 783 struct afs_call *call = (struct afs_call *)user_call_ID; 784 785 call->rxcall = NULL; 786 afs_put_call(call); 787 } 788 789 /* 790 * Notification of an incoming call. 791 */ 792 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, 793 unsigned long user_call_ID) 794 { 795 struct afs_call *call = (struct afs_call *)user_call_ID; 796 struct afs_net *net = afs_sock2net(sk); 797 798 call->peer = rxrpc_kernel_get_call_peer(sk->sk_socket, call->rxcall); 799 call->server = afs_find_server(call->peer); 800 if (!call->server) 801 trace_afs_cm_no_server(call, rxrpc_kernel_remote_srx(call->peer)); 802 803 if (net->live) 804 queue_work(afs_wq, &net->charge_preallocation_work); 805 } 806 807 /* 808 * Grab the operation ID from an incoming cache manager call. The socket 809 * buffer is discarded on error or if we don't yet have sufficient data. 810 */ 811 static int afs_deliver_cm_op_id(struct afs_call *call) 812 { 813 int ret; 814 815 _enter("{%zu}", iov_iter_count(call->iter)); 816 817 /* the operation ID forms the first four bytes of the request data */ 818 ret = afs_extract_data(call, true); 819 if (ret < 0) 820 return ret; 821 822 call->operation_ID = ntohl(call->tmp); 823 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST); 824 825 /* ask the cache manager to route the call (it'll change the call type 826 * if successful) */ 827 if (!afs_cm_incoming_call(call)) 828 return -ENOTSUPP; 829 830 call->security_ix = rxrpc_kernel_query_call_security(call->rxcall, 831 &call->service_id, 832 &call->enctype); 833 834 trace_afs_cb_call(call); 835 call->work.func = call->type->work; 836 837 /* pass responsibility for the remainder of this message off to the 838 * cache manager op */ 839 return call->type->deliver(call); 840 } 841 842 /* 843 * Advance the AFS call state when an RxRPC service call ends the transmit 844 * phase. 845 */ 846 static void afs_notify_end_reply_tx(struct sock *sock, 847 struct rxrpc_call *rxcall, 848 unsigned long call_user_ID) 849 { 850 struct afs_call *call = (struct afs_call *)call_user_ID; 851 852 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK); 853 } 854 855 /* 856 * send an empty reply 857 */ 858 void afs_send_empty_reply(struct afs_call *call) 859 { 860 struct afs_net *net = call->net; 861 struct msghdr msg; 862 863 _enter(""); 864 865 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); 866 867 msg.msg_name = NULL; 868 msg.msg_namelen = 0; 869 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0); 870 msg.msg_control = NULL; 871 msg.msg_controllen = 0; 872 msg.msg_flags = 0; 873 874 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, 875 afs_notify_end_reply_tx)) { 876 case 0: 877 _leave(" [replied]"); 878 return; 879 880 case -ENOMEM: 881 _debug("oom"); 882 rxrpc_kernel_abort_call(net->socket, call->rxcall, 883 RXGEN_SS_MARSHAL, -ENOMEM, 884 afs_abort_oom); 885 fallthrough; 886 default: 887 _leave(" [error]"); 888 return; 889 } 890 } 891 892 /* 893 * send a simple reply 894 */ 895 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) 896 { 897 struct afs_net *net = call->net; 898 struct msghdr msg; 899 struct kvec iov[1]; 900 int n; 901 902 _enter(""); 903 904 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); 905 906 iov[0].iov_base = (void *) buf; 907 iov[0].iov_len = len; 908 msg.msg_name = NULL; 909 msg.msg_namelen = 0; 910 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len); 911 msg.msg_control = NULL; 912 msg.msg_controllen = 0; 913 msg.msg_flags = 0; 914 915 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, 916 afs_notify_end_reply_tx); 917 if (n >= 0) { 918 /* Success */ 919 _leave(" [replied]"); 920 return; 921 } 922 923 if (n == -ENOMEM) { 924 _debug("oom"); 925 rxrpc_kernel_abort_call(net->socket, call->rxcall, 926 RXGEN_SS_MARSHAL, -ENOMEM, 927 afs_abort_oom); 928 } 929 _leave(" [error]"); 930 } 931 932 /* 933 * Extract a piece of data from the received data socket buffers. 934 */ 935 int afs_extract_data(struct afs_call *call, bool want_more) 936 { 937 struct afs_net *net = call->net; 938 struct iov_iter *iter = call->iter; 939 enum afs_call_state state; 940 u32 remote_abort = 0; 941 int ret; 942 943 _enter("{%s,%zu,%zu},%d", 944 call->type->name, call->iov_len, iov_iter_count(iter), want_more); 945 946 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter, 947 &call->iov_len, want_more, &remote_abort, 948 &call->service_id); 949 trace_afs_receive_data(call, call->iter, want_more, ret); 950 if (ret == 0 || ret == -EAGAIN) 951 return ret; 952 953 state = READ_ONCE(call->state); 954 if (ret == 1) { 955 switch (state) { 956 case AFS_CALL_CL_AWAIT_REPLY: 957 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY); 958 break; 959 case AFS_CALL_SV_AWAIT_REQUEST: 960 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING); 961 break; 962 case AFS_CALL_COMPLETE: 963 kdebug("prem complete %d", call->error); 964 return afs_io_error(call, afs_io_error_extract); 965 default: 966 break; 967 } 968 return 0; 969 } 970 971 afs_set_call_complete(call, ret, remote_abort); 972 return ret; 973 } 974 975 /* 976 * Log protocol error production. 977 */ 978 noinline int afs_protocol_error(struct afs_call *call, 979 enum afs_eproto_cause cause) 980 { 981 trace_afs_protocol_error(call, cause); 982 if (call) 983 call->unmarshalling_error = true; 984 return -EBADMSG; 985 } 986 987 /* 988 * Wake up OOB notification processing. 989 */ 990 static void afs_rx_notify_oob(struct sock *sk, struct sk_buff *oob) 991 { 992 struct afs_net *net = sk->sk_user_data; 993 994 if (READ_ONCE(net->live)) 995 queue_work(afs_wq, &net->rx_oob_work); 996 } 997