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/nfsclient/nfs_bio.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  * Copyright (c) 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Rick Macklem at The University of Guelph.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)nfs_bio.c   8.9 (Berkeley) 3/30/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include "opt_kdtrace.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/bio.h>
   43 #include <sys/buf.h>
   44 #include <sys/kernel.h>
   45 #include <sys/mbuf.h>
   46 #include <sys/mount.h>
   47 #include <sys/proc.h>
   48 #include <sys/vmmeter.h>
   49 #include <sys/vnode.h>
   50 
   51 #include <vm/vm.h>
   52 #include <vm/vm_extern.h>
   53 #include <vm/vm_page.h>
   54 #include <vm/vm_object.h>
   55 #include <vm/vm_pager.h>
   56 #include <vm/vnode_pager.h>
   57 
   58 #include <nfs/nfsproto.h>
   59 #include <nfsclient/nfs.h>
   60 #include <nfsclient/nfsmount.h>
   61 #include <nfsclient/nfsnode.h>
   62 #include <nfsclient/nfs_kdtrace.h>
   63 
   64 static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size,
   65                     struct thread *td);
   66 static int nfs_directio_write(struct vnode *vp, struct uio *uiop, 
   67                               struct ucred *cred, int ioflag);
   68 
   69 extern int nfs_directio_enable;
   70 extern int nfs_directio_allow_mmap;
   71 
   72 /*
   73  * Vnode op for VM getpages.
   74  */
   75 int
   76 nfs_getpages(struct vop_getpages_args *ap)
   77 {
   78         int i, error, nextoff, size, toff, count, npages;
   79         struct uio uio;
   80         struct iovec iov;
   81         vm_offset_t kva;
   82         struct buf *bp;
   83         struct vnode *vp;
   84         struct thread *td;
   85         struct ucred *cred;
   86         struct nfsmount *nmp;
   87         vm_object_t object;
   88         vm_page_t *pages;
   89         struct nfsnode *np;
   90 
   91         vp = ap->a_vp;
   92         np = VTONFS(vp);
   93         td = curthread;                         /* XXX */
   94         cred = curthread->td_ucred;             /* XXX */
   95         nmp = VFSTONFS(vp->v_mount);
   96         pages = ap->a_m;
   97         count = ap->a_count;
   98 
   99         if ((object = vp->v_object) == NULL) {
  100                 nfs_printf("nfs_getpages: called with non-merged cache vnode??\n");
  101                 return (VM_PAGER_ERROR);
  102         }
  103 
  104         if (nfs_directio_enable && !nfs_directio_allow_mmap) {
  105                 mtx_lock(&np->n_mtx);
  106                 if ((np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
  107                         mtx_unlock(&np->n_mtx);
  108                         nfs_printf("nfs_getpages: called on non-cacheable vnode??\n");
  109                         return (VM_PAGER_ERROR);
  110                 } else
  111                         mtx_unlock(&np->n_mtx);
  112         }
  113 
  114         mtx_lock(&nmp->nm_mtx);
  115         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  116             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {  
  117                 mtx_unlock(&nmp->nm_mtx);
  118                 /* We'll never get here for v4, because we always have fsinfo */
  119                 (void)nfs_fsinfo(nmp, vp, cred, td);
  120         } else
  121                 mtx_unlock(&nmp->nm_mtx);
  122 
  123         npages = btoc(count);
  124 
  125         /*
  126          * If the requested page is partially valid, just return it and
  127          * allow the pager to zero-out the blanks.  Partially valid pages
  128          * can only occur at the file EOF.
  129          */
  130         VM_OBJECT_LOCK(object);
  131         if (pages[ap->a_reqpage]->valid != 0) {
  132                 vm_page_lock_queues();
  133                 for (i = 0; i < npages; ++i) {
  134                         if (i != ap->a_reqpage)
  135                                 vm_page_free(pages[i]);
  136                 }
  137                 vm_page_unlock_queues();
  138                 VM_OBJECT_UNLOCK(object);
  139                 return (0);
  140         }
  141         VM_OBJECT_UNLOCK(object);
  142 
  143         /*
  144          * We use only the kva address for the buffer, but this is extremely
  145          * convienient and fast.
  146          */
  147         bp = getpbuf(&nfs_pbuf_freecnt);
  148 
  149         kva = (vm_offset_t) bp->b_data;
  150         pmap_qenter(kva, pages, npages);
  151         PCPU_INC(cnt.v_vnodein);
  152         PCPU_ADD(cnt.v_vnodepgsin, npages);
  153 
  154         iov.iov_base = (caddr_t) kva;
  155         iov.iov_len = count;
  156         uio.uio_iov = &iov;
  157         uio.uio_iovcnt = 1;
  158         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
  159         uio.uio_resid = count;
  160         uio.uio_segflg = UIO_SYSSPACE;
  161         uio.uio_rw = UIO_READ;
  162         uio.uio_td = td;
  163 
  164         error = (nmp->nm_rpcops->nr_readrpc)(vp, &uio, cred);
  165         pmap_qremove(kva, npages);
  166 
  167         relpbuf(bp, &nfs_pbuf_freecnt);
  168 
  169         if (error && (uio.uio_resid == count)) {
  170                 nfs_printf("nfs_getpages: error %d\n", error);
  171                 VM_OBJECT_LOCK(object);
  172                 vm_page_lock_queues();
  173                 for (i = 0; i < npages; ++i) {
  174                         if (i != ap->a_reqpage)
  175                                 vm_page_free(pages[i]);
  176                 }
  177                 vm_page_unlock_queues();
  178                 VM_OBJECT_UNLOCK(object);
  179                 return (VM_PAGER_ERROR);
  180         }
  181 
  182         /*
  183          * Calculate the number of bytes read and validate only that number
  184          * of bytes.  Note that due to pending writes, size may be 0.  This
  185          * does not mean that the remaining data is invalid!
  186          */
  187 
  188         size = count - uio.uio_resid;
  189         VM_OBJECT_LOCK(object);
  190         vm_page_lock_queues();
  191         for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
  192                 vm_page_t m;
  193                 nextoff = toff + PAGE_SIZE;
  194                 m = pages[i];
  195 
  196                 if (nextoff <= size) {
  197                         /*
  198                          * Read operation filled an entire page
  199                          */
  200                         m->valid = VM_PAGE_BITS_ALL;
  201                         KASSERT(m->dirty == 0,
  202                             ("nfs_getpages: page %p is dirty", m));
  203                 } else if (size > toff) {
  204                         /*
  205                          * Read operation filled a partial page.
  206                          */
  207                         m->valid = 0;
  208                         vm_page_set_valid(m, 0, size - toff);
  209                         KASSERT(m->dirty == 0,
  210                             ("nfs_getpages: page %p is dirty", m));
  211                 } else {
  212                         /*
  213                          * Read operation was short.  If no error occured
  214                          * we may have hit a zero-fill section.   We simply
  215                          * leave valid set to 0.
  216                          */
  217                         ;
  218                 }
  219                 if (i != ap->a_reqpage) {
  220                         /*
  221                          * Whether or not to leave the page activated is up in
  222                          * the air, but we should put the page on a page queue
  223                          * somewhere (it already is in the object).  Result:
  224                          * It appears that emperical results show that
  225                          * deactivating pages is best.
  226                          */
  227 
  228                         /*
  229                          * Just in case someone was asking for this page we
  230                          * now tell them that it is ok to use.
  231                          */
  232                         if (!error) {
  233                                 if (m->oflags & VPO_WANTED)
  234                                         vm_page_activate(m);
  235                                 else
  236                                         vm_page_deactivate(m);
  237                                 vm_page_wakeup(m);
  238                         } else {
  239                                 vm_page_free(m);
  240                         }
  241                 }
  242         }
  243         vm_page_unlock_queues();
  244         VM_OBJECT_UNLOCK(object);
  245         return (0);
  246 }
  247 
  248 /*
  249  * Vnode op for VM putpages.
  250  */
  251 int
  252 nfs_putpages(struct vop_putpages_args *ap)
  253 {
  254         struct uio uio;
  255         struct iovec iov;
  256         vm_offset_t kva;
  257         struct buf *bp;
  258         int iomode, must_commit, i, error, npages, count;
  259         off_t offset;
  260         int *rtvals;
  261         struct vnode *vp;
  262         struct thread *td;
  263         struct ucred *cred;
  264         struct nfsmount *nmp;
  265         struct nfsnode *np;
  266         vm_page_t *pages;
  267 
  268         vp = ap->a_vp;
  269         np = VTONFS(vp);
  270         td = curthread;                         /* XXX */
  271         /* Set the cred to n_writecred for the write rpcs. */
  272         if (np->n_writecred != NULL)
  273                 cred = crhold(np->n_writecred);
  274         else
  275                 cred = crhold(curthread->td_ucred);     /* XXX */
  276         nmp = VFSTONFS(vp->v_mount);
  277         pages = ap->a_m;
  278         count = ap->a_count;
  279         rtvals = ap->a_rtvals;
  280         npages = btoc(count);
  281         offset = IDX_TO_OFF(pages[0]->pindex);
  282         
  283         mtx_lock(&nmp->nm_mtx);
  284         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  285             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
  286                 mtx_unlock(&nmp->nm_mtx);
  287                 (void)nfs_fsinfo(nmp, vp, cred, td);
  288         } else
  289                 mtx_unlock(&nmp->nm_mtx);
  290 
  291         mtx_lock(&np->n_mtx);
  292         if (nfs_directio_enable && !nfs_directio_allow_mmap && 
  293             (np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
  294                 mtx_unlock(&np->n_mtx);         
  295                 nfs_printf("nfs_putpages: called on noncache-able vnode??\n");
  296                 mtx_lock(&np->n_mtx);
  297         }
  298 
  299         for (i = 0; i < npages; i++)
  300                 rtvals[i] = VM_PAGER_ERROR;
  301 
  302         /*
  303          * When putting pages, do not extend file past EOF.
  304          */
  305         if (offset + count > np->n_size) {
  306                 count = np->n_size - offset;
  307                 if (count < 0)
  308                         count = 0;
  309         }
  310         mtx_unlock(&np->n_mtx);
  311 
  312         /*
  313          * We use only the kva address for the buffer, but this is extremely
  314          * convienient and fast.
  315          */
  316         bp = getpbuf(&nfs_pbuf_freecnt);
  317 
  318         kva = (vm_offset_t) bp->b_data;
  319         pmap_qenter(kva, pages, npages);
  320         PCPU_INC(cnt.v_vnodeout);
  321         PCPU_ADD(cnt.v_vnodepgsout, count);
  322 
  323         iov.iov_base = (caddr_t) kva;
  324         iov.iov_len = count;
  325         uio.uio_iov = &iov;
  326         uio.uio_iovcnt = 1;
  327         uio.uio_offset = offset;
  328         uio.uio_resid = count;
  329         uio.uio_segflg = UIO_SYSSPACE;
  330         uio.uio_rw = UIO_WRITE;
  331         uio.uio_td = td;
  332 
  333         if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
  334             iomode = NFSV3WRITE_UNSTABLE;
  335         else
  336             iomode = NFSV3WRITE_FILESYNC;
  337 
  338         error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, &iomode, &must_commit);
  339         crfree(cred);
  340 
  341         pmap_qremove(kva, npages);
  342         relpbuf(bp, &nfs_pbuf_freecnt);
  343 
  344         if (!error) {
  345                 vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid);
  346                 if (must_commit) {
  347                         nfs_clearcommit(vp->v_mount);
  348                 }
  349         }
  350         return rtvals[0];
  351 }
  352 
  353 /*
  354  * For nfs, cache consistency can only be maintained approximately.
  355  * Although RFC1094 does not specify the criteria, the following is
  356  * believed to be compatible with the reference port.
  357  * For nfs:
  358  * If the file's modify time on the server has changed since the
  359  * last read rpc or you have written to the file,
  360  * you may have lost data cache consistency with the
  361  * server, so flush all of the file's data out of the cache.
  362  * Then force a getattr rpc to ensure that you have up to date
  363  * attributes.
  364  * NB: This implies that cache data can be read when up to
  365  * NFS_ATTRTIMEO seconds out of date. If you find that you need current
  366  * attributes this could be forced by setting n_attrstamp to 0 before
  367  * the VOP_GETATTR() call.
  368  */
  369 static inline int
  370 nfs_bioread_check_cons(struct vnode *vp, struct thread *td, struct ucred *cred)
  371 {
  372         int error = 0;
  373         struct vattr vattr;
  374         struct nfsnode *np = VTONFS(vp);
  375         int old_lock;
  376         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  377         
  378         /*
  379          * Grab the exclusive lock before checking whether the cache is
  380          * consistent.
  381          * XXX - We can make this cheaper later (by acquiring cheaper locks).
  382          * But for now, this suffices.
  383          */
  384         old_lock = nfs_upgrade_vnlock(vp);
  385         if (vp->v_iflag & VI_DOOMED) {
  386                 nfs_downgrade_vnlock(vp, old_lock);
  387                 return (EBADF);
  388         }
  389                 
  390         mtx_lock(&np->n_mtx);
  391         if (np->n_flag & NMODIFIED) {
  392                 mtx_unlock(&np->n_mtx);
  393                 if (vp->v_type != VREG) {
  394                         if (vp->v_type != VDIR)
  395                                 panic("nfs: bioread, not dir");
  396                         (nmp->nm_rpcops->nr_invaldir)(vp);
  397                         error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
  398                         if (error)
  399                                 goto out;
  400                 }
  401                 np->n_attrstamp = 0;
  402                 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
  403                 error = VOP_GETATTR(vp, &vattr, cred);
  404                 if (error)
  405                         goto out;
  406                 mtx_lock(&np->n_mtx);
  407                 np->n_mtime = vattr.va_mtime;
  408                 mtx_unlock(&np->n_mtx);
  409         } else {
  410                 mtx_unlock(&np->n_mtx);
  411                 error = VOP_GETATTR(vp, &vattr, cred);
  412                 if (error)
  413                         return (error);
  414                 mtx_lock(&np->n_mtx);
  415                 if ((np->n_flag & NSIZECHANGED)
  416                     || (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime))) {
  417                         mtx_unlock(&np->n_mtx);
  418                         if (vp->v_type == VDIR)
  419                                 (nmp->nm_rpcops->nr_invaldir)(vp);
  420                         error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
  421                         if (error)
  422                                 goto out;
  423                         mtx_lock(&np->n_mtx);
  424                         np->n_mtime = vattr.va_mtime;
  425                         np->n_flag &= ~NSIZECHANGED;
  426                 }
  427                 mtx_unlock(&np->n_mtx);
  428         }
  429 out:    
  430         nfs_downgrade_vnlock(vp, old_lock);
  431         return error;
  432 }
  433 
  434 /*
  435  * Vnode op for read using bio
  436  */
  437 int
  438 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
  439 {
  440         struct nfsnode *np = VTONFS(vp);
  441         int biosize, i;
  442         struct buf *bp, *rabp;
  443         struct thread *td;
  444         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  445         daddr_t lbn, rabn;
  446         off_t end;
  447         int bcount;
  448         int seqcount;
  449         int nra, error = 0, n = 0, on = 0;
  450 
  451         KASSERT(uio->uio_rw == UIO_READ, ("nfs_read mode"));
  452         if (uio->uio_resid == 0)
  453                 return (0);
  454         if (uio->uio_offset < 0)        /* XXX VDIR cookies can be negative */
  455                 return (EINVAL);
  456         td = uio->uio_td;
  457 
  458         mtx_lock(&nmp->nm_mtx);
  459         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  460             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
  461                 mtx_unlock(&nmp->nm_mtx);
  462                 (void)nfs_fsinfo(nmp, vp, cred, td);
  463         } else
  464                 mtx_unlock(&nmp->nm_mtx);               
  465 
  466         end = uio->uio_offset + uio->uio_resid;
  467         if (vp->v_type != VDIR &&
  468             (end > nmp->nm_maxfilesize || end < uio->uio_offset))
  469                 return (EFBIG);
  470 
  471         if (nfs_directio_enable && (ioflag & IO_DIRECT) && (vp->v_type == VREG))
  472                 /* No caching/ no readaheads. Just read data into the user buffer */
  473                 return nfs_readrpc(vp, uio, cred);
  474 
  475         biosize = vp->v_bufobj.bo_bsize;
  476         seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
  477         
  478         error = nfs_bioread_check_cons(vp, td, cred);
  479         if (error)
  480                 return error;
  481 
  482         do {
  483             u_quad_t nsize;
  484                         
  485             mtx_lock(&np->n_mtx);
  486             nsize = np->n_size;
  487             mtx_unlock(&np->n_mtx);                 
  488 
  489             switch (vp->v_type) {
  490             case VREG:
  491                 nfsstats.biocache_reads++;
  492                 lbn = uio->uio_offset / biosize;
  493                 on = uio->uio_offset & (biosize - 1);
  494 
  495                 /*
  496                  * Start the read ahead(s), as required.
  497                  */
  498                 if (nmp->nm_readahead > 0) {
  499                     for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
  500                         (off_t)(lbn + 1 + nra) * biosize < nsize; nra++) {
  501                         rabn = lbn + 1 + nra;
  502                         if (incore(&vp->v_bufobj, rabn) == NULL) {
  503                             rabp = nfs_getcacheblk(vp, rabn, biosize, td);
  504                             if (!rabp) {
  505                                 error = nfs_sigintr(nmp, td);
  506                                 return (error ? error : EINTR);
  507                             }
  508                             if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
  509                                 rabp->b_flags |= B_ASYNC;
  510                                 rabp->b_iocmd = BIO_READ;
  511                                 vfs_busy_pages(rabp, 0);
  512                                 if (nfs_asyncio(nmp, rabp, cred, td)) {
  513                                     rabp->b_flags |= B_INVAL;
  514                                     rabp->b_ioflags |= BIO_ERROR;
  515                                     vfs_unbusy_pages(rabp);
  516                                     brelse(rabp);
  517                                     break;
  518                                 }
  519                             } else {
  520                                 brelse(rabp);
  521                             }
  522                         }
  523                     }
  524                 }
  525 
  526                 /* Note that bcount is *not* DEV_BSIZE aligned. */
  527                 bcount = biosize;
  528                 if ((off_t)lbn * biosize >= nsize) {
  529                         bcount = 0;
  530                 } else if ((off_t)(lbn + 1) * biosize > nsize) {
  531                         bcount = nsize - (off_t)lbn * biosize;
  532                 }
  533                 bp = nfs_getcacheblk(vp, lbn, bcount, td);
  534 
  535                 if (!bp) {
  536                         error = nfs_sigintr(nmp, td);
  537                         return (error ? error : EINTR);
  538                 }
  539 
  540                 /*
  541                  * If B_CACHE is not set, we must issue the read.  If this
  542                  * fails, we return an error.
  543                  */
  544 
  545                 if ((bp->b_flags & B_CACHE) == 0) {
  546                     bp->b_iocmd = BIO_READ;
  547                     vfs_busy_pages(bp, 0);
  548                     error = nfs_doio(vp, bp, cred, td);
  549                     if (error) {
  550                         brelse(bp);
  551                         return (error);
  552                     }
  553                 }
  554 
  555                 /*
  556                  * on is the offset into the current bp.  Figure out how many
  557                  * bytes we can copy out of the bp.  Note that bcount is
  558                  * NOT DEV_BSIZE aligned.
  559                  *
  560                  * Then figure out how many bytes we can copy into the uio.
  561                  */
  562 
  563                 n = 0;
  564                 if (on < bcount)
  565                         n = min((unsigned)(bcount - on), uio->uio_resid);
  566                 break;
  567             case VLNK:
  568                 nfsstats.biocache_readlinks++;
  569                 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
  570                 if (!bp) {
  571                         error = nfs_sigintr(nmp, td);
  572                         return (error ? error : EINTR);
  573                 }
  574                 if ((bp->b_flags & B_CACHE) == 0) {
  575                     bp->b_iocmd = BIO_READ;
  576                     vfs_busy_pages(bp, 0);
  577                     error = nfs_doio(vp, bp, cred, td);
  578                     if (error) {
  579                         bp->b_ioflags |= BIO_ERROR;
  580                         brelse(bp);
  581                         return (error);
  582                     }
  583                 }
  584                 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
  585                 on = 0;
  586                 break;
  587             case VDIR:
  588                 nfsstats.biocache_readdirs++;
  589                 if (np->n_direofoffset
  590                     && uio->uio_offset >= np->n_direofoffset) {
  591                     return (0);
  592                 }
  593                 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
  594                 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
  595                 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
  596                 if (!bp) {
  597                     error = nfs_sigintr(nmp, td);
  598                     return (error ? error : EINTR);
  599                 }
  600                 if ((bp->b_flags & B_CACHE) == 0) {
  601                     bp->b_iocmd = BIO_READ;
  602                     vfs_busy_pages(bp, 0);
  603                     error = nfs_doio(vp, bp, cred, td);
  604                     if (error) {
  605                             brelse(bp);
  606                     }
  607                     while (error == NFSERR_BAD_COOKIE) {
  608                         (nmp->nm_rpcops->nr_invaldir)(vp);
  609                         error = nfs_vinvalbuf(vp, 0, td, 1);
  610                         /*
  611                          * Yuck! The directory has been modified on the
  612                          * server. The only way to get the block is by
  613                          * reading from the beginning to get all the
  614                          * offset cookies.
  615                          *
  616                          * Leave the last bp intact unless there is an error.
  617                          * Loop back up to the while if the error is another
  618                          * NFSERR_BAD_COOKIE (double yuch!).
  619                          */
  620                         for (i = 0; i <= lbn && !error; i++) {
  621                             if (np->n_direofoffset
  622                                 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
  623                                     return (0);
  624                             bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
  625                             if (!bp) {
  626                                 error = nfs_sigintr(nmp, td);
  627                                 return (error ? error : EINTR);
  628                             }
  629                             if ((bp->b_flags & B_CACHE) == 0) {
  630                                     bp->b_iocmd = BIO_READ;
  631                                     vfs_busy_pages(bp, 0);
  632                                     error = nfs_doio(vp, bp, cred, td);
  633                                     /*
  634                                      * no error + B_INVAL == directory EOF,
  635                                      * use the block.
  636                                      */
  637                                     if (error == 0 && (bp->b_flags & B_INVAL))
  638                                             break;
  639                             }
  640                             /*
  641                              * An error will throw away the block and the
  642                              * for loop will break out.  If no error and this
  643                              * is not the block we want, we throw away the
  644                              * block and go for the next one via the for loop.
  645                              */
  646                             if (error || i < lbn)
  647                                     brelse(bp);
  648                         }
  649                     }
  650                     /*
  651                      * The above while is repeated if we hit another cookie
  652                      * error.  If we hit an error and it wasn't a cookie error,
  653                      * we give up.
  654                      */
  655                     if (error)
  656                             return (error);
  657                 }
  658 
  659                 /*
  660                  * If not eof and read aheads are enabled, start one.
  661                  * (You need the current block first, so that you have the
  662                  *  directory offset cookie of the next block.)
  663                  */
  664                 if (nmp->nm_readahead > 0 &&
  665                     (bp->b_flags & B_INVAL) == 0 &&
  666                     (np->n_direofoffset == 0 ||
  667                     (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
  668                     incore(&vp->v_bufobj, lbn + 1) == NULL) {
  669                         rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
  670                         if (rabp) {
  671                             if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
  672                                 rabp->b_flags |= B_ASYNC;
  673                                 rabp->b_iocmd = BIO_READ;
  674                                 vfs_busy_pages(rabp, 0);
  675                                 if (nfs_asyncio(nmp, rabp, cred, td)) {
  676                                     rabp->b_flags |= B_INVAL;
  677                                     rabp->b_ioflags |= BIO_ERROR;
  678                                     vfs_unbusy_pages(rabp);
  679                                     brelse(rabp);
  680                                 }
  681                             } else {
  682                                 brelse(rabp);
  683                             }
  684                         }
  685                 }
  686                 /*
  687                  * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
  688                  * chopped for the EOF condition, we cannot tell how large
  689                  * NFS directories are going to be until we hit EOF.  So
  690                  * an NFS directory buffer is *not* chopped to its EOF.  Now,
  691                  * it just so happens that b_resid will effectively chop it
  692                  * to EOF.  *BUT* this information is lost if the buffer goes
  693                  * away and is reconstituted into a B_CACHE state ( due to
  694                  * being VMIO ) later.  So we keep track of the directory eof
  695                  * in np->n_direofoffset and chop it off as an extra step
  696                  * right here.
  697                  */
  698                 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
  699                 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
  700                         n = np->n_direofoffset - uio->uio_offset;
  701                 break;
  702             default:
  703                 nfs_printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
  704                 bp = NULL;
  705                 break;
  706             };
  707 
  708             if (n > 0) {
  709                     error = uiomove(bp->b_data + on, (int)n, uio);
  710             }
  711             if (vp->v_type == VLNK)
  712                 n = 0;
  713             if (bp != NULL)
  714                 brelse(bp);
  715         } while (error == 0 && uio->uio_resid > 0 && n > 0);
  716         return (error);
  717 }
  718 
  719 /*
  720  * The NFS write path cannot handle iovecs with len > 1. So we need to 
  721  * break up iovecs accordingly (restricting them to wsize).
  722  * For the SYNC case, we can do this with 1 copy (user buffer -> mbuf). 
  723  * For the ASYNC case, 2 copies are needed. The first a copy from the 
  724  * user buffer to a staging buffer and then a second copy from the staging
  725  * buffer to mbufs. This can be optimized by copying from the user buffer
  726  * directly into mbufs and passing the chain down, but that requires a 
  727  * fair amount of re-working of the relevant codepaths (and can be done
  728  * later).
  729  */
  730 static int
  731 nfs_directio_write(vp, uiop, cred, ioflag)
  732         struct vnode *vp;
  733         struct uio *uiop;
  734         struct ucred *cred;
  735         int ioflag;
  736 {
  737         int error;
  738         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  739         struct thread *td = uiop->uio_td;
  740         int size;
  741         int wsize;
  742         
  743         mtx_lock(&nmp->nm_mtx);
  744         wsize = nmp->nm_wsize;
  745         mtx_unlock(&nmp->nm_mtx);
  746         if (ioflag & IO_SYNC) {
  747                 int iomode, must_commit;
  748                 struct uio uio;
  749                 struct iovec iov;
  750 do_sync:
  751                 while (uiop->uio_resid > 0) {
  752                         size = min(uiop->uio_resid, wsize);
  753                         size = min(uiop->uio_iov->iov_len, size);
  754                         iov.iov_base = uiop->uio_iov->iov_base;
  755                         iov.iov_len = size;
  756                         uio.uio_iov = &iov;
  757                         uio.uio_iovcnt = 1;
  758                         uio.uio_offset = uiop->uio_offset;
  759                         uio.uio_resid = size;
  760                         uio.uio_segflg = UIO_USERSPACE;
  761                         uio.uio_rw = UIO_WRITE;
  762                         uio.uio_td = td;
  763                         iomode = NFSV3WRITE_FILESYNC;
  764                         error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, 
  765                                                       &iomode, &must_commit);
  766                         KASSERT((must_commit == 0), 
  767                                 ("nfs_directio_write: Did not commit write"));
  768                         if (error)
  769                                 return (error);
  770                         uiop->uio_offset += size;
  771                         uiop->uio_resid -= size;
  772                         if (uiop->uio_iov->iov_len <= size) {
  773                                 uiop->uio_iovcnt--;
  774                                 uiop->uio_iov++;
  775                         } else {
  776                                 uiop->uio_iov->iov_base = 
  777                                         (char *)uiop->uio_iov->iov_base + size;
  778                                 uiop->uio_iov->iov_len -= size;
  779                         }
  780                 }
  781         } else {
  782                 struct uio *t_uio;
  783                 struct iovec *t_iov;
  784                 struct buf *bp;
  785                 
  786                 /*
  787                  * Break up the write into blocksize chunks and hand these
  788                  * over to nfsiod's for write back.
  789                  * Unfortunately, this incurs a copy of the data. Since 
  790                  * the user could modify the buffer before the write is 
  791                  * initiated.
  792                  * 
  793                  * The obvious optimization here is that one of the 2 copies
  794                  * in the async write path can be eliminated by copying the
  795                  * data here directly into mbufs and passing the mbuf chain
  796                  * down. But that will require a fair amount of re-working
  797                  * of the code and can be done if there's enough interest
  798                  * in NFS directio access.
  799                  */
  800                 while (uiop->uio_resid > 0) {
  801                         size = min(uiop->uio_resid, wsize);
  802                         size = min(uiop->uio_iov->iov_len, size);
  803                         bp = getpbuf(&nfs_pbuf_freecnt);
  804                         t_uio = malloc(sizeof(struct uio), M_NFSDIRECTIO, M_WAITOK);
  805                         t_iov = malloc(sizeof(struct iovec), M_NFSDIRECTIO, M_WAITOK);
  806                         t_iov->iov_base = malloc(size, M_NFSDIRECTIO, M_WAITOK);
  807                         t_iov->iov_len = size;
  808                         t_uio->uio_iov = t_iov;
  809                         t_uio->uio_iovcnt = 1;
  810                         t_uio->uio_offset = uiop->uio_offset;
  811                         t_uio->uio_resid = size;
  812                         t_uio->uio_segflg = UIO_SYSSPACE;
  813                         t_uio->uio_rw = UIO_WRITE;
  814                         t_uio->uio_td = td;
  815                         KASSERT(uiop->uio_segflg == UIO_USERSPACE ||
  816                             uiop->uio_segflg == UIO_SYSSPACE,
  817                             ("nfs_directio_write: Bad uio_segflg"));
  818                         if (uiop->uio_segflg == UIO_USERSPACE) {
  819                                 error = copyin(uiop->uio_iov->iov_base,
  820                                     t_iov->iov_base, size);
  821                                 if (error != 0)
  822                                         goto err_free;
  823                         } else
  824                                 /*
  825                                  * UIO_SYSSPACE may never happen, but handle
  826                                  * it just in case it does.
  827                                  */
  828                                 bcopy(uiop->uio_iov->iov_base, t_iov->iov_base,
  829                                     size);
  830                         bp->b_flags |= B_DIRECT;
  831                         bp->b_iocmd = BIO_WRITE;
  832                         if (cred != NOCRED) {
  833                                 crhold(cred);
  834                                 bp->b_wcred = cred;
  835                         } else 
  836                                 bp->b_wcred = NOCRED;                   
  837                         bp->b_caller1 = (void *)t_uio;
  838                         bp->b_vp = vp;
  839                         error = nfs_asyncio(nmp, bp, NOCRED, td);
  840 err_free:
  841                         if (error) {
  842                                 free(t_iov->iov_base, M_NFSDIRECTIO);
  843                                 free(t_iov, M_NFSDIRECTIO);
  844                                 free(t_uio, M_NFSDIRECTIO);
  845                                 bp->b_vp = NULL;
  846                                 relpbuf(bp, &nfs_pbuf_freecnt);
  847                                 if (error == EINTR)
  848                                         return (error);
  849                                 goto do_sync;
  850                         }
  851                         uiop->uio_offset += size;
  852                         uiop->uio_resid -= size;
  853                         if (uiop->uio_iov->iov_len <= size) {
  854                                 uiop->uio_iovcnt--;
  855                                 uiop->uio_iov++;
  856                         } else {
  857                                 uiop->uio_iov->iov_base = 
  858                                         (char *)uiop->uio_iov->iov_base + size;
  859                                 uiop->uio_iov->iov_len -= size;
  860                         }
  861                 }
  862         }
  863         return (0);
  864 }
  865 
  866 /*
  867  * Vnode op for write using bio
  868  */
  869 int
  870 nfs_write(struct vop_write_args *ap)
  871 {
  872         int biosize;
  873         struct uio *uio = ap->a_uio;
  874         struct thread *td = uio->uio_td;
  875         struct vnode *vp = ap->a_vp;
  876         struct nfsnode *np = VTONFS(vp);
  877         struct ucred *cred = ap->a_cred;
  878         int ioflag = ap->a_ioflag;
  879         struct buf *bp;
  880         struct vattr vattr;
  881         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  882         daddr_t lbn;
  883         off_t end;
  884         int bcount;
  885         int n, on, error = 0;
  886 
  887         KASSERT(uio->uio_rw == UIO_WRITE, ("nfs_write mode"));
  888         KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
  889             ("nfs_write proc"));
  890         if (vp->v_type != VREG)
  891                 return (EIO);
  892         mtx_lock(&np->n_mtx);
  893         if (np->n_flag & NWRITEERR) {
  894                 np->n_flag &= ~NWRITEERR;
  895                 mtx_unlock(&np->n_mtx);
  896                 return (np->n_error);
  897         } else
  898                 mtx_unlock(&np->n_mtx);
  899         mtx_lock(&nmp->nm_mtx);
  900         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  901             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
  902                 mtx_unlock(&nmp->nm_mtx);
  903                 (void)nfs_fsinfo(nmp, vp, cred, td);
  904         } else
  905                 mtx_unlock(&nmp->nm_mtx);
  906 
  907         /*
  908          * Synchronously flush pending buffers if we are in synchronous
  909          * mode or if we are appending.
  910          */
  911         if (ioflag & (IO_APPEND | IO_SYNC)) {
  912                 mtx_lock(&np->n_mtx);
  913                 if (np->n_flag & NMODIFIED) {
  914                         mtx_unlock(&np->n_mtx);
  915 #ifdef notyet /* Needs matching nonblock semantics elsewhere, too. */
  916                         /*
  917                          * Require non-blocking, synchronous writes to
  918                          * dirty files to inform the program it needs
  919                          * to fsync(2) explicitly.
  920                          */
  921                         if (ioflag & IO_NDELAY)
  922                                 return (EAGAIN);
  923 #endif
  924 flush_and_restart:
  925                         np->n_attrstamp = 0;
  926                         KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
  927                         error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
  928                         if (error)
  929                                 return (error);
  930                 } else
  931                         mtx_unlock(&np->n_mtx);
  932         }
  933 
  934         /*
  935          * If IO_APPEND then load uio_offset.  We restart here if we cannot
  936          * get the append lock.
  937          */
  938         if (ioflag & IO_APPEND) {
  939                 np->n_attrstamp = 0;
  940                 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
  941                 error = VOP_GETATTR(vp, &vattr, cred);
  942                 if (error)
  943                         return (error);
  944                 mtx_lock(&np->n_mtx);
  945                 uio->uio_offset = np->n_size;
  946                 mtx_unlock(&np->n_mtx);
  947         }
  948 
  949         if (uio->uio_offset < 0)
  950                 return (EINVAL);
  951         end = uio->uio_offset + uio->uio_resid;
  952         if (end > nmp->nm_maxfilesize || end < uio->uio_offset)
  953                 return (EFBIG);
  954         if (uio->uio_resid == 0)
  955                 return (0);
  956 
  957         if (nfs_directio_enable && (ioflag & IO_DIRECT) && vp->v_type == VREG)
  958                 return nfs_directio_write(vp, uio, cred, ioflag);
  959 
  960         /*
  961          * Maybe this should be above the vnode op call, but so long as
  962          * file servers have no limits, i don't think it matters
  963          */
  964         if (vn_rlimit_fsize(vp, uio, td))
  965                 return (EFBIG);
  966 
  967         biosize = vp->v_bufobj.bo_bsize;
  968         /*
  969          * Find all of this file's B_NEEDCOMMIT buffers.  If our writes
  970          * would exceed the local maximum per-file write commit size when
  971          * combined with those, we must decide whether to flush,
  972          * go synchronous, or return error.  We don't bother checking
  973          * IO_UNIT -- we just make all writes atomic anyway, as there's
  974          * no point optimizing for something that really won't ever happen.
  975          */
  976         if (!(ioflag & IO_SYNC)) {
  977                 int nflag;
  978 
  979                 mtx_lock(&np->n_mtx);
  980                 nflag = np->n_flag;
  981                 mtx_unlock(&np->n_mtx);         
  982                 int needrestart = 0;
  983                 if (nmp->nm_wcommitsize < uio->uio_resid) {
  984                         /*
  985                          * If this request could not possibly be completed
  986                          * without exceeding the maximum outstanding write
  987                          * commit size, see if we can convert it into a
  988                          * synchronous write operation.
  989                          */
  990                         if (ioflag & IO_NDELAY)
  991                                 return (EAGAIN);
  992                         ioflag |= IO_SYNC;
  993                         if (nflag & NMODIFIED)
  994                                 needrestart = 1;
  995                 } else if (nflag & NMODIFIED) {
  996                         int wouldcommit = 0;
  997                         BO_LOCK(&vp->v_bufobj);
  998                         if (vp->v_bufobj.bo_dirty.bv_cnt != 0) {
  999                                 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd,
 1000                                     b_bobufs) {
 1001                                         if (bp->b_flags & B_NEEDCOMMIT)
 1002                                                 wouldcommit += bp->b_bcount;
 1003                                 }
 1004                         }
 1005                         BO_UNLOCK(&vp->v_bufobj);
 1006                         /*
 1007                          * Since we're not operating synchronously and
 1008                          * bypassing the buffer cache, we are in a commit
 1009                          * and holding all of these buffers whether
 1010                          * transmitted or not.  If not limited, this
 1011                          * will lead to the buffer cache deadlocking,
 1012                          * as no one else can flush our uncommitted buffers.
 1013                          */
 1014                         wouldcommit += uio->uio_resid;
 1015                         /*
 1016                          * If we would initially exceed the maximum
 1017                          * outstanding write commit size, flush and restart.
 1018                          */
 1019                         if (wouldcommit > nmp->nm_wcommitsize)
 1020                                 needrestart = 1;
 1021                 }
 1022                 if (needrestart)
 1023                         goto flush_and_restart;
 1024         }
 1025 
 1026         do {
 1027                 nfsstats.biocache_writes++;
 1028                 lbn = uio->uio_offset / biosize;
 1029                 on = uio->uio_offset & (biosize-1);
 1030                 n = min((unsigned)(biosize - on), uio->uio_resid);
 1031 again:
 1032                 /*
 1033                  * Handle direct append and file extension cases, calculate
 1034                  * unaligned buffer size.
 1035                  */
 1036                 mtx_lock(&np->n_mtx);
 1037                 if (uio->uio_offset == np->n_size && n) {
 1038                         mtx_unlock(&np->n_mtx);
 1039                         /*
 1040                          * Get the buffer (in its pre-append state to maintain
 1041                          * B_CACHE if it was previously set).  Resize the
 1042                          * nfsnode after we have locked the buffer to prevent
 1043                          * readers from reading garbage.
 1044                          */
 1045                         bcount = on;
 1046                         bp = nfs_getcacheblk(vp, lbn, bcount, td);
 1047 
 1048                         if (bp != NULL) {
 1049                                 long save;
 1050 
 1051                                 mtx_lock(&np->n_mtx);
 1052                                 np->n_size = uio->uio_offset + n;
 1053                                 np->n_flag |= NMODIFIED;
 1054                                 vnode_pager_setsize(vp, np->n_size);
 1055                                 mtx_unlock(&np->n_mtx);
 1056 
 1057                                 save = bp->b_flags & B_CACHE;
 1058                                 bcount += n;
 1059                                 allocbuf(bp, bcount);
 1060                                 bp->b_flags |= save;
 1061                         }
 1062                 } else {
 1063                         /*
 1064                          * Obtain the locked cache block first, and then
 1065                          * adjust the file's size as appropriate.
 1066                          */
 1067                         bcount = on + n;
 1068                         if ((off_t)lbn * biosize + bcount < np->n_size) {
 1069                                 if ((off_t)(lbn + 1) * biosize < np->n_size)
 1070                                         bcount = biosize;
 1071                                 else
 1072                                         bcount = np->n_size - (off_t)lbn * biosize;
 1073                         }
 1074                         mtx_unlock(&np->n_mtx);
 1075                         bp = nfs_getcacheblk(vp, lbn, bcount, td);
 1076                         mtx_lock(&np->n_mtx);
 1077                         if (uio->uio_offset + n > np->n_size) {
 1078                                 np->n_size = uio->uio_offset + n;
 1079                                 np->n_flag |= NMODIFIED;
 1080                                 vnode_pager_setsize(vp, np->n_size);
 1081                         }
 1082                         mtx_unlock(&np->n_mtx);
 1083                 }
 1084 
 1085                 if (!bp) {
 1086                         error = nfs_sigintr(nmp, td);
 1087                         if (!error)
 1088                                 error = EINTR;
 1089                         break;
 1090                 }
 1091 
 1092                 /*
 1093                  * Issue a READ if B_CACHE is not set.  In special-append
 1094                  * mode, B_CACHE is based on the buffer prior to the write
 1095                  * op and is typically set, avoiding the read.  If a read
 1096                  * is required in special append mode, the server will
 1097                  * probably send us a short-read since we extended the file
 1098                  * on our end, resulting in b_resid == 0 and, thusly,
 1099                  * B_CACHE getting set.
 1100                  *
 1101                  * We can also avoid issuing the read if the write covers
 1102                  * the entire buffer.  We have to make sure the buffer state
 1103                  * is reasonable in this case since we will not be initiating
 1104                  * I/O.  See the comments in kern/vfs_bio.c's getblk() for
 1105                  * more information.
 1106                  *
 1107                  * B_CACHE may also be set due to the buffer being cached
 1108                  * normally.
 1109                  */
 1110 
 1111                 if (on == 0 && n == bcount) {
 1112                         bp->b_flags |= B_CACHE;
 1113                         bp->b_flags &= ~B_INVAL;
 1114                         bp->b_ioflags &= ~BIO_ERROR;
 1115                 }
 1116 
 1117                 if ((bp->b_flags & B_CACHE) == 0) {
 1118                         bp->b_iocmd = BIO_READ;
 1119                         vfs_busy_pages(bp, 0);
 1120                         error = nfs_doio(vp, bp, cred, td);
 1121                         if (error) {
 1122                                 brelse(bp);
 1123                                 break;
 1124                         }
 1125                 }
 1126                 if (bp->b_wcred == NOCRED)
 1127                         bp->b_wcred = crhold(cred);
 1128                 mtx_lock(&np->n_mtx);
 1129                 np->n_flag |= NMODIFIED;
 1130                 mtx_unlock(&np->n_mtx);
 1131 
 1132                 /*
 1133                  * If dirtyend exceeds file size, chop it down.  This should
 1134                  * not normally occur but there is an append race where it
 1135                  * might occur XXX, so we log it.
 1136                  *
 1137                  * If the chopping creates a reverse-indexed or degenerate
 1138                  * situation with dirtyoff/end, we 0 both of them.
 1139                  */
 1140 
 1141                 if (bp->b_dirtyend > bcount) {
 1142                         nfs_printf("NFS append race @%lx:%d\n",
 1143                             (long)bp->b_blkno * DEV_BSIZE,
 1144                             bp->b_dirtyend - bcount);
 1145                         bp->b_dirtyend = bcount;
 1146                 }
 1147 
 1148                 if (bp->b_dirtyoff >= bp->b_dirtyend)
 1149                         bp->b_dirtyoff = bp->b_dirtyend = 0;
 1150 
 1151                 /*
 1152                  * If the new write will leave a contiguous dirty
 1153                  * area, just update the b_dirtyoff and b_dirtyend,
 1154                  * otherwise force a write rpc of the old dirty area.
 1155                  *
 1156                  * While it is possible to merge discontiguous writes due to
 1157                  * our having a B_CACHE buffer ( and thus valid read data
 1158                  * for the hole), we don't because it could lead to
 1159                  * significant cache coherency problems with multiple clients,
 1160                  * especially if locking is implemented later on.
 1161                  *
 1162                  * as an optimization we could theoretically maintain
 1163                  * a linked list of discontinuous areas, but we would still
 1164                  * have to commit them separately so there isn't much
 1165                  * advantage to it except perhaps a bit of asynchronization.
 1166                  */
 1167 
 1168                 if (bp->b_dirtyend > 0 &&
 1169                     (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
 1170                         if (bwrite(bp) == EINTR) {
 1171                                 error = EINTR;
 1172                                 break;
 1173                         }
 1174                         goto again;
 1175                 }
 1176 
 1177                 error = uiomove((char *)bp->b_data + on, n, uio);
 1178 
 1179                 /*
 1180                  * Since this block is being modified, it must be written
 1181                  * again and not just committed.  Since write clustering does
 1182                  * not work for the stage 1 data write, only the stage 2
 1183                  * commit rpc, we have to clear B_CLUSTEROK as well.
 1184                  */
 1185                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 1186 
 1187                 if (error) {
 1188                         bp->b_ioflags |= BIO_ERROR;
 1189                         brelse(bp);
 1190                         break;
 1191                 }
 1192 
 1193                 /*
 1194                  * Only update dirtyoff/dirtyend if not a degenerate
 1195                  * condition.
 1196                  */
 1197                 if (n) {
 1198                         if (bp->b_dirtyend > 0) {
 1199                                 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
 1200                                 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
 1201                         } else {
 1202                                 bp->b_dirtyoff = on;
 1203                                 bp->b_dirtyend = on + n;
 1204                         }
 1205                         vfs_bio_set_valid(bp, on, n);
 1206                 }
 1207 
 1208                 /*
 1209                  * If IO_SYNC do bwrite().
 1210                  *
 1211                  * IO_INVAL appears to be unused.  The idea appears to be
 1212                  * to turn off caching in this case.  Very odd.  XXX
 1213                  */
 1214                 if ((ioflag & IO_SYNC)) {
 1215                         if (ioflag & IO_INVAL)
 1216                                 bp->b_flags |= B_NOCACHE;
 1217                         error = bwrite(bp);
 1218                         if (error)
 1219                                 break;
 1220                 } else if ((n + on) == biosize) {
 1221                         bp->b_flags |= B_ASYNC;
 1222                         (void) (nmp->nm_rpcops->nr_writebp)(bp, 0, NULL);
 1223                 } else {
 1224                         bdwrite(bp);
 1225                 }
 1226         } while (uio->uio_resid > 0 && n > 0);
 1227 
 1228         return (error);
 1229 }
 1230 
 1231 /*
 1232  * Get an nfs cache block.
 1233  *
 1234  * Allocate a new one if the block isn't currently in the cache
 1235  * and return the block marked busy. If the calling process is
 1236  * interrupted by a signal for an interruptible mount point, return
 1237  * NULL.
 1238  *
 1239  * The caller must carefully deal with the possible B_INVAL state of
 1240  * the buffer.  nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
 1241  * indirectly), so synchronous reads can be issued without worrying about
 1242  * the B_INVAL state.  We have to be a little more careful when dealing
 1243  * with writes (see comments in nfs_write()) when extending a file past
 1244  * its EOF.
 1245  */
 1246 static struct buf *
 1247 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
 1248 {
 1249         struct buf *bp;
 1250         struct mount *mp;
 1251         struct nfsmount *nmp;
 1252 
 1253         mp = vp->v_mount;
 1254         nmp = VFSTONFS(mp);
 1255 
 1256         if (nmp->nm_flag & NFSMNT_INT) {
 1257                 sigset_t oldset;
 1258 
 1259                 nfs_set_sigmask(td, &oldset);
 1260                 bp = getblk(vp, bn, size, NFS_PCATCH, 0, 0);
 1261                 nfs_restore_sigmask(td, &oldset);
 1262                 while (bp == NULL) {
 1263                         if (nfs_sigintr(nmp, td))
 1264                                 return (NULL);
 1265                         bp = getblk(vp, bn, size, 0, 2 * hz, 0);
 1266                 }
 1267         } else {
 1268                 bp = getblk(vp, bn, size, 0, 0, 0);
 1269         }
 1270 
 1271         if (vp->v_type == VREG)
 1272                 bp->b_blkno = bn * (vp->v_bufobj.bo_bsize / DEV_BSIZE);
 1273         return (bp);
 1274 }
 1275 
 1276 /*
 1277  * Flush and invalidate all dirty buffers. If another process is already
 1278  * doing the flush, just wait for completion.
 1279  */
 1280 int
 1281 nfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
 1282 {
 1283         struct nfsnode *np = VTONFS(vp);
 1284         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 1285         int error = 0, slpflag, slptimeo;
 1286         int old_lock = 0;
 1287 
 1288         ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf");
 1289 
 1290         if ((nmp->nm_flag & NFSMNT_INT) == 0)
 1291                 intrflg = 0;
 1292         if (intrflg) {
 1293                 slpflag = NFS_PCATCH;
 1294                 slptimeo = 2 * hz;
 1295         } else {
 1296                 slpflag = 0;
 1297                 slptimeo = 0;
 1298         }
 1299 
 1300         old_lock = nfs_upgrade_vnlock(vp);
 1301         if (vp->v_iflag & VI_DOOMED) {
 1302                 /*
 1303                  * Since vgonel() uses the generic vinvalbuf() to flush
 1304                  * dirty buffers and it does not call this function, it
 1305                  * is safe to just return OK when VI_DOOMED is set.
 1306                  */
 1307                 nfs_downgrade_vnlock(vp, old_lock);
 1308                 return (0);
 1309         }
 1310 
 1311         /*
 1312          * Now, flush as required.
 1313          */
 1314         if ((flags & V_SAVE) && (vp->v_bufobj.bo_object != NULL)) {
 1315                 VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
 1316                 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
 1317                 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
 1318                 /*
 1319                  * If the page clean was interrupted, fail the invalidation.
 1320                  * Not doing so, we run the risk of losing dirty pages in the 
 1321                  * vinvalbuf() call below.
 1322                  */
 1323                 if (intrflg && (error = nfs_sigintr(nmp, td)))
 1324                         goto out;
 1325         }
 1326 
 1327         error = vinvalbuf(vp, flags, slpflag, 0);
 1328         while (error) {
 1329                 if (intrflg && (error = nfs_sigintr(nmp, td)))
 1330                         goto out;
 1331                 error = vinvalbuf(vp, flags, 0, slptimeo);
 1332         }
 1333         mtx_lock(&np->n_mtx);
 1334         if (np->n_directio_asyncwr == 0)
 1335                 np->n_flag &= ~NMODIFIED;
 1336         mtx_unlock(&np->n_mtx);
 1337 out:
 1338         nfs_downgrade_vnlock(vp, old_lock);
 1339         return error;
 1340 }
 1341 
 1342 /*
 1343  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
 1344  * This is mainly to avoid queueing async I/O requests when the nfsiods
 1345  * are all hung on a dead server.
 1346  *
 1347  * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
 1348  * is eventually dequeued by the async daemon, nfs_doio() *will*.
 1349  */
 1350 int
 1351 nfs_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td)
 1352 {
 1353         int iod;
 1354         int gotiod;
 1355         int slpflag = 0;
 1356         int slptimeo = 0;
 1357         int error, error2;
 1358 
 1359         /*
 1360          * Commits are usually short and sweet so lets save some cpu and
 1361          * leave the async daemons for more important rpc's (such as reads
 1362          * and writes).
 1363          *
 1364          * Readdirplus RPCs do vget()s to acquire the vnodes for entries
 1365          * in the directory in order to update attributes. This can deadlock
 1366          * with another thread that is waiting for async I/O to be done by
 1367          * an nfsiod thread while holding a lock on one of these vnodes.
 1368          * To avoid this deadlock, don't allow the async nfsiod threads to
 1369          * perform Readdirplus RPCs.
 1370          */
 1371         mtx_lock(&nfs_iod_mtx);
 1372         if ((bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
 1373              (nmp->nm_bufqiods > nfs_numasync / 2)) ||
 1374             (bp->b_vp->v_type == VDIR && (nmp->nm_flag & NFSMNT_RDIRPLUS))) {
 1375                 mtx_unlock(&nfs_iod_mtx);
 1376                 return(EIO);
 1377         }
 1378 again:
 1379         if (nmp->nm_flag & NFSMNT_INT)
 1380                 slpflag = NFS_PCATCH;
 1381         gotiod = FALSE;
 1382 
 1383         /*
 1384          * Find a free iod to process this request.
 1385          */
 1386         for (iod = 0; iod < nfs_numasync; iod++)
 1387                 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE) {
 1388                         gotiod = TRUE;
 1389                         break;
 1390                 }
 1391 
 1392         /*
 1393          * Try to create one if none are free.
 1394          */
 1395         if (!gotiod)
 1396                 nfs_nfsiodnew();
 1397         else {
 1398                 /*
 1399                  * Found one, so wake it up and tell it which
 1400                  * mount to process.
 1401                  */
 1402                 NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n",
 1403                     iod, nmp));
 1404                 nfs_iodwant[iod] = NFSIOD_NOT_AVAILABLE;
 1405                 nfs_iodmount[iod] = nmp;
 1406                 nmp->nm_bufqiods++;
 1407                 wakeup(&nfs_iodwant[iod]);
 1408         }
 1409 
 1410         /*
 1411          * If none are free, we may already have an iod working on this mount
 1412          * point.  If so, it will process our request.
 1413          */
 1414         if (!gotiod) {
 1415                 if (nmp->nm_bufqiods > 0) {
 1416                         NFS_DPF(ASYNCIO,
 1417                 ("nfs_asyncio: %d iods are already processing mount %p\n",
 1418                                  nmp->nm_bufqiods, nmp));
 1419                         gotiod = TRUE;
 1420                 }
 1421         }
 1422 
 1423         /*
 1424          * If we have an iod which can process the request, then queue
 1425          * the buffer.
 1426          */
 1427         if (gotiod) {
 1428                 /*
 1429                  * Ensure that the queue never grows too large.  We still want
 1430                  * to asynchronize so we block rather then return EIO.
 1431                  */
 1432                 while (nmp->nm_bufqlen >= 2 * nfs_numasync) {
 1433                         NFS_DPF(ASYNCIO,
 1434                 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
 1435                         nmp->nm_bufqwant = TRUE;
 1436                         error = nfs_msleep(td, &nmp->nm_bufq, &nfs_iod_mtx, 
 1437                                            slpflag | PRIBIO,
 1438                                            "nfsaio", slptimeo);
 1439                         if (error) {
 1440                                 error2 = nfs_sigintr(nmp, td);
 1441                                 if (error2) {
 1442                                         mtx_unlock(&nfs_iod_mtx);
 1443                                         return (error2);
 1444                                 }
 1445                                 if (slpflag == NFS_PCATCH) {
 1446                                         slpflag = 0;
 1447                                         slptimeo = 2 * hz;
 1448                                 }
 1449                         }
 1450                         /*
 1451                          * We might have lost our iod while sleeping,
 1452                          * so check and loop if nescessary.
 1453                          */
 1454                         goto again;
 1455                 }
 1456 
 1457                 /* We might have lost our nfsiod */
 1458                 if (nmp->nm_bufqiods == 0) {
 1459                         NFS_DPF(ASYNCIO,
 1460 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
 1461                         goto again;
 1462                 }
 1463 
 1464                 if (bp->b_iocmd == BIO_READ) {
 1465                         if (bp->b_rcred == NOCRED && cred != NOCRED)
 1466                                 bp->b_rcred = crhold(cred);
 1467                 } else {
 1468                         if (bp->b_wcred == NOCRED && cred != NOCRED)
 1469                                 bp->b_wcred = crhold(cred);
 1470                 }
 1471 
 1472                 if (bp->b_flags & B_REMFREE)
 1473                         bremfreef(bp);
 1474                 BUF_KERNPROC(bp);
 1475                 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
 1476                 nmp->nm_bufqlen++;
 1477                 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
 1478                         mtx_lock(&(VTONFS(bp->b_vp))->n_mtx);                   
 1479                         VTONFS(bp->b_vp)->n_flag |= NMODIFIED;
 1480                         VTONFS(bp->b_vp)->n_directio_asyncwr++;
 1481                         mtx_unlock(&(VTONFS(bp->b_vp))->n_mtx);
 1482                 }
 1483                 mtx_unlock(&nfs_iod_mtx);
 1484                 return (0);
 1485         }
 1486 
 1487         mtx_unlock(&nfs_iod_mtx);
 1488 
 1489         /*
 1490          * All the iods are busy on other mounts, so return EIO to
 1491          * force the caller to process the i/o synchronously.
 1492          */
 1493         NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
 1494         return (EIO);
 1495 }
 1496 
 1497 void
 1498 nfs_doio_directwrite(struct buf *bp)
 1499 {
 1500         int iomode, must_commit;
 1501         struct uio *uiop = (struct uio *)bp->b_caller1;
 1502         char *iov_base = uiop->uio_iov->iov_base;
 1503         struct nfsmount *nmp = VFSTONFS(bp->b_vp->v_mount);
 1504         
 1505         iomode = NFSV3WRITE_FILESYNC;
 1506         uiop->uio_td = NULL; /* NULL since we're in nfsiod */
 1507         (nmp->nm_rpcops->nr_writerpc)(bp->b_vp, uiop, bp->b_wcred, &iomode, &must_commit);
 1508         KASSERT((must_commit == 0), ("nfs_doio_directwrite: Did not commit write"));
 1509         free(iov_base, M_NFSDIRECTIO);
 1510         free(uiop->uio_iov, M_NFSDIRECTIO);
 1511         free(uiop, M_NFSDIRECTIO);
 1512         if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
 1513                 struct nfsnode *np = VTONFS(bp->b_vp);
 1514                 mtx_lock(&np->n_mtx);
 1515                 np->n_directio_asyncwr--;
 1516                 if (np->n_directio_asyncwr == 0) {
 1517                         VTONFS(bp->b_vp)->n_flag &= ~NMODIFIED;
 1518                         if ((np->n_flag & NFSYNCWAIT)) {
 1519                                 np->n_flag &= ~NFSYNCWAIT;
 1520                                 wakeup((caddr_t)&np->n_directio_asyncwr);
 1521                         }
 1522                 }
 1523                 mtx_unlock(&np->n_mtx);
 1524         }
 1525         bp->b_vp = NULL;
 1526         relpbuf(bp, &nfs_pbuf_freecnt);
 1527 }
 1528 
 1529 /*
 1530  * Do an I/O operation to/from a cache block. This may be called
 1531  * synchronously or from an nfsiod.
 1532  */
 1533 int
 1534 nfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
 1535 {
 1536         struct uio *uiop;
 1537         struct nfsnode *np;
 1538         struct nfsmount *nmp;
 1539         int error = 0, iomode, must_commit = 0;
 1540         struct uio uio;
 1541         struct iovec io;
 1542         struct proc *p = td ? td->td_proc : NULL;
 1543         uint8_t iocmd;
 1544         
 1545         np = VTONFS(vp);
 1546         nmp = VFSTONFS(vp->v_mount);
 1547         uiop = &uio;
 1548         uiop->uio_iov = &io;
 1549         uiop->uio_iovcnt = 1;
 1550         uiop->uio_segflg = UIO_SYSSPACE;
 1551         uiop->uio_td = td;
 1552 
 1553         /*
 1554          * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
 1555          * do this here so we do not have to do it in all the code that
 1556          * calls us.
 1557          */
 1558         bp->b_flags &= ~B_INVAL;
 1559         bp->b_ioflags &= ~BIO_ERROR;
 1560 
 1561         KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
 1562         iocmd = bp->b_iocmd;
 1563         if (iocmd == BIO_READ) {
 1564             io.iov_len = uiop->uio_resid = bp->b_bcount;
 1565             io.iov_base = bp->b_data;
 1566             uiop->uio_rw = UIO_READ;
 1567 
 1568             switch (vp->v_type) {
 1569             case VREG:
 1570                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
 1571                 nfsstats.read_bios++;
 1572                 error = (nmp->nm_rpcops->nr_readrpc)(vp, uiop, cr);
 1573 
 1574                 if (!error) {
 1575                     if (uiop->uio_resid) {
 1576                         /*
 1577                          * If we had a short read with no error, we must have
 1578                          * hit a file hole.  We should zero-fill the remainder.
 1579                          * This can also occur if the server hits the file EOF.
 1580                          *
 1581                          * Holes used to be able to occur due to pending
 1582                          * writes, but that is not possible any longer.
 1583                          */
 1584                         int nread = bp->b_bcount - uiop->uio_resid;
 1585                         int left  = uiop->uio_resid;
 1586 
 1587                         if (left > 0)
 1588                                 bzero((char *)bp->b_data + nread, left);
 1589                         uiop->uio_resid = 0;
 1590                     }
 1591                 }
 1592                 /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */
 1593                 if (p && (vp->v_vflag & VV_TEXT)) {
 1594                         mtx_lock(&np->n_mtx);
 1595                         if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &np->n_vattr.va_mtime)) {
 1596                                 mtx_unlock(&np->n_mtx);
 1597                                 PROC_LOCK(p);
 1598                                 killproc(p, "text file modification");
 1599                                 PROC_UNLOCK(p);
 1600                         } else
 1601                                 mtx_unlock(&np->n_mtx);
 1602                 }
 1603                 break;
 1604             case VLNK:
 1605                 uiop->uio_offset = (off_t)0;
 1606                 nfsstats.readlink_bios++;
 1607                 error = (nmp->nm_rpcops->nr_readlinkrpc)(vp, uiop, cr);
 1608                 break;
 1609             case VDIR:
 1610                 nfsstats.readdir_bios++;
 1611                 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
 1612                 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) {
 1613                         error = nfs_readdirplusrpc(vp, uiop, cr);
 1614                         if (error == NFSERR_NOTSUPP)
 1615                                 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
 1616                 }
 1617                 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
 1618                         error = nfs_readdirrpc(vp, uiop, cr);
 1619                 /*
 1620                  * end-of-directory sets B_INVAL but does not generate an
 1621                  * error.
 1622                  */
 1623                 if (error == 0 && uiop->uio_resid == bp->b_bcount)
 1624                         bp->b_flags |= B_INVAL;
 1625                 break;
 1626             default:
 1627                 nfs_printf("nfs_doio:  type %x unexpected\n", vp->v_type);
 1628                 break;
 1629             };
 1630             if (error) {
 1631                 bp->b_ioflags |= BIO_ERROR;
 1632                 bp->b_error = error;
 1633             }
 1634         } else {
 1635             /*
 1636              * If we only need to commit, try to commit
 1637              */
 1638             if (bp->b_flags & B_NEEDCOMMIT) {
 1639                     int retv;
 1640                     off_t off;
 1641 
 1642                     off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
 1643                     retv = (nmp->nm_rpcops->nr_commit)(
 1644                                 vp, off, bp->b_dirtyend-bp->b_dirtyoff,
 1645                                 bp->b_wcred, td);
 1646                     if (retv == 0) {
 1647                             bp->b_dirtyoff = bp->b_dirtyend = 0;
 1648                             bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 1649                             bp->b_resid = 0;
 1650                             bufdone(bp);
 1651                             return (0);
 1652                     }
 1653                     if (retv == NFSERR_STALEWRITEVERF) {
 1654                             nfs_clearcommit(vp->v_mount);
 1655                     }
 1656             }
 1657 
 1658             /*
 1659              * Setup for actual write
 1660              */
 1661             mtx_lock(&np->n_mtx);
 1662             if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
 1663                 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
 1664             mtx_unlock(&np->n_mtx);
 1665 
 1666             if (bp->b_dirtyend > bp->b_dirtyoff) {
 1667                 io.iov_len = uiop->uio_resid = bp->b_dirtyend
 1668                     - bp->b_dirtyoff;
 1669                 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
 1670                     + bp->b_dirtyoff;
 1671                 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
 1672                 uiop->uio_rw = UIO_WRITE;
 1673                 nfsstats.write_bios++;
 1674 
 1675                 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
 1676                     iomode = NFSV3WRITE_UNSTABLE;
 1677                 else
 1678                     iomode = NFSV3WRITE_FILESYNC;
 1679 
 1680                 error = (nmp->nm_rpcops->nr_writerpc)(vp, uiop, cr, &iomode, &must_commit);
 1681 
 1682                 /*
 1683                  * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
 1684                  * to cluster the buffers needing commit.  This will allow
 1685                  * the system to submit a single commit rpc for the whole
 1686                  * cluster.  We can do this even if the buffer is not 100%
 1687                  * dirty (relative to the NFS blocksize), so we optimize the
 1688                  * append-to-file-case.
 1689                  *
 1690                  * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
 1691                  * cleared because write clustering only works for commit
 1692                  * rpc's, not for the data portion of the write).
 1693                  */
 1694 
 1695                 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
 1696                     bp->b_flags |= B_NEEDCOMMIT;
 1697                     if (bp->b_dirtyoff == 0
 1698                         && bp->b_dirtyend == bp->b_bcount)
 1699                         bp->b_flags |= B_CLUSTEROK;
 1700                 } else {
 1701                     bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 1702                 }
 1703 
 1704                 /*
 1705                  * For an interrupted write, the buffer is still valid
 1706                  * and the write hasn't been pushed to the server yet,
 1707                  * so we can't set BIO_ERROR and report the interruption
 1708                  * by setting B_EINTR. For the B_ASYNC case, B_EINTR
 1709                  * is not relevant, so the rpc attempt is essentially
 1710                  * a noop.  For the case of a V3 write rpc not being
 1711                  * committed to stable storage, the block is still
 1712                  * dirty and requires either a commit rpc or another
 1713                  * write rpc with iomode == NFSV3WRITE_FILESYNC before
 1714                  * the block is reused. This is indicated by setting
 1715                  * the B_DELWRI and B_NEEDCOMMIT flags.
 1716                  *
 1717                  * If the buffer is marked B_PAGING, it does not reside on
 1718                  * the vp's paging queues so we cannot call bdirty().  The
 1719                  * bp in this case is not an NFS cache block so we should
 1720                  * be safe. XXX
 1721                  *
 1722                  * The logic below breaks up errors into recoverable and 
 1723                  * unrecoverable. For the former, we clear B_INVAL|B_NOCACHE
 1724                  * and keep the buffer around for potential write retries.
 1725                  * For the latter (eg ESTALE), we toss the buffer away (B_INVAL)
 1726                  * and save the error in the nfsnode. This is less than ideal 
 1727                  * but necessary. Keeping such buffers around could potentially
 1728                  * cause buffer exhaustion eventually (they can never be written
 1729                  * out, so will get constantly be re-dirtied). It also causes
 1730                  * all sorts of vfs panics. For non-recoverable write errors, 
 1731                  * also invalidate the attrcache, so we'll be forced to go over
 1732                  * the wire for this object, returning an error to user on next
 1733                  * call (most of the time).
 1734                  */
 1735                 if (error == EINTR || error == EIO || error == ETIMEDOUT
 1736                     || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
 1737                         int s;
 1738 
 1739                         s = splbio();
 1740                         bp->b_flags &= ~(B_INVAL|B_NOCACHE);
 1741                         if ((bp->b_flags & B_PAGING) == 0) {
 1742                             bdirty(bp);
 1743                             bp->b_flags &= ~B_DONE;
 1744                         }
 1745                         if (error && (bp->b_flags & B_ASYNC) == 0)
 1746                             bp->b_flags |= B_EINTR;
 1747                         splx(s);
 1748                 } else {
 1749                     if (error) {
 1750                         bp->b_ioflags |= BIO_ERROR;
 1751                         bp->b_flags |= B_INVAL;
 1752                         bp->b_error = np->n_error = error;
 1753                         mtx_lock(&np->n_mtx);
 1754                         np->n_flag |= NWRITEERR;
 1755                         np->n_attrstamp = 0;
 1756                         KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
 1757                         mtx_unlock(&np->n_mtx);
 1758                     }
 1759                     bp->b_dirtyoff = bp->b_dirtyend = 0;
 1760                 }
 1761             } else {
 1762                 bp->b_resid = 0;
 1763                 bufdone(bp);
 1764                 return (0);
 1765             }
 1766         }
 1767         bp->b_resid = uiop->uio_resid;
 1768         if (must_commit)
 1769             nfs_clearcommit(vp->v_mount);
 1770         bufdone(bp);
 1771         return (error);
 1772 }
 1773 
 1774 /*
 1775  * Used to aid in handling ftruncate() operations on the NFS client side.
 1776  * Truncation creates a number of special problems for NFS.  We have to
 1777  * throw away VM pages and buffer cache buffers that are beyond EOF, and
 1778  * we have to properly handle VM pages or (potentially dirty) buffers
 1779  * that straddle the truncation point.
 1780  */
 1781 
 1782 int
 1783 nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize)
 1784 {
 1785         struct nfsnode *np = VTONFS(vp);
 1786         u_quad_t tsize;
 1787         int biosize = vp->v_bufobj.bo_bsize;
 1788         int error = 0;
 1789 
 1790         mtx_lock(&np->n_mtx);
 1791         tsize = np->n_size;
 1792         np->n_size = nsize;
 1793         mtx_unlock(&np->n_mtx);
 1794 
 1795         if (nsize < tsize) {
 1796                 struct buf *bp;
 1797                 daddr_t lbn;
 1798                 int bufsize;
 1799 
 1800                 /*
 1801                  * vtruncbuf() doesn't get the buffer overlapping the 
 1802                  * truncation point.  We may have a B_DELWRI and/or B_CACHE
 1803                  * buffer that now needs to be truncated.
 1804                  */
 1805                 error = vtruncbuf(vp, cred, td, nsize, biosize);
 1806                 lbn = nsize / biosize;
 1807                 bufsize = nsize & (biosize - 1);
 1808                 bp = nfs_getcacheblk(vp, lbn, bufsize, td);
 1809                 if (!bp)
 1810                         return EINTR;
 1811                 if (bp->b_dirtyoff > bp->b_bcount)
 1812                         bp->b_dirtyoff = bp->b_bcount;
 1813                 if (bp->b_dirtyend > bp->b_bcount)
 1814                         bp->b_dirtyend = bp->b_bcount;
 1815                 bp->b_flags |= B_RELBUF;  /* don't leave garbage around */
 1816                 brelse(bp);
 1817         } else {
 1818                 vnode_pager_setsize(vp, nsize);
 1819         }
 1820         return(error);
 1821 }
 1822 

Cache object: 9aeaa3e69dfaa4be87218d5541d1a1de


[ 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.