*
* %sccs.include.redist.c%
*
- * @(#)union_subr.c 1.9 (Berkeley) %G%
+ * @(#)union_subr.c 2.1 (Berkeley) %G%
*/
#include <sys/param.h>
unvplock = 0;
}
+static void
+union_remlist(un)
+ struct union_node *un;
+{
+ struct union_node **unpp;
+
+ for (unpp = &unhead; *unpp != 0; unpp = &(*unpp)->un_next) {
+ if (*unpp == un) {
+ *unpp = un->un_next;
+ break;
+ }
+ }
+}
+
/*
* allocate a union_node/vnode pair. the vnode is
* referenced and locked. the new vnode is returned
* 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.
*
(UNIONTOV(un)->v_mount == mp)) {
if (vget(UNIONTOV(un), 0))
goto loop;
- if (UNIONTOV(un) != undvp)
- VOP_LOCK(UNIONTOV(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)) {
/*
- * Save information about the upper layer.
+ * Access ``.'', so (un) will already
+ * be locked. Since this process has
+ * the lock on (uppervp) no other
+ * process can hold the lock on (un).
*/
- if (uppervp != un->un_uppervp) {
- if (un->un_uppervp)
- vrele(un->un_uppervp);
- un->un_uppervp = uppervp;
- } else if (uppervp) {
- vrele(uppervp);
+#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;
}
+ un->un_flags |= UN_LOCKED;
- /*
- * 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 (un->un_lowervp) {
- vrele(un->un_lowervp);
- free(un->un_path, M_TEMP);
- vrele(un->un_dirvp);
- }
- 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);
+#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) {
+ if (un->un_uppervp)
+ vrele(un->un_uppervp);
+ un->un_uppervp = 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) {
+ if (un->un_lowervp) {
+ vrele(un->un_lowervp);
+ free(un->un_path, M_TEMP);
+ vrele(un->un_dirvp);
+ }
+ 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;
}
- *vpp = UNIONTOV(un);
- return (0);
+ } else if (lowervp) {
+ vrele(lowervp);
}
+ *vpp = UNIONTOV(un);
+ return (0);
}
/*
unvplock |= UN_LOCKED;
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;
+ }
MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
M_TEMP, M_WAITOK);
un->un_uppervp = uppervp;
un->un_lowervp = lowervp;
un->un_openl = 0;
- un->un_flags = 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);
continue;
*pp = un;
- un->un_flags |= UN_LOCKED;
-
-#ifdef DIAGNOSTIC
- un->un_pid = curproc->p_pid;
-#endif
-
if (xlowervp)
vrele(xlowervp);
union_freevp(vp)
struct vnode *vp;
{
- struct union_node **unpp;
struct union_node *un = VTOUNION(vp);
- for (unpp = &unhead; *unpp != 0; unpp = &(*unpp)->un_next) {
- if (*unpp == un) {
- *unpp = un->un_next;
- break;
- }
- }
+ union_remlist(un);
FREE(vp->v_data, M_TEMP);
vp->v_data = 0;
union_removed_upper(un)
struct union_node *un;
{
- vrele(un->un_uppervp);
+ if (un->un_flags & UN_ULOCK) {
+ un->un_flags &= ~UN_ULOCK;
+ vput(un->un_uppervp);
+ } else {
+ vrele(un->un_uppervp);
+ }
un->un_uppervp = NULLVP;
}
*
* %sccs.include.redist.c%
*
- * @(#)union_vnops.c 1.9 (Berkeley) %G%
+ * @(#)union_vnops.c 2.1 (Berkeley) %G%
*/
#include <sys/param.h>
* on and just return that vnode.
*/
if (upperdvp) {
- VOP_LOCK(upperdvp);
uerror = union_lookup1(um->um_uppervp, upperdvp,
&uppervp, cnp);
- if (uppervp != upperdvp)
- VOP_UNLOCK(upperdvp);
+ /*if (uppervp == upperdvp)
+ dun->un_flags |= UN_KLOCK;*/
if (cnp->cn_consume != 0) {
*ap->a_vpp = uppervp;
* instead.
*/
if (lowerdvp) {
+ int nameiop;
+
VOP_LOCK(lowerdvp);
+
+ /*
+ * Only do a LOOKUP on the bottom node, since
+ * we won't be making changes to it anyway.
+ */
+ nameiop = cnp->cn_nameiop;
+ cnp->cn_nameiop = LOOKUP;
lerror = union_lookup1(um->um_lowervp, lowerdvp,
- &lowervp, cnp);
+ &lowervp, cnp);
+ cnp->cn_nameiop = nameiop;
+
if (lowervp != lowerdvp)
VOP_UNLOCK(lowerdvp);
if (cnp->cn_consume != 0) {
if (uppervp) {
- vput(uppervp);
+ if (uppervp == upperdvp)
+ vrele(uppervp);
+ else
+ vput(uppervp);
uppervp = NULLVP;
}
*ap->a_vpp = lowervp;
/* case 2. */
if (uerror != 0 /* && (lerror == 0) */ ) {
if (lowervp->v_type == VDIR) { /* case 2b. */
+ dun->un_flags &= ~UN_ULOCK;
+ VOP_UNLOCK(upperdvp);
uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
+ VOP_LOCK(upperdvp);
+ dun->un_flags |= UN_ULOCK;
+
if (uerror) {
if (lowervp) {
vput(lowervp);
}
}
- if (uppervp)
- VOP_UNLOCK(uppervp);
if (lowervp)
VOP_UNLOCK(lowervp);
if (error) {
if (uppervp)
- vrele(uppervp);
+ vput(uppervp);
if (lowervp)
vrele(lowervp);
} else {
struct vnode *vp;
VREF(dvp);
- VOP_LOCK(dvp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_dvp);
error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
if (error)
return (error);
- VOP_UNLOCK(vp);
-
error = union_allocvp(
ap->a_vpp,
ap->a_dvp->v_mount,
vp,
NULLVP);
if (error)
- vrele(vp);
+ vput(vp);
return (error);
}
struct vnode *vp;
VREF(dvp);
- VOP_LOCK(dvp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_dvp);
error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
if (error)
return (error);
if (vp) {
- VOP_UNLOCK(vp);
-
error = union_allocvp(
ap->a_vpp,
ap->a_dvp->v_mount,
vp,
NULLVP);
if (error)
- vrele(vp);
+ vput(vp);
}
return (error);
}
error = union_vn_create(&vp, un, p);
if (error)
return (error);
- un->un_uppervp = vp; /* XXX */
+
/* at this point, uppervp is locked */
+ un->un_uppervp = vp; /* XXX */
+ un->un_flags |= UN_ULOCK;
/*
* Now, if the file is being opened with truncation,
} else {
VOP_UNLOCK(tvp);
}
+
+ un->un_flags &= ~UN_ULOCK;
VOP_UNLOCK(un->un_uppervp);
union_vn_close(un->un_uppervp, FWRITE, cred, p);
VOP_LOCK(un->un_uppervp);
+ un->un_flags |= UN_ULOCK;
+
if (!error)
uprintf("union: copied up %s\n",
un->un_path);
if (error == 0)
error = VOP_OPEN(un->un_uppervp, mode, cred, p);
- VOP_UNLOCK(un->un_uppervp);
return (error);
}
+
+ /*
+ * Just open the lower vnode
+ */
un->un_openl++;
+ VOP_LOCK(tvp);
+ error = VOP_OPEN(tvp, mode, cred, p);
+ VOP_UNLOCK(tvp);
+
+ return (error);
}
- VOP_LOCK(tvp);
error = VOP_OPEN(tvp, mode, cred, p);
- VOP_UNLOCK(tvp);
return (error);
}
return (error);
}
- if (vp = un->un_uppervp) {
- VOP_LOCK(vp);
+ if (vp = un->un_uppervp)
error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
- VOP_UNLOCK(vp);
- }
return (error);
}
{
int error;
struct vnode *vp = OTHERVP(ap->a_vp);
+ int dolock = (vp == LOWERVP(ap->a_vp));
- VOP_LOCK(vp);
+ if (dolock)
+ VOP_LOCK(vp);
error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
- VOP_UNLOCK(vp);
+ if (dolock)
+ VOP_UNLOCK(vp);
/* Requires that arguments be restored. */
ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
int error;
if (un->un_uppervp) {
- VOP_LOCK(un->un_uppervp);
error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
ap->a_cred, ap->a_p);
- VOP_UNLOCK(un->un_uppervp);
} else {
/*
* XXX should do a copyfile (perhaps only if
{
int error;
struct vnode *vp = OTHERVP(ap->a_vp);
+ int dolock = (vp == LOWERVP(ap->a_vp));
- VOP_LOCK(vp);
+ if (dolock)
+ VOP_LOCK(vp);
error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
- VOP_UNLOCK(vp);
+ if (dolock)
+ VOP_UNLOCK(vp);
return (error);
}
{
int error;
struct vnode *vp = OTHERVP(ap->a_vp);
+ int dolock = (vp == LOWERVP(ap->a_vp));
- VOP_LOCK(vp);
+ if (dolock)
+ VOP_LOCK(vp);
error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
- VOP_UNLOCK(vp);
+ if (dolock)
+ VOP_UNLOCK(vp);
return (error);
}
struct vnode *targetvp = OTHERVP(ap->a_vp);
if (targetvp) {
- VOP_LOCK(targetvp);
+ int dolock = (targetvp == LOWERVP(ap->a_vp));
+
+ if (dolock)
+ VOP_LOCK(targetvp);
error = VOP_FSYNC(targetvp, ap->a_cred,
ap->a_waitfor, ap->a_p);
- VOP_UNLOCK(targetvp);
+ if (dolock)
+ VOP_UNLOCK(targetvp);
}
return (error);
struct vnode *vp = un->un_uppervp;
VREF(dvp);
- VOP_LOCK(dvp);
+ dun->un_flags |= UN_KLOCK;
vput(ap->a_dvp);
VREF(vp);
- VOP_LOCK(vp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_vp);
error = VOP_REMOVE(dvp, vp, ap->a_cnp);
struct vnode *vp = un->un_uppervp;
VREF(dvp);
- VOP_LOCK(dvp);
+ dun->un_flags |= UN_KLOCK;
vput(ap->a_vp);
VREF(vp);
vrele(ap->a_tdvp);
tdvp = un->un_uppervp;
VREF(tdvp);
- VOP_LOCK(tdvp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_tdvp);
}
tvp = un->un_uppervp;
VREF(tvp);
- VOP_LOCK(tvp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_tvp);
}
struct vnode *vp;
VREF(dvp);
- VOP_LOCK(dvp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_dvp);
error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
if (error)
return (error);
- VOP_UNLOCK(vp);
error = union_allocvp(
ap->a_vpp,
ap->a_dvp->v_mount,
vp,
NULLVP);
if (error)
- vrele(vp);
+ vput(vp);
return (error);
}
struct vnode *vp = un->un_uppervp;
VREF(dvp);
- VOP_LOCK(dvp);
+ dun->un_flags |= UN_KLOCK;
vput(ap->a_dvp);
VREF(vp);
- VOP_LOCK(vp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_vp);
- error = VOP_REMOVE(dvp, vp, ap->a_cnp);
+ error = VOP_RMDIR(dvp, vp, ap->a_cnp);
if (!error)
union_removed_upper(un);
struct mount *mp = ap->a_dvp->v_mount;
VREF(dvp);
- VOP_LOCK(dvp);
+ un->un_flags |= UN_KLOCK;
vput(ap->a_dvp);
error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
ap->a_vap, ap->a_target);
int error = 0;
struct union_node *un = VTOUNION(ap->a_vp);
- if (un->un_uppervp) {
- struct vnode *vp = un->un_uppervp;
-
- VOP_LOCK(vp);
- error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
- VOP_UNLOCK(vp);
- }
+ if (un->un_uppervp)
+ error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred);
return (error);
}
{
int error;
struct vnode *vp = OTHERVP(ap->a_vp);
+ int dolock = (vp == LOWERVP(ap->a_vp));
- VOP_LOCK(vp);
+ if (dolock)
+ VOP_LOCK(vp);
error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
- VOP_UNLOCK(vp);
+ if (dolock)
+ VOP_UNLOCK(vp);
return (error);
}
struct vnode *vp = OTHERVP(ap->a_dvp);
struct union_node *un = VTOUNION(ap->a_dvp);
int islocked = un->un_flags & UN_LOCKED;
+ int dolock = (vp == LOWERVP(ap->a_dvp));
- if (islocked)
+ if (islocked && dolock)
VOP_LOCK(vp);
error = VOP_ABORTOP(vp, ap->a_cnp);
- if (islocked)
+ if (islocked && dolock)
VOP_UNLOCK(vp);
return (error);
{
struct union_node *un = VTOUNION(ap->a_vp);
+ if (un->un_uppervp) {
+ if ((un->un_flags & UN_ULOCK) == 0) {
+ VOP_LOCK(un->un_uppervp);
+ un->un_flags |= UN_ULOCK;
+ }
+#ifdef DIAGNOSTIC
+ if (un->un_flags & UN_KLOCK)
+ panic("union: dangling upper lock");
+#endif
+ }
+
while (un->un_flags & UN_LOCKED) {
#ifdef DIAGNOSTIC
if (curproc && un->un_pid == curproc->p_pid &&
#endif
un->un_flags &= ~UN_LOCKED;
+
+ if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
+ VOP_UNLOCK(un->un_uppervp);
+
+ un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
+
if (un->un_flags & UN_WANT) {
un->un_flags &= ~UN_WANT;
wakeup((caddr_t) &un->un_flags);
{
int error;
struct vnode *vp = OTHERVP(ap->a_vp);
+ int dolock = (vp == LOWERVP(ap->a_vp));
- VOP_LOCK(vp);
+ if (dolock)
+ VOP_LOCK(vp);
error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
- VOP_UNLOCK(vp);
+ if (dolock)
+ VOP_UNLOCK(vp);
return (error);
}
{
int error;
struct vnode *vp = OTHERVP(ap->a_vp);
+ int dolock = (vp == LOWERVP(ap->a_vp));
- VOP_LOCK(vp);
+ if (dolock)
+ VOP_LOCK(vp);
error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
- VOP_UNLOCK(vp);
+ if (dolock)
+ VOP_UNLOCK(vp);
return (error);
}