xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 66057)
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*66057Spendry  *	@(#)union_vnops.c	8.3 (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>
2566053Spendry #include <sys/queue.h>
2666055Spendry #include <miscfs/union/union.h>
2765935Spendry 
2865935Spendry static int
2965989Spendry union_lookup1(udvp, dvp, vpp, cnp)
3065989Spendry 	struct vnode *udvp;
3165935Spendry 	struct vnode *dvp;
3265935Spendry 	struct vnode **vpp;
3365935Spendry 	struct componentname *cnp;
3465935Spendry {
3565935Spendry 	int error;
3665935Spendry 	struct vnode *tdvp;
3765935Spendry 	struct mount *mp;
3865935Spendry 
3965994Spendry 	/*
4065994Spendry 	 * If stepping up the directory tree, check for going
4165994Spendry 	 * back across the mount point, in which case do what
4265994Spendry 	 * lookup would do by stepping back down the mount
4365994Spendry 	 * hierarchy.
4465994Spendry 	 */
4565935Spendry 	if (cnp->cn_flags & ISDOTDOT) {
4665935Spendry 		for (;;) {
4766034Spendry 			/*
4866034Spendry 			 * Don't do the NOCROSSMOUNT check
4966034Spendry 			 * at this level.  By definition,
5066034Spendry 			 * union fs deals with namespaces, not
5166034Spendry 			 * filesystems.
5266034Spendry 			 */
5366034Spendry 			if ((dvp->v_flag & VROOT) == 0)
5465935Spendry 				break;
5565935Spendry 
5665935Spendry 			tdvp = dvp;
5765935Spendry 			dvp = dvp->v_mount->mnt_vnodecovered;
5865935Spendry 			vput(tdvp);
5965935Spendry 			VREF(dvp);
6065935Spendry 			VOP_LOCK(dvp);
6165935Spendry 		}
6265935Spendry 	}
6366027Spendry 
6465935Spendry         error = VOP_LOOKUP(dvp, &tdvp, cnp);
6565935Spendry 	if (error)
6665935Spendry 		return (error);
6765935Spendry 
6865994Spendry 	/*
6966027Spendry 	 * The parent directory will have been unlocked, unless lookup
7066027Spendry 	 * found the last component.  In which case, re-lock the node
7166027Spendry 	 * here to allow it to be unlocked again (phew) in union_lookup.
7265994Spendry 	 */
7366027Spendry 	if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
7465994Spendry 		VOP_LOCK(dvp);
7565994Spendry 
7665935Spendry 	dvp = tdvp;
7765994Spendry 
7865994Spendry 	/*
7965994Spendry 	 * Lastly check if the current node is a mount point in
8066034Spendry 	 * which case walk up the mount hierarchy making sure not to
8165994Spendry 	 * bump into the root of the mount tree (ie. dvp != udvp).
8265994Spendry 	 */
8365989Spendry 	while (dvp != udvp && (dvp->v_type == VDIR) &&
8466034Spendry 	       (mp = dvp->v_mountedhere)) {
8565935Spendry 
8665935Spendry 		if (mp->mnt_flag & MNT_MLOCK) {
8765935Spendry 			mp->mnt_flag |= MNT_MWAIT;
8865935Spendry 			sleep((caddr_t) mp, PVFS);
8965935Spendry 			continue;
9065935Spendry 		}
9165935Spendry 
9265935Spendry 		if (error = VFS_ROOT(mp, &tdvp)) {
9365935Spendry 			vput(dvp);
9465935Spendry 			return (error);
9565935Spendry 		}
9665935Spendry 
9765965Spendry 		vput(dvp);
9865935Spendry 		dvp = tdvp;
9965935Spendry 	}
10065935Spendry 
10165935Spendry 	*vpp = dvp;
10265935Spendry 	return (0);
10365935Spendry }
10465935Spendry 
10565935Spendry int
10665935Spendry union_lookup(ap)
10765935Spendry 	struct vop_lookup_args /* {
10865935Spendry 		struct vnodeop_desc *a_desc;
10965935Spendry 		struct vnode *a_dvp;
11065935Spendry 		struct vnode **a_vpp;
11165935Spendry 		struct componentname *a_cnp;
11265935Spendry 	} */ *ap;
11365935Spendry {
11465965Spendry 	int error;
11565935Spendry 	int uerror, lerror;
11665935Spendry 	struct vnode *uppervp, *lowervp;
11765935Spendry 	struct vnode *upperdvp, *lowerdvp;
11865935Spendry 	struct vnode *dvp = ap->a_dvp;
11965989Spendry 	struct union_node *dun = VTOUNION(dvp);
12065935Spendry 	struct componentname *cnp = ap->a_cnp;
12165935Spendry 	int lockparent = cnp->cn_flags & LOCKPARENT;
12265994Spendry 	int rdonly = cnp->cn_flags & RDONLY;
12365997Spendry 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
12465935Spendry 
12565965Spendry 	cnp->cn_flags |= LOCKPARENT;
12665965Spendry 
12765935Spendry 	upperdvp = dun->un_uppervp;
12865935Spendry 	lowerdvp = dun->un_lowervp;
12965997Spendry 	uppervp = NULLVP;
13065997Spendry 	lowervp = NULLVP;
13165935Spendry 
13265935Spendry 	/*
13365935Spendry 	 * do the lookup in the upper level.
13465935Spendry 	 * if that level comsumes additional pathnames,
13565935Spendry 	 * then assume that something special is going
13665935Spendry 	 * on and just return that vnode.
13765935Spendry 	 */
13865935Spendry 	if (upperdvp) {
13965997Spendry 		uerror = union_lookup1(um->um_uppervp, upperdvp,
14065997Spendry 					&uppervp, cnp);
14166051Spendry 		/*if (uppervp == upperdvp)
14266051Spendry 			dun->un_flags |= UN_KLOCK;*/
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) {
16266051Spendry 		int nameiop;
16366051Spendry 
16465965Spendry 		VOP_LOCK(lowerdvp);
16566051Spendry 
16666051Spendry 		/*
16766051Spendry 		 * Only do a LOOKUP on the bottom node, since
16866051Spendry 		 * we won't be making changes to it anyway.
16966051Spendry 		 */
17066051Spendry 		nameiop = cnp->cn_nameiop;
17166051Spendry 		cnp->cn_nameiop = LOOKUP;
17265997Spendry 		lerror = union_lookup1(um->um_lowervp, lowerdvp,
17366051Spendry 				&lowervp, cnp);
17466051Spendry 		cnp->cn_nameiop = nameiop;
17566051Spendry 
17665989Spendry 		if (lowervp != lowerdvp)
17765989Spendry 			VOP_UNLOCK(lowerdvp);
17865965Spendry 
17965935Spendry 		if (cnp->cn_consume != 0) {
18065935Spendry 			if (uppervp) {
18166051Spendry 				if (uppervp == upperdvp)
18266051Spendry 					vrele(uppervp);
18366051Spendry 				else
18466051Spendry 					vput(uppervp);
18565997Spendry 				uppervp = NULLVP;
18665935Spendry 			}
18765935Spendry 			*ap->a_vpp = lowervp;
18865965Spendry 			if (!lockparent)
18965965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
19065935Spendry 			return (lerror);
19165935Spendry 		}
19265935Spendry 	} else {
19365935Spendry 		lerror = ENOENT;
19465935Spendry 	}
19565935Spendry 
19665965Spendry 	if (!lockparent)
19765965Spendry 		cnp->cn_flags &= ~LOCKPARENT;
19865965Spendry 
19965935Spendry 	/*
20065935Spendry 	 * at this point, we have uerror and lerror indicating
20165935Spendry 	 * possible errors with the lookups in the upper and lower
20265935Spendry 	 * layers.  additionally, uppervp and lowervp are (locked)
20365935Spendry 	 * references to existing vnodes in the upper and lower layers.
20465935Spendry 	 *
20565935Spendry 	 * there are now three cases to consider.
20665935Spendry 	 * 1. if both layers returned an error, then return whatever
20765935Spendry 	 *    error the upper layer generated.
20865935Spendry 	 *
20965935Spendry 	 * 2. if the top layer failed and the bottom layer succeeded
21065935Spendry 	 *    then two subcases occur.
21165935Spendry 	 *    a.  the bottom vnode is not a directory, in which
21265935Spendry 	 *	  case just return a new union vnode referencing
21365935Spendry 	 *	  an empty top layer and the existing bottom layer.
21465935Spendry 	 *    b.  the bottom vnode is a directory, in which case
21565935Spendry 	 *	  create a new directory in the top-level and
21665935Spendry 	 *	  continue as in case 3.
21765935Spendry 	 *
21865935Spendry 	 * 3. if the top layer succeeded then return a new union
21965935Spendry 	 *    vnode referencing whatever the new top layer and
22065935Spendry 	 *    whatever the bottom layer returned.
22165935Spendry 	 */
22265935Spendry 
22366027Spendry 	*ap->a_vpp = NULLVP;
22466027Spendry 
22565935Spendry 	/* case 1. */
22665935Spendry 	if ((uerror != 0) && (lerror != 0)) {
22765935Spendry 		return (uerror);
22865935Spendry 	}
22965935Spendry 
23065935Spendry 	/* case 2. */
23165935Spendry 	if (uerror != 0 /* && (lerror == 0) */ ) {
23265935Spendry 		if (lowervp->v_type == VDIR) { /* case 2b. */
23366051Spendry 			dun->un_flags &= ~UN_ULOCK;
23466051Spendry 			VOP_UNLOCK(upperdvp);
23565997Spendry 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
23666051Spendry 			VOP_LOCK(upperdvp);
23766051Spendry 			dun->un_flags |= UN_ULOCK;
23866051Spendry 
23965935Spendry 			if (uerror) {
24065935Spendry 				if (lowervp) {
24165935Spendry 					vput(lowervp);
24265997Spendry 					lowervp = NULLVP;
24365935Spendry 				}
24465935Spendry 				return (uerror);
24565935Spendry 			}
24665935Spendry 		}
24765935Spendry 	}
24865935Spendry 
24965965Spendry 	if (lowervp)
25065965Spendry 		VOP_UNLOCK(lowervp);
25165965Spendry 
25265997Spendry 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
25365997Spendry 			      uppervp, lowervp);
25465997Spendry 
25565965Spendry 	if (error) {
25665965Spendry 		if (uppervp)
25766051Spendry 			vput(uppervp);
25865965Spendry 		if (lowervp)
25965965Spendry 			vrele(lowervp);
26065965Spendry 	} else {
26165994Spendry 		if (*ap->a_vpp != dvp)
26265994Spendry 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
26365994Spendry 				VOP_UNLOCK(dvp);
26465965Spendry 	}
26565965Spendry 
26665965Spendry 	return (error);
26765935Spendry }
26865935Spendry 
26965963Spendry int
27065963Spendry union_create(ap)
27165963Spendry 	struct vop_create_args /* {
27265963Spendry 		struct vnode *a_dvp;
27365963Spendry 		struct vnode **a_vpp;
27465963Spendry 		struct componentname *a_cnp;
27565963Spendry 		struct vattr *a_vap;
27665963Spendry 	} */ *ap;
27765963Spendry {
27865963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
27965963Spendry 	struct vnode *dvp = un->un_uppervp;
28065963Spendry 
28165963Spendry 	if (dvp) {
28265963Spendry 		int error;
28365963Spendry 		struct vnode *vp;
28465963Spendry 
28565963Spendry 		VREF(dvp);
28666051Spendry 		un->un_flags |= UN_KLOCK;
28765963Spendry 		vput(ap->a_dvp);
28865963Spendry 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
28965963Spendry 		if (error)
29065963Spendry 			return (error);
29165963Spendry 
29265963Spendry 		error = union_allocvp(
29365963Spendry 				ap->a_vpp,
29465989Spendry 				ap->a_dvp->v_mount,
29565989Spendry 				ap->a_dvp,
29665965Spendry 				NULLVP,
29765963Spendry 				ap->a_cnp,
29865963Spendry 				vp,
29965963Spendry 				NULLVP);
30065965Spendry 		if (error)
30166051Spendry 			vput(vp);
30265963Spendry 		return (error);
30365963Spendry 	}
30465963Spendry 
30565963Spendry 	vput(ap->a_dvp);
30665963Spendry 	return (EROFS);
30765963Spendry }
30865963Spendry 
30965963Spendry int
31065963Spendry union_mknod(ap)
31165963Spendry 	struct vop_mknod_args /* {
31265963Spendry 		struct vnode *a_dvp;
31365963Spendry 		struct vnode **a_vpp;
31465963Spendry 		struct componentname *a_cnp;
31565963Spendry 		struct vattr *a_vap;
31665963Spendry 	} */ *ap;
31765963Spendry {
31865963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
31965963Spendry 	struct vnode *dvp = un->un_uppervp;
32065963Spendry 
32165963Spendry 	if (dvp) {
32265963Spendry 		int error;
32365963Spendry 		struct vnode *vp;
32465963Spendry 
32565963Spendry 		VREF(dvp);
32666051Spendry 		un->un_flags |= UN_KLOCK;
32765963Spendry 		vput(ap->a_dvp);
32865963Spendry 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
32965963Spendry 		if (error)
33065963Spendry 			return (error);
33165963Spendry 
33265965Spendry 		if (vp) {
33365965Spendry 			error = union_allocvp(
33465965Spendry 					ap->a_vpp,
33565989Spendry 					ap->a_dvp->v_mount,
33665989Spendry 					ap->a_dvp,
33765965Spendry 					NULLVP,
33865965Spendry 					ap->a_cnp,
33965965Spendry 					vp,
34065965Spendry 					NULLVP);
34165965Spendry 			if (error)
34266051Spendry 				vput(vp);
34365965Spendry 		}
34465963Spendry 		return (error);
34565963Spendry 	}
34665963Spendry 
34765963Spendry 	vput(ap->a_dvp);
34865963Spendry 	return (EROFS);
34965963Spendry }
35065963Spendry 
35165935Spendry int
35265935Spendry union_open(ap)
35365935Spendry 	struct vop_open_args /* {
35465935Spendry 		struct vnodeop_desc *a_desc;
35565935Spendry 		struct vnode *a_vp;
35665935Spendry 		int a_mode;
35765935Spendry 		struct ucred *a_cred;
35865935Spendry 		struct proc *a_p;
35965935Spendry 	} */ *ap;
36065935Spendry {
36165935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
36265965Spendry 	struct vnode *tvp;
36365935Spendry 	int mode = ap->a_mode;
36465935Spendry 	struct ucred *cred = ap->a_cred;
36565935Spendry 	struct proc *p = ap->a_p;
36665965Spendry 	int error;
36765935Spendry 
36865935Spendry 	/*
36965935Spendry 	 * If there is an existing upper vp then simply open that.
37065935Spendry 	 */
37165965Spendry 	tvp = un->un_uppervp;
37265965Spendry 	if (tvp == NULLVP) {
37365935Spendry 		/*
37465965Spendry 		 * If the lower vnode is being opened for writing, then
37565965Spendry 		 * copy the file contents to the upper vnode and open that,
37665965Spendry 		 * otherwise can simply open the lower vnode.
37765935Spendry 		 */
37865965Spendry 		tvp = un->un_lowervp;
37965965Spendry 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
38065994Spendry 			struct vnode *vp;
38165997Spendry 			int i;
38265963Spendry 
38365965Spendry 			/*
38465965Spendry 			 * Open the named file in the upper layer.  Note that
38565965Spendry 			 * the file may have come into existence *since* the
38665965Spendry 			 * lookup was done, since the upper layer may really
38765965Spendry 			 * be a loopback mount of some other filesystem...
38865965Spendry 			 * so open the file with exclusive create and barf if
38965965Spendry 			 * it already exists.
39066027Spendry 			 * XXX - perhaps should re-lookup the node (once more
39165965Spendry 			 * with feeling) and simply open that.  Who knows.
39265965Spendry 			 */
39365997Spendry 			error = union_vn_create(&vp, un, p);
39465965Spendry 			if (error)
39565965Spendry 				return (error);
39666051Spendry 
39766051Spendry 			/* at this point, uppervp is locked */
39866053Spendry 			union_newupper(un, vp);
39966051Spendry 			un->un_flags |= UN_ULOCK;
40065965Spendry 
40165965Spendry 			/*
40265965Spendry 			 * Now, if the file is being opened with truncation,
40365965Spendry 			 * then the (new) upper vnode is ready to fly,
40465965Spendry 			 * otherwise the data from the lower vnode must be
40565965Spendry 			 * copied to the upper layer first.  This only works
40665965Spendry 			 * for regular files (check is made above).
40765965Spendry 			 */
40865965Spendry 			if ((mode & O_TRUNC) == 0) {
40965965Spendry 				/*
41065965Spendry 				 * XXX - should not ignore errors
41165965Spendry 				 * from VOP_CLOSE
41265965Spendry 				 */
41365994Spendry 				VOP_LOCK(tvp);
41465965Spendry 				error = VOP_OPEN(tvp, FREAD, cred, p);
41565965Spendry 				if (error == 0) {
41665965Spendry 					error = union_copyfile(p, cred,
41765965Spendry 						       tvp, un->un_uppervp);
41865965Spendry 					VOP_UNLOCK(tvp);
41965965Spendry 					(void) VOP_CLOSE(tvp, FREAD);
42065965Spendry 				} else {
42165965Spendry 					VOP_UNLOCK(tvp);
42265965Spendry 				}
42366051Spendry 
42465997Spendry 				if (!error)
42566027Spendry 					uprintf("union: copied up %s\n",
42665997Spendry 								un->un_path);
42765935Spendry 			}
42865997Spendry 
429*66057Spendry 			un->un_flags &= ~UN_ULOCK;
430*66057Spendry 			VOP_UNLOCK(un->un_uppervp);
431*66057Spendry 			union_vn_close(un->un_uppervp, FWRITE, cred, p);
432*66057Spendry 			VOP_LOCK(un->un_uppervp);
433*66057Spendry 			un->un_flags |= UN_ULOCK;
434*66057Spendry 
43565997Spendry 			/*
43665997Spendry 			 * Subsequent IOs will go to the top layer, so
43765997Spendry 			 * call close on the lower vnode and open on the
43865997Spendry 			 * upper vnode to ensure that the filesystem keeps
43965997Spendry 			 * its references counts right.  This doesn't do
44065997Spendry 			 * the right thing with (cred) and (FREAD) though.
44165997Spendry 			 * Ignoring error returns is not righ, either.
44265997Spendry 			 */
44366027Spendry 			for (i = 0; i < un->un_openl; i++) {
44465997Spendry 				(void) VOP_CLOSE(tvp, FREAD);
44565997Spendry 				(void) VOP_OPEN(un->un_uppervp, FREAD, cred, p);
44665997Spendry 			}
44766027Spendry 			un->un_openl = 0;
44865997Spendry 
44965965Spendry 			if (error == 0)
45065965Spendry 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
45165965Spendry 			return (error);
45265935Spendry 		}
45366051Spendry 
45466051Spendry 		/*
45566051Spendry 		 * Just open the lower vnode
45666051Spendry 		 */
45766027Spendry 		un->un_openl++;
45866051Spendry 		VOP_LOCK(tvp);
45966051Spendry 		error = VOP_OPEN(tvp, mode, cred, p);
46066051Spendry 		VOP_UNLOCK(tvp);
46166051Spendry 
46266051Spendry 		return (error);
46365935Spendry 	}
46465935Spendry 
46565965Spendry 	error = VOP_OPEN(tvp, mode, cred, p);
46665965Spendry 
46765965Spendry 	return (error);
46865935Spendry }
46965935Spendry 
47065963Spendry int
47165963Spendry union_close(ap)
47265963Spendry 	struct vop_close_args /* {
47365963Spendry 		struct vnode *a_vp;
47465963Spendry 		int  a_fflag;
47565963Spendry 		struct ucred *a_cred;
47665963Spendry 		struct proc *a_p;
47765963Spendry 	} */ *ap;
47865963Spendry {
47965997Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
48065997Spendry 	struct vnode *vp;
48165963Spendry 
48265997Spendry 	if (un->un_uppervp) {
48365997Spendry 		vp = un->un_uppervp;
48465997Spendry 	} else {
48566027Spendry #ifdef UNION_DIAGNOSTIC
48666027Spendry 		if (un->un_openl <= 0)
48766027Spendry 			panic("union: un_openl cnt");
48865997Spendry #endif
48966027Spendry 		--un->un_openl;
49065997Spendry 		vp = un->un_lowervp;
49165997Spendry 	}
49266027Spendry 
49365997Spendry 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
49465963Spendry }
49565963Spendry 
49665935Spendry /*
49765963Spendry  * Check access permission on the union vnode.
49865963Spendry  * The access check being enforced is to check
49965963Spendry  * against both the underlying vnode, and any
50065963Spendry  * copied vnode.  This ensures that no additional
50165963Spendry  * file permissions are given away simply because
50265963Spendry  * the user caused an implicit file copy.
50365963Spendry  */
50465963Spendry int
50565963Spendry union_access(ap)
50665963Spendry 	struct vop_access_args /* {
50765963Spendry 		struct vnodeop_desc *a_desc;
50865963Spendry 		struct vnode *a_vp;
50965963Spendry 		int a_mode;
51065963Spendry 		struct ucred *a_cred;
51165963Spendry 		struct proc *a_p;
51265963Spendry 	} */ *ap;
51365963Spendry {
51465963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
51565965Spendry 	int error = 0;
51665963Spendry 	struct vnode *vp;
51765963Spendry 
51865963Spendry 	if (vp = un->un_lowervp) {
51965965Spendry 		VOP_LOCK(vp);
52065963Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
52165965Spendry 		VOP_UNLOCK(vp);
52265963Spendry 		if (error)
52365963Spendry 			return (error);
52465963Spendry 	}
52565963Spendry 
52666051Spendry 	if (vp = un->un_uppervp)
52765965Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
52865965Spendry 
52965965Spendry 	return (error);
53065963Spendry }
53165963Spendry 
53265963Spendry /*
53365935Spendry  *  We handle getattr only to change the fsid.
53465935Spendry  */
53565935Spendry int
53665935Spendry union_getattr(ap)
53765935Spendry 	struct vop_getattr_args /* {
53865935Spendry 		struct vnode *a_vp;
53965935Spendry 		struct vattr *a_vap;
54065935Spendry 		struct ucred *a_cred;
54165935Spendry 		struct proc *a_p;
54265935Spendry 	} */ *ap;
54365935Spendry {
54465935Spendry 	int error;
54565965Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
54666051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
54765935Spendry 
54866051Spendry 	if (dolock)
54966051Spendry 		VOP_LOCK(vp);
55065965Spendry 	error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
55166051Spendry 	if (dolock)
55266051Spendry 		VOP_UNLOCK(vp);
55365965Spendry 
55465935Spendry 	/* Requires that arguments be restored. */
55565935Spendry 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
55665935Spendry 	return (0);
55765935Spendry }
55865935Spendry 
55965963Spendry int
56065965Spendry union_setattr(ap)
56165963Spendry 	struct vop_setattr_args /* {
56265963Spendry 		struct vnode *a_vp;
56365963Spendry 		struct vattr *a_vap;
56465963Spendry 		struct ucred *a_cred;
56565963Spendry 		struct proc *a_p;
56665963Spendry 	} */ *ap;
56765963Spendry {
56865963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
56965963Spendry 	int error;
57065963Spendry 
571*66057Spendry 	/*
572*66057Spendry 	 * Handle case of truncating lower object to zero size,
573*66057Spendry 	 * by creating a zero length upper object.  This is to
574*66057Spendry 	 * handle the case of open with O_TRUNC and O_CREAT.
575*66057Spendry 	 */
576*66057Spendry 	if ((un->un_uppervp == NULLVP) &&
577*66057Spendry 	    /* assert(un->un_lowervp != NULLVP) */
578*66057Spendry 	    (un->un_lowervp->v_type == VREG) &&
579*66057Spendry 	    (ap->a_vap->va_size == 0)) {
580*66057Spendry 		struct vnode *vp;
581*66057Spendry 
582*66057Spendry 		error = union_vn_create(&vp, un, ap->a_p);
583*66057Spendry 		if (error)
584*66057Spendry 			return (error);
585*66057Spendry 
586*66057Spendry 		/* at this point, uppervp is locked */
587*66057Spendry 		union_newupper(un, vp);
588*66057Spendry 
589*66057Spendry 		VOP_UNLOCK(vp);
590*66057Spendry 		union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p);
591*66057Spendry 		VOP_LOCK(vp);
592*66057Spendry 		un->un_flags |= UN_ULOCK;
593*66057Spendry 	}
594*66057Spendry 
595*66057Spendry 	/*
596*66057Spendry 	 * Try to set attributes in upper layer,
597*66057Spendry 	 * otherwise return read-only filesystem error.
598*66057Spendry 	 */
599*66057Spendry 	if (un->un_uppervp != NULLVP) {
60065963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
60165963Spendry 					ap->a_cred, ap->a_p);
60265963Spendry 	} else {
60365963Spendry 		error = EROFS;
60465963Spendry 	}
60565963Spendry 
60665963Spendry 	return (error);
60765963Spendry }
60865963Spendry 
60965963Spendry int
61065963Spendry union_read(ap)
61165963Spendry 	struct vop_read_args /* {
61265963Spendry 		struct vnode *a_vp;
61365963Spendry 		struct uio *a_uio;
61465963Spendry 		int  a_ioflag;
61565963Spendry 		struct ucred *a_cred;
61665963Spendry 	} */ *ap;
61765963Spendry {
61865963Spendry 	int error;
61965963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
62066051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
62165963Spendry 
62266051Spendry 	if (dolock)
62366051Spendry 		VOP_LOCK(vp);
62465963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
62566051Spendry 	if (dolock)
62666051Spendry 		VOP_UNLOCK(vp);
62765963Spendry 
62865963Spendry 	return (error);
62965963Spendry }
63065963Spendry 
63165963Spendry int
63265963Spendry union_write(ap)
63365963Spendry 	struct vop_read_args /* {
63465963Spendry 		struct vnode *a_vp;
63565963Spendry 		struct uio *a_uio;
63665963Spendry 		int  a_ioflag;
63765963Spendry 		struct ucred *a_cred;
63865963Spendry 	} */ *ap;
63965963Spendry {
64065963Spendry 	int error;
64165963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
64266051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
64365963Spendry 
64466051Spendry 	if (dolock)
64566051Spendry 		VOP_LOCK(vp);
64665963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
64766051Spendry 	if (dolock)
64866051Spendry 		VOP_UNLOCK(vp);
64965963Spendry 
65065963Spendry 	return (error);
65165963Spendry }
65265963Spendry 
65365963Spendry int
65465963Spendry union_ioctl(ap)
65565963Spendry 	struct vop_ioctl_args /* {
65665963Spendry 		struct vnode *a_vp;
65765963Spendry 		int  a_command;
65865963Spendry 		caddr_t  a_data;
65965963Spendry 		int  a_fflag;
66065963Spendry 		struct ucred *a_cred;
66165963Spendry 		struct proc *a_p;
66265963Spendry 	} */ *ap;
66365963Spendry {
66465963Spendry 
66565963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
66665963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
66765963Spendry }
66865963Spendry 
66965963Spendry int
67065963Spendry union_select(ap)
67165963Spendry 	struct vop_select_args /* {
67265963Spendry 		struct vnode *a_vp;
67365963Spendry 		int  a_which;
67465963Spendry 		int  a_fflags;
67565963Spendry 		struct ucred *a_cred;
67665963Spendry 		struct proc *a_p;
67765963Spendry 	} */ *ap;
67865963Spendry {
67965963Spendry 
68065963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
68165963Spendry 				ap->a_cred, ap->a_p));
68265963Spendry }
68365963Spendry 
68465963Spendry int
68565963Spendry union_mmap(ap)
68665963Spendry 	struct vop_mmap_args /* {
68765963Spendry 		struct vnode *a_vp;
68865963Spendry 		int  a_fflags;
68965963Spendry 		struct ucred *a_cred;
69065963Spendry 		struct proc *a_p;
69165963Spendry 	} */ *ap;
69265963Spendry {
69365963Spendry 
69465963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
69565963Spendry 				ap->a_cred, ap->a_p));
69665963Spendry }
69765963Spendry 
69865963Spendry int
69965963Spendry union_fsync(ap)
70065963Spendry 	struct vop_fsync_args /* {
70165963Spendry 		struct vnode *a_vp;
70265963Spendry 		struct ucred *a_cred;
70365963Spendry 		int  a_waitfor;
70465963Spendry 		struct proc *a_p;
70565963Spendry 	} */ *ap;
70665963Spendry {
70765963Spendry 	int error = 0;
70865963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
70965963Spendry 
71065963Spendry 	if (targetvp) {
71166051Spendry 		int dolock = (targetvp == LOWERVP(ap->a_vp));
71266051Spendry 
71366051Spendry 		if (dolock)
71466051Spendry 			VOP_LOCK(targetvp);
71565963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
71665963Spendry 					ap->a_waitfor, ap->a_p);
71766051Spendry 		if (dolock)
71866051Spendry 			VOP_UNLOCK(targetvp);
71965963Spendry 	}
72065963Spendry 
72165963Spendry 	return (error);
72265963Spendry }
72365963Spendry 
72465963Spendry int
72565963Spendry union_seek(ap)
72665963Spendry 	struct vop_seek_args /* {
72765963Spendry 		struct vnode *a_vp;
72865963Spendry 		off_t  a_oldoff;
72965963Spendry 		off_t  a_newoff;
73065963Spendry 		struct ucred *a_cred;
73165963Spendry 	} */ *ap;
73265963Spendry {
73365963Spendry 
73465963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
73565963Spendry }
73665963Spendry 
73765963Spendry int
73865963Spendry union_remove(ap)
73965963Spendry 	struct vop_remove_args /* {
74065963Spendry 		struct vnode *a_dvp;
74165963Spendry 		struct vnode *a_vp;
74265963Spendry 		struct componentname *a_cnp;
74365963Spendry 	} */ *ap;
74465963Spendry {
74565963Spendry 	int error;
74665963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
74765963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
74865963Spendry 
74965963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
75065963Spendry 		struct vnode *dvp = dun->un_uppervp;
75165963Spendry 		struct vnode *vp = un->un_uppervp;
75265963Spendry 
75365963Spendry 		VREF(dvp);
75466051Spendry 		dun->un_flags |= UN_KLOCK;
75565963Spendry 		vput(ap->a_dvp);
75665963Spendry 		VREF(vp);
75766051Spendry 		un->un_flags |= UN_KLOCK;
75865963Spendry 		vput(ap->a_vp);
75965963Spendry 
76065963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
76166027Spendry 		if (!error)
76266027Spendry 			union_removed_upper(un);
76366027Spendry 
76466027Spendry 		/*
76566027Spendry 		 * XXX: should create a whiteout here
76666027Spendry 		 */
76765963Spendry 	} else {
76865963Spendry 		/*
76965963Spendry 		 * XXX: should create a whiteout here
77065963Spendry 		 */
77165963Spendry 		vput(ap->a_dvp);
77265963Spendry 		vput(ap->a_vp);
77365963Spendry 		error = EROFS;
77465963Spendry 	}
77565963Spendry 
77665963Spendry 	return (error);
77765963Spendry }
77865963Spendry 
77965963Spendry int
78065963Spendry union_link(ap)
78165963Spendry 	struct vop_link_args /* {
78265963Spendry 		struct vnode *a_vp;
78365963Spendry 		struct vnode *a_tdvp;
78465963Spendry 		struct componentname *a_cnp;
78565963Spendry 	} */ *ap;
78665963Spendry {
78765963Spendry 	int error;
78865963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
78965963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
79065963Spendry 
79165963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
79265963Spendry 		struct vnode *dvp = dun->un_uppervp;
79365963Spendry 		struct vnode *vp = un->un_uppervp;
79465963Spendry 
79565963Spendry 		VREF(dvp);
79666051Spendry 		dun->un_flags |= UN_KLOCK;
79765963Spendry 		vput(ap->a_vp);
79865963Spendry 		VREF(vp);
79965963Spendry 		vrele(ap->a_tdvp);
80065963Spendry 
80165963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
80265963Spendry 	} else {
80365963Spendry 		/*
80465963Spendry 		 * XXX: need to copy to upper layer
80565963Spendry 		 * and do the link there.
80665963Spendry 		 */
80765963Spendry 		vput(ap->a_vp);
80865963Spendry 		vrele(ap->a_tdvp);
80965963Spendry 		error = EROFS;
81065963Spendry 	}
81165963Spendry 
81265963Spendry 	return (error);
81365963Spendry }
81465963Spendry 
81565963Spendry int
81665963Spendry union_rename(ap)
81765963Spendry 	struct vop_rename_args  /* {
81865963Spendry 		struct vnode *a_fdvp;
81965963Spendry 		struct vnode *a_fvp;
82065963Spendry 		struct componentname *a_fcnp;
82165963Spendry 		struct vnode *a_tdvp;
82265963Spendry 		struct vnode *a_tvp;
82365963Spendry 		struct componentname *a_tcnp;
82465963Spendry 	} */ *ap;
82565963Spendry {
82665963Spendry 	int error;
82765963Spendry 
82865963Spendry 	struct vnode *fdvp = ap->a_fdvp;
82965963Spendry 	struct vnode *fvp = ap->a_fvp;
83065963Spendry 	struct vnode *tdvp = ap->a_tdvp;
83165963Spendry 	struct vnode *tvp = ap->a_tvp;
83265963Spendry 
83365963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
83465963Spendry 		struct union_node *un = VTOUNION(fdvp);
83565997Spendry 		if (un->un_uppervp == NULLVP) {
83665963Spendry 			error = EROFS;
83765963Spendry 			goto bad;
83865963Spendry 		}
83965963Spendry 
84065963Spendry 		fdvp = un->un_uppervp;
84165963Spendry 		VREF(fdvp);
84265963Spendry 		vrele(ap->a_fdvp);
84365963Spendry 	}
84465963Spendry 
84565963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
84665963Spendry 		struct union_node *un = VTOUNION(fvp);
84765997Spendry 		if (un->un_uppervp == NULLVP) {
84865963Spendry 			error = EROFS;
84965963Spendry 			goto bad;
85065963Spendry 		}
85165963Spendry 
85265963Spendry 		fvp = un->un_uppervp;
85365963Spendry 		VREF(fvp);
85465963Spendry 		vrele(ap->a_fvp);
85565963Spendry 	}
85665963Spendry 
85765963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
85865963Spendry 		struct union_node *un = VTOUNION(tdvp);
85965997Spendry 		if (un->un_uppervp == NULLVP) {
86065963Spendry 			error = EROFS;
86165963Spendry 			goto bad;
86265963Spendry 		}
86365963Spendry 
86465963Spendry 		tdvp = un->un_uppervp;
86565963Spendry 		VREF(tdvp);
86666051Spendry 		un->un_flags |= UN_KLOCK;
86765997Spendry 		vput(ap->a_tdvp);
86865963Spendry 	}
86965963Spendry 
87065963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
87165963Spendry 		struct union_node *un = VTOUNION(tvp);
87265997Spendry 		if (un->un_uppervp == NULLVP) {
87365963Spendry 			error = EROFS;
87465963Spendry 			goto bad;
87565963Spendry 		}
87665963Spendry 
87765963Spendry 		tvp = un->un_uppervp;
87865963Spendry 		VREF(tvp);
87966051Spendry 		un->un_flags |= UN_KLOCK;
88065963Spendry 		vput(ap->a_tvp);
88165963Spendry 	}
88265963Spendry 
88365963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
88465963Spendry 
88565963Spendry bad:
88665963Spendry 	vrele(fdvp);
88765963Spendry 	vrele(fvp);
88865963Spendry 	vput(tdvp);
88965963Spendry 	if (tvp)
89065963Spendry 		vput(tvp);
89165963Spendry 
89265963Spendry 	return (error);
89365963Spendry }
89465963Spendry 
89565963Spendry int
89665963Spendry union_mkdir(ap)
89765963Spendry 	struct vop_mkdir_args /* {
89865963Spendry 		struct vnode *a_dvp;
89965963Spendry 		struct vnode **a_vpp;
90065963Spendry 		struct componentname *a_cnp;
90165963Spendry 		struct vattr *a_vap;
90265963Spendry 	} */ *ap;
90365963Spendry {
90465963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
90565963Spendry 	struct vnode *dvp = un->un_uppervp;
90665963Spendry 
90765963Spendry 	if (dvp) {
90865963Spendry 		int error;
90965963Spendry 		struct vnode *vp;
91065963Spendry 
91165963Spendry 		VREF(dvp);
91266051Spendry 		un->un_flags |= UN_KLOCK;
91365963Spendry 		vput(ap->a_dvp);
91465963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
91565963Spendry 		if (error)
91665963Spendry 			return (error);
91765963Spendry 
91865963Spendry 		error = union_allocvp(
91965963Spendry 				ap->a_vpp,
92065989Spendry 				ap->a_dvp->v_mount,
92165989Spendry 				ap->a_dvp,
92265965Spendry 				NULLVP,
92365963Spendry 				ap->a_cnp,
92465963Spendry 				vp,
92565963Spendry 				NULLVP);
92665965Spendry 		if (error)
92766051Spendry 			vput(vp);
92865963Spendry 		return (error);
92965963Spendry 	}
93065963Spendry 
93165963Spendry 	vput(ap->a_dvp);
93265963Spendry 	return (EROFS);
93365963Spendry }
93465963Spendry 
93565963Spendry int
93665963Spendry union_rmdir(ap)
93765963Spendry 	struct vop_rmdir_args /* {
93865963Spendry 		struct vnode *a_dvp;
93965963Spendry 		struct vnode *a_vp;
94065963Spendry 		struct componentname *a_cnp;
94165963Spendry 	} */ *ap;
94265963Spendry {
94365963Spendry 	int error;
94465963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
94565963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
94665963Spendry 
94765963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
94865963Spendry 		struct vnode *dvp = dun->un_uppervp;
94965963Spendry 		struct vnode *vp = un->un_uppervp;
95065963Spendry 
95165963Spendry 		VREF(dvp);
95266051Spendry 		dun->un_flags |= UN_KLOCK;
95365963Spendry 		vput(ap->a_dvp);
95465963Spendry 		VREF(vp);
95566051Spendry 		un->un_flags |= UN_KLOCK;
95665963Spendry 		vput(ap->a_vp);
95765963Spendry 
95866051Spendry 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
95966027Spendry 		if (!error)
96066027Spendry 			union_removed_upper(un);
96166027Spendry 
96266027Spendry 		/*
96366027Spendry 		 * XXX: should create a whiteout here
96466027Spendry 		 */
96565963Spendry 	} else {
96665963Spendry 		/*
96765963Spendry 		 * XXX: should create a whiteout here
96865963Spendry 		 */
96965963Spendry 		vput(ap->a_dvp);
97065963Spendry 		vput(ap->a_vp);
97165963Spendry 		error = EROFS;
97265963Spendry 	}
97365963Spendry 
97465963Spendry 	return (error);
97565963Spendry }
97665963Spendry 
97765963Spendry int
97865963Spendry union_symlink(ap)
97965963Spendry 	struct vop_symlink_args /* {
98065963Spendry 		struct vnode *a_dvp;
98165963Spendry 		struct vnode **a_vpp;
98265963Spendry 		struct componentname *a_cnp;
98365963Spendry 		struct vattr *a_vap;
98465963Spendry 		char *a_target;
98565963Spendry 	} */ *ap;
98665963Spendry {
98765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
98865963Spendry 	struct vnode *dvp = un->un_uppervp;
98965963Spendry 
99065963Spendry 	if (dvp) {
99165963Spendry 		int error;
99265963Spendry 		struct vnode *vp;
99365963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
99465963Spendry 
99565963Spendry 		VREF(dvp);
99666051Spendry 		un->un_flags |= UN_KLOCK;
99765963Spendry 		vput(ap->a_dvp);
99865963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
99965963Spendry 					ap->a_vap, ap->a_target);
100065997Spendry 		*ap->a_vpp = NULLVP;
100165963Spendry 		return (error);
100265963Spendry 	}
100365963Spendry 
100465963Spendry 	vput(ap->a_dvp);
100565963Spendry 	return (EROFS);
100665963Spendry }
100765963Spendry 
100865935Spendry /*
100965935Spendry  * union_readdir works in concert with getdirentries and
101065935Spendry  * readdir(3) to provide a list of entries in the unioned
101165935Spendry  * directories.  getdirentries is responsible for walking
101265935Spendry  * down the union stack.  readdir(3) is responsible for
101365935Spendry  * eliminating duplicate names from the returned data stream.
101465935Spendry  */
101565935Spendry int
101665935Spendry union_readdir(ap)
101765935Spendry 	struct vop_readdir_args /* {
101865935Spendry 		struct vnodeop_desc *a_desc;
101965935Spendry 		struct vnode *a_vp;
102065935Spendry 		struct uio *a_uio;
102165935Spendry 		struct ucred *a_cred;
102265935Spendry 	} */ *ap;
102365935Spendry {
102465963Spendry 	int error = 0;
102565935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
102665935Spendry 
102766051Spendry 	if (un->un_uppervp)
102866051Spendry 		error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred);
102965935Spendry 
103065963Spendry 	return (error);
103165935Spendry }
103265935Spendry 
103365935Spendry int
103465963Spendry union_readlink(ap)
103565963Spendry 	struct vop_readlink_args /* {
103665963Spendry 		struct vnode *a_vp;
103765963Spendry 		struct uio *a_uio;
103865963Spendry 		struct ucred *a_cred;
103965963Spendry 	} */ *ap;
104065963Spendry {
104165963Spendry 	int error;
104265963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
104366051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
104465963Spendry 
104566051Spendry 	if (dolock)
104666051Spendry 		VOP_LOCK(vp);
104765963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
104866051Spendry 	if (dolock)
104966051Spendry 		VOP_UNLOCK(vp);
105065963Spendry 
105165963Spendry 	return (error);
105265963Spendry }
105365963Spendry 
105465963Spendry int
105565963Spendry union_abortop(ap)
105665963Spendry 	struct vop_abortop_args /* {
105765963Spendry 		struct vnode *a_dvp;
105865963Spendry 		struct componentname *a_cnp;
105965963Spendry 	} */ *ap;
106065963Spendry {
106165963Spendry 	int error;
106265965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
106365963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
106465963Spendry 	int islocked = un->un_flags & UN_LOCKED;
106566051Spendry 	int dolock = (vp == LOWERVP(ap->a_dvp));
106665963Spendry 
106766051Spendry 	if (islocked && dolock)
106865963Spendry 		VOP_LOCK(vp);
106965963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
107066051Spendry 	if (islocked && dolock)
107165963Spendry 		VOP_UNLOCK(vp);
107265963Spendry 
107365963Spendry 	return (error);
107465963Spendry }
107565963Spendry 
107665963Spendry int
107765935Spendry union_inactive(ap)
107865935Spendry 	struct vop_inactive_args /* {
107965935Spendry 		struct vnode *a_vp;
108065935Spendry 	} */ *ap;
108165935Spendry {
108265935Spendry 
108365935Spendry 	/*
108465935Spendry 	 * Do nothing (and _don't_ bypass).
108565935Spendry 	 * Wait to vrele lowervp until reclaim,
108665935Spendry 	 * so that until then our union_node is in the
108765935Spendry 	 * cache and reusable.
108865935Spendry 	 *
108965935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
109065935Spendry 	 * the lowervp and then trying to reactivate it
109165935Spendry 	 * with capabilities (v_id)
109265935Spendry 	 * like they do in the name lookup cache code.
109365935Spendry 	 * That's too much work for now.
109465935Spendry 	 */
109565989Spendry 
109666027Spendry #ifdef UNION_DIAGNOSTIC
109765989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
109865989Spendry 
109965989Spendry 	if (un->un_flags & UN_LOCKED)
110065989Spendry 		panic("union: inactivating locked node");
110165989Spendry #endif
110265989Spendry 
110365935Spendry 	return (0);
110465935Spendry }
110565935Spendry 
110665935Spendry int
110765935Spendry union_reclaim(ap)
110865935Spendry 	struct vop_reclaim_args /* {
110965935Spendry 		struct vnode *a_vp;
111065935Spendry 	} */ *ap;
111165935Spendry {
111265935Spendry 
111366053Spendry 	union_freevp(ap->a_vp);
111466053Spendry 
111565935Spendry 	return (0);
111665935Spendry }
111765935Spendry 
111865963Spendry int
111965963Spendry union_lock(ap)
112065963Spendry 	struct vop_lock_args *ap;
112165963Spendry {
112265963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
112365935Spendry 
112466051Spendry 	if (un->un_uppervp) {
112566051Spendry 		if ((un->un_flags & UN_ULOCK) == 0) {
112666051Spendry 			VOP_LOCK(un->un_uppervp);
112766051Spendry 			un->un_flags |= UN_ULOCK;
112866051Spendry 		}
112966051Spendry #ifdef DIAGNOSTIC
113066051Spendry 		if (un->un_flags & UN_KLOCK)
113166051Spendry 			panic("union: dangling upper lock");
113266051Spendry #endif
113366051Spendry 	}
113466051Spendry 
113565965Spendry 	while (un->un_flags & UN_LOCKED) {
113665963Spendry #ifdef DIAGNOSTIC
113765989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
113865989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
113965989Spendry 			panic("union: locking against myself");
114065963Spendry #endif
114165963Spendry 		un->un_flags |= UN_WANT;
114265963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
114365963Spendry 	}
114465963Spendry 	un->un_flags |= UN_LOCKED;
114565989Spendry 
114665963Spendry #ifdef DIAGNOSTIC
114765989Spendry 	if (curproc)
114865989Spendry 		un->un_pid = curproc->p_pid;
114965989Spendry 	else
115065989Spendry 		un->un_pid = -1;
115165963Spendry #endif
115266028Spendry 
115366028Spendry 	return (0);
115465963Spendry }
115565963Spendry 
115665935Spendry int
115765963Spendry union_unlock(ap)
115865963Spendry 	struct vop_lock_args *ap;
115965963Spendry {
116065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
116165963Spendry 
116265963Spendry #ifdef DIAGNOSTIC
116365965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
116465965Spendry 		panic("union: unlock unlocked node");
116565989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
116665989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
116765963Spendry 		panic("union: unlocking other process's union node");
116865963Spendry #endif
116965963Spendry 
117065963Spendry 	un->un_flags &= ~UN_LOCKED;
117166051Spendry 
117266051Spendry 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
117366051Spendry 		VOP_UNLOCK(un->un_uppervp);
117466051Spendry 
117566051Spendry 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
117666051Spendry 
117765963Spendry 	if (un->un_flags & UN_WANT) {
117865963Spendry 		un->un_flags &= ~UN_WANT;
117965963Spendry 		wakeup((caddr_t) &un->un_flags);
118065963Spendry 	}
118165963Spendry 
118265963Spendry #ifdef DIAGNOSTIC
118365963Spendry 	un->un_pid = 0;
118465963Spendry #endif
118566028Spendry 
118666028Spendry 	return (0);
118765963Spendry }
118865963Spendry 
118965963Spendry int
119065963Spendry union_bmap(ap)
119165963Spendry 	struct vop_bmap_args /* {
119265963Spendry 		struct vnode *a_vp;
119365963Spendry 		daddr_t  a_bn;
119465963Spendry 		struct vnode **a_vpp;
119565963Spendry 		daddr_t *a_bnp;
119665963Spendry 		int *a_runp;
119765963Spendry 	} */ *ap;
119865963Spendry {
119965963Spendry 	int error;
120065963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
120166051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
120265963Spendry 
120366051Spendry 	if (dolock)
120466051Spendry 		VOP_LOCK(vp);
120565963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
120666051Spendry 	if (dolock)
120766051Spendry 		VOP_UNLOCK(vp);
120865963Spendry 
120965963Spendry 	return (error);
121065963Spendry }
121165963Spendry 
121265963Spendry int
121365935Spendry union_print(ap)
121465935Spendry 	struct vop_print_args /* {
121565935Spendry 		struct vnode *a_vp;
121665935Spendry 	} */ *ap;
121765935Spendry {
121865935Spendry 	struct vnode *vp = ap->a_vp;
121965935Spendry 
122065935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
122165935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
122265935Spendry 	return (0);
122365935Spendry }
122465935Spendry 
122565963Spendry int
122665963Spendry union_islocked(ap)
122765963Spendry 	struct vop_islocked_args /* {
122865963Spendry 		struct vnode *a_vp;
122965963Spendry 	} */ *ap;
123065963Spendry {
123165935Spendry 
123265963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
123365963Spendry }
123465963Spendry 
123565935Spendry int
123665963Spendry union_pathconf(ap)
123765963Spendry 	struct vop_pathconf_args /* {
123865963Spendry 		struct vnode *a_vp;
123965963Spendry 		int a_name;
124065963Spendry 		int *a_retval;
124165935Spendry 	} */ *ap;
124265935Spendry {
124365935Spendry 	int error;
124465963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
124566051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
124665935Spendry 
124766051Spendry 	if (dolock)
124866051Spendry 		VOP_LOCK(vp);
124965963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
125066051Spendry 	if (dolock)
125166051Spendry 		VOP_UNLOCK(vp);
125265935Spendry 
125365963Spendry 	return (error);
125465963Spendry }
125565935Spendry 
125665963Spendry int
125765963Spendry union_advlock(ap)
125865963Spendry 	struct vop_advlock_args /* {
125965963Spendry 		struct vnode *a_vp;
126065963Spendry 		caddr_t  a_id;
126165963Spendry 		int  a_op;
126265963Spendry 		struct flock *a_fl;
126365963Spendry 		int  a_flags;
126465963Spendry 	} */ *ap;
126565963Spendry {
126665935Spendry 
126765963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
126865963Spendry 				ap->a_fl, ap->a_flags));
126965935Spendry }
127065935Spendry 
127165935Spendry 
127265935Spendry /*
127365963Spendry  * XXX - vop_strategy must be hand coded because it has no
127465935Spendry  * vnode in its arguments.
127565935Spendry  * This goes away with a merged VM/buffer cache.
127665935Spendry  */
127765935Spendry int
127865963Spendry union_strategy(ap)
127965963Spendry 	struct vop_strategy_args /* {
128065935Spendry 		struct buf *a_bp;
128165935Spendry 	} */ *ap;
128265935Spendry {
128365935Spendry 	struct buf *bp = ap->a_bp;
128465935Spendry 	int error;
128565935Spendry 	struct vnode *savedvp;
128665935Spendry 
128765935Spendry 	savedvp = bp->b_vp;
128865963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
128965935Spendry 
129065935Spendry #ifdef DIAGNOSTIC
129165997Spendry 	if (bp->b_vp == NULLVP)
129265963Spendry 		panic("union_strategy: nil vp");
129365963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
129465963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
129565963Spendry 		panic("union_strategy: writing to lowervp");
129665935Spendry #endif
129765935Spendry 
129865963Spendry 	error = VOP_STRATEGY(bp);
129965935Spendry 	bp->b_vp = savedvp;
130065935Spendry 
130165935Spendry 	return (error);
130265935Spendry }
130365935Spendry 
130465935Spendry /*
130565935Spendry  * Global vfs data structures
130665935Spendry  */
130765935Spendry int (**union_vnodeop_p)();
130865965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
130965963Spendry 	{ &vop_default_desc, vn_default_error },
131065963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
131165963Spendry 	{ &vop_create_desc, union_create },		/* create */
131265963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
131365963Spendry 	{ &vop_open_desc, union_open },			/* open */
131465963Spendry 	{ &vop_close_desc, union_close },		/* close */
131565963Spendry 	{ &vop_access_desc, union_access },		/* access */
131665963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
131765963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
131865963Spendry 	{ &vop_read_desc, union_read },			/* read */
131965963Spendry 	{ &vop_write_desc, union_write },		/* write */
132065963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
132165963Spendry 	{ &vop_select_desc, union_select },		/* select */
132265963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
132365963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
132465963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
132565963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
132665963Spendry 	{ &vop_link_desc, union_link },			/* link */
132765963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
132865963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
132965963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
133065963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
133165963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
133265963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
133365963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
133465963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
133565963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
133665963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
133765963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
133865963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
133965963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
134065963Spendry 	{ &vop_print_desc, union_print },		/* print */
134165963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
134265963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
134365963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
134465963Spendry #ifdef notdef
134565963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
134665963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
134765963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
134865963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
134965963Spendry 	{ &vop_update_desc, union_update },		/* update */
135065963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
135165963Spendry #endif
135265935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
135365935Spendry };
135465935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
135565935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1356