detect root node and set VROOT flag
[unix-history] / usr / src / sys / miscfs / union / union_subr.c
index 3319a34..f0cd58e 100644 (file)
@@ -8,7 +8,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)union_subr.c        2.1 (Berkeley) %G%
+ *     @(#)union_subr.c        8.5 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
 #include <sys/malloc.h>
 #include <sys/file.h>
 #include <sys/filedesc.h>
 #include <sys/malloc.h>
 #include <sys/file.h>
 #include <sys/filedesc.h>
-#include "union.h" /*<miscfs/union/union.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;
 {
 
 {
 
-       unhead = 0;
-       unvplock = 0;
+       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
 }
 
 static void
-union_remlist(un)
+union_list_unlock(ix)
+       int ix;
+{
+
+       unvplock[ix] &= ~UN_LOCKED;
+
+       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 union_node *un;
+       struct vnode *uppervp;
+       struct vnode *lowervp;
 {
 {
-       struct union_node **unpp;
+       int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
+       int nhash = UNION_HASH(uppervp, lowervp);
 
 
-       for (unpp = &unhead; *unpp != 0; unpp = &(*unpp)->un_next) {
-               if (*unpp == un) {
-                       *unpp = un->un_next;
-                       break;
+       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);
 }
 
 /*
 }
 
 /*
@@ -95,27 +200,75 @@ 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))
-                               goto loop;
+       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;
                }
                        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) {
        }
 
        if (un) {
@@ -169,9 +322,7 @@ loop:
                 * Save information about the upper layer.
                 */
                if (uppervp != un->un_uppervp) {
                 * Save information about the upper layer.
                 */
                if (uppervp != un->un_uppervp) {
-                       if (un->un_uppervp)
-                               vrele(un->un_uppervp);
-                       un->un_uppervp = uppervp;
+                       union_newupper(un, uppervp);
                } else if (uppervp) {
                        vrele(uppervp);
                }
                } else if (uppervp) {
                        vrele(uppervp);
                }
@@ -188,12 +339,7 @@ loop:
                 * might need.
                 */
                if (lowervp != un->un_lowervp) {
                 * might need.
                 */
                if (lowervp != un->un_lowervp) {
-                       if (un->un_lowervp) {
-                               vrele(un->un_lowervp);
-                               free(un->un_path, M_TEMP);
-                               vrele(un->un_dirvp);
-                       }
-                       un->un_lowervp = lowervp;
+                       union_newlower(un, lowervp);
                        if (cnp && (lowervp != NULLVP) &&
                            (lowervp->v_type == VREG)) {
                                un->un_hash = cnp->cn_hash;
                        if (cnp && (lowervp != NULLVP) &&
                            (lowervp->v_type == VREG)) {
                                un->un_hash = cnp->cn_hash;
@@ -216,12 +362,10 @@ loop:
         * 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);
        if (error) {
 
        error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
        if (error) {
@@ -240,13 +384,13 @@ 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
                (*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_openl = 0;
        un->un_uppervp = uppervp;
        un->un_lowervp = lowervp;
        un->un_openl = 0;
@@ -272,21 +416,13 @@ loop:
                un->un_dirvp = 0;
        }
 
                un->un_dirvp = 0;
        }
 
-       /* add to union vnode list */
-       for (pp = &unhead; *pp; pp = &(*pp)->un_next)
-               continue;
-       *pp = un;
+       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);
 }
@@ -297,10 +433,20 @@ union_freevp(vp)
 {
        struct union_node *un = VTOUNION(vp);
 
 {
        struct union_node *un = VTOUNION(vp);
 
-       union_remlist(un);
+       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)
+               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);
 }
 
@@ -425,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;
@@ -453,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);
 }
@@ -497,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;
@@ -509,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
@@ -579,11 +712,10 @@ union_removed_upper(un)
 {
        if (un->un_flags & UN_ULOCK) {
                un->un_flags &= ~UN_ULOCK;
 {
        if (un->un_flags & UN_ULOCK) {
                un->un_flags &= ~UN_ULOCK;
-               vput(un->un_uppervp);
-       } else {
-               vrele(un->un_uppervp);
+               VOP_UNLOCK(un->un_uppervp);
        }
        }
-       un->un_uppervp = NULLVP;
+
+       union_newupper(un, NULLVP);
 }
 
 struct vnode *
 }
 
 struct vnode *