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  * 3. All advertising materials mentioning features or use of this software
   17  *    must display the following acknowledgement:
   18  *      This product includes software developed by the University of
   19  *      California, Berkeley and its contributors.
   20  * 4. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)nfs_bio.c   8.9 (Berkeley) 3/30/95
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __FBSDID("$FreeBSD: releng/5.1/sys/nfsclient/nfs_bio.c 115041 2003-05-15 21:12:08Z rwatson $");
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/bio.h>
   45 #include <sys/buf.h>
   46 #include <sys/kernel.h>
   47 #include <sys/mount.h>
   48 #include <sys/proc.h>
   49 #include <sys/resourcevar.h>
   50 #include <sys/signalvar.h>
   51 #include <sys/vmmeter.h>
   52 #include <sys/vnode.h>
   53 
   54 #include <vm/vm.h>
   55 #include <vm/vm_extern.h>
   56 #include <vm/vm_page.h>
   57 #include <vm/vm_object.h>
   58 #include <vm/vm_pager.h>
   59 #include <vm/vnode_pager.h>
   60 
   61 #include <nfs/rpcv2.h>
   62 #include <nfs/nfsproto.h>
   63 #include <nfsclient/nfs.h>
   64 #include <nfsclient/nfsmount.h>
   65 #include <nfsclient/nfsnode.h>
   66 
   67 /*
   68  * Just call nfs_writebp() with the force argument set to 1.
   69  *
   70  * NOTE: B_DONE may or may not be set in a_bp on call.
   71  */
   72 static int
   73 nfs_bwrite(struct buf *bp)
   74 {
   75 
   76         return (nfs_writebp(bp, 1, curthread));
   77 }
   78 
   79 struct buf_ops buf_ops_nfs = {
   80         "buf_ops_nfs",
   81         nfs_bwrite
   82 };
   83 
   84 static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size,
   85                     struct thread *td);
   86 
   87 /*
   88  * Vnode op for VM getpages.
   89  */
   90 int
   91 nfs_getpages(struct vop_getpages_args *ap)
   92 {
   93         int i, error, nextoff, size, toff, count, npages;
   94         struct uio uio;
   95         struct iovec iov;
   96         vm_offset_t kva;
   97         struct buf *bp;
   98         struct vnode *vp;
   99         struct thread *td;
  100         struct ucred *cred;
  101         struct nfsmount *nmp;
  102         vm_page_t *pages;
  103 
  104         GIANT_REQUIRED;
  105 
  106         vp = ap->a_vp;
  107         td = curthread;                         /* XXX */
  108         cred = curthread->td_ucred;             /* XXX */
  109         nmp = VFSTONFS(vp->v_mount);
  110         pages = ap->a_m;
  111         count = ap->a_count;
  112 
  113         if (vp->v_object == NULL) {
  114                 printf("nfs_getpages: called with non-merged cache vnode??\n");
  115                 return VM_PAGER_ERROR;
  116         }
  117 
  118         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  119             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
  120                 (void)nfs_fsinfo(nmp, vp, cred, td);
  121         }
  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 
  131         {
  132                 vm_page_t m = pages[ap->a_reqpage];
  133 
  134                 vm_page_lock_queues();
  135                 if (m->valid != 0) {
  136                         /* handled by vm_fault now        */
  137                         /* vm_page_zero_invalid(m, TRUE); */
  138                         for (i = 0; i < npages; ++i) {
  139                                 if (i != ap->a_reqpage)
  140                                         vm_page_free(pages[i]);
  141                         }
  142                         vm_page_unlock_queues();
  143                         return(0);
  144                 }
  145                 vm_page_unlock_queues();
  146         }
  147 
  148         /*
  149          * We use only the kva address for the buffer, but this is extremely
  150          * convienient and fast.
  151          */
  152         bp = getpbuf(&nfs_pbuf_freecnt);
  153 
  154         kva = (vm_offset_t) bp->b_data;
  155         pmap_qenter(kva, pages, npages);
  156         cnt.v_vnodein++;
  157         cnt.v_vnodepgsin += npages;
  158 
  159         iov.iov_base = (caddr_t) kva;
  160         iov.iov_len = count;
  161         uio.uio_iov = &iov;
  162         uio.uio_iovcnt = 1;
  163         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
  164         uio.uio_resid = count;
  165         uio.uio_segflg = UIO_SYSSPACE;
  166         uio.uio_rw = UIO_READ;
  167         uio.uio_td = td;
  168 
  169         error = nfs_readrpc(vp, &uio, cred);
  170         pmap_qremove(kva, npages);
  171 
  172         relpbuf(bp, &nfs_pbuf_freecnt);
  173 
  174         if (error && (uio.uio_resid == count)) {
  175                 printf("nfs_getpages: error %d\n", error);
  176                 vm_page_lock_queues();
  177                 for (i = 0; i < npages; ++i) {
  178                         if (i != ap->a_reqpage)
  179                                 vm_page_free(pages[i]);
  180                 }
  181                 vm_page_unlock_queues();
  182                 return VM_PAGER_ERROR;
  183         }
  184 
  185         /*
  186          * Calculate the number of bytes read and validate only that number
  187          * of bytes.  Note that due to pending writes, size may be 0.  This
  188          * does not mean that the remaining data is invalid!
  189          */
  190 
  191         size = count - uio.uio_resid;
  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                 m->flags &= ~PG_ZERO;
  199 
  200                 if (nextoff <= size) {
  201                         /*
  202                          * Read operation filled an entire page
  203                          */
  204                         m->valid = VM_PAGE_BITS_ALL;
  205                         vm_page_undirty(m);
  206                 } else if (size > toff) {
  207                         /*
  208                          * Read operation filled a partial page.
  209                          */
  210                         m->valid = 0;
  211                         vm_page_set_validclean(m, 0, size - toff);
  212                         /* handled by vm_fault now        */
  213                         /* vm_page_zero_invalid(m, TRUE); */
  214                 } else {
  215                         /*
  216                          * Read operation was short.  If no error occured
  217                          * we may have hit a zero-fill section.   We simply
  218                          * leave valid set to 0.
  219                          */
  220                         ;
  221                 }
  222                 if (i != ap->a_reqpage) {
  223                         /*
  224                          * Whether or not to leave the page activated is up in
  225                          * the air, but we should put the page on a page queue
  226                          * somewhere (it already is in the object).  Result:
  227                          * It appears that emperical results show that
  228                          * deactivating pages is best.
  229                          */
  230 
  231                         /*
  232                          * Just in case someone was asking for this page we
  233                          * now tell them that it is ok to use.
  234                          */
  235                         if (!error) {
  236                                 if (m->flags & PG_WANTED)
  237                                         vm_page_activate(m);
  238                                 else
  239                                         vm_page_deactivate(m);
  240                                 vm_page_wakeup(m);
  241                         } else {
  242                                 vm_page_free(m);
  243                         }
  244                 }
  245         }
  246         vm_page_unlock_queues();
  247         return 0;
  248 }
  249 
  250 /*
  251  * Vnode op for VM putpages.
  252  */
  253 int
  254 nfs_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         GIANT_REQUIRED;
  271 
  272         vp = ap->a_vp;
  273         np = VTONFS(vp);
  274         td = curthread;                         /* XXX */
  275         cred = 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         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  284             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
  285                 (void)nfs_fsinfo(nmp, vp, cred, td);
  286         }
  287 
  288         for (i = 0; i < npages; i++)
  289                 rtvals[i] = VM_PAGER_AGAIN;
  290 
  291         /*
  292          * When putting pages, do not extend file past EOF.
  293          */
  294 
  295         if (offset + count > np->n_size) {
  296                 count = np->n_size - offset;
  297                 if (count < 0)
  298                         count = 0;
  299         }
  300 
  301         /*
  302          * We use only the kva address for the buffer, but this is extremely
  303          * convienient and fast.
  304          */
  305         bp = getpbuf(&nfs_pbuf_freecnt);
  306 
  307         kva = (vm_offset_t) bp->b_data;
  308         pmap_qenter(kva, pages, npages);
  309         cnt.v_vnodeout++;
  310         cnt.v_vnodepgsout += count;
  311 
  312         iov.iov_base = (caddr_t) kva;
  313         iov.iov_len = count;
  314         uio.uio_iov = &iov;
  315         uio.uio_iovcnt = 1;
  316         uio.uio_offset = offset;
  317         uio.uio_resid = count;
  318         uio.uio_segflg = UIO_SYSSPACE;
  319         uio.uio_rw = UIO_WRITE;
  320         uio.uio_td = td;
  321 
  322         if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
  323             iomode = NFSV3WRITE_UNSTABLE;
  324         else
  325             iomode = NFSV3WRITE_FILESYNC;
  326 
  327         error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
  328 
  329         pmap_qremove(kva, npages);
  330         relpbuf(bp, &nfs_pbuf_freecnt);
  331 
  332         if (!error) {
  333                 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
  334                 for (i = 0; i < nwritten; i++) {
  335                         rtvals[i] = VM_PAGER_OK;
  336                         vm_page_undirty(pages[i]);
  337                 }
  338                 if (must_commit) {
  339                         nfs_clearcommit(vp->v_mount);
  340                 }
  341         }
  342         return rtvals[0];
  343 }
  344 
  345 /*
  346  * Vnode op for read using bio
  347  */
  348 int
  349 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
  350 {
  351         struct nfsnode *np = VTONFS(vp);
  352         int biosize, i;
  353         struct buf *bp = 0, *rabp;
  354         struct vattr vattr;
  355         struct thread *td;
  356         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  357         daddr_t lbn, rabn;
  358         int bcount;
  359         int seqcount;
  360         int nra, error = 0, n = 0, on = 0;
  361 
  362 #ifdef DIAGNOSTIC
  363         if (uio->uio_rw != UIO_READ)
  364                 panic("nfs_read mode");
  365 #endif
  366         if (uio->uio_resid == 0)
  367                 return (0);
  368         if (uio->uio_offset < 0)        /* XXX VDIR cookies can be negative */
  369                 return (EINVAL);
  370         td = uio->uio_td;
  371 
  372         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  373             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
  374                 (void)nfs_fsinfo(nmp, vp, cred, td);
  375         if (vp->v_type != VDIR &&
  376             (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
  377                 return (EFBIG);
  378         biosize = vp->v_mount->mnt_stat.f_iosize;
  379         seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
  380         /*
  381          * For nfs, cache consistency can only be maintained approximately.
  382          * Although RFC1094 does not specify the criteria, the following is
  383          * believed to be compatible with the reference port.
  384          * For nfs:
  385          * If the file's modify time on the server has changed since the
  386          * last read rpc or you have written to the file,
  387          * you may have lost data cache consistency with the
  388          * server, so flush all of the file's data out of the cache.
  389          * Then force a getattr rpc to ensure that you have up to date
  390          * attributes.
  391          * NB: This implies that cache data can be read when up to
  392          * NFS_ATTRTIMEO seconds out of date. If you find that you need current
  393          * attributes this could be forced by setting n_attrstamp to 0 before
  394          * the VOP_GETATTR() call.
  395          */
  396         if (np->n_flag & NMODIFIED) {
  397                 if (vp->v_type != VREG) {
  398                         if (vp->v_type != VDIR)
  399                                 panic("nfs: bioread, not dir");
  400                         nfs_invaldir(vp);
  401                         error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
  402                         if (error)
  403                                 return (error);
  404                 }
  405                 np->n_attrstamp = 0;
  406                 error = VOP_GETATTR(vp, &vattr, cred, td);
  407                 if (error)
  408                         return (error);
  409                 np->n_mtime = vattr.va_mtime.tv_sec;
  410         } else {
  411                 error = VOP_GETATTR(vp, &vattr, cred, td);
  412                 if (error)
  413                         return (error);
  414                 if (np->n_mtime != vattr.va_mtime.tv_sec) {
  415                         if (vp->v_type == VDIR)
  416                                 nfs_invaldir(vp);
  417                         error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
  418                         if (error)
  419                                 return (error);
  420                         np->n_mtime = vattr.va_mtime.tv_sec;
  421                 }
  422         }
  423         do {
  424             switch (vp->v_type) {
  425             case VREG:
  426                 nfsstats.biocache_reads++;
  427                 lbn = uio->uio_offset / biosize;
  428                 on = uio->uio_offset & (biosize - 1);
  429 
  430                 /*
  431                  * Start the read ahead(s), as required.
  432                  */
  433                 if (nmp->nm_readahead > 0) {
  434                     for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
  435                         (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
  436                         rabn = lbn + 1 + nra;
  437                         if (incore(vp, rabn) == NULL) {
  438                             rabp = nfs_getcacheblk(vp, rabn, biosize, td);
  439                             if (!rabp)
  440                                 return (EINTR);
  441                             if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
  442                                 rabp->b_flags |= B_ASYNC;
  443                                 rabp->b_iocmd = BIO_READ;
  444                                 vfs_busy_pages(rabp, 0);
  445                                 if (nfs_asyncio(rabp, cred, td)) {
  446                                     rabp->b_flags |= B_INVAL;
  447                                     rabp->b_ioflags |= BIO_ERROR;
  448                                     vfs_unbusy_pages(rabp);
  449                                     brelse(rabp);
  450                                     break;
  451                                 }
  452                             } else {
  453                                 brelse(rabp);
  454                             }
  455                         }
  456                     }
  457                 }
  458 
  459                 /*
  460                  * Obtain the buffer cache block.  Figure out the buffer size
  461                  * when we are at EOF.  If we are modifying the size of the
  462                  * buffer based on an EOF condition we need to hold
  463                  * nfs_rslock() through obtaining the buffer to prevent
  464                  * a potential writer-appender from messing with n_size.
  465                  * Otherwise we may accidently truncate the buffer and
  466                  * lose dirty data.
  467                  *
  468                  * Note that bcount is *not* DEV_BSIZE aligned.
  469                  */
  470 
  471 again:
  472                 bcount = biosize;
  473                 if ((off_t)lbn * biosize >= np->n_size) {
  474                         bcount = 0;
  475                 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
  476                         bcount = np->n_size - (off_t)lbn * biosize;
  477                 }
  478                 if (bcount != biosize) {
  479                         switch(nfs_rslock(np, td)) {
  480                         case ENOLCK:
  481                                 goto again;
  482                                 /* not reached */
  483                         case EINTR:
  484                         case ERESTART:
  485                                 return(EINTR);
  486                                 /* not reached */
  487                         default:
  488                                 break;
  489                         }
  490                 }
  491 
  492                 bp = nfs_getcacheblk(vp, lbn, bcount, td);
  493 
  494                 if (bcount != biosize)
  495                         nfs_rsunlock(np, td);
  496                 if (!bp)
  497                         return (EINTR);
  498 
  499                 /*
  500                  * If B_CACHE is not set, we must issue the read.  If this
  501                  * fails, we return an error.
  502                  */
  503 
  504                 if ((bp->b_flags & B_CACHE) == 0) {
  505                     bp->b_iocmd = BIO_READ;
  506                     vfs_busy_pages(bp, 0);
  507                     error = nfs_doio(bp, cred, td);
  508                     if (error) {
  509                         brelse(bp);
  510                         return (error);
  511                     }
  512                 }
  513 
  514                 /*
  515                  * on is the offset into the current bp.  Figure out how many
  516                  * bytes we can copy out of the bp.  Note that bcount is
  517                  * NOT DEV_BSIZE aligned.
  518                  *
  519                  * Then figure out how many bytes we can copy into the uio.
  520                  */
  521 
  522                 n = 0;
  523                 if (on < bcount)
  524                         n = min((unsigned)(bcount - on), uio->uio_resid);
  525                 break;
  526             case VLNK:
  527                 nfsstats.biocache_readlinks++;
  528                 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
  529                 if (!bp)
  530                         return (EINTR);
  531                 if ((bp->b_flags & B_CACHE) == 0) {
  532                     bp->b_iocmd = BIO_READ;
  533                     vfs_busy_pages(bp, 0);
  534                     error = nfs_doio(bp, cred, td);
  535                     if (error) {
  536                         bp->b_ioflags |= BIO_ERROR;
  537                         brelse(bp);
  538                         return (error);
  539                     }
  540                 }
  541                 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
  542                 on = 0;
  543                 break;
  544             case VDIR:
  545                 nfsstats.biocache_readdirs++;
  546                 if (np->n_direofoffset
  547                     && uio->uio_offset >= np->n_direofoffset) {
  548                     return (0);
  549                 }
  550                 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
  551                 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
  552                 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
  553                 if (!bp)
  554                     return (EINTR);
  555                 if ((bp->b_flags & B_CACHE) == 0) {
  556                     bp->b_iocmd = BIO_READ;
  557                     vfs_busy_pages(bp, 0);
  558                     error = nfs_doio(bp, cred, td);
  559                     if (error) {
  560                             brelse(bp);
  561                     }
  562                     while (error == NFSERR_BAD_COOKIE) {
  563                         printf("got bad cookie vp %p bp %p\n", vp, bp);
  564                         nfs_invaldir(vp);
  565                         error = nfs_vinvalbuf(vp, 0, cred, td, 1);
  566                         /*
  567                          * Yuck! The directory has been modified on the
  568                          * server. The only way to get the block is by
  569                          * reading from the beginning to get all the
  570                          * offset cookies.
  571                          *
  572                          * Leave the last bp intact unless there is an error.
  573                          * Loop back up to the while if the error is another
  574                          * NFSERR_BAD_COOKIE (double yuch!).
  575                          */
  576                         for (i = 0; i <= lbn && !error; i++) {
  577                             if (np->n_direofoffset
  578                                 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
  579                                     return (0);
  580                             bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
  581                             if (!bp)
  582                                 return (EINTR);
  583                             if ((bp->b_flags & B_CACHE) == 0) {
  584                                     bp->b_iocmd = BIO_READ;
  585                                     vfs_busy_pages(bp, 0);
  586                                     error = nfs_doio(bp, cred, td);
  587                                     /*
  588                                      * no error + B_INVAL == directory EOF,
  589                                      * use the block.
  590                                      */
  591                                     if (error == 0 && (bp->b_flags & B_INVAL))
  592                                             break;
  593                             }
  594                             /*
  595                              * An error will throw away the block and the
  596                              * for loop will break out.  If no error and this
  597                              * is not the block we want, we throw away the
  598                              * block and go for the next one via the for loop.
  599                              */
  600                             if (error || i < lbn)
  601                                     brelse(bp);
  602                         }
  603                     }
  604                     /*
  605                      * The above while is repeated if we hit another cookie
  606                      * error.  If we hit an error and it wasn't a cookie error,
  607                      * we give up.
  608                      */
  609                     if (error)
  610                             return (error);
  611                 }
  612 
  613                 /*
  614                  * If not eof and read aheads are enabled, start one.
  615                  * (You need the current block first, so that you have the
  616                  *  directory offset cookie of the next block.)
  617                  */
  618                 if (nmp->nm_readahead > 0 &&
  619                     (bp->b_flags & B_INVAL) == 0 &&
  620                     (np->n_direofoffset == 0 ||
  621                     (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
  622                     incore(vp, lbn + 1) == NULL) {
  623                         rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
  624                         if (rabp) {
  625                             if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
  626                                 rabp->b_flags |= B_ASYNC;
  627                                 rabp->b_iocmd = BIO_READ;
  628                                 vfs_busy_pages(rabp, 0);
  629                                 if (nfs_asyncio(rabp, cred, td)) {
  630                                     rabp->b_flags |= B_INVAL;
  631                                     rabp->b_ioflags |= BIO_ERROR;
  632                                     vfs_unbusy_pages(rabp);
  633                                     brelse(rabp);
  634                                 }
  635                             } else {
  636                                 brelse(rabp);
  637                             }
  638                         }
  639                 }
  640                 /*
  641                  * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
  642                  * chopped for the EOF condition, we cannot tell how large
  643                  * NFS directories are going to be until we hit EOF.  So
  644                  * an NFS directory buffer is *not* chopped to its EOF.  Now,
  645                  * it just so happens that b_resid will effectively chop it
  646                  * to EOF.  *BUT* this information is lost if the buffer goes
  647                  * away and is reconstituted into a B_CACHE state ( due to
  648                  * being VMIO ) later.  So we keep track of the directory eof
  649                  * in np->n_direofoffset and chop it off as an extra step
  650                  * right here.
  651                  */
  652                 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
  653                 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
  654                         n = np->n_direofoffset - uio->uio_offset;
  655                 break;
  656             default:
  657                 printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
  658                 break;
  659             };
  660 
  661             if (n > 0) {
  662                     error = uiomove(bp->b_data + on, (int)n, uio);
  663             }
  664             switch (vp->v_type) {
  665             case VREG:
  666                 break;
  667             case VLNK:
  668                 n = 0;
  669                 break;
  670             case VDIR:
  671                 break;
  672             default:
  673                 printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
  674             }
  675             brelse(bp);
  676         } while (error == 0 && uio->uio_resid > 0 && n > 0);
  677         return (error);
  678 }
  679 
  680 /*
  681  * Vnode op for write using bio
  682  */
  683 int
  684 nfs_write(struct vop_write_args *ap)
  685 {
  686         int biosize;
  687         struct uio *uio = ap->a_uio;
  688         struct thread *td = uio->uio_td;
  689         struct vnode *vp = ap->a_vp;
  690         struct nfsnode *np = VTONFS(vp);
  691         struct ucred *cred = ap->a_cred;
  692         int ioflag = ap->a_ioflag;
  693         struct buf *bp;
  694         struct vattr vattr;
  695         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
  696         daddr_t lbn;
  697         int bcount;
  698         int n, on, error = 0;
  699         int haverslock = 0;
  700         struct proc *p = td?td->td_proc:NULL;
  701 
  702         GIANT_REQUIRED;
  703 
  704 #ifdef DIAGNOSTIC
  705         if (uio->uio_rw != UIO_WRITE)
  706                 panic("nfs_write mode");
  707         if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
  708                 panic("nfs_write proc");
  709 #endif
  710         if (vp->v_type != VREG)
  711                 return (EIO);
  712         if (np->n_flag & NWRITEERR) {
  713                 np->n_flag &= ~NWRITEERR;
  714                 return (np->n_error);
  715         }
  716         if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
  717             (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
  718                 (void)nfs_fsinfo(nmp, vp, cred, td);
  719 
  720         /*
  721          * Synchronously flush pending buffers if we are in synchronous
  722          * mode or if we are appending.
  723          */
  724         if (ioflag & (IO_APPEND | IO_SYNC)) {
  725                 if (np->n_flag & NMODIFIED) {
  726                         np->n_attrstamp = 0;
  727                         error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
  728                         if (error)
  729                                 return (error);
  730                 }
  731         }
  732 
  733         /*
  734          * If IO_APPEND then load uio_offset.  We restart here if we cannot
  735          * get the append lock.
  736          */
  737 restart:
  738         if (ioflag & IO_APPEND) {
  739                 np->n_attrstamp = 0;
  740                 error = VOP_GETATTR(vp, &vattr, cred, td);
  741                 if (error)
  742                         return (error);
  743                 uio->uio_offset = np->n_size;
  744         }
  745 
  746         if (uio->uio_offset < 0)
  747                 return (EINVAL);
  748         if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
  749                 return (EFBIG);
  750         if (uio->uio_resid == 0)
  751                 return (0);
  752 
  753         /*
  754          * We need to obtain the rslock if we intend to modify np->n_size
  755          * in order to guarentee the append point with multiple contending
  756          * writers, to guarentee that no other appenders modify n_size
  757          * while we are trying to obtain a truncated buffer (i.e. to avoid
  758          * accidently truncating data written by another appender due to
  759          * the race), and to ensure that the buffer is populated prior to
  760          * our extending of the file.  We hold rslock through the entire
  761          * operation.
  762          *
  763          * Note that we do not synchronize the case where someone truncates
  764          * the file while we are appending to it because attempting to lock
  765          * this case may deadlock other parts of the system unexpectedly.
  766          */
  767         if ((ioflag & IO_APPEND) ||
  768             uio->uio_offset + uio->uio_resid > np->n_size) {
  769                 switch(nfs_rslock(np, td)) {
  770                 case ENOLCK:
  771                         goto restart;
  772                         /* not reached */
  773                 case EINTR:
  774                 case ERESTART:
  775                         return(EINTR);
  776                         /* not reached */
  777                 default:
  778                         break;
  779                 }
  780                 haverslock = 1;
  781         }
  782 
  783         /*
  784          * Maybe this should be above the vnode op call, but so long as
  785          * file servers have no limits, i don't think it matters
  786          */
  787         if (p && uio->uio_offset + uio->uio_resid >
  788               p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
  789                 PROC_LOCK(p);
  790                 psignal(p, SIGXFSZ);
  791                 PROC_UNLOCK(p);
  792                 if (haverslock)
  793                         nfs_rsunlock(np, td);
  794                 return (EFBIG);
  795         }
  796 
  797         biosize = vp->v_mount->mnt_stat.f_iosize;
  798 
  799         do {
  800                 nfsstats.biocache_writes++;
  801                 lbn = uio->uio_offset / biosize;
  802                 on = uio->uio_offset & (biosize-1);
  803                 n = min((unsigned)(biosize - on), uio->uio_resid);
  804 again:
  805                 /*
  806                  * Handle direct append and file extension cases, calculate
  807                  * unaligned buffer size.
  808                  */
  809 
  810                 if (uio->uio_offset == np->n_size && n) {
  811                         /*
  812                          * Get the buffer (in its pre-append state to maintain
  813                          * B_CACHE if it was previously set).  Resize the
  814                          * nfsnode after we have locked the buffer to prevent
  815                          * readers from reading garbage.
  816                          */
  817                         bcount = on;
  818                         bp = nfs_getcacheblk(vp, lbn, bcount, td);
  819 
  820                         if (bp != NULL) {
  821                                 long save;
  822 
  823                                 np->n_size = uio->uio_offset + n;
  824                                 np->n_flag |= NMODIFIED;
  825                                 vnode_pager_setsize(vp, np->n_size);
  826 
  827                                 save = bp->b_flags & B_CACHE;
  828                                 bcount += n;
  829                                 allocbuf(bp, bcount);
  830                                 bp->b_flags |= save;
  831                                 bp->b_magic = B_MAGIC_NFS;
  832                                 bp->b_op = &buf_ops_nfs;
  833                         }
  834                 } else {
  835                         /*
  836                          * Obtain the locked cache block first, and then
  837                          * adjust the file's size as appropriate.
  838                          */
  839                         bcount = on + n;
  840                         if ((off_t)lbn * biosize + bcount < np->n_size) {
  841                                 if ((off_t)(lbn + 1) * biosize < np->n_size)
  842                                         bcount = biosize;
  843                                 else
  844                                         bcount = np->n_size - (off_t)lbn * biosize;
  845                         }
  846                         bp = nfs_getcacheblk(vp, lbn, bcount, td);
  847                         if (uio->uio_offset + n > np->n_size) {
  848                                 np->n_size = uio->uio_offset + n;
  849                                 np->n_flag |= NMODIFIED;
  850                                 vnode_pager_setsize(vp, np->n_size);
  851                         }
  852                 }
  853 
  854                 if (!bp) {
  855                         error = EINTR;
  856                         break;
  857                 }
  858 
  859                 /*
  860                  * Issue a READ if B_CACHE is not set.  In special-append
  861                  * mode, B_CACHE is based on the buffer prior to the write
  862                  * op and is typically set, avoiding the read.  If a read
  863                  * is required in special append mode, the server will
  864                  * probably send us a short-read since we extended the file
  865                  * on our end, resulting in b_resid == 0 and, thusly,
  866                  * B_CACHE getting set.
  867                  *
  868                  * We can also avoid issuing the read if the write covers
  869                  * the entire buffer.  We have to make sure the buffer state
  870                  * is reasonable in this case since we will not be initiating
  871                  * I/O.  See the comments in kern/vfs_bio.c's getblk() for
  872                  * more information.
  873                  *
  874                  * B_CACHE may also be set due to the buffer being cached
  875                  * normally.
  876                  */
  877 
  878                 if (on == 0 && n == bcount) {
  879                         bp->b_flags |= B_CACHE;
  880                         bp->b_flags &= ~B_INVAL;
  881                         bp->b_ioflags &= ~BIO_ERROR;
  882                 }
  883 
  884                 if ((bp->b_flags & B_CACHE) == 0) {
  885                         bp->b_iocmd = BIO_READ;
  886                         vfs_busy_pages(bp, 0);
  887                         error = nfs_doio(bp, cred, td);
  888                         if (error) {
  889                                 brelse(bp);
  890                                 break;
  891                         }
  892                 }
  893                 if (!bp) {
  894                         error = EINTR;
  895                         break;
  896                 }
  897                 if (bp->b_wcred == NOCRED)
  898                         bp->b_wcred = crhold(cred);
  899                 np->n_flag |= NMODIFIED;
  900 
  901                 /*
  902                  * If dirtyend exceeds file size, chop it down.  This should
  903                  * not normally occur but there is an append race where it
  904                  * might occur XXX, so we log it.
  905                  *
  906                  * If the chopping creates a reverse-indexed or degenerate
  907                  * situation with dirtyoff/end, we 0 both of them.
  908                  */
  909 
  910                 if (bp->b_dirtyend > bcount) {
  911                         printf("NFS append race @%lx:%d\n",
  912                             (long)bp->b_blkno * DEV_BSIZE,
  913                             bp->b_dirtyend - bcount);
  914                         bp->b_dirtyend = bcount;
  915                 }
  916 
  917                 if (bp->b_dirtyoff >= bp->b_dirtyend)
  918                         bp->b_dirtyoff = bp->b_dirtyend = 0;
  919 
  920                 /*
  921                  * If the new write will leave a contiguous dirty
  922                  * area, just update the b_dirtyoff and b_dirtyend,
  923                  * otherwise force a write rpc of the old dirty area.
  924                  *
  925                  * While it is possible to merge discontiguous writes due to
  926                  * our having a B_CACHE buffer ( and thus valid read data
  927                  * for the hole), we don't because it could lead to
  928                  * significant cache coherency problems with multiple clients,
  929                  * especially if locking is implemented later on.
  930                  *
  931                  * as an optimization we could theoretically maintain
  932                  * a linked list of discontinuous areas, but we would still
  933                  * have to commit them separately so there isn't much
  934                  * advantage to it except perhaps a bit of asynchronization.
  935                  */
  936 
  937                 if (bp->b_dirtyend > 0 &&
  938                     (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
  939                         if (BUF_WRITE(bp) == EINTR) {
  940                                 error = EINTR;
  941                                 break;
  942                         }
  943                         goto again;
  944                 }
  945 
  946                 error = uiomove((char *)bp->b_data + on, n, uio);
  947 
  948                 /*
  949                  * Since this block is being modified, it must be written
  950                  * again and not just committed.  Since write clustering does
  951                  * not work for the stage 1 data write, only the stage 2
  952                  * commit rpc, we have to clear B_CLUSTEROK as well.
  953                  */
  954                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
  955 
  956                 if (error) {
  957                         bp->b_ioflags |= BIO_ERROR;
  958                         brelse(bp);
  959                         break;
  960                 }
  961 
  962                 /*
  963                  * Only update dirtyoff/dirtyend if not a degenerate
  964                  * condition.
  965                  */
  966                 if (n) {
  967                         if (bp->b_dirtyend > 0) {
  968                                 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
  969                                 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
  970                         } else {
  971                                 bp->b_dirtyoff = on;
  972                                 bp->b_dirtyend = on + n;
  973                         }
  974                         vfs_bio_set_validclean(bp, on, n);
  975                 }
  976                 /*
  977                  * If IO_NOWDRAIN then set B_NOWDRAIN (nfs-backed MD 
  978                  * filesystem)
  979                  */
  980                 if (ioflag & IO_NOWDRAIN)
  981                         bp->b_flags |= B_NOWDRAIN;
  982 
  983                 /*
  984                  * If IO_SYNC do bwrite().
  985                  *
  986                  * IO_INVAL appears to be unused.  The idea appears to be
  987                  * to turn off caching in this case.  Very odd.  XXX
  988                  */
  989                 if ((ioflag & IO_SYNC)) {
  990                         if (ioflag & IO_INVAL)
  991                                 bp->b_flags |= B_NOCACHE;
  992                         error = BUF_WRITE(bp);
  993                         if (error)
  994                                 break;
  995                 } else if ((n + on) == biosize) {
  996                         bp->b_flags |= B_ASYNC;
  997                         (void)nfs_writebp(bp, 0, 0);
  998                 } else {
  999                         bdwrite(bp);
 1000                 }
 1001         } while (uio->uio_resid > 0 && n > 0);
 1002 
 1003         if (haverslock)
 1004                 nfs_rsunlock(np, td);
 1005 
 1006         return (error);
 1007 }
 1008 
 1009 /*
 1010  * Get an nfs cache block.
 1011  *
 1012  * Allocate a new one if the block isn't currently in the cache
 1013  * and return the block marked busy. If the calling process is
 1014  * interrupted by a signal for an interruptible mount point, return
 1015  * NULL.
 1016  *
 1017  * The caller must carefully deal with the possible B_INVAL state of
 1018  * the buffer.  nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
 1019  * indirectly), so synchronous reads can be issued without worrying about
 1020  * the B_INVAL state.  We have to be a little more careful when dealing
 1021  * with writes (see comments in nfs_write()) when extending a file past
 1022  * its EOF.
 1023  */
 1024 static struct buf *
 1025 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
 1026 {
 1027         struct buf *bp;
 1028         struct mount *mp;
 1029         struct nfsmount *nmp;
 1030 
 1031         mp = vp->v_mount;
 1032         nmp = VFSTONFS(mp);
 1033 
 1034         if (nmp->nm_flag & NFSMNT_INT) {
 1035                 bp = getblk(vp, bn, size, PCATCH, 0, 0);
 1036                 while (bp == NULL) {
 1037                         if (nfs_sigintr(nmp, NULL, td))
 1038                                 return (NULL);
 1039                         bp = getblk(vp, bn, size, 0, 2 * hz, 0);
 1040                 }
 1041         } else {
 1042                 bp = getblk(vp, bn, size, 0, 0, 0);
 1043         }
 1044 
 1045         if (vp->v_type == VREG) {
 1046                 int biosize;
 1047 
 1048                 biosize = mp->mnt_stat.f_iosize;
 1049                 bp->b_blkno = bn * (biosize / DEV_BSIZE);
 1050         }
 1051         return (bp);
 1052 }
 1053 
 1054 /*
 1055  * Flush and invalidate all dirty buffers. If another process is already
 1056  * doing the flush, just wait for completion.
 1057  */
 1058 int
 1059 nfs_vinvalbuf(struct vnode *vp, int flags, struct ucred *cred,
 1060     struct thread *td, int intrflg)
 1061 {
 1062         struct nfsnode *np = VTONFS(vp);
 1063         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 1064         int error = 0, slpflag, slptimeo;
 1065 
 1066         ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf");
 1067 
 1068         VI_LOCK(vp);
 1069         if (vp->v_iflag & VI_XLOCK) {
 1070                 /* XXX Should we wait here? */
 1071                 VI_UNLOCK(vp);
 1072                 return (0);
 1073         }
 1074         VI_UNLOCK(vp);
 1075 
 1076         if ((nmp->nm_flag & NFSMNT_INT) == 0)
 1077                 intrflg = 0;
 1078         if (intrflg) {
 1079                 slpflag = PCATCH;
 1080                 slptimeo = 2 * hz;
 1081         } else {
 1082                 slpflag = 0;
 1083                 slptimeo = 0;
 1084         }
 1085         /*
 1086          * First wait for any other process doing a flush to complete.
 1087          */
 1088         while (np->n_flag & NFLUSHINPROG) {
 1089                 np->n_flag |= NFLUSHWANT;
 1090                 error = tsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
 1091                         slptimeo);
 1092                 if (error && intrflg &&
 1093                     nfs_sigintr(nmp, NULL, td))
 1094                         return (EINTR);
 1095         }
 1096 
 1097         /*
 1098          * Now, flush as required.
 1099          */
 1100         np->n_flag |= NFLUSHINPROG;
 1101         error = vinvalbuf(vp, flags, cred, td, slpflag, 0);
 1102         while (error) {
 1103                 if (intrflg &&
 1104                     nfs_sigintr(nmp, NULL, td)) {
 1105                         np->n_flag &= ~NFLUSHINPROG;
 1106                         if (np->n_flag & NFLUSHWANT) {
 1107                                 np->n_flag &= ~NFLUSHWANT;
 1108                                 wakeup(&np->n_flag);
 1109                         }
 1110                         return (EINTR);
 1111                 }
 1112                 error = vinvalbuf(vp, flags, cred, td, 0, slptimeo);
 1113         }
 1114         np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
 1115         if (np->n_flag & NFLUSHWANT) {
 1116                 np->n_flag &= ~NFLUSHWANT;
 1117                 wakeup(&np->n_flag);
 1118         }
 1119         return (0);
 1120 }
 1121 
 1122 /*
 1123  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
 1124  * This is mainly to avoid queueing async I/O requests when the nfsiods
 1125  * are all hung on a dead server.
 1126  *
 1127  * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
 1128  * is eventually dequeued by the async daemon, nfs_doio() *will*.
 1129  */
 1130 int
 1131 nfs_asyncio(struct buf *bp, struct ucred *cred, struct thread *td)
 1132 {
 1133         struct nfsmount *nmp;
 1134         int iod;
 1135         int gotiod;
 1136         int slpflag = 0;
 1137         int slptimeo = 0;
 1138         int error;
 1139 
 1140         nmp = VFSTONFS(bp->b_vp->v_mount);
 1141 
 1142         /*
 1143          * Commits are usually short and sweet so lets save some cpu and
 1144          * leave the async daemons for more important rpc's (such as reads
 1145          * and writes).
 1146          */
 1147         if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
 1148             (nmp->nm_bufqiods > nfs_numasync / 2)) {
 1149                 return(EIO);
 1150         }
 1151 
 1152 again:
 1153         if (nmp->nm_flag & NFSMNT_INT)
 1154                 slpflag = PCATCH;
 1155         gotiod = FALSE;
 1156 
 1157         /*
 1158          * Find a free iod to process this request.
 1159          */
 1160         for (iod = 0; iod < nfs_numasync; iod++)
 1161                 if (nfs_iodwant[iod]) {
 1162                         gotiod = TRUE;
 1163                         break;
 1164                 }
 1165 
 1166         /*
 1167          * Try to create one if none are free.
 1168          */
 1169         if (!gotiod) {
 1170                 iod = nfs_nfsiodnew();
 1171                 if (iod != -1)
 1172                         gotiod = TRUE;
 1173         }
 1174 
 1175         if (gotiod) {
 1176                 /*
 1177                  * Found one, so wake it up and tell it which
 1178                  * mount to process.
 1179                  */
 1180                 NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n",
 1181                     iod, nmp));
 1182                 nfs_iodwant[iod] = NULL;
 1183                 nfs_iodmount[iod] = nmp;
 1184                 nmp->nm_bufqiods++;
 1185                 wakeup(&nfs_iodwant[iod]);
 1186         }
 1187 
 1188         /*
 1189          * If none are free, we may already have an iod working on this mount
 1190          * point.  If so, it will process our request.
 1191          */
 1192         if (!gotiod) {
 1193                 if (nmp->nm_bufqiods > 0) {
 1194                         NFS_DPF(ASYNCIO,
 1195                                 ("nfs_asyncio: %d iods are already processing mount %p\n",
 1196                                  nmp->nm_bufqiods, nmp));
 1197                         gotiod = TRUE;
 1198                 }
 1199         }
 1200 
 1201         /*
 1202          * If we have an iod which can process the request, then queue
 1203          * the buffer.
 1204          */
 1205         if (gotiod) {
 1206                 /*
 1207                  * Ensure that the queue never grows too large.  We still want
 1208                  * to asynchronize so we block rather then return EIO.
 1209                  */
 1210                 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
 1211                         NFS_DPF(ASYNCIO,
 1212                                 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
 1213                         nmp->nm_bufqwant = TRUE;
 1214                         error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
 1215                                        "nfsaio", slptimeo);
 1216                         if (error) {
 1217                                 if (nfs_sigintr(nmp, NULL, td))
 1218                                         return (EINTR);
 1219                                 if (slpflag == PCATCH) {
 1220                                         slpflag = 0;
 1221                                         slptimeo = 2 * hz;
 1222                                 }
 1223                         }
 1224                         /*
 1225                          * We might have lost our iod while sleeping,
 1226                          * so check and loop if nescessary.
 1227                          */
 1228                         if (nmp->nm_bufqiods == 0) {
 1229                                 NFS_DPF(ASYNCIO,
 1230                                         ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
 1231                                 goto again;
 1232                         }
 1233                 }
 1234 
 1235                 if (bp->b_iocmd == BIO_READ) {
 1236                         if (bp->b_rcred == NOCRED && cred != NOCRED)
 1237                                 bp->b_rcred = crhold(cred);
 1238                 } else {
 1239                         bp->b_flags |= B_WRITEINPROG;
 1240                         if (bp->b_wcred == NOCRED && cred != NOCRED)
 1241                                 bp->b_wcred = crhold(cred);
 1242                 }
 1243 
 1244                 BUF_KERNPROC(bp);
 1245                 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
 1246                 nmp->nm_bufqlen++;
 1247                 return (0);
 1248         }
 1249 
 1250         /*
 1251          * All the iods are busy on other mounts, so return EIO to
 1252          * force the caller to process the i/o synchronously.
 1253          */
 1254         NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
 1255         return (EIO);
 1256 }
 1257 
 1258 /*
 1259  * Do an I/O operation to/from a cache block. This may be called
 1260  * synchronously or from an nfsiod.
 1261  */
 1262 int
 1263 nfs_doio(struct buf *bp, struct ucred *cr, struct thread *td)
 1264 {
 1265         struct uio *uiop;
 1266         struct vnode *vp;
 1267         struct nfsnode *np;
 1268         struct nfsmount *nmp;
 1269         int error = 0, iomode, must_commit = 0;
 1270         struct uio uio;
 1271         struct iovec io;
 1272         struct proc *p = td ? td->td_proc : NULL;
 1273 
 1274         vp = bp->b_vp;
 1275         np = VTONFS(vp);
 1276         nmp = VFSTONFS(vp->v_mount);
 1277         uiop = &uio;
 1278         uiop->uio_iov = &io;
 1279         uiop->uio_iovcnt = 1;
 1280         uiop->uio_segflg = UIO_SYSSPACE;
 1281         uiop->uio_td = td;
 1282 
 1283         /*
 1284          * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
 1285          * do this here so we do not have to do it in all the code that
 1286          * calls us.
 1287          */
 1288         bp->b_flags &= ~B_INVAL;
 1289         bp->b_ioflags &= ~BIO_ERROR;
 1290 
 1291         KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
 1292 
 1293         /*
 1294          * Historically, paging was done with physio, but no more.
 1295          */
 1296         if (bp->b_flags & B_PHYS) {
 1297             /*
 1298              * ...though reading /dev/drum still gets us here.
 1299              */
 1300             io.iov_len = uiop->uio_resid = bp->b_bcount;
 1301             /* mapping was done by vmapbuf() */
 1302             io.iov_base = bp->b_data;
 1303             uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
 1304             if (bp->b_iocmd == BIO_READ) {
 1305                 uiop->uio_rw = UIO_READ;
 1306                 nfsstats.read_physios++;
 1307                 error = nfs_readrpc(vp, uiop, cr);
 1308             } else {
 1309                 int com;
 1310 
 1311                 iomode = NFSV3WRITE_DATASYNC;
 1312                 uiop->uio_rw = UIO_WRITE;
 1313                 nfsstats.write_physios++;
 1314                 error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
 1315             }
 1316             if (error) {
 1317                 bp->b_ioflags |= BIO_ERROR;
 1318                 bp->b_error = error;
 1319             }
 1320         } else if (bp->b_iocmd == BIO_READ) {
 1321             io.iov_len = uiop->uio_resid = bp->b_bcount;
 1322             io.iov_base = bp->b_data;
 1323             uiop->uio_rw = UIO_READ;
 1324 
 1325             switch (vp->v_type) {
 1326             case VREG:
 1327                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
 1328                 nfsstats.read_bios++;
 1329                 error = nfs_readrpc(vp, uiop, cr);
 1330 
 1331                 if (!error) {
 1332                     if (uiop->uio_resid) {
 1333                         /*
 1334                          * If we had a short read with no error, we must have
 1335                          * hit a file hole.  We should zero-fill the remainder.
 1336                          * This can also occur if the server hits the file EOF.
 1337                          *
 1338                          * Holes used to be able to occur due to pending
 1339                          * writes, but that is not possible any longer.
 1340                          */
 1341                         int nread = bp->b_bcount - uiop->uio_resid;
 1342                         int left  = uiop->uio_resid;
 1343 
 1344                         if (left > 0)
 1345                                 bzero((char *)bp->b_data + nread, left);
 1346                         uiop->uio_resid = 0;
 1347                     }
 1348                 }
 1349                 /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */
 1350                 if (p && (vp->v_vflag & VV_TEXT) &&
 1351                         (np->n_mtime != np->n_vattr.va_mtime.tv_sec)) {
 1352                         uprintf("Process killed due to text file modification\n");
 1353                         PROC_LOCK(p);
 1354                         psignal(p, SIGKILL);
 1355                         _PHOLD(p);
 1356                         PROC_UNLOCK(p);
 1357                 }
 1358                 break;
 1359             case VLNK:
 1360                 uiop->uio_offset = (off_t)0;
 1361                 nfsstats.readlink_bios++;
 1362                 error = nfs_readlinkrpc(vp, uiop, cr);
 1363                 break;
 1364             case VDIR:
 1365                 nfsstats.readdir_bios++;
 1366                 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
 1367                 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
 1368                         error = nfs_readdirplusrpc(vp, uiop, cr);
 1369                         if (error == NFSERR_NOTSUPP)
 1370                                 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
 1371                 }
 1372                 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
 1373                         error = nfs_readdirrpc(vp, uiop, cr);
 1374                 /*
 1375                  * end-of-directory sets B_INVAL but does not generate an
 1376                  * error.
 1377                  */
 1378                 if (error == 0 && uiop->uio_resid == bp->b_bcount)
 1379                         bp->b_flags |= B_INVAL;
 1380                 break;
 1381             default:
 1382                 printf("nfs_doio:  type %x unexpected\n", vp->v_type);
 1383                 break;
 1384             };
 1385             if (error) {
 1386                 bp->b_ioflags |= BIO_ERROR;
 1387                 bp->b_error = error;
 1388             }
 1389         } else {
 1390             /*
 1391              * If we only need to commit, try to commit
 1392              */
 1393             if (bp->b_flags & B_NEEDCOMMIT) {
 1394                     int retv;
 1395                     off_t off;
 1396 
 1397                     off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
 1398                     bp->b_flags |= B_WRITEINPROG;
 1399                     retv = nfs_commit(
 1400                                 bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
 1401                                 bp->b_wcred, td);
 1402                     bp->b_flags &= ~B_WRITEINPROG;
 1403                     if (retv == 0) {
 1404                             bp->b_dirtyoff = bp->b_dirtyend = 0;
 1405                             bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 1406                             bp->b_resid = 0;
 1407                             bufdone(bp);
 1408                             return (0);
 1409                     }
 1410                     if (retv == NFSERR_STALEWRITEVERF) {
 1411                             nfs_clearcommit(bp->b_vp->v_mount);
 1412                     }
 1413             }
 1414 
 1415             /*
 1416              * Setup for actual write
 1417              */
 1418 
 1419             if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
 1420                 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
 1421 
 1422             if (bp->b_dirtyend > bp->b_dirtyoff) {
 1423                 io.iov_len = uiop->uio_resid = bp->b_dirtyend
 1424                     - bp->b_dirtyoff;
 1425                 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
 1426                     + bp->b_dirtyoff;
 1427                 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
 1428                 uiop->uio_rw = UIO_WRITE;
 1429                 nfsstats.write_bios++;
 1430 
 1431                 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
 1432                     iomode = NFSV3WRITE_UNSTABLE;
 1433                 else
 1434                     iomode = NFSV3WRITE_FILESYNC;
 1435 
 1436                 bp->b_flags |= B_WRITEINPROG;
 1437                 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
 1438 
 1439                 /*
 1440                  * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
 1441                  * to cluster the buffers needing commit.  This will allow
 1442                  * the system to submit a single commit rpc for the whole
 1443                  * cluster.  We can do this even if the buffer is not 100%
 1444                  * dirty (relative to the NFS blocksize), so we optimize the
 1445                  * append-to-file-case.
 1446                  *
 1447                  * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
 1448                  * cleared because write clustering only works for commit
 1449                  * rpc's, not for the data portion of the write).
 1450                  */
 1451 
 1452                 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
 1453                     bp->b_flags |= B_NEEDCOMMIT;
 1454                     if (bp->b_dirtyoff == 0
 1455                         && bp->b_dirtyend == bp->b_bcount)
 1456                         bp->b_flags |= B_CLUSTEROK;
 1457                 } else {
 1458                     bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 1459                 }
 1460                 bp->b_flags &= ~B_WRITEINPROG;
 1461 
 1462                 /*
 1463                  * For an interrupted write, the buffer is still valid
 1464                  * and the write hasn't been pushed to the server yet,
 1465                  * so we can't set BIO_ERROR and report the interruption
 1466                  * by setting B_EINTR. For the B_ASYNC case, B_EINTR
 1467                  * is not relevant, so the rpc attempt is essentially
 1468                  * a noop.  For the case of a V3 write rpc not being
 1469                  * committed to stable storage, the block is still
 1470                  * dirty and requires either a commit rpc or another
 1471                  * write rpc with iomode == NFSV3WRITE_FILESYNC before
 1472                  * the block is reused. This is indicated by setting
 1473                  * the B_DELWRI and B_NEEDCOMMIT flags.
 1474                  *
 1475                  * If the buffer is marked B_PAGING, it does not reside on
 1476                  * the vp's paging queues so we cannot call bdirty().  The
 1477                  * bp in this case is not an NFS cache block so we should
 1478                  * be safe. XXX
 1479                  */
 1480                 if (error == EINTR
 1481                     || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
 1482                         int s;
 1483 
 1484                         s = splbio();
 1485                         bp->b_flags &= ~(B_INVAL|B_NOCACHE);
 1486                         if ((bp->b_flags & B_PAGING) == 0) {
 1487                             bdirty(bp);
 1488                             bp->b_flags &= ~B_DONE;
 1489                         }
 1490                         if (error && (bp->b_flags & B_ASYNC) == 0)
 1491                             bp->b_flags |= B_EINTR;
 1492                         splx(s);
 1493                 } else {
 1494                     if (error) {
 1495                         bp->b_ioflags |= BIO_ERROR;
 1496                         bp->b_error = np->n_error = error;
 1497                         np->n_flag |= NWRITEERR;
 1498                     }
 1499                     bp->b_dirtyoff = bp->b_dirtyend = 0;
 1500                 }
 1501             } else {
 1502                 bp->b_resid = 0;
 1503                 bufdone(bp);
 1504                 return (0);
 1505             }
 1506         }
 1507         bp->b_resid = uiop->uio_resid;
 1508         if (must_commit)
 1509             nfs_clearcommit(vp->v_mount);
 1510         bufdone(bp);
 1511         return (error);
 1512 }
 1513 
 1514 /*
 1515  * Used to aid in handling ftruncate() operations on the NFS client side.
 1516  * Truncation creates a number of special problems for NFS.  We have to
 1517  * throw away VM pages and buffer cache buffers that are beyond EOF, and
 1518  * we have to properly handle VM pages or (potentially dirty) buffers
 1519  * that straddle the truncation point.
 1520  */
 1521 
 1522 int
 1523 nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize)
 1524 {
 1525         struct nfsnode *np = VTONFS(vp);
 1526         u_quad_t tsize = np->n_size;
 1527         int biosize = vp->v_mount->mnt_stat.f_iosize;
 1528         int error = 0;
 1529 
 1530         np->n_size = nsize;
 1531 
 1532         if (np->n_size < tsize) {
 1533                 struct buf *bp;
 1534                 daddr_t lbn;
 1535                 int bufsize;
 1536 
 1537                 /*
 1538                  * vtruncbuf() doesn't get the buffer overlapping the 
 1539                  * truncation point.  We may have a B_DELWRI and/or B_CACHE
 1540                  * buffer that now needs to be truncated.
 1541                  */
 1542                 error = vtruncbuf(vp, cred, td, nsize, biosize);
 1543                 lbn = nsize / biosize;
 1544                 bufsize = nsize & (biosize - 1);
 1545                 bp = nfs_getcacheblk(vp, lbn, bufsize, td);
 1546                 if (bp->b_dirtyoff > bp->b_bcount)
 1547                         bp->b_dirtyoff = bp->b_bcount;
 1548                 if (bp->b_dirtyend > bp->b_bcount)
 1549                         bp->b_dirtyend = bp->b_bcount;
 1550                 bp->b_flags |= B_RELBUF;  /* don't leave garbage around */
 1551                 brelse(bp);
 1552         } else {
 1553                 vnode_pager_setsize(vp, nsize);
 1554         }
 1555         return(error);
 1556 }
 1557 

Cache object: cac6ed202d97cd10f5affd7fb1fe1fbe


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