xref: /openbmc/linux/fs/nfs/file.c (revision 457c89965399115e5cd8bf38f9c597293405703d)
1*457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/nfs/file.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Changes Copyright (C) 1994 by Florian La Roche
81da177e4SLinus Torvalds  *   - Do not copy data too often around in the kernel.
91da177e4SLinus Torvalds  *   - In nfs_file_read the return value of kmalloc wasn't checked.
101da177e4SLinus Torvalds  *   - Put in a better version of read look-ahead buffering. Original idea
111da177e4SLinus Torvalds  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  Total rewrite of read side for new NFS buffer cache.. Linus.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *  nfs regular file handling functions
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
20ddda8e0aSBryan Schumaker #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/fcntl.h>
251da177e4SLinus Torvalds #include <linux/stat.h>
261da177e4SLinus Torvalds #include <linux/nfs_fs.h>
271da177e4SLinus Torvalds #include <linux/nfs_mount.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/pagemap.h>
305a0e3ad6STejun Heo #include <linux/gfp.h>
31b608b283STrond Myklebust #include <linux/swap.h>
321da177e4SLinus Torvalds 
337c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "delegation.h"
3694387fb1STrond Myklebust #include "internal.h"
3791d5b470SChuck Lever #include "iostat.h"
38545db45fSDavid Howells #include "fscache.h"
39612aa983SChristoph Hellwig #include "pnfs.h"
401da177e4SLinus Torvalds 
41f4ce1299STrond Myklebust #include "nfstrace.h"
42f4ce1299STrond Myklebust 
431da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
441da177e4SLinus Torvalds 
45f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops;
4694387fb1STrond Myklebust 
471da177e4SLinus Torvalds /* Hack for future NFS swap support */
481da177e4SLinus Torvalds #ifndef IS_SWAPFILE
491da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds 
52ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags)
531da177e4SLinus Torvalds {
541da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
551da177e4SLinus Torvalds 		return -EINVAL;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	return 0;
581da177e4SLinus Torvalds }
5989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /*
621da177e4SLinus Torvalds  * Open file
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds static int
651da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	int res;
681da177e4SLinus Torvalds 
696de1472fSAl Viro 	dprintk("NFS: open file(%pD2)\n", filp);
70cc0dd2d1SChuck Lever 
71c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
721da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
731da177e4SLinus Torvalds 	if (res)
741da177e4SLinus Torvalds 		return res;
751da177e4SLinus Torvalds 
7646cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
771da177e4SLinus Torvalds 	return res;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
80ce4ef7c0SBryan Schumaker int
811da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
821da177e4SLinus Torvalds {
836de1472fSAl Viro 	dprintk("NFS: release(%pD2)\n", filp);
846da24bc9SChuck Lever 
8591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
86aff8d8dcSAnna Schumaker 	nfs_file_clear_open_context(filp);
87aff8d8dcSAnna Schumaker 	return 0;
881da177e4SLinus Torvalds }
8989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release);
901da177e4SLinus Torvalds 
91980802e3STrond Myklebust /**
92980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
93302fad7bSTrond Myklebust  * @inode: pointer to inode struct
94302fad7bSTrond Myklebust  * @filp: pointer to struct file
95980802e3STrond Myklebust  *
96980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
97980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
98980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
99980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
100980802e3STrond Myklebust  * shouldn't trust the cache).
101980802e3STrond Myklebust  */
102980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
103980802e3STrond Myklebust {
104980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
105d7cf8dd0STrond Myklebust 
106980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
107980802e3STrond Myklebust 		goto force_reval;
10861540bf6STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE))
109d7cf8dd0STrond Myklebust 		goto force_reval;
110fe51beecSTrond Myklebust 	return 0;
111980802e3STrond Myklebust force_reval:
112980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
113980802e3STrond Myklebust }
114980802e3STrond Myklebust 
115965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
116980802e3STrond Myklebust {
1176de1472fSAl Viro 	dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
1186de1472fSAl Viro 			filp, offset, whence);
119b84e06c5SChuck Lever 
12006222e49SJosef Bacik 	/*
121965c8e59SAndrew Morton 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
12206222e49SJosef Bacik 	 * the cached file length
12306222e49SJosef Bacik 	 */
124965c8e59SAndrew Morton 	if (whence != SEEK_SET && whence != SEEK_CUR) {
125980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
126d5e66348STrond Myklebust 
127980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
128980802e3STrond Myklebust 		if (retval < 0)
129980802e3STrond Myklebust 			return (loff_t)retval;
13079835a71SAndi Kleen 	}
131d5e66348STrond Myklebust 
132965c8e59SAndrew Morton 	return generic_file_llseek(filp, offset, whence);
133980802e3STrond Myklebust }
13489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek);
135980802e3STrond Myklebust 
1361da177e4SLinus Torvalds /*
1371da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1381da177e4SLinus Torvalds  */
1395445b1fbSTrond Myklebust static int
14075e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1411da177e4SLinus Torvalds {
1426de1472fSAl Viro 	struct inode	*inode = file_inode(file);
1431da177e4SLinus Torvalds 
1446de1472fSAl Viro 	dprintk("NFS: flush(%pD2)\n", file);
1451da177e4SLinus Torvalds 
146c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1471da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1481da177e4SLinus Torvalds 		return 0;
1497b159fc1STrond Myklebust 
1507fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
151aded8d7bSTrond Myklebust 	return nfs_wb_all(inode);
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
154ce4ef7c0SBryan Schumaker ssize_t
1553aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
1561da177e4SLinus Torvalds {
1576de1472fSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
1581da177e4SLinus Torvalds 	ssize_t result;
1591da177e4SLinus Torvalds 
1602ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_DIRECT)
161c8b8e32dSChristoph Hellwig 		return nfs_file_direct_read(iocb, to);
1621da177e4SLinus Torvalds 
163619d30b4SAl Viro 	dprintk("NFS: read(%pD2, %zu@%lu)\n",
1646de1472fSAl Viro 		iocb->ki_filp,
1653aa2d199SAl Viro 		iov_iter_count(to), (unsigned long) iocb->ki_pos);
1661da177e4SLinus Torvalds 
167a5864c99STrond Myklebust 	nfs_start_io_read(inode);
168a5864c99STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1694184dcf2SChuck Lever 	if (!result) {
1703aa2d199SAl Viro 		result = generic_file_read_iter(iocb, to);
1714184dcf2SChuck Lever 		if (result > 0)
1724184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1734184dcf2SChuck Lever 	}
174a5864c99STrond Myklebust 	nfs_end_io_read(inode);
1751da177e4SLinus Torvalds 	return result;
1761da177e4SLinus Torvalds }
17789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read);
1781da177e4SLinus Torvalds 
179ce4ef7c0SBryan Schumaker int
1801da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
1811da177e4SLinus Torvalds {
1826de1472fSAl Viro 	struct inode *inode = file_inode(file);
1831da177e4SLinus Torvalds 	int	status;
1841da177e4SLinus Torvalds 
1856de1472fSAl Viro 	dprintk("NFS: mmap(%pD2)\n", file);
1861da177e4SLinus Torvalds 
187e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
188e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
189e1ebfd33STrond Myklebust 	 */
190e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
19194387fb1STrond Myklebust 	if (!status) {
19294387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
193e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
19494387fb1STrond Myklebust 	}
1951da177e4SLinus Torvalds 	return status;
1961da177e4SLinus Torvalds }
19789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /*
2001da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2011da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2021da177e4SLinus Torvalds  * whether any write errors occurred for this process.
2031da177e4SLinus Torvalds  */
2044ff79bc7SChristoph Hellwig static int
205bf4b4905SNeilBrown nfs_file_fsync_commit(struct file *file, int datasync)
2061da177e4SLinus Torvalds {
207cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2086de1472fSAl Viro 	struct inode *inode = file_inode(file);
209bf4b4905SNeilBrown 	int do_resend, status;
210af7fa165STrond Myklebust 	int ret = 0;
211af7fa165STrond Myklebust 
2126de1472fSAl Viro 	dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
2131da177e4SLinus Torvalds 
21491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
21505990d1bSTrond Myklebust 	do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
216af7fa165STrond Myklebust 	status = nfs_commit_inode(inode, FLUSH_SYNC);
2176fbda89bSTrond Myklebust 	if (status == 0)
2186fbda89bSTrond Myklebust 		status = file_check_and_advance_wb_err(file);
21905990d1bSTrond Myklebust 	if (status < 0) {
220af7fa165STrond Myklebust 		ret = status;
22105990d1bSTrond Myklebust 		goto out;
22205990d1bSTrond Myklebust 	}
22305990d1bSTrond Myklebust 	do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
22405990d1bSTrond Myklebust 	if (do_resend)
22505990d1bSTrond Myklebust 		ret = -EAGAIN;
22605990d1bSTrond Myklebust out:
227a5c58892SBryan Schumaker 	return ret;
228a5c58892SBryan Schumaker }
229a5c58892SBryan Schumaker 
2304ff79bc7SChristoph Hellwig int
231a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
232a5c58892SBryan Schumaker {
233a5c58892SBryan Schumaker 	int ret;
234496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
235a5c58892SBryan Schumaker 
236f4ce1299STrond Myklebust 	trace_nfs_fsync_enter(inode);
237f4ce1299STrond Myklebust 
23805990d1bSTrond Myklebust 	do {
2396fbda89bSTrond Myklebust 		ret = file_write_and_wait_range(file, start, end);
2407b281ee0STrond Myklebust 		if (ret != 0)
24105990d1bSTrond Myklebust 			break;
242bf4b4905SNeilBrown 		ret = nfs_file_fsync_commit(file, datasync);
2434ff79bc7SChristoph Hellwig 		if (!ret)
2444ff79bc7SChristoph Hellwig 			ret = pnfs_sync_inode(inode, !!datasync);
245dcfc4f25STrond Myklebust 		/*
246dcfc4f25STrond Myklebust 		 * If nfs_file_fsync_commit detected a server reboot, then
247dcfc4f25STrond Myklebust 		 * resend all dirty pages that might have been covered by
248dcfc4f25STrond Myklebust 		 * the NFS_CONTEXT_RESEND_WRITES flag
249dcfc4f25STrond Myklebust 		 */
250dcfc4f25STrond Myklebust 		start = 0;
251dcfc4f25STrond Myklebust 		end = LLONG_MAX;
25205990d1bSTrond Myklebust 	} while (ret == -EAGAIN);
25305990d1bSTrond Myklebust 
254f4ce1299STrond Myklebust 	trace_nfs_fsync_exit(inode, ret);
255af7fa165STrond Myklebust 	return ret;
2561da177e4SLinus Torvalds }
2574ff79bc7SChristoph Hellwig EXPORT_SYMBOL_GPL(nfs_file_fsync);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds /*
26038c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
26138c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
26238c73044SPeter Staubach  * page cache.
26338c73044SPeter Staubach  *
2642cde04e9SKazuo Ito  * Some pNFS layout drivers can only read/write at a certain block
2652cde04e9SKazuo Ito  * granularity like all block devices and therefore we must perform
2662cde04e9SKazuo Ito  * read/modify/write whenever a page hasn't read yet and the data
2672cde04e9SKazuo Ito  * to be written there is not aligned to a block boundary and/or
2682cde04e9SKazuo Ito  * smaller than the block size.
2692cde04e9SKazuo Ito  *
27038c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
27138c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
27238c73044SPeter Staubach  * page must be completely written to stable storage on the server
27338c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
27438c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
27538c73044SPeter Staubach  * done.
27638c73044SPeter Staubach  *
27738c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
27838c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
27938c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
28038c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
28138c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
28238c73044SPeter Staubach  * and that the new data won't completely replace the old data in
28338c73044SPeter Staubach  * that range of the file.
28438c73044SPeter Staubach  */
2852cde04e9SKazuo Ito static bool nfs_full_page_write(struct page *page, loff_t pos, unsigned int len)
28638c73044SPeter Staubach {
28738c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
28809cbfeafSKirill A. Shutemov 	unsigned int offset = pos & (PAGE_SIZE - 1);
28938c73044SPeter Staubach 	unsigned int end = offset + len;
29038c73044SPeter Staubach 
2912cde04e9SKazuo Ito 	return !pglen || (end >= pglen && !offset);
292612aa983SChristoph Hellwig }
293612aa983SChristoph Hellwig 
2942cde04e9SKazuo Ito static bool nfs_want_read_modify_write(struct file *file, struct page *page,
2952cde04e9SKazuo Ito 			loff_t pos, unsigned int len)
2962cde04e9SKazuo Ito {
2972cde04e9SKazuo Ito 	/*
2982cde04e9SKazuo Ito 	 * Up-to-date pages, those with ongoing or full-page write
2992cde04e9SKazuo Ito 	 * don't need read/modify/write
3002cde04e9SKazuo Ito 	 */
3012cde04e9SKazuo Ito 	if (PageUptodate(page) || PagePrivate(page) ||
3022cde04e9SKazuo Ito 	    nfs_full_page_write(page, pos, len))
3032cde04e9SKazuo Ito 		return false;
3042cde04e9SKazuo Ito 
3052cde04e9SKazuo Ito 	if (pnfs_ld_read_whole_page(file->f_mapping->host))
3062cde04e9SKazuo Ito 		return true;
3072cde04e9SKazuo Ito 	/* Open for reading too? */
3082cde04e9SKazuo Ito 	if (file->f_mode & FMODE_READ)
3092cde04e9SKazuo Ito 		return true;
3102cde04e9SKazuo Ito 	return false;
31138c73044SPeter Staubach }
31238c73044SPeter Staubach 
31338c73044SPeter Staubach /*
3144899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3154899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3164899f9c8SNick Piggin  * data from user space.
3171da177e4SLinus Torvalds  *
3181da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3191da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3201da177e4SLinus Torvalds  */
3214899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3224899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3234899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3241da177e4SLinus Torvalds {
3254899f9c8SNick Piggin 	int ret;
32609cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
3274899f9c8SNick Piggin 	struct page *page;
32838c73044SPeter Staubach 	int once_thru = 0;
3294899f9c8SNick Piggin 
3301e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
3316de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
332b7eaefaaSChuck Lever 
33338c73044SPeter Staubach start:
33454566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3354899f9c8SNick Piggin 	if (!page)
3364899f9c8SNick Piggin 		return -ENOMEM;
3374899f9c8SNick Piggin 	*pagep = page;
3384899f9c8SNick Piggin 
3394899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3404899f9c8SNick Piggin 	if (ret) {
3414899f9c8SNick Piggin 		unlock_page(page);
34209cbfeafSKirill A. Shutemov 		put_page(page);
34338c73044SPeter Staubach 	} else if (!once_thru &&
34438c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
34538c73044SPeter Staubach 		once_thru = 1;
34638c73044SPeter Staubach 		ret = nfs_readpage(file, page);
34709cbfeafSKirill A. Shutemov 		put_page(page);
34838c73044SPeter Staubach 		if (!ret)
34938c73044SPeter Staubach 			goto start;
3504899f9c8SNick Piggin 	}
3514899f9c8SNick Piggin 	return ret;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3544899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3554899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3564899f9c8SNick Piggin 			struct page *page, void *fsdata)
3571da177e4SLinus Torvalds {
35809cbfeafSKirill A. Shutemov 	unsigned offset = pos & (PAGE_SIZE - 1);
359dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3604899f9c8SNick Piggin 	int status;
3611da177e4SLinus Torvalds 
3621e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
3636de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
364b7eaefaaSChuck Lever 
365efc91ed0STrond Myklebust 	/*
366efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
367efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
368efc91ed0STrond Myklebust 	 */
369efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
370efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
371c0cf3ef5SAl Viro 		unsigned end = offset + copied;
372efc91ed0STrond Myklebust 
373efc91ed0STrond Myklebust 		if (pglen == 0) {
374efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
37509cbfeafSKirill A. Shutemov 					end, PAGE_SIZE);
376efc91ed0STrond Myklebust 			SetPageUptodate(page);
377efc91ed0STrond Myklebust 		} else if (end >= pglen) {
37809cbfeafSKirill A. Shutemov 			zero_user_segment(page, end, PAGE_SIZE);
379efc91ed0STrond Myklebust 			if (offset == 0)
380efc91ed0STrond Myklebust 				SetPageUptodate(page);
381efc91ed0STrond Myklebust 		} else
38209cbfeafSKirill A. Shutemov 			zero_user_segment(page, pglen, PAGE_SIZE);
383efc91ed0STrond Myklebust 	}
384efc91ed0STrond Myklebust 
3854899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
3864899f9c8SNick Piggin 
3874899f9c8SNick Piggin 	unlock_page(page);
38809cbfeafSKirill A. Shutemov 	put_page(page);
3894899f9c8SNick Piggin 
3903d509e54SChuck Lever 	if (status < 0)
3913d509e54SChuck Lever 		return status;
3922701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
393dc24826bSAndy Adamson 
394ce52914eSScott Mayhew 	if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
395dc24826bSAndy Adamson 		status = nfs_wb_all(mapping->host);
396dc24826bSAndy Adamson 		if (status < 0)
397dc24826bSAndy Adamson 			return status;
398dc24826bSAndy Adamson 	}
399dc24826bSAndy Adamson 
4003d509e54SChuck Lever 	return copied;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4036b9b3514SDavid Howells /*
4046b9b3514SDavid Howells  * Partially or wholly invalidate a page
4056b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4066b9b3514SDavid Howells  *   page invalidation
407545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4086b9b3514SDavid Howells  * - Caller holds page lock
4096b9b3514SDavid Howells  */
410d47992f8SLukas Czerner static void nfs_invalidate_page(struct page *page, unsigned int offset,
411d47992f8SLukas Czerner 				unsigned int length)
412cd52ed35STrond Myklebust {
413d47992f8SLukas Czerner 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
414d47992f8SLukas Czerner 		 page, offset, length);
415b7eaefaaSChuck Lever 
41609cbfeafSKirill A. Shutemov 	if (offset != 0 || length < PAGE_SIZE)
4171c75950bSTrond Myklebust 		return;
418d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
419d56b4ddfSMel Gorman 	nfs_wb_page_cancel(page_file_mapping(page)->host, page);
420545db45fSDavid Howells 
421545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
422cd52ed35STrond Myklebust }
423cd52ed35STrond Myklebust 
4246b9b3514SDavid Howells /*
4256b9b3514SDavid Howells  * Attempt to release the private state associated with a page
426545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4276b9b3514SDavid Howells  * - Caller holds page lock
4286b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4296b9b3514SDavid Howells  */
430cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
431cd52ed35STrond Myklebust {
432b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
433b7eaefaaSChuck Lever 
434e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
435545db45fSDavid Howells 	if (PagePrivate(page))
436ddeff520SNikita Danilov 		return 0;
437545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
438e3db7691STrond Myklebust }
439e3db7691STrond Myklebust 
440f919b196SMel Gorman static void nfs_check_dirty_writeback(struct page *page,
441f919b196SMel Gorman 				bool *dirty, bool *writeback)
442f919b196SMel Gorman {
443f919b196SMel Gorman 	struct nfs_inode *nfsi;
444f919b196SMel Gorman 	struct address_space *mapping = page_file_mapping(page);
445f919b196SMel Gorman 
446f919b196SMel Gorman 	if (!mapping || PageSwapCache(page))
447f919b196SMel Gorman 		return;
448f919b196SMel Gorman 
449f919b196SMel Gorman 	/*
450f919b196SMel Gorman 	 * Check if an unstable page is currently being committed and
451f919b196SMel Gorman 	 * if so, have the VM treat it as if the page is under writeback
452f919b196SMel Gorman 	 * so it will not block due to pages that will shortly be freeable.
453f919b196SMel Gorman 	 */
454f919b196SMel Gorman 	nfsi = NFS_I(mapping->host);
455af7cf057STrond Myklebust 	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
456f919b196SMel Gorman 		*writeback = true;
457f919b196SMel Gorman 		return;
458f919b196SMel Gorman 	}
459f919b196SMel Gorman 
460f919b196SMel Gorman 	/*
461f919b196SMel Gorman 	 * If PagePrivate() is set, then the page is not freeable and as the
462f919b196SMel Gorman 	 * inode is not being committed, it's not going to be cleaned in the
463f919b196SMel Gorman 	 * near future so treat it as dirty
464f919b196SMel Gorman 	 */
465f919b196SMel Gorman 	if (PagePrivate(page))
466f919b196SMel Gorman 		*dirty = true;
467f919b196SMel Gorman }
468f919b196SMel Gorman 
4696b9b3514SDavid Howells /*
4706b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4716b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4726b9b3514SDavid Howells  * destroyed
473545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4746b9b3514SDavid Howells  * - Caller holds page lock
4756b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4766b9b3514SDavid Howells  */
477e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
478e3db7691STrond Myklebust {
479d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
480545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
481b7eaefaaSChuck Lever 
482b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
483b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
484b7eaefaaSChuck Lever 
485545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
486c373fff7STrond Myklebust 	return nfs_wb_page(inode, page);
487cd52ed35STrond Myklebust }
488cd52ed35STrond Myklebust 
489a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
490a564b8f0SMel Gorman 						sector_t *span)
491a564b8f0SMel Gorman {
492dad2b015SJeff Layton 	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
493dad2b015SJeff Layton 
494a564b8f0SMel Gorman 	*span = sis->pages;
495dad2b015SJeff Layton 
4963c87ef6eSJeff Layton 	return rpc_clnt_swap_activate(clnt);
497a564b8f0SMel Gorman }
498a564b8f0SMel Gorman 
499a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file)
500a564b8f0SMel Gorman {
501dad2b015SJeff Layton 	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
502dad2b015SJeff Layton 
5033c87ef6eSJeff Layton 	rpc_clnt_swap_deactivate(clnt);
504a564b8f0SMel Gorman }
505a564b8f0SMel Gorman 
506f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5071da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5081da177e4SLinus Torvalds 	.readpages = nfs_readpages,
5099cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
5101da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5111da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5124899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5134899f9c8SNick Piggin 	.write_end = nfs_write_end,
514cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
515cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5161da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
517f844cd0dSChao Yu #ifdef CONFIG_MIGRATION
518074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
519f844cd0dSChao Yu #endif
520e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
521f919b196SMel Gorman 	.is_dirty_writeback = nfs_check_dirty_writeback,
522f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
523a564b8f0SMel Gorman 	.swap_activate = nfs_swap_activate,
524a564b8f0SMel Gorman 	.swap_deactivate = nfs_swap_deactivate,
5251da177e4SLinus Torvalds };
5261da177e4SLinus Torvalds 
5276b9b3514SDavid Howells /*
5286b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5296b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5306b9b3514SDavid Howells  * shared-writable mapping
5316b9b3514SDavid Howells  */
53201a36844SSouptick Joarder static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
53394387fb1STrond Myklebust {
534c2ec175cSNick Piggin 	struct page *page = vmf->page;
53511bac800SDave Jiang 	struct file *filp = vmf->vma->vm_file;
5366de1472fSAl Viro 	struct inode *inode = file_inode(filp);
53794387fb1STrond Myklebust 	unsigned pagelen;
53801a36844SSouptick Joarder 	vm_fault_t ret = VM_FAULT_NOPAGE;
5394899f9c8SNick Piggin 	struct address_space *mapping;
54094387fb1STrond Myklebust 
5411e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
5426de1472fSAl Viro 		filp, filp->f_mapping->host->i_ino,
543b7eaefaaSChuck Lever 		(long long)page_offset(page));
544b7eaefaaSChuck Lever 
5459a773e7cSTrond Myklebust 	sb_start_pagefault(inode->i_sb);
5469a773e7cSTrond Myklebust 
547545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
5486de1472fSAl Viro 	nfs_fscache_wait_on_page_write(NFS_I(inode), page);
549545db45fSDavid Howells 
550ef070dcbSTrond Myklebust 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
551ef070dcbSTrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
552ef070dcbSTrond Myklebust 
55394387fb1STrond Myklebust 	lock_page(page);
554d56b4ddfSMel Gorman 	mapping = page_file_mapping(page);
5556de1472fSAl Viro 	if (mapping != inode->i_mapping)
5568b1f9ee5STrond Myklebust 		goto out_unlock;
5578b1f9ee5STrond Myklebust 
5582aeb98f4STrond Myklebust 	wait_on_page_writeback(page);
5592aeb98f4STrond Myklebust 
5604899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5618b1f9ee5STrond Myklebust 	if (pagelen == 0)
5628b1f9ee5STrond Myklebust 		goto out_unlock;
5638b1f9ee5STrond Myklebust 
564bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
565bc4866b6STrond Myklebust 	if (nfs_flush_incompatible(filp, page) == 0 &&
566bc4866b6STrond Myklebust 	    nfs_updatepage(filp, page, 0, pagelen) == 0)
567bc4866b6STrond Myklebust 		goto out;
5688b1f9ee5STrond Myklebust 
569bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
5708b1f9ee5STrond Myklebust out_unlock:
5714899f9c8SNick Piggin 	unlock_page(page);
572bc4866b6STrond Myklebust out:
5739a773e7cSTrond Myklebust 	sb_end_pagefault(inode->i_sb);
574bc4866b6STrond Myklebust 	return ret;
57594387fb1STrond Myklebust }
57694387fb1STrond Myklebust 
577f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
57894387fb1STrond Myklebust 	.fault = filemap_fault,
579f1820361SKirill A. Shutemov 	.map_pages = filemap_map_pages,
58094387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
58194387fb1STrond Myklebust };
58294387fb1STrond Myklebust 
5837e94d6c4STrond Myklebust static int nfs_need_check_write(struct file *filp, struct inode *inode)
5847b159fc1STrond Myklebust {
5857b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5867b159fc1STrond Myklebust 
587cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
5886fbda89bSTrond Myklebust 	if (nfs_ctx_key_to_expire(ctx, inode))
5897b159fc1STrond Myklebust 		return 1;
5907b159fc1STrond Myklebust 	return 0;
5917b159fc1STrond Myklebust }
5927b159fc1STrond Myklebust 
593edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
5941da177e4SLinus Torvalds {
5956de1472fSAl Viro 	struct file *file = iocb->ki_filp;
5966de1472fSAl Viro 	struct inode *inode = file_inode(file);
5977e381172SChuck Lever 	unsigned long written = 0;
5981da177e4SLinus Torvalds 	ssize_t result;
5991da177e4SLinus Torvalds 
6006de1472fSAl Viro 	result = nfs_key_timeout_notify(file, inode);
601dc24826bSAndy Adamson 	if (result)
602dc24826bSAndy Adamson 		return result;
603dc24826bSAndy Adamson 
60489698b24STrond Myklebust 	if (iocb->ki_flags & IOCB_DIRECT)
60565a4a1caSAl Viro 		return nfs_file_direct_write(iocb, from);
6061da177e4SLinus Torvalds 
607619d30b4SAl Viro 	dprintk("NFS: write(%pD2, %zu@%Ld)\n",
60818290650STrond Myklebust 		file, iov_iter_count(from), (long long) iocb->ki_pos);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6111da177e4SLinus Torvalds 		goto out_swapfile;
6127d52e862STrond Myklebust 	/*
6137d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6147d52e862STrond Myklebust 	 */
6152ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_APPEND) {
6166de1472fSAl Viro 		result = nfs_revalidate_file_size(inode, file);
6171da177e4SLinus Torvalds 		if (result)
6181da177e4SLinus Torvalds 			goto out;
619fe51beecSTrond Myklebust 	}
6206ba80d43SNeilBrown 	if (iocb->ki_pos > i_size_read(inode))
6216ba80d43SNeilBrown 		nfs_revalidate_mapping(inode, file->f_mapping);
6221da177e4SLinus Torvalds 
623a5864c99STrond Myklebust 	nfs_start_io_write(inode);
62418290650STrond Myklebust 	result = generic_write_checks(iocb, from);
62518290650STrond Myklebust 	if (result > 0) {
62618290650STrond Myklebust 		current->backing_dev_info = inode_to_bdi(inode);
62718290650STrond Myklebust 		result = generic_perform_write(file, from, iocb->ki_pos);
62818290650STrond Myklebust 		current->backing_dev_info = NULL;
62918290650STrond Myklebust 	}
630a5864c99STrond Myklebust 	nfs_end_io_write(inode);
63118290650STrond Myklebust 	if (result <= 0)
6321da177e4SLinus Torvalds 		goto out;
6331da177e4SLinus Torvalds 
634c49edecdSTrond Myklebust 	written = result;
63518290650STrond Myklebust 	iocb->ki_pos += written;
636e973b1a5Starangg@amazon.com 	result = generic_write_sync(iocb, written);
637e973b1a5Starangg@amazon.com 	if (result < 0)
638e973b1a5Starangg@amazon.com 		goto out;
6397e381172SChuck Lever 
6407e94d6c4STrond Myklebust 	/* Return error values */
64118290650STrond Myklebust 	if (nfs_need_check_write(file, inode)) {
642aded8d7bSTrond Myklebust 		int err = nfs_wb_all(inode);
643200baa21STrond Myklebust 		if (err < 0)
644200baa21STrond Myklebust 			result = err;
645200baa21STrond Myklebust 	}
6467e381172SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
6471da177e4SLinus Torvalds out:
6481da177e4SLinus Torvalds 	return result;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds out_swapfile:
6511da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
65218290650STrond Myklebust 	return -EBUSY;
6531da177e4SLinus Torvalds }
65489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write);
6551da177e4SLinus Torvalds 
6565eebde23SSuresh Jayaraman static int
6575eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6581da177e4SLinus Torvalds {
6591da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6601da177e4SLinus Torvalds 	int status = 0;
66121ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
6621da177e4SLinus Torvalds 
663039c4d7aSTrond Myklebust 	/* Try local locking first */
6646d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6656d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6666d34ac19SJ. Bruce Fields 		/* found a conflict */
667039c4d7aSTrond Myklebust 		goto out;
6681da177e4SLinus Torvalds 	}
66921ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
670039c4d7aSTrond Myklebust 
671011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
672039c4d7aSTrond Myklebust 		goto out_noconflict;
673039c4d7aSTrond Myklebust 
6745eebde23SSuresh Jayaraman 	if (is_local)
675039c4d7aSTrond Myklebust 		goto out_noconflict;
676039c4d7aSTrond Myklebust 
677039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
678039c4d7aSTrond Myklebust out:
6791da177e4SLinus Torvalds 	return status;
680039c4d7aSTrond Myklebust out_noconflict:
681039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
682039c4d7aSTrond Myklebust 	goto out;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6855eebde23SSuresh Jayaraman static int
6865eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6897a8203d8STrond Myklebust 	struct nfs_lock_context *l_ctx;
6901da177e4SLinus Torvalds 	int status;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	/*
6931da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
6941da177e4SLinus Torvalds 	 * with locks..
6951da177e4SLinus Torvalds 	 */
696aded8d7bSTrond Myklebust 	nfs_wb_all(inode);
6971da177e4SLinus Torvalds 
6987a8203d8STrond Myklebust 	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
6997a8203d8STrond Myklebust 	if (!IS_ERR(l_ctx)) {
700210c7c17SBenjamin Coddington 		status = nfs_iocounter_wait(l_ctx);
7017a8203d8STrond Myklebust 		nfs_put_lock_context(l_ctx);
7021da177e4SLinus Torvalds 		/*  NOTE: special case
7031da177e4SLinus Torvalds 		 * 	If we're signalled while cleaning up locks on process exit, we
7041da177e4SLinus Torvalds 		 * 	still need to complete the unlock.
7051da177e4SLinus Torvalds 		 */
706f30cb757SBenjamin Coddington 		if (status < 0 && !(fl->fl_flags & FL_CLOSE))
707f30cb757SBenjamin Coddington 			return status;
708f30cb757SBenjamin Coddington 	}
709f30cb757SBenjamin Coddington 
7105eebde23SSuresh Jayaraman 	/*
7115eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7125eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7135eebde23SSuresh Jayaraman 	 */
7145eebde23SSuresh Jayaraman 	if (!is_local)
7151da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7161da177e4SLinus Torvalds 	else
71775575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7181da177e4SLinus Torvalds 	return status;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7215eebde23SSuresh Jayaraman static int
7225eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7251da177e4SLinus Torvalds 	int status;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	/*
7281da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7291da177e4SLinus Torvalds 	 * with locks..
7301da177e4SLinus Torvalds 	 */
73129884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
73229884df0STrond Myklebust 	if (status != 0)
7331da177e4SLinus Torvalds 		goto out;
7341da177e4SLinus Torvalds 
7355eebde23SSuresh Jayaraman 	/*
7365eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7375eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7385eebde23SSuresh Jayaraman 	 */
7395eebde23SSuresh Jayaraman 	if (!is_local)
7401da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
741c4d7c402STrond Myklebust 	else
74275575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7431da177e4SLinus Torvalds 	if (status < 0)
7441da177e4SLinus Torvalds 		goto out;
7456b96724eSRicardo Labiaga 
7461da177e4SLinus Torvalds 	/*
747779eafabSNeilBrown 	 * Invalidate cache to prevent missing any changes.  If
748779eafabSNeilBrown 	 * the file is mapped, clear the page cache as well so
749779eafabSNeilBrown 	 * those mappings will be loaded.
7506b96724eSRicardo Labiaga 	 *
7511da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7521da177e4SLinus Torvalds 	 */
75329884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
754779eafabSNeilBrown 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
755442ce049SNeilBrown 		nfs_zap_caches(inode);
756779eafabSNeilBrown 		if (mapping_mapped(filp->f_mapping))
757779eafabSNeilBrown 			nfs_revalidate_mapping(inode, filp->f_mapping);
758779eafabSNeilBrown 	}
7591da177e4SLinus Torvalds out:
7601da177e4SLinus Torvalds 	return status;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds  * Lock a (portion of) a file
7651da177e4SLinus Torvalds  */
766ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7692116271aSTrond Myklebust 	int ret = -ENOLCK;
7705eebde23SSuresh Jayaraman 	int is_local = 0;
7711da177e4SLinus Torvalds 
7726de1472fSAl Viro 	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
7736de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags,
7741da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7756da24bc9SChuck Lever 
77691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
779dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
7802116271aSTrond Myklebust 		goto out_err;
7812116271aSTrond Myklebust 
7825eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
7835eebde23SSuresh Jayaraman 		is_local = 1;
7845eebde23SSuresh Jayaraman 
7852116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
7862116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
7872116271aSTrond Myklebust 		if (ret < 0)
7882116271aSTrond Myklebust 			goto out_err;
7892116271aSTrond Myklebust 	}
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
7925eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
7932116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
7945eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
7952116271aSTrond Myklebust 	else
7965eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
7972116271aSTrond Myklebust out_err:
7982116271aSTrond Myklebust 	return ret;
7991da177e4SLinus Torvalds }
80089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds /*
8031da177e4SLinus Torvalds  * Lock a (portion of) a file
8041da177e4SLinus Torvalds  */
805ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8061da177e4SLinus Torvalds {
8075eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8085eebde23SSuresh Jayaraman 	int is_local = 0;
8095eebde23SSuresh Jayaraman 
8106de1472fSAl Viro 	dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
8116de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8141da177e4SLinus Torvalds 		return -ENOLCK;
8151da177e4SLinus Torvalds 
816ad0fcd4eSJeff Layton 	/*
817ad0fcd4eSJeff Layton 	 * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
818ad0fcd4eSJeff Layton 	 * any standard. In principle we might be able to support LOCK_MAND
819ad0fcd4eSJeff Layton 	 * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
820ad0fcd4eSJeff Layton 	 * NFS code is not set up for it.
821ad0fcd4eSJeff Layton 	 */
822ad0fcd4eSJeff Layton 	if (fl->fl_type & LOCK_MAND)
823ad0fcd4eSJeff Layton 		return -EINVAL;
824ad0fcd4eSJeff Layton 
8255eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8265eebde23SSuresh Jayaraman 		is_local = 1;
8275eebde23SSuresh Jayaraman 
828fcfa4470SBenjamin Coddington 	/* We're simulating flock() locks using posix locks on the server */
829fcfa4470SBenjamin Coddington 	if (fl->fl_type == F_UNLCK)
8305eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8315eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8321da177e4SLinus Torvalds }
83389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock);
834370f6599SJ. Bruce Fields 
8350486958fSJeff Layton const struct file_operations nfs_file_operations = {
8360486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8373aa2d199SAl Viro 	.read_iter	= nfs_file_read,
838edaf4369SAl Viro 	.write_iter	= nfs_file_write,
8390486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8400486958fSJeff Layton 	.open		= nfs_file_open,
8410486958fSJeff Layton 	.flush		= nfs_file_flush,
8420486958fSJeff Layton 	.release	= nfs_file_release,
8430486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8440486958fSJeff Layton 	.lock		= nfs_lock,
8450486958fSJeff Layton 	.flock		= nfs_flock,
84682c156f8SAl Viro 	.splice_read	= generic_file_splice_read,
8474da54c21SAl Viro 	.splice_write	= iter_file_splice_write,
8480486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8491c994a09SJeff Layton 	.setlease	= simple_nosetlease,
8500486958fSJeff Layton };
851ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations);
852