xref: /openbmc/linux/fs/nfs/file.c (revision ce4ef7c0a8a0594d7b9d088d73866a4389402a7e)
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 
191da177e4SLinus Torvalds #include <linux/time.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/fcntl.h>
231da177e4SLinus Torvalds #include <linux/stat.h>
241da177e4SLinus Torvalds #include <linux/nfs_fs.h>
251da177e4SLinus Torvalds #include <linux/nfs_mount.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/pagemap.h>
28e8edc6e0SAlexey Dobriyan #include <linux/aio.h>
295a0e3ad6STejun Heo #include <linux/gfp.h>
30b608b283STrond Myklebust #include <linux/swap.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/uaccess.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include "delegation.h"
3594387fb1STrond Myklebust #include "internal.h"
3691d5b470SChuck Lever #include "iostat.h"
37545db45fSDavid Howells #include "fscache.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
401da177e4SLinus Torvalds 
41f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops;
4294387fb1STrond Myklebust 
431da177e4SLinus Torvalds /* Hack for future NFS swap support */
441da177e4SLinus Torvalds #ifndef IS_SWAPFILE
451da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
461da177e4SLinus Torvalds #endif
471da177e4SLinus Torvalds 
48*ce4ef7c0SBryan 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 }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * Open file
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds static int
601da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	int res;
631da177e4SLinus Torvalds 
646da24bc9SChuck Lever 	dprintk("NFS: open file(%s/%s)\n",
65cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
66cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
67cc0dd2d1SChuck Lever 
68c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
691da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
701da177e4SLinus Torvalds 	if (res)
711da177e4SLinus Torvalds 		return res;
721da177e4SLinus Torvalds 
7346cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
741da177e4SLinus Torvalds 	return res;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
77*ce4ef7c0SBryan Schumaker int
781da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
791da177e4SLinus Torvalds {
806da24bc9SChuck Lever 	dprintk("NFS: release(%s/%s)\n",
816f276e49SRakib Mullick 			filp->f_path.dentry->d_parent->d_name.name,
826f276e49SRakib Mullick 			filp->f_path.dentry->d_name.name);
836da24bc9SChuck Lever 
8491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
8546cb650cSTrond Myklebust 	return nfs_release(inode, filp);
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
88980802e3STrond Myklebust /**
89980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
90980802e3STrond Myklebust  * @inode - pointer to inode struct
91980802e3STrond Myklebust  * @file - pointer to struct file
92980802e3STrond Myklebust  *
93980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
94980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
95980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
96980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
97980802e3STrond Myklebust  * shouldn't trust the cache).
98980802e3STrond Myklebust  */
99980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
100980802e3STrond Myklebust {
101980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
102980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
103980802e3STrond Myklebust 
104d7cf8dd0STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
105d7cf8dd0STrond Myklebust 		goto out_noreval;
106d7cf8dd0STrond Myklebust 
107980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
108980802e3STrond Myklebust 		goto force_reval;
109d7cf8dd0STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
110d7cf8dd0STrond Myklebust 		goto force_reval;
111d7cf8dd0STrond Myklebust 	if (nfs_attribute_timeout(inode))
112d7cf8dd0STrond Myklebust 		goto force_reval;
113d7cf8dd0STrond Myklebust out_noreval:
114fe51beecSTrond Myklebust 	return 0;
115980802e3STrond Myklebust force_reval:
116980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
117980802e3STrond Myklebust }
118980802e3STrond Myklebust 
119*ce4ef7c0SBryan Schumaker loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
120980802e3STrond Myklebust {
1216da24bc9SChuck Lever 	dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
122b84e06c5SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
123b84e06c5SChuck Lever 			filp->f_path.dentry->d_name.name,
124b84e06c5SChuck Lever 			offset, origin);
125b84e06c5SChuck Lever 
12606222e49SJosef Bacik 	/*
12706222e49SJosef Bacik 	 * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
12806222e49SJosef Bacik 	 * the cached file length
12906222e49SJosef Bacik 	 */
1306c529617STrond Myklebust 	if (origin != SEEK_SET && origin != SEEK_CUR) {
131980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
132d5e66348STrond Myklebust 
133980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
134980802e3STrond Myklebust 		if (retval < 0)
135980802e3STrond Myklebust 			return (loff_t)retval;
13679835a71SAndi Kleen 	}
137d5e66348STrond Myklebust 
13879835a71SAndi Kleen 	return generic_file_llseek(filp, offset, origin);
139980802e3STrond Myklebust }
140980802e3STrond Myklebust 
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1431da177e4SLinus Torvalds  */
144*ce4ef7c0SBryan Schumaker int
14575e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1461da177e4SLinus Torvalds {
1476da24bc9SChuck Lever 	struct dentry	*dentry = file->f_path.dentry;
1486da24bc9SChuck Lever 	struct inode	*inode = dentry->d_inode;
1491da177e4SLinus Torvalds 
1506da24bc9SChuck Lever 	dprintk("NFS: flush(%s/%s)\n",
1516da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
1526da24bc9SChuck Lever 			dentry->d_name.name);
1531da177e4SLinus Torvalds 
154c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1551da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1561da177e4SLinus Torvalds 		return 0;
1577b159fc1STrond Myklebust 
15814546c33STrond Myklebust 	/*
15914546c33STrond Myklebust 	 * If we're holding a write delegation, then just start the i/o
16014546c33STrond Myklebust 	 * but don't wait for completion (or send a commit).
16114546c33STrond Myklebust 	 */
162011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
16314546c33STrond Myklebust 		return filemap_fdatawrite(file->f_mapping);
16414546c33STrond Myklebust 
1657fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
166af7fa165STrond Myklebust 	return vfs_fsync(file, 0);
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
169*ce4ef7c0SBryan Schumaker ssize_t
170027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
171027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
1721da177e4SLinus Torvalds {
17301cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
1741da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
1751da177e4SLinus Torvalds 	ssize_t result;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
178027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
1791da177e4SLinus Torvalds 
1806da24bc9SChuck Lever 	dprintk("NFS: read(%s/%s, %lu@%lu)\n",
1811da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
1826f276e49SRakib Mullick 		(unsigned long) iov_length(iov, nr_segs), (unsigned long) pos);
1831da177e4SLinus Torvalds 
18444b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1854184dcf2SChuck Lever 	if (!result) {
186027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
1874184dcf2SChuck Lever 		if (result > 0)
1884184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1894184dcf2SChuck Lever 	}
1901da177e4SLinus Torvalds 	return result;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
193*ce4ef7c0SBryan Schumaker ssize_t
194f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
195f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
196f0930fffSJens Axboe 		     unsigned int flags)
1971da177e4SLinus Torvalds {
19801cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
1991da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2001da177e4SLinus Torvalds 	ssize_t res;
2011da177e4SLinus Torvalds 
2026da24bc9SChuck Lever 	dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
2031da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2041da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2051da177e4SLinus Torvalds 
20644b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
207aa2f1ef1SChuck Lever 	if (!res) {
208f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
209aa2f1ef1SChuck Lever 		if (res > 0)
210aa2f1ef1SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
211aa2f1ef1SChuck Lever 	}
2121da177e4SLinus Torvalds 	return res;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
215*ce4ef7c0SBryan Schumaker int
2161da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2171da177e4SLinus Torvalds {
21801cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2191da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2201da177e4SLinus Torvalds 	int	status;
2211da177e4SLinus Torvalds 
2226da24bc9SChuck Lever 	dprintk("NFS: mmap(%s/%s)\n",
2231da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2241da177e4SLinus Torvalds 
225e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
226e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
227e1ebfd33STrond Myklebust 	 */
228e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
22994387fb1STrond Myklebust 	if (!status) {
23094387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
231e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
23294387fb1STrond Myklebust 	}
2331da177e4SLinus Torvalds 	return status;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /*
2371da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2381da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2391da177e4SLinus Torvalds  * whether any write errors occurred for this process.
240af7fa165STrond Myklebust  *
241af7fa165STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
242af7fa165STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
243af7fa165STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
244af7fa165STrond Myklebust  * This is because the former is used to notify the _next_ call to
24525985edcSLucas De Marchi  * nfs_file_write() that a write error occurred, and hence cause it to
246af7fa165STrond Myklebust  * fall back to doing a synchronous write.
2471da177e4SLinus Torvalds  */
248*ce4ef7c0SBryan Schumaker int
249a5c58892SBryan Schumaker nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
2501da177e4SLinus Torvalds {
2517ea80859SChristoph Hellwig 	struct dentry *dentry = file->f_path.dentry;
252cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2531da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
254af7fa165STrond Myklebust 	int have_error, status;
255af7fa165STrond Myklebust 	int ret = 0;
256af7fa165STrond Myklebust 
2576da24bc9SChuck Lever 	dprintk("NFS: fsync file(%s/%s) datasync %d\n",
25854917786SChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
25954917786SChuck Lever 			datasync);
2601da177e4SLinus Torvalds 
26191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
262af7fa165STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
263af7fa165STrond Myklebust 	status = nfs_commit_inode(inode, FLUSH_SYNC);
2642edb6bc3SNeilBrown 	if (status >= 0 && ret < 0)
2652edb6bc3SNeilBrown 		status = ret;
266af7fa165STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
267af7fa165STrond Myklebust 	if (have_error)
268af7fa165STrond Myklebust 		ret = xchg(&ctx->error, 0);
2690702099bSJ. R. Okajima 	if (!ret && status < 0)
270af7fa165STrond Myklebust 		ret = status;
271a5c58892SBryan Schumaker 	return ret;
272a5c58892SBryan Schumaker }
273a5c58892SBryan Schumaker 
274a5c58892SBryan Schumaker static int
275a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
276a5c58892SBryan Schumaker {
277a5c58892SBryan Schumaker 	int ret;
278a5c58892SBryan Schumaker 	struct inode *inode = file->f_path.dentry->d_inode;
279a5c58892SBryan Schumaker 
280a5c58892SBryan Schumaker 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
281a5c58892SBryan Schumaker 	mutex_lock(&inode->i_mutex);
282a5c58892SBryan Schumaker 	ret = nfs_file_fsync_commit(file, start, end, datasync);
28302c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
284a5c58892SBryan Schumaker 
285af7fa165STrond Myklebust 	return ret;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds /*
28938c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
29038c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
29138c73044SPeter Staubach  * page cache.
29238c73044SPeter Staubach  *
29338c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
29438c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
29538c73044SPeter Staubach  * page must be completely written to stable storage on the server
29638c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
29738c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
29838c73044SPeter Staubach  * done.
29938c73044SPeter Staubach  *
30038c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
30138c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
30238c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
30338c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
30438c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
30538c73044SPeter Staubach  * and that the new data won't completely replace the old data in
30638c73044SPeter Staubach  * that range of the file.
30738c73044SPeter Staubach  */
30838c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page,
30938c73044SPeter Staubach 			loff_t pos, unsigned len)
31038c73044SPeter Staubach {
31138c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
31238c73044SPeter Staubach 	unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
31338c73044SPeter Staubach 	unsigned int end = offset + len;
31438c73044SPeter Staubach 
31538c73044SPeter Staubach 	if ((file->f_mode & FMODE_READ) &&	/* open for read? */
31638c73044SPeter Staubach 	    !PageUptodate(page) &&		/* Uptodate? */
31738c73044SPeter Staubach 	    !PagePrivate(page) &&		/* i/o request already? */
31838c73044SPeter Staubach 	    pglen &&				/* valid bytes of file? */
31938c73044SPeter Staubach 	    (end < pglen || offset))		/* replace all valid bytes? */
32038c73044SPeter Staubach 		return 1;
32138c73044SPeter Staubach 	return 0;
32238c73044SPeter Staubach }
32338c73044SPeter Staubach 
32438c73044SPeter Staubach /*
3254899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3264899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3274899f9c8SNick Piggin  * data from user space.
3281da177e4SLinus Torvalds  *
3291da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3301da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3311da177e4SLinus Torvalds  */
3324899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3334899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3344899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3351da177e4SLinus Torvalds {
3364899f9c8SNick Piggin 	int ret;
33738c73044SPeter Staubach 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
3384899f9c8SNick Piggin 	struct page *page;
33938c73044SPeter Staubach 	int once_thru = 0;
3404899f9c8SNick Piggin 
341b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
342b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
343b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
344b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
345b7eaefaaSChuck Lever 
34638c73044SPeter Staubach start:
34772cb77f4STrond Myklebust 	/*
34872cb77f4STrond Myklebust 	 * Prevent starvation issues if someone is doing a consistency
34972cb77f4STrond Myklebust 	 * sync-to-disk
35072cb77f4STrond Myklebust 	 */
35172cb77f4STrond Myklebust 	ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
35272cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
35372cb77f4STrond Myklebust 	if (ret)
35472cb77f4STrond Myklebust 		return ret;
35572cb77f4STrond Myklebust 
35654566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3574899f9c8SNick Piggin 	if (!page)
3584899f9c8SNick Piggin 		return -ENOMEM;
3594899f9c8SNick Piggin 	*pagep = page;
3604899f9c8SNick Piggin 
3614899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3624899f9c8SNick Piggin 	if (ret) {
3634899f9c8SNick Piggin 		unlock_page(page);
3644899f9c8SNick Piggin 		page_cache_release(page);
36538c73044SPeter Staubach 	} else if (!once_thru &&
36638c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
36738c73044SPeter Staubach 		once_thru = 1;
36838c73044SPeter Staubach 		ret = nfs_readpage(file, page);
36938c73044SPeter Staubach 		page_cache_release(page);
37038c73044SPeter Staubach 		if (!ret)
37138c73044SPeter Staubach 			goto start;
3724899f9c8SNick Piggin 	}
3734899f9c8SNick Piggin 	return ret;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3764899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3774899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3784899f9c8SNick Piggin 			struct page *page, void *fsdata)
3791da177e4SLinus Torvalds {
3804899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
3814899f9c8SNick Piggin 	int status;
3821da177e4SLinus Torvalds 
383b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
384b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
385b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
386b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
387b7eaefaaSChuck Lever 
388efc91ed0STrond Myklebust 	/*
389efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
390efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
391efc91ed0STrond Myklebust 	 */
392efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
393efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
394efc91ed0STrond Myklebust 		unsigned end = offset + len;
395efc91ed0STrond Myklebust 
396efc91ed0STrond Myklebust 		if (pglen == 0) {
397efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
398efc91ed0STrond Myklebust 					end, PAGE_CACHE_SIZE);
399efc91ed0STrond Myklebust 			SetPageUptodate(page);
400efc91ed0STrond Myklebust 		} else if (end >= pglen) {
401efc91ed0STrond Myklebust 			zero_user_segment(page, end, PAGE_CACHE_SIZE);
402efc91ed0STrond Myklebust 			if (offset == 0)
403efc91ed0STrond Myklebust 				SetPageUptodate(page);
404efc91ed0STrond Myklebust 		} else
405efc91ed0STrond Myklebust 			zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
406efc91ed0STrond Myklebust 	}
407efc91ed0STrond Myklebust 
4084899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
4094899f9c8SNick Piggin 
4104899f9c8SNick Piggin 	unlock_page(page);
4114899f9c8SNick Piggin 	page_cache_release(page);
4124899f9c8SNick Piggin 
4133d509e54SChuck Lever 	if (status < 0)
4143d509e54SChuck Lever 		return status;
4152701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
4163d509e54SChuck Lever 	return copied;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4196b9b3514SDavid Howells /*
4206b9b3514SDavid Howells  * Partially or wholly invalidate a page
4216b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4226b9b3514SDavid Howells  *   page invalidation
423545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4246b9b3514SDavid Howells  * - Caller holds page lock
4256b9b3514SDavid Howells  */
4262ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
427cd52ed35STrond Myklebust {
428b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
429b7eaefaaSChuck Lever 
4301c75950bSTrond Myklebust 	if (offset != 0)
4311c75950bSTrond Myklebust 		return;
432d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
4331b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
434545db45fSDavid Howells 
435545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
436cd52ed35STrond Myklebust }
437cd52ed35STrond Myklebust 
4386b9b3514SDavid Howells /*
4396b9b3514SDavid Howells  * Attempt to release the private state associated with a page
440545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4416b9b3514SDavid Howells  * - Caller holds page lock
4426b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4436b9b3514SDavid Howells  */
444cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
445cd52ed35STrond Myklebust {
446b608b283STrond Myklebust 	struct address_space *mapping = page->mapping;
447b608b283STrond Myklebust 
448b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
449b7eaefaaSChuck Lever 
450d812e575STrond Myklebust 	/* Only do I/O if gfp is a superset of GFP_KERNEL */
451b608b283STrond Myklebust 	if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) {
452b608b283STrond Myklebust 		int how = FLUSH_SYNC;
453b608b283STrond Myklebust 
454b608b283STrond Myklebust 		/* Don't let kswapd deadlock waiting for OOM RPC calls */
455b608b283STrond Myklebust 		if (current_is_kswapd())
456b608b283STrond Myklebust 			how = 0;
457b608b283STrond Myklebust 		nfs_commit_inode(mapping->host, how);
458b608b283STrond Myklebust 	}
459e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
460545db45fSDavid Howells 	if (PagePrivate(page))
461ddeff520SNikita Danilov 		return 0;
462545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
463e3db7691STrond Myklebust }
464e3db7691STrond Myklebust 
4656b9b3514SDavid Howells /*
4666b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4676b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4686b9b3514SDavid Howells  * destroyed
469545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4706b9b3514SDavid Howells  * - Caller holds page lock
4716b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4726b9b3514SDavid Howells  */
473e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
474e3db7691STrond Myklebust {
475b7eaefaaSChuck Lever 	struct inode *inode = page->mapping->host;
476545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
477b7eaefaaSChuck Lever 
478b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
479b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
480b7eaefaaSChuck Lever 
481545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
482b7eaefaaSChuck Lever 	return nfs_wb_page(inode, page);
483cd52ed35STrond Myklebust }
484cd52ed35STrond Myklebust 
485f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
4861da177e4SLinus Torvalds 	.readpage = nfs_readpage,
4871da177e4SLinus Torvalds 	.readpages = nfs_readpages,
4889cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
4891da177e4SLinus Torvalds 	.writepage = nfs_writepage,
4901da177e4SLinus Torvalds 	.writepages = nfs_writepages,
4914899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
4924899f9c8SNick Piggin 	.write_end = nfs_write_end,
493cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
494cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
4951da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
496074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
497e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
498f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
4991da177e4SLinus Torvalds };
5001da177e4SLinus Torvalds 
5016b9b3514SDavid Howells /*
5026b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5036b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5046b9b3514SDavid Howells  * shared-writable mapping
5056b9b3514SDavid Howells  */
506c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
50794387fb1STrond Myklebust {
508c2ec175cSNick Piggin 	struct page *page = vmf->page;
50994387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
510b7eaefaaSChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
51194387fb1STrond Myklebust 	unsigned pagelen;
512bc4866b6STrond Myklebust 	int ret = VM_FAULT_NOPAGE;
5134899f9c8SNick Piggin 	struct address_space *mapping;
51494387fb1STrond Myklebust 
515b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
516b7eaefaaSChuck Lever 		dentry->d_parent->d_name.name, dentry->d_name.name,
517b7eaefaaSChuck Lever 		filp->f_mapping->host->i_ino,
518b7eaefaaSChuck Lever 		(long long)page_offset(page));
519b7eaefaaSChuck Lever 
520545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
521545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
522545db45fSDavid Howells 
52394387fb1STrond Myklebust 	lock_page(page);
5244899f9c8SNick Piggin 	mapping = page->mapping;
525b7eaefaaSChuck Lever 	if (mapping != dentry->d_inode->i_mapping)
5268b1f9ee5STrond Myklebust 		goto out_unlock;
5278b1f9ee5STrond Myklebust 
5282aeb98f4STrond Myklebust 	wait_on_page_writeback(page);
5292aeb98f4STrond Myklebust 
5304899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5318b1f9ee5STrond Myklebust 	if (pagelen == 0)
5328b1f9ee5STrond Myklebust 		goto out_unlock;
5338b1f9ee5STrond Myklebust 
534bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
535bc4866b6STrond Myklebust 	if (nfs_flush_incompatible(filp, page) == 0 &&
536bc4866b6STrond Myklebust 	    nfs_updatepage(filp, page, 0, pagelen) == 0)
537bc4866b6STrond Myklebust 		goto out;
5388b1f9ee5STrond Myklebust 
539bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
5408b1f9ee5STrond Myklebust out_unlock:
5414899f9c8SNick Piggin 	unlock_page(page);
542bc4866b6STrond Myklebust out:
543bc4866b6STrond Myklebust 	return ret;
54494387fb1STrond Myklebust }
54594387fb1STrond Myklebust 
546f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
54794387fb1STrond Myklebust 	.fault = filemap_fault,
54894387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
54994387fb1STrond Myklebust };
55094387fb1STrond Myklebust 
5517b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
5527b159fc1STrond Myklebust {
5537b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5547b159fc1STrond Myklebust 
5556b2f3d1fSChristoph Hellwig 	if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
5567b159fc1STrond Myklebust 		return 1;
557cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
5587b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
5597b159fc1STrond Myklebust 		return 1;
5607b159fc1STrond Myklebust 	return 0;
5617b159fc1STrond Myklebust }
5627b159fc1STrond Myklebust 
563*ce4ef7c0SBryan Schumaker ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
564027445c3SBadari Pulavarty 		       unsigned long nr_segs, loff_t pos)
5651da177e4SLinus Torvalds {
56601cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
5671da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
5687e381172SChuck Lever 	unsigned long written = 0;
5691da177e4SLinus Torvalds 	ssize_t result;
570027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
573027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
5741da177e4SLinus Torvalds 
5756da24bc9SChuck Lever 	dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
5761da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
5776da24bc9SChuck Lever 		(unsigned long) count, (long long) pos);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	result = -EBUSY;
5801da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
5811da177e4SLinus Torvalds 		goto out_swapfile;
5827d52e862STrond Myklebust 	/*
5837d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
5847d52e862STrond Myklebust 	 */
5857d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
5867d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
5871da177e4SLinus Torvalds 		if (result)
5881da177e4SLinus Torvalds 			goto out;
589fe51beecSTrond Myklebust 	}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	result = count;
5921da177e4SLinus Torvalds 	if (!count)
5931da177e4SLinus Torvalds 		goto out;
5941da177e4SLinus Torvalds 
595027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
5967e381172SChuck Lever 	if (result > 0)
5977e381172SChuck Lever 		written = result;
5987e381172SChuck Lever 
5996b2f3d1fSChristoph Hellwig 	/* Return error values for O_DSYNC and IS_SYNC() */
6007b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
601af7fa165STrond Myklebust 		int err = vfs_fsync(iocb->ki_filp, 0);
602200baa21STrond Myklebust 		if (err < 0)
603200baa21STrond Myklebust 			result = err;
604200baa21STrond Myklebust 	}
6057e381172SChuck Lever 	if (result > 0)
6067e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
6071da177e4SLinus Torvalds out:
6081da177e4SLinus Torvalds 	return result;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds out_swapfile:
6111da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
6121da177e4SLinus Torvalds 	goto out;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
615*ce4ef7c0SBryan Schumaker ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
616bf40d343SSuresh Jayaraman 			      struct file *filp, loff_t *ppos,
617bf40d343SSuresh Jayaraman 			      size_t count, unsigned int flags)
618bf40d343SSuresh Jayaraman {
619bf40d343SSuresh Jayaraman 	struct dentry *dentry = filp->f_path.dentry;
620bf40d343SSuresh Jayaraman 	struct inode *inode = dentry->d_inode;
6217e381172SChuck Lever 	unsigned long written = 0;
622bf40d343SSuresh Jayaraman 	ssize_t ret;
623bf40d343SSuresh Jayaraman 
624bf40d343SSuresh Jayaraman 	dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
625bf40d343SSuresh Jayaraman 		dentry->d_parent->d_name.name, dentry->d_name.name,
626bf40d343SSuresh Jayaraman 		(unsigned long) count, (unsigned long long) *ppos);
627bf40d343SSuresh Jayaraman 
628bf40d343SSuresh Jayaraman 	/*
629bf40d343SSuresh Jayaraman 	 * The combination of splice and an O_APPEND destination is disallowed.
630bf40d343SSuresh Jayaraman 	 */
631bf40d343SSuresh Jayaraman 
632bf40d343SSuresh Jayaraman 	ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
6337e381172SChuck Lever 	if (ret > 0)
6347e381172SChuck Lever 		written = ret;
6357e381172SChuck Lever 
636bf40d343SSuresh Jayaraman 	if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
637af7fa165STrond Myklebust 		int err = vfs_fsync(filp, 0);
638bf40d343SSuresh Jayaraman 		if (err < 0)
639bf40d343SSuresh Jayaraman 			ret = err;
640bf40d343SSuresh Jayaraman 	}
6417e381172SChuck Lever 	if (ret > 0)
6427e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
643bf40d343SSuresh Jayaraman 	return ret;
644bf40d343SSuresh Jayaraman }
645bf40d343SSuresh Jayaraman 
6465eebde23SSuresh Jayaraman static int
6475eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6501da177e4SLinus Torvalds 	int status = 0;
65121ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
6521da177e4SLinus Torvalds 
653039c4d7aSTrond Myklebust 	/* Try local locking first */
6546d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6556d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6566d34ac19SJ. Bruce Fields 		/* found a conflict */
657039c4d7aSTrond Myklebust 		goto out;
6581da177e4SLinus Torvalds 	}
65921ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
660039c4d7aSTrond Myklebust 
661011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
662039c4d7aSTrond Myklebust 		goto out_noconflict;
663039c4d7aSTrond Myklebust 
6645eebde23SSuresh Jayaraman 	if (is_local)
665039c4d7aSTrond Myklebust 		goto out_noconflict;
666039c4d7aSTrond Myklebust 
667039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
668039c4d7aSTrond Myklebust out:
6691da177e4SLinus Torvalds 	return status;
670039c4d7aSTrond Myklebust out_noconflict:
671039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
672039c4d7aSTrond Myklebust 	goto out;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	int res = 0;
6781da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
6791da177e4SLinus Torvalds 		case FL_POSIX:
6801da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
6811da177e4SLinus Torvalds 			break;
6821da177e4SLinus Torvalds 		case FL_FLOCK:
6831da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
6841da177e4SLinus Torvalds 			break;
6851da177e4SLinus Torvalds 		default:
6861da177e4SLinus Torvalds 			BUG();
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 	return res;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6915eebde23SSuresh Jayaraman static int
6925eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6951da177e4SLinus Torvalds 	int status;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/*
6981da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
6991da177e4SLinus Torvalds 	 * with locks..
7001da177e4SLinus Torvalds 	 */
70129884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	/* NOTE: special case
7041da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
7051da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
7061da177e4SLinus Torvalds 	 */
7075eebde23SSuresh Jayaraman 	/*
7085eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7095eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7105eebde23SSuresh Jayaraman 	 */
7115eebde23SSuresh Jayaraman 	if (!is_local)
7121da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7131da177e4SLinus Torvalds 	else
7141da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7151da177e4SLinus Torvalds 	return status;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7185eebde23SSuresh Jayaraman static int
7196b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) {
7206b96724eSRicardo Labiaga 	return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
7216b96724eSRicardo Labiaga }
7226b96724eSRicardo Labiaga 
7236b96724eSRicardo Labiaga static int
7245eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7271da177e4SLinus Torvalds 	int status;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	/*
7301da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7311da177e4SLinus Torvalds 	 * with locks..
7321da177e4SLinus Torvalds 	 */
73329884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
73429884df0STrond Myklebust 	if (status != 0)
7351da177e4SLinus Torvalds 		goto out;
7361da177e4SLinus Torvalds 
7375eebde23SSuresh Jayaraman 	/*
7385eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7395eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7405eebde23SSuresh Jayaraman 	 */
7415eebde23SSuresh Jayaraman 	if (!is_local)
7421da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
743c4d7c402STrond Myklebust 	else
7441da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7451da177e4SLinus Torvalds 	if (status < 0)
7461da177e4SLinus Torvalds 		goto out;
7476b96724eSRicardo Labiaga 
7481da177e4SLinus Torvalds 	/*
7496b96724eSRicardo Labiaga 	 * Revalidate the cache if the server has time stamps granular
7506b96724eSRicardo Labiaga 	 * enough to detect subsecond changes.  Otherwise, clear the
7516b96724eSRicardo Labiaga 	 * cache to prevent missing any changes.
7526b96724eSRicardo Labiaga 	 *
7531da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7541da177e4SLinus Torvalds 	 */
75529884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
756011e2a7fSBryan Schumaker 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
7576b96724eSRicardo Labiaga 		if (is_time_granular(&NFS_SERVER(inode)->time_delta))
7586b96724eSRicardo Labiaga 			__nfs_revalidate_inode(NFS_SERVER(inode), inode);
7596b96724eSRicardo Labiaga 		else
7601da177e4SLinus Torvalds 			nfs_zap_caches(inode);
7616b96724eSRicardo Labiaga 	}
7621da177e4SLinus Torvalds out:
7631da177e4SLinus Torvalds 	return status;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds /*
7671da177e4SLinus Torvalds  * Lock a (portion of) a file
7681da177e4SLinus Torvalds  */
769*ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7701da177e4SLinus Torvalds {
7711da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7722116271aSTrond Myklebust 	int ret = -ENOLCK;
7735eebde23SSuresh Jayaraman 	int is_local = 0;
7741da177e4SLinus Torvalds 
7756da24bc9SChuck Lever 	dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
7766da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
7776da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
7781da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
7791da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7806da24bc9SChuck Lever 
78191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
784dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
7852116271aSTrond Myklebust 		goto out_err;
7862116271aSTrond Myklebust 
7875eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
7885eebde23SSuresh Jayaraman 		is_local = 1;
7895eebde23SSuresh Jayaraman 
7902116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
7912116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
7922116271aSTrond Myklebust 		if (ret < 0)
7932116271aSTrond Myklebust 			goto out_err;
7942116271aSTrond Myklebust 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
7975eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
7982116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
7995eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
8002116271aSTrond Myklebust 	else
8015eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8022116271aSTrond Myklebust out_err:
8032116271aSTrond Myklebust 	return ret;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /*
8071da177e4SLinus Torvalds  * Lock a (portion of) a file
8081da177e4SLinus Torvalds  */
809*ce4ef7c0SBryan 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 
8146da24bc9SChuck Lever 	dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
8156da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
8166da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
8171da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8201da177e4SLinus Torvalds 		return -ENOLCK;
8211da177e4SLinus Torvalds 
8225eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8235eebde23SSuresh Jayaraman 		is_local = 1;
8245eebde23SSuresh Jayaraman 
8251da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
8261da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
8271da177e4SLinus Torvalds 	fl->fl_start = 0;
8281da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
8315eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8325eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8331da177e4SLinus Torvalds }
834370f6599SJ. Bruce Fields 
8356da24bc9SChuck Lever /*
8366da24bc9SChuck Lever  * There is no protocol support for leases, so we have no way to implement
8376da24bc9SChuck Lever  * them correctly in the face of opens by other clients.
8386da24bc9SChuck Lever  */
839*ce4ef7c0SBryan Schumaker int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
840370f6599SJ. Bruce Fields {
8416da24bc9SChuck Lever 	dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
8426da24bc9SChuck Lever 			file->f_path.dentry->d_parent->d_name.name,
8436da24bc9SChuck Lever 			file->f_path.dentry->d_name.name, arg);
844370f6599SJ. Bruce Fields 	return -EINVAL;
845370f6599SJ. Bruce Fields }
8461788ea6eSJeff Layton 
8470486958fSJeff Layton const struct file_operations nfs_file_operations = {
8480486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8490486958fSJeff Layton 	.read		= do_sync_read,
8500486958fSJeff Layton 	.write		= do_sync_write,
8510486958fSJeff Layton 	.aio_read	= nfs_file_read,
8520486958fSJeff Layton 	.aio_write	= nfs_file_write,
8530486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8540486958fSJeff Layton 	.open		= nfs_file_open,
8550486958fSJeff Layton 	.flush		= nfs_file_flush,
8560486958fSJeff Layton 	.release	= nfs_file_release,
8570486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8580486958fSJeff Layton 	.lock		= nfs_lock,
8590486958fSJeff Layton 	.flock		= nfs_flock,
8600486958fSJeff Layton 	.splice_read	= nfs_file_splice_read,
8610486958fSJeff Layton 	.splice_write	= nfs_file_splice_write,
8620486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8630486958fSJeff Layton 	.setlease	= nfs_setlease,
8640486958fSJeff Layton };
865