xref: /openbmc/linux/fs/nfs/file.c (revision a49c3c77)
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,
671da177e4SLinus Torvalds 	.mmap		= nfs_file_mmap,
681da177e4SLinus Torvalds 	.open		= nfs_file_open,
691da177e4SLinus Torvalds 	.flush		= nfs_file_flush,
701da177e4SLinus Torvalds 	.release	= nfs_file_release,
711da177e4SLinus Torvalds 	.fsync		= nfs_fsync,
721da177e4SLinus Torvalds 	.lock		= nfs_lock,
731da177e4SLinus Torvalds 	.flock		= nfs_flock,
74f0930fffSJens Axboe 	.splice_read	= nfs_file_splice_read,
751da177e4SLinus Torvalds 	.check_flags	= nfs_check_flags,
76370f6599SJ. Bruce Fields 	.setlease	= nfs_setlease,
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
7992e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = {
801da177e4SLinus Torvalds 	.permission	= nfs_permission,
811da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
821da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
85b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3
8692e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = {
87b7fa0554SAndreas Gruenbacher 	.permission	= nfs_permission,
88b7fa0554SAndreas Gruenbacher 	.getattr	= nfs_getattr,
89b7fa0554SAndreas Gruenbacher 	.setattr	= nfs_setattr,
90b7fa0554SAndreas Gruenbacher 	.listxattr	= nfs3_listxattr,
91b7fa0554SAndreas Gruenbacher 	.getxattr	= nfs3_getxattr,
92b7fa0554SAndreas Gruenbacher 	.setxattr	= nfs3_setxattr,
93b7fa0554SAndreas Gruenbacher 	.removexattr	= nfs3_removexattr,
94b7fa0554SAndreas Gruenbacher };
95b7fa0554SAndreas Gruenbacher #endif  /* CONFIG_NFS_v3 */
96b7fa0554SAndreas Gruenbacher 
971da177e4SLinus Torvalds /* Hack for future NFS swap support */
981da177e4SLinus Torvalds #ifndef IS_SWAPFILE
991da177e4SLinus Torvalds # define IS_SWAPFILE(inode)	(0)
1001da177e4SLinus Torvalds #endif
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static int nfs_check_flags(int flags)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
1051da177e4SLinus Torvalds 		return -EINVAL;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	return 0;
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * Open file
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds static int
1141da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	int res;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	res = nfs_check_flags(filp->f_flags);
1191da177e4SLinus Torvalds 	if (res)
1201da177e4SLinus Torvalds 		return res;
1211da177e4SLinus Torvalds 
12291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1231da177e4SLinus Torvalds 	lock_kernel();
1241f163415SDavid Howells 	res = NFS_PROTO(inode)->file_open(inode, filp);
1251da177e4SLinus Torvalds 	unlock_kernel();
1261da177e4SLinus Torvalds 	return res;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static int
1301da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	/* Ensure that dirty pages are flushed out with the right creds */
1331da177e4SLinus Torvalds 	if (filp->f_mode & FMODE_WRITE)
134a49c3c77STrond Myklebust 		nfs_wb_all(filp->f_path.dentry->d_inode);
13591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
1361da177e4SLinus Torvalds 	return NFS_PROTO(inode)->file_release(inode, filp);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139980802e3STrond Myklebust /**
140980802e3STrond Myklebust  * nfs_revalidate_size - Revalidate the file size
141980802e3STrond Myklebust  * @inode - pointer to inode struct
142980802e3STrond Myklebust  * @file - pointer to struct file
143980802e3STrond Myklebust  *
144980802e3STrond Myklebust  * Revalidates the file length. This is basically a wrapper around
145980802e3STrond Myklebust  * nfs_revalidate_inode() that takes into account the fact that we may
146980802e3STrond Myklebust  * have cached writes (in which case we don't care about the server's
147980802e3STrond Myklebust  * idea of what the file length is), or O_DIRECT (in which case we
148980802e3STrond Myklebust  * shouldn't trust the cache).
149980802e3STrond Myklebust  */
150980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
151980802e3STrond Myklebust {
152980802e3STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
153980802e3STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
154980802e3STrond Myklebust 
155980802e3STrond Myklebust 	if (server->flags & NFS_MOUNT_NOAC)
156980802e3STrond Myklebust 		goto force_reval;
157980802e3STrond Myklebust 	if (filp->f_flags & O_DIRECT)
158980802e3STrond Myklebust 		goto force_reval;
159980802e3STrond Myklebust 	if (nfsi->npages != 0)
160980802e3STrond Myklebust 		return 0;
16155296809SChuck Lever 	if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
162fe51beecSTrond Myklebust 		return 0;
163980802e3STrond Myklebust force_reval:
164980802e3STrond Myklebust 	return __nfs_revalidate_inode(server, inode);
165980802e3STrond Myklebust }
166980802e3STrond Myklebust 
167980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
168980802e3STrond Myklebust {
169980802e3STrond Myklebust 	/* origin == SEEK_END => we must revalidate the cached file length */
170aec5e175SJosef 'Jeff' Sipek 	if (origin == SEEK_END) {
171980802e3STrond Myklebust 		struct inode *inode = filp->f_mapping->host;
172980802e3STrond Myklebust 		int retval = nfs_revalidate_file_size(inode, filp);
173980802e3STrond Myklebust 		if (retval < 0)
174980802e3STrond Myklebust 			return (loff_t)retval;
175980802e3STrond Myklebust 	}
176980802e3STrond Myklebust 	return remote_llseek(filp, offset, origin);
177980802e3STrond Myklebust }
178980802e3STrond Myklebust 
1791da177e4SLinus Torvalds /*
1807b159fc1STrond Myklebust  * Helper for nfs_file_flush() and nfs_fsync()
1817b159fc1STrond Myklebust  *
1827b159fc1STrond Myklebust  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
1837b159fc1STrond Myklebust  * disk, but it retrieves and clears ctx->error after synching, despite
1847b159fc1STrond Myklebust  * the two being set at the same time in nfs_context_set_write_error().
1857b159fc1STrond Myklebust  * This is because the former is used to notify the _next_ call to
1867b159fc1STrond Myklebust  * nfs_file_write() that a write error occured, and hence cause it to
1877b159fc1STrond Myklebust  * fall back to doing a synchronous write.
1887b159fc1STrond Myklebust  */
1897b159fc1STrond Myklebust static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
1907b159fc1STrond Myklebust {
1917b159fc1STrond Myklebust 	int have_error, status;
1927b159fc1STrond Myklebust 	int ret = 0;
1937b159fc1STrond Myklebust 
1947b159fc1STrond Myklebust 	have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1957b159fc1STrond Myklebust 	status = nfs_wb_all(inode);
1967b159fc1STrond Myklebust 	have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1977b159fc1STrond Myklebust 	if (have_error)
1987b159fc1STrond Myklebust 		ret = xchg(&ctx->error, 0);
1997b159fc1STrond Myklebust 	if (!ret)
2007b159fc1STrond Myklebust 		ret = status;
2017b159fc1STrond Myklebust 	return ret;
2027b159fc1STrond Myklebust }
2037b159fc1STrond Myklebust 
2047b159fc1STrond Myklebust /*
2051da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
2061da177e4SLinus Torvalds  *
2071da177e4SLinus Torvalds  */
2081da177e4SLinus Torvalds static int
20975e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
2101da177e4SLinus Torvalds {
211cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
21201cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
2131da177e4SLinus Torvalds 	int		status;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
2181da177e4SLinus Torvalds 		return 0;
21991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
2207b159fc1STrond Myklebust 
2211da177e4SLinus Torvalds 	/* Ensure that data+attribute caches are up to date after close() */
2227b159fc1STrond Myklebust 	status = nfs_do_fsync(ctx, inode);
2233338c143STrond Myklebust 	if (!status)
2243338c143STrond Myklebust 		nfs_revalidate_inode(NFS_SERVER(inode), inode);
2251da177e4SLinus Torvalds 	return status;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static ssize_t
229027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
230027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
2311da177e4SLinus Torvalds {
23201cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
2331da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
2341da177e4SLinus Torvalds 	ssize_t result;
235027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO
2381da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
239027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
2401da177e4SLinus Torvalds #endif
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
2431da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2441da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long) pos);
2451da177e4SLinus Torvalds 
24644b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
24791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
2481da177e4SLinus Torvalds 	if (!result)
249027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2501da177e4SLinus Torvalds 	return result;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds static ssize_t
254f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
255f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
256f0930fffSJens Axboe 		     unsigned int flags)
2571da177e4SLinus Torvalds {
25801cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2591da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2601da177e4SLinus Torvalds 	ssize_t res;
2611da177e4SLinus Torvalds 
262f0930fffSJens Axboe 	dfprintk(VFS, "nfs: splice_read(%s/%s, %lu@%Lu)\n",
2631da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2641da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long long) *ppos);
2651da177e4SLinus Torvalds 
26644b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
2671da177e4SLinus Torvalds 	if (!res)
268f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
2691da177e4SLinus Torvalds 	return res;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int
2731da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2741da177e4SLinus Torvalds {
27501cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2761da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2771da177e4SLinus Torvalds 	int	status;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: mmap(%s/%s)\n",
2801da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2811da177e4SLinus Torvalds 
28244b11874STrond Myklebust 	status = nfs_revalidate_mapping(inode, file->f_mapping);
28394387fb1STrond Myklebust 	if (!status) {
28494387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
28594387fb1STrond Myklebust 		vma->vm_flags |= VM_CAN_NONLINEAR;
28694387fb1STrond Myklebust 		file_accessed(file);
28794387fb1STrond Myklebust 	}
2881da177e4SLinus Torvalds 	return status;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds /*
2921da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2931da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2941da177e4SLinus Torvalds  * whether any write errors occurred for this process.
2951da177e4SLinus Torvalds  */
2961da177e4SLinus Torvalds static int
2971da177e4SLinus Torvalds nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
2981da177e4SLinus Torvalds {
299cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
3001da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
3031da177e4SLinus Torvalds 
30491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
3057b159fc1STrond Myklebust 	return nfs_do_fsync(ctx, inode);
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /*
3094899f9c8SNick Piggin  * This does the "real" work of the write. We must allocate and lock the
3104899f9c8SNick Piggin  * page to be sent back to the generic routine, which then copies the
3114899f9c8SNick Piggin  * data from user space.
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3141da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3151da177e4SLinus Torvalds  */
3164899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping,
3174899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
3184899f9c8SNick Piggin 			struct page **pagep, void **fsdata)
3191da177e4SLinus Torvalds {
3204899f9c8SNick Piggin 	int ret;
3214899f9c8SNick Piggin 	pgoff_t index;
3224899f9c8SNick Piggin 	struct page *page;
3234899f9c8SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
3244899f9c8SNick Piggin 
3254899f9c8SNick Piggin 	page = __grab_cache_page(mapping, index);
3264899f9c8SNick Piggin 	if (!page)
3274899f9c8SNick Piggin 		return -ENOMEM;
3284899f9c8SNick Piggin 	*pagep = page;
3294899f9c8SNick Piggin 
3304899f9c8SNick Piggin 	ret = nfs_flush_incompatible(file, page);
3314899f9c8SNick Piggin 	if (ret) {
3324899f9c8SNick Piggin 		unlock_page(page);
3334899f9c8SNick Piggin 		page_cache_release(page);
3344899f9c8SNick Piggin 	}
3354899f9c8SNick Piggin 	return ret;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3384899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping,
3394899f9c8SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
3404899f9c8SNick Piggin 			struct page *page, void *fsdata)
3411da177e4SLinus Torvalds {
3424899f9c8SNick Piggin 	unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
3434899f9c8SNick Piggin 	int status;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	lock_kernel();
3464899f9c8SNick Piggin 	status = nfs_updatepage(file, page, offset, copied);
3471da177e4SLinus Torvalds 	unlock_kernel();
3484899f9c8SNick Piggin 
3494899f9c8SNick Piggin 	unlock_page(page);
3504899f9c8SNick Piggin 	page_cache_release(page);
3514899f9c8SNick Piggin 
3524899f9c8SNick Piggin 	return status < 0 ? status : copied;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3552ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
356cd52ed35STrond Myklebust {
3571c75950bSTrond Myklebust 	if (offset != 0)
3581c75950bSTrond Myklebust 		return;
359d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
3601b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
361cd52ed35STrond Myklebust }
362cd52ed35STrond Myklebust 
363cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
364cd52ed35STrond Myklebust {
365e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
366ddeff520SNikita Danilov 	return 0;
367e3db7691STrond Myklebust }
368e3db7691STrond Myklebust 
369e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
370e3db7691STrond Myklebust {
371e3db7691STrond Myklebust 	return nfs_wb_page(page->mapping->host, page);
372cd52ed35STrond Myklebust }
373cd52ed35STrond Myklebust 
374f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
3751da177e4SLinus Torvalds 	.readpage = nfs_readpage,
3761da177e4SLinus Torvalds 	.readpages = nfs_readpages,
3779cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
3781da177e4SLinus Torvalds 	.writepage = nfs_writepage,
3791da177e4SLinus Torvalds 	.writepages = nfs_writepages,
3804899f9c8SNick Piggin 	.write_begin = nfs_write_begin,
3814899f9c8SNick Piggin 	.write_end = nfs_write_end,
382cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
383cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
3841da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO
3851da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
3861da177e4SLinus Torvalds #endif
387e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
3881da177e4SLinus Torvalds };
3891da177e4SLinus Torvalds 
39094387fb1STrond Myklebust static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
39194387fb1STrond Myklebust {
39294387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
39394387fb1STrond Myklebust 	unsigned pagelen;
39494387fb1STrond Myklebust 	int ret = -EINVAL;
3954899f9c8SNick Piggin 	void *fsdata;
3964899f9c8SNick Piggin 	struct address_space *mapping;
3974899f9c8SNick Piggin 	loff_t offset;
39894387fb1STrond Myklebust 
39994387fb1STrond Myklebust 	lock_page(page);
4004899f9c8SNick Piggin 	mapping = page->mapping;
4014899f9c8SNick Piggin 	if (mapping != vma->vm_file->f_path.dentry->d_inode->i_mapping) {
40294387fb1STrond Myklebust 		unlock_page(page);
4034899f9c8SNick Piggin 		return -EINVAL;
4044899f9c8SNick Piggin 	}
4054899f9c8SNick Piggin 	pagelen = nfs_page_length(page);
4064899f9c8SNick Piggin 	offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
4074899f9c8SNick Piggin 	unlock_page(page);
4084899f9c8SNick Piggin 
4094899f9c8SNick Piggin 	/*
4104899f9c8SNick Piggin 	 * we can use mapping after releasing the page lock, because:
4114899f9c8SNick Piggin 	 * we hold mmap_sem on the fault path, which should pin the vma
4124899f9c8SNick Piggin 	 * which should pin the file, which pins the dentry which should
4134899f9c8SNick Piggin 	 * hold a reference on inode.
4144899f9c8SNick Piggin 	 */
4154899f9c8SNick Piggin 
4164899f9c8SNick Piggin 	if (pagelen) {
4174899f9c8SNick Piggin 		struct page *page2 = NULL;
4184899f9c8SNick Piggin 		ret = nfs_write_begin(filp, mapping, offset, pagelen,
4194899f9c8SNick Piggin 			       	0, &page2, &fsdata);
4204899f9c8SNick Piggin 		if (!ret)
4214899f9c8SNick Piggin 			ret = nfs_write_end(filp, mapping, offset, pagelen,
4224899f9c8SNick Piggin 				       	pagelen, page2, fsdata);
4234899f9c8SNick Piggin 	}
42494387fb1STrond Myklebust 	return ret;
42594387fb1STrond Myklebust }
42694387fb1STrond Myklebust 
42794387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = {
42894387fb1STrond Myklebust 	.fault = filemap_fault,
42994387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
43094387fb1STrond Myklebust };
43194387fb1STrond Myklebust 
4327b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode)
4337b159fc1STrond Myklebust {
4347b159fc1STrond Myklebust 	struct nfs_open_context *ctx;
4357b159fc1STrond Myklebust 
4367b159fc1STrond Myklebust 	if (IS_SYNC(inode) || (filp->f_flags & O_SYNC))
4377b159fc1STrond Myklebust 		return 1;
438cd3758e3STrond Myklebust 	ctx = nfs_file_open_context(filp);
4397b159fc1STrond Myklebust 	if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
4407b159fc1STrond Myklebust 		return 1;
4417b159fc1STrond Myklebust 	return 0;
4427b159fc1STrond Myklebust }
4437b159fc1STrond Myklebust 
444027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
445027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
4461da177e4SLinus Torvalds {
44701cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
4481da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
4491da177e4SLinus Torvalds 	ssize_t result;
450027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO
4531da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
454027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
4551da177e4SLinus Torvalds #endif
4561da177e4SLinus Torvalds 
457027445c3SBadari Pulavarty 	dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n",
4581da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
459027445c3SBadari Pulavarty 		inode->i_ino, (unsigned long) count, (long long) pos);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	result = -EBUSY;
4621da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
4631da177e4SLinus Torvalds 		goto out_swapfile;
4647d52e862STrond Myklebust 	/*
4657d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
4667d52e862STrond Myklebust 	 */
4677d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
4687d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
4691da177e4SLinus Torvalds 		if (result)
4701da177e4SLinus Torvalds 			goto out;
471fe51beecSTrond Myklebust 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	result = count;
4741da177e4SLinus Torvalds 	if (!count)
4751da177e4SLinus Torvalds 		goto out;
4761da177e4SLinus Torvalds 
47791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
478027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
479200baa21STrond Myklebust 	/* Return error values for O_SYNC and IS_SYNC() */
4807b159fc1STrond Myklebust 	if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
481cd3758e3STrond Myklebust 		int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
482200baa21STrond Myklebust 		if (err < 0)
483200baa21STrond Myklebust 			result = err;
484200baa21STrond Myklebust 	}
4851da177e4SLinus Torvalds out:
4861da177e4SLinus Torvalds 	return result;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds out_swapfile:
4891da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
4901da177e4SLinus Torvalds 	goto out;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
4961da177e4SLinus Torvalds 	int status = 0;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	lock_kernel();
499039c4d7aSTrond Myklebust 	/* Try local locking first */
5006d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
5016d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
5026d34ac19SJ. Bruce Fields 		/* found a conflict */
503039c4d7aSTrond Myklebust 		goto out;
5041da177e4SLinus Torvalds 	}
505039c4d7aSTrond Myklebust 
506039c4d7aSTrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
507039c4d7aSTrond Myklebust 		goto out_noconflict;
508039c4d7aSTrond Myklebust 
509039c4d7aSTrond Myklebust 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
510039c4d7aSTrond Myklebust 		goto out_noconflict;
511039c4d7aSTrond Myklebust 
512039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
513039c4d7aSTrond Myklebust out:
5141da177e4SLinus Torvalds 	unlock_kernel();
5151da177e4SLinus Torvalds 	return status;
516039c4d7aSTrond Myklebust out_noconflict:
517039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
518039c4d7aSTrond Myklebust 	goto out;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	int res = 0;
5241da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5251da177e4SLinus Torvalds 		case FL_POSIX:
5261da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
5271da177e4SLinus Torvalds 			break;
5281da177e4SLinus Torvalds 		case FL_FLOCK:
5291da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
5301da177e4SLinus Torvalds 			break;
5311da177e4SLinus Torvalds 		default:
5321da177e4SLinus Torvalds 			BUG();
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 	if (res < 0)
53546bae1a9SNeil Brown 		dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
53646bae1a9SNeil Brown 			" - error %d!\n",
53746bae1a9SNeil Brown 				__FUNCTION__, res);
5381da177e4SLinus Torvalds 	return res;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
5441da177e4SLinus Torvalds 	int status;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/*
5471da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
5481da177e4SLinus Torvalds 	 * with locks..
5491da177e4SLinus Torvalds 	 */
55029884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* NOTE: special case
5531da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
5541da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
5551da177e4SLinus Torvalds 	 */
5561da177e4SLinus Torvalds 	lock_kernel();
5571da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
5581da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
5591da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
5601da177e4SLinus Torvalds 	else
5611da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
5621da177e4SLinus Torvalds 	unlock_kernel();
5631da177e4SLinus Torvalds 	return status;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
5691da177e4SLinus Torvalds 	int status;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/*
5721da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
5731da177e4SLinus Torvalds 	 * with locks..
5741da177e4SLinus Torvalds 	 */
57529884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
57629884df0STrond Myklebust 	if (status != 0)
5771da177e4SLinus Torvalds 		goto out;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	lock_kernel();
5801da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
5811da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
5821da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
5831da177e4SLinus Torvalds 		/* If we were signalled we still need to ensure that
5841da177e4SLinus Torvalds 		 * we clean up any state on the server. We therefore
5851da177e4SLinus Torvalds 		 * record the lock call as having succeeded in order to
5861da177e4SLinus Torvalds 		 * ensure that locks_remove_posix() cleans it out when
5871da177e4SLinus Torvalds 		 * the process exits.
5881da177e4SLinus Torvalds 		 */
5891da177e4SLinus Torvalds 		if (status == -EINTR || status == -ERESTARTSYS)
5901da177e4SLinus Torvalds 			do_vfs_lock(filp, fl);
5911da177e4SLinus Torvalds 	} else
5921da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
5931da177e4SLinus Torvalds 	unlock_kernel();
5941da177e4SLinus Torvalds 	if (status < 0)
5951da177e4SLinus Torvalds 		goto out;
5961da177e4SLinus Torvalds 	/*
5971da177e4SLinus Torvalds 	 * Make sure we clear the cache whenever we try to get the lock.
5981da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
5991da177e4SLinus Torvalds 	 */
60029884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
6011da177e4SLinus Torvalds 	nfs_zap_caches(inode);
6021da177e4SLinus Torvalds out:
6031da177e4SLinus Torvalds 	return status;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds /*
6071da177e4SLinus Torvalds  * Lock a (portion of) a file
6081da177e4SLinus Torvalds  */
6091da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	struct inode * inode = filp->f_mapping->host;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
6141da177e4SLinus Torvalds 			inode->i_sb->s_id, inode->i_ino,
6151da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
6161da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
61791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
620dfad9441SPavel Emelyanov 	if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
6211da177e4SLinus Torvalds 		return -ENOLCK;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
6241da177e4SLinus Torvalds 		return do_getlk(filp, cmd, fl);
6251da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
6261da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
6271da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds /*
6311da177e4SLinus Torvalds  * Lock a (portion of) a file
6321da177e4SLinus Torvalds  */
6331da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
63601cce933SJosef "Jeff" Sipek 			filp->f_path.dentry->d_inode->i_sb->s_id,
63701cce933SJosef "Jeff" Sipek 			filp->f_path.dentry->d_inode->i_ino,
6381da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	/*
6411da177e4SLinus Torvalds 	 * No BSD flocks over NFS allowed.
6421da177e4SLinus Torvalds 	 * Note: we could try to fake a POSIX lock request here by
6431da177e4SLinus Torvalds 	 * using ((u32) filp | 0x80000000) or some such as the pid.
6441da177e4SLinus Torvalds 	 * Not sure whether that would be unique, though, or whether
6451da177e4SLinus Torvalds 	 * that would break in other places.
6461da177e4SLinus Torvalds 	 */
6471da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
6481da177e4SLinus Torvalds 		return -ENOLCK;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
6511da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
6521da177e4SLinus Torvalds 	fl->fl_start = 0;
6531da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
6561da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
6571da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
6581da177e4SLinus Torvalds }
659370f6599SJ. Bruce Fields 
660370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
661370f6599SJ. Bruce Fields {
662370f6599SJ. Bruce Fields 	/*
663370f6599SJ. Bruce Fields 	 * There is no protocol support for leases, so we have no way
664370f6599SJ. Bruce Fields 	 * to implement them correctly in the face of opens by other
665370f6599SJ. Bruce Fields 	 * clients.
666370f6599SJ. Bruce Fields 	 */
667370f6599SJ. Bruce Fields 	return -EINVAL;
668370f6599SJ. Bruce Fields }
669