xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 66051)
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*66051Spendry  *	@(#)union_vnops.c	2.1 (Berkeley) 02/10/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 (;;) {
4666034Spendry 			/*
4766034Spendry 			 * Don't do the NOCROSSMOUNT check
4866034Spendry 			 * at this level.  By definition,
4966034Spendry 			 * union fs deals with namespaces, not
5066034Spendry 			 * filesystems.
5166034Spendry 			 */
5266034Spendry 			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
7966034Spendry 	 * 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) &&
8366034Spendry 	       (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) {
13865997Spendry 		uerror = union_lookup1(um->um_uppervp, upperdvp,
13965997Spendry 					&uppervp, cnp);
140*66051Spendry 		/*if (uppervp == upperdvp)
141*66051Spendry 			dun->un_flags |= UN_KLOCK;*/
14265965Spendry 
14365935Spendry 		if (cnp->cn_consume != 0) {
14465935Spendry 			*ap->a_vpp = uppervp;
14565965Spendry 			if (!lockparent)
14665965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
14765935Spendry 			return (uerror);
14865935Spendry 		}
14965935Spendry 	} else {
15065935Spendry 		uerror = ENOENT;
15165935Spendry 	}
15265935Spendry 
15365935Spendry 	/*
15465935Spendry 	 * in a similar way to the upper layer, do the lookup
15565935Spendry 	 * in the lower layer.   this time, if there is some
15665935Spendry 	 * component magic going on, then vput whatever we got
15765935Spendry 	 * back from the upper layer and return the lower vnode
15865935Spendry 	 * instead.
15965935Spendry 	 */
16065935Spendry 	if (lowerdvp) {
161*66051Spendry 		int nameiop;
162*66051Spendry 
16365965Spendry 		VOP_LOCK(lowerdvp);
164*66051Spendry 
165*66051Spendry 		/*
166*66051Spendry 		 * Only do a LOOKUP on the bottom node, since
167*66051Spendry 		 * we won't be making changes to it anyway.
168*66051Spendry 		 */
169*66051Spendry 		nameiop = cnp->cn_nameiop;
170*66051Spendry 		cnp->cn_nameiop = LOOKUP;
17165997Spendry 		lerror = union_lookup1(um->um_lowervp, lowerdvp,
172*66051Spendry 				&lowervp, cnp);
173*66051Spendry 		cnp->cn_nameiop = nameiop;
174*66051Spendry 
17565989Spendry 		if (lowervp != lowerdvp)
17665989Spendry 			VOP_UNLOCK(lowerdvp);
17765965Spendry 
17865935Spendry 		if (cnp->cn_consume != 0) {
17965935Spendry 			if (uppervp) {
180*66051Spendry 				if (uppervp == upperdvp)
181*66051Spendry 					vrele(uppervp);
182*66051Spendry 				else
183*66051Spendry 					vput(uppervp);
18465997Spendry 				uppervp = NULLVP;
18565935Spendry 			}
18665935Spendry 			*ap->a_vpp = lowervp;
18765965Spendry 			if (!lockparent)
18865965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
18965935Spendry 			return (lerror);
19065935Spendry 		}
19165935Spendry 	} else {
19265935Spendry 		lerror = ENOENT;
19365935Spendry 	}
19465935Spendry 
19565965Spendry 	if (!lockparent)
19665965Spendry 		cnp->cn_flags &= ~LOCKPARENT;
19765965Spendry 
19865935Spendry 	/*
19965935Spendry 	 * at this point, we have uerror and lerror indicating
20065935Spendry 	 * possible errors with the lookups in the upper and lower
20165935Spendry 	 * layers.  additionally, uppervp and lowervp are (locked)
20265935Spendry 	 * references to existing vnodes in the upper and lower layers.
20365935Spendry 	 *
20465935Spendry 	 * there are now three cases to consider.
20565935Spendry 	 * 1. if both layers returned an error, then return whatever
20665935Spendry 	 *    error the upper layer generated.
20765935Spendry 	 *
20865935Spendry 	 * 2. if the top layer failed and the bottom layer succeeded
20965935Spendry 	 *    then two subcases occur.
21065935Spendry 	 *    a.  the bottom vnode is not a directory, in which
21165935Spendry 	 *	  case just return a new union vnode referencing
21265935Spendry 	 *	  an empty top layer and the existing bottom layer.
21365935Spendry 	 *    b.  the bottom vnode is a directory, in which case
21465935Spendry 	 *	  create a new directory in the top-level and
21565935Spendry 	 *	  continue as in case 3.
21665935Spendry 	 *
21765935Spendry 	 * 3. if the top layer succeeded then return a new union
21865935Spendry 	 *    vnode referencing whatever the new top layer and
21965935Spendry 	 *    whatever the bottom layer returned.
22065935Spendry 	 */
22165935Spendry 
22266027Spendry 	*ap->a_vpp = NULLVP;
22366027Spendry 
22465935Spendry 	/* case 1. */
22565935Spendry 	if ((uerror != 0) && (lerror != 0)) {
22665935Spendry 		return (uerror);
22765935Spendry 	}
22865935Spendry 
22965935Spendry 	/* case 2. */
23065935Spendry 	if (uerror != 0 /* && (lerror == 0) */ ) {
23165935Spendry 		if (lowervp->v_type == VDIR) { /* case 2b. */
232*66051Spendry 			dun->un_flags &= ~UN_ULOCK;
233*66051Spendry 			VOP_UNLOCK(upperdvp);
23465997Spendry 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
235*66051Spendry 			VOP_LOCK(upperdvp);
236*66051Spendry 			dun->un_flags |= UN_ULOCK;
237*66051Spendry 
23865935Spendry 			if (uerror) {
23965935Spendry 				if (lowervp) {
24065935Spendry 					vput(lowervp);
24165997Spendry 					lowervp = NULLVP;
24265935Spendry 				}
24365935Spendry 				return (uerror);
24465935Spendry 			}
24565935Spendry 		}
24665935Spendry 	}
24765935Spendry 
24865965Spendry 	if (lowervp)
24965965Spendry 		VOP_UNLOCK(lowervp);
25065965Spendry 
25165997Spendry 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
25265997Spendry 			      uppervp, lowervp);
25365997Spendry 
25465965Spendry 	if (error) {
25565965Spendry 		if (uppervp)
256*66051Spendry 			vput(uppervp);
25765965Spendry 		if (lowervp)
25865965Spendry 			vrele(lowervp);
25965965Spendry 	} else {
26065994Spendry 		if (*ap->a_vpp != dvp)
26165994Spendry 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
26265994Spendry 				VOP_UNLOCK(dvp);
26365965Spendry 	}
26465965Spendry 
26565965Spendry 	return (error);
26665935Spendry }
26765935Spendry 
26865963Spendry int
26965963Spendry union_create(ap)
27065963Spendry 	struct vop_create_args /* {
27165963Spendry 		struct vnode *a_dvp;
27265963Spendry 		struct vnode **a_vpp;
27365963Spendry 		struct componentname *a_cnp;
27465963Spendry 		struct vattr *a_vap;
27565963Spendry 	} */ *ap;
27665963Spendry {
27765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
27865963Spendry 	struct vnode *dvp = un->un_uppervp;
27965963Spendry 
28065963Spendry 	if (dvp) {
28165963Spendry 		int error;
28265963Spendry 		struct vnode *vp;
28365963Spendry 
28465963Spendry 		VREF(dvp);
285*66051Spendry 		un->un_flags |= UN_KLOCK;
28665963Spendry 		vput(ap->a_dvp);
28765963Spendry 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
28865963Spendry 		if (error)
28965963Spendry 			return (error);
29065963Spendry 
29165963Spendry 		error = union_allocvp(
29265963Spendry 				ap->a_vpp,
29365989Spendry 				ap->a_dvp->v_mount,
29465989Spendry 				ap->a_dvp,
29565965Spendry 				NULLVP,
29665963Spendry 				ap->a_cnp,
29765963Spendry 				vp,
29865963Spendry 				NULLVP);
29965965Spendry 		if (error)
300*66051Spendry 			vput(vp);
30165963Spendry 		return (error);
30265963Spendry 	}
30365963Spendry 
30465963Spendry 	vput(ap->a_dvp);
30565963Spendry 	return (EROFS);
30665963Spendry }
30765963Spendry 
30865963Spendry int
30965963Spendry union_mknod(ap)
31065963Spendry 	struct vop_mknod_args /* {
31165963Spendry 		struct vnode *a_dvp;
31265963Spendry 		struct vnode **a_vpp;
31365963Spendry 		struct componentname *a_cnp;
31465963Spendry 		struct vattr *a_vap;
31565963Spendry 	} */ *ap;
31665963Spendry {
31765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
31865963Spendry 	struct vnode *dvp = un->un_uppervp;
31965963Spendry 
32065963Spendry 	if (dvp) {
32165963Spendry 		int error;
32265963Spendry 		struct vnode *vp;
32365963Spendry 
32465963Spendry 		VREF(dvp);
325*66051Spendry 		un->un_flags |= UN_KLOCK;
32665963Spendry 		vput(ap->a_dvp);
32765963Spendry 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
32865963Spendry 		if (error)
32965963Spendry 			return (error);
33065963Spendry 
33165965Spendry 		if (vp) {
33265965Spendry 			error = union_allocvp(
33365965Spendry 					ap->a_vpp,
33465989Spendry 					ap->a_dvp->v_mount,
33565989Spendry 					ap->a_dvp,
33665965Spendry 					NULLVP,
33765965Spendry 					ap->a_cnp,
33865965Spendry 					vp,
33965965Spendry 					NULLVP);
34065965Spendry 			if (error)
341*66051Spendry 				vput(vp);
34265965Spendry 		}
34365963Spendry 		return (error);
34465963Spendry 	}
34565963Spendry 
34665963Spendry 	vput(ap->a_dvp);
34765963Spendry 	return (EROFS);
34865963Spendry }
34965963Spendry 
35065935Spendry int
35165935Spendry union_open(ap)
35265935Spendry 	struct vop_open_args /* {
35365935Spendry 		struct vnodeop_desc *a_desc;
35465935Spendry 		struct vnode *a_vp;
35565935Spendry 		int a_mode;
35665935Spendry 		struct ucred *a_cred;
35765935Spendry 		struct proc *a_p;
35865935Spendry 	} */ *ap;
35965935Spendry {
36065935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
36165965Spendry 	struct vnode *tvp;
36265935Spendry 	int mode = ap->a_mode;
36365935Spendry 	struct ucred *cred = ap->a_cred;
36465935Spendry 	struct proc *p = ap->a_p;
36565965Spendry 	int error;
36665935Spendry 
36765935Spendry 	/*
36865935Spendry 	 * If there is an existing upper vp then simply open that.
36965935Spendry 	 */
37065965Spendry 	tvp = un->un_uppervp;
37165965Spendry 	if (tvp == NULLVP) {
37265935Spendry 		/*
37365965Spendry 		 * If the lower vnode is being opened for writing, then
37465965Spendry 		 * copy the file contents to the upper vnode and open that,
37565965Spendry 		 * otherwise can simply open the lower vnode.
37665935Spendry 		 */
37765965Spendry 		tvp = un->un_lowervp;
37865965Spendry 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
37965994Spendry 			struct vnode *vp;
38065997Spendry 			int i;
38165963Spendry 
38265965Spendry 			/*
38365965Spendry 			 * Open the named file in the upper layer.  Note that
38465965Spendry 			 * the file may have come into existence *since* the
38565965Spendry 			 * lookup was done, since the upper layer may really
38665965Spendry 			 * be a loopback mount of some other filesystem...
38765965Spendry 			 * so open the file with exclusive create and barf if
38865965Spendry 			 * it already exists.
38966027Spendry 			 * XXX - perhaps should re-lookup the node (once more
39065965Spendry 			 * with feeling) and simply open that.  Who knows.
39165965Spendry 			 */
39265997Spendry 			error = union_vn_create(&vp, un, p);
39365965Spendry 			if (error)
39465965Spendry 				return (error);
395*66051Spendry 
396*66051Spendry 			/* at this point, uppervp is locked */
39765994Spendry 			un->un_uppervp = vp;	/* XXX */
398*66051Spendry 			un->un_flags |= UN_ULOCK;
39965965Spendry 
40065965Spendry 			/*
40165965Spendry 			 * Now, if the file is being opened with truncation,
40265965Spendry 			 * then the (new) upper vnode is ready to fly,
40365965Spendry 			 * otherwise the data from the lower vnode must be
40465965Spendry 			 * copied to the upper layer first.  This only works
40565965Spendry 			 * for regular files (check is made above).
40665965Spendry 			 */
40765965Spendry 			if ((mode & O_TRUNC) == 0) {
40865965Spendry 				/*
40965965Spendry 				 * XXX - should not ignore errors
41065965Spendry 				 * from VOP_CLOSE
41165965Spendry 				 */
41265994Spendry 				VOP_LOCK(tvp);
41365965Spendry 				error = VOP_OPEN(tvp, FREAD, cred, p);
41465965Spendry 				if (error == 0) {
41565965Spendry 					error = union_copyfile(p, cred,
41665965Spendry 						       tvp, un->un_uppervp);
41765965Spendry 					VOP_UNLOCK(tvp);
41865965Spendry 					(void) VOP_CLOSE(tvp, FREAD);
41965965Spendry 				} else {
42065965Spendry 					VOP_UNLOCK(tvp);
42165965Spendry 				}
422*66051Spendry 
423*66051Spendry 				un->un_flags &= ~UN_ULOCK;
42465965Spendry 				VOP_UNLOCK(un->un_uppervp);
42566028Spendry 				union_vn_close(un->un_uppervp, FWRITE, cred, p);
42665965Spendry 				VOP_LOCK(un->un_uppervp);
427*66051Spendry 				un->un_flags |= UN_ULOCK;
428*66051Spendry 
42965997Spendry 				if (!error)
43066027Spendry 					uprintf("union: copied up %s\n",
43165997Spendry 								un->un_path);
43265935Spendry 			}
43365997Spendry 
43465997Spendry 			/*
43565997Spendry 			 * Subsequent IOs will go to the top layer, so
43665997Spendry 			 * call close on the lower vnode and open on the
43765997Spendry 			 * upper vnode to ensure that the filesystem keeps
43865997Spendry 			 * its references counts right.  This doesn't do
43965997Spendry 			 * the right thing with (cred) and (FREAD) though.
44065997Spendry 			 * Ignoring error returns is not righ, either.
44165997Spendry 			 */
44266027Spendry 			for (i = 0; i < un->un_openl; i++) {
44365997Spendry 				(void) VOP_CLOSE(tvp, FREAD);
44465997Spendry 				(void) VOP_OPEN(un->un_uppervp, FREAD, cred, p);
44565997Spendry 			}
44666027Spendry 			un->un_openl = 0;
44765997Spendry 
44865965Spendry 			if (error == 0)
44965965Spendry 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
45065965Spendry 			return (error);
45165935Spendry 		}
452*66051Spendry 
453*66051Spendry 		/*
454*66051Spendry 		 * Just open the lower vnode
455*66051Spendry 		 */
45666027Spendry 		un->un_openl++;
457*66051Spendry 		VOP_LOCK(tvp);
458*66051Spendry 		error = VOP_OPEN(tvp, mode, cred, p);
459*66051Spendry 		VOP_UNLOCK(tvp);
460*66051Spendry 
461*66051Spendry 		return (error);
46265935Spendry 	}
46365935Spendry 
46465965Spendry 	error = VOP_OPEN(tvp, mode, cred, p);
46565965Spendry 
46665965Spendry 	return (error);
46765935Spendry }
46865935Spendry 
46965963Spendry int
47065963Spendry union_close(ap)
47165963Spendry 	struct vop_close_args /* {
47265963Spendry 		struct vnode *a_vp;
47365963Spendry 		int  a_fflag;
47465963Spendry 		struct ucred *a_cred;
47565963Spendry 		struct proc *a_p;
47665963Spendry 	} */ *ap;
47765963Spendry {
47865997Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
47965997Spendry 	struct vnode *vp;
48065963Spendry 
48165997Spendry 	if (un->un_uppervp) {
48265997Spendry 		vp = un->un_uppervp;
48365997Spendry 	} else {
48466027Spendry #ifdef UNION_DIAGNOSTIC
48566027Spendry 		if (un->un_openl <= 0)
48666027Spendry 			panic("union: un_openl cnt");
48765997Spendry #endif
48866027Spendry 		--un->un_openl;
48965997Spendry 		vp = un->un_lowervp;
49065997Spendry 	}
49166027Spendry 
49265997Spendry 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
49365963Spendry }
49465963Spendry 
49565935Spendry /*
49665963Spendry  * Check access permission on the union vnode.
49765963Spendry  * The access check being enforced is to check
49865963Spendry  * against both the underlying vnode, and any
49965963Spendry  * copied vnode.  This ensures that no additional
50065963Spendry  * file permissions are given away simply because
50165963Spendry  * the user caused an implicit file copy.
50265963Spendry  */
50365963Spendry int
50465963Spendry union_access(ap)
50565963Spendry 	struct vop_access_args /* {
50665963Spendry 		struct vnodeop_desc *a_desc;
50765963Spendry 		struct vnode *a_vp;
50865963Spendry 		int a_mode;
50965963Spendry 		struct ucred *a_cred;
51065963Spendry 		struct proc *a_p;
51165963Spendry 	} */ *ap;
51265963Spendry {
51365963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
51465965Spendry 	int error = 0;
51565963Spendry 	struct vnode *vp;
51665963Spendry 
51765963Spendry 	if (vp = un->un_lowervp) {
51865965Spendry 		VOP_LOCK(vp);
51965963Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
52065965Spendry 		VOP_UNLOCK(vp);
52165963Spendry 		if (error)
52265963Spendry 			return (error);
52365963Spendry 	}
52465963Spendry 
525*66051Spendry 	if (vp = un->un_uppervp)
52665965Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
52765965Spendry 
52865965Spendry 	return (error);
52965963Spendry }
53065963Spendry 
53165963Spendry /*
53265935Spendry  *  We handle getattr only to change the fsid.
53365935Spendry  */
53465935Spendry int
53565935Spendry union_getattr(ap)
53665935Spendry 	struct vop_getattr_args /* {
53765935Spendry 		struct vnode *a_vp;
53865935Spendry 		struct vattr *a_vap;
53965935Spendry 		struct ucred *a_cred;
54065935Spendry 		struct proc *a_p;
54165935Spendry 	} */ *ap;
54265935Spendry {
54365935Spendry 	int error;
54465965Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
545*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
54665935Spendry 
547*66051Spendry 	if (dolock)
548*66051Spendry 		VOP_LOCK(vp);
54965965Spendry 	error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
550*66051Spendry 	if (dolock)
551*66051Spendry 		VOP_UNLOCK(vp);
55265965Spendry 
55365935Spendry 	/* Requires that arguments be restored. */
55465935Spendry 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
55565935Spendry 	return (0);
55665935Spendry }
55765935Spendry 
55865963Spendry int
55965965Spendry union_setattr(ap)
56065963Spendry 	struct vop_setattr_args /* {
56165963Spendry 		struct vnode *a_vp;
56265963Spendry 		struct vattr *a_vap;
56365963Spendry 		struct ucred *a_cred;
56465963Spendry 		struct proc *a_p;
56565963Spendry 	} */ *ap;
56665963Spendry {
56765963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
56865963Spendry 	int error;
56965963Spendry 
57065963Spendry 	if (un->un_uppervp) {
57165963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
57265963Spendry 					ap->a_cred, ap->a_p);
57365963Spendry 	} else {
57465963Spendry 		/*
57565963Spendry 		 * XXX should do a copyfile (perhaps only if
57665963Spendry 		 * the file permission change, which would not
57765963Spendry 		 * track va_ctime correctly).
57865963Spendry 		 */
57965963Spendry 		error = EROFS;
58065963Spendry 	}
58165963Spendry 
58265963Spendry 	return (error);
58365963Spendry }
58465963Spendry 
58565963Spendry int
58665963Spendry union_read(ap)
58765963Spendry 	struct vop_read_args /* {
58865963Spendry 		struct vnode *a_vp;
58965963Spendry 		struct uio *a_uio;
59065963Spendry 		int  a_ioflag;
59165963Spendry 		struct ucred *a_cred;
59265963Spendry 	} */ *ap;
59365963Spendry {
59465963Spendry 	int error;
59565963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
596*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
59765963Spendry 
598*66051Spendry 	if (dolock)
599*66051Spendry 		VOP_LOCK(vp);
60065963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
601*66051Spendry 	if (dolock)
602*66051Spendry 		VOP_UNLOCK(vp);
60365963Spendry 
60465963Spendry 	return (error);
60565963Spendry }
60665963Spendry 
60765963Spendry int
60865963Spendry union_write(ap)
60965963Spendry 	struct vop_read_args /* {
61065963Spendry 		struct vnode *a_vp;
61165963Spendry 		struct uio *a_uio;
61265963Spendry 		int  a_ioflag;
61365963Spendry 		struct ucred *a_cred;
61465963Spendry 	} */ *ap;
61565963Spendry {
61665963Spendry 	int error;
61765963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
618*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
61965963Spendry 
620*66051Spendry 	if (dolock)
621*66051Spendry 		VOP_LOCK(vp);
62265963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
623*66051Spendry 	if (dolock)
624*66051Spendry 		VOP_UNLOCK(vp);
62565963Spendry 
62665963Spendry 	return (error);
62765963Spendry }
62865963Spendry 
62965963Spendry int
63065963Spendry union_ioctl(ap)
63165963Spendry 	struct vop_ioctl_args /* {
63265963Spendry 		struct vnode *a_vp;
63365963Spendry 		int  a_command;
63465963Spendry 		caddr_t  a_data;
63565963Spendry 		int  a_fflag;
63665963Spendry 		struct ucred *a_cred;
63765963Spendry 		struct proc *a_p;
63865963Spendry 	} */ *ap;
63965963Spendry {
64065963Spendry 
64165963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
64265963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
64365963Spendry }
64465963Spendry 
64565963Spendry int
64665963Spendry union_select(ap)
64765963Spendry 	struct vop_select_args /* {
64865963Spendry 		struct vnode *a_vp;
64965963Spendry 		int  a_which;
65065963Spendry 		int  a_fflags;
65165963Spendry 		struct ucred *a_cred;
65265963Spendry 		struct proc *a_p;
65365963Spendry 	} */ *ap;
65465963Spendry {
65565963Spendry 
65665963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
65765963Spendry 				ap->a_cred, ap->a_p));
65865963Spendry }
65965963Spendry 
66065963Spendry int
66165963Spendry union_mmap(ap)
66265963Spendry 	struct vop_mmap_args /* {
66365963Spendry 		struct vnode *a_vp;
66465963Spendry 		int  a_fflags;
66565963Spendry 		struct ucred *a_cred;
66665963Spendry 		struct proc *a_p;
66765963Spendry 	} */ *ap;
66865963Spendry {
66965963Spendry 
67065963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
67165963Spendry 				ap->a_cred, ap->a_p));
67265963Spendry }
67365963Spendry 
67465963Spendry int
67565963Spendry union_fsync(ap)
67665963Spendry 	struct vop_fsync_args /* {
67765963Spendry 		struct vnode *a_vp;
67865963Spendry 		struct ucred *a_cred;
67965963Spendry 		int  a_waitfor;
68065963Spendry 		struct proc *a_p;
68165963Spendry 	} */ *ap;
68265963Spendry {
68365963Spendry 	int error = 0;
68465963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
68565963Spendry 
68665963Spendry 	if (targetvp) {
687*66051Spendry 		int dolock = (targetvp == LOWERVP(ap->a_vp));
688*66051Spendry 
689*66051Spendry 		if (dolock)
690*66051Spendry 			VOP_LOCK(targetvp);
69165963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
69265963Spendry 					ap->a_waitfor, ap->a_p);
693*66051Spendry 		if (dolock)
694*66051Spendry 			VOP_UNLOCK(targetvp);
69565963Spendry 	}
69665963Spendry 
69765963Spendry 	return (error);
69865963Spendry }
69965963Spendry 
70065963Spendry int
70165963Spendry union_seek(ap)
70265963Spendry 	struct vop_seek_args /* {
70365963Spendry 		struct vnode *a_vp;
70465963Spendry 		off_t  a_oldoff;
70565963Spendry 		off_t  a_newoff;
70665963Spendry 		struct ucred *a_cred;
70765963Spendry 	} */ *ap;
70865963Spendry {
70965963Spendry 
71065963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
71165963Spendry }
71265963Spendry 
71365963Spendry int
71465963Spendry union_remove(ap)
71565963Spendry 	struct vop_remove_args /* {
71665963Spendry 		struct vnode *a_dvp;
71765963Spendry 		struct vnode *a_vp;
71865963Spendry 		struct componentname *a_cnp;
71965963Spendry 	} */ *ap;
72065963Spendry {
72165963Spendry 	int error;
72265963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
72365963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
72465963Spendry 
72565963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
72665963Spendry 		struct vnode *dvp = dun->un_uppervp;
72765963Spendry 		struct vnode *vp = un->un_uppervp;
72865963Spendry 
72965963Spendry 		VREF(dvp);
730*66051Spendry 		dun->un_flags |= UN_KLOCK;
73165963Spendry 		vput(ap->a_dvp);
73265963Spendry 		VREF(vp);
733*66051Spendry 		un->un_flags |= UN_KLOCK;
73465963Spendry 		vput(ap->a_vp);
73565963Spendry 
73665963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
73766027Spendry 		if (!error)
73866027Spendry 			union_removed_upper(un);
73966027Spendry 
74066027Spendry 		/*
74166027Spendry 		 * XXX: should create a whiteout here
74266027Spendry 		 */
74365963Spendry 	} else {
74465963Spendry 		/*
74565963Spendry 		 * XXX: should create a whiteout here
74665963Spendry 		 */
74765963Spendry 		vput(ap->a_dvp);
74865963Spendry 		vput(ap->a_vp);
74965963Spendry 		error = EROFS;
75065963Spendry 	}
75165963Spendry 
75265963Spendry 	return (error);
75365963Spendry }
75465963Spendry 
75565963Spendry int
75665963Spendry union_link(ap)
75765963Spendry 	struct vop_link_args /* {
75865963Spendry 		struct vnode *a_vp;
75965963Spendry 		struct vnode *a_tdvp;
76065963Spendry 		struct componentname *a_cnp;
76165963Spendry 	} */ *ap;
76265963Spendry {
76365963Spendry 	int error;
76465963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
76565963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
76665963Spendry 
76765963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
76865963Spendry 		struct vnode *dvp = dun->un_uppervp;
76965963Spendry 		struct vnode *vp = un->un_uppervp;
77065963Spendry 
77165963Spendry 		VREF(dvp);
772*66051Spendry 		dun->un_flags |= UN_KLOCK;
77365963Spendry 		vput(ap->a_vp);
77465963Spendry 		VREF(vp);
77565963Spendry 		vrele(ap->a_tdvp);
77665963Spendry 
77765963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
77865963Spendry 	} else {
77965963Spendry 		/*
78065963Spendry 		 * XXX: need to copy to upper layer
78165963Spendry 		 * and do the link there.
78265963Spendry 		 */
78365963Spendry 		vput(ap->a_vp);
78465963Spendry 		vrele(ap->a_tdvp);
78565963Spendry 		error = EROFS;
78665963Spendry 	}
78765963Spendry 
78865963Spendry 	return (error);
78965963Spendry }
79065963Spendry 
79165963Spendry int
79265963Spendry union_rename(ap)
79365963Spendry 	struct vop_rename_args  /* {
79465963Spendry 		struct vnode *a_fdvp;
79565963Spendry 		struct vnode *a_fvp;
79665963Spendry 		struct componentname *a_fcnp;
79765963Spendry 		struct vnode *a_tdvp;
79865963Spendry 		struct vnode *a_tvp;
79965963Spendry 		struct componentname *a_tcnp;
80065963Spendry 	} */ *ap;
80165963Spendry {
80265963Spendry 	int error;
80365963Spendry 
80465963Spendry 	struct vnode *fdvp = ap->a_fdvp;
80565963Spendry 	struct vnode *fvp = ap->a_fvp;
80665963Spendry 	struct vnode *tdvp = ap->a_tdvp;
80765963Spendry 	struct vnode *tvp = ap->a_tvp;
80865963Spendry 
80965963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
81065963Spendry 		struct union_node *un = VTOUNION(fdvp);
81165997Spendry 		if (un->un_uppervp == NULLVP) {
81265963Spendry 			error = EROFS;
81365963Spendry 			goto bad;
81465963Spendry 		}
81565963Spendry 
81665963Spendry 		fdvp = un->un_uppervp;
81765963Spendry 		VREF(fdvp);
81865963Spendry 		vrele(ap->a_fdvp);
81965963Spendry 	}
82065963Spendry 
82165963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
82265963Spendry 		struct union_node *un = VTOUNION(fvp);
82365997Spendry 		if (un->un_uppervp == NULLVP) {
82465963Spendry 			error = EROFS;
82565963Spendry 			goto bad;
82665963Spendry 		}
82765963Spendry 
82865963Spendry 		fvp = un->un_uppervp;
82965963Spendry 		VREF(fvp);
83065963Spendry 		vrele(ap->a_fvp);
83165963Spendry 	}
83265963Spendry 
83365963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
83465963Spendry 		struct union_node *un = VTOUNION(tdvp);
83565997Spendry 		if (un->un_uppervp == NULLVP) {
83665963Spendry 			error = EROFS;
83765963Spendry 			goto bad;
83865963Spendry 		}
83965963Spendry 
84065963Spendry 		tdvp = un->un_uppervp;
84165963Spendry 		VREF(tdvp);
842*66051Spendry 		un->un_flags |= UN_KLOCK;
84365997Spendry 		vput(ap->a_tdvp);
84465963Spendry 	}
84565963Spendry 
84665963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
84765963Spendry 		struct union_node *un = VTOUNION(tvp);
84865997Spendry 		if (un->un_uppervp == NULLVP) {
84965963Spendry 			error = EROFS;
85065963Spendry 			goto bad;
85165963Spendry 		}
85265963Spendry 
85365963Spendry 		tvp = un->un_uppervp;
85465963Spendry 		VREF(tvp);
855*66051Spendry 		un->un_flags |= UN_KLOCK;
85665963Spendry 		vput(ap->a_tvp);
85765963Spendry 	}
85865963Spendry 
85965963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
86065963Spendry 
86165963Spendry bad:
86265963Spendry 	vrele(fdvp);
86365963Spendry 	vrele(fvp);
86465963Spendry 	vput(tdvp);
86565963Spendry 	if (tvp)
86665963Spendry 		vput(tvp);
86765963Spendry 
86865963Spendry 	return (error);
86965963Spendry }
87065963Spendry 
87165963Spendry int
87265963Spendry union_mkdir(ap)
87365963Spendry 	struct vop_mkdir_args /* {
87465963Spendry 		struct vnode *a_dvp;
87565963Spendry 		struct vnode **a_vpp;
87665963Spendry 		struct componentname *a_cnp;
87765963Spendry 		struct vattr *a_vap;
87865963Spendry 	} */ *ap;
87965963Spendry {
88065963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
88165963Spendry 	struct vnode *dvp = un->un_uppervp;
88265963Spendry 
88365963Spendry 	if (dvp) {
88465963Spendry 		int error;
88565963Spendry 		struct vnode *vp;
88665963Spendry 
88765963Spendry 		VREF(dvp);
888*66051Spendry 		un->un_flags |= UN_KLOCK;
88965963Spendry 		vput(ap->a_dvp);
89065963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
89165963Spendry 		if (error)
89265963Spendry 			return (error);
89365963Spendry 
89465963Spendry 		error = union_allocvp(
89565963Spendry 				ap->a_vpp,
89665989Spendry 				ap->a_dvp->v_mount,
89765989Spendry 				ap->a_dvp,
89865965Spendry 				NULLVP,
89965963Spendry 				ap->a_cnp,
90065963Spendry 				vp,
90165963Spendry 				NULLVP);
90265965Spendry 		if (error)
903*66051Spendry 			vput(vp);
90465963Spendry 		return (error);
90565963Spendry 	}
90665963Spendry 
90765963Spendry 	vput(ap->a_dvp);
90865963Spendry 	return (EROFS);
90965963Spendry }
91065963Spendry 
91165963Spendry int
91265963Spendry union_rmdir(ap)
91365963Spendry 	struct vop_rmdir_args /* {
91465963Spendry 		struct vnode *a_dvp;
91565963Spendry 		struct vnode *a_vp;
91665963Spendry 		struct componentname *a_cnp;
91765963Spendry 	} */ *ap;
91865963Spendry {
91965963Spendry 	int error;
92065963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
92165963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
92265963Spendry 
92365963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
92465963Spendry 		struct vnode *dvp = dun->un_uppervp;
92565963Spendry 		struct vnode *vp = un->un_uppervp;
92665963Spendry 
92765963Spendry 		VREF(dvp);
928*66051Spendry 		dun->un_flags |= UN_KLOCK;
92965963Spendry 		vput(ap->a_dvp);
93065963Spendry 		VREF(vp);
931*66051Spendry 		un->un_flags |= UN_KLOCK;
93265963Spendry 		vput(ap->a_vp);
93365963Spendry 
934*66051Spendry 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
93566027Spendry 		if (!error)
93666027Spendry 			union_removed_upper(un);
93766027Spendry 
93866027Spendry 		/*
93966027Spendry 		 * XXX: should create a whiteout here
94066027Spendry 		 */
94165963Spendry 	} else {
94265963Spendry 		/*
94365963Spendry 		 * XXX: should create a whiteout here
94465963Spendry 		 */
94565963Spendry 		vput(ap->a_dvp);
94665963Spendry 		vput(ap->a_vp);
94765963Spendry 		error = EROFS;
94865963Spendry 	}
94965963Spendry 
95065963Spendry 	return (error);
95165963Spendry }
95265963Spendry 
95365963Spendry int
95465963Spendry union_symlink(ap)
95565963Spendry 	struct vop_symlink_args /* {
95665963Spendry 		struct vnode *a_dvp;
95765963Spendry 		struct vnode **a_vpp;
95865963Spendry 		struct componentname *a_cnp;
95965963Spendry 		struct vattr *a_vap;
96065963Spendry 		char *a_target;
96165963Spendry 	} */ *ap;
96265963Spendry {
96365963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
96465963Spendry 	struct vnode *dvp = un->un_uppervp;
96565963Spendry 
96665963Spendry 	if (dvp) {
96765963Spendry 		int error;
96865963Spendry 		struct vnode *vp;
96965963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
97065963Spendry 
97165963Spendry 		VREF(dvp);
972*66051Spendry 		un->un_flags |= UN_KLOCK;
97365963Spendry 		vput(ap->a_dvp);
97465963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
97565963Spendry 					ap->a_vap, ap->a_target);
97665997Spendry 		*ap->a_vpp = NULLVP;
97765963Spendry 		return (error);
97865963Spendry 	}
97965963Spendry 
98065963Spendry 	vput(ap->a_dvp);
98165963Spendry 	return (EROFS);
98265963Spendry }
98365963Spendry 
98465935Spendry /*
98565935Spendry  * union_readdir works in concert with getdirentries and
98665935Spendry  * readdir(3) to provide a list of entries in the unioned
98765935Spendry  * directories.  getdirentries is responsible for walking
98865935Spendry  * down the union stack.  readdir(3) is responsible for
98965935Spendry  * eliminating duplicate names from the returned data stream.
99065935Spendry  */
99165935Spendry int
99265935Spendry union_readdir(ap)
99365935Spendry 	struct vop_readdir_args /* {
99465935Spendry 		struct vnodeop_desc *a_desc;
99565935Spendry 		struct vnode *a_vp;
99665935Spendry 		struct uio *a_uio;
99765935Spendry 		struct ucred *a_cred;
99865935Spendry 	} */ *ap;
99965935Spendry {
100065963Spendry 	int error = 0;
100165935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
100265935Spendry 
1003*66051Spendry 	if (un->un_uppervp)
1004*66051Spendry 		error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred);
100565935Spendry 
100665963Spendry 	return (error);
100765935Spendry }
100865935Spendry 
100965935Spendry int
101065963Spendry union_readlink(ap)
101165963Spendry 	struct vop_readlink_args /* {
101265963Spendry 		struct vnode *a_vp;
101365963Spendry 		struct uio *a_uio;
101465963Spendry 		struct ucred *a_cred;
101565963Spendry 	} */ *ap;
101665963Spendry {
101765963Spendry 	int error;
101865963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
1019*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
102065963Spendry 
1021*66051Spendry 	if (dolock)
1022*66051Spendry 		VOP_LOCK(vp);
102365963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
1024*66051Spendry 	if (dolock)
1025*66051Spendry 		VOP_UNLOCK(vp);
102665963Spendry 
102765963Spendry 	return (error);
102865963Spendry }
102965963Spendry 
103065963Spendry int
103165963Spendry union_abortop(ap)
103265963Spendry 	struct vop_abortop_args /* {
103365963Spendry 		struct vnode *a_dvp;
103465963Spendry 		struct componentname *a_cnp;
103565963Spendry 	} */ *ap;
103665963Spendry {
103765963Spendry 	int error;
103865965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
103965963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
104065963Spendry 	int islocked = un->un_flags & UN_LOCKED;
1041*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_dvp));
104265963Spendry 
1043*66051Spendry 	if (islocked && dolock)
104465963Spendry 		VOP_LOCK(vp);
104565963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
1046*66051Spendry 	if (islocked && dolock)
104765963Spendry 		VOP_UNLOCK(vp);
104865963Spendry 
104965963Spendry 	return (error);
105065963Spendry }
105165963Spendry 
105265963Spendry int
105365935Spendry union_inactive(ap)
105465935Spendry 	struct vop_inactive_args /* {
105565935Spendry 		struct vnode *a_vp;
105665935Spendry 	} */ *ap;
105765935Spendry {
105865935Spendry 
105965935Spendry 	/*
106065935Spendry 	 * Do nothing (and _don't_ bypass).
106165935Spendry 	 * Wait to vrele lowervp until reclaim,
106265935Spendry 	 * so that until then our union_node is in the
106365935Spendry 	 * cache and reusable.
106465935Spendry 	 *
106565935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
106665935Spendry 	 * the lowervp and then trying to reactivate it
106765935Spendry 	 * with capabilities (v_id)
106865935Spendry 	 * like they do in the name lookup cache code.
106965935Spendry 	 * That's too much work for now.
107065935Spendry 	 */
107165989Spendry 
107266027Spendry #ifdef UNION_DIAGNOSTIC
107365989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
107465989Spendry 
107565989Spendry 	if (un->un_flags & UN_LOCKED)
107665989Spendry 		panic("union: inactivating locked node");
107765989Spendry #endif
107865989Spendry 
107965935Spendry 	return (0);
108065935Spendry }
108165935Spendry 
108265935Spendry int
108365935Spendry union_reclaim(ap)
108465935Spendry 	struct vop_reclaim_args /* {
108565935Spendry 		struct vnode *a_vp;
108665935Spendry 	} */ *ap;
108765935Spendry {
108865935Spendry 	struct vnode *vp = ap->a_vp;
108965935Spendry 	struct union_node *un = VTOUNION(vp);
109065935Spendry 	struct vnode *uppervp = un->un_uppervp;
109165935Spendry 	struct vnode *lowervp = un->un_lowervp;
109265935Spendry 	struct vnode *dirvp = un->un_dirvp;
109365935Spendry 	char *path = un->un_path;
109465935Spendry 
109565935Spendry 	/*
109665935Spendry 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
109765935Spendry 	 * so we can't call VOPs on ourself.
109865935Spendry 	 */
109965935Spendry 	/* After this assignment, this node will not be re-used. */
110065997Spendry 	un->un_uppervp = NULLVP;
110165997Spendry 	un->un_lowervp = NULLVP;
110265997Spendry 	un->un_dirvp = NULLVP;
110365935Spendry 	un->un_path = NULL;
110465935Spendry 	union_freevp(vp);
110565935Spendry 	if (uppervp)
110665935Spendry 		vrele(uppervp);
110765935Spendry 	if (lowervp)
110865935Spendry 		vrele(lowervp);
110965935Spendry 	if (dirvp)
111065935Spendry 		vrele(dirvp);
111165935Spendry 	if (path)
111265935Spendry 		free(path, M_TEMP);
111365935Spendry 	return (0);
111465935Spendry }
111565935Spendry 
111665963Spendry int
111765963Spendry union_lock(ap)
111865963Spendry 	struct vop_lock_args *ap;
111965963Spendry {
112065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
112165935Spendry 
1122*66051Spendry 	if (un->un_uppervp) {
1123*66051Spendry 		if ((un->un_flags & UN_ULOCK) == 0) {
1124*66051Spendry 			VOP_LOCK(un->un_uppervp);
1125*66051Spendry 			un->un_flags |= UN_ULOCK;
1126*66051Spendry 		}
1127*66051Spendry #ifdef DIAGNOSTIC
1128*66051Spendry 		if (un->un_flags & UN_KLOCK)
1129*66051Spendry 			panic("union: dangling upper lock");
1130*66051Spendry #endif
1131*66051Spendry 	}
1132*66051Spendry 
113365965Spendry 	while (un->un_flags & UN_LOCKED) {
113465963Spendry #ifdef DIAGNOSTIC
113565989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
113665989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
113765989Spendry 			panic("union: locking against myself");
113865963Spendry #endif
113965963Spendry 		un->un_flags |= UN_WANT;
114065963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
114165963Spendry 	}
114265963Spendry 	un->un_flags |= UN_LOCKED;
114365989Spendry 
114465963Spendry #ifdef DIAGNOSTIC
114565989Spendry 	if (curproc)
114665989Spendry 		un->un_pid = curproc->p_pid;
114765989Spendry 	else
114865989Spendry 		un->un_pid = -1;
114965963Spendry #endif
115066028Spendry 
115166028Spendry 	return (0);
115265963Spendry }
115365963Spendry 
115465935Spendry int
115565963Spendry union_unlock(ap)
115665963Spendry 	struct vop_lock_args *ap;
115765963Spendry {
115865963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
115965963Spendry 
116065963Spendry #ifdef DIAGNOSTIC
116165965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
116265965Spendry 		panic("union: unlock unlocked node");
116365989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
116465989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
116565963Spendry 		panic("union: unlocking other process's union node");
116665963Spendry #endif
116765963Spendry 
116865963Spendry 	un->un_flags &= ~UN_LOCKED;
1169*66051Spendry 
1170*66051Spendry 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
1171*66051Spendry 		VOP_UNLOCK(un->un_uppervp);
1172*66051Spendry 
1173*66051Spendry 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
1174*66051Spendry 
117565963Spendry 	if (un->un_flags & UN_WANT) {
117665963Spendry 		un->un_flags &= ~UN_WANT;
117765963Spendry 		wakeup((caddr_t) &un->un_flags);
117865963Spendry 	}
117965963Spendry 
118065963Spendry #ifdef DIAGNOSTIC
118165963Spendry 	un->un_pid = 0;
118265963Spendry #endif
118366028Spendry 
118466028Spendry 	return (0);
118565963Spendry }
118665963Spendry 
118765963Spendry int
118865963Spendry union_bmap(ap)
118965963Spendry 	struct vop_bmap_args /* {
119065963Spendry 		struct vnode *a_vp;
119165963Spendry 		daddr_t  a_bn;
119265963Spendry 		struct vnode **a_vpp;
119365963Spendry 		daddr_t *a_bnp;
119465963Spendry 		int *a_runp;
119565963Spendry 	} */ *ap;
119665963Spendry {
119765963Spendry 	int error;
119865963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
1199*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
120065963Spendry 
1201*66051Spendry 	if (dolock)
1202*66051Spendry 		VOP_LOCK(vp);
120365963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
1204*66051Spendry 	if (dolock)
1205*66051Spendry 		VOP_UNLOCK(vp);
120665963Spendry 
120765963Spendry 	return (error);
120865963Spendry }
120965963Spendry 
121065963Spendry int
121165935Spendry union_print(ap)
121265935Spendry 	struct vop_print_args /* {
121365935Spendry 		struct vnode *a_vp;
121465935Spendry 	} */ *ap;
121565935Spendry {
121665935Spendry 	struct vnode *vp = ap->a_vp;
121765935Spendry 
121865935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
121965935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
122065935Spendry 	return (0);
122165935Spendry }
122265935Spendry 
122365963Spendry int
122465963Spendry union_islocked(ap)
122565963Spendry 	struct vop_islocked_args /* {
122665963Spendry 		struct vnode *a_vp;
122765963Spendry 	} */ *ap;
122865963Spendry {
122965935Spendry 
123065963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
123165963Spendry }
123265963Spendry 
123365935Spendry int
123465963Spendry union_pathconf(ap)
123565963Spendry 	struct vop_pathconf_args /* {
123665963Spendry 		struct vnode *a_vp;
123765963Spendry 		int a_name;
123865963Spendry 		int *a_retval;
123965935Spendry 	} */ *ap;
124065935Spendry {
124165935Spendry 	int error;
124265963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
1243*66051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
124465935Spendry 
1245*66051Spendry 	if (dolock)
1246*66051Spendry 		VOP_LOCK(vp);
124765963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
1248*66051Spendry 	if (dolock)
1249*66051Spendry 		VOP_UNLOCK(vp);
125065935Spendry 
125165963Spendry 	return (error);
125265963Spendry }
125365935Spendry 
125465963Spendry int
125565963Spendry union_advlock(ap)
125665963Spendry 	struct vop_advlock_args /* {
125765963Spendry 		struct vnode *a_vp;
125865963Spendry 		caddr_t  a_id;
125965963Spendry 		int  a_op;
126065963Spendry 		struct flock *a_fl;
126165963Spendry 		int  a_flags;
126265963Spendry 	} */ *ap;
126365963Spendry {
126465935Spendry 
126565963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
126665963Spendry 				ap->a_fl, ap->a_flags));
126765935Spendry }
126865935Spendry 
126965935Spendry 
127065935Spendry /*
127165963Spendry  * XXX - vop_strategy must be hand coded because it has no
127265935Spendry  * vnode in its arguments.
127365935Spendry  * This goes away with a merged VM/buffer cache.
127465935Spendry  */
127565935Spendry int
127665963Spendry union_strategy(ap)
127765963Spendry 	struct vop_strategy_args /* {
127865935Spendry 		struct buf *a_bp;
127965935Spendry 	} */ *ap;
128065935Spendry {
128165935Spendry 	struct buf *bp = ap->a_bp;
128265935Spendry 	int error;
128365935Spendry 	struct vnode *savedvp;
128465935Spendry 
128565935Spendry 	savedvp = bp->b_vp;
128665963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
128765935Spendry 
128865935Spendry #ifdef DIAGNOSTIC
128965997Spendry 	if (bp->b_vp == NULLVP)
129065963Spendry 		panic("union_strategy: nil vp");
129165963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
129265963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
129365963Spendry 		panic("union_strategy: writing to lowervp");
129465935Spendry #endif
129565935Spendry 
129665963Spendry 	error = VOP_STRATEGY(bp);
129765935Spendry 	bp->b_vp = savedvp;
129865935Spendry 
129965935Spendry 	return (error);
130065935Spendry }
130165935Spendry 
130265935Spendry /*
130365935Spendry  * Global vfs data structures
130465935Spendry  */
130565935Spendry int (**union_vnodeop_p)();
130665965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
130765963Spendry 	{ &vop_default_desc, vn_default_error },
130865963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
130965963Spendry 	{ &vop_create_desc, union_create },		/* create */
131065963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
131165963Spendry 	{ &vop_open_desc, union_open },			/* open */
131265963Spendry 	{ &vop_close_desc, union_close },		/* close */
131365963Spendry 	{ &vop_access_desc, union_access },		/* access */
131465963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
131565963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
131665963Spendry 	{ &vop_read_desc, union_read },			/* read */
131765963Spendry 	{ &vop_write_desc, union_write },		/* write */
131865963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
131965963Spendry 	{ &vop_select_desc, union_select },		/* select */
132065963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
132165963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
132265963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
132365963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
132465963Spendry 	{ &vop_link_desc, union_link },			/* link */
132565963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
132665963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
132765963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
132865963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
132965963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
133065963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
133165963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
133265963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
133365963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
133465963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
133565963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
133665963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
133765963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
133865963Spendry 	{ &vop_print_desc, union_print },		/* print */
133965963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
134065963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
134165963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
134265963Spendry #ifdef notdef
134365963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
134465963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
134565963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
134665963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
134765963Spendry 	{ &vop_update_desc, union_update },		/* update */
134865963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
134965963Spendry #endif
135065935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
135165935Spendry };
135265935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
135365935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1354