smb: client: prevent lease breaks of cached parents when opening children
Subsystems (2)
PATCH v2 12/20 smb: client: prevent lease breaks of cached parents when opening children
In SMB2_open_init() lookup for a cached parent of target path and set ParentLeaseKey in lease context if found.
Introduce invalidate_cached_dirents() to allow revalidation of cached dirents but still keep the cached directory.
Testing with e.g. xfstests generic/637 (small simple test) shows a performance gain of ~17% less create/open ops, and ~83% less lease breaks:
Before patch
Create/open:
# tshark -Y 'smb2.create.disposition == 1' -r unpatched.pcap 169
Lease breaks:
# tshark -Y 'smb2.cmd == 18' -r unpatched.pcap 12 ----------------- After patch: Create/open: # tshark -Y 'smb2.create.disposition == 1' -r patched.pcap 144
Lease breaks:
# tshark -Y 'smb2.cmd == 18' -r patched.pcap 2
Other:
- set oparms->cifs_sb in open_cached_dir() as we need it in check_cached_parent(); use CIFS_OPARMS() too
- introduce CFID_LOOKUP_PARENT lookup mode (for string paths only)
- add cached_fids->dirsep to save dir separator, used by parent lookups
---
v2:
- added invalidate_cached_dirents()
- fixed conditions to add ParentLeaseKey
- fixed wrong function name in commit message
- added performance comparison without and with patch to commit message
fs/smb/client/cached_dir.c | 106 ++++++++++++++++++++++++------------- fs/smb/client/cached_dir.h | 5 ++ fs/smb/client/dir.c | 23 +------- fs/smb/client/smb2inode.c | 10 +++- fs/smb/client/smb2pdu.c | 63 +++++++++++++++++----- 5 files changed, 133 insertions(+), 74 deletions(-)
fs/smb/client/cached_dir.c +68 -38
@@ -71,11 +71,29 @@ static struct cached_fid *find_cfid(struct cached_fids *cfids, const void *key, bool wait_open) {struct cached_fid *cfid, *found; +const char *parent_path =NULL ;bool match;if (!cfids || !key)return NULL ; +if (mode ==CFID_LOOKUP_PARENT ) { +const char *path = key; + +if (!*path) +return NULL ; + + parent_path =strrchr (path, cfids->dirsep); +if (!parent_path) +return NULL ; + + parent_path =kstrndup (path, parent_path - path,GFP_KERNEL ); +if (WARN_ON_ONCE (!parent_path)) +return NULL ; + + key = parent_path; + mode =CFID_LOOKUP_PATH ; + } retry_find: found =NULL ;
@@ -114,6 +132,8 @@ static struct cached_fid *find_cfid(struct cached_fids *cfids, const void *key,kref_get (&found->refcount); } +kfree (parent_path); +return found; }
@@ -226,7 +246,6 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char *path,struct cached_fids *cfids;const char *npath;int retries =0 , cur_sleep =1 ; -__le32 lease_flags =0 ;if (cifs_sb->root ==NULL )return -ENOENT ;
@@ -236,9 +255,11 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char *path, ses = tcon->ses; cfids = tcon->cfids; -if (!cfids)return -EOPNOTSUPP ; + +if (!cfids->dirsep) + cfids->dirsep =CIFS_DIR_SEP (cifs_sb); replay_again:/* reinitialize for possible replay */ flags =0 ;
@@ -306,24 +327,6 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char *path, rc = -ENOENT ; goto out; } -if (dentry->d_parent && server->dialect >=SMB30_PROT_ID ) { -struct cached_fid *parent_cfid; - -spin_lock (&cfids->cfid_list_lock); -list_for_each_entry (parent_cfid, &cfids->entries, entry) { -if (parent_cfid->dentry == dentry->d_parent) { -if (!cfid_is_valid (parent_cfid)) -break ; - -cifs_dbg (FYI ,"found a parent cached file handle\n" ); - lease_flags |=SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ; -memcpy (pfid->parent_lease_key, parent_cfid->fid.lease_key, -SMB2_LEASE_KEY_SIZE ); -break ; - } - } -spin_unlock (&cfids->cfid_list_lock); - } } cfid->dentry = dentry; cfid->tcon = tcon;
@@ -350,20 +353,13 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char *path, rqst[0 ].rq_iov = open_iov; rqst[0 ].rq_nvec =SMB2_CREATE_IOV_SIZE ; - oparms = (struct cifs_open_parms ) { - .tcon = tcon, - .path = path, - .create_options =cifs_create_options (cifs_sb,CREATE_NOT_FILE ), - .desired_access =FILE_READ_DATA |FILE_READ_ATTRIBUTES | -FILE_READ_EA , - .disposition =FILE_OPEN , - .fid = pfid, - .lease_flags = lease_flags, - .replay = !!(retries), - }; - - rc =SMB2_open_init (tcon, server, - &rqst[0 ], &oplock, &oparms, utf16_path); + oparms =CIFS_OPARMS (cifs_sb, tcon, path, +FILE_READ_DATA |FILE_READ_ATTRIBUTES |FILE_READ_EA ,FILE_OPEN , +cifs_create_options (cifs_sb,CREATE_NOT_FILE ),0 ); + oparms.fid = pfid; + oparms.replay = !!retries; + + rc =SMB2_open_init (tcon, server, &rqst[0 ], &oplock, &oparms, utf16_path);if (rc) goto oshr_free;smb2_set_next_command (tcon, &rqst[0 ]);
@@ -478,20 +474,34 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char *path,return rc; } -static void smb2_close_cached_fid (struct kref *ref) +static void __invalidate_cached_dirents (struct cached_fid *cfid) { - struct cached_fid *cfid = container_of(ref, struct cached_fid, refcount);struct cached_dirent *de, *q; -drop_cfid (cfid); +if (!cfid) +return ; -/* Delete all cached dirent names */ +mutex_lock (&cfid->dirents.de_mutex);list_for_each_entry_safe (de, q, &cfid->dirents.entries, entry) {list_del (&de->entry);kfree (de->name);kfree (de); } + cfid->dirents.is_valid = false; + cfid->dirents.is_failed = false; + cfid->dirents.file =NULL ; + cfid->dirents.pos =0 ; +mutex_unlock (&cfid->dirents.de_mutex); +} + +static void smb2_close_cached_fid (struct kref *ref) +{ + struct cached_fid *cfid = container_of(ref, struct cached_fid, refcount); + +__invalidate_cached_dirents (cfid); +drop_cfid (cfid); +kfree (cfid->file_all_info); cfid->file_all_info =NULL ;kfree (cfid->path);
@@ -531,6 +541,26 @@ bool drop_cached_dir(struct cached_fids *cfids, const void *key, int mode)return true; } +/* +* Invalidate cached dirents from @key's parent, regardless if @key itself is a cached dir. +* +* Lease breaks don't necessarily require this, and would require finding the child to begin with, +* so skip such cases. +*/ +void invalidate_cached_dirents (struct cached_fids *cfids,const void *key,int mode) +{ +struct cached_fid *cfid =NULL ; + +if (mode ==CFID_LOOKUP_LEASEKEY ) +return ; + + cfid =find_cfid (cfids, key, mode, false); +if (cfid) { +__invalidate_cached_dirents (cfid); +kref_put (&cfid->refcount, smb2_close_cached_fid); + } +} +void close_cached_dir (struct cached_fid *cfid) {kref_put (&cfid->refcount, smb2_close_cached_fid);
fs/smb/client/cached_dir.h +5 -0
@@ -54,11 +54,15 @@ struct cached_fids {int num_entries;struct list_head entries;struct delayed_work laundromat_work; + +/* convenience for parent lookups */ +int dirsep; };/* Lookup modes for find_cached_dir() */ enum {CFID_LOOKUP_PATH , +CFID_LOOKUP_PARENT ,CFID_LOOKUP_DENTRY ,CFID_LOOKUP_LEASEKEY , };
@@ -79,6 +83,7 @@ extern int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char struct cifs_sb_info *cifs_sb, struct cached_fid **cfid);extern void close_cached_dir (struct cached_fid *cfid);extern bool drop_cached_dir (struct cached_fids *cfids,const void *key,int mode); +extern void invalidate_cached_dirents (struct cached_fids *cfids,const void *key,int mode);extern void close_all_cached_dirs (struct cifs_sb_info *cifs_sb);extern void invalidate_all_cached_dirs (struct cached_fids *cfids);#endif /* _CACHED_DIR_H */
fs/smb/client/dir.c +1 -22
@@ -190,9 +190,7 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsignedint disposition;struct TCP_Server_Info *server = tcon->ses->server;struct cifs_open_parms oparms; -struct cached_fid *parent_cfid =NULL ;int rdwr_for_fscache =0 ; -__le32 lease_flags =0 ; *oplock =0 ;if (tcon->ses->server->oplocks)
@@ -314,26 +312,8 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsignedif (!tcon->unix_ext && (mode &S_IWUGO ) ==0 ) create_options |=CREATE_OPTION_READONLY ; - retry_open: -if (tcon->cfids && direntry->d_parent && server->dialect >=SMB30_PROT_ID ) { - parent_cfid =NULL ; -spin_lock (&tcon->cfids->cfid_list_lock); -list_for_each_entry (parent_cfid, &tcon->cfids->entries, entry) { -if (parent_cfid->dentry == direntry->d_parent) { -if (!cfid_is_valid (parent_cfid)) -break ; - -cifs_dbg (FYI ,"found a parent cached file handle\n" ); - lease_flags |=SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE ; -memcpy (fid->parent_lease_key, parent_cfid->fid.lease_key, -SMB2_LEASE_KEY_SIZE ); - parent_cfid->dirents.is_valid = false; -break ; - } - } -spin_unlock (&tcon->cfids->cfid_list_lock); - } +invalidate_cached_dirents (tcon->cfids, direntry->d_parent,CFID_LOOKUP_DENTRY ); oparms = (struct cifs_open_parms ) { .tcon = tcon,
@@ -343,7 +323,6 @@ static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned .disposition = disposition, .path = full_path, .fid = fid, - .lease_flags = lease_flags, .mode = mode, }; rc = server->ops->open (xid, &oparms, oplock, buf);
fs/smb/client/smb2inode.c +9 -1
@@ -1120,6 +1120,8 @@ smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode, {struct cifs_open_parms oparms; +invalidate_cached_dirents (tcon->cfids, name,CFID_LOOKUP_PARENT ); + oparms =CIFS_OPARMS (cifs_sb, tcon, name,FILE_WRITE_ATTRIBUTES ,FILE_CREATE ,CREATE_NOT_FILE , mode); return smb2_compound_op(xid, tcon, cifs_sb,
@@ -1141,6 +1143,8 @@ smb2_mkdir_setinfo(struct inode *inode, const char *name,u32 dosattrs;int tmprc; +invalidate_cached_dirents (tcon->cfids, name,CFID_LOOKUP_PARENT ); + in_iov.iov_base = &data; in_iov.iov_len =sizeof (data); cifs_i =CIFS_I (inode);
@@ -1180,8 +1184,12 @@ smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,struct inode *inode =NULL ;int rc; -if (dentry) +if (dentry) { inode =d_inode (dentry); +invalidate_cached_dirents (tcon->cfids, dentry->d_parent,CFID_LOOKUP_DENTRY ); + }else { +invalidate_cached_dirents (tcon->cfids, name,CFID_LOOKUP_PARENT ); + } oparms =CIFS_OPARMS (cifs_sb, tcon, name,DELETE ,FILE_OPEN ,OPEN_REPARSE_POINT ,ACL_NO_MODE );
fs/smb/client/smb2pdu.c +50 -13
83 lines not on this page · raw message
Signature · 1 line
2.49.0