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 686de1472fSAl Viro dprintk("NFS: open file(%pD2)\n", filp); 69cc0dd2d1SChuck Lever 70c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 711da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 721da177e4SLinus Torvalds if (res) 731da177e4SLinus Torvalds return res; 741da177e4SLinus Torvalds 7546cb650cSTrond Myklebust res = nfs_open(inode, filp); 761da177e4SLinus Torvalds return res; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 79ce4ef7c0SBryan Schumaker int 801da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 811da177e4SLinus Torvalds { 826de1472fSAl Viro dprintk("NFS: release(%pD2)\n", filp); 836da24bc9SChuck Lever 8491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 8546cb650cSTrond Myklebust return nfs_release(inode, filp); 861da177e4SLinus Torvalds } 8789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release); 881da177e4SLinus Torvalds 89980802e3STrond Myklebust /** 90980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 91980802e3STrond Myklebust * @inode - pointer to inode struct 92980802e3STrond Myklebust * @file - pointer to struct file 93980802e3STrond Myklebust * 94980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 95980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 96980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 97980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 98980802e3STrond Myklebust * shouldn't trust the cache). 99980802e3STrond Myklebust */ 100980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 101980802e3STrond Myklebust { 102980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 103980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 104980802e3STrond Myklebust 105d7cf8dd0STrond Myklebust if (nfs_have_delegated_attributes(inode)) 106d7cf8dd0STrond Myklebust goto out_noreval; 107d7cf8dd0STrond Myklebust 108980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 109980802e3STrond Myklebust goto force_reval; 110d7cf8dd0STrond Myklebust if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 111d7cf8dd0STrond Myklebust goto force_reval; 112d7cf8dd0STrond Myklebust if (nfs_attribute_timeout(inode)) 113d7cf8dd0STrond Myklebust goto force_reval; 114d7cf8dd0STrond Myklebust out_noreval: 115fe51beecSTrond Myklebust return 0; 116980802e3STrond Myklebust force_reval: 117980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 118980802e3STrond Myklebust } 119980802e3STrond Myklebust 120965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence) 121980802e3STrond Myklebust { 1226de1472fSAl Viro dprintk("NFS: llseek file(%pD2, %lld, %d)\n", 1236de1472fSAl Viro filp, offset, whence); 124b84e06c5SChuck Lever 12506222e49SJosef Bacik /* 126965c8e59SAndrew Morton * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 12706222e49SJosef Bacik * the cached file length 12806222e49SJosef Bacik */ 129965c8e59SAndrew Morton if (whence != SEEK_SET && whence != SEEK_CUR) { 130980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 131d5e66348STrond Myklebust 132980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 133980802e3STrond Myklebust if (retval < 0) 134980802e3STrond Myklebust return (loff_t)retval; 13579835a71SAndi Kleen } 136d5e66348STrond Myklebust 137965c8e59SAndrew Morton return generic_file_llseek(filp, offset, whence); 138980802e3STrond Myklebust } 13989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek); 140980802e3STrond Myklebust 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 1431da177e4SLinus Torvalds */ 144ce4ef7c0SBryan Schumaker int 14575e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 1461da177e4SLinus Torvalds { 1476de1472fSAl Viro struct inode *inode = file_inode(file); 1481da177e4SLinus Torvalds 1496de1472fSAl Viro dprintk("NFS: flush(%pD2)\n", file); 1501da177e4SLinus Torvalds 151c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 1521da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 1531da177e4SLinus Torvalds return 0; 1547b159fc1STrond Myklebust 15514546c33STrond Myklebust /* 15614546c33STrond Myklebust * If we're holding a write delegation, then just start the i/o 15714546c33STrond Myklebust * but don't wait for completion (or send a commit). 15814546c33STrond Myklebust */ 159011e2a7fSBryan Schumaker if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 16014546c33STrond Myklebust return filemap_fdatawrite(file->f_mapping); 16114546c33STrond Myklebust 1627fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 163af7fa165STrond Myklebust return vfs_fsync(file, 0); 1641da177e4SLinus Torvalds } 16589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_flush); 1661da177e4SLinus Torvalds 167ce4ef7c0SBryan Schumaker ssize_t 168027445c3SBadari Pulavarty nfs_file_read(struct kiocb *iocb, const struct iovec *iov, 169027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 1701da177e4SLinus Torvalds { 1716de1472fSAl Viro struct inode *inode = file_inode(iocb->ki_filp); 172*619d30b4SAl Viro size_t count = iov_length(iov, nr_segs); 1731da177e4SLinus Torvalds ssize_t result; 174*619d30b4SAl Viro struct iov_iter to; 175*619d30b4SAl Viro 176*619d30b4SAl Viro iov_iter_init(&to, iov, nr_segs, count, 0); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds if (iocb->ki_filp->f_flags & O_DIRECT) 179*619d30b4SAl Viro return nfs_file_direct_read(iocb, &to, pos, true); 1801da177e4SLinus Torvalds 181*619d30b4SAl Viro dprintk("NFS: read(%pD2, %zu@%lu)\n", 1826de1472fSAl Viro iocb->ki_filp, 183*619d30b4SAl Viro count, (unsigned long) pos); 1841da177e4SLinus Torvalds 18544b11874STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 1864184dcf2SChuck Lever if (!result) { 187027445c3SBadari Pulavarty result = generic_file_aio_read(iocb, iov, nr_segs, pos); 1884184dcf2SChuck Lever if (result > 0) 1894184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 1904184dcf2SChuck Lever } 1911da177e4SLinus Torvalds return result; 1921da177e4SLinus Torvalds } 19389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read); 1941da177e4SLinus Torvalds 195ce4ef7c0SBryan Schumaker ssize_t 196f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 197f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 198f0930fffSJens Axboe unsigned int flags) 1991da177e4SLinus Torvalds { 2006de1472fSAl Viro struct inode *inode = file_inode(filp); 2011da177e4SLinus Torvalds ssize_t res; 2021da177e4SLinus Torvalds 2036de1472fSAl Viro dprintk("NFS: splice_read(%pD2, %lu@%Lu)\n", 2046de1472fSAl Viro filp, (unsigned long) count, (unsigned long long) *ppos); 2051da177e4SLinus Torvalds 20644b11874STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 207aa2f1ef1SChuck Lever if (!res) { 208f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 209aa2f1ef1SChuck Lever if (res > 0) 210aa2f1ef1SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res); 211aa2f1ef1SChuck Lever } 2121da177e4SLinus Torvalds return res; 2131da177e4SLinus Torvalds } 21489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_read); 2151da177e4SLinus Torvalds 216ce4ef7c0SBryan Schumaker int 2171da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2181da177e4SLinus Torvalds { 2196de1472fSAl Viro struct inode *inode = file_inode(file); 2201da177e4SLinus Torvalds int status; 2211da177e4SLinus Torvalds 2226de1472fSAl Viro dprintk("NFS: mmap(%pD2)\n", file); 2231da177e4SLinus Torvalds 224e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 225e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 226e1ebfd33STrond Myklebust */ 227e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 22894387fb1STrond Myklebust if (!status) { 22994387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 230e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 23194387fb1STrond Myklebust } 2321da177e4SLinus Torvalds return status; 2331da177e4SLinus Torvalds } 23489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap); 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds /* 2371da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2381da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2391da177e4SLinus Torvalds * whether any write errors occurred for this process. 240af7fa165STrond Myklebust * 241af7fa165STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 242af7fa165STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 243af7fa165STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 244af7fa165STrond Myklebust * This is because the former is used to notify the _next_ call to 24525985edcSLucas De Marchi * nfs_file_write() that a write error occurred, and hence cause it to 246af7fa165STrond Myklebust * fall back to doing a synchronous write. 2471da177e4SLinus Torvalds */ 248ce4ef7c0SBryan Schumaker int 249a5c58892SBryan Schumaker nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync) 2501da177e4SLinus Torvalds { 251cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 2526de1472fSAl Viro struct inode *inode = file_inode(file); 25305990d1bSTrond Myklebust int have_error, do_resend, status; 254af7fa165STrond Myklebust int ret = 0; 255af7fa165STrond Myklebust 2566de1472fSAl Viro dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync); 2571da177e4SLinus Torvalds 25891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 25905990d1bSTrond Myklebust do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); 260af7fa165STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 261af7fa165STrond Myklebust status = nfs_commit_inode(inode, FLUSH_SYNC); 262af7fa165STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 26305990d1bSTrond Myklebust if (have_error) { 264af7fa165STrond Myklebust ret = xchg(&ctx->error, 0); 26505990d1bSTrond Myklebust if (ret) 26605990d1bSTrond Myklebust goto out; 26705990d1bSTrond Myklebust } 26805990d1bSTrond Myklebust if (status < 0) { 269af7fa165STrond Myklebust ret = status; 27005990d1bSTrond Myklebust goto out; 27105990d1bSTrond Myklebust } 27205990d1bSTrond Myklebust do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); 27305990d1bSTrond Myklebust if (do_resend) 27405990d1bSTrond Myklebust ret = -EAGAIN; 27505990d1bSTrond Myklebust out: 276a5c58892SBryan Schumaker return ret; 277a5c58892SBryan Schumaker } 27889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_fsync_commit); 279a5c58892SBryan Schumaker 280a5c58892SBryan Schumaker static int 281a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 282a5c58892SBryan Schumaker { 283a5c58892SBryan Schumaker int ret; 284496ad9aaSAl Viro struct inode *inode = file_inode(file); 285a5c58892SBryan Schumaker 286f4ce1299STrond Myklebust trace_nfs_fsync_enter(inode); 287f4ce1299STrond Myklebust 28805990d1bSTrond Myklebust do { 289a5c58892SBryan Schumaker ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 2907b281ee0STrond Myklebust if (ret != 0) 29105990d1bSTrond Myklebust break; 292a5c58892SBryan Schumaker mutex_lock(&inode->i_mutex); 293a5c58892SBryan Schumaker ret = nfs_file_fsync_commit(file, start, end, datasync); 29402c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 295dcfc4f25STrond Myklebust /* 296dcfc4f25STrond Myklebust * If nfs_file_fsync_commit detected a server reboot, then 297dcfc4f25STrond Myklebust * resend all dirty pages that might have been covered by 298dcfc4f25STrond Myklebust * the NFS_CONTEXT_RESEND_WRITES flag 299dcfc4f25STrond Myklebust */ 300dcfc4f25STrond Myklebust start = 0; 301dcfc4f25STrond Myklebust end = LLONG_MAX; 30205990d1bSTrond Myklebust } while (ret == -EAGAIN); 30305990d1bSTrond Myklebust 304f4ce1299STrond Myklebust trace_nfs_fsync_exit(inode, ret); 305af7fa165STrond Myklebust return ret; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* 30938c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 31038c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 31138c73044SPeter Staubach * page cache. 31238c73044SPeter Staubach * 31338c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 31438c73044SPeter Staubach * being completely filled by the writer. In this situation, the 31538c73044SPeter Staubach * page must be completely written to stable storage on the server 31638c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 31738c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 31838c73044SPeter Staubach * done. 31938c73044SPeter Staubach * 32038c73044SPeter Staubach * It may be more efficient to read the page first if the file is 32138c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 32238c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 32338c73044SPeter Staubach * indicating that it was previously allocated and then modified, 32438c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 32538c73044SPeter Staubach * and that the new data won't completely replace the old data in 32638c73044SPeter Staubach * that range of the file. 32738c73044SPeter Staubach */ 32838c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page, 32938c73044SPeter Staubach loff_t pos, unsigned len) 33038c73044SPeter Staubach { 33138c73044SPeter Staubach unsigned int pglen = nfs_page_length(page); 33238c73044SPeter Staubach unsigned int offset = pos & (PAGE_CACHE_SIZE - 1); 33338c73044SPeter Staubach unsigned int end = offset + len; 33438c73044SPeter Staubach 33538c73044SPeter Staubach if ((file->f_mode & FMODE_READ) && /* open for read? */ 33638c73044SPeter Staubach !PageUptodate(page) && /* Uptodate? */ 33738c73044SPeter Staubach !PagePrivate(page) && /* i/o request already? */ 33838c73044SPeter Staubach pglen && /* valid bytes of file? */ 33938c73044SPeter Staubach (end < pglen || offset)) /* replace all valid bytes? */ 34038c73044SPeter Staubach return 1; 34138c73044SPeter Staubach return 0; 34238c73044SPeter Staubach } 34338c73044SPeter Staubach 34438c73044SPeter Staubach /* 3454899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3464899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3474899f9c8SNick Piggin * data from user space. 3481da177e4SLinus Torvalds * 3491da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3501da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3511da177e4SLinus Torvalds */ 3524899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3534899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3544899f9c8SNick Piggin struct page **pagep, void **fsdata) 3551da177e4SLinus Torvalds { 3564899f9c8SNick Piggin int ret; 35738c73044SPeter Staubach pgoff_t index = pos >> PAGE_CACHE_SHIFT; 3584899f9c8SNick Piggin struct page *page; 35938c73044SPeter Staubach int once_thru = 0; 3604899f9c8SNick Piggin 3611e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n", 3626de1472fSAl Viro file, mapping->host->i_ino, len, (long long) pos); 363b7eaefaaSChuck Lever 36438c73044SPeter Staubach start: 36572cb77f4STrond Myklebust /* 36672cb77f4STrond Myklebust * Prevent starvation issues if someone is doing a consistency 36772cb77f4STrond Myklebust * sync-to-disk 36872cb77f4STrond Myklebust */ 36972cb77f4STrond Myklebust ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING, 37072cb77f4STrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 37172cb77f4STrond Myklebust if (ret) 37272cb77f4STrond Myklebust return ret; 37372cb77f4STrond Myklebust 37454566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3754899f9c8SNick Piggin if (!page) 3764899f9c8SNick Piggin return -ENOMEM; 3774899f9c8SNick Piggin *pagep = page; 3784899f9c8SNick Piggin 3794899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3804899f9c8SNick Piggin if (ret) { 3814899f9c8SNick Piggin unlock_page(page); 3824899f9c8SNick Piggin page_cache_release(page); 38338c73044SPeter Staubach } else if (!once_thru && 38438c73044SPeter Staubach nfs_want_read_modify_write(file, page, pos, len)) { 38538c73044SPeter Staubach once_thru = 1; 38638c73044SPeter Staubach ret = nfs_readpage(file, page); 38738c73044SPeter Staubach page_cache_release(page); 38838c73044SPeter Staubach if (!ret) 38938c73044SPeter Staubach goto start; 3904899f9c8SNick Piggin } 3914899f9c8SNick Piggin return ret; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3944899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3954899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3964899f9c8SNick Piggin struct page *page, void *fsdata) 3971da177e4SLinus Torvalds { 3984899f9c8SNick Piggin unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 399dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(file); 4004899f9c8SNick Piggin int status; 4011da177e4SLinus Torvalds 4021e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n", 4036de1472fSAl Viro file, mapping->host->i_ino, len, (long long) pos); 404b7eaefaaSChuck Lever 405efc91ed0STrond Myklebust /* 406efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 407efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 408efc91ed0STrond Myklebust */ 409efc91ed0STrond Myklebust if (!PageUptodate(page)) { 410efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 411efc91ed0STrond Myklebust unsigned end = offset + len; 412efc91ed0STrond Myklebust 413efc91ed0STrond Myklebust if (pglen == 0) { 414efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 415efc91ed0STrond Myklebust end, PAGE_CACHE_SIZE); 416efc91ed0STrond Myklebust SetPageUptodate(page); 417efc91ed0STrond Myklebust } else if (end >= pglen) { 418efc91ed0STrond Myklebust zero_user_segment(page, end, PAGE_CACHE_SIZE); 419efc91ed0STrond Myklebust if (offset == 0) 420efc91ed0STrond Myklebust SetPageUptodate(page); 421efc91ed0STrond Myklebust } else 422efc91ed0STrond Myklebust zero_user_segment(page, pglen, PAGE_CACHE_SIZE); 423efc91ed0STrond Myklebust } 424efc91ed0STrond Myklebust 4254899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4264899f9c8SNick Piggin 4274899f9c8SNick Piggin unlock_page(page); 4284899f9c8SNick Piggin page_cache_release(page); 4294899f9c8SNick Piggin 4303d509e54SChuck Lever if (status < 0) 4313d509e54SChuck Lever return status; 4322701d086SAndy Adamson NFS_I(mapping->host)->write_io += copied; 433dc24826bSAndy Adamson 434dc24826bSAndy Adamson if (nfs_ctx_key_to_expire(ctx)) { 435dc24826bSAndy Adamson status = nfs_wb_all(mapping->host); 436dc24826bSAndy Adamson if (status < 0) 437dc24826bSAndy Adamson return status; 438dc24826bSAndy Adamson } 439dc24826bSAndy Adamson 4403d509e54SChuck Lever return copied; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4436b9b3514SDavid Howells /* 4446b9b3514SDavid Howells * Partially or wholly invalidate a page 4456b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4466b9b3514SDavid Howells * page invalidation 447545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4486b9b3514SDavid Howells * - Caller holds page lock 4496b9b3514SDavid Howells */ 450d47992f8SLukas Czerner static void nfs_invalidate_page(struct page *page, unsigned int offset, 451d47992f8SLukas Czerner unsigned int length) 452cd52ed35STrond Myklebust { 453d47992f8SLukas Czerner dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n", 454d47992f8SLukas Czerner page, offset, length); 455b7eaefaaSChuck Lever 456d47992f8SLukas Czerner if (offset != 0 || length < PAGE_CACHE_SIZE) 4571c75950bSTrond Myklebust return; 458d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 459d56b4ddfSMel Gorman nfs_wb_page_cancel(page_file_mapping(page)->host, page); 460545db45fSDavid Howells 461545db45fSDavid Howells nfs_fscache_invalidate_page(page, page->mapping->host); 462cd52ed35STrond Myklebust } 463cd52ed35STrond Myklebust 4646b9b3514SDavid Howells /* 4656b9b3514SDavid Howells * Attempt to release the private state associated with a page 466545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4676b9b3514SDavid Howells * - Caller holds page lock 4686b9b3514SDavid Howells * - Return true (may release page) or false (may not) 4696b9b3514SDavid Howells */ 470cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 471cd52ed35STrond Myklebust { 472b608b283STrond Myklebust struct address_space *mapping = page->mapping; 473b608b283STrond Myklebust 474b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 475b7eaefaaSChuck Lever 4765cf02d09SJeff Layton /* Only do I/O if gfp is a superset of GFP_KERNEL, and we're not 4775cf02d09SJeff Layton * doing this memory reclaim for a fs-related allocation. 4785cf02d09SJeff Layton */ 4795cf02d09SJeff Layton if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL && 4805cf02d09SJeff Layton !(current->flags & PF_FSTRANS)) { 481b608b283STrond Myklebust int how = FLUSH_SYNC; 482b608b283STrond Myklebust 483b608b283STrond Myklebust /* Don't let kswapd deadlock waiting for OOM RPC calls */ 484b608b283STrond Myklebust if (current_is_kswapd()) 485b608b283STrond Myklebust how = 0; 486b608b283STrond Myklebust nfs_commit_inode(mapping->host, how); 487b608b283STrond Myklebust } 488e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 489545db45fSDavid Howells if (PagePrivate(page)) 490ddeff520SNikita Danilov return 0; 491545db45fSDavid Howells return nfs_fscache_release_page(page, gfp); 492e3db7691STrond Myklebust } 493e3db7691STrond Myklebust 494f919b196SMel Gorman static void nfs_check_dirty_writeback(struct page *page, 495f919b196SMel Gorman bool *dirty, bool *writeback) 496f919b196SMel Gorman { 497f919b196SMel Gorman struct nfs_inode *nfsi; 498f919b196SMel Gorman struct address_space *mapping = page_file_mapping(page); 499f919b196SMel Gorman 500f919b196SMel Gorman if (!mapping || PageSwapCache(page)) 501f919b196SMel Gorman return; 502f919b196SMel Gorman 503f919b196SMel Gorman /* 504f919b196SMel Gorman * Check if an unstable page is currently being committed and 505f919b196SMel Gorman * if so, have the VM treat it as if the page is under writeback 506f919b196SMel Gorman * so it will not block due to pages that will shortly be freeable. 507f919b196SMel Gorman */ 508f919b196SMel Gorman nfsi = NFS_I(mapping->host); 509f919b196SMel Gorman if (test_bit(NFS_INO_COMMIT, &nfsi->flags)) { 510f919b196SMel Gorman *writeback = true; 511f919b196SMel Gorman return; 512f919b196SMel Gorman } 513f919b196SMel Gorman 514f919b196SMel Gorman /* 515f919b196SMel Gorman * If PagePrivate() is set, then the page is not freeable and as the 516f919b196SMel Gorman * inode is not being committed, it's not going to be cleaned in the 517f919b196SMel Gorman * near future so treat it as dirty 518f919b196SMel Gorman */ 519f919b196SMel Gorman if (PagePrivate(page)) 520f919b196SMel Gorman *dirty = true; 521f919b196SMel Gorman } 522f919b196SMel Gorman 5236b9b3514SDavid Howells /* 5246b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 5256b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 5266b9b3514SDavid Howells * destroyed 527545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 5286b9b3514SDavid Howells * - Caller holds page lock 5296b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 5306b9b3514SDavid Howells */ 531e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 532e3db7691STrond Myklebust { 533d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 534545db45fSDavid Howells struct nfs_inode *nfsi = NFS_I(inode); 535b7eaefaaSChuck Lever 536b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 537b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 538b7eaefaaSChuck Lever 539545db45fSDavid Howells nfs_fscache_wait_on_page_write(nfsi, page); 540b7eaefaaSChuck Lever return nfs_wb_page(inode, page); 541cd52ed35STrond Myklebust } 542cd52ed35STrond Myklebust 543a564b8f0SMel Gorman #ifdef CONFIG_NFS_SWAP 544a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file, 545a564b8f0SMel Gorman sector_t *span) 546a564b8f0SMel Gorman { 547a564b8f0SMel Gorman *span = sis->pages; 548a564b8f0SMel Gorman return xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 1); 549a564b8f0SMel Gorman } 550a564b8f0SMel Gorman 551a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file) 552a564b8f0SMel Gorman { 553a564b8f0SMel Gorman xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 0); 554a564b8f0SMel Gorman } 555a564b8f0SMel Gorman #endif 556a564b8f0SMel Gorman 557f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 5581da177e4SLinus Torvalds .readpage = nfs_readpage, 5591da177e4SLinus Torvalds .readpages = nfs_readpages, 5609cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 5611da177e4SLinus Torvalds .writepage = nfs_writepage, 5621da177e4SLinus Torvalds .writepages = nfs_writepages, 5634899f9c8SNick Piggin .write_begin = nfs_write_begin, 5644899f9c8SNick Piggin .write_end = nfs_write_end, 565cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 566cd52ed35STrond Myklebust .releasepage = nfs_release_page, 5671da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 568074cc1deSTrond Myklebust .migratepage = nfs_migrate_page, 569e3db7691STrond Myklebust .launder_page = nfs_launder_page, 570f919b196SMel Gorman .is_dirty_writeback = nfs_check_dirty_writeback, 571f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 572a564b8f0SMel Gorman #ifdef CONFIG_NFS_SWAP 573a564b8f0SMel Gorman .swap_activate = nfs_swap_activate, 574a564b8f0SMel Gorman .swap_deactivate = nfs_swap_deactivate, 575a564b8f0SMel Gorman #endif 5761da177e4SLinus Torvalds }; 5771da177e4SLinus Torvalds 5786b9b3514SDavid Howells /* 5796b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5806b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5816b9b3514SDavid Howells * shared-writable mapping 5826b9b3514SDavid Howells */ 583c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 58494387fb1STrond Myklebust { 585c2ec175cSNick Piggin struct page *page = vmf->page; 58694387fb1STrond Myklebust struct file *filp = vma->vm_file; 5876de1472fSAl Viro struct inode *inode = file_inode(filp); 58894387fb1STrond Myklebust unsigned pagelen; 589bc4866b6STrond Myklebust int ret = VM_FAULT_NOPAGE; 5904899f9c8SNick Piggin struct address_space *mapping; 59194387fb1STrond Myklebust 5921e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n", 5936de1472fSAl Viro filp, filp->f_mapping->host->i_ino, 594b7eaefaaSChuck Lever (long long)page_offset(page)); 595b7eaefaaSChuck Lever 596545db45fSDavid Howells /* make sure the cache has finished storing the page */ 5976de1472fSAl Viro nfs_fscache_wait_on_page_write(NFS_I(inode), page); 598545db45fSDavid Howells 59994387fb1STrond Myklebust lock_page(page); 600d56b4ddfSMel Gorman mapping = page_file_mapping(page); 6016de1472fSAl Viro if (mapping != inode->i_mapping) 6028b1f9ee5STrond Myklebust goto out_unlock; 6038b1f9ee5STrond Myklebust 6042aeb98f4STrond Myklebust wait_on_page_writeback(page); 6052aeb98f4STrond Myklebust 6064899f9c8SNick Piggin pagelen = nfs_page_length(page); 6078b1f9ee5STrond Myklebust if (pagelen == 0) 6088b1f9ee5STrond Myklebust goto out_unlock; 6098b1f9ee5STrond Myklebust 610bc4866b6STrond Myklebust ret = VM_FAULT_LOCKED; 611bc4866b6STrond Myklebust if (nfs_flush_incompatible(filp, page) == 0 && 612bc4866b6STrond Myklebust nfs_updatepage(filp, page, 0, pagelen) == 0) 613bc4866b6STrond Myklebust goto out; 6148b1f9ee5STrond Myklebust 615bc4866b6STrond Myklebust ret = VM_FAULT_SIGBUS; 6168b1f9ee5STrond Myklebust out_unlock: 6174899f9c8SNick Piggin unlock_page(page); 618bc4866b6STrond Myklebust out: 619bc4866b6STrond Myklebust return ret; 62094387fb1STrond Myklebust } 62194387fb1STrond Myklebust 622f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 62394387fb1STrond Myklebust .fault = filemap_fault, 624f1820361SKirill A. Shutemov .map_pages = filemap_map_pages, 62594387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 6260b173bc4SKonstantin Khlebnikov .remap_pages = generic_file_remap_pages, 62794387fb1STrond Myklebust }; 62894387fb1STrond Myklebust 6297b159fc1STrond Myklebust static int nfs_need_sync_write(struct file *filp, struct inode *inode) 6307b159fc1STrond Myklebust { 6317b159fc1STrond Myklebust struct nfs_open_context *ctx; 6327b159fc1STrond Myklebust 6336b2f3d1fSChristoph Hellwig if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC)) 6347b159fc1STrond Myklebust return 1; 635cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 636dc24826bSAndy Adamson if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) || 637dc24826bSAndy Adamson nfs_ctx_key_to_expire(ctx)) 6387b159fc1STrond Myklebust return 1; 6397b159fc1STrond Myklebust return 0; 6407b159fc1STrond Myklebust } 6417b159fc1STrond Myklebust 642ce4ef7c0SBryan Schumaker ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov, 643027445c3SBadari Pulavarty unsigned long nr_segs, loff_t pos) 6441da177e4SLinus Torvalds { 6456de1472fSAl Viro struct file *file = iocb->ki_filp; 6466de1472fSAl Viro struct inode *inode = file_inode(file); 6477e381172SChuck Lever unsigned long written = 0; 6481da177e4SLinus Torvalds ssize_t result; 649027445c3SBadari Pulavarty size_t count = iov_length(iov, nr_segs); 650*619d30b4SAl Viro struct iov_iter from; 651*619d30b4SAl Viro iov_iter_init(&from, iov, nr_segs, count, 0); 6521da177e4SLinus Torvalds 6536de1472fSAl Viro result = nfs_key_timeout_notify(file, inode); 654dc24826bSAndy Adamson if (result) 655dc24826bSAndy Adamson return result; 656dc24826bSAndy Adamson 6576de1472fSAl Viro if (file->f_flags & O_DIRECT) 658*619d30b4SAl Viro return nfs_file_direct_write(iocb, &from, pos, true); 6591da177e4SLinus Torvalds 660*619d30b4SAl Viro dprintk("NFS: write(%pD2, %zu@%Ld)\n", 661*619d30b4SAl Viro file, count, (long long) pos); 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds result = -EBUSY; 6641da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 6651da177e4SLinus Torvalds goto out_swapfile; 6667d52e862STrond Myklebust /* 6677d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 6687d52e862STrond Myklebust */ 6696de1472fSAl Viro if (file->f_flags & O_APPEND) { 6706de1472fSAl Viro result = nfs_revalidate_file_size(inode, file); 6711da177e4SLinus Torvalds if (result) 6721da177e4SLinus Torvalds goto out; 673fe51beecSTrond Myklebust } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds result = count; 6761da177e4SLinus Torvalds if (!count) 6771da177e4SLinus Torvalds goto out; 6781da177e4SLinus Torvalds 679027445c3SBadari Pulavarty result = generic_file_aio_write(iocb, iov, nr_segs, pos); 6807e381172SChuck Lever if (result > 0) 6817e381172SChuck Lever written = result; 6827e381172SChuck Lever 6836b2f3d1fSChristoph Hellwig /* Return error values for O_DSYNC and IS_SYNC() */ 6846de1472fSAl Viro if (result >= 0 && nfs_need_sync_write(file, inode)) { 6856de1472fSAl Viro int err = vfs_fsync(file, 0); 686200baa21STrond Myklebust if (err < 0) 687200baa21STrond Myklebust result = err; 688200baa21STrond Myklebust } 6897e381172SChuck Lever if (result > 0) 6907e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 6911da177e4SLinus Torvalds out: 6921da177e4SLinus Torvalds return result; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds out_swapfile: 6951da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 6961da177e4SLinus Torvalds goto out; 6971da177e4SLinus Torvalds } 69889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write); 6991da177e4SLinus Torvalds 700ce4ef7c0SBryan Schumaker ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe, 701bf40d343SSuresh Jayaraman struct file *filp, loff_t *ppos, 702bf40d343SSuresh Jayaraman size_t count, unsigned int flags) 703bf40d343SSuresh Jayaraman { 7046de1472fSAl Viro struct inode *inode = file_inode(filp); 7057e381172SChuck Lever unsigned long written = 0; 706bf40d343SSuresh Jayaraman ssize_t ret; 707bf40d343SSuresh Jayaraman 7086de1472fSAl Viro dprintk("NFS splice_write(%pD2, %lu@%llu)\n", 7096de1472fSAl Viro filp, (unsigned long) count, (unsigned long long) *ppos); 710bf40d343SSuresh Jayaraman 711bf40d343SSuresh Jayaraman /* 712bf40d343SSuresh Jayaraman * The combination of splice and an O_APPEND destination is disallowed. 713bf40d343SSuresh Jayaraman */ 714bf40d343SSuresh Jayaraman 715bf40d343SSuresh Jayaraman ret = generic_file_splice_write(pipe, filp, ppos, count, flags); 7167e381172SChuck Lever if (ret > 0) 7177e381172SChuck Lever written = ret; 7187e381172SChuck Lever 719bf40d343SSuresh Jayaraman if (ret >= 0 && nfs_need_sync_write(filp, inode)) { 720af7fa165STrond Myklebust int err = vfs_fsync(filp, 0); 721bf40d343SSuresh Jayaraman if (err < 0) 722bf40d343SSuresh Jayaraman ret = err; 723bf40d343SSuresh Jayaraman } 7247e381172SChuck Lever if (ret > 0) 7257e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 726bf40d343SSuresh Jayaraman return ret; 727bf40d343SSuresh Jayaraman } 72889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_write); 729bf40d343SSuresh Jayaraman 7305eebde23SSuresh Jayaraman static int 7315eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7341da177e4SLinus Torvalds int status = 0; 73521ac19d4SSergey Vlasov unsigned int saved_type = fl->fl_type; 7361da177e4SLinus Torvalds 737039c4d7aSTrond Myklebust /* Try local locking first */ 7386d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 7396d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 7406d34ac19SJ. Bruce Fields /* found a conflict */ 741039c4d7aSTrond Myklebust goto out; 7421da177e4SLinus Torvalds } 74321ac19d4SSergey Vlasov fl->fl_type = saved_type; 744039c4d7aSTrond Myklebust 745011e2a7fSBryan Schumaker if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 746039c4d7aSTrond Myklebust goto out_noconflict; 747039c4d7aSTrond Myklebust 7485eebde23SSuresh Jayaraman if (is_local) 749039c4d7aSTrond Myklebust goto out_noconflict; 750039c4d7aSTrond Myklebust 751039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 752039c4d7aSTrond Myklebust out: 7531da177e4SLinus Torvalds return status; 754039c4d7aSTrond Myklebust out_noconflict: 755039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 756039c4d7aSTrond Myklebust goto out; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds int res = 0; 7621da177e4SLinus Torvalds switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { 7631da177e4SLinus Torvalds case FL_POSIX: 7641da177e4SLinus Torvalds res = posix_lock_file_wait(file, fl); 7651da177e4SLinus Torvalds break; 7661da177e4SLinus Torvalds case FL_FLOCK: 7671da177e4SLinus Torvalds res = flock_lock_file_wait(file, fl); 7681da177e4SLinus Torvalds break; 7691da177e4SLinus Torvalds default: 7701da177e4SLinus Torvalds BUG(); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds return res; 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7755eebde23SSuresh Jayaraman static int 7765eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7771da177e4SLinus Torvalds { 7781da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7797a8203d8STrond Myklebust struct nfs_lock_context *l_ctx; 7801da177e4SLinus Torvalds int status; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * Flush all pending writes before doing anything 7841da177e4SLinus Torvalds * with locks.. 7851da177e4SLinus Torvalds */ 78629884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 7871da177e4SLinus Torvalds 7887a8203d8STrond Myklebust l_ctx = nfs_get_lock_context(nfs_file_open_context(filp)); 7897a8203d8STrond Myklebust if (!IS_ERR(l_ctx)) { 7907a8203d8STrond Myklebust status = nfs_iocounter_wait(&l_ctx->io_count); 7917a8203d8STrond Myklebust nfs_put_lock_context(l_ctx); 7927a8203d8STrond Myklebust if (status < 0) 7937a8203d8STrond Myklebust return status; 7947a8203d8STrond Myklebust } 7957a8203d8STrond Myklebust 7961da177e4SLinus Torvalds /* NOTE: special case 7971da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 7981da177e4SLinus Torvalds * still need to complete the unlock. 7991da177e4SLinus Torvalds */ 8005eebde23SSuresh Jayaraman /* 8015eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 8025eebde23SSuresh Jayaraman * "-olocal_lock=" 8035eebde23SSuresh Jayaraman */ 8045eebde23SSuresh Jayaraman if (!is_local) 8051da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 8061da177e4SLinus Torvalds else 8071da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 8081da177e4SLinus Torvalds return status; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8115eebde23SSuresh Jayaraman static int 8126b96724eSRicardo Labiaga is_time_granular(struct timespec *ts) { 8136b96724eSRicardo Labiaga return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000)); 8146b96724eSRicardo Labiaga } 8156b96724eSRicardo Labiaga 8166b96724eSRicardo Labiaga static int 8175eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8201da177e4SLinus Torvalds int status; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds /* 8231da177e4SLinus Torvalds * Flush all pending writes before doing anything 8241da177e4SLinus Torvalds * with locks.. 8251da177e4SLinus Torvalds */ 82629884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 82729884df0STrond Myklebust if (status != 0) 8281da177e4SLinus Torvalds goto out; 8291da177e4SLinus Torvalds 8305eebde23SSuresh Jayaraman /* 8315eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 8325eebde23SSuresh Jayaraman * "-olocal_lock=" 8335eebde23SSuresh Jayaraman */ 8345eebde23SSuresh Jayaraman if (!is_local) 8351da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 836c4d7c402STrond Myklebust else 8371da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 8381da177e4SLinus Torvalds if (status < 0) 8391da177e4SLinus Torvalds goto out; 8406b96724eSRicardo Labiaga 8411da177e4SLinus Torvalds /* 8426b96724eSRicardo Labiaga * Revalidate the cache if the server has time stamps granular 8436b96724eSRicardo Labiaga * enough to detect subsecond changes. Otherwise, clear the 8446b96724eSRicardo Labiaga * cache to prevent missing any changes. 8456b96724eSRicardo Labiaga * 8461da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 8471da177e4SLinus Torvalds */ 84829884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 849011e2a7fSBryan Schumaker if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) { 8506b96724eSRicardo Labiaga if (is_time_granular(&NFS_SERVER(inode)->time_delta)) 8516b96724eSRicardo Labiaga __nfs_revalidate_inode(NFS_SERVER(inode), inode); 8526b96724eSRicardo Labiaga else 8531da177e4SLinus Torvalds nfs_zap_caches(inode); 8546b96724eSRicardo Labiaga } 8551da177e4SLinus Torvalds out: 8561da177e4SLinus Torvalds return status; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* 8601da177e4SLinus Torvalds * Lock a (portion of) a file 8611da177e4SLinus Torvalds */ 862ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8652116271aSTrond Myklebust int ret = -ENOLCK; 8665eebde23SSuresh Jayaraman int is_local = 0; 8671da177e4SLinus Torvalds 8686de1472fSAl Viro dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n", 8696de1472fSAl Viro filp, fl->fl_type, fl->fl_flags, 8701da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 8716da24bc9SChuck Lever 87291d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds /* No mandatory locks over NFS */ 875dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 8762116271aSTrond Myklebust goto out_err; 8772116271aSTrond Myklebust 8785eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 8795eebde23SSuresh Jayaraman is_local = 1; 8805eebde23SSuresh Jayaraman 8812116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 8822116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 8832116271aSTrond Myklebust if (ret < 0) 8842116271aSTrond Myklebust goto out_err; 8852116271aSTrond Myklebust } 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds if (IS_GETLK(cmd)) 8885eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8892116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 8905eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8912116271aSTrond Myklebust else 8925eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 8932116271aSTrond Myklebust out_err: 8942116271aSTrond Myklebust return ret; 8951da177e4SLinus Torvalds } 89689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock); 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /* 8991da177e4SLinus Torvalds * Lock a (portion of) a file 9001da177e4SLinus Torvalds */ 901ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 9021da177e4SLinus Torvalds { 9035eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 9045eebde23SSuresh Jayaraman int is_local = 0; 9055eebde23SSuresh Jayaraman 9066de1472fSAl Viro dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n", 9076de1472fSAl Viro filp, fl->fl_type, fl->fl_flags); 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 9101da177e4SLinus Torvalds return -ENOLCK; 9111da177e4SLinus Torvalds 912ad0fcd4eSJeff Layton /* 913ad0fcd4eSJeff Layton * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of 914ad0fcd4eSJeff Layton * any standard. In principle we might be able to support LOCK_MAND 915ad0fcd4eSJeff Layton * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the 916ad0fcd4eSJeff Layton * NFS code is not set up for it. 917ad0fcd4eSJeff Layton */ 918ad0fcd4eSJeff Layton if (fl->fl_type & LOCK_MAND) 919ad0fcd4eSJeff Layton return -EINVAL; 920ad0fcd4eSJeff Layton 9215eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 9225eebde23SSuresh Jayaraman is_local = 1; 9235eebde23SSuresh Jayaraman 9241da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 9251da177e4SLinus Torvalds fl->fl_owner = (fl_owner_t)filp; 9261da177e4SLinus Torvalds fl->fl_start = 0; 9271da177e4SLinus Torvalds fl->fl_end = OFFSET_MAX; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 9305eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 9315eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 9321da177e4SLinus Torvalds } 93389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock); 934370f6599SJ. Bruce Fields 9356da24bc9SChuck Lever /* 9366da24bc9SChuck Lever * There is no protocol support for leases, so we have no way to implement 9376da24bc9SChuck Lever * them correctly in the face of opens by other clients. 9386da24bc9SChuck Lever */ 939ce4ef7c0SBryan Schumaker int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 940370f6599SJ. Bruce Fields { 9416de1472fSAl Viro dprintk("NFS: setlease(%pD2, arg=%ld)\n", file, arg); 942370f6599SJ. Bruce Fields return -EINVAL; 943370f6599SJ. Bruce Fields } 94489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_setlease); 9451788ea6eSJeff Layton 9460486958fSJeff Layton const struct file_operations nfs_file_operations = { 9470486958fSJeff Layton .llseek = nfs_file_llseek, 9480486958fSJeff Layton .read = do_sync_read, 9490486958fSJeff Layton .write = do_sync_write, 9500486958fSJeff Layton .aio_read = nfs_file_read, 9510486958fSJeff Layton .aio_write = nfs_file_write, 9520486958fSJeff Layton .mmap = nfs_file_mmap, 9530486958fSJeff Layton .open = nfs_file_open, 9540486958fSJeff Layton .flush = nfs_file_flush, 9550486958fSJeff Layton .release = nfs_file_release, 9560486958fSJeff Layton .fsync = nfs_file_fsync, 9570486958fSJeff Layton .lock = nfs_lock, 9580486958fSJeff Layton .flock = nfs_flock, 9590486958fSJeff Layton .splice_read = nfs_file_splice_read, 9600486958fSJeff Layton .splice_write = nfs_file_splice_write, 9610486958fSJeff Layton .check_flags = nfs_check_flags, 9620486958fSJeff Layton .setlease = nfs_setlease, 9630486958fSJeff Layton }; 964ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations); 965