don't include "root" in $=L by default
[unix-history] / usr / src / sys / miscfs / union / union_subr.c
index e4bfebc..5e13601 100644 (file)
@@ -8,7 +8,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)union_subr.c        1.5 (Berkeley) %G%
+ *     @(#)union_subr.c        1.9 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
@@ -19,6 +19,7 @@
 #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 <sys/filedesc.h>
 #include "union.h" /*<miscfs/union/union.h>*/
 
 #ifdef DIAGNOSTIC
 #include "union.h" /*<miscfs/union/union.h>*/
 
 #ifdef DIAGNOSTIC
@@ -46,6 +47,8 @@ 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.
+ * 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
@@ -98,15 +101,44 @@ loop:
                                goto loop;
                        if (UNIONTOV(un) != undvp)
                                VOP_LOCK(UNIONTOV(un));
                                goto loop;
                        if (UNIONTOV(un) != undvp)
                                VOP_LOCK(UNIONTOV(un));
+
+                       /*
+                        * Save information about the upper layer.
+                        */
                        if (uppervp != un->un_uppervp) {
                                if (un->un_uppervp)
                                        vrele(un->un_uppervp);
                                un->un_uppervp = uppervp;
                        if (uppervp != un->un_uppervp) {
                                if (un->un_uppervp)
                                        vrele(un->un_uppervp);
                                un->un_uppervp = uppervp;
+                       } else if (uppervp) {
+                               vrele(uppervp);
                        }
                        }
+
+                       /*
+                        * 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) {
                        if (lowervp != un->un_lowervp) {
-                               if (un->un_lowervp)
+                               if (un->un_lowervp) {
                                        vrele(un->un_lowervp);
                                        vrele(un->un_lowervp);
+                                       free(un->un_path, M_TEMP);
+                                       vrele(un->un_dirvp);
+                               }
                                un->un_lowervp = lowervp;
                                un->un_lowervp = 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;
+                               }
+                       } else if (lowervp) {
+                               vrele(lowervp);
                        }
                        *vpp = UNIONTOV(un);
                        return (0);
                        }
                        *vpp = UNIONTOV(un);
                        return (0);
@@ -140,14 +172,17 @@ loop:
        un->un_next = 0;
        un->un_uppervp = uppervp;
        un->un_lowervp = lowervp;
        un->un_next = 0;
        un->un_uppervp = uppervp;
        un->un_lowervp = lowervp;
+       un->un_openl = 0;
        un->un_flags = 0;
        un->un_flags = 0;
-       if (uppervp == 0 && cnp) {
+       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;
        }
@@ -191,9 +226,6 @@ union_freevp(vp)
                }
        }
 
                }
        }
 
-       if (un->un_path)
-               FREE(un->un_path, M_TEMP);
-
        FREE(vp->v_data, M_TEMP);
        vp->v_data = 0;
        return (0);
        FREE(vp->v_data, M_TEMP);
        vp->v_data = 0;
        return (0);
@@ -272,6 +304,87 @@ 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|ISLASTCN);
+       cn.cn_proc = cnp->cn_proc;
+       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);
+
+       VREF(dvp);
+       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 +394,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,12 +405,21 @@ 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_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);
@@ -307,14 +428,25 @@ union_vn_create(vpp, un, cmode, p)
        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);
+       vrele(un->un_dirvp);
+
        if (vp == NULLVP) {
        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;
                VATTR_NULL(vap);
                vap->va_type = VREG;
                vap->va_mode = cmode;
@@ -355,3 +487,37 @@ bad:
        vput(vp);
        return (error);
 }
        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;
+{
+       vrele(un->un_uppervp);
+       un->un_uppervp = 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);
+}