xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 66062)
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*66062Spendry  *	@(#)union_vnops.c	8.4 (Berkeley) 02/11/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 
42966057Spendry 			un->un_flags &= ~UN_ULOCK;
43066057Spendry 			VOP_UNLOCK(un->un_uppervp);
43166057Spendry 			union_vn_close(un->un_uppervp, FWRITE, cred, p);
43266057Spendry 			VOP_LOCK(un->un_uppervp);
43366057Spendry 			un->un_flags |= UN_ULOCK;
43466057Spendry 
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;
545*66062Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
546*66062Spendry 	struct vnode *vp = un->un_uppervp;
547*66062Spendry 	struct vattr *vap;
548*66062Spendry 	struct vattr va;
54965935Spendry 
550*66062Spendry 
551*66062Spendry 	/*
552*66062Spendry 	 * Some programs walk the filesystem hierarchy by counting
553*66062Spendry 	 * links to directories to avoid stat'ing all the time.
554*66062Spendry 	 * This means the link count on directories needs to be "correct".
555*66062Spendry 	 * The only way to do that is to call getattr on both layers
556*66062Spendry 	 * and fix up the link count.  The link count will not necessarily
557*66062Spendry 	 * be accurate but will be large enough to defeat the tree walkers.
558*66062Spendry 	 */
559*66062Spendry 
560*66062Spendry 	vap = ap->a_vap;
561*66062Spendry 
562*66062Spendry 	vp = un->un_uppervp;
563*66062Spendry 	if (vp != NULLVP) {
564*66062Spendry 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
565*66062Spendry 		if (error)
566*66062Spendry 			return (error);
567*66062Spendry 	}
568*66062Spendry 
569*66062Spendry 	if (vp == NULLVP) {
570*66062Spendry 		vp = un->un_lowervp;
571*66062Spendry 	} else if (vp->v_type == VDIR) {
572*66062Spendry 		vp = un->un_lowervp;
573*66062Spendry 		vap = &va;
574*66062Spendry 	} else {
575*66062Spendry 		vp = NULLVP;
576*66062Spendry 	}
577*66062Spendry 
578*66062Spendry 	if (vp != NULLVP) {
57966051Spendry 		VOP_LOCK(vp);
580*66062Spendry 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
58166051Spendry 		VOP_UNLOCK(vp);
582*66062Spendry 		if (error)
583*66062Spendry 			return (error);
584*66062Spendry 	}
58565965Spendry 
586*66062Spendry 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
587*66062Spendry 		ap->a_vap->va_nlink += vap->va_nlink;
588*66062Spendry 
589*66062Spendry 	vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
59065935Spendry 	return (0);
59165935Spendry }
59265935Spendry 
59365963Spendry int
59465965Spendry union_setattr(ap)
59565963Spendry 	struct vop_setattr_args /* {
59665963Spendry 		struct vnode *a_vp;
59765963Spendry 		struct vattr *a_vap;
59865963Spendry 		struct ucred *a_cred;
59965963Spendry 		struct proc *a_p;
60065963Spendry 	} */ *ap;
60165963Spendry {
60265963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
60365963Spendry 	int error;
60465963Spendry 
60566057Spendry 	/*
60666057Spendry 	 * Handle case of truncating lower object to zero size,
60766057Spendry 	 * by creating a zero length upper object.  This is to
60866057Spendry 	 * handle the case of open with O_TRUNC and O_CREAT.
60966057Spendry 	 */
61066057Spendry 	if ((un->un_uppervp == NULLVP) &&
61166057Spendry 	    /* assert(un->un_lowervp != NULLVP) */
61266057Spendry 	    (un->un_lowervp->v_type == VREG) &&
61366057Spendry 	    (ap->a_vap->va_size == 0)) {
61466057Spendry 		struct vnode *vp;
61566057Spendry 
61666057Spendry 		error = union_vn_create(&vp, un, ap->a_p);
61766057Spendry 		if (error)
61866057Spendry 			return (error);
61966057Spendry 
62066057Spendry 		/* at this point, uppervp is locked */
62166057Spendry 		union_newupper(un, vp);
62266057Spendry 
62366057Spendry 		VOP_UNLOCK(vp);
62466057Spendry 		union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p);
62566057Spendry 		VOP_LOCK(vp);
62666057Spendry 		un->un_flags |= UN_ULOCK;
62766057Spendry 	}
62866057Spendry 
62966057Spendry 	/*
63066057Spendry 	 * Try to set attributes in upper layer,
63166057Spendry 	 * otherwise return read-only filesystem error.
63266057Spendry 	 */
63366057Spendry 	if (un->un_uppervp != NULLVP) {
63465963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
63565963Spendry 					ap->a_cred, ap->a_p);
63665963Spendry 	} else {
63765963Spendry 		error = EROFS;
63865963Spendry 	}
63965963Spendry 
64065963Spendry 	return (error);
64165963Spendry }
64265963Spendry 
64365963Spendry int
64465963Spendry union_read(ap)
64565963Spendry 	struct vop_read_args /* {
64665963Spendry 		struct vnode *a_vp;
64765963Spendry 		struct uio *a_uio;
64865963Spendry 		int  a_ioflag;
64965963Spendry 		struct ucred *a_cred;
65065963Spendry 	} */ *ap;
65165963Spendry {
65265963Spendry 	int error;
65365963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
65466051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
65565963Spendry 
65666051Spendry 	if (dolock)
65766051Spendry 		VOP_LOCK(vp);
65865963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
65966051Spendry 	if (dolock)
66066051Spendry 		VOP_UNLOCK(vp);
66165963Spendry 
66265963Spendry 	return (error);
66365963Spendry }
66465963Spendry 
66565963Spendry int
66665963Spendry union_write(ap)
66765963Spendry 	struct vop_read_args /* {
66865963Spendry 		struct vnode *a_vp;
66965963Spendry 		struct uio *a_uio;
67065963Spendry 		int  a_ioflag;
67165963Spendry 		struct ucred *a_cred;
67265963Spendry 	} */ *ap;
67365963Spendry {
67465963Spendry 	int error;
67565963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
67666051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
67765963Spendry 
67866051Spendry 	if (dolock)
67966051Spendry 		VOP_LOCK(vp);
68065963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
68166051Spendry 	if (dolock)
68266051Spendry 		VOP_UNLOCK(vp);
68365963Spendry 
68465963Spendry 	return (error);
68565963Spendry }
68665963Spendry 
68765963Spendry int
68865963Spendry union_ioctl(ap)
68965963Spendry 	struct vop_ioctl_args /* {
69065963Spendry 		struct vnode *a_vp;
69165963Spendry 		int  a_command;
69265963Spendry 		caddr_t  a_data;
69365963Spendry 		int  a_fflag;
69465963Spendry 		struct ucred *a_cred;
69565963Spendry 		struct proc *a_p;
69665963Spendry 	} */ *ap;
69765963Spendry {
69865963Spendry 
69965963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
70065963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
70165963Spendry }
70265963Spendry 
70365963Spendry int
70465963Spendry union_select(ap)
70565963Spendry 	struct vop_select_args /* {
70665963Spendry 		struct vnode *a_vp;
70765963Spendry 		int  a_which;
70865963Spendry 		int  a_fflags;
70965963Spendry 		struct ucred *a_cred;
71065963Spendry 		struct proc *a_p;
71165963Spendry 	} */ *ap;
71265963Spendry {
71365963Spendry 
71465963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
71565963Spendry 				ap->a_cred, ap->a_p));
71665963Spendry }
71765963Spendry 
71865963Spendry int
71965963Spendry union_mmap(ap)
72065963Spendry 	struct vop_mmap_args /* {
72165963Spendry 		struct vnode *a_vp;
72265963Spendry 		int  a_fflags;
72365963Spendry 		struct ucred *a_cred;
72465963Spendry 		struct proc *a_p;
72565963Spendry 	} */ *ap;
72665963Spendry {
72765963Spendry 
72865963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
72965963Spendry 				ap->a_cred, ap->a_p));
73065963Spendry }
73165963Spendry 
73265963Spendry int
73365963Spendry union_fsync(ap)
73465963Spendry 	struct vop_fsync_args /* {
73565963Spendry 		struct vnode *a_vp;
73665963Spendry 		struct ucred *a_cred;
73765963Spendry 		int  a_waitfor;
73865963Spendry 		struct proc *a_p;
73965963Spendry 	} */ *ap;
74065963Spendry {
74165963Spendry 	int error = 0;
74265963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
74365963Spendry 
74465963Spendry 	if (targetvp) {
74566051Spendry 		int dolock = (targetvp == LOWERVP(ap->a_vp));
74666051Spendry 
74766051Spendry 		if (dolock)
74866051Spendry 			VOP_LOCK(targetvp);
74965963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
75065963Spendry 					ap->a_waitfor, ap->a_p);
75166051Spendry 		if (dolock)
75266051Spendry 			VOP_UNLOCK(targetvp);
75365963Spendry 	}
75465963Spendry 
75565963Spendry 	return (error);
75665963Spendry }
75765963Spendry 
75865963Spendry int
75965963Spendry union_seek(ap)
76065963Spendry 	struct vop_seek_args /* {
76165963Spendry 		struct vnode *a_vp;
76265963Spendry 		off_t  a_oldoff;
76365963Spendry 		off_t  a_newoff;
76465963Spendry 		struct ucred *a_cred;
76565963Spendry 	} */ *ap;
76665963Spendry {
76765963Spendry 
76865963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
76965963Spendry }
77065963Spendry 
77165963Spendry int
77265963Spendry union_remove(ap)
77365963Spendry 	struct vop_remove_args /* {
77465963Spendry 		struct vnode *a_dvp;
77565963Spendry 		struct vnode *a_vp;
77665963Spendry 		struct componentname *a_cnp;
77765963Spendry 	} */ *ap;
77865963Spendry {
77965963Spendry 	int error;
78065963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
78165963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
78265963Spendry 
78365963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
78465963Spendry 		struct vnode *dvp = dun->un_uppervp;
78565963Spendry 		struct vnode *vp = un->un_uppervp;
78665963Spendry 
78765963Spendry 		VREF(dvp);
78866051Spendry 		dun->un_flags |= UN_KLOCK;
78965963Spendry 		vput(ap->a_dvp);
79065963Spendry 		VREF(vp);
79166051Spendry 		un->un_flags |= UN_KLOCK;
79265963Spendry 		vput(ap->a_vp);
79365963Spendry 
79465963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
79566027Spendry 		if (!error)
79666027Spendry 			union_removed_upper(un);
79766027Spendry 
79866027Spendry 		/*
79966027Spendry 		 * XXX: should create a whiteout here
80066027Spendry 		 */
80165963Spendry 	} else {
80265963Spendry 		/*
80365963Spendry 		 * XXX: should create a whiteout here
80465963Spendry 		 */
80565963Spendry 		vput(ap->a_dvp);
80665963Spendry 		vput(ap->a_vp);
80765963Spendry 		error = EROFS;
80865963Spendry 	}
80965963Spendry 
81065963Spendry 	return (error);
81165963Spendry }
81265963Spendry 
81365963Spendry int
81465963Spendry union_link(ap)
81565963Spendry 	struct vop_link_args /* {
81665963Spendry 		struct vnode *a_vp;
81765963Spendry 		struct vnode *a_tdvp;
81865963Spendry 		struct componentname *a_cnp;
81965963Spendry 	} */ *ap;
82065963Spendry {
82165963Spendry 	int error;
82265963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
82365963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
82465963Spendry 
82565963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
82665963Spendry 		struct vnode *dvp = dun->un_uppervp;
82765963Spendry 		struct vnode *vp = un->un_uppervp;
82865963Spendry 
82965963Spendry 		VREF(dvp);
83066051Spendry 		dun->un_flags |= UN_KLOCK;
83165963Spendry 		vput(ap->a_vp);
83265963Spendry 		VREF(vp);
83365963Spendry 		vrele(ap->a_tdvp);
83465963Spendry 
83565963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
83665963Spendry 	} else {
83765963Spendry 		/*
83865963Spendry 		 * XXX: need to copy to upper layer
83965963Spendry 		 * and do the link there.
84065963Spendry 		 */
84165963Spendry 		vput(ap->a_vp);
84265963Spendry 		vrele(ap->a_tdvp);
84365963Spendry 		error = EROFS;
84465963Spendry 	}
84565963Spendry 
84665963Spendry 	return (error);
84765963Spendry }
84865963Spendry 
84965963Spendry int
85065963Spendry union_rename(ap)
85165963Spendry 	struct vop_rename_args  /* {
85265963Spendry 		struct vnode *a_fdvp;
85365963Spendry 		struct vnode *a_fvp;
85465963Spendry 		struct componentname *a_fcnp;
85565963Spendry 		struct vnode *a_tdvp;
85665963Spendry 		struct vnode *a_tvp;
85765963Spendry 		struct componentname *a_tcnp;
85865963Spendry 	} */ *ap;
85965963Spendry {
86065963Spendry 	int error;
86165963Spendry 
86265963Spendry 	struct vnode *fdvp = ap->a_fdvp;
86365963Spendry 	struct vnode *fvp = ap->a_fvp;
86465963Spendry 	struct vnode *tdvp = ap->a_tdvp;
86565963Spendry 	struct vnode *tvp = ap->a_tvp;
86665963Spendry 
86765963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
86865963Spendry 		struct union_node *un = VTOUNION(fdvp);
86965997Spendry 		if (un->un_uppervp == NULLVP) {
87065963Spendry 			error = EROFS;
87165963Spendry 			goto bad;
87265963Spendry 		}
87365963Spendry 
87465963Spendry 		fdvp = un->un_uppervp;
87565963Spendry 		VREF(fdvp);
87665963Spendry 		vrele(ap->a_fdvp);
87765963Spendry 	}
87865963Spendry 
87965963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
88065963Spendry 		struct union_node *un = VTOUNION(fvp);
88165997Spendry 		if (un->un_uppervp == NULLVP) {
88265963Spendry 			error = EROFS;
88365963Spendry 			goto bad;
88465963Spendry 		}
88565963Spendry 
88665963Spendry 		fvp = un->un_uppervp;
88765963Spendry 		VREF(fvp);
88865963Spendry 		vrele(ap->a_fvp);
88965963Spendry 	}
89065963Spendry 
89165963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
89265963Spendry 		struct union_node *un = VTOUNION(tdvp);
89365997Spendry 		if (un->un_uppervp == NULLVP) {
89465963Spendry 			error = EROFS;
89565963Spendry 			goto bad;
89665963Spendry 		}
89765963Spendry 
89865963Spendry 		tdvp = un->un_uppervp;
89965963Spendry 		VREF(tdvp);
90066051Spendry 		un->un_flags |= UN_KLOCK;
90165997Spendry 		vput(ap->a_tdvp);
90265963Spendry 	}
90365963Spendry 
90465963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
90565963Spendry 		struct union_node *un = VTOUNION(tvp);
90665997Spendry 		if (un->un_uppervp == NULLVP) {
90765963Spendry 			error = EROFS;
90865963Spendry 			goto bad;
90965963Spendry 		}
91065963Spendry 
91165963Spendry 		tvp = un->un_uppervp;
91265963Spendry 		VREF(tvp);
91366051Spendry 		un->un_flags |= UN_KLOCK;
91465963Spendry 		vput(ap->a_tvp);
91565963Spendry 	}
91665963Spendry 
91765963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
91865963Spendry 
91965963Spendry bad:
92065963Spendry 	vrele(fdvp);
92165963Spendry 	vrele(fvp);
92265963Spendry 	vput(tdvp);
92365963Spendry 	if (tvp)
92465963Spendry 		vput(tvp);
92565963Spendry 
92665963Spendry 	return (error);
92765963Spendry }
92865963Spendry 
92965963Spendry int
93065963Spendry union_mkdir(ap)
93165963Spendry 	struct vop_mkdir_args /* {
93265963Spendry 		struct vnode *a_dvp;
93365963Spendry 		struct vnode **a_vpp;
93465963Spendry 		struct componentname *a_cnp;
93565963Spendry 		struct vattr *a_vap;
93665963Spendry 	} */ *ap;
93765963Spendry {
93865963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
93965963Spendry 	struct vnode *dvp = un->un_uppervp;
94065963Spendry 
94165963Spendry 	if (dvp) {
94265963Spendry 		int error;
94365963Spendry 		struct vnode *vp;
94465963Spendry 
94565963Spendry 		VREF(dvp);
94666051Spendry 		un->un_flags |= UN_KLOCK;
94765963Spendry 		vput(ap->a_dvp);
94865963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
94965963Spendry 		if (error)
95065963Spendry 			return (error);
95165963Spendry 
95265963Spendry 		error = union_allocvp(
95365963Spendry 				ap->a_vpp,
95465989Spendry 				ap->a_dvp->v_mount,
95565989Spendry 				ap->a_dvp,
95665965Spendry 				NULLVP,
95765963Spendry 				ap->a_cnp,
95865963Spendry 				vp,
95965963Spendry 				NULLVP);
96065965Spendry 		if (error)
96166051Spendry 			vput(vp);
96265963Spendry 		return (error);
96365963Spendry 	}
96465963Spendry 
96565963Spendry 	vput(ap->a_dvp);
96665963Spendry 	return (EROFS);
96765963Spendry }
96865963Spendry 
96965963Spendry int
97065963Spendry union_rmdir(ap)
97165963Spendry 	struct vop_rmdir_args /* {
97265963Spendry 		struct vnode *a_dvp;
97365963Spendry 		struct vnode *a_vp;
97465963Spendry 		struct componentname *a_cnp;
97565963Spendry 	} */ *ap;
97665963Spendry {
97765963Spendry 	int error;
97865963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
97965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
98065963Spendry 
98165963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
98265963Spendry 		struct vnode *dvp = dun->un_uppervp;
98365963Spendry 		struct vnode *vp = un->un_uppervp;
98465963Spendry 
98565963Spendry 		VREF(dvp);
98666051Spendry 		dun->un_flags |= UN_KLOCK;
98765963Spendry 		vput(ap->a_dvp);
98865963Spendry 		VREF(vp);
98966051Spendry 		un->un_flags |= UN_KLOCK;
99065963Spendry 		vput(ap->a_vp);
99165963Spendry 
99266051Spendry 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
99366027Spendry 		if (!error)
99466027Spendry 			union_removed_upper(un);
99566027Spendry 
99666027Spendry 		/*
99766027Spendry 		 * XXX: should create a whiteout here
99866027Spendry 		 */
99965963Spendry 	} else {
100065963Spendry 		/*
100165963Spendry 		 * XXX: should create a whiteout here
100265963Spendry 		 */
100365963Spendry 		vput(ap->a_dvp);
100465963Spendry 		vput(ap->a_vp);
100565963Spendry 		error = EROFS;
100665963Spendry 	}
100765963Spendry 
100865963Spendry 	return (error);
100965963Spendry }
101065963Spendry 
101165963Spendry int
101265963Spendry union_symlink(ap)
101365963Spendry 	struct vop_symlink_args /* {
101465963Spendry 		struct vnode *a_dvp;
101565963Spendry 		struct vnode **a_vpp;
101665963Spendry 		struct componentname *a_cnp;
101765963Spendry 		struct vattr *a_vap;
101865963Spendry 		char *a_target;
101965963Spendry 	} */ *ap;
102065963Spendry {
102165963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
102265963Spendry 	struct vnode *dvp = un->un_uppervp;
102365963Spendry 
102465963Spendry 	if (dvp) {
102565963Spendry 		int error;
102665963Spendry 		struct vnode *vp;
102765963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
102865963Spendry 
102965963Spendry 		VREF(dvp);
103066051Spendry 		un->un_flags |= UN_KLOCK;
103165963Spendry 		vput(ap->a_dvp);
103265963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
103365963Spendry 					ap->a_vap, ap->a_target);
103465997Spendry 		*ap->a_vpp = NULLVP;
103565963Spendry 		return (error);
103665963Spendry 	}
103765963Spendry 
103865963Spendry 	vput(ap->a_dvp);
103965963Spendry 	return (EROFS);
104065963Spendry }
104165963Spendry 
104265935Spendry /*
104365935Spendry  * union_readdir works in concert with getdirentries and
104465935Spendry  * readdir(3) to provide a list of entries in the unioned
104565935Spendry  * directories.  getdirentries is responsible for walking
104665935Spendry  * down the union stack.  readdir(3) is responsible for
104765935Spendry  * eliminating duplicate names from the returned data stream.
104865935Spendry  */
104965935Spendry int
105065935Spendry union_readdir(ap)
105165935Spendry 	struct vop_readdir_args /* {
105265935Spendry 		struct vnodeop_desc *a_desc;
105365935Spendry 		struct vnode *a_vp;
105465935Spendry 		struct uio *a_uio;
105565935Spendry 		struct ucred *a_cred;
105665935Spendry 	} */ *ap;
105765935Spendry {
105865963Spendry 	int error = 0;
105965935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
106065935Spendry 
106166051Spendry 	if (un->un_uppervp)
106266051Spendry 		error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred);
106365935Spendry 
106465963Spendry 	return (error);
106565935Spendry }
106665935Spendry 
106765935Spendry int
106865963Spendry union_readlink(ap)
106965963Spendry 	struct vop_readlink_args /* {
107065963Spendry 		struct vnode *a_vp;
107165963Spendry 		struct uio *a_uio;
107265963Spendry 		struct ucred *a_cred;
107365963Spendry 	} */ *ap;
107465963Spendry {
107565963Spendry 	int error;
107665963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
107766051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
107865963Spendry 
107966051Spendry 	if (dolock)
108066051Spendry 		VOP_LOCK(vp);
108165963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
108266051Spendry 	if (dolock)
108366051Spendry 		VOP_UNLOCK(vp);
108465963Spendry 
108565963Spendry 	return (error);
108665963Spendry }
108765963Spendry 
108865963Spendry int
108965963Spendry union_abortop(ap)
109065963Spendry 	struct vop_abortop_args /* {
109165963Spendry 		struct vnode *a_dvp;
109265963Spendry 		struct componentname *a_cnp;
109365963Spendry 	} */ *ap;
109465963Spendry {
109565963Spendry 	int error;
109665965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
109765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
109865963Spendry 	int islocked = un->un_flags & UN_LOCKED;
109966051Spendry 	int dolock = (vp == LOWERVP(ap->a_dvp));
110065963Spendry 
110166051Spendry 	if (islocked && dolock)
110265963Spendry 		VOP_LOCK(vp);
110365963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
110466051Spendry 	if (islocked && dolock)
110565963Spendry 		VOP_UNLOCK(vp);
110665963Spendry 
110765963Spendry 	return (error);
110865963Spendry }
110965963Spendry 
111065963Spendry int
111165935Spendry union_inactive(ap)
111265935Spendry 	struct vop_inactive_args /* {
111365935Spendry 		struct vnode *a_vp;
111465935Spendry 	} */ *ap;
111565935Spendry {
111665935Spendry 
111765935Spendry 	/*
111865935Spendry 	 * Do nothing (and _don't_ bypass).
111965935Spendry 	 * Wait to vrele lowervp until reclaim,
112065935Spendry 	 * so that until then our union_node is in the
112165935Spendry 	 * cache and reusable.
112265935Spendry 	 *
112365935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
112465935Spendry 	 * the lowervp and then trying to reactivate it
112565935Spendry 	 * with capabilities (v_id)
112665935Spendry 	 * like they do in the name lookup cache code.
112765935Spendry 	 * That's too much work for now.
112865935Spendry 	 */
112965989Spendry 
113066027Spendry #ifdef UNION_DIAGNOSTIC
113165989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
113265989Spendry 
113365989Spendry 	if (un->un_flags & UN_LOCKED)
113465989Spendry 		panic("union: inactivating locked node");
113565989Spendry #endif
113665989Spendry 
113765935Spendry 	return (0);
113865935Spendry }
113965935Spendry 
114065935Spendry int
114165935Spendry union_reclaim(ap)
114265935Spendry 	struct vop_reclaim_args /* {
114365935Spendry 		struct vnode *a_vp;
114465935Spendry 	} */ *ap;
114565935Spendry {
114665935Spendry 
114766053Spendry 	union_freevp(ap->a_vp);
114866053Spendry 
114965935Spendry 	return (0);
115065935Spendry }
115165935Spendry 
115265963Spendry int
115365963Spendry union_lock(ap)
115465963Spendry 	struct vop_lock_args *ap;
115565963Spendry {
115665963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
115765935Spendry 
115866051Spendry 	if (un->un_uppervp) {
115966051Spendry 		if ((un->un_flags & UN_ULOCK) == 0) {
116066051Spendry 			VOP_LOCK(un->un_uppervp);
116166051Spendry 			un->un_flags |= UN_ULOCK;
116266051Spendry 		}
116366051Spendry #ifdef DIAGNOSTIC
116466051Spendry 		if (un->un_flags & UN_KLOCK)
116566051Spendry 			panic("union: dangling upper lock");
116666051Spendry #endif
116766051Spendry 	}
116866051Spendry 
116965965Spendry 	while (un->un_flags & UN_LOCKED) {
117065963Spendry #ifdef DIAGNOSTIC
117165989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
117265989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
117365989Spendry 			panic("union: locking against myself");
117465963Spendry #endif
117565963Spendry 		un->un_flags |= UN_WANT;
117665963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
117765963Spendry 	}
117865963Spendry 	un->un_flags |= UN_LOCKED;
117965989Spendry 
118065963Spendry #ifdef DIAGNOSTIC
118165989Spendry 	if (curproc)
118265989Spendry 		un->un_pid = curproc->p_pid;
118365989Spendry 	else
118465989Spendry 		un->un_pid = -1;
118565963Spendry #endif
118666028Spendry 
118766028Spendry 	return (0);
118865963Spendry }
118965963Spendry 
119065935Spendry int
119165963Spendry union_unlock(ap)
119265963Spendry 	struct vop_lock_args *ap;
119365963Spendry {
119465963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
119565963Spendry 
119665963Spendry #ifdef DIAGNOSTIC
119765965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
119865965Spendry 		panic("union: unlock unlocked node");
119965989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
120065989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
120165963Spendry 		panic("union: unlocking other process's union node");
120265963Spendry #endif
120365963Spendry 
120465963Spendry 	un->un_flags &= ~UN_LOCKED;
120566051Spendry 
120666051Spendry 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
120766051Spendry 		VOP_UNLOCK(un->un_uppervp);
120866051Spendry 
120966051Spendry 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
121066051Spendry 
121165963Spendry 	if (un->un_flags & UN_WANT) {
121265963Spendry 		un->un_flags &= ~UN_WANT;
121365963Spendry 		wakeup((caddr_t) &un->un_flags);
121465963Spendry 	}
121565963Spendry 
121665963Spendry #ifdef DIAGNOSTIC
121765963Spendry 	un->un_pid = 0;
121865963Spendry #endif
121966028Spendry 
122066028Spendry 	return (0);
122165963Spendry }
122265963Spendry 
122365963Spendry int
122465963Spendry union_bmap(ap)
122565963Spendry 	struct vop_bmap_args /* {
122665963Spendry 		struct vnode *a_vp;
122765963Spendry 		daddr_t  a_bn;
122865963Spendry 		struct vnode **a_vpp;
122965963Spendry 		daddr_t *a_bnp;
123065963Spendry 		int *a_runp;
123165963Spendry 	} */ *ap;
123265963Spendry {
123365963Spendry 	int error;
123465963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
123566051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
123665963Spendry 
123766051Spendry 	if (dolock)
123866051Spendry 		VOP_LOCK(vp);
123965963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
124066051Spendry 	if (dolock)
124166051Spendry 		VOP_UNLOCK(vp);
124265963Spendry 
124365963Spendry 	return (error);
124465963Spendry }
124565963Spendry 
124665963Spendry int
124765935Spendry union_print(ap)
124865935Spendry 	struct vop_print_args /* {
124965935Spendry 		struct vnode *a_vp;
125065935Spendry 	} */ *ap;
125165935Spendry {
125265935Spendry 	struct vnode *vp = ap->a_vp;
125365935Spendry 
125465935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
125565935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
125665935Spendry 	return (0);
125765935Spendry }
125865935Spendry 
125965963Spendry int
126065963Spendry union_islocked(ap)
126165963Spendry 	struct vop_islocked_args /* {
126265963Spendry 		struct vnode *a_vp;
126365963Spendry 	} */ *ap;
126465963Spendry {
126565935Spendry 
126665963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
126765963Spendry }
126865963Spendry 
126965935Spendry int
127065963Spendry union_pathconf(ap)
127165963Spendry 	struct vop_pathconf_args /* {
127265963Spendry 		struct vnode *a_vp;
127365963Spendry 		int a_name;
127465963Spendry 		int *a_retval;
127565935Spendry 	} */ *ap;
127665935Spendry {
127765935Spendry 	int error;
127865963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
127966051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
128065935Spendry 
128166051Spendry 	if (dolock)
128266051Spendry 		VOP_LOCK(vp);
128365963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
128466051Spendry 	if (dolock)
128566051Spendry 		VOP_UNLOCK(vp);
128665935Spendry 
128765963Spendry 	return (error);
128865963Spendry }
128965935Spendry 
129065963Spendry int
129165963Spendry union_advlock(ap)
129265963Spendry 	struct vop_advlock_args /* {
129365963Spendry 		struct vnode *a_vp;
129465963Spendry 		caddr_t  a_id;
129565963Spendry 		int  a_op;
129665963Spendry 		struct flock *a_fl;
129765963Spendry 		int  a_flags;
129865963Spendry 	} */ *ap;
129965963Spendry {
130065935Spendry 
130165963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
130265963Spendry 				ap->a_fl, ap->a_flags));
130365935Spendry }
130465935Spendry 
130565935Spendry 
130665935Spendry /*
130765963Spendry  * XXX - vop_strategy must be hand coded because it has no
130865935Spendry  * vnode in its arguments.
130965935Spendry  * This goes away with a merged VM/buffer cache.
131065935Spendry  */
131165935Spendry int
131265963Spendry union_strategy(ap)
131365963Spendry 	struct vop_strategy_args /* {
131465935Spendry 		struct buf *a_bp;
131565935Spendry 	} */ *ap;
131665935Spendry {
131765935Spendry 	struct buf *bp = ap->a_bp;
131865935Spendry 	int error;
131965935Spendry 	struct vnode *savedvp;
132065935Spendry 
132165935Spendry 	savedvp = bp->b_vp;
132265963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
132365935Spendry 
132465935Spendry #ifdef DIAGNOSTIC
132565997Spendry 	if (bp->b_vp == NULLVP)
132665963Spendry 		panic("union_strategy: nil vp");
132765963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
132865963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
132965963Spendry 		panic("union_strategy: writing to lowervp");
133065935Spendry #endif
133165935Spendry 
133265963Spendry 	error = VOP_STRATEGY(bp);
133365935Spendry 	bp->b_vp = savedvp;
133465935Spendry 
133565935Spendry 	return (error);
133665935Spendry }
133765935Spendry 
133865935Spendry /*
133965935Spendry  * Global vfs data structures
134065935Spendry  */
134165935Spendry int (**union_vnodeop_p)();
134265965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
134365963Spendry 	{ &vop_default_desc, vn_default_error },
134465963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
134565963Spendry 	{ &vop_create_desc, union_create },		/* create */
134665963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
134765963Spendry 	{ &vop_open_desc, union_open },			/* open */
134865963Spendry 	{ &vop_close_desc, union_close },		/* close */
134965963Spendry 	{ &vop_access_desc, union_access },		/* access */
135065963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
135165963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
135265963Spendry 	{ &vop_read_desc, union_read },			/* read */
135365963Spendry 	{ &vop_write_desc, union_write },		/* write */
135465963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
135565963Spendry 	{ &vop_select_desc, union_select },		/* select */
135665963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
135765963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
135865963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
135965963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
136065963Spendry 	{ &vop_link_desc, union_link },			/* link */
136165963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
136265963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
136365963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
136465963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
136565963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
136665963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
136765963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
136865963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
136965963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
137065963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
137165963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
137265963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
137365963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
137465963Spendry 	{ &vop_print_desc, union_print },		/* print */
137565963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
137665963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
137765963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
137865963Spendry #ifdef notdef
137965963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
138065963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
138165963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
138265963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
138365963Spendry 	{ &vop_update_desc, union_update },		/* update */
138465963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
138565963Spendry #endif
138665935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
138765935Spendry };
138865935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
138965935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1390