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