]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/nfs/nfs4file.c
nfs: allow intra-file CLONE
[mirror_ubuntu-focal-kernel.git] / fs / nfs / nfs4file.c
CommitLineData
ce4ef7c0
BS
1/*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 */
f4ac1674 6#include <linux/fs.h>
bea51b30 7#include <linux/file.h>
f4ac1674 8#include <linux/falloc.h>
ce4ef7c0 9#include <linux/nfs_fs.h>
5445b1fb 10#include "delegation.h"
ce4ef7c0 11#include "internal.h"
5445b1fb 12#include "iostat.h"
a4ff1468 13#include "fscache.h"
ce4ef7c0
BS
14#include "pnfs.h"
15
81b79afb
TM
16#include "nfstrace.h"
17
1c6dcbe5
AS
18#ifdef CONFIG_NFS_V4_2
19#include "nfs42.h"
20#endif
21
ce4ef7c0
BS
22#define NFSDBG_FACILITY NFSDBG_FILE
23
24static int
25nfs4_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
ce4ef7c0
BS
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
6de1472f 44 dprintk("NFS: open file(%pd2)\n", dentry);
ce4ef7c0 45
18a60089
BC
46 err = nfs_check_flags(openflags);
47 if (err)
48 return err;
49
ce4ef7c0
BS
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);
2b0143b5 57 dir = d_inode(parent);
ce4ef7c0
BS
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;
9e1681c2 68 nfs_sync_inode(inode);
ce4ef7c0
BS
69 }
70
c5c3fb5f 71 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
ce4ef7c0
BS
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 }
2b0143b5 85 if (inode != d_inode(dentry))
ce4ef7c0
BS
86 goto out_drop;
87
88 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
89 nfs_file_set_open_context(filp, ctx);
f1fe29b4 90 nfs_fscache_open_file(inode, filp);
ce4ef7c0
BS
91 err = 0;
92
93out_put_ctx:
94 put_nfs_open_context(ctx);
95out:
96 dput(parent);
97 return err;
98
99out_drop:
100 d_drop(dentry);
101 err = -EOPENSTALE;
102 goto out_put_ctx;
103}
104
5445b1fb
TM
105/*
106 * Flush all dirty pages, and check for write errors.
107 */
108static int
109nfs4_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
ce4ef7c0
BS
130static int
131nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
132{
133 int ret;
496ad9aa 134 struct inode *inode = file_inode(file);
ce4ef7c0 135
81b79afb
TM
136 trace_nfs_fsync_enter(inode);
137
a0815d55 138 nfs_inode_dio_wait(inode);
05990d1b
TM
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);
1b33809e 145 if (!ret)
5bb89b47 146 ret = pnfs_sync_inode(inode, !!datasync);
05990d1b 147 mutex_unlock(&inode->i_mutex);
dcfc4f25
TM
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;
05990d1b
TM
155 } while (ret == -EAGAIN);
156
81b79afb 157 trace_nfs_fsync_exit(inode, ret);
ce4ef7c0
BS
158 return ret;
159}
160
1c6dcbe5
AS
161#ifdef CONFIG_NFS_V4_2
162static 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}
f4ac1674
AS
176
177static 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
624bd5b7 185 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
f4ac1674
AS
186 return -EOPNOTSUPP;
187
188 ret = inode_newsize_ok(inode, offset + len);
189 if (ret < 0)
190 return ret;
191
624bd5b7 192 if (mode & FALLOC_FL_PUNCH_HOLE)
f830f7dd
AS
193 return nfs42_proc_deallocate(filep, offset, len);
194 return nfs42_proc_allocate(filep, offset, len);
f4ac1674 195}
bea51b30
PT
196
197static noinline long
198nfs42_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);
811b7b85 202 struct nfs_server *server = NFS_SERVER(dst_inode);
bea51b30
PT
203 struct fd src_file;
204 struct inode *src_inode;
811b7b85 205 unsigned int bs = server->clone_blksize;
21fad313 206 bool same_inode = false;
bea51b30
PT
207 int ret;
208
209 /* dst file must be opened for writing */
210 if (!(dst_file->f_mode & FMODE_WRITE))
211 return -EINVAL;
212
213 ret = mnt_want_write_file(dst_file);
214 if (ret)
215 return ret;
216
217 src_file = fdget(srcfd);
218 if (!src_file.file) {
219 ret = -EBADF;
220 goto out_drop_write;
221 }
222
223 src_inode = file_inode(src_file.file);
224
bea51b30 225 if (src_inode == dst_inode)
21fad313 226 same_inode = true;
bea51b30
PT
227
228 /* src file must be opened for reading */
229 if (!(src_file.file->f_mode & FMODE_READ))
230 goto out_fput;
231
232 /* src and dst must be regular files */
233 ret = -EISDIR;
234 if (!S_ISREG(src_inode->i_mode) || !S_ISREG(dst_inode->i_mode))
235 goto out_fput;
236
237 ret = -EXDEV;
238 if (src_file.file->f_path.mnt != dst_file->f_path.mnt ||
239 src_inode->i_sb != dst_inode->i_sb)
240 goto out_fput;
241
811b7b85
PT
242 /* check alignment w.r.t. clone_blksize */
243 ret = -EINVAL;
244 if (bs) {
245 if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
246 goto out_fput;
247 if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
248 goto out_fput;
249 }
250
21fad313
CH
251 /* verify if ranges are overlapped within the same file */
252 if (same_inode) {
253 if (dst_off + count > src_off && dst_off < src_off + count)
254 goto out_fput;
255 }
256
bea51b30 257 /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
21fad313
CH
258 if (same_inode) {
259 mutex_lock(&src_inode->i_mutex);
260 } else if (dst_inode < src_inode) {
bea51b30
PT
261 mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT);
262 mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
263 } else {
264 mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
265 mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD);
266 }
267
268 /* flush all pending writes on both src and dst so that server
269 * has the latest data */
270 ret = nfs_sync_inode(src_inode);
271 if (ret)
272 goto out_unlock;
273 ret = nfs_sync_inode(dst_inode);
274 if (ret)
275 goto out_unlock;
276
277 ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count);
278
279 /* truncate inode page cache of the dst range so that future reads can fetch
280 * new data from server */
281 if (!ret)
282 truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
283
284out_unlock:
21fad313
CH
285 if (same_inode) {
286 mutex_unlock(&src_inode->i_mutex);
287 } else if (dst_inode < src_inode) {
bea51b30
PT
288 mutex_unlock(&src_inode->i_mutex);
289 mutex_unlock(&dst_inode->i_mutex);
290 } else {
291 mutex_unlock(&dst_inode->i_mutex);
292 mutex_unlock(&src_inode->i_mutex);
293 }
294out_fput:
295 fdput(src_file);
296out_drop_write:
297 mnt_drop_write_file(dst_file);
298 return ret;
299}
a340abcf
PT
300
301static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
302{
303 struct nfs_ioctl_clone_range_args args;
304
305 if (copy_from_user(&args, argp, sizeof(args)))
306 return -EFAULT;
307
308 return nfs42_ioctl_clone(dst_file, args.src_fd, args.src_off, args.dst_off, args.count);
309}
275058a2
TM
310#else
311static long nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd,
312 u64 src_off, u64 dst_off, u64 count)
313{
314 return -ENOTTY;
315}
316
317static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
318{
319 return -ENOTTY;
320}
1c6dcbe5
AS
321#endif /* CONFIG_NFS_V4_2 */
322
bea51b30
PT
323long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
324{
a340abcf
PT
325 void __user *argp = (void __user *)arg;
326
bea51b30 327 switch (cmd) {
bea51b30
PT
328 case NFS_IOC_CLONE:
329 return nfs42_ioctl_clone(file, arg, 0, 0, 0);
a340abcf
PT
330 case NFS_IOC_CLONE_RANGE:
331 return nfs42_ioctl_clone_range(file, argp);
bea51b30
PT
332 }
333
334 return -ENOTTY;
335}
336
ce4ef7c0 337const struct file_operations nfs4_file_operations = {
1c6dcbe5
AS
338#ifdef CONFIG_NFS_V4_2
339 .llseek = nfs4_file_llseek,
340#else
ce4ef7c0 341 .llseek = nfs_file_llseek,
1c6dcbe5 342#endif
3aa2d199 343 .read_iter = nfs_file_read,
edaf4369 344 .write_iter = nfs_file_write,
ce4ef7c0
BS
345 .mmap = nfs_file_mmap,
346 .open = nfs4_file_open,
5445b1fb 347 .flush = nfs4_file_flush,
ce4ef7c0
BS
348 .release = nfs_file_release,
349 .fsync = nfs4_file_fsync,
350 .lock = nfs_lock,
351 .flock = nfs_flock,
352 .splice_read = nfs_file_splice_read,
4da54c21 353 .splice_write = iter_file_splice_write,
f4ac1674
AS
354#ifdef CONFIG_NFS_V4_2
355 .fallocate = nfs42_fallocate,
356#endif /* CONFIG_NFS_V4_2 */
ce4ef7c0 357 .check_flags = nfs_check_flags,
1c994a09 358 .setlease = simple_nosetlease,
bea51b30 359 .unlocked_ioctl = nfs4_ioctl,
bea51b30 360 .compat_ioctl = nfs4_ioctl,
ce4ef7c0 361};