blob: b0dbe0abed53631a22e938b332b675b501b9e978 [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>
7#include <linux/falloc.h>
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -04008#include <linux/nfs_fs.h>
Trond Myklebust5445b1f2015-09-05 19:06:58 -04009#include "delegation.h"
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040010#include "internal.h"
Trond Myklebust5445b1f2015-09-05 19:06:58 -040011#include "iostat.h"
David Howellsa4ff1462012-12-05 16:31:49 +000012#include "fscache.h"
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040013#include "pnfs.h"
14
Trond Myklebust81b79af2015-03-25 19:09:08 -040015#include "nfstrace.h"
16
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040017#ifdef CONFIG_NFS_V4_2
18#include "nfs42.h"
19#endif
20
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040021#define NFSDBG_FACILITY NFSDBG_FILE
22
23static int
24nfs4_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
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040034 /*
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
Al Viro6de14722013-09-16 10:53:17 -040043 dprintk("NFS: open file(%pd2)\n", dentry);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040044
Benjamin Coddington18a60082015-06-25 09:25:50 -040045 err = nfs_check_flags(openflags);
46 if (err)
47 return err;
48
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040049 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);
David Howells2b0143b2015-03-17 22:25:59 +000056 dir = d_inode(parent);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040057
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;
Trond Myklebust9e1681c2015-03-25 17:23:31 -040067 nfs_sync_inode(inode);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040068 }
69
Kinglong Meec5c3fb52015-08-26 21:11:39 +080070 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040071 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 }
David Howells2b0143b2015-03-17 22:25:59 +000084 if (inode != d_inode(dentry))
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040085 goto out_drop;
86
87 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
88 nfs_file_set_open_context(filp, ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +010089 nfs_fscache_open_file(inode, filp);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040090 err = 0;
91
92out_put_ctx:
93 put_nfs_open_context(ctx);
94out:
95 dput(parent);
96 return err;
97
98out_drop:
99 d_drop(dentry);
100 err = -EOPENSTALE;
101 goto out_put_ctx;
102}
103
Trond Myklebust5445b1f2015-09-05 19:06:58 -0400104/*
105 * Flush all dirty pages, and check for write errors.
106 */
107static int
108nfs4_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
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400129static int
130nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
131{
132 int ret;
Al Viro496ad9a2013-01-23 17:07:38 -0500133 struct inode *inode = file_inode(file);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400134
Trond Myklebust81b79af2015-03-25 19:09:08 -0400135 trace_nfs_fsync_enter(inode);
136
Trond Myklebusta0815d52015-03-25 18:58:53 -0400137 nfs_inode_dio_wait(inode);
Trond Myklebust05990d12012-09-11 16:01:22 -0400138 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);
Christoph Hellwig1b338092014-04-21 10:29:17 -0700144 if (!ret)
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400145 ret = pnfs_sync_inode(inode, !!datasync);
Trond Myklebust05990d12012-09-11 16:01:22 -0400146 mutex_unlock(&inode->i_mutex);
Trond Myklebustdcfc4f22012-09-11 16:19:38 -0400147 /*
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;
Trond Myklebust05990d12012-09-11 16:01:22 -0400154 } while (ret == -EAGAIN);
155
Trond Myklebust81b79af2015-03-25 19:09:08 -0400156 trace_nfs_fsync_exit(inode, ret);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400157 return ret;
158}
159
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400160#ifdef CONFIG_NFS_V4_2
161static 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}
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500175
176static 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
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500184 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500185 return -EOPNOTSUPP;
186
187 ret = inode_newsize_ok(inode, offset + len);
188 if (ret < 0)
189 return ret;
190
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500191 if (mode & FALLOC_FL_PUNCH_HOLE)
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400192 return nfs42_proc_deallocate(filep, offset, len);
193 return nfs42_proc_allocate(filep, offset, len);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500194}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400195#endif /* CONFIG_NFS_V4_2 */
196
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400197const struct file_operations nfs4_file_operations = {
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400198#ifdef CONFIG_NFS_V4_2
199 .llseek = nfs4_file_llseek,
200#else
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400201 .llseek = nfs_file_llseek,
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400202#endif
Al Viro3aa2d192014-04-02 20:14:12 -0400203 .read_iter = nfs_file_read,
Al Viroedaf4362014-04-03 14:07:25 -0400204 .write_iter = nfs_file_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400205 .mmap = nfs_file_mmap,
206 .open = nfs4_file_open,
Trond Myklebust5445b1f2015-09-05 19:06:58 -0400207 .flush = nfs4_file_flush,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400208 .release = nfs_file_release,
209 .fsync = nfs4_file_fsync,
210 .lock = nfs_lock,
211 .flock = nfs_flock,
212 .splice_read = nfs_file_splice_read,
Al Viro4da54c22014-04-05 04:37:17 -0400213 .splice_write = iter_file_splice_write,
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500214#ifdef CONFIG_NFS_V4_2
215 .fallocate = nfs42_fallocate,
216#endif /* CONFIG_NFS_V4_2 */
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400217 .check_flags = nfs_check_flags,
Jeff Layton1c994a02014-08-27 06:49:41 -0400218 .setlease = simple_nosetlease,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400219};