1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/nfs/file.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  */
7 #include <linux/fs.h>
8 #include <linux/file.h>
9 #include <linux/falloc.h>
10 #include <linux/nfs_fs.h>
11 #include "delegation.h"
12 #include "internal.h"
13 #include "iostat.h"
14 #include "fscache.h"
15 #include "pnfs.h"
16 
17 #include "nfstrace.h"
18 
19 #ifdef CONFIG_NFS_V4_2
20 #include "nfs42.h"
21 #endif
22 
23 #define NFSDBG_FACILITY		NFSDBG_FILE
24 
25 static int
nfs4_file_open(struct inode * inode,struct file * filp)26 nfs4_file_open(struct inode *inode, struct file *filp)
27 {
28 	struct nfs_open_context *ctx;
29 	struct dentry *dentry = file_dentry(filp);
30 	struct dentry *parent = NULL;
31 	struct inode *dir;
32 	unsigned openflags = filp->f_flags;
33 	struct iattr attr;
34 	int err;
35 
36 	/*
37 	 * If no cached dentry exists or if it's negative, NFSv4 handled the
38 	 * opens in ->lookup() or ->create().
39 	 *
40 	 * We only get this far for a cached positive dentry.  We skipped
41 	 * revalidation, so handle it here by dropping the dentry and returning
42 	 * -EOPENSTALE.  The VFS will retry the lookup/create/open.
43 	 */
44 
45 	dprintk("NFS: open file(%pd2)\n", dentry);
46 
47 	err = nfs_check_flags(openflags);
48 	if (err)
49 		return err;
50 
51 	if ((openflags & O_ACCMODE) == 3)
52 		return nfs_open(inode, filp);
53 
54 	/* We can't create new files here */
55 	openflags &= ~(O_CREAT|O_EXCL);
56 
57 	parent = dget_parent(dentry);
58 	dir = d_inode(parent);
59 
60 	ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
61 	err = PTR_ERR(ctx);
62 	if (IS_ERR(ctx))
63 		goto out;
64 
65 	attr.ia_valid = ATTR_OPEN;
66 	if (openflags & O_TRUNC) {
67 		attr.ia_valid |= ATTR_SIZE;
68 		attr.ia_size = 0;
69 		filemap_write_and_wait(inode->i_mapping);
70 	}
71 
72 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
73 	if (IS_ERR(inode)) {
74 		err = PTR_ERR(inode);
75 		switch (err) {
76 		default:
77 			goto out_put_ctx;
78 		case -ENOENT:
79 		case -ESTALE:
80 		case -EISDIR:
81 		case -ENOTDIR:
82 		case -ELOOP:
83 			goto out_drop;
84 		}
85 	}
86 	if (inode != d_inode(dentry))
87 		goto out_drop;
88 
89 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
90 	nfs_file_set_open_context(filp, ctx);
91 	nfs_fscache_open_file(inode, filp);
92 	err = 0;
93 
94 out_put_ctx:
95 	put_nfs_open_context(ctx);
96 out:
97 	dput(parent);
98 	return err;
99 
100 out_drop:
101 	d_drop(dentry);
102 	err = -EOPENSTALE;
103 	goto out_put_ctx;
104 }
105 
106 /*
107  * Flush all dirty pages, and check for write errors.
108  */
109 static int
nfs4_file_flush(struct file * file,fl_owner_t id)110 nfs4_file_flush(struct file *file, fl_owner_t id)
111 {
112 	struct inode	*inode = file_inode(file);
113 
114 	dprintk("NFS: flush(%pD2)\n", file);
115 
116 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
117 	if ((file->f_mode & FMODE_WRITE) == 0)
118 		return 0;
119 
120 	/*
121 	 * If we're holding a write delegation, then check if we're required
122 	 * to flush the i/o on close. If not, then just start the i/o now.
123 	 */
124 	if (!nfs4_delegation_flush_on_close(inode))
125 		return filemap_fdatawrite(file->f_mapping);
126 
127 	/* Flush writes to the server and return any errors */
128 	return vfs_fsync(file, 0);
129 }
130 
131 #ifdef CONFIG_NFS_V4_2
nfs4_copy_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,size_t count,unsigned int flags)132 static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
133 				    struct file *file_out, loff_t pos_out,
134 				    size_t count, unsigned int flags)
135 {
136 	if (!nfs_server_capable(file_inode(file_out), NFS_CAP_COPY))
137 		return -EOPNOTSUPP;
138 	if (file_inode(file_in) == file_inode(file_out))
139 		return -EOPNOTSUPP;
140 	return nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count);
141 }
142 
nfs4_file_llseek(struct file * filep,loff_t offset,int whence)143 static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
144 {
145 	loff_t ret;
146 
147 	switch (whence) {
148 	case SEEK_HOLE:
149 	case SEEK_DATA:
150 		ret = nfs42_proc_llseek(filep, offset, whence);
151 		if (ret != -EOPNOTSUPP)
152 			return ret;
153 		/* Fall through */
154 	default:
155 		return nfs_file_llseek(filep, offset, whence);
156 	}
157 }
158 
nfs42_fallocate(struct file * filep,int mode,loff_t offset,loff_t len)159 static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
160 {
161 	struct inode *inode = file_inode(filep);
162 	long ret;
163 
164 	if (!S_ISREG(inode->i_mode))
165 		return -EOPNOTSUPP;
166 
167 	if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
168 		return -EOPNOTSUPP;
169 
170 	ret = inode_newsize_ok(inode, offset + len);
171 	if (ret < 0)
172 		return ret;
173 
174 	if (mode & FALLOC_FL_PUNCH_HOLE)
175 		return nfs42_proc_deallocate(filep, offset, len);
176 	return nfs42_proc_allocate(filep, offset, len);
177 }
178 
nfs42_clone_file_range(struct file * src_file,loff_t src_off,struct file * dst_file,loff_t dst_off,u64 count)179 static int nfs42_clone_file_range(struct file *src_file, loff_t src_off,
180 		struct file *dst_file, loff_t dst_off, u64 count)
181 {
182 	struct inode *dst_inode = file_inode(dst_file);
183 	struct nfs_server *server = NFS_SERVER(dst_inode);
184 	struct inode *src_inode = file_inode(src_file);
185 	unsigned int bs = server->clone_blksize;
186 	bool same_inode = false;
187 	int ret;
188 
189 	/* check alignment w.r.t. clone_blksize */
190 	ret = -EINVAL;
191 	if (bs) {
192 		if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
193 			goto out;
194 		if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
195 			goto out;
196 	}
197 
198 	if (src_inode == dst_inode)
199 		same_inode = true;
200 
201 	/* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
202 	if (same_inode) {
203 		inode_lock(src_inode);
204 	} else if (dst_inode < src_inode) {
205 		inode_lock_nested(dst_inode, I_MUTEX_PARENT);
206 		inode_lock_nested(src_inode, I_MUTEX_CHILD);
207 	} else {
208 		inode_lock_nested(src_inode, I_MUTEX_PARENT);
209 		inode_lock_nested(dst_inode, I_MUTEX_CHILD);
210 	}
211 
212 	/* flush all pending writes on both src and dst so that server
213 	 * has the latest data */
214 	ret = nfs_sync_inode(src_inode);
215 	if (ret)
216 		goto out_unlock;
217 	ret = nfs_sync_inode(dst_inode);
218 	if (ret)
219 		goto out_unlock;
220 
221 	ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
222 
223 	/* truncate inode page cache of the dst range so that future reads can fetch
224 	 * new data from server */
225 	if (!ret)
226 		truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
227 
228 out_unlock:
229 	if (same_inode) {
230 		inode_unlock(src_inode);
231 	} else if (dst_inode < src_inode) {
232 		inode_unlock(src_inode);
233 		inode_unlock(dst_inode);
234 	} else {
235 		inode_unlock(dst_inode);
236 		inode_unlock(src_inode);
237 	}
238 out:
239 	return ret;
240 }
241 #endif /* CONFIG_NFS_V4_2 */
242 
243 const struct file_operations nfs4_file_operations = {
244 	.read_iter	= nfs_file_read,
245 	.write_iter	= nfs_file_write,
246 	.mmap		= nfs_file_mmap,
247 	.open		= nfs4_file_open,
248 	.flush		= nfs4_file_flush,
249 	.release	= nfs_file_release,
250 	.fsync		= nfs_file_fsync,
251 	.lock		= nfs_lock,
252 	.flock		= nfs_flock,
253 	.splice_read	= generic_file_splice_read,
254 	.splice_write	= iter_file_splice_write,
255 	.check_flags	= nfs_check_flags,
256 	.setlease	= simple_nosetlease,
257 #ifdef CONFIG_NFS_V4_2
258 	.copy_file_range = nfs4_copy_file_range,
259 	.llseek		= nfs4_file_llseek,
260 	.fallocate	= nfs42_fallocate,
261 	.clone_file_range = nfs42_clone_file_range,
262 #else
263 	.llseek		= nfs_file_llseek,
264 #endif
265 };
266