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 static void close_cached_dir_locked(struct cached_fid *cfid); 20 21 struct cached_dir_dentry { 22 struct list_head entry; 23 struct dentry *dentry; 24 }; 25 26 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids, 27 const char *path, 28 bool lookup_only, 29 __u32 max_cached_dirs) 30 { 31 struct cached_fid *cfid; 32 33 list_for_each_entry(cfid, &cfids->entries, entry) { 34 if (!strcmp(cfid->path, path)) { 35 /* 36 * If it doesn't have a lease it is either not yet 37 * fully cached or it may be in the process of 38 * being deleted due to a lease break. 39 */ 40 if (!is_valid_cached_dir(cfid)) 41 return NULL; 42 kref_get(&cfid->refcount); 43 return cfid; 44 } 45 } 46 if (lookup_only) { 47 return NULL; 48 } 49 if (cfids->num_entries >= max_cached_dirs) { 50 return NULL; 51 } 52 cfid = init_cached_dir(path); 53 if (cfid == NULL) { 54 return NULL; 55 } 56 cfid->cfids = cfids; 57 cfids->num_entries++; 58 list_add(&cfid->entry, &cfids->entries); 59 cfid->on_list = true; 60 kref_get(&cfid->refcount); 61 /* 62 * Set @cfid->has_lease to true during construction so that the lease 63 * reference can be put in cached_dir_lease_break() due to a potential 64 * lease break right after the request is sent or while @cfid is still 65 * being cached, or if a reconnection is triggered during construction. 66 * Concurrent processes won't be to use it yet due to @cfid->time being 67 * zero. 68 */ 69 cfid->has_lease = true; 70 71 return cfid; 72 } 73 74 static struct dentry * 75 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path) 76 { 77 struct dentry *dentry; 78 const char *s, *p; 79 char sep; 80 81 sep = CIFS_DIR_SEP(cifs_sb); 82 dentry = dget(cifs_sb->root); 83 s = path; 84 85 do { 86 struct inode *dir = d_inode(dentry); 87 struct dentry *child; 88 89 if (!S_ISDIR(dir->i_mode)) { 90 dput(dentry); 91 dentry = ERR_PTR(-ENOTDIR); 92 break; 93 } 94 95 /* skip separators */ 96 while (*s == sep) 97 s++; 98 if (!*s) 99 break; 100 p = s++; 101 /* next separator */ 102 while (*s && *s != sep) 103 s++; 104 105 child = lookup_noperm_positive_unlocked(&QSTR_LEN(p, s - p), 106 dentry); 107 dput(dentry); 108 dentry = child; 109 } while (!IS_ERR(dentry)); 110 return dentry; 111 } 112 113 static const char *path_no_prefix(struct cifs_sb_info *cifs_sb, 114 const char *path) 115 { 116 size_t len = 0; 117 118 if (!*path) 119 return path; 120 121 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && 122 cifs_sb->prepath) { 123 len = strlen(cifs_sb->prepath) + 1; 124 if (unlikely(len > strlen(path))) 125 return ERR_PTR(-EINVAL); 126 } 127 return path + len; 128 } 129 130 /* 131 * Open the and cache a directory handle. 132 * If error then *cfid is not initialized. 133 */ 134 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, 135 const char *path, 136 struct cifs_sb_info *cifs_sb, 137 bool lookup_only, struct cached_fid **ret_cfid) 138 { 139 struct cifs_ses *ses; 140 struct TCP_Server_Info *server; 141 struct cifs_open_parms oparms; 142 struct smb2_create_rsp *o_rsp = NULL; 143 struct smb2_query_info_rsp *qi_rsp = NULL; 144 int resp_buftype[2]; 145 struct smb_rqst rqst[2]; 146 struct kvec rsp_iov[2]; 147 struct kvec open_iov[SMB2_CREATE_IOV_SIZE]; 148 struct kvec qi_iov[1]; 149 int rc, flags = 0; 150 __le16 *utf16_path = NULL; 151 u8 oplock = SMB2_OPLOCK_LEVEL_II; 152 struct cifs_fid *pfid; 153 struct dentry *dentry = NULL; 154 struct cached_fid *cfid; 155 struct cached_fids *cfids; 156 const char *npath; 157 int retries = 0, cur_sleep = 1; 158 __le32 lease_flags = 0; 159 160 if (cifs_sb->root == NULL) 161 return -ENOENT; 162 163 if (tcon == NULL) 164 return -EOPNOTSUPP; 165 166 ses = tcon->ses; 167 cfids = tcon->cfids; 168 169 if (cfids == NULL) 170 return -EOPNOTSUPP; 171 172 replay_again: 173 /* reinitialize for possible replay */ 174 flags = 0; 175 oplock = SMB2_OPLOCK_LEVEL_II; 176 server = cifs_pick_channel(ses); 177 178 if (!server->ops->new_lease_key) 179 return -EIO; 180 181 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb); 182 if (!utf16_path) 183 return -ENOMEM; 184 185 spin_lock(&cfids->cfid_list_lock); 186 cfid = find_or_create_cached_dir(cfids, path, lookup_only, tcon->max_cached_dirs); 187 if (cfid == NULL) { 188 spin_unlock(&cfids->cfid_list_lock); 189 kfree(utf16_path); 190 return -ENOENT; 191 } 192 /* 193 * Return cached fid if it is valid (has a lease and has a time). 194 * Otherwise, it is either a new entry or laundromat worker removed it 195 * from @cfids->entries. Caller will put last reference if the latter. 196 */ 197 if (is_valid_cached_dir(cfid)) { 198 cfid->last_access_time = jiffies; 199 spin_unlock(&cfids->cfid_list_lock); 200 *ret_cfid = cfid; 201 kfree(utf16_path); 202 return 0; 203 } 204 spin_unlock(&cfids->cfid_list_lock); 205 206 pfid = &cfid->fid; 207 208 /* 209 * Skip any prefix paths in @path as lookup_noperm_positive_unlocked() ends up 210 * calling ->lookup() which already adds those through 211 * build_path_from_dentry(). Also, do it earlier as we might reconnect 212 * below when trying to send compounded request and then potentially 213 * having a different prefix path (e.g. after DFS failover). 214 */ 215 npath = path_no_prefix(cifs_sb, path); 216 if (IS_ERR(npath)) { 217 rc = PTR_ERR(npath); 218 goto out; 219 } 220 221 if (!npath[0]) { 222 dentry = dget(cifs_sb->root); 223 } else { 224 dentry = path_to_dentry(cifs_sb, npath); 225 if (IS_ERR(dentry)) { 226 rc = -ENOENT; 227 goto out; 228 } 229 if (dentry->d_parent && server->dialect >= SMB30_PROT_ID) { 230 struct cached_fid *parent_cfid; 231 232 spin_lock(&cfids->cfid_list_lock); 233 list_for_each_entry(parent_cfid, &cfids->entries, entry) { 234 if (parent_cfid->dentry == dentry->d_parent) { 235 cifs_dbg(FYI, "found a parent cached file handle\n"); 236 if (is_valid_cached_dir(parent_cfid)) { 237 lease_flags 238 |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; 239 memcpy(pfid->parent_lease_key, 240 parent_cfid->fid.lease_key, 241 SMB2_LEASE_KEY_SIZE); 242 } 243 break; 244 } 245 } 246 spin_unlock(&cfids->cfid_list_lock); 247 } 248 } 249 cfid->dentry = dentry; 250 cfid->tcon = tcon; 251 252 /* 253 * We do not hold the lock for the open because in case 254 * SMB2_open needs to reconnect. 255 * This is safe because no other thread will be able to get a ref 256 * to the cfid until we have finished opening the file and (possibly) 257 * acquired a lease. 258 */ 259 if (smb3_encryption_required(tcon)) 260 flags |= CIFS_TRANSFORM_REQ; 261 262 server->ops->new_lease_key(pfid); 263 264 memset(rqst, 0, sizeof(rqst)); 265 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER; 266 memset(rsp_iov, 0, sizeof(rsp_iov)); 267 268 /* Open */ 269 memset(&open_iov, 0, sizeof(open_iov)); 270 rqst[0].rq_iov = open_iov; 271 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE; 272 273 oparms = (struct cifs_open_parms) { 274 .tcon = tcon, 275 .path = path, 276 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE), 277 .desired_access = FILE_READ_DATA | FILE_READ_ATTRIBUTES | 278 FILE_READ_EA, 279 .disposition = FILE_OPEN, 280 .fid = pfid, 281 .lease_flags = lease_flags, 282 .replay = !!(retries), 283 }; 284 285 rc = SMB2_open_init(tcon, server, 286 &rqst[0], &oplock, &oparms, utf16_path); 287 if (rc) 288 goto oshr_free; 289 smb2_set_next_command(tcon, &rqst[0]); 290 291 memset(&qi_iov, 0, sizeof(qi_iov)); 292 rqst[1].rq_iov = qi_iov; 293 rqst[1].rq_nvec = 1; 294 295 rc = SMB2_query_info_init(tcon, server, 296 &rqst[1], COMPOUND_FID, 297 COMPOUND_FID, FILE_ALL_INFORMATION, 298 SMB2_O_INFO_FILE, 0, 299 sizeof(struct smb2_file_all_info) + 300 PATH_MAX * 2, 0, NULL); 301 if (rc) 302 goto oshr_free; 303 304 smb2_set_related(&rqst[1]); 305 306 if (retries) { 307 smb2_set_replay(server, &rqst[0]); 308 smb2_set_replay(server, &rqst[1]); 309 } 310 311 rc = compound_send_recv(xid, ses, server, 312 flags, 2, rqst, 313 resp_buftype, rsp_iov); 314 if (rc) { 315 if (rc == -EREMCHG) { 316 tcon->need_reconnect = true; 317 pr_warn_once("server share %s deleted\n", 318 tcon->tree_name); 319 } 320 goto oshr_free; 321 } 322 cfid->is_open = true; 323 324 spin_lock(&cfids->cfid_list_lock); 325 326 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base; 327 oparms.fid->persistent_fid = o_rsp->PersistentFileId; 328 oparms.fid->volatile_fid = o_rsp->VolatileFileId; 329 #ifdef CONFIG_CIFS_DEBUG2 330 oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId); 331 #endif /* CIFS_DEBUG2 */ 332 333 334 if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) { 335 spin_unlock(&cfids->cfid_list_lock); 336 rc = -EINVAL; 337 goto oshr_free; 338 } 339 340 rc = smb2_parse_contexts(server, rsp_iov, 341 &oparms.fid->epoch, 342 oparms.fid->lease_key, 343 &oplock, NULL, NULL); 344 if (rc) { 345 spin_unlock(&cfids->cfid_list_lock); 346 goto oshr_free; 347 } 348 349 rc = -EINVAL; 350 if (!(oplock & SMB2_LEASE_READ_CACHING_HE)) { 351 spin_unlock(&cfids->cfid_list_lock); 352 goto oshr_free; 353 } 354 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base; 355 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info)) { 356 spin_unlock(&cfids->cfid_list_lock); 357 goto oshr_free; 358 } 359 if (!smb2_validate_and_copy_iov( 360 le16_to_cpu(qi_rsp->OutputBufferOffset), 361 sizeof(struct smb2_file_all_info), 362 &rsp_iov[1], sizeof(struct smb2_file_all_info), 363 (char *)&cfid->file_all_info)) 364 cfid->file_all_info_is_valid = true; 365 366 cfid->time = jiffies; 367 cfid->last_access_time = jiffies; 368 spin_unlock(&cfids->cfid_list_lock); 369 /* At this point the directory handle is fully cached */ 370 rc = 0; 371 372 oshr_free: 373 SMB2_open_free(&rqst[0]); 374 SMB2_query_info_free(&rqst[1]); 375 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base); 376 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base); 377 out: 378 if (rc) { 379 spin_lock(&cfids->cfid_list_lock); 380 if (cfid->on_list) { 381 list_del(&cfid->entry); 382 cfid->on_list = false; 383 cfids->num_entries--; 384 } 385 if (cfid->has_lease) { 386 /* 387 * We are guaranteed to have two references at this 388 * point. One for the caller and one for a potential 389 * lease. Release one here, and the second below. 390 */ 391 cfid->has_lease = false; 392 close_cached_dir_locked(cfid); 393 } 394 spin_unlock(&cfids->cfid_list_lock); 395 396 close_cached_dir(cfid); 397 } else { 398 *ret_cfid = cfid; 399 atomic_inc(&tcon->num_remote_opens); 400 } 401 kfree(utf16_path); 402 403 if (is_replayable_error(rc) && 404 smb2_should_replay(tcon, &retries, &cur_sleep)) 405 goto replay_again; 406 407 return rc; 408 } 409 410 int open_cached_dir_by_dentry(struct cifs_tcon *tcon, 411 struct dentry *dentry, 412 struct cached_fid **ret_cfid) 413 { 414 struct cached_fid *cfid; 415 struct cached_fids *cfids = tcon->cfids; 416 417 if (cfids == NULL) 418 return -EOPNOTSUPP; 419 420 if (!dentry) 421 return -ENOENT; 422 423 spin_lock(&cfids->cfid_list_lock); 424 list_for_each_entry(cfid, &cfids->entries, entry) { 425 if (cfid->dentry == dentry) { 426 if (!is_valid_cached_dir(cfid)) 427 break; 428 cifs_dbg(FYI, "found a cached file handle by dentry\n"); 429 kref_get(&cfid->refcount); 430 *ret_cfid = cfid; 431 cfid->last_access_time = jiffies; 432 spin_unlock(&cfids->cfid_list_lock); 433 return 0; 434 } 435 } 436 spin_unlock(&cfids->cfid_list_lock); 437 return -ENOENT; 438 } 439 440 static void 441 smb2_close_cached_fid(struct kref *ref) 442 __releases(&cfid->cfids->cfid_list_lock) 443 { 444 struct cached_fid *cfid = container_of(ref, struct cached_fid, 445 refcount); 446 int rc; 447 448 lockdep_assert_held(&cfid->cfids->cfid_list_lock); 449 450 if (cfid->on_list) { 451 list_del(&cfid->entry); 452 cfid->on_list = false; 453 cfid->cfids->num_entries--; 454 } 455 spin_unlock(&cfid->cfids->cfid_list_lock); 456 457 dput(cfid->dentry); 458 cfid->dentry = NULL; 459 460 if (cfid->is_open) { 461 rc = SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid, 462 cfid->fid.volatile_fid); 463 if (rc) /* should we retry on -EBUSY or -EAGAIN? */ 464 cifs_dbg(VFS, "close cached dir rc %d\n", rc); 465 } 466 467 free_cached_dir(cfid); 468 } 469 470 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon, 471 const char *name, struct cifs_sb_info *cifs_sb) 472 { 473 struct cached_fid *cfid = NULL; 474 int rc; 475 476 rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid); 477 if (rc) { 478 cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name); 479 return; 480 } 481 spin_lock(&cfid->cfids->cfid_list_lock); 482 if (cfid->has_lease) { 483 cfid->has_lease = false; 484 close_cached_dir_locked(cfid); 485 } 486 spin_unlock(&cfid->cfids->cfid_list_lock); 487 close_cached_dir(cfid); 488 } 489 490 /** 491 * close_cached_dir - drop a reference of a cached dir 492 * 493 * The release function will be called with cfid_list_lock held to remove the 494 * cached dirs from the list before any other thread can take another @cfid 495 * ref. Must not be called with cfid_list_lock held; use 496 * close_cached_dir_locked() called instead. 497 * 498 * @cfid: cached dir 499 */ 500 void close_cached_dir(struct cached_fid *cfid) 501 { 502 lockdep_assert_not_held(&cfid->cfids->cfid_list_lock); 503 kref_put_lock(&cfid->refcount, smb2_close_cached_fid, &cfid->cfids->cfid_list_lock); 504 } 505 506 /** 507 * close_cached_dir_locked - put a reference of a cached dir with 508 * cfid_list_lock held 509 * 510 * Calling close_cached_dir() with cfid_list_lock held has the potential effect 511 * of causing a deadlock if the invariant of refcount >= 2 is false. 512 * 513 * This function is used in paths that hold cfid_list_lock and expect at least 514 * two references. If that invariant is violated, WARNs and returns without 515 * dropping a reference; the final put must still go through 516 * close_cached_dir(). 517 * 518 * @cfid: cached dir 519 */ 520 static void close_cached_dir_locked(struct cached_fid *cfid) 521 { 522 lockdep_assert_held(&cfid->cfids->cfid_list_lock); 523 524 if (WARN_ON(kref_read(&cfid->refcount) < 2)) 525 return; 526 527 kref_put(&cfid->refcount, smb2_close_cached_fid); 528 } 529 530 /* 531 * Called from cifs_kill_sb when we unmount a share 532 */ 533 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb) 534 { 535 struct rb_root *root = &cifs_sb->tlink_tree; 536 struct rb_node *node; 537 struct cached_fid *cfid; 538 struct cifs_tcon *tcon; 539 struct tcon_link *tlink; 540 struct cached_fids *cfids; 541 struct cached_dir_dentry *tmp_list, *q; 542 LIST_HEAD(entry); 543 544 spin_lock(&cifs_sb->tlink_tree_lock); 545 for (node = rb_first(root); node; node = rb_next(node)) { 546 tlink = rb_entry(node, struct tcon_link, tl_rbnode); 547 tcon = tlink_tcon(tlink); 548 if (IS_ERR(tcon)) 549 continue; 550 cfids = tcon->cfids; 551 if (cfids == NULL) 552 continue; 553 spin_lock(&cfids->cfid_list_lock); 554 list_for_each_entry(cfid, &cfids->entries, entry) { 555 tmp_list = kmalloc(sizeof(*tmp_list), GFP_ATOMIC); 556 if (tmp_list == NULL) { 557 /* 558 * If the malloc() fails, we won't drop all 559 * dentries, and unmounting is likely to trigger 560 * a 'Dentry still in use' error. 561 */ 562 cifs_tcon_dbg(VFS, "Out of memory while dropping dentries\n"); 563 spin_unlock(&cfids->cfid_list_lock); 564 spin_unlock(&cifs_sb->tlink_tree_lock); 565 goto done; 566 } 567 568 tmp_list->dentry = cfid->dentry; 569 cfid->dentry = NULL; 570 571 list_add_tail(&tmp_list->entry, &entry); 572 } 573 spin_unlock(&cfids->cfid_list_lock); 574 } 575 spin_unlock(&cifs_sb->tlink_tree_lock); 576 577 done: 578 list_for_each_entry_safe(tmp_list, q, &entry, entry) { 579 list_del(&tmp_list->entry); 580 dput(tmp_list->dentry); 581 kfree(tmp_list); 582 } 583 584 /* Flush any pending work that will drop dentries */ 585 flush_workqueue(cfid_put_wq); 586 } 587 588 /* 589 * Invalidate all cached dirs when a TCON has been reset 590 * due to a session loss. 591 */ 592 void invalidate_all_cached_dirs(struct cifs_tcon *tcon) 593 { 594 struct cached_fids *cfids = tcon->cfids; 595 struct cached_fid *cfid, *q; 596 597 if (cfids == NULL) 598 return; 599 600 /* 601 * Mark all the cfids as closed, and move them to the cfids->dying list. 602 * They'll be cleaned up by laundromat. Take a reference to each cfid 603 * during this process. 604 */ 605 spin_lock(&cfids->cfid_list_lock); 606 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { 607 list_move(&cfid->entry, &cfids->dying); 608 cfids->num_entries--; 609 cfid->is_open = false; 610 cfid->on_list = false; 611 if (cfid->has_lease) { 612 /* 613 * The lease was never cancelled from the server, 614 * so steal that reference. 615 */ 616 cfid->has_lease = false; 617 } else 618 kref_get(&cfid->refcount); 619 } 620 spin_unlock(&cfids->cfid_list_lock); 621 622 /* run laundromat unconditionally now as there might have been previously queued work */ 623 mod_delayed_work(cfid_put_wq, &cfids->laundromat_work, 0); 624 flush_delayed_work(&cfids->laundromat_work); 625 } 626 627 static void 628 cached_dir_offload_close(struct work_struct *work) 629 { 630 struct cached_fid *cfid = container_of(work, 631 struct cached_fid, close_work); 632 struct cifs_tcon *tcon = cfid->tcon; 633 634 WARN_ON(cfid->on_list); 635 636 close_cached_dir(cfid); 637 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_cached_close); 638 } 639 640 /* 641 * Release the cached directory's dentry, and then queue work to drop cached 642 * directory itself (closing on server if needed). 643 * 644 * Must be called with a reference to the cached_fid and a reference to the 645 * tcon. 646 */ 647 static void cached_dir_put_work(struct work_struct *work) 648 { 649 struct cached_fid *cfid = container_of(work, struct cached_fid, 650 put_work); 651 dput(cfid->dentry); 652 cfid->dentry = NULL; 653 654 queue_work(serverclose_wq, &cfid->close_work); 655 } 656 657 bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]) 658 { 659 struct cached_fids *cfids = tcon->cfids; 660 struct cached_fid *cfid; 661 662 if (cfids == NULL) 663 return false; 664 665 spin_lock(&cfids->cfid_list_lock); 666 list_for_each_entry(cfid, &cfids->entries, entry) { 667 if (cfid->has_lease && 668 !memcmp(lease_key, 669 cfid->fid.lease_key, 670 SMB2_LEASE_KEY_SIZE)) { 671 cfid->has_lease = false; 672 cfid->time = 0; 673 /* 674 * We found a lease remove it from the list 675 * so no threads can access it. 676 */ 677 list_del(&cfid->entry); 678 cfid->on_list = false; 679 cfids->num_entries--; 680 681 ++tcon->tc_count; 682 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, 683 netfs_trace_tcon_ref_get_cached_lease_break); 684 queue_work(cfid_put_wq, &cfid->put_work); 685 spin_unlock(&cfids->cfid_list_lock); 686 return true; 687 } 688 } 689 spin_unlock(&cfids->cfid_list_lock); 690 return false; 691 } 692 693 static struct cached_fid *init_cached_dir(const char *path) 694 { 695 struct cached_fid *cfid; 696 697 cfid = kzalloc(sizeof(*cfid), GFP_ATOMIC); 698 if (!cfid) 699 return NULL; 700 cfid->path = kstrdup(path, GFP_ATOMIC); 701 if (!cfid->path) { 702 kfree(cfid); 703 return NULL; 704 } 705 706 INIT_WORK(&cfid->close_work, cached_dir_offload_close); 707 INIT_WORK(&cfid->put_work, cached_dir_put_work); 708 INIT_LIST_HEAD(&cfid->entry); 709 INIT_LIST_HEAD(&cfid->dirents.entries); 710 mutex_init(&cfid->dirents.de_mutex); 711 kref_init(&cfid->refcount); 712 return cfid; 713 } 714 715 static void free_cached_dir(struct cached_fid *cfid) 716 { 717 struct cached_dirent *dirent, *q; 718 719 WARN_ON(work_pending(&cfid->close_work)); 720 WARN_ON(work_pending(&cfid->put_work)); 721 722 dput(cfid->dentry); 723 cfid->dentry = NULL; 724 725 /* 726 * Delete all cached dirent names 727 */ 728 list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) { 729 list_del(&dirent->entry); 730 kfree(dirent->name); 731 kfree(dirent); 732 } 733 734 /* adjust tcon-level counters and reset per-dir accounting */ 735 if (cfid->cfids) { 736 if (cfid->dirents.entries_count) 737 atomic_long_sub((long)cfid->dirents.entries_count, 738 &cfid->cfids->total_dirents_entries); 739 if (cfid->dirents.bytes_used) { 740 atomic64_sub((long long)cfid->dirents.bytes_used, 741 &cfid->cfids->total_dirents_bytes); 742 atomic64_sub((long long)cfid->dirents.bytes_used, 743 &cifs_dircache_bytes_used); 744 } 745 } 746 cfid->dirents.entries_count = 0; 747 cfid->dirents.bytes_used = 0; 748 749 kfree(cfid->path); 750 cfid->path = NULL; 751 kfree(cfid); 752 } 753 754 static void cfids_laundromat_worker(struct work_struct *work) 755 { 756 struct cached_fids *cfids; 757 struct cached_fid *cfid, *q; 758 LIST_HEAD(entry); 759 760 cfids = container_of(work, struct cached_fids, laundromat_work.work); 761 762 spin_lock(&cfids->cfid_list_lock); 763 /* move cfids->dying to the local list */ 764 list_cut_before(&entry, &cfids->dying, &cfids->dying); 765 766 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { 767 if (cfid->last_access_time && 768 time_after(jiffies, cfid->last_access_time + HZ * dir_cache_timeout)) { 769 cfid->on_list = false; 770 list_move(&cfid->entry, &entry); 771 cfids->num_entries--; 772 if (cfid->has_lease) { 773 /* 774 * Our lease has not yet been cancelled from the 775 * server. Steal that reference. 776 */ 777 cfid->has_lease = false; 778 } else 779 kref_get(&cfid->refcount); 780 } 781 } 782 spin_unlock(&cfids->cfid_list_lock); 783 784 list_for_each_entry_safe(cfid, q, &entry, entry) { 785 list_del(&cfid->entry); 786 787 dput(cfid->dentry); 788 cfid->dentry = NULL; 789 790 if (cfid->is_open) { 791 spin_lock(&cifs_tcp_ses_lock); 792 ++cfid->tcon->tc_count; 793 trace_smb3_tcon_ref(cfid->tcon->debug_id, cfid->tcon->tc_count, 794 netfs_trace_tcon_ref_get_cached_laundromat); 795 spin_unlock(&cifs_tcp_ses_lock); 796 queue_work(serverclose_wq, &cfid->close_work); 797 } else 798 /* 799 * Drop the ref-count from above, either the lease-ref (if there 800 * was one) or the extra one acquired. 801 */ 802 close_cached_dir(cfid); 803 } 804 queue_delayed_work(cfid_put_wq, &cfids->laundromat_work, 805 dir_cache_timeout * HZ); 806 } 807 808 struct cached_fids *init_cached_dirs(void) 809 { 810 struct cached_fids *cfids; 811 812 cfids = kzalloc(sizeof(*cfids), GFP_KERNEL); 813 if (!cfids) 814 return NULL; 815 spin_lock_init(&cfids->cfid_list_lock); 816 INIT_LIST_HEAD(&cfids->entries); 817 INIT_LIST_HEAD(&cfids->dying); 818 819 INIT_DELAYED_WORK(&cfids->laundromat_work, cfids_laundromat_worker); 820 queue_delayed_work(cfid_put_wq, &cfids->laundromat_work, 821 dir_cache_timeout * HZ); 822 823 atomic_long_set(&cfids->total_dirents_entries, 0); 824 atomic64_set(&cfids->total_dirents_bytes, 0); 825 826 return cfids; 827 } 828 829 /* 830 * Called from tconInfoFree when we are tearing down the tcon. 831 * There are no active users or open files/directories at this point. 832 */ 833 void free_cached_dirs(struct cached_fids *cfids) 834 { 835 struct cached_fid *cfid, *q; 836 LIST_HEAD(entry); 837 838 if (cfids == NULL) 839 return; 840 841 cancel_delayed_work_sync(&cfids->laundromat_work); 842 843 spin_lock(&cfids->cfid_list_lock); 844 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { 845 cfid->on_list = false; 846 cfid->is_open = false; 847 list_move(&cfid->entry, &entry); 848 } 849 list_for_each_entry_safe(cfid, q, &cfids->dying, entry) { 850 cfid->on_list = false; 851 cfid->is_open = false; 852 list_move(&cfid->entry, &entry); 853 } 854 spin_unlock(&cfids->cfid_list_lock); 855 856 list_for_each_entry_safe(cfid, q, &entry, entry) { 857 list_del(&cfid->entry); 858 free_cached_dir(cfid); 859 } 860 861 kfree(cfids); 862 } 863