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