detect root node and set VROOT flag
[unix-history] / usr / src / sys / miscfs / union / union_subr.c
index fb06868..f0cd58e 100644 (file)
@@ -8,7 +8,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)union_subr.c        8.2 (Berkeley) %G%
+ *     @(#)union_subr.c        8.5 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
@@ -21,6 +21,7 @@
 #include <sys/file.h>
 #include <sys/filedesc.h>
 #include <sys/queue.h>
 #include <sys/file.h>
 #include <sys/filedesc.h>
 #include <sys/queue.h>
+#include <sys/mount.h>
 #include <miscfs/union/union.h>
 
 #ifdef DIAGNOSTIC
 #include <miscfs/union/union.h>
 
 #ifdef DIAGNOSTIC
@@ -200,7 +201,9 @@ union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
        struct union_node *un;
        struct union_node **pp;
        struct vnode *xlowervp = NULLVP;
        struct union_node *un;
        struct union_node **pp;
        struct vnode *xlowervp = NULLVP;
+       struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
        int hash;
        int hash;
+       int vflag;
        int try;
 
        if (uppervp == NULLVP && lowervp == NULLVP)
        int try;
 
        if (uppervp == NULLVP && lowervp == NULLVP)
@@ -211,6 +214,17 @@ union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
                lowervp = NULLVP;
        }
 
                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:
        for (try = 0; try < 3; try++) {
                switch (try) {
 loop:
        for (try = 0; try < 3; try++) {
                switch (try) {
@@ -370,6 +384,7 @@ loop:
        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
        if (uppervp)
                (*vpp)->v_type = uppervp->v_type;
        else
@@ -556,9 +571,12 @@ union_mkshadow(um, dvp, cnp, vpp)
        cn.cn_pnbuf[cnp->cn_namelen] = '\0';
 
        cn.cn_nameiop = CREATE;
        cn.cn_pnbuf[cnp->cn_namelen] = '\0';
 
        cn.cn_nameiop = CREATE;
-       cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|ISLASTCN);
+       cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
        cn.cn_proc = cnp->cn_proc;
        cn.cn_proc = cnp->cn_proc;
-       cn.cn_cred = um->um_cred;
+       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_nameptr = cn.cn_pnbuf;
        cn.cn_namelen = cnp->cn_namelen;
        cn.cn_hash = cnp->cn_hash;
@@ -584,7 +602,6 @@ union_mkshadow(um, dvp, cnp, vpp)
        /* LEASE_CHECK: dvp is locked */
        LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
 
        /* LEASE_CHECK: dvp is locked */
        LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
 
-       VREF(dvp);
        error = VOP_MKDIR(dvp, vpp, &cn, &va);
        return (error);
 }
        error = VOP_MKDIR(dvp, vpp, &cn, &va);
        return (error);
 }
@@ -628,7 +645,7 @@ union_vn_create(vpp, un, p)
        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_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;
@@ -640,56 +657,41 @@ union_vn_create(vpp, un, p)
                return (error);
        vrele(un->un_dirvp);
 
                return (error);
        vrele(un->un_dirvp);
 
-       if (vp == NULLVP) {
-               /*
-                * 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);
-               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 {
+       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;
-       }
-
-       if (vp->v_type != VREG) {
-               error = EOPNOTSUPP;
-               goto bad;
+                       vput(un->un_dirvp);
+               vrele(vp);
+               return (EEXIST);
        }
 
        }
 
-       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
 }
 
 int