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> 295a0e3ad6STejun Heo #include <linux/gfp.h> 30b608b283STrond Myklebust #include <linux/swap.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <asm/uaccess.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include "delegation.h" 3594387fb1STrond Myklebust #include "internal.h" 3691d5b470SChuck Lever #include "iostat.h" 37545db45fSDavid Howells #include "fscache.h" 38612aa983SChristoph Hellwig #include "pnfs.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); 85aff8d8dcSAnna Schumaker nfs_file_clear_open_context(filp); 86aff8d8dcSAnna Schumaker return 0; 871da177e4SLinus Torvalds } 8889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release); 891da177e4SLinus Torvalds 90980802e3STrond Myklebust /** 91980802e3STrond Myklebust * nfs_revalidate_size - Revalidate the file size 92980802e3STrond Myklebust * @inode - pointer to inode struct 93980802e3STrond Myklebust * @file - pointer to struct file 94980802e3STrond Myklebust * 95980802e3STrond Myklebust * Revalidates the file length. This is basically a wrapper around 96980802e3STrond Myklebust * nfs_revalidate_inode() that takes into account the fact that we may 97980802e3STrond Myklebust * have cached writes (in which case we don't care about the server's 98980802e3STrond Myklebust * idea of what the file length is), or O_DIRECT (in which case we 99980802e3STrond Myklebust * shouldn't trust the cache). 100980802e3STrond Myklebust */ 101980802e3STrond Myklebust static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 102980802e3STrond Myklebust { 103980802e3STrond Myklebust struct nfs_server *server = NFS_SERVER(inode); 104980802e3STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 105980802e3STrond Myklebust 106d7cf8dd0STrond Myklebust if (nfs_have_delegated_attributes(inode)) 107d7cf8dd0STrond Myklebust goto out_noreval; 108d7cf8dd0STrond Myklebust 109980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 110980802e3STrond Myklebust goto force_reval; 111d7cf8dd0STrond Myklebust if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 112d7cf8dd0STrond Myklebust goto force_reval; 113d7cf8dd0STrond Myklebust if (nfs_attribute_timeout(inode)) 114d7cf8dd0STrond Myklebust goto force_reval; 115d7cf8dd0STrond Myklebust out_noreval: 116fe51beecSTrond Myklebust return 0; 117980802e3STrond Myklebust force_reval: 118980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 119980802e3STrond Myklebust } 120980802e3STrond Myklebust 121965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence) 122980802e3STrond Myklebust { 1236de1472fSAl Viro dprintk("NFS: llseek file(%pD2, %lld, %d)\n", 1246de1472fSAl Viro filp, offset, whence); 125b84e06c5SChuck Lever 12606222e49SJosef Bacik /* 127965c8e59SAndrew Morton * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 12806222e49SJosef Bacik * the cached file length 12906222e49SJosef Bacik */ 130965c8e59SAndrew Morton if (whence != SEEK_SET && whence != SEEK_CUR) { 131980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 132d5e66348STrond Myklebust 133980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 134980802e3STrond Myklebust if (retval < 0) 135980802e3STrond Myklebust return (loff_t)retval; 13679835a71SAndi Kleen } 137d5e66348STrond Myklebust 138965c8e59SAndrew Morton return generic_file_llseek(filp, offset, whence); 139980802e3STrond Myklebust } 14089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek); 141980802e3STrond Myklebust 1421da177e4SLinus Torvalds /* 1431da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 1441da177e4SLinus Torvalds */ 1455445b1fbSTrond Myklebust static int 14675e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 1471da177e4SLinus Torvalds { 1486de1472fSAl Viro struct inode *inode = file_inode(file); 1491da177e4SLinus Torvalds 1506de1472fSAl Viro dprintk("NFS: flush(%pD2)\n", file); 1511da177e4SLinus Torvalds 152c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 1531da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 1541da177e4SLinus Torvalds return 0; 1557b159fc1STrond Myklebust 1567fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 157af7fa165STrond Myklebust return vfs_fsync(file, 0); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160ce4ef7c0SBryan Schumaker ssize_t 1613aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to) 1621da177e4SLinus Torvalds { 1636de1472fSAl Viro struct inode *inode = file_inode(iocb->ki_filp); 1641da177e4SLinus Torvalds ssize_t result; 1651da177e4SLinus Torvalds 1662ba48ce5SAl Viro if (iocb->ki_flags & IOCB_DIRECT) 167c8b8e32dSChristoph Hellwig return nfs_file_direct_read(iocb, to); 1681da177e4SLinus Torvalds 169619d30b4SAl Viro dprintk("NFS: read(%pD2, %zu@%lu)\n", 1706de1472fSAl Viro iocb->ki_filp, 1713aa2d199SAl Viro iov_iter_count(to), (unsigned long) iocb->ki_pos); 1721da177e4SLinus Torvalds 173*a5864c99STrond Myklebust nfs_start_io_read(inode); 174*a5864c99STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 1754184dcf2SChuck Lever if (!result) { 1763aa2d199SAl Viro result = generic_file_read_iter(iocb, to); 1774184dcf2SChuck Lever if (result > 0) 1784184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 1794184dcf2SChuck Lever } 180*a5864c99STrond Myklebust nfs_end_io_read(inode); 1811da177e4SLinus Torvalds return result; 1821da177e4SLinus Torvalds } 18389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read); 1841da177e4SLinus Torvalds 185ce4ef7c0SBryan Schumaker ssize_t 186f0930fffSJens Axboe nfs_file_splice_read(struct file *filp, loff_t *ppos, 187f0930fffSJens Axboe struct pipe_inode_info *pipe, size_t count, 188f0930fffSJens Axboe unsigned int flags) 1891da177e4SLinus Torvalds { 1906de1472fSAl Viro struct inode *inode = file_inode(filp); 1911da177e4SLinus Torvalds ssize_t res; 1921da177e4SLinus Torvalds 1936de1472fSAl Viro dprintk("NFS: splice_read(%pD2, %lu@%Lu)\n", 1946de1472fSAl Viro filp, (unsigned long) count, (unsigned long long) *ppos); 1951da177e4SLinus Torvalds 196*a5864c99STrond Myklebust nfs_start_io_read(inode); 197*a5864c99STrond Myklebust res = nfs_revalidate_mapping(inode, filp->f_mapping); 198aa2f1ef1SChuck Lever if (!res) { 199f0930fffSJens Axboe res = generic_file_splice_read(filp, ppos, pipe, count, flags); 200aa2f1ef1SChuck Lever if (res > 0) 201aa2f1ef1SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res); 202aa2f1ef1SChuck Lever } 203*a5864c99STrond Myklebust nfs_end_io_read(inode); 2041da177e4SLinus Torvalds return res; 2051da177e4SLinus Torvalds } 20689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_splice_read); 2071da177e4SLinus Torvalds 208ce4ef7c0SBryan Schumaker int 2091da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2101da177e4SLinus Torvalds { 2116de1472fSAl Viro struct inode *inode = file_inode(file); 2121da177e4SLinus Torvalds int status; 2131da177e4SLinus Torvalds 2146de1472fSAl Viro dprintk("NFS: mmap(%pD2)\n", file); 2151da177e4SLinus Torvalds 216e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 217e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 218e1ebfd33STrond Myklebust */ 219e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 22094387fb1STrond Myklebust if (!status) { 22194387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 222e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 22394387fb1STrond Myklebust } 2241da177e4SLinus Torvalds return status; 2251da177e4SLinus Torvalds } 22689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2301da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2311da177e4SLinus Torvalds * whether any write errors occurred for this process. 232af7fa165STrond Myklebust * 233af7fa165STrond Myklebust * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 234af7fa165STrond Myklebust * disk, but it retrieves and clears ctx->error after synching, despite 235af7fa165STrond Myklebust * the two being set at the same time in nfs_context_set_write_error(). 236af7fa165STrond Myklebust * This is because the former is used to notify the _next_ call to 23725985edcSLucas De Marchi * nfs_file_write() that a write error occurred, and hence cause it to 238af7fa165STrond Myklebust * fall back to doing a synchronous write. 2391da177e4SLinus Torvalds */ 2404ff79bc7SChristoph Hellwig static int 241a5c58892SBryan Schumaker nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync) 2421da177e4SLinus Torvalds { 243cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 2446de1472fSAl Viro struct inode *inode = file_inode(file); 24505990d1bSTrond Myklebust int have_error, do_resend, status; 246af7fa165STrond Myklebust int ret = 0; 247af7fa165STrond Myklebust 2486de1472fSAl Viro dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync); 2491da177e4SLinus Torvalds 25091d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 25105990d1bSTrond Myklebust do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); 252af7fa165STrond Myklebust have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 253af7fa165STrond Myklebust status = nfs_commit_inode(inode, FLUSH_SYNC); 254af7fa165STrond Myklebust have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 25505990d1bSTrond Myklebust if (have_error) { 256af7fa165STrond Myklebust ret = xchg(&ctx->error, 0); 25705990d1bSTrond Myklebust if (ret) 25805990d1bSTrond Myklebust goto out; 25905990d1bSTrond Myklebust } 26005990d1bSTrond Myklebust if (status < 0) { 261af7fa165STrond Myklebust ret = status; 26205990d1bSTrond Myklebust goto out; 26305990d1bSTrond Myklebust } 26405990d1bSTrond Myklebust do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); 26505990d1bSTrond Myklebust if (do_resend) 26605990d1bSTrond Myklebust ret = -EAGAIN; 26705990d1bSTrond Myklebust out: 268a5c58892SBryan Schumaker return ret; 269a5c58892SBryan Schumaker } 270a5c58892SBryan Schumaker 2714ff79bc7SChristoph Hellwig int 272a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 273a5c58892SBryan Schumaker { 274a5c58892SBryan Schumaker int ret; 275496ad9aaSAl Viro struct inode *inode = file_inode(file); 276a5c58892SBryan Schumaker 277f4ce1299STrond Myklebust trace_nfs_fsync_enter(inode); 278f4ce1299STrond Myklebust 27995d9f6c3SChristoph Hellwig inode_dio_wait(inode); 28005990d1bSTrond Myklebust do { 281a5c58892SBryan Schumaker ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 2827b281ee0STrond Myklebust if (ret != 0) 28305990d1bSTrond Myklebust break; 284a5c58892SBryan Schumaker ret = nfs_file_fsync_commit(file, start, end, datasync); 2854ff79bc7SChristoph Hellwig if (!ret) 2864ff79bc7SChristoph Hellwig ret = pnfs_sync_inode(inode, !!datasync); 287dcfc4f25STrond Myklebust /* 288dcfc4f25STrond Myklebust * If nfs_file_fsync_commit detected a server reboot, then 289dcfc4f25STrond Myklebust * resend all dirty pages that might have been covered by 290dcfc4f25STrond Myklebust * the NFS_CONTEXT_RESEND_WRITES flag 291dcfc4f25STrond Myklebust */ 292dcfc4f25STrond Myklebust start = 0; 293dcfc4f25STrond Myklebust end = LLONG_MAX; 29405990d1bSTrond Myklebust } while (ret == -EAGAIN); 29505990d1bSTrond Myklebust 296f4ce1299STrond Myklebust trace_nfs_fsync_exit(inode, ret); 297af7fa165STrond Myklebust return ret; 2981da177e4SLinus Torvalds } 2994ff79bc7SChristoph Hellwig EXPORT_SYMBOL_GPL(nfs_file_fsync); 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* 30238c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 30338c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 30438c73044SPeter Staubach * page cache. 30538c73044SPeter Staubach * 30638c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 30738c73044SPeter Staubach * being completely filled by the writer. In this situation, the 30838c73044SPeter Staubach * page must be completely written to stable storage on the server 30938c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 31038c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 31138c73044SPeter Staubach * done. 31238c73044SPeter Staubach * 31338c73044SPeter Staubach * It may be more efficient to read the page first if the file is 31438c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 31538c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 31638c73044SPeter Staubach * indicating that it was previously allocated and then modified, 31738c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 31838c73044SPeter Staubach * and that the new data won't completely replace the old data in 31938c73044SPeter Staubach * that range of the file. 32038c73044SPeter Staubach */ 32138c73044SPeter Staubach static int nfs_want_read_modify_write(struct file *file, struct page *page, 32238c73044SPeter Staubach loff_t pos, unsigned len) 32338c73044SPeter Staubach { 32438c73044SPeter Staubach unsigned int pglen = nfs_page_length(page); 32509cbfeafSKirill A. Shutemov unsigned int offset = pos & (PAGE_SIZE - 1); 32638c73044SPeter Staubach unsigned int end = offset + len; 32738c73044SPeter Staubach 328612aa983SChristoph Hellwig if (pnfs_ld_read_whole_page(file->f_mapping->host)) { 329612aa983SChristoph Hellwig if (!PageUptodate(page)) 330612aa983SChristoph Hellwig return 1; 331612aa983SChristoph Hellwig return 0; 332612aa983SChristoph Hellwig } 333612aa983SChristoph Hellwig 33438c73044SPeter Staubach if ((file->f_mode & FMODE_READ) && /* open for read? */ 33538c73044SPeter Staubach !PageUptodate(page) && /* Uptodate? */ 33638c73044SPeter Staubach !PagePrivate(page) && /* i/o request already? */ 33738c73044SPeter Staubach pglen && /* valid bytes of file? */ 33838c73044SPeter Staubach (end < pglen || offset)) /* replace all valid bytes? */ 33938c73044SPeter Staubach return 1; 34038c73044SPeter Staubach return 0; 34138c73044SPeter Staubach } 34238c73044SPeter Staubach 34338c73044SPeter Staubach /* 3444899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3454899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3464899f9c8SNick Piggin * data from user space. 3471da177e4SLinus Torvalds * 3481da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3491da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3501da177e4SLinus Torvalds */ 3514899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 3524899f9c8SNick Piggin loff_t pos, unsigned len, unsigned flags, 3534899f9c8SNick Piggin struct page **pagep, void **fsdata) 3541da177e4SLinus Torvalds { 3554899f9c8SNick Piggin int ret; 35609cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 3574899f9c8SNick Piggin struct page *page; 35838c73044SPeter Staubach int once_thru = 0; 3594899f9c8SNick Piggin 3601e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n", 3616de1472fSAl Viro file, mapping->host->i_ino, len, (long long) pos); 362b7eaefaaSChuck Lever 36338c73044SPeter Staubach start: 36472cb77f4STrond Myklebust /* 365aa5acceaSTrond Myklebust * Wait for O_DIRECT to complete 366aa5acceaSTrond Myklebust */ 36795d9f6c3SChristoph Hellwig inode_dio_wait(mapping->host); 36872cb77f4STrond Myklebust 36954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3704899f9c8SNick Piggin if (!page) 3714899f9c8SNick Piggin return -ENOMEM; 3724899f9c8SNick Piggin *pagep = page; 3734899f9c8SNick Piggin 3744899f9c8SNick Piggin ret = nfs_flush_incompatible(file, page); 3754899f9c8SNick Piggin if (ret) { 3764899f9c8SNick Piggin unlock_page(page); 37709cbfeafSKirill A. Shutemov put_page(page); 37838c73044SPeter Staubach } else if (!once_thru && 37938c73044SPeter Staubach nfs_want_read_modify_write(file, page, pos, len)) { 38038c73044SPeter Staubach once_thru = 1; 38138c73044SPeter Staubach ret = nfs_readpage(file, page); 38209cbfeafSKirill A. Shutemov put_page(page); 38338c73044SPeter Staubach if (!ret) 38438c73044SPeter Staubach goto start; 3854899f9c8SNick Piggin } 3864899f9c8SNick Piggin return ret; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3894899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3904899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3914899f9c8SNick Piggin struct page *page, void *fsdata) 3921da177e4SLinus Torvalds { 39309cbfeafSKirill A. Shutemov unsigned offset = pos & (PAGE_SIZE - 1); 394dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(file); 3954899f9c8SNick Piggin int status; 3961da177e4SLinus Torvalds 3971e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n", 3986de1472fSAl Viro file, mapping->host->i_ino, len, (long long) pos); 399b7eaefaaSChuck Lever 400efc91ed0STrond Myklebust /* 401efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 402efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 403efc91ed0STrond Myklebust */ 404efc91ed0STrond Myklebust if (!PageUptodate(page)) { 405efc91ed0STrond Myklebust unsigned pglen = nfs_page_length(page); 406efc91ed0STrond Myklebust unsigned end = offset + len; 407efc91ed0STrond Myklebust 408efc91ed0STrond Myklebust if (pglen == 0) { 409efc91ed0STrond Myklebust zero_user_segments(page, 0, offset, 41009cbfeafSKirill A. Shutemov end, PAGE_SIZE); 411efc91ed0STrond Myklebust SetPageUptodate(page); 412efc91ed0STrond Myklebust } else if (end >= pglen) { 41309cbfeafSKirill A. Shutemov zero_user_segment(page, end, PAGE_SIZE); 414efc91ed0STrond Myklebust if (offset == 0) 415efc91ed0STrond Myklebust SetPageUptodate(page); 416efc91ed0STrond Myklebust } else 41709cbfeafSKirill A. Shutemov zero_user_segment(page, pglen, PAGE_SIZE); 418efc91ed0STrond Myklebust } 419efc91ed0STrond Myklebust 4204899f9c8SNick Piggin status = nfs_updatepage(file, page, offset, copied); 4214899f9c8SNick Piggin 4224899f9c8SNick Piggin unlock_page(page); 42309cbfeafSKirill A. Shutemov put_page(page); 4244899f9c8SNick Piggin 4253d509e54SChuck Lever if (status < 0) 4263d509e54SChuck Lever return status; 4272701d086SAndy Adamson NFS_I(mapping->host)->write_io += copied; 428dc24826bSAndy Adamson 429dc24826bSAndy Adamson if (nfs_ctx_key_to_expire(ctx)) { 430dc24826bSAndy Adamson status = nfs_wb_all(mapping->host); 431dc24826bSAndy Adamson if (status < 0) 432dc24826bSAndy Adamson return status; 433dc24826bSAndy Adamson } 434dc24826bSAndy Adamson 4353d509e54SChuck Lever return copied; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4386b9b3514SDavid Howells /* 4396b9b3514SDavid Howells * Partially or wholly invalidate a page 4406b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4416b9b3514SDavid Howells * page invalidation 442545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4436b9b3514SDavid Howells * - Caller holds page lock 4446b9b3514SDavid Howells */ 445d47992f8SLukas Czerner static void nfs_invalidate_page(struct page *page, unsigned int offset, 446d47992f8SLukas Czerner unsigned int length) 447cd52ed35STrond Myklebust { 448d47992f8SLukas Czerner dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n", 449d47992f8SLukas Czerner page, offset, length); 450b7eaefaaSChuck Lever 45109cbfeafSKirill A. Shutemov if (offset != 0 || length < PAGE_SIZE) 4521c75950bSTrond Myklebust return; 453d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 454d56b4ddfSMel Gorman nfs_wb_page_cancel(page_file_mapping(page)->host, page); 455545db45fSDavid Howells 456545db45fSDavid Howells nfs_fscache_invalidate_page(page, page->mapping->host); 457cd52ed35STrond Myklebust } 458cd52ed35STrond Myklebust 4596b9b3514SDavid Howells /* 4606b9b3514SDavid Howells * Attempt to release the private state associated with a page 461545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4626b9b3514SDavid Howells * - Caller holds page lock 4636b9b3514SDavid Howells * - Return true (may release page) or false (may not) 4646b9b3514SDavid Howells */ 465cd52ed35STrond Myklebust static int nfs_release_page(struct page *page, gfp_t gfp) 466cd52ed35STrond Myklebust { 467b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page); 468b7eaefaaSChuck Lever 469e3db7691STrond Myklebust /* If PagePrivate() is set, then the page is not freeable */ 470545db45fSDavid Howells if (PagePrivate(page)) 471ddeff520SNikita Danilov return 0; 472545db45fSDavid Howells return nfs_fscache_release_page(page, gfp); 473e3db7691STrond Myklebust } 474e3db7691STrond Myklebust 475f919b196SMel Gorman static void nfs_check_dirty_writeback(struct page *page, 476f919b196SMel Gorman bool *dirty, bool *writeback) 477f919b196SMel Gorman { 478f919b196SMel Gorman struct nfs_inode *nfsi; 479f919b196SMel Gorman struct address_space *mapping = page_file_mapping(page); 480f919b196SMel Gorman 481f919b196SMel Gorman if (!mapping || PageSwapCache(page)) 482f919b196SMel Gorman return; 483f919b196SMel Gorman 484f919b196SMel Gorman /* 485f919b196SMel Gorman * Check if an unstable page is currently being committed and 486f919b196SMel Gorman * if so, have the VM treat it as if the page is under writeback 487f919b196SMel Gorman * so it will not block due to pages that will shortly be freeable. 488f919b196SMel Gorman */ 489f919b196SMel Gorman nfsi = NFS_I(mapping->host); 490af7cf057STrond Myklebust if (atomic_read(&nfsi->commit_info.rpcs_out)) { 491f919b196SMel Gorman *writeback = true; 492f919b196SMel Gorman return; 493f919b196SMel Gorman } 494f919b196SMel Gorman 495f919b196SMel Gorman /* 496f919b196SMel Gorman * If PagePrivate() is set, then the page is not freeable and as the 497f919b196SMel Gorman * inode is not being committed, it's not going to be cleaned in the 498f919b196SMel Gorman * near future so treat it as dirty 499f919b196SMel Gorman */ 500f919b196SMel Gorman if (PagePrivate(page)) 501f919b196SMel Gorman *dirty = true; 502f919b196SMel Gorman } 503f919b196SMel Gorman 5046b9b3514SDavid Howells /* 5056b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 5066b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 5076b9b3514SDavid Howells * destroyed 508545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 5096b9b3514SDavid Howells * - Caller holds page lock 5106b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 5116b9b3514SDavid Howells */ 512e3db7691STrond Myklebust static int nfs_launder_page(struct page *page) 513e3db7691STrond Myklebust { 514d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 515545db45fSDavid Howells struct nfs_inode *nfsi = NFS_I(inode); 516b7eaefaaSChuck Lever 517b7eaefaaSChuck Lever dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n", 518b7eaefaaSChuck Lever inode->i_ino, (long long)page_offset(page)); 519b7eaefaaSChuck Lever 520545db45fSDavid Howells nfs_fscache_wait_on_page_write(nfsi, page); 521d6c843b9SPeng Tao return nfs_wb_launder_page(inode, page); 522cd52ed35STrond Myklebust } 523cd52ed35STrond Myklebust 524a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file, 525a564b8f0SMel Gorman sector_t *span) 526a564b8f0SMel Gorman { 527dad2b015SJeff Layton struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host); 528dad2b015SJeff Layton 529a564b8f0SMel Gorman *span = sis->pages; 530dad2b015SJeff Layton 5313c87ef6eSJeff Layton return rpc_clnt_swap_activate(clnt); 532a564b8f0SMel Gorman } 533a564b8f0SMel Gorman 534a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file) 535a564b8f0SMel Gorman { 536dad2b015SJeff Layton struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host); 537dad2b015SJeff Layton 5383c87ef6eSJeff Layton rpc_clnt_swap_deactivate(clnt); 539a564b8f0SMel Gorman } 540a564b8f0SMel Gorman 541f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 5421da177e4SLinus Torvalds .readpage = nfs_readpage, 5431da177e4SLinus Torvalds .readpages = nfs_readpages, 5449cccef95STrond Myklebust .set_page_dirty = __set_page_dirty_nobuffers, 5451da177e4SLinus Torvalds .writepage = nfs_writepage, 5461da177e4SLinus Torvalds .writepages = nfs_writepages, 5474899f9c8SNick Piggin .write_begin = nfs_write_begin, 5484899f9c8SNick Piggin .write_end = nfs_write_end, 549cd52ed35STrond Myklebust .invalidatepage = nfs_invalidate_page, 550cd52ed35STrond Myklebust .releasepage = nfs_release_page, 5511da177e4SLinus Torvalds .direct_IO = nfs_direct_IO, 552074cc1deSTrond Myklebust .migratepage = nfs_migrate_page, 553e3db7691STrond Myklebust .launder_page = nfs_launder_page, 554f919b196SMel Gorman .is_dirty_writeback = nfs_check_dirty_writeback, 555f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 556a564b8f0SMel Gorman .swap_activate = nfs_swap_activate, 557a564b8f0SMel Gorman .swap_deactivate = nfs_swap_deactivate, 5581da177e4SLinus Torvalds }; 5591da177e4SLinus Torvalds 5606b9b3514SDavid Howells /* 5616b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5626b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5636b9b3514SDavid Howells * shared-writable mapping 5646b9b3514SDavid Howells */ 565c2ec175cSNick Piggin static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 56694387fb1STrond Myklebust { 567c2ec175cSNick Piggin struct page *page = vmf->page; 56894387fb1STrond Myklebust struct file *filp = vma->vm_file; 5696de1472fSAl Viro struct inode *inode = file_inode(filp); 57094387fb1STrond Myklebust unsigned pagelen; 571bc4866b6STrond Myklebust int ret = VM_FAULT_NOPAGE; 5724899f9c8SNick Piggin struct address_space *mapping; 57394387fb1STrond Myklebust 5741e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n", 5756de1472fSAl Viro filp, filp->f_mapping->host->i_ino, 576b7eaefaaSChuck Lever (long long)page_offset(page)); 577b7eaefaaSChuck Lever 578545db45fSDavid Howells /* make sure the cache has finished storing the page */ 5796de1472fSAl Viro nfs_fscache_wait_on_page_write(NFS_I(inode), page); 580545db45fSDavid Howells 581ef070dcbSTrond Myklebust wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING, 582ef070dcbSTrond Myklebust nfs_wait_bit_killable, TASK_KILLABLE); 583ef070dcbSTrond Myklebust 58494387fb1STrond Myklebust lock_page(page); 585d56b4ddfSMel Gorman mapping = page_file_mapping(page); 5866de1472fSAl Viro if (mapping != inode->i_mapping) 5878b1f9ee5STrond Myklebust goto out_unlock; 5888b1f9ee5STrond Myklebust 5892aeb98f4STrond Myklebust wait_on_page_writeback(page); 5902aeb98f4STrond Myklebust 5914899f9c8SNick Piggin pagelen = nfs_page_length(page); 5928b1f9ee5STrond Myklebust if (pagelen == 0) 5938b1f9ee5STrond Myklebust goto out_unlock; 5948b1f9ee5STrond Myklebust 595bc4866b6STrond Myklebust ret = VM_FAULT_LOCKED; 596bc4866b6STrond Myklebust if (nfs_flush_incompatible(filp, page) == 0 && 597bc4866b6STrond Myklebust nfs_updatepage(filp, page, 0, pagelen) == 0) 598bc4866b6STrond Myklebust goto out; 5998b1f9ee5STrond Myklebust 600bc4866b6STrond Myklebust ret = VM_FAULT_SIGBUS; 6018b1f9ee5STrond Myklebust out_unlock: 6024899f9c8SNick Piggin unlock_page(page); 603bc4866b6STrond Myklebust out: 604bc4866b6STrond Myklebust return ret; 60594387fb1STrond Myklebust } 60694387fb1STrond Myklebust 607f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 60894387fb1STrond Myklebust .fault = filemap_fault, 609f1820361SKirill A. Shutemov .map_pages = filemap_map_pages, 61094387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 61194387fb1STrond Myklebust }; 61294387fb1STrond Myklebust 6137e94d6c4STrond Myklebust static int nfs_need_check_write(struct file *filp, struct inode *inode) 6147b159fc1STrond Myklebust { 6157b159fc1STrond Myklebust struct nfs_open_context *ctx; 6167b159fc1STrond Myklebust 617cd3758e3STrond Myklebust ctx = nfs_file_open_context(filp); 618dc24826bSAndy Adamson if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) || 619dc24826bSAndy Adamson nfs_ctx_key_to_expire(ctx)) 6207b159fc1STrond Myklebust return 1; 6217b159fc1STrond Myklebust return 0; 6227b159fc1STrond Myklebust } 6237b159fc1STrond Myklebust 624edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from) 6251da177e4SLinus Torvalds { 6266de1472fSAl Viro struct file *file = iocb->ki_filp; 6276de1472fSAl Viro struct inode *inode = file_inode(file); 6287e381172SChuck Lever unsigned long written = 0; 6291da177e4SLinus Torvalds ssize_t result; 6301da177e4SLinus Torvalds 6316de1472fSAl Viro result = nfs_key_timeout_notify(file, inode); 632dc24826bSAndy Adamson if (result) 633dc24826bSAndy Adamson return result; 634dc24826bSAndy Adamson 63589698b24STrond Myklebust if (iocb->ki_flags & IOCB_DIRECT) 63665a4a1caSAl Viro return nfs_file_direct_write(iocb, from); 6371da177e4SLinus Torvalds 638619d30b4SAl Viro dprintk("NFS: write(%pD2, %zu@%Ld)\n", 63918290650STrond Myklebust file, iov_iter_count(from), (long long) iocb->ki_pos); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 6421da177e4SLinus Torvalds goto out_swapfile; 6437d52e862STrond Myklebust /* 6447d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 6457d52e862STrond Myklebust */ 6462ba48ce5SAl Viro if (iocb->ki_flags & IOCB_APPEND) { 6476de1472fSAl Viro result = nfs_revalidate_file_size(inode, file); 6481da177e4SLinus Torvalds if (result) 6491da177e4SLinus Torvalds goto out; 650fe51beecSTrond Myklebust } 6511da177e4SLinus Torvalds 652*a5864c99STrond Myklebust nfs_start_io_write(inode); 65318290650STrond Myklebust result = generic_write_checks(iocb, from); 65418290650STrond Myklebust if (result > 0) { 65518290650STrond Myklebust current->backing_dev_info = inode_to_bdi(inode); 65618290650STrond Myklebust result = generic_perform_write(file, from, iocb->ki_pos); 65718290650STrond Myklebust current->backing_dev_info = NULL; 65818290650STrond Myklebust } 659*a5864c99STrond Myklebust nfs_end_io_write(inode); 66018290650STrond Myklebust if (result <= 0) 6611da177e4SLinus Torvalds goto out; 6621da177e4SLinus Torvalds 66318290650STrond Myklebust written = generic_write_sync(iocb, result); 66418290650STrond Myklebust iocb->ki_pos += written; 6657e381172SChuck Lever 6667e94d6c4STrond Myklebust /* Return error values */ 66718290650STrond Myklebust if (nfs_need_check_write(file, inode)) { 6686de1472fSAl Viro int err = vfs_fsync(file, 0); 669200baa21STrond Myklebust if (err < 0) 670200baa21STrond Myklebust result = err; 671200baa21STrond Myklebust } 6727e381172SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 6731da177e4SLinus Torvalds out: 6741da177e4SLinus Torvalds return result; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds out_swapfile: 6771da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 67818290650STrond Myklebust return -EBUSY; 6791da177e4SLinus Torvalds } 68089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write); 6811da177e4SLinus Torvalds 6825eebde23SSuresh Jayaraman static int 6835eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 6861da177e4SLinus Torvalds int status = 0; 68721ac19d4SSergey Vlasov unsigned int saved_type = fl->fl_type; 6881da177e4SLinus Torvalds 689039c4d7aSTrond Myklebust /* Try local locking first */ 6906d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 6916d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 6926d34ac19SJ. Bruce Fields /* found a conflict */ 693039c4d7aSTrond Myklebust goto out; 6941da177e4SLinus Torvalds } 69521ac19d4SSergey Vlasov fl->fl_type = saved_type; 696039c4d7aSTrond Myklebust 697011e2a7fSBryan Schumaker if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 698039c4d7aSTrond Myklebust goto out_noconflict; 699039c4d7aSTrond Myklebust 7005eebde23SSuresh Jayaraman if (is_local) 701039c4d7aSTrond Myklebust goto out_noconflict; 702039c4d7aSTrond Myklebust 703039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 704039c4d7aSTrond Myklebust out: 7051da177e4SLinus Torvalds return status; 706039c4d7aSTrond Myklebust out_noconflict: 707039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 708039c4d7aSTrond Myklebust goto out; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds static int do_vfs_lock(struct file *file, struct file_lock *fl) 7121da177e4SLinus Torvalds { 7134f656367SBenjamin Coddington return locks_lock_file_wait(file, fl); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7165eebde23SSuresh Jayaraman static int 7175eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7181da177e4SLinus Torvalds { 7191da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7207a8203d8STrond Myklebust struct nfs_lock_context *l_ctx; 7211da177e4SLinus Torvalds int status; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* 7241da177e4SLinus Torvalds * Flush all pending writes before doing anything 7251da177e4SLinus Torvalds * with locks.. 7261da177e4SLinus Torvalds */ 727d9dabc1aSTrond Myklebust vfs_fsync(filp, 0); 7281da177e4SLinus Torvalds 7297a8203d8STrond Myklebust l_ctx = nfs_get_lock_context(nfs_file_open_context(filp)); 7307a8203d8STrond Myklebust if (!IS_ERR(l_ctx)) { 731210c7c17SBenjamin Coddington status = nfs_iocounter_wait(l_ctx); 7327a8203d8STrond Myklebust nfs_put_lock_context(l_ctx); 7337a8203d8STrond Myklebust if (status < 0) 7347a8203d8STrond Myklebust return status; 7357a8203d8STrond Myklebust } 7367a8203d8STrond Myklebust 7371da177e4SLinus Torvalds /* NOTE: special case 7381da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 7391da177e4SLinus Torvalds * still need to complete the unlock. 7401da177e4SLinus Torvalds */ 7415eebde23SSuresh Jayaraman /* 7425eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7435eebde23SSuresh Jayaraman * "-olocal_lock=" 7445eebde23SSuresh Jayaraman */ 7455eebde23SSuresh Jayaraman if (!is_local) 7461da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 7471da177e4SLinus Torvalds else 7481da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7491da177e4SLinus Torvalds return status; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7525eebde23SSuresh Jayaraman static int 7535eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7561da177e4SLinus Torvalds int status; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* 7591da177e4SLinus Torvalds * Flush all pending writes before doing anything 7601da177e4SLinus Torvalds * with locks.. 7611da177e4SLinus Torvalds */ 76229884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 76329884df0STrond Myklebust if (status != 0) 7641da177e4SLinus Torvalds goto out; 7651da177e4SLinus Torvalds 7665eebde23SSuresh Jayaraman /* 7675eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7685eebde23SSuresh Jayaraman * "-olocal_lock=" 7695eebde23SSuresh Jayaraman */ 7705eebde23SSuresh Jayaraman if (!is_local) 7711da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 772c4d7c402STrond Myklebust else 7731da177e4SLinus Torvalds status = do_vfs_lock(filp, fl); 7741da177e4SLinus Torvalds if (status < 0) 7751da177e4SLinus Torvalds goto out; 7766b96724eSRicardo Labiaga 7771da177e4SLinus Torvalds /* 7786b96724eSRicardo Labiaga * Revalidate the cache if the server has time stamps granular 7796b96724eSRicardo Labiaga * enough to detect subsecond changes. Otherwise, clear the 7806b96724eSRicardo Labiaga * cache to prevent missing any changes. 7816b96724eSRicardo Labiaga * 7821da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 7831da177e4SLinus Torvalds */ 78429884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 785ca0daa27STrond Myklebust if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 786ca0daa27STrond Myklebust nfs_zap_mapping(inode, filp->f_mapping); 7871da177e4SLinus Torvalds out: 7881da177e4SLinus Torvalds return status; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * Lock a (portion of) a file 7931da177e4SLinus Torvalds */ 794ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7972116271aSTrond Myklebust int ret = -ENOLCK; 7985eebde23SSuresh Jayaraman int is_local = 0; 7991da177e4SLinus Torvalds 8006de1472fSAl Viro dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n", 8016de1472fSAl Viro filp, fl->fl_type, fl->fl_flags, 8021da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 8036da24bc9SChuck Lever 80491d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* No mandatory locks over NFS */ 807dfad9441SPavel Emelyanov if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 8082116271aSTrond Myklebust goto out_err; 8092116271aSTrond Myklebust 8105eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 8115eebde23SSuresh Jayaraman is_local = 1; 8125eebde23SSuresh Jayaraman 8132116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 8142116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 8152116271aSTrond Myklebust if (ret < 0) 8162116271aSTrond Myklebust goto out_err; 8172116271aSTrond Myklebust } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds if (IS_GETLK(cmd)) 8205eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8212116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 8225eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8232116271aSTrond Myklebust else 8245eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 8252116271aSTrond Myklebust out_err: 8262116271aSTrond Myklebust return ret; 8271da177e4SLinus Torvalds } 82889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* 8311da177e4SLinus Torvalds * Lock a (portion of) a file 8321da177e4SLinus Torvalds */ 833ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 8341da177e4SLinus Torvalds { 8355eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 8365eebde23SSuresh Jayaraman int is_local = 0; 8375eebde23SSuresh Jayaraman 8386de1472fSAl Viro dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n", 8396de1472fSAl Viro filp, fl->fl_type, fl->fl_flags); 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 8421da177e4SLinus Torvalds return -ENOLCK; 8431da177e4SLinus Torvalds 844ad0fcd4eSJeff Layton /* 845ad0fcd4eSJeff Layton * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of 846ad0fcd4eSJeff Layton * any standard. In principle we might be able to support LOCK_MAND 847ad0fcd4eSJeff Layton * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the 848ad0fcd4eSJeff Layton * NFS code is not set up for it. 849ad0fcd4eSJeff Layton */ 850ad0fcd4eSJeff Layton if (fl->fl_type & LOCK_MAND) 851ad0fcd4eSJeff Layton return -EINVAL; 852ad0fcd4eSJeff Layton 8535eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 8545eebde23SSuresh Jayaraman is_local = 1; 8555eebde23SSuresh Jayaraman 8561da177e4SLinus Torvalds /* We're simulating flock() locks using posix locks on the server */ 8571da177e4SLinus Torvalds if (fl->fl_type == F_UNLCK) 8585eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 8595eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 8601da177e4SLinus Torvalds } 86189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock); 862370f6599SJ. Bruce Fields 8630486958fSJeff Layton const struct file_operations nfs_file_operations = { 8640486958fSJeff Layton .llseek = nfs_file_llseek, 8653aa2d199SAl Viro .read_iter = nfs_file_read, 866edaf4369SAl Viro .write_iter = nfs_file_write, 8670486958fSJeff Layton .mmap = nfs_file_mmap, 8680486958fSJeff Layton .open = nfs_file_open, 8690486958fSJeff Layton .flush = nfs_file_flush, 8700486958fSJeff Layton .release = nfs_file_release, 8710486958fSJeff Layton .fsync = nfs_file_fsync, 8720486958fSJeff Layton .lock = nfs_lock, 8730486958fSJeff Layton .flock = nfs_flock, 8740486958fSJeff Layton .splice_read = nfs_file_splice_read, 8754da54c21SAl Viro .splice_write = iter_file_splice_write, 8760486958fSJeff Layton .check_flags = nfs_check_flags, 8771c994a09SJeff Layton .setlease = simple_nosetlease, 8780486958fSJeff Layton }; 879ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations); 880