xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 66053)
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*66053Spendry  *	@(#)union_vnops.c	2.2 (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>
25*66053Spendry #include <sys/queue.h>
2665935Spendry #include "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 */
398*66053Spendry 			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 
42466051Spendry 				un->un_flags &= ~UN_ULOCK;
42565965Spendry 				VOP_UNLOCK(un->un_uppervp);
42666028Spendry 				union_vn_close(un->un_uppervp, FWRITE, cred, p);
42765965Spendry 				VOP_LOCK(un->un_uppervp);
42866051Spendry 				un->un_flags |= UN_ULOCK;
42966051Spendry 
43065997Spendry 				if (!error)
43166027Spendry 					uprintf("union: copied up %s\n",
43265997Spendry 								un->un_path);
43365935Spendry 			}
43465997Spendry 
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 
57165963Spendry 	if (un->un_uppervp) {
57265963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
57365963Spendry 					ap->a_cred, ap->a_p);
57465963Spendry 	} else {
57565963Spendry 		/*
57665963Spendry 		 * XXX should do a copyfile (perhaps only if
57765963Spendry 		 * the file permission change, which would not
57865963Spendry 		 * track va_ctime correctly).
57965963Spendry 		 */
58065963Spendry 		error = EROFS;
58165963Spendry 	}
58265963Spendry 
58365963Spendry 	return (error);
58465963Spendry }
58565963Spendry 
58665963Spendry int
58765963Spendry union_read(ap)
58865963Spendry 	struct vop_read_args /* {
58965963Spendry 		struct vnode *a_vp;
59065963Spendry 		struct uio *a_uio;
59165963Spendry 		int  a_ioflag;
59265963Spendry 		struct ucred *a_cred;
59365963Spendry 	} */ *ap;
59465963Spendry {
59565963Spendry 	int error;
59665963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
59766051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
59865963Spendry 
59966051Spendry 	if (dolock)
60066051Spendry 		VOP_LOCK(vp);
60165963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
60266051Spendry 	if (dolock)
60366051Spendry 		VOP_UNLOCK(vp);
60465963Spendry 
60565963Spendry 	return (error);
60665963Spendry }
60765963Spendry 
60865963Spendry int
60965963Spendry union_write(ap)
61065963Spendry 	struct vop_read_args /* {
61165963Spendry 		struct vnode *a_vp;
61265963Spendry 		struct uio *a_uio;
61365963Spendry 		int  a_ioflag;
61465963Spendry 		struct ucred *a_cred;
61565963Spendry 	} */ *ap;
61665963Spendry {
61765963Spendry 	int error;
61865963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
61966051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
62065963Spendry 
62166051Spendry 	if (dolock)
62266051Spendry 		VOP_LOCK(vp);
62365963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
62466051Spendry 	if (dolock)
62566051Spendry 		VOP_UNLOCK(vp);
62665963Spendry 
62765963Spendry 	return (error);
62865963Spendry }
62965963Spendry 
63065963Spendry int
63165963Spendry union_ioctl(ap)
63265963Spendry 	struct vop_ioctl_args /* {
63365963Spendry 		struct vnode *a_vp;
63465963Spendry 		int  a_command;
63565963Spendry 		caddr_t  a_data;
63665963Spendry 		int  a_fflag;
63765963Spendry 		struct ucred *a_cred;
63865963Spendry 		struct proc *a_p;
63965963Spendry 	} */ *ap;
64065963Spendry {
64165963Spendry 
64265963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
64365963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
64465963Spendry }
64565963Spendry 
64665963Spendry int
64765963Spendry union_select(ap)
64865963Spendry 	struct vop_select_args /* {
64965963Spendry 		struct vnode *a_vp;
65065963Spendry 		int  a_which;
65165963Spendry 		int  a_fflags;
65265963Spendry 		struct ucred *a_cred;
65365963Spendry 		struct proc *a_p;
65465963Spendry 	} */ *ap;
65565963Spendry {
65665963Spendry 
65765963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
65865963Spendry 				ap->a_cred, ap->a_p));
65965963Spendry }
66065963Spendry 
66165963Spendry int
66265963Spendry union_mmap(ap)
66365963Spendry 	struct vop_mmap_args /* {
66465963Spendry 		struct vnode *a_vp;
66565963Spendry 		int  a_fflags;
66665963Spendry 		struct ucred *a_cred;
66765963Spendry 		struct proc *a_p;
66865963Spendry 	} */ *ap;
66965963Spendry {
67065963Spendry 
67165963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
67265963Spendry 				ap->a_cred, ap->a_p));
67365963Spendry }
67465963Spendry 
67565963Spendry int
67665963Spendry union_fsync(ap)
67765963Spendry 	struct vop_fsync_args /* {
67865963Spendry 		struct vnode *a_vp;
67965963Spendry 		struct ucred *a_cred;
68065963Spendry 		int  a_waitfor;
68165963Spendry 		struct proc *a_p;
68265963Spendry 	} */ *ap;
68365963Spendry {
68465963Spendry 	int error = 0;
68565963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
68665963Spendry 
68765963Spendry 	if (targetvp) {
68866051Spendry 		int dolock = (targetvp == LOWERVP(ap->a_vp));
68966051Spendry 
69066051Spendry 		if (dolock)
69166051Spendry 			VOP_LOCK(targetvp);
69265963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
69365963Spendry 					ap->a_waitfor, ap->a_p);
69466051Spendry 		if (dolock)
69566051Spendry 			VOP_UNLOCK(targetvp);
69665963Spendry 	}
69765963Spendry 
69865963Spendry 	return (error);
69965963Spendry }
70065963Spendry 
70165963Spendry int
70265963Spendry union_seek(ap)
70365963Spendry 	struct vop_seek_args /* {
70465963Spendry 		struct vnode *a_vp;
70565963Spendry 		off_t  a_oldoff;
70665963Spendry 		off_t  a_newoff;
70765963Spendry 		struct ucred *a_cred;
70865963Spendry 	} */ *ap;
70965963Spendry {
71065963Spendry 
71165963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
71265963Spendry }
71365963Spendry 
71465963Spendry int
71565963Spendry union_remove(ap)
71665963Spendry 	struct vop_remove_args /* {
71765963Spendry 		struct vnode *a_dvp;
71865963Spendry 		struct vnode *a_vp;
71965963Spendry 		struct componentname *a_cnp;
72065963Spendry 	} */ *ap;
72165963Spendry {
72265963Spendry 	int error;
72365963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
72465963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
72565963Spendry 
72665963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
72765963Spendry 		struct vnode *dvp = dun->un_uppervp;
72865963Spendry 		struct vnode *vp = un->un_uppervp;
72965963Spendry 
73065963Spendry 		VREF(dvp);
73166051Spendry 		dun->un_flags |= UN_KLOCK;
73265963Spendry 		vput(ap->a_dvp);
73365963Spendry 		VREF(vp);
73466051Spendry 		un->un_flags |= UN_KLOCK;
73565963Spendry 		vput(ap->a_vp);
73665963Spendry 
73765963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
73866027Spendry 		if (!error)
73966027Spendry 			union_removed_upper(un);
74066027Spendry 
74166027Spendry 		/*
74266027Spendry 		 * XXX: should create a whiteout here
74366027Spendry 		 */
74465963Spendry 	} else {
74565963Spendry 		/*
74665963Spendry 		 * XXX: should create a whiteout here
74765963Spendry 		 */
74865963Spendry 		vput(ap->a_dvp);
74965963Spendry 		vput(ap->a_vp);
75065963Spendry 		error = EROFS;
75165963Spendry 	}
75265963Spendry 
75365963Spendry 	return (error);
75465963Spendry }
75565963Spendry 
75665963Spendry int
75765963Spendry union_link(ap)
75865963Spendry 	struct vop_link_args /* {
75965963Spendry 		struct vnode *a_vp;
76065963Spendry 		struct vnode *a_tdvp;
76165963Spendry 		struct componentname *a_cnp;
76265963Spendry 	} */ *ap;
76365963Spendry {
76465963Spendry 	int error;
76565963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
76665963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
76765963Spendry 
76865963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
76965963Spendry 		struct vnode *dvp = dun->un_uppervp;
77065963Spendry 		struct vnode *vp = un->un_uppervp;
77165963Spendry 
77265963Spendry 		VREF(dvp);
77366051Spendry 		dun->un_flags |= UN_KLOCK;
77465963Spendry 		vput(ap->a_vp);
77565963Spendry 		VREF(vp);
77665963Spendry 		vrele(ap->a_tdvp);
77765963Spendry 
77865963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
77965963Spendry 	} else {
78065963Spendry 		/*
78165963Spendry 		 * XXX: need to copy to upper layer
78265963Spendry 		 * and do the link there.
78365963Spendry 		 */
78465963Spendry 		vput(ap->a_vp);
78565963Spendry 		vrele(ap->a_tdvp);
78665963Spendry 		error = EROFS;
78765963Spendry 	}
78865963Spendry 
78965963Spendry 	return (error);
79065963Spendry }
79165963Spendry 
79265963Spendry int
79365963Spendry union_rename(ap)
79465963Spendry 	struct vop_rename_args  /* {
79565963Spendry 		struct vnode *a_fdvp;
79665963Spendry 		struct vnode *a_fvp;
79765963Spendry 		struct componentname *a_fcnp;
79865963Spendry 		struct vnode *a_tdvp;
79965963Spendry 		struct vnode *a_tvp;
80065963Spendry 		struct componentname *a_tcnp;
80165963Spendry 	} */ *ap;
80265963Spendry {
80365963Spendry 	int error;
80465963Spendry 
80565963Spendry 	struct vnode *fdvp = ap->a_fdvp;
80665963Spendry 	struct vnode *fvp = ap->a_fvp;
80765963Spendry 	struct vnode *tdvp = ap->a_tdvp;
80865963Spendry 	struct vnode *tvp = ap->a_tvp;
80965963Spendry 
81065963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
81165963Spendry 		struct union_node *un = VTOUNION(fdvp);
81265997Spendry 		if (un->un_uppervp == NULLVP) {
81365963Spendry 			error = EROFS;
81465963Spendry 			goto bad;
81565963Spendry 		}
81665963Spendry 
81765963Spendry 		fdvp = un->un_uppervp;
81865963Spendry 		VREF(fdvp);
81965963Spendry 		vrele(ap->a_fdvp);
82065963Spendry 	}
82165963Spendry 
82265963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
82365963Spendry 		struct union_node *un = VTOUNION(fvp);
82465997Spendry 		if (un->un_uppervp == NULLVP) {
82565963Spendry 			error = EROFS;
82665963Spendry 			goto bad;
82765963Spendry 		}
82865963Spendry 
82965963Spendry 		fvp = un->un_uppervp;
83065963Spendry 		VREF(fvp);
83165963Spendry 		vrele(ap->a_fvp);
83265963Spendry 	}
83365963Spendry 
83465963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
83565963Spendry 		struct union_node *un = VTOUNION(tdvp);
83665997Spendry 		if (un->un_uppervp == NULLVP) {
83765963Spendry 			error = EROFS;
83865963Spendry 			goto bad;
83965963Spendry 		}
84065963Spendry 
84165963Spendry 		tdvp = un->un_uppervp;
84265963Spendry 		VREF(tdvp);
84366051Spendry 		un->un_flags |= UN_KLOCK;
84465997Spendry 		vput(ap->a_tdvp);
84565963Spendry 	}
84665963Spendry 
84765963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
84865963Spendry 		struct union_node *un = VTOUNION(tvp);
84965997Spendry 		if (un->un_uppervp == NULLVP) {
85065963Spendry 			error = EROFS;
85165963Spendry 			goto bad;
85265963Spendry 		}
85365963Spendry 
85465963Spendry 		tvp = un->un_uppervp;
85565963Spendry 		VREF(tvp);
85666051Spendry 		un->un_flags |= UN_KLOCK;
85765963Spendry 		vput(ap->a_tvp);
85865963Spendry 	}
85965963Spendry 
86065963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
86165963Spendry 
86265963Spendry bad:
86365963Spendry 	vrele(fdvp);
86465963Spendry 	vrele(fvp);
86565963Spendry 	vput(tdvp);
86665963Spendry 	if (tvp)
86765963Spendry 		vput(tvp);
86865963Spendry 
86965963Spendry 	return (error);
87065963Spendry }
87165963Spendry 
87265963Spendry int
87365963Spendry union_mkdir(ap)
87465963Spendry 	struct vop_mkdir_args /* {
87565963Spendry 		struct vnode *a_dvp;
87665963Spendry 		struct vnode **a_vpp;
87765963Spendry 		struct componentname *a_cnp;
87865963Spendry 		struct vattr *a_vap;
87965963Spendry 	} */ *ap;
88065963Spendry {
88165963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
88265963Spendry 	struct vnode *dvp = un->un_uppervp;
88365963Spendry 
88465963Spendry 	if (dvp) {
88565963Spendry 		int error;
88665963Spendry 		struct vnode *vp;
88765963Spendry 
88865963Spendry 		VREF(dvp);
88966051Spendry 		un->un_flags |= UN_KLOCK;
89065963Spendry 		vput(ap->a_dvp);
89165963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
89265963Spendry 		if (error)
89365963Spendry 			return (error);
89465963Spendry 
89565963Spendry 		error = union_allocvp(
89665963Spendry 				ap->a_vpp,
89765989Spendry 				ap->a_dvp->v_mount,
89865989Spendry 				ap->a_dvp,
89965965Spendry 				NULLVP,
90065963Spendry 				ap->a_cnp,
90165963Spendry 				vp,
90265963Spendry 				NULLVP);
90365965Spendry 		if (error)
90466051Spendry 			vput(vp);
90565963Spendry 		return (error);
90665963Spendry 	}
90765963Spendry 
90865963Spendry 	vput(ap->a_dvp);
90965963Spendry 	return (EROFS);
91065963Spendry }
91165963Spendry 
91265963Spendry int
91365963Spendry union_rmdir(ap)
91465963Spendry 	struct vop_rmdir_args /* {
91565963Spendry 		struct vnode *a_dvp;
91665963Spendry 		struct vnode *a_vp;
91765963Spendry 		struct componentname *a_cnp;
91865963Spendry 	} */ *ap;
91965963Spendry {
92065963Spendry 	int error;
92165963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
92265963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
92365963Spendry 
92465963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
92565963Spendry 		struct vnode *dvp = dun->un_uppervp;
92665963Spendry 		struct vnode *vp = un->un_uppervp;
92765963Spendry 
92865963Spendry 		VREF(dvp);
92966051Spendry 		dun->un_flags |= UN_KLOCK;
93065963Spendry 		vput(ap->a_dvp);
93165963Spendry 		VREF(vp);
93266051Spendry 		un->un_flags |= UN_KLOCK;
93365963Spendry 		vput(ap->a_vp);
93465963Spendry 
93566051Spendry 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
93666027Spendry 		if (!error)
93766027Spendry 			union_removed_upper(un);
93866027Spendry 
93966027Spendry 		/*
94066027Spendry 		 * XXX: should create a whiteout here
94166027Spendry 		 */
94265963Spendry 	} else {
94365963Spendry 		/*
94465963Spendry 		 * XXX: should create a whiteout here
94565963Spendry 		 */
94665963Spendry 		vput(ap->a_dvp);
94765963Spendry 		vput(ap->a_vp);
94865963Spendry 		error = EROFS;
94965963Spendry 	}
95065963Spendry 
95165963Spendry 	return (error);
95265963Spendry }
95365963Spendry 
95465963Spendry int
95565963Spendry union_symlink(ap)
95665963Spendry 	struct vop_symlink_args /* {
95765963Spendry 		struct vnode *a_dvp;
95865963Spendry 		struct vnode **a_vpp;
95965963Spendry 		struct componentname *a_cnp;
96065963Spendry 		struct vattr *a_vap;
96165963Spendry 		char *a_target;
96265963Spendry 	} */ *ap;
96365963Spendry {
96465963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
96565963Spendry 	struct vnode *dvp = un->un_uppervp;
96665963Spendry 
96765963Spendry 	if (dvp) {
96865963Spendry 		int error;
96965963Spendry 		struct vnode *vp;
97065963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
97165963Spendry 
97265963Spendry 		VREF(dvp);
97366051Spendry 		un->un_flags |= UN_KLOCK;
97465963Spendry 		vput(ap->a_dvp);
97565963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
97665963Spendry 					ap->a_vap, ap->a_target);
97765997Spendry 		*ap->a_vpp = NULLVP;
97865963Spendry 		return (error);
97965963Spendry 	}
98065963Spendry 
98165963Spendry 	vput(ap->a_dvp);
98265963Spendry 	return (EROFS);
98365963Spendry }
98465963Spendry 
98565935Spendry /*
98665935Spendry  * union_readdir works in concert with getdirentries and
98765935Spendry  * readdir(3) to provide a list of entries in the unioned
98865935Spendry  * directories.  getdirentries is responsible for walking
98965935Spendry  * down the union stack.  readdir(3) is responsible for
99065935Spendry  * eliminating duplicate names from the returned data stream.
99165935Spendry  */
99265935Spendry int
99365935Spendry union_readdir(ap)
99465935Spendry 	struct vop_readdir_args /* {
99565935Spendry 		struct vnodeop_desc *a_desc;
99665935Spendry 		struct vnode *a_vp;
99765935Spendry 		struct uio *a_uio;
99865935Spendry 		struct ucred *a_cred;
99965935Spendry 	} */ *ap;
100065935Spendry {
100165963Spendry 	int error = 0;
100265935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
100365935Spendry 
100466051Spendry 	if (un->un_uppervp)
100566051Spendry 		error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred);
100665935Spendry 
100765963Spendry 	return (error);
100865935Spendry }
100965935Spendry 
101065935Spendry int
101165963Spendry union_readlink(ap)
101265963Spendry 	struct vop_readlink_args /* {
101365963Spendry 		struct vnode *a_vp;
101465963Spendry 		struct uio *a_uio;
101565963Spendry 		struct ucred *a_cred;
101665963Spendry 	} */ *ap;
101765963Spendry {
101865963Spendry 	int error;
101965963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
102066051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
102165963Spendry 
102266051Spendry 	if (dolock)
102366051Spendry 		VOP_LOCK(vp);
102465963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
102566051Spendry 	if (dolock)
102666051Spendry 		VOP_UNLOCK(vp);
102765963Spendry 
102865963Spendry 	return (error);
102965963Spendry }
103065963Spendry 
103165963Spendry int
103265963Spendry union_abortop(ap)
103365963Spendry 	struct vop_abortop_args /* {
103465963Spendry 		struct vnode *a_dvp;
103565963Spendry 		struct componentname *a_cnp;
103665963Spendry 	} */ *ap;
103765963Spendry {
103865963Spendry 	int error;
103965965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
104065963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
104165963Spendry 	int islocked = un->un_flags & UN_LOCKED;
104266051Spendry 	int dolock = (vp == LOWERVP(ap->a_dvp));
104365963Spendry 
104466051Spendry 	if (islocked && dolock)
104565963Spendry 		VOP_LOCK(vp);
104665963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
104766051Spendry 	if (islocked && dolock)
104865963Spendry 		VOP_UNLOCK(vp);
104965963Spendry 
105065963Spendry 	return (error);
105165963Spendry }
105265963Spendry 
105365963Spendry int
105465935Spendry union_inactive(ap)
105565935Spendry 	struct vop_inactive_args /* {
105665935Spendry 		struct vnode *a_vp;
105765935Spendry 	} */ *ap;
105865935Spendry {
105965935Spendry 
106065935Spendry 	/*
106165935Spendry 	 * Do nothing (and _don't_ bypass).
106265935Spendry 	 * Wait to vrele lowervp until reclaim,
106365935Spendry 	 * so that until then our union_node is in the
106465935Spendry 	 * cache and reusable.
106565935Spendry 	 *
106665935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
106765935Spendry 	 * the lowervp and then trying to reactivate it
106865935Spendry 	 * with capabilities (v_id)
106965935Spendry 	 * like they do in the name lookup cache code.
107065935Spendry 	 * That's too much work for now.
107165935Spendry 	 */
107265989Spendry 
107366027Spendry #ifdef UNION_DIAGNOSTIC
107465989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
107565989Spendry 
107665989Spendry 	if (un->un_flags & UN_LOCKED)
107765989Spendry 		panic("union: inactivating locked node");
107865989Spendry #endif
107965989Spendry 
108065935Spendry 	return (0);
108165935Spendry }
108265935Spendry 
108365935Spendry int
108465935Spendry union_reclaim(ap)
108565935Spendry 	struct vop_reclaim_args /* {
108665935Spendry 		struct vnode *a_vp;
108765935Spendry 	} */ *ap;
108865935Spendry {
108965935Spendry 
1090*66053Spendry 	union_freevp(ap->a_vp);
1091*66053Spendry 
109265935Spendry 	return (0);
109365935Spendry }
109465935Spendry 
109565963Spendry int
109665963Spendry union_lock(ap)
109765963Spendry 	struct vop_lock_args *ap;
109865963Spendry {
109965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
110065935Spendry 
110166051Spendry 	if (un->un_uppervp) {
110266051Spendry 		if ((un->un_flags & UN_ULOCK) == 0) {
110366051Spendry 			VOP_LOCK(un->un_uppervp);
110466051Spendry 			un->un_flags |= UN_ULOCK;
110566051Spendry 		}
110666051Spendry #ifdef DIAGNOSTIC
110766051Spendry 		if (un->un_flags & UN_KLOCK)
110866051Spendry 			panic("union: dangling upper lock");
110966051Spendry #endif
111066051Spendry 	}
111166051Spendry 
111265965Spendry 	while (un->un_flags & UN_LOCKED) {
111365963Spendry #ifdef DIAGNOSTIC
111465989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
111565989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
111665989Spendry 			panic("union: locking against myself");
111765963Spendry #endif
111865963Spendry 		un->un_flags |= UN_WANT;
111965963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
112065963Spendry 	}
112165963Spendry 	un->un_flags |= UN_LOCKED;
112265989Spendry 
112365963Spendry #ifdef DIAGNOSTIC
112465989Spendry 	if (curproc)
112565989Spendry 		un->un_pid = curproc->p_pid;
112665989Spendry 	else
112765989Spendry 		un->un_pid = -1;
112865963Spendry #endif
112966028Spendry 
113066028Spendry 	return (0);
113165963Spendry }
113265963Spendry 
113365935Spendry int
113465963Spendry union_unlock(ap)
113565963Spendry 	struct vop_lock_args *ap;
113665963Spendry {
113765963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
113865963Spendry 
113965963Spendry #ifdef DIAGNOSTIC
114065965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
114165965Spendry 		panic("union: unlock unlocked node");
114265989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
114365989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
114465963Spendry 		panic("union: unlocking other process's union node");
114565963Spendry #endif
114665963Spendry 
114765963Spendry 	un->un_flags &= ~UN_LOCKED;
114866051Spendry 
114966051Spendry 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
115066051Spendry 		VOP_UNLOCK(un->un_uppervp);
115166051Spendry 
115266051Spendry 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
115366051Spendry 
115465963Spendry 	if (un->un_flags & UN_WANT) {
115565963Spendry 		un->un_flags &= ~UN_WANT;
115665963Spendry 		wakeup((caddr_t) &un->un_flags);
115765963Spendry 	}
115865963Spendry 
115965963Spendry #ifdef DIAGNOSTIC
116065963Spendry 	un->un_pid = 0;
116165963Spendry #endif
116266028Spendry 
116366028Spendry 	return (0);
116465963Spendry }
116565963Spendry 
116665963Spendry int
116765963Spendry union_bmap(ap)
116865963Spendry 	struct vop_bmap_args /* {
116965963Spendry 		struct vnode *a_vp;
117065963Spendry 		daddr_t  a_bn;
117165963Spendry 		struct vnode **a_vpp;
117265963Spendry 		daddr_t *a_bnp;
117365963Spendry 		int *a_runp;
117465963Spendry 	} */ *ap;
117565963Spendry {
117665963Spendry 	int error;
117765963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
117866051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
117965963Spendry 
118066051Spendry 	if (dolock)
118166051Spendry 		VOP_LOCK(vp);
118265963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
118366051Spendry 	if (dolock)
118466051Spendry 		VOP_UNLOCK(vp);
118565963Spendry 
118665963Spendry 	return (error);
118765963Spendry }
118865963Spendry 
118965963Spendry int
119065935Spendry union_print(ap)
119165935Spendry 	struct vop_print_args /* {
119265935Spendry 		struct vnode *a_vp;
119365935Spendry 	} */ *ap;
119465935Spendry {
119565935Spendry 	struct vnode *vp = ap->a_vp;
119665935Spendry 
119765935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
119865935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
119965935Spendry 	return (0);
120065935Spendry }
120165935Spendry 
120265963Spendry int
120365963Spendry union_islocked(ap)
120465963Spendry 	struct vop_islocked_args /* {
120565963Spendry 		struct vnode *a_vp;
120665963Spendry 	} */ *ap;
120765963Spendry {
120865935Spendry 
120965963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
121065963Spendry }
121165963Spendry 
121265935Spendry int
121365963Spendry union_pathconf(ap)
121465963Spendry 	struct vop_pathconf_args /* {
121565963Spendry 		struct vnode *a_vp;
121665963Spendry 		int a_name;
121765963Spendry 		int *a_retval;
121865935Spendry 	} */ *ap;
121965935Spendry {
122065935Spendry 	int error;
122165963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
122266051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
122365935Spendry 
122466051Spendry 	if (dolock)
122566051Spendry 		VOP_LOCK(vp);
122665963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
122766051Spendry 	if (dolock)
122866051Spendry 		VOP_UNLOCK(vp);
122965935Spendry 
123065963Spendry 	return (error);
123165963Spendry }
123265935Spendry 
123365963Spendry int
123465963Spendry union_advlock(ap)
123565963Spendry 	struct vop_advlock_args /* {
123665963Spendry 		struct vnode *a_vp;
123765963Spendry 		caddr_t  a_id;
123865963Spendry 		int  a_op;
123965963Spendry 		struct flock *a_fl;
124065963Spendry 		int  a_flags;
124165963Spendry 	} */ *ap;
124265963Spendry {
124365935Spendry 
124465963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
124565963Spendry 				ap->a_fl, ap->a_flags));
124665935Spendry }
124765935Spendry 
124865935Spendry 
124965935Spendry /*
125065963Spendry  * XXX - vop_strategy must be hand coded because it has no
125165935Spendry  * vnode in its arguments.
125265935Spendry  * This goes away with a merged VM/buffer cache.
125365935Spendry  */
125465935Spendry int
125565963Spendry union_strategy(ap)
125665963Spendry 	struct vop_strategy_args /* {
125765935Spendry 		struct buf *a_bp;
125865935Spendry 	} */ *ap;
125965935Spendry {
126065935Spendry 	struct buf *bp = ap->a_bp;
126165935Spendry 	int error;
126265935Spendry 	struct vnode *savedvp;
126365935Spendry 
126465935Spendry 	savedvp = bp->b_vp;
126565963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
126665935Spendry 
126765935Spendry #ifdef DIAGNOSTIC
126865997Spendry 	if (bp->b_vp == NULLVP)
126965963Spendry 		panic("union_strategy: nil vp");
127065963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
127165963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
127265963Spendry 		panic("union_strategy: writing to lowervp");
127365935Spendry #endif
127465935Spendry 
127565963Spendry 	error = VOP_STRATEGY(bp);
127665935Spendry 	bp->b_vp = savedvp;
127765935Spendry 
127865935Spendry 	return (error);
127965935Spendry }
128065935Spendry 
128165935Spendry /*
128265935Spendry  * Global vfs data structures
128365935Spendry  */
128465935Spendry int (**union_vnodeop_p)();
128565965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
128665963Spendry 	{ &vop_default_desc, vn_default_error },
128765963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
128865963Spendry 	{ &vop_create_desc, union_create },		/* create */
128965963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
129065963Spendry 	{ &vop_open_desc, union_open },			/* open */
129165963Spendry 	{ &vop_close_desc, union_close },		/* close */
129265963Spendry 	{ &vop_access_desc, union_access },		/* access */
129365963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
129465963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
129565963Spendry 	{ &vop_read_desc, union_read },			/* read */
129665963Spendry 	{ &vop_write_desc, union_write },		/* write */
129765963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
129865963Spendry 	{ &vop_select_desc, union_select },		/* select */
129965963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
130065963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
130165963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
130265963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
130365963Spendry 	{ &vop_link_desc, union_link },			/* link */
130465963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
130565963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
130665963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
130765963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
130865963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
130965963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
131065963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
131165963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
131265963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
131365963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
131465963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
131565963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
131665963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
131765963Spendry 	{ &vop_print_desc, union_print },		/* print */
131865963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
131965963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
132065963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
132165963Spendry #ifdef notdef
132265963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
132365963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
132465963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
132565963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
132665963Spendry 	{ &vop_update_desc, union_update },		/* update */
132765963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
132865963Spendry #endif
132965935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
133065935Spendry };
133165935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
133265935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1333