xref: /openbmc/linux/fs/nfs/file.c (revision 011e2a7fd5e9e0c2fdba6b9466d53fc437f8bfaf)
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"
38e5e94017SBenny Halevy #include "pnfs.h"
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
411da177e4SLinus Torvalds 
42f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops;
4394387fb1STrond Myklebust 
4492e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = {
451da177e4SLinus Torvalds 	.permission	= nfs_permission,
461da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
471da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
481da177e4SLinus Torvalds };
491da177e4SLinus Torvalds 
50b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
5192e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = {
52b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
53b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
54b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
55b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
56b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
57b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
58b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
59b7fa0554SAndreas Gruenbacher };
60b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_v3 */
61b7fa0554SAndreas Gruenbacher 
621da177e4SLinus Torvalds /* Hack for future NFS swap support */
631da177e4SLinus Torvalds #ifndef IS_SWAPFILE
641da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static int nfs_check_flags(int flags)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
701da177e4SLinus Torvalds 		return -EINVAL;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	return 0;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /*
761da177e4SLinus Torvalds  * Open file
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds static int
791da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	int res;
821da177e4SLinus Torvalds 
836da24bc9SChuck Lever 	dprintk("NFS: open file(%s/%s)\n",
84cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
85cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
86cc0dd2d1SChuck Lever 
87c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
881da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
891da177e4SLinus Torvalds 	if (res)
901da177e4SLinus Torvalds 		return res;
911da177e4SLinus Torvalds 
9246cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
931da177e4SLinus Torvalds 	return res;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int
971da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
981da177e4SLinus Torvalds {
996da24bc9SChuck Lever 	dprintk("NFS: release(%s/%s)\n",
1006f276e49SRakib Mullick 			filp->f_path.dentry->d_parent->d_name.name,
1016f276e49SRakib Mullick 			filp->f_path.dentry->d_name.name);
1026da24bc9SChuck Lever 
10391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
10446cb650cSTrond Myklebust 	return nfs_release(inode, filp);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
107980802e3STrond Myklebust /**
108980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
109980802e3STrond Myklebust  * @inode - pointer to inode struct
110980802e3STrond Myklebust  * @file - pointer to struct file
111980802e3STrond Myklebust  *
112980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
113980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
114980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
115980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
116980802e3STrond Myklebust  * shouldn't trust the cache).
117980802e3STrond Myklebust  */
118980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
119980802e3STrond Myklebust {
120980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
121980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
122980802e3STrond Myklebust 
123d7cf8dd0STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
124d7cf8dd0STrond Myklebust 		goto out_noreval;
125d7cf8dd0STrond Myklebust 
126980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
127980802e3STrond Myklebust 		goto force_reval;
128d7cf8dd0STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
129d7cf8dd0STrond Myklebust 		goto force_reval;
130d7cf8dd0STrond Myklebust 	if (nfs_attribute_timeout(inode))
131d7cf8dd0STrond Myklebust 		goto force_reval;
132d7cf8dd0STrond Myklebust out_noreval:
133fe51beecSTrond Myklebust 	return 0;
134980802e3STrond Myklebust force_reval:
135980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
136980802e3STrond Myklebust }
137980802e3STrond Myklebust 
138980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
139980802e3STrond Myklebust {
1406da24bc9SChuck Lever 	dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
141b84e06c5SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
142b84e06c5SChuck Lever 			filp->f_path.dentry->d_name.name,
143b84e06c5SChuck Lever 			offset, origin);
144b84e06c5SChuck Lever 
14506222e49SJosef Bacik 	/*
14606222e49SJosef Bacik 	 * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
14706222e49SJosef Bacik 	 * the cached file length
14806222e49SJosef Bacik 	 */
1496c529617STrond Myklebust 	if (origin != SEEK_SET && origin != SEEK_CUR) {
150980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
151d5e66348STrond Myklebust 
152980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
153980802e3STrond Myklebust 		if (retval < 0)
154980802e3STrond Myklebust 			return (loff_t)retval;
15579835a71SAndi Kleen 	}
156d5e66348STrond Myklebust 
15779835a71SAndi Kleen 	return generic_file_llseek(filp, offset, origin);
158980802e3STrond Myklebust }
159980802e3STrond Myklebust 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds static int
16475e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1651da177e4SLinus Torvalds {
1666da24bc9SChuck Lever 	struct dentry	*dentry = file->f_path.dentry;
1676da24bc9SChuck Lever 	struct inode	*inode = dentry->d_inode;
1681da177e4SLinus Torvalds 
1696da24bc9SChuck Lever 	dprintk("NFS: flush(%s/%s)\n",
1706da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
1716da24bc9SChuck Lever 			dentry->d_name.name);
1721da177e4SLinus Torvalds 
173c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1741da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1751da177e4SLinus Torvalds 		return 0;
1767b159fc1STrond Myklebust 
17714546c33STrond Myklebust 	/*
17814546c33STrond Myklebust 	 * If we're holding a write delegation, then just start the i/o
17914546c33STrond Myklebust 	 * but don't wait for completion (or send a commit).
18014546c33STrond Myklebust 	 */
181*011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
18214546c33STrond Myklebust 		return filemap_fdatawrite(file->f_mapping);
18314546c33STrond Myklebust 
1847fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
185af7fa165STrond Myklebust 	return vfs_fsync(file, 0);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static ssize_t
189027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
190027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
1911da177e4SLinus Torvalds {
19201cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
1931da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
1941da177e4SLinus Torvalds 	ssize_t result;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
197027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
1981da177e4SLinus Torvalds 
1996da24bc9SChuck Lever 	dprintk("NFS: read(%s/%s, %lu@%lu)\n",
2001da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2016f276e49SRakib Mullick 		(unsigned long) iov_length(iov, nr_segs), (unsigned long) pos);
2021da177e4SLinus Torvalds 
20344b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
2044184dcf2SChuck Lever 	if (!result) {
205027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2064184dcf2SChuck Lever 		if (result > 0)
2074184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
2084184dcf2SChuck Lever 	}
2091da177e4SLinus Torvalds 	return result;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static ssize_t
213f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
214f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
215f0930fffSJens Axboe 		     unsigned int flags)
2161da177e4SLinus Torvalds {
21701cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2181da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2191da177e4SLinus Torvalds 	ssize_t res;
2201da177e4SLinus Torvalds 
2216da24bc9SChuck Lever 	dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
2221da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2231da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2241da177e4SLinus Torvalds 
22544b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
226aa2f1ef1SChuck Lever 	if (!res) {
227f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
228aa2f1ef1SChuck Lever 		if (res > 0)
229aa2f1ef1SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
230aa2f1ef1SChuck Lever 	}
2311da177e4SLinus Torvalds 	return res;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static int
2351da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2361da177e4SLinus Torvalds {
23701cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2381da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2391da177e4SLinus Torvalds 	int	status;
2401da177e4SLinus Torvalds 
2416da24bc9SChuck Lever 	dprintk("NFS: mmap(%s/%s)\n",
2421da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2431da177e4SLinus Torvalds 
244e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
245e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
246e1ebfd33STrond Myklebust 	 */
247e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
24894387fb1STrond Myklebust 	if (!status) {
24994387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
250e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
25194387fb1STrond Myklebust 	}
2521da177e4SLinus Torvalds 	return status;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /*
2561da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2571da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2581da177e4SLinus Torvalds  * whether any write errors occurred for this process.
259af7fa165STrond Myklebust  *
260af7fa165STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
261af7fa165STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
262af7fa165STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
263af7fa165STrond Myklebust  * This is because the former is used to notify the _next_ call to
26425985edcSLucas De Marchi  * nfs_file_write() that a write error occurred, and hence cause it to
265af7fa165STrond Myklebust  * fall back to doing a synchronous write.
2661da177e4SLinus Torvalds  */
2671da177e4SLinus Torvalds static int
268a5c58892SBryan Schumaker nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
2691da177e4SLinus Torvalds {
2707ea80859SChristoph Hellwig 	struct dentry *dentry = file->f_path.dentry;
271cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2721da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
273af7fa165STrond Myklebust 	int have_error, status;
274af7fa165STrond Myklebust 	int ret = 0;
275af7fa165STrond Myklebust 
2766da24bc9SChuck Lever 	dprintk("NFS: fsync file(%s/%s) datasync %d\n",
27754917786SChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
27854917786SChuck Lever 			datasync);
2791da177e4SLinus Torvalds 
28091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
281af7fa165STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
282af7fa165STrond Myklebust 	status = nfs_commit_inode(inode, FLUSH_SYNC);
2832edb6bc3SNeilBrown 	if (status >= 0 && ret < 0)
2842edb6bc3SNeilBrown 		status = ret;
285af7fa165STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
286af7fa165STrond Myklebust 	if (have_error)
287af7fa165STrond Myklebust 		ret = xchg(&ctx->error, 0);
2880702099bSJ. R. Okajima 	if (!ret && status < 0)
289af7fa165STrond Myklebust 		ret = status;
290a5c58892SBryan Schumaker 	return ret;
291a5c58892SBryan Schumaker }
292a5c58892SBryan Schumaker 
293a5c58892SBryan Schumaker static int
294a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
295a5c58892SBryan Schumaker {
296a5c58892SBryan Schumaker 	int ret;
297a5c58892SBryan Schumaker 	struct inode *inode = file->f_path.dentry->d_inode;
298a5c58892SBryan Schumaker 
299a5c58892SBryan Schumaker 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
300a5c58892SBryan Schumaker 	mutex_lock(&inode->i_mutex);
301a5c58892SBryan Schumaker 	ret = nfs_file_fsync_commit(file, start, end, datasync);
30202c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
303a5c58892SBryan Schumaker 
304af7fa165STrond Myklebust 	return ret;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /*
30838c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
30938c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
31038c73044SPeter Staubach  * page cache.
31138c73044SPeter Staubach  *
31238c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
31338c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
31438c73044SPeter Staubach  * page must be completely written to stable storage on the server
31538c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
31638c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
31738c73044SPeter Staubach  * done.
31838c73044SPeter Staubach  *
31938c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
32038c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
32138c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
32238c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
32338c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
32438c73044SPeter Staubach  * and that the new data won't completely replace the old data in
32538c73044SPeter Staubach  * that range of the file.
32638c73044SPeter Staubach  */
32738c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page,
32838c73044SPeter Staubach 			loff_t pos, unsigned len)
32938c73044SPeter Staubach {
33038c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
33138c73044SPeter Staubach 	unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
33238c73044SPeter Staubach 	unsigned int end = offset + len;
33338c73044SPeter Staubach 
33438c73044SPeter Staubach 	if ((file->f_mode & FMODE_READ) &&	/* open for read? */
33538c73044SPeter Staubach 	    !PageUptodate(page) &&		/* Uptodate? */
33638c73044SPeter Staubach 	    !PagePrivate(page) &&		/* i/o request already? */
33738c73044SPeter Staubach 	    pglen &&				/* valid bytes of file? */
33838c73044SPeter Staubach 	    (end < pglen || offset))		/* replace all valid bytes? */
33938c73044SPeter Staubach 		return 1;
34038c73044SPeter Staubach 	return 0;
34138c73044SPeter Staubach }
34238c73044SPeter Staubach 
34338c73044SPeter Staubach /*
3444899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3454899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3464899f9c8SNick Piggin  * data from user space.
3471da177e4SLinus Torvalds  *
3481da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3491da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3501da177e4SLinus Torvalds  */
3514899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3524899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3534899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3541da177e4SLinus Torvalds {
3554899f9c8SNick Piggin 	int ret;
35638c73044SPeter Staubach 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
3574899f9c8SNick Piggin 	struct page *page;
35838c73044SPeter Staubach 	int once_thru = 0;
3594899f9c8SNick Piggin 
360b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
361b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
362b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
363b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
364b7eaefaaSChuck Lever 
36538c73044SPeter Staubach start:
36672cb77f4STrond Myklebust 	/*
36772cb77f4STrond Myklebust 	 * Prevent starvation issues if someone is doing a consistency
36872cb77f4STrond Myklebust 	 * sync-to-disk
36972cb77f4STrond Myklebust 	 */
37072cb77f4STrond Myklebust 	ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
37172cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
37272cb77f4STrond Myklebust 	if (ret)
37372cb77f4STrond Myklebust 		return ret;
37472cb77f4STrond Myklebust 
37554566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3764899f9c8SNick Piggin 	if (!page)
3774899f9c8SNick Piggin 		return -ENOMEM;
3784899f9c8SNick Piggin 	*pagep = page;
3794899f9c8SNick Piggin 
3804899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3814899f9c8SNick Piggin 	if (ret) {
3824899f9c8SNick Piggin 		unlock_page(page);
3834899f9c8SNick Piggin 		page_cache_release(page);
38438c73044SPeter Staubach 	} else if (!once_thru &&
38538c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
38638c73044SPeter Staubach 		once_thru = 1;
38738c73044SPeter Staubach 		ret = nfs_readpage(file, page);
38838c73044SPeter Staubach 		page_cache_release(page);
38938c73044SPeter Staubach 		if (!ret)
39038c73044SPeter Staubach 			goto start;
3914899f9c8SNick Piggin 	}
3924899f9c8SNick Piggin 	return ret;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
3954899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3964899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3974899f9c8SNick Piggin 			struct page *page, void *fsdata)
3981da177e4SLinus Torvalds {
3994899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
4004899f9c8SNick Piggin 	int status;
4011da177e4SLinus Torvalds 
402b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
403b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
404b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
405b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
406b7eaefaaSChuck Lever 
407efc91ed0STrond Myklebust 	/*
408efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
409efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
410efc91ed0STrond Myklebust 	 */
411efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
412efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
413efc91ed0STrond Myklebust 		unsigned end = offset + len;
414efc91ed0STrond Myklebust 
415efc91ed0STrond Myklebust 		if (pglen == 0) {
416efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
417efc91ed0STrond Myklebust 					end, PAGE_CACHE_SIZE);
418efc91ed0STrond Myklebust 			SetPageUptodate(page);
419efc91ed0STrond Myklebust 		} else if (end >= pglen) {
420efc91ed0STrond Myklebust 			zero_user_segment(page, end, PAGE_CACHE_SIZE);
421efc91ed0STrond Myklebust 			if (offset == 0)
422efc91ed0STrond Myklebust 				SetPageUptodate(page);
423efc91ed0STrond Myklebust 		} else
424efc91ed0STrond Myklebust 			zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
425efc91ed0STrond Myklebust 	}
426efc91ed0STrond Myklebust 
4274899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
4284899f9c8SNick Piggin 
4294899f9c8SNick Piggin 	unlock_page(page);
4304899f9c8SNick Piggin 	page_cache_release(page);
4314899f9c8SNick Piggin 
4323d509e54SChuck Lever 	if (status < 0)
4333d509e54SChuck Lever 		return status;
4342701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
4353d509e54SChuck Lever 	return copied;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4386b9b3514SDavid Howells /*
4396b9b3514SDavid Howells  * Partially or wholly invalidate a page
4406b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4416b9b3514SDavid Howells  *   page invalidation
442545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4436b9b3514SDavid Howells  * - Caller holds page lock
4446b9b3514SDavid Howells  */
4452ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
446cd52ed35STrond Myklebust {
447b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
448b7eaefaaSChuck Lever 
4491c75950bSTrond Myklebust 	if (offset != 0)
4501c75950bSTrond Myklebust 		return;
451d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
4521b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
453545db45fSDavid Howells 
454545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
455cd52ed35STrond Myklebust }
456cd52ed35STrond Myklebust 
4576b9b3514SDavid Howells /*
4586b9b3514SDavid Howells  * Attempt to release the private state associated with a page
459545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4606b9b3514SDavid Howells  * - Caller holds page lock
4616b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4626b9b3514SDavid Howells  */
463cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
464cd52ed35STrond Myklebust {
465b608b283STrond Myklebust 	struct address_space *mapping = page->mapping;
466b608b283STrond Myklebust 
467b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
468b7eaefaaSChuck Lever 
469d812e575STrond Myklebust 	/* Only do I/O if gfp is a superset of GFP_KERNEL */
470b608b283STrond Myklebust 	if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) {
471b608b283STrond Myklebust 		int how = FLUSH_SYNC;
472b608b283STrond Myklebust 
473b608b283STrond Myklebust 		/* Don't let kswapd deadlock waiting for OOM RPC calls */
474b608b283STrond Myklebust 		if (current_is_kswapd())
475b608b283STrond Myklebust 			how = 0;
476b608b283STrond Myklebust 		nfs_commit_inode(mapping->host, how);
477b608b283STrond Myklebust 	}
478e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
479545db45fSDavid Howells 	if (PagePrivate(page))
480ddeff520SNikita Danilov 		return 0;
481545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
482e3db7691STrond Myklebust }
483e3db7691STrond Myklebust 
4846b9b3514SDavid Howells /*
4856b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4866b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4876b9b3514SDavid Howells  * destroyed
488545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4896b9b3514SDavid Howells  * - Caller holds page lock
4906b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4916b9b3514SDavid Howells  */
492e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
493e3db7691STrond Myklebust {
494b7eaefaaSChuck Lever 	struct inode *inode = page->mapping->host;
495545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
496b7eaefaaSChuck Lever 
497b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
498b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
499b7eaefaaSChuck Lever 
500545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
501b7eaefaaSChuck Lever 	return nfs_wb_page(inode, page);
502cd52ed35STrond Myklebust }
503cd52ed35STrond Myklebust 
504f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5051da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5061da177e4SLinus Torvalds 	.readpages = nfs_readpages,
5079cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
5081da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5091da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5104899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5114899f9c8SNick Piggin 	.write_end = nfs_write_end,
512cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
513cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5141da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
515074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
516e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
517f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
5181da177e4SLinus Torvalds };
5191da177e4SLinus Torvalds 
5206b9b3514SDavid Howells /*
5216b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5226b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5236b9b3514SDavid Howells  * shared-writable mapping
5246b9b3514SDavid Howells  */
525c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
52694387fb1STrond Myklebust {
527c2ec175cSNick Piggin 	struct page *page = vmf->page;
52894387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
529b7eaefaaSChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
53094387fb1STrond Myklebust 	unsigned pagelen;
531bc4866b6STrond Myklebust 	int ret = VM_FAULT_NOPAGE;
5324899f9c8SNick Piggin 	struct address_space *mapping;
53394387fb1STrond Myklebust 
534b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
535b7eaefaaSChuck Lever 		dentry->d_parent->d_name.name, dentry->d_name.name,
536b7eaefaaSChuck Lever 		filp->f_mapping->host->i_ino,
537b7eaefaaSChuck Lever 		(long long)page_offset(page));
538b7eaefaaSChuck Lever 
539545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
540545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
541545db45fSDavid Howells 
54294387fb1STrond Myklebust 	lock_page(page);
5434899f9c8SNick Piggin 	mapping = page->mapping;
544b7eaefaaSChuck Lever 	if (mapping != dentry->d_inode->i_mapping)
5458b1f9ee5STrond Myklebust 		goto out_unlock;
5468b1f9ee5STrond Myklebust 
5472aeb98f4STrond Myklebust 	wait_on_page_writeback(page);
5482aeb98f4STrond Myklebust 
5494899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5508b1f9ee5STrond Myklebust 	if (pagelen == 0)
5518b1f9ee5STrond Myklebust 		goto out_unlock;
5528b1f9ee5STrond Myklebust 
553bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
554bc4866b6STrond Myklebust 	if (nfs_flush_incompatible(filp, page) == 0 &&
555bc4866b6STrond Myklebust 	    nfs_updatepage(filp, page, 0, pagelen) == 0)
556bc4866b6STrond Myklebust 		goto out;
5578b1f9ee5STrond Myklebust 
558bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
5598b1f9ee5STrond Myklebust out_unlock:
5604899f9c8SNick Piggin 	unlock_page(page);
561bc4866b6STrond Myklebust out:
562bc4866b6STrond Myklebust 	return ret;
56394387fb1STrond Myklebust }
56494387fb1STrond Myklebust 
565f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
56694387fb1STrond Myklebust 	.fault = filemap_fault,
56794387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
56894387fb1STrond Myklebust };
56994387fb1STrond Myklebust 
5707b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
5717b159fc1STrond Myklebust {
5727b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5737b159fc1STrond Myklebust 
5746b2f3d1fSChristoph Hellwig 	if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
5757b159fc1STrond Myklebust 		return 1;
576cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
5777b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
5787b159fc1STrond Myklebust 		return 1;
5797b159fc1STrond Myklebust 	return 0;
5807b159fc1STrond Myklebust }
5817b159fc1STrond Myklebust 
582027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
583027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
5841da177e4SLinus Torvalds {
58501cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
5861da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
5877e381172SChuck Lever 	unsigned long written = 0;
5881da177e4SLinus Torvalds 	ssize_t result;
589027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
592027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
5931da177e4SLinus Torvalds 
5946da24bc9SChuck Lever 	dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
5951da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
5966da24bc9SChuck Lever 		(unsigned long) count, (long long) pos);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	result = -EBUSY;
5991da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6001da177e4SLinus Torvalds 		goto out_swapfile;
6017d52e862STrond Myklebust 	/*
6027d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6037d52e862STrond Myklebust 	 */
6047d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
6057d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
6061da177e4SLinus Torvalds 		if (result)
6071da177e4SLinus Torvalds 			goto out;
608fe51beecSTrond Myklebust 	}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	result = count;
6111da177e4SLinus Torvalds 	if (!count)
6121da177e4SLinus Torvalds 		goto out;
6131da177e4SLinus Torvalds 
614027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
6157e381172SChuck Lever 	if (result > 0)
6167e381172SChuck Lever 		written = result;
6177e381172SChuck Lever 
6186b2f3d1fSChristoph Hellwig 	/* Return error values for O_DSYNC and IS_SYNC() */
6197b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
620af7fa165STrond Myklebust 		int err = vfs_fsync(iocb->ki_filp, 0);
621200baa21STrond Myklebust 		if (err < 0)
622200baa21STrond Myklebust 			result = err;
623200baa21STrond Myklebust 	}
6247e381172SChuck Lever 	if (result > 0)
6257e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
6261da177e4SLinus Torvalds out:
6271da177e4SLinus Torvalds 	return result;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds out_swapfile:
6301da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
6311da177e4SLinus Torvalds 	goto out;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
634bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
635bf40d343SSuresh Jayaraman 				     struct file *filp, loff_t *ppos,
636bf40d343SSuresh Jayaraman 				     size_t count, unsigned int flags)
637bf40d343SSuresh Jayaraman {
638bf40d343SSuresh Jayaraman 	struct dentry *dentry = filp->f_path.dentry;
639bf40d343SSuresh Jayaraman 	struct inode *inode = dentry->d_inode;
6407e381172SChuck Lever 	unsigned long written = 0;
641bf40d343SSuresh Jayaraman 	ssize_t ret;
642bf40d343SSuresh Jayaraman 
643bf40d343SSuresh Jayaraman 	dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
644bf40d343SSuresh Jayaraman 		dentry->d_parent->d_name.name, dentry->d_name.name,
645bf40d343SSuresh Jayaraman 		(unsigned long) count, (unsigned long long) *ppos);
646bf40d343SSuresh Jayaraman 
647bf40d343SSuresh Jayaraman 	/*
648bf40d343SSuresh Jayaraman 	 * The combination of splice and an O_APPEND destination is disallowed.
649bf40d343SSuresh Jayaraman 	 */
650bf40d343SSuresh Jayaraman 
651bf40d343SSuresh Jayaraman 	ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
6527e381172SChuck Lever 	if (ret > 0)
6537e381172SChuck Lever 		written = ret;
6547e381172SChuck Lever 
655bf40d343SSuresh Jayaraman 	if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
656af7fa165STrond Myklebust 		int err = vfs_fsync(filp, 0);
657bf40d343SSuresh Jayaraman 		if (err < 0)
658bf40d343SSuresh Jayaraman 			ret = err;
659bf40d343SSuresh Jayaraman 	}
6607e381172SChuck Lever 	if (ret > 0)
6617e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
662bf40d343SSuresh Jayaraman 	return ret;
663bf40d343SSuresh Jayaraman }
664bf40d343SSuresh Jayaraman 
6655eebde23SSuresh Jayaraman static int
6665eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6691da177e4SLinus Torvalds 	int status = 0;
67021ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
6711da177e4SLinus Torvalds 
672039c4d7aSTrond Myklebust 	/* Try local locking first */
6736d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6746d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6756d34ac19SJ. Bruce Fields 		/* found a conflict */
676039c4d7aSTrond Myklebust 		goto out;
6771da177e4SLinus Torvalds 	}
67821ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
679039c4d7aSTrond Myklebust 
680*011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
681039c4d7aSTrond Myklebust 		goto out_noconflict;
682039c4d7aSTrond Myklebust 
6835eebde23SSuresh Jayaraman 	if (is_local)
684039c4d7aSTrond Myklebust 		goto out_noconflict;
685039c4d7aSTrond Myklebust 
686039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
687039c4d7aSTrond Myklebust out:
6881da177e4SLinus Torvalds 	return status;
689039c4d7aSTrond Myklebust out_noconflict:
690039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
691039c4d7aSTrond Myklebust 	goto out;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	int res = 0;
6971da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
6981da177e4SLinus Torvalds 		case FL_POSIX:
6991da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 		case FL_FLOCK:
7021da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
7031da177e4SLinus Torvalds 			break;
7041da177e4SLinus Torvalds 		default:
7051da177e4SLinus Torvalds 			BUG();
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 	return res;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7105eebde23SSuresh Jayaraman static int
7115eebde23SSuresh Jayaraman do_unlk(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;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/*
7171da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7181da177e4SLinus Torvalds 	 * with locks..
7191da177e4SLinus Torvalds 	 */
72029884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	/* NOTE: special case
7231da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
7241da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
7251da177e4SLinus Torvalds 	 */
7265eebde23SSuresh Jayaraman 	/*
7275eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7285eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7295eebde23SSuresh Jayaraman 	 */
7305eebde23SSuresh Jayaraman 	if (!is_local)
7311da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7321da177e4SLinus Torvalds 	else
7331da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7341da177e4SLinus Torvalds 	return status;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7375eebde23SSuresh Jayaraman static int
7386b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) {
7396b96724eSRicardo Labiaga 	return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
7406b96724eSRicardo Labiaga }
7416b96724eSRicardo Labiaga 
7426b96724eSRicardo Labiaga static int
7435eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7461da177e4SLinus Torvalds 	int status;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	/*
7491da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7501da177e4SLinus Torvalds 	 * with locks..
7511da177e4SLinus Torvalds 	 */
75229884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
75329884df0STrond Myklebust 	if (status != 0)
7541da177e4SLinus Torvalds 		goto out;
7551da177e4SLinus Torvalds 
7565eebde23SSuresh Jayaraman 	/*
7575eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7585eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7595eebde23SSuresh Jayaraman 	 */
7605eebde23SSuresh Jayaraman 	if (!is_local)
7611da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
762c4d7c402STrond Myklebust 	else
7631da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7641da177e4SLinus Torvalds 	if (status < 0)
7651da177e4SLinus Torvalds 		goto out;
7666b96724eSRicardo Labiaga 
7671da177e4SLinus Torvalds 	/*
7686b96724eSRicardo Labiaga 	 * Revalidate the cache if the server has time stamps granular
7696b96724eSRicardo Labiaga 	 * enough to detect subsecond changes.  Otherwise, clear the
7706b96724eSRicardo Labiaga 	 * cache to prevent missing any changes.
7716b96724eSRicardo Labiaga 	 *
7721da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7731da177e4SLinus Torvalds 	 */
77429884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
775*011e2a7fSBryan Schumaker 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
7766b96724eSRicardo Labiaga 		if (is_time_granular(&NFS_SERVER(inode)->time_delta))
7776b96724eSRicardo Labiaga 			__nfs_revalidate_inode(NFS_SERVER(inode), inode);
7786b96724eSRicardo Labiaga 		else
7791da177e4SLinus Torvalds 			nfs_zap_caches(inode);
7806b96724eSRicardo Labiaga 	}
7811da177e4SLinus Torvalds out:
7821da177e4SLinus Torvalds 	return status;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /*
7861da177e4SLinus Torvalds  * Lock a (portion of) a file
7871da177e4SLinus Torvalds  */
7881da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7912116271aSTrond Myklebust 	int ret = -ENOLCK;
7925eebde23SSuresh Jayaraman 	int is_local = 0;
7931da177e4SLinus Torvalds 
7946da24bc9SChuck Lever 	dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
7956da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
7966da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
7971da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
7981da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7996da24bc9SChuck Lever 
80091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
803dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
8042116271aSTrond Myklebust 		goto out_err;
8052116271aSTrond Myklebust 
8065eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
8075eebde23SSuresh Jayaraman 		is_local = 1;
8085eebde23SSuresh Jayaraman 
8092116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
8102116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
8112116271aSTrond Myklebust 		if (ret < 0)
8122116271aSTrond Myklebust 			goto out_err;
8132116271aSTrond Myklebust 	}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
8165eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
8172116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
8185eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
8192116271aSTrond Myklebust 	else
8205eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8212116271aSTrond Myklebust out_err:
8222116271aSTrond Myklebust 	return ret;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /*
8261da177e4SLinus Torvalds  * Lock a (portion of) a file
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8291da177e4SLinus Torvalds {
8305eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8315eebde23SSuresh Jayaraman 	int is_local = 0;
8325eebde23SSuresh Jayaraman 
8336da24bc9SChuck Lever 	dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
8346da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
8356da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
8361da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8391da177e4SLinus Torvalds 		return -ENOLCK;
8401da177e4SLinus Torvalds 
8415eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8425eebde23SSuresh Jayaraman 		is_local = 1;
8435eebde23SSuresh Jayaraman 
8441da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
8451da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
8461da177e4SLinus Torvalds 	fl->fl_start = 0;
8471da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
8505eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8515eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8521da177e4SLinus Torvalds }
853370f6599SJ. Bruce Fields 
8546da24bc9SChuck Lever /*
8556da24bc9SChuck Lever  * There is no protocol support for leases, so we have no way to implement
8566da24bc9SChuck Lever  * them correctly in the face of opens by other clients.
8576da24bc9SChuck Lever  */
858370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
859370f6599SJ. Bruce Fields {
8606da24bc9SChuck Lever 	dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
8616da24bc9SChuck Lever 			file->f_path.dentry->d_parent->d_name.name,
8626da24bc9SChuck Lever 			file->f_path.dentry->d_name.name, arg);
863370f6599SJ. Bruce Fields 	return -EINVAL;
864370f6599SJ. Bruce Fields }
8651788ea6eSJeff Layton 
8660486958fSJeff Layton const struct file_operations nfs_file_operations = {
8670486958fSJeff Layton 	.llseek		= nfs_file_llseek,
8680486958fSJeff Layton 	.read		= do_sync_read,
8690486958fSJeff Layton 	.write		= do_sync_write,
8700486958fSJeff Layton 	.aio_read	= nfs_file_read,
8710486958fSJeff Layton 	.aio_write	= nfs_file_write,
8720486958fSJeff Layton 	.mmap		= nfs_file_mmap,
8730486958fSJeff Layton 	.open		= nfs_file_open,
8740486958fSJeff Layton 	.flush		= nfs_file_flush,
8750486958fSJeff Layton 	.release	= nfs_file_release,
8760486958fSJeff Layton 	.fsync		= nfs_file_fsync,
8770486958fSJeff Layton 	.lock		= nfs_lock,
8780486958fSJeff Layton 	.flock		= nfs_flock,
8790486958fSJeff Layton 	.splice_read	= nfs_file_splice_read,
8800486958fSJeff Layton 	.splice_write	= nfs_file_splice_write,
8810486958fSJeff Layton 	.check_flags	= nfs_check_flags,
8820486958fSJeff Layton 	.setlease	= nfs_setlease,
8830486958fSJeff Layton };
8840486958fSJeff Layton 
8851788ea6eSJeff Layton #ifdef CONFIG_NFS_V4
8861788ea6eSJeff Layton static int
8871788ea6eSJeff Layton nfs4_file_open(struct inode *inode, struct file *filp)
8881788ea6eSJeff Layton {
8890ef97dcfSMiklos Szeredi 	struct nfs_open_context *ctx;
8900ef97dcfSMiklos Szeredi 	struct dentry *dentry = filp->f_path.dentry;
8910ef97dcfSMiklos Szeredi 	struct dentry *parent = NULL;
8920ef97dcfSMiklos Szeredi 	struct inode *dir;
8930ef97dcfSMiklos Szeredi 	unsigned openflags = filp->f_flags;
8940ef97dcfSMiklos Szeredi 	struct iattr attr;
8950ef97dcfSMiklos Szeredi 	int err;
8960ef97dcfSMiklos Szeredi 
8970ef97dcfSMiklos Szeredi 	BUG_ON(inode != dentry->d_inode);
8981788ea6eSJeff Layton 	/*
8990ef97dcfSMiklos Szeredi 	 * If no cached dentry exists or if it's negative, NFSv4 handled the
9000ef97dcfSMiklos Szeredi 	 * opens in ->lookup() or ->create().
9010ef97dcfSMiklos Szeredi 	 *
9020ef97dcfSMiklos Szeredi 	 * We only get this far for a cached positive dentry.  We skipped
9030ef97dcfSMiklos Szeredi 	 * revalidation, so handle it here by dropping the dentry and returning
9040ef97dcfSMiklos Szeredi 	 * -EOPENSTALE.  The VFS will retry the lookup/create/open.
9051788ea6eSJeff Layton 	 */
9060ef97dcfSMiklos Szeredi 
9070ef97dcfSMiklos Szeredi 	dprintk("NFS: open file(%s/%s)\n",
9080ef97dcfSMiklos Szeredi 		dentry->d_parent->d_name.name,
9090ef97dcfSMiklos Szeredi 		dentry->d_name.name);
9100ef97dcfSMiklos Szeredi 
9110ef97dcfSMiklos Szeredi 	if ((openflags & O_ACCMODE) == 3)
9120ef97dcfSMiklos Szeredi 		openflags--;
9130ef97dcfSMiklos Szeredi 
9140ef97dcfSMiklos Szeredi 	/* We can't create new files here */
9150ef97dcfSMiklos Szeredi 	openflags &= ~(O_CREAT|O_EXCL);
9160ef97dcfSMiklos Szeredi 
9170ef97dcfSMiklos Szeredi 	parent = dget_parent(dentry);
9180ef97dcfSMiklos Szeredi 	dir = parent->d_inode;
9190ef97dcfSMiklos Szeredi 
9200ef97dcfSMiklos Szeredi 	ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
9210ef97dcfSMiklos Szeredi 	err = PTR_ERR(ctx);
9220ef97dcfSMiklos Szeredi 	if (IS_ERR(ctx))
9230ef97dcfSMiklos Szeredi 		goto out;
9240ef97dcfSMiklos Szeredi 
9250ef97dcfSMiklos Szeredi 	attr.ia_valid = ATTR_OPEN;
9260ef97dcfSMiklos Szeredi 	if (openflags & O_TRUNC) {
9270ef97dcfSMiklos Szeredi 		attr.ia_valid |= ATTR_SIZE;
9280ef97dcfSMiklos Szeredi 		attr.ia_size = 0;
9290ef97dcfSMiklos Szeredi 		nfs_wb_all(inode);
9300ef97dcfSMiklos Szeredi 	}
9310ef97dcfSMiklos Szeredi 
9320ef97dcfSMiklos Szeredi 	inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr);
9330ef97dcfSMiklos Szeredi 	if (IS_ERR(inode)) {
9340ef97dcfSMiklos Szeredi 		err = PTR_ERR(inode);
9350ef97dcfSMiklos Szeredi 		switch (err) {
9360ef97dcfSMiklos Szeredi 		case -EPERM:
9370ef97dcfSMiklos Szeredi 		case -EACCES:
9380ef97dcfSMiklos Szeredi 		case -EDQUOT:
9390ef97dcfSMiklos Szeredi 		case -ENOSPC:
9400ef97dcfSMiklos Szeredi 		case -EROFS:
9410ef97dcfSMiklos Szeredi 			goto out_put_ctx;
9420ef97dcfSMiklos Szeredi 		default:
9430ef97dcfSMiklos Szeredi 			goto out_drop;
9440ef97dcfSMiklos Szeredi 		}
9450ef97dcfSMiklos Szeredi 	}
9460ef97dcfSMiklos Szeredi 	iput(inode);
9470ef97dcfSMiklos Szeredi 	if (inode != dentry->d_inode)
9480ef97dcfSMiklos Szeredi 		goto out_drop;
9490ef97dcfSMiklos Szeredi 
9500ef97dcfSMiklos Szeredi 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
9510ef97dcfSMiklos Szeredi 	nfs_file_set_open_context(filp, ctx);
9520ef97dcfSMiklos Szeredi 	err = 0;
9530ef97dcfSMiklos Szeredi 
9540ef97dcfSMiklos Szeredi out_put_ctx:
9550ef97dcfSMiklos Szeredi 	put_nfs_open_context(ctx);
9560ef97dcfSMiklos Szeredi out:
9570ef97dcfSMiklos Szeredi 	dput(parent);
9580ef97dcfSMiklos Szeredi 	return err;
9590ef97dcfSMiklos Szeredi 
9600ef97dcfSMiklos Szeredi out_drop:
9610ef97dcfSMiklos Szeredi 	d_drop(dentry);
9620ef97dcfSMiklos Szeredi 	err = -EOPENSTALE;
9630ef97dcfSMiklos Szeredi 	goto out_put_ctx;
9641788ea6eSJeff Layton }
9651788ea6eSJeff Layton 
966a5c58892SBryan Schumaker static int
967a5c58892SBryan Schumaker nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
968a5c58892SBryan Schumaker {
969a5c58892SBryan Schumaker 	int ret;
970a5c58892SBryan Schumaker 	struct inode *inode = file->f_path.dentry->d_inode;
971a5c58892SBryan Schumaker 
972a5c58892SBryan Schumaker 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
973a5c58892SBryan Schumaker 	mutex_lock(&inode->i_mutex);
974a5c58892SBryan Schumaker 	ret = nfs_file_fsync_commit(file, start, end, datasync);
975a5c58892SBryan Schumaker 	if (!ret && !datasync)
976a5c58892SBryan Schumaker 		/* application has asked for meta-data sync */
977a5c58892SBryan Schumaker 		ret = pnfs_layoutcommit_inode(inode, true);
978a5c58892SBryan Schumaker 	mutex_unlock(&inode->i_mutex);
979a5c58892SBryan Schumaker 
980a5c58892SBryan Schumaker 	return ret;
981a5c58892SBryan Schumaker }
982a5c58892SBryan Schumaker 
9831788ea6eSJeff Layton const struct file_operations nfs4_file_operations = {
9841788ea6eSJeff Layton 	.llseek		= nfs_file_llseek,
9851788ea6eSJeff Layton 	.read		= do_sync_read,
9861788ea6eSJeff Layton 	.write		= do_sync_write,
9871788ea6eSJeff Layton 	.aio_read	= nfs_file_read,
9881788ea6eSJeff Layton 	.aio_write	= nfs_file_write,
9891788ea6eSJeff Layton 	.mmap		= nfs_file_mmap,
9901788ea6eSJeff Layton 	.open		= nfs4_file_open,
9911788ea6eSJeff Layton 	.flush		= nfs_file_flush,
9921788ea6eSJeff Layton 	.release	= nfs_file_release,
993a5c58892SBryan Schumaker 	.fsync		= nfs4_file_fsync,
9941788ea6eSJeff Layton 	.lock		= nfs_lock,
9951788ea6eSJeff Layton 	.flock		= nfs_flock,
9961788ea6eSJeff Layton 	.splice_read	= nfs_file_splice_read,
9971788ea6eSJeff Layton 	.splice_write	= nfs_file_splice_write,
9981788ea6eSJeff Layton 	.check_flags	= nfs_check_flags,
9991788ea6eSJeff Layton 	.setlease	= nfs_setlease,
10001788ea6eSJeff Layton };
10011788ea6eSJeff Layton #endif /* CONFIG_NFS_V4 */
1002