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

Cache object: d6dd42c908cf1125e22f0f5c5ffef239


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