Skip to content

Commit

Permalink
cifs: return a single-use cfid if we did not get a lease
Browse files Browse the repository at this point in the history
If we did not get a lease we can still return a single use cfid to the caller.
The cfid will not have has_lease set and will thus not be shared with any
other concurrent users and will be freed immediately when the caller
drops the handle.

This avoids extra roundtrips for servers that do not support directory leases
where they would first fail to get a cfid with a lease and then fallback
to try a normal SMB2_open()

Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Cc: stable@vger.kernel.org
Reviewed-by: Bharath SM <bharathsm@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@manguebit.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
  • Loading branch information
Ronnie Sahlberg authored and Steve French committed Feb 20, 2023
1 parent 66d45ca commit 8e843bf
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions fs/cifs/cached_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

static struct cached_fid *init_cached_dir(const char *path);
static void free_cached_dir(struct cached_fid *cfid);
static void smb2_close_cached_fid(struct kref *ref);

static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
const char *path,
Expand Down Expand Up @@ -221,6 +222,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
}
goto oshr_free;
}
cfid->tcon = tcon;
cfid->is_open = true;

o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
Expand All @@ -233,7 +235,6 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
goto oshr_free;


smb2_parse_contexts(server, o_rsp,
&oparms.fid->epoch,
oparms.fid->lease_key, &oplock,
Expand All @@ -260,7 +261,6 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
}
}
cfid->dentry = dentry;
cfid->tcon = tcon;
cfid->time = jiffies;
cfid->has_lease = true;

Expand All @@ -271,7 +271,7 @@ 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[1], rsp_iov[1].iov_base);
spin_lock(&cfids->cfid_list_lock);
if (!cfid->has_lease) {
if (rc && !cfid->has_lease) {
if (cfid->on_list) {
list_del(&cfid->entry);
cfid->on_list = false;
Expand All @@ -280,6 +280,15 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
rc = -ENOENT;
}
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 (cfid->is_open)
SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
Expand Down Expand Up @@ -340,6 +349,7 @@ smb2_close_cached_fid(struct kref *ref)
if (cfid->is_open) {
SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
cfid->fid.volatile_fid);
atomic_dec(&cfid->tcon->num_remote_opens);
}

free_cached_dir(cfid);
Expand Down

0 comments on commit 8e843bf

Please sign in to comment.