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/pagemap.h> 28e8edc6e0SAlexey Dobriyan #include <linux/aio.h> 295a0e3ad6STejun Heo #include <linux/gfp.h> 30b608b283STrond Myklebust #include <linux/swap.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" 38545db45fSDavid Howells #include "fscache.h" 39e5e94017SBenny Halevy #include "pnfs.h" 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 421da177e4SLinus Torvalds 43f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops; 4494387fb1STrond Myklebust 4592e1d5beSArjan van de Ven const struct inode_operations nfs_file_inode_operations = { 461da177e4SLinus Torvalds .permission = nfs_permission, 471da177e4SLinus Torvalds .getattr = nfs_getattr, 481da177e4SLinus Torvalds .setattr = nfs_setattr, 491da177e4SLinus Torvalds }; 501da177e4SLinus Torvalds 51b7fa0554SAndreas Gruenbacher #ifdef CONFIG_NFS_V3 5292e1d5beSArjan van de Ven const struct inode_operations nfs3_file_inode_operations = { 53b7fa0554SAndreas Gruenbacher .permission = nfs_permission, 54b7fa0554SAndreas Gruenbacher .getattr = nfs_getattr, 55b7fa0554SAndreas Gruenbacher .setattr = nfs_setattr, 56b7fa0554SAndreas Gruenbacher .listxattr = nfs3_listxattr, 57b7fa0554SAndreas Gruenbacher .getxattr = nfs3_getxattr, 58b7fa0554SAndreas Gruenbacher .setxattr = nfs3_setxattr, 59b7fa0554SAndreas Gruenbacher .removexattr = nfs3_removexattr, 60b7fa0554SAndreas Gruenbacher }; 61b7fa0554SAndreas Gruenbacher #endif /* CONFIG_NFS_v3 */ 62b7fa0554SAndreas Gruenbacher 631da177e4SLinus Torvalds /* Hack for future NFS swap support */ 641da177e4SLinus Torvalds #ifndef IS_SWAPFILE 651da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 661da177e4SLinus Torvalds #endif 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds static int nfs_check_flags(int flags) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 711da177e4SLinus Torvalds return -EINVAL; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds return 0; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* 771da177e4SLinus Torvalds * Open file 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds static int 801da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds int res; 831da177e4SLinus Torvalds 846da24bc9SChuck Lever dprintk("NFS: open file(%s/%s)\n", 85cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 86cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 87cc0dd2d1SChuck Lever 88c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 891da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 901da177e4SLinus Torvalds if (res) 911da177e4SLinus Torvalds return res; 921da177e4SLinus Torvalds 9346cb650cSTrond Myklebust res = nfs_open(inode, filp); 941da177e4SLinus Torvalds return res; 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds static int 981da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 991da177e4SLinus Torvalds { 1006da24bc9SChuck Lever dprintk("NFS: release(%s/%s)\n", 1016f276e49SRakib Mullick filp->f_path.dentry->d_parent->d_name.name, 1026f276e49SRakib Mullick filp->f_path.dentry->d_name.name); 1036da24bc9SChuck Lever 10491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 10546cb650cSTrond Myklebust return nfs_release(inode, filp); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 108980802e3STrond Myklebust /** 109980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 110980802e3STrond Myklebust * @inode - pointer to inode struct 111980802e3STrond Myklebust * @file - pointer to struct file 112980802e3STrond Myklebust * 113980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 114980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 115980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 116980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 117980802e3STrond Myklebust * shouldn't trust the cache). 118980802e3STrond Myklebust */ 119980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 120980802e3STrond Myklebust { 121980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 122980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 123980802e3STrond Myklebust 124d7cf8dd0STrond Myklebust if (nfs_have_delegated_attributes(inode)) 125d7cf8dd0STrond Myklebust goto out_noreval; 126d7cf8dd0STrond Myklebust 127980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 128980802e3STrond Myklebust goto force_reval; 129d7cf8dd0STrond Myklebust if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 130d7cf8dd0STrond Myklebust goto force_reval; 131d7cf8dd0STrond Myklebust if (nfs_attribute_timeout(inode)) 132d7cf8dd0STrond Myklebust goto force_reval; 133d7cf8dd0STrond Myklebust out_noreval: 134fe51beecSTrond Myklebust return 0; 135980802e3STrond Myklebust force_reval: 136980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 137980802e3STrond Myklebust } 138980802e3STrond Myklebust 139980802e3STrond Myklebust static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 140980802e3STrond Myklebust { 1416da24bc9SChuck Lever dprintk("NFS: llseek file(%s/%s, %lld, %d)\n", 142b84e06c5SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 143b84e06c5SChuck Lever filp->f_path.dentry->d_name.name, 144b84e06c5SChuck Lever offset, origin); 145b84e06c5SChuck Lever 14606222e49SJosef Bacik /* 14706222e49SJosef Bacik * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 14806222e49SJosef Bacik * the cached file length 14906222e49SJosef Bacik */ 15006222e49SJosef Bacik if (origin != SEEK_SET || origin != SEEK_CUR) { 151980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 152d5e66348STrond Myklebust 153980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 154980802e3STrond Myklebust if (retval < 0) 155980802e3STrond Myklebust return (loff_t)retval; 15679835a71SAndi Kleen } 157d5e66348STrond Myklebust 15879835a71SAndi Kleen return generic_file_llseek(filp, offset, origin); 159980802e3STrond Myklebust } 160980802e3STrond Myklebust 1611da177e4SLinus Torvalds /* 1621da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds static int 16575e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 1661da177e4SLinus Torvalds { 1676da24bc9SChuck Lever struct dentry *dentry = file->f_path.dentry; 1686da24bc9SChuck Lever struct inode *inode = dentry->d_inode; 1691da177e4SLinus Torvalds 1706da24bc9SChuck Lever dprintk("NFS: flush(%s/%s)\n", 1716da24bc9SChuck Lever dentry->d_parent->d_name.name, 1726da24bc9SChuck Lever dentry->d_name.name); 1731da177e4SLinus Torvalds 174c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 1751da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 1761da177e4SLinus Torvalds return 0; 1777b159fc1STrond Myklebust 1787fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 179af7fa165STrond Myklebust return vfs_fsync(file, 0); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static ssize_t 183027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 184027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 1851da177e4SLinus Torvalds { 18601cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 1871da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 1881da177e4SLinus Torvalds ssize_t result; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 191027445c3SBadari Pulavarty return nfs_file_direct_read(iocb, iov, nr_segs, pos); 1921da177e4SLinus Torvalds 1936da24bc9SChuck Lever dprintk("NFS: read(%s/%s, %lu@%lu)\n", 1941da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 1956f276e49SRakib Mullick (unsigned long) iov_length(iov, nr_segs), (unsigned long) pos); 1961da177e4SLinus Torvalds 19744b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 1984184dcf2SChuck Lever if (!result) { 199027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 2004184dcf2SChuck Lever if (result > 0) 2014184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 2024184dcf2SChuck Lever } 2031da177e4SLinus Torvalds return result; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static ssize_t 207f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 208f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 209f0930fffSJens Axboe unsigned int flags) 2101da177e4SLinus Torvalds { 21101cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 2121da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2131da177e4SLinus Torvalds ssize_t res; 2141da177e4SLinus Torvalds 2156da24bc9SChuck Lever dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n", 2161da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2171da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2181da177e4SLinus Torvalds 21944b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 220aa2f1ef1SChuck Lever if (!res) { 221f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 222aa2f1ef1SChuck Lever if (res > 0) 223aa2f1ef1SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res); 224aa2f1ef1SChuck Lever } 2251da177e4SLinus Torvalds return res; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static int 2291da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2301da177e4SLinus Torvalds { 23101cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 2321da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2331da177e4SLinus Torvalds int status; 2341da177e4SLinus Torvalds 2356da24bc9SChuck Lever dprintk("NFS: mmap(%s/%s)\n", 2361da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2371da177e4SLinus Torvalds 238e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 239e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 240e1ebfd33STrond Myklebust */ 241e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 24294387fb1STrond Myklebust if (!status) { 24394387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 244e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 24594387fb1STrond Myklebust } 2461da177e4SLinus Torvalds return status; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* 2501da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2511da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2521da177e4SLinus Torvalds * whether any write errors occurred for this process. 253af7fa165STrond Myklebust * 254af7fa165STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 255af7fa165STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 256af7fa165STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 257af7fa165STrond Myklebust * This is because the former is used to notify the _next_ call to 25825985edcSLucas De Marchi * nfs_file_write() that a write error occurred, and hence cause it to 259af7fa165STrond Myklebust * fall back to doing a synchronous write. 2601da177e4SLinus Torvalds */ 2611da177e4SLinus Torvalds static int 26202c24a82SJosef Bacik nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 2631da177e4SLinus Torvalds { 2647ea80859SChristoph Hellwig struct dentry *dentry = file->f_path.dentry; 265cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 2661da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 267af7fa165STrond Myklebust int have_error, status; 268af7fa165STrond Myklebust int ret = 0; 269af7fa165STrond Myklebust 2706da24bc9SChuck Lever dprintk("NFS: fsync file(%s/%s) datasync %d\n", 27154917786SChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 27254917786SChuck Lever datasync); 2731da177e4SLinus Torvalds 27402c24a82SJosef Bacik ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 27502c24a82SJosef Bacik if (ret) 27602c24a82SJosef Bacik return ret; 27702c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 27802c24a82SJosef Bacik 27991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 280af7fa165STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 281af7fa165STrond Myklebust status = nfs_commit_inode(inode, FLUSH_SYNC); 282af7fa165STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 283af7fa165STrond Myklebust if (have_error) 284af7fa165STrond Myklebust ret = xchg(&ctx->error, 0); 2850702099bSJ. R. Okajima if (!ret && status < 0) 286af7fa165STrond Myklebust ret = status; 287863a3c6cSAndy Adamson if (!ret && !datasync) 288863a3c6cSAndy Adamson /* application has asked for meta-data sync */ 289ef311537SAndy Adamson ret = pnfs_layoutcommit_inode(inode, true); 29002c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 291af7fa165STrond Myklebust return ret; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /* 29538c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 29638c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 29738c73044SPeter Staubach * page cache. 29838c73044SPeter Staubach * 29938c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 30038c73044SPeter Staubach * being completely filled by the writer. In this situation, the 30138c73044SPeter Staubach * page must be completely written to stable storage on the server 30238c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 30338c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 30438c73044SPeter Staubach * done. 30538c73044SPeter Staubach * 30638c73044SPeter Staubach * It may be more efficient to read the page first if the file is 30738c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 30838c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 30938c73044SPeter Staubach * indicating that it was previously allocated and then modified, 31038c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 31138c73044SPeter Staubach * and that the new data won't completely replace the old data in 31238c73044SPeter Staubach * that range of the file. 31338c73044SPeter Staubach */ 31438c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page, 31538c73044SPeter Staubach loff_t pos, unsigned len) 31638c73044SPeter Staubach { 31738c73044SPeter Staubach unsigned int pglen = nfs_page_length(page); 31838c73044SPeter Staubach unsigned int offset = pos & (PAGE_CACHE_SIZE - 1); 31938c73044SPeter Staubach unsigned int end = offset + len; 32038c73044SPeter Staubach 32138c73044SPeter Staubach if ((file->f_mode & FMODE_READ) && /* open for read? */ 32238c73044SPeter Staubach !PageUptodate(page) && /* Uptodate? */ 32338c73044SPeter Staubach !PagePrivate(page) && /* i/o request already? */ 32438c73044SPeter Staubach pglen && /* valid bytes of file? */ 32538c73044SPeter Staubach (end < pglen || offset)) /* replace all valid bytes? */ 32638c73044SPeter Staubach return 1; 32738c73044SPeter Staubach return 0; 32838c73044SPeter Staubach } 32938c73044SPeter Staubach 33038c73044SPeter Staubach /* 3314899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3324899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3334899f9c8SNick Piggin * data from user space. 3341da177e4SLinus Torvalds * 3351da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3361da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3371da177e4SLinus Torvalds */ 3384899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3394899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3404899f9c8SNick Piggin struct page **pagep, void **fsdata) 3411da177e4SLinus Torvalds { 3424899f9c8SNick Piggin int ret; 34338c73044SPeter Staubach pgoff_t index = pos >> PAGE_CACHE_SHIFT; 3444899f9c8SNick Piggin struct page *page; 34538c73044SPeter Staubach int once_thru = 0; 3464899f9c8SNick Piggin 347b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n", 348b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 349b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 350b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 351b7eaefaaSChuck Lever 35238c73044SPeter Staubach start: 35372cb77f4STrond Myklebust /* 35472cb77f4STrond Myklebust * Prevent starvation issues if someone is doing a consistency 35572cb77f4STrond Myklebust * sync-to-disk 35672cb77f4STrond Myklebust */ 35772cb77f4STrond Myklebust ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING, 35872cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 35972cb77f4STrond Myklebust if (ret) 36072cb77f4STrond Myklebust return ret; 36172cb77f4STrond Myklebust 36254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3634899f9c8SNick Piggin if (!page) 3644899f9c8SNick Piggin return -ENOMEM; 3654899f9c8SNick Piggin *pagep = page; 3664899f9c8SNick Piggin 3674899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3684899f9c8SNick Piggin if (ret) { 3694899f9c8SNick Piggin unlock_page(page); 3704899f9c8SNick Piggin page_cache_release(page); 37138c73044SPeter Staubach } else if (!once_thru && 37238c73044SPeter Staubach nfs_want_read_modify_write(file, page, pos, len)) { 37338c73044SPeter Staubach once_thru = 1; 37438c73044SPeter Staubach ret = nfs_readpage(file, page); 37538c73044SPeter Staubach page_cache_release(page); 37638c73044SPeter Staubach if (!ret) 37738c73044SPeter Staubach goto start; 3784899f9c8SNick Piggin } 3794899f9c8SNick Piggin return ret; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3824899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3834899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3844899f9c8SNick Piggin struct page *page, void *fsdata) 3851da177e4SLinus Torvalds { 3864899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 3874899f9c8SNick Piggin int status; 3881da177e4SLinus Torvalds 389b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n", 390b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 391b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 392b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 393b7eaefaaSChuck Lever 394efc91ed0STrond Myklebust /* 395efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 396efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 397efc91ed0STrond Myklebust */ 398efc91ed0STrond Myklebust if (!PageUptodate(page)) { 399efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 400efc91ed0STrond Myklebust unsigned end = offset + len; 401efc91ed0STrond Myklebust 402efc91ed0STrond Myklebust if (pglen == 0) { 403efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 404efc91ed0STrond Myklebust end, PAGE_CACHE_SIZE); 405efc91ed0STrond Myklebust SetPageUptodate(page); 406efc91ed0STrond Myklebust } else if (end >= pglen) { 407efc91ed0STrond Myklebust zero_user_segment(page, end, PAGE_CACHE_SIZE); 408efc91ed0STrond Myklebust if (offset == 0) 409efc91ed0STrond Myklebust SetPageUptodate(page); 410efc91ed0STrond Myklebust } else 411efc91ed0STrond Myklebust zero_user_segment(page, pglen, PAGE_CACHE_SIZE); 412efc91ed0STrond Myklebust } 413efc91ed0STrond Myklebust 4144899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4154899f9c8SNick Piggin 4164899f9c8SNick Piggin unlock_page(page); 4174899f9c8SNick Piggin page_cache_release(page); 4184899f9c8SNick Piggin 4193d509e54SChuck Lever if (status < 0) 4203d509e54SChuck Lever return status; 4213d509e54SChuck Lever return copied; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4246b9b3514SDavid Howells /* 4256b9b3514SDavid Howells * Partially or wholly invalidate a page 4266b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4276b9b3514SDavid Howells * page invalidation 428545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4296b9b3514SDavid Howells * - Caller holds page lock 4306b9b3514SDavid Howells */ 4312ff28e22SNeilBrown static void nfs_invalidate_page(struct page *page, unsigned long offset) 432cd52ed35STrond Myklebust { 433b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset); 434b7eaefaaSChuck Lever 4351c75950bSTrond Myklebust if (offset != 0) 4361c75950bSTrond Myklebust return; 437d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 4381b3b4a1aSTrond Myklebust nfs_wb_page_cancel(page->mapping->host, page); 439545db45fSDavid Howells 440545db45fSDavid Howells nfs_fscache_invalidate_page(page, page->mapping->host); 441cd52ed35STrond Myklebust } 442cd52ed35STrond Myklebust 4436b9b3514SDavid Howells /* 4446b9b3514SDavid Howells * Attempt to release the private state associated with a page 445545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4466b9b3514SDavid Howells * - Caller holds page lock 4476b9b3514SDavid Howells * - Return true (may release page) or false (may not) 4486b9b3514SDavid Howells */ 449cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 450cd52ed35STrond Myklebust { 451b608b283STrond Myklebust struct address_space *mapping = page->mapping; 452b608b283STrond Myklebust 453b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 454b7eaefaaSChuck Lever 455d812e575STrond Myklebust /* Only do I/O if gfp is a superset of GFP_KERNEL */ 456b608b283STrond Myklebust if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) { 457b608b283STrond Myklebust int how = FLUSH_SYNC; 458b608b283STrond Myklebust 459b608b283STrond Myklebust /* Don't let kswapd deadlock waiting for OOM RPC calls */ 460b608b283STrond Myklebust if (current_is_kswapd()) 461b608b283STrond Myklebust how = 0; 462b608b283STrond Myklebust nfs_commit_inode(mapping->host, how); 463b608b283STrond Myklebust } 464e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 465545db45fSDavid Howells if (PagePrivate(page)) 466ddeff520SNikita Danilov return 0; 467545db45fSDavid Howells return nfs_fscache_release_page(page, gfp); 468e3db7691STrond Myklebust } 469e3db7691STrond Myklebust 4706b9b3514SDavid Howells /* 4716b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 4726b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 4736b9b3514SDavid Howells * destroyed 474545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 4756b9b3514SDavid Howells * - Caller holds page lock 4766b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 4776b9b3514SDavid Howells */ 478e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 479e3db7691STrond Myklebust { 480b7eaefaaSChuck Lever struct inode *inode = page->mapping->host; 481545db45fSDavid Howells struct nfs_inode *nfsi = NFS_I(inode); 482b7eaefaaSChuck Lever 483b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 484b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 485b7eaefaaSChuck Lever 486545db45fSDavid Howells nfs_fscache_wait_on_page_write(nfsi, page); 487b7eaefaaSChuck Lever return nfs_wb_page(inode, page); 488cd52ed35STrond Myklebust } 489cd52ed35STrond Myklebust 490f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 4911da177e4SLinus Torvalds .readpage = nfs_readpage, 4921da177e4SLinus Torvalds .readpages = nfs_readpages, 4939cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 4941da177e4SLinus Torvalds .writepage = nfs_writepage, 4951da177e4SLinus Torvalds .writepages = nfs_writepages, 4964899f9c8SNick Piggin .write_begin = nfs_write_begin, 4974899f9c8SNick Piggin .write_end = nfs_write_end, 498cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 499cd52ed35STrond Myklebust .releasepage = nfs_release_page, 5001da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 501074cc1deSTrond Myklebust .migratepage = nfs_migrate_page, 502e3db7691STrond Myklebust .launder_page = nfs_launder_page, 503f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 5041da177e4SLinus Torvalds }; 5051da177e4SLinus Torvalds 5066b9b3514SDavid Howells /* 5076b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5086b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5096b9b3514SDavid Howells * shared-writable mapping 5106b9b3514SDavid Howells */ 511c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 51294387fb1STrond Myklebust { 513c2ec175cSNick Piggin struct page *page = vmf->page; 51494387fb1STrond Myklebust struct file *filp = vma->vm_file; 515b7eaefaaSChuck Lever struct dentry *dentry = filp->f_path.dentry; 51694387fb1STrond Myklebust unsigned pagelen; 517bc4866b6STrond Myklebust int ret = VM_FAULT_NOPAGE; 5184899f9c8SNick Piggin struct address_space *mapping; 51994387fb1STrond Myklebust 520b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n", 521b7eaefaaSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 522b7eaefaaSChuck Lever filp->f_mapping->host->i_ino, 523b7eaefaaSChuck Lever (long long)page_offset(page)); 524b7eaefaaSChuck Lever 525545db45fSDavid Howells /* make sure the cache has finished storing the page */ 526545db45fSDavid Howells nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page); 527545db45fSDavid Howells 52894387fb1STrond Myklebust lock_page(page); 5294899f9c8SNick Piggin mapping = page->mapping; 530b7eaefaaSChuck Lever if (mapping != dentry->d_inode->i_mapping) 5318b1f9ee5STrond Myklebust goto out_unlock; 5328b1f9ee5STrond Myklebust 5334899f9c8SNick Piggin pagelen = nfs_page_length(page); 5348b1f9ee5STrond Myklebust if (pagelen == 0) 5358b1f9ee5STrond Myklebust goto out_unlock; 5368b1f9ee5STrond Myklebust 537bc4866b6STrond Myklebust ret = VM_FAULT_LOCKED; 538bc4866b6STrond Myklebust if (nfs_flush_incompatible(filp, page) == 0 && 539bc4866b6STrond Myklebust nfs_updatepage(filp, page, 0, pagelen) == 0) 540bc4866b6STrond Myklebust goto out; 5418b1f9ee5STrond Myklebust 542bc4866b6STrond Myklebust ret = VM_FAULT_SIGBUS; 5438b1f9ee5STrond Myklebust out_unlock: 5444899f9c8SNick Piggin unlock_page(page); 545bc4866b6STrond Myklebust out: 546bc4866b6STrond Myklebust return ret; 54794387fb1STrond Myklebust } 54894387fb1STrond Myklebust 549f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 55094387fb1STrond Myklebust .fault = filemap_fault, 55194387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 55294387fb1STrond Myklebust }; 55394387fb1STrond Myklebust 5547b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 5557b159fc1STrond Myklebust { 5567b159fc1STrond Myklebust struct nfs_open_context *ctx; 5577b159fc1STrond Myklebust 5586b2f3d1fSChristoph Hellwig if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC)) 5597b159fc1STrond Myklebust return 1; 560cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 5617b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 5627b159fc1STrond Myklebust return 1; 5637b159fc1STrond Myklebust return 0; 5647b159fc1STrond Myklebust } 5657b159fc1STrond Myklebust 566027445c3SBadari Pulavarty static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 567027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 5681da177e4SLinus Torvalds { 56901cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 5701da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 5717e381172SChuck Lever unsigned long written = 0; 5721da177e4SLinus Torvalds ssize_t result; 573027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 576027445c3SBadari Pulavarty return nfs_file_direct_write(iocb, iov, nr_segs, pos); 5771da177e4SLinus Torvalds 5786da24bc9SChuck Lever dprintk("NFS: write(%s/%s, %lu@%Ld)\n", 5791da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 5806da24bc9SChuck Lever (unsigned long) count, (long long) pos); 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds result = -EBUSY; 5831da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 5841da177e4SLinus Torvalds goto out_swapfile; 5857d52e862STrond Myklebust /* 5867d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 5877d52e862STrond Myklebust */ 5887d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 5897d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 5901da177e4SLinus Torvalds if (result) 5911da177e4SLinus Torvalds goto out; 592fe51beecSTrond Myklebust } 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds result = count; 5951da177e4SLinus Torvalds if (!count) 5961da177e4SLinus Torvalds goto out; 5971da177e4SLinus Torvalds 598027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 5997e381172SChuck Lever if (result > 0) 6007e381172SChuck Lever written = result; 6017e381172SChuck Lever 6026b2f3d1fSChristoph Hellwig /* Return error values for O_DSYNC and IS_SYNC() */ 6037b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 604af7fa165STrond Myklebust int err = vfs_fsync(iocb->ki_filp, 0); 605200baa21STrond Myklebust if (err < 0) 606200baa21STrond Myklebust result = err; 607200baa21STrond Myklebust } 6087e381172SChuck Lever if (result > 0) 6097e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 6101da177e4SLinus Torvalds out: 6111da177e4SLinus Torvalds return result; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds out_swapfile: 6141da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 6151da177e4SLinus Torvalds goto out; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 618bf40d343SSuresh Jayaraman static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 619bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 620bf40d343SSuresh Jayaraman size_t count, unsigned int flags) 621bf40d343SSuresh Jayaraman { 622bf40d343SSuresh Jayaraman struct dentry *dentry = filp->f_path.dentry; 623bf40d343SSuresh Jayaraman struct inode *inode = dentry->d_inode; 6247e381172SChuck Lever unsigned long written = 0; 625bf40d343SSuresh Jayaraman ssize_t ret; 626bf40d343SSuresh Jayaraman 627bf40d343SSuresh Jayaraman dprintk("NFS splice_write(%s/%s, %lu@%llu)\n", 628bf40d343SSuresh Jayaraman dentry->d_parent->d_name.name, dentry->d_name.name, 629bf40d343SSuresh Jayaraman (unsigned long) count, (unsigned long long) *ppos); 630bf40d343SSuresh Jayaraman 631bf40d343SSuresh Jayaraman /* 632bf40d343SSuresh Jayaraman * The combination of splice and an O_APPEND destination is disallowed. 633bf40d343SSuresh Jayaraman */ 634bf40d343SSuresh Jayaraman 635bf40d343SSuresh Jayaraman ret = generic_file_splice_write(pipe, filp, ppos, count, flags); 6367e381172SChuck Lever if (ret > 0) 6377e381172SChuck Lever written = ret; 6387e381172SChuck Lever 639bf40d343SSuresh Jayaraman if (ret >= 0 && nfs_need_sync_write(filp, inode)) { 640af7fa165STrond Myklebust int err = vfs_fsync(filp, 0); 641bf40d343SSuresh Jayaraman if (err < 0) 642bf40d343SSuresh Jayaraman ret = err; 643bf40d343SSuresh Jayaraman } 6447e381172SChuck Lever if (ret > 0) 6457e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 646bf40d343SSuresh Jayaraman return ret; 647bf40d343SSuresh Jayaraman } 648bf40d343SSuresh Jayaraman 6495eebde23SSuresh Jayaraman static int 6505eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6531da177e4SLinus Torvalds int status = 0; 65421ac19d4SSergey Vlasov unsigned int saved_type = fl->fl_type; 6551da177e4SLinus Torvalds 656039c4d7aSTrond Myklebust /* Try local locking first */ 6576d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 6586d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 6596d34ac19SJ. Bruce Fields /* found a conflict */ 660039c4d7aSTrond Myklebust goto out; 6611da177e4SLinus Torvalds } 66221ac19d4SSergey Vlasov fl->fl_type = saved_type; 663039c4d7aSTrond Myklebust 664039c4d7aSTrond Myklebust if (nfs_have_delegation(inode, FMODE_READ)) 665039c4d7aSTrond Myklebust goto out_noconflict; 666039c4d7aSTrond Myklebust 6675eebde23SSuresh Jayaraman if (is_local) 668039c4d7aSTrond Myklebust goto out_noconflict; 669039c4d7aSTrond Myklebust 670039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 671039c4d7aSTrond Myklebust out: 6721da177e4SLinus Torvalds return status; 673039c4d7aSTrond Myklebust out_noconflict: 674039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 675039c4d7aSTrond Myklebust goto out; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds int res = 0; 6811da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 6821da177e4SLinus Torvalds case FL_POSIX: 6831da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 6841da177e4SLinus Torvalds break; 6851da177e4SLinus Torvalds case FL_FLOCK: 6861da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 6871da177e4SLinus Torvalds break; 6881da177e4SLinus Torvalds default: 6891da177e4SLinus Torvalds BUG(); 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds return res; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6945eebde23SSuresh Jayaraman static int 6955eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 6961da177e4SLinus Torvalds { 6971da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6981da177e4SLinus Torvalds int status; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* 7011da177e4SLinus Torvalds * Flush all pending writes before doing anything 7021da177e4SLinus Torvalds * with locks.. 7031da177e4SLinus Torvalds */ 70429884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds /* NOTE: special case 7071da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 7081da177e4SLinus Torvalds * still need to complete the unlock. 7091da177e4SLinus Torvalds */ 7105eebde23SSuresh Jayaraman /* 7115eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7125eebde23SSuresh Jayaraman * "-olocal_lock=" 7135eebde23SSuresh Jayaraman */ 7145eebde23SSuresh Jayaraman if (!is_local) 7151da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 7161da177e4SLinus Torvalds else 7171da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7181da177e4SLinus Torvalds return status; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7215eebde23SSuresh Jayaraman static int 7226b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) { 7236b96724eSRicardo Labiaga return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000)); 7246b96724eSRicardo Labiaga } 7256b96724eSRicardo Labiaga 7266b96724eSRicardo Labiaga static int 7275eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7301da177e4SLinus Torvalds int status; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds /* 7331da177e4SLinus Torvalds * Flush all pending writes before doing anything 7341da177e4SLinus Torvalds * with locks.. 7351da177e4SLinus Torvalds */ 73629884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 73729884df0STrond Myklebust if (status != 0) 7381da177e4SLinus Torvalds goto out; 7391da177e4SLinus Torvalds 7405eebde23SSuresh Jayaraman /* 7415eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7425eebde23SSuresh Jayaraman * "-olocal_lock=" 7435eebde23SSuresh Jayaraman */ 7445eebde23SSuresh Jayaraman if (!is_local) 7451da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 746c4d7c402STrond Myklebust else 7471da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7481da177e4SLinus Torvalds if (status < 0) 7491da177e4SLinus Torvalds goto out; 7506b96724eSRicardo Labiaga 7511da177e4SLinus Torvalds /* 7526b96724eSRicardo Labiaga * Revalidate the cache if the server has time stamps granular 7536b96724eSRicardo Labiaga * enough to detect subsecond changes. Otherwise, clear the 7546b96724eSRicardo Labiaga * cache to prevent missing any changes. 7556b96724eSRicardo Labiaga * 7561da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 7571da177e4SLinus Torvalds */ 75829884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 7596b96724eSRicardo Labiaga if (!nfs_have_delegation(inode, FMODE_READ)) { 7606b96724eSRicardo Labiaga if (is_time_granular(&NFS_SERVER(inode)->time_delta)) 7616b96724eSRicardo Labiaga __nfs_revalidate_inode(NFS_SERVER(inode), inode); 7626b96724eSRicardo Labiaga else 7631da177e4SLinus Torvalds nfs_zap_caches(inode); 7646b96724eSRicardo Labiaga } 7651da177e4SLinus Torvalds out: 7661da177e4SLinus Torvalds return status; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * Lock a (portion of) a file 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 7731da177e4SLinus Torvalds { 7741da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7752116271aSTrond Myklebust int ret = -ENOLCK; 7765eebde23SSuresh Jayaraman int is_local = 0; 7771da177e4SLinus Torvalds 7786da24bc9SChuck Lever dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n", 7796da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 7806da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 7811da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 7821da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 7836da24bc9SChuck Lever 78491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds /* No mandatory locks over NFS */ 787dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 7882116271aSTrond Myklebust goto out_err; 7892116271aSTrond Myklebust 7905eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 7915eebde23SSuresh Jayaraman is_local = 1; 7925eebde23SSuresh Jayaraman 7932116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 7942116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 7952116271aSTrond Myklebust if (ret < 0) 7962116271aSTrond Myklebust goto out_err; 7972116271aSTrond Myklebust } 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds if (IS_GETLK(cmd)) 8005eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8012116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 8025eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8032116271aSTrond Myklebust else 8045eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 8052116271aSTrond Myklebust out_err: 8062116271aSTrond Myklebust return ret; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds /* 8101da177e4SLinus Torvalds * Lock a (portion of) a file 8111da177e4SLinus Torvalds */ 8121da177e4SLinus Torvalds static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 8131da177e4SLinus Torvalds { 8145eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 8155eebde23SSuresh Jayaraman int is_local = 0; 8165eebde23SSuresh Jayaraman 8176da24bc9SChuck Lever dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n", 8186da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 8196da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 8201da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 8231da177e4SLinus Torvalds return -ENOLCK; 8241da177e4SLinus Torvalds 8255eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 8265eebde23SSuresh Jayaraman is_local = 1; 8275eebde23SSuresh Jayaraman 8281da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 8291da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 8301da177e4SLinus Torvalds fl->fl_start = 0; 8311da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 8345eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 8355eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 8361da177e4SLinus Torvalds } 837370f6599SJ. Bruce Fields 8386da24bc9SChuck Lever /* 8396da24bc9SChuck Lever * There is no protocol support for leases, so we have no way to implement 8406da24bc9SChuck Lever * them correctly in the face of opens by other clients. 8416da24bc9SChuck Lever */ 842370f6599SJ. Bruce Fields static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 843370f6599SJ. Bruce Fields { 8446da24bc9SChuck Lever dprintk("NFS: setlease(%s/%s, arg=%ld)\n", 8456da24bc9SChuck Lever file->f_path.dentry->d_parent->d_name.name, 8466da24bc9SChuck Lever file->f_path.dentry->d_name.name, arg); 847370f6599SJ. Bruce Fields return -EINVAL; 848370f6599SJ. Bruce Fields } 8491788ea6eSJeff Layton 850*0486958fSJeff Layton const struct file_operations nfs_file_operations = { 851*0486958fSJeff Layton .llseek = nfs_file_llseek, 852*0486958fSJeff Layton .read = do_sync_read, 853*0486958fSJeff Layton .write = do_sync_write, 854*0486958fSJeff Layton .aio_read = nfs_file_read, 855*0486958fSJeff Layton .aio_write = nfs_file_write, 856*0486958fSJeff Layton .mmap = nfs_file_mmap, 857*0486958fSJeff Layton .open = nfs_file_open, 858*0486958fSJeff Layton .flush = nfs_file_flush, 859*0486958fSJeff Layton .release = nfs_file_release, 860*0486958fSJeff Layton .fsync = nfs_file_fsync, 861*0486958fSJeff Layton .lock = nfs_lock, 862*0486958fSJeff Layton .flock = nfs_flock, 863*0486958fSJeff Layton .splice_read = nfs_file_splice_read, 864*0486958fSJeff Layton .splice_write = nfs_file_splice_write, 865*0486958fSJeff Layton .check_flags = nfs_check_flags, 866*0486958fSJeff Layton .setlease = nfs_setlease, 867*0486958fSJeff Layton }; 868*0486958fSJeff Layton 8691788ea6eSJeff Layton #ifdef CONFIG_NFS_V4 8701788ea6eSJeff Layton static int 8711788ea6eSJeff Layton nfs4_file_open(struct inode *inode, struct file *filp) 8721788ea6eSJeff Layton { 8731788ea6eSJeff Layton /* 8741788ea6eSJeff Layton * NFSv4 opens are handled in d_lookup and d_revalidate. If we get to 8751788ea6eSJeff Layton * this point, then something is very wrong 8761788ea6eSJeff Layton */ 8771788ea6eSJeff Layton dprintk("NFS: %s called! inode=%p filp=%p\n", __func__, inode, filp); 8781788ea6eSJeff Layton return -ENOTDIR; 8791788ea6eSJeff Layton } 8801788ea6eSJeff Layton 8811788ea6eSJeff Layton const struct file_operations nfs4_file_operations = { 8821788ea6eSJeff Layton .llseek = nfs_file_llseek, 8831788ea6eSJeff Layton .read = do_sync_read, 8841788ea6eSJeff Layton .write = do_sync_write, 8851788ea6eSJeff Layton .aio_read = nfs_file_read, 8861788ea6eSJeff Layton .aio_write = nfs_file_write, 8871788ea6eSJeff Layton .mmap = nfs_file_mmap, 8881788ea6eSJeff Layton .open = nfs4_file_open, 8891788ea6eSJeff Layton .flush = nfs_file_flush, 8901788ea6eSJeff Layton .release = nfs_file_release, 8911788ea6eSJeff Layton .fsync = nfs_file_fsync, 8921788ea6eSJeff Layton .lock = nfs_lock, 8931788ea6eSJeff Layton .flock = nfs_flock, 8941788ea6eSJeff Layton .splice_read = nfs_file_splice_read, 8951788ea6eSJeff Layton .splice_write = nfs_file_splice_write, 8961788ea6eSJeff Layton .check_flags = nfs_check_flags, 8971788ea6eSJeff Layton .setlease = nfs_setlease, 8981788ea6eSJeff Layton }; 8991788ea6eSJeff Layton #endif /* CONFIG_NFS_V4 */ 900