blob: fc68ba5f030cfea044e609df8da768b9e6f992cb [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);
202 struct fd src_file;
203 struct inode *src_inode;
204 int ret;
205
206 /* dst file must be opened for writing */
207 if (!(dst_file->f_mode & FMODE_WRITE))
208 return -EINVAL;
209
210 ret = mnt_want_write_file(dst_file);
211 if (ret)
212 return ret;
213
214 src_file = fdget(srcfd);
215 if (!src_file.file) {
216 ret = -EBADF;
217 goto out_drop_write;
218 }
219
220 src_inode = file_inode(src_file.file);
221
222 /* src and dst must be different files */
223 ret = -EINVAL;
224 if (src_inode == dst_inode)
225 goto out_fput;
226
227 /* src file must be opened for reading */
228 if (!(src_file.file->f_mode & FMODE_READ))
229 goto out_fput;
230
231 /* src and dst must be regular files */
232 ret = -EISDIR;
233 if (!S_ISREG(src_inode->i_mode) || !S_ISREG(dst_inode->i_mode))
234 goto out_fput;
235
236 ret = -EXDEV;
237 if (src_file.file->f_path.mnt != dst_file->f_path.mnt ||
238 src_inode->i_sb != dst_inode->i_sb)
239 goto out_fput;
240
241 /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
242 if (dst_inode < src_inode) {
243 mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT);
244 mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
245 } else {
246 mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
247 mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD);
248 }
249
250 /* flush all pending writes on both src and dst so that server
251 * has the latest data */
252 ret = nfs_sync_inode(src_inode);
253 if (ret)
254 goto out_unlock;
255 ret = nfs_sync_inode(dst_inode);
256 if (ret)
257 goto out_unlock;
258
259 ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count);
260
261 /* truncate inode page cache of the dst range so that future reads can fetch
262 * new data from server */
263 if (!ret)
264 truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
265
266out_unlock:
267 if (dst_inode < src_inode) {
268 mutex_unlock(&src_inode->i_mutex);
269 mutex_unlock(&dst_inode->i_mutex);
270 } else {
271 mutex_unlock(&dst_inode->i_mutex);
272 mutex_unlock(&src_inode->i_mutex);
273 }
274out_fput:
275 fdput(src_file);
276out_drop_write:
277 mnt_drop_write_file(dst_file);
278 return ret;
279}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400280#endif /* CONFIG_NFS_V4_2 */
281
Peng Taobea51b32015-09-26 02:24:36 +0800282long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
283{
284 switch (cmd) {
285#ifdef CONFIG_NFS_V4_2
286 case NFS_IOC_CLONE:
287 return nfs42_ioctl_clone(file, arg, 0, 0, 0);
288#endif
289 }
290
291 return -ENOTTY;
292}
293
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400294const struct file_operations nfs4_file_operations = {
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400295#ifdef CONFIG_NFS_V4_2
296 .llseek = nfs4_file_llseek,
297#else
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400298 .llseek = nfs_file_llseek,
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400299#endif
Al Viro3aa2d192014-04-02 20:14:12 -0400300 .read_iter = nfs_file_read,
Al Viroedaf4362014-04-03 14:07:25 -0400301 .write_iter = nfs_file_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400302 .mmap = nfs_file_mmap,
303 .open = nfs4_file_open,
Trond Myklebust5445b1f2015-09-05 19:06:58 -0400304 .flush = nfs4_file_flush,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400305 .release = nfs_file_release,
306 .fsync = nfs4_file_fsync,
307 .lock = nfs_lock,
308 .flock = nfs_flock,
309 .splice_read = nfs_file_splice_read,
Al Viro4da54c22014-04-05 04:37:17 -0400310 .splice_write = iter_file_splice_write,
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500311#ifdef CONFIG_NFS_V4_2
312 .fallocate = nfs42_fallocate,
313#endif /* CONFIG_NFS_V4_2 */
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400314 .check_flags = nfs_check_flags,
Jeff Layton1c994a02014-08-27 06:49:41 -0400315 .setlease = simple_nosetlease,
Peng Taobea51b32015-09-26 02:24:36 +0800316#ifdef CONFIG_COMPAT
317 .unlocked_ioctl = nfs4_ioctl,
318#else
319 .compat_ioctl = nfs4_ioctl,
320#endif /* CONFIG_COMPAT */
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400321};