The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/nfs/file.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  *  linux/fs/nfs/file.c
    3  *
    4  *  Copyright (C) 1992  Rick Sladkey
    5  *
    6  *  Changes Copyright (C) 1994 by Florian La Roche
    7  *   - Do not copy data too often around in the kernel.
    8  *   - In nfs_file_read the return value of kmalloc wasn't checked.
    9  *   - Put in a better version of read look-ahead buffering. Original idea
   10  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
   11  *
   12  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
   13  *
   14  *  Total rewrite of read side for new NFS buffer cache.. Linus.
   15  *
   16  *  nfs regular file handling functions
   17  */
   18 
   19 #include <linux/config.h>
   20 #include <linux/sched.h>
   21 #include <linux/kernel.h>
   22 #include <linux/errno.h>
   23 #include <linux/fcntl.h>
   24 #include <linux/stat.h>
   25 #include <linux/nfs_fs.h>
   26 #include <linux/nfs_mount.h>
   27 #include <linux/mm.h>
   28 #include <linux/slab.h>
   29 #include <linux/pagemap.h>
   30 #include <linux/lockd/bind.h>
   31 #include <linux/smp_lock.h>
   32 
   33 #include <asm/uaccess.h>
   34 #include <asm/system.h>
   35 
   36 #define NFSDBG_FACILITY         NFSDBG_FILE
   37 
   38 static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
   39 static ssize_t nfs_file_read(struct file *, char *, size_t, loff_t *);
   40 static ssize_t nfs_file_write(struct file *, const char *, size_t, loff_t *);
   41 static int  nfs_file_flush(struct file *);
   42 static int  nfs_fsync(struct file *, struct dentry *dentry, int datasync);
   43 
   44 struct file_operations nfs_file_operations = {
   45         llseek:         generic_file_llseek,
   46         read:           nfs_file_read,
   47         write:          nfs_file_write,
   48         mmap:           nfs_file_mmap,
   49         open:           nfs_open,
   50         flush:          nfs_file_flush,
   51         release:        nfs_release,
   52         fsync:          nfs_fsync,
   53         lock:           nfs_lock,
   54 };
   55 
   56 struct inode_operations nfs_file_inode_operations = {
   57         permission:     nfs_permission,
   58         revalidate:     nfs_revalidate,
   59         setattr:        nfs_notify_change,
   60 };
   61 
   62 /* Hack for future NFS swap support */
   63 #ifndef IS_SWAPFILE
   64 # define IS_SWAPFILE(inode)     (0)
   65 #endif
   66 
   67 /*
   68  * Flush all dirty pages, and check for write errors.
   69  *
   70  */
   71 static int
   72 nfs_file_flush(struct file *file)
   73 {
   74         struct inode    *inode = file->f_dentry->d_inode;
   75         int             status;
   76 
   77         dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
   78 
   79         /* Make sure all async reads have been sent off. We don't bother
   80          * waiting on them though... */
   81         if (file->f_mode & FMODE_READ)
   82                 nfs_pagein_inode(inode, 0, 0);
   83 
   84         status = nfs_wb_file(inode, file);
   85         if (!status) {
   86                 status = file->f_error;
   87                 file->f_error = 0;
   88         }
   89         return status;
   90 }
   91 
   92 static ssize_t
   93 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
   94 {
   95         struct dentry * dentry = file->f_dentry;
   96         struct inode * inode = dentry->d_inode;
   97         ssize_t result;
   98 
   99         dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
  100                 dentry->d_parent->d_name.name, dentry->d_name.name,
  101                 (unsigned long) count, (unsigned long) *ppos);
  102 
  103         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  104         if (!result)
  105                 result = generic_file_read(file, buf, count, ppos);
  106         return result;
  107 }
  108 
  109 static int
  110 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  111 {
  112         struct dentry *dentry = file->f_dentry;
  113         struct inode *inode = dentry->d_inode;
  114         int     status;
  115 
  116         dfprintk(VFS, "nfs: mmap(%s/%s)\n",
  117                 dentry->d_parent->d_name.name, dentry->d_name.name);
  118 
  119         status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  120         if (!status)
  121                 status = generic_file_mmap(file, vma);
  122         return status;
  123 }
  124 
  125 /*
  126  * Flush any dirty pages for this process, and check for write errors.
  127  * The return status from this call provides a reliable indication of
  128  * whether any write errors occurred for this process.
  129  */
  130 static int
  131 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  132 {
  133         struct inode *inode = dentry->d_inode;
  134         int status;
  135 
  136         dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
  137 
  138         lock_kernel();
  139         status = nfs_wb_file(inode, file);
  140         if (!status) {
  141                 status = file->f_error;
  142                 file->f_error = 0;
  143         }
  144         unlock_kernel();
  145         return status;
  146 }
  147 
  148 /*
  149  * This does the "real" work of the write. The generic routine has
  150  * allocated the page, locked it, done all the page alignment stuff
  151  * calculations etc. Now we should just copy the data from user
  152  * space and write it back to the real medium..
  153  *
  154  * If the writer ends up delaying the write, the writer needs to
  155  * increment the page use counts until he is done with the page.
  156  */
  157 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  158 {
  159         return nfs_flush_incompatible(file, page);
  160 }
  161 
  162 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  163 {
  164         long status;
  165 
  166         lock_kernel();
  167         status = nfs_updatepage(file, page, offset, to-offset);
  168         unlock_kernel();
  169         return status;
  170 }
  171 
  172 /*
  173  * The following is used by wait_on_page(), generic_file_readahead()
  174  * to initiate the completion of any page readahead operations.
  175  */
  176 static int nfs_sync_page(struct page *page)
  177 {
  178         struct address_space *mapping;
  179         struct inode    *inode;
  180         unsigned long   index = page_index(page);
  181         unsigned int    rpages;
  182         int             result;
  183 
  184         mapping = page->mapping;
  185         if (!mapping)
  186                 return 0;
  187         inode = mapping->host;
  188         if (!inode)
  189                 return 0;
  190 
  191         NFS_SetPageSync(page);
  192         rpages = NFS_SERVER(inode)->rpages;
  193         result = nfs_pagein_inode(inode, index, rpages);
  194         if (result < 0)
  195                 return result;
  196         return 0;
  197 }
  198 
  199 struct address_space_operations nfs_file_aops = {
  200         readpage: nfs_readpage,
  201         sync_page: nfs_sync_page,
  202         writepage: nfs_writepage,
  203 #ifdef CONFIG_NFS_DIRECTIO
  204         direct_fileIO: nfs_direct_IO,
  205 #endif
  206         prepare_write: nfs_prepare_write,
  207         commit_write: nfs_commit_write
  208 };
  209 
  210 /* 
  211  * Write to a file (through the page cache).
  212  */
  213 static ssize_t
  214 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  215 {
  216         struct dentry * dentry = file->f_dentry;
  217         struct inode * inode = dentry->d_inode;
  218         ssize_t result;
  219 
  220         dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
  221                 dentry->d_parent->d_name.name, dentry->d_name.name,
  222                 inode->i_ino, (unsigned long) count, (unsigned long) *ppos);
  223 
  224         result = -EBUSY;
  225         if (IS_SWAPFILE(inode))
  226                 goto out_swapfile;
  227         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  228         if (result)
  229                 goto out;
  230 
  231         result = count;
  232         if (!count)
  233                 goto out;
  234 
  235         result = generic_file_write(file, buf, count, ppos);
  236 out:
  237         return result;
  238 
  239 out_swapfile:
  240         printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  241         goto out;
  242 }
  243 
  244 /*
  245  * Lock a (portion of) a file
  246  */
  247 int
  248 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  249 {
  250         struct inode * inode = filp->f_dentry->d_inode;
  251         int     status = 0;
  252         int     status2;
  253 
  254         dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
  255                         inode->i_dev, inode->i_ino,
  256                         fl->fl_type, fl->fl_flags,
  257                         (long long)fl->fl_start, (long long)fl->fl_end);
  258 
  259         if (!inode)
  260                 return -EINVAL;
  261 
  262         /* No mandatory locks over NFS */
  263         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  264                 return -ENOLCK;
  265 
  266         /* Fake OK code if mounted without NLM support */
  267         if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
  268                 if (IS_GETLK(cmd))
  269                         status = LOCK_USE_CLNT;
  270                 goto out_ok;
  271         }
  272 
  273         /*
  274          * No BSD flocks over NFS allowed.
  275          * Note: we could try to fake a POSIX lock request here by
  276          * using ((u32) filp | 0x80000000) or some such as the pid.
  277          * Not sure whether that would be unique, though, or whether
  278          * that would break in other places.
  279          */
  280         if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
  281                 return -ENOLCK;
  282 
  283         /*
  284          * Flush all pending writes before doing anything
  285          * with locks..
  286          */
  287         status = filemap_fdatasync(inode->i_mapping);
  288         down(&inode->i_sem);
  289         status2 = nfs_wb_all(inode);
  290         if (status2 && !status)
  291                 status = status2;
  292         up(&inode->i_sem);
  293         status2 = filemap_fdatawait(inode->i_mapping);
  294         if (status2 && !status)
  295                 status = status2;
  296         if (status < 0)
  297                 return status;
  298 
  299         lock_kernel();
  300         status = nlmclnt_proc(inode, cmd, fl);
  301         unlock_kernel();
  302         if (status < 0)
  303                 return status;
  304         
  305         status = 0;
  306 
  307         /*
  308          * Make sure we clear the cache whenever we try to get the lock.
  309          * This makes locking act as a cache coherency point.
  310          */
  311  out_ok:
  312         if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  313                 filemap_fdatasync(inode->i_mapping);
  314                 down(&inode->i_sem);
  315                 nfs_wb_all(inode);      /* we may have slept */
  316                 up(&inode->i_sem);
  317                 filemap_fdatawait(inode->i_mapping);
  318                 nfs_zap_caches(inode);
  319         }
  320         return status;
  321 }

Cache object: d403162a04be87e0ac040281fb67842f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.