[PATCH][4/7] cifs: file.c cleanups in incremental bits - newcifs_open_inode_helper helper function

From: Jesper Juhl
Date: Wed Mar 16 2005 - 08:34:27 EST



This (attached) patch adds a new helper function to fs/cifs/file.c called
cifs_open_inode_helper and convers cifs_open to use it.
The point of the patch is to a) reduce nesting, and b) shorten the very
long cifs_open function.

Signed-off-by: Jesper Juhl <juhl-lkml@xxxxxx>


--
Jesper Juhl

diff -up linux-2.6.11-mm3/fs/cifs/file.c.with_patch_4 linux-2.6.11-mm3/fs/cifs/file.c
--- linux-2.6.11-mm3/fs/cifs/file.c.with_patch_4 2005-03-16 00:29:37.000000000 +0100
+++ linux-2.6.11-mm3/fs/cifs/file.c 2005-03-16 00:49:33.000000000 +0100
@@ -51,6 +51,72 @@ static inline struct cifsFileInfo *cifs_
return private_data;
}

+/* all arguments to this function must be checked for validity in caller */
+static inline int cifs_open_inode_helper(struct inode *inode, struct file *file,
+ struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile,
+ struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf,
+ char *full_path, int xid)
+{
+ struct timespec temp;
+ int rc;
+
+ /* want handles we can use to read with first
+ in the list so we do not have to walk the
+ list to search for one in prepare_write */
+ if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
+ list_add_tail(&pCifsFile->flist,
+ &pCifsInode->openFileList);
+ } else {
+ list_add(&pCifsFile->flist,
+ &pCifsInode->openFileList);
+ }
+ write_unlock(&GlobalSMBSeslock);
+ write_unlock(&file->f_owner.lock);
+ if (pCifsInode->clientCanCacheRead) {
+ /* we have the inode open somewhere else
+ no need to discard cache data */
+ goto client_can_cache;
+ }
+
+ /* BB need same check in cifs_create too? */
+ /* 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_dentry->d_inode->i_mtime, &temp) &&
+ (file->f_dentry->d_inode->i_size ==
+ (loff_t)le64_to_cpu(buf->EndOfFile))) {
+ cFYI(1, ("inode unchanged on server"));
+ } else {
+ if (file->f_dentry->d_inode->i_mapping) {
+ /* BB no need to lock inode until after invalidate
+ since namei code should already have it locked? */
+ filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
+ filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
+ }
+ cFYI(1, ("invalidating remote inode since open detected it "
+ "changed"));
+ invalidate_remote_inode(file->f_dentry->d_inode);
+ }
+
+client_can_cache:
+ if (pTcon->ses->capabilities & CAP_UNIX)
+ rc = cifs_get_inode_info_unix(&file->f_dentry->d_inode,
+ full_path, inode->i_sb, xid);
+ else
+ rc = cifs_get_inode_info(&file->f_dentry->d_inode,
+ full_path, buf, inode->i_sb, xid);
+
+ if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) {
+ pCifsInode->clientCanCacheAll = TRUE;
+ pCifsInode->clientCanCacheRead = TRUE;
+ cFYI(1, ("Exclusive Oplock granted on inode %p",
+ file->f_dentry->d_inode));
+ } else if ((*oplock & 0xF) == OPLOCK_READ)
+ pCifsInode->clientCanCacheRead = TRUE;
+
+ return rc;
+}
+
int cifs_open(struct inode *inode, struct file *file)
{
int rc = -EACCES;
@@ -175,11 +241,13 @@ int cifs_open(struct inode *inode, struc
rc = -ENOMEM;
goto out;
}
+
tmppriv = kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
if (!tmppriv) {
rc = -ENOMEM;
goto out;
}
+
rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,
CREATE_NOT_DIR, &netfid, &oplock, buf,
cifs_sb->local_nls);
@@ -195,61 +263,16 @@ int cifs_open(struct inode *inode, struc
write_lock(&GlobalSMBSeslock);
list_add(&pCifsFile->tlist, &pTcon->openFileList);

- pCifsInode = CIFS_I(file->f_dentry->d_inode);
- if (pCifsInode) {
- /* want handles we can use to read with first */
- /* in the list so we do not have to walk the */
- /* list to search for one in prepare_write */
- if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
- list_add_tail(&pCifsFile->flist, &pCifsInode->openFileList);
- } else {
- list_add(&pCifsFile->flist, &pCifsInode->openFileList);
- }
- write_unlock(&GlobalSMBSeslock);
- write_unlock(&file->f_owner.lock);
- if (pCifsInode->clientCanCacheRead) {
- /* we have the inode open somewhere else
- no need to discard cache data */
- } else {
- if (buf) {
- /* BB need same check in cifs_create too? */
-
- /* if not oplocked, invalidate inode pages if mtime
- or file size changed */
- struct timespec temp;
- temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
- if (timespec_equal(&file->f_dentry->d_inode->i_mtime, &temp) &&
- (file->f_dentry->d_inode->i_size == (loff_t)le64_to_cpu(buf->EndOfFile))) {
- cFYI(1, ("inode unchanged on server"));
- } else {
- if (file->f_dentry->d_inode->i_mapping) {
- /* BB no need to lock inode until after invalidate*/
- /* since namei code should already have it locked?*/
- filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
- filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
- }
- cFYI(1, ("invalidating remote inode since open detected it changed"));
- invalidate_remote_inode(file->f_dentry->d_inode);
- }
- }
- }
- if (pTcon->ses->capabilities & CAP_UNIX)
- rc = cifs_get_inode_info_unix(&file->f_dentry->d_inode,
- full_path, inode->i_sb, xid);
- else
- rc = cifs_get_inode_info(&file->f_dentry->d_inode,
- full_path, buf, inode->i_sb, xid);
+ pCifsInode = CIFS_I(file->f_dentry->d_inode);
+ if (pCifsInode) {
+ rc = cifs_open_inode_helper(inode, file, pCifsInode,
+ pCifsFile, pTcon,
+ &oplock, buf, full_path, xid);
+ } else {
+ write_unlock(&GlobalSMBSeslock);
+ write_unlock(&file->f_owner.lock);
+ }

- if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
- pCifsInode->clientCanCacheAll = TRUE;
- pCifsInode->clientCanCacheRead = TRUE;
- cFYI(1, ("Exclusive Oplock granted on inode %p", file->f_dentry->d_inode));
- } else if ((oplock & 0xF) == OPLOCK_READ)
- pCifsInode->clientCanCacheRead = TRUE;
- } else {
- write_unlock(&GlobalSMBSeslock);
- write_unlock(&file->f_owner.lock);
- }
if (oplock & CIFS_CREATE_ACTION) {
/* time to set mode which we can not set earlier due to
problems creating new read-only files */