📄 file.c
字号:
/* * linux/fs/nfs/file.c * * Copyright (C) 1992 Rick Sladkey * * Changes Copyright (C) 1994 by Florian La Roche * - Do not copy data too often around in the kernel. * - In nfs_file_read the return value of kmalloc wasn't checked. * - Put in a better version of read look-ahead buffering. Original idea * and implementation by Wai S Kok elekokws@ee.nus.sg. * * Expire cache on write to a file by Wai S Kok (Oct 1994). * * Total rewrite of read side for new NFS buffer cache.. Linus. * * nfs regular file handling functions */#include <linux/time.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/fcntl.h>#include <linux/stat.h>#include <linux/nfs_fs.h>#include <linux/nfs_mount.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/pagemap.h>#include <linux/smp_lock.h>#include <linux/aio.h>#include <asm/uaccess.h>#include <asm/system.h>#include "delegation.h"#include "internal.h"#include "iostat.h"#define NFSDBG_FACILITY NFSDBG_FILEstatic int nfs_file_open(struct inode *, struct file *);static int nfs_file_release(struct inode *, struct file *);static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);static int nfs_file_mmap(struct file *, struct vm_area_struct *);static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos, struct pipe_inode_info *pipe, size_t count, unsigned int flags);static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov, unsigned long nr_segs, loff_t pos);static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov, unsigned long nr_segs, loff_t pos);static int nfs_file_flush(struct file *, fl_owner_t id);static int nfs_fsync(struct file *, struct dentry *dentry, int datasync);static int nfs_check_flags(int flags);static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);static struct vm_operations_struct nfs_file_vm_ops;const struct file_operations nfs_file_operations = { .llseek = nfs_file_llseek, .read = do_sync_read, .write = do_sync_write, .aio_read = nfs_file_read, .aio_write = nfs_file_write, .mmap = nfs_file_mmap, .open = nfs_file_open, .flush = nfs_file_flush, .release = nfs_file_release, .fsync = nfs_fsync, .lock = nfs_lock, .flock = nfs_flock, .splice_read = nfs_file_splice_read, .check_flags = nfs_check_flags, .setlease = nfs_setlease,};const struct inode_operations nfs_file_inode_operations = { .permission = nfs_permission, .getattr = nfs_getattr, .setattr = nfs_setattr,};#ifdef CONFIG_NFS_V3const struct inode_operations nfs3_file_inode_operations = { .permission = nfs_permission, .getattr = nfs_getattr, .setattr = nfs_setattr, .listxattr = nfs3_listxattr, .getxattr = nfs3_getxattr, .setxattr = nfs3_setxattr, .removexattr = nfs3_removexattr,};#endif /* CONFIG_NFS_v3 *//* Hack for future NFS swap support */#ifndef IS_SWAPFILE# define IS_SWAPFILE(inode) (0)#endifstatic int nfs_check_flags(int flags){ if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) return -EINVAL; return 0;}/* * Open file */static intnfs_file_open(struct inode *inode, struct file *filp){ int res; res = nfs_check_flags(filp->f_flags); if (res) return res; nfs_inc_stats(inode, NFSIOS_VFSOPEN); lock_kernel(); res = NFS_PROTO(inode)->file_open(inode, filp); unlock_kernel(); return res;}static intnfs_file_release(struct inode *inode, struct file *filp){ /* Ensure that dirty pages are flushed out with the right creds */ if (filp->f_mode & FMODE_WRITE) nfs_wb_all(filp->f_path.dentry->d_inode); nfs_inc_stats(inode, NFSIOS_VFSRELEASE); return NFS_PROTO(inode)->file_release(inode, filp);}/** * nfs_revalidate_size - Revalidate the file size * @inode - pointer to inode struct * @file - pointer to struct file * * Revalidates the file length. This is basically a wrapper around * nfs_revalidate_inode() that takes into account the fact that we may * have cached writes (in which case we don't care about the server's * idea of what the file length is), or O_DIRECT (in which case we * shouldn't trust the cache). */static int nfs_revalidate_file_size(struct inode *inode, struct file *filp){ struct nfs_server *server = NFS_SERVER(inode); struct nfs_inode *nfsi = NFS_I(inode); if (server->flags & NFS_MOUNT_NOAC) goto force_reval; if (filp->f_flags & O_DIRECT) goto force_reval; if (nfsi->npages != 0) return 0; if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode)) return 0;force_reval: return __nfs_revalidate_inode(server, inode);}static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin){ /* origin == SEEK_END => we must revalidate the cached file length */ if (origin == SEEK_END) { struct inode *inode = filp->f_mapping->host; int retval = nfs_revalidate_file_size(inode, filp); if (retval < 0) return (loff_t)retval; } return remote_llseek(filp, offset, origin);}/* * Helper for nfs_file_flush() and nfs_fsync() * * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to * disk, but it retrieves and clears ctx->error after synching, despite * the two being set at the same time in nfs_context_set_write_error(). * This is because the former is used to notify the _next_ call to * nfs_file_write() that a write error occured, and hence cause it to * fall back to doing a synchronous write. */static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode){ int have_error, status; int ret = 0; have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); status = nfs_wb_all(inode); have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); if (have_error) ret = xchg(&ctx->error, 0); if (!ret) ret = status; return ret;}/* * Flush all dirty pages, and check for write errors. * */static intnfs_file_flush(struct file *file, fl_owner_t id){ struct nfs_open_context *ctx = nfs_file_open_context(file); struct inode *inode = file->f_path.dentry->d_inode; int status; dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); if ((file->f_mode & FMODE_WRITE) == 0) return 0; nfs_inc_stats(inode, NFSIOS_VFSFLUSH); /* Ensure that data+attribute caches are up to date after close() */ status = nfs_do_fsync(ctx, inode); if (!status) nfs_revalidate_inode(NFS_SERVER(inode), inode); return status;}static ssize_tnfs_file_read(struct kiocb *iocb, const struct iovec *iov, unsigned long nr_segs, loff_t pos){ struct dentry * dentry = iocb->ki_filp->f_path.dentry; struct inode * inode = dentry->d_inode; ssize_t result; size_t count = iov_length(iov, nr_segs);#ifdef CONFIG_NFS_DIRECTIO if (iocb->ki_filp->f_flags & O_DIRECT) return nfs_file_direct_read(iocb, iov, nr_segs, pos);#endif dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n", dentry->d_parent->d_name.name, dentry->d_name.name, (unsigned long) count, (unsigned long) pos); result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count); if (!result) result = generic_file_aio_read(iocb, iov, nr_segs, pos); return result;}static ssize_tnfs_file_splice_read(struct file *filp, loff_t *ppos, struct pipe_inode_info *pipe, size_t count, unsigned int flags){ struct dentry *dentry = filp->f_path.dentry; struct inode *inode = dentry->d_inode; ssize_t res; dfprintk(VFS, "nfs: splice_read(%s/%s, %lu@%Lu)\n", dentry->d_parent->d_name.name, dentry->d_name.name, (unsigned long) count, (unsigned long long) *ppos); res = nfs_revalidate_mapping(inode, filp->f_mapping); if (!res) res = generic_file_splice_read(filp, ppos, pipe, count, flags); return res;}static intnfs_file_mmap(struct file * file, struct vm_area_struct * vma){ struct dentry *dentry = file->f_path.dentry; struct inode *inode = dentry->d_inode; int status; dfprintk(VFS, "nfs: mmap(%s/%s)\n", dentry->d_parent->d_name.name, dentry->d_name.name); status = nfs_revalidate_mapping(inode, file->f_mapping); if (!status) { vma->vm_ops = &nfs_file_vm_ops; vma->vm_flags |= VM_CAN_NONLINEAR; file_accessed(file); } return status;}/* * Flush any dirty pages for this process, and check for write errors. * The return status from this call provides a reliable indication of * whether any write errors occurred for this process. */static intnfs_fsync(struct file *file, struct dentry *dentry, int datasync){ struct nfs_open_context *ctx = nfs_file_open_context(file); struct inode *inode = dentry->d_inode; dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); nfs_inc_stats(inode, NFSIOS_VFSFSYNC); return nfs_do_fsync(ctx, inode);}/* * This does the "real" work of the write. We must allocate and lock the * page to be sent back to the generic routine, which then copies the * data from user space. * * If the writer ends up delaying the write, the writer needs to * increment the page use counts until he is done with the page. */static int nfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata){ int ret; pgoff_t index; struct page *page; index = pos >> PAGE_CACHE_SHIFT; page = __grab_cache_page(mapping, index); if (!page) return -ENOMEM; *pagep = page; ret = nfs_flush_incompatible(file, page); if (ret) { unlock_page(page); page_cache_release(page); } return ret;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -