1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org> 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 6 7 #include <linux/mutex.h> 8 #include <linux/freezer.h> 9 #include <linux/module.h> 10 11 #include "server.h" 12 #include "smb_common.h" 13 #include "mgmt/ksmbd_ida.h" 14 #include "mgmt/user_session.h" 15 #include "connection.h" 16 #include "compress.h" 17 #include "transport_tcp.h" 18 #include "transport_rdma.h" 19 #include "misc.h" 20 21 static DEFINE_MUTEX(init_lock); 22 23 static struct ksmbd_conn_ops default_conn_ops; 24 25 DEFINE_HASHTABLE(conn_list, CONN_HASH_BITS); 26 DECLARE_RWSEM(conn_list_lock); 27 28 #ifdef CONFIG_PROC_FS 29 static struct proc_dir_entry *proc_clients; 30 31 static const char *ksmbd_conn_state_string(struct ksmbd_conn *conn) 32 { 33 switch (READ_ONCE(conn->status)) { 34 case KSMBD_SESS_NEW: 35 return "new"; 36 case KSMBD_SESS_GOOD: 37 return "good"; 38 case KSMBD_SESS_EXITING: 39 return "exiting"; 40 case KSMBD_SESS_NEED_RECONNECT: 41 return "reconnect"; 42 case KSMBD_SESS_NEED_NEGOTIATE: 43 return "negotiate"; 44 case KSMBD_SESS_NEED_SETUP: 45 return "setup"; 46 case KSMBD_SESS_RELEASING: 47 return "releasing"; 48 default: 49 return "unknown"; 50 } 51 } 52 53 static const char *ksmbd_conn_transport_string(struct ksmbd_conn *conn) 54 { 55 if (conn->transport->ops->rdma_read || conn->transport->ops->rdma_write) 56 return "smbdirect"; 57 return "tcp"; 58 } 59 60 static void proc_show_conn_feature(struct seq_file *m, bool *separator, 61 bool enabled, const char *name) 62 { 63 if (!enabled) 64 return; 65 seq_printf(m, "%s%s", *separator ? "," : "", name); 66 *separator = true; 67 } 68 69 static void proc_show_conn_features(struct seq_file *m, 70 struct ksmbd_conn *conn) 71 { 72 bool separator = false; 73 74 proc_show_conn_feature(m, &separator, 75 conn->sign || conn->signing_negotiated, "sign"); 76 proc_show_conn_feature(m, &separator, conn->cipher_type, "encrypt"); 77 proc_show_conn_feature(m, &separator, 78 conn->compress_algorithm != SMB3_COMPRESS_NONE, 79 "compress"); 80 proc_show_conn_feature(m, &separator, conn->posix_ext_supported, "posix"); 81 if (!separator) 82 seq_puts(m, "none"); 83 } 84 85 static int proc_show_clients(struct seq_file *m, void *v) 86 { 87 struct ksmbd_conn *conn; 88 struct timespec64 now, t; 89 int i; 90 91 down_read(&conn_list_lock); 92 hash_for_each(conn_list, i, conn, hlist) { 93 unsigned int outstanding_credits, total_credits; 94 unsigned long id; 95 void *entry; 96 unsigned int sessions = 0; 97 98 jiffies_to_timespec64(jiffies - conn->last_active, &t); 99 ktime_get_real_ts64(&now); 100 t = timespec64_sub(now, t); 101 102 spin_lock(&conn->credits_lock); 103 outstanding_credits = conn->outstanding_credits; 104 total_credits = conn->total_credits; 105 spin_unlock(&conn->credits_lock); 106 107 rcu_read_lock(); 108 xa_for_each(&conn->sessions, id, entry) 109 sessions++; 110 rcu_read_unlock(); 111 #if IS_ENABLED(CONFIG_IPV6) 112 if (!conn->inet_addr) 113 seq_printf(m, "client:\t%pI6c\n", &conn->inet6_addr); 114 else 115 #endif 116 seq_printf(m, "client:\t%pI4\n", &conn->inet_addr); 117 seq_printf(m, "transport:\t%s\n", ksmbd_conn_transport_string(conn)); 118 seq_printf(m, "state:\t%s\n", ksmbd_conn_state_string(conn)); 119 seq_printf(m, "dialect:\t0x%04x\n", conn->dialect); 120 seq_printf(m, "credits:\t%u/%u\n", outstanding_credits, 121 total_credits); 122 seq_printf(m, "sessions:\t%u\n", sessions); 123 seq_printf(m, "open_files:\t%d\n", 124 atomic_read(&conn->stats.open_files_count)); 125 seq_printf(m, "requests:\t%lld\n", 126 atomic64_read(&conn->stats.request_served)); 127 seq_puts(m, "features:\t"); 128 proc_show_conn_features(m, conn); 129 seq_printf(m, "\nlast_active:\t%ptT\n\n", &t); 130 } 131 up_read(&conn_list_lock); 132 return 0; 133 } 134 135 static int create_proc_clients(void) 136 { 137 proc_clients = ksmbd_proc_create("clients", 138 proc_show_clients, NULL); 139 if (!proc_clients) 140 return -ENOMEM; 141 return 0; 142 } 143 144 static void delete_proc_clients(void) 145 { 146 if (proc_clients) { 147 proc_remove(proc_clients); 148 proc_clients = NULL; 149 } 150 } 151 #else 152 static int create_proc_clients(void) { return 0; } 153 static void delete_proc_clients(void) {} 154 #endif 155 156 static struct workqueue_struct *ksmbd_conn_wq; 157 158 int ksmbd_conn_wq_init(void) 159 { 160 ksmbd_conn_wq = alloc_workqueue("ksmbd-conn-release", 161 WQ_UNBOUND | WQ_MEM_RECLAIM, 0); 162 if (!ksmbd_conn_wq) 163 return -ENOMEM; 164 return 0; 165 } 166 167 void ksmbd_conn_wq_destroy(void) 168 { 169 if (ksmbd_conn_wq) { 170 destroy_workqueue(ksmbd_conn_wq); 171 ksmbd_conn_wq = NULL; 172 } 173 } 174 175 /* 176 * __ksmbd_conn_release_work() - perform the final, once-per-struct cleanup 177 * of a ksmbd_conn whose refcount has just dropped to zero. 178 * 179 * This is the common release path used by ksmbd_conn_put() for the embedded 180 * state that outlives the connection thread: async_ida and the attached 181 * transport (which owns the socket and iov for TCP). Called from a workqueue 182 * so that sleep-allowed teardown (sock_release -> tcp_close -> 183 * lock_sock_nested) never runs from an RCU softirq callback (free_opinfo_rcu) 184 * or any other non-sleeping putter context. 185 */ 186 static void __ksmbd_conn_release_work(struct work_struct *work) 187 { 188 struct ksmbd_conn *conn = 189 container_of(work, struct ksmbd_conn, release_work); 190 191 ida_destroy(&conn->async_ida); 192 conn->transport->ops->free_transport(conn->transport); 193 kfree_sensitive(conn); 194 } 195 196 /** 197 * ksmbd_conn_get() - take a reference on @conn and return it. 198 * 199 * @conn: connection instance to get a reference to 200 * 201 * Returns @conn unchanged so callers can write 202 * "fp->conn = ksmbd_conn_get(work->conn);" in one expression. Returns NULL 203 * if @conn is NULL. 204 */ 205 struct ksmbd_conn *ksmbd_conn_get(struct ksmbd_conn *conn) 206 { 207 if (!conn) 208 return NULL; 209 210 atomic_inc(&conn->refcnt); 211 return conn; 212 } 213 214 /** 215 * ksmbd_conn_put() - drop a reference and, if it was the last, queue the 216 * release onto ksmbd_conn_wq so it runs from process context. 217 * 218 * @conn: connection instance to put a reference to 219 * 220 * Callable from any context including RCU softirq callbacks and non-sleeping 221 * locks; the actual release is deferred to the workqueue. ksmbd_conn_wq is 222 * created in ksmbd_server_init() before any conn can be allocated and is 223 * destroyed in ksmbd_server_exit() after rcu_barrier(), so it is always 224 * non-NULL while a conn reference is held. 225 */ 226 void ksmbd_conn_put(struct ksmbd_conn *conn) 227 { 228 if (!conn) 229 return; 230 231 if (atomic_dec_and_test(&conn->refcnt)) 232 queue_work(ksmbd_conn_wq, &conn->release_work); 233 } 234 235 /** 236 * ksmbd_conn_free() - free resources of the connection instance 237 * 238 * @conn: connection instance to be cleaned up 239 * 240 * During the thread termination, the corresponding conn instance 241 * resources(sock/memory) are released and finally the conn object is freed. 242 */ 243 void ksmbd_conn_free(struct ksmbd_conn *conn) 244 { 245 down_write(&conn_list_lock); 246 hash_del(&conn->hlist); 247 up_write(&conn_list_lock); 248 249 /* 250 * request_buf / preauth_info / mechToken are only ever accessed by the 251 * connection handler thread that owns @conn. ksmbd_conn_free() is 252 * called from the transport free_transport() path when that thread is 253 * exiting, so it is safe to release them unconditionally even when 254 * ksmbd_conn_put() below is not the final putter (oplock / ksmbd_file 255 * holders only retain the conn pointer, not these per-thread buffers). 256 */ 257 xa_destroy(&conn->sessions); 258 kvfree(conn->request_buf); 259 kfree_sensitive(conn->preauth_info); 260 kfree(conn->mechToken); 261 ksmbd_preauth_session_destroy(conn); 262 ksmbd_conn_put(conn); 263 } 264 265 /** 266 * ksmbd_conn_alloc() - initialize a new connection instance 267 * 268 * Return: ksmbd_conn struct on success, otherwise NULL 269 */ 270 struct ksmbd_conn *ksmbd_conn_alloc(void) 271 { 272 struct ksmbd_conn *conn; 273 274 conn = kzalloc_obj(struct ksmbd_conn, KSMBD_DEFAULT_GFP); 275 if (!conn) 276 return NULL; 277 278 conn->need_neg = true; 279 ksmbd_conn_set_new(conn); 280 conn->local_nls = load_nls("utf8"); 281 if (!conn->local_nls) 282 conn->local_nls = load_nls_default(); 283 if (IS_ENABLED(CONFIG_UNICODE)) 284 conn->um = utf8_load(UNICODE_AGE(12, 1, 0)); 285 else 286 conn->um = ERR_PTR(-EOPNOTSUPP); 287 if (IS_ERR(conn->um)) 288 conn->um = NULL; 289 INIT_WORK(&conn->release_work, __ksmbd_conn_release_work); 290 atomic_set(&conn->req_running, 0); 291 atomic_set(&conn->r_count, 0); 292 atomic_set(&conn->refcnt, 1); 293 conn->total_credits = 1; 294 conn->outstanding_credits = 0; 295 296 /* 297 * The command sequence window starts as the set { 0 } when the 298 * connection is established. 299 */ 300 conn->seq_low = 0; 301 conn->seq_high = 1; 302 __set_bit(0, conn->seq_bitmap); 303 304 init_waitqueue_head(&conn->req_running_q); 305 init_waitqueue_head(&conn->r_count_q); 306 INIT_LIST_HEAD(&conn->requests); 307 INIT_LIST_HEAD(&conn->async_requests); 308 INIT_LIST_HEAD(&conn->preauth_sess_table); 309 spin_lock_init(&conn->request_lock); 310 spin_lock_init(&conn->credits_lock); 311 ida_init(&conn->async_ida); 312 xa_init(&conn->sessions); 313 314 spin_lock_init(&conn->llist_lock); 315 INIT_LIST_HEAD(&conn->lock_list); 316 317 init_rwsem(&conn->session_lock); 318 319 return conn; 320 } 321 322 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c) 323 { 324 struct ksmbd_conn *t; 325 int bkt; 326 bool ret = false; 327 328 down_read(&conn_list_lock); 329 hash_for_each(conn_list, bkt, t, hlist) { 330 if (memcmp(t->ClientGUID, c->ClientGUID, SMB2_CLIENT_GUID_SIZE)) 331 continue; 332 333 ret = true; 334 break; 335 } 336 up_read(&conn_list_lock); 337 return ret; 338 } 339 340 void ksmbd_conn_enqueue_request(struct ksmbd_work *work) 341 { 342 struct ksmbd_conn *conn = work->conn; 343 struct list_head *requests_queue = NULL; 344 345 if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE) 346 requests_queue = &conn->requests; 347 348 atomic_inc(&conn->req_running); 349 if (requests_queue) { 350 spin_lock(&conn->request_lock); 351 list_add_tail(&work->request_entry, requests_queue); 352 spin_unlock(&conn->request_lock); 353 } 354 } 355 356 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work) 357 { 358 struct ksmbd_conn *conn = work->conn; 359 360 atomic_dec(&conn->req_running); 361 if (waitqueue_active(&conn->req_running_q)) 362 wake_up(&conn->req_running_q); 363 364 if (list_empty(&work->request_entry) && 365 list_empty(&work->async_request_entry)) 366 return; 367 368 spin_lock(&conn->request_lock); 369 list_del_init(&work->request_entry); 370 spin_unlock(&conn->request_lock); 371 if (work->asynchronous) 372 release_async_work(work); 373 374 wake_up_all(&conn->req_running_q); 375 } 376 377 static void ksmbd_conn_cancel_async_requests(struct ksmbd_conn *conn) 378 { 379 struct ksmbd_work *work, *tmp; 380 381 ksmbd_debug(CONN, "Cancel pending async requests on releasing connection\n"); 382 spin_lock(&conn->request_lock); 383 list_for_each_entry_safe(work, tmp, &conn->async_requests, 384 async_request_entry) { 385 if (work->state != KSMBD_WORK_ACTIVE) 386 continue; 387 388 ksmbd_debug(CONN, "Cancel async request id %d\n", 389 work->async_id); 390 work->state = KSMBD_WORK_CANCELLED; 391 if (work->cancel_fn) 392 work->cancel_fn(work->cancel_argv); 393 } 394 spin_unlock(&conn->request_lock); 395 } 396 397 void ksmbd_conn_lock(struct ksmbd_conn *conn) 398 { 399 mutex_lock(&conn->srv_mutex); 400 } 401 402 void ksmbd_conn_unlock(struct ksmbd_conn *conn) 403 { 404 mutex_unlock(&conn->srv_mutex); 405 } 406 407 void ksmbd_all_conn_set_status(u64 sess_id, u32 status) 408 { 409 struct ksmbd_conn *conn; 410 int bkt; 411 412 down_read(&conn_list_lock); 413 hash_for_each(conn_list, bkt, conn, hlist) { 414 if (conn->binding || xa_load(&conn->sessions, sess_id)) 415 WRITE_ONCE(conn->status, status); 416 } 417 up_read(&conn_list_lock); 418 } 419 420 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn) 421 { 422 wait_event(conn->req_running_q, atomic_read(&conn->req_running) < 2); 423 } 424 425 int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id) 426 { 427 struct ksmbd_conn *conn; 428 int rc, retry_count = 0, max_timeout = 120; 429 int rcount, bkt; 430 431 retry_idle: 432 if (retry_count >= max_timeout) 433 return -EIO; 434 435 down_read(&conn_list_lock); 436 hash_for_each(conn_list, bkt, conn, hlist) { 437 if (conn->binding || xa_load(&conn->sessions, sess_id)) { 438 rcount = (conn == curr_conn) ? 2 : 1; 439 if (atomic_read(&conn->req_running) >= rcount) { 440 rc = wait_event_timeout(conn->req_running_q, 441 atomic_read(&conn->req_running) < rcount, 442 HZ); 443 if (!rc) { 444 up_read(&conn_list_lock); 445 retry_count++; 446 goto retry_idle; 447 } 448 } 449 } 450 } 451 up_read(&conn_list_lock); 452 453 return 0; 454 } 455 456 static int __ksmbd_conn_write(struct ksmbd_work *work, 457 struct ksmbd_transport_write *tx) 458 { 459 struct ksmbd_conn *conn = work->conn; 460 int sent; 461 462 if (!work->response_buf) { 463 pr_err("NULL response header\n"); 464 return -EINVAL; 465 } 466 467 if (work->send_no_response) 468 return 0; 469 470 if (!work->iov_idx) 471 return -EINVAL; 472 473 tx->iov = work->iov; 474 tx->iov_cnt = work->iov_cnt; 475 tx->size = get_rfc1002_len(work->iov[0].iov_base) + 4; 476 tx->need_invalidate_rkey = work->need_invalidate_rkey; 477 tx->remote_key = work->remote_key; 478 479 ksmbd_conn_lock(conn); 480 sent = conn->transport->ops->writev(conn->transport, tx); 481 ksmbd_conn_unlock(conn); 482 483 if (sent < 0) { 484 pr_err("Failed to send message: %d\n", sent); 485 return sent; 486 } 487 488 return 0; 489 } 490 491 int ksmbd_conn_write(struct ksmbd_work *work) 492 { 493 struct ksmbd_transport_write tx = {}; 494 495 return __ksmbd_conn_write(work, &tx); 496 } 497 498 int ksmbd_conn_write_eor(struct ksmbd_work *work) 499 { 500 struct ksmbd_transport_write tx = { 501 .msg_flags = MSG_EOR, 502 }; 503 504 return __ksmbd_conn_write(work, &tx); 505 } 506 507 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, 508 void *buf, unsigned int buflen, 509 struct smbdirect_buffer_descriptor_v1 *desc, 510 unsigned int desc_len) 511 { 512 int ret = -EINVAL; 513 514 if (conn->transport->ops->rdma_read) 515 ret = conn->transport->ops->rdma_read(conn->transport, 516 buf, buflen, 517 desc, desc_len); 518 return ret; 519 } 520 521 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, 522 void *buf, unsigned int buflen, 523 struct smbdirect_buffer_descriptor_v1 *desc, 524 unsigned int desc_len) 525 { 526 int ret = -EINVAL; 527 528 if (conn->transport->ops->rdma_write) 529 ret = conn->transport->ops->rdma_write(conn->transport, 530 buf, buflen, 531 desc, desc_len); 532 return ret; 533 } 534 535 bool ksmbd_conn_alive(struct ksmbd_conn *conn) 536 { 537 if (!ksmbd_server_running()) 538 return false; 539 540 if (ksmbd_conn_exiting(conn)) 541 return false; 542 543 if (kthread_should_stop()) 544 return false; 545 546 if (atomic_read(&conn->stats.open_files_count) > 0) 547 return true; 548 549 /* 550 * Stop current session if the time that get last request from client 551 * is bigger than deadtime user configured and opening file count is 552 * zero. 553 */ 554 if (server_conf.deadtime > 0 && 555 time_after(jiffies, conn->last_active + server_conf.deadtime)) { 556 ksmbd_debug(CONN, "No response from client in %lu minutes\n", 557 server_conf.deadtime / SMB_ECHO_INTERVAL); 558 return false; 559 } 560 return true; 561 } 562 563 /* "+2" for BCC field (ByteCount, 2 bytes) */ 564 #define SMB1_MIN_SUPPORTED_PDU_SIZE (sizeof(struct smb_hdr) + 2) 565 #define SMB2_MIN_SUPPORTED_PDU_SIZE (sizeof(struct smb2_pdu)) 566 #define SMB2_TRANSFORM_MIN_SUPPORTED_PDU_SIZE \ 567 (sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) 568 569 /** 570 * ksmbd_conn_handler_loop() - session thread to listen on new smb requests 571 * @p: connection instance 572 * 573 * One thread each per connection 574 * 575 * Return: 0 on success 576 */ 577 int ksmbd_conn_handler_loop(void *p) 578 { 579 struct ksmbd_conn *conn = (struct ksmbd_conn *)p; 580 struct ksmbd_transport *t = conn->transport; 581 unsigned int pdu_size, max_allowed_pdu_size, max_req; 582 __le32 proto; 583 char hdr_buf[4] = {0,}; 584 int size; 585 586 mutex_init(&conn->srv_mutex); 587 __module_get(THIS_MODULE); 588 589 max_req = server_conf.max_inflight_req; 590 conn->last_active = jiffies; 591 set_freezable(); 592 while (ksmbd_conn_alive(conn)) { 593 if (try_to_freeze()) 594 continue; 595 596 kvfree(conn->request_buf); 597 conn->request_buf = NULL; 598 599 recheck: 600 if (atomic_read(&conn->req_running) + 1 > max_req) { 601 wait_event_interruptible(conn->req_running_q, 602 atomic_read(&conn->req_running) < max_req); 603 goto recheck; 604 } 605 606 size = t->ops->read(t, hdr_buf, sizeof(hdr_buf), -1); 607 if (size != sizeof(hdr_buf)) 608 break; 609 610 pdu_size = get_rfc1002_len(hdr_buf); 611 ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size); 612 613 max_allowed_pdu_size = ksmbd_max_allowed_pdu_size(conn); 614 615 if (pdu_size > max_allowed_pdu_size) { 616 pr_err_ratelimited("PDU length(%u) exceeded maximum allowed pdu size(%u) on connection(%d)\n", 617 pdu_size, max_allowed_pdu_size, 618 READ_ONCE(conn->status)); 619 break; 620 } 621 622 /* 623 * Check maximum pdu size(0x00FFFFFF). 624 */ 625 if (pdu_size > MAX_STREAM_PROT_LEN) 626 break; 627 628 if (pdu_size < SMB1_MIN_SUPPORTED_PDU_SIZE) 629 break; 630 631 /* 4 for rfc1002 length field */ 632 /* 1 for implied bcc[0] */ 633 size = pdu_size + 4 + 1; 634 conn->request_buf = kvmalloc(size, KSMBD_DEFAULT_GFP); 635 if (!conn->request_buf) 636 break; 637 638 memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf)); 639 640 /* 641 * We already read 4 bytes to find out PDU size, now 642 * read in PDU 643 */ 644 size = t->ops->read(t, conn->request_buf + 4, pdu_size, 2); 645 if (size < 0) { 646 pr_err("sock_read failed: %d\n", size); 647 break; 648 } 649 650 if (size != pdu_size) { 651 pr_err("PDU error. Read: %d, Expected: %d\n", 652 size, pdu_size); 653 continue; 654 } 655 656 if (((struct smb2_hdr *)smb_get_msg(conn->request_buf))->ProtocolId == 657 SMB2_COMPRESSION_TRANSFORM_ID) { 658 /* 659 * Convert the transform into a normal RFC1002-framed SMB2 660 * request before protocol validation and work allocation. 661 */ 662 if (ksmbd_decompress_request(conn)) 663 break; 664 pdu_size = get_rfc1002_len(conn->request_buf); 665 } 666 667 if (!ksmbd_smb_request(conn)) 668 break; 669 670 proto = *(__le32 *)smb_get_msg(conn->request_buf); 671 if (proto == SMB2_PROTO_NUMBER && 672 pdu_size < SMB2_MIN_SUPPORTED_PDU_SIZE) 673 break; 674 675 if (proto == SMB2_TRANSFORM_PROTO_NUM && 676 pdu_size < SMB2_TRANSFORM_MIN_SUPPORTED_PDU_SIZE) 677 break; 678 679 if (!default_conn_ops.process_fn) { 680 pr_err("No connection request callback\n"); 681 break; 682 } 683 684 if (default_conn_ops.process_fn(conn)) { 685 pr_err("Cannot handle request\n"); 686 break; 687 } 688 } 689 690 ksmbd_conn_set_releasing(conn); 691 ksmbd_conn_cancel_async_requests(conn); 692 /* Wait till all reference dropped to the Server object*/ 693 ksmbd_debug(CONN, "Wait for all pending requests(%d)\n", atomic_read(&conn->r_count)); 694 wait_event(conn->r_count_q, atomic_read(&conn->r_count) == 0); 695 696 if (IS_ENABLED(CONFIG_UNICODE)) 697 utf8_unload(conn->um); 698 unload_nls(conn->local_nls); 699 if (default_conn_ops.terminate_fn) 700 default_conn_ops.terminate_fn(conn); 701 t->ops->disconnect(t); 702 module_put(THIS_MODULE); 703 return 0; 704 } 705 706 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops) 707 { 708 default_conn_ops.process_fn = ops->process_fn; 709 default_conn_ops.terminate_fn = ops->terminate_fn; 710 } 711 712 void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn) 713 { 714 atomic_inc(&conn->r_count); 715 } 716 717 void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn) 718 { 719 /* 720 * Checking waitqueue to dropping pending requests on 721 * disconnection. waitqueue_active is safe because it 722 * uses atomic operation for condition. 723 */ 724 atomic_inc(&conn->refcnt); 725 if (!atomic_dec_return(&conn->r_count) && waitqueue_active(&conn->r_count_q)) 726 wake_up(&conn->r_count_q); 727 728 ksmbd_conn_put(conn); 729 } 730 731 int ksmbd_conn_transport_init(void) 732 { 733 int ret; 734 735 mutex_lock(&init_lock); 736 ret = ksmbd_tcp_init(); 737 if (ret) { 738 pr_err("Failed to init TCP subsystem: %d\n", ret); 739 goto out; 740 } 741 742 ret = ksmbd_rdma_init(); 743 if (ret) { 744 pr_err("Failed to init RDMA subsystem: %d\n", ret); 745 goto out; 746 } 747 out: 748 mutex_unlock(&init_lock); 749 create_proc_clients(); 750 return ret; 751 } 752 753 static void stop_sessions(void) 754 { 755 struct ksmbd_conn *conn, *target; 756 struct ksmbd_transport *t; 757 bool any; 758 int bkt; 759 760 /* 761 * Serialised via init_lock; no concurrent stop_sessions() can 762 * touch conn->stop_called, so writing it under the read lock is 763 * safe. 764 */ 765 again: 766 target = NULL; 767 any = false; 768 down_read(&conn_list_lock); 769 hash_for_each(conn_list, bkt, conn, hlist) { 770 any = true; 771 if (conn->stop_called) 772 continue; 773 atomic_inc(&conn->refcnt); 774 conn->stop_called = true; 775 /* 776 * Mark the connection EXITING while still holding the 777 * read lock so the selection and the status transition 778 * happen together. Do not regress a connection that has 779 * already advanced to RELEASING on its own (e.g. the 780 * handler exited its receive loop for an unrelated 781 * reason). 782 */ 783 if (READ_ONCE(conn->status) != KSMBD_SESS_RELEASING) 784 ksmbd_conn_set_exiting(conn); 785 target = conn; 786 break; 787 } 788 up_read(&conn_list_lock); 789 790 if (target) { 791 t = target->transport; 792 if (t->ops->shutdown) 793 t->ops->shutdown(t); 794 if (atomic_dec_and_test(&target->refcnt)) { 795 ida_destroy(&target->async_ida); 796 t->ops->free_transport(t); 797 kfree_sensitive(target); 798 } 799 goto again; 800 } 801 802 if (any) { 803 msleep(100); 804 goto again; 805 } 806 } 807 808 void ksmbd_conn_transport_destroy(void) 809 { 810 delete_proc_clients(); 811 mutex_lock(&init_lock); 812 ksmbd_tcp_destroy(); 813 ksmbd_rdma_stop_listening(); 814 stop_sessions(); 815 mutex_unlock(&init_lock); 816 } 817