NFSv4: Truncating file opens should also sync O_DIRECT writes
[linux-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
BS
8#include <linux/nfs_fs.h>
9#include "internal.h"
a4ff1468 10#include "fscache.h"
ce4ef7c0
BS
11#include "pnfs.h"
12
81b79afb
TM
13#include "nfstrace.h"
14
1c6dcbe5
AS
15#ifdef CONFIG_NFS_V4_2
16#include "nfs42.h"
17#endif
18
ce4ef7c0
BS
19#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;
5bc2afc2 30 int opened = 0;
ce4ef7c0
BS
31 int err;
32
ce4ef7c0
BS
33 /*
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
6de1472f 42 dprintk("NFS: open file(%pd2)\n", dentry);
ce4ef7c0
BS
43
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;
9e1681c2 62 nfs_sync_inode(inode);
ce4ef7c0
BS
63 }
64
5bc2afc2 65 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
ce4ef7c0
BS
66 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 }
ce4ef7c0
BS
79 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);
f1fe29b4 84 nfs_fscache_open_file(inode, filp);
ce4ef7c0
BS
85 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;
496ad9aa 103 struct inode *inode = file_inode(file);
ce4ef7c0 104
81b79afb
TM
105 trace_nfs_fsync_enter(inode);
106
05990d1b
TM
107 do {
108 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
109 if (ret != 0)
110 break;
111 mutex_lock(&inode->i_mutex);
112 ret = nfs_file_fsync_commit(file, start, end, datasync);
1b33809e 113 if (!ret)
05990d1b
TM
114 ret = pnfs_layoutcommit_inode(inode, true);
115 mutex_unlock(&inode->i_mutex);
dcfc4f25
TM
116 /*
117 * If nfs_file_fsync_commit detected a server reboot, then
118 * resend all dirty pages that might have been covered by
119 * the NFS_CONTEXT_RESEND_WRITES flag
120 */
121 start = 0;
122 end = LLONG_MAX;
05990d1b
TM
123 } while (ret == -EAGAIN);
124
81b79afb 125 trace_nfs_fsync_exit(inode, ret);
ce4ef7c0
BS
126 return ret;
127}
128
1c6dcbe5
AS
129#ifdef CONFIG_NFS_V4_2
130static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
131{
132 loff_t ret;
133
134 switch (whence) {
135 case SEEK_HOLE:
136 case SEEK_DATA:
137 ret = nfs42_proc_llseek(filep, offset, whence);
138 if (ret != -ENOTSUPP)
139 return ret;
140 default:
141 return nfs_file_llseek(filep, offset, whence);
142 }
143}
f4ac1674
AS
144
145static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
146{
147 struct inode *inode = file_inode(filep);
148 long ret;
149
150 if (!S_ISREG(inode->i_mode))
151 return -EOPNOTSUPP;
152
624bd5b7 153 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
f4ac1674
AS
154 return -EOPNOTSUPP;
155
156 ret = inode_newsize_ok(inode, offset + len);
157 if (ret < 0)
158 return ret;
159
160 mutex_lock(&inode->i_mutex);
624bd5b7
AS
161 if (mode & FALLOC_FL_PUNCH_HOLE)
162 ret = nfs42_proc_deallocate(filep, offset, len);
163 else
164 ret = nfs42_proc_allocate(filep, offset, len);
f4ac1674
AS
165 mutex_unlock(&inode->i_mutex);
166
167 nfs_zap_caches(inode);
168 return ret;
169}
1c6dcbe5
AS
170#endif /* CONFIG_NFS_V4_2 */
171
ce4ef7c0 172const struct file_operations nfs4_file_operations = {
1c6dcbe5
AS
173#ifdef CONFIG_NFS_V4_2
174 .llseek = nfs4_file_llseek,
175#else
ce4ef7c0 176 .llseek = nfs_file_llseek,
1c6dcbe5 177#endif
3aa2d199 178 .read = new_sync_read,
edaf4369 179 .write = new_sync_write,
3aa2d199 180 .read_iter = nfs_file_read,
edaf4369 181 .write_iter = nfs_file_write,
ce4ef7c0
BS
182 .mmap = nfs_file_mmap,
183 .open = nfs4_file_open,
184 .flush = nfs_file_flush,
185 .release = nfs_file_release,
186 .fsync = nfs4_file_fsync,
187 .lock = nfs_lock,
188 .flock = nfs_flock,
189 .splice_read = nfs_file_splice_read,
4da54c21 190 .splice_write = iter_file_splice_write,
f4ac1674
AS
191#ifdef CONFIG_NFS_V4_2
192 .fallocate = nfs42_fallocate,
193#endif /* CONFIG_NFS_V4_2 */
ce4ef7c0 194 .check_flags = nfs_check_flags,
1c994a09 195 .setlease = simple_nosetlease,
ce4ef7c0 196};