diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c index 647fa26da4d2..50217f47a0ed 100644 --- a/fs/smb/client/cached_dir.c +++ b/fs/smb/client/cached_dir.c @@ -649,6 +649,17 @@ void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync) flush_delayed_work(&cfids->laundromat_work); } +static bool cached_dir_get_tcon(struct cifs_tcon *tcon, + enum smb3_tcon_ref_trace trace) +{ + guard(spinlock)(&tcon->tc_lock); + if (tcon->status == TID_EXITING || tcon->tc_count <= 0) + return false; + ++tcon->tc_count; + trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, trace); + return true; +} + static void cached_dir_offload_close(struct work_struct *work) { @@ -693,6 +704,9 @@ bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]) !memcmp(lease_key, cfid->fid.lease_key, SMB2_LEASE_KEY_SIZE)) { + if (!cached_dir_get_tcon(tcon, + netfs_trace_tcon_ref_get_cached_lease_break)) + break; cfid->has_lease = false; cfid->time = 0; /* @@ -703,9 +717,6 @@ bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]) cfid->on_list = false; cfids->num_entries--; - ++tcon->tc_count; - trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, - netfs_trace_tcon_ref_get_cached_lease_break); queue_work(cfid_put_wq, &cfid->put_work); spin_unlock(&cfids->cfid_list_lock); return true; @@ -813,12 +824,13 @@ static void cfids_laundromat_worker(struct work_struct *work) cfid->dentry = NULL; if (cfid->is_open) { - spin_lock(&cfid->tcon->tc_lock); - ++cfid->tcon->tc_count; - trace_smb3_tcon_ref(cfid->tcon->debug_id, cfid->tcon->tc_count, - netfs_trace_tcon_ref_get_cached_laundromat); - spin_unlock(&cfid->tcon->tc_lock); - queue_work(serverclose_wq, &cfid->close_work); + if (cached_dir_get_tcon(cfid->tcon, + netfs_trace_tcon_ref_get_cached_laundromat)) { + queue_work(serverclose_wq, &cfid->close_work); + continue; + } + scoped_guard(spinlock, &cfids->cfid_list_lock) + list_add_tail(&cfid->entry, &cfids->dying); } else /* * Drop the ref-count from above, either the lease-ref (if there