4 * Copyright (C) 1992 Rick Sladkey
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.
12 * Expire cache on write to a file by Wai S Kok (Oct 1994).
14 * Total rewrite of read side for new NFS buffer cache.. Linus.
16 * nfs regular file handling functions
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>
28 #include <linux/pagemap.h>
29 #include <linux/aio.h>
30 #include <linux/gfp.h>
31 #include <linux/swap.h>
33 #include <asm/uaccess.h>
35 #include "delegation.h"
42 #define NFSDBG_FACILITY NFSDBG_FILE
44 static const struct vm_operations_struct nfs_file_vm_ops;
46 /* Hack for future NFS swap support */
48 # define IS_SWAPFILE(inode) (0)
51 int nfs_check_flags(int flags)
53 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
58 EXPORT_SYMBOL_GPL(nfs_check_flags);
64 nfs_file_open(struct inode *inode, struct file *filp)
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);
72 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
73 res = nfs_check_flags(filp->f_flags);
77 res = nfs_open(inode, filp);
82 nfs_file_release(struct inode *inode, struct file *filp)
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);
88 nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
89 return nfs_release(inode, filp);
91 EXPORT_SYMBOL_GPL(nfs_file_release);
94 * nfs_revalidate_size - Revalidate the file size
95 * @inode - pointer to inode struct
96 * @file - pointer to struct file
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).
104 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
106 struct nfs_server *server = NFS_SERVER(inode);
107 struct nfs_inode *nfsi = NFS_I(inode);
109 if (nfs_have_delegated_attributes(inode))
112 if (filp->f_flags & O_DIRECT)
114 if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
116 if (nfs_attribute_timeout(inode))
121 return __nfs_revalidate_inode(server, inode);
124 loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
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,
132 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
133 * the cached file length
135 if (whence != SEEK_SET && whence != SEEK_CUR) {
136 struct inode *inode = filp->f_mapping->host;
138 int retval = nfs_revalidate_file_size(inode, filp);
140 return (loff_t)retval;
143 return generic_file_llseek(filp, offset, whence);
145 EXPORT_SYMBOL_GPL(nfs_file_llseek);
148 * Flush all dirty pages, and check for write errors.
151 nfs_file_flush(struct file *file, fl_owner_t id)
153 struct dentry *dentry = file->f_path.dentry;
154 struct inode *inode = dentry->d_inode;
156 dprintk("NFS: flush(%s/%s)\n",
157 dentry->d_parent->d_name.name,
158 dentry->d_name.name);
160 nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
161 if ((file->f_mode & FMODE_WRITE) == 0)
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).
168 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
169 return filemap_fdatawrite(file->f_mapping);
171 /* Flush writes to the server and return any errors */
172 return vfs_fsync(file, 0);
174 EXPORT_SYMBOL_GPL(nfs_file_flush);
177 nfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
179 struct dentry * dentry = iocb->ki_filp->f_path.dentry;
180 struct inode * inode = dentry->d_inode;
183 if (iocb->ki_filp->f_flags & O_DIRECT)
184 return nfs_file_direct_read(iocb, iter, pos);
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);
190 result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
192 result = generic_file_read_iter(iocb, iter, pos);
194 nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
198 EXPORT_SYMBOL_GPL(nfs_file_read_iter);
201 nfs_file_splice_read(struct file *filp, loff_t *ppos,
202 struct pipe_inode_info *pipe, size_t count,
205 struct dentry *dentry = filp->f_path.dentry;
206 struct inode *inode = dentry->d_inode;
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);
213 res = nfs_revalidate_mapping(inode, filp->f_mapping);
215 res = generic_file_splice_read(filp, ppos, pipe, count, flags);
217 nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
221 EXPORT_SYMBOL_GPL(nfs_file_splice_read);
224 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
226 struct dentry *dentry = file->f_path.dentry;
227 struct inode *inode = dentry->d_inode;
230 dprintk("NFS: mmap(%s/%s)\n",
231 dentry->d_parent->d_name.name, dentry->d_name.name);
233 /* Note: generic_file_mmap() returns ENOSYS on nommu systems
234 * so we call that before revalidating the mapping
236 status = generic_file_mmap(file, vma);
238 vma->vm_ops = &nfs_file_vm_ops;
239 status = nfs_revalidate_mapping(inode, file->f_mapping);
243 EXPORT_SYMBOL_GPL(nfs_file_mmap);
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.
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.
258 nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
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;
266 dprintk("NFS: fsync file(%s/%s) datasync %d\n",
267 dentry->d_parent->d_name.name, dentry->d_name.name,
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);
276 ret = xchg(&ctx->error, 0);
284 do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
290 EXPORT_SYMBOL_GPL(nfs_file_fsync_commit);
293 nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
296 struct inode *inode = file_inode(file);
298 trace_nfs_fsync_enter(inode);
301 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
304 mutex_lock(&inode->i_mutex);
305 ret = nfs_file_fsync_commit(file, start, end, datasync);
306 mutex_unlock(&inode->i_mutex);
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
314 } while (ret == -EAGAIN);
316 trace_nfs_fsync_exit(inode, ret);
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
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
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.
340 static int nfs_want_read_modify_write(struct file *file, struct page *page,
341 loff_t pos, unsigned len)
343 unsigned int pglen = nfs_page_length(page);
344 unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
345 unsigned int end = offset + len;
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? */
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.
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.
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)
369 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
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);
380 * Prevent starvation issues if someone is doing a consistency
383 ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
384 nfs_wait_bit_killable, TASK_KILLABLE);
388 page = grab_cache_page_write_begin(mapping, index, flags);
393 ret = nfs_flush_incompatible(file, page);
396 page_cache_release(page);
397 } else if (!once_thru &&
398 nfs_want_read_modify_write(file, page, pos, len)) {
400 ret = nfs_readpage(file, page);
401 page_cache_release(page);
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)
412 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
413 struct nfs_open_context *ctx = nfs_file_open_context(file);
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);
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.
425 if (!PageUptodate(page)) {
426 unsigned pglen = nfs_page_length(page);
427 unsigned end = offset + len;
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);
436 SetPageUptodate(page);
438 zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
441 status = nfs_updatepage(file, page, offset, copied);
444 page_cache_release(page);
448 NFS_I(mapping->host)->write_io += copied;
450 if (nfs_ctx_key_to_expire(ctx)) {
451 status = nfs_wb_all(mapping->host);
460 * Partially or wholly invalidate a page
461 * - Release the private state associated with a page if undergoing complete
463 * - Called if either PG_private or PG_fscache is set on the page
464 * - Caller holds page lock
466 static void nfs_invalidate_page(struct page *page, unsigned int offset,
469 dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
470 page, offset, length);
472 if (offset != 0 || length < PAGE_CACHE_SIZE)
474 /* Cancel any unstarted writes on this page */
475 nfs_wb_page_cancel(page_file_mapping(page)->host, page);
477 nfs_fscache_invalidate_page(page, page->mapping->host);
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)
486 static int nfs_release_page(struct page *page, gfp_t gfp)
488 struct address_space *mapping = page->mapping;
490 dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
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.
495 if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL &&
496 !(current->flags & PF_FSTRANS)) {
497 int how = FLUSH_SYNC;
499 /* Don't let kswapd deadlock waiting for OOM RPC calls */
500 if (current_is_kswapd())
502 nfs_commit_inode(mapping->host, how);
504 /* If PagePrivate() is set, then the page is not freeable */
505 if (PagePrivate(page))
507 return nfs_fscache_release_page(page, gfp);
510 static void nfs_check_dirty_writeback(struct page *page,
511 bool *dirty, bool *writeback)
513 struct nfs_inode *nfsi;
514 struct address_space *mapping = page_file_mapping(page);
516 if (!mapping || PageSwapCache(page))
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.
524 nfsi = NFS_I(mapping->host);
525 if (test_bit(NFS_INO_COMMIT, &nfsi->flags)) {
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
535 if (PagePrivate(page))
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
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
547 static int nfs_launder_page(struct page *page)
549 struct inode *inode = page_file_mapping(page)->host;
550 struct nfs_inode *nfsi = NFS_I(inode);
552 dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
553 inode->i_ino, (long long)page_offset(page));
555 nfs_fscache_wait_on_page_write(nfsi, page);
556 return nfs_wb_page(inode, page);
559 #ifdef CONFIG_NFS_SWAP
560 static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
564 return xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 1);
567 static void nfs_swap_deactivate(struct file *file)
569 xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 0);
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,
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
599 static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
601 struct page *page = vmf->page;
602 struct file *filp = vma->vm_file;
603 struct dentry *dentry = filp->f_path.dentry;
605 int ret = VM_FAULT_NOPAGE;
606 struct address_space *mapping;
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));
613 /* make sure the cache has finished storing the page */
614 nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
617 mapping = page_file_mapping(page);
618 if (mapping != dentry->d_inode->i_mapping)
621 wait_on_page_writeback(page);
623 pagelen = nfs_page_length(page);
627 ret = VM_FAULT_LOCKED;
628 if (nfs_flush_incompatible(filp, page) == 0 &&
629 nfs_updatepage(filp, page, 0, pagelen) == 0)
632 ret = VM_FAULT_SIGBUS;
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,
645 static int nfs_need_sync_write(struct file *filp, struct inode *inode)
647 struct nfs_open_context *ctx;
649 if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
651 ctx = nfs_file_open_context(filp);
652 if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
653 nfs_ctx_key_to_expire(ctx))
658 ssize_t nfs_file_write_iter(struct kiocb *iocb, struct iov_iter *iter,
661 struct dentry * dentry = iocb->ki_filp->f_path.dentry;
662 struct inode * inode = dentry->d_inode;
663 unsigned long written = 0;
665 size_t count = iov_iter_count(iter);
667 result = nfs_key_timeout_notify(iocb->ki_filp, inode);
671 if (iocb->ki_filp->f_flags & O_DIRECT)
672 return nfs_file_direct_write(iocb, iter, pos);
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);
679 if (IS_SWAPFILE(inode))
682 * O_APPEND implies that we must revalidate the file length.
684 if (iocb->ki_filp->f_flags & O_APPEND) {
685 result = nfs_revalidate_file_size(inode, iocb->ki_filp);
694 result = generic_file_write_iter(iocb, iter, pos);
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);
705 nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
710 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
713 EXPORT_SYMBOL_GPL(nfs_file_write_iter);
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)
719 struct dentry *dentry = filp->f_path.dentry;
720 struct inode *inode = dentry->d_inode;
721 unsigned long written = 0;
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);
729 * The combination of splice and an O_APPEND destination is disallowed.
732 ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
736 if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
737 int err = vfs_fsync(filp, 0);
742 nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
745 EXPORT_SYMBOL_GPL(nfs_file_splice_write);
748 do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
750 struct inode *inode = filp->f_mapping->host;
752 unsigned int saved_type = fl->fl_type;
754 /* Try local locking first */
755 posix_test_lock(filp, fl);
756 if (fl->fl_type != F_UNLCK) {
757 /* found a conflict */
760 fl->fl_type = saved_type;
762 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
768 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
772 fl->fl_type = F_UNLCK;
776 static int do_vfs_lock(struct file *file, struct file_lock *fl)
779 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
781 res = posix_lock_file_wait(file, fl);
784 res = flock_lock_file_wait(file, fl);
793 do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
795 struct inode *inode = filp->f_mapping->host;
796 struct nfs_lock_context *l_ctx;
800 * Flush all pending writes before doing anything
803 nfs_sync_mapping(filp->f_mapping);
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);
813 /* NOTE: special case
814 * If we're signalled while cleaning up locks on process exit, we
815 * still need to complete the unlock.
818 * Use local locking if mounted with "-onolock" or with appropriate
822 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
824 status = do_vfs_lock(filp, fl);
829 is_time_granular(struct timespec *ts) {
830 return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
834 do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
836 struct inode *inode = filp->f_mapping->host;
840 * Flush all pending writes before doing anything
843 status = nfs_sync_mapping(filp->f_mapping);
848 * Use local locking if mounted with "-onolock" or with appropriate
852 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
854 status = do_vfs_lock(filp, fl);
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.
863 * This makes locking act as a cache coherency point.
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);
870 nfs_zap_caches(inode);
877 * Lock a (portion of) a file
879 int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
881 struct inode *inode = filp->f_mapping->host;
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);
891 nfs_inc_stats(inode, NFSIOS_VFSLOCK);
893 /* No mandatory locks over NFS */
894 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
897 if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
900 if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
901 ret = NFS_PROTO(inode)->lock_check_bounds(fl);
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);
911 ret = do_setlk(filp, cmd, fl, is_local);
915 EXPORT_SYMBOL_GPL(nfs_lock);
918 * Lock a (portion of) a file
920 int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
922 struct inode *inode = filp->f_mapping->host;
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);
930 if (!(fl->fl_flags & FL_FLOCK))
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.
939 if (fl->fl_type & LOCK_MAND)
942 if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
945 /* We're simulating flock() locks using posix locks on the server */
946 fl->fl_owner = (fl_owner_t)filp;
948 fl->fl_end = OFFSET_MAX;
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);
954 EXPORT_SYMBOL_GPL(nfs_flock);
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.
960 int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
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);
967 EXPORT_SYMBOL_GPL(nfs_setlease);
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,
982 .splice_read = nfs_file_splice_read,
983 .splice_write = nfs_file_splice_write,
984 .check_flags = nfs_check_flags,
985 .setlease = nfs_setlease,
987 EXPORT_SYMBOL_GPL(nfs_file_operations);