xref: /openbmc/linux/fs/nfs/file.c (revision 9465efc9)
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"
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);
50027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
51027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos);
5275e1fcc0SMiklos Szeredi static int  nfs_file_flush(struct file *, fl_owner_t id);
531da177e4SLinus Torvalds static int  nfs_fsync(struct file *, struct dentry *dentry, int datasync);
541da177e4SLinus Torvalds static int nfs_check_flags(int flags);
551da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
561da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
57370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
581da177e4SLinus Torvalds 
5994387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops;
6094387fb1STrond Myklebust 
614b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = {
62980802e3STrond Myklebust 	.llseek		= nfs_file_llseek,
631da177e4SLinus Torvalds 	.read		= do_sync_read,
641da177e4SLinus Torvalds 	.write		= do_sync_write,
651da177e4SLinus Torvalds 	.aio_read	= nfs_file_read,
661da177e4SLinus Torvalds 	.aio_write	= nfs_file_write,
67240ee831SBryan Wu #ifdef CONFIG_MMU
681da177e4SLinus Torvalds 	.mmap		= nfs_file_mmap,
69240ee831SBryan Wu #else
70240ee831SBryan Wu 	.mmap		= generic_file_mmap,
71240ee831SBryan Wu #endif
721da177e4SLinus Torvalds 	.open		= nfs_file_open,
731da177e4SLinus Torvalds 	.flush		= nfs_file_flush,
741da177e4SLinus Torvalds 	.release	= nfs_file_release,
751da177e4SLinus Torvalds 	.fsync		= nfs_fsync,
761da177e4SLinus Torvalds 	.lock		= nfs_lock,
771da177e4SLinus Torvalds 	.flock		= nfs_flock,
78f0930fffSJens Axboe 	.splice_read	= nfs_file_splice_read,
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 
1221da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
1231da177e4SLinus Torvalds 	if (res)
1241da177e4SLinus Torvalds 		return res;
1251da177e4SLinus Torvalds 
12691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1271da177e4SLinus Torvalds 	lock_kernel();
1281f163415SDavid Howells 	res = NFS_PROTO(inode)->file_open(inode, filp);
1291da177e4SLinus Torvalds 	unlock_kernel();
1301da177e4SLinus Torvalds 	return res;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static int
1341da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	/* Ensure that dirty pages are flushed out with the right creds */
1371da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
138a49c3c77STrond Myklebust 		nfs_wb_all(filp->f_path.dentry->d_inode);
13991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
1401da177e4SLinus Torvalds 	return NFS_PROTO(inode)->file_release(inode, filp);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
143980802e3STrond Myklebust /**
144980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
145980802e3STrond Myklebust  * @inode - pointer to inode struct
146980802e3STrond Myklebust  * @file - pointer to struct file
147980802e3STrond Myklebust  *
148980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
149980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
150980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
151980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
152980802e3STrond Myklebust  * shouldn't trust the cache).
153980802e3STrond Myklebust  */
154980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
155980802e3STrond Myklebust {
156980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
157980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
158980802e3STrond Myklebust 
159980802e3STrond Myklebust 	if (server->flags & NFS_MOUNT_NOAC)
160980802e3STrond Myklebust 		goto force_reval;
161980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
162980802e3STrond Myklebust 		goto force_reval;
163980802e3STrond Myklebust 	if (nfsi->npages != 0)
164980802e3STrond Myklebust 		return 0;
16555296809SChuck Lever 	if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
166fe51beecSTrond Myklebust 		return 0;
167980802e3STrond Myklebust force_reval:
168980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
169980802e3STrond Myklebust }
170980802e3STrond Myklebust 
171980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
172980802e3STrond Myklebust {
1739465efc9SAndi Kleen 	loff_t loff;
174980802e3STrond Myklebust 	/* origin == SEEK_END => we must revalidate the cached file length */
175aec5e175SJosef 'Jeff' Sipek 	if (origin == SEEK_END) {
176980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
177980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
178980802e3STrond Myklebust 		if (retval < 0)
179980802e3STrond Myklebust 			return (loff_t)retval;
180980802e3STrond Myklebust 	}
1819465efc9SAndi Kleen 	lock_kernel();	/* BKL needed? */
1829465efc9SAndi Kleen 	loff = generic_file_llseek_unlocked(filp, offset, origin);
1839465efc9SAndi Kleen 	unlock_kernel();
1849465efc9SAndi Kleen 	return loff;
185980802e3STrond Myklebust }
186980802e3STrond Myklebust 
1871da177e4SLinus Torvalds /*
1887b159fc1STrond Myklebust  * Helper for nfs_file_flush() and nfs_fsync()
1897b159fc1STrond Myklebust  *
1907b159fc1STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
1917b159fc1STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
1927b159fc1STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
1937b159fc1STrond Myklebust  * This is because the former is used to notify the _next_ call to
1947b159fc1STrond Myklebust  * nfs_file_write() that a write error occured, and hence cause it to
1957b159fc1STrond Myklebust  * fall back to doing a synchronous write.
1967b159fc1STrond Myklebust  */
1977b159fc1STrond Myklebust static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
1987b159fc1STrond Myklebust {
1997b159fc1STrond Myklebust 	int have_error, status;
2007b159fc1STrond Myklebust 	int ret = 0;
2017b159fc1STrond Myklebust 
2027b159fc1STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
2037b159fc1STrond Myklebust 	status = nfs_wb_all(inode);
2047b159fc1STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
2057b159fc1STrond Myklebust 	if (have_error)
2067b159fc1STrond Myklebust 		ret = xchg(&ctx->error, 0);
2077b159fc1STrond Myklebust 	if (!ret)
2087b159fc1STrond Myklebust 		ret = status;
2097b159fc1STrond Myklebust 	return ret;
2107b159fc1STrond Myklebust }
2117b159fc1STrond Myklebust 
2127b159fc1STrond Myklebust /*
2131da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
2141da177e4SLinus Torvalds  *
2151da177e4SLinus Torvalds  */
2161da177e4SLinus Torvalds static int
21775e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
2181da177e4SLinus Torvalds {
219cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
22001cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
2211da177e4SLinus Torvalds 	int		status;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
2261da177e4SLinus Torvalds 		return 0;
22791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
2287b159fc1STrond Myklebust 
2291da177e4SLinus Torvalds 	/* Ensure that data+attribute caches are up to date after close() */
2307b159fc1STrond Myklebust 	status = nfs_do_fsync(ctx, inode);
2313338c143STrond Myklebust 	if (!status)
2323338c143STrond Myklebust 		nfs_revalidate_inode(NFS_SERVER(inode), inode);
2331da177e4SLinus Torvalds 	return status;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static ssize_t
237027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
238027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
2391da177e4SLinus Torvalds {
24001cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
2411da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
2421da177e4SLinus Torvalds 	ssize_t result;
243027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
246027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
2491da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2501da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long) pos);
2511da177e4SLinus Torvalds 
25244b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
25391d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
2541da177e4SLinus Torvalds 	if (!result)
255027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2561da177e4SLinus Torvalds 	return result;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static ssize_t
260f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
261f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
262f0930fffSJens Axboe 		     unsigned int flags)
2631da177e4SLinus Torvalds {
26401cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2651da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2661da177e4SLinus Torvalds 	ssize_t res;
2671da177e4SLinus Torvalds 
268f0930fffSJens Axboe 	dfprintk(VFS, "nfs: splice_read(%s/%s, %lu@%Lu)\n",
2691da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2701da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2711da177e4SLinus Torvalds 
27244b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
2731da177e4SLinus Torvalds 	if (!res)
274f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
2751da177e4SLinus Torvalds 	return res;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static int
2791da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2801da177e4SLinus Torvalds {
28101cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2821da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2831da177e4SLinus Torvalds 	int	status;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: mmap(%s/%s)\n",
2861da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2871da177e4SLinus Torvalds 
28844b11874STrond Myklebust 	status = nfs_revalidate_mapping(inode, file->f_mapping);
28994387fb1STrond Myklebust 	if (!status) {
29094387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
29194387fb1STrond Myklebust 		vma->vm_flags |= VM_CAN_NONLINEAR;
29294387fb1STrond Myklebust 		file_accessed(file);
29394387fb1STrond Myklebust 	}
2941da177e4SLinus Torvalds 	return status;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds /*
2981da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2991da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
3001da177e4SLinus Torvalds  * whether any write errors occurred for this process.
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds static int
3031da177e4SLinus Torvalds nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
3041da177e4SLinus Torvalds {
305cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3061da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
3091da177e4SLinus Torvalds 
31091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
3117b159fc1STrond Myklebust 	return nfs_do_fsync(ctx, inode);
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds /*
3154899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3164899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3174899f9c8SNick Piggin  * data from user space.
3181da177e4SLinus Torvalds  *
3191da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3201da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3211da177e4SLinus Torvalds  */
3224899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3234899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3244899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3251da177e4SLinus Torvalds {
3264899f9c8SNick Piggin 	int ret;
3274899f9c8SNick Piggin 	pgoff_t index;
3284899f9c8SNick Piggin 	struct page *page;
3294899f9c8SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
3304899f9c8SNick Piggin 
3314899f9c8SNick Piggin 	page = __grab_cache_page(mapping, index);
3324899f9c8SNick Piggin 	if (!page)
3334899f9c8SNick Piggin 		return -ENOMEM;
3344899f9c8SNick Piggin 	*pagep = page;
3354899f9c8SNick Piggin 
3364899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3374899f9c8SNick Piggin 	if (ret) {
3384899f9c8SNick Piggin 		unlock_page(page);
3394899f9c8SNick Piggin 		page_cache_release(page);
3404899f9c8SNick Piggin 	}
3414899f9c8SNick Piggin 	return ret;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3444899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3454899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3464899f9c8SNick Piggin 			struct page *page, void *fsdata)
3471da177e4SLinus Torvalds {
3484899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
3494899f9c8SNick Piggin 	int status;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	lock_kernel();
3524899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
3531da177e4SLinus Torvalds 	unlock_kernel();
3544899f9c8SNick Piggin 
3554899f9c8SNick Piggin 	unlock_page(page);
3564899f9c8SNick Piggin 	page_cache_release(page);
3574899f9c8SNick Piggin 
3583d509e54SChuck Lever 	if (status < 0)
3593d509e54SChuck Lever 		return status;
3603d509e54SChuck Lever 	return copied;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3632ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
364cd52ed35STrond Myklebust {
3651c75950bSTrond Myklebust 	if (offset != 0)
3661c75950bSTrond Myklebust 		return;
367d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
3681b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
369cd52ed35STrond Myklebust }
370cd52ed35STrond Myklebust 
371cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
372cd52ed35STrond Myklebust {
373e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
374ddeff520SNikita Danilov 	return 0;
375e3db7691STrond Myklebust }
376e3db7691STrond Myklebust 
377e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
378e3db7691STrond Myklebust {
379e3db7691STrond Myklebust 	return nfs_wb_page(page->mapping->host, page);
380cd52ed35STrond Myklebust }
381cd52ed35STrond Myklebust 
382f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
3831da177e4SLinus Torvalds 	.readpage = nfs_readpage,
3841da177e4SLinus Torvalds 	.readpages = nfs_readpages,
3859cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
3861da177e4SLinus Torvalds 	.writepage = nfs_writepage,
3871da177e4SLinus Torvalds 	.writepages = nfs_writepages,
3884899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
3894899f9c8SNick Piggin 	.write_end = nfs_write_end,
390cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
391cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
3921da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
393e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
3941da177e4SLinus Torvalds };
3951da177e4SLinus Torvalds 
39694387fb1STrond Myklebust static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
39794387fb1STrond Myklebust {
39894387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
39994387fb1STrond Myklebust 	unsigned pagelen;
40094387fb1STrond Myklebust 	int ret = -EINVAL;
4014899f9c8SNick Piggin 	struct address_space *mapping;
40294387fb1STrond Myklebust 
40394387fb1STrond Myklebust 	lock_page(page);
4044899f9c8SNick Piggin 	mapping = page->mapping;
4058b1f9ee5STrond Myklebust 	if (mapping != vma->vm_file->f_path.dentry->d_inode->i_mapping)
4068b1f9ee5STrond Myklebust 		goto out_unlock;
4078b1f9ee5STrond Myklebust 
4088b1f9ee5STrond Myklebust 	ret = 0;
4094899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
4108b1f9ee5STrond Myklebust 	if (pagelen == 0)
4118b1f9ee5STrond Myklebust 		goto out_unlock;
4128b1f9ee5STrond Myklebust 
4138b1f9ee5STrond Myklebust 	ret = nfs_flush_incompatible(filp, page);
4148b1f9ee5STrond Myklebust 	if (ret != 0)
4158b1f9ee5STrond Myklebust 		goto out_unlock;
4168b1f9ee5STrond Myklebust 
4178b1f9ee5STrond Myklebust 	ret = nfs_updatepage(filp, page, 0, pagelen);
4188b1f9ee5STrond Myklebust 	if (ret == 0)
4198b1f9ee5STrond Myklebust 		ret = pagelen;
4208b1f9ee5STrond Myklebust out_unlock:
4214899f9c8SNick Piggin 	unlock_page(page);
42294387fb1STrond Myklebust 	return ret;
42394387fb1STrond Myklebust }
42494387fb1STrond Myklebust 
42594387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = {
42694387fb1STrond Myklebust 	.fault = filemap_fault,
42794387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
42894387fb1STrond Myklebust };
42994387fb1STrond Myklebust 
4307b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
4317b159fc1STrond Myklebust {
4327b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
4337b159fc1STrond Myklebust 
4347b159fc1STrond Myklebust 	if (IS_SYNC(inode) || (filp->f_flags & O_SYNC))
4357b159fc1STrond Myklebust 		return 1;
436cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
4377b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
4387b159fc1STrond Myklebust 		return 1;
4397b159fc1STrond Myklebust 	return 0;
4407b159fc1STrond Myklebust }
4417b159fc1STrond Myklebust 
442027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
443027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
4441da177e4SLinus Torvalds {
44501cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
4461da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
4471da177e4SLinus Torvalds 	ssize_t result;
448027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
451027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
4521da177e4SLinus Torvalds 
453027445c3SBadari Pulavarty 	dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n",
4541da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
455027445c3SBadari Pulavarty 		inode->i_ino, (unsigned long) count, (long long) pos);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	result = -EBUSY;
4581da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
4591da177e4SLinus Torvalds 		goto out_swapfile;
4607d52e862STrond Myklebust 	/*
4617d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
4627d52e862STrond Myklebust 	 */
4637d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
4647d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
4651da177e4SLinus Torvalds 		if (result)
4661da177e4SLinus Torvalds 			goto out;
467fe51beecSTrond Myklebust 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	result = count;
4701da177e4SLinus Torvalds 	if (!count)
4711da177e4SLinus Torvalds 		goto out;
4721da177e4SLinus Torvalds 
47391d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
474027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
475200baa21STrond Myklebust 	/* Return error values for O_SYNC and IS_SYNC() */
4767b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
477cd3758e3STrond Myklebust 		int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
478200baa21STrond Myklebust 		if (err < 0)
479200baa21STrond Myklebust 			result = err;
480200baa21STrond Myklebust 	}
4811da177e4SLinus Torvalds out:
4821da177e4SLinus Torvalds 	return result;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds out_swapfile:
4851da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
4861da177e4SLinus Torvalds 	goto out;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
4921da177e4SLinus Torvalds 	int status = 0;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	lock_kernel();
495039c4d7aSTrond Myklebust 	/* Try local locking first */
4966d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
4976d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
4986d34ac19SJ. Bruce Fields 		/* found a conflict */
499039c4d7aSTrond Myklebust 		goto out;
5001da177e4SLinus Torvalds 	}
501039c4d7aSTrond Myklebust 
502039c4d7aSTrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
503039c4d7aSTrond Myklebust 		goto out_noconflict;
504039c4d7aSTrond Myklebust 
505039c4d7aSTrond Myklebust 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
506039c4d7aSTrond Myklebust 		goto out_noconflict;
507039c4d7aSTrond Myklebust 
508039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
509039c4d7aSTrond Myklebust out:
5101da177e4SLinus Torvalds 	unlock_kernel();
5111da177e4SLinus Torvalds 	return status;
512039c4d7aSTrond Myklebust out_noconflict:
513039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
514039c4d7aSTrond Myklebust 	goto out;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	int res = 0;
5201da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5211da177e4SLinus Torvalds 		case FL_POSIX:
5221da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
5231da177e4SLinus Torvalds 			break;
5241da177e4SLinus Torvalds 		case FL_FLOCK:
5251da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
5261da177e4SLinus Torvalds 			break;
5271da177e4SLinus Torvalds 		default:
5281da177e4SLinus Torvalds 			BUG();
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 	if (res < 0)
53146bae1a9SNeil Brown 		dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
53246bae1a9SNeil Brown 			" - error %d!\n",
53346bae1a9SNeil Brown 				__FUNCTION__, res);
5341da177e4SLinus Torvalds 	return res;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
5401da177e4SLinus Torvalds 	int status;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	/*
5431da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
5441da177e4SLinus Torvalds 	 * with locks..
5451da177e4SLinus Torvalds 	 */
54629884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	/* NOTE: special case
5491da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
5501da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
5511da177e4SLinus Torvalds 	 */
5521da177e4SLinus Torvalds 	lock_kernel();
5531da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
5541da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
5551da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
5561da177e4SLinus Torvalds 	else
5571da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
5581da177e4SLinus Torvalds 	unlock_kernel();
5591da177e4SLinus Torvalds 	return status;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
5651da177e4SLinus Torvalds 	int status;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/*
5681da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
5691da177e4SLinus Torvalds 	 * with locks..
5701da177e4SLinus Torvalds 	 */
57129884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
57229884df0STrond Myklebust 	if (status != 0)
5731da177e4SLinus Torvalds 		goto out;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	lock_kernel();
5761da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
577c4d7c402STrond Myklebust 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
5781da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
579c4d7c402STrond Myklebust 	else
5801da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
5811da177e4SLinus Torvalds 	unlock_kernel();
5821da177e4SLinus Torvalds 	if (status < 0)
5831da177e4SLinus Torvalds 		goto out;
5841da177e4SLinus Torvalds 	/*
5851da177e4SLinus Torvalds 	 * Make sure we clear the cache whenever we try to get the lock.
5861da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
5871da177e4SLinus Torvalds 	 */
58829884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
5891da177e4SLinus Torvalds 	nfs_zap_caches(inode);
5901da177e4SLinus Torvalds out:
5911da177e4SLinus Torvalds 	return status;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /*
5951da177e4SLinus Torvalds  * Lock a (portion of) a file
5961da177e4SLinus Torvalds  */
5971da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct inode * inode = filp->f_mapping->host;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
6021da177e4SLinus Torvalds 			inode->i_sb->s_id, inode->i_ino,
6031da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
6041da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
60591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
608dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
6091da177e4SLinus Torvalds 		return -ENOLCK;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
6121da177e4SLinus Torvalds 		return do_getlk(filp, cmd, fl);
6131da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
6141da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
6151da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds /*
6191da177e4SLinus Torvalds  * Lock a (portion of) a file
6201da177e4SLinus Torvalds  */
6211da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
62401cce933SJosef "Jeff" Sipek 			filp->f_path.dentry->d_inode->i_sb->s_id,
62501cce933SJosef "Jeff" Sipek 			filp->f_path.dentry->d_inode->i_ino,
6261da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/*
6291da177e4SLinus Torvalds 	 * No BSD flocks over NFS allowed.
6301da177e4SLinus Torvalds 	 * Note: we could try to fake a POSIX lock request here by
6311da177e4SLinus Torvalds 	 * using ((u32) filp | 0x80000000) or some such as the pid.
6321da177e4SLinus Torvalds 	 * Not sure whether that would be unique, though, or whether
6331da177e4SLinus Torvalds 	 * that would break in other places.
6341da177e4SLinus Torvalds 	 */
6351da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
6361da177e4SLinus Torvalds 		return -ENOLCK;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
6391da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
6401da177e4SLinus Torvalds 	fl->fl_start = 0;
6411da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
6441da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
6451da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
6461da177e4SLinus Torvalds }
647370f6599SJ. Bruce Fields 
648370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
649370f6599SJ. Bruce Fields {
650370f6599SJ. Bruce Fields 	/*
651370f6599SJ. Bruce Fields 	 * There is no protocol support for leases, so we have no way
652370f6599SJ. Bruce Fields 	 * to implement them correctly in the face of opens by other
653370f6599SJ. Bruce Fields 	 * clients.
654370f6599SJ. Bruce Fields 	 */
655370f6599SJ. Bruce Fields 	return -EINVAL;
656370f6599SJ. Bruce Fields }
657