| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 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 | |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sched/signal.h> |
| 14 | |
| 15 | #include <net/sock.h> |
| 16 | #include <net/af_rxrpc.h> |
| 17 | #include "internal.h" |
| 18 | #include "afs_cm.h" |
| 19 | |
| 20 | struct workqueue_struct *afs_async_calls; |
| 21 | |
| 22 | static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long); |
| 23 | static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *); |
| 24 | static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long); |
| 25 | static void afs_process_async_call(struct work_struct *); |
| 26 | static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long); |
| 27 | static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long); |
| 28 | static int afs_deliver_cm_op_id(struct afs_call *); |
| 29 | |
| 30 | /* asynchronous incoming call initial processing */ |
| 31 | static const struct afs_call_type afs_RXCMxxxx = { |
| 32 | .name = "CB.xxxx", |
| 33 | .deliver = afs_deliver_cm_op_id, |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * open an RxRPC socket and bind it to be a server for callback notifications |
| 38 | * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT |
| 39 | */ |
| 40 | int afs_open_socket(struct afs_net *net) |
| 41 | { |
| 42 | struct sockaddr_rxrpc srx; |
| 43 | struct socket *socket; |
| 44 | unsigned int min_level; |
| 45 | int ret; |
| 46 | |
| 47 | _enter(""); |
| 48 | |
| 49 | ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); |
| 50 | if (ret < 0) |
| 51 | goto error_1; |
| 52 | |
| 53 | socket->sk->sk_allocation = GFP_NOFS; |
| 54 | |
| 55 | /* bind the callback manager's address to make this a server socket */ |
| 56 | memset(&srx, 0, sizeof(srx)); |
| 57 | srx.srx_family = AF_RXRPC; |
| 58 | srx.srx_service = CM_SERVICE; |
| 59 | srx.transport_type = SOCK_DGRAM; |
| 60 | srx.transport_len = sizeof(srx.transport.sin6); |
| 61 | srx.transport.sin6.sin6_family = AF_INET6; |
| 62 | srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); |
| 63 | |
| 64 | min_level = RXRPC_SECURITY_ENCRYPT; |
| 65 | ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL, |
| 66 | (void *)&min_level, sizeof(min_level)); |
| 67 | if (ret < 0) |
| 68 | goto error_2; |
| 69 | |
| 70 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
| 71 | if (ret == -EADDRINUSE) { |
| 72 | srx.transport.sin6.sin6_port = 0; |
| 73 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
| 74 | } |
| 75 | if (ret < 0) |
| 76 | goto error_2; |
| 77 | |
| 78 | rxrpc_kernel_new_call_notification(socket, afs_rx_new_call, |
| 79 | afs_rx_discard_new_call); |
| 80 | |
| 81 | ret = kernel_listen(socket, INT_MAX); |
| 82 | if (ret < 0) |
| 83 | goto error_2; |
| 84 | |
| 85 | net->socket = socket; |
| 86 | afs_charge_preallocation(&net->charge_preallocation_work); |
| 87 | _leave(" = 0"); |
| 88 | return 0; |
| 89 | |
| 90 | error_2: |
| 91 | sock_release(socket); |
| 92 | error_1: |
| 93 | _leave(" = %d", ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * close the RxRPC socket AFS was using |
| 99 | */ |
| 100 | void afs_close_socket(struct afs_net *net) |
| 101 | { |
| 102 | _enter(""); |
| 103 | |
| 104 | kernel_listen(net->socket, 0); |
| 105 | flush_workqueue(afs_async_calls); |
| 106 | |
| 107 | if (net->spare_incoming_call) { |
| 108 | afs_put_call(net->spare_incoming_call); |
| 109 | net->spare_incoming_call = NULL; |
| 110 | } |
| 111 | |
| 112 | _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls)); |
| 113 | wait_var_event(&net->nr_outstanding_calls, |
| 114 | !atomic_read(&net->nr_outstanding_calls)); |
| 115 | _debug("no outstanding calls"); |
| 116 | |
| 117 | kernel_sock_shutdown(net->socket, SHUT_RDWR); |
| 118 | flush_workqueue(afs_async_calls); |
| 119 | sock_release(net->socket); |
| 120 | |
| 121 | _debug("dework"); |
| 122 | _leave(""); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Allocate a call. |
| 127 | */ |
| 128 | static struct afs_call *afs_alloc_call(struct afs_net *net, |
| 129 | const struct afs_call_type *type, |
| 130 | gfp_t gfp) |
| 131 | { |
| 132 | struct afs_call *call; |
| 133 | int o; |
| 134 | |
| 135 | call = kzalloc(sizeof(*call), gfp); |
| 136 | if (!call) |
| 137 | return NULL; |
| 138 | |
| 139 | call->type = type; |
| 140 | call->net = net; |
| 141 | call->debug_id = atomic_inc_return(&rxrpc_debug_id); |
| 142 | atomic_set(&call->usage, 1); |
| 143 | INIT_WORK(&call->async_work, afs_process_async_call); |
| 144 | init_waitqueue_head(&call->waitq); |
| 145 | spin_lock_init(&call->state_lock); |
| 146 | |
| 147 | o = atomic_inc_return(&net->nr_outstanding_calls); |
| 148 | trace_afs_call(call, afs_call_trace_alloc, 1, o, |
| 149 | __builtin_return_address(0)); |
| 150 | return call; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Dispose of a reference on a call. |
| 155 | */ |
| 156 | void afs_put_call(struct afs_call *call) |
| 157 | { |
| 158 | struct afs_net *net = call->net; |
| 159 | int n = atomic_dec_return(&call->usage); |
| 160 | int o = atomic_read(&net->nr_outstanding_calls); |
| 161 | |
| 162 | trace_afs_call(call, afs_call_trace_put, n + 1, o, |
| 163 | __builtin_return_address(0)); |
| 164 | |
| 165 | ASSERTCMP(n, >=, 0); |
| 166 | if (n == 0) { |
| 167 | ASSERT(!work_pending(&call->async_work)); |
| 168 | ASSERT(call->type->name != NULL); |
| 169 | |
| 170 | if (call->rxcall) { |
| 171 | rxrpc_kernel_end_call(net->socket, call->rxcall); |
| 172 | call->rxcall = NULL; |
| 173 | } |
| 174 | if (call->type->destructor) |
| 175 | call->type->destructor(call); |
| 176 | |
| 177 | afs_put_server(call->net, call->cm_server); |
| 178 | afs_put_cb_interest(call->net, call->cbi); |
| 179 | kfree(call->request); |
| 180 | |
| 181 | trace_afs_call(call, afs_call_trace_free, 0, o, |
| 182 | __builtin_return_address(0)); |
| 183 | kfree(call); |
| 184 | |
| 185 | o = atomic_dec_return(&net->nr_outstanding_calls); |
| 186 | if (o == 0) |
| 187 | wake_up_var(&net->nr_outstanding_calls); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Queue the call for actual work. Returns 0 unconditionally for convenience. |
| 193 | */ |
| 194 | int afs_queue_call_work(struct afs_call *call) |
| 195 | { |
| 196 | int u = atomic_inc_return(&call->usage); |
| 197 | |
| 198 | trace_afs_call(call, afs_call_trace_work, u, |
| 199 | atomic_read(&call->net->nr_outstanding_calls), |
| 200 | __builtin_return_address(0)); |
| 201 | |
| 202 | INIT_WORK(&call->work, call->type->work); |
| 203 | |
| 204 | if (!queue_work(afs_wq, &call->work)) |
| 205 | afs_put_call(call); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * allocate a call with flat request and reply buffers |
| 211 | */ |
| 212 | struct afs_call *afs_alloc_flat_call(struct afs_net *net, |
| 213 | const struct afs_call_type *type, |
| 214 | size_t request_size, size_t reply_max) |
| 215 | { |
| 216 | struct afs_call *call; |
| 217 | |
| 218 | call = afs_alloc_call(net, type, GFP_NOFS); |
| 219 | if (!call) |
| 220 | goto nomem_call; |
| 221 | |
| 222 | if (request_size) { |
| 223 | call->request_size = request_size; |
| 224 | call->request = kmalloc(request_size, GFP_NOFS); |
| 225 | if (!call->request) |
| 226 | goto nomem_free; |
| 227 | } |
| 228 | |
| 229 | if (reply_max) { |
| 230 | call->reply_max = reply_max; |
| 231 | call->buffer = kmalloc(reply_max, GFP_NOFS); |
| 232 | if (!call->buffer) |
| 233 | goto nomem_free; |
| 234 | } |
| 235 | |
| 236 | call->operation_ID = type->op; |
| 237 | init_waitqueue_head(&call->waitq); |
| 238 | return call; |
| 239 | |
| 240 | nomem_free: |
| 241 | afs_put_call(call); |
| 242 | nomem_call: |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * clean up a call with flat buffer |
| 248 | */ |
| 249 | void afs_flat_call_destructor(struct afs_call *call) |
| 250 | { |
| 251 | _enter(""); |
| 252 | |
| 253 | kfree(call->request); |
| 254 | call->request = NULL; |
| 255 | kfree(call->buffer); |
| 256 | call->buffer = NULL; |
| 257 | } |
| 258 | |
| 259 | #define AFS_BVEC_MAX 8 |
| 260 | |
| 261 | /* |
| 262 | * Load the given bvec with the next few pages. |
| 263 | */ |
| 264 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, |
| 265 | struct bio_vec *bv, pgoff_t first, pgoff_t last, |
| 266 | unsigned offset) |
| 267 | { |
| 268 | struct page *pages[AFS_BVEC_MAX]; |
| 269 | unsigned int nr, n, i, to, bytes = 0; |
| 270 | |
| 271 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); |
| 272 | n = find_get_pages_contig(call->mapping, first, nr, pages); |
| 273 | ASSERTCMP(n, ==, nr); |
| 274 | |
| 275 | msg->msg_flags |= MSG_MORE; |
| 276 | for (i = 0; i < nr; i++) { |
| 277 | to = PAGE_SIZE; |
| 278 | if (first + i >= last) { |
| 279 | to = call->last_to; |
| 280 | msg->msg_flags &= ~MSG_MORE; |
| 281 | } |
| 282 | bv[i].bv_page = pages[i]; |
| 283 | bv[i].bv_len = to - offset; |
| 284 | bv[i].bv_offset = offset; |
| 285 | bytes += to - offset; |
| 286 | offset = 0; |
| 287 | } |
| 288 | |
| 289 | iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Advance the AFS call state when the RxRPC call ends the transmit phase. |
| 294 | */ |
| 295 | static void afs_notify_end_request_tx(struct sock *sock, |
| 296 | struct rxrpc_call *rxcall, |
| 297 | unsigned long call_user_ID) |
| 298 | { |
| 299 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 300 | |
| 301 | afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY); |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * attach the data from a bunch of pages on an inode to a call |
| 306 | */ |
| 307 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
| 308 | { |
| 309 | struct bio_vec bv[AFS_BVEC_MAX]; |
| 310 | unsigned int bytes, nr, loop, offset; |
| 311 | pgoff_t first = call->first, last = call->last; |
| 312 | int ret; |
| 313 | |
| 314 | offset = call->first_offset; |
| 315 | call->first_offset = 0; |
| 316 | |
| 317 | do { |
| 318 | afs_load_bvec(call, msg, bv, first, last, offset); |
| 319 | trace_afs_send_pages(call, msg, first, last, offset); |
| 320 | |
| 321 | offset = 0; |
| 322 | bytes = msg->msg_iter.count; |
| 323 | nr = msg->msg_iter.nr_segs; |
| 324 | |
| 325 | ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg, |
| 326 | bytes, afs_notify_end_request_tx); |
| 327 | for (loop = 0; loop < nr; loop++) |
| 328 | put_page(bv[loop].bv_page); |
| 329 | if (ret < 0) |
| 330 | break; |
| 331 | |
| 332 | first += nr; |
| 333 | } while (first <= last); |
| 334 | |
| 335 | trace_afs_sent_pages(call, call->first, last, first, ret); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * initiate a call |
| 341 | */ |
| 342 | long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, |
| 343 | gfp_t gfp, bool async) |
| 344 | { |
| 345 | struct sockaddr_rxrpc *srx = ac->addr; |
| 346 | struct rxrpc_call *rxcall; |
| 347 | struct msghdr msg; |
| 348 | struct kvec iov[1]; |
| 349 | s64 tx_total_len; |
| 350 | int ret; |
| 351 | |
| 352 | _enter(",{%pISp},", &srx->transport); |
| 353 | |
| 354 | ASSERT(call->type != NULL); |
| 355 | ASSERT(call->type->name != NULL); |
| 356 | |
| 357 | _debug("____MAKE %p{%s,%x} [%d]____", |
| 358 | call, call->type->name, key_serial(call->key), |
| 359 | atomic_read(&call->net->nr_outstanding_calls)); |
| 360 | |
| 361 | call->async = async; |
| 362 | |
| 363 | /* Work out the length we're going to transmit. This is awkward for |
| 364 | * calls such as FS.StoreData where there's an extra injection of data |
| 365 | * after the initial fixed part. |
| 366 | */ |
| 367 | tx_total_len = call->request_size; |
| 368 | if (call->send_pages) { |
| 369 | if (call->last == call->first) { |
| 370 | tx_total_len += call->last_to - call->first_offset; |
| 371 | } else { |
| 372 | /* It looks mathematically like you should be able to |
| 373 | * combine the following lines with the ones above, but |
| 374 | * unsigned arithmetic is fun when it wraps... |
| 375 | */ |
| 376 | tx_total_len += PAGE_SIZE - call->first_offset; |
| 377 | tx_total_len += call->last_to; |
| 378 | tx_total_len += (call->last - call->first - 1) * PAGE_SIZE; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* create a call */ |
| 383 | rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key, |
| 384 | (unsigned long)call, |
| 385 | tx_total_len, gfp, |
| 386 | (async ? |
| 387 | afs_wake_up_async_call : |
| 388 | afs_wake_up_call_waiter), |
| 389 | call->upgrade, |
| 390 | call->debug_id); |
| 391 | if (IS_ERR(rxcall)) { |
| 392 | ret = PTR_ERR(rxcall); |
| 393 | goto error_kill_call; |
| 394 | } |
| 395 | |
| 396 | call->rxcall = rxcall; |
| 397 | |
| 398 | /* send the request */ |
| 399 | iov[0].iov_base = call->request; |
| 400 | iov[0].iov_len = call->request_size; |
| 401 | |
| 402 | msg.msg_name = NULL; |
| 403 | msg.msg_namelen = 0; |
| 404 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, |
| 405 | call->request_size); |
| 406 | msg.msg_control = NULL; |
| 407 | msg.msg_controllen = 0; |
| 408 | msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0); |
| 409 | |
| 410 | ret = rxrpc_kernel_send_data(call->net->socket, rxcall, |
| 411 | &msg, call->request_size, |
| 412 | afs_notify_end_request_tx); |
| 413 | if (ret < 0) |
| 414 | goto error_do_abort; |
| 415 | |
| 416 | if (call->send_pages) { |
| 417 | ret = afs_send_pages(call, &msg); |
| 418 | if (ret < 0) |
| 419 | goto error_do_abort; |
| 420 | } |
| 421 | |
| 422 | /* at this point, an async call may no longer exist as it may have |
| 423 | * already completed */ |
| 424 | if (call->async) |
| 425 | return -EINPROGRESS; |
| 426 | |
| 427 | return afs_wait_for_call_to_complete(call, ac); |
| 428 | |
| 429 | error_do_abort: |
| 430 | call->state = AFS_CALL_COMPLETE; |
| 431 | if (ret != -ECONNABORTED) { |
| 432 | rxrpc_kernel_abort_call(call->net->socket, rxcall, |
| 433 | RX_USER_ABORT, ret, "KSD"); |
| 434 | } else { |
| 435 | iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, NULL, 0, 0); |
| 436 | rxrpc_kernel_recv_data(call->net->socket, rxcall, |
| 437 | &msg.msg_iter, false, |
| 438 | &call->abort_code, &call->service_id); |
| 439 | ac->abort_code = call->abort_code; |
| 440 | ac->responded = true; |
| 441 | } |
| 442 | call->error = ret; |
| 443 | trace_afs_call_done(call); |
| 444 | error_kill_call: |
| 445 | afs_put_call(call); |
| 446 | ac->error = ret; |
| 447 | _leave(" = %d", ret); |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * deliver messages to a call |
| 453 | */ |
| 454 | static void afs_deliver_to_call(struct afs_call *call) |
| 455 | { |
| 456 | enum afs_call_state state; |
| 457 | u32 abort_code, remote_abort = 0; |
| 458 | int ret; |
| 459 | |
| 460 | _enter("%s", call->type->name); |
| 461 | |
| 462 | while (state = READ_ONCE(call->state), |
| 463 | state == AFS_CALL_CL_AWAIT_REPLY || |
| 464 | state == AFS_CALL_SV_AWAIT_OP_ID || |
| 465 | state == AFS_CALL_SV_AWAIT_REQUEST || |
| 466 | state == AFS_CALL_SV_AWAIT_ACK |
| 467 | ) { |
| 468 | if (state == AFS_CALL_SV_AWAIT_ACK) { |
| 469 | struct iov_iter iter; |
| 470 | |
| 471 | iov_iter_kvec(&iter, READ | ITER_KVEC, NULL, 0, 0); |
| 472 | ret = rxrpc_kernel_recv_data(call->net->socket, |
| 473 | call->rxcall, &iter, false, |
| 474 | &remote_abort, |
| 475 | &call->service_id); |
| 476 | trace_afs_recv_data(call, 0, 0, false, ret); |
| 477 | |
| 478 | if (ret == -EINPROGRESS || ret == -EAGAIN) |
| 479 | return; |
| 480 | if (ret < 0 || ret == 1) { |
| 481 | if (ret == 1) |
| 482 | ret = 0; |
| 483 | goto call_complete; |
| 484 | } |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | ret = call->type->deliver(call); |
| 489 | state = READ_ONCE(call->state); |
| 490 | switch (ret) { |
| 491 | case 0: |
| 492 | if (state == AFS_CALL_CL_PROC_REPLY) { |
| 493 | if (call->cbi) |
| 494 | set_bit(AFS_SERVER_FL_MAY_HAVE_CB, |
| 495 | &call->cbi->server->flags); |
| 496 | goto call_complete; |
| 497 | } |
| 498 | ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY); |
| 499 | goto done; |
| 500 | case -EINPROGRESS: |
| 501 | case -EAGAIN: |
| 502 | goto out; |
| 503 | case -ECONNABORTED: |
| 504 | ASSERTCMP(state, ==, AFS_CALL_COMPLETE); |
| 505 | goto done; |
| 506 | case -ENOTSUPP: |
| 507 | abort_code = RXGEN_OPCODE; |
| 508 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
| 509 | abort_code, ret, "KIV"); |
| 510 | goto local_abort; |
| 511 | case -EIO: |
| 512 | pr_err("kAFS: Call %u in bad state %u\n", |
| 513 | call->debug_id, state); |
| 514 | /* Fall through */ |
| 515 | case -ENODATA: |
| 516 | case -EBADMSG: |
| 517 | case -EMSGSIZE: |
| 518 | default: |
| 519 | abort_code = RXGEN_CC_UNMARSHAL; |
| 520 | if (state != AFS_CALL_CL_AWAIT_REPLY) |
| 521 | abort_code = RXGEN_SS_UNMARSHAL; |
| 522 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
| 523 | abort_code, -EBADMSG, "KUM"); |
| 524 | goto local_abort; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | done: |
| 529 | if (state == AFS_CALL_COMPLETE && call->incoming) |
| 530 | afs_put_call(call); |
| 531 | out: |
| 532 | _leave(""); |
| 533 | return; |
| 534 | |
| 535 | local_abort: |
| 536 | abort_code = 0; |
| 537 | call_complete: |
| 538 | afs_set_call_complete(call, ret, remote_abort); |
| 539 | state = AFS_CALL_COMPLETE; |
| 540 | goto done; |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * wait synchronously for a call to complete |
| 545 | */ |
| 546 | static long afs_wait_for_call_to_complete(struct afs_call *call, |
| 547 | struct afs_addr_cursor *ac) |
| 548 | { |
| 549 | signed long rtt2, timeout; |
| 550 | long ret; |
| 551 | u64 rtt; |
| 552 | u32 life, last_life; |
| 553 | |
| 554 | DECLARE_WAITQUEUE(myself, current); |
| 555 | |
| 556 | _enter(""); |
| 557 | |
| 558 | rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall); |
| 559 | rtt2 = nsecs_to_jiffies64(rtt) * 2; |
| 560 | if (rtt2 < 2) |
| 561 | rtt2 = 2; |
| 562 | |
| 563 | timeout = rtt2; |
| 564 | last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
| 565 | |
| 566 | add_wait_queue(&call->waitq, &myself); |
| 567 | for (;;) { |
| 568 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 569 | |
| 570 | /* deliver any messages that are in the queue */ |
| 571 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE) && |
| 572 | call->need_attention) { |
| 573 | call->need_attention = false; |
| 574 | __set_current_state(TASK_RUNNING); |
| 575 | afs_deliver_to_call(call); |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | if (afs_check_call_state(call, AFS_CALL_COMPLETE)) |
| 580 | break; |
| 581 | |
| 582 | life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
| 583 | if (timeout == 0 && |
| 584 | life == last_life && signal_pending(current)) |
| 585 | break; |
| 586 | |
| 587 | if (life != last_life) { |
| 588 | timeout = rtt2; |
| 589 | last_life = life; |
| 590 | } |
| 591 | |
| 592 | timeout = schedule_timeout(timeout); |
| 593 | } |
| 594 | |
| 595 | remove_wait_queue(&call->waitq, &myself); |
| 596 | __set_current_state(TASK_RUNNING); |
| 597 | |
| 598 | /* Kill off the call if it's still live. */ |
| 599 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { |
| 600 | _debug("call interrupted"); |
| 601 | if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
| 602 | RX_USER_ABORT, -EINTR, "KWI")) |
| 603 | afs_set_call_complete(call, -EINTR, 0); |
| 604 | } |
| 605 | |
| 606 | spin_lock_bh(&call->state_lock); |
| 607 | ac->abort_code = call->abort_code; |
| 608 | ac->error = call->error; |
| 609 | spin_unlock_bh(&call->state_lock); |
| 610 | |
| 611 | ret = ac->error; |
| 612 | switch (ret) { |
| 613 | case 0: |
| 614 | if (call->ret_reply0) { |
| 615 | ret = (long)call->reply[0]; |
| 616 | call->reply[0] = NULL; |
| 617 | } |
| 618 | /* Fall through */ |
| 619 | case -ECONNABORTED: |
| 620 | ac->responded = true; |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | _debug("call complete"); |
| 625 | afs_put_call(call); |
| 626 | _leave(" = %p", (void *)ret); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * wake up a waiting call |
| 632 | */ |
| 633 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
| 634 | unsigned long call_user_ID) |
| 635 | { |
| 636 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 637 | |
| 638 | call->need_attention = true; |
| 639 | wake_up(&call->waitq); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * wake up an asynchronous call |
| 644 | */ |
| 645 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
| 646 | unsigned long call_user_ID) |
| 647 | { |
| 648 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 649 | int u; |
| 650 | |
| 651 | trace_afs_notify_call(rxcall, call); |
| 652 | call->need_attention = true; |
| 653 | |
| 654 | u = atomic_fetch_add_unless(&call->usage, 1, 0); |
| 655 | if (u != 0) { |
| 656 | trace_afs_call(call, afs_call_trace_wake, u, |
| 657 | atomic_read(&call->net->nr_outstanding_calls), |
| 658 | __builtin_return_address(0)); |
| 659 | |
| 660 | if (!queue_work(afs_async_calls, &call->async_work)) |
| 661 | afs_put_call(call); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Delete an asynchronous call. The work item carries a ref to the call struct |
| 667 | * that we need to release. |
| 668 | */ |
| 669 | static void afs_delete_async_call(struct work_struct *work) |
| 670 | { |
| 671 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
| 672 | |
| 673 | _enter(""); |
| 674 | |
| 675 | afs_put_call(call); |
| 676 | |
| 677 | _leave(""); |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
| 682 | * to the call struct that we either need to release or to pass on. |
| 683 | */ |
| 684 | static void afs_process_async_call(struct work_struct *work) |
| 685 | { |
| 686 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
| 687 | |
| 688 | _enter(""); |
| 689 | |
| 690 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
| 691 | call->need_attention = false; |
| 692 | afs_deliver_to_call(call); |
| 693 | } |
| 694 | |
| 695 | if (call->state == AFS_CALL_COMPLETE) { |
| 696 | /* We have two refs to release - one from the alloc and one |
| 697 | * queued with the work item - and we can't just deallocate the |
| 698 | * call because the work item may be queued again. |
| 699 | */ |
| 700 | call->async_work.func = afs_delete_async_call; |
| 701 | if (!queue_work(afs_async_calls, &call->async_work)) |
| 702 | afs_put_call(call); |
| 703 | } |
| 704 | |
| 705 | afs_put_call(call); |
| 706 | _leave(""); |
| 707 | } |
| 708 | |
| 709 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
| 710 | { |
| 711 | struct afs_call *call = (struct afs_call *)user_call_ID; |
| 712 | |
| 713 | call->rxcall = rxcall; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Charge the incoming call preallocation. |
| 718 | */ |
| 719 | void afs_charge_preallocation(struct work_struct *work) |
| 720 | { |
| 721 | struct afs_net *net = |
| 722 | container_of(work, struct afs_net, charge_preallocation_work); |
| 723 | struct afs_call *call = net->spare_incoming_call; |
| 724 | |
| 725 | for (;;) { |
| 726 | if (!call) { |
| 727 | call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); |
| 728 | if (!call) |
| 729 | break; |
| 730 | |
| 731 | call->async = true; |
| 732 | call->state = AFS_CALL_SV_AWAIT_OP_ID; |
| 733 | init_waitqueue_head(&call->waitq); |
| 734 | } |
| 735 | |
| 736 | if (rxrpc_kernel_charge_accept(net->socket, |
| 737 | afs_wake_up_async_call, |
| 738 | afs_rx_attach, |
| 739 | (unsigned long)call, |
| 740 | GFP_KERNEL, |
| 741 | call->debug_id) < 0) |
| 742 | break; |
| 743 | call = NULL; |
| 744 | } |
| 745 | net->spare_incoming_call = call; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Discard a preallocated call when a socket is shut down. |
| 750 | */ |
| 751 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, |
| 752 | unsigned long user_call_ID) |
| 753 | { |
| 754 | struct afs_call *call = (struct afs_call *)user_call_ID; |
| 755 | |
| 756 | call->rxcall = NULL; |
| 757 | afs_put_call(call); |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Notification of an incoming call. |
| 762 | */ |
| 763 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
| 764 | unsigned long user_call_ID) |
| 765 | { |
| 766 | struct afs_net *net = afs_sock2net(sk); |
| 767 | |
| 768 | queue_work(afs_wq, &net->charge_preallocation_work); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Grab the operation ID from an incoming cache manager call. The socket |
| 773 | * buffer is discarded on error or if we don't yet have sufficient data. |
| 774 | */ |
| 775 | static int afs_deliver_cm_op_id(struct afs_call *call) |
| 776 | { |
| 777 | int ret; |
| 778 | |
| 779 | _enter("{%zu}", call->offset); |
| 780 | |
| 781 | ASSERTCMP(call->offset, <, 4); |
| 782 | |
| 783 | /* the operation ID forms the first four bytes of the request data */ |
| 784 | ret = afs_extract_data(call, &call->tmp, 4, true); |
| 785 | if (ret < 0) |
| 786 | return ret; |
| 787 | |
| 788 | call->operation_ID = ntohl(call->tmp); |
| 789 | afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST); |
| 790 | call->offset = 0; |
| 791 | |
| 792 | /* ask the cache manager to route the call (it'll change the call type |
| 793 | * if successful) */ |
| 794 | if (!afs_cm_incoming_call(call)) |
| 795 | return -ENOTSUPP; |
| 796 | |
| 797 | trace_afs_cb_call(call); |
| 798 | |
| 799 | /* pass responsibility for the remainer of this message off to the |
| 800 | * cache manager op */ |
| 801 | return call->type->deliver(call); |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Advance the AFS call state when an RxRPC service call ends the transmit |
| 806 | * phase. |
| 807 | */ |
| 808 | static void afs_notify_end_reply_tx(struct sock *sock, |
| 809 | struct rxrpc_call *rxcall, |
| 810 | unsigned long call_user_ID) |
| 811 | { |
| 812 | struct afs_call *call = (struct afs_call *)call_user_ID; |
| 813 | |
| 814 | afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK); |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * send an empty reply |
| 819 | */ |
| 820 | void afs_send_empty_reply(struct afs_call *call) |
| 821 | { |
| 822 | struct afs_net *net = call->net; |
| 823 | struct msghdr msg; |
| 824 | |
| 825 | _enter(""); |
| 826 | |
| 827 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); |
| 828 | |
| 829 | msg.msg_name = NULL; |
| 830 | msg.msg_namelen = 0; |
| 831 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0); |
| 832 | msg.msg_control = NULL; |
| 833 | msg.msg_controllen = 0; |
| 834 | msg.msg_flags = 0; |
| 835 | |
| 836 | switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, |
| 837 | afs_notify_end_reply_tx)) { |
| 838 | case 0: |
| 839 | _leave(" [replied]"); |
| 840 | return; |
| 841 | |
| 842 | case -ENOMEM: |
| 843 | _debug("oom"); |
| 844 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
| 845 | RX_USER_ABORT, -ENOMEM, "KOO"); |
| 846 | default: |
| 847 | _leave(" [error]"); |
| 848 | return; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * send a simple reply |
| 854 | */ |
| 855 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) |
| 856 | { |
| 857 | struct afs_net *net = call->net; |
| 858 | struct msghdr msg; |
| 859 | struct kvec iov[1]; |
| 860 | int n; |
| 861 | |
| 862 | _enter(""); |
| 863 | |
| 864 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); |
| 865 | |
| 866 | iov[0].iov_base = (void *) buf; |
| 867 | iov[0].iov_len = len; |
| 868 | msg.msg_name = NULL; |
| 869 | msg.msg_namelen = 0; |
| 870 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len); |
| 871 | msg.msg_control = NULL; |
| 872 | msg.msg_controllen = 0; |
| 873 | msg.msg_flags = 0; |
| 874 | |
| 875 | n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, |
| 876 | afs_notify_end_reply_tx); |
| 877 | if (n >= 0) { |
| 878 | /* Success */ |
| 879 | _leave(" [replied]"); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (n == -ENOMEM) { |
| 884 | _debug("oom"); |
| 885 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
| 886 | RX_USER_ABORT, -ENOMEM, "KOO"); |
| 887 | } |
| 888 | _leave(" [error]"); |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * Extract a piece of data from the received data socket buffers. |
| 893 | */ |
| 894 | int afs_extract_data(struct afs_call *call, void *buf, size_t count, |
| 895 | bool want_more) |
| 896 | { |
| 897 | struct afs_net *net = call->net; |
| 898 | struct iov_iter iter; |
| 899 | struct kvec iov; |
| 900 | enum afs_call_state state; |
| 901 | u32 remote_abort = 0; |
| 902 | int ret; |
| 903 | |
| 904 | _enter("{%s,%zu},,%zu,%d", |
| 905 | call->type->name, call->offset, count, want_more); |
| 906 | |
| 907 | ASSERTCMP(call->offset, <=, count); |
| 908 | |
| 909 | iov.iov_base = buf + call->offset; |
| 910 | iov.iov_len = count - call->offset; |
| 911 | iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, count - call->offset); |
| 912 | |
| 913 | ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, &iter, |
| 914 | want_more, &remote_abort, |
| 915 | &call->service_id); |
| 916 | call->offset += (count - call->offset) - iov_iter_count(&iter); |
| 917 | trace_afs_recv_data(call, count, call->offset, want_more, ret); |
| 918 | if (ret == 0 || ret == -EAGAIN) |
| 919 | return ret; |
| 920 | |
| 921 | state = READ_ONCE(call->state); |
| 922 | if (ret == 1) { |
| 923 | switch (state) { |
| 924 | case AFS_CALL_CL_AWAIT_REPLY: |
| 925 | afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY); |
| 926 | break; |
| 927 | case AFS_CALL_SV_AWAIT_REQUEST: |
| 928 | afs_set_call_state(call, state, AFS_CALL_SV_REPLYING); |
| 929 | break; |
| 930 | case AFS_CALL_COMPLETE: |
| 931 | kdebug("prem complete %d", call->error); |
| 932 | return -EIO; |
| 933 | default: |
| 934 | break; |
| 935 | } |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | afs_set_call_complete(call, ret, remote_abort); |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Log protocol error production. |
| 945 | */ |
| 946 | noinline int afs_protocol_error(struct afs_call *call, int error) |
| 947 | { |
| 948 | trace_afs_protocol_error(call, error, __builtin_return_address(0)); |
| 949 | return error; |
| 950 | } |