BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / nfs / nfs_bio.c
index eb9d49b..44a48a1 100644 (file)
@@ -5,29 +5,52 @@
  * This code is derived from software contributed to Berkeley by
  * Rick Macklem at The University of Guelph.
  *
  * This code is derived from software contributed to Berkeley by
  * Rick Macklem at The University of Guelph.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)nfs_bio.c   7.5 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)nfs_bio.c   7.19 (Berkeley) 4/16/91
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "user.h"
+#include "proc.h"
 #include "buf.h"
 #include "buf.h"
+#include "uio.h"
+#include "namei.h"
 #include "vnode.h"
 #include "trace.h"
 #include "mount.h"
 #include "vnode.h"
 #include "trace.h"
 #include "mount.h"
+#include "resourcevar.h"
+
 #include "nfsnode.h"
 #include "nfsnode.h"
+#include "nfsv2.h"
+#include "nfs.h"
 #include "nfsiom.h"
 #include "nfsiom.h"
+#include "nfsmount.h"
 
 /* True and false, how exciting */
 #define        TRUE    1
 
 /* True and false, how exciting */
 #define        TRUE    1
  * Vnode op for read using bio
  * Any similarity to readip() is purely coincidental
  */
  * Vnode op for read using bio
  * Any similarity to readip() is purely coincidental
  */
-nfs_read(vp, uio, ioflag, cred)
+nfs_bioread(vp, uio, ioflag, cred)
        register struct vnode *vp;
        register struct vnode *vp;
-       struct uio *uio;
+       register struct uio *uio;
        int ioflag;
        struct ucred *cred;
 {
        register struct nfsnode *np = VTONFS(vp);
        int ioflag;
        struct ucred *cred;
 {
        register struct nfsnode *np = VTONFS(vp);
+       register int biosize;
        struct buf *bp;
        struct vattr vattr;
        daddr_t lbn, bn, rablock;
        int diff, error = 0;
        long n, on;
 
        struct buf *bp;
        struct vattr vattr;
        daddr_t lbn, bn, rablock;
        int diff, error = 0;
        long n, on;
 
-       /*
-        * Avoid caching directories. Once everything is using getdirentries()
-        * this will never happen anyhow.
-        */
-       if (vp->v_type == VDIR)
-               return (nfs_readrpc(vp, uio, cred));
+#ifdef lint
+       ioflag = ioflag;
+#endif /* lint */
+#ifdef DIAGNOSTIC
        if (uio->uio_rw != UIO_READ)
                panic("nfs_read mode");
        if (uio->uio_rw != UIO_READ)
                panic("nfs_read mode");
-       if (vp->v_type != VREG)
-               panic("nfs_read type");
+#endif
        if (uio->uio_resid == 0)
                return (0);
        if (uio->uio_resid == 0)
                return (0);
-       if (uio->uio_offset < 0)
+       if (uio->uio_offset < 0 && vp->v_type != VDIR)
                return (EINVAL);
                return (EINVAL);
+       biosize = VFSTONFS(vp->v_mount)->nm_rsize;
        /*
         * If the file's modify time on the server has changed since the
         * last read rpc or you have written to the file,
         * you may have lost data cache consistency with the
         * server, so flush all of the file's data out of the cache.
        /*
         * If the file's modify time on the server has changed since the
         * last read rpc or you have written to the file,
         * you may have lost data cache consistency with the
         * server, so flush all of the file's data out of the cache.
-        * This will implicitly bring the modify time up to date, since
-        * up to date attributes are returned in the reply to any write rpc's
+        * Then force a getattr rpc to ensure that you have up to date
+        * attributes.
         * NB: This implies that cache data can be read when up to
         * NFS_ATTRTIMEO seconds out of date. If you find that you need current
         * attributes this could be forced by setting n_attrstamp to 0 before
         * NB: This implies that cache data can be read when up to
         * NFS_ATTRTIMEO seconds out of date. If you find that you need current
         * attributes this could be forced by setting n_attrstamp to 0 before
-        * the nfs_getattr() call.
+        * the nfs_dogetattr() call.
         */
         */
-       if (np->n_flag & NMODIFIED) {
-               np->n_flag &= ~NMODIFIED;
-               if (error = nfs_blkflush(vp, (daddr_t)0, np->n_size, TRUE))
-                       return (error);
-               if (error = nfs_getattr(vp, &vattr, cred))
-                       return (error);
-               np->n_mtime = vattr.va_mtime.tv_sec;
-       } else {
-               if (error = nfs_getattr(vp, &vattr, cred))
-                       return (error);
-               if (np->n_mtime != vattr.va_mtime.tv_sec) {
-                       if (error = nfs_blkflush(vp, (daddr_t)0,
-                               np->n_size, TRUE))
+       if (vp->v_type != VLNK) {
+               if (np->n_flag & NMODIFIED) {
+                       np->n_flag &= ~NMODIFIED;
+                       vinvalbuf(vp, TRUE);
+                       np->n_attrstamp = 0;
+                       np->n_direofoffset = 0;
+                       if (error = nfs_dogetattr(vp, &vattr, cred, 1,
+                           uio->uio_procp))
                                return (error);
                        np->n_mtime = vattr.va_mtime.tv_sec;
                                return (error);
                        np->n_mtime = vattr.va_mtime.tv_sec;
+               } else {
+                       if (error = nfs_dogetattr(vp, &vattr, cred, 1,
+                           uio->uio_procp))
+                               return (error);
+                       if (np->n_mtime != vattr.va_mtime.tv_sec) {
+                               np->n_direofoffset = 0;
+                               vinvalbuf(vp, TRUE);
+                               np->n_mtime = vattr.va_mtime.tv_sec;
+                       }
                }
        }
                }
        }
-       np->n_flag |= NBUFFERED;
        do {
        do {
-               lbn = uio->uio_offset >> NFS_BIOSHIFT;
-               on = uio->uio_offset & (NFS_BIOSIZE-1);
-               n = MIN((unsigned)(NFS_BIOSIZE - on), uio->uio_resid);
+           switch (vp->v_type) {
+           case VREG:
+               nfsstats.biocache_reads++;
+               lbn = uio->uio_offset / biosize;
+               on = uio->uio_offset & (biosize-1);
+               n = MIN((unsigned)(biosize - on), uio->uio_resid);
                diff = np->n_size - uio->uio_offset;
                if (diff <= 0)
                        return (error);
                if (diff < n)
                        n = diff;
                diff = np->n_size - uio->uio_offset;
                if (diff <= 0)
                        return (error);
                if (diff < n)
                        n = diff;
-               bn = lbn*(NFS_BIOSIZE/DEV_BSIZE);
-               rablock = (lbn+1)*(NFS_BIOSIZE/DEV_BSIZE);
-               if (np->n_lastr+1 == lbn && np->n_size > (rablock*DEV_BSIZE))
-                       error = breada(vp, bn, NFS_BIOSIZE, rablock, NFS_BIOSIZE,
+               bn = lbn*(biosize/DEV_BSIZE);
+               rablock = (lbn+1)*(biosize/DEV_BSIZE);
+               if (vp->v_lastr + 1 == lbn &&
+                   np->n_size > (rablock * DEV_BSIZE))
+                       error = breada(vp, bn, biosize, rablock, biosize,
                                cred, &bp);
                else
                                cred, &bp);
                else
-                       error = bread(vp, bn, NFS_BIOSIZE, cred, &bp);
-               np->n_lastr = lbn;
+                       error = bread(vp, bn, biosize, cred, &bp);
+               vp->v_lastr = lbn;
                if (bp->b_resid) {
                if (bp->b_resid) {
-                       diff = (on >= (NFS_BIOSIZE-bp->b_resid)) ? 0 :
-                               (NFS_BIOSIZE-bp->b_resid-on);
-                       n = MIN(n, diff);
-               }
-               if (error) {
-                       brelse(bp);
-                       return (error);
+                  diff = (on >= (biosize-bp->b_resid)) ? 0 :
+                       (biosize-bp->b_resid-on);
+                  n = MIN(n, diff);
                }
                }
-               if (n > 0)
-                       error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
-               if (n+on == NFS_BIOSIZE || uio->uio_offset == np->n_size)
-                       bp->b_flags |= B_AGE;
+               break;
+           case VLNK:
+               nfsstats.biocache_readlinks++;
+               on = 0;
+               error = bread(vp, (daddr_t)0, NFS_MAXPATHLEN, cred, &bp);
+               n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
+               break;
+           case VDIR:
+               nfsstats.biocache_readdirs++;
+               on = 0;
+               error = bread(vp, uio->uio_offset, NFS_DIRBLKSIZ, cred, &bp);
+               n = MIN(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid);
+               break;
+           };
+           if (error) {
                brelse(bp);
                brelse(bp);
+               return (error);
+           }
+           if (n > 0)
+               error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
+           switch (vp->v_type) {
+           case VREG:
+               if (n+on == biosize || uio->uio_offset == np->n_size)
+                       bp->b_flags |= B_AGE;
+               break;
+           case VLNK:
+               n = 0;
+               break;
+           case VDIR:
+               uio->uio_offset = bp->b_blkno;
+               break;
+           };
+           brelse(bp);
        } while (error == 0 && uio->uio_resid > 0 && n != 0);
        return (error);
 }
        } while (error == 0 && uio->uio_resid > 0 && n != 0);
        return (error);
 }
@@ -138,22 +191,40 @@ nfs_write(vp, uio, ioflag, cred)
        int ioflag;
        struct ucred *cred;
 {
        int ioflag;
        struct ucred *cred;
 {
+       struct proc *p = uio->uio_procp;
+       register int biosize;
        struct buf *bp;
        struct nfsnode *np = VTONFS(vp);
        struct buf *bp;
        struct nfsnode *np = VTONFS(vp);
+       struct vattr vattr;
        daddr_t lbn, bn;
        daddr_t lbn, bn;
-       int i, n, on, count, error = 0;
+       int n, on, error = 0;
 
 
+#ifdef DIAGNOSTIC
+       if (uio->uio_rw != UIO_WRITE)
+               panic("nfs_write mode");
+       if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
+               panic("nfs_write proc");
+#endif
+       if (vp->v_type != VREG)
+               return (EIO);
        /* Should we try and do this ?? */
        /* Should we try and do this ?? */
-       if (vp->v_type == VREG && (ioflag & IO_APPEND))
-               uio->uio_offset = np->n_size;
+       if (ioflag & (IO_APPEND | IO_SYNC)) {
+               if (np->n_flag & NMODIFIED) {
+                       np->n_flag &= ~NMODIFIED;
+                       vinvalbuf(vp, TRUE);
+               }
+               if (ioflag & IO_APPEND) {
+                       np->n_attrstamp = 0;
+                       if (error = nfs_dogetattr(vp, &vattr, cred, 1, p))
+                               return (error);
+                       uio->uio_offset = np->n_size;
+               }
+               return (nfs_writerpc(vp, uio, cred));
+       }
 #ifdef notdef
        cnt = uio->uio_resid;
        osize = np->n_size;
 #endif
 #ifdef notdef
        cnt = uio->uio_resid;
        osize = np->n_size;
 #endif
-       if (uio->uio_rw != UIO_WRITE)
-               panic("nfs_write mode");
-       if (vp->v_type != VREG)
-               panic("nfs_write type");
        if (uio->uio_offset < 0)
                return (EINVAL);
        if (uio->uio_resid == 0)
        if (uio->uio_offset < 0)
                return (EINVAL);
        if (uio->uio_resid == 0)
@@ -162,69 +233,63 @@ nfs_write(vp, uio, ioflag, cred)
         * Maybe this should be above the vnode op call, but so long as
         * file servers have no limits, i don't think it matters
         */
         * Maybe this should be above the vnode op call, but so long as
         * file servers have no limits, i don't think it matters
         */
-       if (vp->v_type == VREG &&
-           uio->uio_offset + uio->uio_resid >
-             u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
-               psignal(u.u_procp, SIGXFSZ);
+       if (uio->uio_offset + uio->uio_resid >
+             p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
+               psignal(p, SIGXFSZ);
                return (EFBIG);
        }
                return (EFBIG);
        }
-       np->n_flag |= (NMODIFIED|NBUFFERED);
+       /*
+        * I use nm_rsize, not nm_wsize so that all buffer cache blocks
+        * will be the same size within a filesystem. nfs_writerpc will
+        * still use nm_wsize when sizing the rpc's.
+        */
+       biosize = VFSTONFS(vp->v_mount)->nm_rsize;
+       np->n_flag |= NMODIFIED;
        do {
        do {
-               lbn = uio->uio_offset >> NFS_BIOSHIFT;
-               on = uio->uio_offset & (NFS_BIOSIZE-1);
-               n = MIN((unsigned)(NFS_BIOSIZE - on), uio->uio_resid);
-               if (uio->uio_offset+n > np->n_size)
+               nfsstats.biocache_writes++;
+               lbn = uio->uio_offset / biosize;
+               on = uio->uio_offset & (biosize-1);
+               n = MIN((unsigned)(biosize - on), uio->uio_resid);
+               if (uio->uio_offset+n > np->n_size) {
                        np->n_size = uio->uio_offset+n;
                        np->n_size = uio->uio_offset+n;
-               bn = lbn*(NFS_BIOSIZE/DEV_BSIZE);
-               count = howmany(NFS_BIOSIZE, CLBYTES);
-               for (i = 0; i < count; i++)
-                       munhash(vp, bn + i * CLBYTES / DEV_BSIZE);
-               bp = getblk(vp, bn, NFS_BIOSIZE);
+                       vnode_pager_setsize(vp, np->n_size);
+               }
+               bn = lbn*(biosize/DEV_BSIZE);
+again:
+               bp = getblk(vp, bn, biosize);
                if (bp->b_wcred == NOCRED) {
                        crhold(cred);
                        bp->b_wcred = cred;
                }
                if (bp->b_dirtyend > 0) {
                        /*
                if (bp->b_wcred == NOCRED) {
                        crhold(cred);
                        bp->b_wcred = cred;
                }
                if (bp->b_dirtyend > 0) {
                        /*
-                        * If the new write will leave a contiguous
-                        * dirty area, just update the b_dirtyoff and
-                        * b_dirtyend
-                        * otherwise force a write rpc of the old dirty
-                        * area
+                        * If the new write will leave a contiguous dirty
+                        * area, just update the b_dirtyoff and b_dirtyend,
+                        * otherwise force a write rpc of the old dirty area.
                         */
                        if (on <= bp->b_dirtyend && (on+n) >= bp->b_dirtyoff) {
                                bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
                                bp->b_dirtyend = MAX((on+n), bp->b_dirtyend);
                        } else {
                         */
                        if (on <= bp->b_dirtyend && (on+n) >= bp->b_dirtyoff) {
                                bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
                                bp->b_dirtyend = MAX((on+n), bp->b_dirtyend);
                        } else {
-                               /*
-                                * Like bwrite() but without the brelse
-                                */
-                               bp->b_flags &= ~(B_READ | B_DONE |
-                                   B_ERROR | B_DELWRI | B_ASYNC);
-                               u.u_ru.ru_oublock++;
-                               VOP_STRATEGY(bp);
-                               error = biowait(bp);
-                               if (bp->b_flags & B_ERROR) {
-                                       brelse(bp);
-                                       if (bp->b_error)
-                                               error = bp->b_error;
-                                       else
-                                               error = EIO;
+                               bp->b_proc = p;
+                               if (error = bwrite(bp))
                                        return (error);
                                        return (error);
-                               }
-                               bp->b_dirtyoff = on;
-                               bp->b_dirtyend = on+n;
+                               goto again;
                        }
                } else {
                        bp->b_dirtyoff = on;
                        bp->b_dirtyend = on+n;
                }
                        }
                } else {
                        bp->b_dirtyoff = on;
                        bp->b_dirtyend = on+n;
                }
-               if (error = uiomove(bp->b_un.b_addr + on, n, uio))
+               if (error = uiomove(bp->b_un.b_addr + on, n, uio)) {
+                       brelse(bp);
                        return (error);
                        return (error);
-               if ((n+on) == NFS_BIOSIZE) {
+               }
+               if ((n+on) == biosize) {
                        bp->b_flags |= B_AGE;
                        bp->b_flags |= B_AGE;
+                       bp->b_proc = (struct proc *)0;
                        bawrite(bp);
                } else {
                        bawrite(bp);
                } else {
+                       bp->b_proc = (struct proc *)0;
                        bdwrite(bp);
                }
        } while (error == 0 && uio->uio_resid > 0 && n != 0);
                        bdwrite(bp);
                }
        } while (error == 0 && uio->uio_resid > 0 && n != 0);
@@ -238,63 +303,3 @@ nfs_write(vp, uio, ioflag, cred)
 #endif
        return (error);
 }
 #endif
        return (error);
 }
-
-/*
- * Flush and invalidate all of the buffers associated with the blocks of vp
- */
-nfs_blkflush(vp, blkno, size, invalidate)
-       struct vnode *vp;
-       daddr_t blkno;
-       long size;
-       int invalidate;
-{
-       register struct buf *ep;
-       struct buf *dp;
-       daddr_t curblk, nextblk, ecurblk, lastblk;
-       int s, error, allerrors = 0;
-     
-       /*
-        * Iterate through each possible hash chain.
-        */
-       lastblk = blkno + btodb(size+DEV_BSIZE-1) - 1;
-       for (curblk = blkno; curblk <= lastblk; curblk = nextblk) {
-#if RND & (RND-1)
-               nextblk = ((curblk / RND) + 1) * RND;
-#else
-               nextblk = ((curblk & ~(RND-1)) + RND);
-#endif
-               ecurblk = nextblk > lastblk ? lastblk : nextblk - 1;
-               dp = BUFHASH(vp, curblk);
-loop:
-               for (ep = dp->b_forw; ep != dp; ep = ep->b_forw) {
-                       if (ep->b_vp != vp || (ep->b_flags & B_INVAL))
-                               continue;
-                       /* look for overlap */
-                       if (ep->b_bcount == 0 || ep->b_blkno > ecurblk ||
-                           ep->b_blkno + btodb(ep->b_bcount) <= curblk)
-                               continue;
-                       s = splbio();
-                       if (ep->b_flags&B_BUSY) {
-                               ep->b_flags |= B_WANTED;
-                               sleep((caddr_t)ep, PRIBIO+1);
-                               splx(s);
-                               goto loop;
-                       }
-                       if (ep->b_flags & B_DELWRI) {
-                               splx(s);
-                               notavail(ep);
-                               if (error = bwrite(ep))
-                                       allerrors = error;
-                               goto loop;
-                       }
-                       splx(s);
-                       if (invalidate) {
-                               notavail(ep);
-                               ep->b_flags |= B_INVAL;
-                               brelvp(ep);
-                               brelse(ep);
-                       }
-               }
-       }
-       return (allerrors);
-}