xref: /openbmc/linux/fs/nfs/file.c (revision 82be934a)
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/slab.h>
281da177e4SLinus Torvalds #include <linux/pagemap.h>
29e8edc6e0SAlexey Dobriyan #include <linux/aio.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);
5654917786SChuck Lever static int  nfs_file_fsync(struct file *, struct dentry *dentry, 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 
1261da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
1271da177e4SLinus Torvalds 	if (res)
1281da177e4SLinus Torvalds 		return res;
1291da177e4SLinus Torvalds 
13091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
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 
164980802e3STrond Myklebust 	if (server->flags & NFS_MOUNT_NOAC)
165980802e3STrond Myklebust 		goto force_reval;
166980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
167980802e3STrond Myklebust 		goto force_reval;
168980802e3STrond Myklebust 	if (nfsi->npages != 0)
169980802e3STrond Myklebust 		return 0;
17055296809SChuck Lever 	if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
171fe51beecSTrond Myklebust 		return 0;
172980802e3STrond Myklebust force_reval:
173980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
174980802e3STrond Myklebust }
175980802e3STrond Myklebust 
176980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
177980802e3STrond Myklebust {
1789465efc9SAndi Kleen 	loff_t loff;
179e89e896dSTrond Myklebust 
1806da24bc9SChuck Lever 	dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
181b84e06c5SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
182b84e06c5SChuck Lever 			filp->f_path.dentry->d_name.name,
183b84e06c5SChuck Lever 			offset, origin);
184b84e06c5SChuck Lever 
185980802e3STrond Myklebust 	/* origin == SEEK_END => we must revalidate the cached file length */
186aec5e175SJosef 'Jeff' Sipek 	if (origin == SEEK_END) {
187980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
188d5e66348STrond Myklebust 
189980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
190980802e3STrond Myklebust 		if (retval < 0)
191980802e3STrond Myklebust 			return (loff_t)retval;
192d5e66348STrond Myklebust 
193d5e66348STrond Myklebust 		spin_lock(&inode->i_lock);
1949465efc9SAndi Kleen 		loff = generic_file_llseek_unlocked(filp, offset, origin);
195d5e66348STrond Myklebust 		spin_unlock(&inode->i_lock);
196d5e66348STrond Myklebust 	} else
197d5e66348STrond Myklebust 		loff = generic_file_llseek_unlocked(filp, offset, origin);
1989465efc9SAndi Kleen 	return loff;
199980802e3STrond Myklebust }
200980802e3STrond Myklebust 
2011da177e4SLinus Torvalds /*
20254917786SChuck Lever  * Helper for nfs_file_flush() and nfs_file_fsync()
2037b159fc1STrond Myklebust  *
2047b159fc1STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
2057b159fc1STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
2067b159fc1STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
2077b159fc1STrond Myklebust  * This is because the former is used to notify the _next_ call to
2087b159fc1STrond Myklebust  * nfs_file_write() that a write error occured, and hence cause it to
2097b159fc1STrond Myklebust  * fall back to doing a synchronous write.
2107b159fc1STrond Myklebust  */
2117b159fc1STrond Myklebust static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
2127b159fc1STrond Myklebust {
2137b159fc1STrond Myklebust 	int have_error, status;
2147b159fc1STrond Myklebust 	int ret = 0;
2157b159fc1STrond Myklebust 
2167b159fc1STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
2177b159fc1STrond Myklebust 	status = nfs_wb_all(inode);
2187b159fc1STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
2197b159fc1STrond Myklebust 	if (have_error)
2207b159fc1STrond Myklebust 		ret = xchg(&ctx->error, 0);
2217b159fc1STrond Myklebust 	if (!ret)
2227b159fc1STrond Myklebust 		ret = status;
2237b159fc1STrond Myklebust 	return ret;
2247b159fc1STrond Myklebust }
2257b159fc1STrond Myklebust 
2267b159fc1STrond Myklebust /*
2271da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
2281da177e4SLinus Torvalds  */
2291da177e4SLinus Torvalds static int
23075e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
2311da177e4SLinus Torvalds {
232cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2336da24bc9SChuck Lever 	struct dentry	*dentry = file->f_path.dentry;
2346da24bc9SChuck Lever 	struct inode	*inode = dentry->d_inode;
2351da177e4SLinus Torvalds 
2366da24bc9SChuck Lever 	dprintk("NFS: flush(%s/%s)\n",
2376da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
2386da24bc9SChuck Lever 			dentry->d_name.name);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
2411da177e4SLinus Torvalds 		return 0;
24291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
2437b159fc1STrond Myklebust 
2447fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
2457fe5c398STrond Myklebust 	return nfs_do_fsync(ctx, inode);
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds static ssize_t
249027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
250027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
2511da177e4SLinus Torvalds {
25201cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
2531da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
2541da177e4SLinus Torvalds 	ssize_t result;
255027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
258027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
2591da177e4SLinus Torvalds 
2606da24bc9SChuck Lever 	dprintk("NFS: read(%s/%s, %lu@%lu)\n",
2611da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2621da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long) pos);
2631da177e4SLinus Torvalds 
26444b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
26591d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
2661da177e4SLinus Torvalds 	if (!result)
267027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2681da177e4SLinus Torvalds 	return result;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds static ssize_t
272f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
273f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
274f0930fffSJens Axboe 		     unsigned int flags)
2751da177e4SLinus Torvalds {
27601cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2771da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2781da177e4SLinus Torvalds 	ssize_t res;
2791da177e4SLinus Torvalds 
2806da24bc9SChuck Lever 	dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
2811da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2821da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2831da177e4SLinus Torvalds 
28444b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
2851da177e4SLinus Torvalds 	if (!res)
286f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
2871da177e4SLinus Torvalds 	return res;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static int
2911da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2921da177e4SLinus Torvalds {
29301cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2941da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2951da177e4SLinus Torvalds 	int	status;
2961da177e4SLinus Torvalds 
2976da24bc9SChuck Lever 	dprintk("NFS: mmap(%s/%s)\n",
2981da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2991da177e4SLinus Torvalds 
300e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
301e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
302e1ebfd33STrond Myklebust 	 */
303e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
30494387fb1STrond Myklebust 	if (!status) {
30594387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
306e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
30794387fb1STrond Myklebust 	}
3081da177e4SLinus Torvalds 	return status;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
3131da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
3141da177e4SLinus Torvalds  * whether any write errors occurred for this process.
3151da177e4SLinus Torvalds  */
3161da177e4SLinus Torvalds static int
31754917786SChuck Lever nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
3181da177e4SLinus Torvalds {
319cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3201da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3211da177e4SLinus Torvalds 
3226da24bc9SChuck Lever 	dprintk("NFS: fsync file(%s/%s) datasync %d\n",
32354917786SChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
32454917786SChuck Lever 			datasync);
3251da177e4SLinus Torvalds 
32691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
3277b159fc1STrond Myklebust 	return nfs_do_fsync(ctx, inode);
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 
48982be934aSTrond Myklebust 	if (gfp & __GFP_WAIT)
49082be934aSTrond Myklebust 		nfs_wb_page(page->mapping->host, page);
491e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
492545db45fSDavid Howells 	if (PagePrivate(page))
493ddeff520SNikita Danilov 		return 0;
494545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
495e3db7691STrond Myklebust }
496e3db7691STrond Myklebust 
4976b9b3514SDavid Howells /*
4986b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4996b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
5006b9b3514SDavid Howells  * destroyed
501545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
5026b9b3514SDavid Howells  * - Caller holds page lock
5036b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
5046b9b3514SDavid Howells  */
505e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
506e3db7691STrond Myklebust {
507b7eaefaaSChuck Lever 	struct inode *inode = page->mapping->host;
508545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
509b7eaefaaSChuck Lever 
510b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
511b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
512b7eaefaaSChuck Lever 
513545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
514b7eaefaaSChuck Lever 	return nfs_wb_page(inode, page);
515cd52ed35STrond Myklebust }
516cd52ed35STrond Myklebust 
517f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
5181da177e4SLinus Torvalds 	.readpage = nfs_readpage,
5191da177e4SLinus Torvalds 	.readpages = nfs_readpages,
5209cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
5211da177e4SLinus Torvalds 	.writepage = nfs_writepage,
5221da177e4SLinus Torvalds 	.writepages = nfs_writepages,
5234899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
5244899f9c8SNick Piggin 	.write_end = nfs_write_end,
525cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
526cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
5271da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
528074cc1deSTrond Myklebust 	.migratepage = nfs_migrate_page,
529e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
530f590f333SAndi Kleen 	.error_remove_page = generic_error_remove_page,
5311da177e4SLinus Torvalds };
5321da177e4SLinus Torvalds 
5336b9b3514SDavid Howells /*
5346b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
5356b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
5366b9b3514SDavid Howells  * shared-writable mapping
5376b9b3514SDavid Howells  */
538c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
53994387fb1STrond Myklebust {
540c2ec175cSNick Piggin 	struct page *page = vmf->page;
54194387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
542b7eaefaaSChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
54394387fb1STrond Myklebust 	unsigned pagelen;
54494387fb1STrond Myklebust 	int ret = -EINVAL;
5454899f9c8SNick Piggin 	struct address_space *mapping;
54694387fb1STrond Myklebust 
547b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
548b7eaefaaSChuck Lever 		dentry->d_parent->d_name.name, dentry->d_name.name,
549b7eaefaaSChuck Lever 		filp->f_mapping->host->i_ino,
550b7eaefaaSChuck Lever 		(long long)page_offset(page));
551b7eaefaaSChuck Lever 
552545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
553545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
554545db45fSDavid Howells 
55594387fb1STrond Myklebust 	lock_page(page);
5564899f9c8SNick Piggin 	mapping = page->mapping;
557b7eaefaaSChuck Lever 	if (mapping != dentry->d_inode->i_mapping)
5588b1f9ee5STrond Myklebust 		goto out_unlock;
5598b1f9ee5STrond Myklebust 
5608b1f9ee5STrond Myklebust 	ret = 0;
5614899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5628b1f9ee5STrond Myklebust 	if (pagelen == 0)
5638b1f9ee5STrond Myklebust 		goto out_unlock;
5648b1f9ee5STrond Myklebust 
5658b1f9ee5STrond Myklebust 	ret = nfs_flush_incompatible(filp, page);
5668b1f9ee5STrond Myklebust 	if (ret != 0)
5678b1f9ee5STrond Myklebust 		goto out_unlock;
5688b1f9ee5STrond Myklebust 
5698b1f9ee5STrond Myklebust 	ret = nfs_updatepage(filp, page, 0, pagelen);
5708b1f9ee5STrond Myklebust out_unlock:
5717fdf5230STrond Myklebust 	if (!ret)
5727fdf5230STrond Myklebust 		return VM_FAULT_LOCKED;
5734899f9c8SNick Piggin 	unlock_page(page);
5747fdf5230STrond Myklebust 	return VM_FAULT_SIGBUS;
57594387fb1STrond Myklebust }
57694387fb1STrond Myklebust 
577f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = {
57894387fb1STrond Myklebust 	.fault = filemap_fault,
57994387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
58094387fb1STrond Myklebust };
58194387fb1STrond Myklebust 
5827b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
5837b159fc1STrond Myklebust {
5847b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5857b159fc1STrond Myklebust 
5866b2f3d1fSChristoph Hellwig 	if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
5877b159fc1STrond Myklebust 		return 1;
588cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
5897b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
5907b159fc1STrond Myklebust 		return 1;
5917b159fc1STrond Myklebust 	return 0;
5927b159fc1STrond Myklebust }
5937b159fc1STrond Myklebust 
594027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
595027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
5961da177e4SLinus Torvalds {
59701cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
5981da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
5991da177e4SLinus Torvalds 	ssize_t result;
600027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
603027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
6041da177e4SLinus Torvalds 
6056da24bc9SChuck Lever 	dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
6061da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
6076da24bc9SChuck Lever 		(unsigned long) count, (long long) pos);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	result = -EBUSY;
6101da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
6111da177e4SLinus Torvalds 		goto out_swapfile;
6127d52e862STrond Myklebust 	/*
6137d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
6147d52e862STrond Myklebust 	 */
6157d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
6167d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
6171da177e4SLinus Torvalds 		if (result)
6181da177e4SLinus Torvalds 			goto out;
619fe51beecSTrond Myklebust 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	result = count;
6221da177e4SLinus Torvalds 	if (!count)
6231da177e4SLinus Torvalds 		goto out;
6241da177e4SLinus Torvalds 
62591d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
626027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
6276b2f3d1fSChristoph Hellwig 	/* Return error values for O_DSYNC and IS_SYNC() */
6287b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
629cd3758e3STrond Myklebust 		int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
630200baa21STrond Myklebust 		if (err < 0)
631200baa21STrond Myklebust 			result = err;
632200baa21STrond Myklebust 	}
6331da177e4SLinus Torvalds out:
6341da177e4SLinus Torvalds 	return result;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds out_swapfile:
6371da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
6381da177e4SLinus Torvalds 	goto out;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
641bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
642bf40d343SSuresh Jayaraman 				     struct file *filp, loff_t *ppos,
643bf40d343SSuresh Jayaraman 				     size_t count, unsigned int flags)
644bf40d343SSuresh Jayaraman {
645bf40d343SSuresh Jayaraman 	struct dentry *dentry = filp->f_path.dentry;
646bf40d343SSuresh Jayaraman 	struct inode *inode = dentry->d_inode;
647bf40d343SSuresh Jayaraman 	ssize_t ret;
648bf40d343SSuresh Jayaraman 
649bf40d343SSuresh Jayaraman 	dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
650bf40d343SSuresh Jayaraman 		dentry->d_parent->d_name.name, dentry->d_name.name,
651bf40d343SSuresh Jayaraman 		(unsigned long) count, (unsigned long long) *ppos);
652bf40d343SSuresh Jayaraman 
653bf40d343SSuresh Jayaraman 	/*
654bf40d343SSuresh Jayaraman 	 * The combination of splice and an O_APPEND destination is disallowed.
655bf40d343SSuresh Jayaraman 	 */
656bf40d343SSuresh Jayaraman 
657bf40d343SSuresh Jayaraman 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
658bf40d343SSuresh Jayaraman 
659bf40d343SSuresh Jayaraman 	ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
660bf40d343SSuresh Jayaraman 	if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
661bf40d343SSuresh Jayaraman 		int err = nfs_do_fsync(nfs_file_open_context(filp), inode);
662bf40d343SSuresh Jayaraman 		if (err < 0)
663bf40d343SSuresh Jayaraman 			ret = err;
664bf40d343SSuresh Jayaraman 	}
665bf40d343SSuresh Jayaraman 	return ret;
666bf40d343SSuresh Jayaraman }
667bf40d343SSuresh Jayaraman 
6681da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6711da177e4SLinus Torvalds 	int status = 0;
6721da177e4SLinus Torvalds 
673039c4d7aSTrond Myklebust 	/* Try local locking first */
6746d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6756d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6766d34ac19SJ. Bruce Fields 		/* found a conflict */
677039c4d7aSTrond Myklebust 		goto out;
6781da177e4SLinus Torvalds 	}
679039c4d7aSTrond Myklebust 
680039c4d7aSTrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
681039c4d7aSTrond Myklebust 		goto out_noconflict;
682039c4d7aSTrond Myklebust 
683039c4d7aSTrond Myklebust 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
684039c4d7aSTrond Myklebust 		goto out_noconflict;
685039c4d7aSTrond Myklebust 
686039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
687039c4d7aSTrond Myklebust out:
6881da177e4SLinus Torvalds 	return status;
689039c4d7aSTrond Myklebust out_noconflict:
690039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
691039c4d7aSTrond Myklebust 	goto out;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	int res = 0;
6971da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
6981da177e4SLinus Torvalds 		case FL_POSIX:
6991da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 		case FL_FLOCK:
7021da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
7031da177e4SLinus Torvalds 			break;
7041da177e4SLinus Torvalds 		default:
7051da177e4SLinus Torvalds 			BUG();
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 	if (res < 0)
70846bae1a9SNeil Brown 		dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
70946bae1a9SNeil Brown 			" - error %d!\n",
7103110ff80SHarvey Harrison 				__func__, res);
7111da177e4SLinus Torvalds 	return res;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7171da177e4SLinus Torvalds 	int status;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	/*
7201da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7211da177e4SLinus Torvalds 	 * with locks..
7221da177e4SLinus Torvalds 	 */
72329884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	/* NOTE: special case
7261da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
7271da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
7281da177e4SLinus Torvalds 	 */
7291da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
7301da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
7311da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
7321da177e4SLinus Torvalds 	else
7331da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7341da177e4SLinus Torvalds 	return status;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7401da177e4SLinus Torvalds 	int status;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/*
7431da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
7441da177e4SLinus Torvalds 	 * with locks..
7451da177e4SLinus Torvalds 	 */
74629884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
74729884df0STrond Myklebust 	if (status != 0)
7481da177e4SLinus Torvalds 		goto out;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
751c4d7c402STrond Myklebust 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
7521da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
753c4d7c402STrond Myklebust 	else
7541da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
7551da177e4SLinus Torvalds 	if (status < 0)
7561da177e4SLinus Torvalds 		goto out;
7571da177e4SLinus Torvalds 	/*
7581da177e4SLinus Torvalds 	 * Make sure we clear the cache whenever we try to get the lock.
7591da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
7601da177e4SLinus Torvalds 	 */
76129884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
762b5418383STrond Myklebust 	if (!nfs_have_delegation(inode, FMODE_READ))
7631da177e4SLinus Torvalds 		nfs_zap_caches(inode);
7641da177e4SLinus Torvalds out:
7651da177e4SLinus Torvalds 	return status;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds /*
7691da177e4SLinus Torvalds  * Lock a (portion of) a file
7701da177e4SLinus Torvalds  */
7711da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7742116271aSTrond Myklebust 	int ret = -ENOLCK;
7751da177e4SLinus Torvalds 
7766da24bc9SChuck Lever 	dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
7776da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
7786da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
7791da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
7801da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7816da24bc9SChuck Lever 
78291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
785dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
7862116271aSTrond Myklebust 		goto out_err;
7872116271aSTrond Myklebust 
7882116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
7892116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
7902116271aSTrond Myklebust 		if (ret < 0)
7912116271aSTrond Myklebust 			goto out_err;
7922116271aSTrond Myklebust 	}
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
7952116271aSTrond Myklebust 		ret = do_getlk(filp, cmd, fl);
7962116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
7972116271aSTrond Myklebust 		ret = do_unlk(filp, cmd, fl);
7982116271aSTrond Myklebust 	else
7992116271aSTrond Myklebust 		ret = do_setlk(filp, cmd, fl);
8002116271aSTrond Myklebust out_err:
8012116271aSTrond Myklebust 	return ret;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds /*
8051da177e4SLinus Torvalds  * Lock a (portion of) a file
8061da177e4SLinus Torvalds  */
8071da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
8081da177e4SLinus Torvalds {
8096da24bc9SChuck Lever 	dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
8106da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
8116da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
8121da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
8151da177e4SLinus Torvalds 		return -ENOLCK;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
8181da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
8191da177e4SLinus Torvalds 	fl->fl_start = 0;
8201da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
8231da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
8241da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
8251da177e4SLinus Torvalds }
826370f6599SJ. Bruce Fields 
8276da24bc9SChuck Lever /*
8286da24bc9SChuck Lever  * There is no protocol support for leases, so we have no way to implement
8296da24bc9SChuck Lever  * them correctly in the face of opens by other clients.
8306da24bc9SChuck Lever  */
831370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
832370f6599SJ. Bruce Fields {
8336da24bc9SChuck Lever 	dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
8346da24bc9SChuck Lever 			file->f_path.dentry->d_parent->d_name.name,
8356da24bc9SChuck Lever 			file->f_path.dentry->d_name.name, arg);
8366da24bc9SChuck Lever 
837370f6599SJ. Bruce Fields 	return -EINVAL;
838370f6599SJ. Bruce Fields }
839