4 * Copyright (C) 1992 Rick Sladkey
7 #include <linux/falloc.h>
8 #include <linux/nfs_fs.h>
15 #ifdef CONFIG_NFS_V4_2
19 #define NFSDBG_FACILITY NFSDBG_FILE
22 nfs4_file_open(struct inode *inode, struct file *filp)
24 struct nfs_open_context *ctx;
25 struct dentry *dentry = filp->f_path.dentry;
26 struct dentry *parent = NULL;
28 unsigned openflags = filp->f_flags;
34 * If no cached dentry exists or if it's negative, NFSv4 handled the
35 * opens in ->lookup() or ->create().
37 * We only get this far for a cached positive dentry. We skipped
38 * revalidation, so handle it here by dropping the dentry and returning
39 * -EOPENSTALE. The VFS will retry the lookup/create/open.
42 dprintk("NFS: open file(%pd2)\n", dentry);
44 err = nfs_check_flags(openflags);
48 if ((openflags & O_ACCMODE) == 3)
51 /* We can't create new files here */
52 openflags &= ~(O_CREAT|O_EXCL);
54 parent = dget_parent(dentry);
55 dir = d_inode(parent);
57 ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
62 attr.ia_valid = ATTR_OPEN;
63 if (openflags & O_TRUNC) {
64 attr.ia_valid |= ATTR_SIZE;
66 nfs_sync_inode(inode);
69 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
83 if (inode != d_inode(dentry))
86 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
87 nfs_file_set_open_context(filp, ctx);
88 nfs_fscache_open_file(inode, filp);
92 put_nfs_open_context(ctx);
104 nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
107 struct inode *inode = file_inode(file);
109 trace_nfs_fsync_enter(inode);
111 nfs_inode_dio_wait(inode);
113 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
116 mutex_lock(&inode->i_mutex);
117 ret = nfs_file_fsync_commit(file, start, end, datasync);
119 ret = pnfs_sync_inode(inode, !!datasync);
120 mutex_unlock(&inode->i_mutex);
122 * If nfs_file_fsync_commit detected a server reboot, then
123 * resend all dirty pages that might have been covered by
124 * the NFS_CONTEXT_RESEND_WRITES flag
128 } while (ret == -EAGAIN);
130 trace_nfs_fsync_exit(inode, ret);
134 #ifdef CONFIG_NFS_V4_2
135 static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
142 ret = nfs42_proc_llseek(filep, offset, whence);
143 if (ret != -ENOTSUPP)
146 return nfs_file_llseek(filep, offset, whence);
150 static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
152 struct inode *inode = file_inode(filep);
155 if (!S_ISREG(inode->i_mode))
158 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
161 ret = inode_newsize_ok(inode, offset + len);
165 if (mode & FALLOC_FL_PUNCH_HOLE)
166 return nfs42_proc_deallocate(filep, offset, len);
167 return nfs42_proc_allocate(filep, offset, len);
169 #endif /* CONFIG_NFS_V4_2 */
171 const struct file_operations nfs4_file_operations = {
172 #ifdef CONFIG_NFS_V4_2
173 .llseek = nfs4_file_llseek,
175 .llseek = nfs_file_llseek,
177 .read_iter = nfs_file_read,
178 .write_iter = nfs_file_write,
179 .mmap = nfs_file_mmap,
180 .open = nfs4_file_open,
181 .flush = nfs_file_flush,
182 .release = nfs_file_release,
183 .fsync = nfs4_file_fsync,
186 .splice_read = nfs_file_splice_read,
187 .splice_write = iter_file_splice_write,
188 #ifdef CONFIG_NFS_V4_2
189 .fallocate = nfs42_fallocate,
190 #endif /* CONFIG_NFS_V4_2 */
191 .check_flags = nfs_check_flags,
192 .setlease = simple_nosetlease,