detect root node and set VROOT flag
[unix-history] / usr / src / sys / miscfs / union / union_subr.c
index e4bfebc..f0cd58e 100644 (file)
@@ -8,7 +8,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)union_subr.c        1.5 (Berkeley) %G%
+ *     @(#)union_subr.c        8.5 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
 #include <sys/namei.h>
 #include <sys/malloc.h>
 #include <sys/file.h>
 #include <sys/namei.h>
 #include <sys/malloc.h>
 #include <sys/file.h>
-#include "union.h" /*<miscfs/union/union.h>*/
+#include <sys/filedesc.h>
+#include <sys/queue.h>
+#include <sys/mount.h>
+#include <miscfs/union/union.h>
 
 #ifdef DIAGNOSTIC
 #include <sys/proc.h>
 #endif
 
 
 #ifdef DIAGNOSTIC
 #include <sys/proc.h>
 #endif
 
-static struct union_node *unhead;
-static int unvplock;
+/* must be power of two, otherwise change UNION_HASH() */
+#define NHASH 32
+
+/* unsigned int ... */
+#define UNION_HASH(u, l) \
+       (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
+
+static LIST_HEAD(unhead, union_node) unhead[NHASH];
+static int unvplock[NHASH];
 
 int
 union_init()
 {
 
 int
 union_init()
 {
+       int i;
+
+       for (i = 0; i < NHASH; i++)
+               LIST_INIT(&unhead[i]);
+       bzero((caddr_t) unvplock, sizeof(unvplock));
+}
+
+static int
+union_list_lock(ix)
+       int ix;
+{
+
+       if (unvplock[ix] & UN_LOCKED) {
+               unvplock[ix] |= UN_WANT;
+               sleep((caddr_t) &unvplock[ix], PINOD);
+               return (1);
+       }
+
+       unvplock[ix] |= UN_LOCKED;
+
+       return (0);
+}
+
+static void
+union_list_unlock(ix)
+       int ix;
+{
+
+       unvplock[ix] &= ~UN_LOCKED;
 
 
-       unhead = 0;
-       unvplock = 0;
+       if (unvplock[ix] & UN_WANT) {
+               unvplock[ix] &= ~UN_WANT;
+               wakeup((caddr_t) &unvplock[ix]);
+       }
+}
+
+void
+union_updatevp(un, uppervp, lowervp)
+       struct union_node *un;
+       struct vnode *uppervp;
+       struct vnode *lowervp;
+{
+       int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
+       int nhash = UNION_HASH(uppervp, lowervp);
+
+       if (ohash != nhash) {
+               /*
+                * Ensure locking is ordered from lower to higher
+                * to avoid deadlocks.
+                */
+               if (nhash < ohash) {
+                       int t = ohash;
+                       ohash = nhash;
+                       nhash = t;
+               }
+
+               while (union_list_lock(ohash))
+                       continue;
+
+               while (union_list_lock(nhash))
+                       continue;
+
+               LIST_REMOVE(un, un_cache);
+               union_list_unlock(ohash);
+       } else {        
+               while (union_list_lock(nhash))
+                       continue;
+       }
+
+       if (un->un_lowervp != lowervp) {
+               if (un->un_lowervp) {
+                       vrele(un->un_lowervp);
+                       if (un->un_path) {
+                               free(un->un_path, M_TEMP);
+                               un->un_path = 0;
+                       }
+                       if (un->un_dirvp) {
+                               vrele(un->un_dirvp);
+                               un->un_dirvp = NULLVP;
+                       }
+               }
+               un->un_lowervp = lowervp;
+       }
+
+       if (un->un_uppervp != uppervp) {
+               if (un->un_uppervp)
+                       vrele(un->un_uppervp);
+
+               un->un_uppervp = uppervp;
+       }
+
+       if (ohash != nhash)
+               LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
+
+       union_list_unlock(nhash);
+}
+
+void
+union_newlower(un, lowervp)
+       struct union_node *un;
+       struct vnode *lowervp;
+{
+
+       union_updatevp(un, un->un_uppervp, lowervp);
+}
+
+void
+union_newupper(un, uppervp)
+       struct union_node *un;
+       struct vnode *uppervp;
+{
+
+       union_updatevp(un, uppervp, un->un_lowervp);
 }
 
 /*
 }
 
 /*
@@ -46,6 +166,9 @@ union_init()
  * layer object to be created at a later time.  (uppervp)
  * and (lowervp) reference the upper and lower layer objects
  * being mapped.  either, but not both, can be nil.
  * layer object to be created at a later time.  (uppervp)
  * and (lowervp) reference the upper and lower layer objects
  * being mapped.  either, but not both, can be nil.
+ * if supplied, (uppervp) is locked.
+ * the reference is either maintained in the new union_node
+ * object which is allocated, or they are vrele'd.
  *
  * all union_nodes are maintained on a singly-linked
  * list.  new nodes are only allocated when they cannot
  *
  * all union_nodes are maintained on a singly-linked
  * list.  new nodes are only allocated when they cannot
@@ -77,102 +200,229 @@ union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
        int error;
        struct union_node *un;
        struct union_node **pp;
        int error;
        struct union_node *un;
        struct union_node **pp;
-       struct vnode *xlowervp = 0;
+       struct vnode *xlowervp = NULLVP;
+       struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
+       int hash;
+       int vflag;
+       int try;
 
 
-       if (uppervp == 0 && lowervp == 0)
+       if (uppervp == NULLVP && lowervp == NULLVP)
                panic("union: unidentifiable allocation");
 
        if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
                xlowervp = lowervp;
                panic("union: unidentifiable allocation");
 
        if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
                xlowervp = lowervp;
-               lowervp = 0;
+               lowervp = NULLVP;
+       }
+
+       /* detect the root vnode (and aliases) */
+       vflag = 0;
+       if ((uppervp == um->um_uppervp) &&
+           ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
+               if (lowervp == NULLVP) {
+                       lowervp = um->um_lowervp;
+                       VREF(lowervp);
+               }
+               vflag = VROOT;
        }
 
 loop:
        }
 
 loop:
-       for (un = unhead; un != 0; un = un->un_next) {
-               if ((un->un_lowervp == lowervp ||
-                    un->un_lowervp == 0) &&
-                   (un->un_uppervp == uppervp ||
-                    un->un_uppervp == 0) &&
-                   (UNIONTOV(un)->v_mount == mp)) {
-                       if (vget(UNIONTOV(un), 0))
+       for (try = 0; try < 3; try++) {
+               switch (try) {
+               case 0:
+                       if (lowervp == NULLVP)
+                               continue;
+                       hash = UNION_HASH(uppervp, lowervp);
+                       break;
+
+               case 1:
+                       if (uppervp == NULLVP)
+                               continue;
+                       hash = UNION_HASH(uppervp, NULLVP);
+                       break;
+
+               case 2:
+                       if (lowervp == NULLVP)
+                               continue;
+                       hash = UNION_HASH(NULLVP, lowervp);
+                       break;
+               }
+
+               while (union_list_lock(hash))
+                       continue;
+
+               for (un = unhead[hash].lh_first; un != 0;
+                                       un = un->un_cache.le_next) {
+                       if ((un->un_lowervp == lowervp ||
+                            un->un_lowervp == NULLVP) &&
+                           (un->un_uppervp == uppervp ||
+                            un->un_uppervp == NULLVP) &&
+                           (UNIONTOV(un)->v_mount == mp)) {
+                               if (vget(UNIONTOV(un), 0)) {
+                                       union_list_unlock(hash);
+                                       goto loop;
+                               }
+                               break;
+                       }
+               }
+
+               union_list_unlock(hash);
+
+               if (un)
+                       break;
+       }
+
+       if (un) {
+               /*
+                * Obtain a lock on the union_node.
+                * uppervp is locked, though un->un_uppervp
+                * may not be.  this doesn't break the locking
+                * hierarchy since in the case that un->un_uppervp
+                * is not yet locked it will be vrele'd and replaced
+                * with uppervp.
+                */
+
+               if ((dvp != NULLVP) && (uppervp == dvp)) {
+                       /*
+                        * Access ``.'', so (un) will already
+                        * be locked.  Since this process has
+                        * the lock on (uppervp) no other
+                        * process can hold the lock on (un).
+                        */
+#ifdef DIAGNOSTIC
+                       if ((un->un_flags & UN_LOCKED) == 0)
+                               panic("union: . not locked");
+                       else if (curproc && un->un_pid != curproc->p_pid &&
+                                   un->un_pid > -1 && curproc->p_pid > -1)
+                               panic("union: allocvp not lock owner");
+#endif
+               } else {
+                       if (un->un_flags & UN_LOCKED) {
+                               vrele(UNIONTOV(un));
+                               un->un_flags |= UN_WANT;
+                               sleep((caddr_t) &un->un_flags, PINOD);
                                goto loop;
                                goto loop;
-                       if (UNIONTOV(un) != undvp)
-                               VOP_LOCK(UNIONTOV(un));
-                       if (uppervp != un->un_uppervp) {
-                               if (un->un_uppervp)
-                                       vrele(un->un_uppervp);
-                               un->un_uppervp = uppervp;
                        }
                        }
-                       if (lowervp != un->un_lowervp) {
-                               if (un->un_lowervp)
-                                       vrele(un->un_lowervp);
-                               un->un_lowervp = lowervp;
+                       un->un_flags |= UN_LOCKED;
+
+#ifdef DIAGNOSTIC
+                       if (curproc)
+                               un->un_pid = curproc->p_pid;
+                       else
+                               un->un_pid = -1;
+#endif
+               }
+
+               /*
+                * At this point, the union_node is locked,
+                * un->un_uppervp may not be locked, and uppervp
+                * is locked or nil.
+                */
+
+               /*
+                * Save information about the upper layer.
+                */
+               if (uppervp != un->un_uppervp) {
+                       union_newupper(un, uppervp);
+               } else if (uppervp) {
+                       vrele(uppervp);
+               }
+
+               if (un->un_uppervp) {
+                       un->un_flags |= UN_ULOCK;
+                       un->un_flags &= ~UN_KLOCK;
+               }
+
+               /*
+                * Save information about the lower layer.
+                * This needs to keep track of pathname
+                * and directory information which union_vn_create
+                * might need.
+                */
+               if (lowervp != un->un_lowervp) {
+                       union_newlower(un, lowervp);
+                       if (cnp && (lowervp != NULLVP) &&
+                           (lowervp->v_type == VREG)) {
+                               un->un_hash = cnp->cn_hash;
+                               un->un_path = malloc(cnp->cn_namelen+1,
+                                               M_TEMP, M_WAITOK);
+                               bcopy(cnp->cn_nameptr, un->un_path,
+                                               cnp->cn_namelen);
+                               un->un_path[cnp->cn_namelen] = '\0';
+                               VREF(dvp);
+                               un->un_dirvp = dvp;
                        }
                        }
-                       *vpp = UNIONTOV(un);
-                       return (0);
+               } else if (lowervp) {
+                       vrele(lowervp);
                }
                }
+               *vpp = UNIONTOV(un);
+               return (0);
        }
 
        /*
         * otherwise lock the vp list while we call getnewvnode
         * since that can block.
         */ 
        }
 
        /*
         * otherwise lock the vp list while we call getnewvnode
         * since that can block.
         */ 
-       if (unvplock & UN_LOCKED) {
-               unvplock |= UN_WANT;
-               sleep((caddr_t) &unvplock, PINOD);
+       hash = UNION_HASH(uppervp, lowervp);
+
+       if (union_list_lock(hash))
                goto loop;
                goto loop;
-       }
-       unvplock |= UN_LOCKED;
 
        error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
 
        error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
-       if (error)
+       if (error) {
+               if (uppervp) {
+                       if (dvp == uppervp)
+                               vrele(uppervp);
+                       else
+                               vput(uppervp);
+               }
+               if (lowervp)
+                       vrele(lowervp);
+
                goto out;
                goto out;
+       }
 
        MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
                M_TEMP, M_WAITOK);
 
 
        MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
                M_TEMP, M_WAITOK);
 
+       (*vpp)->v_flag |= vflag;
        if (uppervp)
                (*vpp)->v_type = uppervp->v_type;
        else
                (*vpp)->v_type = lowervp->v_type;
        un = VTOUNION(*vpp);
        un->un_vnode = *vpp;
        if (uppervp)
                (*vpp)->v_type = uppervp->v_type;
        else
                (*vpp)->v_type = lowervp->v_type;
        un = VTOUNION(*vpp);
        un->un_vnode = *vpp;
-       un->un_next = 0;
        un->un_uppervp = uppervp;
        un->un_lowervp = lowervp;
        un->un_uppervp = uppervp;
        un->un_lowervp = lowervp;
-       un->un_flags = 0;
-       if (uppervp == 0 && cnp) {
+       un->un_openl = 0;
+       un->un_flags = UN_LOCKED;
+       if (un->un_uppervp)
+               un->un_flags |= UN_ULOCK;
+#ifdef DIAGNOSTIC
+       if (curproc)
+               un->un_pid = curproc->p_pid;
+       else
+               un->un_pid = -1;
+#endif
+       if (cnp && (lowervp != NULLVP) && (lowervp->v_type == VREG)) {
+               un->un_hash = cnp->cn_hash;
                un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
                bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
                un->un_path[cnp->cn_namelen] = '\0';
                VREF(dvp);
                un->un_dirvp = dvp;
        } else {
                un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
                bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
                un->un_path[cnp->cn_namelen] = '\0';
                VREF(dvp);
                un->un_dirvp = dvp;
        } else {
+               un->un_hash = 0;
                un->un_path = 0;
                un->un_dirvp = 0;
        }
 
                un->un_path = 0;
                un->un_dirvp = 0;
        }
 
-       /* add to union vnode list */
-       for (pp = &unhead; *pp; pp = &(*pp)->un_next)
-               continue;
-       *pp = un;
-
-       un->un_flags |= UN_LOCKED;
-
-#ifdef DIAGNOSTIC
-       un->un_pid = curproc->p_pid;
-#endif
+       LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
 
        if (xlowervp)
                vrele(xlowervp);
 
 out:
 
        if (xlowervp)
                vrele(xlowervp);
 
 out:
-       unvplock &= ~UN_LOCKED;
-
-       if (unvplock & UN_WANT) {
-               unvplock &= ~UN_WANT;
-               wakeup((caddr_t) &unvplock);
-       }
+       union_list_unlock(hash);
 
        return (error);
 }
 
        return (error);
 }
@@ -181,21 +431,22 @@ int
 union_freevp(vp)
        struct vnode *vp;
 {
 union_freevp(vp)
        struct vnode *vp;
 {
-       struct union_node **unpp;
        struct union_node *un = VTOUNION(vp);
 
        struct union_node *un = VTOUNION(vp);
 
-       for (unpp = &unhead; *unpp != 0; unpp = &(*unpp)->un_next) {
-               if (*unpp == un) {
-                       *unpp = un->un_next;
-                       break;
-               }
-       }
+       LIST_REMOVE(un, un_cache);
 
 
+       if (un->un_uppervp)
+               vrele(un->un_uppervp);
+       if (un->un_lowervp)
+               vrele(un->un_lowervp);
+       if (un->un_dirvp)
+               vrele(un->un_dirvp);
        if (un->un_path)
        if (un->un_path)
-               FREE(un->un_path, M_TEMP);
+               free(un->un_path, M_TEMP);
 
        FREE(vp->v_data, M_TEMP);
        vp->v_data = 0;
 
        FREE(vp->v_data, M_TEMP);
        vp->v_data = 0;
+
        return (0);
 }
 
        return (0);
 }
 
@@ -272,6 +523,89 @@ union_copyfile(p, cred, fvp, tvp)
        return (error);
 }
 
        return (error);
 }
 
+/*
+ * Create a shadow directory in the upper layer.
+ * The new vnode is returned locked.
+ *
+ * (um) points to the union mount structure for access to the
+ * the mounting process's credentials.
+ * (dvp) is the directory in which to create the shadow directory.
+ * it is unlocked on entry and exit.
+ * (cnp) is the componentname to be created.
+ * (vpp) is the returned newly created shadow directory, which
+ * is returned locked.
+ */
+int
+union_mkshadow(um, dvp, cnp, vpp)
+       struct union_mount *um;
+       struct vnode *dvp;
+       struct componentname *cnp;
+       struct vnode **vpp;
+{
+       int error;
+       struct vattr va;
+       struct proc *p = cnp->cn_proc;
+       struct componentname cn;
+
+       /*
+        * policy: when creating the shadow directory in the
+        * upper layer, create it owned by the user who did
+        * the mount, group from parent directory, and mode
+        * 777 modified by umask (ie mostly identical to the
+        * mkdir syscall).  (jsp, kb)
+        */
+
+       /*
+        * A new componentname structure must be faked up because
+        * there is no way to know where the upper level cnp came
+        * from or what it is being used for.  This must duplicate
+        * some of the work done by NDINIT, some of the work done
+        * by namei, some of the work done by lookup and some of
+        * the work done by VOP_LOOKUP when given a CREATE flag.
+        * Conclusion: Horrible.
+        *
+        * The pathname buffer will be FREEed by VOP_MKDIR.
+        */
+       cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK);
+       bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen);
+       cn.cn_pnbuf[cnp->cn_namelen] = '\0';
+
+       cn.cn_nameiop = CREATE;
+       cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
+       cn.cn_proc = cnp->cn_proc;
+       if (um->um_op == UNMNT_ABOVE)
+               cn.cn_cred = cnp->cn_cred;
+       else
+               cn.cn_cred = um->um_cred;
+       cn.cn_nameptr = cn.cn_pnbuf;
+       cn.cn_namelen = cnp->cn_namelen;
+       cn.cn_hash = cnp->cn_hash;
+       cn.cn_consume = cnp->cn_consume;
+
+       VREF(dvp);
+       if (error = relookup(dvp, vpp, &cn))
+               return (error);
+       vrele(dvp);
+
+       if (*vpp) {
+               VOP_ABORTOP(dvp, &cn);
+               VOP_UNLOCK(dvp);
+               vrele(*vpp);
+               *vpp = NULLVP;
+               return (EEXIST);
+       }
+
+       VATTR_NULL(&va);
+       va.va_type = VDIR;
+       va.va_mode = um->um_cmode;
+
+       /* LEASE_CHECK: dvp is locked */
+       LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
+
+       error = VOP_MKDIR(dvp, vpp, &cn, &va);
+       return (error);
+}
+
 /*
  * union_vn_create: creates and opens a new shadow file
  * on the upper union layer.  this function is similar
 /*
  * union_vn_create: creates and opens a new shadow file
  * on the upper union layer.  this function is similar
@@ -281,10 +615,9 @@ union_copyfile(p, cred, fvp, tvp)
  * whereas relookup is told where to start.
  */
 int
  * whereas relookup is told where to start.
  */
 int
-union_vn_create(vpp, un, cmode, p)
+union_vn_create(vpp, un, p)
        struct vnode **vpp;
        struct union_node *un;
        struct vnode **vpp;
        struct union_node *un;
-       int cmode;
        struct proc *p;
 {
        struct vnode *vp;
        struct proc *p;
 {
        struct vnode *vp;
@@ -293,65 +626,108 @@ union_vn_create(vpp, un, cmode, p)
        struct vattr *vap = &vat;
        int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
        int error;
        struct vattr *vap = &vat;
        int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
        int error;
-       int hash;
+       int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
        char *cp;
        struct componentname cn;
 
        *vpp = NULLVP;
 
        char *cp;
        struct componentname cn;
 
        *vpp = NULLVP;
 
+       /*
+        * Build a new componentname structure (for the same
+        * reasons outlines in union_mkshadow).
+        * The difference here is that the file is owned by
+        * the current user, rather than by the person who
+        * did the mount, since the current user needs to be
+        * able to write the file (that's why it is being
+        * copied in the first place).
+        */
        cn.cn_namelen = strlen(un->un_path);
        cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
        bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
        cn.cn_nameiop = CREATE;
        cn.cn_namelen = strlen(un->un_path);
        cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
        bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
        cn.cn_nameiop = CREATE;
-       cn.cn_flags = (LOCKLEAF|LOCKPARENT|HASBUF|SAVENAME|ISLASTCN);
+       cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
        cn.cn_proc = p;
        cn.cn_cred = p->p_ucred;
        cn.cn_nameptr = cn.cn_pnbuf;
        cn.cn_proc = p;
        cn.cn_cred = p->p_ucred;
        cn.cn_nameptr = cn.cn_pnbuf;
-       for (hash = 0, cp = cn.cn_nameptr; *cp != 0 && *cp != '/'; cp++)
-               hash += (unsigned char)*cp;
-       cn.cn_hash = hash;
+       cn.cn_hash = un->un_hash;
        cn.cn_consume = 0;
 
        cn.cn_consume = 0;
 
+       VREF(un->un_dirvp);
        if (error = relookup(un->un_dirvp, &vp, &cn))
                return (error);
        if (error = relookup(un->un_dirvp, &vp, &cn))
                return (error);
-       if (vp == NULLVP) {
-               VATTR_NULL(vap);
-               vap->va_type = VREG;
-               vap->va_mode = cmode;
-               LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE);
-               if (error = VOP_CREATE(un->un_dirvp, &vp,
-                   &cn, vap))
-                       return (error);
-       } else {
+       vrele(un->un_dirvp);
+
+       if (vp) {
                VOP_ABORTOP(un->un_dirvp, &cn);
                if (un->un_dirvp == vp)
                        vrele(un->un_dirvp);
                else
                VOP_ABORTOP(un->un_dirvp, &cn);
                if (un->un_dirvp == vp)
                        vrele(un->un_dirvp);
                else
-                       vput(vp);
-               error = EEXIST;
-               goto bad;
+                       vput(un->un_dirvp);
+               vrele(vp);
+               return (EEXIST);
        }
 
        }
 
-       if (vp->v_type != VREG) {
-               error = EOPNOTSUPP;
-               goto bad;
-       }
-
-       VOP_UNLOCK(vp);                         /* XXX */
-       LEASE_CHECK(vp, p, cred, LEASE_WRITE);
-       VOP_LOCK(vp);                           /* XXX */
+       /*
+        * Good - there was no race to create the file
+        * so go ahead and create it.  The permissions
+        * on the file will be 0666 modified by the
+        * current user's umask.  Access to the file, while
+        * it is unioned, will require access to the top *and*
+        * bottom files.  Access when not unioned will simply
+        * require access to the top-level file.
+        * TODO: confirm choice of access permissions.
+        */
        VATTR_NULL(vap);
        VATTR_NULL(vap);
-       vap->va_size = 0;
-       if (error = VOP_SETATTR(vp, vap, cred, p))
-               goto bad;
+       vap->va_type = VREG;
+       vap->va_mode = cmode;
+       LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE);
+       if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap))
+               return (error);
 
 
-       if (error = VOP_OPEN(vp, fmode, cred, p))
-               goto bad;
+       if (error = VOP_OPEN(vp, fmode, cred, p)) {
+               vput(vp);
+               return (error);
+       }
 
        vp->v_writecount++;
        *vpp = vp;
        return (0);
 
        vp->v_writecount++;
        *vpp = vp;
        return (0);
-bad:
-       vput(vp);
-       return (error);
+}
+
+int
+union_vn_close(vp, fmode, cred, p)
+       struct vnode *vp;
+       int fmode;
+       struct ucred *cred;
+       struct proc *p;
+{
+       if (fmode & FWRITE)
+               --vp->v_writecount;
+       return (VOP_CLOSE(vp, fmode));
+}
+
+void
+union_removed_upper(un)
+       struct union_node *un;
+{
+       if (un->un_flags & UN_ULOCK) {
+               un->un_flags &= ~UN_ULOCK;
+               VOP_UNLOCK(un->un_uppervp);
+       }
+
+       union_newupper(un, NULLVP);
+}
+
+struct vnode *
+union_lowervp(vp)
+       struct vnode *vp;
+{
+       struct union_node *un = VTOUNION(vp);
+
+       if (un->un_lowervp && (vp->v_type == un->un_lowervp->v_type)) {
+               if (vget(un->un_lowervp, 0))
+                       return (NULLVP);
+       }
+
+       return (un->un_lowervp);
 }
 }