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 idr_remove(ft->idr, id); 701 fp->volatile_id = KSMBD_NO_FID; 702 closed = true; 703 if (!atomic_dec_and_test(&fp->refcount)) 704 fp = NULL; 705 } 706 } 707 write_unlock(&ft->lock); 708 709 if (!fp) 710 return closed ? 0 : -EINVAL; 711 712 __put_fd_final(work, fp); 713 return 0; 714 } 715 716 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp) 717 { 718 if (!fp) 719 return; 720 721 if (!atomic_dec_and_test(&fp->refcount)) 722 return; 723 __put_fd_final(work, fp); 724 } 725 726 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) 727 { 728 if (!fp) 729 return false; 730 if (fp->tcon != tcon) 731 return false; 732 return true; 733 } 734 735 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id) 736 { 737 return __ksmbd_lookup_fd(&work->sess->file_table, id); 738 } 739 740 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id) 741 { 742 struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 743 744 if (__sanity_check(work->tcon, fp)) 745 return fp; 746 747 ksmbd_fd_put(work, fp); 748 return NULL; 749 } 750 751 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id, 752 u64 pid) 753 { 754 struct ksmbd_file *fp; 755 756 if (!has_file_id(id)) { 757 id = work->compound_fid; 758 pid = work->compound_pfid; 759 } 760 761 fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 762 if (!__sanity_check(work->tcon, fp)) { 763 ksmbd_fd_put(work, fp); 764 return NULL; 765 } 766 if (fp->persistent_id != pid) { 767 ksmbd_fd_put(work, fp); 768 return NULL; 769 } 770 return fp; 771 } 772 773 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id) 774 { 775 return __ksmbd_lookup_fd(&global_ft, id); 776 } 777 778 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) 779 { 780 struct ksmbd_file *fp; 781 782 fp = __ksmbd_lookup_fd(&global_ft, id); 783 if (fp && (fp->durable_reconnect_disabled || 784 fp->conn || 785 (fp->durable_scavenger_timeout && 786 (fp->durable_scavenger_timeout < 787 jiffies_to_msecs(jiffies))))) { 788 ksmbd_put_durable_fd(fp); 789 fp = NULL; 790 } 791 792 return fp; 793 } 794 795 void ksmbd_put_durable_fd(struct ksmbd_file *fp) 796 { 797 if (!atomic_dec_and_test(&fp->refcount)) 798 return; 799 800 __ksmbd_close_fd(NULL, fp); 801 } 802 803 bool ksmbd_has_other_active_fd(struct ksmbd_file *fp) 804 { 805 struct ksmbd_file *lfp; 806 struct ksmbd_inode *ci = fp->f_ci; 807 bool ret = false; 808 809 down_read(&ci->m_lock); 810 list_for_each_entry(lfp, &ci->m_fp_list, node) { 811 if (lfp == fp) 812 continue; 813 814 if (lfp->f_state == FP_INITED && 815 (READ_ONCE(lfp->conn) || READ_ONCE(lfp->tcon))) { 816 ret = true; 817 break; 818 } 819 } 820 up_read(&ci->m_lock); 821 822 return ret; 823 } 824 825 static struct ksmbd_file *ksmbd_lookup_fd_app_instance_id(char *app_instance_id) 826 { 827 struct ksmbd_file *fp = NULL; 828 unsigned int id; 829 830 if (!memchr_inv(app_instance_id, 0, SMB2_CREATE_GUID_SIZE)) 831 return NULL; 832 833 read_lock(&global_ft.lock); 834 idr_for_each_entry(global_ft.idr, fp, id) { 835 if (!memcmp(fp->app_instance_id, app_instance_id, 836 SMB2_CREATE_GUID_SIZE)) { 837 fp = ksmbd_fp_get(fp); 838 break; 839 } 840 } 841 read_unlock(&global_ft.lock); 842 843 return fp; 844 } 845 846 int ksmbd_close_fd_app_instance_id(char *app_instance_id) 847 { 848 struct ksmbd_file_table *ft; 849 struct ksmbd_file *fp; 850 struct oplock_info *opinfo; 851 int n_to_drop = 0; 852 853 fp = ksmbd_lookup_fd_app_instance_id(app_instance_id); 854 if (!fp) 855 return 0; 856 857 opinfo = opinfo_get(fp); 858 if (!opinfo) 859 goto out; 860 861 down_read(&fp->f_ci->m_lock); 862 if (!opinfo->conn) { 863 up_read(&fp->f_ci->m_lock); 864 goto out; 865 } 866 867 ft = &opinfo->sess->file_table; 868 write_lock(&ft->lock); 869 if (fp->f_state == FP_INITED && has_file_id(fp->volatile_id)) { 870 idr_remove(ft->idr, fp->volatile_id); 871 fp->volatile_id = KSMBD_NO_FID; 872 n_to_drop = ksmbd_mark_fp_closed(fp); 873 } 874 write_unlock(&ft->lock); 875 up_read(&fp->f_ci->m_lock); 876 opinfo_put(opinfo); 877 opinfo = NULL; 878 879 if (!n_to_drop) 880 goto out; 881 882 down_write(&fp->f_ci->m_lock); 883 list_del_init(&fp->node); 884 up_write(&fp->f_ci->m_lock); 885 886 if (atomic_sub_and_test(n_to_drop, &fp->refcount)) { 887 if (fp->conn) 888 atomic_dec(&fp->conn->stats.open_files_count); 889 __ksmbd_close_fd(NULL, fp); 890 } 891 return 0; 892 893 out: 894 if (opinfo) 895 opinfo_put(opinfo); 896 ksmbd_put_durable_fd(fp); 897 return 0; 898 } 899 900 int ksmbd_invalidate_durable_fd(unsigned long long id) 901 { 902 struct ksmbd_file *fp; 903 904 fp = ksmbd_lookup_global_fd(id); 905 if (!fp) 906 return -ENOENT; 907 908 fp->durable_reconnect_disabled = true; 909 910 if (fp->conn) { 911 ksmbd_put_durable_fd(fp); 912 return -ENOENT; 913 } 914 915 fp->durable_timeout = 1; 916 fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies); 917 ksmbd_put_durable_fd(fp); 918 if (waitqueue_active(&dh_wq)) 919 wake_up(&dh_wq); 920 921 return -ENOENT; 922 } 923 924 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid) 925 { 926 struct ksmbd_file *fp = NULL; 927 unsigned int id; 928 929 read_lock(&global_ft.lock); 930 idr_for_each_entry(global_ft.idr, fp, id) { 931 if (!memcmp(fp->create_guid, 932 cguid, 933 SMB2_CREATE_GUID_SIZE)) { 934 fp = ksmbd_fp_get(fp); 935 break; 936 } 937 } 938 read_unlock(&global_ft.lock); 939 940 return fp; 941 } 942 943 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry) 944 { 945 struct ksmbd_file *lfp; 946 struct ksmbd_inode *ci; 947 struct inode *inode = d_inode(dentry); 948 949 read_lock(&inode_hash_lock); 950 ci = __ksmbd_inode_lookup(dentry); 951 read_unlock(&inode_hash_lock); 952 if (!ci) 953 return NULL; 954 955 down_read(&ci->m_lock); 956 list_for_each_entry(lfp, &ci->m_fp_list, node) { 957 if (inode == file_inode(lfp->filp)) { 958 lfp = ksmbd_fp_get(lfp); 959 up_read(&ci->m_lock); 960 ksmbd_inode_put(ci); 961 return lfp; 962 } 963 } 964 up_read(&ci->m_lock); 965 ksmbd_inode_put(ci); 966 return NULL; 967 } 968 969 bool ksmbd_has_open_files(struct dentry *dentry) 970 { 971 struct ksmbd_file *fp; 972 unsigned int id; 973 bool ret = false; 974 975 read_lock(&global_ft.lock); 976 idr_for_each_entry(global_ft.idr, fp, id) { 977 struct dentry *fp_dentry = fp->filp->f_path.dentry; 978 979 if (fp->f_state != FP_INITED) 980 continue; 981 if (fp_dentry == dentry) 982 continue; 983 if (is_subdir(fp_dentry, dentry)) { 984 ret = true; 985 break; 986 } 987 } 988 read_unlock(&global_ft.lock); 989 990 return ret; 991 } 992 993 #define OPEN_ID_TYPE_VOLATILE_ID (0) 994 #define OPEN_ID_TYPE_PERSISTENT_ID (1) 995 996 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type) 997 { 998 if (type == OPEN_ID_TYPE_VOLATILE_ID) 999 fp->volatile_id = id; 1000 if (type == OPEN_ID_TYPE_PERSISTENT_ID) 1001 fp->persistent_id = id; 1002 } 1003 1004 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 1005 int type) 1006 { 1007 u64 id = 0; 1008 int ret; 1009 1010 if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) { 1011 __open_id_set(fp, KSMBD_NO_FID, type); 1012 return -EMFILE; 1013 } 1014 1015 idr_preload(KSMBD_DEFAULT_GFP); 1016 write_lock(&ft->lock); 1017 ret = idr_alloc_cyclic(ft->idr, fp, KSMBD_START_FID, INT_MAX - 1, 1018 GFP_NOWAIT); 1019 if (ret >= 0) { 1020 id = ret; 1021 ret = 0; 1022 } else { 1023 id = KSMBD_NO_FID; 1024 fd_limit_close(); 1025 } 1026 1027 __open_id_set(fp, id, type); 1028 write_unlock(&ft->lock); 1029 idr_preload_end(); 1030 return ret; 1031 } 1032 1033 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp) 1034 { 1035 __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID); 1036 return fp->persistent_id; 1037 } 1038 1039 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp) 1040 { 1041 struct ksmbd_file *fp; 1042 int ret; 1043 1044 fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP); 1045 if (!fp) { 1046 pr_err("Failed to allocate memory\n"); 1047 return ERR_PTR(-ENOMEM); 1048 } 1049 1050 INIT_LIST_HEAD(&fp->blocked_works); 1051 INIT_LIST_HEAD(&fp->node); 1052 INIT_LIST_HEAD(&fp->lock_list); 1053 spin_lock_init(&fp->f_lock); 1054 mutex_init(&fp->readdir_lock); 1055 atomic_set(&fp->refcount, 1); 1056 1057 fp->filp = filp; 1058 /* 1059 * fp owns a strong reference on fp->conn for as long as fp->conn is 1060 * non-NULL, so session_fd_check() and __ksmbd_close_fd() never 1061 * dereference a dangling pointer. Paired with ksmbd_conn_put() in 1062 * session_fd_check() (durable preserve), in __ksmbd_close_fd() 1063 * (final close), and on the error paths below. 1064 */ 1065 fp->conn = ksmbd_conn_get(work->conn); 1066 fp->tcon = work->tcon; 1067 fp->volatile_id = KSMBD_NO_FID; 1068 fp->persistent_id = KSMBD_NO_FID; 1069 fp->f_state = FP_NEW; 1070 fp->f_ci = ksmbd_inode_get(fp); 1071 1072 if (!fp->f_ci) { 1073 ret = -ENOMEM; 1074 goto err_out; 1075 } 1076 1077 ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); 1078 if (ret) { 1079 ksmbd_inode_put(fp->f_ci); 1080 goto err_out; 1081 } 1082 1083 atomic_inc(&work->conn->stats.open_files_count); 1084 return fp; 1085 1086 err_out: 1087 /* fp->conn was set and refcounted before every branch here. */ 1088 ksmbd_conn_put(fp->conn); 1089 kmem_cache_free(filp_cache, fp); 1090 return ERR_PTR(ret); 1091 } 1092 1093 /** 1094 * ksmbd_update_fstate() - update an fp state under the file-table lock 1095 * @ft: file table that publishes @fp's volatile id 1096 * @fp: file pointer to update 1097 * @state: new state 1098 * 1099 * Return: 0 on success. The FP_NEW -> FP_INITED transition is special: 1100 * -ENOENT if teardown already unpublished @fp by advancing the state or 1101 * clearing the volatile id. Other state updates preserve the historical 1102 * fire-and-forget behavior. 1103 */ 1104 int ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 1105 unsigned int state) 1106 { 1107 int ret; 1108 1109 if (!fp) 1110 return -ENOENT; 1111 1112 write_lock(&ft->lock); 1113 if (state == FP_INITED && 1114 (fp->f_state != FP_NEW || !has_file_id(fp->volatile_id))) { 1115 ret = -ENOENT; 1116 } else { 1117 fp->f_state = state; 1118 ret = 0; 1119 } 1120 write_unlock(&ft->lock); 1121 1122 return ret; 1123 } 1124 1125 /* 1126 * ksmbd_mark_fp_closed() - mark fp closed under ft->lock and return how many 1127 * refs the teardown path owns. 1128 * 1129 * FP_INITED has a normal idr-owned reference, so teardown owns both that 1130 * reference and the transient lookup reference. FP_NEW is still owned by the 1131 * in-flight opener/reopener, which will drop the original reference after 1132 * ksmbd_update_fstate(..., FP_INITED) observes the cleared volatile id. 1133 * FP_CLOSED on entry means an earlier ksmbd_close_fd() already consumed the 1134 * idr-owned ref. 1135 */ 1136 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp) 1137 { 1138 if (fp->f_state == FP_INITED) { 1139 set_close_state_blocked_works(fp); 1140 fp->f_state = FP_CLOSED; 1141 return 2; 1142 } 1143 1144 return 1; 1145 } 1146 1147 static int 1148 __close_file_table_ids(struct ksmbd_session *sess, 1149 struct ksmbd_tree_connect *tcon, 1150 bool (*skip)(struct ksmbd_tree_connect *tcon, 1151 struct ksmbd_file *fp, 1152 struct ksmbd_user *user), 1153 bool skip_preserves_fp) 1154 { 1155 struct ksmbd_file_table *ft = &sess->file_table; 1156 struct ksmbd_file *fp; 1157 unsigned int id = 0; 1158 int num = 0; 1159 1160 while (1) { 1161 int n_to_drop; 1162 1163 write_lock(&ft->lock); 1164 fp = idr_get_next(ft->idr, &id); 1165 if (!fp) { 1166 write_unlock(&ft->lock); 1167 break; 1168 } 1169 if (!atomic_inc_not_zero(&fp->refcount)) { 1170 id++; 1171 write_unlock(&ft->lock); 1172 continue; 1173 } 1174 1175 if (skip_preserves_fp) { 1176 /* 1177 * Session teardown: skip() is session_fd_check(), 1178 * which may sleep and mutates fp->conn / fp->tcon / 1179 * fp->volatile_id when it chooses to preserve fp 1180 * for durable reconnect. Unpublish fp from the 1181 * session idr here, under ft->lock, so that 1182 * __ksmbd_lookup_fd() through this session cannot 1183 * grant a new ksmbd_fp_get() reference to an fp 1184 * whose fields are about to be rewritten outside 1185 * the lock. Durable reconnect still reaches fp via 1186 * global_ft. 1187 */ 1188 idr_remove(ft->idr, id); 1189 fp->durable_volatile_id = fp->volatile_id; 1190 fp->volatile_id = KSMBD_NO_FID; 1191 write_unlock(&ft->lock); 1192 1193 if (skip(tcon, fp, sess->user)) { 1194 /* 1195 * session_fd_check() has converted fp to 1196 * durable-preserve state and cleared its 1197 * per-conn fields. fp is already unpublished 1198 * above; the original idr-owned ref keeps it 1199 * alive for the durable scavenger. Drop only 1200 * the transient ref. atomic_dec() is safe -- 1201 * atomic_inc_not_zero() succeeded on a 1202 * positive value and we added one more, so 1203 * refcount cannot be zero here. 1204 */ 1205 atomic_dec(&fp->refcount); 1206 id++; 1207 continue; 1208 } 1209 1210 /* 1211 * Keep the close-state decision under the same lock 1212 * observed by ksmbd_update_fstate(), which is how an 1213 * in-flight FP_NEW opener learns that teardown has 1214 * cleared its volatile id. 1215 */ 1216 write_lock(&ft->lock); 1217 n_to_drop = ksmbd_mark_fp_closed(fp); 1218 write_unlock(&ft->lock); 1219 } else { 1220 /* 1221 * Tree teardown: skip() is tree_conn_fd_check(), a 1222 * cheap pointer compare that doesn't sleep and has 1223 * no side effects, so keep the skip decision plus 1224 * the unpublish-and-mark-closed sequence atomic 1225 * under ft->lock. fps belonging to other tree 1226 * connects (skip() == true) stay fully published in 1227 * the session idr with no lock window. 1228 */ 1229 if (skip(tcon, fp, sess->user)) { 1230 atomic_dec(&fp->refcount); 1231 write_unlock(&ft->lock); 1232 id++; 1233 continue; 1234 } 1235 idr_remove(ft->idr, id); 1236 fp->volatile_id = KSMBD_NO_FID; 1237 n_to_drop = ksmbd_mark_fp_closed(fp); 1238 write_unlock(&ft->lock); 1239 } 1240 1241 /* 1242 * fp->volatile_id is already cleared to prevent stale idr 1243 * removal from a deferred final close. Remove fp from 1244 * m_fp_list here because __ksmbd_remove_fd() will skip the 1245 * list unlink when volatile_id is KSMBD_NO_FID. 1246 */ 1247 down_write(&fp->f_ci->m_lock); 1248 list_del_init(&fp->node); 1249 up_write(&fp->f_ci->m_lock); 1250 1251 /* 1252 * Drop the references this iteration owns: 1253 * 1254 * n_to_drop == 2: we observed FP_INITED and committed 1255 * the FP_CLOSED transition ourselves, so we own the 1256 * transient (+1) and the still-intact idr-owned ref. 1257 * 1258 * n_to_drop == 1: either a prior ksmbd_close_fd() 1259 * already consumed the idr-owned ref, or fp was still 1260 * FP_NEW and the in-flight opener/reopener must keep 1261 * the original reference until ksmbd_update_fstate() 1262 * observes the cleared volatile id. 1263 * 1264 * If we end up as the final putter, finalize fp and 1265 * account the open_files_count decrement via the caller's 1266 * atomic_sub(num, ...). Otherwise the remaining user's 1267 * ksmbd_fd_put() reaches __put_fd_final(), which does its 1268 * own atomic_dec(&open_files_count), so we must not count 1269 * this fp here -- doing so would double-decrement the 1270 * connection-wide counter. 1271 */ 1272 if (atomic_sub_and_test(n_to_drop, &fp->refcount)) { 1273 __ksmbd_close_fd(NULL, fp); 1274 num++; 1275 } 1276 id++; 1277 } 1278 1279 return num; 1280 } 1281 1282 static inline bool is_reconnectable(struct ksmbd_file *fp) 1283 { 1284 struct oplock_info *opinfo = opinfo_get(fp); 1285 bool reconn = false; 1286 1287 if (!opinfo) 1288 return false; 1289 1290 if (opinfo->op_state != OPLOCK_STATE_NONE) { 1291 opinfo_put(opinfo); 1292 return false; 1293 } 1294 1295 if (fp->is_resilient || fp->is_persistent) 1296 reconn = true; 1297 else if (fp->is_durable && opinfo->is_lease && 1298 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 1299 reconn = true; 1300 1301 else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) 1302 reconn = true; 1303 1304 opinfo_put(opinfo); 1305 return reconn; 1306 } 1307 1308 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon, 1309 struct ksmbd_file *fp, 1310 struct ksmbd_user *user) 1311 { 1312 return fp->tcon != tcon; 1313 } 1314 1315 static bool ksmbd_durable_scavenger_alive(void) 1316 { 1317 if (!durable_scavenger_running) 1318 return false; 1319 1320 if (kthread_should_stop()) 1321 return false; 1322 1323 if (idr_is_empty(global_ft.idr)) 1324 return false; 1325 1326 return true; 1327 } 1328 1329 static void ksmbd_scavenger_dispose_dh(struct ksmbd_file *fp) 1330 { 1331 /* 1332 * Durable-preserved fp can remain linked on f_ci->m_fp_list for 1333 * share-mode checks. Unlink it before final close; fp->node is not 1334 * available as a scavenger-private list node because re-adding it to 1335 * another list corrupts m_fp_list. 1336 */ 1337 down_write(&fp->f_ci->m_lock); 1338 list_del_init(&fp->node); 1339 up_write(&fp->f_ci->m_lock); 1340 1341 /* 1342 * Drop both the durable lifetime reference and the transient reference 1343 * taken by the scavenger under global_ft.lock. If a concurrent 1344 * ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp 1345 * before the unlink above, that holder owns the final close via 1346 * ksmbd_fd_put() -> __ksmbd_close_fd(). Otherwise the scavenger is 1347 * the last putter and finalises fp here. 1348 */ 1349 if (atomic_sub_and_test(2, &fp->refcount)) 1350 __ksmbd_close_fd(NULL, fp); 1351 } 1352 1353 static int ksmbd_durable_scavenger(void *dummy) 1354 { 1355 struct ksmbd_file *fp = NULL; 1356 struct ksmbd_file *expired_fp; 1357 unsigned int id; 1358 unsigned int min_timeout = 1; 1359 bool found_fp_timeout; 1360 unsigned long remaining_jiffies; 1361 1362 __module_get(THIS_MODULE); 1363 1364 set_freezable(); 1365 while (ksmbd_durable_scavenger_alive()) { 1366 if (try_to_freeze()) 1367 continue; 1368 1369 remaining_jiffies = wait_event_interruptible_timeout(dh_wq, 1370 ksmbd_durable_scavenger_alive() == false, 1371 __msecs_to_jiffies(min_timeout)); 1372 if ((long)remaining_jiffies > 0) 1373 min_timeout = jiffies_to_msecs(remaining_jiffies); 1374 else 1375 min_timeout = DURABLE_HANDLE_MAX_TIMEOUT; 1376 1377 do { 1378 expired_fp = NULL; 1379 found_fp_timeout = false; 1380 1381 write_lock(&global_ft.lock); 1382 idr_for_each_entry(global_ft.idr, fp, id) { 1383 unsigned long durable_timeout; 1384 1385 if (!fp->durable_timeout) 1386 continue; 1387 1388 if (atomic_read(&fp->refcount) > 1 || 1389 fp->conn) 1390 continue; 1391 1392 found_fp_timeout = true; 1393 if (fp->durable_scavenger_timeout <= 1394 jiffies_to_msecs(jiffies)) { 1395 __ksmbd_remove_durable_fd(fp); 1396 /* 1397 * Take a transient reference so fp 1398 * cannot be freed by an in-flight 1399 * ksmbd_lookup_fd_inode() that found 1400 * it through f_ci->m_fp_list while we 1401 * drop global_ft.lock and reach the 1402 * m_fp_list unlink in 1403 * ksmbd_scavenger_dispose_dh(). 1404 */ 1405 atomic_inc(&fp->refcount); 1406 expired_fp = fp; 1407 break; 1408 } 1409 1410 durable_timeout = 1411 fp->durable_scavenger_timeout - 1412 jiffies_to_msecs(jiffies); 1413 1414 if (min_timeout > durable_timeout) 1415 min_timeout = durable_timeout; 1416 } 1417 write_unlock(&global_ft.lock); 1418 1419 if (expired_fp) 1420 ksmbd_scavenger_dispose_dh(expired_fp); 1421 } while (expired_fp); 1422 1423 if (found_fp_timeout == false) 1424 break; 1425 } 1426 1427 durable_scavenger_running = false; 1428 1429 module_put(THIS_MODULE); 1430 1431 return 0; 1432 } 1433 1434 void ksmbd_launch_ksmbd_durable_scavenger(void) 1435 { 1436 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE)) 1437 return; 1438 1439 mutex_lock(&durable_scavenger_lock); 1440 if (durable_scavenger_running == true) { 1441 mutex_unlock(&durable_scavenger_lock); 1442 return; 1443 } 1444 1445 durable_scavenger_running = true; 1446 1447 server_conf.dh_task = kthread_run(ksmbd_durable_scavenger, 1448 (void *)NULL, "ksmbd-durable-scavenger"); 1449 if (IS_ERR(server_conf.dh_task)) 1450 pr_err("cannot start conn thread, err : %ld\n", 1451 PTR_ERR(server_conf.dh_task)); 1452 mutex_unlock(&durable_scavenger_lock); 1453 } 1454 1455 void ksmbd_stop_durable_scavenger(void) 1456 { 1457 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE)) 1458 return; 1459 1460 mutex_lock(&durable_scavenger_lock); 1461 if (!durable_scavenger_running) { 1462 mutex_unlock(&durable_scavenger_lock); 1463 return; 1464 } 1465 1466 durable_scavenger_running = false; 1467 if (waitqueue_active(&dh_wq)) 1468 wake_up(&dh_wq); 1469 mutex_unlock(&durable_scavenger_lock); 1470 kthread_stop(server_conf.dh_task); 1471 } 1472 1473 /* 1474 * ksmbd_vfs_copy_durable_owner - Copy owner info for durable reconnect 1475 * @fp: ksmbd file pointer to store owner info 1476 * @user: user pointer to copy from 1477 * 1478 * This function binds the current user's identity to the file handle 1479 * to satisfy MS-SMB2 Step 8 (SecurityContext matching) during reconnect. 1480 * 1481 * Return: 0 on success, or negative error code on failure 1482 */ 1483 static int ksmbd_vfs_copy_durable_owner(struct ksmbd_file *fp, 1484 struct ksmbd_user *user) 1485 { 1486 char *name; 1487 1488 if (!user) 1489 return -EINVAL; 1490 1491 /* Duplicate the user name to ensure identity persistence */ 1492 name = kstrdup(user->name, GFP_KERNEL); 1493 if (!name) 1494 return -ENOMEM; 1495 1496 spin_lock(&fp->f_lock); 1497 fp->owner.uid = user->uid; 1498 fp->owner.gid = user->gid; 1499 fp->owner.name = name; 1500 spin_unlock(&fp->f_lock); 1501 1502 return 0; 1503 } 1504 1505 /** 1506 * ksmbd_vfs_compare_durable_owner - Verify if the requester is original owner 1507 * @fp: existing ksmbd file pointer 1508 * @user: user pointer of the reconnect requester 1509 * 1510 * Compares the UID, GID, and name of the current requester against the 1511 * original owner stored in the file handle. 1512 * 1513 * Return: true if the user matches, false otherwise 1514 */ 1515 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp, 1516 struct ksmbd_user *user) 1517 { 1518 bool ret = false; 1519 1520 if (!user) 1521 return false; 1522 1523 spin_lock(&fp->f_lock); 1524 if (!fp->owner.name) 1525 goto out; 1526 1527 /* Check if the UID and GID match first (fast path) */ 1528 if (fp->owner.uid != user->uid || fp->owner.gid != user->gid) 1529 goto out; 1530 1531 /* Validate the account name to ensure the same SecurityContext */ 1532 ret = (strcmp(fp->owner.name, user->name) == 0); 1533 out: 1534 spin_unlock(&fp->f_lock); 1535 return ret; 1536 } 1537 1538 static bool session_fd_check(struct ksmbd_tree_connect *tcon, 1539 struct ksmbd_file *fp, struct ksmbd_user *user) 1540 { 1541 struct ksmbd_inode *ci; 1542 struct oplock_info *op; 1543 struct ksmbd_conn *conn; 1544 struct ksmbd_lock *smb_lock, *tmp_lock; 1545 1546 if (!is_reconnectable(fp)) 1547 return false; 1548 1549 if (fp->f_state != FP_INITED) 1550 return false; 1551 1552 if (WARN_ON_ONCE(!fp->conn)) 1553 return false; 1554 1555 if (ksmbd_vfs_copy_durable_owner(fp, user)) 1556 return false; 1557 1558 /* 1559 * fp owns a strong reference on fp->conn (taken in ksmbd_open_fd() 1560 * / ksmbd_reopen_durable_fd()), so conn stays valid for the whole 1561 * body of this function regardless of any op->conn puts below. 1562 */ 1563 conn = fp->conn; 1564 ci = fp->f_ci; 1565 down_write(&ci->m_lock); 1566 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 1567 lockdep_is_held(&ci->m_lock)) { 1568 if (op->conn != conn) 1569 continue; 1570 ksmbd_conn_put(op->conn); 1571 op->conn = NULL; 1572 op->sess = NULL; 1573 } 1574 up_write(&ci->m_lock); 1575 1576 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) { 1577 struct ksmbd_conn *lock_conn = smb_lock->conn; 1578 1579 if (!lock_conn) 1580 continue; 1581 spin_lock(&lock_conn->llist_lock); 1582 list_del_init(&smb_lock->clist); 1583 smb_lock->conn = NULL; 1584 spin_unlock(&lock_conn->llist_lock); 1585 ksmbd_conn_put(lock_conn); 1586 } 1587 1588 fp->conn = NULL; 1589 fp->tcon = NULL; 1590 fp->volatile_id = KSMBD_NO_FID; 1591 1592 if (fp->durable_timeout) 1593 fp->durable_scavenger_timeout = 1594 jiffies_to_msecs(jiffies) + fp->durable_timeout; 1595 1596 /* Drop fp's own reference on conn. */ 1597 ksmbd_conn_put(conn); 1598 return true; 1599 } 1600 1601 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work) 1602 { 1603 int num = __close_file_table_ids(work->sess, 1604 work->tcon, 1605 tree_conn_fd_check, 1606 false); 1607 1608 atomic_sub(num, &work->conn->stats.open_files_count); 1609 } 1610 1611 void ksmbd_close_session_fds(struct ksmbd_work *work) 1612 { 1613 int num = __close_file_table_ids(work->sess, 1614 work->tcon, 1615 session_fd_check, 1616 true); 1617 1618 atomic_sub(num, &work->conn->stats.open_files_count); 1619 } 1620 1621 int ksmbd_init_global_file_table(void) 1622 { 1623 create_proc_files(); 1624 return ksmbd_init_file_table(&global_ft); 1625 } 1626 1627 void ksmbd_free_global_file_table(void) 1628 { 1629 struct ksmbd_file *fp = NULL; 1630 unsigned int id; 1631 1632 idr_for_each_entry(global_ft.idr, fp, id) { 1633 ksmbd_remove_durable_fd(fp); 1634 __ksmbd_close_fd(NULL, fp); 1635 } 1636 1637 idr_destroy(global_ft.idr); 1638 kfree(global_ft.idr); 1639 } 1640 1641 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share, 1642 struct ksmbd_file *fp, char *name) 1643 { 1644 char *pathname, *ab_pathname; 1645 int ret = 0; 1646 1647 pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP); 1648 if (!pathname) 1649 return -EACCES; 1650 1651 ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX); 1652 if (IS_ERR(ab_pathname)) { 1653 kfree(pathname); 1654 return -EACCES; 1655 } 1656 1657 if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) { 1658 ksmbd_debug(SMB, "invalid name reconnect %s\n", name); 1659 ret = -EINVAL; 1660 } 1661 1662 kfree(pathname); 1663 1664 return ret; 1665 } 1666 1667 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp) 1668 { 1669 struct ksmbd_inode *ci; 1670 struct oplock_info *op; 1671 struct ksmbd_conn *conn = work->conn; 1672 struct ksmbd_lock *smb_lock; 1673 unsigned int old_f_state; 1674 1675 write_lock(&global_ft.lock); 1676 if (!fp->is_durable || fp->conn || fp->tcon) { 1677 write_unlock(&global_ft.lock); 1678 pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon); 1679 return -EBADF; 1680 } 1681 1682 if (has_file_id(fp->volatile_id)) { 1683 write_unlock(&global_ft.lock); 1684 pr_err("Still in use durable fd: %llu\n", fp->volatile_id); 1685 return -EBADF; 1686 } 1687 1688 /* 1689 * Initialize fp's connection binding before publishing fp into the 1690 * session's file table. If __open_id() is ordered first, a 1691 * concurrent teardown that iterates the table can observe a valid 1692 * volatile_id with fp->conn == NULL and preserve a 1693 * partially-initialized fp. fp owns a strong reference on the new 1694 * conn (see ksmbd_open_fd()); undo it on __open_id() failure. 1695 */ 1696 fp->conn = ksmbd_conn_get(conn); 1697 fp->tcon = work->tcon; 1698 write_unlock(&global_ft.lock); 1699 1700 old_f_state = fp->f_state; 1701 fp->f_state = FP_NEW; 1702 1703 __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); 1704 if (!has_file_id(fp->volatile_id)) { 1705 write_lock(&global_ft.lock); 1706 fp->conn = NULL; 1707 fp->tcon = NULL; 1708 write_unlock(&global_ft.lock); 1709 ksmbd_conn_put(conn); 1710 fp->f_state = old_f_state; 1711 return -EBADF; 1712 } 1713 1714 list_for_each_entry(smb_lock, &fp->lock_list, flist) { 1715 smb_lock->conn = ksmbd_conn_get(conn); 1716 spin_lock(&conn->llist_lock); 1717 list_add_tail(&smb_lock->clist, &conn->lock_list); 1718 spin_unlock(&conn->llist_lock); 1719 } 1720 1721 ci = fp->f_ci; 1722 down_write(&ci->m_lock); 1723 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 1724 lockdep_is_held(&ci->m_lock)) { 1725 if (op->conn) 1726 continue; 1727 op->conn = ksmbd_conn_get(fp->conn); 1728 op->sess = work->sess; 1729 } 1730 up_write(&ci->m_lock); 1731 1732 spin_lock(&fp->f_lock); 1733 fp->owner.uid = fp->owner.gid = 0; 1734 kfree(fp->owner.name); 1735 fp->owner.name = NULL; 1736 spin_unlock(&fp->f_lock); 1737 1738 return 0; 1739 } 1740 1741 int ksmbd_init_file_table(struct ksmbd_file_table *ft) 1742 { 1743 ft->idr = kzalloc_obj(struct idr, KSMBD_DEFAULT_GFP); 1744 if (!ft->idr) 1745 return -ENOMEM; 1746 1747 idr_init(ft->idr); 1748 rwlock_init(&ft->lock); 1749 return 0; 1750 } 1751 1752 void ksmbd_destroy_file_table(struct ksmbd_session *sess) 1753 { 1754 struct ksmbd_file_table *ft = &sess->file_table; 1755 1756 if (!ft->idr) 1757 return; 1758 1759 __close_file_table_ids(sess, NULL, session_fd_check, true); 1760 idr_destroy(ft->idr); 1761 kfree(ft->idr); 1762 ft->idr = NULL; 1763 } 1764 1765 int ksmbd_init_file_cache(void) 1766 { 1767 filp_cache = kmem_cache_create("ksmbd_file_cache", 1768 sizeof(struct ksmbd_file), 0, 1769 SLAB_HWCACHE_ALIGN, NULL); 1770 if (!filp_cache) 1771 goto out; 1772 1773 init_waitqueue_head(&dh_wq); 1774 1775 return 0; 1776 1777 out: 1778 pr_err("failed to allocate file cache\n"); 1779 return -ENOMEM; 1780 } 1781 1782 void ksmbd_exit_file_cache(void) 1783 { 1784 kmem_cache_destroy(filp_cache); 1785 } 1786