Merge tag 'efi-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi...
[linux-2.6-block.git] / fs / nfs / nfs4file.c
CommitLineData
ce4ef7c0
BS
1/*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 */
f4ac1674
AS
6#include <linux/fs.h>
7#include <linux/falloc.h>
ce4ef7c0 8#include <linux/nfs_fs.h>
5445b1fb 9#include "delegation.h"
ce4ef7c0 10#include "internal.h"
5445b1fb 11#include "iostat.h"
a4ff1468 12#include "fscache.h"
ce4ef7c0
BS
13#include "pnfs.h"
14
81b79afb
TM
15#include "nfstrace.h"
16
1c6dcbe5
AS
17#ifdef CONFIG_NFS_V4_2
18#include "nfs42.h"
19#endif
20
ce4ef7c0
BS
21#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
ce4ef7c0
BS
34 /*
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
6de1472f 43 dprintk("NFS: open file(%pd2)\n", dentry);
ce4ef7c0 44
18a60089
BC
45 err = nfs_check_flags(openflags);
46 if (err)
47 return err;
48
ce4ef7c0
BS
49 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);
2b0143b5 56 dir = d_inode(parent);
ce4ef7c0
BS
57
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;
9e1681c2 67 nfs_sync_inode(inode);
ce4ef7c0
BS
68 }
69
c5c3fb5f 70 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
ce4ef7c0
BS
71 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 }
2b0143b5 84 if (inode != d_inode(dentry))
ce4ef7c0
BS
85 goto out_drop;
86
87 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
88 nfs_file_set_open_context(filp, ctx);
f1fe29b4 89 nfs_fscache_open_file(inode, filp);
ce4ef7c0
BS
90 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
5445b1fb
TM
104/*
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
ce4ef7c0
BS
129static int
130nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
131{
132 int ret;
496ad9aa 133 struct inode *inode = file_inode(file);
ce4ef7c0 134
81b79afb
TM
135 trace_nfs_fsync_enter(inode);
136
a0815d55 137 nfs_inode_dio_wait(inode);
05990d1b
TM
138 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);
1b33809e 144 if (!ret)
5bb89b47 145 ret = pnfs_sync_inode(inode, !!datasync);
05990d1b 146 mutex_unlock(&inode->i_mutex);
dcfc4f25
TM
147 /*
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;
05990d1b
TM
154 } while (ret == -EAGAIN);
155
81b79afb 156 trace_nfs_fsync_exit(inode, ret);
ce4ef7c0
BS
157 return ret;
158}
159
1c6dcbe5
AS
160#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}
f4ac1674
AS
175
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
624bd5b7 184 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
f4ac1674
AS
185 return -EOPNOTSUPP;
186
187 ret = inode_newsize_ok(inode, offset + len);
188 if (ret < 0)
189 return ret;
190
624bd5b7 191 if (mode & FALLOC_FL_PUNCH_HOLE)
f830f7dd
AS
192 return nfs42_proc_deallocate(filep, offset, len);
193 return nfs42_proc_allocate(filep, offset, len);
f4ac1674 194}
1c6dcbe5
AS
195#endif /* CONFIG_NFS_V4_2 */
196
ce4ef7c0 197const struct file_operations nfs4_file_operations = {
1c6dcbe5
AS
198#ifdef CONFIG_NFS_V4_2
199 .llseek = nfs4_file_llseek,
200#else
ce4ef7c0 201 .llseek = nfs_file_llseek,
1c6dcbe5 202#endif
3aa2d199 203 .read_iter = nfs_file_read,
edaf4369 204 .write_iter = nfs_file_write,
ce4ef7c0
BS
205 .mmap = nfs_file_mmap,
206 .open = nfs4_file_open,
5445b1fb 207 .flush = nfs4_file_flush,
ce4ef7c0
BS
208 .release = nfs_file_release,
209 .fsync = nfs4_file_fsync,
210 .lock = nfs_lock,
211 .flock = nfs_flock,
212 .splice_read = nfs_file_splice_read,
4da54c21 213 .splice_write = iter_file_splice_write,
f4ac1674
AS
214#ifdef CONFIG_NFS_V4_2
215 .fallocate = nfs42_fallocate,
216#endif /* CONFIG_NFS_V4_2 */
ce4ef7c0 217 .check_flags = nfs_check_flags,
1c994a09 218 .setlease = simple_nosetlease,
ce4ef7c0 219};