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 19ddda8e0aSBryan Schumaker #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/time.h> 211da177e4SLinus Torvalds #include <linux/kernel.h> 221da177e4SLinus Torvalds #include <linux/errno.h> 231da177e4SLinus Torvalds #include <linux/fcntl.h> 241da177e4SLinus Torvalds #include <linux/stat.h> 251da177e4SLinus Torvalds #include <linux/nfs_fs.h> 261da177e4SLinus Torvalds #include <linux/nfs_mount.h> 271da177e4SLinus Torvalds #include <linux/mm.h> 281da177e4SLinus Torvalds #include <linux/pagemap.h> 29e8edc6e0SAlexey Dobriyan #include <linux/aio.h> 305a0e3ad6STejun Heo #include <linux/gfp.h> 31b608b283STrond Myklebust #include <linux/swap.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <asm/uaccess.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #include "delegation.h" 3694387fb1STrond Myklebust #include "internal.h" 3791d5b470SChuck Lever #include "iostat.h" 38545db45fSDavid Howells #include "fscache.h" 391da177e4SLinus Torvalds 40f4ce1299STrond Myklebust #include "nfstrace.h" 41f4ce1299STrond Myklebust 421da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 431da177e4SLinus Torvalds 44f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops; 4594387fb1STrond Myklebust 461da177e4SLinus Torvalds /* Hack for future NFS swap support */ 471da177e4SLinus Torvalds #ifndef IS_SWAPFILE 481da177e4SLinus Torvalds # define IS_SWAPFILE(inode) (0) 491da177e4SLinus Torvalds #endif 501da177e4SLinus Torvalds 51ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags) 521da177e4SLinus Torvalds { 531da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 541da177e4SLinus Torvalds return -EINVAL; 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds return 0; 571da177e4SLinus Torvalds } 5889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags); 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* 611da177e4SLinus Torvalds * Open file 621da177e4SLinus Torvalds */ 631da177e4SLinus Torvalds static int 641da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 651da177e4SLinus Torvalds { 661da177e4SLinus Torvalds int res; 671da177e4SLinus Torvalds 686da24bc9SChuck Lever dprintk("NFS: open file(%s/%s)\n", 69cc0dd2d1SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 70cc0dd2d1SChuck Lever filp->f_path.dentry->d_name.name); 71cc0dd2d1SChuck Lever 72c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 731da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 741da177e4SLinus Torvalds if (res) 751da177e4SLinus Torvalds return res; 761da177e4SLinus Torvalds 7746cb650cSTrond Myklebust res = nfs_open(inode, filp); 781da177e4SLinus Torvalds return res; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 81ce4ef7c0SBryan Schumaker int 821da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 831da177e4SLinus Torvalds { 846da24bc9SChuck Lever dprintk("NFS: release(%s/%s)\n", 856f276e49SRakib Mullick filp->f_path.dentry->d_parent->d_name.name, 866f276e49SRakib Mullick filp->f_path.dentry->d_name.name); 876da24bc9SChuck Lever 8891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 8946cb650cSTrond Myklebust return nfs_release(inode, filp); 901da177e4SLinus Torvalds } 9189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release); 921da177e4SLinus Torvalds 93980802e3STrond Myklebust /** 94980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 95980802e3STrond Myklebust * @inode - pointer to inode struct 96980802e3STrond Myklebust * @file - pointer to struct file 97980802e3STrond Myklebust * 98980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 99980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 100980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 101980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 102980802e3STrond Myklebust * shouldn't trust the cache). 103980802e3STrond Myklebust */ 104980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 105980802e3STrond Myklebust { 106980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 107980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 108980802e3STrond Myklebust 109d7cf8dd0STrond Myklebust if (nfs_have_delegated_attributes(inode)) 110d7cf8dd0STrond Myklebust goto out_noreval; 111d7cf8dd0STrond Myklebust 112980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 113980802e3STrond Myklebust goto force_reval; 114d7cf8dd0STrond Myklebust if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 115d7cf8dd0STrond Myklebust goto force_reval; 116d7cf8dd0STrond Myklebust if (nfs_attribute_timeout(inode)) 117d7cf8dd0STrond Myklebust goto force_reval; 118d7cf8dd0STrond Myklebust out_noreval: 119fe51beecSTrond Myklebust return 0; 120980802e3STrond Myklebust force_reval: 121980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 122980802e3STrond Myklebust } 123980802e3STrond Myklebust 124965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence) 125980802e3STrond Myklebust { 1266da24bc9SChuck Lever dprintk("NFS: llseek file(%s/%s, %lld, %d)\n", 127b84e06c5SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 128b84e06c5SChuck Lever filp->f_path.dentry->d_name.name, 129965c8e59SAndrew Morton offset, whence); 130b84e06c5SChuck Lever 13106222e49SJosef Bacik /* 132965c8e59SAndrew Morton * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 13306222e49SJosef Bacik * the cached file length 13406222e49SJosef Bacik */ 135965c8e59SAndrew Morton if (whence != SEEK_SET && whence != SEEK_CUR) { 136980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 137d5e66348STrond Myklebust 138980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 139980802e3STrond Myklebust if (retval < 0) 140980802e3STrond Myklebust return (loff_t)retval; 14179835a71SAndi Kleen } 142d5e66348STrond Myklebust 143965c8e59SAndrew Morton return generic_file_llseek(filp, offset, whence); 144980802e3STrond Myklebust } 14589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek); 146980802e3STrond Myklebust 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 1491da177e4SLinus Torvalds */ 150ce4ef7c0SBryan Schumaker int 15175e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 1521da177e4SLinus Torvalds { 1536da24bc9SChuck Lever struct dentry *dentry = file->f_path.dentry; 1546da24bc9SChuck Lever struct inode *inode = dentry->d_inode; 1551da177e4SLinus Torvalds 1566da24bc9SChuck Lever dprintk("NFS: flush(%s/%s)\n", 1576da24bc9SChuck Lever dentry->d_parent->d_name.name, 1586da24bc9SChuck Lever dentry->d_name.name); 1591da177e4SLinus Torvalds 160c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 1611da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 1621da177e4SLinus Torvalds return 0; 1637b159fc1STrond Myklebust 16414546c33STrond Myklebust /* 16514546c33STrond Myklebust * If we're holding a write delegation, then just start the i/o 16614546c33STrond Myklebust * but don't wait for completion (or send a commit). 16714546c33STrond Myklebust */ 168011e2a7fSBryan Schumaker if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 16914546c33STrond Myklebust return filemap_fdatawrite(file->f_mapping); 17014546c33STrond Myklebust 1717fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 172af7fa165STrond Myklebust return vfs_fsync(file, 0); 1731da177e4SLinus Torvalds } 17489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_flush); 1751da177e4SLinus Torvalds 176ce4ef7c0SBryan Schumaker ssize_t 177027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 178027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 1791da177e4SLinus Torvalds { 18001cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 1811da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 1821da177e4SLinus Torvalds ssize_t result; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 185a564b8f0SMel Gorman return nfs_file_direct_read(iocb, iov, nr_segs, pos, true); 1861da177e4SLinus Torvalds 1876da24bc9SChuck Lever dprintk("NFS: read(%s/%s, %lu@%lu)\n", 1881da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 1896f276e49SRakib Mullick (unsigned long) iov_length(iov, nr_segs), (unsigned long) pos); 1901da177e4SLinus Torvalds 19144b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 1924184dcf2SChuck Lever if (!result) { 193027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 1944184dcf2SChuck Lever if (result > 0) 1954184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 1964184dcf2SChuck Lever } 1971da177e4SLinus Torvalds return result; 1981da177e4SLinus Torvalds } 19989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read); 2001da177e4SLinus Torvalds 201ce4ef7c0SBryan Schumaker ssize_t 202f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 203f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 204f0930fffSJens Axboe unsigned int flags) 2051da177e4SLinus Torvalds { 20601cce933SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 2071da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2081da177e4SLinus Torvalds ssize_t res; 2091da177e4SLinus Torvalds 2106da24bc9SChuck Lever dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n", 2111da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 2121da177e4SLinus Torvalds (unsigned long) count, (unsigned long long) *ppos); 2131da177e4SLinus Torvalds 21444b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 215aa2f1ef1SChuck Lever if (!res) { 216f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 217aa2f1ef1SChuck Lever if (res > 0) 218aa2f1ef1SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res); 219aa2f1ef1SChuck Lever } 2201da177e4SLinus Torvalds return res; 2211da177e4SLinus Torvalds } 22289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_read); 2231da177e4SLinus Torvalds 224ce4ef7c0SBryan Schumaker int 2251da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2261da177e4SLinus Torvalds { 22701cce933SJosef "Jeff" Sipek struct dentry *dentry = file->f_path.dentry; 2281da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2291da177e4SLinus Torvalds int status; 2301da177e4SLinus Torvalds 2316da24bc9SChuck Lever dprintk("NFS: mmap(%s/%s)\n", 2321da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name); 2331da177e4SLinus Torvalds 234e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 235e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 236e1ebfd33STrond Myklebust */ 237e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 23894387fb1STrond Myklebust if (!status) { 23994387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 240e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 24194387fb1STrond Myklebust } 2421da177e4SLinus Torvalds return status; 2431da177e4SLinus Torvalds } 24489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds /* 2471da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2481da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2491da177e4SLinus Torvalds * whether any write errors occurred for this process. 250af7fa165STrond Myklebust * 251af7fa165STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 252af7fa165STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 253af7fa165STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 254af7fa165STrond Myklebust * This is because the former is used to notify the _next_ call to 25525985edcSLucas De Marchi * nfs_file_write() that a write error occurred, and hence cause it to 256af7fa165STrond Myklebust * fall back to doing a synchronous write. 2571da177e4SLinus Torvalds */ 258ce4ef7c0SBryan Schumaker int 259a5c58892SBryan Schumaker nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync) 2601da177e4SLinus Torvalds { 2617ea80859SChristoph Hellwig struct dentry *dentry = file->f_path.dentry; 262cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 2631da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26405990d1bSTrond Myklebust int have_error, do_resend, status; 265af7fa165STrond Myklebust int ret = 0; 266af7fa165STrond Myklebust 2676da24bc9SChuck Lever dprintk("NFS: fsync file(%s/%s) datasync %d\n", 26854917786SChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 26954917786SChuck Lever datasync); 2701da177e4SLinus Torvalds 27191d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 27205990d1bSTrond Myklebust do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); 273af7fa165STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 274af7fa165STrond Myklebust status = nfs_commit_inode(inode, FLUSH_SYNC); 275af7fa165STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 27605990d1bSTrond Myklebust if (have_error) { 277af7fa165STrond Myklebust ret = xchg(&ctx->error, 0); 27805990d1bSTrond Myklebust if (ret) 27905990d1bSTrond Myklebust goto out; 28005990d1bSTrond Myklebust } 28105990d1bSTrond Myklebust if (status < 0) { 282af7fa165STrond Myklebust ret = status; 28305990d1bSTrond Myklebust goto out; 28405990d1bSTrond Myklebust } 28505990d1bSTrond Myklebust do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); 28605990d1bSTrond Myklebust if (do_resend) 28705990d1bSTrond Myklebust ret = -EAGAIN; 28805990d1bSTrond Myklebust out: 289a5c58892SBryan Schumaker return ret; 290a5c58892SBryan Schumaker } 29189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_fsync_commit); 292a5c58892SBryan Schumaker 293a5c58892SBryan Schumaker static int 294a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 295a5c58892SBryan Schumaker { 296a5c58892SBryan Schumaker int ret; 297496ad9aaSAl Viro struct inode *inode = file_inode(file); 298a5c58892SBryan Schumaker 299f4ce1299STrond Myklebust trace_nfs_fsync_enter(inode); 300f4ce1299STrond Myklebust 30105990d1bSTrond Myklebust do { 302a5c58892SBryan Schumaker ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 3037b281ee0STrond Myklebust if (ret != 0) 30405990d1bSTrond Myklebust break; 305a5c58892SBryan Schumaker mutex_lock(&inode->i_mutex); 306a5c58892SBryan Schumaker ret = nfs_file_fsync_commit(file, start, end, datasync); 30702c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 308dcfc4f25STrond Myklebust /* 309dcfc4f25STrond Myklebust * If nfs_file_fsync_commit detected a server reboot, then 310dcfc4f25STrond Myklebust * resend all dirty pages that might have been covered by 311dcfc4f25STrond Myklebust * the NFS_CONTEXT_RESEND_WRITES flag 312dcfc4f25STrond Myklebust */ 313dcfc4f25STrond Myklebust start = 0; 314dcfc4f25STrond Myklebust end = LLONG_MAX; 31505990d1bSTrond Myklebust } while (ret == -EAGAIN); 31605990d1bSTrond Myklebust 317f4ce1299STrond Myklebust trace_nfs_fsync_exit(inode, ret); 318af7fa165STrond Myklebust return ret; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* 32238c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 32338c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 32438c73044SPeter Staubach * page cache. 32538c73044SPeter Staubach * 32638c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 32738c73044SPeter Staubach * being completely filled by the writer. In this situation, the 32838c73044SPeter Staubach * page must be completely written to stable storage on the server 32938c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 33038c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 33138c73044SPeter Staubach * done. 33238c73044SPeter Staubach * 33338c73044SPeter Staubach * It may be more efficient to read the page first if the file is 33438c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 33538c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 33638c73044SPeter Staubach * indicating that it was previously allocated and then modified, 33738c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 33838c73044SPeter Staubach * and that the new data won't completely replace the old data in 33938c73044SPeter Staubach * that range of the file. 34038c73044SPeter Staubach */ 34138c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page, 34238c73044SPeter Staubach loff_t pos, unsigned len) 34338c73044SPeter Staubach { 34438c73044SPeter Staubach unsigned int pglen = nfs_page_length(page); 34538c73044SPeter Staubach unsigned int offset = pos & (PAGE_CACHE_SIZE - 1); 34638c73044SPeter Staubach unsigned int end = offset + len; 34738c73044SPeter Staubach 34838c73044SPeter Staubach if ((file->f_mode & FMODE_READ) && /* open for read? */ 34938c73044SPeter Staubach !PageUptodate(page) && /* Uptodate? */ 35038c73044SPeter Staubach !PagePrivate(page) && /* i/o request already? */ 35138c73044SPeter Staubach pglen && /* valid bytes of file? */ 35238c73044SPeter Staubach (end < pglen || offset)) /* replace all valid bytes? */ 35338c73044SPeter Staubach return 1; 35438c73044SPeter Staubach return 0; 35538c73044SPeter Staubach } 35638c73044SPeter Staubach 35738c73044SPeter Staubach /* 3584899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3594899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3604899f9c8SNick Piggin * data from user space. 3611da177e4SLinus Torvalds * 3621da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3631da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3641da177e4SLinus Torvalds */ 3654899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3664899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3674899f9c8SNick Piggin struct page **pagep, void **fsdata) 3681da177e4SLinus Torvalds { 3694899f9c8SNick Piggin int ret; 37038c73044SPeter Staubach pgoff_t index = pos >> PAGE_CACHE_SHIFT; 3714899f9c8SNick Piggin struct page *page; 37238c73044SPeter Staubach int once_thru = 0; 3734899f9c8SNick Piggin 374b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n", 375b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 376b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 377b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 378b7eaefaaSChuck Lever 37938c73044SPeter Staubach start: 38072cb77f4STrond Myklebust /* 38172cb77f4STrond Myklebust * Prevent starvation issues if someone is doing a consistency 38272cb77f4STrond Myklebust * sync-to-disk 38372cb77f4STrond Myklebust */ 38472cb77f4STrond Myklebust ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING, 38572cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 38672cb77f4STrond Myklebust if (ret) 38772cb77f4STrond Myklebust return ret; 38872cb77f4STrond Myklebust 38954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3904899f9c8SNick Piggin if (!page) 3914899f9c8SNick Piggin return -ENOMEM; 3924899f9c8SNick Piggin *pagep = page; 3934899f9c8SNick Piggin 3944899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3954899f9c8SNick Piggin if (ret) { 3964899f9c8SNick Piggin unlock_page(page); 3974899f9c8SNick Piggin page_cache_release(page); 39838c73044SPeter Staubach } else if (!once_thru && 39938c73044SPeter Staubach nfs_want_read_modify_write(file, page, pos, len)) { 40038c73044SPeter Staubach once_thru = 1; 40138c73044SPeter Staubach ret = nfs_readpage(file, page); 40238c73044SPeter Staubach page_cache_release(page); 40338c73044SPeter Staubach if (!ret) 40438c73044SPeter Staubach goto start; 4054899f9c8SNick Piggin } 4064899f9c8SNick Piggin return ret; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4094899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 4104899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 4114899f9c8SNick Piggin struct page *page, void *fsdata) 4121da177e4SLinus Torvalds { 4134899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 4144899f9c8SNick Piggin int status; 4151da177e4SLinus Torvalds 416b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n", 417b7eaefaaSChuck Lever file->f_path.dentry->d_parent->d_name.name, 418b7eaefaaSChuck Lever file->f_path.dentry->d_name.name, 419b7eaefaaSChuck Lever mapping->host->i_ino, len, (long long) pos); 420b7eaefaaSChuck Lever 421efc91ed0STrond Myklebust /* 422efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 423efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 424efc91ed0STrond Myklebust */ 425efc91ed0STrond Myklebust if (!PageUptodate(page)) { 426efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 427efc91ed0STrond Myklebust unsigned end = offset + len; 428efc91ed0STrond Myklebust 429efc91ed0STrond Myklebust if (pglen == 0) { 430efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 431efc91ed0STrond Myklebust end, PAGE_CACHE_SIZE); 432efc91ed0STrond Myklebust SetPageUptodate(page); 433efc91ed0STrond Myklebust } else if (end >= pglen) { 434efc91ed0STrond Myklebust zero_user_segment(page, end, PAGE_CACHE_SIZE); 435efc91ed0STrond Myklebust if (offset == 0) 436efc91ed0STrond Myklebust SetPageUptodate(page); 437efc91ed0STrond Myklebust } else 438efc91ed0STrond Myklebust zero_user_segment(page, pglen, PAGE_CACHE_SIZE); 439efc91ed0STrond Myklebust } 440efc91ed0STrond Myklebust 4414899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4424899f9c8SNick Piggin 4434899f9c8SNick Piggin unlock_page(page); 4444899f9c8SNick Piggin page_cache_release(page); 4454899f9c8SNick Piggin 4463d509e54SChuck Lever if (status < 0) 4473d509e54SChuck Lever return status; 4482701d086SAndy Adamson NFS_I(mapping->host)->write_io += copied; 4493d509e54SChuck Lever return copied; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4526b9b3514SDavid Howells /* 4536b9b3514SDavid Howells * Partially or wholly invalidate a page 4546b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4556b9b3514SDavid Howells * page invalidation 456545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4576b9b3514SDavid Howells * - Caller holds page lock 4586b9b3514SDavid Howells */ 459d47992f8SLukas Czerner static void nfs_invalidate_page(struct page *page, unsigned int offset, 460d47992f8SLukas Czerner unsigned int length) 461cd52ed35STrond Myklebust { 462d47992f8SLukas Czerner dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n", 463d47992f8SLukas Czerner page, offset, length); 464b7eaefaaSChuck Lever 465d47992f8SLukas Czerner if (offset != 0 || length < PAGE_CACHE_SIZE) 4661c75950bSTrond Myklebust return; 467d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 468d56b4ddfSMel Gorman nfs_wb_page_cancel(page_file_mapping(page)->host, page); 469545db45fSDavid Howells 470545db45fSDavid Howells nfs_fscache_invalidate_page(page, page->mapping->host); 471cd52ed35STrond Myklebust } 472cd52ed35STrond Myklebust 4736b9b3514SDavid Howells /* 4746b9b3514SDavid Howells * Attempt to release the private state associated with a page 475545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4766b9b3514SDavid Howells * - Caller holds page lock 4776b9b3514SDavid Howells * - Return true (may release page) or false (may not) 4786b9b3514SDavid Howells */ 479cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 480cd52ed35STrond Myklebust { 481b608b283STrond Myklebust struct address_space *mapping = page->mapping; 482b608b283STrond Myklebust 483b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 484b7eaefaaSChuck Lever 4855cf02d09SJeff Layton /* Only do I/O if gfp is a superset of GFP_KERNEL, and we're not 4865cf02d09SJeff Layton * doing this memory reclaim for a fs-related allocation. 4875cf02d09SJeff Layton */ 4885cf02d09SJeff Layton if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL && 4895cf02d09SJeff Layton !(current->flags & PF_FSTRANS)) { 490b608b283STrond Myklebust int how = FLUSH_SYNC; 491b608b283STrond Myklebust 492b608b283STrond Myklebust /* Don't let kswapd deadlock waiting for OOM RPC calls */ 493b608b283STrond Myklebust if (current_is_kswapd()) 494b608b283STrond Myklebust how = 0; 495b608b283STrond Myklebust nfs_commit_inode(mapping->host, how); 496b608b283STrond Myklebust } 497e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 498545db45fSDavid Howells if (PagePrivate(page)) 499ddeff520SNikita Danilov return 0; 500545db45fSDavid Howells return nfs_fscache_release_page(page, gfp); 501e3db7691STrond Myklebust } 502e3db7691STrond Myklebust 503f919b196SMel Gorman static void nfs_check_dirty_writeback(struct page *page, 504f919b196SMel Gorman bool *dirty, bool *writeback) 505f919b196SMel Gorman { 506f919b196SMel Gorman struct nfs_inode *nfsi; 507f919b196SMel Gorman struct address_space *mapping = page_file_mapping(page); 508f919b196SMel Gorman 509f919b196SMel Gorman if (!mapping || PageSwapCache(page)) 510f919b196SMel Gorman return; 511f919b196SMel Gorman 512f919b196SMel Gorman /* 513f919b196SMel Gorman * Check if an unstable page is currently being committed and 514f919b196SMel Gorman * if so, have the VM treat it as if the page is under writeback 515f919b196SMel Gorman * so it will not block due to pages that will shortly be freeable. 516f919b196SMel Gorman */ 517f919b196SMel Gorman nfsi = NFS_I(mapping->host); 518f919b196SMel Gorman if (test_bit(NFS_INO_COMMIT, &nfsi->flags)) { 519f919b196SMel Gorman *writeback = true; 520f919b196SMel Gorman return; 521f919b196SMel Gorman } 522f919b196SMel Gorman 523f919b196SMel Gorman /* 524f919b196SMel Gorman * If PagePrivate() is set, then the page is not freeable and as the 525f919b196SMel Gorman * inode is not being committed, it's not going to be cleaned in the 526f919b196SMel Gorman * near future so treat it as dirty 527f919b196SMel Gorman */ 528f919b196SMel Gorman if (PagePrivate(page)) 529f919b196SMel Gorman *dirty = true; 530f919b196SMel Gorman } 531f919b196SMel Gorman 5326b9b3514SDavid Howells /* 5336b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 5346b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 5356b9b3514SDavid Howells * destroyed 536545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 5376b9b3514SDavid Howells * - Caller holds page lock 5386b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 5396b9b3514SDavid Howells */ 540e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 541e3db7691STrond Myklebust { 542d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 543545db45fSDavid Howells struct nfs_inode *nfsi = NFS_I(inode); 544b7eaefaaSChuck Lever 545b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 546b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 547b7eaefaaSChuck Lever 548545db45fSDavid Howells nfs_fscache_wait_on_page_write(nfsi, page); 549b7eaefaaSChuck Lever return nfs_wb_page(inode, page); 550cd52ed35STrond Myklebust } 551cd52ed35STrond Myklebust 552a564b8f0SMel Gorman #ifdef CONFIG_NFS_SWAP 553a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file, 554a564b8f0SMel Gorman sector_t *span) 555a564b8f0SMel Gorman { 556a564b8f0SMel Gorman *span = sis->pages; 557a564b8f0SMel Gorman return xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 1); 558a564b8f0SMel Gorman } 559a564b8f0SMel Gorman 560a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file) 561a564b8f0SMel Gorman { 562a564b8f0SMel Gorman xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 0); 563a564b8f0SMel Gorman } 564a564b8f0SMel Gorman #endif 565a564b8f0SMel Gorman 566f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 5671da177e4SLinus Torvalds .readpage = nfs_readpage, 5681da177e4SLinus Torvalds .readpages = nfs_readpages, 5699cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 5701da177e4SLinus Torvalds .writepage = nfs_writepage, 5711da177e4SLinus Torvalds .writepages = nfs_writepages, 5724899f9c8SNick Piggin .write_begin = nfs_write_begin, 5734899f9c8SNick Piggin .write_end = nfs_write_end, 574cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 575cd52ed35STrond Myklebust .releasepage = nfs_release_page, 5761da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 577074cc1deSTrond Myklebust .migratepage = nfs_migrate_page, 578e3db7691STrond Myklebust .launder_page = nfs_launder_page, 579f919b196SMel Gorman .is_dirty_writeback = nfs_check_dirty_writeback, 580f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 581a564b8f0SMel Gorman #ifdef CONFIG_NFS_SWAP 582a564b8f0SMel Gorman .swap_activate = nfs_swap_activate, 583a564b8f0SMel Gorman .swap_deactivate = nfs_swap_deactivate, 584a564b8f0SMel Gorman #endif 5851da177e4SLinus Torvalds }; 5861da177e4SLinus Torvalds 5876b9b3514SDavid Howells /* 5886b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5896b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5906b9b3514SDavid Howells * shared-writable mapping 5916b9b3514SDavid Howells */ 592c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 59394387fb1STrond Myklebust { 594c2ec175cSNick Piggin struct page *page = vmf->page; 59594387fb1STrond Myklebust struct file *filp = vma->vm_file; 596b7eaefaaSChuck Lever struct dentry *dentry = filp->f_path.dentry; 59794387fb1STrond Myklebust unsigned pagelen; 598bc4866b6STrond Myklebust int ret = VM_FAULT_NOPAGE; 5994899f9c8SNick Piggin struct address_space *mapping; 60094387fb1STrond Myklebust 601b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n", 602b7eaefaaSChuck Lever dentry->d_parent->d_name.name, dentry->d_name.name, 603b7eaefaaSChuck Lever filp->f_mapping->host->i_ino, 604b7eaefaaSChuck Lever (long long)page_offset(page)); 605b7eaefaaSChuck Lever 606545db45fSDavid Howells /* make sure the cache has finished storing the page */ 607545db45fSDavid Howells nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page); 608545db45fSDavid Howells 60994387fb1STrond Myklebust lock_page(page); 610d56b4ddfSMel Gorman mapping = page_file_mapping(page); 611b7eaefaaSChuck Lever if (mapping != dentry->d_inode->i_mapping) 6128b1f9ee5STrond Myklebust goto out_unlock; 6138b1f9ee5STrond Myklebust 6142aeb98f4STrond Myklebust wait_on_page_writeback(page); 6152aeb98f4STrond Myklebust 6164899f9c8SNick Piggin pagelen = nfs_page_length(page); 6178b1f9ee5STrond Myklebust if (pagelen == 0) 6188b1f9ee5STrond Myklebust goto out_unlock; 6198b1f9ee5STrond Myklebust 620bc4866b6STrond Myklebust ret = VM_FAULT_LOCKED; 621bc4866b6STrond Myklebust if (nfs_flush_incompatible(filp, page) == 0 && 622bc4866b6STrond Myklebust nfs_updatepage(filp, page, 0, pagelen) == 0) 623bc4866b6STrond Myklebust goto out; 6248b1f9ee5STrond Myklebust 625bc4866b6STrond Myklebust ret = VM_FAULT_SIGBUS; 6268b1f9ee5STrond Myklebust out_unlock: 6274899f9c8SNick Piggin unlock_page(page); 628bc4866b6STrond Myklebust out: 629bc4866b6STrond Myklebust return ret; 63094387fb1STrond Myklebust } 63194387fb1STrond Myklebust 632f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 63394387fb1STrond Myklebust .fault = filemap_fault, 63494387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 6350b173bc4SKonstantin Khlebnikov .remap_pages = generic_file_remap_pages, 63694387fb1STrond Myklebust }; 63794387fb1STrond Myklebust 6387b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 6397b159fc1STrond Myklebust { 6407b159fc1STrond Myklebust struct nfs_open_context *ctx; 6417b159fc1STrond Myklebust 6426b2f3d1fSChristoph Hellwig if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC)) 6437b159fc1STrond Myklebust return 1; 644cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 6457b159fc1STrond Myklebust if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) 6467b159fc1STrond Myklebust return 1; 6477b159fc1STrond Myklebust return 0; 6487b159fc1STrond Myklebust } 6497b159fc1STrond Myklebust 650ce4ef7c0SBryan Schumaker ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 651027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 6521da177e4SLinus Torvalds { 65301cce933SJosef "Jeff" Sipek struct dentry * dentry = iocb->ki_filp->f_path.dentry; 6541da177e4SLinus Torvalds struct inode * inode = dentry->d_inode; 6557e381172SChuck Lever unsigned long written = 0; 6561da177e4SLinus Torvalds ssize_t result; 657027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 660a564b8f0SMel Gorman return nfs_file_direct_write(iocb, iov, nr_segs, pos, true); 6611da177e4SLinus Torvalds 6626da24bc9SChuck Lever dprintk("NFS: write(%s/%s, %lu@%Ld)\n", 6631da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name, 6646da24bc9SChuck Lever (unsigned long) count, (long long) pos); 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds result = -EBUSY; 6671da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 6681da177e4SLinus Torvalds goto out_swapfile; 6697d52e862STrond Myklebust /* 6707d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 6717d52e862STrond Myklebust */ 6727d52e862STrond Myklebust if (iocb->ki_filp->f_flags & O_APPEND) { 6737d52e862STrond Myklebust result = nfs_revalidate_file_size(inode, iocb->ki_filp); 6741da177e4SLinus Torvalds if (result) 6751da177e4SLinus Torvalds goto out; 676fe51beecSTrond Myklebust } 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds result = count; 6791da177e4SLinus Torvalds if (!count) 6801da177e4SLinus Torvalds goto out; 6811da177e4SLinus Torvalds 682027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 6837e381172SChuck Lever if (result > 0) 6847e381172SChuck Lever written = result; 6857e381172SChuck Lever 6866b2f3d1fSChristoph Hellwig /* Return error values for O_DSYNC and IS_SYNC() */ 6877b159fc1STrond Myklebust if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) { 688af7fa165STrond Myklebust int err = vfs_fsync(iocb->ki_filp, 0); 689200baa21STrond Myklebust if (err < 0) 690200baa21STrond Myklebust result = err; 691200baa21STrond Myklebust } 6927e381172SChuck Lever if (result > 0) 6937e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 6941da177e4SLinus Torvalds out: 6951da177e4SLinus Torvalds return result; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds out_swapfile: 6981da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 6991da177e4SLinus Torvalds goto out; 7001da177e4SLinus Torvalds } 70189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write); 7021da177e4SLinus Torvalds 703ce4ef7c0SBryan Schumaker ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 704bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 705bf40d343SSuresh Jayaraman size_t count, unsigned int flags) 706bf40d343SSuresh Jayaraman { 707bf40d343SSuresh Jayaraman struct dentry *dentry = filp->f_path.dentry; 708bf40d343SSuresh Jayaraman struct inode *inode = dentry->d_inode; 7097e381172SChuck Lever unsigned long written = 0; 710bf40d343SSuresh Jayaraman ssize_t ret; 711bf40d343SSuresh Jayaraman 712bf40d343SSuresh Jayaraman dprintk("NFS splice_write(%s/%s, %lu@%llu)\n", 713bf40d343SSuresh Jayaraman dentry->d_parent->d_name.name, dentry->d_name.name, 714bf40d343SSuresh Jayaraman (unsigned long) count, (unsigned long long) *ppos); 715bf40d343SSuresh Jayaraman 716bf40d343SSuresh Jayaraman /* 717bf40d343SSuresh Jayaraman * The combination of splice and an O_APPEND destination is disallowed. 718bf40d343SSuresh Jayaraman */ 719bf40d343SSuresh Jayaraman 720bf40d343SSuresh Jayaraman ret = generic_file_splice_write(pipe, filp, ppos, count, flags); 7217e381172SChuck Lever if (ret > 0) 7227e381172SChuck Lever written = ret; 7237e381172SChuck Lever 724bf40d343SSuresh Jayaraman if (ret >= 0 && nfs_need_sync_write(filp, inode)) { 725af7fa165STrond Myklebust int err = vfs_fsync(filp, 0); 726bf40d343SSuresh Jayaraman if (err < 0) 727bf40d343SSuresh Jayaraman ret = err; 728bf40d343SSuresh Jayaraman } 7297e381172SChuck Lever if (ret > 0) 7307e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 731bf40d343SSuresh Jayaraman return ret; 732bf40d343SSuresh Jayaraman } 73389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_write); 734bf40d343SSuresh Jayaraman 7355eebde23SSuresh Jayaraman static int 7365eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7391da177e4SLinus Torvalds int status = 0; 74021ac19d4SSergey Vlasov unsigned int saved_type = fl->fl_type; 7411da177e4SLinus Torvalds 742039c4d7aSTrond Myklebust /* Try local locking first */ 7436d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 7446d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 7456d34ac19SJ. Bruce Fields /* found a conflict */ 746039c4d7aSTrond Myklebust goto out; 7471da177e4SLinus Torvalds } 74821ac19d4SSergey Vlasov fl->fl_type = saved_type; 749039c4d7aSTrond Myklebust 750011e2a7fSBryan Schumaker if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 751039c4d7aSTrond Myklebust goto out_noconflict; 752039c4d7aSTrond Myklebust 7535eebde23SSuresh Jayaraman if (is_local) 754039c4d7aSTrond Myklebust goto out_noconflict; 755039c4d7aSTrond Myklebust 756039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 757039c4d7aSTrond Myklebust out: 7581da177e4SLinus Torvalds return status; 759039c4d7aSTrond Myklebust out_noconflict: 760039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 761039c4d7aSTrond Myklebust goto out; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 7651da177e4SLinus Torvalds { 7661da177e4SLinus Torvalds int res = 0; 7671da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 7681da177e4SLinus Torvalds case FL_POSIX: 7691da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 7701da177e4SLinus Torvalds break; 7711da177e4SLinus Torvalds case FL_FLOCK: 7721da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 7731da177e4SLinus Torvalds break; 7741da177e4SLinus Torvalds default: 7751da177e4SLinus Torvalds BUG(); 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds return res; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7805eebde23SSuresh Jayaraman static int 7815eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7847a8203d8STrond Myklebust struct nfs_lock_context *l_ctx; 7851da177e4SLinus Torvalds int status; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /* 7881da177e4SLinus Torvalds * Flush all pending writes before doing anything 7891da177e4SLinus Torvalds * with locks.. 7901da177e4SLinus Torvalds */ 79129884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 7921da177e4SLinus Torvalds 7937a8203d8STrond Myklebust l_ctx = nfs_get_lock_context(nfs_file_open_context(filp)); 7947a8203d8STrond Myklebust if (!IS_ERR(l_ctx)) { 7957a8203d8STrond Myklebust status = nfs_iocounter_wait(&l_ctx->io_count); 7967a8203d8STrond Myklebust nfs_put_lock_context(l_ctx); 7977a8203d8STrond Myklebust if (status < 0) 7987a8203d8STrond Myklebust return status; 7997a8203d8STrond Myklebust } 8007a8203d8STrond Myklebust 8011da177e4SLinus Torvalds /* NOTE: special case 8021da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 8031da177e4SLinus Torvalds * still need to complete the unlock. 8041da177e4SLinus Torvalds */ 8055eebde23SSuresh Jayaraman /* 8065eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 8075eebde23SSuresh Jayaraman * "-olocal_lock=" 8085eebde23SSuresh Jayaraman */ 8095eebde23SSuresh Jayaraman if (!is_local) 8101da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 8111da177e4SLinus Torvalds else 8121da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 8131da177e4SLinus Torvalds return status; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8165eebde23SSuresh Jayaraman static int 8176b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) { 8186b96724eSRicardo Labiaga return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000)); 8196b96724eSRicardo Labiaga } 8206b96724eSRicardo Labiaga 8216b96724eSRicardo Labiaga static int 8225eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 8231da177e4SLinus Torvalds { 8241da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8251da177e4SLinus Torvalds int status; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * Flush all pending writes before doing anything 8291da177e4SLinus Torvalds * with locks.. 8301da177e4SLinus Torvalds */ 83129884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 83229884df0STrond Myklebust if (status != 0) 8331da177e4SLinus Torvalds goto out; 8341da177e4SLinus Torvalds 8355eebde23SSuresh Jayaraman /* 8365eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 8375eebde23SSuresh Jayaraman * "-olocal_lock=" 8385eebde23SSuresh Jayaraman */ 8395eebde23SSuresh Jayaraman if (!is_local) 8401da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 841c4d7c402STrond Myklebust else 8421da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 8431da177e4SLinus Torvalds if (status < 0) 8441da177e4SLinus Torvalds goto out; 8456b96724eSRicardo Labiaga 8461da177e4SLinus Torvalds /* 8476b96724eSRicardo Labiaga * Revalidate the cache if the server has time stamps granular 8486b96724eSRicardo Labiaga * enough to detect subsecond changes. Otherwise, clear the 8496b96724eSRicardo Labiaga * cache to prevent missing any changes. 8506b96724eSRicardo Labiaga * 8511da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 8521da177e4SLinus Torvalds */ 85329884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 854011e2a7fSBryan Schumaker if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) { 8556b96724eSRicardo Labiaga if (is_time_granular(&NFS_SERVER(inode)->time_delta)) 8566b96724eSRicardo Labiaga __nfs_revalidate_inode(NFS_SERVER(inode), inode); 8576b96724eSRicardo Labiaga else 8581da177e4SLinus Torvalds nfs_zap_caches(inode); 8596b96724eSRicardo Labiaga } 8601da177e4SLinus Torvalds out: 8611da177e4SLinus Torvalds return status; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* 8651da177e4SLinus Torvalds * Lock a (portion of) a file 8661da177e4SLinus Torvalds */ 867ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 8681da177e4SLinus Torvalds { 8691da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8702116271aSTrond Myklebust int ret = -ENOLCK; 8715eebde23SSuresh Jayaraman int is_local = 0; 8721da177e4SLinus Torvalds 8736da24bc9SChuck Lever dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n", 8746da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 8756da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 8761da177e4SLinus Torvalds fl->fl_type, fl->fl_flags, 8771da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 8786da24bc9SChuck Lever 87991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* No mandatory locks over NFS */ 882dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 8832116271aSTrond Myklebust goto out_err; 8842116271aSTrond Myklebust 8855eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 8865eebde23SSuresh Jayaraman is_local = 1; 8875eebde23SSuresh Jayaraman 8882116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 8892116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 8902116271aSTrond Myklebust if (ret < 0) 8912116271aSTrond Myklebust goto out_err; 8922116271aSTrond Myklebust } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds if (IS_GETLK(cmd)) 8955eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8962116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 8975eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8982116271aSTrond Myklebust else 8995eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 9002116271aSTrond Myklebust out_err: 9012116271aSTrond Myklebust return ret; 9021da177e4SLinus Torvalds } 90389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock); 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds /* 9061da177e4SLinus Torvalds * Lock a (portion of) a file 9071da177e4SLinus Torvalds */ 908ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 9091da177e4SLinus Torvalds { 9105eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 9115eebde23SSuresh Jayaraman int is_local = 0; 9125eebde23SSuresh Jayaraman 9136da24bc9SChuck Lever dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n", 9146da24bc9SChuck Lever filp->f_path.dentry->d_parent->d_name.name, 9156da24bc9SChuck Lever filp->f_path.dentry->d_name.name, 9161da177e4SLinus Torvalds fl->fl_type, fl->fl_flags); 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 9191da177e4SLinus Torvalds return -ENOLCK; 9201da177e4SLinus Torvalds 921ad0fcd4eSJeff Layton /* 922ad0fcd4eSJeff Layton * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of 923ad0fcd4eSJeff Layton * any standard. In principle we might be able to support LOCK_MAND 924ad0fcd4eSJeff Layton * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the 925ad0fcd4eSJeff Layton * NFS code is not set up for it. 926ad0fcd4eSJeff Layton */ 927ad0fcd4eSJeff Layton if (fl->fl_type & LOCK_MAND) 928ad0fcd4eSJeff Layton return -EINVAL; 929ad0fcd4eSJeff Layton 9305eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 9315eebde23SSuresh Jayaraman is_local = 1; 9325eebde23SSuresh Jayaraman 9331da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 9341da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 9351da177e4SLinus Torvalds fl->fl_start = 0; 9361da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 9395eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 9405eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 9411da177e4SLinus Torvalds } 94289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock); 943370f6599SJ. Bruce Fields 9446da24bc9SChuck Lever /* 9456da24bc9SChuck Lever * There is no protocol support for leases, so we have no way to implement 9466da24bc9SChuck Lever * them correctly in the face of opens by other clients. 9476da24bc9SChuck Lever */ 948ce4ef7c0SBryan Schumaker int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 949370f6599SJ. Bruce Fields { 9506da24bc9SChuck Lever dprintk("NFS: setlease(%s/%s, arg=%ld)\n", 9516da24bc9SChuck Lever file->f_path.dentry->d_parent->d_name.name, 9526da24bc9SChuck Lever file->f_path.dentry->d_name.name, arg); 953370f6599SJ. Bruce Fields return -EINVAL; 954370f6599SJ. Bruce Fields } 95589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_setlease); 9561788ea6eSJeff Layton 9570486958fSJeff Layton const struct file_operations nfs_file_operations = { 9580486958fSJeff Layton .llseek = nfs_file_llseek, 9590486958fSJeff Layton .read = do_sync_read, 9600486958fSJeff Layton .write = do_sync_write, 9610486958fSJeff Layton .aio_read = nfs_file_read, 9620486958fSJeff Layton .aio_write = nfs_file_write, 9630486958fSJeff Layton .mmap = nfs_file_mmap, 9640486958fSJeff Layton .open = nfs_file_open, 9650486958fSJeff Layton .flush = nfs_file_flush, 9660486958fSJeff Layton .release = nfs_file_release, 9670486958fSJeff Layton .fsync = nfs_file_fsync, 9680486958fSJeff Layton .lock = nfs_lock, 9690486958fSJeff Layton .flock = nfs_flock, 9700486958fSJeff Layton .splice_read = nfs_file_splice_read, 9710486958fSJeff Layton .splice_write = nfs_file_splice_write, 9720486958fSJeff Layton .check_flags = nfs_check_flags, 9730486958fSJeff Layton .setlease = nfs_setlease, 9740486958fSJeff Layton }; 975ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations); 976