1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/file.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1992 Rick Sladkey 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Changes Copyright (C) 1994 by Florian La Roche 81da177e4SLinus Torvalds * - Do not copy data too often around in the kernel. 91da177e4SLinus Torvalds * - In nfs_file_read the return value of kmalloc wasn't checked. 101da177e4SLinus Torvalds * - Put in a better version of read look-ahead buffering. Original idea 111da177e4SLinus Torvalds * and implementation by Wai S Kok elekokws@ee.nus.sg. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Expire cache on write to a file by Wai S Kok (Oct 1994). 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Total rewrite of read side for new NFS buffer cache.. Linus. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * nfs regular file handling functions 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 20ddda8e0aSBryan Schumaker #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/time.h> 221da177e4SLinus Torvalds #include <linux/kernel.h> 231da177e4SLinus Torvalds #include <linux/errno.h> 241da177e4SLinus Torvalds #include <linux/fcntl.h> 251da177e4SLinus Torvalds #include <linux/stat.h> 261da177e4SLinus Torvalds #include <linux/nfs_fs.h> 271da177e4SLinus Torvalds #include <linux/nfs_mount.h> 281da177e4SLinus Torvalds #include <linux/mm.h> 291da177e4SLinus Torvalds #include <linux/pagemap.h> 305a0e3ad6STejun Heo #include <linux/gfp.h> 31b608b283STrond Myklebust #include <linux/swap.h> 321da177e4SLinus Torvalds 337c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 345970e15dSJeff Layton #include <linux/filelock.h> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include "delegation.h" 3794387fb1STrond Myklebust #include "internal.h" 3891d5b470SChuck Lever #include "iostat.h" 39545db45fSDavid Howells #include "fscache.h" 40612aa983SChristoph Hellwig #include "pnfs.h" 411da177e4SLinus Torvalds 42f4ce1299STrond Myklebust #include "nfstrace.h" 43f4ce1299STrond Myklebust 441da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_FILE 451da177e4SLinus Torvalds 46f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops; 4794387fb1STrond Myklebust 48ce4ef7c0SBryan Schumaker int nfs_check_flags(int flags) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 511da177e4SLinus Torvalds return -EINVAL; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds return 0; 541da177e4SLinus Torvalds } 5589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_check_flags); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * Open file 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds static int 611da177e4SLinus Torvalds nfs_file_open(struct inode *inode, struct file *filp) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds int res; 641da177e4SLinus Torvalds 656de1472fSAl Viro dprintk("NFS: open file(%pD2)\n", filp); 66cc0dd2d1SChuck Lever 67c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSOPEN); 681da177e4SLinus Torvalds res = nfs_check_flags(filp->f_flags); 691da177e4SLinus Torvalds if (res) 701da177e4SLinus Torvalds return res; 711da177e4SLinus Torvalds 7246cb650cSTrond Myklebust res = nfs_open(inode, filp); 73a2ad63daSNeilBrown if (res == 0) 74a2ad63daSNeilBrown filp->f_mode |= FMODE_CAN_ODIRECT; 751da177e4SLinus Torvalds return res; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 78ce4ef7c0SBryan Schumaker int 791da177e4SLinus Torvalds nfs_file_release(struct inode *inode, struct file *filp) 801da177e4SLinus Torvalds { 816de1472fSAl Viro dprintk("NFS: release(%pD2)\n", filp); 826da24bc9SChuck Lever 8391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 84aff8d8dcSAnna Schumaker nfs_file_clear_open_context(filp); 85a6b5a28eSDave Wysochanski nfs_fscache_release_file(inode, filp); 86aff8d8dcSAnna Schumaker return 0; 871da177e4SLinus Torvalds } 8889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_release); 891da177e4SLinus Torvalds 90980802e3STrond Myklebust /** 9137eaeed1STrond Myklebust * nfs_revalidate_file_size - Revalidate the file size 92302fad7bSTrond Myklebust * @inode: pointer to inode struct 93302fad7bSTrond Myklebust * @filp: 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); 104d7cf8dd0STrond Myklebust 105980802e3STrond Myklebust if (filp->f_flags & O_DIRECT) 106980802e3STrond Myklebust goto force_reval; 10713c0b082STrond Myklebust if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE)) 108d7cf8dd0STrond Myklebust goto force_reval; 109fe51beecSTrond Myklebust return 0; 110980802e3STrond Myklebust force_reval: 111980802e3STrond Myklebust return __nfs_revalidate_inode(server, inode); 112980802e3STrond Myklebust } 113980802e3STrond Myklebust 114965c8e59SAndrew Morton loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence) 115980802e3STrond Myklebust { 1166de1472fSAl Viro dprintk("NFS: llseek file(%pD2, %lld, %d)\n", 1176de1472fSAl Viro filp, offset, whence); 118b84e06c5SChuck Lever 11906222e49SJosef Bacik /* 120965c8e59SAndrew Morton * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 12106222e49SJosef Bacik * the cached file length 12206222e49SJosef Bacik */ 123965c8e59SAndrew Morton if (whence != SEEK_SET && whence != SEEK_CUR) { 124980802e3STrond Myklebust struct inode *inode = filp->f_mapping->host; 125d5e66348STrond Myklebust 126980802e3STrond Myklebust int retval = nfs_revalidate_file_size(inode, filp); 127980802e3STrond Myklebust if (retval < 0) 128980802e3STrond Myklebust return (loff_t)retval; 12979835a71SAndi Kleen } 130d5e66348STrond Myklebust 131965c8e59SAndrew Morton return generic_file_llseek(filp, offset, whence); 132980802e3STrond Myklebust } 13389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_llseek); 134980802e3STrond Myklebust 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Flush all dirty pages, and check for write errors. 1371da177e4SLinus Torvalds */ 1385445b1fbSTrond Myklebust static int 13975e1fcc0SMiklos Szeredi nfs_file_flush(struct file *file, fl_owner_t id) 1401da177e4SLinus Torvalds { 1416de1472fSAl Viro struct inode *inode = file_inode(file); 14267dd23f9SScott Mayhew errseq_t since; 1431da177e4SLinus Torvalds 1446de1472fSAl Viro dprintk("NFS: flush(%pD2)\n", file); 1451da177e4SLinus Torvalds 146c2459dc4SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 1471da177e4SLinus Torvalds if ((file->f_mode & FMODE_WRITE) == 0) 1481da177e4SLinus Torvalds return 0; 1497b159fc1STrond Myklebust 1507fe5c398STrond Myklebust /* Flush writes to the server and return any errors */ 15167dd23f9SScott Mayhew since = filemap_sample_wb_err(file->f_mapping); 15267dd23f9SScott Mayhew nfs_wb_all(inode); 15367dd23f9SScott Mayhew return filemap_check_wb_err(file->f_mapping, since); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 156ce4ef7c0SBryan Schumaker ssize_t 1573aa2d199SAl Viro nfs_file_read(struct kiocb *iocb, struct iov_iter *to) 1581da177e4SLinus Torvalds { 1596de1472fSAl Viro struct inode *inode = file_inode(iocb->ki_filp); 1601da177e4SLinus Torvalds ssize_t result; 1611da177e4SLinus Torvalds 1622ba48ce5SAl Viro if (iocb->ki_flags & IOCB_DIRECT) 16364158668SNeilBrown return nfs_file_direct_read(iocb, to, false); 1641da177e4SLinus Torvalds 165619d30b4SAl Viro dprintk("NFS: read(%pD2, %zu@%lu)\n", 1666de1472fSAl Viro iocb->ki_filp, 1673aa2d199SAl Viro iov_iter_count(to), (unsigned long) iocb->ki_pos); 1681da177e4SLinus Torvalds 169a5864c99STrond Myklebust nfs_start_io_read(inode); 170a5864c99STrond Myklebust result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 1714184dcf2SChuck Lever if (!result) { 1723aa2d199SAl Viro result = generic_file_read_iter(iocb, to); 1734184dcf2SChuck Lever if (result > 0) 1744184dcf2SChuck Lever nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 1754184dcf2SChuck Lever } 176a5864c99STrond Myklebust nfs_end_io_read(inode); 1771da177e4SLinus Torvalds return result; 1781da177e4SLinus Torvalds } 17989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_read); 1801da177e4SLinus Torvalds 181*a7db5034SDavid Howells ssize_t 182*a7db5034SDavid Howells nfs_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, 183*a7db5034SDavid Howells size_t len, unsigned int flags) 184*a7db5034SDavid Howells { 185*a7db5034SDavid Howells struct inode *inode = file_inode(in); 186*a7db5034SDavid Howells ssize_t result; 187*a7db5034SDavid Howells 188*a7db5034SDavid Howells dprintk("NFS: splice_read(%pD2, %zu@%llu)\n", in, len, *ppos); 189*a7db5034SDavid Howells 190*a7db5034SDavid Howells nfs_start_io_read(inode); 191*a7db5034SDavid Howells result = nfs_revalidate_mapping(inode, in->f_mapping); 192*a7db5034SDavid Howells if (!result) { 193*a7db5034SDavid Howells result = filemap_splice_read(in, ppos, pipe, len, flags); 194*a7db5034SDavid Howells if (result > 0) 195*a7db5034SDavid Howells nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 196*a7db5034SDavid Howells } 197*a7db5034SDavid Howells nfs_end_io_read(inode); 198*a7db5034SDavid Howells return result; 199*a7db5034SDavid Howells } 200*a7db5034SDavid Howells EXPORT_SYMBOL_GPL(nfs_file_splice_read); 201*a7db5034SDavid Howells 202ce4ef7c0SBryan Schumaker int 2031da177e4SLinus Torvalds nfs_file_mmap(struct file * file, struct vm_area_struct * vma) 2041da177e4SLinus Torvalds { 2056de1472fSAl Viro struct inode *inode = file_inode(file); 2061da177e4SLinus Torvalds int status; 2071da177e4SLinus Torvalds 2086de1472fSAl Viro dprintk("NFS: mmap(%pD2)\n", file); 2091da177e4SLinus Torvalds 210e1ebfd33STrond Myklebust /* Note: generic_file_mmap() returns ENOSYS on nommu systems 211e1ebfd33STrond Myklebust * so we call that before revalidating the mapping 212e1ebfd33STrond Myklebust */ 213e1ebfd33STrond Myklebust status = generic_file_mmap(file, vma); 21494387fb1STrond Myklebust if (!status) { 21594387fb1STrond Myklebust vma->vm_ops = &nfs_file_vm_ops; 216e1ebfd33STrond Myklebust status = nfs_revalidate_mapping(inode, file->f_mapping); 21794387fb1STrond Myklebust } 2181da177e4SLinus Torvalds return status; 2191da177e4SLinus Torvalds } 22089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_mmap); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 2231da177e4SLinus Torvalds * Flush any dirty pages for this process, and check for write errors. 2241da177e4SLinus Torvalds * The return status from this call provides a reliable indication of 2251da177e4SLinus Torvalds * whether any write errors occurred for this process. 2261da177e4SLinus Torvalds */ 2274ff79bc7SChristoph Hellwig static int 228bf4b4905SNeilBrown nfs_file_fsync_commit(struct file *file, int datasync) 2291da177e4SLinus Torvalds { 2306de1472fSAl Viro struct inode *inode = file_inode(file); 2319641d9bcSTrond Myklebust int ret, ret2; 232af7fa165STrond Myklebust 2336de1472fSAl Viro dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync); 2341da177e4SLinus Torvalds 23591d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 2362197e9b0STrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 2379641d9bcSTrond Myklebust ret2 = file_check_and_advance_wb_err(file); 2389641d9bcSTrond Myklebust if (ret2 < 0) 2399641d9bcSTrond Myklebust return ret2; 240a5c58892SBryan Schumaker return ret; 241a5c58892SBryan Schumaker } 242a5c58892SBryan Schumaker 2434ff79bc7SChristoph Hellwig int 244a5c58892SBryan Schumaker nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 245a5c58892SBryan Schumaker { 246496ad9aaSAl Viro struct inode *inode = file_inode(file); 24767f4b5dcSTrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 24867f4b5dcSTrond Myklebust long save_nredirtied = atomic_long_read(&nfsi->redirtied_pages); 24967f4b5dcSTrond Myklebust long nredirtied; 2502197e9b0STrond Myklebust int ret; 251a5c58892SBryan Schumaker 252f4ce1299STrond Myklebust trace_nfs_fsync_enter(inode); 253f4ce1299STrond Myklebust 2542197e9b0STrond Myklebust for (;;) { 2556fbda89bSTrond Myklebust ret = file_write_and_wait_range(file, start, end); 2567b281ee0STrond Myklebust if (ret != 0) 25705990d1bSTrond Myklebust break; 258bf4b4905SNeilBrown ret = nfs_file_fsync_commit(file, datasync); 2592197e9b0STrond Myklebust if (ret != 0) 2602197e9b0STrond Myklebust break; 2614ff79bc7SChristoph Hellwig ret = pnfs_sync_inode(inode, !!datasync); 2622197e9b0STrond Myklebust if (ret != 0) 2632197e9b0STrond Myklebust break; 26467f4b5dcSTrond Myklebust nredirtied = atomic_long_read(&nfsi->redirtied_pages); 26567f4b5dcSTrond Myklebust if (nredirtied == save_nredirtied) 2662197e9b0STrond Myklebust break; 26767f4b5dcSTrond Myklebust save_nredirtied = nredirtied; 2682197e9b0STrond Myklebust } 26905990d1bSTrond Myklebust 270f4ce1299STrond Myklebust trace_nfs_fsync_exit(inode, ret); 271af7fa165STrond Myklebust return ret; 2721da177e4SLinus Torvalds } 2734ff79bc7SChristoph Hellwig EXPORT_SYMBOL_GPL(nfs_file_fsync); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 27638c73044SPeter Staubach * Decide whether a read/modify/write cycle may be more efficient 27738c73044SPeter Staubach * then a modify/write/read cycle when writing to a page in the 27838c73044SPeter Staubach * page cache. 27938c73044SPeter Staubach * 2802cde04e9SKazuo Ito * Some pNFS layout drivers can only read/write at a certain block 2812cde04e9SKazuo Ito * granularity like all block devices and therefore we must perform 2822cde04e9SKazuo Ito * read/modify/write whenever a page hasn't read yet and the data 2832cde04e9SKazuo Ito * to be written there is not aligned to a block boundary and/or 2842cde04e9SKazuo Ito * smaller than the block size. 2852cde04e9SKazuo Ito * 28638c73044SPeter Staubach * The modify/write/read cycle may occur if a page is read before 28738c73044SPeter Staubach * being completely filled by the writer. In this situation, the 28838c73044SPeter Staubach * page must be completely written to stable storage on the server 28938c73044SPeter Staubach * before it can be refilled by reading in the page from the server. 29038c73044SPeter Staubach * This can lead to expensive, small, FILE_SYNC mode writes being 29138c73044SPeter Staubach * done. 29238c73044SPeter Staubach * 29338c73044SPeter Staubach * It may be more efficient to read the page first if the file is 29438c73044SPeter Staubach * open for reading in addition to writing, the page is not marked 29538c73044SPeter Staubach * as Uptodate, it is not dirty or waiting to be committed, 29638c73044SPeter Staubach * indicating that it was previously allocated and then modified, 29738c73044SPeter Staubach * that there were valid bytes of data in that range of the file, 29838c73044SPeter Staubach * and that the new data won't completely replace the old data in 29938c73044SPeter Staubach * that range of the file. 30038c73044SPeter Staubach */ 30154d99381STrond Myklebust static bool nfs_folio_is_full_write(struct folio *folio, loff_t pos, 30254d99381STrond Myklebust unsigned int len) 30338c73044SPeter Staubach { 30454d99381STrond Myklebust unsigned int pglen = nfs_folio_length(folio); 30554d99381STrond Myklebust unsigned int offset = offset_in_folio(folio, pos); 30638c73044SPeter Staubach unsigned int end = offset + len; 30738c73044SPeter Staubach 3082cde04e9SKazuo Ito return !pglen || (end >= pglen && !offset); 309612aa983SChristoph Hellwig } 310612aa983SChristoph Hellwig 31154d99381STrond Myklebust static bool nfs_want_read_modify_write(struct file *file, struct folio *folio, 3122cde04e9SKazuo Ito loff_t pos, unsigned int len) 3132cde04e9SKazuo Ito { 3142cde04e9SKazuo Ito /* 3152cde04e9SKazuo Ito * Up-to-date pages, those with ongoing or full-page write 3162cde04e9SKazuo Ito * don't need read/modify/write 3172cde04e9SKazuo Ito */ 31854d99381STrond Myklebust if (folio_test_uptodate(folio) || folio_test_private(folio) || 31954d99381STrond Myklebust nfs_folio_is_full_write(folio, pos, len)) 3202cde04e9SKazuo Ito return false; 3212cde04e9SKazuo Ito 32254d99381STrond Myklebust if (pnfs_ld_read_whole_page(file_inode(file))) 3232cde04e9SKazuo Ito return true; 3242cde04e9SKazuo Ito /* Open for reading too? */ 3252cde04e9SKazuo Ito if (file->f_mode & FMODE_READ) 3262cde04e9SKazuo Ito return true; 3272cde04e9SKazuo Ito return false; 32838c73044SPeter Staubach } 32938c73044SPeter Staubach 33038c73044SPeter Staubach /* 3314899f9c8SNick Piggin * This does the "real" work of the write. We must allocate and lock the 3324899f9c8SNick Piggin * page to be sent back to the generic routine, which then copies the 3334899f9c8SNick Piggin * data from user space. 3341da177e4SLinus Torvalds * 3351da177e4SLinus Torvalds * If the writer ends up delaying the write, the writer needs to 3361da177e4SLinus Torvalds * increment the page use counts until he is done with the page. 3371da177e4SLinus Torvalds */ 3384899f9c8SNick Piggin static int nfs_write_begin(struct file *file, struct address_space *mapping, 33954d99381STrond Myklebust loff_t pos, unsigned len, struct page **pagep, 34054d99381STrond Myklebust void **fsdata) 3411da177e4SLinus Torvalds { 3420c493b5cSTrond Myklebust struct folio *folio; 34338c73044SPeter Staubach int once_thru = 0; 34454d99381STrond Myklebust int ret; 3454899f9c8SNick Piggin 3461e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n", 3476de1472fSAl Viro file, mapping->host->i_ino, len, (long long) pos); 348b7eaefaaSChuck Lever 34938c73044SPeter Staubach start: 350e999a5c5SMatthew Wilcox folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT, FGP_WRITEBEGIN, 351e999a5c5SMatthew Wilcox mapping_gfp_mask(mapping)); 35266dabbb6SChristoph Hellwig if (IS_ERR(folio)) 35366dabbb6SChristoph Hellwig return PTR_ERR(folio); 35454d99381STrond Myklebust *pagep = &folio->page; 3554899f9c8SNick Piggin 3560c493b5cSTrond Myklebust ret = nfs_flush_incompatible(file, folio); 3574899f9c8SNick Piggin if (ret) { 35854d99381STrond Myklebust folio_unlock(folio); 35954d99381STrond Myklebust folio_put(folio); 36038c73044SPeter Staubach } else if (!once_thru && 36154d99381STrond Myklebust nfs_want_read_modify_write(file, folio, pos, len)) { 36238c73044SPeter Staubach once_thru = 1; 3630c493b5cSTrond Myklebust ret = nfs_read_folio(file, folio); 36454d99381STrond Myklebust folio_put(folio); 36538c73044SPeter Staubach if (!ret) 36638c73044SPeter Staubach goto start; 3674899f9c8SNick Piggin } 3684899f9c8SNick Piggin return ret; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3714899f9c8SNick Piggin static int nfs_write_end(struct file *file, struct address_space *mapping, 3724899f9c8SNick Piggin loff_t pos, unsigned len, unsigned copied, 3734899f9c8SNick Piggin struct page *page, void *fsdata) 3741da177e4SLinus Torvalds { 375dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(file); 3760c493b5cSTrond Myklebust struct folio *folio = page_folio(page); 37754d99381STrond Myklebust unsigned offset = offset_in_folio(folio, pos); 3784899f9c8SNick Piggin int status; 3791da177e4SLinus Torvalds 3801e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n", 3816de1472fSAl Viro file, mapping->host->i_ino, len, (long long) pos); 382b7eaefaaSChuck Lever 383efc91ed0STrond Myklebust /* 384efc91ed0STrond Myklebust * Zero any uninitialised parts of the page, and then mark the page 385efc91ed0STrond Myklebust * as up to date if it turns out that we're extending the file. 386efc91ed0STrond Myklebust */ 38754d99381STrond Myklebust if (!folio_test_uptodate(folio)) { 38854d99381STrond Myklebust size_t fsize = folio_size(folio); 38954d99381STrond Myklebust unsigned pglen = nfs_folio_length(folio); 390c0cf3ef5SAl Viro unsigned end = offset + copied; 391efc91ed0STrond Myklebust 392efc91ed0STrond Myklebust if (pglen == 0) { 39354d99381STrond Myklebust folio_zero_segments(folio, 0, offset, end, fsize); 39454d99381STrond Myklebust folio_mark_uptodate(folio); 395efc91ed0STrond Myklebust } else if (end >= pglen) { 39654d99381STrond Myklebust folio_zero_segment(folio, end, fsize); 397efc91ed0STrond Myklebust if (offset == 0) 39854d99381STrond Myklebust folio_mark_uptodate(folio); 399efc91ed0STrond Myklebust } else 40054d99381STrond Myklebust folio_zero_segment(folio, pglen, fsize); 401efc91ed0STrond Myklebust } 402efc91ed0STrond Myklebust 4030c493b5cSTrond Myklebust status = nfs_update_folio(file, folio, offset, copied); 4044899f9c8SNick Piggin 40554d99381STrond Myklebust folio_unlock(folio); 40654d99381STrond Myklebust folio_put(folio); 4074899f9c8SNick Piggin 4083d509e54SChuck Lever if (status < 0) 4093d509e54SChuck Lever return status; 4102701d086SAndy Adamson NFS_I(mapping->host)->write_io += copied; 411dc24826bSAndy Adamson 412d95b2665STrond Myklebust if (nfs_ctx_key_to_expire(ctx, mapping->host)) 413d95b2665STrond Myklebust nfs_wb_all(mapping->host); 414dc24826bSAndy Adamson 4153d509e54SChuck Lever return copied; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4186b9b3514SDavid Howells /* 4196b9b3514SDavid Howells * Partially or wholly invalidate a page 4206b9b3514SDavid Howells * - Release the private state associated with a page if undergoing complete 4216b9b3514SDavid Howells * page invalidation 422545db45fSDavid Howells * - Called if either PG_private or PG_fscache is set on the page 4236b9b3514SDavid Howells * - Caller holds page lock 4246b9b3514SDavid Howells */ 4256d740c76SMatthew Wilcox (Oracle) static void nfs_invalidate_folio(struct folio *folio, size_t offset, 4266d740c76SMatthew Wilcox (Oracle) size_t length) 427cd52ed35STrond Myklebust { 428eb5654b3STrond Myklebust struct inode *inode = folio_file_mapping(folio)->host; 4296d740c76SMatthew Wilcox (Oracle) dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n", 4306d740c76SMatthew Wilcox (Oracle) folio->index, offset, length); 431b7eaefaaSChuck Lever 4326d740c76SMatthew Wilcox (Oracle) if (offset != 0 || length < folio_size(folio)) 4331c75950bSTrond Myklebust return; 434d2ccddf0STrond Myklebust /* Cancel any unstarted writes on this page */ 435eb5654b3STrond Myklebust nfs_wb_folio_cancel(inode, folio); 4366d740c76SMatthew Wilcox (Oracle) folio_wait_fscache(folio); 437eb5654b3STrond Myklebust trace_nfs_invalidate_folio(inode, folio); 438cd52ed35STrond Myklebust } 439cd52ed35STrond Myklebust 4406b9b3514SDavid Howells /* 4413577da4aSMatthew Wilcox (Oracle) * Attempt to release the private state associated with a folio 4423577da4aSMatthew Wilcox (Oracle) * - Called if either private or fscache flags are set on the folio 4433577da4aSMatthew Wilcox (Oracle) * - Caller holds folio lock 4443577da4aSMatthew Wilcox (Oracle) * - Return true (may release folio) or false (may not) 4456b9b3514SDavid Howells */ 4463577da4aSMatthew Wilcox (Oracle) static bool nfs_release_folio(struct folio *folio, gfp_t gfp) 447cd52ed35STrond Myklebust { 4483577da4aSMatthew Wilcox (Oracle) dfprintk(PAGECACHE, "NFS: release_folio(%p)\n", folio); 449b7eaefaaSChuck Lever 4503577da4aSMatthew Wilcox (Oracle) /* If the private flag is set, then the folio is not freeable */ 45196780ca5STrond Myklebust if (folio_test_private(folio)) { 45296780ca5STrond Myklebust if ((current_gfp_context(gfp) & GFP_KERNEL) != GFP_KERNEL || 45396780ca5STrond Myklebust current_is_kswapd()) 4543577da4aSMatthew Wilcox (Oracle) return false; 45596780ca5STrond Myklebust if (nfs_wb_folio(folio_file_mapping(folio)->host, folio) < 0) 45696780ca5STrond Myklebust return false; 45796780ca5STrond Myklebust } 4583577da4aSMatthew Wilcox (Oracle) return nfs_fscache_release_folio(folio, gfp); 459e3db7691STrond Myklebust } 460e3db7691STrond Myklebust 461520f301cSMatthew Wilcox (Oracle) static void nfs_check_dirty_writeback(struct folio *folio, 462f919b196SMel Gorman bool *dirty, bool *writeback) 463f919b196SMel Gorman { 464f919b196SMel Gorman struct nfs_inode *nfsi; 465520f301cSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 466f919b196SMel Gorman 467f919b196SMel Gorman /* 468520f301cSMatthew Wilcox (Oracle) * Check if an unstable folio is currently being committed and 469520f301cSMatthew Wilcox (Oracle) * if so, have the VM treat it as if the folio is under writeback 470520f301cSMatthew Wilcox (Oracle) * so it will not block due to folios that will shortly be freeable. 471f919b196SMel Gorman */ 472f919b196SMel Gorman nfsi = NFS_I(mapping->host); 473af7cf057STrond Myklebust if (atomic_read(&nfsi->commit_info.rpcs_out)) { 474f919b196SMel Gorman *writeback = true; 475f919b196SMel Gorman return; 476f919b196SMel Gorman } 477f919b196SMel Gorman 478f919b196SMel Gorman /* 479520f301cSMatthew Wilcox (Oracle) * If the private flag is set, then the folio is not freeable 480520f301cSMatthew Wilcox (Oracle) * and as the inode is not being committed, it's not going to 481520f301cSMatthew Wilcox (Oracle) * be cleaned in the near future so treat it as dirty 482f919b196SMel Gorman */ 483520f301cSMatthew Wilcox (Oracle) if (folio_test_private(folio)) 484f919b196SMel Gorman *dirty = true; 485f919b196SMel Gorman } 486f919b196SMel Gorman 4876b9b3514SDavid Howells /* 4886b9b3514SDavid Howells * Attempt to clear the private state associated with a page when an error 4896b9b3514SDavid Howells * occurs that requires the cached contents of an inode to be written back or 4906b9b3514SDavid Howells * destroyed 491545db45fSDavid Howells * - Called if either PG_private or fscache is set on the page 4926b9b3514SDavid Howells * - Caller holds page lock 4936b9b3514SDavid Howells * - Return 0 if successful, -error otherwise 4946b9b3514SDavid Howells */ 49515a30ab2SMatthew Wilcox (Oracle) static int nfs_launder_folio(struct folio *folio) 496e3db7691STrond Myklebust { 49715a30ab2SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host; 498eb5654b3STrond Myklebust int ret; 499b7eaefaaSChuck Lever 50015a30ab2SMatthew Wilcox (Oracle) dfprintk(PAGECACHE, "NFS: launder_folio(%ld, %llu)\n", 50115a30ab2SMatthew Wilcox (Oracle) inode->i_ino, folio_pos(folio)); 502b7eaefaaSChuck Lever 50315a30ab2SMatthew Wilcox (Oracle) folio_wait_fscache(folio); 504eb5654b3STrond Myklebust ret = nfs_wb_folio(inode, folio); 505eb5654b3STrond Myklebust trace_nfs_launder_folio_done(inode, folio, ret); 506eb5654b3STrond Myklebust return ret; 507cd52ed35STrond Myklebust } 508cd52ed35STrond Myklebust 509a564b8f0SMel Gorman static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file, 510a564b8f0SMel Gorman sector_t *span) 511a564b8f0SMel Gorman { 512bd89bc67SMurphy Zhou unsigned long blocks; 513bd89bc67SMurphy Zhou long long isize; 5144b60c0ffSNeilBrown int ret; 5154dc73c67SNeilBrown struct inode *inode = file_inode(file); 5164dc73c67SNeilBrown struct rpc_clnt *clnt = NFS_CLIENT(inode); 5174dc73c67SNeilBrown struct nfs_client *cl = NFS_SERVER(inode)->nfs_client; 518bd89bc67SMurphy Zhou 519bd89bc67SMurphy Zhou spin_lock(&inode->i_lock); 520bd89bc67SMurphy Zhou blocks = inode->i_blocks; 521bd89bc67SMurphy Zhou isize = inode->i_size; 522bd89bc67SMurphy Zhou spin_unlock(&inode->i_lock); 523bd89bc67SMurphy Zhou if (blocks*512 < isize) { 524bd89bc67SMurphy Zhou pr_warn("swap activate: swapfile has holes\n"); 525bd89bc67SMurphy Zhou return -EINVAL; 526bd89bc67SMurphy Zhou } 527dad2b015SJeff Layton 5284b60c0ffSNeilBrown ret = rpc_clnt_swap_activate(clnt); 5294b60c0ffSNeilBrown if (ret) 5304b60c0ffSNeilBrown return ret; 5314b60c0ffSNeilBrown ret = add_swap_extent(sis, 0, sis->max, 0); 5324b60c0ffSNeilBrown if (ret < 0) { 5334b60c0ffSNeilBrown rpc_clnt_swap_deactivate(clnt); 5344b60c0ffSNeilBrown return ret; 5354b60c0ffSNeilBrown } 536dad2b015SJeff Layton 5374b60c0ffSNeilBrown *span = sis->pages; 5384dc73c67SNeilBrown 5394dc73c67SNeilBrown if (cl->rpc_ops->enable_swap) 5404dc73c67SNeilBrown cl->rpc_ops->enable_swap(inode); 5414dc73c67SNeilBrown 5424b60c0ffSNeilBrown sis->flags |= SWP_FS_OPS; 5434b60c0ffSNeilBrown return ret; 544a564b8f0SMel Gorman } 545a564b8f0SMel Gorman 546a564b8f0SMel Gorman static void nfs_swap_deactivate(struct file *file) 547a564b8f0SMel Gorman { 5484dc73c67SNeilBrown struct inode *inode = file_inode(file); 5494dc73c67SNeilBrown struct rpc_clnt *clnt = NFS_CLIENT(inode); 5504dc73c67SNeilBrown struct nfs_client *cl = NFS_SERVER(inode)->nfs_client; 551dad2b015SJeff Layton 5523c87ef6eSJeff Layton rpc_clnt_swap_deactivate(clnt); 5534dc73c67SNeilBrown if (cl->rpc_ops->disable_swap) 5544dc73c67SNeilBrown cl->rpc_ops->disable_swap(file_inode(file)); 555a564b8f0SMel Gorman } 556a564b8f0SMel Gorman 557f5e54d6eSChristoph Hellwig const struct address_space_operations nfs_file_aops = { 55865d023afSMatthew Wilcox (Oracle) .read_folio = nfs_read_folio, 5598786fde8SMatthew Wilcox (Oracle) .readahead = nfs_readahead, 560187c82cbSMatthew Wilcox (Oracle) .dirty_folio = filemap_dirty_folio, 5611da177e4SLinus Torvalds .writepage = nfs_writepage, 5621da177e4SLinus Torvalds .writepages = nfs_writepages, 5634899f9c8SNick Piggin .write_begin = nfs_write_begin, 5644899f9c8SNick Piggin .write_end = nfs_write_end, 5656d740c76SMatthew Wilcox (Oracle) .invalidate_folio = nfs_invalidate_folio, 5663577da4aSMatthew Wilcox (Oracle) .release_folio = nfs_release_folio, 5674ae84a80SMatthew Wilcox (Oracle) .migrate_folio = nfs_migrate_folio, 56815a30ab2SMatthew Wilcox (Oracle) .launder_folio = nfs_launder_folio, 569f919b196SMel Gorman .is_dirty_writeback = nfs_check_dirty_writeback, 570f590f333SAndi Kleen .error_remove_page = generic_error_remove_page, 571a564b8f0SMel Gorman .swap_activate = nfs_swap_activate, 572a564b8f0SMel Gorman .swap_deactivate = nfs_swap_deactivate, 573eb79f3afSNeilBrown .swap_rw = nfs_swap_rw, 5741da177e4SLinus Torvalds }; 5751da177e4SLinus Torvalds 5766b9b3514SDavid Howells /* 5776b9b3514SDavid Howells * Notification that a PTE pointing to an NFS page is about to be made 5786b9b3514SDavid Howells * writable, implying that someone is about to modify the page through a 5796b9b3514SDavid Howells * shared-writable mapping 5806b9b3514SDavid Howells */ 58101a36844SSouptick Joarder static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf) 58294387fb1STrond Myklebust { 58311bac800SDave Jiang struct file *filp = vmf->vma->vm_file; 5846de1472fSAl Viro struct inode *inode = file_inode(filp); 58594387fb1STrond Myklebust unsigned pagelen; 58601a36844SSouptick Joarder vm_fault_t ret = VM_FAULT_NOPAGE; 5874899f9c8SNick Piggin struct address_space *mapping; 5884fa7a717STrond Myklebust struct folio *folio = page_folio(vmf->page); 58994387fb1STrond Myklebust 5901e8968c5SNiels de Vos dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n", 5916de1472fSAl Viro filp, filp->f_mapping->host->i_ino, 5924fa7a717STrond Myklebust (long long)folio_file_pos(folio)); 593b7eaefaaSChuck Lever 5949a773e7cSTrond Myklebust sb_start_pagefault(inode->i_sb); 5959a773e7cSTrond Myklebust 596545db45fSDavid Howells /* make sure the cache has finished storing the page */ 5974fa7a717STrond Myklebust if (folio_test_fscache(folio) && 5984fa7a717STrond Myklebust folio_wait_fscache_killable(folio) < 0) { 599a6b5a28eSDave Wysochanski ret = VM_FAULT_RETRY; 600a6b5a28eSDave Wysochanski goto out; 601a6b5a28eSDave Wysochanski } 602545db45fSDavid Howells 603ef070dcbSTrond Myklebust wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING, 604f5d39b02SPeter Zijlstra nfs_wait_bit_killable, 605f5d39b02SPeter Zijlstra TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); 606ef070dcbSTrond Myklebust 6074fa7a717STrond Myklebust folio_lock(folio); 6084fa7a717STrond Myklebust mapping = folio_file_mapping(folio); 6096de1472fSAl Viro if (mapping != inode->i_mapping) 6108b1f9ee5STrond Myklebust goto out_unlock; 6118b1f9ee5STrond Myklebust 6124fa7a717STrond Myklebust folio_wait_writeback(folio); 6132aeb98f4STrond Myklebust 6144fa7a717STrond Myklebust pagelen = nfs_folio_length(folio); 6158b1f9ee5STrond Myklebust if (pagelen == 0) 6168b1f9ee5STrond Myklebust goto out_unlock; 6178b1f9ee5STrond Myklebust 618bc4866b6STrond Myklebust ret = VM_FAULT_LOCKED; 6190c493b5cSTrond Myklebust if (nfs_flush_incompatible(filp, folio) == 0 && 6200c493b5cSTrond Myklebust nfs_update_folio(filp, folio, 0, pagelen) == 0) 621bc4866b6STrond Myklebust goto out; 6228b1f9ee5STrond Myklebust 623bc4866b6STrond Myklebust ret = VM_FAULT_SIGBUS; 6248b1f9ee5STrond Myklebust out_unlock: 6254fa7a717STrond Myklebust folio_unlock(folio); 626bc4866b6STrond Myklebust out: 6279a773e7cSTrond Myklebust sb_end_pagefault(inode->i_sb); 628bc4866b6STrond Myklebust return ret; 62994387fb1STrond Myklebust } 63094387fb1STrond Myklebust 631f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct nfs_file_vm_ops = { 63294387fb1STrond Myklebust .fault = filemap_fault, 633f1820361SKirill A. Shutemov .map_pages = filemap_map_pages, 63494387fb1STrond Myklebust .page_mkwrite = nfs_vm_page_mkwrite, 63594387fb1STrond Myklebust }; 63694387fb1STrond Myklebust 637edaf4369SAl Viro ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from) 6381da177e4SLinus Torvalds { 6396de1472fSAl Viro struct file *file = iocb->ki_filp; 6406de1472fSAl Viro struct inode *inode = file_inode(file); 641ed7bcdb3STrond Myklebust unsigned int mntflags = NFS_SERVER(inode)->flags; 642ed7bcdb3STrond Myklebust ssize_t result, written; 643ce368536SScott Mayhew errseq_t since; 644ce368536SScott Mayhew int error; 6451da177e4SLinus Torvalds 6466de1472fSAl Viro result = nfs_key_timeout_notify(file, inode); 647dc24826bSAndy Adamson if (result) 648dc24826bSAndy Adamson return result; 649dc24826bSAndy Adamson 65089698b24STrond Myklebust if (iocb->ki_flags & IOCB_DIRECT) 65164158668SNeilBrown return nfs_file_direct_write(iocb, from, false); 6521da177e4SLinus Torvalds 653619d30b4SAl Viro dprintk("NFS: write(%pD2, %zu@%Ld)\n", 65418290650STrond Myklebust file, iov_iter_count(from), (long long) iocb->ki_pos); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) 6571da177e4SLinus Torvalds goto out_swapfile; 6587d52e862STrond Myklebust /* 6597d52e862STrond Myklebust * O_APPEND implies that we must revalidate the file length. 6607d52e862STrond Myklebust */ 661fc9dc401STrond Myklebust if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) { 6626de1472fSAl Viro result = nfs_revalidate_file_size(inode, file); 6631da177e4SLinus Torvalds if (result) 664e6005436STrond Myklebust return result; 665fe51beecSTrond Myklebust } 6661da177e4SLinus Torvalds 66728aa2f9eSTrond Myklebust nfs_clear_invalid_mapping(file->f_mapping); 66828aa2f9eSTrond Myklebust 669ce368536SScott Mayhew since = filemap_sample_wb_err(file->f_mapping); 670a5864c99STrond Myklebust nfs_start_io_write(inode); 67118290650STrond Myklebust result = generic_write_checks(iocb, from); 67218290650STrond Myklebust if (result > 0) { 67318290650STrond Myklebust current->backing_dev_info = inode_to_bdi(inode); 674800ba295SMatthew Wilcox (Oracle) result = generic_perform_write(iocb, from); 67518290650STrond Myklebust current->backing_dev_info = NULL; 67618290650STrond Myklebust } 677a5864c99STrond Myklebust nfs_end_io_write(inode); 67818290650STrond Myklebust if (result <= 0) 6791da177e4SLinus Torvalds goto out; 6801da177e4SLinus Torvalds 681c49edecdSTrond Myklebust written = result; 68218290650STrond Myklebust iocb->ki_pos += written; 683e6005436STrond Myklebust nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 684ed7bcdb3STrond Myklebust 685ed7bcdb3STrond Myklebust if (mntflags & NFS_MOUNT_WRITE_EAGER) { 686ed7bcdb3STrond Myklebust result = filemap_fdatawrite_range(file->f_mapping, 687ed7bcdb3STrond Myklebust iocb->ki_pos - written, 688ed7bcdb3STrond Myklebust iocb->ki_pos - 1); 689ed7bcdb3STrond Myklebust if (result < 0) 690ed7bcdb3STrond Myklebust goto out; 691ed7bcdb3STrond Myklebust } 692ed7bcdb3STrond Myklebust if (mntflags & NFS_MOUNT_WRITE_WAIT) { 693384edeb4SLukas Bulwahn filemap_fdatawait_range(file->f_mapping, 694ed7bcdb3STrond Myklebust iocb->ki_pos - written, 695ed7bcdb3STrond Myklebust iocb->ki_pos - 1); 696ed7bcdb3STrond Myklebust } 697e973b1a5Starangg@amazon.com result = generic_write_sync(iocb, written); 698e973b1a5Starangg@amazon.com if (result < 0) 699e6005436STrond Myklebust return result; 7007e381172SChuck Lever 701e6005436STrond Myklebust out: 7027e94d6c4STrond Myklebust /* Return error values */ 703ce368536SScott Mayhew error = filemap_check_wb_err(file->f_mapping, since); 704e6005436STrond Myklebust switch (error) { 705e6005436STrond Myklebust default: 706e6005436STrond Myklebust break; 707e6005436STrond Myklebust case -EDQUOT: 708e6005436STrond Myklebust case -EFBIG: 709e6005436STrond Myklebust case -ENOSPC: 710e6005436STrond Myklebust nfs_wb_all(inode); 711e6005436STrond Myklebust error = file_check_and_advance_wb_err(file); 712e6005436STrond Myklebust if (error < 0) 713e6005436STrond Myklebust result = error; 714200baa21STrond Myklebust } 7151da177e4SLinus Torvalds return result; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds out_swapfile: 7181da177e4SLinus Torvalds printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 71989658c4dSAnna Schumaker return -ETXTBSY; 7201da177e4SLinus Torvalds } 72189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_write); 7221da177e4SLinus Torvalds 7235eebde23SSuresh Jayaraman static int 7245eebde23SSuresh Jayaraman do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7271da177e4SLinus Torvalds int status = 0; 72821ac19d4SSergey Vlasov unsigned int saved_type = fl->fl_type; 7291da177e4SLinus Torvalds 730039c4d7aSTrond Myklebust /* Try local locking first */ 7316d34ac19SJ. Bruce Fields posix_test_lock(filp, fl); 7326d34ac19SJ. Bruce Fields if (fl->fl_type != F_UNLCK) { 7336d34ac19SJ. Bruce Fields /* found a conflict */ 734039c4d7aSTrond Myklebust goto out; 7351da177e4SLinus Torvalds } 73621ac19d4SSergey Vlasov fl->fl_type = saved_type; 737039c4d7aSTrond Myklebust 738011e2a7fSBryan Schumaker if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 739039c4d7aSTrond Myklebust goto out_noconflict; 740039c4d7aSTrond Myklebust 7415eebde23SSuresh Jayaraman if (is_local) 742039c4d7aSTrond Myklebust goto out_noconflict; 743039c4d7aSTrond Myklebust 744039c4d7aSTrond Myklebust status = NFS_PROTO(inode)->lock(filp, cmd, fl); 745039c4d7aSTrond Myklebust out: 7461da177e4SLinus Torvalds return status; 747039c4d7aSTrond Myklebust out_noconflict: 748039c4d7aSTrond Myklebust fl->fl_type = F_UNLCK; 749039c4d7aSTrond Myklebust goto out; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7525eebde23SSuresh Jayaraman static int 7535eebde23SSuresh Jayaraman do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7567a8203d8STrond Myklebust struct nfs_lock_context *l_ctx; 7571da177e4SLinus Torvalds int status; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * Flush all pending writes before doing anything 7611da177e4SLinus Torvalds * with locks.. 7621da177e4SLinus Torvalds */ 763aded8d7bSTrond Myklebust nfs_wb_all(inode); 7641da177e4SLinus Torvalds 7657a8203d8STrond Myklebust l_ctx = nfs_get_lock_context(nfs_file_open_context(filp)); 7667a8203d8STrond Myklebust if (!IS_ERR(l_ctx)) { 767210c7c17SBenjamin Coddington status = nfs_iocounter_wait(l_ctx); 7687a8203d8STrond Myklebust nfs_put_lock_context(l_ctx); 7691da177e4SLinus Torvalds /* NOTE: special case 7701da177e4SLinus Torvalds * If we're signalled while cleaning up locks on process exit, we 7711da177e4SLinus Torvalds * still need to complete the unlock. 7721da177e4SLinus Torvalds */ 773f30cb757SBenjamin Coddington if (status < 0 && !(fl->fl_flags & FL_CLOSE)) 774f30cb757SBenjamin Coddington return status; 775f30cb757SBenjamin Coddington } 776f30cb757SBenjamin Coddington 7775eebde23SSuresh Jayaraman /* 7785eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 7795eebde23SSuresh Jayaraman * "-olocal_lock=" 7805eebde23SSuresh Jayaraman */ 7815eebde23SSuresh Jayaraman if (!is_local) 7821da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 7831da177e4SLinus Torvalds else 78475575ddfSJeff Layton status = locks_lock_file_wait(filp, fl); 7851da177e4SLinus Torvalds return status; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7885eebde23SSuresh Jayaraman static int 7895eebde23SSuresh Jayaraman do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 7921da177e4SLinus Torvalds int status; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Flush all pending writes before doing anything 7961da177e4SLinus Torvalds * with locks.. 7971da177e4SLinus Torvalds */ 79829884df0STrond Myklebust status = nfs_sync_mapping(filp->f_mapping); 79929884df0STrond Myklebust if (status != 0) 8001da177e4SLinus Torvalds goto out; 8011da177e4SLinus Torvalds 8025eebde23SSuresh Jayaraman /* 8035eebde23SSuresh Jayaraman * Use local locking if mounted with "-onolock" or with appropriate 8045eebde23SSuresh Jayaraman * "-olocal_lock=" 8055eebde23SSuresh Jayaraman */ 8065eebde23SSuresh Jayaraman if (!is_local) 8071da177e4SLinus Torvalds status = NFS_PROTO(inode)->lock(filp, cmd, fl); 808c4d7c402STrond Myklebust else 80975575ddfSJeff Layton status = locks_lock_file_wait(filp, fl); 8101da177e4SLinus Torvalds if (status < 0) 8111da177e4SLinus Torvalds goto out; 8126b96724eSRicardo Labiaga 8131da177e4SLinus Torvalds /* 814779eafabSNeilBrown * Invalidate cache to prevent missing any changes. If 815779eafabSNeilBrown * the file is mapped, clear the page cache as well so 816779eafabSNeilBrown * those mappings will be loaded. 8176b96724eSRicardo Labiaga * 8181da177e4SLinus Torvalds * This makes locking act as a cache coherency point. 8191da177e4SLinus Torvalds */ 82029884df0STrond Myklebust nfs_sync_mapping(filp->f_mapping); 821779eafabSNeilBrown if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) { 822442ce049SNeilBrown nfs_zap_caches(inode); 823779eafabSNeilBrown if (mapping_mapped(filp->f_mapping)) 824779eafabSNeilBrown nfs_revalidate_mapping(inode, filp->f_mapping); 825779eafabSNeilBrown } 8261da177e4SLinus Torvalds out: 8271da177e4SLinus Torvalds return status; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* 8311da177e4SLinus Torvalds * Lock a (portion of) a file 8321da177e4SLinus Torvalds */ 833ce4ef7c0SBryan Schumaker int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds struct inode *inode = filp->f_mapping->host; 8362116271aSTrond Myklebust int ret = -ENOLCK; 8375eebde23SSuresh Jayaraman int is_local = 0; 8381da177e4SLinus Torvalds 8396de1472fSAl Viro dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n", 8406de1472fSAl Viro filp, fl->fl_type, fl->fl_flags, 8411da177e4SLinus Torvalds (long long)fl->fl_start, (long long)fl->fl_end); 8426da24bc9SChuck Lever 84391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSLOCK); 8441da177e4SLinus Torvalds 845bb0a55bbSJ. Bruce Fields if (fl->fl_flags & FL_RECLAIM) 846bb0a55bbSJ. Bruce Fields return -ENOGRACE; 847bb0a55bbSJ. Bruce Fields 8485eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 8495eebde23SSuresh Jayaraman is_local = 1; 8505eebde23SSuresh Jayaraman 8512116271aSTrond Myklebust if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 8522116271aSTrond Myklebust ret = NFS_PROTO(inode)->lock_check_bounds(fl); 8532116271aSTrond Myklebust if (ret < 0) 8542116271aSTrond Myklebust goto out_err; 8552116271aSTrond Myklebust } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds if (IS_GETLK(cmd)) 8585eebde23SSuresh Jayaraman ret = do_getlk(filp, cmd, fl, is_local); 8592116271aSTrond Myklebust else if (fl->fl_type == F_UNLCK) 8605eebde23SSuresh Jayaraman ret = do_unlk(filp, cmd, fl, is_local); 8612116271aSTrond Myklebust else 8625eebde23SSuresh Jayaraman ret = do_setlk(filp, cmd, fl, is_local); 8632116271aSTrond Myklebust out_err: 8642116271aSTrond Myklebust return ret; 8651da177e4SLinus Torvalds } 86689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lock); 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds /* 8691da177e4SLinus Torvalds * Lock a (portion of) a file 8701da177e4SLinus Torvalds */ 871ce4ef7c0SBryan Schumaker int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 8721da177e4SLinus Torvalds { 8735eebde23SSuresh Jayaraman struct inode *inode = filp->f_mapping->host; 8745eebde23SSuresh Jayaraman int is_local = 0; 8755eebde23SSuresh Jayaraman 8766de1472fSAl Viro dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n", 8776de1472fSAl Viro filp, fl->fl_type, fl->fl_flags); 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds if (!(fl->fl_flags & FL_FLOCK)) 8801da177e4SLinus Torvalds return -ENOLCK; 8811da177e4SLinus Torvalds 8825eebde23SSuresh Jayaraman if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 8835eebde23SSuresh Jayaraman is_local = 1; 8845eebde23SSuresh Jayaraman 885fcfa4470SBenjamin Coddington /* We're simulating flock() locks using posix locks on the server */ 886fcfa4470SBenjamin Coddington if (fl->fl_type == F_UNLCK) 8875eebde23SSuresh Jayaraman return do_unlk(filp, cmd, fl, is_local); 8885eebde23SSuresh Jayaraman return do_setlk(filp, cmd, fl, is_local); 8891da177e4SLinus Torvalds } 89089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_flock); 891370f6599SJ. Bruce Fields 8920486958fSJeff Layton const struct file_operations nfs_file_operations = { 8930486958fSJeff Layton .llseek = nfs_file_llseek, 8943aa2d199SAl Viro .read_iter = nfs_file_read, 895edaf4369SAl Viro .write_iter = nfs_file_write, 8960486958fSJeff Layton .mmap = nfs_file_mmap, 8970486958fSJeff Layton .open = nfs_file_open, 8980486958fSJeff Layton .flush = nfs_file_flush, 8990486958fSJeff Layton .release = nfs_file_release, 9000486958fSJeff Layton .fsync = nfs_file_fsync, 9010486958fSJeff Layton .lock = nfs_lock, 9020486958fSJeff Layton .flock = nfs_flock, 903*a7db5034SDavid Howells .splice_read = nfs_file_splice_read, 9044da54c21SAl Viro .splice_write = iter_file_splice_write, 9050486958fSJeff Layton .check_flags = nfs_check_flags, 9061c994a09SJeff Layton .setlease = simple_nosetlease, 9070486958fSJeff Layton }; 908ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_file_operations); 909