Commit 81ba1095 authored by Paulo Alcantara's avatar Paulo Alcantara Committed by Steve French

smb: client: prevent new fids from being removed by laundromat

Check if @cfid->time is set in laundromat so we guarantee that only
fully cached fids will be selected for removal.  While we're at it,
add missing locks to protect access of @cfid fields in order to avoid
races with open_cached_dir() and cfids_laundromat_worker(),
respectively.
Signed-off-by: default avatarPaulo Alcantara (SUSE) <pc@manguebit.com>
Reviewed-by: default avatarShyam Prasad N <sprasad@microsoft.com>
Signed-off-by: default avatarSteve French <stfrench@microsoft.com>
parent e95f3f74
...@@ -170,15 +170,18 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, ...@@ -170,15 +170,18 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
return -ENOENT; return -ENOENT;
} }
/* /*
* At this point we either have a lease already and we can just * Return cached fid if it has a lease. Otherwise, it is either a new
* return it. If not we are guaranteed to be the only thread accessing * entry or laundromat worker removed it from @cfids->entries. Caller
* this cfid. * will put last reference if the latter.
*/ */
spin_lock(&cfids->cfid_list_lock);
if (cfid->has_lease) { if (cfid->has_lease) {
spin_unlock(&cfids->cfid_list_lock);
*ret_cfid = cfid; *ret_cfid = cfid;
kfree(utf16_path); kfree(utf16_path);
return 0; return 0;
} }
spin_unlock(&cfids->cfid_list_lock);
/* /*
* Skip any prefix paths in @path as lookup_positive_unlocked() ends up * Skip any prefix paths in @path as lookup_positive_unlocked() ends up
...@@ -295,9 +298,11 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, ...@@ -295,9 +298,11 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
goto oshr_free; goto oshr_free;
} }
} }
spin_lock(&cfids->cfid_list_lock);
cfid->dentry = dentry; cfid->dentry = dentry;
cfid->time = jiffies; cfid->time = jiffies;
cfid->has_lease = true; cfid->has_lease = true;
spin_unlock(&cfids->cfid_list_lock);
oshr_free: oshr_free:
kfree(utf16_path); kfree(utf16_path);
...@@ -306,24 +311,28 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, ...@@ -306,24 +311,28 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base); free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base); free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
spin_lock(&cfids->cfid_list_lock); spin_lock(&cfids->cfid_list_lock);
if (rc && !cfid->has_lease) { if (!cfid->has_lease) {
if (cfid->on_list) { if (rc) {
list_del(&cfid->entry); if (cfid->on_list) {
cfid->on_list = false; list_del(&cfid->entry);
cfids->num_entries--; cfid->on_list = false;
cfids->num_entries--;
}
rc = -ENOENT;
} else {
/*
* We are guaranteed to have two references at this
* point. One for the caller and one for a potential
* lease. Release the Lease-ref so that the directory
* will be closed when the caller closes the cached
* handle.
*/
spin_unlock(&cfids->cfid_list_lock);
kref_put(&cfid->refcount, smb2_close_cached_fid);
goto out;
} }
rc = -ENOENT;
} }
spin_unlock(&cfids->cfid_list_lock); spin_unlock(&cfids->cfid_list_lock);
if (!rc && !cfid->has_lease) {
/*
* We are guaranteed to have two references at this point.
* One for the caller and one for a potential lease.
* Release the Lease-ref so that the directory will be closed
* when the caller closes the cached handle.
*/
kref_put(&cfid->refcount, smb2_close_cached_fid);
}
if (rc) { if (rc) {
if (cfid->is_open) if (cfid->is_open)
SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid, SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
...@@ -331,7 +340,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, ...@@ -331,7 +340,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
free_cached_dir(cfid); free_cached_dir(cfid);
cfid = NULL; cfid = NULL;
} }
out:
if (rc == 0) { if (rc == 0) {
*ret_cfid = cfid; *ret_cfid = cfid;
atomic_inc(&tcon->num_remote_opens); atomic_inc(&tcon->num_remote_opens);
...@@ -583,15 +592,18 @@ static void cfids_laundromat_worker(struct work_struct *work) ...@@ -583,15 +592,18 @@ static void cfids_laundromat_worker(struct work_struct *work)
spin_lock(&cfids->cfid_list_lock); spin_lock(&cfids->cfid_list_lock);
list_for_each_entry_safe(cfid, q, &cfids->entries, entry) { list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
if (time_after(jiffies, cfid->time + HZ * dir_cache_timeout)) { if (cfid->time &&
time_after(jiffies, cfid->time + HZ * dir_cache_timeout)) {
cfid->on_list = false;
list_move(&cfid->entry, &entry); list_move(&cfid->entry, &entry);
cfids->num_entries--; cfids->num_entries--;
/* To prevent race with smb2_cached_lease_break() */
kref_get(&cfid->refcount);
} }
} }
spin_unlock(&cfids->cfid_list_lock); spin_unlock(&cfids->cfid_list_lock);
list_for_each_entry_safe(cfid, q, &entry, entry) { list_for_each_entry_safe(cfid, q, &entry, entry) {
cfid->on_list = false;
list_del(&cfid->entry); list_del(&cfid->entry);
/* /*
* Cancel and wait for the work to finish in case we are racing * Cancel and wait for the work to finish in case we are racing
...@@ -608,6 +620,8 @@ static void cfids_laundromat_worker(struct work_struct *work) ...@@ -608,6 +620,8 @@ static void cfids_laundromat_worker(struct work_struct *work)
spin_unlock(&cfids->cfid_list_lock); spin_unlock(&cfids->cfid_list_lock);
kref_put(&cfid->refcount, smb2_close_cached_fid); kref_put(&cfid->refcount, smb2_close_cached_fid);
} }
/* Drop the extra reference opened above */
kref_put(&cfid->refcount, smb2_close_cached_fid);
} }
queue_delayed_work(cifsiod_wq, &cfids->laundromat_work, queue_delayed_work(cifsiod_wq, &cfids->laundromat_work,
dir_cache_timeout * HZ); dir_cache_timeout * HZ);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment