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 12 #include "glob.h" 13 #include "vfs_cache.h" 14 #include "oplock.h" 15 #include "vfs.h" 16 #include "connection.h" 17 #include "mgmt/tree_connect.h" 18 #include "mgmt/user_session.h" 19 #include "smb_common.h" 20 21 #define S_DEL_PENDING 1 22 #define S_DEL_ON_CLS 2 23 #define S_DEL_ON_CLS_STREAM 8 24 25 static unsigned int inode_hash_mask __read_mostly; 26 static unsigned int inode_hash_shift __read_mostly; 27 static struct hlist_head *inode_hashtable __read_mostly; 28 static DEFINE_RWLOCK(inode_hash_lock); 29 30 static struct ksmbd_file_table global_ft; 31 static atomic_long_t fd_limit; 32 static struct kmem_cache *filp_cache; 33 34 void ksmbd_set_fd_limit(unsigned long limit) 35 { 36 limit = min(limit, get_max_files()); 37 atomic_long_set(&fd_limit, limit); 38 } 39 40 static bool fd_limit_depleted(void) 41 { 42 long v = atomic_long_dec_return(&fd_limit); 43 44 if (v >= 0) 45 return false; 46 atomic_long_inc(&fd_limit); 47 return true; 48 } 49 50 static void fd_limit_close(void) 51 { 52 atomic_long_inc(&fd_limit); 53 } 54 55 /* 56 * INODE hash 57 */ 58 59 static unsigned long inode_hash(struct super_block *sb, unsigned long hashval) 60 { 61 unsigned long tmp; 62 63 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / 64 L1_CACHE_BYTES; 65 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> inode_hash_shift); 66 return tmp & inode_hash_mask; 67 } 68 69 static struct ksmbd_inode *__ksmbd_inode_lookup(struct dentry *de) 70 { 71 struct hlist_head *head = inode_hashtable + 72 inode_hash(d_inode(de)->i_sb, (unsigned long)de); 73 struct ksmbd_inode *ci = NULL, *ret_ci = NULL; 74 75 hlist_for_each_entry(ci, head, m_hash) { 76 if (ci->m_de == de) { 77 if (atomic_inc_not_zero(&ci->m_count)) 78 ret_ci = ci; 79 break; 80 } 81 } 82 return ret_ci; 83 } 84 85 static struct ksmbd_inode *ksmbd_inode_lookup(struct ksmbd_file *fp) 86 { 87 return __ksmbd_inode_lookup(fp->filp->f_path.dentry); 88 } 89 90 struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d) 91 { 92 struct ksmbd_inode *ci; 93 94 read_lock(&inode_hash_lock); 95 ci = __ksmbd_inode_lookup(d); 96 read_unlock(&inode_hash_lock); 97 98 return ci; 99 } 100 101 int ksmbd_query_inode_status(struct dentry *dentry) 102 { 103 struct ksmbd_inode *ci; 104 int ret = KSMBD_INODE_STATUS_UNKNOWN; 105 106 read_lock(&inode_hash_lock); 107 ci = __ksmbd_inode_lookup(dentry); 108 if (ci) { 109 ret = KSMBD_INODE_STATUS_OK; 110 if (ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS)) 111 ret = KSMBD_INODE_STATUS_PENDING_DELETE; 112 atomic_dec(&ci->m_count); 113 } 114 read_unlock(&inode_hash_lock); 115 return ret; 116 } 117 118 bool ksmbd_inode_pending_delete(struct ksmbd_file *fp) 119 { 120 return (fp->f_ci->m_flags & (S_DEL_PENDING | S_DEL_ON_CLS)); 121 } 122 123 void ksmbd_set_inode_pending_delete(struct ksmbd_file *fp) 124 { 125 fp->f_ci->m_flags |= S_DEL_PENDING; 126 } 127 128 void ksmbd_clear_inode_pending_delete(struct ksmbd_file *fp) 129 { 130 fp->f_ci->m_flags &= ~S_DEL_PENDING; 131 } 132 133 void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp, 134 int file_info) 135 { 136 if (ksmbd_stream_fd(fp)) { 137 fp->f_ci->m_flags |= S_DEL_ON_CLS_STREAM; 138 return; 139 } 140 141 fp->f_ci->m_flags |= S_DEL_ON_CLS; 142 } 143 144 static void ksmbd_inode_hash(struct ksmbd_inode *ci) 145 { 146 struct hlist_head *b = inode_hashtable + 147 inode_hash(d_inode(ci->m_de)->i_sb, (unsigned long)ci->m_de); 148 149 hlist_add_head(&ci->m_hash, b); 150 } 151 152 static void ksmbd_inode_unhash(struct ksmbd_inode *ci) 153 { 154 write_lock(&inode_hash_lock); 155 hlist_del_init(&ci->m_hash); 156 write_unlock(&inode_hash_lock); 157 } 158 159 static int ksmbd_inode_init(struct ksmbd_inode *ci, struct ksmbd_file *fp) 160 { 161 atomic_set(&ci->m_count, 1); 162 atomic_set(&ci->op_count, 0); 163 atomic_set(&ci->sop_count, 0); 164 ci->m_flags = 0; 165 ci->m_fattr = 0; 166 INIT_LIST_HEAD(&ci->m_fp_list); 167 INIT_LIST_HEAD(&ci->m_op_list); 168 rwlock_init(&ci->m_lock); 169 ci->m_de = fp->filp->f_path.dentry; 170 return 0; 171 } 172 173 static struct ksmbd_inode *ksmbd_inode_get(struct ksmbd_file *fp) 174 { 175 struct ksmbd_inode *ci, *tmpci; 176 int rc; 177 178 read_lock(&inode_hash_lock); 179 ci = ksmbd_inode_lookup(fp); 180 read_unlock(&inode_hash_lock); 181 if (ci) 182 return ci; 183 184 ci = kmalloc(sizeof(struct ksmbd_inode), GFP_KERNEL); 185 if (!ci) 186 return NULL; 187 188 rc = ksmbd_inode_init(ci, fp); 189 if (rc) { 190 pr_err("inode initialized failed\n"); 191 kfree(ci); 192 return NULL; 193 } 194 195 write_lock(&inode_hash_lock); 196 tmpci = ksmbd_inode_lookup(fp); 197 if (!tmpci) { 198 ksmbd_inode_hash(ci); 199 } else { 200 kfree(ci); 201 ci = tmpci; 202 } 203 write_unlock(&inode_hash_lock); 204 return ci; 205 } 206 207 static void ksmbd_inode_free(struct ksmbd_inode *ci) 208 { 209 ksmbd_inode_unhash(ci); 210 kfree(ci); 211 } 212 213 void ksmbd_inode_put(struct ksmbd_inode *ci) 214 { 215 if (atomic_dec_and_test(&ci->m_count)) 216 ksmbd_inode_free(ci); 217 } 218 219 int __init ksmbd_inode_hash_init(void) 220 { 221 unsigned int loop; 222 unsigned long numentries = 16384; 223 unsigned long bucketsize = sizeof(struct hlist_head); 224 unsigned long size; 225 226 inode_hash_shift = ilog2(numentries); 227 inode_hash_mask = (1 << inode_hash_shift) - 1; 228 229 size = bucketsize << inode_hash_shift; 230 231 /* init master fp hash table */ 232 inode_hashtable = vmalloc(size); 233 if (!inode_hashtable) 234 return -ENOMEM; 235 236 for (loop = 0; loop < (1U << inode_hash_shift); loop++) 237 INIT_HLIST_HEAD(&inode_hashtable[loop]); 238 return 0; 239 } 240 241 void ksmbd_release_inode_hash(void) 242 { 243 vfree(inode_hashtable); 244 } 245 246 static void __ksmbd_inode_close(struct ksmbd_file *fp) 247 { 248 struct ksmbd_inode *ci = fp->f_ci; 249 int err; 250 struct file *filp; 251 252 filp = fp->filp; 253 if (ksmbd_stream_fd(fp) && (ci->m_flags & S_DEL_ON_CLS_STREAM)) { 254 ci->m_flags &= ~S_DEL_ON_CLS_STREAM; 255 err = ksmbd_vfs_remove_xattr(file_mnt_idmap(filp), 256 &filp->f_path, 257 fp->stream.name); 258 if (err) 259 pr_err("remove xattr failed : %s\n", 260 fp->stream.name); 261 } 262 263 if (atomic_dec_and_test(&ci->m_count)) { 264 write_lock(&ci->m_lock); 265 if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) { 266 ci->m_flags &= ~(S_DEL_ON_CLS | S_DEL_PENDING); 267 write_unlock(&ci->m_lock); 268 ksmbd_vfs_unlink(filp); 269 write_lock(&ci->m_lock); 270 } 271 write_unlock(&ci->m_lock); 272 273 ksmbd_inode_free(ci); 274 } 275 } 276 277 static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp) 278 { 279 if (!has_file_id(fp->persistent_id)) 280 return; 281 282 write_lock(&global_ft.lock); 283 idr_remove(global_ft.idr, fp->persistent_id); 284 write_unlock(&global_ft.lock); 285 } 286 287 static void __ksmbd_remove_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) 288 { 289 if (!has_file_id(fp->volatile_id)) 290 return; 291 292 write_lock(&fp->f_ci->m_lock); 293 list_del_init(&fp->node); 294 write_unlock(&fp->f_ci->m_lock); 295 296 write_lock(&ft->lock); 297 idr_remove(ft->idr, fp->volatile_id); 298 write_unlock(&ft->lock); 299 } 300 301 static void __ksmbd_close_fd(struct ksmbd_file_table *ft, struct ksmbd_file *fp) 302 { 303 struct file *filp; 304 struct ksmbd_lock *smb_lock, *tmp_lock; 305 306 fd_limit_close(); 307 __ksmbd_remove_durable_fd(fp); 308 __ksmbd_remove_fd(ft, fp); 309 310 close_id_del_oplock(fp); 311 filp = fp->filp; 312 313 __ksmbd_inode_close(fp); 314 if (!IS_ERR_OR_NULL(filp)) 315 fput(filp); 316 317 /* because the reference count of fp is 0, it is guaranteed that 318 * there are not accesses to fp->lock_list. 319 */ 320 list_for_each_entry_safe(smb_lock, tmp_lock, &fp->lock_list, flist) { 321 spin_lock(&fp->conn->llist_lock); 322 list_del(&smb_lock->clist); 323 spin_unlock(&fp->conn->llist_lock); 324 325 list_del(&smb_lock->flist); 326 locks_free_lock(smb_lock->fl); 327 kfree(smb_lock); 328 } 329 330 if (ksmbd_stream_fd(fp)) 331 kfree(fp->stream.name); 332 kmem_cache_free(filp_cache, fp); 333 } 334 335 static struct ksmbd_file *ksmbd_fp_get(struct ksmbd_file *fp) 336 { 337 if (fp->f_state != FP_INITED) 338 return NULL; 339 340 if (!atomic_inc_not_zero(&fp->refcount)) 341 return NULL; 342 return fp; 343 } 344 345 static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft, 346 u64 id) 347 { 348 struct ksmbd_file *fp; 349 350 if (!has_file_id(id)) 351 return NULL; 352 353 read_lock(&ft->lock); 354 fp = idr_find(ft->idr, id); 355 if (fp) 356 fp = ksmbd_fp_get(fp); 357 read_unlock(&ft->lock); 358 return fp; 359 } 360 361 static void __put_fd_final(struct ksmbd_work *work, struct ksmbd_file *fp) 362 { 363 __ksmbd_close_fd(&work->sess->file_table, fp); 364 atomic_dec(&work->conn->stats.open_files_count); 365 } 366 367 static void set_close_state_blocked_works(struct ksmbd_file *fp) 368 { 369 struct ksmbd_work *cancel_work; 370 371 spin_lock(&fp->f_lock); 372 list_for_each_entry(cancel_work, &fp->blocked_works, 373 fp_entry) { 374 cancel_work->state = KSMBD_WORK_CLOSED; 375 cancel_work->cancel_fn(cancel_work->cancel_argv); 376 } 377 spin_unlock(&fp->f_lock); 378 } 379 380 int ksmbd_close_fd(struct ksmbd_work *work, u64 id) 381 { 382 struct ksmbd_file *fp; 383 struct ksmbd_file_table *ft; 384 385 if (!has_file_id(id)) 386 return 0; 387 388 ft = &work->sess->file_table; 389 write_lock(&ft->lock); 390 fp = idr_find(ft->idr, id); 391 if (fp) { 392 set_close_state_blocked_works(fp); 393 394 if (fp->f_state != FP_INITED) 395 fp = NULL; 396 else { 397 fp->f_state = FP_CLOSED; 398 if (!atomic_dec_and_test(&fp->refcount)) 399 fp = NULL; 400 } 401 } 402 write_unlock(&ft->lock); 403 404 if (!fp) 405 return -EINVAL; 406 407 __put_fd_final(work, fp); 408 return 0; 409 } 410 411 void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp) 412 { 413 if (!fp) 414 return; 415 416 if (!atomic_dec_and_test(&fp->refcount)) 417 return; 418 __put_fd_final(work, fp); 419 } 420 421 static bool __sanity_check(struct ksmbd_tree_connect *tcon, struct ksmbd_file *fp) 422 { 423 if (!fp) 424 return false; 425 if (fp->tcon != tcon) 426 return false; 427 return true; 428 } 429 430 struct ksmbd_file *ksmbd_lookup_foreign_fd(struct ksmbd_work *work, u64 id) 431 { 432 return __ksmbd_lookup_fd(&work->sess->file_table, id); 433 } 434 435 struct ksmbd_file *ksmbd_lookup_fd_fast(struct ksmbd_work *work, u64 id) 436 { 437 struct ksmbd_file *fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 438 439 if (__sanity_check(work->tcon, fp)) 440 return fp; 441 442 ksmbd_fd_put(work, fp); 443 return NULL; 444 } 445 446 struct ksmbd_file *ksmbd_lookup_fd_slow(struct ksmbd_work *work, u64 id, 447 u64 pid) 448 { 449 struct ksmbd_file *fp; 450 451 if (!has_file_id(id)) { 452 id = work->compound_fid; 453 pid = work->compound_pfid; 454 } 455 456 fp = __ksmbd_lookup_fd(&work->sess->file_table, id); 457 if (!__sanity_check(work->tcon, fp)) { 458 ksmbd_fd_put(work, fp); 459 return NULL; 460 } 461 if (fp->persistent_id != pid) { 462 ksmbd_fd_put(work, fp); 463 return NULL; 464 } 465 return fp; 466 } 467 468 struct ksmbd_file *ksmbd_lookup_durable_fd(unsigned long long id) 469 { 470 return __ksmbd_lookup_fd(&global_ft, id); 471 } 472 473 struct ksmbd_file *ksmbd_lookup_fd_cguid(char *cguid) 474 { 475 struct ksmbd_file *fp = NULL; 476 unsigned int id; 477 478 read_lock(&global_ft.lock); 479 idr_for_each_entry(global_ft.idr, fp, id) { 480 if (!memcmp(fp->create_guid, 481 cguid, 482 SMB2_CREATE_GUID_SIZE)) { 483 fp = ksmbd_fp_get(fp); 484 break; 485 } 486 } 487 read_unlock(&global_ft.lock); 488 489 return fp; 490 } 491 492 struct ksmbd_file *ksmbd_lookup_fd_inode(struct dentry *dentry) 493 { 494 struct ksmbd_file *lfp; 495 struct ksmbd_inode *ci; 496 struct inode *inode = d_inode(dentry); 497 498 read_lock(&inode_hash_lock); 499 ci = __ksmbd_inode_lookup(dentry); 500 read_unlock(&inode_hash_lock); 501 if (!ci) 502 return NULL; 503 504 read_lock(&ci->m_lock); 505 list_for_each_entry(lfp, &ci->m_fp_list, node) { 506 if (inode == file_inode(lfp->filp)) { 507 atomic_dec(&ci->m_count); 508 lfp = ksmbd_fp_get(lfp); 509 read_unlock(&ci->m_lock); 510 return lfp; 511 } 512 } 513 atomic_dec(&ci->m_count); 514 read_unlock(&ci->m_lock); 515 return NULL; 516 } 517 518 #define OPEN_ID_TYPE_VOLATILE_ID (0) 519 #define OPEN_ID_TYPE_PERSISTENT_ID (1) 520 521 static void __open_id_set(struct ksmbd_file *fp, u64 id, int type) 522 { 523 if (type == OPEN_ID_TYPE_VOLATILE_ID) 524 fp->volatile_id = id; 525 if (type == OPEN_ID_TYPE_PERSISTENT_ID) 526 fp->persistent_id = id; 527 } 528 529 static int __open_id(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 530 int type) 531 { 532 u64 id = 0; 533 int ret; 534 535 if (type == OPEN_ID_TYPE_VOLATILE_ID && fd_limit_depleted()) { 536 __open_id_set(fp, KSMBD_NO_FID, type); 537 return -EMFILE; 538 } 539 540 idr_preload(GFP_KERNEL); 541 write_lock(&ft->lock); 542 ret = idr_alloc_cyclic(ft->idr, fp, 0, INT_MAX - 1, GFP_NOWAIT); 543 if (ret >= 0) { 544 id = ret; 545 ret = 0; 546 } else { 547 id = KSMBD_NO_FID; 548 fd_limit_close(); 549 } 550 551 __open_id_set(fp, id, type); 552 write_unlock(&ft->lock); 553 idr_preload_end(); 554 return ret; 555 } 556 557 unsigned int ksmbd_open_durable_fd(struct ksmbd_file *fp) 558 { 559 __open_id(&global_ft, fp, OPEN_ID_TYPE_PERSISTENT_ID); 560 return fp->persistent_id; 561 } 562 563 struct ksmbd_file *ksmbd_open_fd(struct ksmbd_work *work, struct file *filp) 564 { 565 struct ksmbd_file *fp; 566 int ret; 567 568 fp = kmem_cache_zalloc(filp_cache, GFP_KERNEL); 569 if (!fp) { 570 pr_err("Failed to allocate memory\n"); 571 return ERR_PTR(-ENOMEM); 572 } 573 574 INIT_LIST_HEAD(&fp->blocked_works); 575 INIT_LIST_HEAD(&fp->node); 576 INIT_LIST_HEAD(&fp->lock_list); 577 spin_lock_init(&fp->f_lock); 578 atomic_set(&fp->refcount, 1); 579 580 fp->filp = filp; 581 fp->conn = work->conn; 582 fp->tcon = work->tcon; 583 fp->volatile_id = KSMBD_NO_FID; 584 fp->persistent_id = KSMBD_NO_FID; 585 fp->f_state = FP_NEW; 586 fp->f_ci = ksmbd_inode_get(fp); 587 588 if (!fp->f_ci) { 589 ret = -ENOMEM; 590 goto err_out; 591 } 592 593 ret = __open_id(&work->sess->file_table, fp, OPEN_ID_TYPE_VOLATILE_ID); 594 if (ret) { 595 ksmbd_inode_put(fp->f_ci); 596 goto err_out; 597 } 598 599 atomic_inc(&work->conn->stats.open_files_count); 600 return fp; 601 602 err_out: 603 kmem_cache_free(filp_cache, fp); 604 return ERR_PTR(ret); 605 } 606 607 void ksmbd_update_fstate(struct ksmbd_file_table *ft, struct ksmbd_file *fp, 608 unsigned int state) 609 { 610 if (!fp) 611 return; 612 613 write_lock(&ft->lock); 614 fp->f_state = state; 615 write_unlock(&ft->lock); 616 } 617 618 static int 619 __close_file_table_ids(struct ksmbd_file_table *ft, 620 struct ksmbd_tree_connect *tcon, 621 bool (*skip)(struct ksmbd_tree_connect *tcon, 622 struct ksmbd_file *fp)) 623 { 624 unsigned int id; 625 struct ksmbd_file *fp; 626 int num = 0; 627 628 idr_for_each_entry(ft->idr, fp, id) { 629 if (skip(tcon, fp)) 630 continue; 631 632 set_close_state_blocked_works(fp); 633 634 if (!atomic_dec_and_test(&fp->refcount)) 635 continue; 636 __ksmbd_close_fd(ft, fp); 637 num++; 638 } 639 return num; 640 } 641 642 static bool tree_conn_fd_check(struct ksmbd_tree_connect *tcon, 643 struct ksmbd_file *fp) 644 { 645 return fp->tcon != tcon; 646 } 647 648 static bool session_fd_check(struct ksmbd_tree_connect *tcon, 649 struct ksmbd_file *fp) 650 { 651 return false; 652 } 653 654 void ksmbd_close_tree_conn_fds(struct ksmbd_work *work) 655 { 656 int num = __close_file_table_ids(&work->sess->file_table, 657 work->tcon, 658 tree_conn_fd_check); 659 660 atomic_sub(num, &work->conn->stats.open_files_count); 661 } 662 663 void ksmbd_close_session_fds(struct ksmbd_work *work) 664 { 665 int num = __close_file_table_ids(&work->sess->file_table, 666 work->tcon, 667 session_fd_check); 668 669 atomic_sub(num, &work->conn->stats.open_files_count); 670 } 671 672 int ksmbd_init_global_file_table(void) 673 { 674 return ksmbd_init_file_table(&global_ft); 675 } 676 677 void ksmbd_free_global_file_table(void) 678 { 679 struct ksmbd_file *fp = NULL; 680 unsigned int id; 681 682 idr_for_each_entry(global_ft.idr, fp, id) { 683 __ksmbd_remove_durable_fd(fp); 684 kmem_cache_free(filp_cache, fp); 685 } 686 687 ksmbd_destroy_file_table(&global_ft); 688 } 689 690 int ksmbd_init_file_table(struct ksmbd_file_table *ft) 691 { 692 ft->idr = kzalloc(sizeof(struct idr), GFP_KERNEL); 693 if (!ft->idr) 694 return -ENOMEM; 695 696 idr_init(ft->idr); 697 rwlock_init(&ft->lock); 698 return 0; 699 } 700 701 void ksmbd_destroy_file_table(struct ksmbd_file_table *ft) 702 { 703 if (!ft->idr) 704 return; 705 706 __close_file_table_ids(ft, NULL, session_fd_check); 707 idr_destroy(ft->idr); 708 kfree(ft->idr); 709 ft->idr = NULL; 710 } 711 712 int ksmbd_init_file_cache(void) 713 { 714 filp_cache = kmem_cache_create("ksmbd_file_cache", 715 sizeof(struct ksmbd_file), 0, 716 SLAB_HWCACHE_ALIGN, NULL); 717 if (!filp_cache) 718 goto out; 719 720 return 0; 721 722 out: 723 pr_err("failed to allocate file cache\n"); 724 return -ENOMEM; 725 } 726 727 void ksmbd_exit_file_cache(void) 728 { 729 kmem_cache_destroy(filp_cache); 730 } 731