1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions to handle the cached directory entries 4 * 5 * Copyright (c) 2022, Ronnie Sahlberg <lsahlber@redhat.com> 6 */ 7 8 #include <linux/namei.h> 9 #include "cifsglob.h" 10 #include "cifsproto.h" 11 #include "cifs_debug.h" 12 #include "smb2proto.h" 13 #include "cached_dir.h" 14 15 static struct cached_fid *init_cached_dir(const char *path); 16 static void free_cached_dir(struct cached_fid *cfid); 17 static void smb2_close_cached_fid(struct kref *ref); 18 static void cfids_laundromat_worker(struct work_struct *work); 19 20 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids, 21 const char *path, 22 bool lookup_only, 23 __u32 max_cached_dirs) 24 { 25 struct cached_fid *cfid; 26 27 spin_lock(&cfids->cfid_list_lock); 28 list_for_each_entry(cfid, &cfids->entries, entry) { 29 if (!strcmp(cfid->path, path)) { 30 /* 31 * If it doesn't have a lease it is either not yet 32 * fully cached or it may be in the process of 33 * being deleted due to a lease break. 34 */ 35 if (!cfid->time || !cfid->has_lease) { 36 spin_unlock(&cfids->cfid_list_lock); 37 return NULL; 38 } 39 kref_get(&cfid->refcount); 40 spin_unlock(&cfids->cfid_list_lock); 41 return cfid; 42 } 43 } 44 if (lookup_only) { 45 spin_unlock(&cfids->cfid_list_lock); 46 return NULL; 47 } 48 if (cfids->num_entries >= max_cached_dirs) { 49 spin_unlock(&cfids->cfid_list_lock); 50 return NULL; 51 } 52 cfid = init_cached_dir(path); 53 if (cfid == NULL) { 54 spin_unlock(&cfids->cfid_list_lock); 55 return NULL; 56 } 57 cfid->cfids = cfids; 58 cfids->num_entries++; 59 list_add(&cfid->entry, &cfids->entries); 60 cfid->on_list = true; 61 kref_get(&cfid->refcount); 62 spin_unlock(&cfids->cfid_list_lock); 63 return cfid; 64 } 65 66 static struct dentry * 67 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path) 68 { 69 struct dentry *dentry; 70 const char *s, *p; 71 char sep; 72 73 sep = CIFS_DIR_SEP(cifs_sb); 74 dentry = dget(cifs_sb->root); 75 s = path; 76 77 do { 78 struct inode *dir = d_inode(dentry); 79 struct dentry *child; 80 81 if (!S_ISDIR(dir->i_mode)) { 82 dput(dentry); 83 dentry = ERR_PTR(-ENOTDIR); 84 break; 85 } 86 87 /* skip separators */ 88 while (*s == sep) 89 s++; 90 if (!*s) 91 break; 92 p = s++; 93 /* next separator */ 94 while (*s && *s != sep) 95 s++; 96 97 child = lookup_positive_unlocked(p, dentry, s - p); 98 dput(dentry); 99 dentry = child; 100 } while (!IS_ERR(dentry)); 101 return dentry; 102 } 103 104 static const char *path_no_prefix(struct cifs_sb_info *cifs_sb, 105 const char *path) 106 { 107 size_t len = 0; 108 109 if (!*path) 110 return path; 111 112 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && 113 cifs_sb->prepath) { 114 len = strlen(cifs_sb->prepath) + 1; 115 if (unlikely(len > strlen(path))) 116 return ERR_PTR(-EINVAL); 117 } 118 return path + len; 119 } 120 121 /* 122 * Open the and cache a directory handle. 123 * If error then *cfid is not initialized. 124 */ 125 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, 126 const char *path, 127 struct cifs_sb_info *cifs_sb, 128 bool lookup_only, struct cached_fid **ret_cfid) 129 { 130 struct cifs_ses *ses; 131 struct TCP_Server_Info *server; 132 struct cifs_open_parms oparms; 133 struct smb2_create_rsp *o_rsp = NULL; 134 struct smb2_query_info_rsp *qi_rsp = NULL; 135 int resp_buftype[2]; 136 struct smb_rqst rqst[2]; 137 struct kvec rsp_iov[2]; 138 struct kvec open_iov[SMB2_CREATE_IOV_SIZE]; 139 struct kvec qi_iov[1]; 140 int rc, flags = 0; 141 __le16 *utf16_path = NULL; 142 u8 oplock = SMB2_OPLOCK_LEVEL_II; 143 struct cifs_fid *pfid; 144 struct dentry *dentry = NULL; 145 struct cached_fid *cfid; 146 struct cached_fids *cfids; 147 const char *npath; 148 149 if (tcon == NULL || tcon->cfids == NULL || tcon->nohandlecache || 150 is_smb1_server(tcon->ses->server) || (dir_cache_timeout == 0)) 151 return -EOPNOTSUPP; 152 153 ses = tcon->ses; 154 server = cifs_pick_channel(ses); 155 cfids = tcon->cfids; 156 157 if (!server->ops->new_lease_key) 158 return -EIO; 159 160 if (cifs_sb->root == NULL) 161 return -ENOENT; 162 163 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb); 164 if (!utf16_path) 165 return -ENOMEM; 166 167 cfid = find_or_create_cached_dir(cfids, path, lookup_only, tcon->max_cached_dirs); 168 if (cfid == NULL) { 169 kfree(utf16_path); 170 return -ENOENT; 171 } 172 /* 173 * Return cached fid if it has a lease. Otherwise, it is either a new 174 * entry or laundromat worker removed it from @cfids->entries. Caller 175 * will put last reference if the latter. 176 */ 177 spin_lock(&cfids->cfid_list_lock); 178 if (cfid->has_lease) { 179 spin_unlock(&cfids->cfid_list_lock); 180 *ret_cfid = cfid; 181 kfree(utf16_path); 182 return 0; 183 } 184 spin_unlock(&cfids->cfid_list_lock); 185 186 /* 187 * Skip any prefix paths in @path as lookup_positive_unlocked() ends up 188 * calling ->lookup() which already adds those through 189 * build_path_from_dentry(). Also, do it earlier as we might reconnect 190 * below when trying to send compounded request and then potentially 191 * having a different prefix path (e.g. after DFS failover). 192 */ 193 npath = path_no_prefix(cifs_sb, path); 194 if (IS_ERR(npath)) { 195 rc = PTR_ERR(npath); 196 goto out; 197 } 198 199 if (!npath[0]) { 200 dentry = dget(cifs_sb->root); 201 } else { 202 dentry = path_to_dentry(cifs_sb, npath); 203 if (IS_ERR(dentry)) { 204 rc = -ENOENT; 205 goto out; 206 } 207 } 208 cfid->dentry = dentry; 209 210 /* 211 * We do not hold the lock for the open because in case 212 * SMB2_open needs to reconnect. 213 * This is safe because no other thread will be able to get a ref 214 * to the cfid until we have finished opening the file and (possibly) 215 * acquired a lease. 216 */ 217 if (smb3_encryption_required(tcon)) 218 flags |= CIFS_TRANSFORM_REQ; 219 220 pfid = &cfid->fid; 221 server->ops->new_lease_key(pfid); 222 223 memset(rqst, 0, sizeof(rqst)); 224 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER; 225 memset(rsp_iov, 0, sizeof(rsp_iov)); 226 227 /* Open */ 228 memset(&open_iov, 0, sizeof(open_iov)); 229 rqst[0].rq_iov = open_iov; 230 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE; 231 232 oparms = (struct cifs_open_parms) { 233 .tcon = tcon, 234 .path = path, 235 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE), 236 .desired_access = FILE_READ_DATA | FILE_READ_ATTRIBUTES | 237 FILE_READ_EA, 238 .disposition = FILE_OPEN, 239 .fid = pfid, 240 }; 241 242 rc = SMB2_open_init(tcon, server, 243 &rqst[0], &oplock, &oparms, utf16_path); 244 if (rc) 245 goto oshr_free; 246 smb2_set_next_command(tcon, &rqst[0]); 247 248 memset(&qi_iov, 0, sizeof(qi_iov)); 249 rqst[1].rq_iov = qi_iov; 250 rqst[1].rq_nvec = 1; 251 252 rc = SMB2_query_info_init(tcon, server, 253 &rqst[1], COMPOUND_FID, 254 COMPOUND_FID, FILE_ALL_INFORMATION, 255 SMB2_O_INFO_FILE, 0, 256 sizeof(struct smb2_file_all_info) + 257 PATH_MAX * 2, 0, NULL); 258 if (rc) 259 goto oshr_free; 260 261 smb2_set_related(&rqst[1]); 262 263 /* 264 * Set @cfid->has_lease to true before sending out compounded request so 265 * its lease reference can be put in cached_dir_lease_break() due to a 266 * potential lease break right after the request is sent or while @cfid 267 * is still being cached. Concurrent processes won't be to use it yet 268 * due to @cfid->time being zero. 269 */ 270 cfid->has_lease = true; 271 272 rc = compound_send_recv(xid, ses, server, 273 flags, 2, rqst, 274 resp_buftype, rsp_iov); 275 if (rc) { 276 if (rc == -EREMCHG) { 277 tcon->need_reconnect = true; 278 pr_warn_once("server share %s deleted\n", 279 tcon->tree_name); 280 } 281 goto oshr_free; 282 } 283 cfid->tcon = tcon; 284 cfid->is_open = true; 285 286 spin_lock(&cfids->cfid_list_lock); 287 288 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base; 289 oparms.fid->persistent_fid = o_rsp->PersistentFileId; 290 oparms.fid->volatile_fid = o_rsp->VolatileFileId; 291 #ifdef CONFIG_CIFS_DEBUG2 292 oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId); 293 #endif /* CIFS_DEBUG2 */ 294 295 296 if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) { 297 spin_unlock(&cfids->cfid_list_lock); 298 rc = -EINVAL; 299 goto oshr_free; 300 } 301 302 rc = smb2_parse_contexts(server, rsp_iov, 303 &oparms.fid->epoch, 304 oparms.fid->lease_key, 305 &oplock, NULL, NULL); 306 if (rc) { 307 spin_unlock(&cfids->cfid_list_lock); 308 goto oshr_free; 309 } 310 311 rc = -EINVAL; 312 if (!(oplock & SMB2_LEASE_READ_CACHING_HE)) { 313 spin_unlock(&cfids->cfid_list_lock); 314 goto oshr_free; 315 } 316 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base; 317 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info)) { 318 spin_unlock(&cfids->cfid_list_lock); 319 goto oshr_free; 320 } 321 if (!smb2_validate_and_copy_iov( 322 le16_to_cpu(qi_rsp->OutputBufferOffset), 323 sizeof(struct smb2_file_all_info), 324 &rsp_iov[1], sizeof(struct smb2_file_all_info), 325 (char *)&cfid->file_all_info)) 326 cfid->file_all_info_is_valid = true; 327 328 cfid->time = jiffies; 329 spin_unlock(&cfids->cfid_list_lock); 330 /* At this point the directory handle is fully cached */ 331 rc = 0; 332 333 oshr_free: 334 SMB2_open_free(&rqst[0]); 335 SMB2_query_info_free(&rqst[1]); 336 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base); 337 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base); 338 if (rc) { 339 spin_lock(&cfids->cfid_list_lock); 340 if (cfid->on_list) { 341 list_del(&cfid->entry); 342 cfid->on_list = false; 343 cfids->num_entries--; 344 } 345 if (cfid->has_lease) { 346 /* 347 * We are guaranteed to have two references at this 348 * point. One for the caller and one for a potential 349 * lease. Release the Lease-ref so that the directory 350 * will be closed when the caller closes the cached 351 * handle. 352 */ 353 cfid->has_lease = false; 354 spin_unlock(&cfids->cfid_list_lock); 355 kref_put(&cfid->refcount, smb2_close_cached_fid); 356 goto out; 357 } 358 spin_unlock(&cfids->cfid_list_lock); 359 } 360 out: 361 if (rc) { 362 if (cfid->is_open) 363 SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid, 364 cfid->fid.volatile_fid); 365 free_cached_dir(cfid); 366 } else { 367 *ret_cfid = cfid; 368 atomic_inc(&tcon->num_remote_opens); 369 } 370 kfree(utf16_path); 371 372 return rc; 373 } 374 375 int open_cached_dir_by_dentry(struct cifs_tcon *tcon, 376 struct dentry *dentry, 377 struct cached_fid **ret_cfid) 378 { 379 struct cached_fid *cfid; 380 struct cached_fids *cfids = tcon->cfids; 381 382 if (cfids == NULL) 383 return -ENOENT; 384 385 spin_lock(&cfids->cfid_list_lock); 386 list_for_each_entry(cfid, &cfids->entries, entry) { 387 if (dentry && cfid->dentry == dentry) { 388 cifs_dbg(FYI, "found a cached root file handle by dentry\n"); 389 kref_get(&cfid->refcount); 390 *ret_cfid = cfid; 391 spin_unlock(&cfids->cfid_list_lock); 392 return 0; 393 } 394 } 395 spin_unlock(&cfids->cfid_list_lock); 396 return -ENOENT; 397 } 398 399 static void 400 smb2_close_cached_fid(struct kref *ref) 401 { 402 struct cached_fid *cfid = container_of(ref, struct cached_fid, 403 refcount); 404 405 spin_lock(&cfid->cfids->cfid_list_lock); 406 if (cfid->on_list) { 407 list_del(&cfid->entry); 408 cfid->on_list = false; 409 cfid->cfids->num_entries--; 410 } 411 spin_unlock(&cfid->cfids->cfid_list_lock); 412 413 dput(cfid->dentry); 414 cfid->dentry = NULL; 415 416 if (cfid->is_open) { 417 SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid, 418 cfid->fid.volatile_fid); 419 atomic_dec(&cfid->tcon->num_remote_opens); 420 } 421 422 free_cached_dir(cfid); 423 } 424 425 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon, 426 const char *name, struct cifs_sb_info *cifs_sb) 427 { 428 struct cached_fid *cfid = NULL; 429 int rc; 430 431 rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid); 432 if (rc) { 433 cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name); 434 return; 435 } 436 spin_lock(&cfid->cfids->cfid_list_lock); 437 if (cfid->has_lease) { 438 cfid->has_lease = false; 439 kref_put(&cfid->refcount, smb2_close_cached_fid); 440 } 441 spin_unlock(&cfid->cfids->cfid_list_lock); 442 close_cached_dir(cfid); 443 } 444 445 446 void close_cached_dir(struct cached_fid *cfid) 447 { 448 kref_put(&cfid->refcount, smb2_close_cached_fid); 449 } 450 451 /* 452 * Called from cifs_kill_sb when we unmount a share 453 */ 454 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb) 455 { 456 struct rb_root *root = &cifs_sb->tlink_tree; 457 struct rb_node *node; 458 struct cached_fid *cfid; 459 struct cifs_tcon *tcon; 460 struct tcon_link *tlink; 461 struct cached_fids *cfids; 462 463 for (node = rb_first(root); node; node = rb_next(node)) { 464 tlink = rb_entry(node, struct tcon_link, tl_rbnode); 465 tcon = tlink_tcon(tlink); 466 if (IS_ERR(tcon)) 467 continue; 468 cfids = tcon->cfids; 469 if (cfids == NULL) 470 continue; 471 list_for_each_entry(cfid, &cfids->entries, entry) { 472 dput(cfid->dentry); 473 cfid->dentry = NULL; 474 } 475 } 476 } 477 478 /* 479 * Invalidate all cached dirs when a TCON has been reset 480 * due to a session loss. 481 */ 482 void invalidate_all_cached_dirs(struct cifs_tcon *tcon) 483 { 484 struct cached_fids *cfids = tcon->cfids; 485 struct cached_fid *cfid, *q; 486 LIST_HEAD(entry); 487 488 if (cfids == NULL) 489 return; 490 491 spin_lock(&cfids->cfid_list_lock); 492 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { 493 list_move(&cfid->entry, &entry); 494 cfids->num_entries--; 495 cfid->is_open = false; 496 cfid->on_list = false; 497 /* To prevent race with smb2_cached_lease_break() */ 498 kref_get(&cfid->refcount); 499 } 500 spin_unlock(&cfids->cfid_list_lock); 501 502 list_for_each_entry_safe(cfid, q, &entry, entry) { 503 list_del(&cfid->entry); 504 cancel_work_sync(&cfid->lease_break); 505 if (cfid->has_lease) { 506 /* 507 * We lease was never cancelled from the server so we 508 * need to drop the reference. 509 */ 510 spin_lock(&cfids->cfid_list_lock); 511 cfid->has_lease = false; 512 spin_unlock(&cfids->cfid_list_lock); 513 kref_put(&cfid->refcount, smb2_close_cached_fid); 514 } 515 /* Drop the extra reference opened above*/ 516 kref_put(&cfid->refcount, smb2_close_cached_fid); 517 } 518 } 519 520 static void 521 smb2_cached_lease_break(struct work_struct *work) 522 { 523 struct cached_fid *cfid = container_of(work, 524 struct cached_fid, lease_break); 525 526 spin_lock(&cfid->cfids->cfid_list_lock); 527 cfid->has_lease = false; 528 spin_unlock(&cfid->cfids->cfid_list_lock); 529 kref_put(&cfid->refcount, smb2_close_cached_fid); 530 } 531 532 int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]) 533 { 534 struct cached_fids *cfids = tcon->cfids; 535 struct cached_fid *cfid; 536 537 if (cfids == NULL) 538 return false; 539 540 spin_lock(&cfids->cfid_list_lock); 541 list_for_each_entry(cfid, &cfids->entries, entry) { 542 if (cfid->has_lease && 543 !memcmp(lease_key, 544 cfid->fid.lease_key, 545 SMB2_LEASE_KEY_SIZE)) { 546 cfid->time = 0; 547 /* 548 * We found a lease remove it from the list 549 * so no threads can access it. 550 */ 551 list_del(&cfid->entry); 552 cfid->on_list = false; 553 cfids->num_entries--; 554 555 queue_work(cifsiod_wq, 556 &cfid->lease_break); 557 spin_unlock(&cfids->cfid_list_lock); 558 return true; 559 } 560 } 561 spin_unlock(&cfids->cfid_list_lock); 562 return false; 563 } 564 565 static struct cached_fid *init_cached_dir(const char *path) 566 { 567 struct cached_fid *cfid; 568 569 cfid = kzalloc(sizeof(*cfid), GFP_ATOMIC); 570 if (!cfid) 571 return NULL; 572 cfid->path = kstrdup(path, GFP_ATOMIC); 573 if (!cfid->path) { 574 kfree(cfid); 575 return NULL; 576 } 577 578 INIT_WORK(&cfid->lease_break, smb2_cached_lease_break); 579 INIT_LIST_HEAD(&cfid->entry); 580 INIT_LIST_HEAD(&cfid->dirents.entries); 581 mutex_init(&cfid->dirents.de_mutex); 582 spin_lock_init(&cfid->fid_lock); 583 kref_init(&cfid->refcount); 584 return cfid; 585 } 586 587 static void free_cached_dir(struct cached_fid *cfid) 588 { 589 struct cached_dirent *dirent, *q; 590 591 dput(cfid->dentry); 592 cfid->dentry = NULL; 593 594 /* 595 * Delete all cached dirent names 596 */ 597 list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) { 598 list_del(&dirent->entry); 599 kfree(dirent->name); 600 kfree(dirent); 601 } 602 603 kfree(cfid->path); 604 cfid->path = NULL; 605 kfree(cfid); 606 } 607 608 static void cfids_laundromat_worker(struct work_struct *work) 609 { 610 struct cached_fids *cfids; 611 struct cached_fid *cfid, *q; 612 LIST_HEAD(entry); 613 614 cfids = container_of(work, struct cached_fids, laundromat_work.work); 615 616 spin_lock(&cfids->cfid_list_lock); 617 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { 618 if (cfid->time && 619 time_after(jiffies, cfid->time + HZ * dir_cache_timeout)) { 620 cfid->on_list = false; 621 list_move(&cfid->entry, &entry); 622 cfids->num_entries--; 623 /* To prevent race with smb2_cached_lease_break() */ 624 kref_get(&cfid->refcount); 625 } 626 } 627 spin_unlock(&cfids->cfid_list_lock); 628 629 list_for_each_entry_safe(cfid, q, &entry, entry) { 630 list_del(&cfid->entry); 631 /* 632 * Cancel and wait for the work to finish in case we are racing 633 * with it. 634 */ 635 cancel_work_sync(&cfid->lease_break); 636 if (cfid->has_lease) { 637 /* 638 * Our lease has not yet been cancelled from the server 639 * so we need to drop the reference. 640 */ 641 spin_lock(&cfids->cfid_list_lock); 642 cfid->has_lease = false; 643 spin_unlock(&cfids->cfid_list_lock); 644 kref_put(&cfid->refcount, smb2_close_cached_fid); 645 } 646 /* Drop the extra reference opened above */ 647 kref_put(&cfid->refcount, smb2_close_cached_fid); 648 } 649 queue_delayed_work(cifsiod_wq, &cfids->laundromat_work, 650 dir_cache_timeout * HZ); 651 } 652 653 struct cached_fids *init_cached_dirs(void) 654 { 655 struct cached_fids *cfids; 656 657 cfids = kzalloc(sizeof(*cfids), GFP_KERNEL); 658 if (!cfids) 659 return NULL; 660 spin_lock_init(&cfids->cfid_list_lock); 661 INIT_LIST_HEAD(&cfids->entries); 662 663 INIT_DELAYED_WORK(&cfids->laundromat_work, cfids_laundromat_worker); 664 queue_delayed_work(cifsiod_wq, &cfids->laundromat_work, 665 dir_cache_timeout * HZ); 666 667 return cfids; 668 } 669 670 /* 671 * Called from tconInfoFree when we are tearing down the tcon. 672 * There are no active users or open files/directories at this point. 673 */ 674 void free_cached_dirs(struct cached_fids *cfids) 675 { 676 struct cached_fid *cfid, *q; 677 LIST_HEAD(entry); 678 679 if (cfids == NULL) 680 return; 681 682 cancel_delayed_work_sync(&cfids->laundromat_work); 683 684 spin_lock(&cfids->cfid_list_lock); 685 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { 686 cfid->on_list = false; 687 cfid->is_open = false; 688 list_move(&cfid->entry, &entry); 689 } 690 spin_unlock(&cfids->cfid_list_lock); 691 692 list_for_each_entry_safe(cfid, q, &entry, entry) { 693 list_del(&cfid->entry); 694 free_cached_dir(cfid); 695 } 696 697 kfree(cfids); 698 } 699