merge in changes for Sparc
[unix-history] / usr / src / sys / kern / vfs_syscalls.c
index 014a1f0..0dd6591 100644 (file)
@@ -4,7 +4,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)vfs_syscalls.c      7.69 (Berkeley) %G%
+ *     @(#)vfs_syscalls.c      7.87 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
 #include "proc.h"
 #include "uio.h"
 #include "malloc.h"
 #include "proc.h"
 #include "uio.h"
 #include "malloc.h"
+#include <vm/vm.h>
+
+#ifdef REF_DIAGNOSTIC
+#define CURCOUNT (curproc ? curproc->p_spare[0] : 0)
+#define CHECKPOINTREF int oldrefcount = CURCOUNT;
+#define CHECKREFS(F) if (oldrefcount != CURCOUNT) \
+       printf("REFCOUNT: %s, old=%d, new=%d\n", (F), oldrefcount, CURCOUNT);
+#else
+#define CHECKPOINTREF
+#define CHECKREFS(D)
+#endif
 
 /*
  * Virtual File System System Calls
 
 /*
  * Virtual File System System Calls
@@ -38,7 +49,7 @@ mount(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_UNLOCK;
        register struct vnode *vp;
        register struct mount *mp;
        int error, flag;
        register struct vnode *vp;
        register struct mount *mp;
        int error, flag;
@@ -52,13 +63,10 @@ mount(p, uap, retval)
        /*
         * Get vnode to be covered
         */
        /*
         * Get vnode to be covered
         */
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->dir;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->dir, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (uap->flags & MNT_UPDATE) {
                if ((vp->v_flag & VROOT) == 0) {
                        vput(vp);
        if (uap->flags & MNT_UPDATE) {
                if ((vp->v_flag & VROOT) == 0) {
                        vput(vp);
@@ -99,10 +107,8 @@ mount(p, uap, retval)
         */
        mp = (struct mount *)malloc((u_long)sizeof(struct mount),
                M_MOUNT, M_WAITOK);
         */
        mp = (struct mount *)malloc((u_long)sizeof(struct mount),
                M_MOUNT, M_WAITOK);
+       bzero((char *)mp, (u_long)sizeof(struct mount));
        mp->mnt_op = vfssw[uap->type];
        mp->mnt_op = vfssw[uap->type];
-       mp->mnt_flag = 0;
-       mp->mnt_exroot = 0;
-       mp->mnt_mounth = NULLVP;
        if (error = vfs_lock(mp)) {
                free((caddr_t)mp, M_MOUNT);
                vput(vp);
        if (error = vfs_lock(mp)) {
                free((caddr_t)mp, M_MOUNT);
                vput(vp);
@@ -143,7 +149,7 @@ update:
        /*
         * Mount the filesystem.
         */
        /*
         * Mount the filesystem.
         */
-       error = VFS_MOUNT(mp, uap->dir, uap->data, ndp, p);
+       error = VFS_MOUNT(mp, uap->dir, uap->data, &nd, p);
        if (mp->mnt_flag & MNT_UPDATE) {
                mp->mnt_flag &= ~MNT_UPDATE;
                vrele(vp);
        if (mp->mnt_flag & MNT_UPDATE) {
                mp->mnt_flag &= ~MNT_UPDATE;
                vrele(vp);
@@ -187,7 +193,6 @@ unmount(p, uap, retval)
        int *retval;
 {
        register struct vnode *vp;
        int *retval;
 {
        register struct vnode *vp;
-       register struct nameidata *ndp;
        struct mount *mp;
        int error;
        struct nameidata nd;
        struct mount *mp;
        int error;
        struct nameidata nd;
@@ -198,13 +203,10 @@ unmount(p, uap, retval)
        if (error = suser(p->p_ucred, &p->p_acflag))
                return (error);
 
        if (error = suser(p->p_ucred, &p->p_acflag))
                return (error);
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->pathp;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->pathp, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        /*
         * Must be the root of the filesystem
         */
        /*
         * Must be the root of the filesystem
         */
@@ -246,6 +248,8 @@ dounmount(mp, flags, p)
        } else {
                vrele(coveredvp);
                vfs_remove(mp);
        } else {
                vrele(coveredvp);
                vfs_remove(mp);
+               if (mp->mnt_mounth != NULL)
+                       panic("unmount: dangling vnode");
                free((caddr_t)mp, M_MOUNT);
        }
        return (error);
                free((caddr_t)mp, M_MOUNT);
        }
        return (error);
@@ -297,18 +301,14 @@ quotactl(p, uap, retval)
        int *retval;
 {
        register struct mount *mp;
        int *retval;
 {
        register struct mount *mp;
-       register struct nameidata *ndp;
        int error;
        struct nameidata nd;
 
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->path;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       mp = ndp->ni_vp->v_mount;
-       vrele(ndp->ni_vp);
+       mp = nd.ni_vp->v_mount;
+       vrele(nd.ni_vp);
        return (VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, p));
 }
 
        return (VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, p));
 }
 
@@ -325,20 +325,16 @@ statfs(p, uap, retval)
        int *retval;
 {
        register struct mount *mp;
        int *retval;
 {
        register struct mount *mp;
-       register struct nameidata *ndp;
        register struct statfs *sp;
        int error;
        struct nameidata nd;
 
        register struct statfs *sp;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->path;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       mp = ndp->ni_vp->v_mount;
+       mp = nd.ni_vp->v_mount;
        sp = &mp->mnt_stat;
        sp = &mp->mnt_stat;
-       vrele(ndp->ni_vp);
+       vrele(nd.ni_vp);
        if (error = VFS_STATFS(mp, sp, p))
                return (error);
        sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
        if (error = VFS_STATFS(mp, sp, p))
                return (error);
        sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
@@ -433,6 +429,9 @@ fchdir(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_ACCESS;
+       USES_VOP_LOCK;
+       USES_VOP_UNLOCK;
        register struct filedesc *fdp = p->p_fd;
        register struct vnode *vp;
        struct file *fp;
        register struct filedesc *fdp = p->p_fd;
        register struct vnode *vp;
        struct file *fp;
@@ -466,19 +465,15 @@ chdir(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
        register struct filedesc *fdp = p->p_fd;
        int error;
        struct nameidata nd;
 
        register struct filedesc *fdp = p->p_fd;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = chdirec(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = chdirec(&nd, p))
                return (error);
        vrele(fdp->fd_cdir);
                return (error);
        vrele(fdp->fd_cdir);
-       fdp->fd_cdir = ndp->ni_vp;
+       fdp->fd_cdir = nd.ni_vp;
        return (0);
 }
 
        return (0);
 }
 
@@ -493,22 +488,18 @@ chroot(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
        register struct filedesc *fdp = p->p_fd;
        int error;
        struct nameidata nd;
 
        if (error = suser(p->p_ucred, &p->p_acflag))
                return (error);
        register struct filedesc *fdp = p->p_fd;
        int error;
        struct nameidata nd;
 
        if (error = suser(p->p_ucred, &p->p_acflag))
                return (error);
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = chdirec(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = chdirec(&nd, p))
                return (error);
        if (fdp->fd_rdir != NULL)
                vrele(fdp->fd_rdir);
                return (error);
        if (fdp->fd_rdir != NULL)
                vrele(fdp->fd_rdir);
-       fdp->fd_rdir = ndp->ni_vp;
+       fdp->fd_rdir = nd.ni_vp;
        return (0);
 }
 
        return (0);
 }
 
@@ -516,13 +507,15 @@ chroot(p, uap, retval)
  * Common routine for chroot and chdir.
  */
 chdirec(ndp, p)
  * Common routine for chroot and chdir.
  */
 chdirec(ndp, p)
-       struct nameidata *ndp;
+       register struct nameidata *ndp;
        struct proc *p;
 {
        struct proc *p;
 {
+       USES_VOP_ACCESS;
+       USES_VOP_UNLOCK;
        struct vnode *vp;
        int error;
 
        struct vnode *vp;
        int error;
 
-       if (error = namei(ndp, p))
+       if (error = namei(ndp))
                return (error);
        vp = ndp->ni_vp;
        if (vp->v_type != VDIR)
                return (error);
        vp = ndp->ni_vp;
        if (vp->v_type != VDIR)
@@ -549,12 +542,15 @@ open(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       struct nameidata *ndp;
+       USES_VOP_ADVLOCK;
+       USES_VOP_UNLOCK;
        register struct filedesc *fdp = p->p_fd;
        register struct file *fp;
        register struct filedesc *fdp = p->p_fd;
        register struct file *fp;
+       register struct vnode *vp;
        int fmode, cmode;
        struct file *nfp;
        int fmode, cmode;
        struct file *nfp;
-       int indx, error;
+       int type, indx, error;
+       struct flock lf;
        struct nameidata nd;
        extern struct fileops vnops;
 
        struct nameidata nd;
        extern struct fileops vnops;
 
@@ -563,16 +559,16 @@ open(p, uap, retval)
        fp = nfp;
        fmode = FFLAGS(uap->mode);
        cmode = ((uap->crtmode &~ fdp->fd_cmask) & 07777) &~ S_ISVTX;
        fp = nfp;
        fmode = FFLAGS(uap->mode);
        cmode = ((uap->crtmode &~ fdp->fd_cmask) & 07777) &~ S_ISVTX;
-       ndp = &nd;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
+       NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->fname, p);
        p->p_dupfd = -indx - 1;                 /* XXX check for fdopen */
        p->p_dupfd = -indx - 1;                 /* XXX check for fdopen */
-       if (error = vn_open(ndp, p, fmode, cmode)) {
-               crfree(fp->f_cred);
-               fp->f_count--;
-               if (error == ENODEV &&          /* XXX from fdopen */
-                   p->p_dupfd >= 0 &&
-                   (error = dupfdopen(fdp, indx, p->p_dupfd, fmode)) == 0) {
+       if (error = vn_open(&nd, fmode, cmode)) {
+               int dfd = p->p_dupfd;
+               p->p_dupfd = 0;
+               ffree(fp);
+               if ((error == ENODEV || error == ENXIO) && /* XXX from fdopen */
+                   dfd >= 0 &&
+                   (error = dupfdopen(fdp, indx, p->p_dupfd,
+                                       fmode, error)) == 0) {
                        *retval = indx;
                        return (0);
                }
                        *retval = indx;
                        return (0);
                }
@@ -581,10 +577,33 @@ open(p, uap, retval)
                fdp->fd_ofiles[indx] = NULL;
                return (error);
        }
                fdp->fd_ofiles[indx] = NULL;
                return (error);
        }
+       p->p_dupfd = 0;
+       vp = nd.ni_vp;
        fp->f_flag = fmode & FMASK;
        fp->f_type = DTYPE_VNODE;
        fp->f_ops = &vnops;
        fp->f_flag = fmode & FMASK;
        fp->f_type = DTYPE_VNODE;
        fp->f_ops = &vnops;
-       fp->f_data = (caddr_t)ndp->ni_vp;
+       fp->f_data = (caddr_t)vp;
+       if (fmode & (O_EXLOCK | O_SHLOCK)) {
+               lf.l_whence = SEEK_SET;
+               lf.l_start = 0;
+               lf.l_len = 0;
+               if (fmode & O_EXLOCK)
+                       lf.l_type = F_WRLCK;
+               else
+                       lf.l_type = F_RDLCK;
+               type = F_FLOCK;
+               if ((fmode & FNONBLOCK) == 0)
+                       type |= F_WAIT;
+               if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {
+                       VOP_UNLOCK(vp);
+                       (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
+                       ffree(fp);
+                       fdp->fd_ofiles[indx] = NULL;
+                       return (error);
+               }
+               fp->f_flag |= FHASLOCK;
+       }
+       VOP_UNLOCK(vp);
        *retval = indx;
        return (0);
 }
        *retval = indx;
        return (0);
 }
@@ -627,21 +646,20 @@ mknod(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_MKNOD;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
+       CHECKPOINTREF;
        if (error = suser(p->p_ucred, &p->p_acflag))
                return (error);
        if (error = suser(p->p_ucred, &p->p_acflag))
                return (error);
-       ndp = &nd;
-       ndp->ni_nameiop = CREATE | LOCKPARENT;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp != NULL) {
                error = EEXIST;
                goto out;
        if (vp != NULL) {
                error = EEXIST;
                goto out;
@@ -666,16 +684,18 @@ mknod(p, uap, retval)
        vattr.va_rdev = uap->dev;
 out:
        if (!error) {
        vattr.va_rdev = uap->dev;
 out:
        if (!error) {
-               error = VOP_MKNOD(ndp, &vattr, p->p_ucred, p);
+               LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+               error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
        } else {
        } else {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == vp)
-                       vrele(ndp->ni_dvp);
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
+                       vput(nd.ni_dvp);
                if (vp)
                        vrele(vp);
        }
                if (vp)
                        vrele(vp);
        }
+       CHECKREFS("mknod");
        return (error);
 }
 
        return (error);
 }
 
@@ -691,7 +711,8 @@ mkfifo(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_MKNOD;
        struct vattr vattr;
        int error;
        struct nameidata nd;
        struct vattr vattr;
        int error;
        struct nameidata nd;
@@ -699,25 +720,23 @@ mkfifo(p, uap, retval)
 #ifndef FIFO
        return (EOPNOTSUPP);
 #else
 #ifndef FIFO
        return (EOPNOTSUPP);
 #else
-       ndp = &nd;
-       ndp->ni_nameiop = CREATE | LOCKPARENT;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       if (ndp->ni_vp != NULL) {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == ndp->ni_vp)
-                       vrele(ndp->ni_dvp);
+       if (nd.ni_vp != NULL) {
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == nd.ni_vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
-               vrele(ndp->ni_vp);
+                       vput(nd.ni_dvp);
+               vrele(nd.ni_vp);
                return (EEXIST);
        }
        VATTR_NULL(&vattr);
        vattr.va_type = VFIFO;
        vattr.va_mode = (uap->fmode & 07777) &~ p->p_fd->fd_cmask;
                return (EEXIST);
        }
        VATTR_NULL(&vattr);
        vattr.va_type = VFIFO;
        vattr.va_mode = (uap->fmode & 07777) &~ p->p_fd->fd_cmask;
-       return (VOP_MKNOD(ndp, &vattr, p->p_ucred, p));
+       LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
 #endif /* FIFO */
 }
 
 #endif /* FIFO */
 }
 
@@ -733,47 +752,48 @@ link(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_LINK;
        register struct vnode *vp, *xp;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp, *xp;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->target;
-       if (error = namei(ndp, p))
+       CHECKPOINTREF;
+       NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->target, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_type == VDIR &&
            (error = suser(p->p_ucred, &p->p_acflag)))
                goto out1;
        if (vp->v_type == VDIR &&
            (error = suser(p->p_ucred, &p->p_acflag)))
                goto out1;
-       ndp->ni_nameiop = CREATE | LOCKPARENT;
-       ndp->ni_dirp = (caddr_t)uap->linkname;
-       if (error = namei(ndp, p))
+       nd.ni_cnd.cn_nameiop = CREATE;
+       nd.ni_cnd.cn_flags = LOCKPARENT;
+       nd.ni_dirp = (caddr_t)uap->linkname;
+       if (error = namei(&nd))
                goto out1;
                goto out1;
-       xp = ndp->ni_vp;
+       xp = nd.ni_vp;
        if (xp != NULL) {
                error = EEXIST;
                goto out;
        }
        if (xp != NULL) {
                error = EEXIST;
                goto out;
        }
-       xp = ndp->ni_dvp;
-       if (vp->v_mount != xp->v_mount)
-               error = EXDEV;
+       xp = nd.ni_dvp;
 out:
        if (!error) {
 out:
        if (!error) {
-               error = VOP_LINK(vp, ndp, p);
+               LEASE_CHECK(xp, p, p->p_ucred, LEASE_WRITE);
+               LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+               error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
        } else {
        } else {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == ndp->ni_vp)
-                       vrele(ndp->ni_dvp);
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == nd.ni_vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
-               if (ndp->ni_vp)
-                       vrele(ndp->ni_vp);
+                       vput(nd.ni_dvp);
+               if (nd.ni_vp)
+                       vrele(nd.ni_vp);
        }
 out1:
        vrele(vp);
        }
 out1:
        vrele(vp);
+       CHECKREFS("link");
        return (error);
 }
 
        return (error);
 }
 
@@ -789,36 +809,37 @@ symlink(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_SYMLINK;
        struct vattr vattr;
        char *target;
        int error;
        struct nameidata nd;
 
        struct vattr vattr;
        char *target;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->linkname;
+       CHECKPOINTREF;
        MALLOC(target, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
        if (error = copyinstr(uap->target, target, MAXPATHLEN, (u_int *)0))
                goto out;
        MALLOC(target, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
        if (error = copyinstr(uap->target, target, MAXPATHLEN, (u_int *)0))
                goto out;
-       ndp->ni_nameiop = CREATE | LOCKPARENT;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->linkname, p);
+       if (error = namei(&nd))
                goto out;
                goto out;
-       if (ndp->ni_vp) {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == ndp->ni_vp)
-                       vrele(ndp->ni_dvp);
+       if (nd.ni_vp) {
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == nd.ni_vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
-               vrele(ndp->ni_vp);
+                       vput(nd.ni_dvp);
+               vrele(nd.ni_vp);
                error = EEXIST;
                goto out;
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = 0777 &~ p->p_fd->fd_cmask;
                error = EEXIST;
                goto out;
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = 0777 &~ p->p_fd->fd_cmask;
-       error = VOP_SYMLINK(ndp, &vattr, target, p);
+       LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, target);
 out:
        FREE(target, M_NAMEI);
 out:
        FREE(target, M_NAMEI);
+       CHECKREFS("symlink");
        return (error);
 }
 
        return (error);
 }
 
@@ -829,22 +850,21 @@ out:
 unlink(p, uap, retval)
        struct proc *p;
        struct args {
 unlink(p, uap, retval)
        struct proc *p;
        struct args {
-               char    *fname;
+               char    *name;
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_REMOVE;
        register struct vnode *vp;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       CHECKPOINTREF;
+       NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE, uap->name, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_type == VDIR &&
            (error = suser(p->p_ucred, &p->p_acflag)))
                goto out;
        if (vp->v_type == VDIR &&
            (error = suser(p->p_ucred, &p->p_acflag)))
                goto out;
@@ -858,22 +878,55 @@ unlink(p, uap, retval)
        (void) vnode_pager_uncache(vp);
 out:
        if (!error) {
        (void) vnode_pager_uncache(vp);
 out:
        if (!error) {
-               error = VOP_REMOVE(ndp, p);
+               LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+               LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+               error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
        } else {
        } else {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == vp)
-                       vrele(ndp->ni_dvp);
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
+                       vput(nd.ni_dvp);
                vput(vp);
        }
                vput(vp);
        }
+       CHECKREFS("unlink");
        return (error);
 }
 
        return (error);
 }
 
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
 /*
  * Seek system call.
  */
 lseek(p, uap, retval)
 /*
  * Seek system call.
  */
 lseek(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     fdes;
+               long    off;
+               int     sbase;
+       } *uap;
+       long *retval;
+{
+       struct nargs {
+               int     fdes;
+               off_t   off;
+               int     sbase;
+       } nuap;
+       quad_t qret;
+       int error;
+
+       nuap.fdes = uap->fdes;
+       nuap.off = uap->off;
+       nuap.sbase = uap->sbase;
+       error = __lseek(p, &nuap, &qret);
+       *retval = qret;
+       return (error);
+}
+#endif /* COMPAT_43 || COMPAT_SUNOS */
+
+/*
+ * Seek system call.
+ */
+__lseek(p, uap, retval)
        struct proc *p;
        register struct args {
                int     fdes;
        struct proc *p;
        register struct args {
                int     fdes;
@@ -882,6 +935,7 @@ lseek(p, uap, retval)
        } *uap;
        off_t *retval;
 {
        } *uap;
        off_t *retval;
 {
+       USES_VOP_GETATTR;
        struct ucred *cred = p->p_ucred;
        register struct filedesc *fdp = p->p_fd;
        register struct file *fp;
        struct ucred *cred = p->p_ucred;
        register struct filedesc *fdp = p->p_fd;
        register struct file *fp;
@@ -929,23 +983,20 @@ saccess(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ACCESS;
        register struct ucred *cred = p->p_ucred;
        register struct vnode *vp;
        int error, mode, svuid, svgid;
        struct nameidata nd;
 
        register struct ucred *cred = p->p_ucred;
        register struct vnode *vp;
        int error, mode, svuid, svgid;
        struct nameidata nd;
 
-       ndp = &nd;
        svuid = cred->cr_uid;
        svgid = cred->cr_groups[0];
        cred->cr_uid = p->p_cred->p_ruid;
        cred->cr_groups[0] = p->p_cred->p_rgid;
        svuid = cred->cr_uid;
        svgid = cred->cr_groups[0];
        cred->cr_uid = p->p_cred->p_ruid;
        cred->cr_groups[0] = p->p_cred->p_rgid;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                goto out1;
                goto out1;
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        /*
         * fmode == 0 means only check for exist
         */
        /*
         * fmode == 0 means only check for exist
         */
@@ -967,6 +1018,96 @@ out1:
        return (error);
 }
 
        return (error);
 }
 
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
+/*
+ * Stat system call.
+ * This version follows links.
+ */
+/* ARGSUSED */
+ostat(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               char    *fname;
+               struct ostat *ub;
+       } *uap;
+       int *retval;
+{
+       struct stat sb;
+       struct ostat osb;
+       int error;
+       struct nameidata nd;
+
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
+               return (error);
+       error = vn_stat(nd.ni_vp, &sb, p);
+       vput(nd.ni_vp);
+       if (error)
+               return (error);
+       cvtstat(&sb, &osb);
+       error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb));
+       return (error);
+}
+
+/*
+ * Lstat system call.
+ * This version does not follow links.
+ */
+/* ARGSUSED */
+olstat(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               char    *fname;
+               struct ostat *ub;
+       } *uap;
+       int *retval;
+{
+       struct stat sb;
+       struct ostat osb;
+       int error;
+       struct nameidata nd;
+
+       NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
+               return (error);
+       error = vn_stat(nd.ni_vp, &sb, p);
+       vput(nd.ni_vp);
+       if (error)
+               return (error);
+       cvtstat(&sb, &osb);
+       error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb));
+       return (error);
+}
+
+/*
+ * convert from an old to a new stat structure.
+ */
+cvtstat(st, ost)
+       struct stat *st;
+       struct ostat *ost;
+{
+
+       ost->st_dev = st->st_dev;
+       ost->st_ino = st->st_ino;
+       ost->st_mode = st->st_mode;
+       ost->st_nlink = st->st_nlink;
+       ost->st_uid = st->st_uid;
+       ost->st_gid = st->st_gid;
+       ost->st_rdev = st->st_rdev;
+       if (st->st_size < (quad_t)1 << 32)
+               ost->st_size = st->st_size;
+       else
+               ost->st_size = -2;
+       ost->st_atime = st->st_atime;
+       ost->st_mtime = st->st_mtime;
+       ost->st_ctime = st->st_ctime;
+       ost->st_blksize = st->st_blksize;
+       ost->st_blocks = st->st_blocks;
+       ost->st_flags = st->st_flags;
+       ost->st_gen = st->st_gen;
+}
+#endif /* COMPAT_43 || COMPAT_SUNOS */
+
 /*
  * Stat system call.
  * This version follows links.
 /*
  * Stat system call.
  * This version follows links.
@@ -980,19 +1121,15 @@ stat(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
        struct stat sb;
        int error;
        struct nameidata nd;
 
        struct stat sb;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       error = vn_stat(ndp->ni_vp, &sb, p);
-       vput(ndp->ni_vp);
+       error = vn_stat(nd.ni_vp, &sb, p);
+       vput(nd.ni_vp);
        if (error)
                return (error);
        error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
        if (error)
                return (error);
        error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
@@ -1012,19 +1149,15 @@ lstat(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
        struct stat sb;
        int error;
        struct nameidata nd;
 
        struct stat sb;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | LOCKLEAF | NOFOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       error = vn_stat(ndp->ni_vp, &sb, p);
-       vput(ndp->ni_vp);
+       error = vn_stat(nd.ni_vp, &sb, p);
+       vput(nd.ni_vp);
        if (error)
                return (error);
        error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
        if (error)
                return (error);
        error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
@@ -1044,20 +1177,18 @@ readlink(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_READLINK;
        register struct vnode *vp;
        struct iovec aiov;
        struct uio auio;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct iovec aiov;
        struct uio auio;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->name;
-       if (error = namei(ndp, p))
+       CHECKPOINTREF;
+       NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->name, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_type != VLNK) {
                error = EINVAL;
                goto out;
        if (vp->v_type != VLNK) {
                error = EINVAL;
                goto out;
@@ -1075,6 +1206,7 @@ readlink(p, uap, retval)
 out:
        vput(vp);
        *retval = uap->count - auio.uio_resid;
 out:
        vput(vp);
        *retval = uap->count - auio.uio_resid;
+       CHECKREFS("readlink");
        return (error);
 }
 
        return (error);
 }
 
@@ -1090,25 +1222,23 @@ chflags(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_SETATTR;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        }
        VATTR_NULL(&vattr);
        vattr.va_flags = uap->flags;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        }
        VATTR_NULL(&vattr);
        vattr.va_flags = uap->flags;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
@@ -1127,6 +1257,9 @@ fchflags(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_LOCK;
+       USES_VOP_SETATTR;
+       USES_VOP_UNLOCK;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
@@ -1142,6 +1275,7 @@ fchflags(p, uap, retval)
        }
        VATTR_NULL(&vattr);
        vattr.va_flags = uap->flags;
        }
        VATTR_NULL(&vattr);
        vattr.va_flags = uap->flags;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        VOP_UNLOCK(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        VOP_UNLOCK(vp);
@@ -1160,25 +1294,23 @@ chmod(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_SETATTR;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = uap->fmode & 07777;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = uap->fmode & 07777;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
@@ -1197,6 +1329,9 @@ fchmod(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_LOCK;
+       USES_VOP_SETATTR;
+       USES_VOP_UNLOCK;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
@@ -1212,6 +1347,7 @@ fchmod(p, uap, retval)
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = uap->fmode & 07777;
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = uap->fmode & 07777;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        VOP_UNLOCK(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        VOP_UNLOCK(vp);
@@ -1231,19 +1367,16 @@ chown(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_SETATTR;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | NOFOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
@@ -1251,6 +1384,7 @@ chown(p, uap, retval)
        VATTR_NULL(&vattr);
        vattr.va_uid = uap->uid;
        vattr.va_gid = uap->gid;
        VATTR_NULL(&vattr);
        vattr.va_uid = uap->uid;
        vattr.va_gid = uap->gid;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
@@ -1270,6 +1404,9 @@ fchown(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_LOCK;
+       USES_VOP_SETATTR;
+       USES_VOP_UNLOCK;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
@@ -1286,6 +1423,7 @@ fchown(p, uap, retval)
        VATTR_NULL(&vattr);
        vattr.va_uid = uap->uid;
        vattr.va_gid = uap->gid;
        VATTR_NULL(&vattr);
        vattr.va_uid = uap->uid;
        vattr.va_gid = uap->gid;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        VOP_UNLOCK(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        VOP_UNLOCK(vp);
@@ -1304,7 +1442,7 @@ utimes(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_SETATTR;
        register struct vnode *vp;
        struct timeval tv[2];
        struct vattr vattr;
        register struct vnode *vp;
        struct timeval tv[2];
        struct vattr vattr;
@@ -1313,31 +1451,77 @@ utimes(p, uap, retval)
 
        if (error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)))
                return (error);
 
        if (error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)))
                return (error);
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        }
        VATTR_NULL(&vattr);
        if (vp->v_mount->mnt_flag & MNT_RDONLY) {
                error = EROFS;
                goto out;
        }
        VATTR_NULL(&vattr);
-       vattr.va_atime = tv[0];
-       vattr.va_mtime = tv[1];
+       vattr.va_atime.ts_sec = tv[0].tv_sec;
+       vattr.va_atime.ts_nsec = tv[0].tv_usec * 1000;
+       vattr.va_mtime.ts_sec = tv[1].tv_sec;
+       vattr.va_mtime.ts_nsec = tv[1].tv_usec * 1000;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
        return (error);
 }
 
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
        return (error);
 }
 
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
 /*
  * Truncate a file given its path name.
  */
 /* ARGSUSED */
 truncate(p, uap, retval)
 /*
  * Truncate a file given its path name.
  */
 /* ARGSUSED */
 truncate(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               char    *fname;
+               long    length;
+       } *uap;
+       int *retval;
+{
+       struct nargs {
+               char    *fname;
+               off_t   length;
+       } nuap;
+
+       nuap.fname = uap->fname;
+       nuap.length = uap->length;
+       return (__truncate(p, &nuap, retval));
+}
+
+/*
+ * Truncate a file given a file descriptor.
+ */
+/* ARGSUSED */
+ftruncate(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     fd;
+               long    length;
+       } *uap;
+       int *retval;
+{
+       struct nargs {
+               int     fd;
+               off_t   length;
+       } nuap;
+
+       nuap.fd = uap->fd;
+       nuap.length = uap->length;
+       return (__ftruncate(p, &nuap, retval));
+}
+#endif /* COMPAT_43 || COMPAT_SUNOS */
+
+/*
+ * Truncate a file given its path name.
+ */
+/* ARGSUSED */
+__truncate(p, uap, retval)
        struct proc *p;
        register struct args {
                char    *fname;
        struct proc *p;
        register struct args {
                char    *fname;
@@ -1345,19 +1529,17 @@ truncate(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ACCESS;
+       USES_VOP_SETATTR;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_type == VDIR) {
                error = EISDIR;
                goto out;
        if (vp->v_type == VDIR) {
                error = EISDIR;
                goto out;
@@ -1367,6 +1549,7 @@ truncate(p, uap, retval)
                goto out;
        VATTR_NULL(&vattr);
        vattr.va_size = uap->length;
                goto out;
        VATTR_NULL(&vattr);
        vattr.va_size = uap->length;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
        error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
 out:
        vput(vp);
@@ -1377,7 +1560,7 @@ out:
  * Truncate a file given a file descriptor.
  */
 /* ARGSUSED */
  * Truncate a file given a file descriptor.
  */
 /* ARGSUSED */
-ftruncate(p, uap, retval)
+__ftruncate(p, uap, retval)
        struct proc *p;
        register struct args {
                int     fd;
        struct proc *p;
        register struct args {
                int     fd;
@@ -1385,6 +1568,9 @@ ftruncate(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_LOCK;
+       USES_VOP_SETATTR;
+       USES_VOP_UNLOCK;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
        struct vattr vattr;
        struct vnode *vp;
        struct file *fp;
@@ -1404,6 +1590,7 @@ ftruncate(p, uap, retval)
                goto out;
        VATTR_NULL(&vattr);
        vattr.va_size = uap->length;
                goto out;
        VATTR_NULL(&vattr);
        vattr.va_size = uap->length;
+       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
 out:
        VOP_UNLOCK(vp);
        error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
 out:
        VOP_UNLOCK(vp);
@@ -1421,6 +1608,9 @@ fsync(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_FSYNC;
+       USES_VOP_LOCK;
+       USES_VOP_UNLOCK;
        register struct vnode *vp;
        struct file *fp;
        int error;
        register struct vnode *vp;
        struct file *fp;
        int error;
@@ -1449,25 +1639,23 @@ rename(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_ABORTOP;
+       USES_VOP_RENAME;
        register struct vnode *tvp, *fvp, *tdvp;
        register struct vnode *tvp, *fvp, *tdvp;
-       register struct nameidata *ndp;
+       struct nameidata fromnd, tond;
        int error;
        int error;
-       struct nameidata nd, tond;
 
 
-       ndp = &nd;
-       ndp->ni_nameiop = DELETE | WANTPARENT;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->from;
-       if (error = namei(ndp, p))
+       CHECKPOINTREF;
+       NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
+               uap->from, p);
+       if (error = namei(&fromnd))
                return (error);
                return (error);
-       fvp = ndp->ni_vp;
-       nddup(ndp, &tond);
-       tond.ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
-       tond.ni_segflg = UIO_USERSPACE;
-       tond.ni_dirp = uap->to;
-       if (error = namei(&tond, p)) {
-               VOP_ABORTOP(ndp);
-               vrele(ndp->ni_dvp);
+       fvp = fromnd.ni_vp;
+       NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
+               UIO_USERSPACE, uap->to, p);
+       if (error = namei(&tond)) {
+               VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
+               vrele(fromnd.ni_dvp);
                vrele(fvp);
                goto out1;
        }
                vrele(fvp);
                goto out1;
        }
@@ -1481,40 +1669,48 @@ rename(p, uap, retval)
                        error = EISDIR;
                        goto out;
                }
                        error = EISDIR;
                        goto out;
                }
-               if (fvp->v_mount != tvp->v_mount) {
-                       error = EXDEV;
-                       goto out;
-               }
-       }
-       if (fvp->v_mount != tdvp->v_mount) {
-               error = EXDEV;
-               goto out;
        }
        if (fvp == tdvp)
                error = EINVAL;
        /*
        }
        if (fvp == tdvp)
                error = EINVAL;
        /*
-        * If source is the same as the destination,
+        * If source is the same as the destination (that is the
+        * same inode number with the same name in the same directory),
         * then there is nothing to do.
         */
         * then there is nothing to do.
         */
-       if (fvp == tvp)
+       if (fvp == tvp && fromnd.ni_dvp == tdvp &&
+           fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
+           !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
+             fromnd.ni_cnd.cn_namelen))
                error = -1;
 out:
        if (!error) {
                error = -1;
 out:
        if (!error) {
-               error = VOP_RENAME(ndp, &tond, p);
+               LEASE_CHECK(tdvp, p, p->p_ucred, LEASE_WRITE);
+               if (fromnd.ni_dvp != tdvp)
+                       LEASE_CHECK(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+               if (tvp)
+                       LEASE_CHECK(tvp, p, p->p_ucred, LEASE_WRITE);
+               error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
+                                  tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
        } else {
        } else {
-               VOP_ABORTOP(&tond);
+               VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
                if (tdvp == tvp)
                        vrele(tdvp);
                else
                        vput(tdvp);
                if (tvp)
                        vput(tvp);
                if (tdvp == tvp)
                        vrele(tdvp);
                else
                        vput(tdvp);
                if (tvp)
                        vput(tvp);
-               VOP_ABORTOP(ndp);
-               vrele(ndp->ni_dvp);
+               VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
+               vrele(fromnd.ni_dvp);
                vrele(fvp);
        }
                vrele(fvp);
        }
+       p->p_spare[1]--;
+       vrele(tond.ni_startdir);
+       FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
 out1:
 out1:
-       ndrele(&tond);
+       p->p_spare[1]--;
+       vrele(fromnd.ni_startdir);
+       FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
+       CHECKREFS("rename");
        if (error == -1)
                return (0);
        return (error);
        if (error == -1)
                return (0);
        return (error);
@@ -1532,34 +1728,36 @@ mkdir(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_MKDIR;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = CREATE | LOCKPARENT;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->name;
-       if (error = namei(ndp, p))
+       CHECKPOINTREF;
+       NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->name, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp != NULL) {
        if (vp != NULL) {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == vp)
-                       vrele(ndp->ni_dvp);
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
+                       vput(nd.ni_dvp);
                vrele(vp);
                vrele(vp);
+               CHECKREFS("mkdir1");
                return (EEXIST);
        }
        VATTR_NULL(&vattr);
        vattr.va_type = VDIR;
        vattr.va_mode = (uap->dmode & 0777) &~ p->p_fd->fd_cmask;
                return (EEXIST);
        }
        VATTR_NULL(&vattr);
        vattr.va_type = VDIR;
        vattr.va_mode = (uap->dmode & 0777) &~ p->p_fd->fd_cmask;
-       error = VOP_MKDIR(ndp, &vattr, p);
+       LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
        if (!error)
        if (!error)
-               vput(ndp->ni_vp);
+               vput(nd.ni_vp);
+       CHECKREFS("mkdir2");
        return (error);
 }
 
        return (error);
 }
 
@@ -1574,18 +1772,17 @@ rmdir(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_ABORTOP;
+       USES_VOP_RMDIR;
        register struct vnode *vp;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->name;
-       if (error = namei(ndp, p))
+       CHECKPOINTREF;
+       NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE, uap->name, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_type != VDIR) {
                error = ENOTDIR;
                goto out;
        if (vp->v_type != VDIR) {
                error = ENOTDIR;
                goto out;
@@ -1593,7 +1790,7 @@ rmdir(p, uap, retval)
        /*
         * No rmdir "." please.
         */
        /*
         * No rmdir "." please.
         */
-       if (ndp->ni_dvp == vp) {
+       if (nd.ni_dvp == vp) {
                error = EINVAL;
                goto out;
        }
                error = EINVAL;
                goto out;
        }
@@ -1604,15 +1801,18 @@ rmdir(p, uap, retval)
                error = EBUSY;
 out:
        if (!error) {
                error = EBUSY;
 out:
        if (!error) {
-               error = VOP_RMDIR(ndp, p);
+               LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+               LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+               error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
        } else {
        } else {
-               VOP_ABORTOP(ndp);
-               if (ndp->ni_dvp == vp)
-                       vrele(ndp->ni_dvp);
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == vp)
+                       vrele(nd.ni_dvp);
                else
                else
-                       vput(ndp->ni_dvp);
+                       vput(nd.ni_dvp);
                vput(vp);
        }
                vput(vp);
        }
+       CHECKREFS("rmdir");
        return (error);
 }
 
        return (error);
 }
 
@@ -1629,6 +1829,9 @@ getdirentries(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
+       USES_VOP_LOCK;
+       USES_VOP_READDIR;
+       USES_VOP_UNLOCK;
        register struct vnode *vp;
        struct file *fp;
        struct uio auio;
        register struct vnode *vp;
        struct file *fp;
        struct uio auio;
@@ -1693,19 +1896,16 @@ revoke(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct nameidata *ndp;
+       USES_VOP_GETATTR;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        struct nameidata nd;
 
-       ndp = &nd;
-       ndp->ni_nameiop = LOOKUP | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp, p))
+       NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->fname, p);
+       if (error = namei(&nd))
                return (error);
                return (error);
-       vp = ndp->ni_vp;
+       vp = nd.ni_vp;
        if (vp->v_type != VCHR && vp->v_type != VBLK) {
                error = EINVAL;
                goto out;
        if (vp->v_type != VCHR && vp->v_type != VBLK) {
                error = EINVAL;
                goto out;