NFS: Split out NFS v2 inode operations
[linux-block.git] / fs / nfs / file.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 *
6 * Changes Copyright (C) 1994 by Florian La Roche
7 * - Do not copy data too often around in the kernel.
8 * - In nfs_file_read the return value of kmalloc wasn't checked.
9 * - Put in a better version of read look-ahead buffering. Original idea
10 * and implementation by Wai S Kok elekokws@ee.nus.sg.
11 *
12 * Expire cache on write to a file by Wai S Kok (Oct 1994).
13 *
14 * Total rewrite of read side for new NFS buffer cache.. Linus.
15 *
16 * nfs regular file handling functions
17 */
18
19#include <linux/time.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/fcntl.h>
23#include <linux/stat.h>
24#include <linux/nfs_fs.h>
25#include <linux/nfs_mount.h>
26#include <linux/mm.h>
1da177e4 27#include <linux/pagemap.h>
e8edc6e0 28#include <linux/aio.h>
5a0e3ad6 29#include <linux/gfp.h>
b608b283 30#include <linux/swap.h>
1da177e4
LT
31
32#include <asm/uaccess.h>
1da177e4
LT
33
34#include "delegation.h"
94387fb1 35#include "internal.h"
91d5b470 36#include "iostat.h"
545db45f 37#include "fscache.h"
e5e94017 38#include "pnfs.h"
1da177e4
LT
39
40#define NFSDBG_FACILITY NFSDBG_FILE
41
f0f37e2f 42static const struct vm_operations_struct nfs_file_vm_ops;
94387fb1 43
b7fa0554 44#ifdef CONFIG_NFS_V3
92e1d5be 45const struct inode_operations nfs3_file_inode_operations = {
b7fa0554
AG
46 .permission = nfs_permission,
47 .getattr = nfs_getattr,
48 .setattr = nfs_setattr,
49 .listxattr = nfs3_listxattr,
50 .getxattr = nfs3_getxattr,
51 .setxattr = nfs3_setxattr,
52 .removexattr = nfs3_removexattr,
53};
54#endif /* CONFIG_NFS_v3 */
55
1da177e4
LT
56/* Hack for future NFS swap support */
57#ifndef IS_SWAPFILE
58# define IS_SWAPFILE(inode) (0)
59#endif
60
61static int nfs_check_flags(int flags)
62{
63 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
64 return -EINVAL;
65
66 return 0;
67}
68
69/*
70 * Open file
71 */
72static int
73nfs_file_open(struct inode *inode, struct file *filp)
74{
1da177e4
LT
75 int res;
76
6da24bc9 77 dprintk("NFS: open file(%s/%s)\n",
cc0dd2d1
CL
78 filp->f_path.dentry->d_parent->d_name.name,
79 filp->f_path.dentry->d_name.name);
80
c2459dc4 81 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1da177e4
LT
82 res = nfs_check_flags(filp->f_flags);
83 if (res)
84 return res;
85
46cb650c 86 res = nfs_open(inode, filp);
1da177e4
LT
87 return res;
88}
89
90static int
91nfs_file_release(struct inode *inode, struct file *filp)
92{
6da24bc9 93 dprintk("NFS: release(%s/%s)\n",
6f276e49
RM
94 filp->f_path.dentry->d_parent->d_name.name,
95 filp->f_path.dentry->d_name.name);
6da24bc9 96
91d5b470 97 nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
46cb650c 98 return nfs_release(inode, filp);
1da177e4
LT
99}
100
980802e3
TM
101/**
102 * nfs_revalidate_size - Revalidate the file size
103 * @inode - pointer to inode struct
104 * @file - pointer to struct file
105 *
106 * Revalidates the file length. This is basically a wrapper around
107 * nfs_revalidate_inode() that takes into account the fact that we may
108 * have cached writes (in which case we don't care about the server's
109 * idea of what the file length is), or O_DIRECT (in which case we
110 * shouldn't trust the cache).
111 */
112static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
113{
114 struct nfs_server *server = NFS_SERVER(inode);
115 struct nfs_inode *nfsi = NFS_I(inode);
116
d7cf8dd0
TM
117 if (nfs_have_delegated_attributes(inode))
118 goto out_noreval;
119
980802e3
TM
120 if (filp->f_flags & O_DIRECT)
121 goto force_reval;
d7cf8dd0
TM
122 if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
123 goto force_reval;
124 if (nfs_attribute_timeout(inode))
125 goto force_reval;
126out_noreval:
127 return 0;
980802e3
TM
128force_reval:
129 return __nfs_revalidate_inode(server, inode);
130}
131
132static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
133{
6da24bc9 134 dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
b84e06c5
CL
135 filp->f_path.dentry->d_parent->d_name.name,
136 filp->f_path.dentry->d_name.name,
137 offset, origin);
138
06222e49
JB
139 /*
140 * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
141 * the cached file length
142 */
6c529617 143 if (origin != SEEK_SET && origin != SEEK_CUR) {
980802e3 144 struct inode *inode = filp->f_mapping->host;
d5e66348 145
980802e3
TM
146 int retval = nfs_revalidate_file_size(inode, filp);
147 if (retval < 0)
148 return (loff_t)retval;
79835a71 149 }
d5e66348 150
79835a71 151 return generic_file_llseek(filp, offset, origin);
980802e3
TM
152}
153
1da177e4
LT
154/*
155 * Flush all dirty pages, and check for write errors.
1da177e4
LT
156 */
157static int
75e1fcc0 158nfs_file_flush(struct file *file, fl_owner_t id)
1da177e4 159{
6da24bc9
CL
160 struct dentry *dentry = file->f_path.dentry;
161 struct inode *inode = dentry->d_inode;
1da177e4 162
6da24bc9
CL
163 dprintk("NFS: flush(%s/%s)\n",
164 dentry->d_parent->d_name.name,
165 dentry->d_name.name);
1da177e4 166
c2459dc4 167 nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1da177e4
LT
168 if ((file->f_mode & FMODE_WRITE) == 0)
169 return 0;
7b159fc1 170
14546c33
TM
171 /*
172 * If we're holding a write delegation, then just start the i/o
173 * but don't wait for completion (or send a commit).
174 */
011e2a7f 175 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
14546c33
TM
176 return filemap_fdatawrite(file->f_mapping);
177
7fe5c398 178 /* Flush writes to the server and return any errors */
af7fa165 179 return vfs_fsync(file, 0);
1da177e4
LT
180}
181
182static ssize_t
027445c3
BP
183nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
184 unsigned long nr_segs, loff_t pos)
1da177e4 185{
01cce933 186 struct dentry * dentry = iocb->ki_filp->f_path.dentry;
1da177e4
LT
187 struct inode * inode = dentry->d_inode;
188 ssize_t result;
189
1da177e4 190 if (iocb->ki_filp->f_flags & O_DIRECT)
027445c3 191 return nfs_file_direct_read(iocb, iov, nr_segs, pos);
1da177e4 192
6da24bc9 193 dprintk("NFS: read(%s/%s, %lu@%lu)\n",
1da177e4 194 dentry->d_parent->d_name.name, dentry->d_name.name,
6f276e49 195 (unsigned long) iov_length(iov, nr_segs), (unsigned long) pos);
1da177e4 196
44b11874 197 result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
4184dcf2 198 if (!result) {
027445c3 199 result = generic_file_aio_read(iocb, iov, nr_segs, pos);
4184dcf2
CL
200 if (result > 0)
201 nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
202 }
1da177e4
LT
203 return result;
204}
205
206static ssize_t
f0930fff
JA
207nfs_file_splice_read(struct file *filp, loff_t *ppos,
208 struct pipe_inode_info *pipe, size_t count,
209 unsigned int flags)
1da177e4 210{
01cce933 211 struct dentry *dentry = filp->f_path.dentry;
1da177e4
LT
212 struct inode *inode = dentry->d_inode;
213 ssize_t res;
214
6da24bc9 215 dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
1da177e4
LT
216 dentry->d_parent->d_name.name, dentry->d_name.name,
217 (unsigned long) count, (unsigned long long) *ppos);
218
44b11874 219 res = nfs_revalidate_mapping(inode, filp->f_mapping);
aa2f1ef1 220 if (!res) {
f0930fff 221 res = generic_file_splice_read(filp, ppos, pipe, count, flags);
aa2f1ef1
CL
222 if (res > 0)
223 nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
224 }
1da177e4
LT
225 return res;
226}
227
228static int
229nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
230{
01cce933 231 struct dentry *dentry = file->f_path.dentry;
1da177e4
LT
232 struct inode *inode = dentry->d_inode;
233 int status;
234
6da24bc9 235 dprintk("NFS: mmap(%s/%s)\n",
1da177e4
LT
236 dentry->d_parent->d_name.name, dentry->d_name.name);
237
e1ebfd33
TM
238 /* Note: generic_file_mmap() returns ENOSYS on nommu systems
239 * so we call that before revalidating the mapping
240 */
241 status = generic_file_mmap(file, vma);
94387fb1
TM
242 if (!status) {
243 vma->vm_ops = &nfs_file_vm_ops;
e1ebfd33 244 status = nfs_revalidate_mapping(inode, file->f_mapping);
94387fb1 245 }
1da177e4
LT
246 return status;
247}
248
249/*
250 * Flush any dirty pages for this process, and check for write errors.
251 * The return status from this call provides a reliable indication of
252 * whether any write errors occurred for this process.
af7fa165
TM
253 *
254 * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
255 * disk, but it retrieves and clears ctx->error after synching, despite
256 * the two being set at the same time in nfs_context_set_write_error().
257 * This is because the former is used to notify the _next_ call to
25985edc 258 * nfs_file_write() that a write error occurred, and hence cause it to
af7fa165 259 * fall back to doing a synchronous write.
1da177e4
LT
260 */
261static int
a5c58892 262nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
1da177e4 263{
7ea80859 264 struct dentry *dentry = file->f_path.dentry;
cd3758e3 265 struct nfs_open_context *ctx = nfs_file_open_context(file);
1da177e4 266 struct inode *inode = dentry->d_inode;
af7fa165
TM
267 int have_error, status;
268 int ret = 0;
269
6da24bc9 270 dprintk("NFS: fsync file(%s/%s) datasync %d\n",
54917786
CL
271 dentry->d_parent->d_name.name, dentry->d_name.name,
272 datasync);
1da177e4 273
91d5b470 274 nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
af7fa165
TM
275 have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
276 status = nfs_commit_inode(inode, FLUSH_SYNC);
2edb6bc3
N
277 if (status >= 0 && ret < 0)
278 status = ret;
af7fa165
TM
279 have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
280 if (have_error)
281 ret = xchg(&ctx->error, 0);
0702099b 282 if (!ret && status < 0)
af7fa165 283 ret = status;
a5c58892
BS
284 return ret;
285}
286
287static int
288nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
289{
290 int ret;
291 struct inode *inode = file->f_path.dentry->d_inode;
292
293 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
294 mutex_lock(&inode->i_mutex);
295 ret = nfs_file_fsync_commit(file, start, end, datasync);
02c24a82 296 mutex_unlock(&inode->i_mutex);
a5c58892 297
af7fa165 298 return ret;
1da177e4
LT
299}
300
38c73044
PS
301/*
302 * Decide whether a read/modify/write cycle may be more efficient
303 * then a modify/write/read cycle when writing to a page in the
304 * page cache.
305 *
306 * The modify/write/read cycle may occur if a page is read before
307 * being completely filled by the writer. In this situation, the
308 * page must be completely written to stable storage on the server
309 * before it can be refilled by reading in the page from the server.
310 * This can lead to expensive, small, FILE_SYNC mode writes being
311 * done.
312 *
313 * It may be more efficient to read the page first if the file is
314 * open for reading in addition to writing, the page is not marked
315 * as Uptodate, it is not dirty or waiting to be committed,
316 * indicating that it was previously allocated and then modified,
317 * that there were valid bytes of data in that range of the file,
318 * and that the new data won't completely replace the old data in
319 * that range of the file.
320 */
321static int nfs_want_read_modify_write(struct file *file, struct page *page,
322 loff_t pos, unsigned len)
323{
324 unsigned int pglen = nfs_page_length(page);
325 unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
326 unsigned int end = offset + len;
327
328 if ((file->f_mode & FMODE_READ) && /* open for read? */
329 !PageUptodate(page) && /* Uptodate? */
330 !PagePrivate(page) && /* i/o request already? */
331 pglen && /* valid bytes of file? */
332 (end < pglen || offset)) /* replace all valid bytes? */
333 return 1;
334 return 0;
335}
336
1da177e4 337/*
4899f9c8
NP
338 * This does the "real" work of the write. We must allocate and lock the
339 * page to be sent back to the generic routine, which then copies the
340 * data from user space.
1da177e4
LT
341 *
342 * If the writer ends up delaying the write, the writer needs to
343 * increment the page use counts until he is done with the page.
344 */
4899f9c8
NP
345static int nfs_write_begin(struct file *file, struct address_space *mapping,
346 loff_t pos, unsigned len, unsigned flags,
347 struct page **pagep, void **fsdata)
1da177e4 348{
4899f9c8 349 int ret;
38c73044 350 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
4899f9c8 351 struct page *page;
38c73044 352 int once_thru = 0;
4899f9c8 353
b7eaefaa
CL
354 dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
355 file->f_path.dentry->d_parent->d_name.name,
356 file->f_path.dentry->d_name.name,
357 mapping->host->i_ino, len, (long long) pos);
358
38c73044 359start:
72cb77f4
TM
360 /*
361 * Prevent starvation issues if someone is doing a consistency
362 * sync-to-disk
363 */
364 ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
365 nfs_wait_bit_killable, TASK_KILLABLE);
366 if (ret)
367 return ret;
368
54566b2c 369 page = grab_cache_page_write_begin(mapping, index, flags);
4899f9c8
NP
370 if (!page)
371 return -ENOMEM;
372 *pagep = page;
373
374 ret = nfs_flush_incompatible(file, page);
375 if (ret) {
376 unlock_page(page);
377 page_cache_release(page);
38c73044
PS
378 } else if (!once_thru &&
379 nfs_want_read_modify_write(file, page, pos, len)) {
380 once_thru = 1;
381 ret = nfs_readpage(file, page);
382 page_cache_release(page);
383 if (!ret)
384 goto start;
4899f9c8
NP
385 }
386 return ret;
1da177e4
LT
387}
388
4899f9c8
NP
389static int nfs_write_end(struct file *file, struct address_space *mapping,
390 loff_t pos, unsigned len, unsigned copied,
391 struct page *page, void *fsdata)
1da177e4 392{
4899f9c8
NP
393 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
394 int status;
1da177e4 395
b7eaefaa
CL
396 dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
397 file->f_path.dentry->d_parent->d_name.name,
398 file->f_path.dentry->d_name.name,
399 mapping->host->i_ino, len, (long long) pos);
400
efc91ed0
TM
401 /*
402 * Zero any uninitialised parts of the page, and then mark the page
403 * as up to date if it turns out that we're extending the file.
404 */
405 if (!PageUptodate(page)) {
406 unsigned pglen = nfs_page_length(page);
407 unsigned end = offset + len;
408
409 if (pglen == 0) {
410 zero_user_segments(page, 0, offset,
411 end, PAGE_CACHE_SIZE);
412 SetPageUptodate(page);
413 } else if (end >= pglen) {
414 zero_user_segment(page, end, PAGE_CACHE_SIZE);
415 if (offset == 0)
416 SetPageUptodate(page);
417 } else
418 zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
419 }
420
4899f9c8 421 status = nfs_updatepage(file, page, offset, copied);
4899f9c8
NP
422
423 unlock_page(page);
424 page_cache_release(page);
425
3d509e54
CL
426 if (status < 0)
427 return status;
2701d086 428 NFS_I(mapping->host)->write_io += copied;
3d509e54 429 return copied;
1da177e4
LT
430}
431
6b9b3514
DH
432/*
433 * Partially or wholly invalidate a page
434 * - Release the private state associated with a page if undergoing complete
435 * page invalidation
545db45f 436 * - Called if either PG_private or PG_fscache is set on the page
6b9b3514
DH
437 * - Caller holds page lock
438 */
2ff28e22 439static void nfs_invalidate_page(struct page *page, unsigned long offset)
cd52ed35 440{
b7eaefaa
CL
441 dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
442
1c75950b
TM
443 if (offset != 0)
444 return;
d2ccddf0 445 /* Cancel any unstarted writes on this page */
1b3b4a1a 446 nfs_wb_page_cancel(page->mapping->host, page);
545db45f
DH
447
448 nfs_fscache_invalidate_page(page, page->mapping->host);
cd52ed35
TM
449}
450
6b9b3514
DH
451/*
452 * Attempt to release the private state associated with a page
545db45f 453 * - Called if either PG_private or PG_fscache is set on the page
6b9b3514
DH
454 * - Caller holds page lock
455 * - Return true (may release page) or false (may not)
456 */
cd52ed35
TM
457static int nfs_release_page(struct page *page, gfp_t gfp)
458{
b608b283
TM
459 struct address_space *mapping = page->mapping;
460
b7eaefaa
CL
461 dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
462
d812e575 463 /* Only do I/O if gfp is a superset of GFP_KERNEL */
b608b283
TM
464 if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) {
465 int how = FLUSH_SYNC;
466
467 /* Don't let kswapd deadlock waiting for OOM RPC calls */
468 if (current_is_kswapd())
469 how = 0;
470 nfs_commit_inode(mapping->host, how);
471 }
e3db7691 472 /* If PagePrivate() is set, then the page is not freeable */
545db45f
DH
473 if (PagePrivate(page))
474 return 0;
475 return nfs_fscache_release_page(page, gfp);
e3db7691
TM
476}
477
6b9b3514
DH
478/*
479 * Attempt to clear the private state associated with a page when an error
480 * occurs that requires the cached contents of an inode to be written back or
481 * destroyed
545db45f 482 * - Called if either PG_private or fscache is set on the page
6b9b3514
DH
483 * - Caller holds page lock
484 * - Return 0 if successful, -error otherwise
485 */
e3db7691
TM
486static int nfs_launder_page(struct page *page)
487{
b7eaefaa 488 struct inode *inode = page->mapping->host;
545db45f 489 struct nfs_inode *nfsi = NFS_I(inode);
b7eaefaa
CL
490
491 dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
492 inode->i_ino, (long long)page_offset(page));
493
545db45f 494 nfs_fscache_wait_on_page_write(nfsi, page);
b7eaefaa 495 return nfs_wb_page(inode, page);
cd52ed35
TM
496}
497
f5e54d6e 498const struct address_space_operations nfs_file_aops = {
1da177e4
LT
499 .readpage = nfs_readpage,
500 .readpages = nfs_readpages,
9cccef95 501 .set_page_dirty = __set_page_dirty_nobuffers,
1da177e4
LT
502 .writepage = nfs_writepage,
503 .writepages = nfs_writepages,
4899f9c8
NP
504 .write_begin = nfs_write_begin,
505 .write_end = nfs_write_end,
cd52ed35
TM
506 .invalidatepage = nfs_invalidate_page,
507 .releasepage = nfs_release_page,
1da177e4 508 .direct_IO = nfs_direct_IO,
074cc1de 509 .migratepage = nfs_migrate_page,
e3db7691 510 .launder_page = nfs_launder_page,
f590f333 511 .error_remove_page = generic_error_remove_page,
1da177e4
LT
512};
513
6b9b3514
DH
514/*
515 * Notification that a PTE pointing to an NFS page is about to be made
516 * writable, implying that someone is about to modify the page through a
517 * shared-writable mapping
518 */
c2ec175c 519static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
94387fb1 520{
c2ec175c 521 struct page *page = vmf->page;
94387fb1 522 struct file *filp = vma->vm_file;
b7eaefaa 523 struct dentry *dentry = filp->f_path.dentry;
94387fb1 524 unsigned pagelen;
bc4866b6 525 int ret = VM_FAULT_NOPAGE;
4899f9c8 526 struct address_space *mapping;
94387fb1 527
b7eaefaa
CL
528 dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
529 dentry->d_parent->d_name.name, dentry->d_name.name,
530 filp->f_mapping->host->i_ino,
531 (long long)page_offset(page));
532
545db45f
DH
533 /* make sure the cache has finished storing the page */
534 nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
535
94387fb1 536 lock_page(page);
4899f9c8 537 mapping = page->mapping;
b7eaefaa 538 if (mapping != dentry->d_inode->i_mapping)
8b1f9ee5
TM
539 goto out_unlock;
540
2aeb98f4
TM
541 wait_on_page_writeback(page);
542
94387fb1 543 pagelen = nfs_page_length(page);
8b1f9ee5
TM
544 if (pagelen == 0)
545 goto out_unlock;
4899f9c8 546
bc4866b6
TM
547 ret = VM_FAULT_LOCKED;
548 if (nfs_flush_incompatible(filp, page) == 0 &&
549 nfs_updatepage(filp, page, 0, pagelen) == 0)
550 goto out;
8b1f9ee5 551
bc4866b6 552 ret = VM_FAULT_SIGBUS;
8b1f9ee5
TM
553out_unlock:
554 unlock_page(page);
bc4866b6
TM
555out:
556 return ret;
94387fb1
TM
557}
558
f0f37e2f 559static const struct vm_operations_struct nfs_file_vm_ops = {
94387fb1
TM
560 .fault = filemap_fault,
561 .page_mkwrite = nfs_vm_page_mkwrite,
562};
563
7b159fc1
TM
564static int nfs_need_sync_write(struct file *filp, struct inode *inode)
565{
566 struct nfs_open_context *ctx;
567
6b2f3d1f 568 if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
7b159fc1 569 return 1;
cd3758e3 570 ctx = nfs_file_open_context(filp);
7b159fc1
TM
571 if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
572 return 1;
573 return 0;
574}
575
027445c3
BP
576static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
577 unsigned long nr_segs, loff_t pos)
1da177e4 578{
01cce933 579 struct dentry * dentry = iocb->ki_filp->f_path.dentry;
1da177e4 580 struct inode * inode = dentry->d_inode;
7e381172 581 unsigned long written = 0;
1da177e4 582 ssize_t result;
027445c3 583 size_t count = iov_length(iov, nr_segs);
1da177e4 584
1da177e4 585 if (iocb->ki_filp->f_flags & O_DIRECT)
027445c3 586 return nfs_file_direct_write(iocb, iov, nr_segs, pos);
1da177e4 587
6da24bc9 588 dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
1da177e4 589 dentry->d_parent->d_name.name, dentry->d_name.name,
6da24bc9 590 (unsigned long) count, (long long) pos);
1da177e4
LT
591
592 result = -EBUSY;
593 if (IS_SWAPFILE(inode))
594 goto out_swapfile;
7d52e862
TM
595 /*
596 * O_APPEND implies that we must revalidate the file length.
597 */
598 if (iocb->ki_filp->f_flags & O_APPEND) {
599 result = nfs_revalidate_file_size(inode, iocb->ki_filp);
600 if (result)
601 goto out;
fe51beec 602 }
1da177e4
LT
603
604 result = count;
605 if (!count)
606 goto out;
607
027445c3 608 result = generic_file_aio_write(iocb, iov, nr_segs, pos);
7e381172
CL
609 if (result > 0)
610 written = result;
611
6b2f3d1f 612 /* Return error values for O_DSYNC and IS_SYNC() */
7b159fc1 613 if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
af7fa165 614 int err = vfs_fsync(iocb->ki_filp, 0);
200baa21
TM
615 if (err < 0)
616 result = err;
617 }
7e381172
CL
618 if (result > 0)
619 nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
1da177e4
LT
620out:
621 return result;
622
623out_swapfile:
624 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
625 goto out;
626}
627
bf40d343
SJ
628static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
629 struct file *filp, loff_t *ppos,
630 size_t count, unsigned int flags)
631{
632 struct dentry *dentry = filp->f_path.dentry;
633 struct inode *inode = dentry->d_inode;
7e381172 634 unsigned long written = 0;
bf40d343
SJ
635 ssize_t ret;
636
637 dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
638 dentry->d_parent->d_name.name, dentry->d_name.name,
639 (unsigned long) count, (unsigned long long) *ppos);
640
641 /*
642 * The combination of splice and an O_APPEND destination is disallowed.
643 */
644
bf40d343 645 ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
7e381172
CL
646 if (ret > 0)
647 written = ret;
648
bf40d343 649 if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
af7fa165 650 int err = vfs_fsync(filp, 0);
bf40d343
SJ
651 if (err < 0)
652 ret = err;
653 }
7e381172
CL
654 if (ret > 0)
655 nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
bf40d343
SJ
656 return ret;
657}
658
5eebde23
SJ
659static int
660do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
1da177e4
LT
661{
662 struct inode *inode = filp->f_mapping->host;
663 int status = 0;
21ac19d4 664 unsigned int saved_type = fl->fl_type;
1da177e4 665
039c4d7a 666 /* Try local locking first */
6d34ac19
BF
667 posix_test_lock(filp, fl);
668 if (fl->fl_type != F_UNLCK) {
669 /* found a conflict */
039c4d7a 670 goto out;
1da177e4 671 }
21ac19d4 672 fl->fl_type = saved_type;
039c4d7a 673
011e2a7f 674 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
039c4d7a
TM
675 goto out_noconflict;
676
5eebde23 677 if (is_local)
039c4d7a
TM
678 goto out_noconflict;
679
680 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
681out:
1da177e4 682 return status;
039c4d7a
TM
683out_noconflict:
684 fl->fl_type = F_UNLCK;
685 goto out;
1da177e4
LT
686}
687
688static int do_vfs_lock(struct file *file, struct file_lock *fl)
689{
690 int res = 0;
691 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
692 case FL_POSIX:
693 res = posix_lock_file_wait(file, fl);
694 break;
695 case FL_FLOCK:
696 res = flock_lock_file_wait(file, fl);
697 break;
698 default:
699 BUG();
700 }
1da177e4
LT
701 return res;
702}
703
5eebde23
SJ
704static int
705do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
1da177e4
LT
706{
707 struct inode *inode = filp->f_mapping->host;
1da177e4
LT
708 int status;
709
1da177e4
LT
710 /*
711 * Flush all pending writes before doing anything
712 * with locks..
713 */
29884df0 714 nfs_sync_mapping(filp->f_mapping);
1da177e4
LT
715
716 /* NOTE: special case
717 * If we're signalled while cleaning up locks on process exit, we
718 * still need to complete the unlock.
719 */
5eebde23
SJ
720 /*
721 * Use local locking if mounted with "-onolock" or with appropriate
722 * "-olocal_lock="
723 */
724 if (!is_local)
1da177e4
LT
725 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
726 else
727 status = do_vfs_lock(filp, fl);
1da177e4
LT
728 return status;
729}
730
6b96724e
RL
731static int
732is_time_granular(struct timespec *ts) {
733 return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
734}
735
5eebde23
SJ
736static int
737do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
1da177e4
LT
738{
739 struct inode *inode = filp->f_mapping->host;
1da177e4
LT
740 int status;
741
1da177e4
LT
742 /*
743 * Flush all pending writes before doing anything
744 * with locks..
745 */
29884df0
TM
746 status = nfs_sync_mapping(filp->f_mapping);
747 if (status != 0)
1da177e4
LT
748 goto out;
749
5eebde23
SJ
750 /*
751 * Use local locking if mounted with "-onolock" or with appropriate
752 * "-olocal_lock="
753 */
754 if (!is_local)
1da177e4 755 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
c4d7c402 756 else
1da177e4 757 status = do_vfs_lock(filp, fl);
1da177e4
LT
758 if (status < 0)
759 goto out;
6b96724e 760
1da177e4 761 /*
6b96724e
RL
762 * Revalidate the cache if the server has time stamps granular
763 * enough to detect subsecond changes. Otherwise, clear the
764 * cache to prevent missing any changes.
765 *
1da177e4
LT
766 * This makes locking act as a cache coherency point.
767 */
29884df0 768 nfs_sync_mapping(filp->f_mapping);
011e2a7f 769 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
6b96724e
RL
770 if (is_time_granular(&NFS_SERVER(inode)->time_delta))
771 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
772 else
773 nfs_zap_caches(inode);
774 }
1da177e4 775out:
1da177e4
LT
776 return status;
777}
778
779/*
780 * Lock a (portion of) a file
781 */
782static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
783{
6da24bc9 784 struct inode *inode = filp->f_mapping->host;
2116271a 785 int ret = -ENOLCK;
5eebde23 786 int is_local = 0;
1da177e4 787
6da24bc9
CL
788 dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
789 filp->f_path.dentry->d_parent->d_name.name,
790 filp->f_path.dentry->d_name.name,
1da177e4
LT
791 fl->fl_type, fl->fl_flags,
792 (long long)fl->fl_start, (long long)fl->fl_end);
6da24bc9 793
91d5b470 794 nfs_inc_stats(inode, NFSIOS_VFSLOCK);
1da177e4
LT
795
796 /* No mandatory locks over NFS */
dfad9441 797 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
2116271a
TM
798 goto out_err;
799
5eebde23
SJ
800 if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
801 is_local = 1;
802
2116271a
TM
803 if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
804 ret = NFS_PROTO(inode)->lock_check_bounds(fl);
805 if (ret < 0)
806 goto out_err;
807 }
1da177e4
LT
808
809 if (IS_GETLK(cmd))
5eebde23 810 ret = do_getlk(filp, cmd, fl, is_local);
2116271a 811 else if (fl->fl_type == F_UNLCK)
5eebde23 812 ret = do_unlk(filp, cmd, fl, is_local);
2116271a 813 else
5eebde23 814 ret = do_setlk(filp, cmd, fl, is_local);
2116271a
TM
815out_err:
816 return ret;
1da177e4
LT
817}
818
819/*
820 * Lock a (portion of) a file
821 */
822static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
823{
5eebde23
SJ
824 struct inode *inode = filp->f_mapping->host;
825 int is_local = 0;
826
6da24bc9
CL
827 dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
828 filp->f_path.dentry->d_parent->d_name.name,
829 filp->f_path.dentry->d_name.name,
1da177e4
LT
830 fl->fl_type, fl->fl_flags);
831
1da177e4
LT
832 if (!(fl->fl_flags & FL_FLOCK))
833 return -ENOLCK;
834
5eebde23
SJ
835 if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
836 is_local = 1;
837
1da177e4
LT
838 /* We're simulating flock() locks using posix locks on the server */
839 fl->fl_owner = (fl_owner_t)filp;
840 fl->fl_start = 0;
841 fl->fl_end = OFFSET_MAX;
842
843 if (fl->fl_type == F_UNLCK)
5eebde23
SJ
844 return do_unlk(filp, cmd, fl, is_local);
845 return do_setlk(filp, cmd, fl, is_local);
1da177e4 846}
370f6599 847
6da24bc9
CL
848/*
849 * There is no protocol support for leases, so we have no way to implement
850 * them correctly in the face of opens by other clients.
851 */
370f6599
BF
852static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
853{
6da24bc9
CL
854 dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
855 file->f_path.dentry->d_parent->d_name.name,
856 file->f_path.dentry->d_name.name, arg);
370f6599
BF
857 return -EINVAL;
858}
1788ea6e 859
0486958f
JL
860const struct file_operations nfs_file_operations = {
861 .llseek = nfs_file_llseek,
862 .read = do_sync_read,
863 .write = do_sync_write,
864 .aio_read = nfs_file_read,
865 .aio_write = nfs_file_write,
866 .mmap = nfs_file_mmap,
867 .open = nfs_file_open,
868 .flush = nfs_file_flush,
869 .release = nfs_file_release,
870 .fsync = nfs_file_fsync,
871 .lock = nfs_lock,
872 .flock = nfs_flock,
873 .splice_read = nfs_file_splice_read,
874 .splice_write = nfs_file_splice_write,
875 .check_flags = nfs_check_flags,
876 .setlease = nfs_setlease,
877};
878
1788ea6e
JL
879#ifdef CONFIG_NFS_V4
880static int
881nfs4_file_open(struct inode *inode, struct file *filp)
882{
0ef97dcf
MS
883 struct nfs_open_context *ctx;
884 struct dentry *dentry = filp->f_path.dentry;
885 struct dentry *parent = NULL;
886 struct inode *dir;
887 unsigned openflags = filp->f_flags;
888 struct iattr attr;
889 int err;
890
891 BUG_ON(inode != dentry->d_inode);
1788ea6e 892 /*
0ef97dcf
MS
893 * If no cached dentry exists or if it's negative, NFSv4 handled the
894 * opens in ->lookup() or ->create().
895 *
896 * We only get this far for a cached positive dentry. We skipped
897 * revalidation, so handle it here by dropping the dentry and returning
898 * -EOPENSTALE. The VFS will retry the lookup/create/open.
1788ea6e 899 */
0ef97dcf
MS
900
901 dprintk("NFS: open file(%s/%s)\n",
902 dentry->d_parent->d_name.name,
903 dentry->d_name.name);
904
905 if ((openflags & O_ACCMODE) == 3)
906 openflags--;
907
908 /* We can't create new files here */
909 openflags &= ~(O_CREAT|O_EXCL);
910
911 parent = dget_parent(dentry);
912 dir = parent->d_inode;
913
914 ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
915 err = PTR_ERR(ctx);
916 if (IS_ERR(ctx))
917 goto out;
918
919 attr.ia_valid = ATTR_OPEN;
920 if (openflags & O_TRUNC) {
921 attr.ia_valid |= ATTR_SIZE;
922 attr.ia_size = 0;
923 nfs_wb_all(inode);
924 }
925
926 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr);
927 if (IS_ERR(inode)) {
928 err = PTR_ERR(inode);
929 switch (err) {
930 case -EPERM:
931 case -EACCES:
932 case -EDQUOT:
933 case -ENOSPC:
934 case -EROFS:
935 goto out_put_ctx;
936 default:
937 goto out_drop;
938 }
939 }
940 iput(inode);
941 if (inode != dentry->d_inode)
942 goto out_drop;
943
944 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
945 nfs_file_set_open_context(filp, ctx);
946 err = 0;
947
948out_put_ctx:
949 put_nfs_open_context(ctx);
950out:
951 dput(parent);
952 return err;
953
954out_drop:
955 d_drop(dentry);
956 err = -EOPENSTALE;
957 goto out_put_ctx;
1788ea6e
JL
958}
959
a5c58892
BS
960static int
961nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
962{
963 int ret;
964 struct inode *inode = file->f_path.dentry->d_inode;
965
966 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
967 mutex_lock(&inode->i_mutex);
968 ret = nfs_file_fsync_commit(file, start, end, datasync);
969 if (!ret && !datasync)
970 /* application has asked for meta-data sync */
971 ret = pnfs_layoutcommit_inode(inode, true);
972 mutex_unlock(&inode->i_mutex);
973
974 return ret;
975}
976
1788ea6e
JL
977const struct file_operations nfs4_file_operations = {
978 .llseek = nfs_file_llseek,
979 .read = do_sync_read,
980 .write = do_sync_write,
981 .aio_read = nfs_file_read,
982 .aio_write = nfs_file_write,
983 .mmap = nfs_file_mmap,
984 .open = nfs4_file_open,
985 .flush = nfs_file_flush,
986 .release = nfs_file_release,
a5c58892 987 .fsync = nfs4_file_fsync,
1788ea6e
JL
988 .lock = nfs_lock,
989 .flock = nfs_flock,
990 .splice_read = nfs_file_splice_read,
991 .splice_write = nfs_file_splice_write,
992 .check_flags = nfs_check_flags,
993 .setlease = nfs_setlease,
994};
995#endif /* CONFIG_NFS_V4 */