xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 66034)
165935Spendry /*
265935Spendry  * Copyright (c) 1992, 1993, 1994 The Regents of the University of California.
365935Spendry  * Copyright (c) 1992, 1993, 1994 Jan-Simon Pendry.
465935Spendry  * All rights reserved.
565935Spendry  *
665935Spendry  * This code is derived from software contributed to Berkeley by
765963Spendry  * Jan-Simon Pendry.
865935Spendry  *
965935Spendry  * %sccs.include.redist.c%
1065935Spendry  *
11*66034Spendry  *	@(#)union_vnops.c	1.9 (Berkeley) 02/08/94
1265935Spendry  */
1365935Spendry 
1465935Spendry #include <sys/param.h>
1565935Spendry #include <sys/systm.h>
1665935Spendry #include <sys/proc.h>
1765935Spendry #include <sys/file.h>
1865935Spendry #include <sys/time.h>
1965935Spendry #include <sys/types.h>
2065935Spendry #include <sys/vnode.h>
2165935Spendry #include <sys/mount.h>
2265935Spendry #include <sys/namei.h>
2365935Spendry #include <sys/malloc.h>
2465935Spendry #include <sys/buf.h>
2565935Spendry #include "union.h"
2665935Spendry 
2765935Spendry static int
2865989Spendry union_lookup1(udvp, dvp, vpp, cnp)
2965989Spendry 	struct vnode *udvp;
3065935Spendry 	struct vnode *dvp;
3165935Spendry 	struct vnode **vpp;
3265935Spendry 	struct componentname *cnp;
3365935Spendry {
3465935Spendry 	int error;
3565935Spendry 	struct vnode *tdvp;
3665935Spendry 	struct mount *mp;
3765935Spendry 
3865994Spendry 	/*
3965994Spendry 	 * If stepping up the directory tree, check for going
4065994Spendry 	 * back across the mount point, in which case do what
4165994Spendry 	 * lookup would do by stepping back down the mount
4265994Spendry 	 * hierarchy.
4365994Spendry 	 */
4465935Spendry 	if (cnp->cn_flags & ISDOTDOT) {
4565935Spendry 		for (;;) {
46*66034Spendry 			/*
47*66034Spendry 			 * Don't do the NOCROSSMOUNT check
48*66034Spendry 			 * at this level.  By definition,
49*66034Spendry 			 * union fs deals with namespaces, not
50*66034Spendry 			 * filesystems.
51*66034Spendry 			 */
52*66034Spendry 			if ((dvp->v_flag & VROOT) == 0)
5365935Spendry 				break;
5465935Spendry 
5565935Spendry 			tdvp = dvp;
5665935Spendry 			dvp = dvp->v_mount->mnt_vnodecovered;
5765935Spendry 			vput(tdvp);
5865935Spendry 			VREF(dvp);
5965935Spendry 			VOP_LOCK(dvp);
6065935Spendry 		}
6165935Spendry 	}
6266027Spendry 
6365935Spendry         error = VOP_LOOKUP(dvp, &tdvp, cnp);
6465935Spendry 	if (error)
6565935Spendry 		return (error);
6665935Spendry 
6765994Spendry 	/*
6866027Spendry 	 * The parent directory will have been unlocked, unless lookup
6966027Spendry 	 * found the last component.  In which case, re-lock the node
7066027Spendry 	 * here to allow it to be unlocked again (phew) in union_lookup.
7165994Spendry 	 */
7266027Spendry 	if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
7365994Spendry 		VOP_LOCK(dvp);
7465994Spendry 
7565935Spendry 	dvp = tdvp;
7665994Spendry 
7765994Spendry 	/*
7865994Spendry 	 * Lastly check if the current node is a mount point in
79*66034Spendry 	 * which case walk up the mount hierarchy making sure not to
8065994Spendry 	 * bump into the root of the mount tree (ie. dvp != udvp).
8165994Spendry 	 */
8265989Spendry 	while (dvp != udvp && (dvp->v_type == VDIR) &&
83*66034Spendry 	       (mp = dvp->v_mountedhere)) {
8465935Spendry 
8565935Spendry 		if (mp->mnt_flag & MNT_MLOCK) {
8665935Spendry 			mp->mnt_flag |= MNT_MWAIT;
8765935Spendry 			sleep((caddr_t) mp, PVFS);
8865935Spendry 			continue;
8965935Spendry 		}
9065935Spendry 
9165935Spendry 		if (error = VFS_ROOT(mp, &tdvp)) {
9265935Spendry 			vput(dvp);
9365935Spendry 			return (error);
9465935Spendry 		}
9565935Spendry 
9665965Spendry 		vput(dvp);
9765935Spendry 		dvp = tdvp;
9865935Spendry 	}
9965935Spendry 
10065935Spendry 	*vpp = dvp;
10165935Spendry 	return (0);
10265935Spendry }
10365935Spendry 
10465935Spendry int
10565935Spendry union_lookup(ap)
10665935Spendry 	struct vop_lookup_args /* {
10765935Spendry 		struct vnodeop_desc *a_desc;
10865935Spendry 		struct vnode *a_dvp;
10965935Spendry 		struct vnode **a_vpp;
11065935Spendry 		struct componentname *a_cnp;
11165935Spendry 	} */ *ap;
11265935Spendry {
11365965Spendry 	int error;
11465935Spendry 	int uerror, lerror;
11565935Spendry 	struct vnode *uppervp, *lowervp;
11665935Spendry 	struct vnode *upperdvp, *lowerdvp;
11765935Spendry 	struct vnode *dvp = ap->a_dvp;
11865989Spendry 	struct union_node *dun = VTOUNION(dvp);
11965935Spendry 	struct componentname *cnp = ap->a_cnp;
12065935Spendry 	int lockparent = cnp->cn_flags & LOCKPARENT;
12165994Spendry 	int rdonly = cnp->cn_flags & RDONLY;
12265997Spendry 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
12365935Spendry 
12465965Spendry 	cnp->cn_flags |= LOCKPARENT;
12565965Spendry 
12665935Spendry 	upperdvp = dun->un_uppervp;
12765935Spendry 	lowerdvp = dun->un_lowervp;
12865997Spendry 	uppervp = NULLVP;
12965997Spendry 	lowervp = NULLVP;
13065935Spendry 
13165935Spendry 	/*
13265935Spendry 	 * do the lookup in the upper level.
13365935Spendry 	 * if that level comsumes additional pathnames,
13465935Spendry 	 * then assume that something special is going
13565935Spendry 	 * on and just return that vnode.
13665935Spendry 	 */
13765935Spendry 	if (upperdvp) {
13865965Spendry 		VOP_LOCK(upperdvp);
13965997Spendry 		uerror = union_lookup1(um->um_uppervp, upperdvp,
14065997Spendry 					&uppervp, cnp);
14165989Spendry 		if (uppervp != upperdvp)
14265989Spendry 			VOP_UNLOCK(upperdvp);
14365965Spendry 
14465935Spendry 		if (cnp->cn_consume != 0) {
14565935Spendry 			*ap->a_vpp = uppervp;
14665965Spendry 			if (!lockparent)
14765965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
14865935Spendry 			return (uerror);
14965935Spendry 		}
15065935Spendry 	} else {
15165935Spendry 		uerror = ENOENT;
15265935Spendry 	}
15365935Spendry 
15465935Spendry 	/*
15565935Spendry 	 * in a similar way to the upper layer, do the lookup
15665935Spendry 	 * in the lower layer.   this time, if there is some
15765935Spendry 	 * component magic going on, then vput whatever we got
15865935Spendry 	 * back from the upper layer and return the lower vnode
15965935Spendry 	 * instead.
16065935Spendry 	 */
16165935Spendry 	if (lowerdvp) {
16265965Spendry 		VOP_LOCK(lowerdvp);
16365997Spendry 		lerror = union_lookup1(um->um_lowervp, lowerdvp,
16465997Spendry 					&lowervp, cnp);
16565989Spendry 		if (lowervp != lowerdvp)
16665989Spendry 			VOP_UNLOCK(lowerdvp);
16765965Spendry 
16865935Spendry 		if (cnp->cn_consume != 0) {
16965935Spendry 			if (uppervp) {
17065935Spendry 				vput(uppervp);
17165997Spendry 				uppervp = NULLVP;
17265935Spendry 			}
17365935Spendry 			*ap->a_vpp = lowervp;
17465965Spendry 			if (!lockparent)
17565965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
17665935Spendry 			return (lerror);
17765935Spendry 		}
17865935Spendry 	} else {
17965935Spendry 		lerror = ENOENT;
18065935Spendry 	}
18165935Spendry 
18265965Spendry 	if (!lockparent)
18365965Spendry 		cnp->cn_flags &= ~LOCKPARENT;
18465965Spendry 
18565935Spendry 	/*
18665935Spendry 	 * at this point, we have uerror and lerror indicating
18765935Spendry 	 * possible errors with the lookups in the upper and lower
18865935Spendry 	 * layers.  additionally, uppervp and lowervp are (locked)
18965935Spendry 	 * references to existing vnodes in the upper and lower layers.
19065935Spendry 	 *
19165935Spendry 	 * there are now three cases to consider.
19265935Spendry 	 * 1. if both layers returned an error, then return whatever
19365935Spendry 	 *    error the upper layer generated.
19465935Spendry 	 *
19565935Spendry 	 * 2. if the top layer failed and the bottom layer succeeded
19665935Spendry 	 *    then two subcases occur.
19765935Spendry 	 *    a.  the bottom vnode is not a directory, in which
19865935Spendry 	 *	  case just return a new union vnode referencing
19965935Spendry 	 *	  an empty top layer and the existing bottom layer.
20065935Spendry 	 *    b.  the bottom vnode is a directory, in which case
20165935Spendry 	 *	  create a new directory in the top-level and
20265935Spendry 	 *	  continue as in case 3.
20365935Spendry 	 *
20465935Spendry 	 * 3. if the top layer succeeded then return a new union
20565935Spendry 	 *    vnode referencing whatever the new top layer and
20665935Spendry 	 *    whatever the bottom layer returned.
20765935Spendry 	 */
20865935Spendry 
20966027Spendry 	*ap->a_vpp = NULLVP;
21066027Spendry 
21165935Spendry 	/* case 1. */
21265935Spendry 	if ((uerror != 0) && (lerror != 0)) {
21365935Spendry 		return (uerror);
21465935Spendry 	}
21565935Spendry 
21665935Spendry 	/* case 2. */
21765935Spendry 	if (uerror != 0 /* && (lerror == 0) */ ) {
21865935Spendry 		if (lowervp->v_type == VDIR) { /* case 2b. */
21965997Spendry 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
22065935Spendry 			if (uerror) {
22165935Spendry 				if (lowervp) {
22265935Spendry 					vput(lowervp);
22365997Spendry 					lowervp = NULLVP;
22465935Spendry 				}
22565935Spendry 				return (uerror);
22665935Spendry 			}
22765935Spendry 		}
22865935Spendry 	}
22965935Spendry 
23065965Spendry 	if (uppervp)
23165965Spendry 		VOP_UNLOCK(uppervp);
23265965Spendry 	if (lowervp)
23365965Spendry 		VOP_UNLOCK(lowervp);
23465965Spendry 
23565997Spendry 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
23665997Spendry 			      uppervp, lowervp);
23765997Spendry 
23865965Spendry 	if (error) {
23965965Spendry 		if (uppervp)
24065965Spendry 			vrele(uppervp);
24165965Spendry 		if (lowervp)
24265965Spendry 			vrele(lowervp);
24365965Spendry 	} else {
24465994Spendry 		if (*ap->a_vpp != dvp)
24565994Spendry 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
24665994Spendry 				VOP_UNLOCK(dvp);
24765965Spendry 	}
24865965Spendry 
24965965Spendry 	return (error);
25065935Spendry }
25165935Spendry 
25265963Spendry int
25365963Spendry union_create(ap)
25465963Spendry 	struct vop_create_args /* {
25565963Spendry 		struct vnode *a_dvp;
25665963Spendry 		struct vnode **a_vpp;
25765963Spendry 		struct componentname *a_cnp;
25865963Spendry 		struct vattr *a_vap;
25965963Spendry 	} */ *ap;
26065963Spendry {
26165963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
26265963Spendry 	struct vnode *dvp = un->un_uppervp;
26365963Spendry 
26465963Spendry 	if (dvp) {
26565963Spendry 		int error;
26665963Spendry 		struct vnode *vp;
26765963Spendry 
26865963Spendry 		VREF(dvp);
26965963Spendry 		VOP_LOCK(dvp);
27065963Spendry 		vput(ap->a_dvp);
27165963Spendry 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
27265963Spendry 		if (error)
27365963Spendry 			return (error);
27465963Spendry 
27565997Spendry 		VOP_UNLOCK(vp);
27665997Spendry 
27765963Spendry 		error = union_allocvp(
27865963Spendry 				ap->a_vpp,
27965989Spendry 				ap->a_dvp->v_mount,
28065989Spendry 				ap->a_dvp,
28165965Spendry 				NULLVP,
28265963Spendry 				ap->a_cnp,
28365963Spendry 				vp,
28465963Spendry 				NULLVP);
28565965Spendry 		if (error)
28665965Spendry 			vrele(vp);
28765963Spendry 		return (error);
28865963Spendry 	}
28965963Spendry 
29065963Spendry 	vput(ap->a_dvp);
29165963Spendry 	return (EROFS);
29265963Spendry }
29365963Spendry 
29465963Spendry int
29565963Spendry union_mknod(ap)
29665963Spendry 	struct vop_mknod_args /* {
29765963Spendry 		struct vnode *a_dvp;
29865963Spendry 		struct vnode **a_vpp;
29965963Spendry 		struct componentname *a_cnp;
30065963Spendry 		struct vattr *a_vap;
30165963Spendry 	} */ *ap;
30265963Spendry {
30365963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
30465963Spendry 	struct vnode *dvp = un->un_uppervp;
30565963Spendry 
30665963Spendry 	if (dvp) {
30765963Spendry 		int error;
30865963Spendry 		struct vnode *vp;
30965963Spendry 
31065963Spendry 		VREF(dvp);
31165963Spendry 		VOP_LOCK(dvp);
31265963Spendry 		vput(ap->a_dvp);
31365963Spendry 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
31465963Spendry 		if (error)
31565963Spendry 			return (error);
31665963Spendry 
31765965Spendry 		if (vp) {
31865997Spendry 			VOP_UNLOCK(vp);
31965997Spendry 
32065965Spendry 			error = union_allocvp(
32165965Spendry 					ap->a_vpp,
32265989Spendry 					ap->a_dvp->v_mount,
32365989Spendry 					ap->a_dvp,
32465965Spendry 					NULLVP,
32565965Spendry 					ap->a_cnp,
32665965Spendry 					vp,
32765965Spendry 					NULLVP);
32865965Spendry 			if (error)
32965965Spendry 				vrele(vp);
33065965Spendry 		}
33165963Spendry 		return (error);
33265963Spendry 	}
33365963Spendry 
33465963Spendry 	vput(ap->a_dvp);
33565963Spendry 	return (EROFS);
33665963Spendry }
33765963Spendry 
33865935Spendry int
33965935Spendry union_open(ap)
34065935Spendry 	struct vop_open_args /* {
34165935Spendry 		struct vnodeop_desc *a_desc;
34265935Spendry 		struct vnode *a_vp;
34365935Spendry 		int a_mode;
34465935Spendry 		struct ucred *a_cred;
34565935Spendry 		struct proc *a_p;
34665935Spendry 	} */ *ap;
34765935Spendry {
34865935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
34965965Spendry 	struct vnode *tvp;
35065935Spendry 	int mode = ap->a_mode;
35165935Spendry 	struct ucred *cred = ap->a_cred;
35265935Spendry 	struct proc *p = ap->a_p;
35365965Spendry 	int error;
35465935Spendry 
35565935Spendry 	/*
35665935Spendry 	 * If there is an existing upper vp then simply open that.
35765935Spendry 	 */
35865965Spendry 	tvp = un->un_uppervp;
35965965Spendry 	if (tvp == NULLVP) {
36065935Spendry 		/*
36165965Spendry 		 * If the lower vnode is being opened for writing, then
36265965Spendry 		 * copy the file contents to the upper vnode and open that,
36365965Spendry 		 * otherwise can simply open the lower vnode.
36465935Spendry 		 */
36565965Spendry 		tvp = un->un_lowervp;
36665965Spendry 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
36765994Spendry 			struct vnode *vp;
36865997Spendry 			int i;
36965963Spendry 
37065965Spendry 			/*
37165965Spendry 			 * Open the named file in the upper layer.  Note that
37265965Spendry 			 * the file may have come into existence *since* the
37365965Spendry 			 * lookup was done, since the upper layer may really
37465965Spendry 			 * be a loopback mount of some other filesystem...
37565965Spendry 			 * so open the file with exclusive create and barf if
37665965Spendry 			 * it already exists.
37766027Spendry 			 * XXX - perhaps should re-lookup the node (once more
37865965Spendry 			 * with feeling) and simply open that.  Who knows.
37965965Spendry 			 */
38065997Spendry 			error = union_vn_create(&vp, un, p);
38165965Spendry 			if (error)
38265965Spendry 				return (error);
38365994Spendry 			un->un_uppervp = vp;	/* XXX */
38465965Spendry 			/* at this point, uppervp is locked */
38565965Spendry 
38665965Spendry 			/*
38765965Spendry 			 * Now, if the file is being opened with truncation,
38865965Spendry 			 * then the (new) upper vnode is ready to fly,
38965965Spendry 			 * otherwise the data from the lower vnode must be
39065965Spendry 			 * copied to the upper layer first.  This only works
39165965Spendry 			 * for regular files (check is made above).
39265965Spendry 			 */
39365965Spendry 			if ((mode & O_TRUNC) == 0) {
39465965Spendry 				/*
39565965Spendry 				 * XXX - should not ignore errors
39665965Spendry 				 * from VOP_CLOSE
39765965Spendry 				 */
39865994Spendry 				VOP_LOCK(tvp);
39965965Spendry 				error = VOP_OPEN(tvp, FREAD, cred, p);
40065965Spendry 				if (error == 0) {
40165965Spendry 					error = union_copyfile(p, cred,
40265965Spendry 						       tvp, un->un_uppervp);
40365965Spendry 					VOP_UNLOCK(tvp);
40465965Spendry 					(void) VOP_CLOSE(tvp, FREAD);
40565965Spendry 				} else {
40665965Spendry 					VOP_UNLOCK(tvp);
40765965Spendry 				}
40865965Spendry 				VOP_UNLOCK(un->un_uppervp);
40966028Spendry 				union_vn_close(un->un_uppervp, FWRITE, cred, p);
41065965Spendry 				VOP_LOCK(un->un_uppervp);
41165997Spendry 				if (!error)
41266027Spendry 					uprintf("union: copied up %s\n",
41365997Spendry 								un->un_path);
41465935Spendry 			}
41565997Spendry 
41665997Spendry 			/*
41765997Spendry 			 * Subsequent IOs will go to the top layer, so
41865997Spendry 			 * call close on the lower vnode and open on the
41965997Spendry 			 * upper vnode to ensure that the filesystem keeps
42065997Spendry 			 * its references counts right.  This doesn't do
42165997Spendry 			 * the right thing with (cred) and (FREAD) though.
42265997Spendry 			 * Ignoring error returns is not righ, either.
42365997Spendry 			 */
42466027Spendry 			for (i = 0; i < un->un_openl; i++) {
42565997Spendry 				(void) VOP_CLOSE(tvp, FREAD);
42665997Spendry 				(void) VOP_OPEN(un->un_uppervp, FREAD, cred, p);
42765997Spendry 			}
42866027Spendry 			un->un_openl = 0;
42965997Spendry 
43065965Spendry 			if (error == 0)
43165965Spendry 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
43265963Spendry 			VOP_UNLOCK(un->un_uppervp);
43365965Spendry 			return (error);
43465935Spendry 		}
43566027Spendry 		un->un_openl++;
43665935Spendry 	}
43765935Spendry 
43865965Spendry 	VOP_LOCK(tvp);
43965965Spendry 	error = VOP_OPEN(tvp, mode, cred, p);
44065965Spendry 	VOP_UNLOCK(tvp);
44165965Spendry 
44265965Spendry 	return (error);
44365935Spendry }
44465935Spendry 
44565963Spendry int
44665963Spendry union_close(ap)
44765963Spendry 	struct vop_close_args /* {
44865963Spendry 		struct vnode *a_vp;
44965963Spendry 		int  a_fflag;
45065963Spendry 		struct ucred *a_cred;
45165963Spendry 		struct proc *a_p;
45265963Spendry 	} */ *ap;
45365963Spendry {
45465997Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
45565997Spendry 	struct vnode *vp;
45665963Spendry 
45765997Spendry 	if (un->un_uppervp) {
45865997Spendry 		vp = un->un_uppervp;
45965997Spendry 	} else {
46066027Spendry #ifdef UNION_DIAGNOSTIC
46166027Spendry 		if (un->un_openl <= 0)
46266027Spendry 			panic("union: un_openl cnt");
46365997Spendry #endif
46466027Spendry 		--un->un_openl;
46565997Spendry 		vp = un->un_lowervp;
46665997Spendry 	}
46766027Spendry 
46865997Spendry 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
46965963Spendry }
47065963Spendry 
47165935Spendry /*
47265963Spendry  * Check access permission on the union vnode.
47365963Spendry  * The access check being enforced is to check
47465963Spendry  * against both the underlying vnode, and any
47565963Spendry  * copied vnode.  This ensures that no additional
47665963Spendry  * file permissions are given away simply because
47765963Spendry  * the user caused an implicit file copy.
47865963Spendry  */
47965963Spendry int
48065963Spendry union_access(ap)
48165963Spendry 	struct vop_access_args /* {
48265963Spendry 		struct vnodeop_desc *a_desc;
48365963Spendry 		struct vnode *a_vp;
48465963Spendry 		int a_mode;
48565963Spendry 		struct ucred *a_cred;
48665963Spendry 		struct proc *a_p;
48765963Spendry 	} */ *ap;
48865963Spendry {
48965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
49065965Spendry 	int error = 0;
49165963Spendry 	struct vnode *vp;
49265963Spendry 
49365963Spendry 	if (vp = un->un_lowervp) {
49465965Spendry 		VOP_LOCK(vp);
49565963Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
49665965Spendry 		VOP_UNLOCK(vp);
49765963Spendry 		if (error)
49865963Spendry 			return (error);
49965963Spendry 	}
50065963Spendry 
50165965Spendry 	if (vp = un->un_uppervp) {
50265965Spendry 		VOP_LOCK(vp);
50365965Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
50465965Spendry 		VOP_UNLOCK(vp);
50565965Spendry 	}
50665965Spendry 
50765965Spendry 	return (error);
50865963Spendry }
50965963Spendry 
51065963Spendry /*
51165935Spendry  *  We handle getattr only to change the fsid.
51265935Spendry  */
51365935Spendry int
51465935Spendry union_getattr(ap)
51565935Spendry 	struct vop_getattr_args /* {
51665935Spendry 		struct vnode *a_vp;
51765935Spendry 		struct vattr *a_vap;
51865935Spendry 		struct ucred *a_cred;
51965935Spendry 		struct proc *a_p;
52065935Spendry 	} */ *ap;
52165935Spendry {
52265935Spendry 	int error;
52365965Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
52465935Spendry 
52565965Spendry 	VOP_LOCK(vp);
52665965Spendry 	error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
52765965Spendry 	VOP_UNLOCK(vp);
52865965Spendry 
52965935Spendry 	/* Requires that arguments be restored. */
53065935Spendry 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
53165935Spendry 	return (0);
53265935Spendry }
53365935Spendry 
53465963Spendry int
53565965Spendry union_setattr(ap)
53665963Spendry 	struct vop_setattr_args /* {
53765963Spendry 		struct vnode *a_vp;
53865963Spendry 		struct vattr *a_vap;
53965963Spendry 		struct ucred *a_cred;
54065963Spendry 		struct proc *a_p;
54165963Spendry 	} */ *ap;
54265963Spendry {
54365963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
54465963Spendry 	int error;
54565963Spendry 
54665963Spendry 	if (un->un_uppervp) {
54765963Spendry 		VOP_LOCK(un->un_uppervp);
54865963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
54965963Spendry 					ap->a_cred, ap->a_p);
55065963Spendry 		VOP_UNLOCK(un->un_uppervp);
55165963Spendry 	} else {
55265963Spendry 		/*
55365963Spendry 		 * XXX should do a copyfile (perhaps only if
55465963Spendry 		 * the file permission change, which would not
55565963Spendry 		 * track va_ctime correctly).
55665963Spendry 		 */
55765963Spendry 		error = EROFS;
55865963Spendry 	}
55965963Spendry 
56065963Spendry 	return (error);
56165963Spendry }
56265963Spendry 
56365963Spendry int
56465963Spendry union_read(ap)
56565963Spendry 	struct vop_read_args /* {
56665963Spendry 		struct vnode *a_vp;
56765963Spendry 		struct uio *a_uio;
56865963Spendry 		int  a_ioflag;
56965963Spendry 		struct ucred *a_cred;
57065963Spendry 	} */ *ap;
57165963Spendry {
57265963Spendry 	int error;
57365963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
57465963Spendry 
57565965Spendry 	VOP_LOCK(vp);
57665963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
57765965Spendry 	VOP_UNLOCK(vp);
57865963Spendry 
57965963Spendry 	return (error);
58065963Spendry }
58165963Spendry 
58265963Spendry int
58365963Spendry union_write(ap)
58465963Spendry 	struct vop_read_args /* {
58565963Spendry 		struct vnode *a_vp;
58665963Spendry 		struct uio *a_uio;
58765963Spendry 		int  a_ioflag;
58865963Spendry 		struct ucred *a_cred;
58965963Spendry 	} */ *ap;
59065963Spendry {
59165963Spendry 	int error;
59265963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
59365963Spendry 
59465963Spendry 	VOP_LOCK(vp);
59565963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
59665963Spendry 	VOP_UNLOCK(vp);
59765963Spendry 
59865963Spendry 	return (error);
59965963Spendry }
60065963Spendry 
60165963Spendry int
60265963Spendry union_ioctl(ap)
60365963Spendry 	struct vop_ioctl_args /* {
60465963Spendry 		struct vnode *a_vp;
60565963Spendry 		int  a_command;
60665963Spendry 		caddr_t  a_data;
60765963Spendry 		int  a_fflag;
60865963Spendry 		struct ucred *a_cred;
60965963Spendry 		struct proc *a_p;
61065963Spendry 	} */ *ap;
61165963Spendry {
61265963Spendry 
61365963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
61465963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
61565963Spendry }
61665963Spendry 
61765963Spendry int
61865963Spendry union_select(ap)
61965963Spendry 	struct vop_select_args /* {
62065963Spendry 		struct vnode *a_vp;
62165963Spendry 		int  a_which;
62265963Spendry 		int  a_fflags;
62365963Spendry 		struct ucred *a_cred;
62465963Spendry 		struct proc *a_p;
62565963Spendry 	} */ *ap;
62665963Spendry {
62765963Spendry 
62865963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
62965963Spendry 				ap->a_cred, ap->a_p));
63065963Spendry }
63165963Spendry 
63265963Spendry int
63365963Spendry union_mmap(ap)
63465963Spendry 	struct vop_mmap_args /* {
63565963Spendry 		struct vnode *a_vp;
63665963Spendry 		int  a_fflags;
63765963Spendry 		struct ucred *a_cred;
63865963Spendry 		struct proc *a_p;
63965963Spendry 	} */ *ap;
64065963Spendry {
64165963Spendry 
64265963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
64365963Spendry 				ap->a_cred, ap->a_p));
64465963Spendry }
64565963Spendry 
64665963Spendry int
64765963Spendry union_fsync(ap)
64865963Spendry 	struct vop_fsync_args /* {
64965963Spendry 		struct vnode *a_vp;
65065963Spendry 		struct ucred *a_cred;
65165963Spendry 		int  a_waitfor;
65265963Spendry 		struct proc *a_p;
65365963Spendry 	} */ *ap;
65465963Spendry {
65565963Spendry 	int error = 0;
65665963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
65765963Spendry 
65865963Spendry 	if (targetvp) {
65965963Spendry 		VOP_LOCK(targetvp);
66065963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
66165963Spendry 					ap->a_waitfor, ap->a_p);
66265963Spendry 		VOP_UNLOCK(targetvp);
66365963Spendry 	}
66465963Spendry 
66565963Spendry 	return (error);
66665963Spendry }
66765963Spendry 
66865963Spendry int
66965963Spendry union_seek(ap)
67065963Spendry 	struct vop_seek_args /* {
67165963Spendry 		struct vnode *a_vp;
67265963Spendry 		off_t  a_oldoff;
67365963Spendry 		off_t  a_newoff;
67465963Spendry 		struct ucred *a_cred;
67565963Spendry 	} */ *ap;
67665963Spendry {
67765963Spendry 
67865963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
67965963Spendry }
68065963Spendry 
68165963Spendry int
68265963Spendry union_remove(ap)
68365963Spendry 	struct vop_remove_args /* {
68465963Spendry 		struct vnode *a_dvp;
68565963Spendry 		struct vnode *a_vp;
68665963Spendry 		struct componentname *a_cnp;
68765963Spendry 	} */ *ap;
68865963Spendry {
68965963Spendry 	int error;
69065963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
69165963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
69265963Spendry 
69365963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
69465963Spendry 		struct vnode *dvp = dun->un_uppervp;
69565963Spendry 		struct vnode *vp = un->un_uppervp;
69665963Spendry 
69765963Spendry 		VREF(dvp);
69865963Spendry 		VOP_LOCK(dvp);
69965963Spendry 		vput(ap->a_dvp);
70065963Spendry 		VREF(vp);
70165963Spendry 		VOP_LOCK(vp);
70265963Spendry 		vput(ap->a_vp);
70365963Spendry 
70465963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
70566027Spendry 		if (!error)
70666027Spendry 			union_removed_upper(un);
70766027Spendry 
70866027Spendry 		/*
70966027Spendry 		 * XXX: should create a whiteout here
71066027Spendry 		 */
71165963Spendry 	} else {
71265963Spendry 		/*
71365963Spendry 		 * XXX: should create a whiteout here
71465963Spendry 		 */
71565963Spendry 		vput(ap->a_dvp);
71665963Spendry 		vput(ap->a_vp);
71765963Spendry 		error = EROFS;
71865963Spendry 	}
71965963Spendry 
72065963Spendry 	return (error);
72165963Spendry }
72265963Spendry 
72365963Spendry int
72465963Spendry union_link(ap)
72565963Spendry 	struct vop_link_args /* {
72665963Spendry 		struct vnode *a_vp;
72765963Spendry 		struct vnode *a_tdvp;
72865963Spendry 		struct componentname *a_cnp;
72965963Spendry 	} */ *ap;
73065963Spendry {
73165963Spendry 	int error;
73265963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
73365963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
73465963Spendry 
73565963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
73665963Spendry 		struct vnode *dvp = dun->un_uppervp;
73765963Spendry 		struct vnode *vp = un->un_uppervp;
73865963Spendry 
73965963Spendry 		VREF(dvp);
74065963Spendry 		VOP_LOCK(dvp);
74165963Spendry 		vput(ap->a_vp);
74265963Spendry 		VREF(vp);
74365963Spendry 		vrele(ap->a_tdvp);
74465963Spendry 
74565963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
74665963Spendry 	} else {
74765963Spendry 		/*
74865963Spendry 		 * XXX: need to copy to upper layer
74965963Spendry 		 * and do the link there.
75065963Spendry 		 */
75165963Spendry 		vput(ap->a_vp);
75265963Spendry 		vrele(ap->a_tdvp);
75365963Spendry 		error = EROFS;
75465963Spendry 	}
75565963Spendry 
75665963Spendry 	return (error);
75765963Spendry }
75865963Spendry 
75965963Spendry int
76065963Spendry union_rename(ap)
76165963Spendry 	struct vop_rename_args  /* {
76265963Spendry 		struct vnode *a_fdvp;
76365963Spendry 		struct vnode *a_fvp;
76465963Spendry 		struct componentname *a_fcnp;
76565963Spendry 		struct vnode *a_tdvp;
76665963Spendry 		struct vnode *a_tvp;
76765963Spendry 		struct componentname *a_tcnp;
76865963Spendry 	} */ *ap;
76965963Spendry {
77065963Spendry 	int error;
77165963Spendry 
77265963Spendry 	struct vnode *fdvp = ap->a_fdvp;
77365963Spendry 	struct vnode *fvp = ap->a_fvp;
77465963Spendry 	struct vnode *tdvp = ap->a_tdvp;
77565963Spendry 	struct vnode *tvp = ap->a_tvp;
77665963Spendry 
77765963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
77865963Spendry 		struct union_node *un = VTOUNION(fdvp);
77965997Spendry 		if (un->un_uppervp == NULLVP) {
78065963Spendry 			error = EROFS;
78165963Spendry 			goto bad;
78265963Spendry 		}
78365963Spendry 
78465963Spendry 		fdvp = un->un_uppervp;
78565963Spendry 		VREF(fdvp);
78665963Spendry 		vrele(ap->a_fdvp);
78765963Spendry 	}
78865963Spendry 
78965963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
79065963Spendry 		struct union_node *un = VTOUNION(fvp);
79165997Spendry 		if (un->un_uppervp == NULLVP) {
79265963Spendry 			error = EROFS;
79365963Spendry 			goto bad;
79465963Spendry 		}
79565963Spendry 
79665963Spendry 		fvp = un->un_uppervp;
79765963Spendry 		VREF(fvp);
79865963Spendry 		vrele(ap->a_fvp);
79965963Spendry 	}
80065963Spendry 
80165963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
80265963Spendry 		struct union_node *un = VTOUNION(tdvp);
80365997Spendry 		if (un->un_uppervp == NULLVP) {
80465963Spendry 			error = EROFS;
80565963Spendry 			goto bad;
80665963Spendry 		}
80765963Spendry 
80865963Spendry 		tdvp = un->un_uppervp;
80965963Spendry 		VREF(tdvp);
81065963Spendry 		VOP_LOCK(tdvp);
81165997Spendry 		vput(ap->a_tdvp);
81265963Spendry 	}
81365963Spendry 
81465963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
81565963Spendry 		struct union_node *un = VTOUNION(tvp);
81665997Spendry 		if (un->un_uppervp == NULLVP) {
81765963Spendry 			error = EROFS;
81865963Spendry 			goto bad;
81965963Spendry 		}
82065963Spendry 
82165963Spendry 		tvp = un->un_uppervp;
82265963Spendry 		VREF(tvp);
82365963Spendry 		VOP_LOCK(tvp);
82465963Spendry 		vput(ap->a_tvp);
82565963Spendry 	}
82665963Spendry 
82765963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
82865963Spendry 
82965963Spendry bad:
83065963Spendry 	vrele(fdvp);
83165963Spendry 	vrele(fvp);
83265963Spendry 	vput(tdvp);
83365963Spendry 	if (tvp)
83465963Spendry 		vput(tvp);
83565963Spendry 
83665963Spendry 	return (error);
83765963Spendry }
83865963Spendry 
83965963Spendry int
84065963Spendry union_mkdir(ap)
84165963Spendry 	struct vop_mkdir_args /* {
84265963Spendry 		struct vnode *a_dvp;
84365963Spendry 		struct vnode **a_vpp;
84465963Spendry 		struct componentname *a_cnp;
84565963Spendry 		struct vattr *a_vap;
84665963Spendry 	} */ *ap;
84765963Spendry {
84865963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
84965963Spendry 	struct vnode *dvp = un->un_uppervp;
85065963Spendry 
85165963Spendry 	if (dvp) {
85265963Spendry 		int error;
85365963Spendry 		struct vnode *vp;
85465963Spendry 
85565963Spendry 		VREF(dvp);
85665963Spendry 		VOP_LOCK(dvp);
85765963Spendry 		vput(ap->a_dvp);
85865963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
85965963Spendry 		if (error)
86065963Spendry 			return (error);
86165963Spendry 
86265997Spendry 		VOP_UNLOCK(vp);
86365963Spendry 		error = union_allocvp(
86465963Spendry 				ap->a_vpp,
86565989Spendry 				ap->a_dvp->v_mount,
86665989Spendry 				ap->a_dvp,
86765965Spendry 				NULLVP,
86865963Spendry 				ap->a_cnp,
86965963Spendry 				vp,
87065963Spendry 				NULLVP);
87165965Spendry 		if (error)
87265965Spendry 			vrele(vp);
87365963Spendry 		return (error);
87465963Spendry 	}
87565963Spendry 
87665963Spendry 	vput(ap->a_dvp);
87765963Spendry 	return (EROFS);
87865963Spendry }
87965963Spendry 
88065963Spendry int
88165963Spendry union_rmdir(ap)
88265963Spendry 	struct vop_rmdir_args /* {
88365963Spendry 		struct vnode *a_dvp;
88465963Spendry 		struct vnode *a_vp;
88565963Spendry 		struct componentname *a_cnp;
88665963Spendry 	} */ *ap;
88765963Spendry {
88865963Spendry 	int error;
88965963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
89065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
89165963Spendry 
89265963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
89365963Spendry 		struct vnode *dvp = dun->un_uppervp;
89465963Spendry 		struct vnode *vp = un->un_uppervp;
89565963Spendry 
89665963Spendry 		VREF(dvp);
89765963Spendry 		VOP_LOCK(dvp);
89865963Spendry 		vput(ap->a_dvp);
89965963Spendry 		VREF(vp);
90065963Spendry 		VOP_LOCK(vp);
90165963Spendry 		vput(ap->a_vp);
90265963Spendry 
90365963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
90466027Spendry 		if (!error)
90566027Spendry 			union_removed_upper(un);
90666027Spendry 
90766027Spendry 		/*
90866027Spendry 		 * XXX: should create a whiteout here
90966027Spendry 		 */
91065963Spendry 	} else {
91165963Spendry 		/*
91265963Spendry 		 * XXX: should create a whiteout here
91365963Spendry 		 */
91465963Spendry 		vput(ap->a_dvp);
91565963Spendry 		vput(ap->a_vp);
91665963Spendry 		error = EROFS;
91765963Spendry 	}
91865963Spendry 
91965963Spendry 	return (error);
92065963Spendry }
92165963Spendry 
92265963Spendry int
92365963Spendry union_symlink(ap)
92465963Spendry 	struct vop_symlink_args /* {
92565963Spendry 		struct vnode *a_dvp;
92665963Spendry 		struct vnode **a_vpp;
92765963Spendry 		struct componentname *a_cnp;
92865963Spendry 		struct vattr *a_vap;
92965963Spendry 		char *a_target;
93065963Spendry 	} */ *ap;
93165963Spendry {
93265963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
93365963Spendry 	struct vnode *dvp = un->un_uppervp;
93465963Spendry 
93565963Spendry 	if (dvp) {
93665963Spendry 		int error;
93765963Spendry 		struct vnode *vp;
93865963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
93965963Spendry 
94065963Spendry 		VREF(dvp);
94165963Spendry 		VOP_LOCK(dvp);
94265963Spendry 		vput(ap->a_dvp);
94365963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
94465963Spendry 					ap->a_vap, ap->a_target);
94565997Spendry 		*ap->a_vpp = NULLVP;
94665963Spendry 		return (error);
94765963Spendry 	}
94865963Spendry 
94965963Spendry 	vput(ap->a_dvp);
95065963Spendry 	return (EROFS);
95165963Spendry }
95265963Spendry 
95365935Spendry /*
95465935Spendry  * union_readdir works in concert with getdirentries and
95565935Spendry  * readdir(3) to provide a list of entries in the unioned
95665935Spendry  * directories.  getdirentries is responsible for walking
95765935Spendry  * down the union stack.  readdir(3) is responsible for
95865935Spendry  * eliminating duplicate names from the returned data stream.
95965935Spendry  */
96065935Spendry int
96165935Spendry union_readdir(ap)
96265935Spendry 	struct vop_readdir_args /* {
96365935Spendry 		struct vnodeop_desc *a_desc;
96465935Spendry 		struct vnode *a_vp;
96565935Spendry 		struct uio *a_uio;
96665935Spendry 		struct ucred *a_cred;
96765935Spendry 	} */ *ap;
96865935Spendry {
96965963Spendry 	int error = 0;
97065935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
97165935Spendry 
97265963Spendry 	if (un->un_uppervp) {
97366028Spendry 		struct vnode *vp = un->un_uppervp;
97465935Spendry 
97565963Spendry 		VOP_LOCK(vp);
97665963Spendry 		error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
97765963Spendry 		VOP_UNLOCK(vp);
97865963Spendry 	}
97965963Spendry 
98065963Spendry 	return (error);
98165935Spendry }
98265935Spendry 
98365935Spendry int
98465963Spendry union_readlink(ap)
98565963Spendry 	struct vop_readlink_args /* {
98665963Spendry 		struct vnode *a_vp;
98765963Spendry 		struct uio *a_uio;
98865963Spendry 		struct ucred *a_cred;
98965963Spendry 	} */ *ap;
99065963Spendry {
99165963Spendry 	int error;
99265963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
99365963Spendry 
99465963Spendry 	VOP_LOCK(vp);
99565963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
99665963Spendry 	VOP_UNLOCK(vp);
99765963Spendry 
99865963Spendry 	return (error);
99965963Spendry }
100065963Spendry 
100165963Spendry int
100265963Spendry union_abortop(ap)
100365963Spendry 	struct vop_abortop_args /* {
100465963Spendry 		struct vnode *a_dvp;
100565963Spendry 		struct componentname *a_cnp;
100665963Spendry 	} */ *ap;
100765963Spendry {
100865963Spendry 	int error;
100965965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
101065963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
101165963Spendry 	int islocked = un->un_flags & UN_LOCKED;
101265963Spendry 
101365963Spendry 	if (islocked)
101465963Spendry 		VOP_LOCK(vp);
101565963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
101665963Spendry 	if (islocked)
101765963Spendry 		VOP_UNLOCK(vp);
101865963Spendry 
101965963Spendry 	return (error);
102065963Spendry }
102165963Spendry 
102265963Spendry int
102365935Spendry union_inactive(ap)
102465935Spendry 	struct vop_inactive_args /* {
102565935Spendry 		struct vnode *a_vp;
102665935Spendry 	} */ *ap;
102765935Spendry {
102865935Spendry 
102965935Spendry 	/*
103065935Spendry 	 * Do nothing (and _don't_ bypass).
103165935Spendry 	 * Wait to vrele lowervp until reclaim,
103265935Spendry 	 * so that until then our union_node is in the
103365935Spendry 	 * cache and reusable.
103465935Spendry 	 *
103565935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
103665935Spendry 	 * the lowervp and then trying to reactivate it
103765935Spendry 	 * with capabilities (v_id)
103865935Spendry 	 * like they do in the name lookup cache code.
103965935Spendry 	 * That's too much work for now.
104065935Spendry 	 */
104165989Spendry 
104266027Spendry #ifdef UNION_DIAGNOSTIC
104365989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
104465989Spendry 
104565989Spendry 	if (un->un_flags & UN_LOCKED)
104665989Spendry 		panic("union: inactivating locked node");
104765989Spendry #endif
104865989Spendry 
104965935Spendry 	return (0);
105065935Spendry }
105165935Spendry 
105265935Spendry int
105365935Spendry union_reclaim(ap)
105465935Spendry 	struct vop_reclaim_args /* {
105565935Spendry 		struct vnode *a_vp;
105665935Spendry 	} */ *ap;
105765935Spendry {
105865935Spendry 	struct vnode *vp = ap->a_vp;
105965935Spendry 	struct union_node *un = VTOUNION(vp);
106065935Spendry 	struct vnode *uppervp = un->un_uppervp;
106165935Spendry 	struct vnode *lowervp = un->un_lowervp;
106265935Spendry 	struct vnode *dirvp = un->un_dirvp;
106365935Spendry 	char *path = un->un_path;
106465935Spendry 
106565935Spendry 	/*
106665935Spendry 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
106765935Spendry 	 * so we can't call VOPs on ourself.
106865935Spendry 	 */
106965935Spendry 	/* After this assignment, this node will not be re-used. */
107065997Spendry 	un->un_uppervp = NULLVP;
107165997Spendry 	un->un_lowervp = NULLVP;
107265997Spendry 	un->un_dirvp = NULLVP;
107365935Spendry 	un->un_path = NULL;
107465935Spendry 	union_freevp(vp);
107565935Spendry 	if (uppervp)
107665935Spendry 		vrele(uppervp);
107765935Spendry 	if (lowervp)
107865935Spendry 		vrele(lowervp);
107965935Spendry 	if (dirvp)
108065935Spendry 		vrele(dirvp);
108165935Spendry 	if (path)
108265935Spendry 		free(path, M_TEMP);
108365935Spendry 	return (0);
108465935Spendry }
108565935Spendry 
108665963Spendry int
108765963Spendry union_lock(ap)
108865963Spendry 	struct vop_lock_args *ap;
108965963Spendry {
109065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
109165935Spendry 
109265965Spendry 	while (un->un_flags & UN_LOCKED) {
109365963Spendry #ifdef DIAGNOSTIC
109465989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
109565989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
109665989Spendry 			panic("union: locking against myself");
109765963Spendry #endif
109865963Spendry 		un->un_flags |= UN_WANT;
109965963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
110065963Spendry 	}
110165963Spendry 	un->un_flags |= UN_LOCKED;
110265989Spendry 
110365963Spendry #ifdef DIAGNOSTIC
110465989Spendry 	if (curproc)
110565989Spendry 		un->un_pid = curproc->p_pid;
110665989Spendry 	else
110765989Spendry 		un->un_pid = -1;
110865963Spendry #endif
110966028Spendry 
111066028Spendry 	return (0);
111165963Spendry }
111265963Spendry 
111365935Spendry int
111465963Spendry union_unlock(ap)
111565963Spendry 	struct vop_lock_args *ap;
111665963Spendry {
111765963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
111865963Spendry 
111965963Spendry #ifdef DIAGNOSTIC
112065965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
112165965Spendry 		panic("union: unlock unlocked node");
112265989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
112365989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
112465963Spendry 		panic("union: unlocking other process's union node");
112565963Spendry #endif
112665963Spendry 
112765963Spendry 	un->un_flags &= ~UN_LOCKED;
112865963Spendry 	if (un->un_flags & UN_WANT) {
112965963Spendry 		un->un_flags &= ~UN_WANT;
113065963Spendry 		wakeup((caddr_t) &un->un_flags);
113165963Spendry 	}
113265963Spendry 
113365963Spendry #ifdef DIAGNOSTIC
113465963Spendry 	un->un_pid = 0;
113565963Spendry #endif
113666028Spendry 
113766028Spendry 	return (0);
113865963Spendry }
113965963Spendry 
114065963Spendry int
114165963Spendry union_bmap(ap)
114265963Spendry 	struct vop_bmap_args /* {
114365963Spendry 		struct vnode *a_vp;
114465963Spendry 		daddr_t  a_bn;
114565963Spendry 		struct vnode **a_vpp;
114665963Spendry 		daddr_t *a_bnp;
114765963Spendry 		int *a_runp;
114865963Spendry 	} */ *ap;
114965963Spendry {
115065963Spendry 	int error;
115165963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
115265963Spendry 
115365963Spendry 	VOP_LOCK(vp);
115465963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
115565963Spendry 	VOP_UNLOCK(vp);
115665963Spendry 
115765963Spendry 	return (error);
115865963Spendry }
115965963Spendry 
116065963Spendry int
116165935Spendry union_print(ap)
116265935Spendry 	struct vop_print_args /* {
116365935Spendry 		struct vnode *a_vp;
116465935Spendry 	} */ *ap;
116565935Spendry {
116665935Spendry 	struct vnode *vp = ap->a_vp;
116765935Spendry 
116865935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
116965935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
117065935Spendry 	return (0);
117165935Spendry }
117265935Spendry 
117365963Spendry int
117465963Spendry union_islocked(ap)
117565963Spendry 	struct vop_islocked_args /* {
117665963Spendry 		struct vnode *a_vp;
117765963Spendry 	} */ *ap;
117865963Spendry {
117965935Spendry 
118065963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
118165963Spendry }
118265963Spendry 
118365935Spendry int
118465963Spendry union_pathconf(ap)
118565963Spendry 	struct vop_pathconf_args /* {
118665963Spendry 		struct vnode *a_vp;
118765963Spendry 		int a_name;
118865963Spendry 		int *a_retval;
118965935Spendry 	} */ *ap;
119065935Spendry {
119165935Spendry 	int error;
119265963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
119365935Spendry 
119465963Spendry 	VOP_LOCK(vp);
119565963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
119665963Spendry 	VOP_UNLOCK(vp);
119765935Spendry 
119865963Spendry 	return (error);
119965963Spendry }
120065935Spendry 
120165963Spendry int
120265963Spendry union_advlock(ap)
120365963Spendry 	struct vop_advlock_args /* {
120465963Spendry 		struct vnode *a_vp;
120565963Spendry 		caddr_t  a_id;
120665963Spendry 		int  a_op;
120765963Spendry 		struct flock *a_fl;
120865963Spendry 		int  a_flags;
120965963Spendry 	} */ *ap;
121065963Spendry {
121165935Spendry 
121265963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
121365963Spendry 				ap->a_fl, ap->a_flags));
121465935Spendry }
121565935Spendry 
121665935Spendry 
121765935Spendry /*
121865963Spendry  * XXX - vop_strategy must be hand coded because it has no
121965935Spendry  * vnode in its arguments.
122065935Spendry  * This goes away with a merged VM/buffer cache.
122165935Spendry  */
122265935Spendry int
122365963Spendry union_strategy(ap)
122465963Spendry 	struct vop_strategy_args /* {
122565935Spendry 		struct buf *a_bp;
122665935Spendry 	} */ *ap;
122765935Spendry {
122865935Spendry 	struct buf *bp = ap->a_bp;
122965935Spendry 	int error;
123065935Spendry 	struct vnode *savedvp;
123165935Spendry 
123265935Spendry 	savedvp = bp->b_vp;
123365963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
123465935Spendry 
123565935Spendry #ifdef DIAGNOSTIC
123665997Spendry 	if (bp->b_vp == NULLVP)
123765963Spendry 		panic("union_strategy: nil vp");
123865963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
123965963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
124065963Spendry 		panic("union_strategy: writing to lowervp");
124165935Spendry #endif
124265935Spendry 
124365963Spendry 	error = VOP_STRATEGY(bp);
124465935Spendry 	bp->b_vp = savedvp;
124565935Spendry 
124665935Spendry 	return (error);
124765935Spendry }
124865935Spendry 
124965935Spendry /*
125065935Spendry  * Global vfs data structures
125165935Spendry  */
125265935Spendry int (**union_vnodeop_p)();
125365965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
125465963Spendry 	{ &vop_default_desc, vn_default_error },
125565963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
125665963Spendry 	{ &vop_create_desc, union_create },		/* create */
125765963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
125865963Spendry 	{ &vop_open_desc, union_open },			/* open */
125965963Spendry 	{ &vop_close_desc, union_close },		/* close */
126065963Spendry 	{ &vop_access_desc, union_access },		/* access */
126165963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
126265963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
126365963Spendry 	{ &vop_read_desc, union_read },			/* read */
126465963Spendry 	{ &vop_write_desc, union_write },		/* write */
126565963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
126665963Spendry 	{ &vop_select_desc, union_select },		/* select */
126765963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
126865963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
126965963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
127065963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
127165963Spendry 	{ &vop_link_desc, union_link },			/* link */
127265963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
127365963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
127465963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
127565963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
127665963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
127765963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
127865963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
127965963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
128065963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
128165963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
128265963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
128365963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
128465963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
128565963Spendry 	{ &vop_print_desc, union_print },		/* print */
128665963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
128765963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
128865963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
128965963Spendry #ifdef notdef
129065963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
129165963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
129265963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
129365963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
129465963Spendry 	{ &vop_update_desc, union_update },		/* update */
129565963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
129665963Spendry #endif
129765935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
129865935Spendry };
129965935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
130065935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1301