Commit | Line | Data |
---|---|---|
ce4ef7c0 BS |
1 | /* |
2 | * linux/fs/nfs/file.c | |
3 | * | |
4 | * Copyright (C) 1992 Rick Sladkey | |
5 | */ | |
f4ac1674 | 6 | #include <linux/fs.h> |
bea51b30 | 7 | #include <linux/file.h> |
f4ac1674 | 8 | #include <linux/falloc.h> |
ce4ef7c0 | 9 | #include <linux/nfs_fs.h> |
5445b1fb | 10 | #include "delegation.h" |
ce4ef7c0 | 11 | #include "internal.h" |
5445b1fb | 12 | #include "iostat.h" |
a4ff1468 | 13 | #include "fscache.h" |
ce4ef7c0 BS |
14 | #include "pnfs.h" |
15 | ||
81b79afb TM |
16 | #include "nfstrace.h" |
17 | ||
1c6dcbe5 AS |
18 | #ifdef CONFIG_NFS_V4_2 |
19 | #include "nfs42.h" | |
20 | #endif | |
21 | ||
ce4ef7c0 BS |
22 | #define NFSDBG_FACILITY NFSDBG_FILE |
23 | ||
24 | static int | |
25 | nfs4_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 | ||
ce4ef7c0 BS |
35 | /* |
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 | ||
6de1472f | 44 | dprintk("NFS: open file(%pd2)\n", dentry); |
ce4ef7c0 | 45 | |
18a60089 BC |
46 | err = nfs_check_flags(openflags); |
47 | if (err) | |
48 | return err; | |
49 | ||
ce4ef7c0 BS |
50 | 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); | |
2b0143b5 | 57 | dir = d_inode(parent); |
ce4ef7c0 BS |
58 | |
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; | |
9e1681c2 | 68 | nfs_sync_inode(inode); |
ce4ef7c0 BS |
69 | } |
70 | ||
c5c3fb5f | 71 | inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL); |
ce4ef7c0 BS |
72 | 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 | } | |
2b0143b5 | 85 | if (inode != d_inode(dentry)) |
ce4ef7c0 BS |
86 | goto out_drop; |
87 | ||
88 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); | |
89 | nfs_file_set_open_context(filp, ctx); | |
f1fe29b4 | 90 | nfs_fscache_open_file(inode, filp); |
ce4ef7c0 BS |
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 | ||
5445b1fb TM |
105 | /* |
106 | * Flush all dirty pages, and check for write errors. | |
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 | * 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 | ||
ce4ef7c0 BS |
130 | static int |
131 | nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) | |
132 | { | |
133 | int ret; | |
496ad9aa | 134 | struct inode *inode = file_inode(file); |
ce4ef7c0 | 135 | |
81b79afb TM |
136 | trace_nfs_fsync_enter(inode); |
137 | ||
a0815d55 | 138 | nfs_inode_dio_wait(inode); |
05990d1b TM |
139 | 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); | |
1b33809e | 145 | if (!ret) |
5bb89b47 | 146 | ret = pnfs_sync_inode(inode, !!datasync); |
05990d1b | 147 | mutex_unlock(&inode->i_mutex); |
dcfc4f25 TM |
148 | /* |
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; | |
05990d1b TM |
155 | } while (ret == -EAGAIN); |
156 | ||
81b79afb | 157 | trace_nfs_fsync_exit(inode, ret); |
ce4ef7c0 BS |
158 | return ret; |
159 | } | |
160 | ||
1c6dcbe5 AS |
161 | #ifdef CONFIG_NFS_V4_2 |
162 | static 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 | } | |
f4ac1674 AS |
176 | |
177 | static 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 | ||
624bd5b7 | 185 | if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))) |
f4ac1674 AS |
186 | return -EOPNOTSUPP; |
187 | ||
188 | ret = inode_newsize_ok(inode, offset + len); | |
189 | if (ret < 0) | |
190 | return ret; | |
191 | ||
624bd5b7 | 192 | if (mode & FALLOC_FL_PUNCH_HOLE) |
f830f7dd AS |
193 | return nfs42_proc_deallocate(filep, offset, len); |
194 | return nfs42_proc_allocate(filep, offset, len); | |
f4ac1674 | 195 | } |
bea51b30 PT |
196 | |
197 | static noinline long | |
198 | nfs42_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); | |
811b7b85 | 202 | struct nfs_server *server = NFS_SERVER(dst_inode); |
bea51b30 PT |
203 | struct fd src_file; |
204 | struct inode *src_inode; | |
811b7b85 | 205 | unsigned int bs = server->clone_blksize; |
bea51b30 PT |
206 | int ret; |
207 | ||
208 | /* dst file must be opened for writing */ | |
209 | if (!(dst_file->f_mode & FMODE_WRITE)) | |
210 | return -EINVAL; | |
211 | ||
212 | ret = mnt_want_write_file(dst_file); | |
213 | if (ret) | |
214 | return ret; | |
215 | ||
216 | src_file = fdget(srcfd); | |
217 | if (!src_file.file) { | |
218 | ret = -EBADF; | |
219 | goto out_drop_write; | |
220 | } | |
221 | ||
222 | src_inode = file_inode(src_file.file); | |
223 | ||
224 | /* src and dst must be different files */ | |
225 | ret = -EINVAL; | |
226 | if (src_inode == dst_inode) | |
227 | goto out_fput; | |
228 | ||
229 | /* src file must be opened for reading */ | |
230 | if (!(src_file.file->f_mode & FMODE_READ)) | |
231 | goto out_fput; | |
232 | ||
233 | /* src and dst must be regular files */ | |
234 | ret = -EISDIR; | |
235 | if (!S_ISREG(src_inode->i_mode) || !S_ISREG(dst_inode->i_mode)) | |
236 | goto out_fput; | |
237 | ||
238 | ret = -EXDEV; | |
239 | if (src_file.file->f_path.mnt != dst_file->f_path.mnt || | |
240 | src_inode->i_sb != dst_inode->i_sb) | |
241 | goto out_fput; | |
242 | ||
811b7b85 PT |
243 | /* check alignment w.r.t. clone_blksize */ |
244 | ret = -EINVAL; | |
245 | if (bs) { | |
246 | if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs)) | |
247 | goto out_fput; | |
248 | if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count)) | |
249 | goto out_fput; | |
250 | } | |
251 | ||
bea51b30 PT |
252 | /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */ |
253 | if (dst_inode < src_inode) { | |
254 | mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT); | |
255 | mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD); | |
256 | } else { | |
257 | mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT); | |
258 | mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD); | |
259 | } | |
260 | ||
261 | /* flush all pending writes on both src and dst so that server | |
262 | * has the latest data */ | |
263 | ret = nfs_sync_inode(src_inode); | |
264 | if (ret) | |
265 | goto out_unlock; | |
266 | ret = nfs_sync_inode(dst_inode); | |
267 | if (ret) | |
268 | goto out_unlock; | |
269 | ||
270 | ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count); | |
271 | ||
272 | /* truncate inode page cache of the dst range so that future reads can fetch | |
273 | * new data from server */ | |
274 | if (!ret) | |
275 | truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1); | |
276 | ||
277 | out_unlock: | |
278 | if (dst_inode < src_inode) { | |
279 | mutex_unlock(&src_inode->i_mutex); | |
280 | mutex_unlock(&dst_inode->i_mutex); | |
281 | } else { | |
282 | mutex_unlock(&dst_inode->i_mutex); | |
283 | mutex_unlock(&src_inode->i_mutex); | |
284 | } | |
285 | out_fput: | |
286 | fdput(src_file); | |
287 | out_drop_write: | |
288 | mnt_drop_write_file(dst_file); | |
289 | return ret; | |
290 | } | |
a340abcf PT |
291 | |
292 | static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp) | |
293 | { | |
294 | struct nfs_ioctl_clone_range_args args; | |
295 | ||
296 | if (copy_from_user(&args, argp, sizeof(args))) | |
297 | return -EFAULT; | |
298 | ||
299 | return nfs42_ioctl_clone(dst_file, args.src_fd, args.src_off, args.dst_off, args.count); | |
300 | } | |
275058a2 TM |
301 | #else |
302 | static long nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd, | |
303 | u64 src_off, u64 dst_off, u64 count) | |
304 | { | |
305 | return -ENOTTY; | |
306 | } | |
307 | ||
308 | static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp) | |
309 | { | |
310 | return -ENOTTY; | |
311 | } | |
1c6dcbe5 AS |
312 | #endif /* CONFIG_NFS_V4_2 */ |
313 | ||
bea51b30 PT |
314 | long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
315 | { | |
a340abcf PT |
316 | void __user *argp = (void __user *)arg; |
317 | ||
bea51b30 | 318 | switch (cmd) { |
bea51b30 PT |
319 | case NFS_IOC_CLONE: |
320 | return nfs42_ioctl_clone(file, arg, 0, 0, 0); | |
a340abcf PT |
321 | case NFS_IOC_CLONE_RANGE: |
322 | return nfs42_ioctl_clone_range(file, argp); | |
bea51b30 PT |
323 | } |
324 | ||
325 | return -ENOTTY; | |
326 | } | |
327 | ||
ce4ef7c0 | 328 | const struct file_operations nfs4_file_operations = { |
1c6dcbe5 AS |
329 | #ifdef CONFIG_NFS_V4_2 |
330 | .llseek = nfs4_file_llseek, | |
331 | #else | |
ce4ef7c0 | 332 | .llseek = nfs_file_llseek, |
1c6dcbe5 | 333 | #endif |
3aa2d199 | 334 | .read_iter = nfs_file_read, |
edaf4369 | 335 | .write_iter = nfs_file_write, |
ce4ef7c0 BS |
336 | .mmap = nfs_file_mmap, |
337 | .open = nfs4_file_open, | |
5445b1fb | 338 | .flush = nfs4_file_flush, |
ce4ef7c0 BS |
339 | .release = nfs_file_release, |
340 | .fsync = nfs4_file_fsync, | |
341 | .lock = nfs_lock, | |
342 | .flock = nfs_flock, | |
343 | .splice_read = nfs_file_splice_read, | |
4da54c21 | 344 | .splice_write = iter_file_splice_write, |
f4ac1674 AS |
345 | #ifdef CONFIG_NFS_V4_2 |
346 | .fallocate = nfs42_fallocate, | |
347 | #endif /* CONFIG_NFS_V4_2 */ | |
ce4ef7c0 | 348 | .check_flags = nfs_check_flags, |
1c994a09 | 349 | .setlease = simple_nosetlease, |
bea51b30 | 350 | .unlocked_ioctl = nfs4_ioctl, |
bea51b30 | 351 | .compat_ioctl = nfs4_ioctl, |
ce4ef7c0 | 352 | }; |