NFS: Fix byte accounting for generic NFS reads
[safe/jmp/linux-2.6] / fs / nfs / file.c
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>
27 #include <linux/slab.h>
28 #include <linux/pagemap.h>
29 #include <linux/aio.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33
34 #include "delegation.h"
35 #include "internal.h"
36 #include "iostat.h"
37 #include "fscache.h"
38
39 #define NFSDBG_FACILITY         NFSDBG_FILE
40
41 static int nfs_file_open(struct inode *, struct file *);
42 static int nfs_file_release(struct inode *, struct file *);
43 static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
44 static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
45 static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
46                                         struct pipe_inode_info *pipe,
47                                         size_t count, unsigned int flags);
48 static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
49                                 unsigned long nr_segs, loff_t pos);
50 static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
51                                         struct file *filp, loff_t *ppos,
52                                         size_t count, unsigned int flags);
53 static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
54                                 unsigned long nr_segs, loff_t pos);
55 static int  nfs_file_flush(struct file *, fl_owner_t id);
56 static int  nfs_file_fsync(struct file *, struct dentry *dentry, int datasync);
57 static int nfs_check_flags(int flags);
58 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
59 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
60 static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
61
62 static const struct vm_operations_struct nfs_file_vm_ops;
63
64 const struct file_operations nfs_file_operations = {
65         .llseek         = nfs_file_llseek,
66         .read           = do_sync_read,
67         .write          = do_sync_write,
68         .aio_read       = nfs_file_read,
69         .aio_write      = nfs_file_write,
70         .mmap           = nfs_file_mmap,
71         .open           = nfs_file_open,
72         .flush          = nfs_file_flush,
73         .release        = nfs_file_release,
74         .fsync          = nfs_file_fsync,
75         .lock           = nfs_lock,
76         .flock          = nfs_flock,
77         .splice_read    = nfs_file_splice_read,
78         .splice_write   = nfs_file_splice_write,
79         .check_flags    = nfs_check_flags,
80         .setlease       = nfs_setlease,
81 };
82
83 const struct inode_operations nfs_file_inode_operations = {
84         .permission     = nfs_permission,
85         .getattr        = nfs_getattr,
86         .setattr        = nfs_setattr,
87 };
88
89 #ifdef CONFIG_NFS_V3
90 const struct inode_operations nfs3_file_inode_operations = {
91         .permission     = nfs_permission,
92         .getattr        = nfs_getattr,
93         .setattr        = nfs_setattr,
94         .listxattr      = nfs3_listxattr,
95         .getxattr       = nfs3_getxattr,
96         .setxattr       = nfs3_setxattr,
97         .removexattr    = nfs3_removexattr,
98 };
99 #endif  /* CONFIG_NFS_v3 */
100
101 /* Hack for future NFS swap support */
102 #ifndef IS_SWAPFILE
103 # define IS_SWAPFILE(inode)     (0)
104 #endif
105
106 static int nfs_check_flags(int flags)
107 {
108         if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
109                 return -EINVAL;
110
111         return 0;
112 }
113
114 /*
115  * Open file
116  */
117 static int
118 nfs_file_open(struct inode *inode, struct file *filp)
119 {
120         int res;
121
122         dprintk("NFS: open file(%s/%s)\n",
123                         filp->f_path.dentry->d_parent->d_name.name,
124                         filp->f_path.dentry->d_name.name);
125
126         nfs_inc_stats(inode, NFSIOS_VFSOPEN);
127         res = nfs_check_flags(filp->f_flags);
128         if (res)
129                 return res;
130
131         res = nfs_open(inode, filp);
132         return res;
133 }
134
135 static int
136 nfs_file_release(struct inode *inode, struct file *filp)
137 {
138         struct dentry *dentry = filp->f_path.dentry;
139
140         dprintk("NFS: release(%s/%s)\n",
141                         dentry->d_parent->d_name.name,
142                         dentry->d_name.name);
143
144         nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
145         return nfs_release(inode, filp);
146 }
147
148 /**
149  * nfs_revalidate_size - Revalidate the file size
150  * @inode - pointer to inode struct
151  * @file - pointer to struct file
152  *
153  * Revalidates the file length. This is basically a wrapper around
154  * nfs_revalidate_inode() that takes into account the fact that we may
155  * have cached writes (in which case we don't care about the server's
156  * idea of what the file length is), or O_DIRECT (in which case we
157  * shouldn't trust the cache).
158  */
159 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
160 {
161         struct nfs_server *server = NFS_SERVER(inode);
162         struct nfs_inode *nfsi = NFS_I(inode);
163
164         if (server->flags & NFS_MOUNT_NOAC)
165                 goto force_reval;
166         if (filp->f_flags & O_DIRECT)
167                 goto force_reval;
168         if (nfsi->npages != 0)
169                 return 0;
170         if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
171                 return 0;
172 force_reval:
173         return __nfs_revalidate_inode(server, inode);
174 }
175
176 static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
177 {
178         loff_t loff;
179
180         dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
181                         filp->f_path.dentry->d_parent->d_name.name,
182                         filp->f_path.dentry->d_name.name,
183                         offset, origin);
184
185         /* origin == SEEK_END => we must revalidate the cached file length */
186         if (origin == SEEK_END) {
187                 struct inode *inode = filp->f_mapping->host;
188
189                 int retval = nfs_revalidate_file_size(inode, filp);
190                 if (retval < 0)
191                         return (loff_t)retval;
192
193                 spin_lock(&inode->i_lock);
194                 loff = generic_file_llseek_unlocked(filp, offset, origin);
195                 spin_unlock(&inode->i_lock);
196         } else
197                 loff = generic_file_llseek_unlocked(filp, offset, origin);
198         return loff;
199 }
200
201 /*
202  * Helper for nfs_file_flush() and nfs_file_fsync()
203  *
204  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
205  * disk, but it retrieves and clears ctx->error after synching, despite
206  * the two being set at the same time in nfs_context_set_write_error().
207  * This is because the former is used to notify the _next_ call to
208  * nfs_file_write() that a write error occured, and hence cause it to
209  * fall back to doing a synchronous write.
210  */
211 static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
212 {
213         int have_error, status;
214         int ret = 0;
215
216         have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
217         status = nfs_wb_all(inode);
218         have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
219         if (have_error)
220                 ret = xchg(&ctx->error, 0);
221         if (!ret)
222                 ret = status;
223         return ret;
224 }
225
226 /*
227  * Flush all dirty pages, and check for write errors.
228  */
229 static int
230 nfs_file_flush(struct file *file, fl_owner_t id)
231 {
232         struct nfs_open_context *ctx = nfs_file_open_context(file);
233         struct dentry   *dentry = file->f_path.dentry;
234         struct inode    *inode = dentry->d_inode;
235
236         dprintk("NFS: flush(%s/%s)\n",
237                         dentry->d_parent->d_name.name,
238                         dentry->d_name.name);
239
240         nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
241         if ((file->f_mode & FMODE_WRITE) == 0)
242                 return 0;
243
244         /* Flush writes to the server and return any errors */
245         return nfs_do_fsync(ctx, inode);
246 }
247
248 static ssize_t
249 nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
250                 unsigned long nr_segs, loff_t pos)
251 {
252         struct dentry * dentry = iocb->ki_filp->f_path.dentry;
253         struct inode * inode = dentry->d_inode;
254         ssize_t result;
255         size_t count = iov_length(iov, nr_segs);
256
257         if (iocb->ki_filp->f_flags & O_DIRECT)
258                 return nfs_file_direct_read(iocb, iov, nr_segs, pos);
259
260         dprintk("NFS: read(%s/%s, %lu@%lu)\n",
261                 dentry->d_parent->d_name.name, dentry->d_name.name,
262                 (unsigned long) count, (unsigned long) pos);
263
264         result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
265         if (!result) {
266                 result = generic_file_aio_read(iocb, iov, nr_segs, pos);
267                 if (result > 0)
268                         nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
269         }
270         return result;
271 }
272
273 static ssize_t
274 nfs_file_splice_read(struct file *filp, loff_t *ppos,
275                      struct pipe_inode_info *pipe, size_t count,
276                      unsigned int flags)
277 {
278         struct dentry *dentry = filp->f_path.dentry;
279         struct inode *inode = dentry->d_inode;
280         ssize_t res;
281
282         dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
283                 dentry->d_parent->d_name.name, dentry->d_name.name,
284                 (unsigned long) count, (unsigned long long) *ppos);
285
286         res = nfs_revalidate_mapping(inode, filp->f_mapping);
287         if (!res)
288                 res = generic_file_splice_read(filp, ppos, pipe, count, flags);
289         return res;
290 }
291
292 static int
293 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
294 {
295         struct dentry *dentry = file->f_path.dentry;
296         struct inode *inode = dentry->d_inode;
297         int     status;
298
299         dprintk("NFS: mmap(%s/%s)\n",
300                 dentry->d_parent->d_name.name, dentry->d_name.name);
301
302         /* Note: generic_file_mmap() returns ENOSYS on nommu systems
303          *       so we call that before revalidating the mapping
304          */
305         status = generic_file_mmap(file, vma);
306         if (!status) {
307                 vma->vm_ops = &nfs_file_vm_ops;
308                 status = nfs_revalidate_mapping(inode, file->f_mapping);
309         }
310         return status;
311 }
312
313 /*
314  * Flush any dirty pages for this process, and check for write errors.
315  * The return status from this call provides a reliable indication of
316  * whether any write errors occurred for this process.
317  */
318 static int
319 nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
320 {
321         struct nfs_open_context *ctx = nfs_file_open_context(file);
322         struct inode *inode = dentry->d_inode;
323
324         dprintk("NFS: fsync file(%s/%s) datasync %d\n",
325                         dentry->d_parent->d_name.name, dentry->d_name.name,
326                         datasync);
327
328         nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
329         return nfs_do_fsync(ctx, inode);
330 }
331
332 /*
333  * Decide whether a read/modify/write cycle may be more efficient
334  * then a modify/write/read cycle when writing to a page in the
335  * page cache.
336  *
337  * The modify/write/read cycle may occur if a page is read before
338  * being completely filled by the writer.  In this situation, the
339  * page must be completely written to stable storage on the server
340  * before it can be refilled by reading in the page from the server.
341  * This can lead to expensive, small, FILE_SYNC mode writes being
342  * done.
343  *
344  * It may be more efficient to read the page first if the file is
345  * open for reading in addition to writing, the page is not marked
346  * as Uptodate, it is not dirty or waiting to be committed,
347  * indicating that it was previously allocated and then modified,
348  * that there were valid bytes of data in that range of the file,
349  * and that the new data won't completely replace the old data in
350  * that range of the file.
351  */
352 static int nfs_want_read_modify_write(struct file *file, struct page *page,
353                         loff_t pos, unsigned len)
354 {
355         unsigned int pglen = nfs_page_length(page);
356         unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
357         unsigned int end = offset + len;
358
359         if ((file->f_mode & FMODE_READ) &&      /* open for read? */
360             !PageUptodate(page) &&              /* Uptodate? */
361             !PagePrivate(page) &&               /* i/o request already? */
362             pglen &&                            /* valid bytes of file? */
363             (end < pglen || offset))            /* replace all valid bytes? */
364                 return 1;
365         return 0;
366 }
367
368 /*
369  * This does the "real" work of the write. We must allocate and lock the
370  * page to be sent back to the generic routine, which then copies the
371  * data from user space.
372  *
373  * If the writer ends up delaying the write, the writer needs to
374  * increment the page use counts until he is done with the page.
375  */
376 static int nfs_write_begin(struct file *file, struct address_space *mapping,
377                         loff_t pos, unsigned len, unsigned flags,
378                         struct page **pagep, void **fsdata)
379 {
380         int ret;
381         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
382         struct page *page;
383         int once_thru = 0;
384
385         dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
386                 file->f_path.dentry->d_parent->d_name.name,
387                 file->f_path.dentry->d_name.name,
388                 mapping->host->i_ino, len, (long long) pos);
389
390 start:
391         /*
392          * Prevent starvation issues if someone is doing a consistency
393          * sync-to-disk
394          */
395         ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
396                         nfs_wait_bit_killable, TASK_KILLABLE);
397         if (ret)
398                 return ret;
399
400         page = grab_cache_page_write_begin(mapping, index, flags);
401         if (!page)
402                 return -ENOMEM;
403         *pagep = page;
404
405         ret = nfs_flush_incompatible(file, page);
406         if (ret) {
407                 unlock_page(page);
408                 page_cache_release(page);
409         } else if (!once_thru &&
410                    nfs_want_read_modify_write(file, page, pos, len)) {
411                 once_thru = 1;
412                 ret = nfs_readpage(file, page);
413                 page_cache_release(page);
414                 if (!ret)
415                         goto start;
416         }
417         return ret;
418 }
419
420 static int nfs_write_end(struct file *file, struct address_space *mapping,
421                         loff_t pos, unsigned len, unsigned copied,
422                         struct page *page, void *fsdata)
423 {
424         unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
425         int status;
426
427         dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
428                 file->f_path.dentry->d_parent->d_name.name,
429                 file->f_path.dentry->d_name.name,
430                 mapping->host->i_ino, len, (long long) pos);
431
432         /*
433          * Zero any uninitialised parts of the page, and then mark the page
434          * as up to date if it turns out that we're extending the file.
435          */
436         if (!PageUptodate(page)) {
437                 unsigned pglen = nfs_page_length(page);
438                 unsigned end = offset + len;
439
440                 if (pglen == 0) {
441                         zero_user_segments(page, 0, offset,
442                                         end, PAGE_CACHE_SIZE);
443                         SetPageUptodate(page);
444                 } else if (end >= pglen) {
445                         zero_user_segment(page, end, PAGE_CACHE_SIZE);
446                         if (offset == 0)
447                                 SetPageUptodate(page);
448                 } else
449                         zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
450         }
451
452         status = nfs_updatepage(file, page, offset, copied);
453
454         unlock_page(page);
455         page_cache_release(page);
456
457         if (status < 0)
458                 return status;
459         return copied;
460 }
461
462 /*
463  * Partially or wholly invalidate a page
464  * - Release the private state associated with a page if undergoing complete
465  *   page invalidation
466  * - Called if either PG_private or PG_fscache is set on the page
467  * - Caller holds page lock
468  */
469 static void nfs_invalidate_page(struct page *page, unsigned long offset)
470 {
471         dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
472
473         if (offset != 0)
474                 return;
475         /* Cancel any unstarted writes on this page */
476         nfs_wb_page_cancel(page->mapping->host, page);
477
478         nfs_fscache_invalidate_page(page, page->mapping->host);
479 }
480
481 /*
482  * Attempt to release the private state associated with a page
483  * - Called if either PG_private or PG_fscache is set on the page
484  * - Caller holds page lock
485  * - Return true (may release page) or false (may not)
486  */
487 static int nfs_release_page(struct page *page, gfp_t gfp)
488 {
489         dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
490
491         if (gfp & __GFP_WAIT)
492                 nfs_wb_page(page->mapping->host, page);
493         /* If PagePrivate() is set, then the page is not freeable */
494         if (PagePrivate(page))
495                 return 0;
496         return nfs_fscache_release_page(page, gfp);
497 }
498
499 /*
500  * Attempt to clear the private state associated with a page when an error
501  * occurs that requires the cached contents of an inode to be written back or
502  * destroyed
503  * - Called if either PG_private or fscache is set on the page
504  * - Caller holds page lock
505  * - Return 0 if successful, -error otherwise
506  */
507 static int nfs_launder_page(struct page *page)
508 {
509         struct inode *inode = page->mapping->host;
510         struct nfs_inode *nfsi = NFS_I(inode);
511
512         dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
513                 inode->i_ino, (long long)page_offset(page));
514
515         nfs_fscache_wait_on_page_write(nfsi, page);
516         return nfs_wb_page(inode, page);
517 }
518
519 const struct address_space_operations nfs_file_aops = {
520         .readpage = nfs_readpage,
521         .readpages = nfs_readpages,
522         .set_page_dirty = __set_page_dirty_nobuffers,
523         .writepage = nfs_writepage,
524         .writepages = nfs_writepages,
525         .write_begin = nfs_write_begin,
526         .write_end = nfs_write_end,
527         .invalidatepage = nfs_invalidate_page,
528         .releasepage = nfs_release_page,
529         .direct_IO = nfs_direct_IO,
530         .migratepage = nfs_migrate_page,
531         .launder_page = nfs_launder_page,
532         .error_remove_page = generic_error_remove_page,
533 };
534
535 /*
536  * Notification that a PTE pointing to an NFS page is about to be made
537  * writable, implying that someone is about to modify the page through a
538  * shared-writable mapping
539  */
540 static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
541 {
542         struct page *page = vmf->page;
543         struct file *filp = vma->vm_file;
544         struct dentry *dentry = filp->f_path.dentry;
545         unsigned pagelen;
546         int ret = -EINVAL;
547         struct address_space *mapping;
548
549         dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
550                 dentry->d_parent->d_name.name, dentry->d_name.name,
551                 filp->f_mapping->host->i_ino,
552                 (long long)page_offset(page));
553
554         /* make sure the cache has finished storing the page */
555         nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
556
557         lock_page(page);
558         mapping = page->mapping;
559         if (mapping != dentry->d_inode->i_mapping)
560                 goto out_unlock;
561
562         ret = 0;
563         pagelen = nfs_page_length(page);
564         if (pagelen == 0)
565                 goto out_unlock;
566
567         ret = nfs_flush_incompatible(filp, page);
568         if (ret != 0)
569                 goto out_unlock;
570
571         ret = nfs_updatepage(filp, page, 0, pagelen);
572 out_unlock:
573         if (!ret)
574                 return VM_FAULT_LOCKED;
575         unlock_page(page);
576         return VM_FAULT_SIGBUS;
577 }
578
579 static const struct vm_operations_struct nfs_file_vm_ops = {
580         .fault = filemap_fault,
581         .page_mkwrite = nfs_vm_page_mkwrite,
582 };
583
584 static int nfs_need_sync_write(struct file *filp, struct inode *inode)
585 {
586         struct nfs_open_context *ctx;
587
588         if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
589                 return 1;
590         ctx = nfs_file_open_context(filp);
591         if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
592                 return 1;
593         return 0;
594 }
595
596 static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
597                                 unsigned long nr_segs, loff_t pos)
598 {
599         struct dentry * dentry = iocb->ki_filp->f_path.dentry;
600         struct inode * inode = dentry->d_inode;
601         ssize_t result;
602         size_t count = iov_length(iov, nr_segs);
603
604         if (iocb->ki_filp->f_flags & O_DIRECT)
605                 return nfs_file_direct_write(iocb, iov, nr_segs, pos);
606
607         dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
608                 dentry->d_parent->d_name.name, dentry->d_name.name,
609                 (unsigned long) count, (long long) pos);
610
611         result = -EBUSY;
612         if (IS_SWAPFILE(inode))
613                 goto out_swapfile;
614         /*
615          * O_APPEND implies that we must revalidate the file length.
616          */
617         if (iocb->ki_filp->f_flags & O_APPEND) {
618                 result = nfs_revalidate_file_size(inode, iocb->ki_filp);
619                 if (result)
620                         goto out;
621         }
622
623         result = count;
624         if (!count)
625                 goto out;
626
627         nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
628         result = generic_file_aio_write(iocb, iov, nr_segs, pos);
629         /* Return error values for O_DSYNC and IS_SYNC() */
630         if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
631                 int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
632                 if (err < 0)
633                         result = err;
634         }
635 out:
636         return result;
637
638 out_swapfile:
639         printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
640         goto out;
641 }
642
643 static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
644                                      struct file *filp, loff_t *ppos,
645                                      size_t count, unsigned int flags)
646 {
647         struct dentry *dentry = filp->f_path.dentry;
648         struct inode *inode = dentry->d_inode;
649         ssize_t ret;
650
651         dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
652                 dentry->d_parent->d_name.name, dentry->d_name.name,
653                 (unsigned long) count, (unsigned long long) *ppos);
654
655         /*
656          * The combination of splice and an O_APPEND destination is disallowed.
657          */
658
659         nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
660
661         ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
662         if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
663                 int err = nfs_do_fsync(nfs_file_open_context(filp), inode);
664                 if (err < 0)
665                         ret = err;
666         }
667         return ret;
668 }
669
670 static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
671 {
672         struct inode *inode = filp->f_mapping->host;
673         int status = 0;
674
675         /* Try local locking first */
676         posix_test_lock(filp, fl);
677         if (fl->fl_type != F_UNLCK) {
678                 /* found a conflict */
679                 goto out;
680         }
681
682         if (nfs_have_delegation(inode, FMODE_READ))
683                 goto out_noconflict;
684
685         if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
686                 goto out_noconflict;
687
688         status = NFS_PROTO(inode)->lock(filp, cmd, fl);
689 out:
690         return status;
691 out_noconflict:
692         fl->fl_type = F_UNLCK;
693         goto out;
694 }
695
696 static int do_vfs_lock(struct file *file, struct file_lock *fl)
697 {
698         int res = 0;
699         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
700                 case FL_POSIX:
701                         res = posix_lock_file_wait(file, fl);
702                         break;
703                 case FL_FLOCK:
704                         res = flock_lock_file_wait(file, fl);
705                         break;
706                 default:
707                         BUG();
708         }
709         if (res < 0)
710                 dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
711                         " - error %d!\n",
712                                 __func__, res);
713         return res;
714 }
715
716 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
717 {
718         struct inode *inode = filp->f_mapping->host;
719         int status;
720
721         /*
722          * Flush all pending writes before doing anything
723          * with locks..
724          */
725         nfs_sync_mapping(filp->f_mapping);
726
727         /* NOTE: special case
728          *      If we're signalled while cleaning up locks on process exit, we
729          *      still need to complete the unlock.
730          */
731         /* Use local locking if mounted with "-onolock" */
732         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
733                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
734         else
735                 status = do_vfs_lock(filp, fl);
736         return status;
737 }
738
739 static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
740 {
741         struct inode *inode = filp->f_mapping->host;
742         int status;
743
744         /*
745          * Flush all pending writes before doing anything
746          * with locks..
747          */
748         status = nfs_sync_mapping(filp->f_mapping);
749         if (status != 0)
750                 goto out;
751
752         /* Use local locking if mounted with "-onolock" */
753         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
754                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
755         else
756                 status = do_vfs_lock(filp, fl);
757         if (status < 0)
758                 goto out;
759         /*
760          * Make sure we clear the cache whenever we try to get the lock.
761          * This makes locking act as a cache coherency point.
762          */
763         nfs_sync_mapping(filp->f_mapping);
764         if (!nfs_have_delegation(inode, FMODE_READ))
765                 nfs_zap_caches(inode);
766 out:
767         return status;
768 }
769
770 /*
771  * Lock a (portion of) a file
772  */
773 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
774 {
775         struct inode *inode = filp->f_mapping->host;
776         int ret = -ENOLCK;
777
778         dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
779                         filp->f_path.dentry->d_parent->d_name.name,
780                         filp->f_path.dentry->d_name.name,
781                         fl->fl_type, fl->fl_flags,
782                         (long long)fl->fl_start, (long long)fl->fl_end);
783
784         nfs_inc_stats(inode, NFSIOS_VFSLOCK);
785
786         /* No mandatory locks over NFS */
787         if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
788                 goto out_err;
789
790         if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
791                 ret = NFS_PROTO(inode)->lock_check_bounds(fl);
792                 if (ret < 0)
793                         goto out_err;
794         }
795
796         if (IS_GETLK(cmd))
797                 ret = do_getlk(filp, cmd, fl);
798         else if (fl->fl_type == F_UNLCK)
799                 ret = do_unlk(filp, cmd, fl);
800         else
801                 ret = do_setlk(filp, cmd, fl);
802 out_err:
803         return ret;
804 }
805
806 /*
807  * Lock a (portion of) a file
808  */
809 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
810 {
811         dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
812                         filp->f_path.dentry->d_parent->d_name.name,
813                         filp->f_path.dentry->d_name.name,
814                         fl->fl_type, fl->fl_flags);
815
816         if (!(fl->fl_flags & FL_FLOCK))
817                 return -ENOLCK;
818
819         /* We're simulating flock() locks using posix locks on the server */
820         fl->fl_owner = (fl_owner_t)filp;
821         fl->fl_start = 0;
822         fl->fl_end = OFFSET_MAX;
823
824         if (fl->fl_type == F_UNLCK)
825                 return do_unlk(filp, cmd, fl);
826         return do_setlk(filp, cmd, fl);
827 }
828
829 /*
830  * There is no protocol support for leases, so we have no way to implement
831  * them correctly in the face of opens by other clients.
832  */
833 static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
834 {
835         dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
836                         file->f_path.dentry->d_parent->d_name.name,
837                         file->f_path.dentry->d_name.name, arg);
838
839         return -EINVAL;
840 }