xref: /openbmc/linux/fs/nfs/file.c (revision dcfc4f25)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/file.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Changes Copyright (C) 1994 by Florian La Roche
71da177e4SLinus Torvalds  *   - Do not copy data too often around in the kernel.
81da177e4SLinus Torvalds  *   - In nfs_file_read the return value of kmalloc wasn't checked.
91da177e4SLinus Torvalds  *   - Put in a better version of read look-ahead buffering. Original idea
101da177e4SLinus Torvalds  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  Total rewrite of read side for new NFS buffer cache.. Linus.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  nfs regular file handling functions
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
19ddda8e0aSBryan Schumaker #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/fcntl.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/nfs_fs.h>
261da177e4SLinus Torvalds #include <linux/nfs_mount.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/pagemap.h>
29e8edc6e0SAlexey Dobriyan #include <linux/aio.h>
305a0e3ad6STejun Heo #include <linux/gfp.h>
31b608b283STrond Myklebust #include <linux/swap.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <asm/uaccess.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "delegation.h"
3694387fb1STrond Myklebust #include "internal.h"
3791d5b470SChuck Lever #include "iostat.h"
38545db45fSDavid Howells #include "fscache.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 
441da177e4SLinus Torvalds /* Hack for future NFS swap support */
451da177e4SLinus Torvalds #ifndef IS_SWAPFILE
461da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
471da177e4SLinus Torvalds #endif
481da177e4SLinus Torvalds 
49ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
521da177e4SLinus Torvalds 		return -EINVAL;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	return 0;
551da177e4SLinus Torvalds }
5689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /*
591da177e4SLinus Torvalds  * Open file
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds static int
621da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	int res;
651da177e4SLinus Torvalds 
666da24bc9SChuck Lever 	dprintk("NFS: open file(%s/%s)\n",
67cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
68cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
69cc0dd2d1SChuck Lever 
70c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
711da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
721da177e4SLinus Torvalds 	if (res)
731da177e4SLinus Torvalds 		return res;
741da177e4SLinus Torvalds 
7546cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
761da177e4SLinus Torvalds 	return res;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
79ce4ef7c0SBryan Schumaker int
801da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
811da177e4SLinus Torvalds {
826da24bc9SChuck Lever 	dprintk("NFS: release(%s/%s)\n",
836f276e49SRakib Mullick 			filp->f_path.dentry->d_parent->d_name.name,
846f276e49SRakib Mullick 			filp->f_path.dentry->d_name.name);
856da24bc9SChuck Lever 
8691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
8746cb650cSTrond Myklebust 	return nfs_release(inode, filp);
881da177e4SLinus Torvalds }
8989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release);
901da177e4SLinus Torvalds 
91980802e3STrond Myklebust /**
92980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
93980802e3STrond Myklebust  * @inode - pointer to inode struct
94980802e3STrond Myklebust  * @file - pointer to struct file
95980802e3STrond Myklebust  *
96980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
97980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
98980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
99980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
100980802e3STrond Myklebust  * shouldn't trust the cache).
101980802e3STrond Myklebust  */
102980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
103980802e3STrond Myklebust {
104980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
105980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
106980802e3STrond Myklebust 
107d7cf8dd0STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
108d7cf8dd0STrond Myklebust 		goto out_noreval;
109d7cf8dd0STrond Myklebust 
110980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
111980802e3STrond Myklebust 		goto force_reval;
112d7cf8dd0STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
113d7cf8dd0STrond Myklebust 		goto force_reval;
114d7cf8dd0STrond Myklebust 	if (nfs_attribute_timeout(inode))
115d7cf8dd0STrond Myklebust 		goto force_reval;
116d7cf8dd0STrond Myklebust out_noreval:
117fe51beecSTrond Myklebust 	return 0;
118980802e3STrond Myklebust force_reval:
119980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
120980802e3STrond Myklebust }
121980802e3STrond Myklebust 
122ce4ef7c0SBryan Schumaker loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
123980802e3STrond Myklebust {
1246da24bc9SChuck Lever 	dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
125b84e06c5SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
126b84e06c5SChuck Lever 			filp->f_path.dentry->d_name.name,
127b84e06c5SChuck Lever 			offset, origin);
128b84e06c5SChuck Lever 
12906222e49SJosef Bacik 	/*
13006222e49SJosef Bacik 	 * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
13106222e49SJosef Bacik 	 * the cached file length
13206222e49SJosef Bacik 	 */
1336c529617STrond Myklebust 	if (origin != SEEK_SET && origin != SEEK_CUR) {
134980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
135d5e66348STrond Myklebust 
136980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
137980802e3STrond Myklebust 		if (retval < 0)
138980802e3STrond Myklebust 			return (loff_t)retval;
13979835a71SAndi Kleen 	}
140d5e66348STrond Myklebust 
14179835a71SAndi Kleen 	return generic_file_llseek(filp, offset, origin);
142980802e3STrond Myklebust }
14389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek);
144980802e3STrond Myklebust 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1471da177e4SLinus Torvalds  */
148ce4ef7c0SBryan Schumaker int
14975e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1501da177e4SLinus Torvalds {
1516da24bc9SChuck Lever 	struct dentry	*dentry = file->f_path.dentry;
1526da24bc9SChuck Lever 	struct inode	*inode = dentry->d_inode;
1531da177e4SLinus Torvalds 
1546da24bc9SChuck Lever 	dprintk("NFS: flush(%s/%s)\n",
1556da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
1566da24bc9SChuck Lever 			dentry->d_name.name);
1571da177e4SLinus Torvalds 
158c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1591da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1601da177e4SLinus Torvalds 		return 0;
1617b159fc1STrond Myklebust 
16214546c33STrond Myklebust 	/*
16314546c33STrond Myklebust 	 * If we're holding a write delegation, then just start the i/o
16414546c33STrond Myklebust 	 * but don't wait for completion (or send a commit).
16514546c33STrond Myklebust 	 */
166011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
16714546c33STrond Myklebust 		return filemap_fdatawrite(file->f_mapping);
16814546c33STrond Myklebust 
1697fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
170af7fa165STrond Myklebust 	return vfs_fsync(file, 0);
1711da177e4SLinus Torvalds }
17289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_flush);
1731da177e4SLinus Torvalds 
174ce4ef7c0SBryan Schumaker ssize_t
175027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
176027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
1771da177e4SLinus Torvalds {
17801cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
1791da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
1801da177e4SLinus Torvalds 	ssize_t result;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
183a564b8f0SMel Gorman 		return nfs_file_direct_read(iocb, iov, nr_segs, pos, true);
1841da177e4SLinus Torvalds 
1856da24bc9SChuck Lever 	dprintk("NFS: read(%s/%s, %lu@%lu)\n",
1861da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
1876f276e49SRakib Mullick 		(unsigned long) iov_length(iov, nr_segs), (unsigned long) pos);
1881da177e4SLinus Torvalds 
18944b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
1904184dcf2SChuck Lever 	if (!result) {
191027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
1924184dcf2SChuck Lever 		if (result > 0)
1934184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
1944184dcf2SChuck Lever 	}
1951da177e4SLinus Torvalds 	return result;
1961da177e4SLinus Torvalds }
19789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read);
1981da177e4SLinus Torvalds 
199ce4ef7c0SBryan Schumaker ssize_t
200f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
201f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
202f0930fffSJens Axboe 		     unsigned int flags)
2031da177e4SLinus Torvalds {
20401cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2051da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2061da177e4SLinus Torvalds 	ssize_t res;
2071da177e4SLinus Torvalds 
2086da24bc9SChuck Lever 	dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
2091da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2101da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2111da177e4SLinus Torvalds 
21244b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
213aa2f1ef1SChuck Lever 	if (!res) {
214f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
215aa2f1ef1SChuck Lever 		if (res > 0)
216aa2f1ef1SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
217aa2f1ef1SChuck Lever 	}
2181da177e4SLinus Torvalds 	return res;
2191da177e4SLinus Torvalds }
22089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_read);
2211da177e4SLinus Torvalds 
222ce4ef7c0SBryan Schumaker int
2231da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2241da177e4SLinus Torvalds {
22501cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2261da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2271da177e4SLinus Torvalds 	int	status;
2281da177e4SLinus Torvalds 
2296da24bc9SChuck Lever 	dprintk("NFS: mmap(%s/%s)\n",
2301da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2311da177e4SLinus Torvalds 
232e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
233e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
234e1ebfd33STrond Myklebust 	 */
235e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
23694387fb1STrond Myklebust 	if (!status) {
23794387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
238e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
23994387fb1STrond Myklebust 	}
2401da177e4SLinus Torvalds 	return status;
2411da177e4SLinus Torvalds }
24289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /*
2451da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2461da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2471da177e4SLinus Torvalds  * whether any write errors occurred for this process.
248af7fa165STrond Myklebust  *
249af7fa165STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
250af7fa165STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
251af7fa165STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
252af7fa165STrond Myklebust  * This is because the former is used to notify the _next_ call to
25325985edcSLucas De Marchi  * nfs_file_write() that a write error occurred, and hence cause it to
254af7fa165STrond Myklebust  * fall back to doing a synchronous write.
2551da177e4SLinus Torvalds  */
256ce4ef7c0SBryan Schumaker int
257a5c58892SBryan Schumaker nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
2581da177e4SLinus Torvalds {
2597ea80859SChristoph Hellwig 	struct dentry *dentry = file->f_path.dentry;
260cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2611da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
26205990d1bSTrond Myklebust 	int have_error, do_resend, status;
263af7fa165STrond Myklebust 	int ret = 0;
264af7fa165STrond Myklebust 
2656da24bc9SChuck Lever 	dprintk("NFS: fsync file(%s/%s) datasync %d\n",
26654917786SChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
26754917786SChuck Lever 			datasync);
2681da177e4SLinus Torvalds 
26991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
27005990d1bSTrond Myklebust 	do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
271af7fa165STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
272af7fa165STrond Myklebust 	status = nfs_commit_inode(inode, FLUSH_SYNC);
273af7fa165STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
27405990d1bSTrond Myklebust 	if (have_error) {
275af7fa165STrond Myklebust 		ret = xchg(&ctx->error, 0);
27605990d1bSTrond Myklebust 		if (ret)
27705990d1bSTrond Myklebust 			goto out;
27805990d1bSTrond Myklebust 	}
27905990d1bSTrond Myklebust 	if (status < 0) {
280af7fa165STrond Myklebust 		ret = status;
28105990d1bSTrond Myklebust 		goto out;
28205990d1bSTrond Myklebust 	}
28305990d1bSTrond Myklebust 	do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
28405990d1bSTrond Myklebust 	if (do_resend)
28505990d1bSTrond Myklebust 		ret = -EAGAIN;
28605990d1bSTrond Myklebust out:
287a5c58892SBryan Schumaker 	return ret;
288a5c58892SBryan Schumaker }
28989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_fsync_commit);
290a5c58892SBryan Schumaker 
291a5c58892SBryan Schumaker static int
292a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
293a5c58892SBryan Schumaker {
294a5c58892SBryan Schumaker 	int ret;
295a5c58892SBryan Schumaker 	struct inode *inode = file->f_path.dentry->d_inode;
296a5c58892SBryan Schumaker 
29705990d1bSTrond Myklebust 	do {
298a5c58892SBryan Schumaker 		ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
2997b281ee0STrond Myklebust 		if (ret != 0)
30005990d1bSTrond Myklebust 			break;
301a5c58892SBryan Schumaker 		mutex_lock(&inode->i_mutex);
302a5c58892SBryan Schumaker 		ret = nfs_file_fsync_commit(file, start, end, datasync);
30302c24a82SJosef Bacik 		mutex_unlock(&inode->i_mutex);
304dcfc4f25STrond Myklebust 		/*
305dcfc4f25STrond Myklebust 		 * If nfs_file_fsync_commit detected a server reboot, then
306dcfc4f25STrond Myklebust 		 * resend all dirty pages that might have been covered by
307dcfc4f25STrond Myklebust 		 * the NFS_CONTEXT_RESEND_WRITES flag
308dcfc4f25STrond Myklebust 		 */
309dcfc4f25STrond Myklebust 		start = 0;
310dcfc4f25STrond Myklebust 		end = LLONG_MAX;
31105990d1bSTrond Myklebust 	} while (ret == -EAGAIN);
31205990d1bSTrond Myklebust 
313af7fa165STrond Myklebust 	return ret;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds /*
31738c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
31838c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
31938c73044SPeter Staubach  * page cache.
32038c73044SPeter Staubach  *
32138c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
32238c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
32338c73044SPeter Staubach  * page must be completely written to stable storage on the server
32438c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
32538c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
32638c73044SPeter Staubach  * done.
32738c73044SPeter Staubach  *
32838c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
32938c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
33038c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
33138c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
33238c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
33338c73044SPeter Staubach  * and that the new data won't completely replace the old data in
33438c73044SPeter Staubach  * that range of the file.
33538c73044SPeter Staubach  */
33638c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page,
33738c73044SPeter Staubach 			loff_t pos, unsigned len)
33838c73044SPeter Staubach {
33938c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
34038c73044SPeter Staubach 	unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
34138c73044SPeter Staubach 	unsigned int end = offset + len;
34238c73044SPeter Staubach 
34338c73044SPeter Staubach 	if ((file->f_mode & FMODE_READ) &&	/* open for read? */
34438c73044SPeter Staubach 	    !PageUptodate(page) &&		/* Uptodate? */
34538c73044SPeter Staubach 	    !PagePrivate(page) &&		/* i/o request already? */
34638c73044SPeter Staubach 	    pglen &&				/* valid bytes of file? */
34738c73044SPeter Staubach 	    (end < pglen || offset))		/* replace all valid bytes? */
34838c73044SPeter Staubach 		return 1;
34938c73044SPeter Staubach 	return 0;
35038c73044SPeter Staubach }
35138c73044SPeter Staubach 
35238c73044SPeter Staubach /*
3534899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3544899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3554899f9c8SNick Piggin  * data from user space.
3561da177e4SLinus Torvalds  *
3571da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3581da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3591da177e4SLinus Torvalds  */
3604899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3614899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3624899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3631da177e4SLinus Torvalds {
3644899f9c8SNick Piggin 	int ret;
36538c73044SPeter Staubach 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
3664899f9c8SNick Piggin 	struct page *page;
36738c73044SPeter Staubach 	int once_thru = 0;
3684899f9c8SNick Piggin 
369b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
370b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
371b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
372b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
373b7eaefaaSChuck Lever 
37438c73044SPeter Staubach start:
37572cb77f4STrond Myklebust 	/*
37672cb77f4STrond Myklebust 	 * Prevent starvation issues if someone is doing a consistency
37772cb77f4STrond Myklebust 	 * sync-to-disk
37872cb77f4STrond Myklebust 	 */
37972cb77f4STrond Myklebust 	ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
38072cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
38172cb77f4STrond Myklebust 	if (ret)
38272cb77f4STrond Myklebust 		return ret;
38372cb77f4STrond Myklebust 
38454566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3854899f9c8SNick Piggin 	if (!page)
3864899f9c8SNick Piggin 		return -ENOMEM;
3874899f9c8SNick Piggin 	*pagep = page;
3884899f9c8SNick Piggin 
3894899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3904899f9c8SNick Piggin 	if (ret) {
3914899f9c8SNick Piggin 		unlock_page(page);
3924899f9c8SNick Piggin 		page_cache_release(page);
39338c73044SPeter Staubach 	} else if (!once_thru &&
39438c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
39538c73044SPeter Staubach 		once_thru = 1;
39638c73044SPeter Staubach 		ret = nfs_readpage(file, page);
39738c73044SPeter Staubach 		page_cache_release(page);
39838c73044SPeter Staubach 		if (!ret)
39938c73044SPeter Staubach 			goto start;
4004899f9c8SNick Piggin 	}
4014899f9c8SNick Piggin 	return ret;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4044899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
4054899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
4064899f9c8SNick Piggin 			struct page *page, void *fsdata)
4071da177e4SLinus Torvalds {
4084899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
4094899f9c8SNick Piggin 	int status;
4101da177e4SLinus Torvalds 
411b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
412b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
413b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
414b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
415b7eaefaaSChuck Lever 
416efc91ed0STrond Myklebust 	/*
417efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
418efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
419efc91ed0STrond Myklebust 	 */
420efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
421efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
422efc91ed0STrond Myklebust 		unsigned end = offset + len;
423efc91ed0STrond Myklebust 
424efc91ed0STrond Myklebust 		if (pglen == 0) {
425efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
426efc91ed0STrond Myklebust 					end, PAGE_CACHE_SIZE);
427efc91ed0STrond Myklebust 			SetPageUptodate(page);
428efc91ed0STrond Myklebust 		} else if (end >= pglen) {
429efc91ed0STrond Myklebust 			zero_user_segment(page, end, PAGE_CACHE_SIZE);
430efc91ed0STrond Myklebust 			if (offset == 0)
431efc91ed0STrond Myklebust 				SetPageUptodate(page);
432efc91ed0STrond Myklebust 		} else
433efc91ed0STrond Myklebust 			zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
434efc91ed0STrond Myklebust 	}
435efc91ed0STrond Myklebust 
4364899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
4374899f9c8SNick Piggin 
4384899f9c8SNick Piggin 	unlock_page(page);
4394899f9c8SNick Piggin 	page_cache_release(page);
4404899f9c8SNick Piggin 
4413d509e54SChuck Lever 	if (status < 0)
4423d509e54SChuck Lever 		return status;
4432701d086SAndy Adamson 	NFS_I(mapping->host)->write_io += copied;
4443d509e54SChuck Lever 	return copied;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4476b9b3514SDavid Howells /*
4486b9b3514SDavid Howells  * Partially or wholly invalidate a page
4496b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4506b9b3514SDavid Howells  *   page invalidation
451545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4526b9b3514SDavid Howells  * - Caller holds page lock
4536b9b3514SDavid Howells  */
4542ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
455cd52ed35STrond Myklebust {
456b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
457b7eaefaaSChuck Lever 
4581c75950bSTrond Myklebust 	if (offset != 0)
4591c75950bSTrond Myklebust 		return;
460d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
461d56b4ddfSMel Gorman 	nfs_wb_page_cancel(page_file_mapping(page)->host, page);
462545db45fSDavid Howells 
463545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
464cd52ed35STrond Myklebust }
465cd52ed35STrond Myklebust 
4666b9b3514SDavid Howells /*
4676b9b3514SDavid Howells  * Attempt to release the private state associated with a page
468545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4696b9b3514SDavid Howells  * - Caller holds page lock
4706b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4716b9b3514SDavid Howells  */
472cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
473cd52ed35STrond Myklebust {
474b608b283STrond Myklebust 	struct address_space *mapping = page->mapping;
475b608b283STrond Myklebust 
476b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
477b7eaefaaSChuck Lever 
4785cf02d09SJeff Layton 	/* Only do I/O if gfp is a superset of GFP_KERNEL, and we're not
4795cf02d09SJeff Layton 	 * doing this memory reclaim for a fs-related allocation.
4805cf02d09SJeff Layton 	 */
4815cf02d09SJeff Layton 	if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL &&
4825cf02d09SJeff Layton 	    !(current->flags & PF_FSTRANS)) {
483b608b283STrond Myklebust 		int how = FLUSH_SYNC;
484b608b283STrond Myklebust 
485b608b283STrond Myklebust 		/* Don't let kswapd deadlock waiting for OOM RPC calls */
486b608b283STrond Myklebust 		if (current_is_kswapd())
487b608b283STrond Myklebust 			how = 0;
488b608b283STrond Myklebust 		nfs_commit_inode(mapping->host, how);
489b608b283STrond Myklebust 	}
490e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
491545db45fSDavid Howells 	if (PagePrivate(page))
492ddeff520SNikita Danilov 		return 0;
493545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
494e3db7691STrond Myklebust }
495e3db7691STrond Myklebust 
4966b9b3514SDavid Howells /*
4976b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4986b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4996b9b3514SDavid Howells  * destroyed
500545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
5016b9b3514SDavid Howells  * - Caller holds page lock
5026b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
5036b9b3514SDavid Howells  */
504e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
505e3db7691STrond Myklebust {
506d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
507545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
508b7eaefaaSChuck Lever 
509b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
510b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
511b7eaefaaSChuck Lever 
512545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
513b7eaefaaSChuck Lever 	return nfs_wb_page(inode, page);
514cd52ed35STrond Myklebust }
515cd52ed35STrond Myklebust 
516a564b8f0SMel Gorman #ifdef CONFIG_NFS_SWAP
517a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
518a564b8f0SMel Gorman 						sector_t *span)
519a564b8f0SMel Gorman {
520a564b8f0SMel Gorman 	*span = sis->pages;
521a564b8f0SMel Gorman 	return xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 1);
522a564b8f0SMel Gorman }
523a564b8f0SMel Gorman 
524a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file)
525a564b8f0SMel Gorman {
526a564b8f0SMel Gorman 	xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 0);
527a564b8f0SMel Gorman }
528a564b8f0SMel Gorman #endif
529a564b8f0SMel Gorman 
530f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5311da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5321da177e4SLinus Torvalds 	.readpages = nfs_readpages,
5339cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
5341da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5351da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5364899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5374899f9c8SNick Piggin 	.write_end = nfs_write_end,
538cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
539cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5401da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
541074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
542e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
543f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
544a564b8f0SMel Gorman #ifdef CONFIG_NFS_SWAP
545a564b8f0SMel Gorman 	.swap_activate = nfs_swap_activate,
546a564b8f0SMel Gorman 	.swap_deactivate = nfs_swap_deactivate,
547a564b8f0SMel Gorman #endif
5481da177e4SLinus Torvalds };
5491da177e4SLinus Torvalds 
5506b9b3514SDavid Howells /*
5516b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5526b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5536b9b3514SDavid Howells  * shared-writable mapping
5546b9b3514SDavid Howells  */
555c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
55694387fb1STrond Myklebust {
557c2ec175cSNick Piggin 	struct page *page = vmf->page;
55894387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
559b7eaefaaSChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
56094387fb1STrond Myklebust 	unsigned pagelen;
561bc4866b6STrond Myklebust 	int ret = VM_FAULT_NOPAGE;
5624899f9c8SNick Piggin 	struct address_space *mapping;
56394387fb1STrond Myklebust 
564b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
565b7eaefaaSChuck Lever 		dentry->d_parent->d_name.name, dentry->d_name.name,
566b7eaefaaSChuck Lever 		filp->f_mapping->host->i_ino,
567b7eaefaaSChuck Lever 		(long long)page_offset(page));
568b7eaefaaSChuck Lever 
569545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
570545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
571545db45fSDavid Howells 
57294387fb1STrond Myklebust 	lock_page(page);
573d56b4ddfSMel Gorman 	mapping = page_file_mapping(page);
574b7eaefaaSChuck Lever 	if (mapping != dentry->d_inode->i_mapping)
5758b1f9ee5STrond Myklebust 		goto out_unlock;
5768b1f9ee5STrond Myklebust 
5772aeb98f4STrond Myklebust 	wait_on_page_writeback(page);
5782aeb98f4STrond Myklebust 
5794899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5808b1f9ee5STrond Myklebust 	if (pagelen == 0)
5818b1f9ee5STrond Myklebust 		goto out_unlock;
5828b1f9ee5STrond Myklebust 
583bc4866b6STrond Myklebust 	ret = VM_FAULT_LOCKED;
584bc4866b6STrond Myklebust 	if (nfs_flush_incompatible(filp, page) == 0 &&
585bc4866b6STrond Myklebust 	    nfs_updatepage(filp, page, 0, pagelen) == 0)
586bc4866b6STrond Myklebust 		goto out;
5878b1f9ee5STrond Myklebust 
588bc4866b6STrond Myklebust 	ret = VM_FAULT_SIGBUS;
5898b1f9ee5STrond Myklebust out_unlock:
5904899f9c8SNick Piggin 	unlock_page(page);
591bc4866b6STrond Myklebust out:
592bc4866b6STrond Myklebust 	return ret;
59394387fb1STrond Myklebust }
59494387fb1STrond Myklebust 
595f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
59694387fb1STrond Myklebust 	.fault = filemap_fault,
59794387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
59894387fb1STrond Myklebust };
59994387fb1STrond Myklebust 
6007b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
6017b159fc1STrond Myklebust {
6027b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
6037b159fc1STrond Myklebust 
6046b2f3d1fSChristoph Hellwig 	if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
6057b159fc1STrond Myklebust 		return 1;
606cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
6077b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
6087b159fc1STrond Myklebust 		return 1;
6097b159fc1STrond Myklebust 	return 0;
6107b159fc1STrond Myklebust }
6117b159fc1STrond Myklebust 
612ce4ef7c0SBryan Schumaker ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
613027445c3SBadari Pulavarty 		       unsigned long nr_segs, loff_t pos)
6141da177e4SLinus Torvalds {
61501cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
6161da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
6177e381172SChuck Lever 	unsigned long written = 0;
6181da177e4SLinus Torvalds 	ssize_t result;
619027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
622a564b8f0SMel Gorman 		return nfs_file_direct_write(iocb, iov, nr_segs, pos, true);
6231da177e4SLinus Torvalds 
6246da24bc9SChuck Lever 	dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
6251da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
6266da24bc9SChuck Lever 		(unsigned long) count, (long long) pos);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	result = -EBUSY;
6291da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6301da177e4SLinus Torvalds 		goto out_swapfile;
6317d52e862STrond Myklebust 	/*
6327d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6337d52e862STrond Myklebust 	 */
6347d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
6357d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
6361da177e4SLinus Torvalds 		if (result)
6371da177e4SLinus Torvalds 			goto out;
638fe51beecSTrond Myklebust 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	result = count;
6411da177e4SLinus Torvalds 	if (!count)
6421da177e4SLinus Torvalds 		goto out;
6431da177e4SLinus Torvalds 
644027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
6457e381172SChuck Lever 	if (result > 0)
6467e381172SChuck Lever 		written = result;
6477e381172SChuck Lever 
6486b2f3d1fSChristoph Hellwig 	/* Return error values for O_DSYNC and IS_SYNC() */
6497b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
650af7fa165STrond Myklebust 		int err = vfs_fsync(iocb->ki_filp, 0);
651200baa21STrond Myklebust 		if (err < 0)
652200baa21STrond Myklebust 			result = err;
653200baa21STrond Myklebust 	}
6547e381172SChuck Lever 	if (result > 0)
6557e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
6561da177e4SLinus Torvalds out:
6571da177e4SLinus Torvalds 	return result;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds out_swapfile:
6601da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
6611da177e4SLinus Torvalds 	goto out;
6621da177e4SLinus Torvalds }
66389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write);
6641da177e4SLinus Torvalds 
665ce4ef7c0SBryan Schumaker ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
666bf40d343SSuresh Jayaraman 			      struct file *filp, loff_t *ppos,
667bf40d343SSuresh Jayaraman 			      size_t count, unsigned int flags)
668bf40d343SSuresh Jayaraman {
669bf40d343SSuresh Jayaraman 	struct dentry *dentry = filp->f_path.dentry;
670bf40d343SSuresh Jayaraman 	struct inode *inode = dentry->d_inode;
6717e381172SChuck Lever 	unsigned long written = 0;
672bf40d343SSuresh Jayaraman 	ssize_t ret;
673bf40d343SSuresh Jayaraman 
674bf40d343SSuresh Jayaraman 	dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
675bf40d343SSuresh Jayaraman 		dentry->d_parent->d_name.name, dentry->d_name.name,
676bf40d343SSuresh Jayaraman 		(unsigned long) count, (unsigned long long) *ppos);
677bf40d343SSuresh Jayaraman 
678bf40d343SSuresh Jayaraman 	/*
679bf40d343SSuresh Jayaraman 	 * The combination of splice and an O_APPEND destination is disallowed.
680bf40d343SSuresh Jayaraman 	 */
681bf40d343SSuresh Jayaraman 
682bf40d343SSuresh Jayaraman 	ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
6837e381172SChuck Lever 	if (ret > 0)
6847e381172SChuck Lever 		written = ret;
6857e381172SChuck Lever 
686bf40d343SSuresh Jayaraman 	if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
687af7fa165STrond Myklebust 		int err = vfs_fsync(filp, 0);
688bf40d343SSuresh Jayaraman 		if (err < 0)
689bf40d343SSuresh Jayaraman 			ret = err;
690bf40d343SSuresh Jayaraman 	}
6917e381172SChuck Lever 	if (ret > 0)
6927e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
693bf40d343SSuresh Jayaraman 	return ret;
694bf40d343SSuresh Jayaraman }
69589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_write);
696bf40d343SSuresh Jayaraman 
6975eebde23SSuresh Jayaraman static int
6985eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7011da177e4SLinus Torvalds 	int status = 0;
70221ac19d4SSergey Vlasov 	unsigned int saved_type = fl->fl_type;
7031da177e4SLinus Torvalds 
704039c4d7aSTrond Myklebust 	/* Try local locking first */
7056d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
7066d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
7076d34ac19SJ. Bruce Fields 		/* found a conflict */
708039c4d7aSTrond Myklebust 		goto out;
7091da177e4SLinus Torvalds 	}
71021ac19d4SSergey Vlasov 	fl->fl_type = saved_type;
711039c4d7aSTrond Myklebust 
712011e2a7fSBryan Schumaker 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
713039c4d7aSTrond Myklebust 		goto out_noconflict;
714039c4d7aSTrond Myklebust 
7155eebde23SSuresh Jayaraman 	if (is_local)
716039c4d7aSTrond Myklebust 		goto out_noconflict;
717039c4d7aSTrond Myklebust 
718039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
719039c4d7aSTrond Myklebust out:
7201da177e4SLinus Torvalds 	return status;
721039c4d7aSTrond Myklebust out_noconflict:
722039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
723039c4d7aSTrond Myklebust 	goto out;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	int res = 0;
7291da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
7301da177e4SLinus Torvalds 		case FL_POSIX:
7311da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
7321da177e4SLinus Torvalds 			break;
7331da177e4SLinus Torvalds 		case FL_FLOCK:
7341da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
7351da177e4SLinus Torvalds 			break;
7361da177e4SLinus Torvalds 		default:
7371da177e4SLinus Torvalds 			BUG();
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	return res;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7425eebde23SSuresh Jayaraman static int
7435eebde23SSuresh Jayaraman do_unlk(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 	nfs_sync_mapping(filp->f_mapping);
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	/* NOTE: special case
7551da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
7561da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
7571da177e4SLinus Torvalds 	 */
7585eebde23SSuresh Jayaraman 	/*
7595eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7605eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7615eebde23SSuresh Jayaraman 	 */
7625eebde23SSuresh Jayaraman 	if (!is_local)
7631da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7641da177e4SLinus Torvalds 	else
7651da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7661da177e4SLinus Torvalds 	return status;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7695eebde23SSuresh Jayaraman static int
7706b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) {
7716b96724eSRicardo Labiaga 	return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
7726b96724eSRicardo Labiaga }
7736b96724eSRicardo Labiaga 
7746b96724eSRicardo Labiaga static int
7755eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7781da177e4SLinus Torvalds 	int status;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	/*
7811da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7821da177e4SLinus Torvalds 	 * with locks..
7831da177e4SLinus Torvalds 	 */
78429884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
78529884df0STrond Myklebust 	if (status != 0)
7861da177e4SLinus Torvalds 		goto out;
7871da177e4SLinus Torvalds 
7885eebde23SSuresh Jayaraman 	/*
7895eebde23SSuresh Jayaraman 	 * Use local locking if mounted with "-onolock" or with appropriate
7905eebde23SSuresh Jayaraman 	 * "-olocal_lock="
7915eebde23SSuresh Jayaraman 	 */
7925eebde23SSuresh Jayaraman 	if (!is_local)
7931da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
794c4d7c402STrond Myklebust 	else
7951da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7961da177e4SLinus Torvalds 	if (status < 0)
7971da177e4SLinus Torvalds 		goto out;
7986b96724eSRicardo Labiaga 
7991da177e4SLinus Torvalds 	/*
8006b96724eSRicardo Labiaga 	 * Revalidate the cache if the server has time stamps granular
8016b96724eSRicardo Labiaga 	 * enough to detect subsecond changes.  Otherwise, clear the
8026b96724eSRicardo Labiaga 	 * cache to prevent missing any changes.
8036b96724eSRicardo Labiaga 	 *
8041da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
8051da177e4SLinus Torvalds 	 */
80629884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
807011e2a7fSBryan Schumaker 	if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
8086b96724eSRicardo Labiaga 		if (is_time_granular(&NFS_SERVER(inode)->time_delta))
8096b96724eSRicardo Labiaga 			__nfs_revalidate_inode(NFS_SERVER(inode), inode);
8106b96724eSRicardo Labiaga 		else
8111da177e4SLinus Torvalds 			nfs_zap_caches(inode);
8126b96724eSRicardo Labiaga 	}
8131da177e4SLinus Torvalds out:
8141da177e4SLinus Torvalds 	return status;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /*
8181da177e4SLinus Torvalds  * Lock a (portion of) a file
8191da177e4SLinus Torvalds  */
820ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
8232116271aSTrond Myklebust 	int ret = -ENOLCK;
8245eebde23SSuresh Jayaraman 	int is_local = 0;
8251da177e4SLinus Torvalds 
8266da24bc9SChuck Lever 	dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
8276da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
8286da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
8291da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
8301da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
8316da24bc9SChuck Lever 
83291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
835dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
8362116271aSTrond Myklebust 		goto out_err;
8372116271aSTrond Myklebust 
8385eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
8395eebde23SSuresh Jayaraman 		is_local = 1;
8405eebde23SSuresh Jayaraman 
8412116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
8422116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
8432116271aSTrond Myklebust 		if (ret < 0)
8442116271aSTrond Myklebust 			goto out_err;
8452116271aSTrond Myklebust 	}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
8485eebde23SSuresh Jayaraman 		ret = do_getlk(filp, cmd, fl, is_local);
8492116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
8505eebde23SSuresh Jayaraman 		ret = do_unlk(filp, cmd, fl, is_local);
8512116271aSTrond Myklebust 	else
8525eebde23SSuresh Jayaraman 		ret = do_setlk(filp, cmd, fl, is_local);
8532116271aSTrond Myklebust out_err:
8542116271aSTrond Myklebust 	return ret;
8551da177e4SLinus Torvalds }
85689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds /*
8591da177e4SLinus Torvalds  * Lock a (portion of) a file
8601da177e4SLinus Torvalds  */
861ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8621da177e4SLinus Torvalds {
8635eebde23SSuresh Jayaraman 	struct inode *inode = filp->f_mapping->host;
8645eebde23SSuresh Jayaraman 	int is_local = 0;
8655eebde23SSuresh Jayaraman 
8666da24bc9SChuck Lever 	dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
8676da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
8686da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
8691da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8721da177e4SLinus Torvalds 		return -ENOLCK;
8731da177e4SLinus Torvalds 
874ad0fcd4eSJeff Layton 	/*
875ad0fcd4eSJeff Layton 	 * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
876ad0fcd4eSJeff Layton 	 * any standard. In principle we might be able to support LOCK_MAND
877ad0fcd4eSJeff Layton 	 * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
878ad0fcd4eSJeff Layton 	 * NFS code is not set up for it.
879ad0fcd4eSJeff Layton 	 */
880ad0fcd4eSJeff Layton 	if (fl->fl_type & LOCK_MAND)
881ad0fcd4eSJeff Layton 		return -EINVAL;
882ad0fcd4eSJeff Layton 
8835eebde23SSuresh Jayaraman 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
8845eebde23SSuresh Jayaraman 		is_local = 1;
8855eebde23SSuresh Jayaraman 
8861da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
8871da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
8881da177e4SLinus Torvalds 	fl->fl_start = 0;
8891da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
8925eebde23SSuresh Jayaraman 		return do_unlk(filp, cmd, fl, is_local);
8935eebde23SSuresh Jayaraman 	return do_setlk(filp, cmd, fl, is_local);
8941da177e4SLinus Torvalds }
89589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock);
896370f6599SJ. Bruce Fields 
8976da24bc9SChuck Lever /*
8986da24bc9SChuck Lever  * There is no protocol support for leases, so we have no way to implement
8996da24bc9SChuck Lever  * them correctly in the face of opens by other clients.
9006da24bc9SChuck Lever  */
901ce4ef7c0SBryan Schumaker int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
902370f6599SJ. Bruce Fields {
9036da24bc9SChuck Lever 	dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
9046da24bc9SChuck Lever 			file->f_path.dentry->d_parent->d_name.name,
9056da24bc9SChuck Lever 			file->f_path.dentry->d_name.name, arg);
906370f6599SJ. Bruce Fields 	return -EINVAL;
907370f6599SJ. Bruce Fields }
90889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_setlease);
9091788ea6eSJeff Layton 
9100486958fSJeff Layton const struct file_operations nfs_file_operations = {
9110486958fSJeff Layton 	.llseek		= nfs_file_llseek,
9120486958fSJeff Layton 	.read		= do_sync_read,
9130486958fSJeff Layton 	.write		= do_sync_write,
9140486958fSJeff Layton 	.aio_read	= nfs_file_read,
9150486958fSJeff Layton 	.aio_write	= nfs_file_write,
9160486958fSJeff Layton 	.mmap		= nfs_file_mmap,
9170486958fSJeff Layton 	.open		= nfs_file_open,
9180486958fSJeff Layton 	.flush		= nfs_file_flush,
9190486958fSJeff Layton 	.release	= nfs_file_release,
9200486958fSJeff Layton 	.fsync		= nfs_file_fsync,
9210486958fSJeff Layton 	.lock		= nfs_lock,
9220486958fSJeff Layton 	.flock		= nfs_flock,
9230486958fSJeff Layton 	.splice_read	= nfs_file_splice_read,
9240486958fSJeff Layton 	.splice_write	= nfs_file_splice_write,
9250486958fSJeff Layton 	.check_flags	= nfs_check_flags,
9260486958fSJeff Layton 	.setlease	= nfs_setlease,
9270486958fSJeff Layton };
928ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations);
929