xref: /openbmc/linux/fs/nfs/file.c (revision eb5654b3b89d5e836312cea9f3fdb49457852e89)
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);
72a2ad63daSNeilBrown 	if (res == 0)
73a2ad63daSNeilBrown 		filp->f_mode |= FMODE_CAN_ODIRECT;
741da177e4SLinus Torvalds 	return res;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
77ce4ef7c0SBryan Schumaker int
781da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
791da177e4SLinus Torvalds {
806de1472fSAl Viro 	dprintk("NFS: release(%pD2)\n", filp);
816da24bc9SChuck Lever 
8291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
83aff8d8dcSAnna Schumaker 	nfs_file_clear_open_context(filp);
84a6b5a28eSDave Wysochanski 	nfs_fscache_release_file(inode, filp);
85aff8d8dcSAnna Schumaker 	return 0;
861da177e4SLinus Torvalds }
8789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release);
881da177e4SLinus Torvalds 
89980802e3STrond Myklebust /**
9037eaeed1STrond Myklebust  * nfs_revalidate_file_size - Revalidate the file size
91302fad7bSTrond Myklebust  * @inode: pointer to inode struct
92302fad7bSTrond Myklebust  * @filp: pointer to struct file
93980802e3STrond Myklebust  *
94980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
95980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
96980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
97980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
98980802e3STrond Myklebust  * shouldn't trust the cache).
99980802e3STrond Myklebust  */
100980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
101980802e3STrond Myklebust {
102980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
103d7cf8dd0STrond Myklebust 
104980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
105980802e3STrond Myklebust 		goto force_reval;
10613c0b082STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE))
107d7cf8dd0STrond Myklebust 		goto force_reval;
108fe51beecSTrond Myklebust 	return 0;
109980802e3STrond Myklebust force_reval:
110980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
111980802e3STrond Myklebust }
112980802e3STrond Myklebust 
113965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
114980802e3STrond Myklebust {
1156de1472fSAl Viro 	dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
1166de1472fSAl Viro 			filp, offset, whence);
117b84e06c5SChuck Lever 
11806222e49SJosef Bacik 	/*
119965c8e59SAndrew Morton 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
12006222e49SJosef Bacik 	 * the cached file length
12106222e49SJosef Bacik 	 */
122965c8e59SAndrew Morton 	if (whence != SEEK_SET && whence != SEEK_CUR) {
123980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
124d5e66348STrond Myklebust 
125980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
126980802e3STrond Myklebust 		if (retval < 0)
127980802e3STrond Myklebust 			return (loff_t)retval;
12879835a71SAndi Kleen 	}
129d5e66348STrond Myklebust 
130965c8e59SAndrew Morton 	return generic_file_llseek(filp, offset, whence);
131980802e3STrond Myklebust }
13289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek);
133980802e3STrond Myklebust 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1361da177e4SLinus Torvalds  */
1375445b1fbSTrond Myklebust static int
13875e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1391da177e4SLinus Torvalds {
1406de1472fSAl Viro 	struct inode	*inode = file_inode(file);
14167dd23f9SScott Mayhew 	errseq_t since;
1421da177e4SLinus Torvalds 
1436de1472fSAl Viro 	dprintk("NFS: flush(%pD2)\n", file);
1441da177e4SLinus Torvalds 
145c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1461da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1471da177e4SLinus Torvalds 		return 0;
1487b159fc1STrond Myklebust 
1497fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
15067dd23f9SScott Mayhew 	since = filemap_sample_wb_err(file->f_mapping);
15167dd23f9SScott Mayhew 	nfs_wb_all(inode);
15267dd23f9SScott Mayhew 	return filemap_check_wb_err(file->f_mapping, since);
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
155ce4ef7c0SBryan Schumaker ssize_t
1563aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
1571da177e4SLinus Torvalds {
1586de1472fSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
1591da177e4SLinus Torvalds 	ssize_t result;
1601da177e4SLinus Torvalds 
1612ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_DIRECT)
16264158668SNeilBrown 		return nfs_file_direct_read(iocb, to, false);
1631da177e4SLinus Torvalds 
164619d30b4SAl Viro 	dprintk("NFS: read(%pD2, %zu@%lu)\n",
1656de1472fSAl Viro 		iocb->ki_filp,
1663aa2d199SAl Viro 		iov_iter_count(to), (unsigned long) iocb->ki_pos);
1671da177e4SLinus Torvalds 
168a5864c99STrond Myklebust 	nfs_start_io_read(inode);
169a5864c99STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1704184dcf2SChuck Lever 	if (!result) {
1713aa2d199SAl Viro 		result = generic_file_read_iter(iocb, to);
1724184dcf2SChuck Lever 		if (result > 0)
1734184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1744184dcf2SChuck Lever 	}
175a5864c99STrond Myklebust 	nfs_end_io_read(inode);
1761da177e4SLinus Torvalds 	return result;
1771da177e4SLinus Torvalds }
17889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read);
1791da177e4SLinus Torvalds 
180ce4ef7c0SBryan Schumaker int
1811da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
1821da177e4SLinus Torvalds {
1836de1472fSAl Viro 	struct inode *inode = file_inode(file);
1841da177e4SLinus Torvalds 	int	status;
1851da177e4SLinus Torvalds 
1866de1472fSAl Viro 	dprintk("NFS: mmap(%pD2)\n", file);
1871da177e4SLinus Torvalds 
188e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
189e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
190e1ebfd33STrond Myklebust 	 */
191e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
19294387fb1STrond Myklebust 	if (!status) {
19394387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
194e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
19594387fb1STrond Myklebust 	}
1961da177e4SLinus Torvalds 	return status;
1971da177e4SLinus Torvalds }
19889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /*
2011da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2021da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2031da177e4SLinus Torvalds  * whether any write errors occurred for this process.
2041da177e4SLinus Torvalds  */
2054ff79bc7SChristoph Hellwig static int
206bf4b4905SNeilBrown nfs_file_fsync_commit(struct file *file, int datasync)
2071da177e4SLinus Torvalds {
2086de1472fSAl Viro 	struct inode *inode = file_inode(file);
2099641d9bcSTrond Myklebust 	int ret, ret2;
210af7fa165STrond Myklebust 
2116de1472fSAl Viro 	dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
2121da177e4SLinus Torvalds 
21391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
2142197e9b0STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
2159641d9bcSTrond Myklebust 	ret2 = file_check_and_advance_wb_err(file);
2169641d9bcSTrond Myklebust 	if (ret2 < 0)
2179641d9bcSTrond Myklebust 		return ret2;
218a5c58892SBryan Schumaker 	return ret;
219a5c58892SBryan Schumaker }
220a5c58892SBryan Schumaker 
2214ff79bc7SChristoph Hellwig int
222a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
223a5c58892SBryan Schumaker {
224496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
22567f4b5dcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
22667f4b5dcSTrond Myklebust 	long save_nredirtied = atomic_long_read(&nfsi->redirtied_pages);
22767f4b5dcSTrond Myklebust 	long nredirtied;
2282197e9b0STrond Myklebust 	int ret;
229a5c58892SBryan Schumaker 
230f4ce1299STrond Myklebust 	trace_nfs_fsync_enter(inode);
231f4ce1299STrond Myklebust 
2322197e9b0STrond Myklebust 	for (;;) {
2336fbda89bSTrond Myklebust 		ret = file_write_and_wait_range(file, start, end);
2347b281ee0STrond Myklebust 		if (ret != 0)
23505990d1bSTrond Myklebust 			break;
236bf4b4905SNeilBrown 		ret = nfs_file_fsync_commit(file, datasync);
2372197e9b0STrond Myklebust 		if (ret != 0)
2382197e9b0STrond Myklebust 			break;
2394ff79bc7SChristoph Hellwig 		ret = pnfs_sync_inode(inode, !!datasync);
2402197e9b0STrond Myklebust 		if (ret != 0)
2412197e9b0STrond Myklebust 			break;
24267f4b5dcSTrond Myklebust 		nredirtied = atomic_long_read(&nfsi->redirtied_pages);
24367f4b5dcSTrond Myklebust 		if (nredirtied == save_nredirtied)
2442197e9b0STrond Myklebust 			break;
24567f4b5dcSTrond Myklebust 		save_nredirtied = nredirtied;
2462197e9b0STrond Myklebust 	}
24705990d1bSTrond Myklebust 
248f4ce1299STrond Myklebust 	trace_nfs_fsync_exit(inode, ret);
249af7fa165STrond Myklebust 	return ret;
2501da177e4SLinus Torvalds }
2514ff79bc7SChristoph Hellwig EXPORT_SYMBOL_GPL(nfs_file_fsync);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /*
25438c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
25538c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
25638c73044SPeter Staubach  * page cache.
25738c73044SPeter Staubach  *
2582cde04e9SKazuo Ito  * Some pNFS layout drivers can only read/write at a certain block
2592cde04e9SKazuo Ito  * granularity like all block devices and therefore we must perform
2602cde04e9SKazuo Ito  * read/modify/write whenever a page hasn't read yet and the data
2612cde04e9SKazuo Ito  * to be written there is not aligned to a block boundary and/or
2622cde04e9SKazuo Ito  * smaller than the block size.
2632cde04e9SKazuo Ito  *
26438c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
26538c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
26638c73044SPeter Staubach  * page must be completely written to stable storage on the server
26738c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
26838c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
26938c73044SPeter Staubach  * done.
27038c73044SPeter Staubach  *
27138c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
27238c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
27338c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
27438c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
27538c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
27638c73044SPeter Staubach  * and that the new data won't completely replace the old data in
27738c73044SPeter Staubach  * that range of the file.
27838c73044SPeter Staubach  */
27954d99381STrond Myklebust static bool nfs_folio_is_full_write(struct folio *folio, loff_t pos,
28054d99381STrond Myklebust 				    unsigned int len)
28138c73044SPeter Staubach {
28254d99381STrond Myklebust 	unsigned int pglen = nfs_folio_length(folio);
28354d99381STrond Myklebust 	unsigned int offset = offset_in_folio(folio, pos);
28438c73044SPeter Staubach 	unsigned int end = offset + len;
28538c73044SPeter Staubach 
2862cde04e9SKazuo Ito 	return !pglen || (end >= pglen && !offset);
287612aa983SChristoph Hellwig }
288612aa983SChristoph Hellwig 
28954d99381STrond Myklebust static bool nfs_want_read_modify_write(struct file *file, struct folio *folio,
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 	 */
29654d99381STrond Myklebust 	if (folio_test_uptodate(folio) || folio_test_private(folio) ||
29754d99381STrond Myklebust 	    nfs_folio_is_full_write(folio, pos, len))
2982cde04e9SKazuo Ito 		return false;
2992cde04e9SKazuo Ito 
30054d99381STrond Myklebust 	if (pnfs_ld_read_whole_page(file_inode(file)))
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 
30854d99381STrond Myklebust static struct folio *
30954d99381STrond Myklebust nfs_folio_grab_cache_write_begin(struct address_space *mapping, pgoff_t index)
31054d99381STrond Myklebust {
31154d99381STrond Myklebust 	unsigned fgp_flags = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE;
31254d99381STrond Myklebust 
31354d99381STrond Myklebust 	return __filemap_get_folio(mapping, index, fgp_flags,
31454d99381STrond Myklebust 				   mapping_gfp_mask(mapping));
31554d99381STrond Myklebust }
31654d99381STrond Myklebust 
31738c73044SPeter Staubach /*
3184899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3194899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3204899f9c8SNick Piggin  * data from user space.
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3231da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3241da177e4SLinus Torvalds  */
3254899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
32654d99381STrond Myklebust 			   loff_t pos, unsigned len, struct page **pagep,
32754d99381STrond Myklebust 			   void **fsdata)
3281da177e4SLinus Torvalds {
3290c493b5cSTrond Myklebust 	struct folio *folio;
33038c73044SPeter Staubach 	int once_thru = 0;
33154d99381STrond Myklebust 	int ret;
3324899f9c8SNick Piggin 
3331e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
3346de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
335b7eaefaaSChuck Lever 
33638c73044SPeter Staubach start:
33754d99381STrond Myklebust 	folio = nfs_folio_grab_cache_write_begin(mapping, pos >> PAGE_SHIFT);
33854d99381STrond Myklebust 	if (!folio)
3394899f9c8SNick Piggin 		return -ENOMEM;
34054d99381STrond Myklebust 	*pagep = &folio->page;
3414899f9c8SNick Piggin 
3420c493b5cSTrond Myklebust 	ret = nfs_flush_incompatible(file, folio);
3434899f9c8SNick Piggin 	if (ret) {
34454d99381STrond Myklebust 		folio_unlock(folio);
34554d99381STrond Myklebust 		folio_put(folio);
34638c73044SPeter Staubach 	} else if (!once_thru &&
34754d99381STrond Myklebust 		   nfs_want_read_modify_write(file, folio, pos, len)) {
34838c73044SPeter Staubach 		once_thru = 1;
3490c493b5cSTrond Myklebust 		ret = nfs_read_folio(file, folio);
35054d99381STrond Myklebust 		folio_put(folio);
35138c73044SPeter Staubach 		if (!ret)
35238c73044SPeter Staubach 			goto start;
3534899f9c8SNick Piggin 	}
3544899f9c8SNick Piggin 	return ret;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3574899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3584899f9c8SNick Piggin 			 loff_t pos, unsigned len, unsigned copied,
3594899f9c8SNick Piggin 			 struct page *page, void *fsdata)
3601da177e4SLinus Torvalds {
361dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3620c493b5cSTrond Myklebust 	struct folio *folio = page_folio(page);
36354d99381STrond Myklebust 	unsigned offset = offset_in_folio(folio, pos);
3644899f9c8SNick Piggin 	int status;
3651da177e4SLinus Torvalds 
3661e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
3676de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
368b7eaefaaSChuck Lever 
369efc91ed0STrond Myklebust 	/*
370efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
371efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
372efc91ed0STrond Myklebust 	 */
37354d99381STrond Myklebust 	if (!folio_test_uptodate(folio)) {
37454d99381STrond Myklebust 		size_t fsize = folio_size(folio);
37554d99381STrond Myklebust 		unsigned pglen = nfs_folio_length(folio);
376c0cf3ef5SAl Viro 		unsigned end = offset + copied;
377efc91ed0STrond Myklebust 
378efc91ed0STrond Myklebust 		if (pglen == 0) {
37954d99381STrond Myklebust 			folio_zero_segments(folio, 0, offset, end, fsize);
38054d99381STrond Myklebust 			folio_mark_uptodate(folio);
381efc91ed0STrond Myklebust 		} else if (end >= pglen) {
38254d99381STrond Myklebust 			folio_zero_segment(folio, end, fsize);
383efc91ed0STrond Myklebust 			if (offset == 0)
38454d99381STrond Myklebust 				folio_mark_uptodate(folio);
385efc91ed0STrond Myklebust 		} else
38654d99381STrond Myklebust 			folio_zero_segment(folio, pglen, fsize);
387efc91ed0STrond Myklebust 	}
388efc91ed0STrond Myklebust 
3890c493b5cSTrond Myklebust 	status = nfs_update_folio(file, folio, offset, copied);
3904899f9c8SNick Piggin 
39154d99381STrond Myklebust 	folio_unlock(folio);
39254d99381STrond Myklebust 	folio_put(folio);
3934899f9c8SNick Piggin 
3943d509e54SChuck Lever 	if (status < 0)
3953d509e54SChuck Lever 		return status;
3962701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
397dc24826bSAndy Adamson 
398d95b2665STrond Myklebust 	if (nfs_ctx_key_to_expire(ctx, mapping->host))
399d95b2665STrond Myklebust 		nfs_wb_all(mapping->host);
400dc24826bSAndy Adamson 
4013d509e54SChuck Lever 	return copied;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4046b9b3514SDavid Howells /*
4056b9b3514SDavid Howells  * Partially or wholly invalidate a page
4066b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4076b9b3514SDavid Howells  *   page invalidation
408545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4096b9b3514SDavid Howells  * - Caller holds page lock
4106b9b3514SDavid Howells  */
4116d740c76SMatthew Wilcox (Oracle) static void nfs_invalidate_folio(struct folio *folio, size_t offset,
4126d740c76SMatthew Wilcox (Oracle) 				size_t length)
413cd52ed35STrond Myklebust {
414*eb5654b3STrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
4156d740c76SMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n",
4166d740c76SMatthew Wilcox (Oracle) 		 folio->index, offset, length);
417b7eaefaaSChuck Lever 
4186d740c76SMatthew Wilcox (Oracle) 	if (offset != 0 || length < folio_size(folio))
4191c75950bSTrond Myklebust 		return;
420d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
421*eb5654b3STrond Myklebust 	nfs_wb_folio_cancel(inode, folio);
4226d740c76SMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
423*eb5654b3STrond Myklebust 	trace_nfs_invalidate_folio(inode, folio);
424cd52ed35STrond Myklebust }
425cd52ed35STrond Myklebust 
4266b9b3514SDavid Howells /*
4273577da4aSMatthew Wilcox (Oracle)  * Attempt to release the private state associated with a folio
4283577da4aSMatthew Wilcox (Oracle)  * - Called if either private or fscache flags are set on the folio
4293577da4aSMatthew Wilcox (Oracle)  * - Caller holds folio lock
4303577da4aSMatthew Wilcox (Oracle)  * - Return true (may release folio) or false (may not)
4316b9b3514SDavid Howells  */
4323577da4aSMatthew Wilcox (Oracle) static bool nfs_release_folio(struct folio *folio, gfp_t gfp)
433cd52ed35STrond Myklebust {
4343577da4aSMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: release_folio(%p)\n", folio);
435b7eaefaaSChuck Lever 
4363577da4aSMatthew Wilcox (Oracle) 	/* If the private flag is set, then the folio is not freeable */
43796780ca5STrond Myklebust 	if (folio_test_private(folio)) {
43896780ca5STrond Myklebust 		if ((current_gfp_context(gfp) & GFP_KERNEL) != GFP_KERNEL ||
43996780ca5STrond Myklebust 		    current_is_kswapd())
4403577da4aSMatthew Wilcox (Oracle) 			return false;
44196780ca5STrond Myklebust 		if (nfs_wb_folio(folio_file_mapping(folio)->host, folio) < 0)
44296780ca5STrond Myklebust 			return false;
44396780ca5STrond Myklebust 	}
4443577da4aSMatthew Wilcox (Oracle) 	return nfs_fscache_release_folio(folio, gfp);
445e3db7691STrond Myklebust }
446e3db7691STrond Myklebust 
447520f301cSMatthew Wilcox (Oracle) static void nfs_check_dirty_writeback(struct folio *folio,
448f919b196SMel Gorman 				bool *dirty, bool *writeback)
449f919b196SMel Gorman {
450f919b196SMel Gorman 	struct nfs_inode *nfsi;
451520f301cSMatthew Wilcox (Oracle) 	struct address_space *mapping = folio->mapping;
452f919b196SMel Gorman 
453f919b196SMel Gorman 	/*
454520f301cSMatthew Wilcox (Oracle) 	 * Check if an unstable folio is currently being committed and
455520f301cSMatthew Wilcox (Oracle) 	 * if so, have the VM treat it as if the folio is under writeback
456520f301cSMatthew Wilcox (Oracle) 	 * so it will not block due to folios that will shortly be freeable.
457f919b196SMel Gorman 	 */
458f919b196SMel Gorman 	nfsi = NFS_I(mapping->host);
459af7cf057STrond Myklebust 	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
460f919b196SMel Gorman 		*writeback = true;
461f919b196SMel Gorman 		return;
462f919b196SMel Gorman 	}
463f919b196SMel Gorman 
464f919b196SMel Gorman 	/*
465520f301cSMatthew Wilcox (Oracle) 	 * If the private flag is set, then the folio is not freeable
466520f301cSMatthew Wilcox (Oracle) 	 * and as the inode is not being committed, it's not going to
467520f301cSMatthew Wilcox (Oracle) 	 * be cleaned in the near future so treat it as dirty
468f919b196SMel Gorman 	 */
469520f301cSMatthew Wilcox (Oracle) 	if (folio_test_private(folio))
470f919b196SMel Gorman 		*dirty = true;
471f919b196SMel Gorman }
472f919b196SMel Gorman 
4736b9b3514SDavid Howells /*
4746b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4756b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4766b9b3514SDavid Howells  * destroyed
477545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4786b9b3514SDavid Howells  * - Caller holds page lock
4796b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4806b9b3514SDavid Howells  */
48115a30ab2SMatthew Wilcox (Oracle) static int nfs_launder_folio(struct folio *folio)
482e3db7691STrond Myklebust {
48315a30ab2SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
484*eb5654b3STrond Myklebust 	int ret;
485b7eaefaaSChuck Lever 
48615a30ab2SMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: launder_folio(%ld, %llu)\n",
48715a30ab2SMatthew Wilcox (Oracle) 		inode->i_ino, folio_pos(folio));
488b7eaefaaSChuck Lever 
48915a30ab2SMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
490*eb5654b3STrond Myklebust 	ret = nfs_wb_folio(inode, folio);
491*eb5654b3STrond Myklebust 	trace_nfs_launder_folio_done(inode, folio, ret);
492*eb5654b3STrond Myklebust 	return ret;
493cd52ed35STrond Myklebust }
494cd52ed35STrond Myklebust 
495a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
496a564b8f0SMel Gorman 						sector_t *span)
497a564b8f0SMel Gorman {
498bd89bc67SMurphy Zhou 	unsigned long blocks;
499bd89bc67SMurphy Zhou 	long long isize;
5004b60c0ffSNeilBrown 	int ret;
5014dc73c67SNeilBrown 	struct inode *inode = file_inode(file);
5024dc73c67SNeilBrown 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
5034dc73c67SNeilBrown 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
504bd89bc67SMurphy Zhou 
505bd89bc67SMurphy Zhou 	spin_lock(&inode->i_lock);
506bd89bc67SMurphy Zhou 	blocks = inode->i_blocks;
507bd89bc67SMurphy Zhou 	isize = inode->i_size;
508bd89bc67SMurphy Zhou 	spin_unlock(&inode->i_lock);
509bd89bc67SMurphy Zhou 	if (blocks*512 < isize) {
510bd89bc67SMurphy Zhou 		pr_warn("swap activate: swapfile has holes\n");
511bd89bc67SMurphy Zhou 		return -EINVAL;
512bd89bc67SMurphy Zhou 	}
513dad2b015SJeff Layton 
5144b60c0ffSNeilBrown 	ret = rpc_clnt_swap_activate(clnt);
5154b60c0ffSNeilBrown 	if (ret)
5164b60c0ffSNeilBrown 		return ret;
5174b60c0ffSNeilBrown 	ret = add_swap_extent(sis, 0, sis->max, 0);
5184b60c0ffSNeilBrown 	if (ret < 0) {
5194b60c0ffSNeilBrown 		rpc_clnt_swap_deactivate(clnt);
5204b60c0ffSNeilBrown 		return ret;
5214b60c0ffSNeilBrown 	}
522dad2b015SJeff Layton 
5234b60c0ffSNeilBrown 	*span = sis->pages;
5244dc73c67SNeilBrown 
5254dc73c67SNeilBrown 	if (cl->rpc_ops->enable_swap)
5264dc73c67SNeilBrown 		cl->rpc_ops->enable_swap(inode);
5274dc73c67SNeilBrown 
5284b60c0ffSNeilBrown 	sis->flags |= SWP_FS_OPS;
5294b60c0ffSNeilBrown 	return ret;
530a564b8f0SMel Gorman }
531a564b8f0SMel Gorman 
532a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file)
533a564b8f0SMel Gorman {
5344dc73c67SNeilBrown 	struct inode *inode = file_inode(file);
5354dc73c67SNeilBrown 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
5364dc73c67SNeilBrown 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
537dad2b015SJeff Layton 
5383c87ef6eSJeff Layton 	rpc_clnt_swap_deactivate(clnt);
5394dc73c67SNeilBrown 	if (cl->rpc_ops->disable_swap)
5404dc73c67SNeilBrown 		cl->rpc_ops->disable_swap(file_inode(file));
541a564b8f0SMel Gorman }
542a564b8f0SMel Gorman 
543f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
54465d023afSMatthew Wilcox (Oracle) 	.read_folio = nfs_read_folio,
5458786fde8SMatthew Wilcox (Oracle) 	.readahead = nfs_readahead,
546187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio = filemap_dirty_folio,
5471da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5481da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5494899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5504899f9c8SNick Piggin 	.write_end = nfs_write_end,
5516d740c76SMatthew Wilcox (Oracle) 	.invalidate_folio = nfs_invalidate_folio,
5523577da4aSMatthew Wilcox (Oracle) 	.release_folio = nfs_release_folio,
5534ae84a80SMatthew Wilcox (Oracle) 	.migrate_folio = nfs_migrate_folio,
55415a30ab2SMatthew Wilcox (Oracle) 	.launder_folio = nfs_launder_folio,
555f919b196SMel Gorman 	.is_dirty_writeback = nfs_check_dirty_writeback,
556f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
557a564b8f0SMel Gorman 	.swap_activate = nfs_swap_activate,
558a564b8f0SMel Gorman 	.swap_deactivate = nfs_swap_deactivate,
559eb79f3afSNeilBrown 	.swap_rw = nfs_swap_rw,
5601da177e4SLinus Torvalds };
5611da177e4SLinus Torvalds 
5626b9b3514SDavid Howells /*
5636b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5646b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5656b9b3514SDavid Howells  * shared-writable mapping
5666b9b3514SDavid Howells  */
56701a36844SSouptick Joarder static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
56894387fb1STrond Myklebust {
56911bac800SDave Jiang 	struct file *filp = vmf->vma->vm_file;
5706de1472fSAl Viro 	struct inode *inode = file_inode(filp);
57194387fb1STrond Myklebust 	unsigned pagelen;
57201a36844SSouptick Joarder 	vm_fault_t ret = VM_FAULT_NOPAGE;
5734899f9c8SNick Piggin 	struct address_space *mapping;
5744fa7a717STrond Myklebust 	struct folio *folio = page_folio(vmf->page);
57594387fb1STrond Myklebust 
5761e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
5776de1472fSAl Viro 		 filp, filp->f_mapping->host->i_ino,
5784fa7a717STrond Myklebust 		 (long long)folio_file_pos(folio));
579b7eaefaaSChuck Lever 
5809a773e7cSTrond Myklebust 	sb_start_pagefault(inode->i_sb);
5819a773e7cSTrond Myklebust 
582545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
5834fa7a717STrond Myklebust 	if (folio_test_fscache(folio) &&
5844fa7a717STrond Myklebust 	    folio_wait_fscache_killable(folio) < 0) {
585a6b5a28eSDave Wysochanski 		ret = VM_FAULT_RETRY;
586a6b5a28eSDave Wysochanski 		goto out;
587a6b5a28eSDave Wysochanski 	}
588545db45fSDavid Howells 
589ef070dcbSTrond Myklebust 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
590f5d39b02SPeter Zijlstra 			   nfs_wait_bit_killable,
591f5d39b02SPeter Zijlstra 			   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
592ef070dcbSTrond Myklebust 
5934fa7a717STrond Myklebust 	folio_lock(folio);
5944fa7a717STrond Myklebust 	mapping = folio_file_mapping(folio);
5956de1472fSAl Viro 	if (mapping != inode->i_mapping)
5968b1f9ee5STrond Myklebust 		goto out_unlock;
5978b1f9ee5STrond Myklebust 
5984fa7a717STrond Myklebust 	folio_wait_writeback(folio);
5992aeb98f4STrond Myklebust 
6004fa7a717STrond Myklebust 	pagelen = nfs_folio_length(folio);
6018b1f9ee5STrond Myklebust 	if (pagelen == 0)
6028b1f9ee5STrond Myklebust 		goto out_unlock;
6038b1f9ee5STrond Myklebust 
604bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
6050c493b5cSTrond Myklebust 	if (nfs_flush_incompatible(filp, folio) == 0 &&
6060c493b5cSTrond Myklebust 	    nfs_update_folio(filp, folio, 0, pagelen) == 0)
607bc4866b6STrond Myklebust 		goto out;
6088b1f9ee5STrond Myklebust 
609bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
6108b1f9ee5STrond Myklebust out_unlock:
6114fa7a717STrond Myklebust 	folio_unlock(folio);
612bc4866b6STrond Myklebust out:
6139a773e7cSTrond Myklebust 	sb_end_pagefault(inode->i_sb);
614bc4866b6STrond Myklebust 	return ret;
61594387fb1STrond Myklebust }
61694387fb1STrond Myklebust 
617f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
61894387fb1STrond Myklebust 	.fault = filemap_fault,
619f1820361SKirill A. Shutemov 	.map_pages = filemap_map_pages,
62094387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
62194387fb1STrond Myklebust };
62294387fb1STrond Myklebust 
623edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
6241da177e4SLinus Torvalds {
6256de1472fSAl Viro 	struct file *file = iocb->ki_filp;
6266de1472fSAl Viro 	struct inode *inode = file_inode(file);
627ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
628ed7bcdb3STrond Myklebust 	ssize_t result, written;
629ce368536SScott Mayhew 	errseq_t since;
630ce368536SScott Mayhew 	int error;
6311da177e4SLinus Torvalds 
6326de1472fSAl Viro 	result = nfs_key_timeout_notify(file, inode);
633dc24826bSAndy Adamson 	if (result)
634dc24826bSAndy Adamson 		return result;
635dc24826bSAndy Adamson 
63689698b24STrond Myklebust 	if (iocb->ki_flags & IOCB_DIRECT)
63764158668SNeilBrown 		return nfs_file_direct_write(iocb, from, false);
6381da177e4SLinus Torvalds 
639619d30b4SAl Viro 	dprintk("NFS: write(%pD2, %zu@%Ld)\n",
64018290650STrond Myklebust 		file, iov_iter_count(from), (long long) iocb->ki_pos);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6431da177e4SLinus Torvalds 		goto out_swapfile;
6447d52e862STrond Myklebust 	/*
6457d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6467d52e862STrond Myklebust 	 */
647fc9dc401STrond Myklebust 	if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) {
6486de1472fSAl Viro 		result = nfs_revalidate_file_size(inode, file);
6491da177e4SLinus Torvalds 		if (result)
650e6005436STrond Myklebust 			return result;
651fe51beecSTrond Myklebust 	}
6521da177e4SLinus Torvalds 
65328aa2f9eSTrond Myklebust 	nfs_clear_invalid_mapping(file->f_mapping);
65428aa2f9eSTrond Myklebust 
655ce368536SScott Mayhew 	since = filemap_sample_wb_err(file->f_mapping);
656a5864c99STrond Myklebust 	nfs_start_io_write(inode);
65718290650STrond Myklebust 	result = generic_write_checks(iocb, from);
65818290650STrond Myklebust 	if (result > 0) {
65918290650STrond Myklebust 		current->backing_dev_info = inode_to_bdi(inode);
660800ba295SMatthew Wilcox (Oracle) 		result = generic_perform_write(iocb, from);
66118290650STrond Myklebust 		current->backing_dev_info = NULL;
66218290650STrond Myklebust 	}
663a5864c99STrond Myklebust 	nfs_end_io_write(inode);
66418290650STrond Myklebust 	if (result <= 0)
6651da177e4SLinus Torvalds 		goto out;
6661da177e4SLinus Torvalds 
667c49edecdSTrond Myklebust 	written = result;
66818290650STrond Myklebust 	iocb->ki_pos += written;
669e6005436STrond Myklebust 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
670ed7bcdb3STrond Myklebust 
671ed7bcdb3STrond Myklebust 	if (mntflags & NFS_MOUNT_WRITE_EAGER) {
672ed7bcdb3STrond Myklebust 		result = filemap_fdatawrite_range(file->f_mapping,
673ed7bcdb3STrond Myklebust 						  iocb->ki_pos - written,
674ed7bcdb3STrond Myklebust 						  iocb->ki_pos - 1);
675ed7bcdb3STrond Myklebust 		if (result < 0)
676ed7bcdb3STrond Myklebust 			goto out;
677ed7bcdb3STrond Myklebust 	}
678ed7bcdb3STrond Myklebust 	if (mntflags & NFS_MOUNT_WRITE_WAIT) {
679384edeb4SLukas Bulwahn 		filemap_fdatawait_range(file->f_mapping,
680ed7bcdb3STrond Myklebust 					iocb->ki_pos - written,
681ed7bcdb3STrond Myklebust 					iocb->ki_pos - 1);
682ed7bcdb3STrond Myklebust 	}
683e973b1a5Starangg@amazon.com 	result = generic_write_sync(iocb, written);
684e973b1a5Starangg@amazon.com 	if (result < 0)
685e6005436STrond Myklebust 		return result;
6867e381172SChuck Lever 
687e6005436STrond Myklebust out:
6887e94d6c4STrond Myklebust 	/* Return error values */
689ce368536SScott Mayhew 	error = filemap_check_wb_err(file->f_mapping, since);
690e6005436STrond Myklebust 	switch (error) {
691e6005436STrond Myklebust 	default:
692e6005436STrond Myklebust 		break;
693e6005436STrond Myklebust 	case -EDQUOT:
694e6005436STrond Myklebust 	case -EFBIG:
695e6005436STrond Myklebust 	case -ENOSPC:
696e6005436STrond Myklebust 		nfs_wb_all(inode);
697e6005436STrond Myklebust 		error = file_check_and_advance_wb_err(file);
698e6005436STrond Myklebust 		if (error < 0)
699e6005436STrond Myklebust 			result = error;
700200baa21STrond Myklebust 	}
7011da177e4SLinus Torvalds 	return result;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds out_swapfile:
7041da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
70589658c4dSAnna Schumaker 	return -ETXTBSY;
7061da177e4SLinus Torvalds }
70789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write);
7081da177e4SLinus Torvalds 
7095eebde23SSuresh Jayaraman static int
7105eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7131da177e4SLinus Torvalds 	int status = 0;
71421ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
7151da177e4SLinus Torvalds 
716039c4d7aSTrond Myklebust 	/* Try local locking first */
7176d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
7186d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
7196d34ac19SJ. Bruce Fields 		/* found a conflict */
720039c4d7aSTrond Myklebust 		goto out;
7211da177e4SLinus Torvalds 	}
72221ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
723039c4d7aSTrond Myklebust 
724011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
725039c4d7aSTrond Myklebust 		goto out_noconflict;
726039c4d7aSTrond Myklebust 
7275eebde23SSuresh Jayaraman 	if (is_local)
728039c4d7aSTrond Myklebust 		goto out_noconflict;
729039c4d7aSTrond Myklebust 
730039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
731039c4d7aSTrond Myklebust out:
7321da177e4SLinus Torvalds 	return status;
733039c4d7aSTrond Myklebust out_noconflict:
734039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
735039c4d7aSTrond Myklebust 	goto out;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7385eebde23SSuresh Jayaraman static int
7395eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7427a8203d8STrond Myklebust 	struct nfs_lock_context *l_ctx;
7431da177e4SLinus Torvalds 	int status;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	/*
7461da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7471da177e4SLinus Torvalds 	 * with locks..
7481da177e4SLinus Torvalds 	 */
749aded8d7bSTrond Myklebust 	nfs_wb_all(inode);
7501da177e4SLinus Torvalds 
7517a8203d8STrond Myklebust 	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
7527a8203d8STrond Myklebust 	if (!IS_ERR(l_ctx)) {
753210c7c17SBenjamin Coddington 		status = nfs_iocounter_wait(l_ctx);
7547a8203d8STrond Myklebust 		nfs_put_lock_context(l_ctx);
7551da177e4SLinus Torvalds 		/*  NOTE: special case
7561da177e4SLinus Torvalds 		 * 	If we're signalled while cleaning up locks on process exit, we
7571da177e4SLinus Torvalds 		 * 	still need to complete the unlock.
7581da177e4SLinus Torvalds 		 */
759f30cb757SBenjamin Coddington 		if (status < 0 && !(fl->fl_flags & FL_CLOSE))
760f30cb757SBenjamin Coddington 			return status;
761f30cb757SBenjamin Coddington 	}
762f30cb757SBenjamin Coddington 
7635eebde23SSuresh Jayaraman 	/*
7645eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7655eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7665eebde23SSuresh Jayaraman 	 */
7675eebde23SSuresh Jayaraman 	if (!is_local)
7681da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7691da177e4SLinus Torvalds 	else
77075575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7711da177e4SLinus Torvalds 	return status;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7745eebde23SSuresh Jayaraman static int
7755eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7781da177e4SLinus Torvalds 	int status;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	/*
7811da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7821da177e4SLinus Torvalds 	 * with locks..
7831da177e4SLinus Torvalds 	 */
78429884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
78529884df0STrond Myklebust 	if (status != 0)
7861da177e4SLinus Torvalds 		goto out;
7871da177e4SLinus Torvalds 
7885eebde23SSuresh Jayaraman 	/*
7895eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7905eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7915eebde23SSuresh Jayaraman 	 */
7925eebde23SSuresh Jayaraman 	if (!is_local)
7931da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
794c4d7c402STrond Myklebust 	else
79575575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7961da177e4SLinus Torvalds 	if (status < 0)
7971da177e4SLinus Torvalds 		goto out;
7986b96724eSRicardo Labiaga 
7991da177e4SLinus Torvalds 	/*
800779eafabSNeilBrown 	 * Invalidate cache to prevent missing any changes.  If
801779eafabSNeilBrown 	 * the file is mapped, clear the page cache as well so
802779eafabSNeilBrown 	 * those mappings will be loaded.
8036b96724eSRicardo Labiaga 	 *
8041da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
8051da177e4SLinus Torvalds 	 */
80629884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
807779eafabSNeilBrown 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
808442ce049SNeilBrown 		nfs_zap_caches(inode);
809779eafabSNeilBrown 		if (mapping_mapped(filp->f_mapping))
810779eafabSNeilBrown 			nfs_revalidate_mapping(inode, filp->f_mapping);
811779eafabSNeilBrown 	}
8121da177e4SLinus Torvalds out:
8131da177e4SLinus Torvalds 	return status;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds /*
8171da177e4SLinus Torvalds  * Lock a (portion of) a file
8181da177e4SLinus Torvalds  */
819ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
8222116271aSTrond Myklebust 	int ret = -ENOLCK;
8235eebde23SSuresh Jayaraman 	int is_local = 0;
8241da177e4SLinus Torvalds 
8256de1472fSAl Viro 	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
8266de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags,
8271da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
8286da24bc9SChuck Lever 
82991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
8301da177e4SLinus Torvalds 
831bb0a55bbSJ. Bruce Fields 	if (fl->fl_flags & FL_RECLAIM)
832bb0a55bbSJ. Bruce Fields 		return -ENOGRACE;
833bb0a55bbSJ. Bruce Fields 
8345eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
8355eebde23SSuresh Jayaraman 		is_local = 1;
8365eebde23SSuresh Jayaraman 
8372116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
8382116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
8392116271aSTrond Myklebust 		if (ret < 0)
8402116271aSTrond Myklebust 			goto out_err;
8412116271aSTrond Myklebust 	}
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
8445eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
8452116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
8465eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
8472116271aSTrond Myklebust 	else
8485eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8492116271aSTrond Myklebust out_err:
8502116271aSTrond Myklebust 	return ret;
8511da177e4SLinus Torvalds }
85289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds /*
8551da177e4SLinus Torvalds  * Lock a (portion of) a file
8561da177e4SLinus Torvalds  */
857ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8581da177e4SLinus Torvalds {
8595eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8605eebde23SSuresh Jayaraman 	int is_local = 0;
8615eebde23SSuresh Jayaraman 
8626de1472fSAl Viro 	dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
8636de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8661da177e4SLinus Torvalds 		return -ENOLCK;
8671da177e4SLinus Torvalds 
8685eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8695eebde23SSuresh Jayaraman 		is_local = 1;
8705eebde23SSuresh Jayaraman 
871fcfa4470SBenjamin Coddington 	/* We're simulating flock() locks using posix locks on the server */
872fcfa4470SBenjamin Coddington 	if (fl->fl_type == F_UNLCK)
8735eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8745eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8751da177e4SLinus Torvalds }
87689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock);
877370f6599SJ. Bruce Fields 
8780486958fSJeff Layton const struct file_operations nfs_file_operations = {
8790486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8803aa2d199SAl Viro 	.read_iter	= nfs_file_read,
881edaf4369SAl Viro 	.write_iter	= nfs_file_write,
8820486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8830486958fSJeff Layton 	.open		= nfs_file_open,
8840486958fSJeff Layton 	.flush		= nfs_file_flush,
8850486958fSJeff Layton 	.release	= nfs_file_release,
8860486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8870486958fSJeff Layton 	.lock		= nfs_lock,
8880486958fSJeff Layton 	.flock		= nfs_flock,
88982c156f8SAl Viro 	.splice_read	= generic_file_splice_read,
8904da54c21SAl Viro 	.splice_write	= iter_file_splice_write,
8910486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8921c994a09SJeff Layton 	.setlease	= simple_nosetlease,
8930486958fSJeff Layton };
894ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations);
895