extensive checking of directories only when DIIAGNOSTIC set
[unix-history] / usr / src / sys / ufs / ffs / ufs_lookup.c
index ad2edd0..7bb8f77 100644 (file)
@@ -2,32 +2,27 @@
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * 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.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)ufs_lookup.c        7.21 (Berkeley) %G%
+ *     @(#)ufs_lookup.c        7.31 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "user.h"
+#include "namei.h"
 #include "buf.h"
 #include "file.h"
 #include "vnode.h"
 #include "buf.h"
 #include "file.h"
 #include "vnode.h"
-#include "../ufs/quota.h"
-#include "../ufs/inode.h"
-#include "../ufs/fs.h"
+
+#include "quota.h"
+#include "inode.h"
+#include "fs.h"
 
 struct nchstats nchstats;
 
 struct nchstats nchstats;
+#ifdef DIAGNOSTIC
 int    dirchk = 1;
 int    dirchk = 1;
+#else
+int    dirchk = 0;
+#endif
 
 /*
  * Convert a component of a pathname into a pointer to a locked inode.
 
 /*
  * Convert a component of a pathname into a pointer to a locked inode.
@@ -64,9 +59,10 @@ int  dirchk = 1;
  *
  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
  */
  *
  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
  */
-ufs_lookup(vdp, ndp)
+ufs_lookup(vdp, ndp, p)
        register struct vnode *vdp;
        register struct nameidata *ndp;
        register struct vnode *vdp;
        register struct nameidata *ndp;
+       struct proc *p;
 {
        register struct inode *dp;      /* the directory we are searching */
        register struct fs *fs;         /* file system that directory is in */
 {
        register struct inode *dp;      /* the directory we are searching */
        register struct fs *fs;         /* file system that directory is in */
@@ -94,7 +90,7 @@ ufs_lookup(vdp, ndp)
        dp = VTOI(vdp);
        fs = dp->i_fs;
        lockparent = ndp->ni_nameiop & LOCKPARENT;
        dp = VTOI(vdp);
        fs = dp->i_fs;
        lockparent = ndp->ni_nameiop & LOCKPARENT;
-       flag = ndp->ni_nameiop & OPFLAG;
+       flag = ndp->ni_nameiop & OPMASK;
        wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
 
        /*
        wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
 
        /*
@@ -102,7 +98,7 @@ ufs_lookup(vdp, ndp)
         */
        if ((dp->i_mode&IFMT) != IFDIR)
                return (ENOTDIR);
         */
        if ((dp->i_mode&IFMT) != IFDIR)
                return (ENOTDIR);
-       if (error = ufs_access(vdp, VEXEC, ndp->ni_cred))
+       if (error = ufs_access(vdp, VEXEC, ndp->ni_cred, p))
                return (error);
 
        /*
                return (error);
 
        /*
@@ -117,8 +113,10 @@ ufs_lookup(vdp, ndp)
 
                if (error == ENOENT)
                        return (error);
 
                if (error == ENOENT)
                        return (error);
+#ifdef PARANOID
                if (vdp == ndp->ni_rdir && ndp->ni_isdotdot)
                        panic("ufs_lookup: .. through root");
                if (vdp == ndp->ni_rdir && ndp->ni_isdotdot)
                        panic("ufs_lookup: .. through root");
+#endif
                /*
                 * Get the next vnode in the path.
                 * See comment below starting `Step through' for
                /*
                 * Get the next vnode in the path.
                 * See comment below starting `Step through' for
@@ -134,9 +132,12 @@ ufs_lookup(vdp, ndp)
                } else if (ndp->ni_isdotdot) {
                        IUNLOCK(pdp);
                        error = vget(vdp);
                } else if (ndp->ni_isdotdot) {
                        IUNLOCK(pdp);
                        error = vget(vdp);
+                       if (!error && lockparent && *ndp->ni_next == '\0')
+                               ILOCK(pdp);
                } else {
                        error = vget(vdp);
                } else {
                        error = vget(vdp);
-                       IUNLOCK(pdp);
+                       if (!lockparent || error || *ndp->ni_next != '\0')
+                               IUNLOCK(pdp);
                }
                /*
                 * Check that the capability number did not change
                }
                /*
                 * Check that the capability number did not change
@@ -145,8 +146,9 @@ ufs_lookup(vdp, ndp)
                if (!error) {
                        if (vpid == vdp->v_id)
                                return (0);
                if (!error) {
                        if (vpid == vdp->v_id)
                                return (0);
-                       else
-                               iput(dp);
+                       iput(dp);
+                       if (lockparent && pdp != dp && *ndp->ni_next == '\0')
+                               IUNLOCK(pdp);
                }
                ILOCK(pdp);
                dp = pdp;
                }
                ILOCK(pdp);
                dp = pdp;
@@ -315,7 +317,7 @@ searchloop:
                 * Access for write is interpreted as allowing
                 * creation of files in the directory.
                 */
                 * Access for write is interpreted as allowing
                 * creation of files in the directory.
                 */
-               if (error = ufs_access(vdp, VWRITE, ndp->ni_cred))
+               if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
                        return (error);
                /*
                 * Return an indication of where the new directory
                        return (error);
                /*
                 * Return an indication of where the new directory
@@ -354,7 +356,7 @@ searchloop:
        /*
         * Insert name into cache (as non-existent) if appropriate.
         */
        /*
         * Insert name into cache (as non-existent) if appropriate.
         */
-       if (ndp->ni_makeentry)
+       if (ndp->ni_makeentry && flag != CREATE)
                cache_enter(ndp);
        return (ENOENT);
 
                cache_enter(ndp);
        return (ENOENT);
 
@@ -390,7 +392,7 @@ found:
                /*
                 * Write access to directory required to delete files.
                 */
                /*
                 * Write access to directory required to delete files.
                 */
-               if (error = ufs_access(vdp, VWRITE, ndp->ni_cred))
+               if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
                        return (error);
                /*
                 * Return pointer to current entry in ndp->ni_offset,
                        return (error);
                /*
                 * Return pointer to current entry in ndp->ni_offset,
@@ -435,7 +437,7 @@ found:
         * regular file, or empty directory.
         */
        if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
         * regular file, or empty directory.
         */
        if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
-               if (error = ufs_access(vdp, VWRITE, ndp->ni_cred))
+               if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
                        return (error);
                /*
                 * Careful about locking second inode.
                        return (error);
                /*
                 * Careful about locking second inode.
@@ -508,7 +510,8 @@ dirbad(ip, offset, how)
 
        printf("%s: bad dir ino %d at offset %d: %s\n",
            ip->i_fs->fs_fsmnt, ip->i_number, offset, how);
 
        printf("%s: bad dir ino %d at offset %d: %s\n",
            ip->i_fs->fs_fsmnt, ip->i_number, offset, how);
-       panic("bad dir");
+       if (ip->i_fs->fs_ronly == 0)
+               panic("bad dir");
 }
 
 /*
 }
 
 /*
@@ -570,14 +573,18 @@ direnter(ip, ndp)
                ndp->ni_count = newentrysize;
                ndp->ni_resid = newentrysize;
                ndp->ni_base = (caddr_t)&ndp->ni_dent;
                ndp->ni_count = newentrysize;
                ndp->ni_resid = newentrysize;
                ndp->ni_base = (caddr_t)&ndp->ni_dent;
+               ndp->ni_iov = &ndp->ni_nd.nd_iovec;
+               ndp->ni_iovcnt = 1;
+               ndp->ni_rw = UIO_WRITE;
                ndp->ni_uioseg = UIO_SYSSPACE;
                error =
                    ufs_write(ndp->ni_dvp, &ndp->ni_uio, IO_SYNC, ndp->ni_cred);
                ndp->ni_uioseg = UIO_SYSSPACE;
                error =
                    ufs_write(ndp->ni_dvp, &ndp->ni_uio, IO_SYNC, ndp->ni_cred);
-               if (DIRBLKSIZ > dp->i_fs->fs_fsize)
+               if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
                        panic("wdir: blksize"); /* XXX - should grow w/balloc */
                        panic("wdir: blksize"); /* XXX - should grow w/balloc */
-               else
+               } else {
                        dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
                        dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
-               iput(dp);
+                       dp->i_flag |= ICHG;
+               }
                return (error);
        }
 
                return (error);
        }
 
@@ -602,10 +609,8 @@ direnter(ip, ndp)
        /*
         * Get the block containing the space for the new directory entry.
         */
        /*
         * Get the block containing the space for the new directory entry.
         */
-       if (error = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf, &bp)) {
-               iput(dp);
+       if (error = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf, &bp))
                return (error);
                return (error);
-       }
        /*
         * Find space for the new entry.  In the simple case, the
         * entry at offset base will have the space.  If it does
        /*
         * Find space for the new entry.  In the simple case, the
         * entry at offset base will have the space.  If it does
@@ -650,7 +655,6 @@ direnter(ip, ndp)
        dp->i_flag |= IUPD|ICHG;
        if (!error && ndp->ni_endoff && ndp->ni_endoff < dp->i_size)
                error = itrunc(dp, (u_long)ndp->ni_endoff, IO_SYNC);
        dp->i_flag |= IUPD|ICHG;
        if (!error && ndp->ni_endoff && ndp->ni_endoff < dp->i_size)
                error = itrunc(dp, (u_long)ndp->ni_endoff, IO_SYNC);
-       iput(dp);
        return (error);
 }
 
        return (error);
 }
 
@@ -681,6 +685,9 @@ dirremove(ndp)
                ndp->ni_dent.d_ino = 0;
                ndp->ni_count = ndp->ni_resid = DIRSIZ(&ndp->ni_dent);
                ndp->ni_base = (caddr_t)&ndp->ni_dent;
                ndp->ni_dent.d_ino = 0;
                ndp->ni_count = ndp->ni_resid = DIRSIZ(&ndp->ni_dent);
                ndp->ni_base = (caddr_t)&ndp->ni_dent;
+               ndp->ni_iov = &ndp->ni_nd.nd_iovec;
+               ndp->ni_iovcnt = 1;
+               ndp->ni_rw = UIO_WRITE;
                ndp->ni_uioseg = UIO_SYSSPACE;
                error =
                    ufs_write(ndp->ni_dvp, &ndp->ni_uio, IO_SYNC, ndp->ni_cred);
                ndp->ni_uioseg = UIO_SYSSPACE;
                error =
                    ufs_write(ndp->ni_dvp, &ndp->ni_uio, IO_SYNC, ndp->ni_cred);
@@ -712,6 +719,9 @@ dirrewrite(dp, ip, ndp)
        ndp->ni_dent.d_ino = ip->i_number;
        ndp->ni_count = ndp->ni_resid = DIRSIZ(&ndp->ni_dent);
        ndp->ni_base = (caddr_t)&ndp->ni_dent;
        ndp->ni_dent.d_ino = ip->i_number;
        ndp->ni_count = ndp->ni_resid = DIRSIZ(&ndp->ni_dent);
        ndp->ni_base = (caddr_t)&ndp->ni_dent;
+       ndp->ni_iov = &ndp->ni_nd.nd_iovec;
+       ndp->ni_iovcnt = 1;
+       ndp->ni_rw = UIO_WRITE;
        ndp->ni_uioseg = UIO_SYSSPACE;
        return (ufs_write(ITOV(dp), &ndp->ni_uio, IO_SYNC, ndp->ni_cred));
 }
        ndp->ni_uioseg = UIO_SYSSPACE;
        return (ufs_write(ITOV(dp), &ndp->ni_uio, IO_SYNC, ndp->ni_cred));
 }
@@ -767,8 +777,8 @@ dirempty(ip, parentino, cred)
 #define        MINDIRSIZ (sizeof (struct dirtemplate) / 2)
 
        for (off = 0; off < ip->i_size; off += dp->d_reclen) {
 #define        MINDIRSIZ (sizeof (struct dirtemplate) / 2)
 
        for (off = 0; off < ip->i_size; off += dp->d_reclen) {
-               error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
-                   off, UIO_SYSSPACE, IO_NODELOCKED, cred, &count);
+               error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
+                  UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
                /*
                 * Since we read MINDIRSIZ, residual must
                 * be 0 unless we're at end of file.
                /*
                 * Since we read MINDIRSIZ, residual must
                 * be 0 unless we're at end of file.
@@ -828,7 +838,7 @@ checkpath(source, target, cred)
                }
                error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)&dirbuf,
                        sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
                }
                error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)&dirbuf,
                        sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
-                       IO_NODELOCKED, cred, (int *)0);
+                       IO_NODELOCKED, cred, (int *)0, (struct proc *)0);
                if (error != 0)
                        break;
                if (dirbuf.dotdot_namlen != 2 ||
                if (error != 0)
                        break;
                if (dirbuf.dotdot_namlen != 2 ||