fix readdir for non-linear union stacks
[unix-history] / usr / src / sys / kern / vfs_syscalls.c
index 3cb5c3b..4063304 100644 (file)
@@ -1,10 +1,15 @@
 /*
  * Copyright (c) 1989, 1993
  *     The Regents of the University of California.  All rights reserved.
 /*
  * Copyright (c) 1989, 1993
  *     The Regents of the University of California.  All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)vfs_syscalls.c      8.2 (Berkeley) %G%
+ *     @(#)vfs_syscalls.c      8.28 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
@@ -58,13 +63,9 @@ mount(p, uap, retval)
        register struct vnode *vp;
        register struct mount *mp;
        int error, flag;
        register struct vnode *vp;
        register struct mount *mp;
        int error, flag;
+       struct vattr va;
        struct nameidata nd;
 
        struct nameidata nd;
 
-       /*
-        * Must be super user
-        */
-       if (error = suser(p->p_ucred, &p->p_acflag))
-               return (error);
        /*
         * Get vnode to be covered
         */
        /*
         * Get vnode to be covered
         */
@@ -90,12 +91,49 @@ mount(p, uap, retval)
                }
                mp->mnt_flag |=
                    uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
                }
                mp->mnt_flag |=
                    uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
+               /*
+                * Only root, or the user that did the original mount is
+                * permitted to update it.
+                */
+               if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
+                   (error = suser(p->p_ucred, &p->p_acflag))) {
+                       vput(vp);
+                       return (error);
+               }
+               /*
+                * Do not allow NFS export by non-root users. Silently
+                * enforce MNT_NOSUID and MNT_NODEV for non-root users.
+                */
+               if (p->p_ucred->cr_uid != 0) {
+                       if (uap->flags & MNT_EXPORTED) {
+                               vput(vp);
+                               return (EPERM);
+                       }
+                       uap->flags |= MNT_NOSUID | MNT_NODEV;
+               }
                VOP_UNLOCK(vp);
                goto update;
        }
                VOP_UNLOCK(vp);
                goto update;
        }
-       if (vp->v_usecount != 1 && (uap->flags & MNT_UNION) == 0) {
+       /*
+        * If the user is not root, ensure that they own the directory
+        * onto which we are attempting to mount.
+        */
+       if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
+           (va.va_uid != p->p_ucred->cr_uid &&
+            (error = suser(p->p_ucred, &p->p_acflag)))) {
                vput(vp);
                vput(vp);
-               return (EBUSY);
+               return (error);
+       }
+       /*
+        * Do not allow NFS export by non-root users. Silently
+        * enforce MNT_NOSUID and MNT_NODEV for non-root users.
+        */
+       if (p->p_ucred->cr_uid != 0) {
+               if (uap->flags & MNT_EXPORTED) {
+                       vput(vp);
+                       return (EPERM);
+               }
+               uap->flags |= MNT_NOSUID | MNT_NODEV;
        }
        if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0))
                return (error);
        }
        if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0))
                return (error);
@@ -107,6 +145,10 @@ mount(p, uap, retval)
                vput(vp);
                return (ENODEV);
        }
                vput(vp);
                return (ENODEV);
        }
+       if (vp->v_mountedhere != NULL) {
+               vput(vp);
+               return (EBUSY);
+       }
 
        /*
         * Allocate and initialize the file system.
 
        /*
         * Allocate and initialize the file system.
@@ -120,14 +162,9 @@ mount(p, uap, retval)
                vput(vp);
                return (error);
        }
                vput(vp);
                return (error);
        }
-       if (vp->v_mountedhere != NULL) {
-               vfs_unlock(mp);
-               free((caddr_t)mp, M_MOUNT);
-               vput(vp);
-               return (EBUSY);
-       }
        vp->v_mountedhere = mp;
        mp->mnt_vnodecovered = vp;
        vp->v_mountedhere = mp;
        mp->mnt_vnodecovered = vp;
+       mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
 update:
        /*
         * Set the mount level flags.
 update:
        /*
         * Set the mount level flags.
@@ -136,10 +173,10 @@ update:
                mp->mnt_flag |= MNT_RDONLY;
        else if (mp->mnt_flag & MNT_RDONLY)
                mp->mnt_flag |= MNT_WANTRDWR;
                mp->mnt_flag |= MNT_RDONLY;
        else if (mp->mnt_flag & MNT_RDONLY)
                mp->mnt_flag |= MNT_WANTRDWR;
-       mp->mnt_flag &=~
-           (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION);
-       mp->mnt_flag |= uap->flags &
-           (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION);
+       mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
+           MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
+       mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
+           MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
        /*
         * Mount the filesystem.
         */
        /*
         * Mount the filesystem.
         */
@@ -157,23 +194,59 @@ update:
        /*
         * Put the new filesystem on the mount list after root.
         */
        /*
         * Put the new filesystem on the mount list after root.
         */
-       mp->mnt_next = rootfs->mnt_next;
-       mp->mnt_prev = rootfs;
-       rootfs->mnt_next = mp;
-       mp->mnt_next->mnt_prev = mp;
        cache_purge(vp);
        if (!error) {
        cache_purge(vp);
        if (!error) {
+               TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
+               checkdirs(vp);
                VOP_UNLOCK(vp);
                vfs_unlock(mp);
                error = VFS_START(mp, 0, p);
        } else {
                VOP_UNLOCK(vp);
                vfs_unlock(mp);
                error = VFS_START(mp, 0, p);
        } else {
-               vfs_remove(mp);
+               mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
+               vfs_unlock(mp);
                free((caddr_t)mp, M_MOUNT);
                vput(vp);
        }
        return (error);
 }
 
                free((caddr_t)mp, M_MOUNT);
                vput(vp);
        }
        return (error);
 }
 
+/*
+ * Scan all active processes to see if any of them have a current
+ * or root directory onto which the new filesystem has just been
+ * mounted. If so, replace them with the new mount point.
+ */
+checkdirs(olddp)
+       struct vnode *olddp;
+{
+       struct filedesc *fdp;
+       struct vnode *newdp;
+       struct proc *p;
+
+       if (olddp->v_usecount == 1)
+               return;
+       if (VFS_ROOT(olddp->v_mountedhere, &newdp))
+               panic("mount: lost mount");
+       for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
+               fdp = p->p_fd;
+               if (fdp->fd_cdir == olddp) {
+                       vrele(fdp->fd_cdir);
+                       VREF(newdp);
+                       fdp->fd_cdir = newdp;
+               }
+               if (fdp->fd_rdir == olddp) {
+                       vrele(fdp->fd_rdir);
+                       VREF(newdp);
+                       fdp->fd_rdir = newdp;
+               }
+       }
+       if (rootvnode == olddp) {
+               vrele(rootvnode);
+               VREF(newdp);
+               rootvnode = newdp;
+       }
+       vput(newdp);
+}
+
 /*
  * Unmount a file system.
  *
 /*
  * Unmount a file system.
  *
@@ -195,16 +268,22 @@ unmount(p, uap, retval)
        int error;
        struct nameidata nd;
 
        int error;
        struct nameidata nd;
 
-       /*
-        * Must be super user
-        */
-       if (error = suser(p->p_ucred, &p->p_acflag))
-               return (error);
-
        NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
+       mp = vp->v_mount;
+
+       /*
+        * Only root, or the user that did the original mount is
+        * permitted to unmount this filesystem.
+        */
+       if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
+           (error = suser(p->p_ucred, &p->p_acflag))) {
+               vput(vp);
+               return (error);
+       }
+
        /*
         * Must be the root of the filesystem
         */
        /*
         * Must be the root of the filesystem
         */
@@ -212,7 +291,6 @@ unmount(p, uap, retval)
                vput(vp);
                return (EINVAL);
        }
                vput(vp);
                return (EINVAL);
        }
-       mp = vp->v_mount;
        vput(vp);
        return (dounmount(mp, uap->flags, p));
 }
        vput(vp);
        return (dounmount(mp, uap->flags, p));
 }
@@ -235,6 +313,7 @@ dounmount(mp, flags, p)
        if (error = vfs_lock(mp))
                return (error);
 
        if (error = vfs_lock(mp))
                return (error);
 
+       mp->mnt_flag &=~ MNT_ASYNC;
        vnode_pager_umount(mp); /* release cached vnodes */
        cache_purgevfs(mp);     /* remove cache entries for this file sys */
        if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
        vnode_pager_umount(mp); /* release cached vnodes */
        cache_purgevfs(mp);     /* remove cache entries for this file sys */
        if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
@@ -246,8 +325,10 @@ dounmount(mp, flags, p)
                vfs_unlock(mp);
        } else {
                vrele(coveredvp);
                vfs_unlock(mp);
        } else {
                vrele(coveredvp);
-               vfs_remove(mp);
-               if (mp->mnt_mounth != NULL)
+               TAILQ_REMOVE(&mountlist, mp, mnt_list);
+               mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
+               vfs_unlock(mp);
+               if (mp->mnt_vnodelist.lh_first != NULL)
                        panic("unmount: dangling vnode");
                free((caddr_t)mp, M_MOUNT);
        }
                        panic("unmount: dangling vnode");
                free((caddr_t)mp, M_MOUNT);
        }
@@ -257,7 +338,7 @@ dounmount(mp, flags, p)
 /*
  * Sync each mounted filesystem.
  */
 /*
  * Sync each mounted filesystem.
  */
-#ifdef DIAGNOSTIC
+#ifdef DEBUG
 int syncprt = 0;
 struct ctldebug debug0 = { "syncprt", &syncprt };
 #endif
 int syncprt = 0;
 struct ctldebug debug0 = { "syncprt", &syncprt };
 #endif
@@ -271,24 +352,34 @@ sync(p, uap, retval)
        struct sync_args *uap;
        int *retval;
 {
        struct sync_args *uap;
        int *retval;
 {
-       register struct mount *mp;
-       struct mount *omp;
+       register struct mount *mp, *nmp;
+       int asyncflag;
 
 
-       mp = rootfs;
-       do {
+       for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
+               /*
+                * Get the next pointer in case we hang on vfs_busy
+                * while we are being unmounted.
+                */
+               nmp = mp->mnt_list.tqe_next;
                /*
                 * The lock check below is to avoid races with mount
                 * and unmount.
                 */
                if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
                    !vfs_busy(mp)) {
                /*
                 * The lock check below is to avoid races with mount
                 * and unmount.
                 */
                if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
                    !vfs_busy(mp)) {
+                       asyncflag = mp->mnt_flag & MNT_ASYNC;
+                       mp->mnt_flag &= ~MNT_ASYNC;
                        VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
                        VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
-                       omp = mp;
-                       mp = mp->mnt_next;
-                       vfs_unbusy(omp);
-               } else
-                       mp = mp->mnt_next;
-       } while (mp != rootfs);
+                       if (asyncflag)
+                               mp->mnt_flag |= MNT_ASYNC;
+                       /*
+                        * Get the next pointer again, as the next filesystem
+                        * might have been unmounted while we were sync'ing.
+                        */
+                       nmp = mp->mnt_list.tqe_next;
+                       vfs_unbusy(mp);
+               }
+       }
 #ifdef DIAGNOSTIC
        if (syncprt)
                vfs_bufstats();
 #ifdef DIAGNOSTIC
        if (syncprt)
                vfs_bufstats();
@@ -394,16 +485,15 @@ getfsstat(p, uap, retval)
        register struct getfsstat_args *uap;
        int *retval;
 {
        register struct getfsstat_args *uap;
        int *retval;
 {
-       register struct mount *mp;
+       register struct mount *mp, *nmp;
        register struct statfs *sp;
        caddr_t sfsp;
        long count, maxcount, error;
 
        maxcount = uap->bufsize / sizeof(struct statfs);
        sfsp = (caddr_t)uap->buf;
        register struct statfs *sp;
        caddr_t sfsp;
        long count, maxcount, error;
 
        maxcount = uap->bufsize / sizeof(struct statfs);
        sfsp = (caddr_t)uap->buf;
-       mp = rootfs;
-       count = 0;
-       do {
+       for (count = 0, mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
+               nmp = mp->mnt_list.tqe_next;
                if (sfsp && count < maxcount &&
                    ((mp->mnt_flag & MNT_MLOCK) == 0)) {
                        sp = &mp->mnt_stat;
                if (sfsp && count < maxcount &&
                    ((mp->mnt_flag & MNT_MLOCK) == 0)) {
                        sp = &mp->mnt_stat;
@@ -413,18 +503,15 @@ getfsstat(p, uap, retval)
                         */
                        if (((uap->flags & MNT_NOWAIT) == 0 ||
                            (uap->flags & MNT_WAIT)) &&
                         */
                        if (((uap->flags & MNT_NOWAIT) == 0 ||
                            (uap->flags & MNT_WAIT)) &&
-                           (error = VFS_STATFS(mp, sp, p))) {
-                               mp = mp->mnt_prev;
+                           (error = VFS_STATFS(mp, sp, p)))
                                continue;
                                continue;
-                       }
                        sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
                        if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp)))
                                return (error);
                        sfsp += sizeof(*sp);
                }
                count++;
                        sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
                        if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp)))
                                return (error);
                        sfsp += sizeof(*sp);
                }
                count++;
-               mp = mp->mnt_prev;
-       } while (mp != rootfs);
+       }
        if (sfsp && count > maxcount)
                *retval = maxcount;
        else
        if (sfsp && count > maxcount)
                *retval = maxcount;
        else
@@ -445,22 +532,36 @@ fchdir(p, uap, retval)
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
-       register struct vnode *vp;
+       struct vnode *vp, *tdp;
+       struct mount *mp;
        struct file *fp;
        int error;
 
        if (error = getvnode(fdp, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
        struct file *fp;
        int error;
 
        if (error = getvnode(fdp, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
+       VREF(vp);
        VOP_LOCK(vp);
        if (vp->v_type != VDIR)
                error = ENOTDIR;
        else
                error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
        VOP_LOCK(vp);
        if (vp->v_type != VDIR)
                error = ENOTDIR;
        else
                error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
+       while (!error && (mp = vp->v_mountedhere) != NULL) {
+               if (mp->mnt_flag & MNT_MLOCK) {
+                       mp->mnt_flag |= MNT_MWAIT;
+                       sleep((caddr_t)mp, PVFS);
+                       continue;
+               }
+               if (error = VFS_ROOT(mp, &tdp))
+                       break;
+               vput(vp);
+               vp = tdp;
+       }
        VOP_UNLOCK(vp);
        VOP_UNLOCK(vp);
-       if (error)
+       if (error) {
+               vrele(vp);
                return (error);
                return (error);
-       VREF(vp);
+       }
        vrele(fdp->fd_cdir);
        fdp->fd_cdir = vp;
        return (0);
        vrele(fdp->fd_cdir);
        fdp->fd_cdir = vp;
        return (0);
@@ -603,13 +704,14 @@ open(p, uap, retval)
                type = F_FLOCK;
                if ((flags & FNONBLOCK) == 0)
                        type |= F_WAIT;
                type = F_FLOCK;
                if ((flags & FNONBLOCK) == 0)
                        type |= F_WAIT;
+               VOP_UNLOCK(vp);
                if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {
                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);
                }
                        (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
                        ffree(fp);
                        fdp->fd_ofiles[indx] = NULL;
                        return (error);
                }
+               VOP_LOCK(vp);
                fp->f_flag |= FHASLOCK;
        }
        VOP_UNLOCK(vp);
                fp->f_flag |= FHASLOCK;
        }
        VOP_UNLOCK(vp);
@@ -656,6 +758,7 @@ mknod(p, uap, retval)
        register struct vnode *vp;
        struct vattr vattr;
        int error;
        register struct vnode *vp;
        struct vattr vattr;
        int error;
+       int whiteout;
        struct nameidata nd;
 
        CHECKPOINTREF;
        struct nameidata nd;
 
        CHECKPOINTREF;
@@ -665,31 +768,43 @@ mknod(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       if (vp != NULL) {
+       if (vp != NULL)
                error = EEXIST;
                error = EEXIST;
-               goto out;
-       }
-       VATTR_NULL(&vattr);
-       switch (uap->mode & S_IFMT) {
-       case S_IFMT:    /* used by badsect to flag bad sectors */
-               vattr.va_type = VBAD;
-               break;
-       case S_IFCHR:
-               vattr.va_type = VCHR;
-               break;
-       case S_IFBLK:
-               vattr.va_type = VBLK;
-               break;
-       default:
-               error = EINVAL;
-               goto out;
+       else {
+               VATTR_NULL(&vattr);
+               vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;
+               vattr.va_rdev = uap->dev;
+               whiteout = 0;
+
+               switch (uap->mode & S_IFMT) {
+               case S_IFMT:    /* used by badsect to flag bad sectors */
+                       vattr.va_type = VBAD;
+                       break;
+               case S_IFCHR:
+                       vattr.va_type = VCHR;
+                       break;
+               case S_IFBLK:
+                       vattr.va_type = VBLK;
+                       break;
+               case S_IFWHT:
+                       whiteout = 1;
+                       break;
+               default:
+                       error = EINVAL;
+                       break;
+               }
        }
        }
-       vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;
-       vattr.va_rdev = uap->dev;
-out:
        if (!error) {
        if (!error) {
-               LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
-               error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
+               VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+               if (whiteout) {
+                       error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
+                       if (error)
+                               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+                       vput(nd.ni_dvp);
+               } else {
+                       error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
+                                               &nd.ni_cnd, &vattr);
+               }
        } else {
                VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
                if (nd.ni_dvp == vp)
        } else {
                VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
                if (nd.ni_dvp == vp)
@@ -738,7 +853,7 @@ mkfifo(p, uap, retval)
        VATTR_NULL(&vattr);
        vattr.va_type = VFIFO;
        vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;
        VATTR_NULL(&vattr);
        vattr.va_type = VFIFO;
        vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;
-       LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
        return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
 #endif /* FIFO */
 }
        return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
 #endif /* FIFO */
 }
@@ -765,30 +880,31 @@ link(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       if (vp->v_type == VDIR &&
-           (error = suser(p->p_ucred, &p->p_acflag)))
-               goto out;
-       nd.ni_cnd.cn_nameiop = CREATE;
-       nd.ni_cnd.cn_flags = LOCKPARENT;
-       nd.ni_dirp = uap->link;
-       if (error = namei(&nd))
-               goto out;
-       if (nd.ni_vp != NULL)
-               error = EEXIST;
-       if (!error) {
-               LEASE_CHECK(nd.ni_dvp, 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 {
-               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
-               if (nd.ni_dvp == nd.ni_vp)
-                       vrele(nd.ni_dvp);
-               else
-                       vput(nd.ni_dvp);
-               if (nd.ni_vp)
-                       vrele(nd.ni_vp);
+       if (vp->v_type != VDIR ||
+           (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
+               nd.ni_cnd.cn_nameiop = CREATE;
+               nd.ni_cnd.cn_flags = LOCKPARENT;
+               nd.ni_dirp = uap->link;
+               if ((error = namei(&nd)) == 0) {
+                       if (nd.ni_vp != NULL)
+                               error = EEXIST;
+                       if (!error) {
+                               VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
+                                   LEASE_WRITE);
+                               VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
+                               error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
+                       } else {
+                               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+                               if (nd.ni_dvp == nd.ni_vp)
+                                       vrele(nd.ni_dvp);
+                               else
+                                       vput(nd.ni_dvp);
+                               if (nd.ni_vp)
+                                       vrele(nd.ni_vp);
+                       }
+               }
        }
        }
-out:   vrele(vp);
+       vrele(vp);
        CHECKREFS("link");
        return (error);
 }
        CHECKREFS("link");
        return (error);
 }
@@ -830,7 +946,7 @@ symlink(p, uap, retval)
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
        }
        VATTR_NULL(&vattr);
        vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
-       LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
 out:
        FREE(path, M_NAMEI);
        error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
 out:
        FREE(path, M_NAMEI);
@@ -838,6 +954,44 @@ out:
        return (error);
 }
 
        return (error);
 }
 
+/*
+ * Delete a whiteout from the filesystem.
+ */
+struct undelete_args {
+       char    *path;
+};
+/* ARGSUSED */
+undelete(p, uap, retval)
+       struct proc *p;
+       struct undelete_args *uap;
+       int *retval;
+{
+       int error;
+       struct nameidata nd;
+
+       NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE, uap->path, p);
+       error = namei(&nd);
+       if (error)
+               return (error);
+
+       if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+               if (nd.ni_dvp == nd.ni_vp)
+                       vrele(nd.ni_dvp);
+               else
+                       vput(nd.ni_dvp);
+               if (nd.ni_vp)
+                       vrele(nd.ni_vp);
+               return (EEXIST);
+       }
+
+       VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       if (error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE))
+               VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
+       vput(nd.ni_dvp);
+       return (error);
+}
+
 /*
  * Delete a name from the filesystem.
  */
 /*
  * Delete a name from the filesystem.
  */
@@ -859,21 +1013,22 @@ unlink(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        VOP_LOCK(vp);
-       if (vp->v_type == VDIR &&
-           (error = suser(p->p_ucred, &p->p_acflag)))
-               goto out;
-       /*
-        * The root of a mounted filesystem cannot be deleted.
-        */
-       if (vp->v_flag & VROOT)
-               error = EBUSY;
-       else
-               (void)vnode_pager_uncache(vp);
 
 
-out:   if (!error) {
-               LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       if (vp->v_type != VDIR ||
+           (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
+               /*
+                * The root of a mounted filesystem cannot be deleted.
+                */
+               if (vp->v_flag & VROOT)
+                       error = EBUSY;
+               else
+                       (void)vnode_pager_uncache(vp);
+       }
+
+       if (!error) {
+               VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
                error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
        } else {
                VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
                error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
        } else {
                VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
@@ -881,7 +1036,8 @@ out:       if (!error) {
                        vrele(nd.ni_dvp);
                else
                        vput(nd.ni_dvp);
                        vrele(nd.ni_dvp);
                else
                        vput(nd.ni_dvp);
-               vput(vp);
+               if (vp != NULLVP)
+                       vput(vp);
        }
        CHECKREFS("unlink");
        return (error);
        }
        CHECKREFS("unlink");
        return (error);
@@ -973,11 +1129,11 @@ access(p, uap, retval)
 {
        register struct ucred *cred = p->p_ucred;
        register struct vnode *vp;
 {
        register struct ucred *cred = p->p_ucred;
        register struct vnode *vp;
-       int error, flags, saved_uid, saved_gid;
+       int error, flags, t_gid, t_uid;
        struct nameidata nd;
 
        struct nameidata nd;
 
-       saved_uid = cred->cr_uid;
-       saved_gid = cred->cr_groups[0];
+       t_uid = cred->cr_uid;
+       t_gid = cred->cr_groups[0];
        cred->cr_uid = p->p_cred->p_ruid;
        cred->cr_groups[0] = p->p_cred->p_rgid;
        NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
        cred->cr_uid = p->p_cred->p_ruid;
        cred->cr_groups[0] = p->p_cred->p_rgid;
        NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
@@ -999,8 +1155,8 @@ access(p, uap, retval)
        }
        vput(vp);
 out1:
        }
        vput(vp);
 out1:
-       cred->cr_uid = saved_uid;
-       cred->cr_groups[0] = saved_gid;
+       cred->cr_uid = t_uid;
+       cred->cr_groups[0] = t_gid;
        return (error);
 }
 
        return (error);
 }
 
@@ -1048,18 +1204,48 @@ olstat(p, uap, retval)
        register struct olstat_args *uap;
        int *retval;
 {
        register struct olstat_args *uap;
        int *retval;
 {
-       struct stat sb;
+       struct vnode *vp, *dvp;
+       struct stat sb, sb1;
        struct ostat osb;
        int error;
        struct nameidata nd;
 
        struct ostat osb;
        int error;
        struct nameidata nd;
 
-       NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
+       NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
+           uap->path, p);
        if (error = namei(&nd))
                return (error);
        if (error = namei(&nd))
                return (error);
-       error = vn_stat(nd.ni_vp, &sb, p);
-       vput(nd.ni_vp);
-       if (error)
-               return (error);
+       /*
+        * For symbolic links, always return the attributes of its
+        * containing directory, except for mode, size, and links.
+        */
+       vp = nd.ni_vp;
+       dvp = nd.ni_dvp;
+       if (vp->v_type != VLNK) {
+               if (dvp == vp)
+                       vrele(dvp);
+               else
+                       vput(dvp);
+               error = vn_stat(vp, &sb, p);
+               vput(vp);
+               if (error)
+                       return (error);
+       } else {
+               error = vn_stat(dvp, &sb, p);
+               vput(dvp);
+               if (error) {
+                       vput(vp);
+                       return (error);
+               }
+               error = vn_stat(vp, &sb1, p);
+               vput(vp);
+               if (error)
+                       return (error);
+               sb.st_mode &= ~S_IFDIR;
+               sb.st_mode |= S_IFLNK;
+               sb.st_nlink = sb1.st_nlink;
+               sb.st_size = sb1.st_size;
+               sb.st_blocks = sb1.st_blocks;
+       }
        cvtstat(&sb, &osb);
        error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb));
        return (error);
        cvtstat(&sb, &osb);
        error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb));
        return (error);
@@ -1271,7 +1457,7 @@ chflags(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1305,7 +1491,7 @@ fchflags(p, uap, retval)
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1340,7 +1526,7 @@ chmod(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1374,7 +1560,7 @@ fchmod(p, uap, retval)
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1406,11 +1592,11 @@ chown(p, uap, retval)
        int error;
        struct nameidata nd;
 
        int error;
        struct nameidata nd;
 
-       NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, p);
+       NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1446,7 +1632,7 @@ fchown(p, uap, retval)
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
        vp = (struct vnode *)fp->f_data;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1490,7 +1676,7 @@ utimes(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
        VOP_LOCK(vp);
        if (vp->v_mount->mnt_flag & MNT_RDONLY)
                error = EROFS;
@@ -1528,7 +1714,7 @@ truncate(p, uap, retval)
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
        if (error = namei(&nd))
                return (error);
        vp = nd.ni_vp;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_type == VDIR)
                error = EISDIR;
        VOP_LOCK(vp);
        if (vp->v_type == VDIR)
                error = EISDIR;
@@ -1566,7 +1752,7 @@ ftruncate(p, uap, retval)
        if ((fp->f_flag & FWRITE) == 0)
                return (EINVAL);
        vp = (struct vnode *)fp->f_data;
        if ((fp->f_flag & FWRITE) == 0)
                return (EINVAL);
        vp = (struct vnode *)fp->f_data;
-       LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
        VOP_LOCK(vp);
        if (vp->v_type == VDIR)
                error = EISDIR;
        VOP_LOCK(vp);
        if (vp->v_type == VDIR)
                error = EISDIR;
@@ -1703,11 +1889,11 @@ rename(p, uap, retval)
                error = -1;
 out:
        if (!error) {
                error = -1;
 out:
        if (!error) {
-               LEASE_CHECK(tdvp, p, p->p_ucred, LEASE_WRITE);
+               VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
                if (fromnd.ni_dvp != tdvp)
                if (fromnd.ni_dvp != tdvp)
-                       LEASE_CHECK(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+                       VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
                if (tvp)
                if (tvp)
-                       LEASE_CHECK(tvp, p, p->p_ucred, LEASE_WRITE);
+                       VOP_LEASE(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 {
                error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
                                   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
        } else {
@@ -1727,7 +1913,8 @@ out:
        FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
 out1:
        p->p_spare[1]--;
        FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
 out1:
        p->p_spare[1]--;
-       vrele(fromnd.ni_startdir);
+       if (fromnd.ni_startdir)
+               vrele(fromnd.ni_startdir);
        FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
        CHECKREFS("rename");
        if (error == -1)
        FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
        CHECKREFS("rename");
        if (error == -1)
@@ -1771,7 +1958,7 @@ mkdir(p, uap, retval)
        VATTR_NULL(&vattr);
        vattr.va_type = VDIR;
        vattr.va_mode = (uap->mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
        VATTR_NULL(&vattr);
        vattr.va_type = VDIR;
        vattr.va_mode = (uap->mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
-       LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+       VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
        error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
        if (!error)
                vput(nd.ni_vp);
        error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
        if (!error)
                vput(nd.ni_vp);
@@ -1818,8 +2005,8 @@ rmdir(p, uap, retval)
                error = EBUSY;
 out:
        if (!error) {
                error = EBUSY;
 out:
        if (!error) {
-               LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
-               LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
+               VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
+               VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
                error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
        } else {
                VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
                error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
        } else {
                VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
@@ -1854,7 +2041,7 @@ ogetdirentries(p, uap, retval)
        struct iovec aiov, kiov;
        struct dirent *dp, *edp;
        caddr_t dirbuf;
        struct iovec aiov, kiov;
        struct dirent *dp, *edp;
        caddr_t dirbuf;
-       int error, readcnt;
+       int error, eofflag, readcnt;
        long loff;
 
        if (error = getvnode(p->p_fd, uap->fd, &fp))
        long loff;
 
        if (error = getvnode(p->p_fd, uap->fd, &fp))
@@ -1862,6 +2049,7 @@ ogetdirentries(p, uap, retval)
        if ((fp->f_flag & FREAD) == 0)
                return (EBADF);
        vp = (struct vnode *)fp->f_data;
        if ((fp->f_flag & FREAD) == 0)
                return (EBADF);
        vp = (struct vnode *)fp->f_data;
+unionread:
        if (vp->v_type != VDIR)
                return (EINVAL);
        aiov.iov_base = uap->buf;
        if (vp->v_type != VDIR)
                return (EINVAL);
        aiov.iov_base = uap->buf;
@@ -1876,7 +2064,8 @@ ogetdirentries(p, uap, retval)
        loff = auio.uio_offset = fp->f_offset;
 #      if (BYTE_ORDER != LITTLE_ENDIAN)
                if (vp->v_mount->mnt_maxsymlinklen <= 0) {
        loff = auio.uio_offset = fp->f_offset;
 #      if (BYTE_ORDER != LITTLE_ENDIAN)
                if (vp->v_mount->mnt_maxsymlinklen <= 0) {
-                       error = VOP_READDIR(vp, &auio, fp->f_cred);
+                       error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
+                           (u_long *)0, 0);
                        fp->f_offset = auio.uio_offset;
                } else
 #      endif
                        fp->f_offset = auio.uio_offset;
                } else
 #      endif
@@ -1887,7 +2076,8 @@ ogetdirentries(p, uap, retval)
                kiov.iov_len = uap->count;
                MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
                kiov.iov_base = dirbuf;
                kiov.iov_len = uap->count;
                MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
                kiov.iov_base = dirbuf;
-               error = VOP_READDIR(vp, &kuio, fp->f_cred);
+               error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
+                           (u_long *)0, 0);
                fp->f_offset = kuio.uio_offset;
                if (error == 0) {
                        readcnt = uap->count - kuio.uio_resid;
                fp->f_offset = kuio.uio_offset;
                if (error == 0) {
                        readcnt = uap->count - kuio.uio_resid;
@@ -1926,11 +2116,67 @@ ogetdirentries(p, uap, retval)
        VOP_UNLOCK(vp);
        if (error)
                return (error);
        VOP_UNLOCK(vp);
        if (error)
                return (error);
+
+#ifdef UNION
+{
+       extern int (**union_vnodeop_p)();
+       extern struct vnode *union_dircache __P((struct vnode *));
+
+       if ((uap->count == auio.uio_resid) &&
+           (vp->v_op == union_vnodeop_p)) {
+               struct vnode *lvp;
+
+               lvp = union_dircache(vp);
+               if (lvp != NULLVP) {
+                       struct vattr va;
+
+                       /*
+                        * If the directory is opaque,
+                        * then don't show lower entries
+                        */
+                       error = VOP_GETATTR(vp, &va, fp->f_cred, p);
+                       if (va.va_flags & OPAQUE) {
+                               vput(lvp);
+                               lvp = NULL;
+                       }
+               }
+               
+               if (lvp != NULLVP) {
+                       error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
+                       VOP_UNLOCK(lvp);
+
+                       if (error) {
+                               vrele(lvp);
+                               return (error);
+                       }
+                       fp->f_data = (caddr_t) lvp;
+                       fp->f_offset = 0;
+                       error = vn_close(vp, FREAD, fp->f_cred, p);
+                       if (error)
+                               return (error);
+                       vp = lvp;
+                       goto unionread;
+               }
+       }
+}
+#endif /* UNION */
+
+       if ((uap->count == auio.uio_resid) &&
+           (vp->v_flag & VROOT) &&
+           (vp->v_mount->mnt_flag & MNT_UNION)) {
+               struct vnode *tvp = vp;
+               vp = vp->v_mount->mnt_vnodecovered;
+               VREF(vp);
+               fp->f_data = (caddr_t) vp;
+               fp->f_offset = 0;
+               vrele(tvp);
+               goto unionread;
+       }
        error = copyout((caddr_t)&loff, (caddr_t)uap->basep, sizeof(long));
        *retval = uap->count - auio.uio_resid;
        return (error);
 }
        error = copyout((caddr_t)&loff, (caddr_t)uap->basep, sizeof(long));
        *retval = uap->count - auio.uio_resid;
        return (error);
 }
-#endif
+#endif /* COMPAT_43 */
 
 /*
  * Read a block of directory entries in a file system independent format.
 
 /*
  * Read a block of directory entries in a file system independent format.
@@ -1951,7 +2197,7 @@ getdirentries(p, uap, retval)
        struct uio auio;
        struct iovec aiov;
        long loff;
        struct uio auio;
        struct iovec aiov;
        long loff;
-       int error;
+       int error, eofflag;
 
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
 
        if (error = getvnode(p->p_fd, uap->fd, &fp))
                return (error);
@@ -1971,11 +2217,56 @@ unionread:
        auio.uio_resid = uap->count;
        VOP_LOCK(vp);
        loff = auio.uio_offset = fp->f_offset;
        auio.uio_resid = uap->count;
        VOP_LOCK(vp);
        loff = auio.uio_offset = fp->f_offset;
-       error = VOP_READDIR(vp, &auio, fp->f_cred);
+       error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
        fp->f_offset = auio.uio_offset;
        VOP_UNLOCK(vp);
        if (error)
                return (error);
        fp->f_offset = auio.uio_offset;
        VOP_UNLOCK(vp);
        if (error)
                return (error);
+
+#ifdef UNION
+{
+       extern int (**union_vnodeop_p)();
+       extern struct vnode *union_dircache __P((struct vnode *));
+
+       if ((uap->count == auio.uio_resid) &&
+           (vp->v_op == union_vnodeop_p)) {
+               struct vnode *lvp;
+
+               lvp = union_dircache(vp);
+               if (lvp != NULLVP) {
+                       struct vattr va;
+
+                       /*
+                        * If the directory is opaque,
+                        * then don't show lower entries
+                        */
+                       error = VOP_GETATTR(vp, &va, fp->f_cred, p);
+                       if (va.va_flags & OPAQUE) {
+                               vput(lvp);
+                               lvp = NULL;
+                       }
+               }
+               
+               if (lvp != NULLVP) {
+                       error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
+                       VOP_UNLOCK(lvp);
+
+                       if (error) {
+                               vrele(lvp);
+                               return (error);
+                       }
+                       fp->f_data = (caddr_t) lvp;
+                       fp->f_offset = 0;
+                       error = vn_close(vp, FREAD, fp->f_cred, p);
+                       if (error)
+                               return (error);
+                       vp = lvp;
+                       goto unionread;
+               }
+       }
+}
+#endif
+
        if ((uap->count == auio.uio_resid) &&
            (vp->v_flag & VROOT) &&
            (vp->v_mount->mnt_flag & MNT_UNION)) {
        if ((uap->count == auio.uio_resid) &&
            (vp->v_flag & VROOT) &&
            (vp->v_mount->mnt_flag & MNT_UNION)) {