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 #include <linux/hashtable.h> 15 #include <linux/jiffies.h> 16 17 #include "glob.h" 18 #include "vfs_cache.h" 19 #include "oplock.h" 20 #include "vfs.h" 21 #include "connection.h" 22 #include "misc.h" 23 #include "mgmt/tree_connect.h" 24 #include "mgmt/user_session.h" 25 #include "mgmt/user_config.h" 26 #include "smb_common.h" 27 #include "server.h" 28 #include "smb2pdu.h" 29 30 #define S_DEL_PENDING 1 31 #define S_DEL_ON_CLS 2 32 #define S_DEL_ON_CLS_STREAM 8 33 34 static unsigned int inode_hash_mask __read_mostly; 35 static unsigned int inode_hash_shift __read_mostly; 36 static struct hlist_head *inode_hashtable __read_mostly; 37 static DEFINE_RWLOCK(inode_hash_lock); 38 39 static struct ksmbd_file_table global_ft; 40 static DEFINE_MUTEX(oplock_break_timer_lock); 41 static struct delayed_work oplock_break_timer_work; 42 static unsigned long oplock_break_timer_next; 43 static bool oplock_break_timer_pending; 44 45 static void ksmbd_oplock_break_timer(struct work_struct *work) 46 { 47 struct ksmbd_file *fp; 48 struct oplock_info *opinfo; 49 unsigned long next = 0, now; 50 unsigned int id; 51 bool pending = false; 52 bool expired; 53 54 mutex_lock(&oplock_break_timer_lock); 55 now = jiffies; 56 read_lock(&global_ft.lock); 57 idr_for_each_entry(global_ft.idr, fp, id) { 58 opinfo = opinfo_get(fp); 59 if (!opinfo) 60 continue; 61 62 expired = false; 63 spin_lock(&opinfo->state_lock); 64 if (!opinfo->is_lease && 65 opinfo->op_state == OPLOCK_ACK_WAIT && 66 opinfo->oplock_timeout_set) { 67 if (time_after_eq(now, opinfo->oplock_timeout)) { 68 /* ksmbd owns the oplock state for the local file. */ 69 opinfo->level = SMB2_OPLOCK_LEVEL_NONE; 70 opinfo->op_state = OPLOCK_STATE_NONE; 71 opinfo->oplock_timeout_set = false; 72 opinfo->oplock_timed_out = true; 73 expired = true; 74 } else if (!pending || 75 time_before(opinfo->oplock_timeout, next)) { 76 next = opinfo->oplock_timeout; 77 pending = true; 78 } 79 } 80 spin_unlock(&opinfo->state_lock); 81 if (expired) 82 wake_up_all(&opinfo->oplock_q); 83 opinfo_put(opinfo); 84 } 85 read_unlock(&global_ft.lock); 86 87 oplock_break_timer_pending = pending; 88 if (pending) { 89 oplock_break_timer_next = next; 90 mod_delayed_work(system_percpu_wq, &oplock_break_timer_work, 91 time_after_eq(jiffies, next) ? 0 : next - jiffies); 92 } 93 mutex_unlock(&oplock_break_timer_lock); 94 } 95 96 void ksmbd_schedule_oplock_break_timer(unsigned long timeout) 97 { 98 mutex_lock(&oplock_break_timer_lock); 99 if (!oplock_break_timer_pending || 100 time_before(timeout, oplock_break_timer_next)) { 101 oplock_break_timer_pending = true; 102 oplock_break_timer_next = timeout; 103 mod_delayed_work(system_percpu_wq, &oplock_break_timer_work, 104 time_after_eq(jiffies, timeout) ? 0 : timeout - jiffies); 105 } 106 mutex_unlock(&oplock_break_timer_lock); 107 } 108 109 static atomic_long_t fd_limit; 110 static struct kmem_cache *filp_cache; 111 DECLARE_WAIT_QUEUE_HEAD(ksmbd_lock_wait); 112 113 /* Both the inode hash and each open's lock_list use this lock. */ 114 static DEFINE_SPINLOCK(file_locks_lock); 115 static DEFINE_HASHTABLE(file_locks, 8); 116 117 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp); 118 119 #define OPLOCK_NONE 0 120 #define OPLOCK_EXCLUSIVE 1 121 #define OPLOCK_BATCH 2 122 #define OPLOCK_READ 3 /* level 2 oplock */ 123 124 #ifdef CONFIG_PROC_FS 125 126 static const struct ksmbd_const_name ksmbd_lease_const_names[] = { 127 {le32_to_cpu(SMB2_LEASE_NONE_LE), "LEASE_NONE"}, 128 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE), "LEASE_R"}, 129 {le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_H"}, 130 {le32_to_cpu(SMB2_LEASE_WRITE_CACHING_LE), "LEASE_W"}, 131 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE | 132 SMB2_LEASE_HANDLE_CACHING_LE), "LEASE_RH"}, 133 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE | 134 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RW"}, 135 {le32_to_cpu(SMB2_LEASE_HANDLE_CACHING_LE | 136 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_WH"}, 137 {le32_to_cpu(SMB2_LEASE_READ_CACHING_LE | 138 SMB2_LEASE_HANDLE_CACHING_LE | 139 SMB2_LEASE_WRITE_CACHING_LE), "LEASE_RWH"}, 140 }; 141 142 static const struct ksmbd_const_name ksmbd_oplock_const_names[] = { 143 {SMB2_OPLOCK_LEVEL_NONE, "OPLOCK_NONE"}, 144 {SMB2_OPLOCK_LEVEL_II, "OPLOCK_II"}, 145 {SMB2_OPLOCK_LEVEL_EXCLUSIVE, "OPLOCK_EXCLUSIVE"}, 146 {SMB2_OPLOCK_LEVEL_BATCH, "OPLOCK_BATCH"}, 147 }; 148 149 static const struct ksmbd_const_name ksmbd_file_state_names[] = { 150 {FP_NEW, "new"}, 151 {FP_INITED, "open"}, 152 {FP_CLOSED, "closed"}, 153 }; 154 155 #define KSMBD_PROC_FILE_DURABLE BIT(0) 156 #define KSMBD_PROC_FILE_PERSISTENT BIT(1) 157 #define KSMBD_PROC_FILE_RESILIENT BIT(2) 158 #define KSMBD_PROC_FILE_DELETE_ON_CLOSE BIT(3) 159 #define KSMBD_PROC_FILE_STREAM BIT(4) 160 #define KSMBD_PROC_FILE_POSIX BIT(5) 161 #define KSMBD_PROC_FILE_ATTRIB_ONLY BIT(6) 162 163 static const struct ksmbd_const_name ksmbd_file_flag_names[] = { 164 {KSMBD_PROC_FILE_DURABLE, "durable"}, 165 {KSMBD_PROC_FILE_PERSISTENT, "persistent"}, 166 {KSMBD_PROC_FILE_RESILIENT, "resilient"}, 167 {KSMBD_PROC_FILE_DELETE_ON_CLOSE, "delete-on-close"}, 168 {KSMBD_PROC_FILE_STREAM, "stream"}, 169 {KSMBD_PROC_FILE_POSIX, "posix"}, 170 {KSMBD_PROC_FILE_ATTRIB_ONLY, "attrib-only"}, 171 }; 172 173 static unsigned int ksmbd_proc_file_flags(struct ksmbd_file *fp) 174 { 175 unsigned int flags = 0; 176 177 if (fp->is_durable) 178 flags |= KSMBD_PROC_FILE_DURABLE; 179 if (fp->is_persistent) 180 flags |= KSMBD_PROC_FILE_PERSISTENT; 181 if (fp->is_resilient) 182 flags |= KSMBD_PROC_FILE_RESILIENT; 183 if (fp->coption & FILE_DELETE_ON_CLOSE_LE) 184 flags |= KSMBD_PROC_FILE_DELETE_ON_CLOSE; 185 if (fp->stream.name) 186 flags |= KSMBD_PROC_FILE_STREAM; 187 if (fp->is_posix_ctxt) 188 flags |= KSMBD_PROC_FILE_POSIX; 189 if (fp->attrib_only) 190 flags |= KSMBD_PROC_FILE_ATTRIB_ONLY; 191 return flags; 192 } 193 194 static int proc_show_files(struct seq_file *m, void *v) 195 { 196 struct ksmbd_file *fp = NULL; 197 unsigned int id; 198 struct oplock_info *opinfo; 199 200 read_lock(&global_ft.lock); 201 idr_for_each_entry(global_ft.idr, fp, id) { 202 seq_printf(m, "tree_id:\t0x%x\n", fp->tcon ? fp->tcon->id : 0); 203 seq_printf(m, "persistent_id:\t0x%llx\n", fp->persistent_id); 204 seq_printf(m, "volatile_id:\t0x%llx\n", fp->volatile_id); 205 seq_printf(m, "refcount:\t%d\n", atomic_read(&fp->refcount)); 206 207 rcu_read_lock(); 208 opinfo = rcu_dereference(fp->f_opinfo); 209 if (opinfo) { 210 const struct ksmbd_const_name *const_names; 211 const char *name; 212 int count; 213 unsigned int level; 214 215 if (opinfo->is_lease) { 216 const_names = ksmbd_lease_const_names; 217 count = ARRAY_SIZE(ksmbd_lease_const_names); 218 level = le32_to_cpu(opinfo->o_lease->state); 219 } else { 220 const_names = ksmbd_oplock_const_names; 221 count = ARRAY_SIZE(ksmbd_oplock_const_names); 222 level = opinfo->level; 223 } 224 rcu_read_unlock(); 225 name = ksmbd_proc_const_name(const_names, count, level); 226 if (name) 227 seq_printf(m, "oplock:\t%s\n", name); 228 else 229 seq_printf(m, "oplock:\t0x%x\n", level); 230 } else { 231 rcu_read_unlock(); 232 seq_puts(m, "oplock:\tnone\n"); 233 } 234 235 seq_printf(m, "state:\t%s\n", 236 ksmbd_proc_const_name(ksmbd_file_state_names, 237 ARRAY_SIZE(ksmbd_file_state_names), 238 fp->f_state)); 239 seq_printf(m, "durable_timeout:\t%u\n", fp->durable_timeout); 240 seq_printf(m, "create_options:\t0x%08x\n", 241 le32_to_cpu(fp->coption)); 242 seq_printf(m, "desired_access:\t0x%08x\n", 243 le32_to_cpu(fp->daccess)); 244 seq_printf(m, "share_access:\t0x%08x\n", 245 le32_to_cpu(fp->saccess)); 246 seq_puts(m, "flags:\t"); 247 ksmbd_proc_show_flag_names(m, ksmbd_file_flag_names, 248 ARRAY_SIZE(ksmbd_file_flag_names), 249 ksmbd_proc_file_flags(fp)); 250 seq_printf(m, "\nname:\t%s\n\n", 251 fp->filp->f_path.dentry->d_name.name); 252 } 253 read_unlock(&global_ft.lock); 254 return 0; 255 } 256 257 static int create_proc_files(void) 258 { 259 if (!ksmbd_proc_create("files", proc_show_files, NULL)) 260 return -ENOMEM; 261 return 0; 262 } 263 #else 264 static int create_proc_files(void) { return 0; } 265 #endif 266 267 static bool durable_scavenger_running; 268 static DEFINE_MUTEX(durable_scavenger_lock); 269 static wait_queue_head_t dh_wq; 270 271 bool ksmbd_durable_scavenger_active(void) 272 { 273 bool active; 274 275 mutex_lock(&durable_scavenger_lock); 276 active = durable_scavenger_running; 277 mutex_unlock(&durable_scavenger_lock); 278 return active; 279 } 280 281 void ksmbd_set_fd_limit(unsigned long limit) 282 { 283 limit = min(limit, get_max_files()); 284 atomic_long_set(&fd_limit, limit); 285 } 286 287 static bool fd_limit_depleted(void) 288 { 289 long v = atomic_long_dec_return(&fd_limit); 290 291 if (v >= 0) 292 return false; 293 atomic_long_inc(&fd_limit); 294 return true; 295 } 296 297 static void fd_limit_close(void) 298 { 299 atomic_long_inc(&fd_limit); 300 } 301 302 /* 303 * INODE hash 304 */ 305 306 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval) 307 { 308 unsigned long tmp; 309 310 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / 311 L1_CACHE_BYTES; 312 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift); 313 return tmp & inode_hash_mask; 314 } 315 316 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de) 317 { 318 struct hlist_head *head = inode_hashtable + 319 inode_hash(d_inode(de)->i_sb, (unsigned long)de); 320 struct ksmbd_inode *ci = NULL, *ret_ci = NULL; 321 322 hlist_for_each_entry(ci, head, m_hash) { 323 if (ci->m_de == de) { 324 if (atomic_inc_not_zero(&ci->m_count)) 325 ret_ci = ci; 326 break; 327 } 328 } 329 return ret_ci; 330 } 331 332 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp) 333 { 334 return __ksmbd_inode_lookup(fp->filp->f_path.dentry); 335 } 336 337 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d) 338 { 339 struct ksmbd_inode *ci; 340 341 read_lock(&inode_hash_lock); 342 ci = __ksmbd_inode_lookup(d); 343 read_unlock(&inode_hash_lock); 344 345 return ci; 346 } 347 348 int ksmbd_query_inode_status(struct dentry *dentry) 349 { 350 struct ksmbd_inode *ci; 351 int ret = KSMBD_INODE_STATUS_UNKNOWN; 352 353 read_lock(&inode_hash_lock); 354 ci = __ksmbd_inode_lookup(dentry); 355 read_unlock(&inode_hash_lock); 356 if (!ci) 357 return ret; 358 359 down_read(&ci->m_lock); 360 if (ci->m_flags & S_DEL_PENDING) 361 ret = KSMBD_INODE_STATUS_PENDING_DELETE; 362 else 363 ret = KSMBD_INODE_STATUS_OK; 364 up_read(&ci->m_lock); 365 366 ksmbd_inode_put(ci); 367 return ret; 368 } 369 370 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp) 371 { 372 struct ksmbd_inode *ci = fp->f_ci; 373 int ret; 374 375 down_read(&ci->m_lock); 376 ret = (ci->m_flags & S_DEL_PENDING); 377 up_read(&ci->m_lock); 378 if (ret || !ksmbd_stream_fd(fp)) 379 return ret; 380 381 spin_lock(&fp->f_lock); 382 ret = fp->stream_del_pending; 383 spin_unlock(&fp->f_lock); 384 385 return ret; 386 } 387 388 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp) 389 { 390 struct ksmbd_inode *ci = fp->f_ci; 391 392 down_write(&ci->m_lock); 393 ci->m_flags |= S_DEL_PENDING; 394 up_write(&ci->m_lock); 395 } 396 397 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp) 398 { 399 struct ksmbd_inode *ci = fp->f_ci; 400 401 down_write(&ci->m_lock); 402 ci->m_flags &= ~S_DEL_PENDING; 403 up_write(&ci->m_lock); 404 } 405 406 bool ksmbd_has_stream_without_delete_share(struct ksmbd_file *fp) 407 { 408 struct ksmbd_file *prev_fp; 409 struct ksmbd_inode *ci = fp->f_ci; 410 bool ret = false; 411 412 if (ksmbd_stream_fd(fp)) 413 return false; 414 415 down_read(&ci->m_lock); 416 list_for_each_entry(prev_fp, &ci->m_fp_list, node) { 417 if (prev_fp == fp || !ksmbd_stream_fd(prev_fp)) 418 continue; 419 420 if (file_inode(fp->filp) != file_inode(prev_fp->filp)) 421 continue; 422 423 /* 424 * A stream open with no right in FILE_SHARE_CHECKED_ACCESS_LE, 425 * implied writes included, never conflicts. 426 */ 427 if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) && 428 ksmbd_sharing_daccess(prev_fp) & FILE_SHARE_CHECKED_ACCESS_LE) { 429 ret = true; 430 break; 431 } 432 } 433 up_read(&ci->m_lock); 434 435 return ret; 436 } 437 438 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp, 439 int file_info) 440 { 441 struct ksmbd_inode *ci = fp->f_ci; 442 443 down_write(&ci->m_lock); 444 if (ksmbd_stream_fd(fp)) 445 ci->m_flags |= S_DEL_ON_CLS_STREAM; 446 else 447 ci->m_flags |= S_DEL_ON_CLS; 448 up_write(&ci->m_lock); 449 } 450 451 /* 452 * FileDispositionInformation (SET_INFO) on a stream handle must only 453 * mark the stream for deletion, not the whole file -- otherwise 454 * deleting a single alternate data stream (e.g. AFP_AfpInfo) deletes 455 * the entire file's data along with it. 456 * 457 * This is tracked on fp itself (stream_del_pending), not the shared 458 * ksmbd_inode: the inode-wide S_DEL_ON_CLS_STREAM flag used by 459 * ksmbd_fd_set_delete_on_close() can't record *which* stream should be 460 * deleted, so if a different stream handle on the same file closed 461 * first, it would delete the wrong stream. 462 */ 463 void ksmbd_fd_set_delete_pending(struct ksmbd_file *fp) 464 { 465 if (ksmbd_stream_fd(fp)) { 466 spin_lock(&fp->f_lock); 467 fp->stream_del_pending = true; 468 spin_unlock(&fp->f_lock); 469 } else { 470 ksmbd_set_inode_pending_delete(fp); 471 } 472 } 473 474 void ksmbd_fd_clear_delete_pending(struct ksmbd_file *fp) 475 { 476 if (ksmbd_stream_fd(fp)) { 477 spin_lock(&fp->f_lock); 478 fp->stream_del_pending = false; 479 spin_unlock(&fp->f_lock); 480 } else { 481 ksmbd_clear_inode_pending_delete(fp); 482 } 483 } 484 485 static void ksmbd_inode_hash(struct ksmbd_inode *ci) 486 { 487 struct hlist_head *b = inode_hashtable + 488 inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de); 489 490 hlist_add_head(&ci->m_hash, b); 491 } 492 493 static void ksmbd_inode_unhash(struct ksmbd_inode *ci) 494 { 495 write_lock(&inode_hash_lock); 496 hlist_del_init(&ci->m_hash); 497 write_unlock(&inode_hash_lock); 498 } 499 500 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp) 501 { 502 atomic_set(&ci->m_count, 1); 503 atomic_set(&ci->op_count, 0); 504 atomic_set(&ci->sop_count, 0); 505 ci->m_flags = 0; 506 ci->m_fattr = 0; 507 INIT_LIST_HEAD(&ci->m_fp_list); 508 INIT_LIST_HEAD(&ci->m_op_list); 509 init_rwsem(&ci->m_lock); 510 ci->m_de = fp->filp->f_path.dentry; 511 return 0; 512 } 513 514 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp) 515 { 516 struct ksmbd_inode *ci, *tmpci; 517 int rc; 518 519 read_lock(&inode_hash_lock); 520 ci = ksmbd_inode_lookup(fp); 521 read_unlock(&inode_hash_lock); 522 if (ci) 523 return ci; 524 525 ci = kmalloc_obj(struct ksmbd_inode, KSMBD_DEFAULT_GFP); 526 if (!ci) 527 return NULL; 528 529 rc = ksmbd_inode_init(ci, fp); 530 if (rc) { 531 pr_err("inode initialized failed\n"); 532 kfree(ci); 533 return NULL; 534 } 535 536 write_lock(&inode_hash_lock); 537 tmpci = ksmbd_inode_lookup(fp); 538 if (!tmpci) { 539 ksmbd_inode_hash(ci); 540 } else { 541 kfree(ci); 542 ci = tmpci; 543 } 544 write_unlock(&inode_hash_lock); 545 return ci; 546 } 547 548 static void ksmbd_inode_free(struct ksmbd_inode *ci) 549 { 550 ksmbd_inode_unhash(ci); 551 kfree(ci); 552 } 553 554 void ksmbd_inode_put(struct ksmbd_inode *ci) 555 { 556 if (atomic_dec_and_test(&ci->m_count)) 557 ksmbd_inode_free(ci); 558 } 559 560 int __init ksmbd_inode_hash_init(void) 561 { 562 unsigned int loop; 563 unsigned long numentries = 16384; 564 unsigned long bucketsize = sizeof(struct hlist_head); 565 unsigned long size; 566 567 inode_hash_shift = ilog2(numentries); 568 inode_hash_mask = (1 << inode_hash_shift) - 1; 569 570 size = bucketsize << inode_hash_shift; 571 572 /* init master fp hash table */ 573 inode_hashtable = vmalloc(size); 574 if (!inode_hashtable) 575 return -ENOMEM; 576 577 for (loop = 0; loop < (1U << inode_hash_shift); loop++) 578 INIT_HLIST_HEAD(&inode_hashtable[loop]); 579 return 0; 580 } 581 582 void ksmbd_release_inode_hash(void) 583 { 584 vfree(inode_hashtable); 585 } 586 587 static void __ksmbd_inode_close(struct ksmbd_file *fp) 588 { 589 struct ksmbd_inode *ci = fp->f_ci; 590 int err; 591 struct file *filp; 592 593 filp = fp->filp; 594 595 if (ksmbd_stream_fd(fp)) { 596 bool remove_stream_xattr = false; 597 598 down_write(&ci->m_lock); 599 if (ci->m_flags & S_DEL_ON_CLS_STREAM) { 600 ci->m_flags &= ~S_DEL_ON_CLS_STREAM; 601 remove_stream_xattr = true; 602 } 603 up_write(&ci->m_lock); 604 605 /* 606 * Per-handle delete-pending from ksmbd_fd_set_delete_pending() 607 * (FileDispositionInformation on this stream) -- separate from 608 * the inode-wide flag above, which only ever meant "some 609 * stream on this file" with no way to say which one. 610 */ 611 spin_lock(&fp->f_lock); 612 if (fp->stream_del_pending) { 613 fp->stream_del_pending = false; 614 remove_stream_xattr = true; 615 } 616 spin_unlock(&fp->f_lock); 617 618 if (remove_stream_xattr) { 619 const struct cred *saved_cred; 620 621 saved_cred = override_creds(filp->f_cred); 622 err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp), 623 &filp->f_path, 624 fp->stream.name, 625 true); 626 revert_creds(saved_cred); 627 if (err) 628 pr_err("remove xattr failed : %s\n", 629 fp->stream.name); 630 } 631 } 632 633 down_write(&ci->m_lock); 634 /* Promote S_DEL_ON_CLS to S_DEL_PENDING when close */ 635 if (ci->m_flags & S_DEL_ON_CLS) { 636 ci->m_flags &= ~S_DEL_ON_CLS; 637 ci->m_flags |= S_DEL_PENDING; 638 } 639 up_write(&ci->m_lock); 640 641 if (atomic_dec_and_test(&ci->m_count)) { 642 bool do_unlink = false; 643 644 down_write(&ci->m_lock); 645 if (ci->m_flags & S_DEL_PENDING) { 646 ci->m_flags &= ~S_DEL_PENDING; 647 do_unlink = true; 648 } 649 up_write(&ci->m_lock); 650 651 if (do_unlink) 652 ksmbd_vfs_unlink(filp); 653 654 ksmbd_inode_free(ci); 655 } 656 } 657 658 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp) 659 { 660 if (!has_file_id(fp->persistent_id)) 661 return; 662 663 idr_remove(global_ft.idr, fp->persistent_id); 664 /* 665 * Clear persistent_id so a later __ksmbd_close_fd() that runs from a 666 * delayed putter (e.g. when a concurrent ksmbd_lookup_fd_inode() 667 * walker held the final reference) does not re-issue idr_remove() on 668 * an id that idr_alloc_cyclic() may have already handed out to a new 669 * durable handle. 670 */ 671 fp->persistent_id = KSMBD_NO_FID; 672 } 673 674 static void ksmbd_remove_durable_fd(struct ksmbd_file *fp) 675 { 676 write_lock(&global_ft.lock); 677 __ksmbd_remove_durable_fd(fp); 678 write_unlock(&global_ft.lock); 679 if (waitqueue_active(&dh_wq)) 680 wake_up(&dh_wq); 681 } 682 683 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) 684 { 685 down_write(&fp->f_ci->m_lock); 686 list_del_init(&fp->node); 687 up_write(&fp->f_ci->m_lock); 688 689 if (!ft || !has_file_id(fp->volatile_id)) 690 return; 691 692 write_lock(&ft->lock); 693 idr_remove(ft->idr, fp->volatile_id); 694 write_unlock(&ft->lock); 695 } 696 697 /* file_locks_lock protects both committed locks and pending reservations. */ 698 static bool __ksmbd_file_lock_conflict(struct ksmbd_lock *lock) 699 { 700 struct inode *inode = file_inode(lock->fl->c.flc_file); 701 struct ksmbd_lock *cmp; 702 703 hash_for_each_possible(file_locks, cmp, hlist, (unsigned long)inode) { 704 if (cmp == lock || file_inode(cmp->fl->c.flc_file) != inode) 705 continue; 706 if ((cmp->flags & SMB2_LOCKFLAG_SHARED) && 707 (lock->flags & SMB2_LOCKFLAG_SHARED)) 708 continue; 709 if ((cmp->zero_len && !lock->zero_len && 710 cmp->start > lock->start && cmp->start <= lock->end) || 711 (lock->zero_len && !cmp->zero_len && 712 lock->start > cmp->start && lock->start <= cmp->end) || 713 (!cmp->zero_len && !lock->zero_len && 714 cmp->start <= lock->end && lock->start <= cmp->end)) 715 return true; 716 } 717 return false; 718 } 719 720 bool ksmbd_file_lock_conflict(struct ksmbd_lock *lock) 721 { 722 bool conflict; 723 724 spin_lock(&file_locks_lock); 725 conflict = __ksmbd_file_lock_conflict(lock); 726 spin_unlock(&file_locks_lock); 727 return conflict; 728 } 729 730 int ksmbd_file_lock_reserve(struct ksmbd_file *fp, struct ksmbd_lock *lock) 731 { 732 int ret = 0; 733 734 spin_lock(&file_locks_lock); 735 if (__ksmbd_file_lock_conflict(lock)) { 736 ret = -EAGAIN; 737 } else { 738 /* Reserve the range until the whole LOCK request commits. */ 739 lock->pending = true; 740 hash_add(file_locks, &lock->hlist, 741 (unsigned long)file_inode(fp->filp)); 742 list_add_tail(&lock->flist, &fp->lock_list); 743 } 744 spin_unlock(&file_locks_lock); 745 return ret; 746 } 747 748 void ksmbd_file_lock_remove(struct ksmbd_lock *lock) 749 { 750 spin_lock(&file_locks_lock); 751 if (!hlist_unhashed(&lock->hlist)) { 752 hash_del(&lock->hlist); 753 list_del_init(&lock->flist); 754 } 755 spin_unlock(&file_locks_lock); 756 wake_up_all(&ksmbd_lock_wait); 757 } 758 759 /* Unlock a large range, including any portion represented by a VFS lock. */ 760 int ksmbd_file_lock_unlock(struct ksmbd_lock *lock) 761 { 762 struct inode *inode = file_inode(lock->fl->c.flc_file); 763 struct ksmbd_lock *cmp, *match = NULL; 764 int ret = 0; 765 766 spin_lock(&file_locks_lock); 767 hash_for_each_possible(file_locks, cmp, hlist, (unsigned long)inode) { 768 if (cmp->fl->c.flc_file != lock->fl->c.flc_file || 769 cmp->start != lock->start || cmp->end != lock->end || 770 cmp->pending) 771 continue; 772 if (waitqueue_active(&cmp->fl->c.flc_wait)) /* VFS waiters */ 773 continue; 774 match = cmp; 775 if (cmp->flags & SMB2_LOCKFLAG_EXCLUSIVE) 776 break; 777 } 778 if (match) 779 match->pending = true; 780 spin_unlock(&file_locks_lock); 781 if (!match) 782 return -ENOENT; 783 784 if (match->vfs_locked) 785 ret = vfs_lock_file(lock->fl->c.flc_file, F_SETLK, lock->fl, NULL); 786 if (ret) { 787 spin_lock(&file_locks_lock); 788 match->pending = false; 789 spin_unlock(&file_locks_lock); 790 return ret; 791 } 792 ksmbd_file_lock_remove(match); 793 if (match->vfs_locked) 794 ksmbd_vfs_posix_lock_unblock(match->fl); 795 locks_free_lock(match->fl); 796 kfree(match); 797 return 0; 798 } 799 800 /** 801 * ksmbd_file_lock_check() - check conflicts or remove an exact unlock match 802 * @lock: unregistered lock from the current request 803 * 804 * Return: 0 on success, -ENOENT for an unmatched unlock, or -EAGAIN on conflict. 805 */ 806 int ksmbd_file_lock_check(struct ksmbd_lock *lock) 807 { 808 struct inode *inode = file_inode(lock->fl->c.flc_file); 809 struct ksmbd_lock *cmp, *lock_to_remove = NULL; 810 int ret = lock_is_unlock(lock->fl) ? -ENOENT : 0; 811 812 spin_lock(&file_locks_lock); 813 hash_for_each_possible(file_locks, cmp, hlist, (unsigned long)inode) { 814 if (file_inode(cmp->fl->c.flc_file) != inode) 815 continue; 816 817 if (lock_is_unlock(lock->fl)) { 818 if (cmp->fl->c.flc_file != lock->fl->c.flc_file || 819 cmp->start != lock->start || cmp->end != lock->end || 820 cmp->pending) 821 continue; 822 if (waitqueue_active(&cmp->fl->c.flc_wait)) /* VFS waiters */ 823 continue; 824 825 /* Prefer an exclusive lock over matching shared locks. */ 826 lock_to_remove = cmp; 827 if (cmp->flags & SMB2_LOCKFLAG_EXCLUSIVE) 828 break; 829 continue; 830 } 831 832 if (cmp->fl->c.flc_file == lock->fl->c.flc_file) { 833 if (lock->flags & SMB2_LOCKFLAG_SHARED) 834 continue; 835 } else if (cmp->flags & SMB2_LOCKFLAG_SHARED) { 836 continue; 837 } 838 839 if ((cmp->zero_len && !lock->zero_len && 840 cmp->start > lock->start && cmp->start <= lock->end) || 841 (lock->zero_len && !cmp->zero_len && 842 lock->start > cmp->start && lock->start <= cmp->end) || 843 (!cmp->zero_len && !lock->zero_len && 844 cmp->start <= lock->end && lock->start <= cmp->end)) { 845 ret = -EAGAIN; 846 break; 847 } 848 } 849 if (lock_to_remove) { 850 hash_del(&lock_to_remove->hlist); 851 list_del_init(&lock_to_remove->flist); 852 ret = 0; 853 } 854 spin_unlock(&file_locks_lock); 855 if (lock_to_remove) { 856 if (lock_to_remove->vfs_locked) 857 ksmbd_vfs_posix_lock_unblock(lock_to_remove->fl); 858 locks_free_lock(lock_to_remove->fl); 859 kfree(lock_to_remove); 860 wake_up_all(&ksmbd_lock_wait); 861 } 862 return ret; 863 } 864 865 /** 866 * ksmbd_file_lock_add() - publish a committed batch independently of its channel 867 * @fp: open holding the locks 868 * @locks: granted locks awaiting publication 869 */ 870 void ksmbd_file_lock_add(struct ksmbd_file *fp, struct list_head *locks) 871 { 872 struct ksmbd_lock *lock, *tmp; 873 874 spin_lock(&file_locks_lock); 875 list_for_each_entry_safe(lock, tmp, locks, llist) { 876 list_del_init(&lock->llist); 877 if (!hlist_unhashed(&lock->hlist)) { 878 lock->pending = false; 879 continue; 880 } 881 hash_add(file_locks, &lock->hlist, 882 (unsigned long)file_inode(fp->filp)); 883 list_add_tail(&lock->flist, &fp->lock_list); 884 } 885 spin_unlock(&file_locks_lock); 886 } 887 888 static void ksmbd_file_lock_clear(struct ksmbd_file *fp) 889 { 890 struct ksmbd_lock *lock, *tmp; 891 LIST_HEAD(dispose); 892 893 spin_lock(&file_locks_lock); 894 list_for_each_entry(lock, &fp->lock_list, flist) 895 hash_del(&lock->hlist); 896 list_splice_init(&fp->lock_list, &dispose); 897 spin_unlock(&file_locks_lock); 898 899 list_for_each_entry_safe(lock, tmp, &dispose, flist) { 900 list_del_init(&lock->flist); 901 if (lock->vfs_locked) 902 ksmbd_vfs_posix_lock_unblock(lock->fl); 903 locks_free_lock(lock->fl); 904 kfree(lock); 905 } 906 wake_up_all(&ksmbd_lock_wait); 907 } 908 909 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) 910 { 911 struct file *filp; 912 913 fd_limit_close(); 914 ksmbd_remove_durable_fd(fp); 915 __ksmbd_remove_fd(ft, fp); 916 917 close_id_del_oplock(fp); 918 filp = fp->filp; 919 920 /* Remove lookup entries before dropping the file's inode reference. */ 921 ksmbd_file_lock_clear(fp); 922 __ksmbd_inode_close(fp); 923 if (!IS_ERR_OR_NULL(filp)) 924 fput(filp); 925 926 /* 927 * Drop fp's strong connection references. Durable fps that reached 928 * the scavenger have already had these cleared by session_fd_check(). 929 */ 930 if (fp->conn) { 931 atomic_dec(&fp->conn->stats.open_files_count); 932 ksmbd_conn_put(fp->conn); 933 fp->conn = NULL; 934 } 935 if (fp->tcon) { 936 ksmbd_tree_connect_put(fp->tcon); 937 fp->tcon = NULL; 938 } 939 940 if (ksmbd_stream_fd(fp)) 941 kfree(fp->stream.name); 942 ksmbd_share_config_put(fp->share_conf); 943 kfree(fp->owner.name); 944 945 kmem_cache_free(filp_cache, fp); 946 } 947 948 /** 949 * ksmbd_close_disconnected_durable_delete_on_close() - drop a delete-on-close 950 * file kept present only by disconnected durable handles 951 * @dentry: dentry of the file being opened 952 * 953 * A durable handle opened with delete-on-close is preserved across a 954 * disconnect so it can be reclaimed by a durable reconnect. When a new 955 * (non-reconnect) open arrives for the same name instead, the disconnected 956 * handle has to give way. Close such handles so their delete-on-close is 957 * applied and the file is removed once the last handle is gone, letting the 958 * new open create a fresh file. 959 * 960 * The caller's inode reference is dropped before closing so that the final 961 * close can promote S_DEL_ON_CLS to S_DEL_PENDING and unlink the file. 962 * 963 * Return: true if a disconnected durable handle was closed. 964 */ 965 bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry *dentry) 966 { 967 struct ksmbd_inode *ci; 968 struct ksmbd_file *fp, *tmp; 969 LIST_HEAD(dispose); 970 bool closed = false; 971 972 ci = ksmbd_inode_lookup_lock(dentry); 973 if (!ci) 974 return false; 975 976 down_write(&ci->m_lock); 977 if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_ON_CLS_STREAM | S_DEL_PENDING)) { 978 list_for_each_entry_safe(fp, tmp, &ci->m_fp_list, node) { 979 if (fp->conn || !fp->is_durable || 980 fp->f_state != FP_INITED) 981 continue; 982 983 /* 984 * Claim the close before unlinking fp from m_fp_list. 985 * refcount == 1 means only the durable lifetime ref is 986 * left. Add a transient ref so final close can drop both. 987 */ 988 write_lock(&global_ft.lock); 989 if (atomic_read(&fp->refcount) == 1) { 990 atomic_inc(&fp->refcount); 991 __ksmbd_remove_durable_fd(fp); 992 ksmbd_mark_fp_closed(fp); 993 list_move_tail(&fp->node, &dispose); 994 } 995 write_unlock(&global_ft.lock); 996 } 997 } 998 up_write(&ci->m_lock); 999 1000 /* 1001 * Drop our lookup reference before closing so the last __ksmbd_close_fd() 1002 * can drop m_count to zero and unlink the delete-on-close file. The 1003 * collected handles still hold the transient reference taken above, so 1004 * ci stays valid until they are closed below. 1005 */ 1006 ksmbd_inode_put(ci); 1007 1008 while (!list_empty(&dispose)) { 1009 fp = list_first_entry(&dispose, struct ksmbd_file, node); 1010 list_del_init(&fp->node); 1011 if (atomic_sub_and_test(2, &fp->refcount)) { 1012 __ksmbd_close_fd(NULL, fp); 1013 closed = true; 1014 } 1015 } 1016 1017 return closed; 1018 } 1019 1020 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp) 1021 { 1022 if (fp->f_state != FP_INITED) 1023 return NULL; 1024 1025 if (!atomic_inc_not_zero(&fp->refcount)) 1026 return NULL; 1027 return fp; 1028 } 1029 1030 struct ksmbd_file *ksmbd_file_get(struct ksmbd_file *fp) 1031 { 1032 return ksmbd_fp_get(fp); 1033 } 1034 1035 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft, 1036 u64 id) 1037 { 1038 struct ksmbd_file *fp; 1039 1040 if (!has_file_id(id)) 1041 return NULL; 1042 1043 read_lock(&ft->lock); 1044 fp = idr_find(ft->idr, id); 1045 if (fp) 1046 fp = ksmbd_fp_get(fp); 1047 read_unlock(&ft->lock); 1048 return fp; 1049 } 1050 1051 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp) 1052 { 1053 __ksmbd_close_fd(fp->conn ? &work->sess->file_table : NULL, fp); 1054 } 1055 1056 static void set_close_state_blocked_works(struct ksmbd_file *fp) 1057 { 1058 struct ksmbd_work *cancel_work; 1059 1060 spin_lock(&fp->f_lock); 1061 list_for_each_entry(cancel_work, &fp->blocked_works, 1062 fp_entry) { 1063 if (xchg(&cancel_work->state, KSMBD_WORK_CLOSED) == 1064 KSMBD_WORK_ACTIVE) 1065 cancel_work->cancel_fn(cancel_work->cancel_argv); 1066 } 1067 spin_unlock(&fp->f_lock); 1068 } 1069 1070 void ksmbd_wake_session_blocked_works(struct ksmbd_session *sess) 1071 { 1072 struct ksmbd_file_table *ft = &sess->file_table; 1073 struct ksmbd_file *fp; 1074 unsigned int id; 1075 1076 read_lock(&ft->lock); 1077 idr_for_each_entry(ft->idr, fp, id) 1078 set_close_state_blocked_works(fp); 1079 read_unlock(&ft->lock); 1080 } 1081 1082 /** 1083 * ksmbd_unpublish_reopened_fd() - remove a reopened durable handle from the 1084 * session file table after its CREATE failed 1085 * @work: smb work of the failed CREATE 1086 * @fp: durable handle republished by ksmbd_reopen_durable_fd() 1087 * 1088 * The reopen publishes @fp in this session's table without taking a reference 1089 * for the entry, so the CREATE error path's puts need not be the last ones. 1090 * Another holder of a global-table reference, such as a concurrent reconnect 1091 * of the same handle, can drop the last one through ksmbd_put_durable_fd(), 1092 * which frees @fp without touching this table. Remove the id here, while 1093 * the caller still holds its references. 1094 * 1095 * Every path that removes the id before the final put also clears 1096 * @fp->volatile_id under ft->lock, so a valid id is still @fp's entry. 1097 */ 1098 void ksmbd_unpublish_reopened_fd(struct ksmbd_work *work, struct ksmbd_file *fp) 1099 { 1100 struct ksmbd_file_table *ft = &work->sess->file_table; 1101 1102 write_lock(&ft->lock); 1103 if (has_file_id(fp->volatile_id)) { 1104 idr_remove(ft->idr, fp->volatile_id); 1105 fp->volatile_id = KSMBD_NO_FID; 1106 } 1107 write_unlock(&ft->lock); 1108 } 1109 1110 int ksmbd_close_fd(struct ksmbd_work *work, u64 id) 1111 { 1112 struct ksmbd_file *fp; 1113 struct ksmbd_file_table *ft; 1114 bool closed = false; 1115 1116 if (!has_file_id(id)) 1117 return 0; 1118 1119 ft = &work->sess->file_table; 1120 write_lock(&ft->lock); 1121 fp = idr_find(ft->idr, id); 1122 if (fp) { 1123 set_close_state_blocked_works(fp); 1124 1125 if (fp->f_state != FP_INITED) 1126 fp = NULL; 1127 else { 1128 fp->f_state = FP_CLOSED; 1129 idr_remove(ft->idr, id); 1130 fp->volatile_id = KSMBD_NO_FID; 1131 closed = true; 1132 if (!atomic_dec_and_test(&fp->refcount)) 1133 fp = NULL; 1134 } 1135 } 1136 write_unlock(&ft->lock); 1137 1138 if (!fp) 1139 return closed ? 0 : -EINVAL; 1140 1141 __put_fd_final(work, fp); 1142 return 0; 1143 } 1144 1145 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp) 1146 { 1147 if (!fp) 1148 return; 1149 1150 if (!atomic_dec_and_test(&fp->refcount)) 1151 return; 1152 __put_fd_final(work, fp); 1153 } 1154 1155 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) 1156 { 1157 if (!fp) 1158 return false; 1159 if (fp->tcon != tcon) 1160 return false; 1161 return true; 1162 } 1163 1164 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id) 1165 { 1166 return __ksmbd_lookup_fd(&work->sess->file_table, id); 1167 } 1168 1169 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id) 1170 { 1171 struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 1172 1173 if (__sanity_check(work->tcon, fp)) 1174 return fp; 1175 1176 ksmbd_fd_put(work, fp); 1177 return NULL; 1178 } 1179 1180 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id, 1181 u64 pid) 1182 { 1183 struct ksmbd_file *fp; 1184 1185 if (!has_file_id(id)) { 1186 id = work->compound_fid; 1187 pid = work->compound_pfid; 1188 } 1189 1190 fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 1191 if (!__sanity_check(work->tcon, fp)) { 1192 ksmbd_fd_put(work, fp); 1193 return NULL; 1194 } 1195 if (fp->persistent_id != pid) { 1196 ksmbd_fd_put(work, fp); 1197 return NULL; 1198 } 1199 return fp; 1200 } 1201 1202 struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id) 1203 { 1204 return __ksmbd_lookup_fd(&global_ft, id); 1205 } 1206 1207 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) 1208 { 1209 struct ksmbd_file *fp; 1210 1211 fp = __ksmbd_lookup_fd(&global_ft, id); 1212 if (fp && (fp->durable_reconnect_disabled || 1213 fp->conn || 1214 (fp->durable_scavenger_timeout && 1215 (fp->durable_scavenger_timeout < 1216 jiffies_to_msecs(jiffies))))) { 1217 ksmbd_put_durable_fd(fp); 1218 fp = NULL; 1219 } 1220 1221 return fp; 1222 } 1223 1224 void ksmbd_put_durable_fd(struct ksmbd_file *fp) 1225 { 1226 if (!atomic_dec_and_test(&fp->refcount)) 1227 return; 1228 1229 __ksmbd_close_fd(NULL, fp); 1230 } 1231 1232 bool ksmbd_has_other_active_fd(struct ksmbd_file *fp) 1233 { 1234 struct ksmbd_file *lfp; 1235 struct ksmbd_inode *ci = fp->f_ci; 1236 bool ret = false; 1237 1238 down_read(&ci->m_lock); 1239 list_for_each_entry(lfp, &ci->m_fp_list, node) { 1240 if (lfp == fp) 1241 continue; 1242 1243 if (lfp->f_state == FP_INITED && 1244 (READ_ONCE(lfp->conn) || READ_ONCE(lfp->tcon))) { 1245 ret = true; 1246 break; 1247 } 1248 } 1249 up_read(&ci->m_lock); 1250 1251 return ret; 1252 } 1253 1254 bool ksmbd_app_instance_id_matches(struct ksmbd_work *work, 1255 struct ksmbd_file *fp) 1256 { 1257 bool matches; 1258 1259 /* Keep the current connection and share alive across the comparison. */ 1260 spin_lock(&fp->f_lock); 1261 matches = READ_ONCE(fp->f_state) == FP_INITED && 1262 fp->conn && fp->share_conf && 1263 !strcmp(fp->share_conf->name, work->tcon->share_conf->name) && 1264 memcmp(fp->conn->ClientGUID, work->conn->ClientGUID, 1265 SMB2_CLIENT_GUID_SIZE); 1266 spin_unlock(&fp->f_lock); 1267 1268 return matches; 1269 } 1270 1271 struct ksmbd_file * 1272 ksmbd_lookup_fd_app_instance_id_next(char *app_instance_id, int *cursor) 1273 { 1274 struct ksmbd_file *fp, *found = NULL; 1275 int id = *cursor; 1276 1277 read_lock(&global_ft.lock); 1278 while ((fp = idr_get_next(global_ft.idr, &id))) { 1279 *cursor = id + 1; 1280 if (fp->has_app_instance_id && 1281 !memcmp(fp->app_instance_id, app_instance_id, 1282 SMB2_CREATE_GUID_SIZE)) { 1283 found = ksmbd_fp_get(fp); 1284 if (found) 1285 break; 1286 } 1287 id++; 1288 } 1289 read_unlock(&global_ft.lock); 1290 1291 return found; 1292 } 1293 1294 int ksmbd_close_fd_app_instance_id(struct ksmbd_work *work, 1295 struct ksmbd_file *fp, bool *closed) 1296 { 1297 struct ksmbd_file_table *ft; 1298 struct ksmbd_session *sess; 1299 struct oplock_info *opinfo; 1300 u64 session_id; 1301 int n_to_drop = 0; 1302 1303 *closed = false; 1304 opinfo = opinfo_get(fp); 1305 if (!opinfo) 1306 goto out; 1307 1308 down_read(&fp->f_ci->m_lock); 1309 if (!opinfo->conn) { 1310 up_read(&fp->f_ci->m_lock); 1311 goto out; 1312 } 1313 /* 1314 * opinfo->sess is not refcounted, and m_lock does not keep it alive. 1315 * The owner can be torn down while we hold only a reference to fp. 1316 * Look the owner up by id to take a real reference instead. 1317 */ 1318 session_id = opinfo->session_id; 1319 up_read(&fp->f_ci->m_lock); 1320 1321 /* destroy_previous_session() takes m_lock under sessions_table_lock. */ 1322 sess = ksmbd_session_lookup_slowpath(session_id); 1323 if (!sess) 1324 goto out; 1325 1326 down_read(&fp->f_ci->m_lock); 1327 if (opinfo->sess != sess) { 1328 up_read(&fp->f_ci->m_lock); 1329 ksmbd_user_session_put(sess); 1330 goto out; 1331 } 1332 ft = &sess->file_table; 1333 write_lock(&ft->lock); 1334 /* 1335 * The table may already be detached by destroy_previous_session(). 1336 * A new session could also reuse the old id and address and pass the 1337 * check above. Only remove fp if this table still publishes it. 1338 */ 1339 if (ft->idr && fp->f_state == FP_INITED && 1340 has_file_id(fp->volatile_id) && 1341 idr_find(ft->idr, fp->volatile_id) == fp && 1342 ksmbd_app_instance_id_matches(work, fp)) { 1343 idr_remove(ft->idr, fp->volatile_id); 1344 fp->volatile_id = KSMBD_NO_FID; 1345 n_to_drop = ksmbd_mark_fp_closed(fp); 1346 } 1347 write_unlock(&ft->lock); 1348 up_read(&fp->f_ci->m_lock); 1349 ksmbd_user_session_put(sess); 1350 opinfo_put(opinfo); 1351 opinfo = NULL; 1352 1353 if (!n_to_drop) 1354 goto out; 1355 1356 down_write(&fp->f_ci->m_lock); 1357 list_del_init(&fp->node); 1358 up_write(&fp->f_ci->m_lock); 1359 1360 *closed = true; 1361 if (atomic_sub_and_test(n_to_drop, &fp->refcount)) 1362 __ksmbd_close_fd(NULL, fp); 1363 return 0; 1364 1365 out: 1366 if (opinfo) 1367 opinfo_put(opinfo); 1368 ksmbd_put_durable_fd(fp); 1369 return 0; 1370 } 1371 1372 int ksmbd_invalidate_durable_fd(unsigned long long id) 1373 { 1374 struct ksmbd_file *fp; 1375 1376 fp = ksmbd_lookup_global_fd(id); 1377 if (!fp) 1378 return -ENOENT; 1379 1380 fp->durable_reconnect_disabled = true; 1381 1382 if (fp->conn) { 1383 ksmbd_put_durable_fd(fp); 1384 return -ENOENT; 1385 } 1386 1387 fp->durable_timeout = 1; 1388 fp->durable_scavenger_timeout = jiffies_to_msecs(jiffies); 1389 ksmbd_put_durable_fd(fp); 1390 if (waitqueue_active(&dh_wq)) 1391 wake_up(&dh_wq); 1392 1393 return -ENOENT; 1394 } 1395 1396 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid) 1397 { 1398 struct ksmbd_file *fp = NULL; 1399 unsigned int id; 1400 1401 read_lock(&global_ft.lock); 1402 idr_for_each_entry(global_ft.idr, fp, id) { 1403 if (!memcmp(fp->create_guid, 1404 cguid, 1405 SMB2_CREATE_GUID_SIZE)) { 1406 fp = ksmbd_fp_get(fp); 1407 break; 1408 } 1409 } 1410 read_unlock(&global_ft.lock); 1411 1412 return fp; 1413 } 1414 1415 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry) 1416 { 1417 struct ksmbd_file *lfp; 1418 struct ksmbd_inode *ci; 1419 struct inode *inode = d_inode(dentry); 1420 1421 read_lock(&inode_hash_lock); 1422 ci = __ksmbd_inode_lookup(dentry); 1423 read_unlock(&inode_hash_lock); 1424 if (!ci) 1425 return NULL; 1426 1427 down_read(&ci->m_lock); 1428 list_for_each_entry(lfp, &ci->m_fp_list, node) { 1429 if (inode == file_inode(lfp->filp)) { 1430 lfp = ksmbd_fp_get(lfp); 1431 up_read(&ci->m_lock); 1432 ksmbd_inode_put(ci); 1433 return lfp; 1434 } 1435 } 1436 up_read(&ci->m_lock); 1437 ksmbd_inode_put(ci); 1438 return NULL; 1439 } 1440 1441 bool ksmbd_has_other_nonposix_open(struct dentry *dentry) 1442 { 1443 struct ksmbd_file *fp; 1444 struct inode *inode = d_inode(dentry); 1445 unsigned int id; 1446 bool ret = false; 1447 1448 if (!inode) 1449 return false; 1450 1451 read_lock(&global_ft.lock); 1452 idr_for_each_entry(global_ft.idr, fp, id) { 1453 if (READ_ONCE(fp->f_state) != FP_INITED) 1454 continue; 1455 if (inode != file_inode(fp->filp)) 1456 continue; 1457 if (fp->is_posix_ctxt) 1458 continue; 1459 1460 ret = true; 1461 break; 1462 } 1463 read_unlock(&global_ft.lock); 1464 1465 return ret; 1466 } 1467 1468 bool ksmbd_has_nonposix_open_child(struct ksmbd_file *old_fp) 1469 { 1470 struct dentry *dentry = old_fp->filp->f_path.dentry; 1471 struct ksmbd_file *fp; 1472 unsigned int id; 1473 bool ret = false; 1474 1475 read_lock(&global_ft.lock); 1476 idr_for_each_entry(global_ft.idr, fp, id) { 1477 struct dentry *fp_dentry = fp->filp->f_path.dentry; 1478 1479 if (fp->f_state != FP_INITED) 1480 continue; 1481 if (fp_dentry == dentry) 1482 continue; 1483 if (old_fp->is_posix_ctxt && fp->is_posix_ctxt) 1484 continue; 1485 if (is_subdir(fp_dentry, dentry)) { 1486 ret = true; 1487 break; 1488 } 1489 } 1490 read_unlock(&global_ft.lock); 1491 1492 return ret; 1493 } 1494 1495 #define OPEN_ID_TYPE_VOLATILE_ID (0) 1496 #define OPEN_ID_TYPE_PERSISTENT_ID (1) 1497 1498 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type) 1499 { 1500 if (type == OPEN_ID_TYPE_VOLATILE_ID) 1501 fp->volatile_id = id; 1502 if (type == OPEN_ID_TYPE_PERSISTENT_ID) 1503 fp->persistent_id = id; 1504 } 1505 1506 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 1507 int type) 1508 { 1509 u64 id = 0; 1510 int ret; 1511 1512 idr_preload(KSMBD_DEFAULT_GFP); 1513 write_lock(&ft->lock); 1514 ret = idr_alloc_cyclic(ft->idr, fp, KSMBD_START_FID, INT_MAX - 1, 1515 GFP_NOWAIT); 1516 if (ret >= 0) { 1517 id = ret; 1518 ret = 0; 1519 } else { 1520 id = KSMBD_NO_FID; 1521 } 1522 1523 __open_id_set(fp, id, type); 1524 write_unlock(&ft->lock); 1525 idr_preload_end(); 1526 return ret; 1527 } 1528 1529 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp) 1530 { 1531 __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID); 1532 return fp->persistent_id; 1533 } 1534 1535 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp) 1536 { 1537 struct ksmbd_file *fp; 1538 int ret; 1539 1540 /* Durable reconnects reuse the slot held for the lifetime of fp. */ 1541 if (fd_limit_depleted()) 1542 return ERR_PTR(-EMFILE); 1543 1544 fp = kmem_cache_zalloc(filp_cache, KSMBD_DEFAULT_GFP); 1545 if (!fp) { 1546 pr_err("Failed to allocate memory\n"); 1547 ret = -ENOMEM; 1548 goto err_fd_limit; 1549 } 1550 1551 INIT_LIST_HEAD(&fp->blocked_works); 1552 INIT_LIST_HEAD(&fp->node); 1553 INIT_LIST_HEAD(&fp->lock_list); 1554 spin_lock_init(&fp->f_lock); 1555 mutex_init(&fp->readdir_lock); 1556 atomic_set(&fp->refcount, 1); 1557 1558 fp->filp = filp; 1559 /* 1560 * fp owns strong references on fp->conn and fp->tcon for as long as 1561 * they are non-NULL. Paired with the puts in session_fd_check() 1562 * (durable preserve), in __ksmbd_close_fd() (final close), and on the 1563 * error paths below. 1564 */ 1565 fp->conn = ksmbd_conn_get(work->conn); 1566 fp->tcon = ksmbd_tree_connect_get(work->tcon); 1567 fp->share_conf = work->tcon->share_conf; 1568 atomic_inc(&fp->share_conf->refcount); 1569 fp->volatile_id = KSMBD_NO_FID; 1570 fp->persistent_id = KSMBD_NO_FID; 1571 fp->f_state = FP_NEW; 1572 fp->f_ci = ksmbd_inode_get(fp); 1573 1574 if (!fp->f_ci) { 1575 ret = -ENOMEM; 1576 goto err_out; 1577 } 1578 1579 down_read(&work->sess->tree_conns_lock); 1580 if (work->tcon->t_state == TREE_CONNECTED) 1581 ret = __open_id(&work->sess->file_table, fp, 1582 OPEN_ID_TYPE_VOLATILE_ID); 1583 else 1584 ret = -ENOTCONN; 1585 up_read(&work->sess->tree_conns_lock); 1586 if (ret) { 1587 ksmbd_inode_put(fp->f_ci); 1588 goto err_out; 1589 } 1590 1591 atomic_inc(&work->conn->stats.open_files_count); 1592 return fp; 1593 1594 err_out: 1595 /* fp->conn and fp->tcon were refcounted before every branch here. */ 1596 ksmbd_tree_connect_put(fp->tcon); 1597 ksmbd_conn_put(fp->conn); 1598 ksmbd_share_config_put(fp->share_conf); 1599 kmem_cache_free(filp_cache, fp); 1600 err_fd_limit: 1601 fd_limit_close(); 1602 return ERR_PTR(ret); 1603 } 1604 1605 /** 1606 * ksmbd_update_fstate() - update an fp state under the file-table lock 1607 * @ft: file table that publishes @fp's volatile id 1608 * @fp: file pointer to update 1609 * @state: new state 1610 * 1611 * Return: 0 on success. The FP_NEW -> FP_INITED transition is special: 1612 * -ENOENT if teardown already unpublished @fp by advancing the state or 1613 * clearing the volatile id. On success it also takes a reference for the 1614 * opening request, which must be released with ksmbd_fd_put(). Other state 1615 * updates preserve the historical fire-and-forget behavior. 1616 */ 1617 int ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 1618 unsigned int state) 1619 { 1620 int ret; 1621 1622 if (!fp) 1623 return -ENOENT; 1624 1625 write_lock(&ft->lock); 1626 if (state == FP_INITED && 1627 (fp->f_state != FP_NEW || !has_file_id(fp->volatile_id))) { 1628 ret = -ENOENT; 1629 } else { 1630 fp->f_state = state; 1631 if (state == FP_INITED) 1632 atomic_inc(&fp->refcount); 1633 ret = 0; 1634 } 1635 write_unlock(&ft->lock); 1636 1637 return ret; 1638 } 1639 1640 /* 1641 * ksmbd_mark_fp_closed() - mark fp closed under ft->lock and return how many 1642 * refs the teardown path owns. 1643 * 1644 * FP_INITED has a normal idr-owned reference, so teardown owns both that 1645 * reference and the transient lookup reference. FP_NEW is still owned by the 1646 * in-flight opener/reopener, which will drop the original reference after 1647 * ksmbd_update_fstate(..., FP_INITED) observes the cleared volatile id. 1648 * FP_CLOSED on entry means an earlier ksmbd_close_fd() already consumed the 1649 * idr-owned ref. 1650 */ 1651 static int ksmbd_mark_fp_closed(struct ksmbd_file *fp) 1652 { 1653 if (fp->f_state == FP_INITED) { 1654 set_close_state_blocked_works(fp); 1655 fp->f_state = FP_CLOSED; 1656 return 2; 1657 } 1658 1659 return 1; 1660 } 1661 1662 static void 1663 __close_file_table_ids(struct ksmbd_session *sess, 1664 struct ksmbd_tree_connect *tcon, 1665 bool (*skip)(struct ksmbd_tree_connect *tcon, 1666 struct ksmbd_file *fp, 1667 struct ksmbd_user *user), 1668 bool skip_preserves_fp) 1669 { 1670 struct ksmbd_file_table *ft = &sess->file_table; 1671 struct ksmbd_file *fp; 1672 unsigned int id = 0; 1673 1674 while (1) { 1675 int n_to_drop; 1676 1677 write_lock(&ft->lock); 1678 fp = idr_get_next(ft->idr, &id); 1679 if (!fp) { 1680 write_unlock(&ft->lock); 1681 break; 1682 } 1683 if (!atomic_inc_not_zero(&fp->refcount)) { 1684 id++; 1685 write_unlock(&ft->lock); 1686 continue; 1687 } 1688 1689 if (skip_preserves_fp) { 1690 /* 1691 * Session teardown: skip() is session_fd_check(), 1692 * which may sleep and mutates fp->conn / fp->tcon / 1693 * fp->volatile_id when it chooses to preserve fp 1694 * for durable reconnect. Unpublish fp from the 1695 * session idr here, under ft->lock, so that 1696 * __ksmbd_lookup_fd() through this session cannot 1697 * grant a new ksmbd_fp_get() reference to an fp 1698 * whose fields are about to be rewritten outside 1699 * the lock. Durable reconnect still reaches fp via 1700 * global_ft. 1701 */ 1702 idr_remove(ft->idr, id); 1703 fp->durable_volatile_id = fp->volatile_id; 1704 fp->volatile_id = KSMBD_NO_FID; 1705 write_unlock(&ft->lock); 1706 1707 if (skip(tcon, fp, sess->user)) { 1708 /* 1709 * session_fd_check() has converted fp to 1710 * durable-preserve state and cleared its 1711 * per-conn fields. fp is already unpublished 1712 * above; the original idr-owned ref keeps it 1713 * alive for the durable scavenger. Drop only 1714 * the transient ref. atomic_dec() is safe -- 1715 * atomic_inc_not_zero() succeeded on a 1716 * positive value and we added one more, so 1717 * refcount cannot be zero here. 1718 */ 1719 atomic_dec(&fp->refcount); 1720 id++; 1721 continue; 1722 } 1723 1724 /* 1725 * Keep the close-state decision under the same lock 1726 * observed by ksmbd_update_fstate(), which is how an 1727 * in-flight FP_NEW opener learns that teardown has 1728 * cleared its volatile id. 1729 */ 1730 write_lock(&ft->lock); 1731 n_to_drop = ksmbd_mark_fp_closed(fp); 1732 write_unlock(&ft->lock); 1733 } else { 1734 /* 1735 * Tree teardown: skip() is tree_conn_fd_check(), a 1736 * cheap pointer compare that doesn't sleep and has 1737 * no side effects, so keep the skip decision plus 1738 * the unpublish-and-mark-closed sequence atomic 1739 * under ft->lock. fps belonging to other tree 1740 * connects (skip() == true) stay fully published in 1741 * the session idr with no lock window. 1742 */ 1743 if (skip(tcon, fp, sess->user)) { 1744 atomic_dec(&fp->refcount); 1745 write_unlock(&ft->lock); 1746 id++; 1747 continue; 1748 } 1749 idr_remove(ft->idr, id); 1750 fp->volatile_id = KSMBD_NO_FID; 1751 n_to_drop = ksmbd_mark_fp_closed(fp); 1752 write_unlock(&ft->lock); 1753 } 1754 1755 /* 1756 * Stop inode walkers from finding the open after close is committed. 1757 * Final close also unlinks fp if a global lookup owns the last put. 1758 */ 1759 down_write(&fp->f_ci->m_lock); 1760 list_del_init(&fp->node); 1761 up_write(&fp->f_ci->m_lock); 1762 1763 /* 1764 * Drop the references this iteration owns: 1765 * 1766 * n_to_drop == 2: we observed FP_INITED and committed 1767 * the FP_CLOSED transition ourselves, so we own the 1768 * transient (+1) and the still-intact idr-owned ref. 1769 * 1770 * n_to_drop == 1: either a prior ksmbd_close_fd() 1771 * already consumed the idr-owned ref, or fp was still 1772 * FP_NEW and the in-flight opener/reopener must keep 1773 * the original reference until ksmbd_update_fstate() 1774 * observes the cleared volatile id. 1775 * 1776 * The final close accounts the open against fp->conn, 1777 * including when an in-flight user drops the last reference. 1778 */ 1779 if (atomic_sub_and_test(n_to_drop, &fp->refcount)) 1780 __ksmbd_close_fd(NULL, fp); 1781 id++; 1782 } 1783 } 1784 1785 static inline bool is_reconnectable(struct ksmbd_file *fp) 1786 { 1787 struct oplock_info *opinfo = opinfo_get(fp); 1788 bool reconn = false; 1789 1790 if (!opinfo) 1791 return false; 1792 1793 if (opinfo->op_state != OPLOCK_STATE_NONE) { 1794 opinfo_put(opinfo); 1795 return false; 1796 } 1797 1798 if (fp->is_resilient || fp->is_persistent) 1799 reconn = true; 1800 else if (fp->is_durable && opinfo->is_lease && 1801 opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE) 1802 reconn = true; 1803 1804 else if (fp->is_durable && opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) 1805 reconn = true; 1806 1807 opinfo_put(opinfo); 1808 return reconn; 1809 } 1810 1811 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon, 1812 struct ksmbd_file *fp, 1813 struct ksmbd_user *user) 1814 { 1815 return fp->tcon != tcon; 1816 } 1817 1818 static bool ksmbd_durable_scavenger_alive(void) 1819 { 1820 if (!durable_scavenger_running) 1821 return false; 1822 1823 if (kthread_should_stop()) 1824 return false; 1825 1826 if (idr_is_empty(global_ft.idr)) 1827 return false; 1828 1829 return true; 1830 } 1831 1832 static void ksmbd_scavenger_dispose_dh(struct ksmbd_file *fp) 1833 { 1834 /* 1835 * Durable-preserved fp can remain linked on f_ci->m_fp_list for 1836 * share-mode checks. Unlink it before final close; fp->node is not 1837 * available as a scavenger-private list node because re-adding it to 1838 * another list corrupts m_fp_list. 1839 */ 1840 down_write(&fp->f_ci->m_lock); 1841 list_del_init(&fp->node); 1842 up_write(&fp->f_ci->m_lock); 1843 1844 /* 1845 * Drop both the durable lifetime reference and the transient reference 1846 * taken by the scavenger under global_ft.lock. If a concurrent 1847 * ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp 1848 * before the unlink above, that holder owns the final close via 1849 * ksmbd_fd_put() -> __ksmbd_close_fd(). Otherwise the scavenger is 1850 * the last putter and finalises fp here. 1851 */ 1852 if (atomic_sub_and_test(2, &fp->refcount)) 1853 __ksmbd_close_fd(NULL, fp); 1854 } 1855 1856 static int ksmbd_durable_scavenger(void *dummy) 1857 { 1858 struct ksmbd_file *fp = NULL; 1859 struct ksmbd_file *expired_fp; 1860 unsigned int id; 1861 unsigned int min_timeout = 1; 1862 bool found_fp_timeout; 1863 unsigned long remaining_jiffies; 1864 1865 __module_get(THIS_MODULE); 1866 1867 set_freezable(); 1868 while (ksmbd_durable_scavenger_alive()) { 1869 if (try_to_freeze()) 1870 continue; 1871 1872 remaining_jiffies = wait_event_interruptible_timeout(dh_wq, 1873 ksmbd_durable_scavenger_alive() == false, 1874 __msecs_to_jiffies(min_timeout)); 1875 if ((long)remaining_jiffies > 0) 1876 min_timeout = jiffies_to_msecs(remaining_jiffies); 1877 else 1878 min_timeout = DURABLE_HANDLE_MAX_TIMEOUT; 1879 1880 do { 1881 expired_fp = NULL; 1882 found_fp_timeout = false; 1883 1884 write_lock(&global_ft.lock); 1885 idr_for_each_entry(global_ft.idr, fp, id) { 1886 unsigned long durable_timeout; 1887 1888 if (!fp->durable_timeout) 1889 continue; 1890 1891 if (atomic_read(&fp->refcount) > 1 || 1892 fp->conn) 1893 continue; 1894 1895 found_fp_timeout = true; 1896 if (fp->durable_scavenger_timeout <= 1897 jiffies_to_msecs(jiffies)) { 1898 __ksmbd_remove_durable_fd(fp); 1899 /* 1900 * Take a transient reference so fp 1901 * cannot be freed by an in-flight 1902 * ksmbd_lookup_fd_inode() that found 1903 * it through f_ci->m_fp_list while we 1904 * drop global_ft.lock and reach the 1905 * m_fp_list unlink in 1906 * ksmbd_scavenger_dispose_dh(). 1907 */ 1908 atomic_inc(&fp->refcount); 1909 expired_fp = fp; 1910 break; 1911 } 1912 1913 durable_timeout = 1914 fp->durable_scavenger_timeout - 1915 jiffies_to_msecs(jiffies); 1916 1917 if (min_timeout > durable_timeout) 1918 min_timeout = durable_timeout; 1919 } 1920 write_unlock(&global_ft.lock); 1921 1922 if (expired_fp) 1923 ksmbd_scavenger_dispose_dh(expired_fp); 1924 } while (expired_fp); 1925 1926 if (found_fp_timeout == false) 1927 break; 1928 } 1929 1930 durable_scavenger_running = false; 1931 1932 module_put(THIS_MODULE); 1933 1934 return 0; 1935 } 1936 1937 void ksmbd_launch_ksmbd_durable_scavenger(void) 1938 { 1939 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE)) 1940 return; 1941 1942 mutex_lock(&durable_scavenger_lock); 1943 if (durable_scavenger_running == true) { 1944 mutex_unlock(&durable_scavenger_lock); 1945 return; 1946 } 1947 1948 durable_scavenger_running = true; 1949 1950 server_conf.dh_task = kthread_run(ksmbd_durable_scavenger, 1951 (void *)NULL, "ksmbd-durable-scavenger"); 1952 if (IS_ERR(server_conf.dh_task)) { 1953 pr_err("cannot start conn thread, err : %ld\n", 1954 PTR_ERR(server_conf.dh_task)); 1955 server_conf.dh_task = NULL; 1956 durable_scavenger_running = false; 1957 } 1958 mutex_unlock(&durable_scavenger_lock); 1959 } 1960 1961 void ksmbd_stop_durable_scavenger(void) 1962 { 1963 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE)) 1964 return; 1965 1966 mutex_lock(&durable_scavenger_lock); 1967 if (!durable_scavenger_running) { 1968 mutex_unlock(&durable_scavenger_lock); 1969 return; 1970 } 1971 1972 durable_scavenger_running = false; 1973 if (waitqueue_active(&dh_wq)) 1974 wake_up(&dh_wq); 1975 mutex_unlock(&durable_scavenger_lock); 1976 kthread_stop(server_conf.dh_task); 1977 } 1978 1979 /* 1980 * ksmbd_vfs_set_durable_owner - Store owner info for durable replay/reconnect 1981 * @fp: ksmbd file pointer to store owner info 1982 * @user: user pointer to copy from 1983 * 1984 * This function binds the current user's identity to the file handle 1985 * to satisfy MS-SMB2 Step 8 (SecurityContext matching) during reconnect. 1986 * 1987 * Return: 0 on success, or negative error code on failure 1988 */ 1989 int ksmbd_vfs_set_durable_owner(struct ksmbd_file *fp, 1990 struct ksmbd_user *user) 1991 { 1992 char *name, *old_name; 1993 1994 if (!user) 1995 return -EINVAL; 1996 1997 /* Duplicate the user name to ensure identity persistence */ 1998 name = kstrdup(user->name, GFP_KERNEL); 1999 if (!name) 2000 return -ENOMEM; 2001 2002 spin_lock(&fp->f_lock); 2003 old_name = fp->owner.name; 2004 fp->owner.uid = user->uid; 2005 fp->owner.gid = user->gid; 2006 fp->owner.name = name; 2007 spin_unlock(&fp->f_lock); 2008 kfree(old_name); 2009 2010 return 0; 2011 } 2012 2013 /** 2014 * ksmbd_vfs_compare_durable_owner - Verify if the requester is original owner 2015 * @fp: existing ksmbd file pointer 2016 * @user: user pointer of the reconnect requester 2017 * 2018 * Compares the UID, GID, and name of the current requester against the 2019 * original owner stored in the file handle. 2020 * 2021 * Return: true if the user matches, false otherwise 2022 */ 2023 bool ksmbd_vfs_compare_durable_owner(struct ksmbd_file *fp, 2024 struct ksmbd_user *user) 2025 { 2026 bool ret = false; 2027 2028 if (!user) 2029 return false; 2030 2031 spin_lock(&fp->f_lock); 2032 if (!fp->owner.name) 2033 goto out; 2034 2035 /* Check if the UID and GID match first (fast path) */ 2036 if (fp->owner.uid != user->uid || fp->owner.gid != user->gid) 2037 goto out; 2038 2039 /* Validate the account name to ensure the same SecurityContext */ 2040 ret = (strcmp(fp->owner.name, user->name) == 0); 2041 out: 2042 spin_unlock(&fp->f_lock); 2043 return ret; 2044 } 2045 2046 static bool session_fd_check(struct ksmbd_tree_connect *tcon, 2047 struct ksmbd_file *fp, struct ksmbd_user *user) 2048 { 2049 struct ksmbd_inode *ci; 2050 struct oplock_info *op; 2051 struct ksmbd_conn *conn; 2052 struct ksmbd_tree_connect *tree_conn; 2053 2054 if (!is_reconnectable(fp)) 2055 return false; 2056 2057 if (fp->f_state != FP_INITED) 2058 return false; 2059 2060 if (WARN_ON_ONCE(!fp->conn)) 2061 return false; 2062 2063 if (ksmbd_vfs_set_durable_owner(fp, user)) 2064 return false; 2065 2066 /* 2067 * fp owns a strong reference on fp->conn (taken in ksmbd_open_fd() 2068 * / ksmbd_reopen_durable_fd()), so conn stays valid for the whole 2069 * body of this function regardless of any op->conn puts below. 2070 */ 2071 ci = fp->f_ci; 2072 down_write(&ci->m_lock); 2073 conn = fp->conn; 2074 tree_conn = fp->tcon; 2075 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 2076 lockdep_is_held(&ci->m_lock)) { 2077 if (op->o_fp != fp) 2078 continue; 2079 ksmbd_conn_put(op->conn); 2080 op->conn = NULL; 2081 op->sess = NULL; 2082 op->session_id = 0; 2083 } 2084 write_lock(&global_ft.lock); 2085 fp->conn = NULL; 2086 fp->tcon = NULL; 2087 fp->volatile_id = KSMBD_NO_FID; 2088 2089 if (fp->durable_timeout) 2090 fp->durable_scavenger_timeout = 2091 jiffies_to_msecs(jiffies) + fp->durable_timeout; 2092 atomic_dec(&conn->stats.open_files_count); 2093 write_unlock(&global_ft.lock); 2094 up_write(&ci->m_lock); 2095 2096 /* Drop fp's own reference on conn. */ 2097 ksmbd_conn_put(conn); 2098 ksmbd_tree_connect_put(tree_conn); 2099 return true; 2100 } 2101 2102 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work) 2103 { 2104 __close_file_table_ids(work->sess, work->tcon, tree_conn_fd_check, false); 2105 } 2106 2107 void ksmbd_close_session_fds(struct ksmbd_work *work) 2108 { 2109 __close_file_table_ids(work->sess, work->tcon, session_fd_check, true); 2110 } 2111 2112 int ksmbd_init_global_file_table(void) 2113 { 2114 INIT_DELAYED_WORK(&oplock_break_timer_work, ksmbd_oplock_break_timer); 2115 oplock_break_timer_pending = false; 2116 if (create_proc_files()) 2117 pr_warn("Unable to create files procfs entry\n"); 2118 return ksmbd_init_file_table(&global_ft); 2119 } 2120 2121 void ksmbd_free_global_file_table(void) 2122 { 2123 struct ksmbd_file *fp = NULL; 2124 unsigned int id; 2125 2126 cancel_delayed_work_sync(&oplock_break_timer_work); 2127 2128 idr_for_each_entry(global_ft.idr, fp, id) { 2129 ksmbd_remove_durable_fd(fp); 2130 __ksmbd_close_fd(NULL, fp); 2131 } 2132 2133 idr_destroy(global_ft.idr); 2134 kfree(global_ft.idr); 2135 } 2136 2137 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share, 2138 struct ksmbd_file *fp, char *name) 2139 { 2140 const char *share_path = share->real_path; 2141 size_t share_path_sz = share->real_path_sz; 2142 char *pathname, *ab_pathname; 2143 int ret = 0; 2144 2145 pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP); 2146 if (!pathname) 2147 return -EACCES; 2148 2149 ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX); 2150 if (IS_ERR(ab_pathname)) { 2151 kfree(pathname); 2152 return -EACCES; 2153 } 2154 2155 if (name) { 2156 size_t len = strlen(ab_pathname); 2157 2158 if (len == share_path_sz && !strncmp(ab_pathname, share_path, len)) { 2159 /* the durable fp is the share root itself */ 2160 if (name[0]) 2161 ret = -EINVAL; 2162 } else if (share_path_sz == 1 && share_path[0] == '/') { 2163 if (ab_pathname[0] != '/' || 2164 strcmp(ab_pathname + 1, name)) 2165 ret = -EINVAL; 2166 } else if (len <= share_path_sz || 2167 strncmp(ab_pathname, share_path, share_path_sz) || 2168 ab_pathname[share_path_sz] != '/' || 2169 strcmp(&ab_pathname[share_path_sz + 1], name)) { 2170 ret = -EINVAL; 2171 } 2172 if (ret) 2173 ksmbd_debug(SMB, "invalid name reconnect %s\n", name); 2174 } 2175 2176 kfree(pathname); 2177 2178 return ret; 2179 } 2180 2181 int ksmbd_reopen_durable_fd(struct ksmbd_work *work, struct ksmbd_file *fp) 2182 { 2183 struct ksmbd_inode *ci = fp->f_ci; 2184 struct oplock_info *op; 2185 struct ksmbd_conn *conn = work->conn; 2186 struct ksmbd_share_config *old_share; 2187 unsigned int old_f_state; 2188 int ret = -EBADF; 2189 2190 down_write(&ci->m_lock); 2191 write_lock(&global_ft.lock); 2192 if ((!fp->is_durable && !fp->is_persistent) || fp->conn || fp->tcon || 2193 fp->f_state != FP_INITED) { 2194 write_unlock(&global_ft.lock); 2195 pr_err("Invalid durable fd [%p:%p]\n", fp->conn, fp->tcon); 2196 goto out; 2197 } 2198 2199 if (has_file_id(fp->volatile_id)) { 2200 write_unlock(&global_ft.lock); 2201 pr_err("Still in use durable fd: %llu\n", fp->volatile_id); 2202 goto out; 2203 } 2204 2205 /* 2206 * Initialize fp's connection binding before publishing fp into the 2207 * session's file table. If __open_id() is ordered first, a 2208 * concurrent teardown that iterates the table can observe a valid 2209 * volatile_id with fp->conn == NULL and preserve a 2210 * partially-initialized fp. fp owns a strong reference on the new 2211 * conn (see ksmbd_open_fd()); undo it on __open_id() failure. Serialize 2212 * publication with tree disconnect just as ksmbd_open_fd() does. 2213 */ 2214 spin_lock(&fp->f_lock); 2215 old_f_state = fp->f_state; 2216 fp->f_state = FP_NEW; 2217 old_share = fp->share_conf; 2218 fp->share_conf = work->tcon->share_conf; 2219 atomic_inc(&fp->share_conf->refcount); 2220 fp->conn = ksmbd_conn_get(conn); 2221 fp->tcon = ksmbd_tree_connect_get(work->tcon); 2222 spin_unlock(&fp->f_lock); 2223 atomic_inc(&conn->stats.open_files_count); 2224 write_unlock(&global_ft.lock); 2225 2226 down_read(&work->sess->tree_conns_lock); 2227 if (work->tcon->t_state == TREE_CONNECTED) 2228 ret = __open_id(&work->sess->file_table, fp, 2229 OPEN_ID_TYPE_VOLATILE_ID); 2230 else 2231 ret = -ENOTCONN; 2232 up_read(&work->sess->tree_conns_lock); 2233 if (ret) { 2234 write_lock(&global_ft.lock); 2235 spin_lock(&fp->f_lock); 2236 fp->conn = NULL; 2237 fp->tcon = NULL; 2238 fp->share_conf = old_share; 2239 fp->f_state = old_f_state; 2240 spin_unlock(&fp->f_lock); 2241 atomic_dec(&conn->stats.open_files_count); 2242 write_unlock(&global_ft.lock); 2243 ksmbd_conn_put(conn); 2244 ksmbd_tree_connect_put(work->tcon); 2245 ksmbd_share_config_put(work->tcon->share_conf); 2246 if (ret != -ENOTCONN) 2247 ret = -EBADF; 2248 goto out; 2249 } 2250 2251 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 2252 lockdep_is_held(&ci->m_lock)) { 2253 if (op->conn || op->o_fp != fp) 2254 continue; 2255 op->conn = ksmbd_conn_get(fp->conn); 2256 op->sess = work->sess; 2257 op->session_id = work->sess->id; 2258 } 2259 2260 spin_lock(&fp->f_lock); 2261 fp->owner.uid = fp->owner.gid = 0; 2262 kfree(fp->owner.name); 2263 fp->owner.name = NULL; 2264 spin_unlock(&fp->f_lock); 2265 ksmbd_share_config_put(old_share); 2266 2267 ret = 0; 2268 out: 2269 up_write(&ci->m_lock); 2270 return ret; 2271 } 2272 2273 /** 2274 * ksmbd_rebind_replay_fd() - bind a completed replay to a surviving channel 2275 * @work: replay request, with a referenced session 2276 * @fp: referenced open whose owner and lease key have been checked 2277 * 2278 * Return: 0 on success, -ENOEXEC for another session, or -EBADF for a closed open. 2279 */ 2280 int ksmbd_rebind_replay_fd(struct ksmbd_work *work, struct ksmbd_file *fp) 2281 { 2282 struct ksmbd_file_table *ft = &work->sess->file_table; 2283 struct ksmbd_inode *ci = fp->f_ci; 2284 struct ksmbd_conn *old_conn = NULL; 2285 struct oplock_info *op; 2286 int ret = 0; 2287 2288 /* CLOSE unpublishes under ft->lock. Detach takes m_lock afterward. */ 2289 down_write(&ci->m_lock); 2290 read_lock(&ft->lock); 2291 if (fp->f_state != FP_INITED) { 2292 ret = -EBADF; 2293 goto out; 2294 } 2295 /* Detached durable opens follow the existing reconnect path. */ 2296 if (!fp->conn) 2297 goto out; 2298 if (!has_file_id(fp->volatile_id)) { 2299 ret = -EBADF; 2300 goto out; 2301 } 2302 if (idr_find(ft->idr, fp->volatile_id) != fp) { 2303 ret = -ENOEXEC; 2304 goto out; 2305 } 2306 if (fp->conn == work->conn || 2307 (!ksmbd_conn_exiting(fp->conn) && !ksmbd_conn_releasing(fp->conn))) 2308 goto out; 2309 2310 write_lock(&global_ft.lock); 2311 old_conn = fp->conn; 2312 fp->conn = ksmbd_conn_get(work->conn); 2313 atomic_inc(&fp->conn->stats.open_files_count); 2314 atomic_dec(&old_conn->stats.open_files_count); 2315 write_unlock(&global_ft.lock); 2316 2317 list_for_each_entry_rcu(op, &ci->m_op_list, op_entry, 2318 lockdep_is_held(&ci->m_lock)) { 2319 struct ksmbd_conn *conn; 2320 2321 if (op->o_fp != fp) 2322 continue; 2323 conn = op->conn; 2324 op->conn = ksmbd_conn_get(fp->conn); 2325 op->sess = work->sess; 2326 op->session_id = work->sess->id; 2327 ksmbd_conn_put(conn); 2328 } 2329 out: 2330 read_unlock(&ft->lock); 2331 up_write(&ci->m_lock); 2332 ksmbd_conn_put(old_conn); 2333 return ret; 2334 } 2335 2336 int ksmbd_init_file_table(struct ksmbd_file_table *ft) 2337 { 2338 ft->idr = kzalloc_obj(struct idr, KSMBD_DEFAULT_GFP); 2339 if (!ft->idr) 2340 return -ENOMEM; 2341 2342 idr_init(ft->idr); 2343 rwlock_init(&ft->lock); 2344 return 0; 2345 } 2346 2347 void ksmbd_destroy_file_table(struct ksmbd_session *sess) 2348 { 2349 struct ksmbd_file_table *ft = &sess->file_table; 2350 struct idr *idr; 2351 2352 if (!ft->idr) 2353 return; 2354 2355 __close_file_table_ids(sess, NULL, session_fd_check, true); 2356 /* Stop procfs readers before freeing the detached file table. */ 2357 write_lock(&ft->lock); 2358 idr = ft->idr; 2359 ft->idr = NULL; 2360 write_unlock(&ft->lock); 2361 idr_destroy(idr); 2362 kfree(idr); 2363 } 2364 2365 int ksmbd_init_file_cache(void) 2366 { 2367 filp_cache = kmem_cache_create("ksmbd_file_cache", 2368 sizeof(struct ksmbd_file), 0, 2369 SLAB_HWCACHE_ALIGN, NULL); 2370 if (!filp_cache) 2371 goto out; 2372 2373 init_waitqueue_head(&dh_wq); 2374 2375 return 0; 2376 2377 out: 2378 pr_err("failed to allocate file cache\n"); 2379 return -ENOMEM; 2380 } 2381 2382 void ksmbd_exit_file_cache(void) 2383 { 2384 kmem_cache_destroy(filp_cache); 2385 } 2386