Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/fs/nfs/file.c
0004  *
0005  *  Copyright (C) 1992  Rick Sladkey
0006  *
0007  *  Changes Copyright (C) 1994 by Florian La Roche
0008  *   - Do not copy data too often around in the kernel.
0009  *   - In nfs_file_read the return value of kmalloc wasn't checked.
0010  *   - Put in a better version of read look-ahead buffering. Original idea
0011  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
0012  *
0013  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
0014  *
0015  *  Total rewrite of read side for new NFS buffer cache.. Linus.
0016  *
0017  *  nfs regular file handling functions
0018  */
0019 
0020 #include <linux/module.h>
0021 #include <linux/time.h>
0022 #include <linux/kernel.h>
0023 #include <linux/errno.h>
0024 #include <linux/fcntl.h>
0025 #include <linux/stat.h>
0026 #include <linux/nfs_fs.h>
0027 #include <linux/nfs_mount.h>
0028 #include <linux/mm.h>
0029 #include <linux/pagemap.h>
0030 #include <linux/gfp.h>
0031 #include <linux/swap.h>
0032 
0033 #include <linux/uaccess.h>
0034 
0035 #include "delegation.h"
0036 #include "internal.h"
0037 #include "iostat.h"
0038 #include "fscache.h"
0039 #include "pnfs.h"
0040 
0041 #include "nfstrace.h"
0042 
0043 #define NFSDBG_FACILITY     NFSDBG_FILE
0044 
0045 static const struct vm_operations_struct nfs_file_vm_ops;
0046 
0047 int nfs_check_flags(int flags)
0048 {
0049     if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
0050         return -EINVAL;
0051 
0052     return 0;
0053 }
0054 EXPORT_SYMBOL_GPL(nfs_check_flags);
0055 
0056 /*
0057  * Open file
0058  */
0059 static int
0060 nfs_file_open(struct inode *inode, struct file *filp)
0061 {
0062     int res;
0063 
0064     dprintk("NFS: open file(%pD2)\n", filp);
0065 
0066     nfs_inc_stats(inode, NFSIOS_VFSOPEN);
0067     res = nfs_check_flags(filp->f_flags);
0068     if (res)
0069         return res;
0070 
0071     res = nfs_open(inode, filp);
0072     if (res == 0)
0073         filp->f_mode |= FMODE_CAN_ODIRECT;
0074     return res;
0075 }
0076 
0077 int
0078 nfs_file_release(struct inode *inode, struct file *filp)
0079 {
0080     dprintk("NFS: release(%pD2)\n", filp);
0081 
0082     nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
0083     nfs_file_clear_open_context(filp);
0084     nfs_fscache_release_file(inode, filp);
0085     return 0;
0086 }
0087 EXPORT_SYMBOL_GPL(nfs_file_release);
0088 
0089 /**
0090  * nfs_revalidate_file_size - Revalidate the file size
0091  * @inode: pointer to inode struct
0092  * @filp: pointer to struct file
0093  *
0094  * Revalidates the file length. This is basically a wrapper around
0095  * nfs_revalidate_inode() that takes into account the fact that we may
0096  * have cached writes (in which case we don't care about the server's
0097  * idea of what the file length is), or O_DIRECT (in which case we
0098  * shouldn't trust the cache).
0099  */
0100 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
0101 {
0102     struct nfs_server *server = NFS_SERVER(inode);
0103 
0104     if (filp->f_flags & O_DIRECT)
0105         goto force_reval;
0106     if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE))
0107         goto force_reval;
0108     return 0;
0109 force_reval:
0110     return __nfs_revalidate_inode(server, inode);
0111 }
0112 
0113 loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
0114 {
0115     dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
0116             filp, offset, whence);
0117 
0118     /*
0119      * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
0120      * the cached file length
0121      */
0122     if (whence != SEEK_SET && whence != SEEK_CUR) {
0123         struct inode *inode = filp->f_mapping->host;
0124 
0125         int retval = nfs_revalidate_file_size(inode, filp);
0126         if (retval < 0)
0127             return (loff_t)retval;
0128     }
0129 
0130     return generic_file_llseek(filp, offset, whence);
0131 }
0132 EXPORT_SYMBOL_GPL(nfs_file_llseek);
0133 
0134 /*
0135  * Flush all dirty pages, and check for write errors.
0136  */
0137 static int
0138 nfs_file_flush(struct file *file, fl_owner_t id)
0139 {
0140     struct inode    *inode = file_inode(file);
0141     errseq_t since;
0142 
0143     dprintk("NFS: flush(%pD2)\n", file);
0144 
0145     nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
0146     if ((file->f_mode & FMODE_WRITE) == 0)
0147         return 0;
0148 
0149     /* Flush writes to the server and return any errors */
0150     since = filemap_sample_wb_err(file->f_mapping);
0151     nfs_wb_all(inode);
0152     return filemap_check_wb_err(file->f_mapping, since);
0153 }
0154 
0155 ssize_t
0156 nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
0157 {
0158     struct inode *inode = file_inode(iocb->ki_filp);
0159     ssize_t result;
0160 
0161     if (iocb->ki_flags & IOCB_DIRECT)
0162         return nfs_file_direct_read(iocb, to, false);
0163 
0164     dprintk("NFS: read(%pD2, %zu@%lu)\n",
0165         iocb->ki_filp,
0166         iov_iter_count(to), (unsigned long) iocb->ki_pos);
0167 
0168     nfs_start_io_read(inode);
0169     result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
0170     if (!result) {
0171         result = generic_file_read_iter(iocb, to);
0172         if (result > 0)
0173             nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
0174     }
0175     nfs_end_io_read(inode);
0176     return result;
0177 }
0178 EXPORT_SYMBOL_GPL(nfs_file_read);
0179 
0180 int
0181 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
0182 {
0183     struct inode *inode = file_inode(file);
0184     int status;
0185 
0186     dprintk("NFS: mmap(%pD2)\n", file);
0187 
0188     /* Note: generic_file_mmap() returns ENOSYS on nommu systems
0189      *       so we call that before revalidating the mapping
0190      */
0191     status = generic_file_mmap(file, vma);
0192     if (!status) {
0193         vma->vm_ops = &nfs_file_vm_ops;
0194         status = nfs_revalidate_mapping(inode, file->f_mapping);
0195     }
0196     return status;
0197 }
0198 EXPORT_SYMBOL_GPL(nfs_file_mmap);
0199 
0200 /*
0201  * Flush any dirty pages for this process, and check for write errors.
0202  * The return status from this call provides a reliable indication of
0203  * whether any write errors occurred for this process.
0204  */
0205 static int
0206 nfs_file_fsync_commit(struct file *file, int datasync)
0207 {
0208     struct inode *inode = file_inode(file);
0209     int ret, ret2;
0210 
0211     dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
0212 
0213     nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
0214     ret = nfs_commit_inode(inode, FLUSH_SYNC);
0215     ret2 = file_check_and_advance_wb_err(file);
0216     if (ret2 < 0)
0217         return ret2;
0218     return ret;
0219 }
0220 
0221 int
0222 nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
0223 {
0224     struct inode *inode = file_inode(file);
0225     struct nfs_inode *nfsi = NFS_I(inode);
0226     long save_nredirtied = atomic_long_read(&nfsi->redirtied_pages);
0227     long nredirtied;
0228     int ret;
0229 
0230     trace_nfs_fsync_enter(inode);
0231 
0232     for (;;) {
0233         ret = file_write_and_wait_range(file, start, end);
0234         if (ret != 0)
0235             break;
0236         ret = nfs_file_fsync_commit(file, datasync);
0237         if (ret != 0)
0238             break;
0239         ret = pnfs_sync_inode(inode, !!datasync);
0240         if (ret != 0)
0241             break;
0242         nredirtied = atomic_long_read(&nfsi->redirtied_pages);
0243         if (nredirtied == save_nredirtied)
0244             break;
0245         save_nredirtied = nredirtied;
0246     }
0247 
0248     trace_nfs_fsync_exit(inode, ret);
0249     return ret;
0250 }
0251 EXPORT_SYMBOL_GPL(nfs_file_fsync);
0252 
0253 /*
0254  * Decide whether a read/modify/write cycle may be more efficient
0255  * then a modify/write/read cycle when writing to a page in the
0256  * page cache.
0257  *
0258  * Some pNFS layout drivers can only read/write at a certain block
0259  * granularity like all block devices and therefore we must perform
0260  * read/modify/write whenever a page hasn't read yet and the data
0261  * to be written there is not aligned to a block boundary and/or
0262  * smaller than the block size.
0263  *
0264  * The modify/write/read cycle may occur if a page is read before
0265  * being completely filled by the writer.  In this situation, the
0266  * page must be completely written to stable storage on the server
0267  * before it can be refilled by reading in the page from the server.
0268  * This can lead to expensive, small, FILE_SYNC mode writes being
0269  * done.
0270  *
0271  * It may be more efficient to read the page first if the file is
0272  * open for reading in addition to writing, the page is not marked
0273  * as Uptodate, it is not dirty or waiting to be committed,
0274  * indicating that it was previously allocated and then modified,
0275  * that there were valid bytes of data in that range of the file,
0276  * and that the new data won't completely replace the old data in
0277  * that range of the file.
0278  */
0279 static bool nfs_full_page_write(struct page *page, loff_t pos, unsigned int len)
0280 {
0281     unsigned int pglen = nfs_page_length(page);
0282     unsigned int offset = pos & (PAGE_SIZE - 1);
0283     unsigned int end = offset + len;
0284 
0285     return !pglen || (end >= pglen && !offset);
0286 }
0287 
0288 static bool nfs_want_read_modify_write(struct file *file, struct page *page,
0289             loff_t pos, unsigned int len)
0290 {
0291     /*
0292      * Up-to-date pages, those with ongoing or full-page write
0293      * don't need read/modify/write
0294      */
0295     if (PageUptodate(page) || PagePrivate(page) ||
0296         nfs_full_page_write(page, pos, len))
0297         return false;
0298 
0299     if (pnfs_ld_read_whole_page(file->f_mapping->host))
0300         return true;
0301     /* Open for reading too? */
0302     if (file->f_mode & FMODE_READ)
0303         return true;
0304     return false;
0305 }
0306 
0307 /*
0308  * This does the "real" work of the write. We must allocate and lock the
0309  * page to be sent back to the generic routine, which then copies the
0310  * data from user space.
0311  *
0312  * If the writer ends up delaying the write, the writer needs to
0313  * increment the page use counts until he is done with the page.
0314  */
0315 static int nfs_write_begin(struct file *file, struct address_space *mapping,
0316             loff_t pos, unsigned len,
0317             struct page **pagep, void **fsdata)
0318 {
0319     int ret;
0320     pgoff_t index = pos >> PAGE_SHIFT;
0321     struct page *page;
0322     int once_thru = 0;
0323 
0324     dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
0325         file, mapping->host->i_ino, len, (long long) pos);
0326 
0327 start:
0328     page = grab_cache_page_write_begin(mapping, index);
0329     if (!page)
0330         return -ENOMEM;
0331     *pagep = page;
0332 
0333     ret = nfs_flush_incompatible(file, page);
0334     if (ret) {
0335         unlock_page(page);
0336         put_page(page);
0337     } else if (!once_thru &&
0338            nfs_want_read_modify_write(file, page, pos, len)) {
0339         once_thru = 1;
0340         ret = nfs_read_folio(file, page_folio(page));
0341         put_page(page);
0342         if (!ret)
0343             goto start;
0344     }
0345     return ret;
0346 }
0347 
0348 static int nfs_write_end(struct file *file, struct address_space *mapping,
0349             loff_t pos, unsigned len, unsigned copied,
0350             struct page *page, void *fsdata)
0351 {
0352     unsigned offset = pos & (PAGE_SIZE - 1);
0353     struct nfs_open_context *ctx = nfs_file_open_context(file);
0354     int status;
0355 
0356     dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
0357         file, mapping->host->i_ino, len, (long long) pos);
0358 
0359     /*
0360      * Zero any uninitialised parts of the page, and then mark the page
0361      * as up to date if it turns out that we're extending the file.
0362      */
0363     if (!PageUptodate(page)) {
0364         unsigned pglen = nfs_page_length(page);
0365         unsigned end = offset + copied;
0366 
0367         if (pglen == 0) {
0368             zero_user_segments(page, 0, offset,
0369                     end, PAGE_SIZE);
0370             SetPageUptodate(page);
0371         } else if (end >= pglen) {
0372             zero_user_segment(page, end, PAGE_SIZE);
0373             if (offset == 0)
0374                 SetPageUptodate(page);
0375         } else
0376             zero_user_segment(page, pglen, PAGE_SIZE);
0377     }
0378 
0379     status = nfs_updatepage(file, page, offset, copied);
0380 
0381     unlock_page(page);
0382     put_page(page);
0383 
0384     if (status < 0)
0385         return status;
0386     NFS_I(mapping->host)->write_io += copied;
0387 
0388     if (nfs_ctx_key_to_expire(ctx, mapping->host))
0389         nfs_wb_all(mapping->host);
0390 
0391     return copied;
0392 }
0393 
0394 /*
0395  * Partially or wholly invalidate a page
0396  * - Release the private state associated with a page if undergoing complete
0397  *   page invalidation
0398  * - Called if either PG_private or PG_fscache is set on the page
0399  * - Caller holds page lock
0400  */
0401 static void nfs_invalidate_folio(struct folio *folio, size_t offset,
0402                 size_t length)
0403 {
0404     dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n",
0405          folio->index, offset, length);
0406 
0407     if (offset != 0 || length < folio_size(folio))
0408         return;
0409     /* Cancel any unstarted writes on this page */
0410     nfs_wb_folio_cancel(folio->mapping->host, folio);
0411     folio_wait_fscache(folio);
0412 }
0413 
0414 /*
0415  * Attempt to release the private state associated with a folio
0416  * - Called if either private or fscache flags are set on the folio
0417  * - Caller holds folio lock
0418  * - Return true (may release folio) or false (may not)
0419  */
0420 static bool nfs_release_folio(struct folio *folio, gfp_t gfp)
0421 {
0422     dfprintk(PAGECACHE, "NFS: release_folio(%p)\n", folio);
0423 
0424     /* If the private flag is set, then the folio is not freeable */
0425     if (folio_test_private(folio))
0426         return false;
0427     return nfs_fscache_release_folio(folio, gfp);
0428 }
0429 
0430 static void nfs_check_dirty_writeback(struct folio *folio,
0431                 bool *dirty, bool *writeback)
0432 {
0433     struct nfs_inode *nfsi;
0434     struct address_space *mapping = folio->mapping;
0435 
0436     /*
0437      * Check if an unstable folio is currently being committed and
0438      * if so, have the VM treat it as if the folio is under writeback
0439      * so it will not block due to folios that will shortly be freeable.
0440      */
0441     nfsi = NFS_I(mapping->host);
0442     if (atomic_read(&nfsi->commit_info.rpcs_out)) {
0443         *writeback = true;
0444         return;
0445     }
0446 
0447     /*
0448      * If the private flag is set, then the folio is not freeable
0449      * and as the inode is not being committed, it's not going to
0450      * be cleaned in the near future so treat it as dirty
0451      */
0452     if (folio_test_private(folio))
0453         *dirty = true;
0454 }
0455 
0456 /*
0457  * Attempt to clear the private state associated with a page when an error
0458  * occurs that requires the cached contents of an inode to be written back or
0459  * destroyed
0460  * - Called if either PG_private or fscache is set on the page
0461  * - Caller holds page lock
0462  * - Return 0 if successful, -error otherwise
0463  */
0464 static int nfs_launder_folio(struct folio *folio)
0465 {
0466     struct inode *inode = folio->mapping->host;
0467 
0468     dfprintk(PAGECACHE, "NFS: launder_folio(%ld, %llu)\n",
0469         inode->i_ino, folio_pos(folio));
0470 
0471     folio_wait_fscache(folio);
0472     return nfs_wb_page(inode, &folio->page);
0473 }
0474 
0475 static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
0476                         sector_t *span)
0477 {
0478     unsigned long blocks;
0479     long long isize;
0480     int ret;
0481     struct inode *inode = file_inode(file);
0482     struct rpc_clnt *clnt = NFS_CLIENT(inode);
0483     struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
0484 
0485     spin_lock(&inode->i_lock);
0486     blocks = inode->i_blocks;
0487     isize = inode->i_size;
0488     spin_unlock(&inode->i_lock);
0489     if (blocks*512 < isize) {
0490         pr_warn("swap activate: swapfile has holes\n");
0491         return -EINVAL;
0492     }
0493 
0494     ret = rpc_clnt_swap_activate(clnt);
0495     if (ret)
0496         return ret;
0497     ret = add_swap_extent(sis, 0, sis->max, 0);
0498     if (ret < 0) {
0499         rpc_clnt_swap_deactivate(clnt);
0500         return ret;
0501     }
0502 
0503     *span = sis->pages;
0504 
0505     if (cl->rpc_ops->enable_swap)
0506         cl->rpc_ops->enable_swap(inode);
0507 
0508     sis->flags |= SWP_FS_OPS;
0509     return ret;
0510 }
0511 
0512 static void nfs_swap_deactivate(struct file *file)
0513 {
0514     struct inode *inode = file_inode(file);
0515     struct rpc_clnt *clnt = NFS_CLIENT(inode);
0516     struct nfs_client *cl = NFS_SERVER(inode)->nfs_client;
0517 
0518     rpc_clnt_swap_deactivate(clnt);
0519     if (cl->rpc_ops->disable_swap)
0520         cl->rpc_ops->disable_swap(file_inode(file));
0521 }
0522 
0523 const struct address_space_operations nfs_file_aops = {
0524     .read_folio = nfs_read_folio,
0525     .readahead = nfs_readahead,
0526     .dirty_folio = filemap_dirty_folio,
0527     .writepage = nfs_writepage,
0528     .writepages = nfs_writepages,
0529     .write_begin = nfs_write_begin,
0530     .write_end = nfs_write_end,
0531     .invalidate_folio = nfs_invalidate_folio,
0532     .release_folio = nfs_release_folio,
0533     .migrate_folio = nfs_migrate_folio,
0534     .launder_folio = nfs_launder_folio,
0535     .is_dirty_writeback = nfs_check_dirty_writeback,
0536     .error_remove_page = generic_error_remove_page,
0537     .swap_activate = nfs_swap_activate,
0538     .swap_deactivate = nfs_swap_deactivate,
0539     .swap_rw = nfs_swap_rw,
0540 };
0541 
0542 /*
0543  * Notification that a PTE pointing to an NFS page is about to be made
0544  * writable, implying that someone is about to modify the page through a
0545  * shared-writable mapping
0546  */
0547 static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
0548 {
0549     struct page *page = vmf->page;
0550     struct file *filp = vmf->vma->vm_file;
0551     struct inode *inode = file_inode(filp);
0552     unsigned pagelen;
0553     vm_fault_t ret = VM_FAULT_NOPAGE;
0554     struct address_space *mapping;
0555 
0556     dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
0557         filp, filp->f_mapping->host->i_ino,
0558         (long long)page_offset(page));
0559 
0560     sb_start_pagefault(inode->i_sb);
0561 
0562     /* make sure the cache has finished storing the page */
0563     if (PageFsCache(page) &&
0564         wait_on_page_fscache_killable(vmf->page) < 0) {
0565         ret = VM_FAULT_RETRY;
0566         goto out;
0567     }
0568 
0569     wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
0570             nfs_wait_bit_killable, TASK_KILLABLE);
0571 
0572     lock_page(page);
0573     mapping = page_file_mapping(page);
0574     if (mapping != inode->i_mapping)
0575         goto out_unlock;
0576 
0577     wait_on_page_writeback(page);
0578 
0579     pagelen = nfs_page_length(page);
0580     if (pagelen == 0)
0581         goto out_unlock;
0582 
0583     ret = VM_FAULT_LOCKED;
0584     if (nfs_flush_incompatible(filp, page) == 0 &&
0585         nfs_updatepage(filp, page, 0, pagelen) == 0)
0586         goto out;
0587 
0588     ret = VM_FAULT_SIGBUS;
0589 out_unlock:
0590     unlock_page(page);
0591 out:
0592     sb_end_pagefault(inode->i_sb);
0593     return ret;
0594 }
0595 
0596 static const struct vm_operations_struct nfs_file_vm_ops = {
0597     .fault = filemap_fault,
0598     .map_pages = filemap_map_pages,
0599     .page_mkwrite = nfs_vm_page_mkwrite,
0600 };
0601 
0602 ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
0603 {
0604     struct file *file = iocb->ki_filp;
0605     struct inode *inode = file_inode(file);
0606     unsigned int mntflags = NFS_SERVER(inode)->flags;
0607     ssize_t result, written;
0608     errseq_t since;
0609     int error;
0610 
0611     result = nfs_key_timeout_notify(file, inode);
0612     if (result)
0613         return result;
0614 
0615     if (iocb->ki_flags & IOCB_DIRECT)
0616         return nfs_file_direct_write(iocb, from, false);
0617 
0618     dprintk("NFS: write(%pD2, %zu@%Ld)\n",
0619         file, iov_iter_count(from), (long long) iocb->ki_pos);
0620 
0621     if (IS_SWAPFILE(inode))
0622         goto out_swapfile;
0623     /*
0624      * O_APPEND implies that we must revalidate the file length.
0625      */
0626     if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) {
0627         result = nfs_revalidate_file_size(inode, file);
0628         if (result)
0629             return result;
0630     }
0631 
0632     nfs_clear_invalid_mapping(file->f_mapping);
0633 
0634     since = filemap_sample_wb_err(file->f_mapping);
0635     nfs_start_io_write(inode);
0636     result = generic_write_checks(iocb, from);
0637     if (result > 0) {
0638         current->backing_dev_info = inode_to_bdi(inode);
0639         result = generic_perform_write(iocb, from);
0640         current->backing_dev_info = NULL;
0641     }
0642     nfs_end_io_write(inode);
0643     if (result <= 0)
0644         goto out;
0645 
0646     written = result;
0647     iocb->ki_pos += written;
0648     nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
0649 
0650     if (mntflags & NFS_MOUNT_WRITE_EAGER) {
0651         result = filemap_fdatawrite_range(file->f_mapping,
0652                           iocb->ki_pos - written,
0653                           iocb->ki_pos - 1);
0654         if (result < 0)
0655             goto out;
0656     }
0657     if (mntflags & NFS_MOUNT_WRITE_WAIT) {
0658         result = filemap_fdatawait_range(file->f_mapping,
0659                          iocb->ki_pos - written,
0660                          iocb->ki_pos - 1);
0661     }
0662     result = generic_write_sync(iocb, written);
0663     if (result < 0)
0664         return result;
0665 
0666 out:
0667     /* Return error values */
0668     error = filemap_check_wb_err(file->f_mapping, since);
0669     switch (error) {
0670     default:
0671         break;
0672     case -EDQUOT:
0673     case -EFBIG:
0674     case -ENOSPC:
0675         nfs_wb_all(inode);
0676         error = file_check_and_advance_wb_err(file);
0677         if (error < 0)
0678             result = error;
0679     }
0680     return result;
0681 
0682 out_swapfile:
0683     printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
0684     return -ETXTBSY;
0685 }
0686 EXPORT_SYMBOL_GPL(nfs_file_write);
0687 
0688 static int
0689 do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
0690 {
0691     struct inode *inode = filp->f_mapping->host;
0692     int status = 0;
0693     unsigned int saved_type = fl->fl_type;
0694 
0695     /* Try local locking first */
0696     posix_test_lock(filp, fl);
0697     if (fl->fl_type != F_UNLCK) {
0698         /* found a conflict */
0699         goto out;
0700     }
0701     fl->fl_type = saved_type;
0702 
0703     if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
0704         goto out_noconflict;
0705 
0706     if (is_local)
0707         goto out_noconflict;
0708 
0709     status = NFS_PROTO(inode)->lock(filp, cmd, fl);
0710 out:
0711     return status;
0712 out_noconflict:
0713     fl->fl_type = F_UNLCK;
0714     goto out;
0715 }
0716 
0717 static int
0718 do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
0719 {
0720     struct inode *inode = filp->f_mapping->host;
0721     struct nfs_lock_context *l_ctx;
0722     int status;
0723 
0724     /*
0725      * Flush all pending writes before doing anything
0726      * with locks..
0727      */
0728     nfs_wb_all(inode);
0729 
0730     l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
0731     if (!IS_ERR(l_ctx)) {
0732         status = nfs_iocounter_wait(l_ctx);
0733         nfs_put_lock_context(l_ctx);
0734         /*  NOTE: special case
0735          *  If we're signalled while cleaning up locks on process exit, we
0736          *  still need to complete the unlock.
0737          */
0738         if (status < 0 && !(fl->fl_flags & FL_CLOSE))
0739             return status;
0740     }
0741 
0742     /*
0743      * Use local locking if mounted with "-onolock" or with appropriate
0744      * "-olocal_lock="
0745      */
0746     if (!is_local)
0747         status = NFS_PROTO(inode)->lock(filp, cmd, fl);
0748     else
0749         status = locks_lock_file_wait(filp, fl);
0750     return status;
0751 }
0752 
0753 static int
0754 do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
0755 {
0756     struct inode *inode = filp->f_mapping->host;
0757     int status;
0758 
0759     /*
0760      * Flush all pending writes before doing anything
0761      * with locks..
0762      */
0763     status = nfs_sync_mapping(filp->f_mapping);
0764     if (status != 0)
0765         goto out;
0766 
0767     /*
0768      * Use local locking if mounted with "-onolock" or with appropriate
0769      * "-olocal_lock="
0770      */
0771     if (!is_local)
0772         status = NFS_PROTO(inode)->lock(filp, cmd, fl);
0773     else
0774         status = locks_lock_file_wait(filp, fl);
0775     if (status < 0)
0776         goto out;
0777 
0778     /*
0779      * Invalidate cache to prevent missing any changes.  If
0780      * the file is mapped, clear the page cache as well so
0781      * those mappings will be loaded.
0782      *
0783      * This makes locking act as a cache coherency point.
0784      */
0785     nfs_sync_mapping(filp->f_mapping);
0786     if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
0787         nfs_zap_caches(inode);
0788         if (mapping_mapped(filp->f_mapping))
0789             nfs_revalidate_mapping(inode, filp->f_mapping);
0790     }
0791 out:
0792     return status;
0793 }
0794 
0795 /*
0796  * Lock a (portion of) a file
0797  */
0798 int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
0799 {
0800     struct inode *inode = filp->f_mapping->host;
0801     int ret = -ENOLCK;
0802     int is_local = 0;
0803 
0804     dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
0805             filp, fl->fl_type, fl->fl_flags,
0806             (long long)fl->fl_start, (long long)fl->fl_end);
0807 
0808     nfs_inc_stats(inode, NFSIOS_VFSLOCK);
0809 
0810     if (fl->fl_flags & FL_RECLAIM)
0811         return -ENOGRACE;
0812 
0813     if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
0814         is_local = 1;
0815 
0816     if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
0817         ret = NFS_PROTO(inode)->lock_check_bounds(fl);
0818         if (ret < 0)
0819             goto out_err;
0820     }
0821 
0822     if (IS_GETLK(cmd))
0823         ret = do_getlk(filp, cmd, fl, is_local);
0824     else if (fl->fl_type == F_UNLCK)
0825         ret = do_unlk(filp, cmd, fl, is_local);
0826     else
0827         ret = do_setlk(filp, cmd, fl, is_local);
0828 out_err:
0829     return ret;
0830 }
0831 EXPORT_SYMBOL_GPL(nfs_lock);
0832 
0833 /*
0834  * Lock a (portion of) a file
0835  */
0836 int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
0837 {
0838     struct inode *inode = filp->f_mapping->host;
0839     int is_local = 0;
0840 
0841     dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
0842             filp, fl->fl_type, fl->fl_flags);
0843 
0844     if (!(fl->fl_flags & FL_FLOCK))
0845         return -ENOLCK;
0846 
0847     if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
0848         is_local = 1;
0849 
0850     /* We're simulating flock() locks using posix locks on the server */
0851     if (fl->fl_type == F_UNLCK)
0852         return do_unlk(filp, cmd, fl, is_local);
0853     return do_setlk(filp, cmd, fl, is_local);
0854 }
0855 EXPORT_SYMBOL_GPL(nfs_flock);
0856 
0857 const struct file_operations nfs_file_operations = {
0858     .llseek     = nfs_file_llseek,
0859     .read_iter  = nfs_file_read,
0860     .write_iter = nfs_file_write,
0861     .mmap       = nfs_file_mmap,
0862     .open       = nfs_file_open,
0863     .flush      = nfs_file_flush,
0864     .release    = nfs_file_release,
0865     .fsync      = nfs_file_fsync,
0866     .lock       = nfs_lock,
0867     .flock      = nfs_flock,
0868     .splice_read    = generic_file_splice_read,
0869     .splice_write   = iter_file_splice_write,
0870     .check_flags    = nfs_check_flags,
0871     .setlease   = simple_nosetlease,
0872 };
0873 EXPORT_SYMBOL_GPL(nfs_file_operations);