📄 inode.c
字号:
} else { rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL, direntry->d_sb, xid); if (rc) { cFYI(1, ("error on getting revalidate info %d", rc));/* if (rc != -ENOENT) rc = 0; */ /* BB should we cache info on certain errors? */ } } /* should we remap certain errors, access denied?, to zero */ /* if not oplocked, we invalidate inode pages if mtime or file size had changed on server */ if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) && (local_size == direntry->d_inode->i_size)) { cFYI(1, ("cifs_revalidate - inode unchanged")); } else { /* file may have changed on server */ if (cifsInode->clientCanCacheRead) { /* no need to invalidate inode pages since we were the only ones who could have modified the file and the server copy is staler than ours */ } else { invalidate_inode = TRUE; } } /* can not grab this sem since kernel filesys locking documentation indicates i_mutex may be taken by the kernel on lookup and rename which could deadlock if we grab the i_mutex here as well *//* mutex_lock(&direntry->d_inode->i_mutex);*/ /* need to write out dirty pages here */ if (direntry->d_inode->i_mapping) { /* do we need to lock inode until after invalidate completes below? */ filemap_fdatawrite(direntry->d_inode->i_mapping); } if (invalidate_inode) { /* shrink_dcache not necessary now that cifs dentry ops are exported for negative dentries *//* if (S_ISDIR(direntry->d_inode->i_mode)) shrink_dcache_parent(direntry); */ if (S_ISREG(direntry->d_inode->i_mode)) { if (direntry->d_inode->i_mapping) filemap_fdatawait(direntry->d_inode->i_mapping); /* may eventually have to do this for open files too */ if (list_empty(&(cifsInode->openFileList))) { /* changed on server - flush read ahead pages */ cFYI(1, ("Invalidating read ahead data on " "closed file")); invalidate_remote_inode(direntry->d_inode); } } }/* mutex_unlock(&direntry->d_inode->i_mutex); */ kfree(full_path); FreeXid(xid); return rc;}#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat){ int err = cifs_revalidate(dentry); if (!err) { generic_fillattr(dentry->d_inode, stat); stat->blksize = CIFS_MAX_MSGSIZE; } return err;}#endifstatic int cifs_truncate_page(struct address_space *mapping, loff_t from){#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) pgoff_t index = from >> PAGE_CACHE_SHIFT;#else unsigned long index = from >> PAGE_CACHE_SHIFT;#endif unsigned offset = from & (PAGE_CACHE_SIZE - 1); struct page *page; char *kaddr; int rc = 0; page = grab_cache_page(mapping, index); if (!page) return -ENOMEM; kaddr = kmap_atomic(page, KM_USER0); memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); flush_dcache_page(page); kunmap_atomic(kaddr, KM_USER0); unlock_page(page); page_cache_release(page); return rc;}static int cifs_vmtruncate(struct inode *inode, loff_t offset){ struct address_space *mapping = inode->i_mapping; unsigned long limit; spin_lock(&inode->i_lock); if (inode->i_size < offset) goto do_expand; /* * truncation of in-use swapfiles is disallowed - it would cause * subsequent swapout to scribble on the now-freed blocks. */ if (IS_SWAPFILE(inode)) { spin_unlock(&inode->i_lock); goto out_busy; } i_size_write(inode, offset); spin_unlock(&inode->i_lock); /* * unmap_mapping_range is called twice, first simply for efficiency * so that truncate_inode_pages does fewer single-page unmaps. However * after this first call, and before truncate_inode_pages finishes, * it is possible for private pages to be COWed, which remain after * truncate_inode_pages finishes, hence the second unmap_mapping_range * call must be made for correctness. */ unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); truncate_inode_pages(mapping, offset); unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); goto out_truncate;do_expand:#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10) limit = current->rlim[RLIMIT_FSIZE].rlim_cur;#else limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;#endif if (limit != RLIM_INFINITY && offset > limit) { spin_unlock(&inode->i_lock); goto out_sig; } if (offset > inode->i_sb->s_maxbytes) { spin_unlock(&inode->i_lock); goto out_big; } i_size_write(inode, offset); spin_unlock(&inode->i_lock);out_truncate: if (inode->i_op && inode->i_op->truncate) inode->i_op->truncate(inode); return 0;out_sig: send_sig(SIGXFSZ, current, 0);out_big: return -EFBIG;out_busy: return -ETXTBSY;}int cifs_setattr(struct dentry *direntry, struct iattr *attrs){ int xid; struct cifs_sb_info *cifs_sb; struct cifsTconInfo *pTcon; char *full_path = NULL; int rc = -EACCES; struct cifsFileInfo *open_file = NULL; FILE_BASIC_INFO time_buf; int set_time = FALSE; int set_dosattr = FALSE; __u64 mode = 0xFFFFFFFFFFFFFFFFULL; __u64 uid = 0xFFFFFFFFFFFFFFFFULL; __u64 gid = 0xFFFFFFFFFFFFFFFFULL; struct cifsInodeInfo *cifsInode; xid = GetXid(); cFYI(1, ("setattr on file %s attrs->iavalid 0x%x", direntry->d_name.name, attrs->ia_valid)); cifs_sb = CIFS_SB(direntry->d_inode->i_sb); pTcon = cifs_sb->tcon; if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { /* check if we have permission to change attrs */ rc = inode_change_ok(direntry->d_inode, attrs); if (rc < 0) { FreeXid(xid); return rc; } else rc = 0; } full_path = build_path_from_dentry(direntry); if (full_path == NULL) { FreeXid(xid); return -ENOMEM; } cifsInode = CIFS_I(direntry->d_inode); /* BB check if we need to refresh inode from server now ? BB */ /* need to flush data before changing file size on server */#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 15) filemap_write_and_wait(direntry->d_inode->i_mapping);#else filemap_fdatawrite(direntry->d_inode->i_mapping); filemap_fdatawait(direntry->d_inode->i_mapping);#endif if (attrs->ia_valid & ATTR_SIZE) { /* To avoid spurious oplock breaks from server, in the case of inodes that we already have open, avoid doing path based setting of file size if we can do it by handle. This keeps our caching token (oplock) and avoids timeouts when the local oplock break takes longer to flush writebehind data than the SMB timeout for the SetPathInfo request would allow */ open_file = find_writable_file(cifsInode); if (open_file) { __u16 nfid = open_file->netfid; __u32 npid = open_file->pid; rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid, npid, FALSE); atomic_dec(&open_file->wrtPending); cFYI(1, ("SetFSize for attrs rc = %d", rc)); if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { unsigned int bytes_written; rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size, &bytes_written, NULL, NULL, 1 /* 45 seconds */); cFYI(1, ("Wrt seteof rc %d", rc)); } } else rc = -EINVAL; if (rc != 0) { /* Set file size by pathname rather than by handle either because no valid, writeable file handle for it was found or because there was an error setting it by handle */ rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, FALSE, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc)); if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { __u16 netfid; int oplock = FALSE; rc = SMBLegacyOpen(xid, pTcon, full_path, FILE_OPEN, SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR, &netfid, &oplock, NULL, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); if (rc == 0) { unsigned int bytes_written; rc = CIFSSMBWrite(xid, pTcon, netfid, 0, attrs->ia_size, &bytes_written, NULL, NULL, 1 /* 45 sec */); cFYI(1, ("wrt seteof rc %d", rc)); CIFSSMBClose(xid, pTcon, netfid); } } } /* Server is ok setting allocation size implicitly - no need to call: CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE, cifs_sb->local_nls); */ if (rc == 0) { rc = cifs_vmtruncate(direntry->d_inode, attrs->ia_size); cifs_truncate_page(direntry->d_inode->i_mapping, direntry->d_inode->i_size); } else goto cifs_setattr_exit; } if (attrs->ia_valid & ATTR_UID) { cFYI(1, ("UID changed to %d", attrs->ia_uid)); uid = attrs->ia_uid; } if (attrs->ia_valid & ATTR_GID) { cFYI(1, ("GID changed to %d", attrs->ia_gid)); gid = attrs->ia_gid; } time_buf.Attributes = 0; if (attrs->ia_valid & ATTR_MODE) { cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode)); mode = attrs->ia_mode; } if ((pTcon->unix_ext) && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID))) rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid, 0 /* dev_t */, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); else if (attrs->ia_valid & ATTR_MODE) { rc = 0; if ((mode & S_IWUGO) == 0) /* not writeable */ { if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) { set_dosattr = TRUE; time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs | ATTR_READONLY); } } else if (cifsInode->cifsAttrs & ATTR_READONLY) { /* If file is readonly on server, we would not be able to write to it - so if any write bit is enabled for user or group or other we need to at least try to remove r/o dos attr */ set_dosattr = TRUE; time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs & (~ATTR_READONLY)); /* Windows ignores set to zero */ if (time_buf.Attributes == 0) time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL); } /* BB to be implemented - via Windows security descriptors or streams */ /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid, cifs_sb->local_nls); */ } if (attrs->ia_valid & ATTR_ATIME) { set_time = TRUE; time_buf.LastAccessTime = cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); } else time_buf.LastAccessTime = 0; if (attrs->ia_valid & ATTR_MTIME) { set_time = TRUE; time_buf.LastWriteTime = cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); } else time_buf.LastWriteTime = 0; /* Do not set ctime explicitly unless other time stamps are changed explicitly (i.e. by utime() since we would then have a mix of client and server times */ if (set_time && (attrs->ia_valid & ATTR_CTIME)) { set_time = TRUE; /* Although Samba throws this field away it may be useful to Windows - but we do not want to set ctime unless some other timestamp is changing */ cFYI(1, ("CIFS - CTIME changed")); time_buf.ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); } else time_buf.ChangeTime = 0; if (set_time || set_dosattr) { time_buf.CreationTime = 0; /* do not change */ /* In the future we should experiment - try setting timestamps via Handle (SetFileInfo) instead of by path */ if (!(pTcon->ses->flags & CIFS_SES_NT4)) rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); else rc = -EOPNOTSUPP; if (rc == -EOPNOTSUPP) { int oplock = FALSE; __u16 netfid; cFYI(1, ("calling SetFileInfo since SetPathInfo for " "times not supported by this server")); /* BB we could scan to see if we already have it open and pass in pid of opener to function */ rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR, &netfid, &oplock, NULL, cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); if (rc == 0) { rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf, netfid); CIFSSMBClose(xid, pTcon, netfid); } else { /* BB For even older servers we could convert time_buf into old DOS style which uses two second granularity */ /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path, &time_buf, cifs_sb->local_nls); */ } } /* Even if error on time set, no sense failing the call if the server would set the time to a reasonable value anyway, and this check ensures that we are not being called from sys_utimes in which case we ought to fail the call back to the user when the server rejects the call */ if ((rc) && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE))) rc = 0; } /* do not need local check to inode_check_ok since the server does that */ if (!rc) rc = inode_setattr(direntry->d_inode, attrs);cifs_setattr_exit: kfree(full_path); FreeXid(xid); return rc;}#if 0void cifs_delete_inode(struct inode *inode){ cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode)); /* may have to add back in if and when safe distributed caching of directories added e.g. via FindNotify */}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -