xref: /openbmc/linux/fs/nfs/file.c (revision 545db45f0fc0d4203b045047798ce156972a3056)
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>
291da177e4SLinus Torvalds #include <linux/smp_lock.h>
30e8edc6e0SAlexey Dobriyan #include <linux/aio.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/uaccess.h>
331da177e4SLinus Torvalds #include <asm/system.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "delegation.h"
3694387fb1STrond Myklebust #include "internal.h"
3791d5b470SChuck Lever #include "iostat.h"
38*545db45fSDavid Howells #include "fscache.h"
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_FILE
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static int nfs_file_open(struct inode *, struct file *);
431da177e4SLinus Torvalds static int nfs_file_release(struct inode *, struct file *);
44980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
451da177e4SLinus Torvalds static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
46f0930fffSJens Axboe static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
47f0930fffSJens Axboe 					struct pipe_inode_info *pipe,
48f0930fffSJens Axboe 					size_t count, unsigned int flags);
49027445c3SBadari Pulavarty static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
50027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos);
51027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
52027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos);
5375e1fcc0SMiklos Szeredi static int  nfs_file_flush(struct file *, fl_owner_t id);
5454917786SChuck Lever static int  nfs_file_fsync(struct file *, struct dentry *dentry, int datasync);
551da177e4SLinus Torvalds static int nfs_check_flags(int flags);
561da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
571da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
58370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
591da177e4SLinus Torvalds 
6094387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops;
6194387fb1STrond Myklebust 
624b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = {
63980802e3STrond Myklebust 	.llseek		= nfs_file_llseek,
641da177e4SLinus Torvalds 	.read		= do_sync_read,
651da177e4SLinus Torvalds 	.write		= do_sync_write,
661da177e4SLinus Torvalds 	.aio_read	= nfs_file_read,
671da177e4SLinus Torvalds 	.aio_write	= nfs_file_write,
681da177e4SLinus Torvalds 	.mmap		= nfs_file_mmap,
691da177e4SLinus Torvalds 	.open		= nfs_file_open,
701da177e4SLinus Torvalds 	.flush		= nfs_file_flush,
711da177e4SLinus Torvalds 	.release	= nfs_file_release,
7254917786SChuck Lever 	.fsync		= nfs_file_fsync,
731da177e4SLinus Torvalds 	.lock		= nfs_lock,
741da177e4SLinus Torvalds 	.flock		= nfs_flock,
75f0930fffSJens Axboe 	.splice_read	= nfs_file_splice_read,
761da177e4SLinus Torvalds 	.check_flags	= nfs_check_flags,
77370f6599SJ. Bruce Fields 	.setlease	= nfs_setlease,
781da177e4SLinus Torvalds };
791da177e4SLinus Torvalds 
8092e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = {
811da177e4SLinus Torvalds 	.permission	= nfs_permission,
821da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
831da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
86b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
8792e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = {
88b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
89b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
90b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
91b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
92b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
93b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
94b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
95b7fa0554SAndreas Gruenbacher };
96b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_v3 */
97b7fa0554SAndreas Gruenbacher 
981da177e4SLinus Torvalds /* Hack for future NFS swap support */
991da177e4SLinus Torvalds #ifndef IS_SWAPFILE
1001da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static int nfs_check_flags(int flags)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
1061da177e4SLinus Torvalds 		return -EINVAL;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	return 0;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * Open file
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static int
1151da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	int res;
1181da177e4SLinus Torvalds 
1196da24bc9SChuck Lever 	dprintk("NFS: open file(%s/%s)\n",
120cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
121cc0dd2d1SChuck Lever 			filp->f_path.dentry->d_name.name);
122cc0dd2d1SChuck Lever 
1231da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
1241da177e4SLinus Torvalds 	if (res)
1251da177e4SLinus Torvalds 		return res;
1261da177e4SLinus Torvalds 
12791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
12846cb650cSTrond Myklebust 	res = nfs_open(inode, filp);
1291da177e4SLinus Torvalds 	return res;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static int
1331da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
1341da177e4SLinus Torvalds {
1356da24bc9SChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
1366da24bc9SChuck Lever 
1376da24bc9SChuck Lever 	dprintk("NFS: release(%s/%s)\n",
1386da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
1396da24bc9SChuck Lever 			dentry->d_name.name);
1406da24bc9SChuck Lever 
14191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
14246cb650cSTrond Myklebust 	return nfs_release(inode, filp);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
145980802e3STrond Myklebust /**
146980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
147980802e3STrond Myklebust  * @inode - pointer to inode struct
148980802e3STrond Myklebust  * @file - pointer to struct file
149980802e3STrond Myklebust  *
150980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
151980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
152980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
153980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
154980802e3STrond Myklebust  * shouldn't trust the cache).
155980802e3STrond Myklebust  */
156980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
157980802e3STrond Myklebust {
158980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
159980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
160980802e3STrond Myklebust 
161980802e3STrond Myklebust 	if (server->flags & NFS_MOUNT_NOAC)
162980802e3STrond Myklebust 		goto force_reval;
163980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
164980802e3STrond Myklebust 		goto force_reval;
165980802e3STrond Myklebust 	if (nfsi->npages != 0)
166980802e3STrond Myklebust 		return 0;
16755296809SChuck Lever 	if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
168fe51beecSTrond Myklebust 		return 0;
169980802e3STrond Myklebust force_reval:
170980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
171980802e3STrond Myklebust }
172980802e3STrond Myklebust 
173980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
174980802e3STrond Myklebust {
1759465efc9SAndi Kleen 	loff_t loff;
176e89e896dSTrond Myklebust 
1776da24bc9SChuck Lever 	dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
178b84e06c5SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
179b84e06c5SChuck Lever 			filp->f_path.dentry->d_name.name,
180b84e06c5SChuck Lever 			offset, origin);
181b84e06c5SChuck Lever 
182980802e3STrond Myklebust 	/* origin == SEEK_END => we must revalidate the cached file length */
183aec5e175SJosef 'Jeff' Sipek 	if (origin == SEEK_END) {
184980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
185d5e66348STrond Myklebust 
186980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
187980802e3STrond Myklebust 		if (retval < 0)
188980802e3STrond Myklebust 			return (loff_t)retval;
189d5e66348STrond Myklebust 
190d5e66348STrond Myklebust 		spin_lock(&inode->i_lock);
1919465efc9SAndi Kleen 		loff = generic_file_llseek_unlocked(filp, offset, origin);
192d5e66348STrond Myklebust 		spin_unlock(&inode->i_lock);
193d5e66348STrond Myklebust 	} else
194d5e66348STrond Myklebust 		loff = generic_file_llseek_unlocked(filp, offset, origin);
1959465efc9SAndi Kleen 	return loff;
196980802e3STrond Myklebust }
197980802e3STrond Myklebust 
1981da177e4SLinus Torvalds /*
19954917786SChuck Lever  * Helper for nfs_file_flush() and nfs_file_fsync()
2007b159fc1STrond Myklebust  *
2017b159fc1STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
2027b159fc1STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
2037b159fc1STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
2047b159fc1STrond Myklebust  * This is because the former is used to notify the _next_ call to
2057b159fc1STrond Myklebust  * nfs_file_write() that a write error occured, and hence cause it to
2067b159fc1STrond Myklebust  * fall back to doing a synchronous write.
2077b159fc1STrond Myklebust  */
2087b159fc1STrond Myklebust static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
2097b159fc1STrond Myklebust {
2107b159fc1STrond Myklebust 	int have_error, status;
2117b159fc1STrond Myklebust 	int ret = 0;
2127b159fc1STrond Myklebust 
2137b159fc1STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
2147b159fc1STrond Myklebust 	status = nfs_wb_all(inode);
2157b159fc1STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
2167b159fc1STrond Myklebust 	if (have_error)
2177b159fc1STrond Myklebust 		ret = xchg(&ctx->error, 0);
2187b159fc1STrond Myklebust 	if (!ret)
2197b159fc1STrond Myklebust 		ret = status;
2207b159fc1STrond Myklebust 	return ret;
2217b159fc1STrond Myklebust }
2227b159fc1STrond Myklebust 
2237b159fc1STrond Myklebust /*
2241da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds static int
22775e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
2281da177e4SLinus Torvalds {
229cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
2306da24bc9SChuck Lever 	struct dentry	*dentry = file->f_path.dentry;
2316da24bc9SChuck Lever 	struct inode	*inode = dentry->d_inode;
2321da177e4SLinus Torvalds 
2336da24bc9SChuck Lever 	dprintk("NFS: flush(%s/%s)\n",
2346da24bc9SChuck Lever 			dentry->d_parent->d_name.name,
2356da24bc9SChuck Lever 			dentry->d_name.name);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
2381da177e4SLinus Torvalds 		return 0;
23991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
2407b159fc1STrond Myklebust 
2417fe5c398STrond Myklebust 	/* Flush writes to the server and return any errors */
2427fe5c398STrond Myklebust 	return nfs_do_fsync(ctx, inode);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static ssize_t
246027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
247027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
2481da177e4SLinus Torvalds {
24901cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
2501da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
2511da177e4SLinus Torvalds 	ssize_t result;
252027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
255027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
2561da177e4SLinus Torvalds 
2576da24bc9SChuck Lever 	dprintk("NFS: read(%s/%s, %lu@%lu)\n",
2581da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2591da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long) pos);
2601da177e4SLinus Torvalds 
26144b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
26291d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
2631da177e4SLinus Torvalds 	if (!result)
264027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2651da177e4SLinus Torvalds 	return result;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static ssize_t
269f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
270f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
271f0930fffSJens Axboe 		     unsigned int flags)
2721da177e4SLinus Torvalds {
27301cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2741da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2751da177e4SLinus Torvalds 	ssize_t res;
2761da177e4SLinus Torvalds 
2776da24bc9SChuck Lever 	dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
2781da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2791da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2801da177e4SLinus Torvalds 
28144b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
2821da177e4SLinus Torvalds 	if (!res)
283f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
2841da177e4SLinus Torvalds 	return res;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static int
2881da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2891da177e4SLinus Torvalds {
29001cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2911da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2921da177e4SLinus Torvalds 	int	status;
2931da177e4SLinus Torvalds 
2946da24bc9SChuck Lever 	dprintk("NFS: mmap(%s/%s)\n",
2951da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2961da177e4SLinus Torvalds 
297e1ebfd33STrond Myklebust 	/* Note: generic_file_mmap() returns ENOSYS on nommu systems
298e1ebfd33STrond Myklebust 	 *       so we call that before revalidating the mapping
299e1ebfd33STrond Myklebust 	 */
300e1ebfd33STrond Myklebust 	status = generic_file_mmap(file, vma);
30194387fb1STrond Myklebust 	if (!status) {
30294387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
303e1ebfd33STrond Myklebust 		status = nfs_revalidate_mapping(inode, file->f_mapping);
30494387fb1STrond Myklebust 	}
3051da177e4SLinus Torvalds 	return status;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /*
3091da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
3101da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
3111da177e4SLinus Torvalds  * whether any write errors occurred for this process.
3121da177e4SLinus Torvalds  */
3131da177e4SLinus Torvalds static int
31454917786SChuck Lever nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
3151da177e4SLinus Torvalds {
316cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3171da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3181da177e4SLinus Torvalds 
3196da24bc9SChuck Lever 	dprintk("NFS: fsync file(%s/%s) datasync %d\n",
32054917786SChuck Lever 			dentry->d_parent->d_name.name, dentry->d_name.name,
32154917786SChuck Lever 			datasync);
3221da177e4SLinus Torvalds 
32391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
3247b159fc1STrond Myklebust 	return nfs_do_fsync(ctx, inode);
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds /*
3284899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3294899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3304899f9c8SNick Piggin  * data from user space.
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3331da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3341da177e4SLinus Torvalds  */
3354899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3364899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3374899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3381da177e4SLinus Torvalds {
3394899f9c8SNick Piggin 	int ret;
3404899f9c8SNick Piggin 	pgoff_t index;
3414899f9c8SNick Piggin 	struct page *page;
3424899f9c8SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
3434899f9c8SNick Piggin 
344b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
345b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
346b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
347b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
348b7eaefaaSChuck Lever 
34972cb77f4STrond Myklebust 	/*
35072cb77f4STrond Myklebust 	 * Prevent starvation issues if someone is doing a consistency
35172cb77f4STrond Myklebust 	 * sync-to-disk
35272cb77f4STrond Myklebust 	 */
35372cb77f4STrond Myklebust 	ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
35472cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
35572cb77f4STrond Myklebust 	if (ret)
35672cb77f4STrond Myklebust 		return ret;
35772cb77f4STrond Myklebust 
35854566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
3594899f9c8SNick Piggin 	if (!page)
3604899f9c8SNick Piggin 		return -ENOMEM;
3614899f9c8SNick Piggin 	*pagep = page;
3624899f9c8SNick Piggin 
3634899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3644899f9c8SNick Piggin 	if (ret) {
3654899f9c8SNick Piggin 		unlock_page(page);
3664899f9c8SNick Piggin 		page_cache_release(page);
3674899f9c8SNick Piggin 	}
3684899f9c8SNick Piggin 	return ret;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3714899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3724899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3734899f9c8SNick Piggin 			struct page *page, void *fsdata)
3741da177e4SLinus Torvalds {
3754899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
3764899f9c8SNick Piggin 	int status;
3771da177e4SLinus Torvalds 
378b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
379b7eaefaaSChuck Lever 		file->f_path.dentry->d_parent->d_name.name,
380b7eaefaaSChuck Lever 		file->f_path.dentry->d_name.name,
381b7eaefaaSChuck Lever 		mapping->host->i_ino, len, (long long) pos);
382b7eaefaaSChuck Lever 
383efc91ed0STrond Myklebust 	/*
384efc91ed0STrond Myklebust 	 * Zero any uninitialised parts of the page, and then mark the page
385efc91ed0STrond Myklebust 	 * as up to date if it turns out that we're extending the file.
386efc91ed0STrond Myklebust 	 */
387efc91ed0STrond Myklebust 	if (!PageUptodate(page)) {
388efc91ed0STrond Myklebust 		unsigned pglen = nfs_page_length(page);
389efc91ed0STrond Myklebust 		unsigned end = offset + len;
390efc91ed0STrond Myklebust 
391efc91ed0STrond Myklebust 		if (pglen == 0) {
392efc91ed0STrond Myklebust 			zero_user_segments(page, 0, offset,
393efc91ed0STrond Myklebust 					end, PAGE_CACHE_SIZE);
394efc91ed0STrond Myklebust 			SetPageUptodate(page);
395efc91ed0STrond Myklebust 		} else if (end >= pglen) {
396efc91ed0STrond Myklebust 			zero_user_segment(page, end, PAGE_CACHE_SIZE);
397efc91ed0STrond Myklebust 			if (offset == 0)
398efc91ed0STrond Myklebust 				SetPageUptodate(page);
399efc91ed0STrond Myklebust 		} else
400efc91ed0STrond Myklebust 			zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
401efc91ed0STrond Myklebust 	}
402efc91ed0STrond Myklebust 
4034899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
4044899f9c8SNick Piggin 
4054899f9c8SNick Piggin 	unlock_page(page);
4064899f9c8SNick Piggin 	page_cache_release(page);
4074899f9c8SNick Piggin 
4083d509e54SChuck Lever 	if (status < 0)
4093d509e54SChuck Lever 		return status;
4103d509e54SChuck Lever 	return copied;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4136b9b3514SDavid Howells /*
4146b9b3514SDavid Howells  * Partially or wholly invalidate a page
4156b9b3514SDavid Howells  * - Release the private state associated with a page if undergoing complete
4166b9b3514SDavid Howells  *   page invalidation
417*545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4186b9b3514SDavid Howells  * - Caller holds page lock
4196b9b3514SDavid Howells  */
4202ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
421cd52ed35STrond Myklebust {
422b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
423b7eaefaaSChuck Lever 
4241c75950bSTrond Myklebust 	if (offset != 0)
4251c75950bSTrond Myklebust 		return;
426d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
4271b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
428*545db45fSDavid Howells 
429*545db45fSDavid Howells 	nfs_fscache_invalidate_page(page, page->mapping->host);
430cd52ed35STrond Myklebust }
431cd52ed35STrond Myklebust 
4326b9b3514SDavid Howells /*
4336b9b3514SDavid Howells  * Attempt to release the private state associated with a page
434*545db45fSDavid Howells  * - Called if either PG_private or PG_fscache is set on the page
4356b9b3514SDavid Howells  * - Caller holds page lock
4366b9b3514SDavid Howells  * - Return true (may release page) or false (may not)
4376b9b3514SDavid Howells  */
438cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
439cd52ed35STrond Myklebust {
440b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
441b7eaefaaSChuck Lever 
442e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
443*545db45fSDavid Howells 	if (PagePrivate(page))
444ddeff520SNikita Danilov 		return 0;
445*545db45fSDavid Howells 	return nfs_fscache_release_page(page, gfp);
446e3db7691STrond Myklebust }
447e3db7691STrond Myklebust 
4486b9b3514SDavid Howells /*
4496b9b3514SDavid Howells  * Attempt to clear the private state associated with a page when an error
4506b9b3514SDavid Howells  * occurs that requires the cached contents of an inode to be written back or
4516b9b3514SDavid Howells  * destroyed
452*545db45fSDavid Howells  * - Called if either PG_private or fscache is set on the page
4536b9b3514SDavid Howells  * - Caller holds page lock
4546b9b3514SDavid Howells  * - Return 0 if successful, -error otherwise
4556b9b3514SDavid Howells  */
456e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
457e3db7691STrond Myklebust {
458b7eaefaaSChuck Lever 	struct inode *inode = page->mapping->host;
459*545db45fSDavid Howells 	struct nfs_inode *nfsi = NFS_I(inode);
460b7eaefaaSChuck Lever 
461b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
462b7eaefaaSChuck Lever 		inode->i_ino, (long long)page_offset(page));
463b7eaefaaSChuck Lever 
464*545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(nfsi, page);
465b7eaefaaSChuck Lever 	return nfs_wb_page(inode, page);
466cd52ed35STrond Myklebust }
467cd52ed35STrond Myklebust 
468f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
4691da177e4SLinus Torvalds 	.readpage = nfs_readpage,
4701da177e4SLinus Torvalds 	.readpages = nfs_readpages,
4719cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
4721da177e4SLinus Torvalds 	.writepage = nfs_writepage,
4731da177e4SLinus Torvalds 	.writepages = nfs_writepages,
4744899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
4754899f9c8SNick Piggin 	.write_end = nfs_write_end,
476cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
477cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
4781da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
479e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
4801da177e4SLinus Torvalds };
4811da177e4SLinus Torvalds 
4826b9b3514SDavid Howells /*
4836b9b3514SDavid Howells  * Notification that a PTE pointing to an NFS page is about to be made
4846b9b3514SDavid Howells  * writable, implying that someone is about to modify the page through a
4856b9b3514SDavid Howells  * shared-writable mapping
4866b9b3514SDavid Howells  */
487c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
48894387fb1STrond Myklebust {
489c2ec175cSNick Piggin 	struct page *page = vmf->page;
49094387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
491b7eaefaaSChuck Lever 	struct dentry *dentry = filp->f_path.dentry;
49294387fb1STrond Myklebust 	unsigned pagelen;
49394387fb1STrond Myklebust 	int ret = -EINVAL;
4944899f9c8SNick Piggin 	struct address_space *mapping;
49594387fb1STrond Myklebust 
496b7eaefaaSChuck Lever 	dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
497b7eaefaaSChuck Lever 		dentry->d_parent->d_name.name, dentry->d_name.name,
498b7eaefaaSChuck Lever 		filp->f_mapping->host->i_ino,
499b7eaefaaSChuck Lever 		(long long)page_offset(page));
500b7eaefaaSChuck Lever 
501*545db45fSDavid Howells 	/* make sure the cache has finished storing the page */
502*545db45fSDavid Howells 	nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
503*545db45fSDavid Howells 
50494387fb1STrond Myklebust 	lock_page(page);
5054899f9c8SNick Piggin 	mapping = page->mapping;
506b7eaefaaSChuck Lever 	if (mapping != dentry->d_inode->i_mapping)
5078b1f9ee5STrond Myklebust 		goto out_unlock;
5088b1f9ee5STrond Myklebust 
5098b1f9ee5STrond Myklebust 	ret = 0;
5104899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
5118b1f9ee5STrond Myklebust 	if (pagelen == 0)
5128b1f9ee5STrond Myklebust 		goto out_unlock;
5138b1f9ee5STrond Myklebust 
5148b1f9ee5STrond Myklebust 	ret = nfs_flush_incompatible(filp, page);
5158b1f9ee5STrond Myklebust 	if (ret != 0)
5168b1f9ee5STrond Myklebust 		goto out_unlock;
5178b1f9ee5STrond Myklebust 
5188b1f9ee5STrond Myklebust 	ret = nfs_updatepage(filp, page, 0, pagelen);
5198b1f9ee5STrond Myklebust 	if (ret == 0)
5208b1f9ee5STrond Myklebust 		ret = pagelen;
5218b1f9ee5STrond Myklebust out_unlock:
5224899f9c8SNick Piggin 	unlock_page(page);
523c2ec175cSNick Piggin 	if (ret)
524c2ec175cSNick Piggin 		ret = VM_FAULT_SIGBUS;
52594387fb1STrond Myklebust 	return ret;
52694387fb1STrond Myklebust }
52794387fb1STrond Myklebust 
52894387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = {
52994387fb1STrond Myklebust 	.fault = filemap_fault,
53094387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
53194387fb1STrond Myklebust };
53294387fb1STrond Myklebust 
5337b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
5347b159fc1STrond Myklebust {
5357b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
5367b159fc1STrond Myklebust 
5377b159fc1STrond Myklebust 	if (IS_SYNC(inode) || (filp->f_flags & O_SYNC))
5387b159fc1STrond Myklebust 		return 1;
539cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
5407b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
5417b159fc1STrond Myklebust 		return 1;
5427b159fc1STrond Myklebust 	return 0;
5437b159fc1STrond Myklebust }
5447b159fc1STrond Myklebust 
545027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
546027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
5471da177e4SLinus Torvalds {
54801cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
5491da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
5501da177e4SLinus Torvalds 	ssize_t result;
551027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
554027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
5551da177e4SLinus Torvalds 
5566da24bc9SChuck Lever 	dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
5571da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
5586da24bc9SChuck Lever 		(unsigned long) count, (long long) pos);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	result = -EBUSY;
5611da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
5621da177e4SLinus Torvalds 		goto out_swapfile;
5637d52e862STrond Myklebust 	/*
5647d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
5657d52e862STrond Myklebust 	 */
5667d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
5677d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
5681da177e4SLinus Torvalds 		if (result)
5691da177e4SLinus Torvalds 			goto out;
570fe51beecSTrond Myklebust 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	result = count;
5731da177e4SLinus Torvalds 	if (!count)
5741da177e4SLinus Torvalds 		goto out;
5751da177e4SLinus Torvalds 
57691d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
577027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
578200baa21STrond Myklebust 	/* Return error values for O_SYNC and IS_SYNC() */
5797b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
580cd3758e3STrond Myklebust 		int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
581200baa21STrond Myklebust 		if (err < 0)
582200baa21STrond Myklebust 			result = err;
583200baa21STrond Myklebust 	}
5841da177e4SLinus Torvalds out:
5851da177e4SLinus Torvalds 	return result;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds out_swapfile:
5881da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
5891da177e4SLinus Torvalds 	goto out;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
5951da177e4SLinus Torvalds 	int status = 0;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	lock_kernel();
598039c4d7aSTrond Myklebust 	/* Try local locking first */
5996d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
6006d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
6016d34ac19SJ. Bruce Fields 		/* found a conflict */
602039c4d7aSTrond Myklebust 		goto out;
6031da177e4SLinus Torvalds 	}
604039c4d7aSTrond Myklebust 
605039c4d7aSTrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
606039c4d7aSTrond Myklebust 		goto out_noconflict;
607039c4d7aSTrond Myklebust 
608039c4d7aSTrond Myklebust 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
609039c4d7aSTrond Myklebust 		goto out_noconflict;
610039c4d7aSTrond Myklebust 
611039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
612039c4d7aSTrond Myklebust out:
6131da177e4SLinus Torvalds 	unlock_kernel();
6141da177e4SLinus Torvalds 	return status;
615039c4d7aSTrond Myklebust out_noconflict:
616039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
617039c4d7aSTrond Myklebust 	goto out;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	int res = 0;
6231da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
6241da177e4SLinus Torvalds 		case FL_POSIX:
6251da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
6261da177e4SLinus Torvalds 			break;
6271da177e4SLinus Torvalds 		case FL_FLOCK:
6281da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
6291da177e4SLinus Torvalds 			break;
6301da177e4SLinus Torvalds 		default:
6311da177e4SLinus Torvalds 			BUG();
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 	if (res < 0)
63446bae1a9SNeil Brown 		dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
63546bae1a9SNeil Brown 			" - error %d!\n",
6363110ff80SHarvey Harrison 				__func__, res);
6371da177e4SLinus Torvalds 	return res;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6431da177e4SLinus Torvalds 	int status;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/*
6461da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
6471da177e4SLinus Torvalds 	 * with locks..
6481da177e4SLinus Torvalds 	 */
64929884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	/* NOTE: special case
6521da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
6531da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
6541da177e4SLinus Torvalds 	 */
6551da177e4SLinus Torvalds 	lock_kernel();
6561da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
6571da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
6581da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
6591da177e4SLinus Torvalds 	else
6601da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
6611da177e4SLinus Torvalds 	unlock_kernel();
6621da177e4SLinus Torvalds 	return status;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
6681da177e4SLinus Torvalds 	int status;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	/*
6711da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
6721da177e4SLinus Torvalds 	 * with locks..
6731da177e4SLinus Torvalds 	 */
67429884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
67529884df0STrond Myklebust 	if (status != 0)
6761da177e4SLinus Torvalds 		goto out;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	lock_kernel();
6791da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
680c4d7c402STrond Myklebust 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
6811da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
682c4d7c402STrond Myklebust 	else
6831da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
6841da177e4SLinus Torvalds 	unlock_kernel();
6851da177e4SLinus Torvalds 	if (status < 0)
6861da177e4SLinus Torvalds 		goto out;
6871da177e4SLinus Torvalds 	/*
6881da177e4SLinus Torvalds 	 * Make sure we clear the cache whenever we try to get the lock.
6891da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
6901da177e4SLinus Torvalds 	 */
69129884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
692b5418383STrond Myklebust 	if (!nfs_have_delegation(inode, FMODE_READ))
6931da177e4SLinus Torvalds 		nfs_zap_caches(inode);
6941da177e4SLinus Torvalds out:
6951da177e4SLinus Torvalds 	return status;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds /*
6991da177e4SLinus Torvalds  * Lock a (portion of) a file
7001da177e4SLinus Torvalds  */
7011da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
7042116271aSTrond Myklebust 	int ret = -ENOLCK;
7051da177e4SLinus Torvalds 
7066da24bc9SChuck Lever 	dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
7076da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
7086da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
7091da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
7101da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
7116da24bc9SChuck Lever 
71291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
715dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
7162116271aSTrond Myklebust 		goto out_err;
7172116271aSTrond Myklebust 
7182116271aSTrond Myklebust 	if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
7192116271aSTrond Myklebust 		ret = NFS_PROTO(inode)->lock_check_bounds(fl);
7202116271aSTrond Myklebust 		if (ret < 0)
7212116271aSTrond Myklebust 			goto out_err;
7222116271aSTrond Myklebust 	}
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
7252116271aSTrond Myklebust 		ret = do_getlk(filp, cmd, fl);
7262116271aSTrond Myklebust 	else if (fl->fl_type == F_UNLCK)
7272116271aSTrond Myklebust 		ret = do_unlk(filp, cmd, fl);
7282116271aSTrond Myklebust 	else
7292116271aSTrond Myklebust 		ret = do_setlk(filp, cmd, fl);
7302116271aSTrond Myklebust out_err:
7312116271aSTrond Myklebust 	return ret;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds /*
7351da177e4SLinus Torvalds  * Lock a (portion of) a file
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
7381da177e4SLinus Torvalds {
7396da24bc9SChuck Lever 	dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
7406da24bc9SChuck Lever 			filp->f_path.dentry->d_parent->d_name.name,
7416da24bc9SChuck Lever 			filp->f_path.dentry->d_name.name,
7421da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
7451da177e4SLinus Torvalds 		return -ENOLCK;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
7481da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
7491da177e4SLinus Torvalds 	fl->fl_start = 0;
7501da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
7531da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
7541da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
7551da177e4SLinus Torvalds }
756370f6599SJ. Bruce Fields 
7576da24bc9SChuck Lever /*
7586da24bc9SChuck Lever  * There is no protocol support for leases, so we have no way to implement
7596da24bc9SChuck Lever  * them correctly in the face of opens by other clients.
7606da24bc9SChuck Lever  */
761370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
762370f6599SJ. Bruce Fields {
7636da24bc9SChuck Lever 	dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
7646da24bc9SChuck Lever 			file->f_path.dentry->d_parent->d_name.name,
7656da24bc9SChuck Lever 			file->f_path.dentry->d_name.name, arg);
7666da24bc9SChuck Lever 
767370f6599SJ. Bruce Fields 	return -EINVAL;
768370f6599SJ. Bruce Fields }
769