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