xref: /openbmc/linux/fs/nfs/file.c (revision e6005436f6cc9ed13288f936903f0151e5543485)
1457c8996SThomas 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 
47ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
501da177e4SLinus Torvalds 		return -EINVAL;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	return 0;
531da177e4SLinus Torvalds }
5489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags);
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * Open file
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds static int
601da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	int res;
631da177e4SLinus Torvalds 
646de1472fSAl Viro 	dprintk("NFS: open file(%pD2)\n", filp);
65cc0dd2d1SChuck Lever 
66c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
671da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
681da177e4SLinus Torvalds 	if (res)
691da177e4SLinus Torvalds 		return res;
701da177e4SLinus Torvalds 
7146cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
721da177e4SLinus Torvalds 	return res;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
75ce4ef7c0SBryan Schumaker int
761da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
771da177e4SLinus Torvalds {
786de1472fSAl Viro 	dprintk("NFS: release(%pD2)\n", filp);
796da24bc9SChuck Lever 
8091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
81aff8d8dcSAnna Schumaker 	nfs_file_clear_open_context(filp);
82a6b5a28eSDave Wysochanski 	nfs_fscache_release_file(inode, filp);
83aff8d8dcSAnna Schumaker 	return 0;
841da177e4SLinus Torvalds }
8589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release);
861da177e4SLinus Torvalds 
87980802e3STrond Myklebust /**
8837eaeed1STrond Myklebust  * nfs_revalidate_file_size - Revalidate the file size
89302fad7bSTrond Myklebust  * @inode: pointer to inode struct
90302fad7bSTrond Myklebust  * @filp: pointer to struct file
91980802e3STrond Myklebust  *
92980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
93980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
94980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
95980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
96980802e3STrond Myklebust  * shouldn't trust the cache).
97980802e3STrond Myklebust  */
98980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
99980802e3STrond Myklebust {
100980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
101d7cf8dd0STrond Myklebust 
102980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
103980802e3STrond Myklebust 		goto force_reval;
10413c0b082STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE))
105d7cf8dd0STrond Myklebust 		goto force_reval;
106fe51beecSTrond Myklebust 	return 0;
107980802e3STrond Myklebust force_reval:
108980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
109980802e3STrond Myklebust }
110980802e3STrond Myklebust 
111965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
112980802e3STrond Myklebust {
1136de1472fSAl Viro 	dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
1146de1472fSAl Viro 			filp, offset, whence);
115b84e06c5SChuck Lever 
11606222e49SJosef Bacik 	/*
117965c8e59SAndrew Morton 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
11806222e49SJosef Bacik 	 * the cached file length
11906222e49SJosef Bacik 	 */
120965c8e59SAndrew Morton 	if (whence != SEEK_SET && whence != SEEK_CUR) {
121980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
122d5e66348STrond Myklebust 
123980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
124980802e3STrond Myklebust 		if (retval < 0)
125980802e3STrond Myklebust 			return (loff_t)retval;
12679835a71SAndi Kleen 	}
127d5e66348STrond Myklebust 
128965c8e59SAndrew Morton 	return generic_file_llseek(filp, offset, whence);
129980802e3STrond Myklebust }
13089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek);
131980802e3STrond Myklebust 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1341da177e4SLinus Torvalds  */
1355445b1fbSTrond Myklebust static int
13675e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1371da177e4SLinus Torvalds {
1386de1472fSAl Viro 	struct inode	*inode = file_inode(file);
13967dd23f9SScott Mayhew 	errseq_t since;
1401da177e4SLinus Torvalds 
1416de1472fSAl Viro 	dprintk("NFS: flush(%pD2)\n", file);
1421da177e4SLinus Torvalds 
143c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1441da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1451da177e4SLinus Torvalds 		return 0;
1467b159fc1STrond Myklebust 
1477fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
14867dd23f9SScott Mayhew 	since = filemap_sample_wb_err(file->f_mapping);
14967dd23f9SScott Mayhew 	nfs_wb_all(inode);
15067dd23f9SScott Mayhew 	return filemap_check_wb_err(file->f_mapping, since);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153ce4ef7c0SBryan Schumaker ssize_t
1543aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
1551da177e4SLinus Torvalds {
1566de1472fSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
1571da177e4SLinus Torvalds 	ssize_t result;
1581da177e4SLinus Torvalds 
1592ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_DIRECT)
16064158668SNeilBrown 		return nfs_file_direct_read(iocb, to, false);
1611da177e4SLinus Torvalds 
162619d30b4SAl Viro 	dprintk("NFS: read(%pD2, %zu@%lu)\n",
1636de1472fSAl Viro 		iocb->ki_filp,
1643aa2d199SAl Viro 		iov_iter_count(to), (unsigned long) iocb->ki_pos);
1651da177e4SLinus Torvalds 
166a5864c99STrond Myklebust 	nfs_start_io_read(inode);
167a5864c99STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1684184dcf2SChuck Lever 	if (!result) {
1693aa2d199SAl Viro 		result = generic_file_read_iter(iocb, to);
1704184dcf2SChuck Lever 		if (result > 0)
1714184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1724184dcf2SChuck Lever 	}
173a5864c99STrond Myklebust 	nfs_end_io_read(inode);
1741da177e4SLinus Torvalds 	return result;
1751da177e4SLinus Torvalds }
17689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read);
1771da177e4SLinus Torvalds 
178ce4ef7c0SBryan Schumaker int
1791da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
1801da177e4SLinus Torvalds {
1816de1472fSAl Viro 	struct inode *inode = file_inode(file);
1821da177e4SLinus Torvalds 	int	status;
1831da177e4SLinus Torvalds 
1846de1472fSAl Viro 	dprintk("NFS: mmap(%pD2)\n", file);
1851da177e4SLinus Torvalds 
186e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
187e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
188e1ebfd33STrond Myklebust 	 */
189e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
19094387fb1STrond Myklebust 	if (!status) {
19194387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
192e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
19394387fb1STrond Myklebust 	}
1941da177e4SLinus Torvalds 	return status;
1951da177e4SLinus Torvalds }
19689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2001da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2011da177e4SLinus Torvalds  * whether any write errors occurred for this process.
2021da177e4SLinus Torvalds  */
2034ff79bc7SChristoph Hellwig static int
204bf4b4905SNeilBrown nfs_file_fsync_commit(struct file *file, int datasync)
2051da177e4SLinus Torvalds {
2066de1472fSAl Viro 	struct inode *inode = file_inode(file);
2079641d9bcSTrond Myklebust 	int ret, ret2;
208af7fa165STrond Myklebust 
2096de1472fSAl Viro 	dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
2101da177e4SLinus Torvalds 
21191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
2122197e9b0STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
2139641d9bcSTrond Myklebust 	ret2 = file_check_and_advance_wb_err(file);
2149641d9bcSTrond Myklebust 	if (ret2 < 0)
2159641d9bcSTrond Myklebust 		return ret2;
216a5c58892SBryan Schumaker 	return ret;
217a5c58892SBryan Schumaker }
218a5c58892SBryan Schumaker 
2194ff79bc7SChristoph Hellwig int
220a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
221a5c58892SBryan Schumaker {
2222197e9b0STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
223496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2242197e9b0STrond Myklebust 	int ret;
225a5c58892SBryan Schumaker 
226f4ce1299STrond Myklebust 	trace_nfs_fsync_enter(inode);
227f4ce1299STrond Myklebust 
2282197e9b0STrond Myklebust 	for (;;) {
2296fbda89bSTrond Myklebust 		ret = file_write_and_wait_range(file, start, end);
2307b281ee0STrond Myklebust 		if (ret != 0)
23105990d1bSTrond Myklebust 			break;
232bf4b4905SNeilBrown 		ret = nfs_file_fsync_commit(file, datasync);
2332197e9b0STrond Myklebust 		if (ret != 0)
2342197e9b0STrond Myklebust 			break;
2354ff79bc7SChristoph Hellwig 		ret = pnfs_sync_inode(inode, !!datasync);
2362197e9b0STrond Myklebust 		if (ret != 0)
2372197e9b0STrond Myklebust 			break;
2382197e9b0STrond Myklebust 		if (!test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags))
2392197e9b0STrond Myklebust 			break;
240dcfc4f25STrond Myklebust 		/*
241dcfc4f25STrond Myklebust 		 * If nfs_file_fsync_commit detected a server reboot, then
242dcfc4f25STrond Myklebust 		 * resend all dirty pages that might have been covered by
243dcfc4f25STrond Myklebust 		 * the NFS_CONTEXT_RESEND_WRITES flag
244dcfc4f25STrond Myklebust 		 */
245dcfc4f25STrond Myklebust 		start = 0;
246dcfc4f25STrond Myklebust 		end = LLONG_MAX;
2472197e9b0STrond Myklebust 	}
24805990d1bSTrond Myklebust 
249f4ce1299STrond Myklebust 	trace_nfs_fsync_exit(inode, ret);
250af7fa165STrond Myklebust 	return ret;
2511da177e4SLinus Torvalds }
2524ff79bc7SChristoph Hellwig EXPORT_SYMBOL_GPL(nfs_file_fsync);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
25538c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
25638c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
25738c73044SPeter Staubach  * page cache.
25838c73044SPeter Staubach  *
2592cde04e9SKazuo Ito  * Some pNFS layout drivers can only read/write at a certain block
2602cde04e9SKazuo Ito  * granularity like all block devices and therefore we must perform
2612cde04e9SKazuo Ito  * read/modify/write whenever a page hasn't read yet and the data
2622cde04e9SKazuo Ito  * to be written there is not aligned to a block boundary and/or
2632cde04e9SKazuo Ito  * smaller than the block size.
2642cde04e9SKazuo Ito  *
26538c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
26638c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
26738c73044SPeter Staubach  * page must be completely written to stable storage on the server
26838c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
26938c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
27038c73044SPeter Staubach  * done.
27138c73044SPeter Staubach  *
27238c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
27338c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
27438c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
27538c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
27638c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
27738c73044SPeter Staubach  * and that the new data won't completely replace the old data in
27838c73044SPeter Staubach  * that range of the file.
27938c73044SPeter Staubach  */
2802cde04e9SKazuo Ito static bool nfs_full_page_write(struct page *page, loff_t pos, unsigned int len)
28138c73044SPeter Staubach {
28238c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
28309cbfeafSKirill A. Shutemov 	unsigned int offset = pos & (PAGE_SIZE - 1);
28438c73044SPeter Staubach 	unsigned int end = offset + len;
28538c73044SPeter Staubach 
2862cde04e9SKazuo Ito 	return !pglen || (end >= pglen && !offset);
287612aa983SChristoph Hellwig }
288612aa983SChristoph Hellwig 
2892cde04e9SKazuo Ito static bool nfs_want_read_modify_write(struct file *file, struct page *page,
2902cde04e9SKazuo Ito 			loff_t pos, unsigned int len)
2912cde04e9SKazuo Ito {
2922cde04e9SKazuo Ito 	/*
2932cde04e9SKazuo Ito 	 * Up-to-date pages, those with ongoing or full-page write
2942cde04e9SKazuo Ito 	 * don't need read/modify/write
2952cde04e9SKazuo Ito 	 */
2962cde04e9SKazuo Ito 	if (PageUptodate(page) || PagePrivate(page) ||
2972cde04e9SKazuo Ito 	    nfs_full_page_write(page, pos, len))
2982cde04e9SKazuo Ito 		return false;
2992cde04e9SKazuo Ito 
3002cde04e9SKazuo Ito 	if (pnfs_ld_read_whole_page(file->f_mapping->host))
3012cde04e9SKazuo Ito 		return true;
3022cde04e9SKazuo Ito 	/* Open for reading too? */
3032cde04e9SKazuo Ito 	if (file->f_mode & FMODE_READ)
3042cde04e9SKazuo Ito 		return true;
3052cde04e9SKazuo Ito 	return false;
30638c73044SPeter Staubach }
30738c73044SPeter Staubach 
30838c73044SPeter Staubach /*
3094899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3104899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3114899f9c8SNick Piggin  * data from user space.
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3141da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3151da177e4SLinus Torvalds  */
3164899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3174899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3184899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3191da177e4SLinus Torvalds {
3204899f9c8SNick Piggin 	int ret;
32109cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
3224899f9c8SNick Piggin 	struct page *page;
32338c73044SPeter Staubach 	int once_thru = 0;
3244899f9c8SNick Piggin 
3251e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
3266de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
327b7eaefaaSChuck Lever 
32838c73044SPeter Staubach start:
32954566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3304899f9c8SNick Piggin 	if (!page)
3314899f9c8SNick Piggin 		return -ENOMEM;
3324899f9c8SNick Piggin 	*pagep = page;
3334899f9c8SNick Piggin 
3344899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3354899f9c8SNick Piggin 	if (ret) {
3364899f9c8SNick Piggin 		unlock_page(page);
33709cbfeafSKirill A. Shutemov 		put_page(page);
33838c73044SPeter Staubach 	} else if (!once_thru &&
33938c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
34038c73044SPeter Staubach 		once_thru = 1;
34138c73044SPeter Staubach 		ret = nfs_readpage(file, page);
34209cbfeafSKirill A. Shutemov 		put_page(page);
34338c73044SPeter Staubach 		if (!ret)
34438c73044SPeter Staubach 			goto start;
3454899f9c8SNick Piggin 	}
3464899f9c8SNick Piggin 	return ret;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3494899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3504899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3514899f9c8SNick Piggin 			struct page *page, void *fsdata)
3521da177e4SLinus Torvalds {
35309cbfeafSKirill A. Shutemov 	unsigned offset = pos & (PAGE_SIZE - 1);
354dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3554899f9c8SNick Piggin 	int status;
3561da177e4SLinus Torvalds 
3571e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
3586de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
359b7eaefaaSChuck Lever 
360efc91ed0STrond Myklebust 	/*
361efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
362efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
363efc91ed0STrond Myklebust 	 */
364efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
365efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
366c0cf3ef5SAl Viro 		unsigned end = offset + copied;
367efc91ed0STrond Myklebust 
368efc91ed0STrond Myklebust 		if (pglen == 0) {
369efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
37009cbfeafSKirill A. Shutemov 					end, PAGE_SIZE);
371efc91ed0STrond Myklebust 			SetPageUptodate(page);
372efc91ed0STrond Myklebust 		} else if (end >= pglen) {
37309cbfeafSKirill A. Shutemov 			zero_user_segment(page, end, PAGE_SIZE);
374efc91ed0STrond Myklebust 			if (offset == 0)
375efc91ed0STrond Myklebust 				SetPageUptodate(page);
376efc91ed0STrond Myklebust 		} else
37709cbfeafSKirill A. Shutemov 			zero_user_segment(page, pglen, PAGE_SIZE);
378efc91ed0STrond Myklebust 	}
379efc91ed0STrond Myklebust 
3804899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
3814899f9c8SNick Piggin 
3824899f9c8SNick Piggin 	unlock_page(page);
38309cbfeafSKirill A. Shutemov 	put_page(page);
3844899f9c8SNick Piggin 
3853d509e54SChuck Lever 	if (status < 0)
3863d509e54SChuck Lever 		return status;
3872701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
388dc24826bSAndy Adamson 
389ce52914eSScott Mayhew 	if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
390dc24826bSAndy Adamson 		status = nfs_wb_all(mapping->host);
391dc24826bSAndy Adamson 		if (status < 0)
392dc24826bSAndy Adamson 			return status;
393dc24826bSAndy Adamson 	}
394dc24826bSAndy Adamson 
3953d509e54SChuck Lever 	return copied;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3986b9b3514SDavid Howells /*
3996b9b3514SDavid Howells  * Partially or wholly invalidate a page
4006b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4016b9b3514SDavid Howells  *   page invalidation
402545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4036b9b3514SDavid Howells  * - Caller holds page lock
4046b9b3514SDavid Howells  */
4056d740c76SMatthew Wilcox (Oracle) static void nfs_invalidate_folio(struct folio *folio, size_t offset,
4066d740c76SMatthew Wilcox (Oracle) 				size_t length)
407cd52ed35STrond Myklebust {
4086d740c76SMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n",
4096d740c76SMatthew Wilcox (Oracle) 		 folio->index, offset, length);
410b7eaefaaSChuck Lever 
4116d740c76SMatthew Wilcox (Oracle) 	if (offset != 0 || length < folio_size(folio))
4121c75950bSTrond Myklebust 		return;
413d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
4146d740c76SMatthew Wilcox (Oracle) 	nfs_wb_folio_cancel(folio->mapping->host, folio);
4156d740c76SMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
416cd52ed35STrond Myklebust }
417cd52ed35STrond Myklebust 
4186b9b3514SDavid Howells /*
4196b9b3514SDavid Howells  * Attempt to release the private state associated with a page
420545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4216b9b3514SDavid Howells  * - Caller holds page lock
4226b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4236b9b3514SDavid Howells  */
424cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
425cd52ed35STrond Myklebust {
426b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
427b7eaefaaSChuck Lever 
428e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
429545db45fSDavid Howells 	if (PagePrivate(page))
430ddeff520SNikita Danilov 		return 0;
431545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
432e3db7691STrond Myklebust }
433e3db7691STrond Myklebust 
434f919b196SMel Gorman static void nfs_check_dirty_writeback(struct page *page,
435f919b196SMel Gorman 				bool *dirty, bool *writeback)
436f919b196SMel Gorman {
437f919b196SMel Gorman 	struct nfs_inode *nfsi;
438f919b196SMel Gorman 	struct address_space *mapping = page_file_mapping(page);
439f919b196SMel Gorman 
440f919b196SMel Gorman 	if (!mapping || PageSwapCache(page))
441f919b196SMel Gorman 		return;
442f919b196SMel Gorman 
443f919b196SMel Gorman 	/*
444f919b196SMel Gorman 	 * Check if an unstable page is currently being committed and
445f919b196SMel Gorman 	 * if so, have the VM treat it as if the page is under writeback
446f919b196SMel Gorman 	 * so it will not block due to pages that will shortly be freeable.
447f919b196SMel Gorman 	 */
448f919b196SMel Gorman 	nfsi = NFS_I(mapping->host);
449af7cf057STrond Myklebust 	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
450f919b196SMel Gorman 		*writeback = true;
451f919b196SMel Gorman 		return;
452f919b196SMel Gorman 	}
453f919b196SMel Gorman 
454f919b196SMel Gorman 	/*
455f919b196SMel Gorman 	 * If PagePrivate() is set, then the page is not freeable and as the
456f919b196SMel Gorman 	 * inode is not being committed, it's not going to be cleaned in the
457f919b196SMel Gorman 	 * near future so treat it as dirty
458f919b196SMel Gorman 	 */
459f919b196SMel Gorman 	if (PagePrivate(page))
460f919b196SMel Gorman 		*dirty = true;
461f919b196SMel Gorman }
462f919b196SMel Gorman 
4636b9b3514SDavid Howells /*
4646b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4656b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4666b9b3514SDavid Howells  * destroyed
467545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4686b9b3514SDavid Howells  * - Caller holds page lock
4696b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4706b9b3514SDavid Howells  */
47115a30ab2SMatthew Wilcox (Oracle) static int nfs_launder_folio(struct folio *folio)
472e3db7691STrond Myklebust {
47315a30ab2SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
474b7eaefaaSChuck Lever 
47515a30ab2SMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: launder_folio(%ld, %llu)\n",
47615a30ab2SMatthew Wilcox (Oracle) 		inode->i_ino, folio_pos(folio));
477b7eaefaaSChuck Lever 
47815a30ab2SMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
47915a30ab2SMatthew Wilcox (Oracle) 	return nfs_wb_page(inode, &folio->page);
480cd52ed35STrond Myklebust }
481cd52ed35STrond Myklebust 
482a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
483a564b8f0SMel Gorman 						sector_t *span)
484a564b8f0SMel Gorman {
485bd89bc67SMurphy Zhou 	unsigned long blocks;
486bd89bc67SMurphy Zhou 	long long isize;
4874dc73c67SNeilBrown 	struct inode *inode = file_inode(file);
4884dc73c67SNeilBrown 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4894dc73c67SNeilBrown 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
490bd89bc67SMurphy Zhou 
491bd89bc67SMurphy Zhou 	spin_lock(&inode->i_lock);
492bd89bc67SMurphy Zhou 	blocks = inode->i_blocks;
493bd89bc67SMurphy Zhou 	isize = inode->i_size;
494bd89bc67SMurphy Zhou 	spin_unlock(&inode->i_lock);
495bd89bc67SMurphy Zhou 	if (blocks*512 < isize) {
496bd89bc67SMurphy Zhou 		pr_warn("swap activate: swapfile has holes\n");
497bd89bc67SMurphy Zhou 		return -EINVAL;
498bd89bc67SMurphy Zhou 	}
499dad2b015SJeff Layton 
500a564b8f0SMel Gorman 	*span = sis->pages;
501dad2b015SJeff Layton 
5024dc73c67SNeilBrown 
5034dc73c67SNeilBrown 	if (cl->rpc_ops->enable_swap)
5044dc73c67SNeilBrown 		cl->rpc_ops->enable_swap(inode);
5054dc73c67SNeilBrown 
5063c87ef6eSJeff Layton 	return rpc_clnt_swap_activate(clnt);
507a564b8f0SMel Gorman }
508a564b8f0SMel Gorman 
509a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file)
510a564b8f0SMel Gorman {
5114dc73c67SNeilBrown 	struct inode *inode = file_inode(file);
5124dc73c67SNeilBrown 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
5134dc73c67SNeilBrown 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
514dad2b015SJeff Layton 
5153c87ef6eSJeff Layton 	rpc_clnt_swap_deactivate(clnt);
5164dc73c67SNeilBrown 	if (cl->rpc_ops->disable_swap)
5174dc73c67SNeilBrown 		cl->rpc_ops->disable_swap(file_inode(file));
518a564b8f0SMel Gorman }
519a564b8f0SMel Gorman 
520f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5211da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5228786fde8SMatthew Wilcox (Oracle) 	.readahead = nfs_readahead,
523187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio = filemap_dirty_folio,
5241da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5251da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5264899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5274899f9c8SNick Piggin 	.write_end = nfs_write_end,
5286d740c76SMatthew Wilcox (Oracle) 	.invalidate_folio = nfs_invalidate_folio,
529cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5301da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
531f844cd0dSChao Yu #ifdef CONFIG_MIGRATION
532074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
533f844cd0dSChao Yu #endif
53415a30ab2SMatthew Wilcox (Oracle) 	.launder_folio = nfs_launder_folio,
535f919b196SMel Gorman 	.is_dirty_writeback = nfs_check_dirty_writeback,
536f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
537a564b8f0SMel Gorman 	.swap_activate = nfs_swap_activate,
538a564b8f0SMel Gorman 	.swap_deactivate = nfs_swap_deactivate,
5391da177e4SLinus Torvalds };
5401da177e4SLinus Torvalds 
5416b9b3514SDavid Howells /*
5426b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5436b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5446b9b3514SDavid Howells  * shared-writable mapping
5456b9b3514SDavid Howells  */
54601a36844SSouptick Joarder static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
54794387fb1STrond Myklebust {
548c2ec175cSNick Piggin 	struct page *page = vmf->page;
54911bac800SDave Jiang 	struct file *filp = vmf->vma->vm_file;
5506de1472fSAl Viro 	struct inode *inode = file_inode(filp);
55194387fb1STrond Myklebust 	unsigned pagelen;
55201a36844SSouptick Joarder 	vm_fault_t ret = VM_FAULT_NOPAGE;
5534899f9c8SNick Piggin 	struct address_space *mapping;
55494387fb1STrond Myklebust 
5551e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
5566de1472fSAl Viro 		filp, filp->f_mapping->host->i_ino,
557b7eaefaaSChuck Lever 		(long long)page_offset(page));
558b7eaefaaSChuck Lever 
5599a773e7cSTrond Myklebust 	sb_start_pagefault(inode->i_sb);
5609a773e7cSTrond Myklebust 
561545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
562a6b5a28eSDave Wysochanski 	if (PageFsCache(page) &&
563a6b5a28eSDave Wysochanski 	    wait_on_page_fscache_killable(vmf->page) < 0) {
564a6b5a28eSDave Wysochanski 		ret = VM_FAULT_RETRY;
565a6b5a28eSDave Wysochanski 		goto out;
566a6b5a28eSDave Wysochanski 	}
567545db45fSDavid Howells 
568ef070dcbSTrond Myklebust 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
569ef070dcbSTrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
570ef070dcbSTrond Myklebust 
57194387fb1STrond Myklebust 	lock_page(page);
572d56b4ddfSMel Gorman 	mapping = page_file_mapping(page);
5736de1472fSAl Viro 	if (mapping != inode->i_mapping)
5748b1f9ee5STrond Myklebust 		goto out_unlock;
5758b1f9ee5STrond Myklebust 
5762aeb98f4STrond Myklebust 	wait_on_page_writeback(page);
5772aeb98f4STrond Myklebust 
5784899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5798b1f9ee5STrond Myklebust 	if (pagelen == 0)
5808b1f9ee5STrond Myklebust 		goto out_unlock;
5818b1f9ee5STrond Myklebust 
582bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
583bc4866b6STrond Myklebust 	if (nfs_flush_incompatible(filp, page) == 0 &&
584bc4866b6STrond Myklebust 	    nfs_updatepage(filp, page, 0, pagelen) == 0)
585bc4866b6STrond Myklebust 		goto out;
5868b1f9ee5STrond Myklebust 
587bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
5888b1f9ee5STrond Myklebust out_unlock:
5894899f9c8SNick Piggin 	unlock_page(page);
590bc4866b6STrond Myklebust out:
5919a773e7cSTrond Myklebust 	sb_end_pagefault(inode->i_sb);
592bc4866b6STrond Myklebust 	return ret;
59394387fb1STrond Myklebust }
59494387fb1STrond Myklebust 
595f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
59694387fb1STrond Myklebust 	.fault = filemap_fault,
597f1820361SKirill A. Shutemov 	.map_pages = filemap_map_pages,
59894387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
59994387fb1STrond Myklebust };
60094387fb1STrond Myklebust 
601edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
6021da177e4SLinus Torvalds {
6036de1472fSAl Viro 	struct file *file = iocb->ki_filp;
6046de1472fSAl Viro 	struct inode *inode = file_inode(file);
605ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
606ed7bcdb3STrond Myklebust 	ssize_t result, written;
607ce368536SScott Mayhew 	errseq_t since;
608ce368536SScott Mayhew 	int error;
6091da177e4SLinus Torvalds 
6106de1472fSAl Viro 	result = nfs_key_timeout_notify(file, inode);
611dc24826bSAndy Adamson 	if (result)
612dc24826bSAndy Adamson 		return result;
613dc24826bSAndy Adamson 
61489698b24STrond Myklebust 	if (iocb->ki_flags & IOCB_DIRECT)
61564158668SNeilBrown 		return nfs_file_direct_write(iocb, from, false);
6161da177e4SLinus Torvalds 
617619d30b4SAl Viro 	dprintk("NFS: write(%pD2, %zu@%Ld)\n",
61818290650STrond Myklebust 		file, iov_iter_count(from), (long long) iocb->ki_pos);
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6211da177e4SLinus Torvalds 		goto out_swapfile;
6227d52e862STrond Myklebust 	/*
6237d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6247d52e862STrond Myklebust 	 */
625fc9dc401STrond Myklebust 	if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) {
6266de1472fSAl Viro 		result = nfs_revalidate_file_size(inode, file);
6271da177e4SLinus Torvalds 		if (result)
628*e6005436STrond Myklebust 			return result;
629fe51beecSTrond Myklebust 	}
6301da177e4SLinus Torvalds 
63128aa2f9eSTrond Myklebust 	nfs_clear_invalid_mapping(file->f_mapping);
63228aa2f9eSTrond Myklebust 
633ce368536SScott Mayhew 	since = filemap_sample_wb_err(file->f_mapping);
634a5864c99STrond Myklebust 	nfs_start_io_write(inode);
63518290650STrond Myklebust 	result = generic_write_checks(iocb, from);
63618290650STrond Myklebust 	if (result > 0) {
63718290650STrond Myklebust 		current->backing_dev_info = inode_to_bdi(inode);
638800ba295SMatthew Wilcox (Oracle) 		result = generic_perform_write(iocb, from);
63918290650STrond Myklebust 		current->backing_dev_info = NULL;
64018290650STrond Myklebust 	}
641a5864c99STrond Myklebust 	nfs_end_io_write(inode);
64218290650STrond Myklebust 	if (result <= 0)
6431da177e4SLinus Torvalds 		goto out;
6441da177e4SLinus Torvalds 
645c49edecdSTrond Myklebust 	written = result;
64618290650STrond Myklebust 	iocb->ki_pos += written;
647*e6005436STrond Myklebust 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
648ed7bcdb3STrond Myklebust 
649ed7bcdb3STrond Myklebust 	if (mntflags & NFS_MOUNT_WRITE_EAGER) {
650ed7bcdb3STrond Myklebust 		result = filemap_fdatawrite_range(file->f_mapping,
651ed7bcdb3STrond Myklebust 						  iocb->ki_pos - written,
652ed7bcdb3STrond Myklebust 						  iocb->ki_pos - 1);
653ed7bcdb3STrond Myklebust 		if (result < 0)
654ed7bcdb3STrond Myklebust 			goto out;
655ed7bcdb3STrond Myklebust 	}
656ed7bcdb3STrond Myklebust 	if (mntflags & NFS_MOUNT_WRITE_WAIT) {
657ed7bcdb3STrond Myklebust 		result = filemap_fdatawait_range(file->f_mapping,
658ed7bcdb3STrond Myklebust 						 iocb->ki_pos - written,
659ed7bcdb3STrond Myklebust 						 iocb->ki_pos - 1);
660ed7bcdb3STrond Myklebust 		if (result < 0)
661ed7bcdb3STrond Myklebust 			goto out;
662ed7bcdb3STrond Myklebust 	}
663e973b1a5Starangg@amazon.com 	result = generic_write_sync(iocb, written);
664e973b1a5Starangg@amazon.com 	if (result < 0)
665*e6005436STrond Myklebust 		return result;
6667e381172SChuck Lever 
667*e6005436STrond Myklebust out:
6687e94d6c4STrond Myklebust 	/* Return error values */
669ce368536SScott Mayhew 	error = filemap_check_wb_err(file->f_mapping, since);
670*e6005436STrond Myklebust 	switch (error) {
671*e6005436STrond Myklebust 	default:
672*e6005436STrond Myklebust 		break;
673*e6005436STrond Myklebust 	case -EDQUOT:
674*e6005436STrond Myklebust 	case -EFBIG:
675*e6005436STrond Myklebust 	case -ENOSPC:
676*e6005436STrond Myklebust 		nfs_wb_all(inode);
677*e6005436STrond Myklebust 		error = file_check_and_advance_wb_err(file);
678*e6005436STrond Myklebust 		if (error < 0)
679*e6005436STrond Myklebust 			result = error;
680200baa21STrond Myklebust 	}
6811da177e4SLinus Torvalds 	return result;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds out_swapfile:
6841da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
68589658c4dSAnna Schumaker 	return -ETXTBSY;
6861da177e4SLinus Torvalds }
68789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write);
6881da177e4SLinus Torvalds 
6895eebde23SSuresh Jayaraman static int
6905eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6931da177e4SLinus Torvalds 	int status = 0;
69421ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
6951da177e4SLinus Torvalds 
696039c4d7aSTrond Myklebust 	/* Try local locking first */
6976d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6986d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6996d34ac19SJ. Bruce Fields 		/* found a conflict */
700039c4d7aSTrond Myklebust 		goto out;
7011da177e4SLinus Torvalds 	}
70221ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
703039c4d7aSTrond Myklebust 
704011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
705039c4d7aSTrond Myklebust 		goto out_noconflict;
706039c4d7aSTrond Myklebust 
7075eebde23SSuresh Jayaraman 	if (is_local)
708039c4d7aSTrond Myklebust 		goto out_noconflict;
709039c4d7aSTrond Myklebust 
710039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
711039c4d7aSTrond Myklebust out:
7121da177e4SLinus Torvalds 	return status;
713039c4d7aSTrond Myklebust out_noconflict:
714039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
715039c4d7aSTrond Myklebust 	goto out;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7185eebde23SSuresh Jayaraman static int
7195eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7227a8203d8STrond Myklebust 	struct nfs_lock_context *l_ctx;
7231da177e4SLinus Torvalds 	int status;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	/*
7261da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7271da177e4SLinus Torvalds 	 * with locks..
7281da177e4SLinus Torvalds 	 */
729aded8d7bSTrond Myklebust 	nfs_wb_all(inode);
7301da177e4SLinus Torvalds 
7317a8203d8STrond Myklebust 	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
7327a8203d8STrond Myklebust 	if (!IS_ERR(l_ctx)) {
733210c7c17SBenjamin Coddington 		status = nfs_iocounter_wait(l_ctx);
7347a8203d8STrond Myklebust 		nfs_put_lock_context(l_ctx);
7351da177e4SLinus Torvalds 		/*  NOTE: special case
7361da177e4SLinus Torvalds 		 * 	If we're signalled while cleaning up locks on process exit, we
7371da177e4SLinus Torvalds 		 * 	still need to complete the unlock.
7381da177e4SLinus Torvalds 		 */
739f30cb757SBenjamin Coddington 		if (status < 0 && !(fl->fl_flags & FL_CLOSE))
740f30cb757SBenjamin Coddington 			return status;
741f30cb757SBenjamin Coddington 	}
742f30cb757SBenjamin Coddington 
7435eebde23SSuresh Jayaraman 	/*
7445eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7455eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7465eebde23SSuresh Jayaraman 	 */
7475eebde23SSuresh Jayaraman 	if (!is_local)
7481da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7491da177e4SLinus Torvalds 	else
75075575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7511da177e4SLinus Torvalds 	return status;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7545eebde23SSuresh Jayaraman static int
7555eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7581da177e4SLinus Torvalds 	int status;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/*
7611da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7621da177e4SLinus Torvalds 	 * with locks..
7631da177e4SLinus Torvalds 	 */
76429884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
76529884df0STrond Myklebust 	if (status != 0)
7661da177e4SLinus Torvalds 		goto out;
7671da177e4SLinus Torvalds 
7685eebde23SSuresh Jayaraman 	/*
7695eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7705eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7715eebde23SSuresh Jayaraman 	 */
7725eebde23SSuresh Jayaraman 	if (!is_local)
7731da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
774c4d7c402STrond Myklebust 	else
77575575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7761da177e4SLinus Torvalds 	if (status < 0)
7771da177e4SLinus Torvalds 		goto out;
7786b96724eSRicardo Labiaga 
7791da177e4SLinus Torvalds 	/*
780779eafabSNeilBrown 	 * Invalidate cache to prevent missing any changes.  If
781779eafabSNeilBrown 	 * the file is mapped, clear the page cache as well so
782779eafabSNeilBrown 	 * those mappings will be loaded.
7836b96724eSRicardo Labiaga 	 *
7841da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7851da177e4SLinus Torvalds 	 */
78629884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
787779eafabSNeilBrown 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
788442ce049SNeilBrown 		nfs_zap_caches(inode);
789779eafabSNeilBrown 		if (mapping_mapped(filp->f_mapping))
790779eafabSNeilBrown 			nfs_revalidate_mapping(inode, filp->f_mapping);
791779eafabSNeilBrown 	}
7921da177e4SLinus Torvalds out:
7931da177e4SLinus Torvalds 	return status;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /*
7971da177e4SLinus Torvalds  * Lock a (portion of) a file
7981da177e4SLinus Torvalds  */
799ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
8022116271aSTrond Myklebust 	int ret = -ENOLCK;
8035eebde23SSuresh Jayaraman 	int is_local = 0;
8041da177e4SLinus Torvalds 
8056de1472fSAl Viro 	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
8066de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags,
8071da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
8086da24bc9SChuck Lever 
80991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
8101da177e4SLinus Torvalds 
811bb0a55bbSJ. Bruce Fields 	if (fl->fl_flags & FL_RECLAIM)
812bb0a55bbSJ. Bruce Fields 		return -ENOGRACE;
813bb0a55bbSJ. Bruce Fields 
8145eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
8155eebde23SSuresh Jayaraman 		is_local = 1;
8165eebde23SSuresh Jayaraman 
8172116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
8182116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
8192116271aSTrond Myklebust 		if (ret < 0)
8202116271aSTrond Myklebust 			goto out_err;
8212116271aSTrond Myklebust 	}
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
8245eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
8252116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
8265eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
8272116271aSTrond Myklebust 	else
8285eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8292116271aSTrond Myklebust out_err:
8302116271aSTrond Myklebust 	return ret;
8311da177e4SLinus Torvalds }
83289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds /*
8351da177e4SLinus Torvalds  * Lock a (portion of) a file
8361da177e4SLinus Torvalds  */
837ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8381da177e4SLinus Torvalds {
8395eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8405eebde23SSuresh Jayaraman 	int is_local = 0;
8415eebde23SSuresh Jayaraman 
8426de1472fSAl Viro 	dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
8436de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8461da177e4SLinus Torvalds 		return -ENOLCK;
8471da177e4SLinus Torvalds 
8485eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8495eebde23SSuresh Jayaraman 		is_local = 1;
8505eebde23SSuresh Jayaraman 
851fcfa4470SBenjamin Coddington 	/* We're simulating flock() locks using posix locks on the server */
852fcfa4470SBenjamin Coddington 	if (fl->fl_type == F_UNLCK)
8535eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8545eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8551da177e4SLinus Torvalds }
85689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock);
857370f6599SJ. Bruce Fields 
8580486958fSJeff Layton const struct file_operations nfs_file_operations = {
8590486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8603aa2d199SAl Viro 	.read_iter	= nfs_file_read,
861edaf4369SAl Viro 	.write_iter	= nfs_file_write,
8620486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8630486958fSJeff Layton 	.open		= nfs_file_open,
8640486958fSJeff Layton 	.flush		= nfs_file_flush,
8650486958fSJeff Layton 	.release	= nfs_file_release,
8660486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8670486958fSJeff Layton 	.lock		= nfs_lock,
8680486958fSJeff Layton 	.flock		= nfs_flock,
86982c156f8SAl Viro 	.splice_read	= generic_file_splice_read,
8704da54c21SAl Viro 	.splice_write	= iter_file_splice_write,
8710486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8721c994a09SJeff Layton 	.setlease	= simple_nosetlease,
8730486958fSJeff Layton };
874ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations);
875