📄 file.c
字号:
/* * fs/cifs/file.c * * vfs operations that deal with files * * Copyright (C) International Business Machines Corp., 2002,2007 * Author(s): Steve French (sfrench@us.ibm.com) * Jeremy Allison (jra@samba.org) * * 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/delay.h>#include <asm/div64.h>#include "cifsfs.h"#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)#include <linux/backing-dev.h>#include <linux/mpage.h>#include <linux/pagevec.h>#include <linux/writeback.h>#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 19)#include <linux/task_io_accounting_ops.h>#endif /* 2.6.19 */#endif#include <asm/uaccess.h>#ifndef FL_SLEEP#define FL_SLEEP 0#endif#include "cifspdu.h"#include "cifsglob.h"#include "cifsproto.h"#include "cifs_unicode.h"#include "cifs_debug.h"#include "cifs_fs_sb.h"static inline struct cifsFileInfo *cifs_init_private( struct cifsFileInfo *private_data, struct inode *inode, struct file *file, __u16 netfid){ memset(private_data, 0, sizeof(struct cifsFileInfo)); private_data->netfid = netfid; private_data->pid = current->tgid; init_MUTEX(&private_data->fh_sem);#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17) mutex_init(&private_data->lock_mutex);#else init_MUTEX(&private_data->lock_mutex);#endif INIT_LIST_HEAD(&private_data->llist); private_data->pfile = file; /* needed for writepage */ private_data->pInode = inode; private_data->invalidHandle = FALSE; private_data->closePend = FALSE; /* we have to track num writers to the inode, since writepages does not tell us which handle the write is for so there can be a close (overlapping with write) of the filehandle that cifs_writepages chose to use */ atomic_set(&private_data->wrtPending, 0); return private_data;}static inline int cifs_convert_flags(unsigned int flags){ if ((flags & O_ACCMODE) == O_RDONLY) return GENERIC_READ; else if ((flags & O_ACCMODE) == O_WRONLY) return GENERIC_WRITE; else if ((flags & O_ACCMODE) == O_RDWR) { /* GENERIC_ALL is too much permission to request can cause unnecessary access denied on create */ /* return GENERIC_ALL; */ return (GENERIC_READ | GENERIC_WRITE); } return 0x20197;}static inline int cifs_get_disposition(unsigned int flags){ if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) return FILE_CREATE;#else return FILE_OPEN_IF;#endif else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) return FILE_OVERWRITE_IF; else if ((flags & O_CREAT) == O_CREAT) return FILE_OPEN_IF; else if ((flags & O_TRUNC) == O_TRUNC) return FILE_OVERWRITE; else return FILE_OPEN;}/* 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){#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) struct timespec temp;#else time_t temp;#endif 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); 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 LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) 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) {#else 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) {#endif /* BB no need to lock inode until after invalidate since namei code should already have it locked? */#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 19) filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping); #elif LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 15) filemap_write_and_wait(file->f_dentry->d_inode->i_mapping);#else filemap_fdatawrite(file->f_dentry->d_inode->i_mapping); filemap_fdatawait(file->f_dentry->d_inode->i_mapping);#endif } cFYI(1, ("invalidating remote inode since open detected it " "changed"));#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) invalidate_remote_inode(file->f_dentry->d_inode);#else invalidate_remote_inode(file->f_path.dentry->d_inode);#endif }client_can_cache: if (pTcon->unix_ext)#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) rc = cifs_get_inode_info_unix(&file->f_dentry->d_inode,#else rc = cifs_get_inode_info_unix(&file->f_path.dentry->d_inode,#endif full_path, inode->i_sb, xid);#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) rc = cifs_get_inode_info(&file->f_dentry->d_inode,#else rc = cifs_get_inode_info(&file->f_path.dentry->d_inode,#endif full_path, buf, inode->i_sb, xid); if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) { pCifsInode->clientCanCacheAll = TRUE; pCifsInode->clientCanCacheRead = TRUE;#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) cFYI(1, ("Exclusive Oplock granted on inode %p", file->f_dentry->d_inode));#else cFYI(1, ("Exclusive Oplock granted on inode %p", file->f_path.dentry->d_inode));#endif } else if ((*oplock & 0xF) == OPLOCK_READ) pCifsInode->clientCanCacheRead = TRUE; return rc;}int cifs_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; 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 */#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) pCifsInode = CIFS_I(file->f_dentry->d_inode);#else pCifsInode = CIFS_I(file->f_path.dentry->d_inode);#endif read_lock(&GlobalSMBSeslock); list_for_each(tmp, &pCifsInode->openFileList) { pCifsFile = list_entry(tmp, struct cifsFileInfo, flist); if ((pCifsFile->pfile == NULL) && (pCifsFile->pid == current->tgid)) { /* mode set in cifs_create */ /* needed for writepage */ pCifsFile->pfile = file; 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)); } }#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) full_path = build_path_from_dentry(file->f_dentry);#else full_path = build_path_from_dentry(file->f_path.dentry);#endif 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)); desiredAccess = cifs_convert_flags(file->f_flags);/********************************************************************* * 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 *********************************************************************/ disposition = cifs_get_disposition(file->f_flags); 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) { rc = -ENOMEM; goto out; } if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS) rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); else rc = -EIO; /* no NT SMB support fall into legacy open below */ if (rc == -EIO) { /* Old server, try legacy style OpenX */ rc = SMBLegacyOpen(xid, pTcon, full_path, disposition, desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); } if (rc) { cFYI(1, ("cifs_open returned 0x%x", rc)); goto out; } file->private_data = kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); if (file->private_data == NULL) { rc = -ENOMEM; goto out; } pCifsFile = cifs_init_private(file->private_data, inode, file, netfid); write_lock(&GlobalSMBSeslock); list_add(&pCifsFile->tlist, &pTcon->openFileList);#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) pCifsInode = CIFS_I(file->f_dentry->d_inode);#else pCifsInode = CIFS_I(file->f_path.dentry->d_inode);#endif if (pCifsInode) { rc = cifs_open_inode_helper(inode, file, pCifsInode, pCifsFile, pTcon, &oplock, buf, full_path, xid); } else { write_unlock(&GlobalSMBSeslock); } if (oplock & CIFS_CREATE_ACTION) { /* time to set mode which we can not set earlier due to problems creating new read-only files */ if (pTcon->unix_ext) { CIFSSMBUnixSetPerms(xid, pTcon, full_path, inode->i_mode, (__u64)-1, (__u64)-1, 0 /* dev */, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); } 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 */ } }out: kfree(buf); kfree(full_path); FreeXid(xid); return rc;}/* Try to reacquire 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 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; struct inode *inode; char *full_path = NULL; int desiredAccess; int disposition = FILE_OPEN; __u16 netfid; 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 LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) if (file->f_dentry == NULL) { cERROR(1, ("no valid name if dentry freed")); dump_stack(); rc = -EBADF; goto reopen_error_exit; } inode = file->f_dentry->d_inode; if (inode == NULL) { cERROR(1, ("inode not valid")); dump_stack(); rc = -EBADF; goto reopen_error_exit; } cifs_sb = CIFS_SB(inode->i_sb); pTcon = cifs_sb->tcon;/* can not grab rename sem here because various ops, including those that already have the rename sem can end up causing writepage to 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);#else if (file->f_path.dentry == NULL) { cERROR(1, ("no valid name if dentry freed")); dump_stack(); rc = -EBADF; goto reopen_error_exit; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -