⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.c

📁 Linux Kernel 2.6.9 for OMAP1710
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *   fs/cifs/file.c * *   vfs operations that deal with files *  *   Copyright (C) International Business Machines  Corp., 2002,2003 *   Author(s): Steve French (sfrench@us.ibm.com) * *   This library is free software; you can redistribute it and/or modify *   it under the terms of the GNU Lesser General Public License as published *   by the Free Software Foundation; either version 2.1 of the License, or *   (at your option) any later version. * *   This library is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See *   the GNU Lesser General Public License for more details. * *   You should have received a copy of the GNU Lesser General Public License *   along with this library; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <linux/fs.h>#include <linux/stat.h>#include <linux/fcntl.h>#include <linux/pagemap.h>#include <linux/pagevec.h>#include <linux/smp_lock.h>#include <asm/div64.h>#include "cifsfs.h"#include "cifspdu.h"#include "cifsglob.h"#include "cifsproto.h"#include "cifs_unicode.h"#include "cifs_debug.h"#include "cifs_fs_sb.h"intcifs_open(struct inode *inode, struct file *file){	int rc = -EACCES;	int xid, oplock;	struct cifs_sb_info *cifs_sb;	struct cifsTconInfo *pTcon;	struct cifsFileInfo *pCifsFile;	struct cifsInodeInfo *pCifsInode;	struct list_head * tmp;	char *full_path = NULL;	int desiredAccess = 0x20197;	int disposition;	__u16 netfid;	FILE_ALL_INFO * buf = NULL;	xid = GetXid();	cifs_sb = CIFS_SB(inode->i_sb);	pTcon = cifs_sb->tcon;	if (file->f_flags & O_CREAT) {		/* search inode for this file and fill in file->private_data = */		pCifsInode = CIFS_I(file->f_dentry->d_inode);		read_lock(&GlobalSMBSeslock);		list_for_each(tmp, &pCifsInode->openFileList) {            			pCifsFile = list_entry(tmp,struct cifsFileInfo, flist);           			if((pCifsFile->pfile == NULL)&& (pCifsFile->pid = current->pid)){			/* mode set in cifs_create */				pCifsFile->pfile = file; /* needed for writepage */				file->private_data = pCifsFile;				break;			}		}		read_unlock(&GlobalSMBSeslock);		if(file->private_data != NULL) {			rc = 0;			FreeXid(xid);			return rc;		} else {			if(file->f_flags & O_EXCL)				cERROR(1,("could not find file instance for new file %p ",file));		}	}	down(&inode->i_sb->s_vfs_rename_sem);	full_path = build_path_from_dentry(file->f_dentry);	up(&inode->i_sb->s_vfs_rename_sem);	if(full_path == NULL) {		FreeXid(xid);		return -ENOMEM;	}	cFYI(1, (" inode = 0x%p file flags are 0x%x for %s", inode, file->f_flags,full_path));	if ((file->f_flags & O_ACCMODE) == O_RDONLY)		desiredAccess = GENERIC_READ;	else if ((file->f_flags & O_ACCMODE) == O_WRONLY)		desiredAccess = GENERIC_WRITE;	else if ((file->f_flags & O_ACCMODE) == O_RDWR) {		/* GENERIC_ALL is too much permission to request */		/* can cause unnecessary access denied on create */		/* desiredAccess = GENERIC_ALL; */		desiredAccess = GENERIC_READ | GENERIC_WRITE;	}/********************************************************************* *  open flag mapping table: *   *	POSIX Flag            CIFS Disposition *	----------            ----------------  *	O_CREAT               FILE_OPEN_IF *	O_CREAT | O_EXCL      FILE_CREATE *	O_CREAT | O_TRUNC     FILE_OVERWRITE_IF *	O_TRUNC               FILE_OVERWRITE *	none of the above     FILE_OPEN * *	Note that there is not a direct match between disposition *	FILE_SUPERSEDE (ie create whether or not file exists although  *	O_CREAT | O_TRUNC is similar but truncates the existing *	file rather than creating a new file as FILE_SUPERSEDE does *	(which uses the attributes / metadata passed in on open call) *? *?  O_SYNC is a reasonable match to CIFS writethrough flag   *?  and the read write flags match reasonably.  O_LARGEFILE *?  is irrelevant because largefile support is always used *?  by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, *	 O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation *********************************************************************/	if((file->f_flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))		disposition = FILE_CREATE;	else if((file->f_flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))		disposition = FILE_OVERWRITE_IF;	else if((file->f_flags & O_CREAT) == O_CREAT)		disposition = FILE_OPEN_IF;	else		disposition = FILE_OPEN;	if (oplockEnabled)		oplock = REQ_OPLOCK;	else		oplock = FALSE;	/* BB pass O_SYNC flag through on file attributes .. BB */	/* Also refresh inode by passing in file_info buf returned by SMBOpen 	   and calling get_inode_info with returned buf (at least 	   helps non-Unix server case */	/* BB we can not do this if this is the second open of a file 	and the first handle has writebehind data, we might be 	able to simply do a filemap_fdatawrite/filemap_fdatawait first */	buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);	if(buf== NULL) {		if (full_path)			kfree(full_path);		FreeXid(xid);		return -ENOMEM;	}	rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,			CREATE_NOT_DIR, &netfid, &oplock, buf, cifs_sb->local_nls);	if (rc) {		cFYI(1, ("cifs_open returned 0x%x ", rc));		cFYI(1, ("oplock: %d ", oplock));		} else {		file->private_data =			kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);		if (file->private_data) {			memset(file->private_data, 0, sizeof(struct cifsFileInfo));			pCifsFile = (struct cifsFileInfo *) file->private_data;			pCifsFile->netfid = netfid;			pCifsFile->pid = current->pid;			init_MUTEX(&pCifsFile->fh_sem);			pCifsFile->pfile = file; /* needed for writepage */			pCifsFile->pInode = inode;			pCifsFile->invalidHandle = FALSE;			pCifsFile->closePend     = FALSE;			write_lock(&file->f_owner.lock);			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);				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 */				if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)                					CIFSSMBUnixSetPerms(xid, pTcon, full_path, inode->i_mode,						(__u64)-1, 						(__u64)-1,						0 /* dev */,						cifs_sb->local_nls);				else {/* BB implement via Windows security descriptors */			/* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/			/* in the meantime could set r/o dos attribute when perms are eg:					mode & 0222 == 0 */				}			}		}	}	if (buf)		kfree(buf);	if (full_path)		kfree(full_path);	FreeXid(xid);	return rc;}/* Try to reaquire byte range locks that were released when session *//* to server was lost */static int cifs_relock_file(struct cifsFileInfo * cifsFile){	int rc = 0;/* BB list all locks open on this file and relock */	return rc;}static int cifs_reopen_file(struct inode *inode, struct file *file, int can_flush){	int rc = -EACCES;	int xid, oplock;	struct cifs_sb_info *cifs_sb;	struct cifsTconInfo *pTcon;	struct cifsFileInfo *pCifsFile;	struct cifsInodeInfo *pCifsInode;	char *full_path = NULL;	int desiredAccess = 0x20197;	int disposition = FILE_OPEN;	__u16 netfid;	if(inode == NULL)		return -EBADF;	if (file->private_data) {		pCifsFile = (struct cifsFileInfo *) file->private_data;	} else		return -EBADF;	xid = GetXid();	down(&pCifsFile->fh_sem);	if(pCifsFile->invalidHandle == FALSE) {		up(&pCifsFile->fh_sem);		FreeXid(xid);		return 0;	}	if(file->f_dentry == NULL) {		up(&pCifsFile->fh_sem);		cFYI(1,("failed file reopen, no valid name if dentry freed"));		FreeXid(xid);		return -EBADF;	}	cifs_sb = CIFS_SB(inode->i_sb);	pTcon = cifs_sb->tcon;/* can not grab rename sem here because various ops, includingthose that already have the rename sem can end up causing writepageto get called and if the server was down that means we end up here,and we can never tell if the caller already has the rename_sem */	full_path = build_path_from_dentry(file->f_dentry);	if(full_path == NULL) {		up(&pCifsFile->fh_sem);		FreeXid(xid);		return -ENOMEM;	}	cFYI(1, (" inode = 0x%p file flags are 0x%x for %s", inode, file->f_flags,full_path));	if ((file->f_flags & O_ACCMODE) == O_RDONLY)		desiredAccess = GENERIC_READ;	else if ((file->f_flags & O_ACCMODE) == O_WRONLY)		desiredAccess = GENERIC_WRITE;	else if ((file->f_flags & O_ACCMODE) == O_RDWR) {		/* GENERIC_ALL is too much permission to request */		/* can cause unnecessary access denied on create */		/* desiredAccess = GENERIC_ALL; */		desiredAccess = GENERIC_READ | GENERIC_WRITE;	}	if (oplockEnabled)		oplock = REQ_OPLOCK;	else		oplock = FALSE;		/* Can not refresh inode by passing in file_info buf to be returned	 by SMBOpen and then calling get_inode_info with returned buf 	 since file might have write behind data that needs to be flushed 	 and server version of file size can be stale. If we 	 knew for sure that inode was not dirty locally we could do this *//*	buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);	if(buf==0) {		up(&pCifsFile->fh_sem);		if (full_path)			kfree(full_path);		FreeXid(xid);		return -ENOMEM;	}*/	rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,				CREATE_NOT_DIR, &netfid, &oplock, NULL, cifs_sb->local_nls);	if (rc) {		up(&pCifsFile->fh_sem);		cFYI(1, ("cifs_open returned 0x%x ", rc));		cFYI(1, ("oplock: %d ", oplock));	} else {		pCifsFile->netfid = netfid;		pCifsFile->invalidHandle = FALSE;		up(&pCifsFile->fh_sem);		pCifsInode = CIFS_I(inode);		if(pCifsInode) {			if(can_flush) {				filemap_fdatawrite(inode->i_mapping);				filemap_fdatawait(inode->i_mapping);			/* temporarily disable caching while we			go to server to get inode info */				pCifsInode->clientCanCacheAll = FALSE;				pCifsInode->clientCanCacheRead = FALSE;				if (pTcon->ses->capabilities & CAP_UNIX)					rc = cifs_get_inode_info_unix(&inode,						full_path, inode->i_sb,xid);				else					rc = cifs_get_inode_info(&inode,						full_path, NULL, inode->i_sb,xid);			} /* else we are writing out data to server already			and could deadlock if we tried to flush data, and 			since we do not know if we have data that would			invalidate the current end of file on the server			we can not go to the server to get the new			inod info */			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;				pCifsInode->clientCanCacheAll =  FALSE;			} else {				pCifsInode->clientCanCacheRead = FALSE;				pCifsInode->clientCanCacheAll =  FALSE;			}			cifs_relock_file(pCifsFile);		}	}	if (full_path)		kfree(full_path);	FreeXid(xid);	return rc;}intcifs_close(struct inode *inode, struct file *file){	int rc = 0;	int xid;	struct cifs_sb_info *cifs_sb;	struct cifsTconInfo *pTcon;	struct cifsFileInfo *pSMBFile =		(struct cifsFileInfo *) file->private_data;	xid = GetXid();	cifs_sb = CIFS_SB(inode->i_sb);	pTcon = cifs_sb->tcon;	if (pSMBFile) {		pSMBFile->closePend    = TRUE;		write_lock(&file->f_owner.lock);		if(pTcon) {			/* no sense reconnecting to close a file that is				already closed */			if (pTcon->tidStatus != CifsNeedReconnect) {				write_unlock(&file->f_owner.lock);				rc = CIFSSMBClose(xid,pTcon,pSMBFile->netfid);				write_lock(&file->f_owner.lock);			}		}		list_del(&pSMBFile->flist);		list_del(&pSMBFile->tlist);		write_unlock(&file->f_owner.lock);		if(pSMBFile->search_resume_name)			kfree(pSMBFile->search_resume_name);		kfree(file->private_data);		file->private_data = NULL;	} else		rc = -EBADF;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -