blob: cb3c7879e59fbfac80b4bd36376af933eafa663a [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>
9#include "internal.h"
David Howellsa4ff1462012-12-05 16:31:49 +000010#include "fscache.h"
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040011#include "pnfs.h"
12
Trond Myklebust81b79af2015-03-25 19:09:08 -040013#include "nfstrace.h"
14
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040015#ifdef CONFIG_NFS_V4_2
16#include "nfs42.h"
17#endif
18
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040019#define NFSDBG_FACILITY NFSDBG_FILE
20
21static int
22nfs4_file_open(struct inode *inode, struct file *filp)
23{
24 struct nfs_open_context *ctx;
25 struct dentry *dentry = filp->f_path.dentry;
26 struct dentry *parent = NULL;
27 struct inode *dir;
28 unsigned openflags = filp->f_flags;
29 struct iattr attr;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -040030 int opened = 0;
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040031 int err;
32
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040033 /*
34 * If no cached dentry exists or if it's negative, NFSv4 handled the
35 * opens in ->lookup() or ->create().
36 *
37 * We only get this far for a cached positive dentry. We skipped
38 * revalidation, so handle it here by dropping the dentry and returning
39 * -EOPENSTALE. The VFS will retry the lookup/create/open.
40 */
41
Al Viro6de14722013-09-16 10:53:17 -040042 dprintk("NFS: open file(%pd2)\n", dentry);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040043
44 if ((openflags & O_ACCMODE) == 3)
45 openflags--;
46
47 /* We can't create new files here */
48 openflags &= ~(O_CREAT|O_EXCL);
49
50 parent = dget_parent(dentry);
51 dir = parent->d_inode;
52
53 ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
54 err = PTR_ERR(ctx);
55 if (IS_ERR(ctx))
56 goto out;
57
58 attr.ia_valid = ATTR_OPEN;
59 if (openflags & O_TRUNC) {
60 attr.ia_valid |= ATTR_SIZE;
61 attr.ia_size = 0;
Trond Myklebust9e1681c2015-03-25 17:23:31 -040062 nfs_sync_inode(inode);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040063 }
64
Trond Myklebust5bc2afc2013-09-23 18:01:28 -040065 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040066 if (IS_ERR(inode)) {
67 err = PTR_ERR(inode);
68 switch (err) {
69 case -EPERM:
70 case -EACCES:
71 case -EDQUOT:
72 case -ENOSPC:
73 case -EROFS:
74 goto out_put_ctx;
75 default:
76 goto out_drop;
77 }
78 }
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040079 if (inode != dentry->d_inode)
80 goto out_drop;
81
82 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
83 nfs_file_set_open_context(filp, ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +010084 nfs_fscache_open_file(inode, filp);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040085 err = 0;
86
87out_put_ctx:
88 put_nfs_open_context(ctx);
89out:
90 dput(parent);
91 return err;
92
93out_drop:
94 d_drop(dentry);
95 err = -EOPENSTALE;
96 goto out_put_ctx;
97}
98
99static int
100nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
101{
102 int ret;
Al Viro496ad9a2013-01-23 17:07:38 -0500103 struct inode *inode = file_inode(file);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400104
Trond Myklebust81b79af2015-03-25 19:09:08 -0400105 trace_nfs_fsync_enter(inode);
106
Trond Myklebusta0815d52015-03-25 18:58:53 -0400107 nfs_inode_dio_wait(inode);
Trond Myklebust05990d12012-09-11 16:01:22 -0400108 do {
109 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
110 if (ret != 0)
111 break;
112 mutex_lock(&inode->i_mutex);
113 ret = nfs_file_fsync_commit(file, start, end, datasync);
Christoph Hellwig1b338092014-04-21 10:29:17 -0700114 if (!ret)
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400115 ret = pnfs_sync_inode(inode, !!datasync);
Trond Myklebust05990d12012-09-11 16:01:22 -0400116 mutex_unlock(&inode->i_mutex);
Trond Myklebustdcfc4f22012-09-11 16:19:38 -0400117 /*
118 * If nfs_file_fsync_commit detected a server reboot, then
119 * resend all dirty pages that might have been covered by
120 * the NFS_CONTEXT_RESEND_WRITES flag
121 */
122 start = 0;
123 end = LLONG_MAX;
Trond Myklebust05990d12012-09-11 16:01:22 -0400124 } while (ret == -EAGAIN);
125
Trond Myklebust81b79af2015-03-25 19:09:08 -0400126 trace_nfs_fsync_exit(inode, ret);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400127 return ret;
128}
129
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400130#ifdef CONFIG_NFS_V4_2
131static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
132{
133 loff_t ret;
134
135 switch (whence) {
136 case SEEK_HOLE:
137 case SEEK_DATA:
138 ret = nfs42_proc_llseek(filep, offset, whence);
139 if (ret != -ENOTSUPP)
140 return ret;
141 default:
142 return nfs_file_llseek(filep, offset, whence);
143 }
144}
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500145
146static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
147{
148 struct inode *inode = file_inode(filep);
149 long ret;
150
151 if (!S_ISREG(inode->i_mode))
152 return -EOPNOTSUPP;
153
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500154 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500155 return -EOPNOTSUPP;
156
157 ret = inode_newsize_ok(inode, offset + len);
158 if (ret < 0)
159 return ret;
160
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500161 if (mode & FALLOC_FL_PUNCH_HOLE)
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400162 return nfs42_proc_deallocate(filep, offset, len);
163 return nfs42_proc_allocate(filep, offset, len);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500164}
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400165#endif /* CONFIG_NFS_V4_2 */
166
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400167const struct file_operations nfs4_file_operations = {
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400168#ifdef CONFIG_NFS_V4_2
169 .llseek = nfs4_file_llseek,
170#else
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400171 .llseek = nfs_file_llseek,
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400172#endif
Al Viro3aa2d192014-04-02 20:14:12 -0400173 .read = new_sync_read,
Al Viroedaf4362014-04-03 14:07:25 -0400174 .write = new_sync_write,
Al Viro3aa2d192014-04-02 20:14:12 -0400175 .read_iter = nfs_file_read,
Al Viroedaf4362014-04-03 14:07:25 -0400176 .write_iter = nfs_file_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400177 .mmap = nfs_file_mmap,
178 .open = nfs4_file_open,
179 .flush = nfs_file_flush,
180 .release = nfs_file_release,
181 .fsync = nfs4_file_fsync,
182 .lock = nfs_lock,
183 .flock = nfs_flock,
184 .splice_read = nfs_file_splice_read,
Al Viro4da54c22014-04-05 04:37:17 -0400185 .splice_write = iter_file_splice_write,
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500186#ifdef CONFIG_NFS_V4_2
187 .fallocate = nfs42_fallocate,
188#endif /* CONFIG_NFS_V4_2 */
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400189 .check_flags = nfs_check_flags,
Jeff Layton1c994a02014-08-27 06:49:41 -0400190 .setlease = simple_nosetlease,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400191};