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