This source file includes following definitions.
- nfs4_file_open
- nfs4_file_flush
- __nfs4_copy_file_range
- nfs4_copy_file_range
- nfs4_file_llseek
- nfs42_fallocate
- nfs42_remap_file_range
1
2
3
4
5
6
7 #include <linux/fs.h>
8 #include <linux/file.h>
9 #include <linux/falloc.h>
10 #include <linux/nfs_fs.h>
11 #include "delegation.h"
12 #include "internal.h"
13 #include "iostat.h"
14 #include "fscache.h"
15 #include "pnfs.h"
16
17 #include "nfstrace.h"
18
19 #ifdef CONFIG_NFS_V4_2
20 #include "nfs42.h"
21 #endif
22
23 #define NFSDBG_FACILITY NFSDBG_FILE
24
25 static int
26 nfs4_file_open(struct inode *inode, struct file *filp)
27 {
28 struct nfs_open_context *ctx;
29 struct dentry *dentry = file_dentry(filp);
30 struct dentry *parent = NULL;
31 struct inode *dir;
32 unsigned openflags = filp->f_flags;
33 struct iattr attr;
34 int err;
35
36
37
38
39
40
41
42
43
44
45 dprintk("NFS: open file(%pd2)\n", dentry);
46
47 err = nfs_check_flags(openflags);
48 if (err)
49 return err;
50
51 if ((openflags & O_ACCMODE) == 3)
52 return nfs_open(inode, filp);
53
54
55 openflags &= ~(O_CREAT|O_EXCL);
56
57 parent = dget_parent(dentry);
58 dir = d_inode(parent);
59
60 ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
61 err = PTR_ERR(ctx);
62 if (IS_ERR(ctx))
63 goto out;
64
65 attr.ia_valid = ATTR_OPEN;
66 if (openflags & O_TRUNC) {
67 attr.ia_valid |= ATTR_SIZE;
68 attr.ia_size = 0;
69 filemap_write_and_wait(inode->i_mapping);
70 }
71
72 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
73 if (IS_ERR(inode)) {
74 err = PTR_ERR(inode);
75 switch (err) {
76 default:
77 goto out_put_ctx;
78 case -ENOENT:
79 case -ESTALE:
80 case -EISDIR:
81 case -ENOTDIR:
82 case -ELOOP:
83 goto out_drop;
84 }
85 }
86 if (inode != d_inode(dentry))
87 goto out_drop;
88
89 nfs_file_set_open_context(filp, ctx);
90 nfs_fscache_open_file(inode, filp);
91 err = 0;
92
93 out_put_ctx:
94 put_nfs_open_context(ctx);
95 out:
96 dput(parent);
97 return err;
98
99 out_drop:
100 d_drop(dentry);
101 err = -EOPENSTALE;
102 goto out_put_ctx;
103 }
104
105
106
107
108 static int
109 nfs4_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
121
122
123 if (!nfs4_delegation_flush_on_close(inode))
124 return filemap_fdatawrite(file->f_mapping);
125
126
127 return nfs_wb_all(inode);
128 }
129
130 #ifdef CONFIG_NFS_V4_2
131 static ssize_t __nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
132 struct file *file_out, loff_t pos_out,
133 size_t count, unsigned int flags)
134 {
135
136 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
137 return -EXDEV;
138 if (!nfs_server_capable(file_inode(file_out), NFS_CAP_COPY))
139 return -EOPNOTSUPP;
140 if (file_inode(file_in) == file_inode(file_out))
141 return -EOPNOTSUPP;
142 return nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count);
143 }
144
145 static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
146 struct file *file_out, loff_t pos_out,
147 size_t count, unsigned int flags)
148 {
149 ssize_t ret;
150
151 ret = __nfs4_copy_file_range(file_in, pos_in, file_out, pos_out, count,
152 flags);
153 if (ret == -EOPNOTSUPP || ret == -EXDEV)
154 ret = generic_copy_file_range(file_in, pos_in, file_out,
155 pos_out, count, flags);
156 return ret;
157 }
158
159 static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
160 {
161 loff_t ret;
162
163 switch (whence) {
164 case SEEK_HOLE:
165 case SEEK_DATA:
166 ret = nfs42_proc_llseek(filep, offset, whence);
167 if (ret != -ENOTSUPP)
168 return ret;
169
170 default:
171 return nfs_file_llseek(filep, offset, whence);
172 }
173 }
174
175 static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
176 {
177 struct inode *inode = file_inode(filep);
178 long ret;
179
180 if (!S_ISREG(inode->i_mode))
181 return -EOPNOTSUPP;
182
183 if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
184 return -EOPNOTSUPP;
185
186 ret = inode_newsize_ok(inode, offset + len);
187 if (ret < 0)
188 return ret;
189
190 if (mode & FALLOC_FL_PUNCH_HOLE)
191 return nfs42_proc_deallocate(filep, offset, len);
192 return nfs42_proc_allocate(filep, offset, len);
193 }
194
195 static loff_t nfs42_remap_file_range(struct file *src_file, loff_t src_off,
196 struct file *dst_file, loff_t dst_off, loff_t count,
197 unsigned int remap_flags)
198 {
199 struct inode *dst_inode = file_inode(dst_file);
200 struct nfs_server *server = NFS_SERVER(dst_inode);
201 struct inode *src_inode = file_inode(src_file);
202 unsigned int bs = server->clone_blksize;
203 bool same_inode = false;
204 int ret;
205
206
207 if (remap_flags & REMAP_FILE_DEDUP)
208 return -EOPNOTSUPP;
209
210 if (remap_flags & ~REMAP_FILE_ADVISORY)
211 return -EINVAL;
212
213 if (IS_SWAPFILE(dst_inode) || IS_SWAPFILE(src_inode))
214 return -ETXTBSY;
215
216
217 ret = -EINVAL;
218 if (bs) {
219 if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
220 goto out;
221 if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
222 goto out;
223 }
224
225 if (src_inode == dst_inode)
226 same_inode = true;
227
228
229 if (same_inode) {
230 inode_lock(src_inode);
231 } else if (dst_inode < src_inode) {
232 inode_lock_nested(dst_inode, I_MUTEX_PARENT);
233 inode_lock_nested(src_inode, I_MUTEX_CHILD);
234 } else {
235 inode_lock_nested(src_inode, I_MUTEX_PARENT);
236 inode_lock_nested(dst_inode, I_MUTEX_CHILD);
237 }
238
239
240
241 ret = nfs_sync_inode(src_inode);
242 if (ret)
243 goto out_unlock;
244 ret = nfs_sync_inode(dst_inode);
245 if (ret)
246 goto out_unlock;
247
248 ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
249
250
251
252 if (!ret)
253 truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
254
255 out_unlock:
256 if (same_inode) {
257 inode_unlock(src_inode);
258 } else if (dst_inode < src_inode) {
259 inode_unlock(src_inode);
260 inode_unlock(dst_inode);
261 } else {
262 inode_unlock(dst_inode);
263 inode_unlock(src_inode);
264 }
265 out:
266 return ret < 0 ? ret : count;
267 }
268 #endif
269
270 const struct file_operations nfs4_file_operations = {
271 .read_iter = nfs_file_read,
272 .write_iter = nfs_file_write,
273 .mmap = nfs_file_mmap,
274 .open = nfs4_file_open,
275 .flush = nfs4_file_flush,
276 .release = nfs_file_release,
277 .fsync = nfs_file_fsync,
278 .lock = nfs_lock,
279 .flock = nfs_flock,
280 .splice_read = generic_file_splice_read,
281 .splice_write = iter_file_splice_write,
282 .check_flags = nfs_check_flags,
283 .setlease = simple_nosetlease,
284 #ifdef CONFIG_NFS_V4_2
285 .copy_file_range = nfs4_copy_file_range,
286 .llseek = nfs4_file_llseek,
287 .fallocate = nfs42_fallocate,
288 .remap_file_range = nfs42_remap_file_range,
289 #else
290 .llseek = nfs_file_llseek,
291 #endif
292 };