xref: /openbmc/linux/fs/nfs/file.c (revision 66dabbb65d673aef40dd17bf62c042be8f6d4a4b)
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>
345970e15dSJeff Layton #include <linux/filelock.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include "delegation.h"
3794387fb1STrond Myklebust #include "internal.h"
3891d5b470SChuck Lever #include "iostat.h"
39545db45fSDavid Howells #include "fscache.h"
40612aa983SChristoph Hellwig #include "pnfs.h"
411da177e4SLinus Torvalds 
42f4ce1299STrond Myklebust #include "nfstrace.h"
43f4ce1299STrond Myklebust 
441da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
451da177e4SLinus Torvalds 
46f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops;
4794387fb1STrond Myklebust 
48ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
511da177e4SLinus Torvalds 		return -EINVAL;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	return 0;
541da177e4SLinus Torvalds }
5589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Open file
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds static int
611da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	int res;
641da177e4SLinus Torvalds 
656de1472fSAl Viro 	dprintk("NFS: open file(%pD2)\n", filp);
66cc0dd2d1SChuck Lever 
67c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
681da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
691da177e4SLinus Torvalds 	if (res)
701da177e4SLinus Torvalds 		return res;
711da177e4SLinus Torvalds 
7246cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
73a2ad63daSNeilBrown 	if (res == 0)
74a2ad63daSNeilBrown 		filp->f_mode |= FMODE_CAN_ODIRECT;
751da177e4SLinus Torvalds 	return res;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
78ce4ef7c0SBryan Schumaker int
791da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
801da177e4SLinus Torvalds {
816de1472fSAl Viro 	dprintk("NFS: release(%pD2)\n", filp);
826da24bc9SChuck Lever 
8391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
84aff8d8dcSAnna Schumaker 	nfs_file_clear_open_context(filp);
85a6b5a28eSDave Wysochanski 	nfs_fscache_release_file(inode, filp);
86aff8d8dcSAnna Schumaker 	return 0;
871da177e4SLinus Torvalds }
8889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release);
891da177e4SLinus Torvalds 
90980802e3STrond Myklebust /**
9137eaeed1STrond Myklebust  * nfs_revalidate_file_size - Revalidate the file size
92302fad7bSTrond Myklebust  * @inode: pointer to inode struct
93302fad7bSTrond Myklebust  * @filp: pointer to struct file
94980802e3STrond Myklebust  *
95980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
96980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
97980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
98980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
99980802e3STrond Myklebust  * shouldn't trust the cache).
100980802e3STrond Myklebust  */
101980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
102980802e3STrond Myklebust {
103980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
104d7cf8dd0STrond Myklebust 
105980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
106980802e3STrond Myklebust 		goto force_reval;
10713c0b082STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE))
108d7cf8dd0STrond Myklebust 		goto force_reval;
109fe51beecSTrond Myklebust 	return 0;
110980802e3STrond Myklebust force_reval:
111980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
112980802e3STrond Myklebust }
113980802e3STrond Myklebust 
114965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
115980802e3STrond Myklebust {
1166de1472fSAl Viro 	dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
1176de1472fSAl Viro 			filp, offset, whence);
118b84e06c5SChuck Lever 
11906222e49SJosef Bacik 	/*
120965c8e59SAndrew Morton 	 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
12106222e49SJosef Bacik 	 * the cached file length
12206222e49SJosef Bacik 	 */
123965c8e59SAndrew Morton 	if (whence != SEEK_SET && whence != SEEK_CUR) {
124980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
125d5e66348STrond Myklebust 
126980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
127980802e3STrond Myklebust 		if (retval < 0)
128980802e3STrond Myklebust 			return (loff_t)retval;
12979835a71SAndi Kleen 	}
130d5e66348STrond Myklebust 
131965c8e59SAndrew Morton 	return generic_file_llseek(filp, offset, whence);
132980802e3STrond Myklebust }
13389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek);
134980802e3STrond Myklebust 
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1371da177e4SLinus Torvalds  */
1385445b1fbSTrond Myklebust static int
13975e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1401da177e4SLinus Torvalds {
1416de1472fSAl Viro 	struct inode	*inode = file_inode(file);
14267dd23f9SScott Mayhew 	errseq_t since;
1431da177e4SLinus Torvalds 
1446de1472fSAl Viro 	dprintk("NFS: flush(%pD2)\n", file);
1451da177e4SLinus Torvalds 
146c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1471da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1481da177e4SLinus Torvalds 		return 0;
1497b159fc1STrond Myklebust 
1507fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
15167dd23f9SScott Mayhew 	since = filemap_sample_wb_err(file->f_mapping);
15267dd23f9SScott Mayhew 	nfs_wb_all(inode);
15367dd23f9SScott Mayhew 	return filemap_check_wb_err(file->f_mapping, since);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
156ce4ef7c0SBryan Schumaker ssize_t
1573aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
1581da177e4SLinus Torvalds {
1596de1472fSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
1601da177e4SLinus Torvalds 	ssize_t result;
1611da177e4SLinus Torvalds 
1622ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_DIRECT)
16364158668SNeilBrown 		return nfs_file_direct_read(iocb, to, false);
1641da177e4SLinus Torvalds 
165619d30b4SAl Viro 	dprintk("NFS: read(%pD2, %zu@%lu)\n",
1666de1472fSAl Viro 		iocb->ki_filp,
1673aa2d199SAl Viro 		iov_iter_count(to), (unsigned long) iocb->ki_pos);
1681da177e4SLinus Torvalds 
169a5864c99STrond Myklebust 	nfs_start_io_read(inode);
170a5864c99STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1714184dcf2SChuck Lever 	if (!result) {
1723aa2d199SAl Viro 		result = generic_file_read_iter(iocb, to);
1734184dcf2SChuck Lever 		if (result > 0)
1744184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1754184dcf2SChuck Lever 	}
176a5864c99STrond Myklebust 	nfs_end_io_read(inode);
1771da177e4SLinus Torvalds 	return result;
1781da177e4SLinus Torvalds }
17989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read);
1801da177e4SLinus Torvalds 
181ce4ef7c0SBryan Schumaker int
1821da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
1831da177e4SLinus Torvalds {
1846de1472fSAl Viro 	struct inode *inode = file_inode(file);
1851da177e4SLinus Torvalds 	int	status;
1861da177e4SLinus Torvalds 
1876de1472fSAl Viro 	dprintk("NFS: mmap(%pD2)\n", file);
1881da177e4SLinus Torvalds 
189e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
190e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
191e1ebfd33STrond Myklebust 	 */
192e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
19394387fb1STrond Myklebust 	if (!status) {
19494387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
195e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
19694387fb1STrond Myklebust 	}
1971da177e4SLinus Torvalds 	return status;
1981da177e4SLinus Torvalds }
19989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /*
2021da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2031da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2041da177e4SLinus Torvalds  * whether any write errors occurred for this process.
2051da177e4SLinus Torvalds  */
2064ff79bc7SChristoph Hellwig static int
207bf4b4905SNeilBrown nfs_file_fsync_commit(struct file *file, int datasync)
2081da177e4SLinus Torvalds {
2096de1472fSAl Viro 	struct inode *inode = file_inode(file);
2109641d9bcSTrond Myklebust 	int ret, ret2;
211af7fa165STrond Myklebust 
2126de1472fSAl Viro 	dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
2131da177e4SLinus Torvalds 
21491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
2152197e9b0STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
2169641d9bcSTrond Myklebust 	ret2 = file_check_and_advance_wb_err(file);
2179641d9bcSTrond Myklebust 	if (ret2 < 0)
2189641d9bcSTrond Myklebust 		return ret2;
219a5c58892SBryan Schumaker 	return ret;
220a5c58892SBryan Schumaker }
221a5c58892SBryan Schumaker 
2224ff79bc7SChristoph Hellwig int
223a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
224a5c58892SBryan Schumaker {
225496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
22667f4b5dcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
22767f4b5dcSTrond Myklebust 	long save_nredirtied = atomic_long_read(&nfsi->redirtied_pages);
22867f4b5dcSTrond Myklebust 	long nredirtied;
2292197e9b0STrond Myklebust 	int ret;
230a5c58892SBryan Schumaker 
231f4ce1299STrond Myklebust 	trace_nfs_fsync_enter(inode);
232f4ce1299STrond Myklebust 
2332197e9b0STrond Myklebust 	for (;;) {
2346fbda89bSTrond Myklebust 		ret = file_write_and_wait_range(file, start, end);
2357b281ee0STrond Myklebust 		if (ret != 0)
23605990d1bSTrond Myklebust 			break;
237bf4b4905SNeilBrown 		ret = nfs_file_fsync_commit(file, datasync);
2382197e9b0STrond Myklebust 		if (ret != 0)
2392197e9b0STrond Myklebust 			break;
2404ff79bc7SChristoph Hellwig 		ret = pnfs_sync_inode(inode, !!datasync);
2412197e9b0STrond Myklebust 		if (ret != 0)
2422197e9b0STrond Myklebust 			break;
24367f4b5dcSTrond Myklebust 		nredirtied = atomic_long_read(&nfsi->redirtied_pages);
24467f4b5dcSTrond Myklebust 		if (nredirtied == save_nredirtied)
2452197e9b0STrond Myklebust 			break;
24667f4b5dcSTrond Myklebust 		save_nredirtied = nredirtied;
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  */
28054d99381STrond Myklebust static bool nfs_folio_is_full_write(struct folio *folio, loff_t pos,
28154d99381STrond Myklebust 				    unsigned int len)
28238c73044SPeter Staubach {
28354d99381STrond Myklebust 	unsigned int pglen = nfs_folio_length(folio);
28454d99381STrond Myklebust 	unsigned int offset = offset_in_folio(folio, pos);
28538c73044SPeter Staubach 	unsigned int end = offset + len;
28638c73044SPeter Staubach 
2872cde04e9SKazuo Ito 	return !pglen || (end >= pglen && !offset);
288612aa983SChristoph Hellwig }
289612aa983SChristoph Hellwig 
29054d99381STrond Myklebust static bool nfs_want_read_modify_write(struct file *file, struct folio *folio,
2912cde04e9SKazuo Ito 				       loff_t pos, unsigned int len)
2922cde04e9SKazuo Ito {
2932cde04e9SKazuo Ito 	/*
2942cde04e9SKazuo Ito 	 * Up-to-date pages, those with ongoing or full-page write
2952cde04e9SKazuo Ito 	 * don't need read/modify/write
2962cde04e9SKazuo Ito 	 */
29754d99381STrond Myklebust 	if (folio_test_uptodate(folio) || folio_test_private(folio) ||
29854d99381STrond Myklebust 	    nfs_folio_is_full_write(folio, pos, len))
2992cde04e9SKazuo Ito 		return false;
3002cde04e9SKazuo Ito 
30154d99381STrond Myklebust 	if (pnfs_ld_read_whole_page(file_inode(file)))
3022cde04e9SKazuo Ito 		return true;
3032cde04e9SKazuo Ito 	/* Open for reading too? */
3042cde04e9SKazuo Ito 	if (file->f_mode & FMODE_READ)
3052cde04e9SKazuo Ito 		return true;
3062cde04e9SKazuo Ito 	return false;
30738c73044SPeter Staubach }
30838c73044SPeter Staubach 
30954d99381STrond Myklebust static struct folio *
31054d99381STrond Myklebust nfs_folio_grab_cache_write_begin(struct address_space *mapping, pgoff_t index)
31154d99381STrond Myklebust {
31254d99381STrond Myklebust 	unsigned fgp_flags = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE;
31354d99381STrond Myklebust 
31454d99381STrond Myklebust 	return __filemap_get_folio(mapping, index, fgp_flags,
31554d99381STrond Myklebust 				   mapping_gfp_mask(mapping));
31654d99381STrond Myklebust }
31754d99381STrond Myklebust 
31838c73044SPeter Staubach /*
3194899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3204899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3214899f9c8SNick Piggin  * data from user space.
3221da177e4SLinus Torvalds  *
3231da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3241da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3251da177e4SLinus Torvalds  */
3264899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
32754d99381STrond Myklebust 			   loff_t pos, unsigned len, struct page **pagep,
32854d99381STrond Myklebust 			   void **fsdata)
3291da177e4SLinus Torvalds {
3300c493b5cSTrond Myklebust 	struct folio *folio;
33138c73044SPeter Staubach 	int once_thru = 0;
33254d99381STrond Myklebust 	int ret;
3334899f9c8SNick Piggin 
3341e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
3356de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
336b7eaefaaSChuck Lever 
33738c73044SPeter Staubach start:
33854d99381STrond Myklebust 	folio = nfs_folio_grab_cache_write_begin(mapping, pos >> PAGE_SHIFT);
339*66dabbb6SChristoph Hellwig 	if (IS_ERR(folio))
340*66dabbb6SChristoph Hellwig 		return PTR_ERR(folio);
34154d99381STrond Myklebust 	*pagep = &folio->page;
3424899f9c8SNick Piggin 
3430c493b5cSTrond Myklebust 	ret = nfs_flush_incompatible(file, folio);
3444899f9c8SNick Piggin 	if (ret) {
34554d99381STrond Myklebust 		folio_unlock(folio);
34654d99381STrond Myklebust 		folio_put(folio);
34738c73044SPeter Staubach 	} else if (!once_thru &&
34854d99381STrond Myklebust 		   nfs_want_read_modify_write(file, folio, pos, len)) {
34938c73044SPeter Staubach 		once_thru = 1;
3500c493b5cSTrond Myklebust 		ret = nfs_read_folio(file, folio);
35154d99381STrond Myklebust 		folio_put(folio);
35238c73044SPeter Staubach 		if (!ret)
35338c73044SPeter Staubach 			goto start;
3544899f9c8SNick Piggin 	}
3554899f9c8SNick Piggin 	return ret;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3584899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3594899f9c8SNick Piggin 			 loff_t pos, unsigned len, unsigned copied,
3604899f9c8SNick Piggin 			 struct page *page, void *fsdata)
3611da177e4SLinus Torvalds {
362dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3630c493b5cSTrond Myklebust 	struct folio *folio = page_folio(page);
36454d99381STrond Myklebust 	unsigned offset = offset_in_folio(folio, pos);
3654899f9c8SNick Piggin 	int status;
3661da177e4SLinus Torvalds 
3671e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
3686de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
369b7eaefaaSChuck Lever 
370efc91ed0STrond Myklebust 	/*
371efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
372efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
373efc91ed0STrond Myklebust 	 */
37454d99381STrond Myklebust 	if (!folio_test_uptodate(folio)) {
37554d99381STrond Myklebust 		size_t fsize = folio_size(folio);
37654d99381STrond Myklebust 		unsigned pglen = nfs_folio_length(folio);
377c0cf3ef5SAl Viro 		unsigned end = offset + copied;
378efc91ed0STrond Myklebust 
379efc91ed0STrond Myklebust 		if (pglen == 0) {
38054d99381STrond Myklebust 			folio_zero_segments(folio, 0, offset, end, fsize);
38154d99381STrond Myklebust 			folio_mark_uptodate(folio);
382efc91ed0STrond Myklebust 		} else if (end >= pglen) {
38354d99381STrond Myklebust 			folio_zero_segment(folio, end, fsize);
384efc91ed0STrond Myklebust 			if (offset == 0)
38554d99381STrond Myklebust 				folio_mark_uptodate(folio);
386efc91ed0STrond Myklebust 		} else
38754d99381STrond Myklebust 			folio_zero_segment(folio, pglen, fsize);
388efc91ed0STrond Myklebust 	}
389efc91ed0STrond Myklebust 
3900c493b5cSTrond Myklebust 	status = nfs_update_folio(file, folio, offset, copied);
3914899f9c8SNick Piggin 
39254d99381STrond Myklebust 	folio_unlock(folio);
39354d99381STrond Myklebust 	folio_put(folio);
3944899f9c8SNick Piggin 
3953d509e54SChuck Lever 	if (status < 0)
3963d509e54SChuck Lever 		return status;
3972701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
398dc24826bSAndy Adamson 
399d95b2665STrond Myklebust 	if (nfs_ctx_key_to_expire(ctx, mapping->host))
400d95b2665STrond Myklebust 		nfs_wb_all(mapping->host);
401dc24826bSAndy Adamson 
4023d509e54SChuck Lever 	return copied;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4056b9b3514SDavid Howells /*
4066b9b3514SDavid Howells  * Partially or wholly invalidate a page
4076b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4086b9b3514SDavid Howells  *   page invalidation
409545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4106b9b3514SDavid Howells  * - Caller holds page lock
4116b9b3514SDavid Howells  */
4126d740c76SMatthew Wilcox (Oracle) static void nfs_invalidate_folio(struct folio *folio, size_t offset,
4136d740c76SMatthew Wilcox (Oracle) 				size_t length)
414cd52ed35STrond Myklebust {
415eb5654b3STrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
4166d740c76SMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n",
4176d740c76SMatthew Wilcox (Oracle) 		 folio->index, offset, length);
418b7eaefaaSChuck Lever 
4196d740c76SMatthew Wilcox (Oracle) 	if (offset != 0 || length < folio_size(folio))
4201c75950bSTrond Myklebust 		return;
421d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
422eb5654b3STrond Myklebust 	nfs_wb_folio_cancel(inode, folio);
4236d740c76SMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
424eb5654b3STrond Myklebust 	trace_nfs_invalidate_folio(inode, folio);
425cd52ed35STrond Myklebust }
426cd52ed35STrond Myklebust 
4276b9b3514SDavid Howells /*
4283577da4aSMatthew Wilcox (Oracle)  * Attempt to release the private state associated with a folio
4293577da4aSMatthew Wilcox (Oracle)  * - Called if either private or fscache flags are set on the folio
4303577da4aSMatthew Wilcox (Oracle)  * - Caller holds folio lock
4313577da4aSMatthew Wilcox (Oracle)  * - Return true (may release folio) or false (may not)
4326b9b3514SDavid Howells  */
4333577da4aSMatthew Wilcox (Oracle) static bool nfs_release_folio(struct folio *folio, gfp_t gfp)
434cd52ed35STrond Myklebust {
4353577da4aSMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: release_folio(%p)\n", folio);
436b7eaefaaSChuck Lever 
4373577da4aSMatthew Wilcox (Oracle) 	/* If the private flag is set, then the folio is not freeable */
43896780ca5STrond Myklebust 	if (folio_test_private(folio)) {
43996780ca5STrond Myklebust 		if ((current_gfp_context(gfp) & GFP_KERNEL) != GFP_KERNEL ||
44096780ca5STrond Myklebust 		    current_is_kswapd())
4413577da4aSMatthew Wilcox (Oracle) 			return false;
44296780ca5STrond Myklebust 		if (nfs_wb_folio(folio_file_mapping(folio)->host, folio) < 0)
44396780ca5STrond Myklebust 			return false;
44496780ca5STrond Myklebust 	}
4453577da4aSMatthew Wilcox (Oracle) 	return nfs_fscache_release_folio(folio, gfp);
446e3db7691STrond Myklebust }
447e3db7691STrond Myklebust 
448520f301cSMatthew Wilcox (Oracle) static void nfs_check_dirty_writeback(struct folio *folio,
449f919b196SMel Gorman 				bool *dirty, bool *writeback)
450f919b196SMel Gorman {
451f919b196SMel Gorman 	struct nfs_inode *nfsi;
452520f301cSMatthew Wilcox (Oracle) 	struct address_space *mapping = folio->mapping;
453f919b196SMel Gorman 
454f919b196SMel Gorman 	/*
455520f301cSMatthew Wilcox (Oracle) 	 * Check if an unstable folio is currently being committed and
456520f301cSMatthew Wilcox (Oracle) 	 * if so, have the VM treat it as if the folio is under writeback
457520f301cSMatthew Wilcox (Oracle) 	 * so it will not block due to folios that will shortly be freeable.
458f919b196SMel Gorman 	 */
459f919b196SMel Gorman 	nfsi = NFS_I(mapping->host);
460af7cf057STrond Myklebust 	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
461f919b196SMel Gorman 		*writeback = true;
462f919b196SMel Gorman 		return;
463f919b196SMel Gorman 	}
464f919b196SMel Gorman 
465f919b196SMel Gorman 	/*
466520f301cSMatthew Wilcox (Oracle) 	 * If the private flag is set, then the folio is not freeable
467520f301cSMatthew Wilcox (Oracle) 	 * and as the inode is not being committed, it's not going to
468520f301cSMatthew Wilcox (Oracle) 	 * be cleaned in the near future so treat it as dirty
469f919b196SMel Gorman 	 */
470520f301cSMatthew Wilcox (Oracle) 	if (folio_test_private(folio))
471f919b196SMel Gorman 		*dirty = true;
472f919b196SMel Gorman }
473f919b196SMel Gorman 
4746b9b3514SDavid Howells /*
4756b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4766b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4776b9b3514SDavid Howells  * destroyed
478545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4796b9b3514SDavid Howells  * - Caller holds page lock
4806b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4816b9b3514SDavid Howells  */
48215a30ab2SMatthew Wilcox (Oracle) static int nfs_launder_folio(struct folio *folio)
483e3db7691STrond Myklebust {
48415a30ab2SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
485eb5654b3STrond Myklebust 	int ret;
486b7eaefaaSChuck Lever 
48715a30ab2SMatthew Wilcox (Oracle) 	dfprintk(PAGECACHE, "NFS: launder_folio(%ld, %llu)\n",
48815a30ab2SMatthew Wilcox (Oracle) 		inode->i_ino, folio_pos(folio));
489b7eaefaaSChuck Lever 
49015a30ab2SMatthew Wilcox (Oracle) 	folio_wait_fscache(folio);
491eb5654b3STrond Myklebust 	ret = nfs_wb_folio(inode, folio);
492eb5654b3STrond Myklebust 	trace_nfs_launder_folio_done(inode, folio, ret);
493eb5654b3STrond Myklebust 	return ret;
494cd52ed35STrond Myklebust }
495cd52ed35STrond Myklebust 
496a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
497a564b8f0SMel Gorman 						sector_t *span)
498a564b8f0SMel Gorman {
499bd89bc67SMurphy Zhou 	unsigned long blocks;
500bd89bc67SMurphy Zhou 	long long isize;
5014b60c0ffSNeilBrown 	int ret;
5024dc73c67SNeilBrown 	struct inode *inode = file_inode(file);
5034dc73c67SNeilBrown 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
5044dc73c67SNeilBrown 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
505bd89bc67SMurphy Zhou 
506bd89bc67SMurphy Zhou 	spin_lock(&inode->i_lock);
507bd89bc67SMurphy Zhou 	blocks = inode->i_blocks;
508bd89bc67SMurphy Zhou 	isize = inode->i_size;
509bd89bc67SMurphy Zhou 	spin_unlock(&inode->i_lock);
510bd89bc67SMurphy Zhou 	if (blocks*512 < isize) {
511bd89bc67SMurphy Zhou 		pr_warn("swap activate: swapfile has holes\n");
512bd89bc67SMurphy Zhou 		return -EINVAL;
513bd89bc67SMurphy Zhou 	}
514dad2b015SJeff Layton 
5154b60c0ffSNeilBrown 	ret = rpc_clnt_swap_activate(clnt);
5164b60c0ffSNeilBrown 	if (ret)
5174b60c0ffSNeilBrown 		return ret;
5184b60c0ffSNeilBrown 	ret = add_swap_extent(sis, 0, sis->max, 0);
5194b60c0ffSNeilBrown 	if (ret < 0) {
5204b60c0ffSNeilBrown 		rpc_clnt_swap_deactivate(clnt);
5214b60c0ffSNeilBrown 		return ret;
5224b60c0ffSNeilBrown 	}
523dad2b015SJeff Layton 
5244b60c0ffSNeilBrown 	*span = sis->pages;
5254dc73c67SNeilBrown 
5264dc73c67SNeilBrown 	if (cl->rpc_ops->enable_swap)
5274dc73c67SNeilBrown 		cl->rpc_ops->enable_swap(inode);
5284dc73c67SNeilBrown 
5294b60c0ffSNeilBrown 	sis->flags |= SWP_FS_OPS;
5304b60c0ffSNeilBrown 	return ret;
531a564b8f0SMel Gorman }
532a564b8f0SMel Gorman 
533a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file)
534a564b8f0SMel Gorman {
5354dc73c67SNeilBrown 	struct inode *inode = file_inode(file);
5364dc73c67SNeilBrown 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
5374dc73c67SNeilBrown 	struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
538dad2b015SJeff Layton 
5393c87ef6eSJeff Layton 	rpc_clnt_swap_deactivate(clnt);
5404dc73c67SNeilBrown 	if (cl->rpc_ops->disable_swap)
5414dc73c67SNeilBrown 		cl->rpc_ops->disable_swap(file_inode(file));
542a564b8f0SMel Gorman }
543a564b8f0SMel Gorman 
544f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
54565d023afSMatthew Wilcox (Oracle) 	.read_folio = nfs_read_folio,
5468786fde8SMatthew Wilcox (Oracle) 	.readahead = nfs_readahead,
547187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio = filemap_dirty_folio,
5481da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5491da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5504899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5514899f9c8SNick Piggin 	.write_end = nfs_write_end,
5526d740c76SMatthew Wilcox (Oracle) 	.invalidate_folio = nfs_invalidate_folio,
5533577da4aSMatthew Wilcox (Oracle) 	.release_folio = nfs_release_folio,
5544ae84a80SMatthew Wilcox (Oracle) 	.migrate_folio = nfs_migrate_folio,
55515a30ab2SMatthew Wilcox (Oracle) 	.launder_folio = nfs_launder_folio,
556f919b196SMel Gorman 	.is_dirty_writeback = nfs_check_dirty_writeback,
557f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
558a564b8f0SMel Gorman 	.swap_activate = nfs_swap_activate,
559a564b8f0SMel Gorman 	.swap_deactivate = nfs_swap_deactivate,
560eb79f3afSNeilBrown 	.swap_rw = nfs_swap_rw,
5611da177e4SLinus Torvalds };
5621da177e4SLinus Torvalds 
5636b9b3514SDavid Howells /*
5646b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5656b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5666b9b3514SDavid Howells  * shared-writable mapping
5676b9b3514SDavid Howells  */
56801a36844SSouptick Joarder static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
56994387fb1STrond Myklebust {
57011bac800SDave Jiang 	struct file *filp = vmf->vma->vm_file;
5716de1472fSAl Viro 	struct inode *inode = file_inode(filp);
57294387fb1STrond Myklebust 	unsigned pagelen;
57301a36844SSouptick Joarder 	vm_fault_t ret = VM_FAULT_NOPAGE;
5744899f9c8SNick Piggin 	struct address_space *mapping;
5754fa7a717STrond Myklebust 	struct folio *folio = page_folio(vmf->page);
57694387fb1STrond Myklebust 
5771e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
5786de1472fSAl Viro 		 filp, filp->f_mapping->host->i_ino,
5794fa7a717STrond Myklebust 		 (long long)folio_file_pos(folio));
580b7eaefaaSChuck Lever 
5819a773e7cSTrond Myklebust 	sb_start_pagefault(inode->i_sb);
5829a773e7cSTrond Myklebust 
583545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
5844fa7a717STrond Myklebust 	if (folio_test_fscache(folio) &&
5854fa7a717STrond Myklebust 	    folio_wait_fscache_killable(folio) < 0) {
586a6b5a28eSDave Wysochanski 		ret = VM_FAULT_RETRY;
587a6b5a28eSDave Wysochanski 		goto out;
588a6b5a28eSDave Wysochanski 	}
589545db45fSDavid Howells 
590ef070dcbSTrond Myklebust 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
591f5d39b02SPeter Zijlstra 			   nfs_wait_bit_killable,
592f5d39b02SPeter Zijlstra 			   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
593ef070dcbSTrond Myklebust 
5944fa7a717STrond Myklebust 	folio_lock(folio);
5954fa7a717STrond Myklebust 	mapping = folio_file_mapping(folio);
5966de1472fSAl Viro 	if (mapping != inode->i_mapping)
5978b1f9ee5STrond Myklebust 		goto out_unlock;
5988b1f9ee5STrond Myklebust 
5994fa7a717STrond Myklebust 	folio_wait_writeback(folio);
6002aeb98f4STrond Myklebust 
6014fa7a717STrond Myklebust 	pagelen = nfs_folio_length(folio);
6028b1f9ee5STrond Myklebust 	if (pagelen == 0)
6038b1f9ee5STrond Myklebust 		goto out_unlock;
6048b1f9ee5STrond Myklebust 
605bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
6060c493b5cSTrond Myklebust 	if (nfs_flush_incompatible(filp, folio) == 0 &&
6070c493b5cSTrond Myklebust 	    nfs_update_folio(filp, folio, 0, pagelen) == 0)
608bc4866b6STrond Myklebust 		goto out;
6098b1f9ee5STrond Myklebust 
610bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
6118b1f9ee5STrond Myklebust out_unlock:
6124fa7a717STrond Myklebust 	folio_unlock(folio);
613bc4866b6STrond Myklebust out:
6149a773e7cSTrond Myklebust 	sb_end_pagefault(inode->i_sb);
615bc4866b6STrond Myklebust 	return ret;
61694387fb1STrond Myklebust }
61794387fb1STrond Myklebust 
618f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
61994387fb1STrond Myklebust 	.fault = filemap_fault,
620f1820361SKirill A. Shutemov 	.map_pages = filemap_map_pages,
62194387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
62294387fb1STrond Myklebust };
62394387fb1STrond Myklebust 
624edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
6251da177e4SLinus Torvalds {
6266de1472fSAl Viro 	struct file *file = iocb->ki_filp;
6276de1472fSAl Viro 	struct inode *inode = file_inode(file);
628ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
629ed7bcdb3STrond Myklebust 	ssize_t result, written;
630ce368536SScott Mayhew 	errseq_t since;
631ce368536SScott Mayhew 	int error;
6321da177e4SLinus Torvalds 
6336de1472fSAl Viro 	result = nfs_key_timeout_notify(file, inode);
634dc24826bSAndy Adamson 	if (result)
635dc24826bSAndy Adamson 		return result;
636dc24826bSAndy Adamson 
63789698b24STrond Myklebust 	if (iocb->ki_flags & IOCB_DIRECT)
63864158668SNeilBrown 		return nfs_file_direct_write(iocb, from, false);
6391da177e4SLinus Torvalds 
640619d30b4SAl Viro 	dprintk("NFS: write(%pD2, %zu@%Ld)\n",
64118290650STrond Myklebust 		file, iov_iter_count(from), (long long) iocb->ki_pos);
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6441da177e4SLinus Torvalds 		goto out_swapfile;
6457d52e862STrond Myklebust 	/*
6467d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6477d52e862STrond Myklebust 	 */
648fc9dc401STrond Myklebust 	if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) {
6496de1472fSAl Viro 		result = nfs_revalidate_file_size(inode, file);
6501da177e4SLinus Torvalds 		if (result)
651e6005436STrond Myklebust 			return result;
652fe51beecSTrond Myklebust 	}
6531da177e4SLinus Torvalds 
65428aa2f9eSTrond Myklebust 	nfs_clear_invalid_mapping(file->f_mapping);
65528aa2f9eSTrond Myklebust 
656ce368536SScott Mayhew 	since = filemap_sample_wb_err(file->f_mapping);
657a5864c99STrond Myklebust 	nfs_start_io_write(inode);
65818290650STrond Myklebust 	result = generic_write_checks(iocb, from);
65918290650STrond Myklebust 	if (result > 0) {
66018290650STrond Myklebust 		current->backing_dev_info = inode_to_bdi(inode);
661800ba295SMatthew Wilcox (Oracle) 		result = generic_perform_write(iocb, from);
66218290650STrond Myklebust 		current->backing_dev_info = NULL;
66318290650STrond Myklebust 	}
664a5864c99STrond Myklebust 	nfs_end_io_write(inode);
66518290650STrond Myklebust 	if (result <= 0)
6661da177e4SLinus Torvalds 		goto out;
6671da177e4SLinus Torvalds 
668c49edecdSTrond Myklebust 	written = result;
66918290650STrond Myklebust 	iocb->ki_pos += written;
670e6005436STrond Myklebust 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
671ed7bcdb3STrond Myklebust 
672ed7bcdb3STrond Myklebust 	if (mntflags & NFS_MOUNT_WRITE_EAGER) {
673ed7bcdb3STrond Myklebust 		result = filemap_fdatawrite_range(file->f_mapping,
674ed7bcdb3STrond Myklebust 						  iocb->ki_pos - written,
675ed7bcdb3STrond Myklebust 						  iocb->ki_pos - 1);
676ed7bcdb3STrond Myklebust 		if (result < 0)
677ed7bcdb3STrond Myklebust 			goto out;
678ed7bcdb3STrond Myklebust 	}
679ed7bcdb3STrond Myklebust 	if (mntflags & NFS_MOUNT_WRITE_WAIT) {
680384edeb4SLukas Bulwahn 		filemap_fdatawait_range(file->f_mapping,
681ed7bcdb3STrond Myklebust 					iocb->ki_pos - written,
682ed7bcdb3STrond Myklebust 					iocb->ki_pos - 1);
683ed7bcdb3STrond Myklebust 	}
684e973b1a5Starangg@amazon.com 	result = generic_write_sync(iocb, written);
685e973b1a5Starangg@amazon.com 	if (result < 0)
686e6005436STrond Myklebust 		return result;
6877e381172SChuck Lever 
688e6005436STrond Myklebust out:
6897e94d6c4STrond Myklebust 	/* Return error values */
690ce368536SScott Mayhew 	error = filemap_check_wb_err(file->f_mapping, since);
691e6005436STrond Myklebust 	switch (error) {
692e6005436STrond Myklebust 	default:
693e6005436STrond Myklebust 		break;
694e6005436STrond Myklebust 	case -EDQUOT:
695e6005436STrond Myklebust 	case -EFBIG:
696e6005436STrond Myklebust 	case -ENOSPC:
697e6005436STrond Myklebust 		nfs_wb_all(inode);
698e6005436STrond Myklebust 		error = file_check_and_advance_wb_err(file);
699e6005436STrond Myklebust 		if (error < 0)
700e6005436STrond Myklebust 			result = error;
701200baa21STrond Myklebust 	}
7021da177e4SLinus Torvalds 	return result;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds out_swapfile:
7051da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
70689658c4dSAnna Schumaker 	return -ETXTBSY;
7071da177e4SLinus Torvalds }
70889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write);
7091da177e4SLinus Torvalds 
7105eebde23SSuresh Jayaraman static int
7115eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7141da177e4SLinus Torvalds 	int status = 0;
71521ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
7161da177e4SLinus Torvalds 
717039c4d7aSTrond Myklebust 	/* Try local locking first */
7186d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
7196d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
7206d34ac19SJ. Bruce Fields 		/* found a conflict */
721039c4d7aSTrond Myklebust 		goto out;
7221da177e4SLinus Torvalds 	}
72321ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
724039c4d7aSTrond Myklebust 
725011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
726039c4d7aSTrond Myklebust 		goto out_noconflict;
727039c4d7aSTrond Myklebust 
7285eebde23SSuresh Jayaraman 	if (is_local)
729039c4d7aSTrond Myklebust 		goto out_noconflict;
730039c4d7aSTrond Myklebust 
731039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
732039c4d7aSTrond Myklebust out:
7331da177e4SLinus Torvalds 	return status;
734039c4d7aSTrond Myklebust out_noconflict:
735039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
736039c4d7aSTrond Myklebust 	goto out;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
7395eebde23SSuresh Jayaraman static int
7405eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7437a8203d8STrond Myklebust 	struct nfs_lock_context *l_ctx;
7441da177e4SLinus Torvalds 	int status;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	/*
7471da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7481da177e4SLinus Torvalds 	 * with locks..
7491da177e4SLinus Torvalds 	 */
750aded8d7bSTrond Myklebust 	nfs_wb_all(inode);
7511da177e4SLinus Torvalds 
7527a8203d8STrond Myklebust 	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
7537a8203d8STrond Myklebust 	if (!IS_ERR(l_ctx)) {
754210c7c17SBenjamin Coddington 		status = nfs_iocounter_wait(l_ctx);
7557a8203d8STrond Myklebust 		nfs_put_lock_context(l_ctx);
7561da177e4SLinus Torvalds 		/*  NOTE: special case
7571da177e4SLinus Torvalds 		 * 	If we're signalled while cleaning up locks on process exit, we
7581da177e4SLinus Torvalds 		 * 	still need to complete the unlock.
7591da177e4SLinus Torvalds 		 */
760f30cb757SBenjamin Coddington 		if (status < 0 && !(fl->fl_flags & FL_CLOSE))
761f30cb757SBenjamin Coddington 			return status;
762f30cb757SBenjamin Coddington 	}
763f30cb757SBenjamin Coddington 
7645eebde23SSuresh Jayaraman 	/*
7655eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7665eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7675eebde23SSuresh Jayaraman 	 */
7685eebde23SSuresh Jayaraman 	if (!is_local)
7691da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7701da177e4SLinus Torvalds 	else
77175575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7721da177e4SLinus Torvalds 	return status;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7755eebde23SSuresh Jayaraman static int
7765eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7791da177e4SLinus Torvalds 	int status;
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/*
7821da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7831da177e4SLinus Torvalds 	 * with locks..
7841da177e4SLinus Torvalds 	 */
78529884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
78629884df0STrond Myklebust 	if (status != 0)
7871da177e4SLinus Torvalds 		goto out;
7881da177e4SLinus Torvalds 
7895eebde23SSuresh Jayaraman 	/*
7905eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7915eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7925eebde23SSuresh Jayaraman 	 */
7935eebde23SSuresh Jayaraman 	if (!is_local)
7941da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
795c4d7c402STrond Myklebust 	else
79675575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7971da177e4SLinus Torvalds 	if (status < 0)
7981da177e4SLinus Torvalds 		goto out;
7996b96724eSRicardo Labiaga 
8001da177e4SLinus Torvalds 	/*
801779eafabSNeilBrown 	 * Invalidate cache to prevent missing any changes.  If
802779eafabSNeilBrown 	 * the file is mapped, clear the page cache as well so
803779eafabSNeilBrown 	 * those mappings will be loaded.
8046b96724eSRicardo Labiaga 	 *
8051da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
8061da177e4SLinus Torvalds 	 */
80729884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
808779eafabSNeilBrown 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
809442ce049SNeilBrown 		nfs_zap_caches(inode);
810779eafabSNeilBrown 		if (mapping_mapped(filp->f_mapping))
811779eafabSNeilBrown 			nfs_revalidate_mapping(inode, filp->f_mapping);
812779eafabSNeilBrown 	}
8131da177e4SLinus Torvalds out:
8141da177e4SLinus Torvalds 	return status;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /*
8181da177e4SLinus Torvalds  * Lock a (portion of) a file
8191da177e4SLinus Torvalds  */
820ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
8232116271aSTrond Myklebust 	int ret = -ENOLCK;
8245eebde23SSuresh Jayaraman 	int is_local = 0;
8251da177e4SLinus Torvalds 
8266de1472fSAl Viro 	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
8276de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags,
8281da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
8296da24bc9SChuck Lever 
83091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
8311da177e4SLinus Torvalds 
832bb0a55bbSJ. Bruce Fields 	if (fl->fl_flags & FL_RECLAIM)
833bb0a55bbSJ. Bruce Fields 		return -ENOGRACE;
834bb0a55bbSJ. Bruce Fields 
8355eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
8365eebde23SSuresh Jayaraman 		is_local = 1;
8375eebde23SSuresh Jayaraman 
8382116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
8392116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
8402116271aSTrond Myklebust 		if (ret < 0)
8412116271aSTrond Myklebust 			goto out_err;
8422116271aSTrond Myklebust 	}
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
8455eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
8462116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
8475eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
8482116271aSTrond Myklebust 	else
8495eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8502116271aSTrond Myklebust out_err:
8512116271aSTrond Myklebust 	return ret;
8521da177e4SLinus Torvalds }
85389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds /*
8561da177e4SLinus Torvalds  * Lock a (portion of) a file
8571da177e4SLinus Torvalds  */
858ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8591da177e4SLinus Torvalds {
8605eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8615eebde23SSuresh Jayaraman 	int is_local = 0;
8625eebde23SSuresh Jayaraman 
8636de1472fSAl Viro 	dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
8646de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8671da177e4SLinus Torvalds 		return -ENOLCK;
8681da177e4SLinus Torvalds 
8695eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8705eebde23SSuresh Jayaraman 		is_local = 1;
8715eebde23SSuresh Jayaraman 
872fcfa4470SBenjamin Coddington 	/* We're simulating flock() locks using posix locks on the server */
873fcfa4470SBenjamin Coddington 	if (fl->fl_type == F_UNLCK)
8745eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8755eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8761da177e4SLinus Torvalds }
87789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock);
878370f6599SJ. Bruce Fields 
8790486958fSJeff Layton const struct file_operations nfs_file_operations = {
8800486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8813aa2d199SAl Viro 	.read_iter	= nfs_file_read,
882edaf4369SAl Viro 	.write_iter	= nfs_file_write,
8830486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8840486958fSJeff Layton 	.open		= nfs_file_open,
8850486958fSJeff Layton 	.flush		= nfs_file_flush,
8860486958fSJeff Layton 	.release	= nfs_file_release,
8870486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8880486958fSJeff Layton 	.lock		= nfs_lock,
8890486958fSJeff Layton 	.flock		= nfs_flock,
89082c156f8SAl Viro 	.splice_read	= generic_file_splice_read,
8914da54c21SAl Viro 	.splice_write	= iter_file_splice_write,
8920486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8931c994a09SJeff Layton 	.setlease	= simple_nosetlease,
8940486958fSJeff Layton };
895ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations);
896