xref: /openbmc/linux/fs/nfs/file.c (revision 9cccef95)
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)
1341da177e4SLinus Torvalds 		filemap_fdatawrite(filp->f_mapping);
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 /*
1801da177e4SLinus Torvalds  * Flush all dirty pages, and check for write errors.
1811da177e4SLinus Torvalds  *
1821da177e4SLinus Torvalds  */
1831da177e4SLinus Torvalds static int
18475e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
18701cce933SJosef "Jeff" Sipek 	struct inode	*inode = file->f_path.dentry->d_inode;
1881da177e4SLinus Torvalds 	int		status;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	if ((file->f_mode & FMODE_WRITE) == 0)
1931da177e4SLinus Torvalds 		return 0;
19491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
1951da177e4SLinus Torvalds 	lock_kernel();
1961da177e4SLinus Torvalds 	/* Ensure that data+attribute caches are up to date after close() */
1971da177e4SLinus Torvalds 	status = nfs_wb_all(inode);
1981da177e4SLinus Torvalds 	if (!status) {
1991da177e4SLinus Torvalds 		status = ctx->error;
2001da177e4SLinus Torvalds 		ctx->error = 0;
2013338c143STrond Myklebust 		if (!status)
2023338c143STrond Myklebust 			nfs_revalidate_inode(NFS_SERVER(inode), inode);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 	unlock_kernel();
2051da177e4SLinus Torvalds 	return status;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static ssize_t
209027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
210027445c3SBadari Pulavarty 		unsigned long nr_segs, loff_t pos)
2111da177e4SLinus Torvalds {
21201cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
2131da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
2141da177e4SLinus Torvalds 	ssize_t result;
215027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO
2181da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
219027445c3SBadari Pulavarty 		return nfs_file_direct_read(iocb, iov, nr_segs, pos);
2201da177e4SLinus Torvalds #endif
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
2231da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
2241da177e4SLinus Torvalds 		(unsigned long) count, (unsigned long) pos);
2251da177e4SLinus Torvalds 
22644b11874STrond Myklebust 	result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
22791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
2281da177e4SLinus Torvalds 	if (!result)
229027445c3SBadari Pulavarty 		result = generic_file_aio_read(iocb, iov, nr_segs, pos);
2301da177e4SLinus Torvalds 	return result;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static ssize_t
234f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos,
235f0930fffSJens Axboe 		     struct pipe_inode_info *pipe, size_t count,
236f0930fffSJens Axboe 		     unsigned int flags)
2371da177e4SLinus Torvalds {
23801cce933SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
2391da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2401da177e4SLinus Torvalds 	ssize_t res;
2411da177e4SLinus Torvalds 
242f0930fffSJens Axboe 	dfprintk(VFS, "nfs: splice_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 long) *ppos);
2451da177e4SLinus Torvalds 
24644b11874STrond Myklebust 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
2471da177e4SLinus Torvalds 	if (!res)
248f0930fffSJens Axboe 		res = generic_file_splice_read(filp, ppos, pipe, count, flags);
2491da177e4SLinus Torvalds 	return res;
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds static int
2531da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
2541da177e4SLinus Torvalds {
25501cce933SJosef "Jeff" Sipek 	struct dentry *dentry = file->f_path.dentry;
2561da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2571da177e4SLinus Torvalds 	int	status;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: mmap(%s/%s)\n",
2601da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
2611da177e4SLinus Torvalds 
26244b11874STrond Myklebust 	status = nfs_revalidate_mapping(inode, file->f_mapping);
26394387fb1STrond Myklebust 	if (!status) {
26494387fb1STrond Myklebust 		vma->vm_ops = &nfs_file_vm_ops;
26594387fb1STrond Myklebust 		vma->vm_flags |= VM_CAN_NONLINEAR;
26694387fb1STrond Myklebust 		file_accessed(file);
26794387fb1STrond Myklebust 	}
2681da177e4SLinus Torvalds 	return status;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /*
2721da177e4SLinus Torvalds  * Flush any dirty pages for this process, and check for write errors.
2731da177e4SLinus Torvalds  * The return status from this call provides a reliable indication of
2741da177e4SLinus Torvalds  * whether any write errors occurred for this process.
2751da177e4SLinus Torvalds  */
2761da177e4SLinus Torvalds static int
2771da177e4SLinus Torvalds nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
2801da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2811da177e4SLinus Torvalds 	int status;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino);
2841da177e4SLinus Torvalds 
28591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
2861da177e4SLinus Torvalds 	lock_kernel();
2871da177e4SLinus Torvalds 	status = nfs_wb_all(inode);
2881da177e4SLinus Torvalds 	if (!status) {
2891da177e4SLinus Torvalds 		status = ctx->error;
2901da177e4SLinus Torvalds 		ctx->error = 0;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	unlock_kernel();
2931da177e4SLinus Torvalds 	return status;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * This does the "real" work of the write. The generic routine has
2981da177e4SLinus Torvalds  * allocated the page, locked it, done all the page alignment stuff
2991da177e4SLinus Torvalds  * calculations etc. Now we should just copy the data from user
3001da177e4SLinus Torvalds  * space and write it back to the real medium..
3011da177e4SLinus Torvalds  *
3021da177e4SLinus Torvalds  * If the writer ends up delaying the write, the writer needs to
3031da177e4SLinus Torvalds  * increment the page use counts until he is done with the page.
3041da177e4SLinus Torvalds  */
3051da177e4SLinus Torvalds static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	return nfs_flush_incompatible(file, page);
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	long status;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	lock_kernel();
3151da177e4SLinus Torvalds 	status = nfs_updatepage(file, page, offset, to-offset);
3161da177e4SLinus Torvalds 	unlock_kernel();
3171da177e4SLinus Torvalds 	return status;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3202ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset)
321cd52ed35STrond Myklebust {
3221c75950bSTrond Myklebust 	if (offset != 0)
3231c75950bSTrond Myklebust 		return;
324d2ccddf0STrond Myklebust 	/* Cancel any unstarted writes on this page */
3251b3b4a1aSTrond Myklebust 	nfs_wb_page_cancel(page->mapping->host, page);
326cd52ed35STrond Myklebust }
327cd52ed35STrond Myklebust 
328cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp)
329cd52ed35STrond Myklebust {
330e3db7691STrond Myklebust 	/* If PagePrivate() is set, then the page is not freeable */
331ddeff520SNikita Danilov 	return 0;
332e3db7691STrond Myklebust }
333e3db7691STrond Myklebust 
334e3db7691STrond Myklebust static int nfs_launder_page(struct page *page)
335e3db7691STrond Myklebust {
336e3db7691STrond Myklebust 	return nfs_wb_page(page->mapping->host, page);
337cd52ed35STrond Myklebust }
338cd52ed35STrond Myklebust 
339f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = {
3401da177e4SLinus Torvalds 	.readpage = nfs_readpage,
3411da177e4SLinus Torvalds 	.readpages = nfs_readpages,
3429cccef95STrond Myklebust 	.set_page_dirty = __set_page_dirty_nobuffers,
3431da177e4SLinus Torvalds 	.writepage = nfs_writepage,
3441da177e4SLinus Torvalds 	.writepages = nfs_writepages,
3451da177e4SLinus Torvalds 	.prepare_write = nfs_prepare_write,
3461da177e4SLinus Torvalds 	.commit_write = nfs_commit_write,
347cd52ed35STrond Myklebust 	.invalidatepage = nfs_invalidate_page,
348cd52ed35STrond Myklebust 	.releasepage = nfs_release_page,
3491da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO
3501da177e4SLinus Torvalds 	.direct_IO = nfs_direct_IO,
3511da177e4SLinus Torvalds #endif
352e3db7691STrond Myklebust 	.launder_page = nfs_launder_page,
3531da177e4SLinus Torvalds };
3541da177e4SLinus Torvalds 
35594387fb1STrond Myklebust static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
35694387fb1STrond Myklebust {
35794387fb1STrond Myklebust 	struct file *filp = vma->vm_file;
35894387fb1STrond Myklebust 	unsigned pagelen;
35994387fb1STrond Myklebust 	int ret = -EINVAL;
36094387fb1STrond Myklebust 
36194387fb1STrond Myklebust 	lock_page(page);
36294387fb1STrond Myklebust 	if (page->mapping != vma->vm_file->f_path.dentry->d_inode->i_mapping)
36394387fb1STrond Myklebust 		goto out_unlock;
36494387fb1STrond Myklebust 	pagelen = nfs_page_length(page);
36594387fb1STrond Myklebust 	if (pagelen == 0)
36694387fb1STrond Myklebust 		goto out_unlock;
36794387fb1STrond Myklebust 	ret = nfs_prepare_write(filp, page, 0, pagelen);
36894387fb1STrond Myklebust 	if (!ret)
36994387fb1STrond Myklebust 		ret = nfs_commit_write(filp, page, 0, pagelen);
37094387fb1STrond Myklebust out_unlock:
37194387fb1STrond Myklebust 	unlock_page(page);
37294387fb1STrond Myklebust 	return ret;
37394387fb1STrond Myklebust }
37494387fb1STrond Myklebust 
37594387fb1STrond Myklebust static struct vm_operations_struct nfs_file_vm_ops = {
37694387fb1STrond Myklebust 	.fault = filemap_fault,
37794387fb1STrond Myklebust 	.page_mkwrite = nfs_vm_page_mkwrite,
37894387fb1STrond Myklebust };
37994387fb1STrond Myklebust 
380027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
381027445c3SBadari Pulavarty 				unsigned long nr_segs, loff_t pos)
3821da177e4SLinus Torvalds {
38301cce933SJosef "Jeff" Sipek 	struct dentry * dentry = iocb->ki_filp->f_path.dentry;
3841da177e4SLinus Torvalds 	struct inode * inode = dentry->d_inode;
3851da177e4SLinus Torvalds 	ssize_t result;
386027445c3SBadari Pulavarty 	size_t count = iov_length(iov, nr_segs);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO
3891da177e4SLinus Torvalds 	if (iocb->ki_filp->f_flags & O_DIRECT)
390027445c3SBadari Pulavarty 		return nfs_file_direct_write(iocb, iov, nr_segs, pos);
3911da177e4SLinus Torvalds #endif
3921da177e4SLinus Torvalds 
393027445c3SBadari Pulavarty 	dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n",
3941da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
395027445c3SBadari Pulavarty 		inode->i_ino, (unsigned long) count, (long long) pos);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	result = -EBUSY;
3981da177e4SLinus Torvalds 	if (IS_SWAPFILE(inode))
3991da177e4SLinus Torvalds 		goto out_swapfile;
4007d52e862STrond Myklebust 	/*
4017d52e862STrond Myklebust 	 * O_APPEND implies that we must revalidate the file length.
4027d52e862STrond Myklebust 	 */
4037d52e862STrond Myklebust 	if (iocb->ki_filp->f_flags & O_APPEND) {
4047d52e862STrond Myklebust 		result = nfs_revalidate_file_size(inode, iocb->ki_filp);
4051da177e4SLinus Torvalds 		if (result)
4061da177e4SLinus Torvalds 			goto out;
407fe51beecSTrond Myklebust 	}
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	result = count;
4101da177e4SLinus Torvalds 	if (!count)
4111da177e4SLinus Torvalds 		goto out;
4121da177e4SLinus Torvalds 
41391d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
414027445c3SBadari Pulavarty 	result = generic_file_aio_write(iocb, iov, nr_segs, pos);
415200baa21STrond Myklebust 	/* Return error values for O_SYNC and IS_SYNC() */
416200baa21STrond Myklebust 	if (result >= 0 && (IS_SYNC(inode) || (iocb->ki_filp->f_flags & O_SYNC))) {
417200baa21STrond Myklebust 		int err = nfs_fsync(iocb->ki_filp, dentry, 1);
418200baa21STrond Myklebust 		if (err < 0)
419200baa21STrond Myklebust 			result = err;
420200baa21STrond Myklebust 	}
4211da177e4SLinus Torvalds out:
4221da177e4SLinus Torvalds 	return result;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds out_swapfile:
4251da177e4SLinus Torvalds 	printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
4261da177e4SLinus Torvalds 	goto out;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
4321da177e4SLinus Torvalds 	int status = 0;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	lock_kernel();
435039c4d7aSTrond Myklebust 	/* Try local locking first */
4366d34ac19SJ. Bruce Fields 	posix_test_lock(filp, fl);
4376d34ac19SJ. Bruce Fields 	if (fl->fl_type != F_UNLCK) {
4386d34ac19SJ. Bruce Fields 		/* found a conflict */
439039c4d7aSTrond Myklebust 		goto out;
4401da177e4SLinus Torvalds 	}
441039c4d7aSTrond Myklebust 
442039c4d7aSTrond Myklebust 	if (nfs_have_delegation(inode, FMODE_READ))
443039c4d7aSTrond Myklebust 		goto out_noconflict;
444039c4d7aSTrond Myklebust 
445039c4d7aSTrond Myklebust 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
446039c4d7aSTrond Myklebust 		goto out_noconflict;
447039c4d7aSTrond Myklebust 
448039c4d7aSTrond Myklebust 	status = NFS_PROTO(inode)->lock(filp, cmd, fl);
449039c4d7aSTrond Myklebust out:
4501da177e4SLinus Torvalds 	unlock_kernel();
4511da177e4SLinus Torvalds 	return status;
452039c4d7aSTrond Myklebust out_noconflict:
453039c4d7aSTrond Myklebust 	fl->fl_type = F_UNLCK;
454039c4d7aSTrond Myklebust 	goto out;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	int res = 0;
4601da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4611da177e4SLinus Torvalds 		case FL_POSIX:
4621da177e4SLinus Torvalds 			res = posix_lock_file_wait(file, fl);
4631da177e4SLinus Torvalds 			break;
4641da177e4SLinus Torvalds 		case FL_FLOCK:
4651da177e4SLinus Torvalds 			res = flock_lock_file_wait(file, fl);
4661da177e4SLinus Torvalds 			break;
4671da177e4SLinus Torvalds 		default:
4681da177e4SLinus Torvalds 			BUG();
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 	if (res < 0)
47146bae1a9SNeil Brown 		dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
47246bae1a9SNeil Brown 			" - error %d!\n",
47346bae1a9SNeil Brown 				__FUNCTION__, res);
4741da177e4SLinus Torvalds 	return res;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
4801da177e4SLinus Torvalds 	int status;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	/*
4831da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
4841da177e4SLinus Torvalds 	 * with locks..
4851da177e4SLinus Torvalds 	 */
48629884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* NOTE: special case
4891da177e4SLinus Torvalds 	 * 	If we're signalled while cleaning up locks on process exit, we
4901da177e4SLinus Torvalds 	 * 	still need to complete the unlock.
4911da177e4SLinus Torvalds 	 */
4921da177e4SLinus Torvalds 	lock_kernel();
4931da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
4941da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
4951da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
4961da177e4SLinus Torvalds 	else
4971da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
4981da177e4SLinus Torvalds 	unlock_kernel();
4991da177e4SLinus Torvalds 	return status;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct inode *inode = filp->f_mapping->host;
5051da177e4SLinus Torvalds 	int status;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	/*
5081da177e4SLinus Torvalds 	 * Flush all pending writes before doing anything
5091da177e4SLinus Torvalds 	 * with locks..
5101da177e4SLinus Torvalds 	 */
51129884df0STrond Myklebust 	status = nfs_sync_mapping(filp->f_mapping);
51229884df0STrond Myklebust 	if (status != 0)
5131da177e4SLinus Torvalds 		goto out;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	lock_kernel();
5161da177e4SLinus Torvalds 	/* Use local locking if mounted with "-onolock" */
5171da177e4SLinus Torvalds 	if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) {
5181da177e4SLinus Torvalds 		status = NFS_PROTO(inode)->lock(filp, cmd, fl);
5191da177e4SLinus Torvalds 		/* If we were signalled we still need to ensure that
5201da177e4SLinus Torvalds 		 * we clean up any state on the server. We therefore
5211da177e4SLinus Torvalds 		 * record the lock call as having succeeded in order to
5221da177e4SLinus Torvalds 		 * ensure that locks_remove_posix() cleans it out when
5231da177e4SLinus Torvalds 		 * the process exits.
5241da177e4SLinus Torvalds 		 */
5251da177e4SLinus Torvalds 		if (status == -EINTR || status == -ERESTARTSYS)
5261da177e4SLinus Torvalds 			do_vfs_lock(filp, fl);
5271da177e4SLinus Torvalds 	} else
5281da177e4SLinus Torvalds 		status = do_vfs_lock(filp, fl);
5291da177e4SLinus Torvalds 	unlock_kernel();
5301da177e4SLinus Torvalds 	if (status < 0)
5311da177e4SLinus Torvalds 		goto out;
5321da177e4SLinus Torvalds 	/*
5331da177e4SLinus Torvalds 	 * Make sure we clear the cache whenever we try to get the lock.
5341da177e4SLinus Torvalds 	 * This makes locking act as a cache coherency point.
5351da177e4SLinus Torvalds 	 */
53629884df0STrond Myklebust 	nfs_sync_mapping(filp->f_mapping);
5371da177e4SLinus Torvalds 	nfs_zap_caches(inode);
5381da177e4SLinus Torvalds out:
5391da177e4SLinus Torvalds 	return status;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds /*
5431da177e4SLinus Torvalds  * Lock a (portion of) a file
5441da177e4SLinus Torvalds  */
5451da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	struct inode * inode = filp->f_mapping->host;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
5501da177e4SLinus Torvalds 			inode->i_sb->s_id, inode->i_ino,
5511da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags,
5521da177e4SLinus Torvalds 			(long long)fl->fl_start, (long long)fl->fl_end);
55391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSLOCK);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	/* No mandatory locks over NFS */
5560800c5f7SASANO Masahiro 	if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
5570800c5f7SASANO Masahiro 	    fl->fl_type != F_UNLCK)
5581da177e4SLinus Torvalds 		return -ENOLCK;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	if (IS_GETLK(cmd))
5611da177e4SLinus Torvalds 		return do_getlk(filp, cmd, fl);
5621da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
5631da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
5641da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /*
5681da177e4SLinus Torvalds  * Lock a (portion of) a file
5691da177e4SLinus Torvalds  */
5701da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
57301cce933SJosef "Jeff" Sipek 			filp->f_path.dentry->d_inode->i_sb->s_id,
57401cce933SJosef "Jeff" Sipek 			filp->f_path.dentry->d_inode->i_ino,
5751da177e4SLinus Torvalds 			fl->fl_type, fl->fl_flags);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/*
5781da177e4SLinus Torvalds 	 * No BSD flocks over NFS allowed.
5791da177e4SLinus Torvalds 	 * Note: we could try to fake a POSIX lock request here by
5801da177e4SLinus Torvalds 	 * using ((u32) filp | 0x80000000) or some such as the pid.
5811da177e4SLinus Torvalds 	 * Not sure whether that would be unique, though, or whether
5821da177e4SLinus Torvalds 	 * that would break in other places.
5831da177e4SLinus Torvalds 	 */
5841da177e4SLinus Torvalds 	if (!(fl->fl_flags & FL_FLOCK))
5851da177e4SLinus Torvalds 		return -ENOLCK;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/* We're simulating flock() locks using posix locks on the server */
5881da177e4SLinus Torvalds 	fl->fl_owner = (fl_owner_t)filp;
5891da177e4SLinus Torvalds 	fl->fl_start = 0;
5901da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (fl->fl_type == F_UNLCK)
5931da177e4SLinus Torvalds 		return do_unlk(filp, cmd, fl);
5941da177e4SLinus Torvalds 	return do_setlk(filp, cmd, fl);
5951da177e4SLinus Torvalds }
596370f6599SJ. Bruce Fields 
597370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
598370f6599SJ. Bruce Fields {
599370f6599SJ. Bruce Fields 	/*
600370f6599SJ. Bruce Fields 	 * There is no protocol support for leases, so we have no way
601370f6599SJ. Bruce Fields 	 * to implement them correctly in the face of opens by other
602370f6599SJ. Bruce Fields 	 * clients.
603370f6599SJ. Bruce Fields 	 */
604370f6599SJ. Bruce Fields 	return -EINVAL;
605370f6599SJ. Bruce Fields }
606