xref: /openbmc/linux/fs/nfs/file.c (revision af7fa16506bf9b6323e862a61e14c20555152bb3)
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>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/uaccess.h>
321da177e4SLinus Torvalds #include <asm/system.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include "delegation.h"
3594387fb1STrond Myklebust #include "internal.h"
3691d5b470SChuck Lever #include "iostat.h"
37545db45fSDavid Howells #include "fscache.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static int nfs_file_open(struct inode *, struct file *);
421da177e4SLinus Torvalds static int nfs_file_release(struct inode *, struct file *);
43980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
441da177e4SLinus Torvalds static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
45f0930fffSJens Axboe static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
46f0930fffSJens Axboe 					struct pipe_inode_info *pipe,
47f0930fffSJens Axboe 					size_t count, unsigned int flags);
48027445c3SBadari Pulavarty static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
49027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos);
50bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
51bf40d343SSuresh Jayaraman 					struct file *filp, loff_t *ppos,
52bf40d343SSuresh Jayaraman 					size_t count, unsigned int flags);
53027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
54027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos);
5575e1fcc0SMiklos Szeredi static int  nfs_file_flush(struct file *, fl_owner_t id);
567ea80859SChristoph Hellwig static int  nfs_file_fsync(struct file *, int datasync);
571da177e4SLinus Torvalds static int nfs_check_flags(int flags);
581da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
591da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
60370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
611da177e4SLinus Torvalds 
62f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops;
6394387fb1STrond Myklebust 
644b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = {
65980802e3STrond Myklebust 	.llseek		= nfs_file_llseek,
661da177e4SLinus Torvalds 	.read		= do_sync_read,
671da177e4SLinus Torvalds 	.write		= do_sync_write,
681da177e4SLinus Torvalds 	.aio_read	= nfs_file_read,
691da177e4SLinus Torvalds 	.aio_write	= nfs_file_write,
701da177e4SLinus Torvalds 	.mmap		= nfs_file_mmap,
711da177e4SLinus Torvalds 	.open		= nfs_file_open,
721da177e4SLinus Torvalds 	.flush		= nfs_file_flush,
731da177e4SLinus Torvalds 	.release	= nfs_file_release,
7454917786SChuck Lever 	.fsync		= nfs_file_fsync,
751da177e4SLinus Torvalds 	.lock		= nfs_lock,
761da177e4SLinus Torvalds 	.flock		= nfs_flock,
77f0930fffSJens Axboe 	.splice_read	= nfs_file_splice_read,
78bf40d343SSuresh Jayaraman 	.splice_write	= nfs_file_splice_write,
791da177e4SLinus Torvalds 	.check_flags	= nfs_check_flags,
80370f6599SJ. Bruce Fields 	.setlease	= nfs_setlease,
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
8392e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = {
841da177e4SLinus Torvalds 	.permission	= nfs_permission,
851da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
861da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
871da177e4SLinus Torvalds };
881da177e4SLinus Torvalds 
89b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
9092e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = {
91b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
92b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
93b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
94b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
95b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
96b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
97b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
98b7fa0554SAndreas Gruenbacher };
99b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_v3 */
100b7fa0554SAndreas Gruenbacher 
1011da177e4SLinus Torvalds /* Hack for future NFS swap support */
1021da177e4SLinus Torvalds #ifndef IS_SWAPFILE
1031da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static int nfs_check_flags(int flags)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
1091da177e4SLinus Torvalds 		return -EINVAL;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	return 0;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * Open file
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds static int
1181da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int res;
1211da177e4SLinus Torvalds 
1226da24bc9SChuck Lever 	dprintk("NFS: open file(%s/%s)\n",
123cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
124cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
125cc0dd2d1SChuck Lever 
126c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1271da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
1281da177e4SLinus Torvalds 	if (res)
1291da177e4SLinus Torvalds 		return res;
1301da177e4SLinus Torvalds 
13146cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
1321da177e4SLinus Torvalds 	return res;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static int
1361da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
1371da177e4SLinus Torvalds {
1386da24bc9SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
1396da24bc9SChuck Lever 
1406da24bc9SChuck Lever 	dprintk("NFS: release(%s/%s)\n",
1416da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
1426da24bc9SChuck Lever 			dentry->d_name.name);
1436da24bc9SChuck Lever 
14491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
14546cb650cSTrond Myklebust 	return nfs_release(inode, filp);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
148980802e3STrond Myklebust /**
149980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
150980802e3STrond Myklebust  * @inode - pointer to inode struct
151980802e3STrond Myklebust  * @file - pointer to struct file
152980802e3STrond Myklebust  *
153980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
154980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
155980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
156980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
157980802e3STrond Myklebust  * shouldn't trust the cache).
158980802e3STrond Myklebust  */
159980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
160980802e3STrond Myklebust {
161980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
162980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
163980802e3STrond Myklebust 
164d7cf8dd0STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
165d7cf8dd0STrond Myklebust 		goto out_noreval;
166d7cf8dd0STrond Myklebust 
167980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
168980802e3STrond Myklebust 		goto force_reval;
169d7cf8dd0STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
170d7cf8dd0STrond Myklebust 		goto force_reval;
171d7cf8dd0STrond Myklebust 	if (nfs_attribute_timeout(inode))
172d7cf8dd0STrond Myklebust 		goto force_reval;
173d7cf8dd0STrond Myklebust out_noreval:
174fe51beecSTrond Myklebust 	return 0;
175980802e3STrond Myklebust force_reval:
176980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
177980802e3STrond Myklebust }
178980802e3STrond Myklebust 
179980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
180980802e3STrond Myklebust {
1819465efc9SAndi Kleen 	loff_t loff;
182e89e896dSTrond Myklebust 
1836da24bc9SChuck Lever 	dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
184b84e06c5SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
185b84e06c5SChuck Lever 			filp->f_path.dentry->d_name.name,
186b84e06c5SChuck Lever 			offset, origin);
187b84e06c5SChuck Lever 
188980802e3STrond Myklebust 	/* origin == SEEK_END => we must revalidate the cached file length */
189aec5e175SJosef 'Jeff' Sipek 	if (origin == SEEK_END) {
190980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
191d5e66348STrond Myklebust 
192980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
193980802e3STrond Myklebust 		if (retval < 0)
194980802e3STrond Myklebust 			return (loff_t)retval;
195d5e66348STrond Myklebust 
196d5e66348STrond Myklebust 		spin_lock(&inode->i_lock);
1979465efc9SAndi Kleen 		loff = generic_file_llseek_unlocked(filp, offset, origin);
198d5e66348STrond Myklebust 		spin_unlock(&inode->i_lock);
199d5e66348STrond Myklebust 	} else
200d5e66348STrond Myklebust 		loff = generic_file_llseek_unlocked(filp, offset, origin);
2019465efc9SAndi Kleen 	return loff;
202980802e3STrond Myklebust }
203980802e3STrond Myklebust 
2041da177e4SLinus Torvalds /*
2051da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
2061da177e4SLinus Torvalds  */
2071da177e4SLinus Torvalds static int
20875e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
2091da177e4SLinus Torvalds {
2106da24bc9SChuck Lever 	struct dentry	*dentry = file->f_path.dentry;
2116da24bc9SChuck Lever 	struct inode	*inode = dentry->d_inode;
2121da177e4SLinus Torvalds 
2136da24bc9SChuck Lever 	dprintk("NFS: flush(%s/%s)\n",
2146da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
2156da24bc9SChuck Lever 			dentry->d_name.name);
2161da177e4SLinus Torvalds 
217c2459dc4SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
2181da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
2191da177e4SLinus Torvalds 		return 0;
2207b159fc1STrond Myklebust 
2217fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
222*af7fa165STrond Myklebust 	return vfs_fsync(file, 0);
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static ssize_t
226027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
227027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
2281da177e4SLinus Torvalds {
22901cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
2301da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
2311da177e4SLinus Torvalds 	ssize_t result;
232027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
235027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
2361da177e4SLinus Torvalds 
2376da24bc9SChuck Lever 	dprintk("NFS: read(%s/%s, %lu@%lu)\n",
2381da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2391da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long) pos);
2401da177e4SLinus Torvalds 
24144b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
2424184dcf2SChuck Lever 	if (!result) {
243027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2444184dcf2SChuck Lever 		if (result > 0)
2454184dcf2SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
2464184dcf2SChuck Lever 	}
2471da177e4SLinus Torvalds 	return result;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static ssize_t
251f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
252f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
253f0930fffSJens Axboe 		     unsigned int flags)
2541da177e4SLinus Torvalds {
25501cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2561da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2571da177e4SLinus Torvalds 	ssize_t res;
2581da177e4SLinus Torvalds 
2596da24bc9SChuck Lever 	dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
2601da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2611da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2621da177e4SLinus Torvalds 
26344b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
264aa2f1ef1SChuck Lever 	if (!res) {
265f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
266aa2f1ef1SChuck Lever 		if (res > 0)
267aa2f1ef1SChuck Lever 			nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
268aa2f1ef1SChuck Lever 	}
2691da177e4SLinus Torvalds 	return res;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int
2731da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2741da177e4SLinus Torvalds {
27501cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2761da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2771da177e4SLinus Torvalds 	int	status;
2781da177e4SLinus Torvalds 
2796da24bc9SChuck Lever 	dprintk("NFS: mmap(%s/%s)\n",
2801da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2811da177e4SLinus Torvalds 
282e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
283e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
284e1ebfd33STrond Myklebust 	 */
285e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
28694387fb1STrond Myklebust 	if (!status) {
28794387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
288e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
28994387fb1STrond Myklebust 	}
2901da177e4SLinus Torvalds 	return status;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /*
2941da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2951da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2961da177e4SLinus Torvalds  * whether any write errors occurred for this process.
297*af7fa165STrond Myklebust  *
298*af7fa165STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
299*af7fa165STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
300*af7fa165STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
301*af7fa165STrond Myklebust  * This is because the former is used to notify the _next_ call to
302*af7fa165STrond Myklebust  * nfs_file_write() that a write error occured, and hence cause it to
303*af7fa165STrond Myklebust  * fall back to doing a synchronous write.
3041da177e4SLinus Torvalds  */
3051da177e4SLinus Torvalds static int
3067ea80859SChristoph Hellwig nfs_file_fsync(struct file *file, int datasync)
3071da177e4SLinus Torvalds {
3087ea80859SChristoph Hellwig 	struct dentry *dentry = file->f_path.dentry;
309cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3101da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
311*af7fa165STrond Myklebust 	int have_error, status;
312*af7fa165STrond Myklebust 	int ret = 0;
313*af7fa165STrond Myklebust 
3141da177e4SLinus Torvalds 
3156da24bc9SChuck Lever 	dprintk("NFS: fsync file(%s/%s) datasync %d\n",
31654917786SChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
31754917786SChuck Lever 			datasync);
3181da177e4SLinus Torvalds 
31991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
320*af7fa165STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
321*af7fa165STrond Myklebust 	status = nfs_commit_inode(inode, FLUSH_SYNC);
322*af7fa165STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
323*af7fa165STrond Myklebust 	if (have_error)
324*af7fa165STrond Myklebust 		ret = xchg(&ctx->error, 0);
325*af7fa165STrond Myklebust 	if (!ret)
326*af7fa165STrond Myklebust 		ret = status;
327*af7fa165STrond Myklebust 	return ret;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds /*
33138c73044SPeter Staubach  * Decide whether a read/modify/write cycle may be more efficient
33238c73044SPeter Staubach  * then a modify/write/read cycle when writing to a page in the
33338c73044SPeter Staubach  * page cache.
33438c73044SPeter Staubach  *
33538c73044SPeter Staubach  * The modify/write/read cycle may occur if a page is read before
33638c73044SPeter Staubach  * being completely filled by the writer.  In this situation, the
33738c73044SPeter Staubach  * page must be completely written to stable storage on the server
33838c73044SPeter Staubach  * before it can be refilled by reading in the page from the server.
33938c73044SPeter Staubach  * This can lead to expensive, small, FILE_SYNC mode writes being
34038c73044SPeter Staubach  * done.
34138c73044SPeter Staubach  *
34238c73044SPeter Staubach  * It may be more efficient to read the page first if the file is
34338c73044SPeter Staubach  * open for reading in addition to writing, the page is not marked
34438c73044SPeter Staubach  * as Uptodate, it is not dirty or waiting to be committed,
34538c73044SPeter Staubach  * indicating that it was previously allocated and then modified,
34638c73044SPeter Staubach  * that there were valid bytes of data in that range of the file,
34738c73044SPeter Staubach  * and that the new data won't completely replace the old data in
34838c73044SPeter Staubach  * that range of the file.
34938c73044SPeter Staubach  */
35038c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page,
35138c73044SPeter Staubach 			loff_t pos, unsigned len)
35238c73044SPeter Staubach {
35338c73044SPeter Staubach 	unsigned int pglen = nfs_page_length(page);
35438c73044SPeter Staubach 	unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
35538c73044SPeter Staubach 	unsigned int end = offset + len;
35638c73044SPeter Staubach 
35738c73044SPeter Staubach 	if ((file->f_mode & FMODE_READ) &&	/* open for read? */
35838c73044SPeter Staubach 	    !PageUptodate(page) &&		/* Uptodate? */
35938c73044SPeter Staubach 	    !PagePrivate(page) &&		/* i/o request already? */
36038c73044SPeter Staubach 	    pglen &&				/* valid bytes of file? */
36138c73044SPeter Staubach 	    (end < pglen || offset))		/* replace all valid bytes? */
36238c73044SPeter Staubach 		return 1;
36338c73044SPeter Staubach 	return 0;
36438c73044SPeter Staubach }
36538c73044SPeter Staubach 
36638c73044SPeter Staubach /*
3674899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3684899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3694899f9c8SNick Piggin  * data from user space.
3701da177e4SLinus Torvalds  *
3711da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3721da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3731da177e4SLinus Torvalds  */
3744899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3754899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3764899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3771da177e4SLinus Torvalds {
3784899f9c8SNick Piggin 	int ret;
37938c73044SPeter Staubach 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
3804899f9c8SNick Piggin 	struct page *page;
38138c73044SPeter Staubach 	int once_thru = 0;
3824899f9c8SNick Piggin 
383b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
384b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
385b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
386b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
387b7eaefaaSChuck Lever 
38838c73044SPeter Staubach start:
38972cb77f4STrond Myklebust 	/*
39072cb77f4STrond Myklebust 	 * Prevent starvation issues if someone is doing a consistency
39172cb77f4STrond Myklebust 	 * sync-to-disk
39272cb77f4STrond Myklebust 	 */
39372cb77f4STrond Myklebust 	ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
39472cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
39572cb77f4STrond Myklebust 	if (ret)
39672cb77f4STrond Myklebust 		return ret;
39772cb77f4STrond Myklebust 
39854566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3994899f9c8SNick Piggin 	if (!page)
4004899f9c8SNick Piggin 		return -ENOMEM;
4014899f9c8SNick Piggin 	*pagep = page;
4024899f9c8SNick Piggin 
4034899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
4044899f9c8SNick Piggin 	if (ret) {
4054899f9c8SNick Piggin 		unlock_page(page);
4064899f9c8SNick Piggin 		page_cache_release(page);
40738c73044SPeter Staubach 	} else if (!once_thru &&
40838c73044SPeter Staubach 		   nfs_want_read_modify_write(file, page, pos, len)) {
40938c73044SPeter Staubach 		once_thru = 1;
41038c73044SPeter Staubach 		ret = nfs_readpage(file, page);
41138c73044SPeter Staubach 		page_cache_release(page);
41238c73044SPeter Staubach 		if (!ret)
41338c73044SPeter Staubach 			goto start;
4144899f9c8SNick Piggin 	}
4154899f9c8SNick Piggin 	return ret;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4184899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
4194899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
4204899f9c8SNick Piggin 			struct page *page, void *fsdata)
4211da177e4SLinus Torvalds {
4224899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
4234899f9c8SNick Piggin 	int status;
4241da177e4SLinus Torvalds 
425b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
426b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
427b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
428b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
429b7eaefaaSChuck Lever 
430efc91ed0STrond Myklebust 	/*
431efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
432efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
433efc91ed0STrond Myklebust 	 */
434efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
435efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
436efc91ed0STrond Myklebust 		unsigned end = offset + len;
437efc91ed0STrond Myklebust 
438efc91ed0STrond Myklebust 		if (pglen == 0) {
439efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
440efc91ed0STrond Myklebust 					end, PAGE_CACHE_SIZE);
441efc91ed0STrond Myklebust 			SetPageUptodate(page);
442efc91ed0STrond Myklebust 		} else if (end >= pglen) {
443efc91ed0STrond Myklebust 			zero_user_segment(page, end, PAGE_CACHE_SIZE);
444efc91ed0STrond Myklebust 			if (offset == 0)
445efc91ed0STrond Myklebust 				SetPageUptodate(page);
446efc91ed0STrond Myklebust 		} else
447efc91ed0STrond Myklebust 			zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
448efc91ed0STrond Myklebust 	}
449efc91ed0STrond Myklebust 
4504899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
4514899f9c8SNick Piggin 
4524899f9c8SNick Piggin 	unlock_page(page);
4534899f9c8SNick Piggin 	page_cache_release(page);
4544899f9c8SNick Piggin 
4553d509e54SChuck Lever 	if (status < 0)
4563d509e54SChuck Lever 		return status;
4573d509e54SChuck Lever 	return copied;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4606b9b3514SDavid Howells /*
4616b9b3514SDavid Howells  * Partially or wholly invalidate a page
4626b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4636b9b3514SDavid Howells  *   page invalidation
464545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4656b9b3514SDavid Howells  * - Caller holds page lock
4666b9b3514SDavid Howells  */
4672ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
468cd52ed35STrond Myklebust {
469b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
470b7eaefaaSChuck Lever 
4711c75950bSTrond Myklebust 	if (offset != 0)
4721c75950bSTrond Myklebust 		return;
473d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
4741b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
475545db45fSDavid Howells 
476545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
477cd52ed35STrond Myklebust }
478cd52ed35STrond Myklebust 
4796b9b3514SDavid Howells /*
4806b9b3514SDavid Howells  * Attempt to release the private state associated with a page
481545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4826b9b3514SDavid Howells  * - Caller holds page lock
4836b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4846b9b3514SDavid Howells  */
485cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
486cd52ed35STrond Myklebust {
487b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
488b7eaefaaSChuck Lever 
489d812e575STrond Myklebust 	/* Only do I/O if gfp is a superset of GFP_KERNEL */
490d812e575STrond Myklebust 	if ((gfp & GFP_KERNEL) == GFP_KERNEL)
49182be934aSTrond Myklebust 		nfs_wb_page(page->mapping->host, page);
492e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
493545db45fSDavid Howells 	if (PagePrivate(page))
494ddeff520SNikita Danilov 		return 0;
495545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
496e3db7691STrond Myklebust }
497e3db7691STrond Myklebust 
4986b9b3514SDavid Howells /*
4996b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
5006b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
5016b9b3514SDavid Howells  * destroyed
502545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
5036b9b3514SDavid Howells  * - Caller holds page lock
5046b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
5056b9b3514SDavid Howells  */
506e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
507e3db7691STrond Myklebust {
508b7eaefaaSChuck Lever 	struct inode *inode = page->mapping->host;
509545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
510b7eaefaaSChuck Lever 
511b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
512b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
513b7eaefaaSChuck Lever 
514545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
515b7eaefaaSChuck Lever 	return nfs_wb_page(inode, page);
516cd52ed35STrond Myklebust }
517cd52ed35STrond Myklebust 
518f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5191da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5201da177e4SLinus Torvalds 	.readpages = nfs_readpages,
5219cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
5221da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5231da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5244899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5254899f9c8SNick Piggin 	.write_end = nfs_write_end,
526cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
527cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5281da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
529074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
530e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
531f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
5321da177e4SLinus Torvalds };
5331da177e4SLinus Torvalds 
5346b9b3514SDavid Howells /*
5356b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5366b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5376b9b3514SDavid Howells  * shared-writable mapping
5386b9b3514SDavid Howells  */
539c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
54094387fb1STrond Myklebust {
541c2ec175cSNick Piggin 	struct page *page = vmf->page;
54294387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
543b7eaefaaSChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
54494387fb1STrond Myklebust 	unsigned pagelen;
54594387fb1STrond Myklebust 	int ret = -EINVAL;
5464899f9c8SNick Piggin 	struct address_space *mapping;
54794387fb1STrond Myklebust 
548b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
549b7eaefaaSChuck Lever 		dentry->d_parent->d_name.name, dentry->d_name.name,
550b7eaefaaSChuck Lever 		filp->f_mapping->host->i_ino,
551b7eaefaaSChuck Lever 		(long long)page_offset(page));
552b7eaefaaSChuck Lever 
553545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
554545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
555545db45fSDavid Howells 
55694387fb1STrond Myklebust 	lock_page(page);
5574899f9c8SNick Piggin 	mapping = page->mapping;
558b7eaefaaSChuck Lever 	if (mapping != dentry->d_inode->i_mapping)
5598b1f9ee5STrond Myklebust 		goto out_unlock;
5608b1f9ee5STrond Myklebust 
5618b1f9ee5STrond Myklebust 	ret = 0;
5624899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5638b1f9ee5STrond Myklebust 	if (pagelen == 0)
5648b1f9ee5STrond Myklebust 		goto out_unlock;
5658b1f9ee5STrond Myklebust 
5668b1f9ee5STrond Myklebust 	ret = nfs_flush_incompatible(filp, page);
5678b1f9ee5STrond Myklebust 	if (ret != 0)
5688b1f9ee5STrond Myklebust 		goto out_unlock;
5698b1f9ee5STrond Myklebust 
5708b1f9ee5STrond Myklebust 	ret = nfs_updatepage(filp, page, 0, pagelen);
5718b1f9ee5STrond Myklebust out_unlock:
5727fdf5230STrond Myklebust 	if (!ret)
5737fdf5230STrond Myklebust 		return VM_FAULT_LOCKED;
5744899f9c8SNick Piggin 	unlock_page(page);
5757fdf5230STrond Myklebust 	return VM_FAULT_SIGBUS;
57694387fb1STrond Myklebust }
57794387fb1STrond Myklebust 
578f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
57994387fb1STrond Myklebust 	.fault = filemap_fault,
58094387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
58194387fb1STrond Myklebust };
58294387fb1STrond Myklebust 
5837b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
5847b159fc1STrond Myklebust {
5857b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5867b159fc1STrond Myklebust 
5876b2f3d1fSChristoph Hellwig 	if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
5887b159fc1STrond Myklebust 		return 1;
589cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
5907b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
5917b159fc1STrond Myklebust 		return 1;
5927b159fc1STrond Myklebust 	return 0;
5937b159fc1STrond Myklebust }
5947b159fc1STrond Myklebust 
595027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
596027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
5971da177e4SLinus Torvalds {
59801cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
5991da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
6007e381172SChuck Lever 	unsigned long written = 0;
6011da177e4SLinus Torvalds 	ssize_t result;
602027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
605027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
6061da177e4SLinus Torvalds 
6076da24bc9SChuck Lever 	dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
6081da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
6096da24bc9SChuck Lever 		(unsigned long) count, (long long) pos);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	result = -EBUSY;
6121da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6131da177e4SLinus Torvalds 		goto out_swapfile;
6147d52e862STrond Myklebust 	/*
6157d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6167d52e862STrond Myklebust 	 */
6177d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
6187d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
6191da177e4SLinus Torvalds 		if (result)
6201da177e4SLinus Torvalds 			goto out;
621fe51beecSTrond Myklebust 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	result = count;
6241da177e4SLinus Torvalds 	if (!count)
6251da177e4SLinus Torvalds 		goto out;
6261da177e4SLinus Torvalds 
627027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
6287e381172SChuck Lever 	if (result > 0)
6297e381172SChuck Lever 		written = result;
6307e381172SChuck Lever 
6316b2f3d1fSChristoph Hellwig 	/* Return error values for O_DSYNC and IS_SYNC() */
6327b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
633*af7fa165STrond Myklebust 		int err = vfs_fsync(iocb->ki_filp, 0);
634200baa21STrond Myklebust 		if (err < 0)
635200baa21STrond Myklebust 			result = err;
636200baa21STrond Myklebust 	}
6377e381172SChuck Lever 	if (result > 0)
6387e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
6391da177e4SLinus Torvalds out:
6401da177e4SLinus Torvalds 	return result;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds out_swapfile:
6431da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
6441da177e4SLinus Torvalds 	goto out;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
647bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
648bf40d343SSuresh Jayaraman 				     struct file *filp, loff_t *ppos,
649bf40d343SSuresh Jayaraman 				     size_t count, unsigned int flags)
650bf40d343SSuresh Jayaraman {
651bf40d343SSuresh Jayaraman 	struct dentry *dentry = filp->f_path.dentry;
652bf40d343SSuresh Jayaraman 	struct inode *inode = dentry->d_inode;
6537e381172SChuck Lever 	unsigned long written = 0;
654bf40d343SSuresh Jayaraman 	ssize_t ret;
655bf40d343SSuresh Jayaraman 
656bf40d343SSuresh Jayaraman 	dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
657bf40d343SSuresh Jayaraman 		dentry->d_parent->d_name.name, dentry->d_name.name,
658bf40d343SSuresh Jayaraman 		(unsigned long) count, (unsigned long long) *ppos);
659bf40d343SSuresh Jayaraman 
660bf40d343SSuresh Jayaraman 	/*
661bf40d343SSuresh Jayaraman 	 * The combination of splice and an O_APPEND destination is disallowed.
662bf40d343SSuresh Jayaraman 	 */
663bf40d343SSuresh Jayaraman 
664bf40d343SSuresh Jayaraman 	ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
6657e381172SChuck Lever 	if (ret > 0)
6667e381172SChuck Lever 		written = ret;
6677e381172SChuck Lever 
668bf40d343SSuresh Jayaraman 	if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
669*af7fa165STrond Myklebust 		int err = vfs_fsync(filp, 0);
670bf40d343SSuresh Jayaraman 		if (err < 0)
671bf40d343SSuresh Jayaraman 			ret = err;
672bf40d343SSuresh Jayaraman 	}
6737e381172SChuck Lever 	if (ret > 0)
6747e381172SChuck Lever 		nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
675bf40d343SSuresh Jayaraman 	return ret;
676bf40d343SSuresh Jayaraman }
677bf40d343SSuresh Jayaraman 
6781da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6811da177e4SLinus Torvalds 	int status = 0;
6821da177e4SLinus Torvalds 
683039c4d7aSTrond Myklebust 	/* Try local locking first */
6846d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6856d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6866d34ac19SJ. Bruce Fields 		/* found a conflict */
687039c4d7aSTrond Myklebust 		goto out;
6881da177e4SLinus Torvalds 	}
689039c4d7aSTrond Myklebust 
690039c4d7aSTrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
691039c4d7aSTrond Myklebust 		goto out_noconflict;
692039c4d7aSTrond Myklebust 
693039c4d7aSTrond Myklebust 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
694039c4d7aSTrond Myklebust 		goto out_noconflict;
695039c4d7aSTrond Myklebust 
696039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
697039c4d7aSTrond Myklebust out:
6981da177e4SLinus Torvalds 	return status;
699039c4d7aSTrond Myklebust out_noconflict:
700039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
701039c4d7aSTrond Myklebust 	goto out;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	int res = 0;
7071da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
7081da177e4SLinus Torvalds 		case FL_POSIX:
7091da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
7101da177e4SLinus Torvalds 			break;
7111da177e4SLinus Torvalds 		case FL_FLOCK:
7121da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
7131da177e4SLinus Torvalds 			break;
7141da177e4SLinus Torvalds 		default:
7151da177e4SLinus Torvalds 			BUG();
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 	if (res < 0)
71846bae1a9SNeil Brown 		dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
71946bae1a9SNeil Brown 			" - error %d!\n",
7203110ff80SHarvey Harrison 				__func__, res);
7211da177e4SLinus Torvalds 	return res;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7271da177e4SLinus Torvalds 	int status;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	/*
7301da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7311da177e4SLinus Torvalds 	 * with locks..
7321da177e4SLinus Torvalds 	 */
73329884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/* NOTE: special case
7361da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
7371da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
7381da177e4SLinus Torvalds 	 */
7391da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
7401da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
7411da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7421da177e4SLinus Torvalds 	else
7431da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7441da177e4SLinus Torvalds 	return status;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7501da177e4SLinus Torvalds 	int status;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/*
7531da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7541da177e4SLinus Torvalds 	 * with locks..
7551da177e4SLinus Torvalds 	 */
75629884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
75729884df0STrond Myklebust 	if (status != 0)
7581da177e4SLinus Torvalds 		goto out;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
761c4d7c402STrond Myklebust 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
7621da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
763c4d7c402STrond Myklebust 	else
7641da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7651da177e4SLinus Torvalds 	if (status < 0)
7661da177e4SLinus Torvalds 		goto out;
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * Make sure we clear the cache whenever we try to get the lock.
7691da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7701da177e4SLinus Torvalds 	 */
77129884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
772b5418383STrond Myklebust 	if (!nfs_have_delegation(inode, FMODE_READ))
7731da177e4SLinus Torvalds 		nfs_zap_caches(inode);
7741da177e4SLinus Torvalds out:
7751da177e4SLinus Torvalds 	return status;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /*
7791da177e4SLinus Torvalds  * Lock a (portion of) a file
7801da177e4SLinus Torvalds  */
7811da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7842116271aSTrond Myklebust 	int ret = -ENOLCK;
7851da177e4SLinus Torvalds 
7866da24bc9SChuck Lever 	dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
7876da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
7886da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
7891da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
7901da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7916da24bc9SChuck Lever 
79291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
795dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
7962116271aSTrond Myklebust 		goto out_err;
7972116271aSTrond Myklebust 
7982116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
7992116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
8002116271aSTrond Myklebust 		if (ret < 0)
8012116271aSTrond Myklebust 			goto out_err;
8022116271aSTrond Myklebust 	}
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
8052116271aSTrond Myklebust 		ret = do_getlk(filp, cmd, fl);
8062116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
8072116271aSTrond Myklebust 		ret = do_unlk(filp, cmd, fl);
8082116271aSTrond Myklebust 	else
8092116271aSTrond Myklebust 		ret = do_setlk(filp, cmd, fl);
8102116271aSTrond Myklebust out_err:
8112116271aSTrond Myklebust 	return ret;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds /*
8151da177e4SLinus Torvalds  * Lock a (portion of) a file
8161da177e4SLinus Torvalds  */
8171da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8181da177e4SLinus Torvalds {
8196da24bc9SChuck Lever 	dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
8206da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
8216da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
8221da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8251da177e4SLinus Torvalds 		return -ENOLCK;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
8281da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
8291da177e4SLinus Torvalds 	fl->fl_start = 0;
8301da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
8331da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
8341da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
8351da177e4SLinus Torvalds }
836370f6599SJ. Bruce Fields 
8376da24bc9SChuck Lever /*
8386da24bc9SChuck Lever  * There is no protocol support for leases, so we have no way to implement
8396da24bc9SChuck Lever  * them correctly in the face of opens by other clients.
8406da24bc9SChuck Lever  */
841370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
842370f6599SJ. Bruce Fields {
8436da24bc9SChuck Lever 	dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
8446da24bc9SChuck Lever 			file->f_path.dentry->d_parent->d_name.name,
8456da24bc9SChuck Lever 			file->f_path.dentry->d_name.name, arg);
8466da24bc9SChuck Lever 
847370f6599SJ. Bruce Fields 	return -EINVAL;
848370f6599SJ. Bruce Fields }
849