Commit ebe98f14 authored by Ronnie Sahlberg's avatar Ronnie Sahlberg Committed by Steve French

cifs: enable caching of directories for which a lease is held

This expands the directory caching to now cache an open handle for all
directories (up to a maximum) and not just the root directory.

In this patch, locking and refcounting is intended to work as so:

The main function to get a reference to a cached handle is
find_or_create_cached_dir() called from open_cached_dir()
These functions are protected under the cfid_list_lock spin-lock
to make sure we do not race creating new references for cached dirs
with deletion of expired ones.

An successful open_cached_dir() will take out 2 references to the cfid if
this was the very first and successful call to open the directory and
it acquired a lease from the server.
One reference is for the lease  and the other is for the cfid that we
return. The is lease reference is tracked by cfid->has_lease.
If the directory already has a handle with an active lease, then we just
take out one new reference for the cfid and return it.
It can happen that we have a thread that tries to open a cached directory
where we have a cfid already but we do not, yet, have a working lease. In
this case we will just return NULL, and this the caller will fall back to
the case when no handle was available.

In this model the total number of references we have on a cfid is
1 for while the handle is open and we have a lease, and one additional
reference for each open instance of a cfid.

Once we get a lease break (cached_dir_lease_break()) we remove the
cfid from the list under the spinlock. This prevents any new threads to
use it, and we also call smb2_cached_lease_break() via the work_queue
in order to drop the reference we got for the lease (we drop it outside
of the spin-lock.)
Anytime a thread calls close_cached_dir() we also drop a reference to the
cfid.
When the last reference to the cfid is released smb2_close_cached_fid()
will be invoked which will drop the reference ot the dentry we held for
this cfid and it will also, if we the handle is open/has a lease
also call SMB2_close() to close the handle on the server.

Two events require special handling:
invalidate_all_cached_dirs() this function is called from SMB2_tdis()
and cifs_mark_open_files_invalid().
In both cases the tcon is either gone already or will be shortly so
we do not need to actually close the handles. They will be dropped
server side as part of the tcon dropping.
But we have to be careful about a potential race with a concurrent
lease break so we need to take out additional refences to avoid the
cfid from being freed while we are still referencing it.

free_cached_dirs() which is called from tconInfoFree().
This is called quite late in the umount process so there should no longer
be any open handles or files and we can just free all the remaining data.
Signed-off-by: default avatarRonnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: default avatarSteve French <stfrench@microsoft.com>
parent 9ee2afe5
This diff is collapsed.
...@@ -31,14 +31,17 @@ struct cached_dirents { ...@@ -31,14 +31,17 @@ struct cached_dirents {
}; };
struct cached_fid { struct cached_fid {
struct list_head entry;
struct cached_fids *cfids;
const char *path; const char *path;
bool is_valid:1; /* Do we have a useable root fid */
bool file_all_info_is_valid:1;
bool has_lease:1; bool has_lease:1;
bool is_open:1;
bool on_list:1;
bool file_all_info_is_valid:1;
unsigned long time; /* jiffies of when lease was taken */ unsigned long time; /* jiffies of when lease was taken */
struct kref refcount; struct kref refcount;
struct cifs_fid fid; struct cifs_fid fid;
struct mutex fid_mutex; spinlock_t fid_lock;
struct cifs_tcon *tcon; struct cifs_tcon *tcon;
struct dentry *dentry; struct dentry *dentry;
struct work_struct lease_break; struct work_struct lease_break;
...@@ -46,9 +49,14 @@ struct cached_fid { ...@@ -46,9 +49,14 @@ struct cached_fid {
struct cached_dirents dirents; struct cached_dirents dirents;
}; };
#define MAX_CACHED_FIDS 16
struct cached_fids { struct cached_fids {
struct mutex cfid_list_mutex; /* Must be held when:
struct cached_fid *cfid; * - accessing the cfids->entries list
*/
spinlock_t cfid_list_lock;
int num_entries;
struct list_head entries;
}; };
extern struct cached_fids *init_cached_dirs(void); extern struct cached_fids *init_cached_dirs(void);
...@@ -61,8 +69,6 @@ extern int open_cached_dir_by_dentry(struct cifs_tcon *tcon, ...@@ -61,8 +69,6 @@ extern int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
struct dentry *dentry, struct dentry *dentry,
struct cached_fid **cfid); struct cached_fid **cfid);
extern void close_cached_dir(struct cached_fid *cfid); extern void close_cached_dir(struct cached_fid *cfid);
extern void close_cached_dir_lease(struct cached_fid *cfid);
extern void close_cached_dir_lease_locked(struct cached_fid *cfid);
extern void close_all_cached_dirs(struct cifs_sb_info *cifs_sb); extern void close_all_cached_dirs(struct cifs_sb_info *cifs_sb);
extern void invalidate_all_cached_dirs(struct cifs_tcon *tcon); extern void invalidate_all_cached_dirs(struct cifs_tcon *tcon);
extern int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]); extern int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]);
......
...@@ -2299,13 +2299,13 @@ cifs_dentry_needs_reval(struct dentry *dentry) ...@@ -2299,13 +2299,13 @@ cifs_dentry_needs_reval(struct dentry *dentry)
return true; return true;
if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) { if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
mutex_lock(&cfid->fid_mutex); spin_lock(&cfid->fid_lock);
if (cfid->time && cifs_i->time > cfid->time) { if (cfid->time && cifs_i->time > cfid->time) {
mutex_unlock(&cfid->fid_mutex); spin_unlock(&cfid->fid_lock);
close_cached_dir(cfid); close_cached_dir(cfid);
return false; return false;
} }
mutex_unlock(&cfid->fid_mutex); spin_unlock(&cfid->fid_lock);
close_cached_dir(cfid); close_cached_dir(cfid);
} }
/* /*
......
...@@ -801,7 +801,7 @@ smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon, ...@@ -801,7 +801,7 @@ smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid); rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid);
if (!rc) { if (!rc) {
if (cfid->is_valid) { if (cfid->has_lease) {
close_cached_dir(cfid); close_cached_dir(cfid);
return 0; return 0;
} }
......
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