xref: /openbmc/linux/fs/nfs/file.c (revision 302fad7b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/file.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Changes Copyright (C) 1994 by Florian La Roche
71da177e4SLinus Torvalds  *   - Do not copy data too often around in the kernel.
81da177e4SLinus Torvalds  *   - In nfs_file_read the return value of kmalloc wasn't checked.
91da177e4SLinus Torvalds  *   - Put in a better version of read look-ahead buffering. Original idea
101da177e4SLinus Torvalds  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  Total rewrite of read side for new NFS buffer cache.. Linus.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  nfs regular file handling functions
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
19ddda8e0aSBryan Schumaker #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/fcntl.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/nfs_fs.h>
261da177e4SLinus Torvalds #include <linux/nfs_mount.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/pagemap.h>
295a0e3ad6STejun Heo #include <linux/gfp.h>
30b608b283STrond Myklebust #include <linux/swap.h>
311da177e4SLinus Torvalds 
327c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include "delegation.h"
3594387fb1STrond Myklebust #include "internal.h"
3691d5b470SChuck Lever #include "iostat.h"
37545db45fSDavid Howells #include "fscache.h"
38612aa983SChristoph Hellwig #include "pnfs.h"
391da177e4SLinus Torvalds 
40f4ce1299STrond Myklebust #include "nfstrace.h"
41f4ce1299STrond Myklebust 
421da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
431da177e4SLinus Torvalds 
44f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops;
4594387fb1STrond Myklebust 
461da177e4SLinus Torvalds /* Hack for future NFS swap support */
471da177e4SLinus Torvalds #ifndef IS_SWAPFILE
481da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
491da177e4SLinus Torvalds #endif
501da177e4SLinus Torvalds 
51ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
541da177e4SLinus Torvalds 		return -EINVAL;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	return 0;
571da177e4SLinus Torvalds }
5889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds  * Open file
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds static int
641da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	int res;
671da177e4SLinus Torvalds 
686de1472fSAl Viro 	dprintk("NFS: open file(%pD2)\n", filp);
69cc0dd2d1SChuck Lever 
70c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
711da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
721da177e4SLinus Torvalds 	if (res)
731da177e4SLinus Torvalds 		return res;
741da177e4SLinus Torvalds 
7546cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
761da177e4SLinus Torvalds 	return res;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
79ce4ef7c0SBryan Schumaker int
801da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
811da177e4SLinus Torvalds {
826de1472fSAl Viro 	dprintk("NFS: release(%pD2)\n", filp);
836da24bc9SChuck Lever 
8491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
85aff8d8dcSAnna Schumaker 	nfs_file_clear_open_context(filp);
86aff8d8dcSAnna Schumaker 	return 0;
871da177e4SLinus Torvalds }
8889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release);
891da177e4SLinus Torvalds 
90980802e3STrond Myklebust /**
91980802e3STrond Myklebust  * nfs_revalidate_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;
10761540bf6STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_REVAL_PAGECACHE))
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);
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 */
150af7fa165STrond Myklebust 	return vfs_fsync(file, 0);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153ce4ef7c0SBryan Schumaker ssize_t
1543aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
1551da177e4SLinus Torvalds {
1566de1472fSAl Viro 	struct inode *inode = file_inode(iocb->ki_filp);
1571da177e4SLinus Torvalds 	ssize_t result;
1581da177e4SLinus Torvalds 
1592ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_DIRECT)
160c8b8e32dSChristoph Hellwig 		return nfs_file_direct_read(iocb, to);
1611da177e4SLinus Torvalds 
162619d30b4SAl Viro 	dprintk("NFS: read(%pD2, %zu@%lu)\n",
1636de1472fSAl Viro 		iocb->ki_filp,
1643aa2d199SAl Viro 		iov_iter_count(to), (unsigned long) iocb->ki_pos);
1651da177e4SLinus Torvalds 
166a5864c99STrond Myklebust 	nfs_start_io_read(inode);
167a5864c99STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1684184dcf2SChuck Lever 	if (!result) {
1693aa2d199SAl Viro 		result = generic_file_read_iter(iocb, to);
1704184dcf2SChuck Lever 		if (result > 0)
1714184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1724184dcf2SChuck Lever 	}
173a5864c99STrond Myklebust 	nfs_end_io_read(inode);
1741da177e4SLinus Torvalds 	return result;
1751da177e4SLinus Torvalds }
17689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read);
1771da177e4SLinus Torvalds 
178ce4ef7c0SBryan Schumaker int
1791da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
1801da177e4SLinus Torvalds {
1816de1472fSAl Viro 	struct inode *inode = file_inode(file);
1821da177e4SLinus Torvalds 	int	status;
1831da177e4SLinus Torvalds 
1846de1472fSAl Viro 	dprintk("NFS: mmap(%pD2)\n", file);
1851da177e4SLinus Torvalds 
186e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
187e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
188e1ebfd33STrond Myklebust 	 */
189e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
19094387fb1STrond Myklebust 	if (!status) {
19194387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
192e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
19394387fb1STrond Myklebust 	}
1941da177e4SLinus Torvalds 	return status;
1951da177e4SLinus Torvalds }
19689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2001da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2011da177e4SLinus Torvalds  * whether any write errors occurred for this process.
202af7fa165STrond Myklebust  *
203af7fa165STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
204af7fa165STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
205af7fa165STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
206af7fa165STrond Myklebust  * This is because the former is used to notify the _next_ call to
20725985edcSLucas De Marchi  * nfs_file_write() that a write error occurred, and hence cause it to
208af7fa165STrond Myklebust  * fall back to doing a synchronous write.
2091da177e4SLinus Torvalds  */
2104ff79bc7SChristoph Hellwig static int
211bf4b4905SNeilBrown nfs_file_fsync_commit(struct file *file, int datasync)
2121da177e4SLinus Torvalds {
213cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2146de1472fSAl Viro 	struct inode *inode = file_inode(file);
215bf4b4905SNeilBrown 	int do_resend, status;
216af7fa165STrond Myklebust 	int ret = 0;
217af7fa165STrond Myklebust 
2186de1472fSAl Viro 	dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
2191da177e4SLinus Torvalds 
22091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
22105990d1bSTrond Myklebust 	do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
222af7fa165STrond Myklebust 	status = nfs_commit_inode(inode, FLUSH_SYNC);
223bf4b4905SNeilBrown 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) {
224af7fa165STrond Myklebust 		ret = xchg(&ctx->error, 0);
22505990d1bSTrond Myklebust 		if (ret)
22605990d1bSTrond Myklebust 			goto out;
22705990d1bSTrond Myklebust 	}
22805990d1bSTrond Myklebust 	if (status < 0) {
229af7fa165STrond Myklebust 		ret = status;
23005990d1bSTrond Myklebust 		goto out;
23105990d1bSTrond Myklebust 	}
23205990d1bSTrond Myklebust 	do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
23305990d1bSTrond Myklebust 	if (do_resend)
23405990d1bSTrond Myklebust 		ret = -EAGAIN;
23505990d1bSTrond Myklebust out:
236a5c58892SBryan Schumaker 	return ret;
237a5c58892SBryan Schumaker }
238a5c58892SBryan Schumaker 
2394ff79bc7SChristoph Hellwig int
240a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
241a5c58892SBryan Schumaker {
242a5c58892SBryan Schumaker 	int ret;
243496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
244a5c58892SBryan Schumaker 
245f4ce1299STrond Myklebust 	trace_nfs_fsync_enter(inode);
246f4ce1299STrond Myklebust 
24705990d1bSTrond Myklebust 	do {
248bf4b4905SNeilBrown 		struct nfs_open_context *ctx = nfs_file_open_context(file);
249a5c58892SBryan Schumaker 		ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
250bf4b4905SNeilBrown 		if (test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) {
251bf4b4905SNeilBrown 			int ret2 = xchg(&ctx->error, 0);
252bf4b4905SNeilBrown 			if (ret2)
253bf4b4905SNeilBrown 				ret = ret2;
254bf4b4905SNeilBrown 		}
2557b281ee0STrond Myklebust 		if (ret != 0)
25605990d1bSTrond Myklebust 			break;
257bf4b4905SNeilBrown 		ret = nfs_file_fsync_commit(file, datasync);
2584ff79bc7SChristoph Hellwig 		if (!ret)
2594ff79bc7SChristoph Hellwig 			ret = pnfs_sync_inode(inode, !!datasync);
260dcfc4f25STrond Myklebust 		/*
261dcfc4f25STrond Myklebust 		 * If nfs_file_fsync_commit detected a server reboot, then
262dcfc4f25STrond Myklebust 		 * resend all dirty pages that might have been covered by
263dcfc4f25STrond Myklebust 		 * the NFS_CONTEXT_RESEND_WRITES flag
264dcfc4f25STrond Myklebust 		 */
265dcfc4f25STrond Myklebust 		start = 0;
266dcfc4f25STrond Myklebust 		end = LLONG_MAX;
26705990d1bSTrond Myklebust 	} while (ret == -EAGAIN);
26805990d1bSTrond Myklebust 
269f4ce1299STrond Myklebust 	trace_nfs_fsync_exit(inode, ret);
270af7fa165STrond Myklebust 	return ret;
2711da177e4SLinus Torvalds }
2724ff79bc7SChristoph Hellwig EXPORT_SYMBOL_GPL(nfs_file_fsync);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds /*
27538c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
27638c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
27738c73044SPeter Staubach  * page cache.
27838c73044SPeter Staubach  *
27938c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
28038c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
28138c73044SPeter Staubach  * page must be completely written to stable storage on the server
28238c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
28338c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
28438c73044SPeter Staubach  * done.
28538c73044SPeter Staubach  *
28638c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
28738c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
28838c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
28938c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
29038c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
29138c73044SPeter Staubach  * and that the new data won't completely replace the old data in
29238c73044SPeter Staubach  * that range of the file.
29338c73044SPeter Staubach  */
29438c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page,
29538c73044SPeter Staubach 			loff_t pos, unsigned len)
29638c73044SPeter Staubach {
29738c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
29809cbfeafSKirill A. Shutemov 	unsigned int offset = pos & (PAGE_SIZE - 1);
29938c73044SPeter Staubach 	unsigned int end = offset + len;
30038c73044SPeter Staubach 
301612aa983SChristoph Hellwig 	if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
302612aa983SChristoph Hellwig 		if (!PageUptodate(page))
303612aa983SChristoph Hellwig 			return 1;
304612aa983SChristoph Hellwig 		return 0;
305612aa983SChristoph Hellwig 	}
306612aa983SChristoph Hellwig 
30738c73044SPeter Staubach 	if ((file->f_mode & FMODE_READ) &&	/* open for read? */
30838c73044SPeter Staubach 	    !PageUptodate(page) &&		/* Uptodate? */
30938c73044SPeter Staubach 	    !PagePrivate(page) &&		/* i/o request already? */
31038c73044SPeter Staubach 	    pglen &&				/* valid bytes of file? */
31138c73044SPeter Staubach 	    (end < pglen || offset))		/* replace all valid bytes? */
31238c73044SPeter Staubach 		return 1;
31338c73044SPeter Staubach 	return 0;
31438c73044SPeter Staubach }
31538c73044SPeter Staubach 
31638c73044SPeter Staubach /*
3174899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3184899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3194899f9c8SNick Piggin  * data from user space.
3201da177e4SLinus Torvalds  *
3211da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3221da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3231da177e4SLinus Torvalds  */
3244899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3254899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3264899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3271da177e4SLinus Torvalds {
3284899f9c8SNick Piggin 	int ret;
32909cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
3304899f9c8SNick Piggin 	struct page *page;
33138c73044SPeter Staubach 	int once_thru = 0;
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:
33754566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3384899f9c8SNick Piggin 	if (!page)
3394899f9c8SNick Piggin 		return -ENOMEM;
3404899f9c8SNick Piggin 	*pagep = page;
3414899f9c8SNick Piggin 
3424899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3434899f9c8SNick Piggin 	if (ret) {
3444899f9c8SNick Piggin 		unlock_page(page);
34509cbfeafSKirill A. Shutemov 		put_page(page);
34638c73044SPeter Staubach 	} else if (!once_thru &&
34738c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
34838c73044SPeter Staubach 		once_thru = 1;
34938c73044SPeter Staubach 		ret = nfs_readpage(file, page);
35009cbfeafSKirill A. Shutemov 		put_page(page);
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 {
36109cbfeafSKirill A. Shutemov 	unsigned offset = pos & (PAGE_SIZE - 1);
362dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3634899f9c8SNick Piggin 	int status;
3641da177e4SLinus Torvalds 
3651e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
3666de1472fSAl Viro 		file, mapping->host->i_ino, len, (long long) pos);
367b7eaefaaSChuck Lever 
368efc91ed0STrond Myklebust 	/*
369efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
370efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
371efc91ed0STrond Myklebust 	 */
372efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
373efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
374c0cf3ef5SAl Viro 		unsigned end = offset + copied;
375efc91ed0STrond Myklebust 
376efc91ed0STrond Myklebust 		if (pglen == 0) {
377efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
37809cbfeafSKirill A. Shutemov 					end, PAGE_SIZE);
379efc91ed0STrond Myklebust 			SetPageUptodate(page);
380efc91ed0STrond Myklebust 		} else if (end >= pglen) {
38109cbfeafSKirill A. Shutemov 			zero_user_segment(page, end, PAGE_SIZE);
382efc91ed0STrond Myklebust 			if (offset == 0)
383efc91ed0STrond Myklebust 				SetPageUptodate(page);
384efc91ed0STrond Myklebust 		} else
38509cbfeafSKirill A. Shutemov 			zero_user_segment(page, pglen, PAGE_SIZE);
386efc91ed0STrond Myklebust 	}
387efc91ed0STrond Myklebust 
3884899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
3894899f9c8SNick Piggin 
3904899f9c8SNick Piggin 	unlock_page(page);
39109cbfeafSKirill A. Shutemov 	put_page(page);
3924899f9c8SNick Piggin 
3933d509e54SChuck Lever 	if (status < 0)
3943d509e54SChuck Lever 		return status;
3952701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
396dc24826bSAndy Adamson 
397ce52914eSScott Mayhew 	if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
398dc24826bSAndy Adamson 		status = nfs_wb_all(mapping->host);
399dc24826bSAndy Adamson 		if (status < 0)
400dc24826bSAndy Adamson 			return status;
401dc24826bSAndy Adamson 	}
402dc24826bSAndy Adamson 
4033d509e54SChuck Lever 	return copied;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4066b9b3514SDavid Howells /*
4076b9b3514SDavid Howells  * Partially or wholly invalidate a page
4086b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4096b9b3514SDavid Howells  *   page invalidation
410545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4116b9b3514SDavid Howells  * - Caller holds page lock
4126b9b3514SDavid Howells  */
413d47992f8SLukas Czerner static void nfs_invalidate_page(struct page *page, unsigned int offset,
414d47992f8SLukas Czerner 				unsigned int length)
415cd52ed35STrond Myklebust {
416d47992f8SLukas Czerner 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
417d47992f8SLukas Czerner 		 page, offset, length);
418b7eaefaaSChuck Lever 
41909cbfeafSKirill A. Shutemov 	if (offset != 0 || length < PAGE_SIZE)
4201c75950bSTrond Myklebust 		return;
421d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
422d56b4ddfSMel Gorman 	nfs_wb_page_cancel(page_file_mapping(page)->host, page);
423545db45fSDavid Howells 
424545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
425cd52ed35STrond Myklebust }
426cd52ed35STrond Myklebust 
4276b9b3514SDavid Howells /*
4286b9b3514SDavid Howells  * Attempt to release the private state associated with a page
429545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4306b9b3514SDavid Howells  * - Caller holds page lock
4316b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4326b9b3514SDavid Howells  */
433cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
434cd52ed35STrond Myklebust {
435b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
436b7eaefaaSChuck Lever 
437e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
438545db45fSDavid Howells 	if (PagePrivate(page))
439ddeff520SNikita Danilov 		return 0;
440545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
441e3db7691STrond Myklebust }
442e3db7691STrond Myklebust 
443f919b196SMel Gorman static void nfs_check_dirty_writeback(struct page *page,
444f919b196SMel Gorman 				bool *dirty, bool *writeback)
445f919b196SMel Gorman {
446f919b196SMel Gorman 	struct nfs_inode *nfsi;
447f919b196SMel Gorman 	struct address_space *mapping = page_file_mapping(page);
448f919b196SMel Gorman 
449f919b196SMel Gorman 	if (!mapping || PageSwapCache(page))
450f919b196SMel Gorman 		return;
451f919b196SMel Gorman 
452f919b196SMel Gorman 	/*
453f919b196SMel Gorman 	 * Check if an unstable page is currently being committed and
454f919b196SMel Gorman 	 * if so, have the VM treat it as if the page is under writeback
455f919b196SMel Gorman 	 * so it will not block due to pages that will shortly be freeable.
456f919b196SMel Gorman 	 */
457f919b196SMel Gorman 	nfsi = NFS_I(mapping->host);
458af7cf057STrond Myklebust 	if (atomic_read(&nfsi->commit_info.rpcs_out)) {
459f919b196SMel Gorman 		*writeback = true;
460f919b196SMel Gorman 		return;
461f919b196SMel Gorman 	}
462f919b196SMel Gorman 
463f919b196SMel Gorman 	/*
464f919b196SMel Gorman 	 * If PagePrivate() is set, then the page is not freeable and as the
465f919b196SMel Gorman 	 * inode is not being committed, it's not going to be cleaned in the
466f919b196SMel Gorman 	 * near future so treat it as dirty
467f919b196SMel Gorman 	 */
468f919b196SMel Gorman 	if (PagePrivate(page))
469f919b196SMel Gorman 		*dirty = true;
470f919b196SMel Gorman }
471f919b196SMel Gorman 
4726b9b3514SDavid Howells /*
4736b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4746b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4756b9b3514SDavid Howells  * destroyed
476545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4776b9b3514SDavid Howells  * - Caller holds page lock
4786b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4796b9b3514SDavid Howells  */
480e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
481e3db7691STrond Myklebust {
482d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
483545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
484b7eaefaaSChuck Lever 
485b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
486b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
487b7eaefaaSChuck Lever 
488545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
489c373fff7STrond Myklebust 	return nfs_wb_page(inode, page);
490cd52ed35STrond Myklebust }
491cd52ed35STrond Myklebust 
492a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
493a564b8f0SMel Gorman 						sector_t *span)
494a564b8f0SMel Gorman {
495dad2b015SJeff Layton 	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
496dad2b015SJeff Layton 
497a564b8f0SMel Gorman 	*span = sis->pages;
498dad2b015SJeff Layton 
4993c87ef6eSJeff Layton 	return rpc_clnt_swap_activate(clnt);
500a564b8f0SMel Gorman }
501a564b8f0SMel Gorman 
502a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file)
503a564b8f0SMel Gorman {
504dad2b015SJeff Layton 	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
505dad2b015SJeff Layton 
5063c87ef6eSJeff Layton 	rpc_clnt_swap_deactivate(clnt);
507a564b8f0SMel Gorman }
508a564b8f0SMel Gorman 
509f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5101da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5111da177e4SLinus Torvalds 	.readpages = nfs_readpages,
5129cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
5131da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5141da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5154899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5164899f9c8SNick Piggin 	.write_end = nfs_write_end,
517cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
518cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5191da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
520f844cd0dSChao Yu #ifdef CONFIG_MIGRATION
521074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
522f844cd0dSChao Yu #endif
523e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
524f919b196SMel Gorman 	.is_dirty_writeback = nfs_check_dirty_writeback,
525f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
526a564b8f0SMel Gorman 	.swap_activate = nfs_swap_activate,
527a564b8f0SMel Gorman 	.swap_deactivate = nfs_swap_deactivate,
5281da177e4SLinus Torvalds };
5291da177e4SLinus Torvalds 
5306b9b3514SDavid Howells /*
5316b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5326b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5336b9b3514SDavid Howells  * shared-writable mapping
5346b9b3514SDavid Howells  */
53501a36844SSouptick Joarder static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
53694387fb1STrond Myklebust {
537c2ec175cSNick Piggin 	struct page *page = vmf->page;
53811bac800SDave Jiang 	struct file *filp = vmf->vma->vm_file;
5396de1472fSAl Viro 	struct inode *inode = file_inode(filp);
54094387fb1STrond Myklebust 	unsigned pagelen;
54101a36844SSouptick Joarder 	vm_fault_t ret = VM_FAULT_NOPAGE;
5424899f9c8SNick Piggin 	struct address_space *mapping;
54394387fb1STrond Myklebust 
5441e8968c5SNiels de Vos 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
5456de1472fSAl Viro 		filp, filp->f_mapping->host->i_ino,
546b7eaefaaSChuck Lever 		(long long)page_offset(page));
547b7eaefaaSChuck Lever 
5489a773e7cSTrond Myklebust 	sb_start_pagefault(inode->i_sb);
5499a773e7cSTrond Myklebust 
550545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
5516de1472fSAl Viro 	nfs_fscache_wait_on_page_write(NFS_I(inode), page);
552545db45fSDavid Howells 
553ef070dcbSTrond Myklebust 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
554ef070dcbSTrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
555ef070dcbSTrond Myklebust 
55694387fb1STrond Myklebust 	lock_page(page);
557d56b4ddfSMel Gorman 	mapping = page_file_mapping(page);
5586de1472fSAl Viro 	if (mapping != inode->i_mapping)
5598b1f9ee5STrond Myklebust 		goto out_unlock;
5608b1f9ee5STrond Myklebust 
5612aeb98f4STrond Myklebust 	wait_on_page_writeback(page);
5622aeb98f4STrond Myklebust 
5634899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5648b1f9ee5STrond Myklebust 	if (pagelen == 0)
5658b1f9ee5STrond Myklebust 		goto out_unlock;
5668b1f9ee5STrond Myklebust 
567bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
568bc4866b6STrond Myklebust 	if (nfs_flush_incompatible(filp, page) == 0 &&
569bc4866b6STrond Myklebust 	    nfs_updatepage(filp, page, 0, pagelen) == 0)
570bc4866b6STrond Myklebust 		goto out;
5718b1f9ee5STrond Myklebust 
572bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
5738b1f9ee5STrond Myklebust out_unlock:
5744899f9c8SNick Piggin 	unlock_page(page);
575bc4866b6STrond Myklebust out:
5769a773e7cSTrond Myklebust 	sb_end_pagefault(inode->i_sb);
577bc4866b6STrond Myklebust 	return ret;
57894387fb1STrond Myklebust }
57994387fb1STrond Myklebust 
580f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
58194387fb1STrond Myklebust 	.fault = filemap_fault,
582f1820361SKirill A. Shutemov 	.map_pages = filemap_map_pages,
58394387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
58494387fb1STrond Myklebust };
58594387fb1STrond Myklebust 
5867e94d6c4STrond Myklebust static int nfs_need_check_write(struct file *filp, struct inode *inode)
5877b159fc1STrond Myklebust {
5887b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5897b159fc1STrond Myklebust 
590cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
591dc24826bSAndy Adamson 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
592ce52914eSScott Mayhew 	    nfs_ctx_key_to_expire(ctx, inode))
5937b159fc1STrond Myklebust 		return 1;
5947b159fc1STrond Myklebust 	return 0;
5957b159fc1STrond Myklebust }
5967b159fc1STrond Myklebust 
597edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
5981da177e4SLinus Torvalds {
5996de1472fSAl Viro 	struct file *file = iocb->ki_filp;
6006de1472fSAl Viro 	struct inode *inode = file_inode(file);
6017e381172SChuck Lever 	unsigned long written = 0;
6021da177e4SLinus Torvalds 	ssize_t result;
6031da177e4SLinus Torvalds 
6046de1472fSAl Viro 	result = nfs_key_timeout_notify(file, inode);
605dc24826bSAndy Adamson 	if (result)
606dc24826bSAndy Adamson 		return result;
607dc24826bSAndy Adamson 
60889698b24STrond Myklebust 	if (iocb->ki_flags & IOCB_DIRECT)
60965a4a1caSAl Viro 		return nfs_file_direct_write(iocb, from);
6101da177e4SLinus Torvalds 
611619d30b4SAl Viro 	dprintk("NFS: write(%pD2, %zu@%Ld)\n",
61218290650STrond Myklebust 		file, iov_iter_count(from), (long long) iocb->ki_pos);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6151da177e4SLinus Torvalds 		goto out_swapfile;
6167d52e862STrond Myklebust 	/*
6177d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6187d52e862STrond Myklebust 	 */
6192ba48ce5SAl Viro 	if (iocb->ki_flags & IOCB_APPEND) {
6206de1472fSAl Viro 		result = nfs_revalidate_file_size(inode, file);
6211da177e4SLinus Torvalds 		if (result)
6221da177e4SLinus Torvalds 			goto out;
623fe51beecSTrond Myklebust 	}
6246ba80d43SNeilBrown 	if (iocb->ki_pos > i_size_read(inode))
6256ba80d43SNeilBrown 		nfs_revalidate_mapping(inode, file->f_mapping);
6261da177e4SLinus Torvalds 
627a5864c99STrond Myklebust 	nfs_start_io_write(inode);
62818290650STrond Myklebust 	result = generic_write_checks(iocb, from);
62918290650STrond Myklebust 	if (result > 0) {
63018290650STrond Myklebust 		current->backing_dev_info = inode_to_bdi(inode);
63118290650STrond Myklebust 		result = generic_perform_write(file, from, iocb->ki_pos);
63218290650STrond Myklebust 		current->backing_dev_info = NULL;
63318290650STrond Myklebust 	}
634a5864c99STrond Myklebust 	nfs_end_io_write(inode);
63518290650STrond Myklebust 	if (result <= 0)
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 
638c49edecdSTrond Myklebust 	written = result;
63918290650STrond Myklebust 	iocb->ki_pos += written;
640e973b1a5Starangg@amazon.com 	result = generic_write_sync(iocb, written);
641e973b1a5Starangg@amazon.com 	if (result < 0)
642e973b1a5Starangg@amazon.com 		goto out;
6437e381172SChuck Lever 
6447e94d6c4STrond Myklebust 	/* Return error values */
64518290650STrond Myklebust 	if (nfs_need_check_write(file, inode)) {
6466de1472fSAl Viro 		int err = vfs_fsync(file, 0);
647200baa21STrond Myklebust 		if (err < 0)
648200baa21STrond Myklebust 			result = err;
649200baa21STrond Myklebust 	}
6507e381172SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
6511da177e4SLinus Torvalds out:
6521da177e4SLinus Torvalds 	return result;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds out_swapfile:
6551da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
65618290650STrond Myklebust 	return -EBUSY;
6571da177e4SLinus Torvalds }
65889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write);
6591da177e4SLinus Torvalds 
6605eebde23SSuresh Jayaraman static int
6615eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6641da177e4SLinus Torvalds 	int status = 0;
66521ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
6661da177e4SLinus Torvalds 
667039c4d7aSTrond Myklebust 	/* Try local locking first */
6686d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6696d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6706d34ac19SJ. Bruce Fields 		/* found a conflict */
671039c4d7aSTrond Myklebust 		goto out;
6721da177e4SLinus Torvalds 	}
67321ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
674039c4d7aSTrond Myklebust 
675011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
676039c4d7aSTrond Myklebust 		goto out_noconflict;
677039c4d7aSTrond Myklebust 
6785eebde23SSuresh Jayaraman 	if (is_local)
679039c4d7aSTrond Myklebust 		goto out_noconflict;
680039c4d7aSTrond Myklebust 
681039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
682039c4d7aSTrond Myklebust out:
6831da177e4SLinus Torvalds 	return status;
684039c4d7aSTrond Myklebust out_noconflict:
685039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
686039c4d7aSTrond Myklebust 	goto out;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6895eebde23SSuresh Jayaraman static int
6905eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6937a8203d8STrond Myklebust 	struct nfs_lock_context *l_ctx;
6941da177e4SLinus Torvalds 	int status;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/*
6971da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
6981da177e4SLinus Torvalds 	 * with locks..
6991da177e4SLinus Torvalds 	 */
700d9dabc1aSTrond Myklebust 	vfs_fsync(filp, 0);
7011da177e4SLinus Torvalds 
7027a8203d8STrond Myklebust 	l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
7037a8203d8STrond Myklebust 	if (!IS_ERR(l_ctx)) {
704210c7c17SBenjamin Coddington 		status = nfs_iocounter_wait(l_ctx);
7057a8203d8STrond Myklebust 		nfs_put_lock_context(l_ctx);
7061da177e4SLinus Torvalds 		/*  NOTE: special case
7071da177e4SLinus Torvalds 		 * 	If we're signalled while cleaning up locks on process exit, we
7081da177e4SLinus Torvalds 		 * 	still need to complete the unlock.
7091da177e4SLinus Torvalds 		 */
710f30cb757SBenjamin Coddington 		if (status < 0 && !(fl->fl_flags & FL_CLOSE))
711f30cb757SBenjamin Coddington 			return status;
712f30cb757SBenjamin Coddington 	}
713f30cb757SBenjamin Coddington 
7145eebde23SSuresh Jayaraman 	/*
7155eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7165eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7175eebde23SSuresh Jayaraman 	 */
7185eebde23SSuresh Jayaraman 	if (!is_local)
7191da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7201da177e4SLinus Torvalds 	else
72175575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7221da177e4SLinus Torvalds 	return status;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7255eebde23SSuresh Jayaraman static int
7265eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7291da177e4SLinus Torvalds 	int status;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	/*
7321da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7331da177e4SLinus Torvalds 	 * with locks..
7341da177e4SLinus Torvalds 	 */
73529884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
73629884df0STrond Myklebust 	if (status != 0)
7371da177e4SLinus Torvalds 		goto out;
7381da177e4SLinus Torvalds 
7395eebde23SSuresh Jayaraman 	/*
7405eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7415eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7425eebde23SSuresh Jayaraman 	 */
7435eebde23SSuresh Jayaraman 	if (!is_local)
7441da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
745c4d7c402STrond Myklebust 	else
74675575ddfSJeff Layton 		status = locks_lock_file_wait(filp, fl);
7471da177e4SLinus Torvalds 	if (status < 0)
7481da177e4SLinus Torvalds 		goto out;
7496b96724eSRicardo Labiaga 
7501da177e4SLinus Torvalds 	/*
751779eafabSNeilBrown 	 * Invalidate cache to prevent missing any changes.  If
752779eafabSNeilBrown 	 * the file is mapped, clear the page cache as well so
753779eafabSNeilBrown 	 * those mappings will be loaded.
7546b96724eSRicardo Labiaga 	 *
7551da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7561da177e4SLinus Torvalds 	 */
75729884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
758779eafabSNeilBrown 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
759442ce049SNeilBrown 		nfs_zap_caches(inode);
760779eafabSNeilBrown 		if (mapping_mapped(filp->f_mapping))
761779eafabSNeilBrown 			nfs_revalidate_mapping(inode, filp->f_mapping);
762779eafabSNeilBrown 	}
7631da177e4SLinus Torvalds out:
7641da177e4SLinus Torvalds 	return status;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds /*
7681da177e4SLinus Torvalds  * Lock a (portion of) a file
7691da177e4SLinus Torvalds  */
770ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7732116271aSTrond Myklebust 	int ret = -ENOLCK;
7745eebde23SSuresh Jayaraman 	int is_local = 0;
7751da177e4SLinus Torvalds 
7766de1472fSAl Viro 	dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
7776de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags,
7781da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7796da24bc9SChuck Lever 
78091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
783dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
7842116271aSTrond Myklebust 		goto out_err;
7852116271aSTrond Myklebust 
7865eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
7875eebde23SSuresh Jayaraman 		is_local = 1;
7885eebde23SSuresh Jayaraman 
7892116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
7902116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
7912116271aSTrond Myklebust 		if (ret < 0)
7922116271aSTrond Myklebust 			goto out_err;
7932116271aSTrond Myklebust 	}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
7965eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
7972116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
7985eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
7992116271aSTrond Myklebust 	else
8005eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8012116271aSTrond Myklebust out_err:
8022116271aSTrond Myklebust 	return ret;
8031da177e4SLinus Torvalds }
80489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock);
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /*
8071da177e4SLinus Torvalds  * Lock a (portion of) a file
8081da177e4SLinus Torvalds  */
809ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8101da177e4SLinus Torvalds {
8115eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8125eebde23SSuresh Jayaraman 	int is_local = 0;
8135eebde23SSuresh Jayaraman 
8146de1472fSAl Viro 	dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
8156de1472fSAl Viro 			filp, fl->fl_type, fl->fl_flags);
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8181da177e4SLinus Torvalds 		return -ENOLCK;
8191da177e4SLinus Torvalds 
820ad0fcd4eSJeff Layton 	/*
821ad0fcd4eSJeff Layton 	 * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
822ad0fcd4eSJeff Layton 	 * any standard. In principle we might be able to support LOCK_MAND
823ad0fcd4eSJeff Layton 	 * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
824ad0fcd4eSJeff Layton 	 * NFS code is not set up for it.
825ad0fcd4eSJeff Layton 	 */
826ad0fcd4eSJeff Layton 	if (fl->fl_type & LOCK_MAND)
827ad0fcd4eSJeff Layton 		return -EINVAL;
828ad0fcd4eSJeff Layton 
8295eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8305eebde23SSuresh Jayaraman 		is_local = 1;
8315eebde23SSuresh Jayaraman 
832fcfa4470SBenjamin Coddington 	/* We're simulating flock() locks using posix locks on the server */
833fcfa4470SBenjamin Coddington 	if (fl->fl_type == F_UNLCK)
8345eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8355eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8361da177e4SLinus Torvalds }
83789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock);
838370f6599SJ. Bruce Fields 
8390486958fSJeff Layton const struct file_operations nfs_file_operations = {
8400486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8413aa2d199SAl Viro 	.read_iter	= nfs_file_read,
842edaf4369SAl Viro 	.write_iter	= nfs_file_write,
8430486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8440486958fSJeff Layton 	.open		= nfs_file_open,
8450486958fSJeff Layton 	.flush		= nfs_file_flush,
8460486958fSJeff Layton 	.release	= nfs_file_release,
8470486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8480486958fSJeff Layton 	.lock		= nfs_lock,
8490486958fSJeff Layton 	.flock		= nfs_flock,
85082c156f8SAl Viro 	.splice_read	= generic_file_splice_read,
8514da54c21SAl Viro 	.splice_write	= iter_file_splice_write,
8520486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8531c994a09SJeff Layton 	.setlease	= simple_nosetlease,
8540486958fSJeff Layton };
855ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations);
856