cifs: clean up management of open filehandle (try #2)
Subsystems (2)
cover cifs: clean up management of open filehandle (try #2)
This is the second attempt at the patchset to clean up the management of open filehandles in CIFS. The main changes are:
1) elimination of some more unnecessary NULL pointer checks
2) the cifs_file_list_lock (aka GlobalSMBSeslock) has been left as a rwlock_t, though it has been renamed as before
3) the order of operations in cifs_oplock_break_put has been fixed -- previously it put the sb reference before the cifsFileInfo reference which could have led to oopses.
4) cifs_flush only waits for writeback to complete on filehandles that were opened with FMODE_WRITE set.
The patchset has passed several hours running the connectathon testsuite in a loop.
Suresh and Shaggy both sent acks for various parts of the patchset, but the last few patches had enough substantial changes that I didn't want to add them without their "re-acks".
Jeff Layton (15):
cifs: keep dentry reference in cifsFileInfo instead of inode
reference
cifs: don't use vfsmount to pin superblock for oplock breaks
cifs: eliminate cifs_posix_open_inode_helper
cifs: eliminate oflags option from cifs_new_fileinfo
cifs: eliminate the inode argument from cifs_new_fileinfo
cifs: clean up cifs_reopen_file
cifs: cifs_write argument change and cleanup
cifs: eliminate pfile pointer from cifsFileInfo
cifs: move cifs_new_fileinfo to file.c
cifs: rename GlobalSMBSeslock to cifs_file_list_lock
cifs: move cifsFileInfo_put to file.c
cifs: move close processing from cifs_close to cifsFileInfo_put
cifs: convert cifsFileInfo->count to non-atomic counter
cifs: wait for writeback to complete in cifs_flush
cifs: eliminate cifsInodeInfo->write_behind_rc
fs/cifs/cifs_fs_sb.h | 1 + fs/cifs/cifsfs.c | 21 ++- fs/cifs/cifsfs.h | 6 +- fs/cifs/cifsglob.h | 28 +-- fs/cifs/cifsproto.h | 6 +- fs/cifs/cifssmb.c | 4 +- fs/cifs/dir.c | 60 +----- fs/cifs/file.c | 575 +++++++++++++++++++------------------------------- fs/cifs/inode.c | 15 +- fs/cifs/misc.c | 18 +-- fs/cifs/readdir.c | 6 +- 11 files changed, 266 insertions(+), 474 deletions(-)
Signature · 1 line
1.7.2.3
A message that is not in the archive (sent off-list or not yet received): 1286394857-32541-1-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
PATCH 1/15 cifs: keep dentry reference in cifsFileInfo instead of inode reference
cifsFileInfo is a bit problematic. It contains a reference back to the struct file itself. This makes it difficult for a cifsFileInfo to exist without a corresponding struct file.
It would be better instead of the cifsFileInfo just held info pertaining to the open file on the server instead without any back refrences to the struct file. This would allow it to exist after the filp to which it was originally attached was closed.
Much of the use of the file pointer in this struct is to get at the dentry. Begin divorcing the cifsFileInfo from the struct file by keeping a reference to the dentry. Since the dentry will have a reference to the inode, we can eliminate the "pInode" field too and convert the igrab/iput to dget/dput.
--- fs/cifs/cifsglob.h | 4 ++-- fs/cifs/dir.c | 3 ++- fs/cifs/file.c | 2 +- fs/cifs/misc.c | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-)
fs/cifs/cifsglob.h +2 -2
@@ -387,7 +387,7 @@ struct cifsFileInfo {/* BB add lock scope info here if needed */ ;/* lock scope id (0 if none) */ struct file *pfile;/* needed for writepage */ -struct inode *pInode;/* needed for oplock break */ +struct dentry *dentry;struct vfsmount *mnt;struct tcon_link *tlink;struct mutex lock_mutex;
@@ -412,7 +412,7 @@ static inline void cifsFileInfo_put(struct cifsFileInfo *cifs_file) {if (atomic_dec_and_test (&cifs_file->count)) {cifs_put_tlink (cifs_file->tlink); -iput (cifs_file->pInode); +dput (cifs_file->dentry);kfree (cifs_file); } }
fs/cifs/dir.c +2 -1
@@ -135,6 +135,7 @@ cifs_new_fileinfo(struct inode *newinode, __u16 fileHandle, struct file *file, struct vfsmount *mnt, struct tcon_link *tlink, unsigned int oflags, __u32 oplock) { + struct dentry *dentry = file->f_path.dentry;struct cifsFileInfo *pCifsFile;struct cifsInodeInfo *pCifsInode;
@@ -145,7 +146,7 @@ cifs_new_fileinfo(struct inode *newinode, __u16 fileHandle, struct file *file, pCifsFile->netfid = fileHandle; pCifsFile->pid = current->tgid; pCifsFile->uid =current_fsuid (); - pCifsFile->pInode =igrab (newinode); + pCifsFile->dentry =dget (dentry); pCifsFile->mnt = mnt; pCifsFile->pfile = file; pCifsFile->invalidHandle = false;
fs/cifs/file.c +1 -1
@@ -2335,7 +2335,7 @@ void cifs_oplock_break(struct work_struct *work) { struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, oplock_break); -struct inode *inode = cfile->pInode; +struct inode *inode = cfile->dentry->d_inode;struct cifsInodeInfo *cinode =CIFS_I (inode);int rc, waitrc =0 ;
fs/cifs/misc.c +1 -1
@@ -578,7 +578,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv) }cFYI (1 ,"file id match, oplock break" ); - pCifsInode =CIFS_I (netfile->pInode); + pCifsInode =CIFS_I (netfile->dentry->d_inode); pCifsInode->clientCanCacheAll = false;if (pSMB->OplockLevel ==0 ) pCifsInode->clientCanCacheRead = false;
Signature · 1 line
1.7.2.3
PATCH 2/15 cifs: don't use vfsmount to pin superblock for oplock breaks
In reply to a message not in the archiveFilesystems aren't really supposed to do anything with a vfsmount. It's considered a layering violation since vfsmounts are entirely managed at the VFS layer.
CIFS currently keeps an active reference to a vfsmount in order to prevent the superblock vanishing before an oplock break has completed. What we really want to do instead is to keep sb->s_active high until the oplock break has completed. This patch borrows the scheme that NFS uses for handling sillyrenames.
An atomic_t is added to the cifs_sb_info. When it transitions from 0 to 1, an extra reference to the superblock is taken (by bumping the s_active value). When it transitions from 1 to 0, that reference is dropped and a the superblock teardown may proceed if there are no more references to it.
Also, the vfsmount pointer is removed from cifsFileInfo and from cifs_new_fileinfo, and some bogus forward declarations are removed from cifsfs.h.
--- fs/cifs/cifs_fs_sb.h | 1 + fs/cifs/cifsfs.c | 18 ++++++++++++++++++ fs/cifs/cifsfs.h | 6 ++---- fs/cifs/cifsglob.h | 1 - fs/cifs/cifsproto.h | 2 +- fs/cifs/dir.c | 10 +++------- fs/cifs/file.c | 9 ++++----- 7 files changed, 29 insertions(+), 18 deletions(-)
fs/cifs/cifs_fs_sb.h +1 -0
@@ -48,6 +48,7 @@ struct cifs_sb_info {struct nls_table *local_nls;unsigned int rsize;unsigned int wsize; +atomic_t active;uid_t mnt_uid;gid_t mnt_gid;mode_t mnt_file_mode;
fs/cifs/cifsfs.c +18 -0
@@ -83,6 +83,24 @@ extern mempool_t *cifs_sm_req_poolp;extern mempool_t *cifs_req_poolp;extern mempool_t *cifs_mid_poolp; +void +cifs_sb_active (struct super_block *sb) +{ +struct cifs_sb_info *server =CIFS_SB (sb); + +if (atomic_inc_return (&server->active) ==1 ) +atomic_inc (&sb->s_active); +} + +void +cifs_sb_deactive (struct super_block *sb) +{ +struct cifs_sb_info *server =CIFS_SB (sb); + +if (atomic_dec_and_test (&server->active)) +deactivate_super (sb); +} +static int cifs_read_super (struct super_block *sb,void *data,const char *devname,int silent)
fs/cifs/cifsfs.h +2 -4
@@ -42,10 +42,8 @@ extern const struct address_space_operations cifs_addr_ops;extern const struct address_space_operations cifs_addr_ops_smallbuf;/* Functions related to super block operations */ -/* extern const struct super_operations cifs_super_ops;*/ -extern void cifs_read_inode (struct inode *); -/*extern void cifs_delete_inode(struct inode *);*/ /* BB not needed yet */ -/* extern void cifs_write_inode(struct inode *); */ /* BB not needed yet */ +extern void cifs_sb_active (struct super_block *sb); +extern void cifs_sb_deactive (struct super_block *sb);/* Functions related to inodes */ extern const struct inode_operations cifs_dir_inode_ops;
fs/cifs/cifsglob.h +0 -1
@@ -388,7 +388,6 @@ struct cifsFileInfo {/* lock scope id (0 if none) */ struct file *pfile;/* needed for writepage */ struct dentry *dentry; -struct vfsmount *mnt;struct tcon_link *tlink;struct mutex lock_mutex;struct list_head llist;/* list of byte range locks we have. */
fs/cifs/cifsproto.h +1 -1
@@ -107,7 +107,7 @@ extern struct timespec cnvrtDosUnixTm(__le16 le_date, __le16 le_time,extern struct cifsFileInfo *cifs_new_fileinfo (struct inode *newinode,__u16 fileHandle,struct file *file, -struct vfsmount *mnt,struct tcon_link *tlink, +struct tcon_link *tlink,unsigned int oflags,__u32 oplock); extern int cifs_posix_open(char *full_path, struct inode **pinode, struct super_block *sb,
fs/cifs/dir.c +3 -7
@@ -132,8 +132,7 @@ cifs_bp_rename_retry:struct cifsFileInfo *cifs_new_fileinfo (struct inode *newinode,__u16 fileHandle,struct file *file, -struct vfsmount *mnt,struct tcon_link *tlink, -unsigned int oflags,__u32 oplock) +struct tcon_link *tlink,unsigned int oflags,__u32 oplock) {struct dentry *dentry = file->f_path.dentry;struct cifsFileInfo *pCifsFile;
@@ -147,7 +146,6 @@ cifs_new_fileinfo(struct inode *newinode, __u16 fileHandle, struct file *file, pCifsFile->pid = current->tgid; pCifsFile->uid =current_fsuid (); pCifsFile->dentry =dget (dentry); - pCifsFile->mnt = mnt; pCifsFile->pfile = file; pCifsFile->invalidHandle = false; pCifsFile->closePend = false;
@@ -485,8 +483,7 @@ cifs_create_set_dentry: } pfile_info =cifs_new_fileinfo (newinode, fileHandle, filp, - nd->path.mnt, tlink, oflags, - oplock); + tlink, oflags, oplock);if (pfile_info ==NULL ) {fput (filp);CIFSSMBClose (xid, tcon, fileHandle);
@@ -760,8 +757,7 @@ cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, } cfile =cifs_new_fileinfo (newInode, fileHandle, filp, - nd->path.mnt, tlink, - nd->intent.open.flags, + tlink, nd->intent.open.flags, oplock);if (cfile ==NULL ) {fput (filp);
fs/cifs/file.c +4 -5
@@ -282,7 +282,6 @@ int cifs_open(struct inode *inode, struct file *file) } pCifsFile =cifs_new_fileinfo (inode, netfid, file, - file->f_path.mnt, tlink, oflags, oplock);if (pCifsFile ==NULL ) {CIFSSMBClose (xid, tcon, netfid);
@@ -375,8 +374,8 @@ int cifs_open(struct inode *inode, struct file *file)if (rc !=0 ) goto out; - pCifsFile =cifs_new_fileinfo (inode, netfid, file, file->f_path.mnt, - tlink, file->f_flags, oplock); + pCifsFile =cifs_new_fileinfo (inode, netfid, file, tlink, + file->f_flags, oplock);if (pCifsFile ==NULL ) { rc = -ENOMEM ; goto out;
@@ -2381,14 +2380,14 @@ void cifs_oplock_break(struct work_struct *work)void cifs_oplock_break_get (struct cifsFileInfo *cfile) { -mntget (cfile->mnt); +cifs_sb_active (cfile->dentry->d_sb);cifsFileInfo_get (cfile); }void cifs_oplock_break_put (struct cifsFileInfo *cfile) { -mntput (cfile->mnt);cifsFileInfo_put (cfile); +cifs_sb_deactive (cfile->dentry->d_sb); }const struct address_space_operations cifs_addr_ops = {
Signature · 1 line
1.7.2.3
PATCH 3/15 cifs: eliminate cifs_posix_open_inode_helper
In reply to a message not in the archiveThis function is redundant. The only thing it does is set the canCache flags, but those get set in cifs_new_fileinfo anyway.
--- fs/cifs/file.c | 67 -------------------------------------------------------- 1 files changed, 0 insertions(+), 67 deletions(-)
fs/cifs/file.c +0 -67
@@ -106,64 +106,6 @@ static inline int cifs_get_disposition(unsigned int flags)return FILE_OPEN ; } -/* all arguments to this function must be checked for validity in caller */ -static inline int -cifs_posix_open_inode_helper (struct inode *inode,struct file *file, -struct cifsInodeInfo *pCifsInode,__u32 oplock, -u16 netfid) -{ - -write_lock (&GlobalSMBSeslock); - - pCifsInode =CIFS_I (file->f_path.dentry->d_inode); -if (pCifsInode ==NULL ) { -write_unlock (&GlobalSMBSeslock); -return -EINVAL ; - } - -if (pCifsInode->clientCanCacheRead) { -/* we have the inode open somewhere else -no need to discard cache data */ - goto psx_client_can_cache; - } - -/* BB FIXME need to fix this check to move it earlier into posix_open -BB fIX following section BB FIXME */ - -/* if not oplocked, invalidate inode pages if mtime or file -size changed */ -/* temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime)); -if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) && -(file->f_path.dentry->d_inode->i_size == -(loff_t)le64_to_cpu(buf->EndOfFile))) { -cFYI(1, "inode unchanged on server"); -} else { -if (file->f_path.dentry->d_inode->i_mapping) { -rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping); -if (rc != 0) -CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc; -} -cFYI(1, "invalidating remote inode since open detected it " -"changed"); -invalidate_remote_inode(file->f_path.dentry->d_inode); -} */ - -psx_client_can_cache: -if ((oplock &0xF ) ==OPLOCK_EXCLUSIVE ) { - pCifsInode->clientCanCacheAll = true; - pCifsInode->clientCanCacheRead = true; -cFYI (1 ,"Exclusive Oplock granted on inode %p" , - file->f_path.dentry->d_inode); - }else if ((oplock &0xF ) ==OPLOCK_READ ) - pCifsInode->clientCanCacheRead = true; - -/* will have to change the unlock if we reenable the -filemap_fdatawrite (which does not seem necessary */ -write_unlock (&GlobalSMBSeslock); -return 0 ; -} - -/* all arguments to this function must be checked for validity in caller */ static inline int cifs_open_inode_helper (struct inode *inode,struct cifsTconInfo *pTcon,__u32 oplock,FILE_ALL_INFO *buf,char *full_path,int xid)
@@ -271,15 +213,6 @@ int cifs_open(struct inode *inode, struct file *file) oflags, &oplock, &netfid, xid);if (rc ==0 ) {cFYI (1 ,"posix open succeeded" ); -/* no need for special case handling of setting mode -on read only files needed here */ - - rc =cifs_posix_open_inode_helper (inode, file, - pCifsInode, oplock, netfid); -if (rc !=0 ) { -CIFSSMBClose (xid, tcon, netfid); - goto out; - } pCifsFile =cifs_new_fileinfo (inode, netfid, file, tlink, oflags, oplock);
Signature · 1 line
1.7.2.3
PATCH 4/15 cifs: eliminate oflags option from cifs_new_fileinfo
In reply to a message not in the archiveEliminate the poor, misunderstood "oflags" option from cifs_new_fileinfo. The callers mostly pass in the filp->f_flags here, except for one place that passes in the flags after they've been converted to their SMB_O_* equivalents.
None of these are correct however since we're checking that value for the presence of FMODE_READ. Luckily that only affects how the f_list is ordered.
What we really want here is the file->f_mode. Since we pass in a file pointer already anyway, just use the f_mode there for this purpose.
--- fs/cifs/cifsproto.h | 3 +-- fs/cifs/dir.c | 9 ++++----- fs/cifs/file.c | 5 ++--- 3 files changed, 7 insertions(+), 10 deletions(-)
fs/cifs/cifsproto.h +1 -2
@@ -107,8 +107,7 @@ extern struct timespec cnvrtDosUnixTm(__le16 le_date, __le16 le_time,extern struct cifsFileInfo *cifs_new_fileinfo (struct inode *newinode,__u16 fileHandle,struct file *file, -struct tcon_link *tlink, -unsigned int oflags,__u32 oplock); +struct tcon_link *tlink,__u32 oplock); extern int cifs_posix_open(char *full_path, struct inode **pinode, struct super_block *sb, int mode, int oflags,
fs/cifs/dir.c +4 -5
@@ -132,7 +132,7 @@ cifs_bp_rename_retry:struct cifsFileInfo *cifs_new_fileinfo (struct inode *newinode,__u16 fileHandle,struct file *file, -struct tcon_link *tlink,unsigned int oflags,__u32 oplock) +struct tcon_link *tlink,__u32 oplock) {struct dentry *dentry = file->f_path.dentry;struct cifsFileInfo *pCifsFile;
@@ -161,7 +161,7 @@ cifs_new_fileinfo(struct inode *newinode, __u16 fileHandle, struct file *file, pCifsInode =CIFS_I (newinode);if (pCifsInode) {/* if readable file instance put first in list*/ -if (oflags &FMODE_READ ) +if (file->f_mode &FMODE_READ )list_add (&pCifsFile->flist, &pCifsInode->openFileList);else list_add_tail(&pCifsFile->flist,
@@ -483,7 +483,7 @@ cifs_create_set_dentry: } pfile_info =cifs_new_fileinfo (newinode, fileHandle, filp, - tlink, oflags, oplock); + tlink, oplock);if (pfile_info ==NULL ) {fput (filp);CIFSSMBClose (xid, tcon, fileHandle);
@@ -757,8 +757,7 @@ cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, } cfile =cifs_new_fileinfo (newInode, fileHandle, filp, - tlink, nd->intent.open.flags, - oplock); + tlink, oplock);if (cfile ==NULL ) {fput (filp);CIFSSMBClose (xid, pTcon, fileHandle);
fs/cifs/file.c +2 -3
@@ -215,7 +215,7 @@ int cifs_open(struct inode *inode, struct file *file)cFYI (1 ,"posix open succeeded" ); pCifsFile =cifs_new_fileinfo (inode, netfid, file, - tlink, oflags, oplock); + tlink, oplock);if (pCifsFile ==NULL ) {CIFSSMBClose (xid, tcon, netfid); rc = -ENOMEM ;
@@ -307,8 +307,7 @@ int cifs_open(struct inode *inode, struct file *file)if (rc !=0 ) goto out; - pCifsFile =cifs_new_fileinfo (inode, netfid, file, tlink, - file->f_flags, oplock); + pCifsFile =cifs_new_fileinfo (inode, netfid, file, tlink, oplock);if (pCifsFile ==NULL ) { rc = -ENOMEM ; goto out;
Signature · 1 line
1.7.2.3
PATCH 5/15 cifs: eliminate the inode argument from cifs_new_fileinfo
In reply to a message not in the archiveIt already takes a file pointer. The inode associated with that had damn well better be the same one we're passing in anyway. Thus, there's no need for a separate argument here.
Also, get rid of the bogus check for a null pCifsInode pointer. The CIFS_I macro uses container_of(), and that will virtually never return a NULL pointer anyway.
--- fs/cifs/cifsproto.h | 3 +-- fs/cifs/dir.c | 42 ++++++++++++++++++++---------------------- fs/cifs/file.c | 6 +++--- 3 files changed, 24 insertions(+), 27 deletions(-)
fs/cifs/cifsproto.h +1 -2
@@ -105,8 +105,7 @@ extern u64 cifs_UnixTimeToNT(struct timespec);extern struct timespec cnvrtDosUnixTm (__le16 le_date,__le16 le_time,int offset); -extern struct cifsFileInfo *cifs_new_fileinfo (struct inode *newinode, -__u16 fileHandle,struct file *file, +extern struct cifsFileInfo *cifs_new_fileinfo (__u16 fileHandle,struct file *file,struct tcon_link *tlink,__u32 oplock); extern int cifs_posix_open(char *full_path, struct inode **pinode, struct super_block *sb,
fs/cifs/dir.c +20 -22
70 lines not on this page · raw message
fs/cifs/file.c +3 -3
@@ -214,8 +214,8 @@ int cifs_open(struct inode *inode, struct file *file)if (rc ==0 ) {cFYI (1 ,"posix open succeeded" ); - pCifsFile =cifs_new_fileinfo (inode, netfid, file, - tlink, oplock); + pCifsFile =cifs_new_fileinfo (netfid, file, tlink, + oplock);if (pCifsFile ==NULL ) {CIFSSMBClose (xid, tcon, netfid); rc = -ENOMEM ;
@@ -307,7 +307,7 @@ int cifs_open(struct inode *inode, struct file *file)if (rc !=0 ) goto out; - pCifsFile =cifs_new_fileinfo (inode, netfid, file, tlink, oplock); + pCifsFile =cifs_new_fileinfo (netfid, file, tlink, oplock);if (pCifsFile ==NULL ) { rc = -ENOMEM ; goto out;
Signature · 1 line
1.7.2.3
PATCH 6/15 cifs: clean up cifs_reopen_file
In reply to a message not in the archiveAdd a f_flags field that holds the f_flags field from the filp. We'll need this info in case the filp ever goes away before the cifsFileInfo does. Have cifs_reopen_file use that value instead of filp->f_flags too and have it take a cifsFileInfo arg instead of a filp.
While we're at it, get rid of some bogus cargo-cult NULL pointer checks in that function and reduce the level of indentation.
--- fs/cifs/cifsglob.h | 1 + fs/cifs/dir.c | 1 + fs/cifs/file.c | 128 ++++++++++++++++++++++------------------------------ 3 files changed, 56 insertions(+), 74 deletions(-)
fs/cifs/cifsglob.h +1 -0
@@ -388,6 +388,7 @@ struct cifsFileInfo {/* lock scope id (0 if none) */ struct file *pfile;/* needed for writepage */ struct dentry *dentry; +unsigned int f_flags;struct tcon_link *tlink;struct mutex lock_mutex;struct list_head llist;/* list of byte range locks we have. */
fs/cifs/dir.c +1 -0
@@ -147,6 +147,7 @@ cifs_new_fileinfo(__u16 fileHandle, struct file *file, pCifsFile->pid = current->tgid; pCifsFile->uid =current_fsuid (); pCifsFile->dentry =dget (dentry); + pCifsFile->f_flags = file->f_flags; pCifsFile->pfile = file; pCifsFile->invalidHandle = false; pCifsFile->closePend = false;
fs/cifs/file.c +54 -74
222 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 7/15 cifs: cifs_write argument change and cleanup
In reply to a message not in the archiveHave cifs_write take a cifsFileInfo pointer instead of a filp. Since cifsFileInfo holds references on the dentry, and that holds one to the inode, we can eliminate some unneeded NULL pointer checks.
--- fs/cifs/file.c | 51 +++++++++++++++++---------------------------------- 1 files changed, 17 insertions(+), 34 deletions(-)
fs/cifs/file.c +17 -34
@@ -963,8 +963,9 @@ ssize_t cifs_user_write(struct file *file, const char __user *write_data,return total_written; } -static ssize_t cifs_write (struct file *file,const char *write_data, -size_t write_size,loff_t *poffset) +static ssize_t cifs_write (struct cifsFileInfo *open_file, +const char *write_data,size_t write_size, +loff_t *poffset) {int rc =0 ;unsigned int bytes_written =0 ;
@@ -972,17 +973,14 @@ static ssize_t cifs_write(struct file *file, const char *write_data,struct cifs_sb_info *cifs_sb;struct cifsTconInfo *pTcon;int xid, long_op; -struct cifsFileInfo *open_file; -struct cifsInodeInfo *cifsi =CIFS_I (file->f_path.dentry->d_inode); +struct dentry *dentry = open_file->dentry; +struct cifsInodeInfo *cifsi =CIFS_I (dentry->d_inode); - cifs_sb =CIFS_SB (file->f_path.dentry->d_sb); + cifs_sb =CIFS_SB (dentry->d_sb);cFYI (1 ,"write %zd bytes to offset %lld of %s" , write_size, - *poffset, file->f_path.dentry->d_name.name); + *poffset, dentry->d_name.name); -if (file->private_data ==NULL ) -return -EBADF ; - open_file = file->private_data; pTcon =tlink_tcon (open_file->tlink); xid =GetXid ();
@@ -992,15 +990,6 @@ static ssize_t cifs_write(struct file *file, const char *write_data, total_written += bytes_written) { rc = -EAGAIN ;while (rc == -EAGAIN ) { -if (file->private_data ==NULL ) { -/* file has been closed on us */ -FreeXid (xid); -/* if we have gotten here we have written some data -and blocked, and the file has been freed on us -while we blocked so return what we managed to -write */ -return total_written; - }if (open_file->closePend) {FreeXid (xid);if (total_written)
@@ -1060,20 +1049,13 @@ static ssize_t cifs_write(struct file *file, const char *write_data,cifs_stats_bytes_written (pTcon, total_written); -/* since the write may have blocked check these pointers again */ -if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) { -/*BB We could make this contingent on superblock ATIME flag too */ -/* file->f_path.dentry->d_inode->i_ctime = -file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/ -if (total_written >0 ) { -spin_lock (&file->f_path.dentry->d_inode->i_lock); -if (*poffset > file->f_path.dentry->d_inode->i_size) -i_size_write (file->f_path.dentry->d_inode, - *poffset); -spin_unlock (&file->f_path.dentry->d_inode->i_lock); - } -mark_inode_dirty_sync (file->f_path.dentry->d_inode); +if (total_written >0 ) { +spin_lock (&dentry->d_inode->i_lock); +if (*poffset > dentry->d_inode->i_size) +i_size_write (dentry->d_inode, *poffset); +spin_unlock (&dentry->d_inode->i_lock); } +mark_inode_dirty_sync (dentry->d_inode);FreeXid (xid);return total_written; }
@@ -1244,8 +1226,8 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) open_file =find_writable_file (CIFS_I (mapping->host), false);if (open_file) { - bytes_written =cifs_write (open_file->pfile, write_data, - to-from, &offset); + bytes_written =cifs_write (open_file, write_data, + to - from, &offset);cifsFileInfo_put (open_file);/* Does mm or vfs already set times? */ inode->i_atime = inode->i_mtime =current_fs_time (inode->i_sb);
@@ -1560,7 +1542,8 @@ static int cifs_write_end(struct file *file, struct address_space *mapping,/* BB check if anything else missing out of ppw such as updating last write time */ page_data =kmap (page); - rc =cifs_write (file, page_data + offset, copied, &pos); + rc =cifs_write (file->private_data, page_data + offset, + copied, &pos);/* if (rc < 0) should we set writebehind rc? */ kunmap (page);
Signature · 1 line
1.7.2.3
PATCH 8/15 cifs: eliminate pfile pointer from cifsFileInfo
In reply to a message not in the archiveAll the remaining users of cifsFileInfo->pfile just use it to get at the f_flags/f_mode. Now that we store that separately in the cifsFileInfo, there's no need to consult the pfile at all from a cifsFileInfo pointer.
--- fs/cifs/cifsglob.h | 1 - fs/cifs/dir.c | 1 - fs/cifs/file.c | 11 +++-------- 3 files changed, 3 insertions(+), 10 deletions(-)
fs/cifs/cifsglob.h +0 -1
@@ -386,7 +386,6 @@ struct cifsFileInfo {__u16 netfid;/* file id from remote */ /* BB add lock scope info here if needed */ ;/* lock scope id (0 if none) */ -struct file *pfile;/* needed for writepage */ struct dentry *dentry;unsigned int f_flags;struct tcon_link *tlink;
fs/cifs/dir.c +0 -1
@@ -148,7 +148,6 @@ cifs_new_fileinfo(__u16 fileHandle, struct file *file, pCifsFile->uid =current_fsuid (); pCifsFile->dentry =dget (dentry); pCifsFile->f_flags = file->f_flags; - pCifsFile->pfile = file; pCifsFile->invalidHandle = false; pCifsFile->closePend = false; pCifsFile->tlink =cifs_get_tlink (tlink);
fs/cifs/file.c +3 -8
@@ -1080,8 +1080,7 @@ struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,continue ;if (fsuid_only && open_file->uid !=current_fsuid ())continue ; -if (open_file->pfile && ((open_file->pfile->f_flags &O_RDWR ) || - (open_file->pfile->f_flags &O_RDONLY ))) { +if (OPEN_FMODE (open_file->f_flags) &FMODE_READ ) {if (!open_file->invalidHandle) {/* found a good file */ /* lock it so it will not be closed on us */
@@ -1130,9 +1129,7 @@ refind_writable:continue ;if (fsuid_only && open_file->uid !=current_fsuid ())continue ; -if (open_file->pfile && - ((open_file->pfile->f_flags &O_RDWR ) || - (open_file->pfile->f_flags &O_WRONLY ))) { +if (OPEN_FMODE (open_file->f_flags) &FMODE_WRITE ) {cifsFileInfo_get (open_file);if (!open_file->invalidHandle) {
@@ -2096,9 +2093,7 @@ static int is_inode_writable(struct cifsInodeInfo *cifs_inode)list_for_each_entry (open_file, &cifs_inode->openFileList, flist) {if (open_file->closePend)continue ; -if (open_file->pfile && - ((open_file->pfile->f_flags &O_RDWR ) || - (open_file->pfile->f_flags &O_WRONLY ))) { +if (OPEN_FMODE (open_file->f_flags) &FMODE_WRITE ) {read_unlock (&GlobalSMBSeslock);return 1 ; }
Signature · 1 line
1.7.2.3
PATCH 9/15 cifs: move cifs_new_fileinfo to file.c
In reply to a message not in the archiveIt's currently in dir.c which makes little sense...
--- fs/cifs/dir.c | 50 -------------------------------------------------- fs/cifs/file.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 50 deletions(-)
fs/cifs/dir.c +0 -50
@@ -130,56 +130,6 @@ cifs_bp_rename_retry:return full_path; } -struct cifsFileInfo * -cifs_new_fileinfo (__u16 fileHandle,struct file *file, -struct tcon_link *tlink,__u32 oplock) -{ -struct dentry *dentry = file->f_path.dentry; -struct inode *inode = dentry->d_inode; -struct cifsInodeInfo *pCifsInode =CIFS_I (inode); -struct cifsFileInfo *pCifsFile; - - pCifsFile =kzalloc (sizeof (struct cifsFileInfo ),GFP_KERNEL ); -if (pCifsFile ==NULL ) -return pCifsFile; - - pCifsFile->netfid = fileHandle; - pCifsFile->pid = current->tgid; - pCifsFile->uid =current_fsuid (); - pCifsFile->dentry =dget (dentry); - pCifsFile->f_flags = file->f_flags; - pCifsFile->invalidHandle = false; - pCifsFile->closePend = false; - pCifsFile->tlink =cifs_get_tlink (tlink); -mutex_init (&pCifsFile->fh_mutex); -mutex_init (&pCifsFile->lock_mutex); -INIT_LIST_HEAD (&pCifsFile->llist); -atomic_set (&pCifsFile->count,1 ); -INIT_WORK (&pCifsFile->oplock_break, cifs_oplock_break); - -write_lock (&GlobalSMBSeslock); -list_add (&pCifsFile->tlist, &(tlink_tcon (tlink)->openFileList)); - -/* if readable file instance put first in list*/ -if (file->f_mode &FMODE_READ ) -list_add (&pCifsFile->flist, &pCifsInode->openFileList); -else -list_add_tail (&pCifsFile->flist, &pCifsInode->openFileList); - -if ((oplock &0xF ) ==OPLOCK_EXCLUSIVE ) { - pCifsInode->clientCanCacheAll = true; - pCifsInode->clientCanCacheRead = true; -cFYI (1 ,"Exclusive Oplock inode %p" , inode); - }else if ((oplock &0xF ) ==OPLOCK_READ ) - pCifsInode->clientCanCacheRead = true; - -write_unlock (&GlobalSMBSeslock); - - file->private_data = pCifsFile; - -return pCifsFile; -} -int cifs_posix_open (char *full_path,struct inode **pinode,struct super_block *sb,int mode,int oflags,__u32 *poplock,__u16 *pnetfid,int xid)
fs/cifs/file.c +48 -0
54 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH 10/15 cifs: rename GlobalSMBSeslock to cifs_file_list_lock
In reply to a message not in the archiveI am never able to type the old name right on the first try. It's also a misleading name for the lock which really is used to protect the lists that a cifsFileInfo sits on.
--- fs/cifs/cifsfs.c | 2 +- fs/cifs/cifsglob.h | 2 +- fs/cifs/cifssmb.c | 4 +- fs/cifs/file.c | 54 ++++++++++++++++++++++++++-------------------------- fs/cifs/misc.c | 8 +++--- fs/cifs/readdir.c | 6 ++-- 6 files changed, 38 insertions(+), 38 deletions(-)
fs/cifs/cifsfs.c +1 -1
@@ -939,7 +939,7 @@ init_cifs(void) GlobalTotalActiveXid =0 ; GlobalMaxActiveXid =0 ;memset (Local_System_Name,0 ,15 ); -rwlock_init (&GlobalSMBSeslock); +rwlock_init (&cifs_file_list_lock);rwlock_init (&cifs_tcp_ses_lock);spin_lock_init (&GlobalMid_Lock);
fs/cifs/cifsglob.h +1 -1
@@ -720,7 +720,7 @@ GLOBAL_EXTERN rwlock_t cifs_tcp_ses_lock;* If cifs_tcp_ses_lock and the lock below are both needed to be held, then * the cifs_tcp_ses_lock must be grabbed first and released last. */ -GLOBAL_EXTERN rwlock_t GlobalSMBSeslock; +GLOBAL_EXTERN rwlock_t cifs_file_list_lock;/* Outstanding dir notify requests */ GLOBAL_EXTERN struct list_head GlobalDnotifyReqList;
fs/cifs/cifssmb.c +2 -2
@@ -91,13 +91,13 @@ static void mark_open_files_invalid(struct cifsTconInfo *pTcon)struct list_head *tmp1;/* list all files open on tree connection and mark them invalid */ -write_lock (&GlobalSMBSeslock); +write_lock (&cifs_file_list_lock);list_for_each_safe (tmp, tmp1, &pTcon->openFileList) { open_file = list_entry(tmp, struct cifsFileInfo, tlist); open_file->invalidHandle = true; open_file->oplock_break_cancelled = true; } -write_unlock (&GlobalSMBSeslock); +write_unlock (&cifs_file_list_lock);/* BB Add call to invalidate_inodes(sb) for all superblocks mounted to this tcon */ }
fs/cifs/file.c +27 -27
188 lines not on this page · raw message
fs/cifs/misc.c +4 -4
@@ -560,7 +560,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)continue ;cifs_stats_inc (&tcon->num_oplock_brks); -read_lock (&GlobalSMBSeslock); +read_lock (&cifs_file_list_lock);list_for_each (tmp2, &tcon->openFileList) { netfile = list_entry(tmp2, struct cifsFileInfo, tlist);
@@ -572,7 +572,7 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)* closed anyway. */ if (netfile->closePend) { -read_unlock (&GlobalSMBSeslock); +read_unlock (&cifs_file_list_lock);read_unlock (&cifs_tcp_ses_lock);return true; }
@@ -594,11 +594,11 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)cifs_oplock_break_get (netfile); netfile->oplock_break_cancelled = false; -read_unlock (&GlobalSMBSeslock); +read_unlock (&cifs_file_list_lock);read_unlock (&cifs_tcp_ses_lock);return true; } -read_unlock (&GlobalSMBSeslock); +read_unlock (&cifs_file_list_lock);read_unlock (&cifs_tcp_ses_lock);cFYI (1 ,"No matching file for oplock break" );return true;
fs/cifs/readdir.c +3 -3
@@ -529,14 +529,14 @@ static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon, (index_to_find < first_entry_in_buffer)) {/* close and restart search */ cFYI (1 ,"search backing up - close and restart search" ); -write_lock (&GlobalSMBSeslock); +write_lock (&cifs_file_list_lock);if (!cifsFile->srch_inf.endOfSearch && !cifsFile->invalidHandle) { cifsFile->invalidHandle = true; -write_unlock (&GlobalSMBSeslock); +write_unlock (&cifs_file_list_lock);CIFSFindClose (xid, pTcon, cifsFile->netfid); }else -write_unlock (&GlobalSMBSeslock); +write_unlock (&cifs_file_list_lock);if (cifsFile->srch_inf.ntwrk_buf_start) {cFYI (1 ,"freeing SMB ff cache buf on search rewind" );if (cifsFile->srch_inf.smallBuf)
Signature · 1 line
1.7.2.3
PATCH 11/15 cifs: move cifsFileInfo_put to file.c
In reply to a message not in the archive...and make it non-inlined in preparation for the move of most of cifs_close to it.
--- fs/cifs/cifsglob.h | 10 +--------- fs/cifs/file.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 9 deletions(-)
fs/cifs/cifsglob.h +1 -9
@@ -406,15 +406,7 @@ static inline void cifsFileInfo_get(struct cifsFileInfo *cifs_file)atomic_inc (&cifs_file->count); } -/* Release a reference on the file private data */ -static inline void cifsFileInfo_put (struct cifsFileInfo *cifs_file) -{ -if (atomic_dec_and_test (&cifs_file->count)) { -cifs_put_tlink (cifs_file->tlink); -dput (cifs_file->dentry); -kfree (cifs_file); - } -} +void cifsFileInfo_put (struct cifsFileInfo *cifs_file);/* * One of these for each file inode
fs/cifs/file.c +10 -0
@@ -207,6 +207,16 @@ cifs_new_fileinfo(__u16 fileHandle, struct file *file,return pCifsFile; } +/* Release a reference on the file private data */ +void cifsFileInfo_put (struct cifsFileInfo *cifs_file) +{ +if (atomic_dec_and_test (&cifs_file->count)) { +cifs_put_tlink (cifs_file->tlink); +dput (cifs_file->dentry); +kfree (cifs_file); + } +} +int cifs_open (struct inode *inode,struct file *file) {int rc = -EACCES ;
Signature · 1 line
1.7.2.3
PATCH 12/15 cifs: move close processing from cifs_close to cifsFileInfo_put
In reply to a message not in the archiveNow that it's feasible for a cifsFileInfo to outlive the filp under which it was created, move the close processing into cifsFileInfo_put.
This means that the last user of the filehandle always does the actual on the wire close call. This also allows us to get rid of the closePend flag from cifsFileInfo. If we have an active reference to the file then it's never going to have a close pending.
cifs_close is converted to simply put the filehandle.
--- fs/cifs/cifsglob.h | 1 - fs/cifs/file.c | 187 +++++++++++++++++----------------------------------- fs/cifs/misc.c | 10 --- 3 files changed, 60 insertions(+), 138 deletions(-)
fs/cifs/cifsglob.h +0 -1
@@ -391,7 +391,6 @@ struct cifsFileInfo {struct tcon_link *tlink;struct mutex lock_mutex;struct list_head llist;/* list of byte range locks we have. */ - bool closePend:1; /* file is marked to close */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1;atomic_t count;/* reference count */
fs/cifs/file.c +60 -127
275 lines not on this page · raw message
fs/cifs/misc.c +0 -10
@@ -567,16 +567,6 @@ is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)if (pSMB->Fid != netfile->netfid)continue ; -/* -* don't do anything if file is about to be -* closed anyway. -*/ -if (netfile->closePend) { -read_unlock (&cifs_file_list_lock); -read_unlock (&cifs_tcp_ses_lock); -return true; - } -cFYI (1 ,"file id match, oplock break" ); pCifsInode =CIFS_I (netfile->dentry->d_inode); pCifsInode->clientCanCacheAll = false;
Signature · 1 line
1.7.2.3
A message that is not in the archive (sent off-list or not yet received): 1286394857-32541-13-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Re: [PATCH 12/15] cifs: move close processing from cifs_close to cifsFileInfo_put
Quoted 90 lines · Jeff Layton
Now that it's feasible for a cifsFileInfo to outlive the filp under which it was created, move the close processing into
cifsFileInfo_put.This means that the last user of the filehandle always does the actual on the wire close call. This also allows us to get rid of the closePend flag from cifsFileInfo. If we have an active reference to the file then it's never going to have a close pending.
cifs_closeis converted to simply put the filehandle.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 1 - fs/cifs/file.c | 187 +++++++++++++++++----------------------------------- fs/cifs/misc.c | 10 --- 3 files changed, 60 insertions(+), 138 deletions(-)fs/cifs/file.c +46
-16-/* Release a reference on the file private data */ +/* +* Release a reference on the file private data. This may involve closing +* the filehandle out on the server. +*/ void cifsFileInfo_put (struct cifsFileInfo *cifs_file) { -if (atomic_dec_and_test (&cifs_file->count)) { -cifs_put_tlink (cifs_file->tlink); -dput (cifs_file->dentry); -kfree (cifs_file); +struct cifsTconInfo *tcon =tlink_tcon (cifs_file->tlink); +struct cifsInodeInfo *cifsi =CIFS_I (cifs_file->dentry->d_inode); +struct cifsLockInfo *li, *tmp; + +write_lock (&cifs_file_list_lock); +if (!atomic_dec_and_test (&cifs_file_list_lock)) { +write_unlock (&cifs_file_list_lock); +return ; + } + +/* remove it from the lists */ +list_del (&cifs_file->flist); +list_del (&cifs_file->tlist); + +if (list_empty (&cifsi->openFileList)) { +cFYI (1 ,"closing last open instance for inode %p" , + cifs_file->dentry->d_inode); + cifsi->clientCanCacheRead = false; + cifsi->clientCanCacheAll = false; + } +write_unlock (&cifs_file_list_lock); + +if (!tcon->need_reconnect && !cifs_file->invalidHandle) { +int xid, rc; + + xid =GetXid (); + rc =CIFSSMBClose (xid, tcon, cifs_file->netfid); +FreeXid (xid); + } + +/* Delete any outstanding lock records. We'll lose them when the file +* is closed anyway. +*/ +mutex_lock (&cifs_file->lock_mutex); +list_for_each_entry_safe (li, tmp, &cifs_file->llist, llist) { +list_del (&li->llist); +kfree (li); } +mutex_unlock (&cifs_file->lock_mutex); + +cifs_put_tlink (cifs_file->tlink); +dput (cifs_file->dentry); +kfree (cifs_file); }int cifs_open (struct inode *inode,struct file *file)@@ -542,79 +582,11 @@ reopen_error_exit:int cifs_close (struct inode *inode,struct file *file) { -int rc =0 ; -int xid, timeout; -struct cifs_sb_info *cifs_sb; -struct cifsTconInfo *pTcon; -struct cifsFileInfo *pSMBFile = file->private_data; - - xid =GetXid (); - - cifs_sb =CIFS_SB (inode->i_sb); - pTcon =tlink_tcon (pSMBFile->tlink); -if (pSMBFile) {
fs/cifs/file.c reopen_error_exit: · line 565
Quoted 10 lines
fs/cifs/file.c +0
-10-struct cifsLockInfo *li, *tmp; -write_lock (&cifs_file_list_lock); - pSMBFile->closePend = true; -if (pTcon) { -/* no sense reconnecting to close a file that is -already closed */ -if (!pTcon->need_reconnect) { -write_unlock (&cifs_file_list_lock); - timeout =2 ; - while ((atomic_read(&pSMBFile->count) != 1)
This portion of code which induces manual delay seems missing. While I'm not sure about the practical significance of this code, explaining why we do not need this in the change log would help?
Quoted 58 lines
fs/cifs/file.c +4
-51- && (timeout <= 2048)) { -/* Give write a better chance to get to -server ahead of the close. We do not -want to add a wait_q here as it would -increase the memory utilization as -the struct would be in each open file, -but this should give enough time to -clear the socket */ -cFYI (DBG2 ,"close delay, write pending" ); -msleep (timeout); - timeout *=4 ; - } -if (!pTcon->need_reconnect && - !pSMBFile->invalidHandle) - rc =CIFSSMBClose (xid, pTcon, - pSMBFile->netfid); - }else -write_unlock (&cifs_file_list_lock); - }else -write_unlock (&cifs_file_list_lock); - -/* Delete any outstanding lock records. -We'll lose them when the file is closed anyway. */ -mutex_lock (&pSMBFile->lock_mutex); -list_for_each_entry_safe (li, tmp, &pSMBFile->llist, llist) { -list_del (&li->llist); -kfree (li); - } -mutex_unlock (&pSMBFile->lock_mutex); +cifsFileInfo_put (file->private_data); + file->private_data =NULL ; -write_lock (&cifs_file_list_lock); -list_del (&pSMBFile->flist); -list_del (&pSMBFile->tlist); -write_unlock (&cifs_file_list_lock); -cifsFileInfo_put (file->private_data); - file->private_data =NULL ; - }else - rc = -EBADF ; - -read_lock (&cifs_file_list_lock); -if (list_empty (&(CIFS_I (inode)->openFileList))) { -cFYI (1 ,"closing last open instance for inode %p" , inode); -/* if the file is not open we do not know if we can cache info -on this inode, much less write behind and read ahead */ -CIFS_I (inode)->clientCanCacheRead = false; -CIFS_I (inode)->clientCanCacheAll = false; - } -read_unlock (&cifs_file_list_lock); -if ((rc ==0 ) &&CIFS_I (inode)->write_behind_rc) - rc =CIFS_I (inode)->write_behind_rc; -FreeXid (xid); -return rc; +/* return code from the ->release op is always ignored */ +return 0 ; }
Signature · 1 line
Suresh Jayaraman
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 12/15] cifs: move close processing from cifs_close to cifsFileInfo_put
Quoted 97 lines · On Thu, 07 Oct 2010 12:37:19 +0530 Suresh Jayaraman…
Quoted 96 lines · Jeff Layton
Now that it's feasible for a cifsFileInfo to outlive the filp under which it was created, move the close processing into
cifsFileInfo_put.This means that the last user of the filehandle always does the actual on the wire close call. This also allows us to get rid of the closePend flag from cifsFileInfo. If we have an active reference to the file then it's never going to have a close pending.
cifs_closeis converted to simply put the filehandle.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 1 - fs/cifs/file.c | 187 +++++++++++++++++----------------------------------- fs/cifs/misc.c | 10 --- 3 files changed, 60 insertions(+), 138 deletions(-)fs/cifs/file.c +46
-22-/* Release a reference on the file private data */ +/* +* Release a reference on the file private data. This may involve closing +* the filehandle out on the server. +*/ void cifsFileInfo_put (struct cifsFileInfo *cifs_file) { -if (atomic_dec_and_test (&cifs_file->count)) { -cifs_put_tlink (cifs_file->tlink); -dput (cifs_file->dentry); -kfree (cifs_file); +struct cifsTconInfo *tcon =tlink_tcon (cifs_file->tlink); +struct cifsInodeInfo *cifsi =CIFS_I (cifs_file->dentry->d_inode); +struct cifsLockInfo *li, *tmp; + +write_lock (&cifs_file_list_lock); +if (!atomic_dec_and_test (&cifs_file_list_lock)) { +write_unlock (&cifs_file_list_lock); +return ; + } + +/* remove it from the lists */ +list_del (&cifs_file->flist); +list_del (&cifs_file->tlist); + +if (list_empty (&cifsi->openFileList)) { +cFYI (1 ,"closing last open instance for inode %p" , + cifs_file->dentry->d_inode); + cifsi->clientCanCacheRead = false; + cifsi->clientCanCacheAll = false; + } +write_unlock (&cifs_file_list_lock); + +if (!tcon->need_reconnect && !cifs_file->invalidHandle) { +int xid, rc; + + xid =GetXid (); + rc =CIFSSMBClose (xid, tcon, cifs_file->netfid); +FreeXid (xid); + } + +/* Delete any outstanding lock records. We'll lose them when the file +* is closed anyway. +*/ +mutex_lock (&cifs_file->lock_mutex); +list_for_each_entry_safe (li, tmp, &cifs_file->llist, llist) { +list_del (&li->llist); +kfree (li); } +mutex_unlock (&cifs_file->lock_mutex); + +cifs_put_tlink (cifs_file->tlink); +dput (cifs_file->dentry); +kfree (cifs_file); }int cifs_open (struct inode *inode,struct file *file)@@ -542,79 +582,11 @@ reopen_error_exit:int cifs_close (struct inode *inode,struct file *file) { -int rc =0 ; -int xid, timeout; -struct cifs_sb_info *cifs_sb; -struct cifsTconInfo *pTcon; -struct cifsFileInfo *pSMBFile = file->private_data; - - xid =GetXid (); - - cifs_sb =CIFS_SB (inode->i_sb); - pTcon =tlink_tcon (pSMBFile->tlink); -if (pSMBFile) { -struct cifsLockInfo *li, *tmp; -write_lock (&cifs_file_list_lock); - pSMBFile->closePend = true; -if (pTcon) { -/* no sense reconnecting to close a file that is -already closed */
Quoted 9 lines
fs/cifs/file.c reopen_error_exit: · line 565
Quoted 4 lines
fs/cifs/file.c +0
-4-if (!pTcon->need_reconnect) { -write_unlock (&cifs_file_list_lock); - timeout =2 ; - while ((atomic_read(&pSMBFile->count) != 1)This portion of code which induces manual delay seems missing. While I'm not sure about the practical significance of this code, explaining why we do not need this in the change log would help?
This is the part of the code that I was particularly keen to eliminate.
To my understanding the idea was that the code would set the closePend flag and then wait a "little while" until the count had gone to 0. The closePend flag would prevent the find_*_file interfaces from picking that filehandle, and the "little while" would help ensure that the current users of it would finish with it. Eventually though (in 2s) the code gives up and just destroys the cifsFileInfo anyway even though the refcount is still high.
Needless to say, that's not a particularly robust scheme. It's possible the refcount went high just before the server went offline but before closePend was set. 2s may not be enough to prevent a use-after-free.
It also had the problem that the actual users of the filehandle had to deal with the fact that the closePend flag could be set while they were using it. Then what do they do?
The patch I'm proposing moves this to a real refcounted scheme where the last user closes out the filehandle on the server. Far simpler and more certain not to cause these sorts of problems.
Quoted 63 lines
Quoted 59 lines
fs/cifs/file.c +4
-51- && (timeout <= 2048)) { -/* Give write a better chance to get to -server ahead of the close. We do not -want to add a wait_q here as it would -increase the memory utilization as -the struct would be in each open file, -but this should give enough time to -clear the socket */ -cFYI (DBG2 ,"close delay, write pending" ); -msleep (timeout); - timeout *=4 ; - } -if (!pTcon->need_reconnect && - !pSMBFile->invalidHandle) - rc =CIFSSMBClose (xid, pTcon, - pSMBFile->netfid); - }else -write_unlock (&cifs_file_list_lock); - }else -write_unlock (&cifs_file_list_lock); - -/* Delete any outstanding lock records. -We'll lose them when the file is closed anyway. */ -mutex_lock (&pSMBFile->lock_mutex); -list_for_each_entry_safe (li, tmp, &pSMBFile->llist, llist) { -list_del (&li->llist); -kfree (li); - } -mutex_unlock (&pSMBFile->lock_mutex); +cifsFileInfo_put (file->private_data); + file->private_data =NULL ; -write_lock (&cifs_file_list_lock); -list_del (&pSMBFile->flist); -list_del (&pSMBFile->tlist); -write_unlock (&cifs_file_list_lock); -cifsFileInfo_put (file->private_data); - file->private_data =NULL ; - }else - rc = -EBADF ; - -read_lock (&cifs_file_list_lock); -if (list_empty (&(CIFS_I (inode)->openFileList))) { -cFYI (1 ,"closing last open instance for inode %p" , inode); -/* if the file is not open we do not know if we can cache info -on this inode, much less write behind and read ahead */ -CIFS_I (inode)->clientCanCacheRead = false; -CIFS_I (inode)->clientCanCacheAll = false; - } -read_unlock (&cifs_file_list_lock); -if ((rc ==0 ) &&CIFS_I (inode)->write_behind_rc) - rc =CIFS_I (inode)->write_behind_rc; -FreeXid (xid); -return rc; +/* return code from the ->release op is always ignored */ +return 0 ; }Signature · 1 line
Suresh Jayaraman
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101007070726.0ef1b1a8-xSBYVWDuneFaJnirhKH9O4GKTjYczspe@public.gmane.org
Re: [PATCH 12/15] cifs: move close processing from cifs_close to cifsFileInfo_put
Quoted 67 lines · Jeff Layton
Quoted 40 lines · On Thu, 07 Oct 2010 12:37:19 +0530 Suresh Jayaraman…
Quoted 39 lines · Jeff Layton
Now that it's feasible for a cifsFileInfo to outlive the filp under which it was created, move the close processing into
cifsFileInfo_put.This means that the last user of the filehandle always does the actual on the wire close call. This also allows us to get rid of the closePend flag from cifsFileInfo. If we have an active reference to the file then it's never going to have a close pending.
cifs_closeis converted to simply put the filehandle.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 1 - fs/cifs/file.c | 187 +++++++++++++++++----------------------------------- fs/cifs/misc.c | 10 --- 3 files changed, 60 insertions(+), 138 deletions(-)int
cifs_open(struct inode *inode, struct file *file)fs/cifs/file.c +0
-17@@ -542,79 +582,11 @@ reopen_error_exit:int cifs_close (struct inode *inode,struct file *file) { -int rc =0 ; -int xid, timeout; -struct cifs_sb_info *cifs_sb; -struct cifsTconInfo *pTcon; -struct cifsFileInfo *pSMBFile = file->private_data; - - xid =GetXid (); - - cifs_sb =CIFS_SB (inode->i_sb); - pTcon =tlink_tcon (pSMBFile->tlink); -if (pSMBFile) { -struct cifsLockInfo *li, *tmp; -write_lock (&cifs_file_list_lock); - pSMBFile->closePend = true; -if (pTcon) { -/* no sense reconnecting to close a file that is -already closed */ Quoted 9 lines
fs/cifs/file.c reopen_error_exit: · line 565
Quoted 4 lines
fs/cifs/file.c +0
-4-if (!pTcon->need_reconnect) { -write_unlock (&cifs_file_list_lock); - timeout =2 ; - while ((atomic_read(&pSMBFile->count) != 1)This portion of code which induces manual delay seems missing. While I'm not sure about the practical significance of this code, explaining why we do not need this in the change log would help?
This is the part of the code that I was particularly keen to eliminate.
To my understanding the idea was that the code would set the closePend flag and then wait a "little while" until the count had gone to 0. The closePend flag would prevent the find_*_file interfaces from picking that filehandle, and the "little while" would help ensure that the current users of it would finish with it. Eventually though (in 2s) the code gives up and just destroys the cifsFileInfo anyway even though the refcount is still high.
Needless to say, that's not a particularly robust scheme. It's possible the refcount went high just before the server went offline but before closePend was set. 2s may not be enough to prevent a use-after-free.
Quoted 7 lines
It also had the problem that the actual users of the filehandle had to deal with the fact that the closePend flag could be set while they were using it. Then what do they do?
The patch I'm proposing moves this to a real refcounted scheme where the last user closes out the filehandle on the server. Far simpler and more certain not to cause these sorts of problems.
Sounds good and makes sense to me.
Signature · 1 line
Suresh Jayaraman
PATCH 13/15 cifs: convert cifsFileInfo->count to non-atomic counter
In reply to a message not in the archiveThe count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold cifs_file_list_lock to traverse the lists anyway so we might as well make this counter non-atomic and simply use the cifs_file_list_lock to protect it.
--- fs/cifs/cifsglob.h | 9 ++++++--- fs/cifs/file.c | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-)
fs/cifs/cifsglob.h +6 -3
@@ -393,16 +393,19 @@ struct cifsFileInfo {struct list_head llist;/* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1; -atomic_t count;/* reference count */ +int count;/* refcount -- protected by cifs_file_list_lock */ struct mutex fh_mutex;/* prevents reopen race after dead ses*/ struct cifs_search_info srch_inf;struct work_struct oplock_break;/* work for oplock breaks */ }; -/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void cifsFileInfo_get (struct cifsFileInfo *cifs_file) { -atomic_inc (&cifs_file->count); + ++cifs_file->count; }void cifsFileInfo_put (struct cifsFileInfo *cifs_file);
fs/cifs/file.c +5 -3
@@ -172,6 +172,7 @@ cifs_new_fileinfo(__u16 fileHandle, struct file *file,if (pCifsFile ==NULL )return pCifsFile; + pCifsFile->count =1 ; pCifsFile->netfid = fileHandle; pCifsFile->pid = current->tgid; pCifsFile->uid =current_fsuid ();
@@ -182,7 +183,6 @@ cifs_new_fileinfo(__u16 fileHandle, struct file *file,mutex_init (&pCifsFile->fh_mutex);mutex_init (&pCifsFile->lock_mutex);INIT_LIST_HEAD (&pCifsFile->llist); -atomic_set (&pCifsFile->count,1 );INIT_WORK (&pCifsFile->oplock_break, cifs_oplock_break);write_lock (&cifs_file_list_lock);
@@ -208,7 +208,8 @@ cifs_new_fileinfo(__u16 fileHandle, struct file *file,/* * Release a reference on the file private data. This may involve closing -* the filehandle out on the server. +* the filehandle out on the server. Must be called without holding +* cifs_file_list_lock. */ void cifsFileInfo_put (struct cifsFileInfo *cifs_file) {
@@ -217,7 +218,7 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file)struct cifsLockInfo *li, *tmp;write_lock (&cifs_file_list_lock); -if (!atomic_dec_and_test (&cifs_file_list_lock)) { +if (--cifs_file->count >0 ) {write_unlock (&cifs_file_list_lock);return ; }
@@ -2259,6 +2260,7 @@ void cifs_oplock_break(struct work_struct *work)cifs_oplock_break_put (cfile); } +/* must be called while holding cifs_file_list_lock for read or write */ void cifs_oplock_break_get (struct cifsFileInfo *cfile) {cifs_sb_active (cfile->dentry->d_sb);
Signature · 1 line
1.7.2.3
A message that is not in the archive (sent off-list or not yet received): 1286394857-32541-14-git-send-email-jlayton-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Quoted 25 lines · Jeff Layton
The count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold
cifs_file_list_lockto traverse the lists anyway so we might as well make this counter non-atomic and simply use thecifs_file_list_lockto protect it.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 9 ++++++--- fs/cifs/file.c | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-)fs/cifs/cifsglob.h +1
-1@@ -393,16 +393,19 @@ struct cifsFileInfo {struct list_head llist;/* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1; -atomic_t count;/* reference count */ +int count;/* refcount -- protected by cifs_file_list_lock */ struct mutex fh_mutex;/* prevents reopen race after dead ses*/ struct cifs_search_info srch_inf;struct work_struct oplock_break;/* work for oplock breaks */ };
fs/cifs/cifsglob.h struct cifsFileInfo { · line 408
Quoted 10 lines
fs/cifs/cifsglob.h +5
-2-/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void cifsFileInfo_get (struct cifsFileInfo *cifs_file) { -atomic_inc (&cifs_file->count); + ++cifs_file->count;
Since we now use cifs_file_list_lock to protect cifs_file->count too, shouldn't all the callers of cifsFileInfo_get() need to acquire a write lock instead of read lock?
Signature · 1 line
Suresh Jayaraman
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Quoted 27 lines · On Thu, 07 Oct 2010 14:18:50 +0530 Suresh Jayaraman…
Quoted 26 lines · Jeff Layton
The count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold
cifs_file_list_lockto traverse the lists anyway so we might as well make this counter non-atomic and simply use thecifs_file_list_lockto protect it.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 9 ++++++--- fs/cifs/file.c | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-)fs/cifs/cifsglob.h +1
-1@@ -393,16 +393,19 @@ struct cifsFileInfo {struct list_head llist;/* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1; -atomic_t count;/* reference count */ +int count;/* refcount -- protected by cifs_file_list_lock */ struct mutex fh_mutex;/* prevents reopen race after dead ses*/ struct cifs_search_info srch_inf;struct work_struct oplock_break;/* work for oplock breaks */ };
Quoted 14 lines
fs/cifs/cifsglob.h struct cifsFileInfo { · line 408
Quoted 9 lines
fs/cifs/cifsglob.h +5
-2-/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void cifsFileInfo_get (struct cifsFileInfo *cifs_file) { -atomic_inc (&cifs_file->count); + ++cifs_file->count;Since we now use
cifs_file_list_lockto protectcifs_file->counttoo, shouldn't all the callers ofcifsFileInfo_get()need to acquire a write lock instead of read lock?
I puzzled over that too, but it's not really necessary. The lock is really there to make sure that the list traversals are safe from removals. We do need the write lock when inserting or removing the cifsFileInfo into the lists, but for incrementing a non-zero refcount a read lock is sufficient. That will prevent cifsFileInfo_put from racing in, decrementing the refcount and modifying the list since it needs a write lock.
TBH, I think it would be far simpler (and maybe even more efficient) to use a regular spinlock here, but I figured this would be easier to get past review.
Thoughts?
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101007071819.2446312b-xSBYVWDuneFaJnirhKH9O4GKTjYczspe@public.gmane.org
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Quoted 45 lines · Jeff Layton
Quoted 27 lines · On Thu, 07 Oct 2010 14:18:50 +0530 Suresh Jayaraman…
Quoted 26 lines · Jeff Layton
The count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold
cifs_file_list_lockto traverse the lists anyway so we might as well make this counter non-atomic and simply use thecifs_file_list_lockto protect it.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 9 ++++++--- fs/cifs/file.c | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-)fs/cifs/cifsglob.h +1
-1@@ -393,16 +393,19 @@ struct cifsFileInfo {struct list_head llist;/* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1; -atomic_t count;/* reference count */ +int count;/* refcount -- protected by cifs_file_list_lock */ struct mutex fh_mutex;/* prevents reopen race after dead ses*/ struct cifs_search_info srch_inf;struct work_struct oplock_break;/* work for oplock breaks */ };Quoted 14 lines
fs/cifs/cifsglob.h struct cifsFileInfo { · line 408
Quoted 9 lines
fs/cifs/cifsglob.h +5
-2-/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void cifsFileInfo_get (struct cifsFileInfo *cifs_file) { -atomic_inc (&cifs_file->count); + ++cifs_file->count;Since we now use
cifs_file_list_lockto protectcifs_file->counttoo, shouldn't all the callers ofcifsFileInfo_get()need to acquire a write lock instead of read lock?
Quoted 7 lines
I puzzled over that too, but it's not really necessary. The lock is really there to make sure that the list traversals are safe from removals. We do need the write lock when inserting or removing the cifsFileInfo into the lists, but for incrementing a non-zero refcount a read lock is sufficient. That will prevent
cifsFileInfo_putfrom racing in, decrementing the refcount and modifying the list since it needs a write lock.
Fine, but, isn't possible to get the refcount wrong due to calls to cifsFileInfo_get() from multiple callers?
Quoted 4 lines
TBH, I think it would be far simpler (and maybe even more efficient) to use a regular spinlock here, but I figured this would be easier to get past review.
I'm little worried about the overhead as I'm not sure whether all the paths are low contention paths...
Signature · 1 line
Suresh Jayaraman
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Quoted 54 lines · On Thu, 07 Oct 2010 17:42:57 +0530 Suresh Jayaraman…
Quoted 45 lines · Jeff Layton
Quoted 27 lines · On Thu, 07 Oct 2010 14:18:50 +0530 Suresh Jayaraman…
Quoted 26 lines · Jeff Layton
The count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold
cifs_file_list_lockto traverse the lists anyway so we might as well make this counter non-atomic and simply use thecifs_file_list_lockto protect it.Signed-off-by Jeff Layton <[email protected]>--- fs/cifs/cifsglob.h | 9 ++++++--- fs/cifs/file.c | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-)fs/cifs/cifsglob.h +1
-1@@ -393,16 +393,19 @@ struct cifsFileInfo {struct list_head llist;/* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1; -atomic_t count;/* reference count */ +int count;/* refcount -- protected by cifs_file_list_lock */ struct mutex fh_mutex;/* prevents reopen race after dead ses*/ struct cifs_search_info srch_inf;struct work_struct oplock_break;/* work for oplock breaks */ };Quoted 14 lines
fs/cifs/cifsglob.h struct cifsFileInfo { · line 408
Quoted 9 lines
fs/cifs/cifsglob.h +5
-2-/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void cifsFileInfo_get (struct cifsFileInfo *cifs_file) { -atomic_inc (&cifs_file->count); + ++cifs_file->count;Since we now use
cifs_file_list_lockto protectcifs_file->counttoo, shouldn't all the callers ofcifsFileInfo_get()need to acquire a write lock instead of read lock?Quoted 7 lines
I puzzled over that too, but it's not really necessary. The lock is really there to make sure that the list traversals are safe from removals. We do need the write lock when inserting or removing the cifsFileInfo into the lists, but for incrementing a non-zero refcount a read lock is sufficient. That will prevent
cifsFileInfo_putfrom racing in, decrementing the refcount and modifying the list since it needs a write lock.
Quoted 3 lines
Fine, but, isn't possible to get the refcount wrong due to calls to
cifsFileInfo_get()from multiple callers?
Argh...good point. Maybe we do need to make this a regular spinlock after all. The times you can use a read lock safely are pretty limited here.
Quoted 8 lines
Quoted 4 lines
TBH, I think it would be far simpler (and maybe even more efficient) to use a regular spinlock here, but I figured this would be easier to get past review.
I'm little worried about the overhead as I'm not sure whether all the paths are low contention paths...
I think they are generally low contention paths. Also, the lists are generally very short so the time the lock is held is too. Unless most of your accesses (like 90%) are read locked, then it's actually worse to use a rwlock than a regular spinlock.
Even if a regular spinlock adds some overhead, it's really unlikely to make any real difference in performance. CIFS is almost always bound by network latency, not CPU time.
Steve, I mostly left this as a rwlock as I figured you might have issues moving this to a normal spinlock. Would you?
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101007084334.0e03f586-xSBYVWDuneFaJnirhKH9O4GKTjYczspe@public.gmane.org
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Quoted 79 lines · Jeff Layton
Quoted 54 lines · On Thu, 07 Oct 2010 17:42:57 +0530 Suresh Jayaraman…
Quoted 45 lines · Jeff Layton
Quoted 27 lines · On Thu, 07 Oct 2010 14:18:50 +0530 Suresh Jayaraman…
Quoted 26 lines · Jeff Layton
The count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold
cifs_file_list_lockto traverse the lists anyway so we might as well make this counter non-atomic and simply use thecifs_file_list_lockto protect it.Signed-off-by Jeff Layton <[email protected]>---
fs/cifs/cifsglob.h | 9 ++++++---
fs/cifs/file.c | 8 +++++---2 files changed, 11 insertions(+), 6 deletions(-)fs/cifs/cifsglob.h +0
-0@@ -393,16 +393,19 @@ struct cifsFileInfo {struct
list_headllist; /* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1;
-atomic_tcount; /* reference count */
+ int count; /* refcount -- protected bycifs_file_list_lock*/ struct mutexfh_mutex; /* prevents reopen race after dead ses*/ structcifs_search_infosrch_inf;
structwork_structoplock_break; /* work for oplock breaks */ };Quoted 14 lines
Quoted 9 lines
fs/cifs/cifsglob.h +4
-1-/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void
cifsFileInfo_get(struct cifsFileInfo *cifs_file){
-atomic_inc(&cifs_file->count);
+ ++cifs_file->count;Since we now use
cifs_file_list_lockto protectcifs_file->counttoo, shouldn't all the callers ofcifsFileInfo_get()need to acquire a write lock instead of read lock?Quoted 7 lines
I puzzled over that too, but it's not really necessary. The lock is really there to make sure that the list traversals are safe from removals. We do need the write lock when inserting or removing the cifsFileInfo into the lists, but for incrementing a non-zero refcount a read lock is sufficient. That will prevent
cifsFileInfo_putfrom racing in, decrementing the refcount and modifying the list since it needs a write lock.Quoted 3 lines
Fine, but, isn't possible to get the refcount wrong due to calls to
cifsFileInfo_get()from multiple callers?Argh...good point. Maybe we do need to make this a regular spinlock after all. The times you can use a read lock safely are pretty limited here.
Quoted 8 lines
Quoted 4 lines
TBH, I think it would be far simpler (and maybe even more efficient) to use a regular spinlock here, but I figured this would be easier to get past review.
I'm little worried about the overhead as I'm not sure whether all the paths are low contention paths...
I think they are generally low contention paths. Also, the lists are generally very short so the time the lock is held is too. Unless most of your accesses (like 90%) are read locked, then it's actually worse to use a rwlock than a regular spinlock.
Quoted 6 lines
Even if a regular spinlock adds some overhead, it's really unlikely to make any real difference in performance. CIFS is almost always bound by network latency, not CPU time.
Steve, I mostly left this as a rwlock as I figured you might have issues moving this to a normal spinlock. Would you?
I doubt that a spinlock vs. a rwlock makes much difference, but a rwlock lock for list insertion/removal seems more intuitive and overloading it to also protect a counter adds complexity. Seems more natural to leave it as an atomic counter.
Signature · 3 lines
Thanks, Steve
A message that is not in the archive (sent off-list or not yet received): AANLkTi=MMzU6nr6+19PKW=gPKTdk9e-O5pjrYWmbV4AJ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Quoted 84 lines · On Thu, 7 Oct 2010 10:37:54 -0500 Steve French…
Quoted 83 lines · Jeff Layton
Quoted 54 lines · On Thu, 07 Oct 2010 17:42:57 +0530 Suresh Jayaraman…
Quoted 45 lines · Jeff Layton
Quoted 27 lines · On Thu, 07 Oct 2010 14:18:50 +0530 Suresh Jayaraman…
Quoted 26 lines · Jeff Layton
The count for cifsFileInfo is currently an atomic, but that just adds complexity for little value. We generally need to hold
cifs_file_list_lockto traverse the lists anyway so we might as well make this counter non-atomic and simply use thecifs_file_list_lockto protect it.Signed-off-by Jeff Layton <[email protected]>---
fs/cifs/cifsglob.h | 9 ++++++---
fs/cifs/file.c | 8 +++++---2 files changed, 11 insertions(+), 6 deletions(-)fs/cifs/cifsglob.h +0
-0@@ -393,16 +393,19 @@ struct cifsFileInfo {struct
list_headllist; /* list of byte range locks we have. */ bool invalidHandle:1; /* file closed via session abend */ bool oplock_break_cancelled:1;
-atomic_tcount; /* reference count */
+ int count; /* refcount -- protected bycifs_file_list_lock*/ struct mutexfh_mutex; /* prevents reopen race after dead ses*/ structcifs_search_infosrch_inf;
structwork_structoplock_break; /* work for oplock breaks */ };Quoted 14 lines
Quoted 9 lines
fs/cifs/cifsglob.h +4
-1-/* Take a reference on the file private data */ +/* +* Take a reference on the file private data. Must be called with +* cifs_file_list_lock held for read or write. +*/ static inline void
cifsFileInfo_get(struct cifsFileInfo *cifs_file){
-atomic_inc(&cifs_file->count);
+ ++cifs_file->count;Since we now use
cifs_file_list_lockto protectcifs_file->counttoo, shouldn't all the callers ofcifsFileInfo_get()need to acquire a write lock instead of read lock?Quoted 7 lines
I puzzled over that too, but it's not really necessary. The lock is really there to make sure that the list traversals are safe from removals. We do need the write lock when inserting or removing the cifsFileInfo into the lists, but for incrementing a non-zero refcount a read lock is sufficient. That will prevent
cifsFileInfo_putfrom racing in, decrementing the refcount and modifying the list since it needs a write lock.Quoted 3 lines
Fine, but, isn't possible to get the refcount wrong due to calls to
cifsFileInfo_get()from multiple callers?Argh...good point. Maybe we do need to make this a regular spinlock after all. The times you can use a read lock safely are pretty limited here.
Quoted 8 lines
Quoted 4 lines
TBH, I think it would be far simpler (and maybe even more efficient) to use a regular spinlock here, but I figured this would be easier to get past review.
I'm little worried about the overhead as I'm not sure whether all the paths are low contention paths...
I think they are generally low contention paths. Also, the lists are generally very short so the time the lock is held is too. Unless most of your accesses (like 90%) are read locked, then it's actually worse to use a rwlock than a regular spinlock.
Even if a regular spinlock adds some overhead, it's really unlikely to make any real difference in performance. CIFS is almost always bound by network latency, not CPU time.
Quoted 8 lines
Quoted 2 lines
Steve, I mostly left this as a rwlock as I figured you might have issues moving this to a normal spinlock. Would you?
I doubt that a spinlock vs. a rwlock makes much difference, but a rwlock lock for list insertion/removal seems more intuitive and overloading it to also protect a counter adds complexity. Seems more natural to leave it as an atomic counter.
Unfortunately, it's not that simple. When the count goes to 0 on a put, you need to atomically take the lock that protects the lists so that you can safely remove it from those lists. You can't do that with a rwlock_t.
It gets worse though -- if the atomic_t refcount can be increased while you're holding the lock, then you need to check that refcount twice. Once to get the lock, and then again to make sure it didn't change while you were spinning on it.
For instance:
+ if (!atomic_dec_and_lock(&cifs_file->count, &cifs_file_list_lock))
+ return;
+
+ /* count can be incremented inside the lock, so must check again */
+ if (atomic_read(&cifs_file->count) > 0) {
+ spin_unlock(&cifs_file_list_lock);
+ return;
+ }
If we don't want to use atomics then we can go with a rwlock_t, but then you need to ensure that any time you could increment the refcount that you take a write_lock. That's almost all of the code that runs under this lock. There will only be one or two seldom-used functions that are able to use a read_lock at that point. rwlocks have more overhead than simple spinlocks so again that's a loss.
Based on the above, I would argue that the most clear way to do this is to use a simple spinlock and a simple (non-atomic) counter that is protected by that lock.
Thoughts?
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20101007125932.4506f3e6-xSBYVWDuneFaJnirhKH9O4GKTjYczspe@public.gmane.org
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
Review
Quoted 12 lines · On Thu, 7 Oct 2010 12:59:32 -0400 Jeff Layton…
For instance:
(quoted diff) +8
-0+ if (!atomic_dec_and_lock(&cifs_file->count, &cifs_file_list_lock)) + return; + + /* count can be incremented inside the lock, so must check again */ + if (atomic_read(&cifs_file->count) > 0) { + spin_unlock(&cifs_file_list_lock); + return; + }
...oh and to make matters worse, the above code is still racy if the count can be incremented outside the lock too.
I think we need to try to keep this code as simple as possible with clear, unambiguous locking rules around a simple counter.
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH 13/15] cifs: convert cifsFileInfo->count to non-atomic counter
In reply to a message not in the archiveQuoted 5 lines · Steve French
I doubt that a spinlock vs. a rwlock makes much difference, but a rwlock lock for list insertion/removal seems more intuitive and overloading it to also protect a counter adds complexity. Seems more natural to leave it as an atomic counter.
The rwlock here is rather non-sensical. A shared/exclusie lock only makes sense if the critical section is large enough to actually benefit from concurrency. With a short critical section like here you still pay the same price in terms of cache line effects, and operations on rwlocks actually are more expensive than on a normal spinlock. We don't actually hae a lot of places where the plain rwlock makes any sense at all - if there's enough reason for concurreny you're better off doing RCU locking anyway.
PATCH 14/15 cifs: wait for writeback to complete in cifs_flush
In reply to a message not in the archiveThe f_op->flush operation is the last chance to return a writeback related error when closing a file. Ensure that we don't miss reporting any errors by waiting for writeback to complete in cifs_flush before proceeding.
There's no reason to do this when the file isn't open for write however.
--- fs/cifs/file.c | 21 +++++++-------------- 1 files changed, 7 insertions(+), 14 deletions(-)
fs/cifs/file.c +7 -14
@@ -1625,20 +1625,13 @@ int cifs_flush(struct file *file, fl_owner_t id)struct inode *inode = file->f_path.dentry->d_inode;int rc =0 ; -/* Rather than do the steps manually: -lock the inode for writing -loop through pages looking for write behind data (dirty pages) -coalesce into contiguous 16K (or smaller) chunks to write to server -send to server (prefer in parallel) -deal with writebehind errors -unlock inode for writing -filemapfdatawrite appears easier for the time being */ - - rc =filemap_fdatawrite (inode->i_mapping); -/* reset wb rc if we were able to write out dirty pages */ -if (!rc) { - rc =CIFS_I (inode)->write_behind_rc; -CIFS_I (inode)->write_behind_rc =0 ; +if (file->f_mode &FMODE_WRITE ) { + rc =filemap_write_and_wait (inode->i_mapping); +/* reset wb rc if we were able to write out dirty pages */ +if (!rc) { + rc =CIFS_I (inode)->write_behind_rc; +CIFS_I (inode)->write_behind_rc =0 ; + } }cFYI (1 ,"Flush inode %p file %p rc %d" , inode, file, rc);
Signature · 1 line
1.7.2.3
PATCH 15/15 cifs: eliminate cifsInodeInfo->write_behind_rc
In reply to a message not in the archivewrite_behind_rc is redundant and just adds complexity to the code. What we really want to do instead is to use mapping_set_error to reset the flags on the mapping when we find a writeback error and can't report it to userspace yet.
For cifs_flush and cifs_fsync, we shouldn't reset the flags since errors returned there do get reported to userspace.
--- fs/cifs/cifsfs.c | 1 - fs/cifs/cifsglob.h | 1 - fs/cifs/file.c | 34 ++++++++-------------------------- fs/cifs/inode.c | 15 +++++---------- 4 files changed, 13 insertions(+), 38 deletions(-)
fs/cifs/cifsfs.c +0 -1
@@ -320,7 +320,6 @@ cifs_alloc_inode(struct super_block *sb)return NULL ; cifs_inode->cifsAttrs =0x20 ;/* default */ cifs_inode->time =0 ; - cifs_inode->write_behind_rc =0 ;/* Until the file is open and we have gotten oplock info back from the server, can not assume caching of file data or metadata */
fs/cifs/cifsglob.h +0 -1
@@ -418,7 +418,6 @@ struct cifsInodeInfo {struct list_head lockList;/* BB add in lists for dirty pages i.e. write caching info for oplock */ struct list_head openFileList; -int write_behind_rc;__u32 cifsAttrs;/* e.g. DOS archive bit, sparse, compressed, system */ unsigned long time;/* jiffies of last update/check of inode */ bool clientCanCacheRead:1; /* read oplock */
fs/cifs/file.c +8 -26
@@ -133,8 +133,7 @@ static inline int cifs_open_inode_helper(struct inode *inode,/* BB no need to lock inode until after invalidate since namei code should already have it locked? */ rc =filemap_write_and_wait (inode->i_mapping); -if (rc !=0 ) - pCifsInode->write_behind_rc = rc; +mapping_set_error (inode->i_mapping, rc); }cFYI (1 ,"invalidating remote inode since open detected it " "changed" );
@@ -543,8 +542,7 @@ reopen_success:if (can_flush) { rc =filemap_write_and_wait (inode->i_mapping); -if (rc !=0 ) -CIFS_I (inode)->write_behind_rc = rc; +mapping_set_error (inode->i_mapping, rc); pCifsInode->clientCanCacheAll = false; pCifsInode->clientCanCacheRead = false;
@@ -1426,12 +1424,7 @@ retry:if (rc || bytes_written < bytes_to_write) {cERROR (1 ,"Write2 ret %d, wrote %d" , rc, bytes_written); -/* BB what if continued retry is -requested via mount flags? */ -if (rc == -ENOSPC ) -set_bit (AS_ENOSPC , &mapping->flags); -else -set_bit (AS_EIO , &mapping->flags); +mapping_set_error (mapping, rc); }else {cifs_stats_bytes_written (tcon, bytes_written); }
@@ -1576,10 +1569,8 @@ int cifs_fsync(struct file *file, int datasync) rc =filemap_write_and_wait (inode->i_mapping);if (rc ==0 ) { - rc =CIFS_I (inode)->write_behind_rc; -CIFS_I (inode)->write_behind_rc =0 ; tcon =tlink_tcon (smbfile->tlink); -if (!rc && tcon && smbfile && +if (rc ==0 && !(CIFS_SB (inode->i_sb)->mnt_cifs_flags &CIFS_MOUNT_NOSSYNC )) rc =CIFSSMBFlush (xid, tcon, smbfile->netfid); }
@@ -1625,14 +1616,8 @@ int cifs_flush(struct file *file, fl_owner_t id)struct inode *inode = file->f_path.dentry->d_inode;int rc =0 ; -if (file->f_mode &FMODE_WRITE ) { +if (file->f_mode &FMODE_WRITE ) rc =filemap_write_and_wait (inode->i_mapping); -/* reset wb rc if we were able to write out dirty pages */ -if (!rc) { - rc =CIFS_I (inode)->write_behind_rc; -CIFS_I (inode)->write_behind_rc =0 ; - } - }cFYI (1 ,"Flush inode %p file %p rc %d" , inode, file, rc);
@@ -2211,7 +2196,7 @@ void cifs_oplock_break(struct work_struct *work) oplock_break);struct inode *inode = cfile->dentry->d_inode;struct cifsInodeInfo *cinode =CIFS_I (inode); -int rc, waitrc =0 ; +int rc =0 ;if (inode &&S_ISREG (inode->i_mode)) {if (cinode->clientCanCacheRead)
@@ -2220,13 +2205,10 @@ void cifs_oplock_break(struct work_struct *work)break_lease (inode,O_WRONLY ); rc =filemap_fdatawrite (inode->i_mapping);if (cinode->clientCanCacheRead ==0 ) { - waitrc =filemap_fdatawait (inode->i_mapping); + rc =filemap_fdatawait (inode->i_mapping); +mapping_set_error (inode->i_mapping, rc);invalidate_remote_inode (inode); } -if (!rc) - rc = waitrc; -if (rc) - cinode->write_behind_rc = rc;cFYI (1 ,"Oplock flush inode %p rc %d" , inode, rc); }
fs/cifs/inode.c +5 -10
@@ -1688,8 +1688,7 @@ cifs_invalidate_mapping(struct inode *inode)/* write back any cached data */ if (inode->i_mapping && inode->i_mapping->nrpages !=0 ) { rc =filemap_write_and_wait (inode->i_mapping); -if (rc) - cifs_i->write_behind_rc = rc; +mapping_set_error (inode->i_mapping, rc); }invalidate_remote_inode (inode);cifs_fscache_reset_inode_cookie (inode);
@@ -1941,10 +1940,8 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)* the flush returns error? */ rc =filemap_write_and_wait (inode->i_mapping); -if (rc !=0 ) { - cifsInode->write_behind_rc = rc; - rc =0 ; - } +mapping_set_error (inode->i_mapping, rc); + rc =0 ;if (attrs->ia_valid &ATTR_SIZE ) { rc =cifs_set_file_size (inode, attrs, xid, full_path);
@@ -2085,10 +2082,8 @@ cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)* the flush returns error? */ rc =filemap_write_and_wait (inode->i_mapping); -if (rc !=0 ) { - cifsInode->write_behind_rc = rc; - rc =0 ; - } +mapping_set_error (inode->i_mapping, rc); + rc =0 ;if (attrs->ia_valid &ATTR_SIZE ) { rc =cifs_set_file_size (inode, attrs, xid, full_path);
Signature · 1 line
1.7.2.3
Suresh Jayaraman
Reviewed-byQuoted 15 lines · Jeff Layton
This is the second attempt at the patchset to clean up the management of open filehandles in CIFS. The main changes are:
1) elimination of some more unnecessary NULL pointer checks
2) the
cifs_file_list_lock(aka GlobalSMBSeslock) has been left as arwlock_t, though it has been renamed as before3) the order of operations in
cifs_oplock_break_puthas been fixed -- previously it put the sb reference before the cifsFileInfo reference which could have led to oopses.4)
cifs_flushonly waits for writeback to complete on filehandles that were opened withFMODE_WRITEset.
Quoted 7 lines
The patchset has passed several hours running the connectathon testsuite in a loop.
Suresh and Shaggy both sent acks for various parts of the patchset, but the last few patches had enough substantial changes that I didn't want to add them without their "re-acks".
Except for the patches [10/15] and [13/15] for which it appears spinlock is the way to go, rest of the patches look fine to me.