1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> 4 * Copyright (C) 2019 Samsung Electronics Co., Ltd. 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/filelock.h> 9 #include <linux/slab.h> 10 #include <linux/vmalloc.h> 11 #include <linux/kthread.h> 12 #include <linux/freezer.h> 13 #include <linux/dcache.h> 14 15 #include "glob.h" 16 #include "vfs_cache.h" 17 #include "oplock.h" 18 #include "vfs.h" 19 #include "connection.h" 20 #include "misc.h" 21 #include "mgmt/tree_connect.h" 22 #include "mgmt/user_session.h" 23 #include "mgmt/user_config.h" 24 #include "smb_common.h" 25 #include "server.h" 26 #include "smb2pdu.h" 27 28 #define S_DEL_PENDING 1 29 #define S_DEL_ON_CLS 2 30 #define S_DEL_ON_CLS_STREAM 8 31 32 static unsigned int inode_hash_mask __read_mostly; 33 static unsigned int inode_hash_shift __read_mostly; 34 static struct hlist_head *inode_hashtable __read_mostly; 35 static DEFINE_RWLOCK(inode_hash_lock); 36 37 static struct ksmbd_file_table global_ft; 38 static atomic_long_t fd_limit; 39 static struct kmem_cache *filp_cache; 40 41 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp); 42 43 #define OPLOCK_NONE 0 44 #define OPLOCK_EXCLUSIVE 1 45 #define OPLOCK_BATCH 2 46 #define OPLOCK_READ 3 /* level 2 oplock */ 47 48 #ifdef CONFIG_PROC_FS 49 50 static const struct ksmbd_const_name ksmbd_lease_const_names[] = { 51 {le32_to_cpu(SMB2_LEASE_NONE_LE), "LEASE_NONE"}, 52 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE), "LEASE_R"}, 53 {le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_H"}, 54 {le32_to_cpu(SMB2_LEASE_WRITE_CACHING_LE), "LEASE_W"}, 55 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE | 56 SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_RH"}, 57 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE | 58 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RW"}, 59 {le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE | 60 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_WH"}, 61 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE | 62 SMB2_LEASE_HANDLE_CACHING_LE | 63 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RWH"}, 64 }; 65 66 static const struct ksmbd_const_name ksmbd_oplock_const_names[] = { 67 {SMB2_OPLOCK_LEVEL_NONE, "OPLOCK_NONE"}, 68 {SMB2_OPLOCK_LEVEL_II, "OPLOCK_II"}, 69 {SMB2_OPLOCK_LEVEL_EXCLUSIVE, "OPLOCK_EXECL"}, 70 {SMB2_OPLOCK_LEVEL_BATCH, "OPLOCK_BATCH"}, 71 }; 72 73 static int proc_show_files(struct seq_file *m, void *v) 74 { 75 struct ksmbd_file *fp = NULL; 76 unsigned int id; 77 struct oplock_info *opinfo; 78 79 seq_printf(m, "#%-10s %-10s %-10s %-10s %-15s %-10s %-10s %s\n", 80 "<tree id>", "<pid>", "<vid>", "<refcnt>", 81 "<oplock>", "<daccess>", "<saccess>", 82 "<name>"); 83 84 read_lock(&global_ft.lock); 85 idr_for_each_entry(global_ft.idr, fp, id) { 86 seq_printf(m, "%#-10x %#-10llx %#-10llx %#-10x", 87 fp->tcon ? fp->tcon->id : 0, 88 fp->persistent_id, 89 fp->volatile_id, 90 atomic_read(&fp->refcount)); 91 92 rcu_read_lock(); 93 opinfo = rcu_dereference(fp->f_opinfo); 94 if (opinfo) { 95 const struct ksmbd_const_name *const_names; 96 int count; 97 unsigned int level; 98 99 if (opinfo->is_lease) { 100 const_names = ksmbd_lease_const_names; 101 count = ARRAY_SIZE(ksmbd_lease_const_names); 102 level = le32_to_cpu(opinfo->o_lease->state); 103 } else { 104 const_names = ksmbd_oplock_const_names; 105 count = ARRAY_SIZE(ksmbd_oplock_const_names); 106 level = opinfo->level; 107 } 108 rcu_read_unlock(); 109 ksmbd_proc_show_const_name(m, " %-15s", 110 const_names, count, level); 111 } else { 112 rcu_read_unlock(); 113 seq_printf(m, " %-15s", " "); 114 } 115 116 seq_printf(m, " %#010x %#010x %s\n", 117 le32_to_cpu(fp->daccess), 118 le32_to_cpu(fp->saccess), 119 fp->filp->f_path.dentry->d_name.name); 120 } 121 read_unlock(&global_ft.lock); 122 return 0; 123 } 124 125 static int create_proc_files(void) 126 { 127 ksmbd_proc_create("files", proc_show_files, NULL); 128 return 0; 129 } 130 #else 131 static int create_proc_files(void) { return 0; } 132 #endif 133 134 static bool durable_scavenger_running; 135 static DEFINE_MUTEX(durable_scavenger_lock); 136 static wait_queue_head_t dh_wq; 137 138 void ksmbd_set_fd_limit(unsigned long limit) 139 { 140 limit = min(limit, get_max_files()); 141 atomic_long_set(&fd_limit, limit); 142 } 143 144 static bool fd_limit_depleted(void) 145 { 146 long v = atomic_long_dec_return(&fd_limit); 147 148 if (v >= 0) 149 return false; 150 atomic_long_inc(&fd_limit); 151 return true; 152 } 153 154 static void fd_limit_close(void) 155 { 156 atomic_long_inc(&fd_limit); 157 } 158 159 /* 160 * INODE hash 161 */ 162 163 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval) 164 { 165 unsigned long tmp; 166 167 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / 168 L1_CACHE_BYTES; 169 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift); 170 return tmp & inode_hash_mask; 171 } 172 173 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de) 174 { 175 struct hlist_head *head = inode_hashtable + 176 inode_hash(d_inode(de)->i_sb, (unsigned long)de); 177 struct ksmbd_inode *ci = NULL, *ret_ci = NULL; 178 179 hlist_for_each_entry(ci, head, m_hash) { 180 if (ci->m_de == de) { 181 if (atomic_inc_not_zero(&ci->m_count)) 182 ret_ci = ci; 183 break; 184 } 185 } 186 return ret_ci; 187 } 188 189 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp) 190 { 191 return __ksmbd_inode_lookup(fp->filp->f_path.dentry); 192 } 193 194 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d) 195 { 196 struct ksmbd_inode *ci; 197 198 read_lock(&inode_hash_lock); 199 ci = __ksmbd_inode_lookup(d); 200 read_unlock(&inode_hash_lock); 201 202 return ci; 203 } 204 205 int ksmbd_query_inode_status(struct dentry *dentry) 206 { 207 struct ksmbd_inode *ci; 208 int ret = KSMBD_INODE_STATUS_UNKNOWN; 209 210 read_lock(&inode_hash_lock); 211 ci = __ksmbd_inode_lookup(dentry); 212 read_unlock(&inode_hash_lock); 213 if (!ci) 214 return ret; 215 216 down_read(&ci->m_lock); 217 if (ci->m_flags & S_DEL_PENDING) 218 ret = KSMBD_INODE_STATUS_PENDING_DELETE; 219 else 220 ret = KSMBD_INODE_STATUS_OK; 221 up_read(&ci->m_lock); 222 223 ksmbd_inode_put(ci); 224 return ret; 225 } 226 227 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp) 228 { 229 struct ksmbd_inode *ci = fp->f_ci; 230 int ret; 231 232 down_read(&ci->m_lock); 233 ret = (ci->m_flags & S_DEL_PENDING); 234 up_read(&ci->m_lock); 235 236 return ret; 237 } 238 239 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp) 240 { 241 struct ksmbd_inode *ci = fp->f_ci; 242 243 down_write(&ci->m_lock); 244 ci->m_flags |= S_DEL_PENDING; 245 up_write(&ci->m_lock); 246 } 247 248 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp) 249 { 250 struct ksmbd_inode *ci = fp->f_ci; 251 252 down_write(&ci->m_lock); 253 ci->m_flags &= ~S_DEL_PENDING; 254 up_write(&ci->m_lock); 255 } 256 257 bool ksmbd_has_stream_without_delete_share(struct ksmbd_file *fp) 258 { 259 struct ksmbd_file *prev_fp; 260 struct ksmbd_inode *ci = fp->f_ci; 261 bool ret = false; 262 263 if (ksmbd_stream_fd(fp)) 264 return false; 265 266 down_read(&ci->m_lock); 267 list_for_each_entry(prev_fp, &ci->m_fp_list, node) { 268 if (prev_fp == fp || !ksmbd_stream_fd(prev_fp)) 269 continue; 270 271 if (file_inode(fp->filp) != file_inode(prev_fp->filp)) 272 continue; 273 274 if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE)) { 275 ret = true; 276 break; 277 } 278 } 279 up_read(&ci->m_lock); 280 281 return ret; 282 } 283 284 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp, 285 int file_info) 286 { 287 struct ksmbd_inode *ci = fp->f_ci; 288 289 down_write(&ci->m_lock); 290 if (ksmbd_stream_fd(fp)) 291 ci->m_flags |= S_DEL_ON_CLS_STREAM; 292 else 293 ci->m_flags |= S_DEL_ON_CLS; 294 up_write(&ci->m_lock); 295 } 296 297 static void ksmbd_inode_hash(struct ksmbd_inode *ci) 298 { 299 struct hlist_head *b = inode_hashtable + 300 inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de); 301 302 hlist_add_head(&ci->m_hash, b); 303 } 304 305 static void ksmbd_inode_unhash(struct ksmbd_inode *ci) 306 { 307 write_lock(&inode_hash_lock); 308 hlist_del_init(&ci->m_hash); 309 write_unlock(&inode_hash_lock); 310 } 311 312 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp) 313 { 314 atomic_set(&ci->m_count, 1); 315 atomic_set(&ci->op_count, 0); 316 atomic_set(&ci->sop_count, 0); 317 ci->m_flags = 0; 318 ci->m_fattr = 0; 319 INIT_LIST_HEAD(&ci->m_fp_list); 320 INIT_LIST_HEAD(&ci->m_op_list); 321 init_rwsem(&ci->m_lock); 322 ci->m_de = fp->filp->f_path.dentry; 323 return 0; 324 } 325 326 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp) 327 { 328 struct ksmbd_inode *ci, *tmpci; 329 int rc; 330 331 read_lock(&inode_hash_lock); 332 ci = ksmbd_inode_lookup(fp); 333 read_unlock(&inode_hash_lock); 334 if (ci) 335 return ci; 336 337 ci = kmalloc_obj(struct ksmbd_inode, KSMBD_DEFAULT_GFP); 338 if (!ci) 339 return NULL; 340 341 rc = ksmbd_inode_init(ci, fp); 342 if (rc) { 343 pr_err("inode initialized failed\n"); 344 kfree(ci); 345 return NULL; 346 } 347 348 write_lock(&inode_hash_lock); 349 tmpci = ksmbd_inode_lookup(fp); 350 if (!tmpci) { 351 ksmbd_inode_hash(ci); 352 } else { 353 kfree(ci); 354 ci = tmpci; 355 } 356 write_unlock(&inode_hash_lock); 357 return ci; 358 } 359 360 static void ksmbd_inode_free(struct ksmbd_inode *ci) 361 { 362 ksmbd_inode_unhash(ci); 363 kfree(ci); 364 } 365 366 void ksmbd_inode_put(struct ksmbd_inode *ci) 367 { 368 if (atomic_dec_and_test(&ci->m_count)) 369 ksmbd_inode_free(ci); 370 } 371 372 int __init ksmbd_inode_hash_init(void) 373 { 374 unsigned int loop; 375 unsigned long numentries = 16384; 376 unsigned long bucketsize = sizeof(struct hlist_head); 377 unsigned long size; 378 379 inode_hash_shift = ilog2(numentries); 380 inode_hash_mask = (1 << inode_hash_shift) - 1; 381 382 size = bucketsize << inode_hash_shift; 383 384 /* init master fp hash table */ 385 inode_hashtable = vmalloc(size); 386 if (!inode_hashtable) 387 return -ENOMEM; 388 389 for (loop = 0; loop < (1U << inode_hash_shift); loop++) 390 INIT_HLIST_HEAD(&inode_hashtable[loop]); 391 return 0; 392 } 393 394 void ksmbd_release_inode_hash(void) 395 { 396 vfree(inode_hashtable); 397 } 398 399 static void __ksmbd_inode_close(struct ksmbd_file *fp) 400 { 401 struct ksmbd_inode *ci = fp->f_ci; 402 int err; 403 struct file *filp; 404 405 filp = fp->filp; 406 407 if (ksmbd_stream_fd(fp)) { 408 bool remove_stream_xattr = false; 409 410 down_write(&ci->m_lock); 411 if (ci->m_flags & S_DEL_ON_CLS_STREAM) { 412 ci->m_flags &= ~S_DEL_ON_CLS_STREAM; 413 remove_stream_xattr = true; 414 } 415 up_write(&ci->m_lock); 416 417 if (remove_stream_xattr) { 418 const struct cred *saved_cred; 419 420 saved_cred = override_creds(filp->f_cred); 421 err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp), 422 &filp->f_path, 423 fp->stream.name, 424 true); 425 revert_creds(saved_cred); 426 if (err) 427 pr_err("remove xattr failed : %s\n", 428 fp->stream.name); 429 } 430 } 431 432 down_write(&ci->m_lock); 433 /* Promote S_DEL_ON_CLS to S_DEL_PENDING when close */ 434 if (ci->m_flags & S_DEL_ON_CLS) { 435 ci->m_flags &= ~S_DEL_ON_CLS; 436 ci->m_flags |= S_DEL_PENDING; 437 } 438 up_write(&ci->m_lock); 439 440 if (atomic_dec_and_test(&ci->m_count)) { 441 bool do_unlink = false; 442 443 down_write(&ci->m_lock); 444 if (ci->m_flags & S_DEL_PENDING) { 445 ci->m_flags &= ~S_DEL_PENDING; 446 do_unlink = true; 447 } 448 up_write(&ci->m_lock); 449 450 if (do_unlink) 451 ksmbd_vfs_unlink(filp); 452 453 ksmbd_inode_free(ci); 454 } 455 } 456 457 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp) 458 { 459 if (!has_file_id(fp->persistent_id)) 460 return; 461 462 idr_remove(global_ft.idr, fp->persistent_id); 463 /* 464 * Clear persistent_id so a later __ksmbd_close_fd() that runs from a 465 * delayed putter (e.g. when a concurrent ksmbd_lookup_fd_inode() 466 * walker held the final reference) does not re-issue idr_remove() on 467 * an id that idr_alloc_cyclic() may have already handed out to a new 468 * durable handle. 469 */ 470 fp->persistent_id = KSMBD_NO_FID; 471 } 472 473 static void ksmbd_remove_durable_fd(struct ksmbd_file *fp) 474 { 475 write_lock(&global_ft.lock); 476 __ksmbd_remove_durable_fd(fp); 477 write_unlock(&global_ft.lock); 478 if (waitqueue_active(&dh_wq)) 479 wake_up(&dh_wq); 480 } 481 482 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) 483 { 484 down_write(&fp->f_ci->m_lock); 485 list_del_init(&fp->node); 486 up_write(&fp->f_ci->m_lock); 487 488 if (!has_file_id(fp->volatile_id)) 489 return; 490 491 write_lock(&ft->lock); 492 idr_remove(ft->idr, fp->volatile_id); 493 write_unlock(&ft->lock); 494 } 495 496 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) 497 { 498 struct file *filp; 499 struct ksmbd_lock *smb_lock, *tmp_lock; 500 501 fd_limit_close(); 502 ksmbd_remove_durable_fd(fp); 503 if (ft) 504 __ksmbd_remove_fd(ft, fp); 505 506 close_id_del_oplock(fp); 507 filp = fp->filp; 508 509 __ksmbd_inode_close(fp); 510 if (!IS_ERR_OR_NULL(filp)) 511 fput(filp); 512 513 /* because the reference count of fp is 0, it is guaranteed that 514 * there are not accesses to fp->lock_list. 515 */ 516 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) { 517 struct ksmbd_conn *conn = smb_lock->conn; 518 519 if (conn) { 520 spin_lock(&conn->llist_lock); 521 list_del_init(&smb_lock->clist); 522 smb_lock->conn = NULL; 523 spin_unlock(&conn->llist_lock); 524 ksmbd_conn_put(conn); 525 } 526 527 list_del(&smb_lock->flist); 528 locks_free_lock(smb_lock->fl); 529 kfree(smb_lock); 530 } 531 532 /* 533 * Drop fp's strong reference on conn (taken in ksmbd_open_fd() / 534 * ksmbd_reopen_durable_fd()). Durable fps that reached the 535 * scavenger have already had fp->conn cleared by session_fd_check(), 536 * in which case there is nothing to drop here. 537 */ 538 if (fp->conn) { 539 ksmbd_conn_put(fp->conn); 540 fp->conn = NULL; 541 } 542 543 if (ksmbd_stream_fd(fp)) 544 kfree(fp->stream.name); 545 kfree(fp->owner.name); 546 547 kmem_cache_free(filp_cache, fp); 548 } 549 550 /** 551 * ksmbd_close_disconnected_durable_delete_on_close() - drop a delete-on-close 552 * file kept present only by disconnected durable handles 553 * @dentry: dentry of the file being opened 554 * 555 * A durable handle opened with delete-on-close is preserved across a 556 * disconnect so it can be reclaimed by a durable reconnect. When a new 557 * (non-reconnect) open arrives for the same name instead, the disconnected 558 * handle has to give way. Close such handles so their delete-on-close is 559 * applied and the file is removed once the last handle is gone, letting the 560 * new open create a fresh file. 561 * 562 * The caller's inode reference is dropped before closing so that the final 563 * close can promote S_DEL_ON_CLS to S_DEL_PENDING and unlink the file. 564 * 565 * Return: true if a disconnected durable handle was closed. 566 */ 567 bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry *dentry) 568 { 569 struct ksmbd_inode *ci; 570 struct ksmbd_file *fp, *tmp; 571 LIST_HEAD(dispose); 572 bool closed = false; 573 574 ci = ksmbd_inode_lookup_lock(dentry); 575 if (!ci) 576 return false; 577 578 down_write(&ci->m_lock); 579 if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_ON_CLS_STREAM | S_DEL_PENDING)) { 580 list_for_each_entry_safe(fp, tmp, &ci->m_fp_list, node) { 581 if (fp->conn || !fp->is_durable || 582 fp->f_state != FP_INITED) 583 continue; 584 585 /* 586 * Claim the close before unlinking fp from m_fp_list. 587 * refcount == 1 means only the durable lifetime ref is 588 * left. Add a transient ref so final close can drop both. 589 */ 590 write_lock(&global_ft.lock); 591 if (atomic_read(&fp->refcount) == 1) { 592 atomic_inc(&fp->refcount); 593 __ksmbd_remove_durable_fd(fp); 594 ksmbd_mark_fp_closed(fp); 595 list_move_tail(&fp->node, &dispose); 596 } 597 write_unlock(&global_ft.lock); 598 } 599 } 600 up_write(&ci->m_lock); 601 602 /* 603 * Drop our lookup reference before closing so the last __ksmbd_close_fd() 604 * can drop m_count to zero and unlink the delete-on-close file. The 605 * collected handles still hold the transient reference taken above, so 606 * ci stays valid until they are closed below. 607 */ 608 ksmbd_inode_put(ci); 609 610 while (!list_empty(&dispose)) { 611 fp = list_first_entry(&dispose, struct ksmbd_file, node); 612 list_del_init(&fp->node); 613 if (atomic_sub_and_test(2, &fp->refcount)) { 614 __ksmbd_close_fd(NULL, fp); 615 closed = true; 616 } 617 } 618 619 return closed; 620 } 621 622 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp) 623 { 624 if (fp->f_state != FP_INITED) 625 return NULL; 626 627 if (!atomic_inc_not_zero(&fp->refcount)) 628 return NULL; 629 return fp; 630 } 631 632 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft, 633 u64 id) 634 { 635 struct ksmbd_file *fp; 636 637 if (!has_file_id(id)) 638 return NULL; 639 640 read_lock(&ft->lock); 641 fp = idr_find(ft->idr, id); 642 if (fp) 643 fp = ksmbd_fp_get(fp); 644 read_unlock(&ft->lock); 645 return fp; 646 } 647 648 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp) 649 { 650 /* 651 * Detached durable fp -- session_fd_check() cleared fp->conn at 652 * preserve, so this fp is no longer tracked by any conn's 653 * stats.open_files_count. This happens when 654 * ksmbd_scavenger_dispose_dh() hands the final close off to an 655 * m_fp_list walker (e.g. ksmbd_lookup_fd_inode()) whose work->conn 656 * is unrelated to the conn that originally opened the handle; close 657 * via the NULL-ft path so we do not underflow that unrelated 658 * counter. 659 */ 660 if (!fp->conn) { 661 __ksmbd_close_fd(NULL, fp); 662 return; 663 } 664 __ksmbd_close_fd(&work->sess->file_table, fp); 665 atomic_dec(&work->conn->stats.open_files_count); 666 } 667 668 static void set_close_state_blocked_works(struct ksmbd_file *fp) 669 { 670 struct ksmbd_work *cancel_work; 671 672 spin_lock(&fp->f_lock); 673 list_for_each_entry(cancel_work, &fp->blocked_works, 674 fp_entry) { 675 cancel_work->state = KSMBD_WORK_CLOSED; 676 cancel_work->cancel_fn(cancel_work->cancel_argv); 677 } 678 spin_unlock(&fp->f_lock); 679 } 680 681 int ksmbd_close_fd(struct ksmbd_work *work, u64 id) 682 { 683 struct ksmbd_file *fp; 684 struct ksmbd_file_table *ft; 685 bool closed = false; 686 687 if (!has_file_id(id)) 688 return 0; 689 690 ft = &work->sess->file_table; 691 write_lock(&ft->lock); 692 fp = idr_find(ft->idr, id); 693 if (fp) { 694 set_close_state_blocked_works(fp); 695 696 if (fp->f_state != FP_INITED) 697 fp = NULL; 698 else { 699 fp->f_state = FP_CLOSED; 700 closed = true; 701 if (!atomic_dec_and_test(&fp->refcount)) 702 fp = NULL; 703 } 704 } 705 write_unlock(&ft->lock); 706 707 if (!fp) 708 return closed ? 0 : -EINVAL; 709 710 __put_fd_final(work, fp); 711 return 0; 712 } 713 714 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp) 715 { 716 if (!fp) 717 return; 718 719 if (!atomic_dec_and_test(&fp->refcount)) 720 return; 721 __put_fd_final(work, fp); 722 } 723 724 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) 725 { 726 if (!fp) 727 return false; 728 if (fp->tcon != tcon) 729 return false; 730 return true; 731 } 732 733 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id) 734 { 735 return __ksmbd_lookup_fd(&work->sess->file_table, id); 736 } 737 738 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id) 739 { 740 struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 741 742 if (__sanity_check(work->tcon, fp)) 743 return fp; 744 745 ksmbd_fd_put(work, fp); 746 return NULL; 747 } 748 749 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id, 750 u64 pid) 751 { 752 struct ksmbd_file *fp; 753 754 if (!has_file_id(id)) { 755 id = work->compound_fid; 756 pid = work->compound_pfid; 757 } 758 759 fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 760 if (!__sanity_check(work->tcon, fp)) { 761 ksmbd_fd_put(work, fp); 762 return NULL; 763 } 764 if (fp->persistent_id != pid) { 765 ksmbd_fd_put(work, fp); 766 return NULL; 767 } 768 return fp; 769 } 770 771 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id) 772 { 773 return __ksmbd_lookup_fd(&global_ft, id); 774 } 775 776 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) 777 { 778 struct ksmbd_file *fp; 779 780 fp = __ksmbd_lookup_fd(&global_ft, id); 781 if (fp && (fp->durable_reconnect_disabled || 782 fp->conn || 783 (fp->durable_scavenger_timeout && 784 (fp->durable_scavenger_timeout < 785 jiffies_to_msecs(jiffies))))) { 786 ksmbd_put_durable_fd(fp); 787 fp = NULL; 788 } 789 790 return fp; 791 } 792 793 void ksmbd_put_durable_fd(struct ksmbd_file *fp) 794 { 795 if (!atomic_dec_and_test(&fp->refcount)) 796 return; 797 798 __ksmbd_close_fd(NULL, fp); 799 } 800 801 bool ksmbd_has_other_active_fd(struct ksmbd_file *fp) 802 { 803 struct ksmbd_file *lfp; 804 struct ksmbd_inode *ci = fp->f_ci; 805 bool ret = false; 806 807 down_read(&ci->m_lock); 808 list_for_each_entry(lfp, &ci->m_fp_list, node) { 809 if (lfp == fp) 810 continue; 811 812 if (lfp->f_state == FP_INITED && 813 (READ_ONCE(lfp->conn) || READ_ONCE(lfp->tcon))) { 814 ret = true; 815 break; 816 } 817 } 818 up_read(&ci->m_lock); 819 820 return ret; 821 } 822 823 static struct ksmbd_file *ksmbd_lookup_fd_app_instance_id(char *app_instance_id) 824 { 825 struct ksmbd_file *fp = NULL; 826 unsigned int id; 827 828 if (!memchr_inv(app_instance_id, 0, SMB2_CREATE_GUID_SIZE)) 829 return NULL; 830 831 read_lock(&global_ft.lock); 832 idr_for_each_entry(global_ft.idr, fp, id) { 833 if (!memcmp(fp->app_instance_id, app_instance_id, 834 SMB2_CREATE_GUID_SIZE)) { 835 fp = ksmbd_fp_get(fp); 836 break; 837 } 838 } 839 read_unlock(&global_ft.lock); 840 841 return fp; 842 } 843 844 int ksmbd_close_fd_app_instance_id(char *app_instance_id) 845 { 846 struct ksmbd_file_table *ft; 847 struct ksmbd_file *fp; 848 struct oplock_info *opinfo; 849 int n_to_drop = 0; 850 851 fp = ksmbd_lookup_fd_app_instance_id(app_instance_id); 852 if (!fp) 853 return 0; 854 855 opinfo = opinfo_get(fp); 856 if (!opinfo) 857 goto out; 858 859 down_read(&fp->f_ci->m_lock); 860 if (!opinfo->conn) { 861 up_read(&fp->f_ci->m_lock); 862 goto out; 863 } 864 865 ft = &opinfo->sess->file_table; 866 write_lock(&ft->lock); 867 if (fp->f_state == FP_INITED && has_file_id(fp->volatile_id)) { 868 idr_remove(ft->idr, fp->volatile_id); 869 fp->volatile_id = KSMBD_NO_FID; 870 n_to_drop = ksmbd_mark_fp_closed(fp); 871 } 872 write_unlock(&ft->lock); 873 up_read(&fp->f_ci->m_lock); 874 opinfo_put(opinfo); 875 opinfo = NULL; 876 877 if (!n_to_drop) 878 goto out; 879 880 down_write(&fp->f_ci->m_lock); 881 list_del_init(&fp->node); 882 up_write(&fp->f_ci->m_lock); 883 884 if (atomic_sub_and_test(n_to_drop, &fp->refcount)) { 885 if (fp->conn) 886 atomic_dec(&fp->conn->stats.open_files_count); 887 __ksmbd_close_fd(NULL, fp); 888 } 889 return 0; 890 891 out: 892 if (opinfo) 893 opinfo_put(opinfo); 894 ksmbd_put_durable_fd(fp); 895 return 0; 896 } 897 898 int ksmbd_invalidate_durable_fd(unsigned long long id) 899 { 900 struct ksmbd_file *fp; 901 902 fp = ksmbd_lookup_global_fd(id); 903 if (!fp) 904 return -ENOENT; 905 906 fp->durable_reconnect_disabled = true; 907 908 if (fp->conn) { 909 ksmbd_put_durable_fd(fp); 910 return -ENOENT; 911 } 912 913 fp->durable_timeout = 1; 914 fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies); 915 ksmbd_put_durable_fd(fp); 916 if (waitqueue_active(&dh_wq)) 917 wake_up(&dh_wq); 918 919 return -ENOENT; 920 } 921 922 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid) 923 { 924 struct ksmbd_file *fp = NULL; 925 unsigned int id; 926 927 read_lock(&global_ft.lock); 928 idr_for_each_entry(global_ft.idr, fp, id) { 929 if (!memcmp(fp->create_guid, 930 cguid, 931 SMB2_CREATE_GUID_SIZE)) { 932 fp = ksmbd_fp_get(fp); 933 break; 934 } 935 } 936 read_unlock(&global_ft.lock); 937 938 return fp; 939 } 940 941 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry) 942 { 943 struct ksmbd_file *lfp; 944 struct ksmbd_inode *ci; 945 struct inode *inode = d_inode(dentry); 946 947 read_lock(&inode_hash_lock); 948 ci = __ksmbd_inode_lookup(dentry); 949 read_unlock(&inode_hash_lock); 950 if (!ci) 951 return NULL; 952 953 down_read(&ci->m_lock); 954 list_for_each_entry(lfp, &ci->m_fp_list, node) { 955 if (inode == file_inode(lfp->filp)) { 956 lfp = ksmbd_fp_get(lfp); 957 up_read(&ci->m_lock); 958 ksmbd_inode_put(ci); 959 return lfp; 960 } 961 } 962 up_read(&ci->m_lock); 963 ksmbd_inode_put(ci); 964 return NULL; 965 } 966 967 bool ksmbd_has_open_files(struct dentry *dentry) 968 { 969 struct ksmbd_file *fp; 970 unsigned int id; 971 bool ret = false; 972 973 read_lock(&global_ft.lock); 974 idr_for_each_entry(global_ft.idr, fp, id) { 975 struct dentry *fp_dentry = fp->filp->f_path.dentry; 976 977 if (fp->f_state != FP_INITED) 978 continue; 979 if (fp_dentry == dentry) 980 continue; 981 if (is_subdir(fp_dentry, dentry)) { 982 ret = true; 983 break; 984 } 985 } 986 read_unlock(&global_ft.lock); 987 988 return ret; 989 } 990 991 #define OPEN_ID_TYPE_VOLATILE_ID (0) 992 #define OPEN_ID_TYPE_PERSISTENT_ID (1) 993 994 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type) 995 { 996 if (type == OPEN_ID_TYPE_VOLATILE_ID) 997 fp->volatile_id = id; 998 if (type == OPEN_ID_TYPE_PERSISTENT_ID) 999 fp->persistent_id = id; 1000 } 1001 1002 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 1003 int type) 1004 { 1005 u64 id = 0; 1006 int ret; 1007 1008 if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) { 1009 __open_id_set(fp, KSMBD_NO_FID, type); 1010 return -EMFILE; 1011 } 1012 1013 idr_preload(KSMBD_DEFAULT_GFP); 1014 write_lock(&ft->lock); 1015 ret = idr_alloc_cyclic(ft->idr, fp, KSMBD_START_FID, INT_MAX - 1, 1016 GFP_NOWAIT); 1017 if (ret >= 0) { 1018 id = ret; 1019 ret = 0; 1020 } else { 1021 id = KSMBD_NO_FID; 1022 fd_limit_close(); 1023 } 1024 1025 __open_id_set(fp, id, type); 1026 write_unlock(&ft->lock); 1027 idr_preload_end(); 1028 return ret; 1029 } 1030 1031 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp) 1032 { 1033 __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID); 1034 return fp->persistent_id; 1035 } 1036 1037 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp) 1038 { 1039 struct ksmbd_file *fp; 1040 int ret; 1041 1042 fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP); 1043 if (!fp) { 1044 pr_err("Failed to allocate memory\n"); 1045 return ERR_PTR(-ENOMEM); 1046 } 1047 1048 INIT_LIST_HEAD(&fp->blocked_works); 1049 INIT_LIST_HEAD(&fp->node); 1050 INIT_LIST_HEAD(&fp->lock_list); 1051 spin_lock_init(&fp->f_lock); 1052 mutex_init(&fp->readdir_lock); 1053 atomic_set(&fp->refcount, 1); 1054 1055 fp->filp = filp; 1056 /* 1057 * fp owns a strong reference on fp->conn for as long as fp->conn is 1058 * non-NULL, so session_fd_check() and __ksmbd_close_fd() never 1059 * dereference a dangling pointer. Paired with ksmbd_conn_put() in 1060 * session_fd_check() (durable preserve), in __ksmbd_close_fd() 1061 * (final close), and on the error paths below. 1062 */ 1063 fp->conn = ksmbd_conn_get(work->conn); 1064 fp->tcon = work->tcon; 1065 fp->volatile_id = KSMBD_NO_FID; 1066 fp->persistent_id = KSMBD_NO_FID; 1067 fp->f_state = FP_NEW; 1068 fp->f_ci = ksmbd_inode_get(fp); 1069 1070 if (!fp->f_ci) { 1071 ret = -ENOMEM; 1072 goto err_out; 1073 } 1074 1075 ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); 1076 if (ret) { 1077 ksmbd_inode_put(fp->f_ci); 1078 goto err_out; 1079 } 1080 1081 atomic_inc(&work->conn->stats.open_files_count); 1082 return fp; 1083 1084 err_out: 1085 /* fp->conn was set and refcounted before every branch here. */ 1086 ksmbd_conn_put(fp->conn); 1087 kmem_cache_free(filp_cache, fp); 1088 return ERR_PTR(ret); 1089 } 1090 1091 /** 1092 * ksmbd_update_fstate() - update an fp state under the file-table lock 1093 * @ft: file table that publishes @fp's volatile id 1094 * @fp: file pointer to update 1095 * @state: new state 1096 * 1097 * Return: 0 on success. The FP_NEW -> FP_INITED transition is special: 1098 * -ENOENT if teardown already unpublished @fp by advancing the state or 1099 * clearing the volatile id. Other state updates preserve the historical 1100 * fire-and-forget behavior. 1101 */ 1102 int ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 1103 unsigned int state) 1104 { 1105 int ret; 1106 1107 if (!fp) 1108 return -ENOENT; 1109 1110 write_lock(&ft->lock); 1111 if (state == FP_INITED && 1112 (fp->f_state != FP_NEW || !has_file_id(fp->volatile_id))) { 1113 ret = -ENOENT; 1114 } else { 1115 fp->f_state = state; 1116 ret = 0; 1117 } 1118 write_unlock(&ft->lock); 1119 1120 return ret; 1121 } 1122 1123 /* 1124 * ksmbd_mark_fp_closed() - mark fp closed under ft->lock and return how many 1125 * refs the teardown path owns. 1126 * 1127 * FP_INITED has a normal idr-owned reference, so teardown owns both that 1128 * reference and the transient lookup reference. FP_NEW is still owned by the 1129 * in-flight opener/reopener, which will drop the original reference after 1130 * ksmbd_update_fstate(..., FP_INITED) observes the cleared volatile id. 1131 * FP_CLOSED on entry means an earlier ksmbd_close_fd() already consumed the 1132 * idr-owned ref. 1133 */ 1134 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp) 1135 { 1136 if (fp->f_state == FP_INITED) { 1137 set_close_state_blocked_works(fp); 1138 fp->f_state = FP_CLOSED; 1139 return 2; 1140 } 1141 1142 return 1; 1143 } 1144 1145 static int 1146 __close_file_table_ids(struct ksmbd_session *sess, 1147 struct ksmbd_tree_connect *tcon, 1148 bool (*skip)(struct ksmbd_tree_connect *tcon, 1149 struct ksmbd_file *fp, 1150 struct ksmbd_user *user), 1151 bool skip_preserves_fp) 1152 { 1153 struct ksmbd_file_table *ft = &sess->file_table; 1154 struct ksmbd_file *fp; 1155 unsigned int id = 0; 1156 int num = 0; 1157 1158 while (1) { 1159 int n_to_drop; 1160 1161 write_lock(&ft->lock); 1162 fp = idr_get_next(ft->idr, &id); 1163 if (!fp) { 1164 write_unlock(&ft->lock); 1165 break; 1166 } 1167 if (!atomic_inc_not_zero(&fp->refcount)) { 1168 id++; 1169 write_unlock(&ft->lock); 1170 continue; 1171 } 1172 1173 if (skip_preserves_fp) { 1174 /* 1175 * Session teardown: skip() is session_fd_check(), 1176 * which may sleep and mutates fp->conn / fp->tcon / 1177 * fp->volatile_id when it chooses to preserve fp 1178 * for durable reconnect. Unpublish fp from the 1179 * session idr here, under ft->lock, so that 1180 * __ksmbd_lookup_fd() through this session cannot 1181 * grant a new ksmbd_fp_get() reference to an fp 1182 * whose fields are about to be rewritten outside 1183 * the lock. Durable reconnect still reaches fp via 1184 * global_ft. 1185 */ 1186 idr_remove(ft->idr, id); 1187 fp->durable_volatile_id = fp->volatile_id; 1188 fp->volatile_id = KSMBD_NO_FID; 1189 write_unlock(&ft->lock); 1190 1191 if (skip(tcon, fp, sess->user)) { 1192 /* 1193 * session_fd_check() has converted fp to 1194 * durable-preserve state and cleared its 1195 * per-conn fields. fp is already unpublished 1196 * above; the original idr-owned ref keeps it 1197 * alive for the durable scavenger. Drop only 1198 * the transient ref. atomic_dec() is safe -- 1199 * atomic_inc_not_zero() succeeded on a 1200 * positive value and we added one more, so 1201 * refcount cannot be zero here. 1202 */ 1203 atomic_dec(&fp->refcount); 1204 id++; 1205 continue; 1206 } 1207 1208 /* 1209 * Keep the close-state decision under the same lock 1210 * observed by ksmbd_update_fstate(), which is how an 1211 * in-flight FP_NEW opener learns that teardown has 1212 * cleared its volatile id. 1213 */ 1214 write_lock(&ft->lock); 1215 n_to_drop = ksmbd_mark_fp_closed(fp); 1216 write_unlock(&ft->lock); 1217 } else { 1218 /* 1219 * Tree teardown: skip() is tree_conn_fd_check(), a 1220 * cheap pointer compare that doesn't sleep and has 1221 * no side effects, so keep the skip decision plus 1222 * the unpublish-and-mark-closed sequence atomic 1223 * under ft->lock. fps belonging to other tree 1224 * connects (skip() == true) stay fully published in 1225 * the session idr with no lock window. 1226 */ 1227 if (skip(tcon, fp, sess->user)) { 1228 atomic_dec(&fp->refcount); 1229 write_unlock(&ft->lock); 1230 id++; 1231 continue; 1232 } 1233 idr_remove(ft->idr, id); 1234 fp->volatile_id = KSMBD_NO_FID; 1235 n_to_drop = ksmbd_mark_fp_closed(fp); 1236 write_unlock(&ft->lock); 1237 } 1238 1239 /* 1240 * fp->volatile_id is already cleared to prevent stale idr 1241 * removal from a deferred final close. Remove fp from 1242 * m_fp_list here because __ksmbd_remove_fd() will skip the 1243 * list unlink when volatile_id is KSMBD_NO_FID. 1244 */ 1245 down_write(&fp->f_ci->m_lock); 1246 list_del_init(&fp->node); 1247 up_write(&fp->f_ci->m_lock); 1248 1249 /* 1250 * Drop the references this iteration owns: 1251 * 1252 * n_to_drop == 2: we observed FP_INITED and committed 1253 * the FP_CLOSED transition ourselves, so we own the 1254 * transient (+1) and the still-intact idr-owned ref. 1255 * 1256 * n_to_drop == 1: either a prior ksmbd_close_fd() 1257 * already consumed the idr-owned ref, or fp was still 1258 * FP_NEW and the in-flight opener/reopener must keep 1259 * the original reference until ksmbd_update_fstate() 1260 * observes the cleared volatile id. 1261 * 1262 * If we end up as the final putter, finalize fp and 1263 * account the open_files_count decrement via the caller's 1264 * atomic_sub(num, ...). Otherwise the remaining user's 1265 * ksmbd_fd_put() reaches __put_fd_final(), which does its 1266 * own atomic_dec(&open_files_count), so we must not count 1267 * this fp here -- doing so would double-decrement the 1268 * connection-wide counter. 1269 */ 1270 if (atomic_sub_and_test(n_to_drop, &fp->refcount)) { 1271 __ksmbd_close_fd(NULL, fp); 1272 num++; 1273 } 1274 id++; 1275 } 1276 1277 return num; 1278 } 1279 1280 static inline bool is_reconnectable(struct ksmbd_file *fp) 1281 { 1282 struct oplock_info *opinfo = opinfo_get(fp); 1283 bool reconn = false; 1284 1285 if (!opinfo) 1286 return false; 1287 1288 if (opinfo->op_state != OPLOCK_STATE_NONE) { 1289 opinfo_put(opinfo); 1290 return false; 1291 } 1292 1293 if (fp->is_resilient || fp->is_persistent) 1294 reconn = true; 1295 else if (fp->is_durable && opinfo->is_lease && 1296 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 1297 reconn = true; 1298 1299 else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) 1300 reconn = true; 1301 1302 opinfo_put(opinfo); 1303 return reconn; 1304 } 1305 1306 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon, 1307 struct ksmbd_file *fp, 1308 struct ksmbd_user *user) 1309 { 1310 return fp->tcon != tcon; 1311 } 1312 1313 static bool ksmbd_durable_scavenger_alive(void) 1314 { 1315 if (!durable_scavenger_running) 1316 return false; 1317 1318 if (kthread_should_stop()) 1319 return false; 1320 1321 if (idr_is_empty(global_ft.idr)) 1322 return false; 1323 1324 return true; 1325 } 1326 1327 static void ksmbd_scavenger_dispose_dh(struct ksmbd_file *fp) 1328 { 1329 /* 1330 * Durable-preserved fp can remain linked on f_ci->m_fp_list for 1331 * share-mode checks. Unlink it before final close; fp->node is not 1332 * available as a scavenger-private list node because re-adding it to 1333 * another list corrupts m_fp_list. 1334 */ 1335 down_write(&fp->f_ci->m_lock); 1336 list_del_init(&fp->node); 1337 up_write(&fp->f_ci->m_lock); 1338 1339 /* 1340 * Drop both the durable lifetime reference and the transient reference 1341 * taken by the scavenger under global_ft.lock. If a concurrent 1342 * ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp 1343 * before the unlink above, that holder owns the final close via 1344 * ksmbd_fd_put() -> __ksmbd_close_fd(). Otherwise the scavenger is 1345 * the last putter and finalises fp here. 1346 */ 1347 if (atomic_sub_and_test(2, &fp->refcount)) 1348 __ksmbd_close_fd(NULL, fp); 1349 } 1350 1351 static int ksmbd_durable_scavenger(void *dummy) 1352 { 1353 struct ksmbd_file *fp = NULL; 1354 struct ksmbd_file *expired_fp; 1355 unsigned int id; 1356 unsigned int min_timeout = 1; 1357 bool found_fp_timeout; 1358 unsigned long remaining_jiffies; 1359 1360 __module_get(THIS_MODULE); 1361 1362 set_freezable(); 1363 while (ksmbd_durable_scavenger_alive()) { 1364 if (try_to_freeze()) 1365 continue; 1366 1367 remaining_jiffies = wait_event_interruptible_timeout(dh_wq, 1368 ksmbd_durable_scavenger_alive() == false, 1369 __msecs_to_jiffies(min_timeout)); 1370 if ((long)remaining_jiffies > 0) 1371 min_timeout = jiffies_to_msecs(remaining_jiffies); 1372 else 1373 min_timeout = DURABLE_HANDLE_MAX_TIMEOUT; 1374 1375 do { 1376 expired_fp = NULL; 1377 found_fp_timeout = false; 1378 1379 write_lock(&global_ft.lock); 1380 idr_for_each_entry(global_ft.idr, fp, id) { 1381 unsigned long durable_timeout; 1382 1383 if (!fp->durable_timeout) 1384 continue; 1385 1386 if (atomic_read(&fp->refcount) > 1 || 1387 fp->conn) 1388 continue; 1389 1390 found_fp_timeout = true; 1391 if (fp->durable_scavenger_timeout <= 1392 jiffies_to_msecs(jiffies)) { 1393 __ksmbd_remove_durable_fd(fp); 1394 /* 1395 * Take a transient reference so fp 1396 * cannot be freed by an in-flight 1397 * ksmbd_lookup_fd_inode() that found 1398 * it through f_ci->m_fp_list while we 1399 * drop global_ft.lock and reach the 1400 * m_fp_list unlink in 1401 * ksmbd_scavenger_dispose_dh(). 1402 */ 1403 atomic_inc(&fp->refcount); 1404 expired_fp = fp; 1405 break; 1406 } 1407 1408 durable_timeout = 1409 fp->durable_scavenger_timeout - 1410 jiffies_to_msecs(jiffies); 1411 1412 if (min_timeout > durable_timeout) 1413 min_timeout = durable_timeout; 1414 } 1415 write_unlock(&global_ft.lock); 1416 1417 if (expired_fp) 1418 ksmbd_scavenger_dispose_dh(expired_fp); 1419 } while (expired_fp); 1420 1421 if (found_fp_timeout == false) 1422 break; 1423 } 1424 1425 durable_scavenger_running = false; 1426 1427 module_put(THIS_MODULE); 1428 1429 return 0; 1430 } 1431 1432 void ksmbd_launch_ksmbd_durable_scavenger(void) 1433 { 1434 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE)) 1435 return; 1436 1437 mutex_lock(&durable_scavenger_lock); 1438 if (durable_scavenger_running == true) { 1439 mutex_unlock(&durable_scavenger_lock); 1440 return; 1441 } 1442 1443 durable_scavenger_running = true; 1444 1445 server_conf.dh_task = kthread_run(ksmbd_durable_scavenger, 1446 (void *)NULL, "ksmbd-durable-scavenger"); 1447 if (IS_ERR(server_conf.dh_task)) 1448 pr_err("cannot start conn thread, err : %ld\n", 1449 PTR_ERR(server_conf.dh_task)); 1450 mutex_unlock(&durable_scavenger_lock); 1451 } 1452 1453 void ksmbd_stop_durable_scavenger(void) 1454 { 1455 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE)) 1456 return; 1457 1458 mutex_lock(&durable_scavenger_lock); 1459 if (!durable_scavenger_running) { 1460 mutex_unlock(&durable_scavenger_lock); 1461 return; 1462 } 1463 1464 durable_scavenger_running = false; 1465 if (waitqueue_active(&dh_wq)) 1466 wake_up(&dh_wq); 1467 mutex_unlock(&durable_scavenger_lock); 1468 kthread_stop(server_conf.dh_task); 1469 } 1470 1471 /* 1472 * ksmbd_vfs_copy_durable_owner - Copy owner info for durable reconnect 1473 * @fp: ksmbd file pointer to store owner info 1474 * @user: user pointer to copy from 1475 * 1476 * This function binds the current user's identity to the file handle 1477 * to satisfy MS-SMB2 Step 8 (SecurityContext matching) during reconnect. 1478 * 1479 * Return: 0 on success, or negative error code on failure 1480 */ 1481 static int ksmbd_vfs_copy_durable_owner(struct ksmbd_file *fp, 1482 struct ksmbd_user *user) 1483 { 1484 char *name; 1485 1486 if (!user) 1487 return -EINVAL; 1488 1489 /* Duplicate the user name to ensure identity persistence */ 1490 name = kstrdup(user->name, GFP_KERNEL); 1491 if (!name) 1492 return -ENOMEM; 1493 1494 spin_lock(&fp->f_lock); 1495 fp->owner.uid = user->uid; 1496 fp->owner.gid = user->gid; 1497 fp->owner.name = name; 1498 spin_unlock(&fp->f_lock); 1499 1500 return 0; 1501 } 1502 1503 /** 1504 * ksmbd_vfs_compare_durable_owner - Verify if the requester is original owner 1505 * @fp: existing ksmbd file pointer 1506 * @user: user pointer of the reconnect requester 1507 * 1508 * Compares the UID, GID, and name of the current requester against the 1509 * original owner stored in the file handle. 1510 * 1511 * Return: true if the user matches, false otherwise 1512 */ 1513 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp, 1514 struct ksmbd_user *user) 1515 { 1516 bool ret = false; 1517 1518 if (!user) 1519 return false; 1520 1521 spin_lock(&fp->f_lock); 1522 if (!fp->owner.name) 1523 goto out; 1524 1525 /* Check if the UID and GID match first (fast path) */ 1526 if (fp->owner.uid != user->uid || fp->owner.gid != user->gid) 1527 goto out; 1528 1529 /* Validate the account name to ensure the same SecurityContext */ 1530 ret = (strcmp(fp->owner.name, user->name) == 0); 1531 out: 1532 spin_unlock(&fp->f_lock); 1533 return ret; 1534 } 1535 1536 static bool session_fd_check(struct ksmbd_tree_connect *tcon, 1537 struct ksmbd_file *fp, struct ksmbd_user *user) 1538 { 1539 struct ksmbd_inode *ci; 1540 struct oplock_info *op; 1541 struct ksmbd_conn *conn; 1542 struct ksmbd_lock *smb_lock, *tmp_lock; 1543 1544 if (!is_reconnectable(fp)) 1545 return false; 1546 1547 if (fp->f_state != FP_INITED) 1548 return false; 1549 1550 if (WARN_ON_ONCE(!fp->conn)) 1551 return false; 1552 1553 if (ksmbd_vfs_copy_durable_owner(fp, user)) 1554 return false; 1555 1556 /* 1557 * fp owns a strong reference on fp->conn (taken in ksmbd_open_fd() 1558 * / ksmbd_reopen_durable_fd()), so conn stays valid for the whole 1559 * body of this function regardless of any op->conn puts below. 1560 */ 1561 conn = fp->conn; 1562 ci = fp->f_ci; 1563 down_write(&ci->m_lock); 1564 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 1565 lockdep_is_held(&ci->m_lock)) { 1566 if (op->conn != conn) 1567 continue; 1568 ksmbd_conn_put(op->conn); 1569 op->conn = NULL; 1570 op->sess = NULL; 1571 } 1572 up_write(&ci->m_lock); 1573 1574 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) { 1575 struct ksmbd_conn *lock_conn = smb_lock->conn; 1576 1577 if (!lock_conn) 1578 continue; 1579 spin_lock(&lock_conn->llist_lock); 1580 list_del_init(&smb_lock->clist); 1581 smb_lock->conn = NULL; 1582 spin_unlock(&lock_conn->llist_lock); 1583 ksmbd_conn_put(lock_conn); 1584 } 1585 1586 fp->conn = NULL; 1587 fp->tcon = NULL; 1588 fp->volatile_id = KSMBD_NO_FID; 1589 1590 if (fp->durable_timeout) 1591 fp->durable_scavenger_timeout = 1592 jiffies_to_msecs(jiffies) + fp->durable_timeout; 1593 1594 /* Drop fp's own reference on conn. */ 1595 ksmbd_conn_put(conn); 1596 return true; 1597 } 1598 1599 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work) 1600 { 1601 int num = __close_file_table_ids(work->sess, 1602 work->tcon, 1603 tree_conn_fd_check, 1604 false); 1605 1606 atomic_sub(num, &work->conn->stats.open_files_count); 1607 } 1608 1609 void ksmbd_close_session_fds(struct ksmbd_work *work) 1610 { 1611 int num = __close_file_table_ids(work->sess, 1612 work->tcon, 1613 session_fd_check, 1614 true); 1615 1616 atomic_sub(num, &work->conn->stats.open_files_count); 1617 } 1618 1619 int ksmbd_init_global_file_table(void) 1620 { 1621 create_proc_files(); 1622 return ksmbd_init_file_table(&global_ft); 1623 } 1624 1625 void ksmbd_free_global_file_table(void) 1626 { 1627 struct ksmbd_file *fp = NULL; 1628 unsigned int id; 1629 1630 idr_for_each_entry(global_ft.idr, fp, id) { 1631 ksmbd_remove_durable_fd(fp); 1632 __ksmbd_close_fd(NULL, fp); 1633 } 1634 1635 idr_destroy(global_ft.idr); 1636 kfree(global_ft.idr); 1637 } 1638 1639 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share, 1640 struct ksmbd_file *fp, char *name) 1641 { 1642 char *pathname, *ab_pathname; 1643 int ret = 0; 1644 1645 pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP); 1646 if (!pathname) 1647 return -EACCES; 1648 1649 ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX); 1650 if (IS_ERR(ab_pathname)) { 1651 kfree(pathname); 1652 return -EACCES; 1653 } 1654 1655 if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) { 1656 ksmbd_debug(SMB, "invalid name reconnect %s\n", name); 1657 ret = -EINVAL; 1658 } 1659 1660 kfree(pathname); 1661 1662 return ret; 1663 } 1664 1665 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp) 1666 { 1667 struct ksmbd_inode *ci; 1668 struct oplock_info *op; 1669 struct ksmbd_conn *conn = work->conn; 1670 struct ksmbd_lock *smb_lock; 1671 unsigned int old_f_state; 1672 1673 write_lock(&global_ft.lock); 1674 if (!fp->is_durable || fp->conn || fp->tcon) { 1675 write_unlock(&global_ft.lock); 1676 pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon); 1677 return -EBADF; 1678 } 1679 1680 if (has_file_id(fp->volatile_id)) { 1681 write_unlock(&global_ft.lock); 1682 pr_err("Still in use durable fd: %llu\n", fp->volatile_id); 1683 return -EBADF; 1684 } 1685 1686 /* 1687 * Initialize fp's connection binding before publishing fp into the 1688 * session's file table. If __open_id() is ordered first, a 1689 * concurrent teardown that iterates the table can observe a valid 1690 * volatile_id with fp->conn == NULL and preserve a 1691 * partially-initialized fp. fp owns a strong reference on the new 1692 * conn (see ksmbd_open_fd()); undo it on __open_id() failure. 1693 */ 1694 fp->conn = ksmbd_conn_get(conn); 1695 fp->tcon = work->tcon; 1696 write_unlock(&global_ft.lock); 1697 1698 old_f_state = fp->f_state; 1699 fp->f_state = FP_NEW; 1700 1701 __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); 1702 if (!has_file_id(fp->volatile_id)) { 1703 write_lock(&global_ft.lock); 1704 fp->conn = NULL; 1705 fp->tcon = NULL; 1706 write_unlock(&global_ft.lock); 1707 ksmbd_conn_put(conn); 1708 fp->f_state = old_f_state; 1709 return -EBADF; 1710 } 1711 1712 list_for_each_entry(smb_lock, &fp->lock_list, flist) { 1713 smb_lock->conn = ksmbd_conn_get(conn); 1714 spin_lock(&conn->llist_lock); 1715 list_add_tail(&smb_lock->clist, &conn->lock_list); 1716 spin_unlock(&conn->llist_lock); 1717 } 1718 1719 ci = fp->f_ci; 1720 down_write(&ci->m_lock); 1721 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 1722 lockdep_is_held(&ci->m_lock)) { 1723 if (op->conn) 1724 continue; 1725 op->conn = ksmbd_conn_get(fp->conn); 1726 op->sess = work->sess; 1727 } 1728 up_write(&ci->m_lock); 1729 1730 spin_lock(&fp->f_lock); 1731 fp->owner.uid = fp->owner.gid = 0; 1732 kfree(fp->owner.name); 1733 fp->owner.name = NULL; 1734 spin_unlock(&fp->f_lock); 1735 1736 return 0; 1737 } 1738 1739 int ksmbd_init_file_table(struct ksmbd_file_table *ft) 1740 { 1741 ft->idr = kzalloc_obj(struct idr, KSMBD_DEFAULT_GFP); 1742 if (!ft->idr) 1743 return -ENOMEM; 1744 1745 idr_init(ft->idr); 1746 rwlock_init(&ft->lock); 1747 return 0; 1748 } 1749 1750 void ksmbd_destroy_file_table(struct ksmbd_session *sess) 1751 { 1752 struct ksmbd_file_table *ft = &sess->file_table; 1753 1754 if (!ft->idr) 1755 return; 1756 1757 __close_file_table_ids(sess, NULL, session_fd_check, true); 1758 idr_destroy(ft->idr); 1759 kfree(ft->idr); 1760 ft->idr = NULL; 1761 } 1762 1763 int ksmbd_init_file_cache(void) 1764 { 1765 filp_cache = kmem_cache_create("ksmbd_file_cache", 1766 sizeof(struct ksmbd_file), 0, 1767 SLAB_HWCACHE_ALIGN, NULL); 1768 if (!filp_cache) 1769 goto out; 1770 1771 init_waitqueue_head(&dh_wq); 1772 1773 return 0; 1774 1775 out: 1776 pr_err("failed to allocate file cache\n"); 1777 return -ENOMEM; 1778 } 1779 1780 void ksmbd_exit_file_cache(void) 1781 { 1782 kmem_cache_destroy(filp_cache); 1783 } 1784