xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 65997)
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*65997Spendry  *	@(#)union_vnops.c	1.6 (Berkeley) 02/04/94
1265935Spendry  */
1365935Spendry 
1465935Spendry #include <sys/param.h>
1565935Spendry #include <sys/systm.h>
1665935Spendry #include <sys/proc.h>
1765935Spendry #include <sys/file.h>
1865935Spendry #include <sys/time.h>
1965935Spendry #include <sys/types.h>
2065935Spendry #include <sys/vnode.h>
2165935Spendry #include <sys/mount.h>
2265935Spendry #include <sys/namei.h>
2365935Spendry #include <sys/malloc.h>
2465935Spendry #include <sys/buf.h>
2565935Spendry #include "union.h"
2665935Spendry 
2765935Spendry static int
2865989Spendry union_lookup1(udvp, dvp, vpp, cnp)
2965989Spendry 	struct vnode *udvp;
3065935Spendry 	struct vnode *dvp;
3165935Spendry 	struct vnode **vpp;
3265935Spendry 	struct componentname *cnp;
3365935Spendry {
3465935Spendry 	int error;
3565935Spendry 	struct vnode *tdvp;
3665935Spendry 	struct mount *mp;
3765935Spendry 
3865994Spendry 	/*
3965994Spendry 	 * If stepping up the directory tree, check for going
4065994Spendry 	 * back across the mount point, in which case do what
4165994Spendry 	 * lookup would do by stepping back down the mount
4265994Spendry 	 * hierarchy.
4365994Spendry 	 */
4465935Spendry 	if (cnp->cn_flags & ISDOTDOT) {
4565935Spendry 		for (;;) {
4665935Spendry 			if ((dvp->v_flag & VROOT) == 0 ||
4765935Spendry 			    (cnp->cn_flags & NOCROSSMOUNT))
4865935Spendry 				break;
4965935Spendry 
5065935Spendry 			tdvp = dvp;
5165935Spendry 			dvp = dvp->v_mount->mnt_vnodecovered;
5265935Spendry 			vput(tdvp);
5365935Spendry 			VREF(dvp);
5465935Spendry 			VOP_LOCK(dvp);
5565935Spendry 		}
5665935Spendry 	}
5765935Spendry 
5865935Spendry         error = VOP_LOOKUP(dvp, &tdvp, cnp);
5965935Spendry 	if (error)
6065935Spendry 		return (error);
6165935Spendry 
6265994Spendry 	/*
6365994Spendry 	 * If going back up the directory tree, then the parent directory
6465994Spendry 	 * will have been unlocked, unless lookup found the last
6565994Spendry 	 * component.  In which case, re-lock the node here to allow
6665994Spendry 	 * it to be unlocked again (phew) in union_lookup.
6765994Spendry 	 */
6865994Spendry 	if ((cnp->cn_flags & ISDOTDOT) && !(cnp->cn_flags & ISLASTCN))
6965994Spendry 		VOP_LOCK(dvp);
7065994Spendry 
7165935Spendry 	dvp = tdvp;
7265994Spendry 
7365994Spendry 	/*
7465994Spendry 	 * Lastly check if the current node is a mount point in
7565994Spendry 	 * which cse walk up the mount hierarchy making sure not to
7665994Spendry 	 * bump into the root of the mount tree (ie. dvp != udvp).
7765994Spendry 	 */
7865989Spendry 	while (dvp != udvp && (dvp->v_type == VDIR) &&
7965989Spendry 	       (mp = dvp->v_mountedhere) &&
8065935Spendry 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
8165935Spendry 
8265935Spendry 		if (mp->mnt_flag & MNT_MLOCK) {
8365935Spendry 			mp->mnt_flag |= MNT_MWAIT;
8465935Spendry 			sleep((caddr_t) mp, PVFS);
8565935Spendry 			continue;
8665935Spendry 		}
8765935Spendry 
8865935Spendry 		if (error = VFS_ROOT(mp, &tdvp)) {
8965935Spendry 			vput(dvp);
9065935Spendry 			return (error);
9165935Spendry 		}
9265935Spendry 
9365965Spendry 		vput(dvp);
9465935Spendry 		dvp = tdvp;
9565935Spendry 	}
9665935Spendry 
9765935Spendry 	*vpp = dvp;
9865935Spendry 	return (0);
9965935Spendry }
10065935Spendry 
10165935Spendry int
10265935Spendry union_lookup(ap)
10365935Spendry 	struct vop_lookup_args /* {
10465935Spendry 		struct vnodeop_desc *a_desc;
10565935Spendry 		struct vnode *a_dvp;
10665935Spendry 		struct vnode **a_vpp;
10765935Spendry 		struct componentname *a_cnp;
10865935Spendry 	} */ *ap;
10965935Spendry {
11065965Spendry 	int error;
11165935Spendry 	int uerror, lerror;
11265935Spendry 	struct vnode *uppervp, *lowervp;
11365935Spendry 	struct vnode *upperdvp, *lowerdvp;
11465935Spendry 	struct vnode *dvp = ap->a_dvp;
11565989Spendry 	struct union_node *dun = VTOUNION(dvp);
11665935Spendry 	struct componentname *cnp = ap->a_cnp;
11765935Spendry 	int lockparent = cnp->cn_flags & LOCKPARENT;
11865994Spendry 	int rdonly = cnp->cn_flags & RDONLY;
119*65997Spendry 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
12065935Spendry 
12165965Spendry 	cnp->cn_flags |= LOCKPARENT;
12265965Spendry 
12365935Spendry 	upperdvp = dun->un_uppervp;
12465935Spendry 	lowerdvp = dun->un_lowervp;
125*65997Spendry 	uppervp = NULLVP;
126*65997Spendry 	lowervp = NULLVP;
12765935Spendry 
12865935Spendry 	/*
12965935Spendry 	 * do the lookup in the upper level.
13065935Spendry 	 * if that level comsumes additional pathnames,
13165935Spendry 	 * then assume that something special is going
13265935Spendry 	 * on and just return that vnode.
13365935Spendry 	 */
13465935Spendry 	if (upperdvp) {
13565965Spendry 		VOP_LOCK(upperdvp);
136*65997Spendry 		uerror = union_lookup1(um->um_uppervp, upperdvp,
137*65997Spendry 					&uppervp, cnp);
13865989Spendry 		if (uppervp != upperdvp)
13965989Spendry 			VOP_UNLOCK(upperdvp);
14065965Spendry 
14165935Spendry 		if (cnp->cn_consume != 0) {
14265935Spendry 			*ap->a_vpp = uppervp;
14365965Spendry 			if (!lockparent)
14465965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
14565935Spendry 			return (uerror);
14665935Spendry 		}
14765935Spendry 	} else {
14865935Spendry 		uerror = ENOENT;
14965935Spendry 	}
15065935Spendry 
15165935Spendry 	/*
15265935Spendry 	 * in a similar way to the upper layer, do the lookup
15365935Spendry 	 * in the lower layer.   this time, if there is some
15465935Spendry 	 * component magic going on, then vput whatever we got
15565935Spendry 	 * back from the upper layer and return the lower vnode
15665935Spendry 	 * instead.
15765935Spendry 	 */
15865935Spendry 	if (lowerdvp) {
15965965Spendry 		VOP_LOCK(lowerdvp);
160*65997Spendry 		lerror = union_lookup1(um->um_lowervp, lowerdvp,
161*65997Spendry 					&lowervp, cnp);
16265989Spendry 		if (lowervp != lowerdvp)
16365989Spendry 			VOP_UNLOCK(lowerdvp);
16465965Spendry 
16565935Spendry 		if (cnp->cn_consume != 0) {
16665935Spendry 			if (uppervp) {
16765935Spendry 				vput(uppervp);
168*65997Spendry 				uppervp = NULLVP;
16965935Spendry 			}
17065935Spendry 			*ap->a_vpp = lowervp;
17165965Spendry 			if (!lockparent)
17265965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
17365935Spendry 			return (lerror);
17465935Spendry 		}
17565935Spendry 	} else {
17665935Spendry 		lerror = ENOENT;
17765935Spendry 	}
17865935Spendry 
17965965Spendry 	if (!lockparent)
18065965Spendry 		cnp->cn_flags &= ~LOCKPARENT;
18165965Spendry 
18265935Spendry 	/*
18365935Spendry 	 * at this point, we have uerror and lerror indicating
18465935Spendry 	 * possible errors with the lookups in the upper and lower
18565935Spendry 	 * layers.  additionally, uppervp and lowervp are (locked)
18665935Spendry 	 * references to existing vnodes in the upper and lower layers.
18765935Spendry 	 *
18865935Spendry 	 * there are now three cases to consider.
18965935Spendry 	 * 1. if both layers returned an error, then return whatever
19065935Spendry 	 *    error the upper layer generated.
19165935Spendry 	 *
19265935Spendry 	 * 2. if the top layer failed and the bottom layer succeeded
19365935Spendry 	 *    then two subcases occur.
19465935Spendry 	 *    a.  the bottom vnode is not a directory, in which
19565935Spendry 	 *	  case just return a new union vnode referencing
19665935Spendry 	 *	  an empty top layer and the existing bottom layer.
19765935Spendry 	 *    b.  the bottom vnode is a directory, in which case
19865935Spendry 	 *	  create a new directory in the top-level and
19965935Spendry 	 *	  continue as in case 3.
20065935Spendry 	 *
20165935Spendry 	 * 3. if the top layer succeeded then return a new union
20265935Spendry 	 *    vnode referencing whatever the new top layer and
20365935Spendry 	 *    whatever the bottom layer returned.
20465935Spendry 	 */
20565935Spendry 
20665935Spendry 	/* case 1. */
20765935Spendry 	if ((uerror != 0) && (lerror != 0)) {
208*65997Spendry 		*ap->a_vpp = NULLVP;
20965935Spendry 		return (uerror);
21065935Spendry 	}
21165935Spendry 
21265935Spendry 	/* case 2. */
21365935Spendry 	if (uerror != 0 /* && (lerror == 0) */ ) {
21465935Spendry 		if (lowervp->v_type == VDIR) { /* case 2b. */
215*65997Spendry 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
21665935Spendry 			if (uerror) {
21765935Spendry 				if (lowervp) {
21865935Spendry 					vput(lowervp);
219*65997Spendry 					lowervp = NULLVP;
22065935Spendry 				}
22165935Spendry 				return (uerror);
22265935Spendry 			}
22365935Spendry 		}
22465935Spendry 	}
22565935Spendry 
22665965Spendry 	if (uppervp)
22765965Spendry 		VOP_UNLOCK(uppervp);
22865965Spendry 	if (lowervp)
22965965Spendry 		VOP_UNLOCK(lowervp);
23065965Spendry 
231*65997Spendry 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
232*65997Spendry 			      uppervp, lowervp);
233*65997Spendry 
23465965Spendry 	if (error) {
23565965Spendry 		if (uppervp)
23665965Spendry 			vrele(uppervp);
23765965Spendry 		if (lowervp)
23865965Spendry 			vrele(lowervp);
23965965Spendry 	} else {
24065994Spendry 		if (*ap->a_vpp != dvp)
24165994Spendry 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
24265994Spendry 				VOP_UNLOCK(dvp);
24365965Spendry 	}
24465965Spendry 
24565965Spendry 	return (error);
24665935Spendry }
24765935Spendry 
24865963Spendry int
24965963Spendry union_create(ap)
25065963Spendry 	struct vop_create_args /* {
25165963Spendry 		struct vnode *a_dvp;
25265963Spendry 		struct vnode **a_vpp;
25365963Spendry 		struct componentname *a_cnp;
25465963Spendry 		struct vattr *a_vap;
25565963Spendry 	} */ *ap;
25665963Spendry {
25765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
25865963Spendry 	struct vnode *dvp = un->un_uppervp;
25965963Spendry 
26065963Spendry 	if (dvp) {
26165963Spendry 		int error;
26265963Spendry 		struct vnode *vp;
26365963Spendry 
26465963Spendry 		VREF(dvp);
26565963Spendry 		VOP_LOCK(dvp);
26665963Spendry 		vput(ap->a_dvp);
26765963Spendry 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
26865963Spendry 		if (error)
26965963Spendry 			return (error);
27065963Spendry 
271*65997Spendry 		VOP_UNLOCK(vp);
272*65997Spendry 
27365963Spendry 		error = union_allocvp(
27465963Spendry 				ap->a_vpp,
27565989Spendry 				ap->a_dvp->v_mount,
27665989Spendry 				ap->a_dvp,
27765965Spendry 				NULLVP,
27865963Spendry 				ap->a_cnp,
27965963Spendry 				vp,
28065963Spendry 				NULLVP);
28165965Spendry 		if (error)
28265965Spendry 			vrele(vp);
28365963Spendry 		return (error);
28465963Spendry 	}
28565963Spendry 
28665963Spendry 	vput(ap->a_dvp);
28765963Spendry 	return (EROFS);
28865963Spendry }
28965963Spendry 
29065963Spendry int
29165963Spendry union_mknod(ap)
29265963Spendry 	struct vop_mknod_args /* {
29365963Spendry 		struct vnode *a_dvp;
29465963Spendry 		struct vnode **a_vpp;
29565963Spendry 		struct componentname *a_cnp;
29665963Spendry 		struct vattr *a_vap;
29765963Spendry 	} */ *ap;
29865963Spendry {
29965963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
30065963Spendry 	struct vnode *dvp = un->un_uppervp;
30165963Spendry 
30265963Spendry 	if (dvp) {
30365963Spendry 		int error;
30465963Spendry 		struct vnode *vp;
30565963Spendry 
30665963Spendry 		VREF(dvp);
30765963Spendry 		VOP_LOCK(dvp);
30865963Spendry 		vput(ap->a_dvp);
30965963Spendry 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
31065963Spendry 		if (error)
31165963Spendry 			return (error);
31265963Spendry 
31365965Spendry 		if (vp) {
314*65997Spendry 			VOP_UNLOCK(vp);
315*65997Spendry 
31665965Spendry 			error = union_allocvp(
31765965Spendry 					ap->a_vpp,
31865989Spendry 					ap->a_dvp->v_mount,
31965989Spendry 					ap->a_dvp,
32065965Spendry 					NULLVP,
32165965Spendry 					ap->a_cnp,
32265965Spendry 					vp,
32365965Spendry 					NULLVP);
32465965Spendry 			if (error)
32565965Spendry 				vrele(vp);
32665965Spendry 		}
32765963Spendry 		return (error);
32865963Spendry 	}
32965963Spendry 
33065963Spendry 	vput(ap->a_dvp);
33165963Spendry 	return (EROFS);
33265963Spendry }
33365963Spendry 
33465935Spendry int
33565935Spendry union_open(ap)
33665935Spendry 	struct vop_open_args /* {
33765935Spendry 		struct vnodeop_desc *a_desc;
33865935Spendry 		struct vnode *a_vp;
33965935Spendry 		int a_mode;
34065935Spendry 		struct ucred *a_cred;
34165935Spendry 		struct proc *a_p;
34265935Spendry 	} */ *ap;
34365935Spendry {
34465935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
34565965Spendry 	struct vnode *tvp;
34665935Spendry 	int mode = ap->a_mode;
34765935Spendry 	struct ucred *cred = ap->a_cred;
34865935Spendry 	struct proc *p = ap->a_p;
34965965Spendry 	int error;
35065935Spendry 
35165935Spendry 	/*
35265935Spendry 	 * If there is an existing upper vp then simply open that.
35365935Spendry 	 */
35465965Spendry 	tvp = un->un_uppervp;
35565965Spendry 	if (tvp == NULLVP) {
35665935Spendry 		/*
35765965Spendry 		 * If the lower vnode is being opened for writing, then
35865965Spendry 		 * copy the file contents to the upper vnode and open that,
35965965Spendry 		 * otherwise can simply open the lower vnode.
36065935Spendry 		 */
36165965Spendry 		tvp = un->un_lowervp;
36265965Spendry 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
36365994Spendry 			struct vnode *vp;
364*65997Spendry 			int i;
36565963Spendry 
36665965Spendry 			/*
36765965Spendry 			 * Open the named file in the upper layer.  Note that
36865965Spendry 			 * the file may have come into existence *since* the
36965965Spendry 			 * lookup was done, since the upper layer may really
37065965Spendry 			 * be a loopback mount of some other filesystem...
37165965Spendry 			 * so open the file with exclusive create and barf if
37265965Spendry 			 * it already exists.
37365965Spendry 			 * XXX - perhaps shoudl re-lookup the node (once more
37465965Spendry 			 * with feeling) and simply open that.  Who knows.
37565965Spendry 			 */
376*65997Spendry 			error = union_vn_create(&vp, un, p);
37765965Spendry 			if (error)
37865965Spendry 				return (error);
37965994Spendry 			un->un_uppervp = vp;	/* XXX */
38065965Spendry 			/* at this point, uppervp is locked */
38165965Spendry 
38265965Spendry 			/*
38365965Spendry 			 * Now, if the file is being opened with truncation,
38465965Spendry 			 * then the (new) upper vnode is ready to fly,
38565965Spendry 			 * otherwise the data from the lower vnode must be
38665965Spendry 			 * copied to the upper layer first.  This only works
38765965Spendry 			 * for regular files (check is made above).
38865965Spendry 			 */
38965965Spendry 			if ((mode & O_TRUNC) == 0) {
39065965Spendry 				/*
39165965Spendry 				 * XXX - should not ignore errors
39265965Spendry 				 * from VOP_CLOSE
39365965Spendry 				 */
39465994Spendry 				VOP_LOCK(tvp);
39565965Spendry 				error = VOP_OPEN(tvp, FREAD, cred, p);
39665965Spendry 				if (error == 0) {
39765965Spendry 					error = union_copyfile(p, cred,
39865965Spendry 						       tvp, un->un_uppervp);
39965965Spendry 					VOP_UNLOCK(tvp);
40065965Spendry 					(void) VOP_CLOSE(tvp, FREAD);
40165965Spendry 				} else {
40265965Spendry 					VOP_UNLOCK(tvp);
40365965Spendry 				}
40465965Spendry 				VOP_UNLOCK(un->un_uppervp);
40565965Spendry 				(void) VOP_CLOSE(un->un_uppervp, FWRITE);
40665965Spendry 				VOP_LOCK(un->un_uppervp);
407*65997Spendry 				if (!error)
408*65997Spendry 					uprintf("union: copied up\n",
409*65997Spendry 								un->un_path);
41065935Spendry 			}
411*65997Spendry 
412*65997Spendry 			/*
413*65997Spendry 			 * Subsequent IOs will go to the top layer, so
414*65997Spendry 			 * call close on the lower vnode and open on the
415*65997Spendry 			 * upper vnode to ensure that the filesystem keeps
416*65997Spendry 			 * its references counts right.  This doesn't do
417*65997Spendry 			 * the right thing with (cred) and (FREAD) though.
418*65997Spendry 			 * Ignoring error returns is not righ, either.
419*65997Spendry 			 */
420*65997Spendry 			for (i = 0; i < un->un_open; i++) {
421*65997Spendry 				(void) VOP_CLOSE(tvp, FREAD);
422*65997Spendry 				(void) VOP_OPEN(un->un_uppervp, FREAD, cred, p);
423*65997Spendry 			}
424*65997Spendry 			un->un_open = 0;
425*65997Spendry 
42665965Spendry 			if (error == 0)
42765965Spendry 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
42865963Spendry 			VOP_UNLOCK(un->un_uppervp);
42965965Spendry 			return (error);
43065935Spendry 		}
431*65997Spendry 		un->un_open++;
43265935Spendry 	}
43365935Spendry 
43465965Spendry 	VOP_LOCK(tvp);
43565965Spendry 	error = VOP_OPEN(tvp, mode, cred, p);
43665965Spendry 	VOP_UNLOCK(tvp);
43765965Spendry 
43865965Spendry 	return (error);
43965935Spendry }
44065935Spendry 
44165963Spendry int
44265963Spendry union_close(ap)
44365963Spendry 	struct vop_close_args /* {
44465963Spendry 		struct vnode *a_vp;
44565963Spendry 		int  a_fflag;
44665963Spendry 		struct ucred *a_cred;
44765963Spendry 		struct proc *a_p;
44865963Spendry 	} */ *ap;
44965963Spendry {
450*65997Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
451*65997Spendry 	struct vnode *vp;
45265963Spendry 
453*65997Spendry 	if (un->un_uppervp) {
454*65997Spendry 		vp = un->un_uppervp;
455*65997Spendry 	} else {
456*65997Spendry #ifdef DIAGNOSTIC
457*65997Spendry 		if (un->un_open <= 0)
458*65997Spendry 			panic("union: un_open cnt");
459*65997Spendry #endif
460*65997Spendry 		--un->un_open;
461*65997Spendry 		vp = un->un_lowervp;
462*65997Spendry 	}
463*65997Spendry 
464*65997Spendry 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
46565963Spendry }
46665963Spendry 
46765935Spendry /*
46865963Spendry  * Check access permission on the union vnode.
46965963Spendry  * The access check being enforced is to check
47065963Spendry  * against both the underlying vnode, and any
47165963Spendry  * copied vnode.  This ensures that no additional
47265963Spendry  * file permissions are given away simply because
47365963Spendry  * the user caused an implicit file copy.
47465963Spendry  */
47565963Spendry int
47665963Spendry union_access(ap)
47765963Spendry 	struct vop_access_args /* {
47865963Spendry 		struct vnodeop_desc *a_desc;
47965963Spendry 		struct vnode *a_vp;
48065963Spendry 		int a_mode;
48165963Spendry 		struct ucred *a_cred;
48265963Spendry 		struct proc *a_p;
48365963Spendry 	} */ *ap;
48465963Spendry {
48565963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
48665965Spendry 	int error = 0;
48765963Spendry 	struct vnode *vp;
48865963Spendry 
48965963Spendry 	if (vp = un->un_lowervp) {
49065965Spendry 		VOP_LOCK(vp);
49165963Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
49265965Spendry 		VOP_UNLOCK(vp);
49365963Spendry 		if (error)
49465963Spendry 			return (error);
49565963Spendry 	}
49665963Spendry 
49765965Spendry 	if (vp = un->un_uppervp) {
49865965Spendry 		VOP_LOCK(vp);
49965965Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
50065965Spendry 		VOP_UNLOCK(vp);
50165965Spendry 	}
50265965Spendry 
50365965Spendry 	return (error);
50465963Spendry }
50565963Spendry 
50665963Spendry /*
50765935Spendry  *  We handle getattr only to change the fsid.
50865935Spendry  */
50965935Spendry int
51065935Spendry union_getattr(ap)
51165935Spendry 	struct vop_getattr_args /* {
51265935Spendry 		struct vnode *a_vp;
51365935Spendry 		struct vattr *a_vap;
51465935Spendry 		struct ucred *a_cred;
51565935Spendry 		struct proc *a_p;
51665935Spendry 	} */ *ap;
51765935Spendry {
51865935Spendry 	int error;
51965965Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
52065935Spendry 
52165965Spendry 	VOP_LOCK(vp);
52265965Spendry 	error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
52365965Spendry 	VOP_UNLOCK(vp);
52465965Spendry 
52565935Spendry 	/* Requires that arguments be restored. */
52665935Spendry 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
52765935Spendry 	return (0);
52865935Spendry }
52965935Spendry 
53065963Spendry int
53165965Spendry union_setattr(ap)
53265963Spendry 	struct vop_setattr_args /* {
53365963Spendry 		struct vnode *a_vp;
53465963Spendry 		struct vattr *a_vap;
53565963Spendry 		struct ucred *a_cred;
53665963Spendry 		struct proc *a_p;
53765963Spendry 	} */ *ap;
53865963Spendry {
53965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
54065963Spendry 	int error;
54165963Spendry 
54265963Spendry 	if (un->un_uppervp) {
54365963Spendry 		VOP_LOCK(un->un_uppervp);
54465963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
54565963Spendry 					ap->a_cred, ap->a_p);
54665963Spendry 		VOP_UNLOCK(un->un_uppervp);
54765963Spendry 	} else {
54865963Spendry 		/*
54965963Spendry 		 * XXX should do a copyfile (perhaps only if
55065963Spendry 		 * the file permission change, which would not
55165963Spendry 		 * track va_ctime correctly).
55265963Spendry 		 */
55365963Spendry 		error = EROFS;
55465963Spendry 	}
55565963Spendry 
55665963Spendry 	return (error);
55765963Spendry }
55865963Spendry 
55965963Spendry int
56065963Spendry union_read(ap)
56165963Spendry 	struct vop_read_args /* {
56265963Spendry 		struct vnode *a_vp;
56365963Spendry 		struct uio *a_uio;
56465963Spendry 		int  a_ioflag;
56565963Spendry 		struct ucred *a_cred;
56665963Spendry 	} */ *ap;
56765963Spendry {
56865963Spendry 	int error;
56965963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
57065963Spendry 
57165965Spendry 	VOP_LOCK(vp);
57265963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
57365965Spendry 	VOP_UNLOCK(vp);
57465963Spendry 
57565963Spendry 	return (error);
57665963Spendry }
57765963Spendry 
57865963Spendry int
57965963Spendry union_write(ap)
58065963Spendry 	struct vop_read_args /* {
58165963Spendry 		struct vnode *a_vp;
58265963Spendry 		struct uio *a_uio;
58365963Spendry 		int  a_ioflag;
58465963Spendry 		struct ucred *a_cred;
58565963Spendry 	} */ *ap;
58665963Spendry {
58765963Spendry 	int error;
58865963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
58965963Spendry 
59065963Spendry 	VOP_LOCK(vp);
59165963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
59265963Spendry 	VOP_UNLOCK(vp);
59365963Spendry 
59465963Spendry 	return (error);
59565963Spendry }
59665963Spendry 
59765963Spendry int
59865963Spendry union_ioctl(ap)
59965963Spendry 	struct vop_ioctl_args /* {
60065963Spendry 		struct vnode *a_vp;
60165963Spendry 		int  a_command;
60265963Spendry 		caddr_t  a_data;
60365963Spendry 		int  a_fflag;
60465963Spendry 		struct ucred *a_cred;
60565963Spendry 		struct proc *a_p;
60665963Spendry 	} */ *ap;
60765963Spendry {
60865963Spendry 
60965963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
61065963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
61165963Spendry }
61265963Spendry 
61365963Spendry int
61465963Spendry union_select(ap)
61565963Spendry 	struct vop_select_args /* {
61665963Spendry 		struct vnode *a_vp;
61765963Spendry 		int  a_which;
61865963Spendry 		int  a_fflags;
61965963Spendry 		struct ucred *a_cred;
62065963Spendry 		struct proc *a_p;
62165963Spendry 	} */ *ap;
62265963Spendry {
62365963Spendry 
62465963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
62565963Spendry 				ap->a_cred, ap->a_p));
62665963Spendry }
62765963Spendry 
62865963Spendry int
62965963Spendry union_mmap(ap)
63065963Spendry 	struct vop_mmap_args /* {
63165963Spendry 		struct vnode *a_vp;
63265963Spendry 		int  a_fflags;
63365963Spendry 		struct ucred *a_cred;
63465963Spendry 		struct proc *a_p;
63565963Spendry 	} */ *ap;
63665963Spendry {
63765963Spendry 
63865963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
63965963Spendry 				ap->a_cred, ap->a_p));
64065963Spendry }
64165963Spendry 
64265963Spendry int
64365963Spendry union_fsync(ap)
64465963Spendry 	struct vop_fsync_args /* {
64565963Spendry 		struct vnode *a_vp;
64665963Spendry 		struct ucred *a_cred;
64765963Spendry 		int  a_waitfor;
64865963Spendry 		struct proc *a_p;
64965963Spendry 	} */ *ap;
65065963Spendry {
65165963Spendry 	int error = 0;
65265963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
65365963Spendry 
65465963Spendry 	if (targetvp) {
65565963Spendry 		VOP_LOCK(targetvp);
65665963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
65765963Spendry 					ap->a_waitfor, ap->a_p);
65865963Spendry 		VOP_UNLOCK(targetvp);
65965963Spendry 	}
66065963Spendry 
66165963Spendry 	return (error);
66265963Spendry }
66365963Spendry 
66465963Spendry int
66565963Spendry union_seek(ap)
66665963Spendry 	struct vop_seek_args /* {
66765963Spendry 		struct vnode *a_vp;
66865963Spendry 		off_t  a_oldoff;
66965963Spendry 		off_t  a_newoff;
67065963Spendry 		struct ucred *a_cred;
67165963Spendry 	} */ *ap;
67265963Spendry {
67365963Spendry 
67465963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
67565963Spendry }
67665963Spendry 
67765963Spendry int
67865963Spendry union_remove(ap)
67965963Spendry 	struct vop_remove_args /* {
68065963Spendry 		struct vnode *a_dvp;
68165963Spendry 		struct vnode *a_vp;
68265963Spendry 		struct componentname *a_cnp;
68365963Spendry 	} */ *ap;
68465963Spendry {
68565963Spendry 	int error;
68665963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
68765963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
68865963Spendry 
68965963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
69065963Spendry 		struct vnode *dvp = dun->un_uppervp;
69165963Spendry 		struct vnode *vp = un->un_uppervp;
69265963Spendry 
69365963Spendry 		VREF(dvp);
69465963Spendry 		VOP_LOCK(dvp);
69565963Spendry 		vput(ap->a_dvp);
69665963Spendry 		VREF(vp);
69765963Spendry 		VOP_LOCK(vp);
69865963Spendry 		vput(ap->a_vp);
69965963Spendry 
70065963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
70165963Spendry 	} else {
70265963Spendry 		/*
70365963Spendry 		 * XXX: should create a whiteout here
70465963Spendry 		 */
70565963Spendry 		vput(ap->a_dvp);
70665963Spendry 		vput(ap->a_vp);
70765963Spendry 		error = EROFS;
70865963Spendry 	}
70965963Spendry 
71065963Spendry 	return (error);
71165963Spendry }
71265963Spendry 
71365963Spendry int
71465963Spendry union_link(ap)
71565963Spendry 	struct vop_link_args /* {
71665963Spendry 		struct vnode *a_vp;
71765963Spendry 		struct vnode *a_tdvp;
71865963Spendry 		struct componentname *a_cnp;
71965963Spendry 	} */ *ap;
72065963Spendry {
72165963Spendry 	int error;
72265963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
72365963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
72465963Spendry 
72565963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
72665963Spendry 		struct vnode *dvp = dun->un_uppervp;
72765963Spendry 		struct vnode *vp = un->un_uppervp;
72865963Spendry 
72965963Spendry 		VREF(dvp);
73065963Spendry 		VOP_LOCK(dvp);
73165963Spendry 		vput(ap->a_vp);
73265963Spendry 		VREF(vp);
73365963Spendry 		vrele(ap->a_tdvp);
73465963Spendry 
73565963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
73665963Spendry 	} else {
73765963Spendry 		/*
73865963Spendry 		 * XXX: need to copy to upper layer
73965963Spendry 		 * and do the link there.
74065963Spendry 		 */
74165963Spendry 		vput(ap->a_vp);
74265963Spendry 		vrele(ap->a_tdvp);
74365963Spendry 		error = EROFS;
74465963Spendry 	}
74565963Spendry 
74665963Spendry 	return (error);
74765963Spendry }
74865963Spendry 
74965963Spendry int
75065963Spendry union_rename(ap)
75165963Spendry 	struct vop_rename_args  /* {
75265963Spendry 		struct vnode *a_fdvp;
75365963Spendry 		struct vnode *a_fvp;
75465963Spendry 		struct componentname *a_fcnp;
75565963Spendry 		struct vnode *a_tdvp;
75665963Spendry 		struct vnode *a_tvp;
75765963Spendry 		struct componentname *a_tcnp;
75865963Spendry 	} */ *ap;
75965963Spendry {
76065963Spendry 	int error;
76165963Spendry 
76265963Spendry 	struct vnode *fdvp = ap->a_fdvp;
76365963Spendry 	struct vnode *fvp = ap->a_fvp;
76465963Spendry 	struct vnode *tdvp = ap->a_tdvp;
76565963Spendry 	struct vnode *tvp = ap->a_tvp;
76665963Spendry 
76765963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
76865963Spendry 		struct union_node *un = VTOUNION(fdvp);
769*65997Spendry 		if (un->un_uppervp == NULLVP) {
77065963Spendry 			error = EROFS;
77165963Spendry 			goto bad;
77265963Spendry 		}
77365963Spendry 
77465963Spendry 		fdvp = un->un_uppervp;
77565963Spendry 		VREF(fdvp);
77665963Spendry 		vrele(ap->a_fdvp);
77765963Spendry 	}
77865963Spendry 
77965963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
78065963Spendry 		struct union_node *un = VTOUNION(fvp);
781*65997Spendry 		if (un->un_uppervp == NULLVP) {
78265963Spendry 			error = EROFS;
78365963Spendry 			goto bad;
78465963Spendry 		}
78565963Spendry 
78665963Spendry 		fvp = un->un_uppervp;
78765963Spendry 		VREF(fvp);
78865963Spendry 		vrele(ap->a_fvp);
78965963Spendry 	}
79065963Spendry 
79165963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
79265963Spendry 		struct union_node *un = VTOUNION(tdvp);
793*65997Spendry 		if (un->un_uppervp == NULLVP) {
79465963Spendry 			error = EROFS;
79565963Spendry 			goto bad;
79665963Spendry 		}
79765963Spendry 
79865963Spendry 		tdvp = un->un_uppervp;
79965963Spendry 		VREF(tdvp);
80065963Spendry 		VOP_LOCK(tdvp);
801*65997Spendry 		vput(ap->a_tdvp);
80265963Spendry 	}
80365963Spendry 
80465963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
80565963Spendry 		struct union_node *un = VTOUNION(tvp);
806*65997Spendry 		if (un->un_uppervp == NULLVP) {
80765963Spendry 			error = EROFS;
80865963Spendry 			goto bad;
80965963Spendry 		}
81065963Spendry 
81165963Spendry 		tvp = un->un_uppervp;
81265963Spendry 		VREF(tvp);
81365963Spendry 		VOP_LOCK(tvp);
81465963Spendry 		vput(ap->a_tvp);
81565963Spendry 	}
81665963Spendry 
81765963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
81865963Spendry 
81965963Spendry bad:
82065963Spendry 	vrele(fdvp);
82165963Spendry 	vrele(fvp);
82265963Spendry 	vput(tdvp);
82365963Spendry 	if (tvp)
82465963Spendry 		vput(tvp);
82565963Spendry 
82665963Spendry 	return (error);
82765963Spendry }
82865963Spendry 
82965963Spendry int
83065963Spendry union_mkdir(ap)
83165963Spendry 	struct vop_mkdir_args /* {
83265963Spendry 		struct vnode *a_dvp;
83365963Spendry 		struct vnode **a_vpp;
83465963Spendry 		struct componentname *a_cnp;
83565963Spendry 		struct vattr *a_vap;
83665963Spendry 	} */ *ap;
83765963Spendry {
83865963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
83965963Spendry 	struct vnode *dvp = un->un_uppervp;
84065963Spendry 
84165963Spendry 	if (dvp) {
84265963Spendry 		int error;
84365963Spendry 		struct vnode *vp;
84465963Spendry 
84565963Spendry 		VREF(dvp);
84665963Spendry 		VOP_LOCK(dvp);
84765963Spendry 		vput(ap->a_dvp);
84865963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
84965963Spendry 		if (error)
85065963Spendry 			return (error);
85165963Spendry 
852*65997Spendry 		VOP_UNLOCK(vp);
85365963Spendry 		error = union_allocvp(
85465963Spendry 				ap->a_vpp,
85565989Spendry 				ap->a_dvp->v_mount,
85665989Spendry 				ap->a_dvp,
85765965Spendry 				NULLVP,
85865963Spendry 				ap->a_cnp,
85965963Spendry 				vp,
86065963Spendry 				NULLVP);
86165965Spendry 		if (error)
86265965Spendry 			vrele(vp);
86365963Spendry 		return (error);
86465963Spendry 	}
86565963Spendry 
86665963Spendry 	vput(ap->a_dvp);
86765963Spendry 	return (EROFS);
86865963Spendry }
86965963Spendry 
87065963Spendry int
87165963Spendry union_rmdir(ap)
87265963Spendry 	struct vop_rmdir_args /* {
87365963Spendry 		struct vnode *a_dvp;
87465963Spendry 		struct vnode *a_vp;
87565963Spendry 		struct componentname *a_cnp;
87665963Spendry 	} */ *ap;
87765963Spendry {
87865963Spendry 	int error;
87965963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
88065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
88165963Spendry 
88265963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
88365963Spendry 		struct vnode *dvp = dun->un_uppervp;
88465963Spendry 		struct vnode *vp = un->un_uppervp;
88565963Spendry 
88665963Spendry 		VREF(dvp);
88765963Spendry 		VOP_LOCK(dvp);
88865963Spendry 		vput(ap->a_dvp);
88965963Spendry 		VREF(vp);
89065963Spendry 		VOP_LOCK(vp);
89165963Spendry 		vput(ap->a_vp);
89265963Spendry 
89365963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
89465963Spendry 	} else {
89565963Spendry 		/*
89665963Spendry 		 * XXX: should create a whiteout here
89765963Spendry 		 */
89865963Spendry 		vput(ap->a_dvp);
89965963Spendry 		vput(ap->a_vp);
90065963Spendry 		error = EROFS;
90165963Spendry 	}
90265963Spendry 
90365963Spendry 	return (error);
90465963Spendry }
90565963Spendry 
90665963Spendry int
90765963Spendry union_symlink(ap)
90865963Spendry 	struct vop_symlink_args /* {
90965963Spendry 		struct vnode *a_dvp;
91065963Spendry 		struct vnode **a_vpp;
91165963Spendry 		struct componentname *a_cnp;
91265963Spendry 		struct vattr *a_vap;
91365963Spendry 		char *a_target;
91465963Spendry 	} */ *ap;
91565963Spendry {
91665963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
91765963Spendry 	struct vnode *dvp = un->un_uppervp;
91865963Spendry 
91965963Spendry 	if (dvp) {
92065963Spendry 		int error;
92165963Spendry 		struct vnode *vp;
92265963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
92365963Spendry 
92465963Spendry 		VREF(dvp);
92565963Spendry 		VOP_LOCK(dvp);
92665963Spendry 		vput(ap->a_dvp);
92765963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
92865963Spendry 					ap->a_vap, ap->a_target);
929*65997Spendry 		*ap->a_vpp = NULLVP;
93065963Spendry 		return (error);
93165963Spendry 	}
93265963Spendry 
93365963Spendry 	vput(ap->a_dvp);
93465963Spendry 	return (EROFS);
93565963Spendry }
93665963Spendry 
93765935Spendry /*
93865935Spendry  * union_readdir works in concert with getdirentries and
93965935Spendry  * readdir(3) to provide a list of entries in the unioned
94065935Spendry  * directories.  getdirentries is responsible for walking
94165935Spendry  * down the union stack.  readdir(3) is responsible for
94265935Spendry  * eliminating duplicate names from the returned data stream.
94365935Spendry  */
94465935Spendry int
94565935Spendry union_readdir(ap)
94665935Spendry 	struct vop_readdir_args /* {
94765935Spendry 		struct vnodeop_desc *a_desc;
94865935Spendry 		struct vnode *a_vp;
94965935Spendry 		struct uio *a_uio;
95065935Spendry 		struct ucred *a_cred;
95165935Spendry 	} */ *ap;
95265935Spendry {
95365963Spendry 	int error = 0;
95465935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
95565935Spendry 
95665963Spendry 	if (un->un_uppervp) {
95765963Spendry 		struct vnode *vp = OTHERVP(ap->a_vp);
95865935Spendry 
95965963Spendry 		VOP_LOCK(vp);
96065963Spendry 		error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
96165963Spendry 		VOP_UNLOCK(vp);
96265963Spendry 	}
96365963Spendry 
96465963Spendry 	return (error);
96565935Spendry }
96665935Spendry 
96765935Spendry int
96865963Spendry union_readlink(ap)
96965963Spendry 	struct vop_readlink_args /* {
97065963Spendry 		struct vnode *a_vp;
97165963Spendry 		struct uio *a_uio;
97265963Spendry 		struct ucred *a_cred;
97365963Spendry 	} */ *ap;
97465963Spendry {
97565963Spendry 	int error;
97665963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
97765963Spendry 
97865963Spendry 	VOP_LOCK(vp);
97965963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
98065963Spendry 	VOP_UNLOCK(vp);
98165963Spendry 
98265963Spendry 	return (error);
98365963Spendry }
98465963Spendry 
98565963Spendry int
98665963Spendry union_abortop(ap)
98765963Spendry 	struct vop_abortop_args /* {
98865963Spendry 		struct vnode *a_dvp;
98965963Spendry 		struct componentname *a_cnp;
99065963Spendry 	} */ *ap;
99165963Spendry {
99265963Spendry 	int error;
99365965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
99465963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
99565963Spendry 	int islocked = un->un_flags & UN_LOCKED;
99665963Spendry 
99765963Spendry 	if (islocked)
99865963Spendry 		VOP_LOCK(vp);
99965963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
100065963Spendry 	if (islocked)
100165963Spendry 		VOP_UNLOCK(vp);
100265963Spendry 
100365963Spendry 	return (error);
100465963Spendry }
100565963Spendry 
100665963Spendry int
100765935Spendry union_inactive(ap)
100865935Spendry 	struct vop_inactive_args /* {
100965935Spendry 		struct vnode *a_vp;
101065935Spendry 	} */ *ap;
101165935Spendry {
101265935Spendry 
101365935Spendry 	/*
101465935Spendry 	 * Do nothing (and _don't_ bypass).
101565935Spendry 	 * Wait to vrele lowervp until reclaim,
101665935Spendry 	 * so that until then our union_node is in the
101765935Spendry 	 * cache and reusable.
101865935Spendry 	 *
101965935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
102065935Spendry 	 * the lowervp and then trying to reactivate it
102165935Spendry 	 * with capabilities (v_id)
102265935Spendry 	 * like they do in the name lookup cache code.
102365935Spendry 	 * That's too much work for now.
102465935Spendry 	 */
102565989Spendry 
102665989Spendry #ifdef DIAGNOSTIC
102765989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
102865989Spendry 
102965989Spendry 	if (un->un_flags & UN_LOCKED)
103065989Spendry 		panic("union: inactivating locked node");
103165989Spendry #endif
103265989Spendry 
103365935Spendry 	return (0);
103465935Spendry }
103565935Spendry 
103665935Spendry int
103765935Spendry union_reclaim(ap)
103865935Spendry 	struct vop_reclaim_args /* {
103965935Spendry 		struct vnode *a_vp;
104065935Spendry 	} */ *ap;
104165935Spendry {
104265935Spendry 	struct vnode *vp = ap->a_vp;
104365935Spendry 	struct union_node *un = VTOUNION(vp);
104465935Spendry 	struct vnode *uppervp = un->un_uppervp;
104565935Spendry 	struct vnode *lowervp = un->un_lowervp;
104665935Spendry 	struct vnode *dirvp = un->un_dirvp;
104765935Spendry 	char *path = un->un_path;
104865935Spendry 
104965935Spendry 	/*
105065935Spendry 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
105165935Spendry 	 * so we can't call VOPs on ourself.
105265935Spendry 	 */
105365935Spendry 	/* After this assignment, this node will not be re-used. */
1054*65997Spendry 	un->un_uppervp = NULLVP;
1055*65997Spendry 	un->un_lowervp = NULLVP;
1056*65997Spendry 	un->un_dirvp = NULLVP;
105765935Spendry 	un->un_path = NULL;
105865935Spendry 	union_freevp(vp);
105965935Spendry 	if (uppervp)
106065935Spendry 		vrele(uppervp);
106165935Spendry 	if (lowervp)
106265935Spendry 		vrele(lowervp);
106365935Spendry 	if (dirvp)
106465935Spendry 		vrele(dirvp);
106565935Spendry 	if (path)
106665935Spendry 		free(path, M_TEMP);
106765935Spendry 	return (0);
106865935Spendry }
106965935Spendry 
107065963Spendry int
107165963Spendry union_lock(ap)
107265963Spendry 	struct vop_lock_args *ap;
107365963Spendry {
107465963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
107565935Spendry 
107665965Spendry 	while (un->un_flags & UN_LOCKED) {
107765963Spendry #ifdef DIAGNOSTIC
107865989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
107965989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
108065989Spendry 			panic("union: locking against myself");
108165963Spendry #endif
108265963Spendry 		un->un_flags |= UN_WANT;
108365963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
108465963Spendry 	}
108565963Spendry 	un->un_flags |= UN_LOCKED;
108665989Spendry 
108765963Spendry #ifdef DIAGNOSTIC
108865989Spendry 	if (curproc)
108965989Spendry 		un->un_pid = curproc->p_pid;
109065989Spendry 	else
109165989Spendry 		un->un_pid = -1;
109265963Spendry #endif
109365963Spendry }
109465963Spendry 
109565935Spendry int
109665963Spendry union_unlock(ap)
109765963Spendry 	struct vop_lock_args *ap;
109865963Spendry {
109965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
110065963Spendry 
110165963Spendry #ifdef DIAGNOSTIC
110265965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
110365965Spendry 		panic("union: unlock unlocked node");
110465989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
110565989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
110665963Spendry 		panic("union: unlocking other process's union node");
110765963Spendry #endif
110865963Spendry 
110965963Spendry 	un->un_flags &= ~UN_LOCKED;
111065963Spendry 	if (un->un_flags & UN_WANT) {
111165963Spendry 		un->un_flags &= ~UN_WANT;
111265963Spendry 		wakeup((caddr_t) &un->un_flags);
111365963Spendry 	}
111465963Spendry 
111565963Spendry #ifdef DIAGNOSTIC
111665963Spendry 	un->un_pid = 0;
111765963Spendry #endif
111865963Spendry }
111965963Spendry 
112065963Spendry int
112165963Spendry union_bmap(ap)
112265963Spendry 	struct vop_bmap_args /* {
112365963Spendry 		struct vnode *a_vp;
112465963Spendry 		daddr_t  a_bn;
112565963Spendry 		struct vnode **a_vpp;
112665963Spendry 		daddr_t *a_bnp;
112765963Spendry 		int *a_runp;
112865963Spendry 	} */ *ap;
112965963Spendry {
113065963Spendry 	int error;
113165963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
113265963Spendry 
113365963Spendry 	VOP_LOCK(vp);
113465963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
113565963Spendry 	VOP_UNLOCK(vp);
113665963Spendry 
113765963Spendry 	return (error);
113865963Spendry }
113965963Spendry 
114065963Spendry int
114165935Spendry union_print(ap)
114265935Spendry 	struct vop_print_args /* {
114365935Spendry 		struct vnode *a_vp;
114465935Spendry 	} */ *ap;
114565935Spendry {
114665935Spendry 	struct vnode *vp = ap->a_vp;
114765935Spendry 
114865935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
114965935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
115065935Spendry 	return (0);
115165935Spendry }
115265935Spendry 
115365963Spendry int
115465963Spendry union_islocked(ap)
115565963Spendry 	struct vop_islocked_args /* {
115665963Spendry 		struct vnode *a_vp;
115765963Spendry 	} */ *ap;
115865963Spendry {
115965935Spendry 
116065963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
116165963Spendry }
116265963Spendry 
116365935Spendry int
116465963Spendry union_pathconf(ap)
116565963Spendry 	struct vop_pathconf_args /* {
116665963Spendry 		struct vnode *a_vp;
116765963Spendry 		int a_name;
116865963Spendry 		int *a_retval;
116965935Spendry 	} */ *ap;
117065935Spendry {
117165935Spendry 	int error;
117265963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
117365935Spendry 
117465963Spendry 	VOP_LOCK(vp);
117565963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
117665963Spendry 	VOP_UNLOCK(vp);
117765935Spendry 
117865963Spendry 	return (error);
117965963Spendry }
118065935Spendry 
118165963Spendry int
118265963Spendry union_advlock(ap)
118365963Spendry 	struct vop_advlock_args /* {
118465963Spendry 		struct vnode *a_vp;
118565963Spendry 		caddr_t  a_id;
118665963Spendry 		int  a_op;
118765963Spendry 		struct flock *a_fl;
118865963Spendry 		int  a_flags;
118965963Spendry 	} */ *ap;
119065963Spendry {
119165935Spendry 
119265963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
119365963Spendry 				ap->a_fl, ap->a_flags));
119465935Spendry }
119565935Spendry 
119665935Spendry 
119765935Spendry /*
119865963Spendry  * XXX - vop_strategy must be hand coded because it has no
119965935Spendry  * vnode in its arguments.
120065935Spendry  * This goes away with a merged VM/buffer cache.
120165935Spendry  */
120265935Spendry int
120365963Spendry union_strategy(ap)
120465963Spendry 	struct vop_strategy_args /* {
120565935Spendry 		struct buf *a_bp;
120665935Spendry 	} */ *ap;
120765935Spendry {
120865935Spendry 	struct buf *bp = ap->a_bp;
120965935Spendry 	int error;
121065935Spendry 	struct vnode *savedvp;
121165935Spendry 
121265935Spendry 	savedvp = bp->b_vp;
121365963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
121465935Spendry 
121565935Spendry #ifdef DIAGNOSTIC
1216*65997Spendry 	if (bp->b_vp == NULLVP)
121765963Spendry 		panic("union_strategy: nil vp");
121865963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
121965963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
122065963Spendry 		panic("union_strategy: writing to lowervp");
122165935Spendry #endif
122265935Spendry 
122365963Spendry 	error = VOP_STRATEGY(bp);
122465935Spendry 	bp->b_vp = savedvp;
122565935Spendry 
122665935Spendry 	return (error);
122765935Spendry }
122865935Spendry 
122965935Spendry /*
123065935Spendry  * Global vfs data structures
123165935Spendry  */
123265935Spendry int (**union_vnodeop_p)();
123365965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
123465963Spendry 	{ &vop_default_desc, vn_default_error },
123565963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
123665963Spendry 	{ &vop_create_desc, union_create },		/* create */
123765963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
123865963Spendry 	{ &vop_open_desc, union_open },			/* open */
123965963Spendry 	{ &vop_close_desc, union_close },		/* close */
124065963Spendry 	{ &vop_access_desc, union_access },		/* access */
124165963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
124265963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
124365963Spendry 	{ &vop_read_desc, union_read },			/* read */
124465963Spendry 	{ &vop_write_desc, union_write },		/* write */
124565963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
124665963Spendry 	{ &vop_select_desc, union_select },		/* select */
124765963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
124865963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
124965963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
125065963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
125165963Spendry 	{ &vop_link_desc, union_link },			/* link */
125265963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
125365963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
125465963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
125565963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
125665963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
125765963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
125865963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
125965963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
126065963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
126165963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
126265963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
126365963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
126465963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
126565963Spendry 	{ &vop_print_desc, union_print },		/* print */
126665963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
126765963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
126865963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
126965963Spendry #ifdef notdef
127065963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
127165963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
127265963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
127365963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
127465963Spendry 	{ &vop_update_desc, union_update },		/* update */
127565963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
127665963Spendry #endif
127765935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
127865935Spendry };
127965935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
128065935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1281