xref: /openbmc/linux/fs/nfs/nfs4file.c (revision b96fc2f3)
1 /*
2  *  linux/fs/nfs/file.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  */
6 #include <linux/fs.h>
7 #include <linux/falloc.h>
8 #include <linux/nfs_fs.h>
9 #include "delegation.h"
10 #include "internal.h"
11 #include "iostat.h"
12 #include "fscache.h"
13 #include "pnfs.h"
14 
15 #include "nfstrace.h"
16 
17 #ifdef CONFIG_NFS_V4_2
18 #include "nfs42.h"
19 #endif
20 
21 #define NFSDBG_FACILITY		NFSDBG_FILE
22 
23 static int
24 nfs4_file_open(struct inode *inode, struct file *filp)
25 {
26 	struct nfs_open_context *ctx;
27 	struct dentry *dentry = filp->f_path.dentry;
28 	struct dentry *parent = NULL;
29 	struct inode *dir;
30 	unsigned openflags = filp->f_flags;
31 	struct iattr attr;
32 	int err;
33 
34 	/*
35 	 * If no cached dentry exists or if it's negative, NFSv4 handled the
36 	 * opens in ->lookup() or ->create().
37 	 *
38 	 * We only get this far for a cached positive dentry.  We skipped
39 	 * revalidation, so handle it here by dropping the dentry and returning
40 	 * -EOPENSTALE.  The VFS will retry the lookup/create/open.
41 	 */
42 
43 	dprintk("NFS: open file(%pd2)\n", dentry);
44 
45 	err = nfs_check_flags(openflags);
46 	if (err)
47 		return err;
48 
49 	if ((openflags & O_ACCMODE) == 3)
50 		openflags--;
51 
52 	/* We can't create new files here */
53 	openflags &= ~(O_CREAT|O_EXCL);
54 
55 	parent = dget_parent(dentry);
56 	dir = d_inode(parent);
57 
58 	ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
59 	err = PTR_ERR(ctx);
60 	if (IS_ERR(ctx))
61 		goto out;
62 
63 	attr.ia_valid = ATTR_OPEN;
64 	if (openflags & O_TRUNC) {
65 		attr.ia_valid |= ATTR_SIZE;
66 		attr.ia_size = 0;
67 		nfs_sync_inode(inode);
68 	}
69 
70 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
71 	if (IS_ERR(inode)) {
72 		err = PTR_ERR(inode);
73 		switch (err) {
74 		case -EPERM:
75 		case -EACCES:
76 		case -EDQUOT:
77 		case -ENOSPC:
78 		case -EROFS:
79 			goto out_put_ctx;
80 		default:
81 			goto out_drop;
82 		}
83 	}
84 	if (inode != d_inode(dentry))
85 		goto out_drop;
86 
87 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
88 	nfs_file_set_open_context(filp, ctx);
89 	nfs_fscache_open_file(inode, filp);
90 	err = 0;
91 
92 out_put_ctx:
93 	put_nfs_open_context(ctx);
94 out:
95 	dput(parent);
96 	return err;
97 
98 out_drop:
99 	d_drop(dentry);
100 	err = -EOPENSTALE;
101 	goto out_put_ctx;
102 }
103 
104 /*
105  * Flush all dirty pages, and check for write errors.
106  */
107 static int
108 nfs4_file_flush(struct file *file, fl_owner_t id)
109 {
110 	struct inode	*inode = file_inode(file);
111 
112 	dprintk("NFS: flush(%pD2)\n", file);
113 
114 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
115 	if ((file->f_mode & FMODE_WRITE) == 0)
116 		return 0;
117 
118 	/*
119 	 * If we're holding a write delegation, then check if we're required
120 	 * to flush the i/o on close. If not, then just start the i/o now.
121 	 */
122 	if (!nfs4_delegation_flush_on_close(inode))
123 		return filemap_fdatawrite(file->f_mapping);
124 
125 	/* Flush writes to the server and return any errors */
126 	return vfs_fsync(file, 0);
127 }
128 
129 static int
130 nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
131 {
132 	int ret;
133 	struct inode *inode = file_inode(file);
134 
135 	trace_nfs_fsync_enter(inode);
136 
137 	nfs_inode_dio_wait(inode);
138 	do {
139 		ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
140 		if (ret != 0)
141 			break;
142 		mutex_lock(&inode->i_mutex);
143 		ret = nfs_file_fsync_commit(file, start, end, datasync);
144 		if (!ret)
145 			ret = pnfs_sync_inode(inode, !!datasync);
146 		mutex_unlock(&inode->i_mutex);
147 		/*
148 		 * If nfs_file_fsync_commit detected a server reboot, then
149 		 * resend all dirty pages that might have been covered by
150 		 * the NFS_CONTEXT_RESEND_WRITES flag
151 		 */
152 		start = 0;
153 		end = LLONG_MAX;
154 	} while (ret == -EAGAIN);
155 
156 	trace_nfs_fsync_exit(inode, ret);
157 	return ret;
158 }
159 
160 #ifdef CONFIG_NFS_V4_2
161 static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
162 {
163 	loff_t ret;
164 
165 	switch (whence) {
166 	case SEEK_HOLE:
167 	case SEEK_DATA:
168 		ret = nfs42_proc_llseek(filep, offset, whence);
169 		if (ret != -ENOTSUPP)
170 			return ret;
171 	default:
172 		return nfs_file_llseek(filep, offset, whence);
173 	}
174 }
175 
176 static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
177 {
178 	struct inode *inode = file_inode(filep);
179 	long ret;
180 
181 	if (!S_ISREG(inode->i_mode))
182 		return -EOPNOTSUPP;
183 
184 	if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
185 		return -EOPNOTSUPP;
186 
187 	ret = inode_newsize_ok(inode, offset + len);
188 	if (ret < 0)
189 		return ret;
190 
191 	if (mode & FALLOC_FL_PUNCH_HOLE)
192 		return nfs42_proc_deallocate(filep, offset, len);
193 	return nfs42_proc_allocate(filep, offset, len);
194 }
195 #endif /* CONFIG_NFS_V4_2 */
196 
197 const struct file_operations nfs4_file_operations = {
198 #ifdef CONFIG_NFS_V4_2
199 	.llseek		= nfs4_file_llseek,
200 #else
201 	.llseek		= nfs_file_llseek,
202 #endif
203 	.read_iter	= nfs_file_read,
204 	.write_iter	= nfs_file_write,
205 	.mmap		= nfs_file_mmap,
206 	.open		= nfs4_file_open,
207 	.flush		= nfs4_file_flush,
208 	.release	= nfs_file_release,
209 	.fsync		= nfs4_file_fsync,
210 	.lock		= nfs_lock,
211 	.flock		= nfs_flock,
212 	.splice_read	= nfs_file_splice_read,
213 	.splice_write	= iter_file_splice_write,
214 #ifdef CONFIG_NFS_V4_2
215 	.fallocate	= nfs42_fallocate,
216 #endif /* CONFIG_NFS_V4_2 */
217 	.check_flags	= nfs_check_flags,
218 	.setlease	= simple_nosetlease,
219 };
220