blob: 61f1c1c02d06b82126b25f308f79e1f43de6da9c [file] [log] [blame]
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -04001/*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 */
Anna Schumakerf4ac1672014-11-25 13:18:15 -05006#include <linux/fs.h>
Peng Taobea51b32015-09-26 02:24:36 +08007#include <linux/file.h>
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008#include <linux/falloc.h>
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -04009#include <linux/nfs_fs.h>
Trond Myklebust5445b1f2015-09-05 19:06:58 -040010#include "delegation.h"
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040011#include "internal.h"
Trond Myklebust5445b1f2015-09-05 19:06:58 -040012#include "iostat.h"
David Howellsa4ff1462012-12-05 16:31:49 +000013#include "fscache.h"
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040014#include "pnfs.h"
15
Trond Myklebust81b79af2015-03-25 19:09:08 -040016#include "nfstrace.h"
17
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040018#ifdef CONFIG_NFS_V4_2
19#include "nfs42.h"
20#endif
21
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040022#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
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040035 /*
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
Al Viro6de14722013-09-16 10:53:17 -040044 dprintk("NFS: open file(%pd2)\n", dentry);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040045
Benjamin Coddington18a60082015-06-25 09:25:50 -040046 err = nfs_check_flags(openflags);
47 if (err)
48 return err;
49
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040050 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);
David Howells2b0143b2015-03-17 22:25:59 +000057 dir = d_inode(parent);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040058
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;
Trond Myklebust9e1681c2015-03-25 17:23:31 -040068 nfs_sync_inode(inode);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040069 }
70
Kinglong Meec5c3fb52015-08-26 21:11:39 +080071 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040072 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 }
David Howells2b0143b2015-03-17 22:25:59 +000085 if (inode != d_inode(dentry))
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040086 goto out_drop;
87
88 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
89 nfs_file_set_open_context(filp, ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +010090 nfs_fscache_open_file(inode, filp);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040091 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
Trond Myklebust5445b1f2015-09-05 19:06:58 -0400105/*
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
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400130static int
131nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
132{
133 int ret;
Al Viro496ad9a2013-01-23 17:07:38 -0500134 struct inode *inode = file_inode(file);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400135
Trond Myklebust81b79af2015-03-25 19:09:08 -0400136 trace_nfs_fsync_enter(inode);
137
Trond Myklebusta0815d52015-03-25 18:58:53 -0400138 nfs_inode_dio_wait(inode);
Trond Myklebust05990d12012-09-11 16:01:22 -0400139 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);
Christoph Hellwig1b338092014-04-21 10:29:17 -0700145 if (!ret)
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400146 ret = pnfs_sync_inode(inode, !!datasync);
Trond Myklebust05990d12012-09-11 16:01:22 -0400147 mutex_unlock(&inode->i_mutex);
Trond Myklebustdcfc4f22012-09-11 16:19:38 -0400148 /*
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;
Trond Myklebust05990d12012-09-11 16:01:22 -0400155 } while (ret == -EAGAIN);
156
Trond Myklebust81b79af2015-03-25 19:09:08 -0400157 trace_nfs_fsync_exit(inode, ret);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400158 return ret;
159}
160
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400161#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}
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500176
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
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500185 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500186 return -EOPNOTSUPP;
187
188 ret = inode_newsize_ok(inode, offset + len);
189 if (ret < 0)
190 return ret;
191
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500192 if (mode & FALLOC_FL_PUNCH_HOLE)
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400193 return nfs42_proc_deallocate(filep, offset, len);
194 return nfs42_proc_allocate(filep, offset, len);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500195}
Peng Taobea51b32015-09-26 02:24:36 +0800196
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);
Peng Tao811b7b82015-09-26 02:24:38 +0800202 struct nfs_server *server = NFS_SERVER(dst_inode);
Peng Taobea51b32015-09-26 02:24:36 +0800203 struct fd src_file;
204 struct inode *src_inode;
Peng Tao811b7b82015-09-26 02:24:38 +0800205 unsigned int bs = server->clone_blksize;
Christoph Hellwig21fad3132015-11-13 09:38:47 +0100206 bool same_inode = false;
Peng Taobea51b32015-09-26 02:24:36 +0800207 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
Peng Taobea51b32015-09-26 02:24:36 +0800225 if (src_inode == dst_inode)
Christoph Hellwig21fad3132015-11-13 09:38:47 +0100226 same_inode = true;
Peng Taobea51b32015-09-26 02:24:36 +0800227
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
Peng Tao811b7b82015-09-26 02:24:38 +0800242 /* 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
Christoph Hellwig21fad3132015-11-13 09:38:47 +0100251 /* 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
Peng Taobea51b32015-09-26 02:24:36 +0800257 /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
Christoph Hellwig21fad3132015-11-13 09:38:47 +0100258 if (same_inode) {
259 mutex_lock(&src_inode->i_mutex);
260 } else if (dst_inode < src_inode) {
Peng Taobea51b32015-09-26 02:24:36 +0800261 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:
Christoph Hellwig21fad3132015-11-13 09:38:47 +0100285 if (same_inode) {
286 mutex_unlock(&src_inode->i_mutex);
287 } else if (dst_inode < src_inode) {
Peng Taobea51b32015-09-26 02:24:36 +0800288 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}
Peng Taoa340abc2015-09-26 02:24:39 +0800300
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}
Trond Myklebust275058a2015-10-15 16:20:11 -0400310#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}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400321#endif /* CONFIG_NFS_V4_2 */
322
Peng Taobea51b32015-09-26 02:24:36 +0800323long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
324{
Peng Taoa340abc2015-09-26 02:24:39 +0800325 void __user *argp = (void __user *)arg;
326
Peng Taobea51b32015-09-26 02:24:36 +0800327 switch (cmd) {
Peng Taobea51b32015-09-26 02:24:36 +0800328 case NFS_IOC_CLONE:
329 return nfs42_ioctl_clone(file, arg, 0, 0, 0);
Peng Taoa340abc2015-09-26 02:24:39 +0800330 case NFS_IOC_CLONE_RANGE:
331 return nfs42_ioctl_clone_range(file, argp);
Peng Taobea51b32015-09-26 02:24:36 +0800332 }
333
334 return -ENOTTY;
335}
336
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400337const struct file_operations nfs4_file_operations = {
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400338#ifdef CONFIG_NFS_V4_2
339 .llseek = nfs4_file_llseek,
340#else
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400341 .llseek = nfs_file_llseek,
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400342#endif
Al Viro3aa2d192014-04-02 20:14:12 -0400343 .read_iter = nfs_file_read,
Al Viroedaf4362014-04-03 14:07:25 -0400344 .write_iter = nfs_file_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400345 .mmap = nfs_file_mmap,
346 .open = nfs4_file_open,
Trond Myklebust5445b1f2015-09-05 19:06:58 -0400347 .flush = nfs4_file_flush,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400348 .release = nfs_file_release,
349 .fsync = nfs4_file_fsync,
350 .lock = nfs_lock,
351 .flock = nfs_flock,
352 .splice_read = nfs_file_splice_read,
Al Viro4da54c22014-04-05 04:37:17 -0400353 .splice_write = iter_file_splice_write,
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500354#ifdef CONFIG_NFS_V4_2
355 .fallocate = nfs42_fallocate,
356#endif /* CONFIG_NFS_V4_2 */
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400357 .check_flags = nfs_check_flags,
Jeff Layton1c994a02014-08-27 06:49:41 -0400358 .setlease = simple_nosetlease,
Peng Taobea51b32015-09-26 02:24:36 +0800359 .unlocked_ioctl = nfs4_ioctl,
Peng Taobea51b32015-09-26 02:24:36 +0800360 .compat_ioctl = nfs4_ioctl,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400361};