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> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include <asm/uaccess.h> 321da177e4SLinus Torvalds #include <asm/system.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include "delegation.h" 3591d5b470SChuck Lever #include "iostat.h" 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds static int nfs_file_open(struct inode *, struct file *); 401da177e4SLinus Torvalds static int nfs_file_release(struct inode *, struct file *); 41980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin); 421da177e4SLinus Torvalds static int nfs_file_mmap(struct file *, struct vm_area_struct *); 431da177e4SLinus Torvalds static ssize_t nfs_file_sendfile(struct file *, loff_t *, size_t, read_actor_t, void *); 44027445c3SBadari Pulavarty static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov, 45027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 46027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov, 47027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos); 4875e1fcc0SMiklos Szeredi static int nfs_file_flush(struct file *, fl_owner_t id); 491da177e4SLinus Torvalds static int nfs_fsync(struct file *, struct dentry *dentry, int datasync); 501da177e4SLinus Torvalds static int nfs_check_flags(int flags); 511da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl); 521da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl); 531da177e4SLinus Torvalds 544b6f5d20SArjan van de Ven const struct file_operations nfs_file_operations = { 55980802e3STrond Myklebust .llseek = nfs_file_llseek, 561da177e4SLinus Torvalds .read = do_sync_read, 571da177e4SLinus Torvalds .write = do_sync_write, 581da177e4SLinus Torvalds .aio_read = nfs_file_read, 591da177e4SLinus Torvalds .aio_write = nfs_file_write, 601da177e4SLinus Torvalds .mmap = nfs_file_mmap, 611da177e4SLinus Torvalds .open = nfs_file_open, 621da177e4SLinus Torvalds .flush = nfs_file_flush, 631da177e4SLinus Torvalds .release = nfs_file_release, 641da177e4SLinus Torvalds .fsync = nfs_fsync, 651da177e4SLinus Torvalds .lock = nfs_lock, 661da177e4SLinus Torvalds .flock = nfs_flock, 671da177e4SLinus Torvalds .sendfile = nfs_file_sendfile, 681da177e4SLinus Torvalds .check_flags = nfs_check_flags, 691da177e4SLinus Torvalds }; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds struct inode_operations nfs_file_inode_operations = { 721da177e4SLinus Torvalds .permission = nfs_permission, 731da177e4SLinus Torvalds .getattr = nfs_getattr, 741da177e4SLinus Torvalds .setattr = nfs_setattr, 751da177e4SLinus Torvalds }; 761da177e4SLinus Torvalds 77b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 78b7fa0554SAndreas Gruenbacher struct inode_operations nfs3_file_inode_operations = { 79b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 80b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 81b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 82b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 83b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 84b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 85b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 86b7fa0554SAndreas Gruenbacher }; 87b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_v3 */ 88b7fa0554SAndreas Gruenbacher 891da177e4SLinus Torvalds /* Hack for future NFS swap support */ 901da177e4SLinus Torvalds #ifndef IS_SWAPFILE 911da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 921da177e4SLinus Torvalds #endif 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds static int nfs_check_flags(int flags) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 971da177e4SLinus Torvalds return -EINVAL; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds return 0; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* 1031da177e4SLinus Torvalds * Open file 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds static int 1061da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds int res; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 1111da177e4SLinus Torvalds if (res) 1121da177e4SLinus Torvalds return res; 1131da177e4SLinus Torvalds 11491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 1151da177e4SLinus Torvalds lock_kernel(); 1161f163415SDavid Howells res = NFS_PROTO(inode)->file_open(inode, filp); 1171da177e4SLinus Torvalds unlock_kernel(); 1181da177e4SLinus Torvalds return res; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds static int 1221da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds /* Ensure that dirty pages are flushed out with the right creds */ 1251da177e4SLinus Torvalds if (filp->f_mode & FMODE_WRITE) 1261da177e4SLinus Torvalds filemap_fdatawrite(filp->f_mapping); 12791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 1281da177e4SLinus Torvalds return NFS_PROTO(inode)->file_release(inode, filp); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 131980802e3STrond Myklebust /** 132980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 133980802e3STrond Myklebust * @inode - pointer to inode struct 134980802e3STrond Myklebust * @file - pointer to struct file 135980802e3STrond Myklebust * 136980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 137980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 138980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 139980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 140980802e3STrond Myklebust * shouldn't trust the cache). 141980802e3STrond Myklebust */ 142980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 143980802e3STrond Myklebust { 144980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 145980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 146980802e3STrond Myklebust 147980802e3STrond Myklebust if (server->flags & NFS_MOUNT_NOAC) 148980802e3STrond Myklebust goto force_reval; 149980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 150980802e3STrond Myklebust goto force_reval; 151980802e3STrond Myklebust if (nfsi->npages != 0) 152980802e3STrond Myklebust return 0; 15355296809SChuck Lever if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode)) 154fe51beecSTrond Myklebust return 0; 155980802e3STrond Myklebust force_reval: 156980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 157980802e3STrond Myklebust } 158980802e3STrond Myklebust 159980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 160980802e3STrond Myklebust { 161980802e3STrond Myklebust /* origin == SEEK_END => we must revalidate the cached file length */ 162aec5e175SJosef 'Jeff' Sipek if (origin == SEEK_END) { 163980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 164980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 165980802e3STrond Myklebust if (retval < 0) 166980802e3STrond Myklebust return (loff_t)retval; 167980802e3STrond Myklebust } 168980802e3STrond Myklebust return remote_llseek(filp, offset, origin); 169980802e3STrond Myklebust } 170980802e3STrond Myklebust 1711da177e4SLinus Torvalds /* 1721da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 1731da177e4SLinus Torvalds * 1741da177e4SLinus Torvalds */ 1751da177e4SLinus Torvalds static int 17675e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 1791da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 1801da177e4SLinus Torvalds int status; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 1851da177e4SLinus Torvalds return 0; 18691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 1871da177e4SLinus Torvalds lock_kernel(); 1881da177e4SLinus Torvalds /* Ensure that data+attribute caches are up to date after close() */ 1891da177e4SLinus Torvalds status = nfs_wb_all(inode); 1901da177e4SLinus Torvalds if (!status) { 1911da177e4SLinus Torvalds status = ctx->error; 1921da177e4SLinus Torvalds ctx->error = 0; 1933338c143STrond Myklebust if (!status) 1943338c143STrond Myklebust nfs_revalidate_inode(NFS_SERVER(inode), inode); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds unlock_kernel(); 1971da177e4SLinus Torvalds return status; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds static ssize_t 201027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 202027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds struct dentry * dentry = iocb->ki_filp->f_dentry; 2051da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 2061da177e4SLinus Torvalds ssize_t result; 207027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO 2101da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 211027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 2121da177e4SLinus Torvalds #endif 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n", 2151da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2161da177e4SLinus Torvalds (unsigned long) count, (unsigned long) pos); 2171da177e4SLinus Torvalds 21844b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 21991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count); 2201da177e4SLinus Torvalds if (!result) 221027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2221da177e4SLinus Torvalds return result; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds static ssize_t 2261da177e4SLinus Torvalds nfs_file_sendfile(struct file *filp, loff_t *ppos, size_t count, 2271da177e4SLinus Torvalds read_actor_t actor, void *target) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 2301da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2311da177e4SLinus Torvalds ssize_t res; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds dfprintk(VFS, "nfs: sendfile(%s/%s, %lu@%Lu)\n", 2341da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2351da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2361da177e4SLinus Torvalds 23744b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 2381da177e4SLinus Torvalds if (!res) 2391da177e4SLinus Torvalds res = generic_file_sendfile(filp, ppos, count, actor, target); 2401da177e4SLinus Torvalds return res; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static int 2441da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds struct dentry *dentry = file->f_dentry; 2471da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2481da177e4SLinus Torvalds int status; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds dfprintk(VFS, "nfs: mmap(%s/%s)\n", 2511da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2521da177e4SLinus Torvalds 25344b11874STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 2541da177e4SLinus Torvalds if (!status) 2551da177e4SLinus Torvalds status = generic_file_mmap(file, vma); 2561da177e4SLinus Torvalds return status; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /* 2601da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2611da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2621da177e4SLinus Torvalds * whether any write errors occurred for this process. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds static int 2651da177e4SLinus Torvalds nfs_fsync(struct file *file, struct dentry *dentry, int datasync) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 2681da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2691da177e4SLinus Torvalds int status; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 2721da177e4SLinus Torvalds 27391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 2741da177e4SLinus Torvalds lock_kernel(); 2751da177e4SLinus Torvalds status = nfs_wb_all(inode); 2761da177e4SLinus Torvalds if (!status) { 2771da177e4SLinus Torvalds status = ctx->error; 2781da177e4SLinus Torvalds ctx->error = 0; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds unlock_kernel(); 2811da177e4SLinus Torvalds return status; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* 2851da177e4SLinus Torvalds * This does the "real" work of the write. The generic routine has 2861da177e4SLinus Torvalds * allocated the page, locked it, done all the page alignment stuff 2871da177e4SLinus Torvalds * calculations etc. Now we should just copy the data from user 2881da177e4SLinus Torvalds * space and write it back to the real medium.. 2891da177e4SLinus Torvalds * 2901da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 2911da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 2921da177e4SLinus Torvalds */ 2931da177e4SLinus Torvalds static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds return nfs_flush_incompatible(file, page); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds long status; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds lock_kernel(); 3031da177e4SLinus Torvalds status = nfs_updatepage(file, page, offset, to-offset); 3041da177e4SLinus Torvalds unlock_kernel(); 3051da177e4SLinus Torvalds return status; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3082ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset) 309cd52ed35STrond Myklebust { 3101c75950bSTrond Myklebust loff_t range_start, range_end; 311d2ccddf0STrond Myklebust 3121c75950bSTrond Myklebust if (offset != 0) 3131c75950bSTrond Myklebust return; 314d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 3151c75950bSTrond Myklebust range_start = page_offset(page); 3161c75950bSTrond Myklebust range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 3171c75950bSTrond Myklebust nfs_sync_mapping_range(page->mapping, range_start, range_end, FLUSH_INVALIDATE); 318cd52ed35STrond Myklebust } 319cd52ed35STrond Myklebust 320cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 321cd52ed35STrond Myklebust { 322ddeff520SNikita Danilov if (gfp & __GFP_FS) 323cd52ed35STrond Myklebust return !nfs_wb_page(page->mapping->host, page); 324ddeff520SNikita Danilov else 325ddeff520SNikita Danilov /* 326ddeff520SNikita Danilov * Avoid deadlock on nfs_wait_on_request(). 327ddeff520SNikita Danilov */ 328ddeff520SNikita Danilov return 0; 329cd52ed35STrond Myklebust } 330cd52ed35STrond Myklebust 331f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 3321da177e4SLinus Torvalds .readpage = nfs_readpage, 3331da177e4SLinus Torvalds .readpages = nfs_readpages, 3341a54533eSTrond Myklebust .set_page_dirty = nfs_set_page_dirty, 3351da177e4SLinus Torvalds .writepage = nfs_writepage, 3361da177e4SLinus Torvalds .writepages = nfs_writepages, 3371da177e4SLinus Torvalds .prepare_write = nfs_prepare_write, 3381da177e4SLinus Torvalds .commit_write = nfs_commit_write, 339cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 340cd52ed35STrond Myklebust .releasepage = nfs_release_page, 3411da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO 3421da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 3431da177e4SLinus Torvalds #endif 3441da177e4SLinus Torvalds }; 3451da177e4SLinus Torvalds 346027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 347027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds struct dentry * dentry = iocb->ki_filp->f_dentry; 3501da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 3511da177e4SLinus Torvalds ssize_t result; 352027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds #ifdef CONFIG_NFS_DIRECTIO 3551da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 356027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 3571da177e4SLinus Torvalds #endif 3581da177e4SLinus Torvalds 359027445c3SBadari Pulavarty dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n", 3601da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 361027445c3SBadari Pulavarty inode->i_ino, (unsigned long) count, (long long) pos); 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds result = -EBUSY; 3641da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 3651da177e4SLinus Torvalds goto out_swapfile; 3667d52e862STrond Myklebust /* 3677d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 3687d52e862STrond Myklebust */ 3697d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 3707d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 3711da177e4SLinus Torvalds if (result) 3721da177e4SLinus Torvalds goto out; 373fe51beecSTrond Myklebust } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds result = count; 3761da177e4SLinus Torvalds if (!count) 3771da177e4SLinus Torvalds goto out; 3781da177e4SLinus Torvalds 37991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count); 380027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 381200baa21STrond Myklebust /* Return error values for O_SYNC and IS_SYNC() */ 382200baa21STrond Myklebust if (result >= 0 && (IS_SYNC(inode) || (iocb->ki_filp->f_flags & O_SYNC))) { 383200baa21STrond Myklebust int err = nfs_fsync(iocb->ki_filp, dentry, 1); 384200baa21STrond Myklebust if (err < 0) 385200baa21STrond Myklebust result = err; 386200baa21STrond Myklebust } 3871da177e4SLinus Torvalds out: 3881da177e4SLinus Torvalds return result; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds out_swapfile: 3911da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 3921da177e4SLinus Torvalds goto out; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds static int do_getlk(struct file *filp, int cmd, struct file_lock *fl) 3961da177e4SLinus Torvalds { 3978dc7c311SAndy Adamson struct file_lock cfl; 3981da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 3991da177e4SLinus Torvalds int status = 0; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds lock_kernel(); 402039c4d7aSTrond Myklebust /* Try local locking first */ 4038dc7c311SAndy Adamson if (posix_test_lock(filp, fl, &cfl)) { 404e4cd038aSTrond Myklebust fl->fl_start = cfl.fl_start; 405e4cd038aSTrond Myklebust fl->fl_end = cfl.fl_end; 406e4cd038aSTrond Myklebust fl->fl_type = cfl.fl_type; 407e4cd038aSTrond Myklebust fl->fl_pid = cfl.fl_pid; 408039c4d7aSTrond Myklebust goto out; 4091da177e4SLinus Torvalds } 410039c4d7aSTrond Myklebust 411039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 412039c4d7aSTrond Myklebust goto out_noconflict; 413039c4d7aSTrond Myklebust 414039c4d7aSTrond Myklebust if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) 415039c4d7aSTrond Myklebust goto out_noconflict; 416039c4d7aSTrond Myklebust 417039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 418039c4d7aSTrond Myklebust out: 4191da177e4SLinus Torvalds unlock_kernel(); 4201da177e4SLinus Torvalds return status; 421039c4d7aSTrond Myklebust out_noconflict: 422039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 423039c4d7aSTrond Myklebust goto out; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds int res = 0; 4291da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 4301da177e4SLinus Torvalds case FL_POSIX: 4311da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 4321da177e4SLinus Torvalds break; 4331da177e4SLinus Torvalds case FL_FLOCK: 4341da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 4351da177e4SLinus Torvalds break; 4361da177e4SLinus Torvalds default: 4371da177e4SLinus Torvalds BUG(); 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds if (res < 0) 4401da177e4SLinus Torvalds printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", 4411da177e4SLinus Torvalds __FUNCTION__); 4421da177e4SLinus Torvalds return res; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds static int do_unlk(struct file *filp, int cmd, struct file_lock *fl) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 4481da177e4SLinus Torvalds int status; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /* 4511da177e4SLinus Torvalds * Flush all pending writes before doing anything 4521da177e4SLinus Torvalds * with locks.. 4531da177e4SLinus Torvalds */ 45429884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* NOTE: special case 4571da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 4581da177e4SLinus Torvalds * still need to complete the unlock. 4591da177e4SLinus Torvalds */ 4601da177e4SLinus Torvalds lock_kernel(); 4611da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 4621da177e4SLinus Torvalds if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 4631da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 4641da177e4SLinus Torvalds else 4651da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 4661da177e4SLinus Torvalds unlock_kernel(); 4671da177e4SLinus Torvalds return status; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds static int do_setlk(struct file *filp, int cmd, struct file_lock *fl) 4711da177e4SLinus Torvalds { 4721da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 4731da177e4SLinus Torvalds int status; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds /* 4761da177e4SLinus Torvalds * Flush all pending writes before doing anything 4771da177e4SLinus Torvalds * with locks.. 4781da177e4SLinus Torvalds */ 47929884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 48029884df0STrond Myklebust if (status != 0) 4811da177e4SLinus Torvalds goto out; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds lock_kernel(); 4841da177e4SLinus Torvalds /* Use local locking if mounted with "-onolock" */ 4851da177e4SLinus Torvalds if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) { 4861da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 4871da177e4SLinus Torvalds /* If we were signalled we still need to ensure that 4881da177e4SLinus Torvalds * we clean up any state on the server. We therefore 4891da177e4SLinus Torvalds * record the lock call as having succeeded in order to 4901da177e4SLinus Torvalds * ensure that locks_remove_posix() cleans it out when 4911da177e4SLinus Torvalds * the process exits. 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds if (status == -EINTR || status == -ERESTARTSYS) 4941da177e4SLinus Torvalds do_vfs_lock(filp, fl); 4951da177e4SLinus Torvalds } else 4961da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 4971da177e4SLinus Torvalds unlock_kernel(); 4981da177e4SLinus Torvalds if (status < 0) 4991da177e4SLinus Torvalds goto out; 5001da177e4SLinus Torvalds /* 5011da177e4SLinus Torvalds * Make sure we clear the cache whenever we try to get the lock. 5021da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 5031da177e4SLinus Torvalds */ 50429884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 5051da177e4SLinus Torvalds nfs_zap_caches(inode); 5061da177e4SLinus Torvalds out: 5071da177e4SLinus Torvalds return status; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* 5111da177e4SLinus Torvalds * Lock a (portion of) a file 5121da177e4SLinus Torvalds */ 5131da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds struct inode * inode = filp->f_mapping->host; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n", 5181da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino, 5191da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 5201da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 52191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* No mandatory locks over NFS */ 5240800c5f7SASANO Masahiro if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && 5250800c5f7SASANO Masahiro fl->fl_type != F_UNLCK) 5261da177e4SLinus Torvalds return -ENOLCK; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds if (IS_GETLK(cmd)) 5291da177e4SLinus Torvalds return do_getlk(filp, cmd, fl); 5301da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 5311da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 5321da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* 5361da177e4SLinus Torvalds * Lock a (portion of) a file 5371da177e4SLinus Torvalds */ 5381da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n", 541e99170ffSTrond Myklebust filp->f_dentry->d_inode->i_sb->s_id, 542e99170ffSTrond Myklebust filp->f_dentry->d_inode->i_ino, 5431da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* 5461da177e4SLinus Torvalds * No BSD flocks over NFS allowed. 5471da177e4SLinus Torvalds * Note: we could try to fake a POSIX lock request here by 5481da177e4SLinus Torvalds * using ((u32) filp | 0x80000000) or some such as the pid. 5491da177e4SLinus Torvalds * Not sure whether that would be unique, though, or whether 5501da177e4SLinus Torvalds * that would break in other places. 5511da177e4SLinus Torvalds */ 5521da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 5531da177e4SLinus Torvalds return -ENOLCK; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 5561da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 5571da177e4SLinus Torvalds fl->fl_start = 0; 5581da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 5611da177e4SLinus Torvalds return do_unlk(filp, cmd, fl); 5621da177e4SLinus Torvalds return do_setlk(filp, cmd, fl); 5631da177e4SLinus Torvalds } 564