xref: /openbmc/linux/fs/nfs/nfs4file.c (revision 9cfc5c90)
1 /*
2  *  linux/fs/nfs/file.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  */
6 #include <linux/fs.h>
7 #include <linux/file.h>
8 #include <linux/falloc.h>
9 #include <linux/nfs_fs.h>
10 #include "delegation.h"
11 #include "internal.h"
12 #include "iostat.h"
13 #include "fscache.h"
14 #include "pnfs.h"
15 
16 #include "nfstrace.h"
17 
18 #ifdef CONFIG_NFS_V4_2
19 #include "nfs42.h"
20 #endif
21 
22 #define NFSDBG_FACILITY		NFSDBG_FILE
23 
24 static int
25 nfs4_file_open(struct inode *inode, struct file *filp)
26 {
27 	struct nfs_open_context *ctx;
28 	struct dentry *dentry = filp->f_path.dentry;
29 	struct dentry *parent = NULL;
30 	struct inode *dir;
31 	unsigned openflags = filp->f_flags;
32 	struct iattr attr;
33 	int err;
34 
35 	/*
36 	 * If no cached dentry exists or if it's negative, NFSv4 handled the
37 	 * opens in ->lookup() or ->create().
38 	 *
39 	 * We only get this far for a cached positive dentry.  We skipped
40 	 * revalidation, so handle it here by dropping the dentry and returning
41 	 * -EOPENSTALE.  The VFS will retry the lookup/create/open.
42 	 */
43 
44 	dprintk("NFS: open file(%pd2)\n", dentry);
45 
46 	err = nfs_check_flags(openflags);
47 	if (err)
48 		return err;
49 
50 	if ((openflags & O_ACCMODE) == 3)
51 		openflags--;
52 
53 	/* We can't create new files here */
54 	openflags &= ~(O_CREAT|O_EXCL);
55 
56 	parent = dget_parent(dentry);
57 	dir = d_inode(parent);
58 
59 	ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
60 	err = PTR_ERR(ctx);
61 	if (IS_ERR(ctx))
62 		goto out;
63 
64 	attr.ia_valid = ATTR_OPEN;
65 	if (openflags & O_TRUNC) {
66 		attr.ia_valid |= ATTR_SIZE;
67 		attr.ia_size = 0;
68 		nfs_sync_inode(inode);
69 	}
70 
71 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
72 	if (IS_ERR(inode)) {
73 		err = PTR_ERR(inode);
74 		switch (err) {
75 		case -EPERM:
76 		case -EACCES:
77 		case -EDQUOT:
78 		case -ENOSPC:
79 		case -EROFS:
80 			goto out_put_ctx;
81 		default:
82 			goto out_drop;
83 		}
84 	}
85 	if (inode != d_inode(dentry))
86 		goto out_drop;
87 
88 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
89 	nfs_file_set_open_context(filp, ctx);
90 	nfs_fscache_open_file(inode, filp);
91 	err = 0;
92 
93 out_put_ctx:
94 	put_nfs_open_context(ctx);
95 out:
96 	dput(parent);
97 	return err;
98 
99 out_drop:
100 	d_drop(dentry);
101 	err = -EOPENSTALE;
102 	goto out_put_ctx;
103 }
104 
105 /*
106  * Flush all dirty pages, and check for write errors.
107  */
108 static int
109 nfs4_file_flush(struct file *file, fl_owner_t id)
110 {
111 	struct inode	*inode = file_inode(file);
112 
113 	dprintk("NFS: flush(%pD2)\n", file);
114 
115 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
116 	if ((file->f_mode & FMODE_WRITE) == 0)
117 		return 0;
118 
119 	/*
120 	 * If we're holding a write delegation, then check if we're required
121 	 * to flush the i/o on close. If not, then just start the i/o now.
122 	 */
123 	if (!nfs4_delegation_flush_on_close(inode))
124 		return filemap_fdatawrite(file->f_mapping);
125 
126 	/* Flush writes to the server and return any errors */
127 	return vfs_fsync(file, 0);
128 }
129 
130 static int
131 nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
132 {
133 	int ret;
134 	struct inode *inode = file_inode(file);
135 
136 	trace_nfs_fsync_enter(inode);
137 
138 	nfs_inode_dio_wait(inode);
139 	do {
140 		ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
141 		if (ret != 0)
142 			break;
143 		mutex_lock(&inode->i_mutex);
144 		ret = nfs_file_fsync_commit(file, start, end, datasync);
145 		if (!ret)
146 			ret = pnfs_sync_inode(inode, !!datasync);
147 		mutex_unlock(&inode->i_mutex);
148 		/*
149 		 * If nfs_file_fsync_commit detected a server reboot, then
150 		 * resend all dirty pages that might have been covered by
151 		 * the NFS_CONTEXT_RESEND_WRITES flag
152 		 */
153 		start = 0;
154 		end = LLONG_MAX;
155 	} while (ret == -EAGAIN);
156 
157 	trace_nfs_fsync_exit(inode, ret);
158 	return ret;
159 }
160 
161 #ifdef CONFIG_NFS_V4_2
162 static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
163 {
164 	loff_t ret;
165 
166 	switch (whence) {
167 	case SEEK_HOLE:
168 	case SEEK_DATA:
169 		ret = nfs42_proc_llseek(filep, offset, whence);
170 		if (ret != -ENOTSUPP)
171 			return ret;
172 	default:
173 		return nfs_file_llseek(filep, offset, whence);
174 	}
175 }
176 
177 static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
178 {
179 	struct inode *inode = file_inode(filep);
180 	long ret;
181 
182 	if (!S_ISREG(inode->i_mode))
183 		return -EOPNOTSUPP;
184 
185 	if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
186 		return -EOPNOTSUPP;
187 
188 	ret = inode_newsize_ok(inode, offset + len);
189 	if (ret < 0)
190 		return ret;
191 
192 	if (mode & FALLOC_FL_PUNCH_HOLE)
193 		return nfs42_proc_deallocate(filep, offset, len);
194 	return nfs42_proc_allocate(filep, offset, len);
195 }
196 
197 static noinline long
198 nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd,
199 		  u64 src_off, u64 dst_off, u64 count)
200 {
201 	struct inode *dst_inode = file_inode(dst_file);
202 	struct nfs_server *server = NFS_SERVER(dst_inode);
203 	struct fd src_file;
204 	struct inode *src_inode;
205 	unsigned int bs = server->clone_blksize;
206 	int ret;
207 
208 	/* dst file must be opened for writing */
209 	if (!(dst_file->f_mode & FMODE_WRITE))
210 		return -EINVAL;
211 
212 	ret = mnt_want_write_file(dst_file);
213 	if (ret)
214 		return ret;
215 
216 	src_file = fdget(srcfd);
217 	if (!src_file.file) {
218 		ret = -EBADF;
219 		goto out_drop_write;
220 	}
221 
222 	src_inode = file_inode(src_file.file);
223 
224 	/* src and dst must be different files */
225 	ret = -EINVAL;
226 	if (src_inode == dst_inode)
227 		goto out_fput;
228 
229 	/* src file must be opened for reading */
230 	if (!(src_file.file->f_mode & FMODE_READ))
231 		goto out_fput;
232 
233 	/* src and dst must be regular files */
234 	ret = -EISDIR;
235 	if (!S_ISREG(src_inode->i_mode) || !S_ISREG(dst_inode->i_mode))
236 		goto out_fput;
237 
238 	ret = -EXDEV;
239 	if (src_file.file->f_path.mnt != dst_file->f_path.mnt ||
240 	    src_inode->i_sb != dst_inode->i_sb)
241 		goto out_fput;
242 
243 	/* check alignment w.r.t. clone_blksize */
244 	ret = -EINVAL;
245 	if (bs) {
246 		if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
247 			goto out_fput;
248 		if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
249 			goto out_fput;
250 	}
251 
252 	/* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
253 	if (dst_inode < src_inode) {
254 		mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT);
255 		mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
256 	} else {
257 		mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
258 		mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD);
259 	}
260 
261 	/* flush all pending writes on both src and dst so that server
262 	 * has the latest data */
263 	ret = nfs_sync_inode(src_inode);
264 	if (ret)
265 		goto out_unlock;
266 	ret = nfs_sync_inode(dst_inode);
267 	if (ret)
268 		goto out_unlock;
269 
270 	ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count);
271 
272 	/* truncate inode page cache of the dst range so that future reads can fetch
273 	 * new data from server */
274 	if (!ret)
275 		truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
276 
277 out_unlock:
278 	if (dst_inode < src_inode) {
279 		mutex_unlock(&src_inode->i_mutex);
280 		mutex_unlock(&dst_inode->i_mutex);
281 	} else {
282 		mutex_unlock(&dst_inode->i_mutex);
283 		mutex_unlock(&src_inode->i_mutex);
284 	}
285 out_fput:
286 	fdput(src_file);
287 out_drop_write:
288 	mnt_drop_write_file(dst_file);
289 	return ret;
290 }
291 
292 static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
293 {
294 	struct nfs_ioctl_clone_range_args args;
295 
296 	if (copy_from_user(&args, argp, sizeof(args)))
297 		return -EFAULT;
298 
299 	return nfs42_ioctl_clone(dst_file, args.src_fd, args.src_off, args.dst_off, args.count);
300 }
301 #else
302 static long nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd,
303 		u64 src_off, u64 dst_off, u64 count)
304 {
305 	return -ENOTTY;
306 }
307 
308 static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
309 {
310 	return -ENOTTY;
311 }
312 #endif /* CONFIG_NFS_V4_2 */
313 
314 long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
315 {
316 	void __user *argp = (void __user *)arg;
317 
318 	switch (cmd) {
319 	case NFS_IOC_CLONE:
320 		return nfs42_ioctl_clone(file, arg, 0, 0, 0);
321 	case NFS_IOC_CLONE_RANGE:
322 		return nfs42_ioctl_clone_range(file, argp);
323 	}
324 
325 	return -ENOTTY;
326 }
327 
328 const struct file_operations nfs4_file_operations = {
329 #ifdef CONFIG_NFS_V4_2
330 	.llseek		= nfs4_file_llseek,
331 #else
332 	.llseek		= nfs_file_llseek,
333 #endif
334 	.read_iter	= nfs_file_read,
335 	.write_iter	= nfs_file_write,
336 	.mmap		= nfs_file_mmap,
337 	.open		= nfs4_file_open,
338 	.flush		= nfs4_file_flush,
339 	.release	= nfs_file_release,
340 	.fsync		= nfs4_file_fsync,
341 	.lock		= nfs_lock,
342 	.flock		= nfs_flock,
343 	.splice_read	= nfs_file_splice_read,
344 	.splice_write	= iter_file_splice_write,
345 #ifdef CONFIG_NFS_V4_2
346 	.fallocate	= nfs42_fallocate,
347 #endif /* CONFIG_NFS_V4_2 */
348 	.check_flags	= nfs_check_flags,
349 	.setlease	= simple_nosetlease,
350 #ifdef CONFIG_COMPAT
351 	.unlocked_ioctl = nfs4_ioctl,
352 #else
353 	.compat_ioctl	= nfs4_ioctl,
354 #endif /* CONFIG_COMPAT */
355 };
356