xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 65989)
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*65989Spendry  *	@(#)union_vnops.c	1.4 (Berkeley) 02/03/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/filedesc.h>
1965935Spendry #include <sys/time.h>
2065935Spendry #include <sys/types.h>
2165935Spendry #include <sys/vnode.h>
2265935Spendry #include <sys/mount.h>
2365935Spendry #include <sys/namei.h>
2465935Spendry #include <sys/malloc.h>
2565935Spendry #include <sys/buf.h>
2665935Spendry #include "union.h"
2765935Spendry 
2865965Spendry /*
2965965Spendry  * Create a shadow directory in the upper layer.
3065965Spendry  * The new vnode is returned locked.
3165965Spendry  */
3265935Spendry static int
3365935Spendry union_mkshadow(dvp, cnp, vpp)
3465935Spendry 	struct vnode *dvp;
3565935Spendry 	struct componentname *cnp;
3665935Spendry 	struct vnode *vpp;
3765935Spendry {
3865935Spendry 	int error;
3965935Spendry 	struct vattr va;
4065935Spendry 	struct proc *p = cnp->cn_proc;
4165935Spendry 
4265935Spendry 	/*
4365935Spendry 	 * policy: when creating the shadow directory in the
4465935Spendry 	 * upper layer, create it owned by the current user,
4565935Spendry 	 * group from parent directory, and mode 777 modified
4665935Spendry 	 * by umask (ie mostly identical to the mkdir syscall).
4765935Spendry 	 * (jsp, kb)
4865935Spendry 	 * TODO: create the directory owned by the user who
4965935Spendry 	 * did the mount (um->um_cred).
5065935Spendry 	 */
5165935Spendry 
5265935Spendry 	VATTR_NULL(&va);
5365935Spendry 	va.va_type = VDIR;
5465935Spendry 	va.va_mode = UN_DIRMODE &~ p->p_fd->fd_cmask;
5565965Spendry 	VOP_UNLOCK(dvp);
5665935Spendry 	LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
5765965Spendry 	VREF(dvp);
5865935Spendry 	VOP_LOCK(dvp);
5965935Spendry 	error = VOP_MKDIR(dvp, vpp, cnp, &va);
6065965Spendry 	VOP_LOCK(dvp);
6165935Spendry 	return (error);
6265935Spendry }
6365935Spendry 
6465935Spendry static int
65*65989Spendry union_lookup1(udvp, dvp, vpp, cnp)
66*65989Spendry 	struct vnode *udvp;
6765935Spendry 	struct vnode *dvp;
6865935Spendry 	struct vnode **vpp;
6965935Spendry 	struct componentname *cnp;
7065935Spendry {
7165935Spendry 	int error;
7265935Spendry 	struct vnode *tdvp;
7365935Spendry 	struct mount *mp;
7465935Spendry 
7565935Spendry 	if (cnp->cn_flags & ISDOTDOT) {
7665935Spendry 		for (;;) {
7765935Spendry 			if ((dvp->v_flag & VROOT) == 0 ||
7865935Spendry 			    (cnp->cn_flags & NOCROSSMOUNT))
7965935Spendry 				break;
8065935Spendry 
8165935Spendry 			tdvp = dvp;
8265935Spendry 			dvp = dvp->v_mount->mnt_vnodecovered;
8365935Spendry 			vput(tdvp);
8465935Spendry 			VREF(dvp);
8565935Spendry 			VOP_LOCK(dvp);
8665935Spendry 		}
8765935Spendry 	}
8865935Spendry 
8965935Spendry         error = VOP_LOOKUP(dvp, &tdvp, cnp);
9065935Spendry 	if (error)
9165935Spendry 		return (error);
9265935Spendry 
9365935Spendry 	dvp = tdvp;
94*65989Spendry 	while (dvp != udvp && (dvp->v_type == VDIR) &&
95*65989Spendry 	       (mp = dvp->v_mountedhere) &&
9665935Spendry 	       (cnp->cn_flags & NOCROSSMOUNT) == 0) {
9765935Spendry 
9865935Spendry 		if (mp->mnt_flag & MNT_MLOCK) {
9965935Spendry 			mp->mnt_flag |= MNT_MWAIT;
10065935Spendry 			sleep((caddr_t) mp, PVFS);
10165935Spendry 			continue;
10265935Spendry 		}
10365935Spendry 
10465935Spendry 		if (error = VFS_ROOT(mp, &tdvp)) {
10565935Spendry 			vput(dvp);
10665935Spendry 			return (error);
10765935Spendry 		}
10865935Spendry 
10965965Spendry 		vput(dvp);
11065935Spendry 		dvp = tdvp;
11165935Spendry 	}
11265935Spendry 
11365935Spendry 	*vpp = dvp;
11465935Spendry 	return (0);
11565935Spendry }
11665935Spendry 
11765935Spendry int
11865935Spendry union_lookup(ap)
11965935Spendry 	struct vop_lookup_args /* {
12065935Spendry 		struct vnodeop_desc *a_desc;
12165935Spendry 		struct vnode *a_dvp;
12265935Spendry 		struct vnode **a_vpp;
12365935Spendry 		struct componentname *a_cnp;
12465935Spendry 	} */ *ap;
12565935Spendry {
12665965Spendry 	int error;
12765935Spendry 	int uerror, lerror;
12865935Spendry 	struct vnode *uppervp, *lowervp;
12965935Spendry 	struct vnode *upperdvp, *lowerdvp;
13065935Spendry 	struct vnode *dvp = ap->a_dvp;
131*65989Spendry 	struct union_node *dun = VTOUNION(dvp);
13265935Spendry 	struct componentname *cnp = ap->a_cnp;
13365935Spendry 	int lockparent = cnp->cn_flags & LOCKPARENT;
13465935Spendry 
13565965Spendry 	cnp->cn_flags |= LOCKPARENT;
13665965Spendry 
13765935Spendry 	upperdvp = dun->un_uppervp;
13865935Spendry 	lowerdvp = dun->un_lowervp;
13965965Spendry 	uppervp = 0;
14065965Spendry 	lowervp = 0;
14165935Spendry 
14265935Spendry 	/*
14365935Spendry 	 * do the lookup in the upper level.
14465935Spendry 	 * if that level comsumes additional pathnames,
14565935Spendry 	 * then assume that something special is going
14665935Spendry 	 * on and just return that vnode.
14765935Spendry 	 */
14865935Spendry 	if (upperdvp) {
14965965Spendry 		VOP_LOCK(upperdvp);
150*65989Spendry 		uerror = union_lookup1(
151*65989Spendry 			MOUNTTOUNIONMOUNT(dvp->v_mount)->um_uppervp,
152*65989Spendry 			upperdvp, &uppervp, cnp);
153*65989Spendry 		if (uppervp != upperdvp)
154*65989Spendry 			VOP_UNLOCK(upperdvp);
15565965Spendry 
15665935Spendry 		if (cnp->cn_consume != 0) {
15765935Spendry 			*ap->a_vpp = uppervp;
15865965Spendry 			if (!lockparent)
15965965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
16065935Spendry 			return (uerror);
16165935Spendry 		}
16265935Spendry 	} else {
16365935Spendry 		uerror = ENOENT;
16465935Spendry 	}
16565935Spendry 
16665935Spendry 	/*
16765935Spendry 	 * in a similar way to the upper layer, do the lookup
16865935Spendry 	 * in the lower layer.   this time, if there is some
16965935Spendry 	 * component magic going on, then vput whatever we got
17065935Spendry 	 * back from the upper layer and return the lower vnode
17165935Spendry 	 * instead.
17265935Spendry 	 */
17365935Spendry 	if (lowerdvp) {
17465965Spendry 		VOP_LOCK(lowerdvp);
175*65989Spendry 		lerror = union_lookup1(
176*65989Spendry 			MOUNTTOUNIONMOUNT(dvp->v_mount)->um_lowervp,
177*65989Spendry 			lowerdvp, &lowervp, cnp);
178*65989Spendry 		if (lowervp != lowerdvp)
179*65989Spendry 			VOP_UNLOCK(lowerdvp);
18065965Spendry 
18165935Spendry 		if (cnp->cn_consume != 0) {
18265935Spendry 			if (uppervp) {
18365935Spendry 				vput(uppervp);
18465935Spendry 				uppervp = 0;
18565935Spendry 			}
18665935Spendry 			*ap->a_vpp = lowervp;
18765965Spendry 			if (!lockparent)
18865965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
18965935Spendry 			return (lerror);
19065935Spendry 		}
19165935Spendry 	} else {
19265935Spendry 		lerror = ENOENT;
19365935Spendry 	}
19465935Spendry 
19565965Spendry 	if (!lockparent)
19665965Spendry 		cnp->cn_flags &= ~LOCKPARENT;
19765965Spendry 
19865935Spendry 	/*
19965935Spendry 	 * at this point, we have uerror and lerror indicating
20065935Spendry 	 * possible errors with the lookups in the upper and lower
20165935Spendry 	 * layers.  additionally, uppervp and lowervp are (locked)
20265935Spendry 	 * references to existing vnodes in the upper and lower layers.
20365935Spendry 	 *
20465935Spendry 	 * there are now three cases to consider.
20565935Spendry 	 * 1. if both layers returned an error, then return whatever
20665935Spendry 	 *    error the upper layer generated.
20765935Spendry 	 *
20865935Spendry 	 * 2. if the top layer failed and the bottom layer succeeded
20965935Spendry 	 *    then two subcases occur.
21065935Spendry 	 *    a.  the bottom vnode is not a directory, in which
21165935Spendry 	 *	  case just return a new union vnode referencing
21265935Spendry 	 *	  an empty top layer and the existing bottom layer.
21365935Spendry 	 *    b.  the bottom vnode is a directory, in which case
21465935Spendry 	 *	  create a new directory in the top-level and
21565935Spendry 	 *	  continue as in case 3.
21665935Spendry 	 *
21765935Spendry 	 * 3. if the top layer succeeded then return a new union
21865935Spendry 	 *    vnode referencing whatever the new top layer and
21965935Spendry 	 *    whatever the bottom layer returned.
22065935Spendry 	 */
22165935Spendry 
22265935Spendry 	/* case 1. */
22365935Spendry 	if ((uerror != 0) && (lerror != 0)) {
22465935Spendry 		*ap->a_vpp = 0;
22565935Spendry 		return (uerror);
22665935Spendry 	}
22765935Spendry 
22865935Spendry 	/* case 2. */
22965935Spendry 	if (uerror != 0 /* && (lerror == 0) */ ) {
23065935Spendry 		if (lowervp->v_type == VDIR) { /* case 2b. */
231*65989Spendry 			if (uppervp != upperdvp)
232*65989Spendry 				VOP_LOCK(upperdvp);
23365935Spendry 			uerror = union_mkshadow(upperdvp, cnp, &uppervp);
234*65989Spendry 			if (uppervp != upperdvp)
235*65989Spendry 				VOP_UNLOCK(upperdvp);
23665935Spendry 			if (uerror) {
23765935Spendry 				if (lowervp) {
23865935Spendry 					vput(lowervp);
23965935Spendry 					lowervp = 0;
24065935Spendry 				}
24165935Spendry 				return (uerror);
24265935Spendry 			}
24365935Spendry 		}
24465935Spendry 	}
24565935Spendry 
246*65989Spendry 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
24765965Spendry 			      uppervp, lowervp);
24865965Spendry 
24965965Spendry 	if (uppervp)
25065965Spendry 		VOP_UNLOCK(uppervp);
25165965Spendry 	if (lowervp)
25265965Spendry 		VOP_UNLOCK(lowervp);
25365965Spendry 
25465965Spendry 	if (error) {
25565965Spendry 		if (uppervp)
25665965Spendry 			vrele(uppervp);
25765965Spendry 		if (lowervp)
25865965Spendry 			vrele(lowervp);
25965965Spendry 	} else {
260*65989Spendry 		if (!lockparent && (*ap->a_vpp != dvp))
261*65989Spendry 			VOP_UNLOCK(dvp);
26265965Spendry 	}
26365965Spendry 
26465965Spendry 	return (error);
26565935Spendry }
26665935Spendry 
26765963Spendry int
26865963Spendry union_create(ap)
26965963Spendry 	struct vop_create_args /* {
27065963Spendry 		struct vnode *a_dvp;
27165963Spendry 		struct vnode **a_vpp;
27265963Spendry 		struct componentname *a_cnp;
27365963Spendry 		struct vattr *a_vap;
27465963Spendry 	} */ *ap;
27565963Spendry {
27665963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
27765963Spendry 	struct vnode *dvp = un->un_uppervp;
27865963Spendry 
27965963Spendry 	if (dvp) {
28065963Spendry 		int error;
28165963Spendry 		struct vnode *vp;
28265963Spendry 
28365963Spendry 		VREF(dvp);
28465963Spendry 		VOP_LOCK(dvp);
28565963Spendry 		vput(ap->a_dvp);
28665963Spendry 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
28765963Spendry 		if (error)
28865963Spendry 			return (error);
28965963Spendry 
29065963Spendry 		error = union_allocvp(
29165963Spendry 				ap->a_vpp,
292*65989Spendry 				ap->a_dvp->v_mount,
293*65989Spendry 				ap->a_dvp,
29465965Spendry 				NULLVP,
29565963Spendry 				ap->a_cnp,
29665963Spendry 				vp,
29765963Spendry 				NULLVP);
29865965Spendry 		VOP_UNLOCK(vp);
29965965Spendry 		if (error)
30065965Spendry 			vrele(vp);
30165963Spendry 		return (error);
30265963Spendry 	}
30365963Spendry 
30465963Spendry 	vput(ap->a_dvp);
30565963Spendry 	return (EROFS);
30665963Spendry }
30765963Spendry 
30865963Spendry int
30965963Spendry union_mknod(ap)
31065963Spendry 	struct vop_mknod_args /* {
31165963Spendry 		struct vnode *a_dvp;
31265963Spendry 		struct vnode **a_vpp;
31365963Spendry 		struct componentname *a_cnp;
31465963Spendry 		struct vattr *a_vap;
31565963Spendry 	} */ *ap;
31665963Spendry {
31765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
31865963Spendry 	struct vnode *dvp = un->un_uppervp;
31965963Spendry 
32065963Spendry 	if (dvp) {
32165963Spendry 		int error;
32265963Spendry 		struct vnode *vp;
32365963Spendry 
32465963Spendry 		VREF(dvp);
32565963Spendry 		VOP_LOCK(dvp);
32665963Spendry 		vput(ap->a_dvp);
32765963Spendry 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
32865963Spendry 		if (error)
32965963Spendry 			return (error);
33065963Spendry 
33165965Spendry 		if (vp) {
33265965Spendry 			error = union_allocvp(
33365965Spendry 					ap->a_vpp,
334*65989Spendry 					ap->a_dvp->v_mount,
335*65989Spendry 					ap->a_dvp,
33665965Spendry 					NULLVP,
33765965Spendry 					ap->a_cnp,
33865965Spendry 					vp,
33965965Spendry 					NULLVP);
34065965Spendry 			VOP_UNLOCK(vp);
34165965Spendry 			if (error)
34265965Spendry 				vrele(vp);
34365965Spendry 		}
34465963Spendry 		return (error);
34565963Spendry 	}
34665963Spendry 
34765963Spendry 	vput(ap->a_dvp);
34865963Spendry 	return (EROFS);
34965963Spendry }
35065963Spendry 
35165935Spendry /*
35265935Spendry  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
35365963Spendry  * using a sequence of reads and writes.  both (fvp)
35465963Spendry  * and (tvp) are locked on entry and exit.
35565935Spendry  */
35665935Spendry static int
35765935Spendry union_copyfile(p, cred, fvp, tvp)
35865935Spendry 	struct proc *p;
35965935Spendry 	struct ucred *cred;
36065935Spendry 	struct vnode *fvp;
36165935Spendry 	struct vnode *tvp;
36265935Spendry {
36365935Spendry 	char *buf;
36465935Spendry 	struct uio uio;
36565935Spendry 	struct iovec iov;
36665935Spendry 	int error = 0;
36765935Spendry 	off_t offset;
36865935Spendry 
36965935Spendry 	/*
37065935Spendry 	 * strategy:
37165935Spendry 	 * allocate a buffer of size MAXBSIZE.
37265935Spendry 	 * loop doing reads and writes, keeping track
37365935Spendry 	 * of the current uio offset.
37465935Spendry 	 * give up at the first sign of trouble.
37565935Spendry 	 */
37665935Spendry 
37765935Spendry 	uio.uio_procp = p;
37865935Spendry 	uio.uio_segflg = UIO_SYSSPACE;
37965935Spendry 	offset = 0;
38065935Spendry 
38165935Spendry 	VOP_UNLOCK(fvp);				/* XXX */
38265935Spendry 	LEASE_CHECK(fvp, p, cred, LEASE_READ);
38365935Spendry 	VOP_LOCK(fvp);					/* XXX */
38465935Spendry 	VOP_UNLOCK(tvp);				/* XXX */
38565935Spendry 	LEASE_CHECK(tvp, p, cred, LEASE_WRITE);
38665935Spendry 	VOP_LOCK(tvp);					/* XXX */
38765935Spendry 
38865935Spendry 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
38965935Spendry 	do {
39065935Spendry 		uio.uio_iov = &iov;
39165935Spendry 		uio.uio_iovcnt = 1;
39265935Spendry 		iov.iov_base = buf;
39365935Spendry 		iov.iov_len = MAXBSIZE;
39465935Spendry 		uio.uio_resid = iov.iov_len;
39565935Spendry 		uio.uio_offset = offset;
39665935Spendry 		uio.uio_rw = UIO_READ;
39765935Spendry 		error = VOP_READ(fvp, &uio, 0, cred);
39865935Spendry 
39965935Spendry 		if (error == 0) {
40065935Spendry 			uio.uio_iov = &iov;
40165935Spendry 			uio.uio_iovcnt = 1;
40265935Spendry 			iov.iov_base = buf;
40365935Spendry 			iov.iov_len = MAXBSIZE - uio.uio_resid;
40465935Spendry 			uio.uio_rw = UIO_WRITE;
40565935Spendry 			uio.uio_resid = iov.iov_len;
40665935Spendry 			uio.uio_offset = offset;
40765935Spendry 
40865935Spendry 			do {
40965935Spendry 				error = VOP_WRITE(tvp, &uio, 0, cred);
41065935Spendry 			} while (error == 0 && uio.uio_resid > 0);
41165935Spendry 			if (error == 0)
41265935Spendry 				offset = uio.uio_offset;
41365935Spendry 		}
41465935Spendry 	} while ((uio.uio_resid == 0) && (error == 0));
41565935Spendry 
41665935Spendry 	free(buf, M_TEMP);
41765935Spendry 	return (error);
41865935Spendry }
41965935Spendry 
42065935Spendry int
42165935Spendry union_open(ap)
42265935Spendry 	struct vop_open_args /* {
42365935Spendry 		struct vnodeop_desc *a_desc;
42465935Spendry 		struct vnode *a_vp;
42565935Spendry 		int a_mode;
42665935Spendry 		struct ucred *a_cred;
42765935Spendry 		struct proc *a_p;
42865935Spendry 	} */ *ap;
42965935Spendry {
43065935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
43165965Spendry 	struct vnode *tvp;
43265935Spendry 	int mode = ap->a_mode;
43365935Spendry 	struct ucred *cred = ap->a_cred;
43465935Spendry 	struct proc *p = ap->a_p;
43565965Spendry 	int error;
43665935Spendry 
43765935Spendry 	/*
43865935Spendry 	 * If there is an existing upper vp then simply open that.
43965935Spendry 	 */
44065965Spendry 	tvp = un->un_uppervp;
44165965Spendry 	if (tvp == NULLVP) {
44265935Spendry 		/*
44365965Spendry 		 * If the lower vnode is being opened for writing, then
44465965Spendry 		 * copy the file contents to the upper vnode and open that,
44565965Spendry 		 * otherwise can simply open the lower vnode.
44665935Spendry 		 */
44765965Spendry 		tvp = un->un_lowervp;
44865965Spendry 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
44965965Spendry 			struct nameidata nd;
45065965Spendry 			struct filedesc *fdp = p->p_fd;
45165965Spendry 			int fmode;
45265965Spendry 			int cmode;
45365963Spendry 
45465965Spendry 			/*
45565965Spendry 			 * Open the named file in the upper layer.  Note that
45665965Spendry 			 * the file may have come into existence *since* the
45765965Spendry 			 * lookup was done, since the upper layer may really
45865965Spendry 			 * be a loopback mount of some other filesystem...
45965965Spendry 			 * so open the file with exclusive create and barf if
46065965Spendry 			 * it already exists.
46165965Spendry 			 * XXX - perhaps shoudl re-lookup the node (once more
46265965Spendry 			 * with feeling) and simply open that.  Who knows.
46365965Spendry 			 */
46465965Spendry 			NDINIT(&nd, CREATE, 0, UIO_SYSSPACE, un->un_path, p);
46565965Spendry 			fmode = (O_CREAT|O_TRUNC|O_EXCL);
46665965Spendry 			cmode = UN_FILEMODE & ~fdp->fd_cmask;
46765965Spendry 			error = vn_open(&nd, fmode, cmode);
46865965Spendry 			if (error)
46965965Spendry 				return (error);
47065965Spendry 			un->un_uppervp = nd.ni_vp;	/* XXX */
47165965Spendry 			/* at this point, uppervp is locked */
47265965Spendry 
47365965Spendry 			/*
47465965Spendry 			 * Now, if the file is being opened with truncation,
47565965Spendry 			 * then the (new) upper vnode is ready to fly,
47665965Spendry 			 * otherwise the data from the lower vnode must be
47765965Spendry 			 * copied to the upper layer first.  This only works
47865965Spendry 			 * for regular files (check is made above).
47965965Spendry 			 */
48065965Spendry 			if ((mode & O_TRUNC) == 0) {
48165965Spendry 				/*
48265965Spendry 				 * XXX - should not ignore errors
48365965Spendry 				 * from VOP_CLOSE
48465965Spendry 				 */
48565965Spendry 				VOP_LOCK(un->un_lowervp);
48665965Spendry 				error = VOP_OPEN(tvp, FREAD, cred, p);
48765965Spendry 				if (error == 0) {
48865965Spendry 					error = union_copyfile(p, cred,
48965965Spendry 						       tvp, un->un_uppervp);
49065965Spendry 					VOP_UNLOCK(tvp);
49165965Spendry 					(void) VOP_CLOSE(tvp, FREAD);
49265965Spendry 				} else {
49365965Spendry 					VOP_UNLOCK(tvp);
49465965Spendry 				}
49565965Spendry 				VOP_UNLOCK(un->un_uppervp);
49665965Spendry 				(void) VOP_CLOSE(un->un_uppervp, FWRITE);
49765965Spendry 				VOP_LOCK(un->un_uppervp);
49865935Spendry 			}
49965965Spendry 			if (error == 0)
50065965Spendry 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
50165963Spendry 			VOP_UNLOCK(un->un_uppervp);
50265965Spendry 			return (error);
50365935Spendry 		}
50465935Spendry 	}
50565935Spendry 
50665965Spendry 	VOP_LOCK(tvp);
50765965Spendry 	error = VOP_OPEN(tvp, mode, cred, p);
50865965Spendry 	VOP_UNLOCK(tvp);
50965965Spendry 
51065965Spendry 	return (error);
51165935Spendry }
51265935Spendry 
51365963Spendry int
51465963Spendry union_close(ap)
51565963Spendry 	struct vop_close_args /* {
51665963Spendry 		struct vnode *a_vp;
51765963Spendry 		int  a_fflag;
51865963Spendry 		struct ucred *a_cred;
51965963Spendry 		struct proc *a_p;
52065963Spendry 	} */ *ap;
52165963Spendry {
52265963Spendry 
52365963Spendry 	return (VOP_CLOSE(OTHERVP(ap->a_vp), ap->a_fflag, ap->a_cred, ap->a_p));
52465963Spendry }
52565963Spendry 
52665935Spendry /*
52765963Spendry  * Check access permission on the union vnode.
52865963Spendry  * The access check being enforced is to check
52965963Spendry  * against both the underlying vnode, and any
53065963Spendry  * copied vnode.  This ensures that no additional
53165963Spendry  * file permissions are given away simply because
53265963Spendry  * the user caused an implicit file copy.
53365963Spendry  */
53465963Spendry int
53565963Spendry union_access(ap)
53665963Spendry 	struct vop_access_args /* {
53765963Spendry 		struct vnodeop_desc *a_desc;
53865963Spendry 		struct vnode *a_vp;
53965963Spendry 		int a_mode;
54065963Spendry 		struct ucred *a_cred;
54165963Spendry 		struct proc *a_p;
54265963Spendry 	} */ *ap;
54365963Spendry {
54465963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
54565965Spendry 	int error = 0;
54665963Spendry 	struct vnode *vp;
54765963Spendry 
54865963Spendry 	if (vp = un->un_lowervp) {
54965965Spendry 		VOP_LOCK(vp);
55065963Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
55165965Spendry 		VOP_UNLOCK(vp);
55265963Spendry 		if (error)
55365963Spendry 			return (error);
55465963Spendry 	}
55565963Spendry 
55665965Spendry 	if (vp = un->un_uppervp) {
55765965Spendry 		VOP_LOCK(vp);
55865965Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
55965965Spendry 		VOP_UNLOCK(vp);
56065965Spendry 	}
56165965Spendry 
56265965Spendry 	return (error);
56365963Spendry }
56465963Spendry 
56565963Spendry /*
56665935Spendry  *  We handle getattr only to change the fsid.
56765935Spendry  */
56865935Spendry int
56965935Spendry union_getattr(ap)
57065935Spendry 	struct vop_getattr_args /* {
57165935Spendry 		struct vnode *a_vp;
57265935Spendry 		struct vattr *a_vap;
57365935Spendry 		struct ucred *a_cred;
57465935Spendry 		struct proc *a_p;
57565935Spendry 	} */ *ap;
57665935Spendry {
57765935Spendry 	int error;
57865965Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
57965935Spendry 
58065965Spendry 	VOP_LOCK(vp);
58165965Spendry 	error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
58265965Spendry 	VOP_UNLOCK(vp);
58365965Spendry 
58465935Spendry 	/* Requires that arguments be restored. */
58565935Spendry 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
58665935Spendry 	return (0);
58765935Spendry }
58865935Spendry 
58965963Spendry int
59065965Spendry union_setattr(ap)
59165963Spendry 	struct vop_setattr_args /* {
59265963Spendry 		struct vnode *a_vp;
59365963Spendry 		struct vattr *a_vap;
59465963Spendry 		struct ucred *a_cred;
59565963Spendry 		struct proc *a_p;
59665963Spendry 	} */ *ap;
59765963Spendry {
59865963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
59965963Spendry 	int error;
60065963Spendry 
60165963Spendry 	if (un->un_uppervp) {
60265963Spendry 		VOP_LOCK(un->un_uppervp);
60365963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
60465963Spendry 					ap->a_cred, ap->a_p);
60565963Spendry 		VOP_UNLOCK(un->un_uppervp);
60665963Spendry 	} else {
60765963Spendry 		/*
60865963Spendry 		 * XXX should do a copyfile (perhaps only if
60965963Spendry 		 * the file permission change, which would not
61065963Spendry 		 * track va_ctime correctly).
61165963Spendry 		 */
61265963Spendry 		error = EROFS;
61365963Spendry 	}
61465963Spendry 
61565963Spendry 	return (error);
61665963Spendry }
61765963Spendry 
61865963Spendry int
61965963Spendry union_read(ap)
62065963Spendry 	struct vop_read_args /* {
62165963Spendry 		struct vnode *a_vp;
62265963Spendry 		struct uio *a_uio;
62365963Spendry 		int  a_ioflag;
62465963Spendry 		struct ucred *a_cred;
62565963Spendry 	} */ *ap;
62665963Spendry {
62765963Spendry 	int error;
62865963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
62965963Spendry 
63065965Spendry 	VOP_LOCK(vp);
63165963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
63265965Spendry 	VOP_UNLOCK(vp);
63365963Spendry 
63465963Spendry 	return (error);
63565963Spendry }
63665963Spendry 
63765963Spendry int
63865963Spendry union_write(ap)
63965963Spendry 	struct vop_read_args /* {
64065963Spendry 		struct vnode *a_vp;
64165963Spendry 		struct uio *a_uio;
64265963Spendry 		int  a_ioflag;
64365963Spendry 		struct ucred *a_cred;
64465963Spendry 	} */ *ap;
64565963Spendry {
64665963Spendry 	int error;
64765963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
64865963Spendry 
64965963Spendry 	VOP_LOCK(vp);
65065963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
65165963Spendry 	VOP_UNLOCK(vp);
65265963Spendry 
65365963Spendry 	return (error);
65465963Spendry }
65565963Spendry 
65665963Spendry int
65765963Spendry union_ioctl(ap)
65865963Spendry 	struct vop_ioctl_args /* {
65965963Spendry 		struct vnode *a_vp;
66065963Spendry 		int  a_command;
66165963Spendry 		caddr_t  a_data;
66265963Spendry 		int  a_fflag;
66365963Spendry 		struct ucred *a_cred;
66465963Spendry 		struct proc *a_p;
66565963Spendry 	} */ *ap;
66665963Spendry {
66765963Spendry 
66865963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
66965963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
67065963Spendry }
67165963Spendry 
67265963Spendry int
67365963Spendry union_select(ap)
67465963Spendry 	struct vop_select_args /* {
67565963Spendry 		struct vnode *a_vp;
67665963Spendry 		int  a_which;
67765963Spendry 		int  a_fflags;
67865963Spendry 		struct ucred *a_cred;
67965963Spendry 		struct proc *a_p;
68065963Spendry 	} */ *ap;
68165963Spendry {
68265963Spendry 
68365963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
68465963Spendry 				ap->a_cred, ap->a_p));
68565963Spendry }
68665963Spendry 
68765963Spendry int
68865963Spendry union_mmap(ap)
68965963Spendry 	struct vop_mmap_args /* {
69065963Spendry 		struct vnode *a_vp;
69165963Spendry 		int  a_fflags;
69265963Spendry 		struct ucred *a_cred;
69365963Spendry 		struct proc *a_p;
69465963Spendry 	} */ *ap;
69565963Spendry {
69665963Spendry 
69765963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
69865963Spendry 				ap->a_cred, ap->a_p));
69965963Spendry }
70065963Spendry 
70165963Spendry int
70265963Spendry union_fsync(ap)
70365963Spendry 	struct vop_fsync_args /* {
70465963Spendry 		struct vnode *a_vp;
70565963Spendry 		struct ucred *a_cred;
70665963Spendry 		int  a_waitfor;
70765963Spendry 		struct proc *a_p;
70865963Spendry 	} */ *ap;
70965963Spendry {
71065963Spendry 	int error = 0;
71165963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
71265963Spendry 
71365963Spendry 	if (targetvp) {
71465963Spendry 		VOP_LOCK(targetvp);
71565963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
71665963Spendry 					ap->a_waitfor, ap->a_p);
71765963Spendry 		VOP_UNLOCK(targetvp);
71865963Spendry 	}
71965963Spendry 
72065963Spendry 	return (error);
72165963Spendry }
72265963Spendry 
72365963Spendry int
72465963Spendry union_seek(ap)
72565963Spendry 	struct vop_seek_args /* {
72665963Spendry 		struct vnode *a_vp;
72765963Spendry 		off_t  a_oldoff;
72865963Spendry 		off_t  a_newoff;
72965963Spendry 		struct ucred *a_cred;
73065963Spendry 	} */ *ap;
73165963Spendry {
73265963Spendry 
73365963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
73465963Spendry }
73565963Spendry 
73665963Spendry int
73765963Spendry union_remove(ap)
73865963Spendry 	struct vop_remove_args /* {
73965963Spendry 		struct vnode *a_dvp;
74065963Spendry 		struct vnode *a_vp;
74165963Spendry 		struct componentname *a_cnp;
74265963Spendry 	} */ *ap;
74365963Spendry {
74465963Spendry 	int error;
74565963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
74665963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
74765963Spendry 
74865963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
74965963Spendry 		struct vnode *dvp = dun->un_uppervp;
75065963Spendry 		struct vnode *vp = un->un_uppervp;
75165963Spendry 
75265963Spendry 		VREF(dvp);
75365963Spendry 		VOP_LOCK(dvp);
75465963Spendry 		vput(ap->a_dvp);
75565963Spendry 		VREF(vp);
75665963Spendry 		VOP_LOCK(vp);
75765963Spendry 		vput(ap->a_vp);
75865963Spendry 
75965963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
76065963Spendry 	} else {
76165963Spendry 		/*
76265963Spendry 		 * XXX: should create a whiteout here
76365963Spendry 		 */
76465963Spendry 		vput(ap->a_dvp);
76565963Spendry 		vput(ap->a_vp);
76665963Spendry 		error = EROFS;
76765963Spendry 	}
76865963Spendry 
76965963Spendry 	return (error);
77065963Spendry }
77165963Spendry 
77265963Spendry int
77365963Spendry union_link(ap)
77465963Spendry 	struct vop_link_args /* {
77565963Spendry 		struct vnode *a_vp;
77665963Spendry 		struct vnode *a_tdvp;
77765963Spendry 		struct componentname *a_cnp;
77865963Spendry 	} */ *ap;
77965963Spendry {
78065963Spendry 	int error;
78165963Spendry 	struct union_node *dun = VTOUNION(ap->a_vp);
78265963Spendry 	struct union_node *un = VTOUNION(ap->a_tdvp);
78365963Spendry 
78465963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
78565963Spendry 		struct vnode *dvp = dun->un_uppervp;
78665963Spendry 		struct vnode *vp = un->un_uppervp;
78765963Spendry 
78865963Spendry 		VREF(dvp);
78965963Spendry 		VOP_LOCK(dvp);
79065963Spendry 		vput(ap->a_vp);
79165963Spendry 		VREF(vp);
79265963Spendry 		vrele(ap->a_tdvp);
79365963Spendry 
79465963Spendry 		error = VOP_LINK(dvp, vp, ap->a_cnp);
79565963Spendry 	} else {
79665963Spendry 		/*
79765963Spendry 		 * XXX: need to copy to upper layer
79865963Spendry 		 * and do the link there.
79965963Spendry 		 */
80065963Spendry 		vput(ap->a_vp);
80165963Spendry 		vrele(ap->a_tdvp);
80265963Spendry 		error = EROFS;
80365963Spendry 	}
80465963Spendry 
80565963Spendry 	return (error);
80665963Spendry }
80765963Spendry 
80865963Spendry int
80965963Spendry union_rename(ap)
81065963Spendry 	struct vop_rename_args  /* {
81165963Spendry 		struct vnode *a_fdvp;
81265963Spendry 		struct vnode *a_fvp;
81365963Spendry 		struct componentname *a_fcnp;
81465963Spendry 		struct vnode *a_tdvp;
81565963Spendry 		struct vnode *a_tvp;
81665963Spendry 		struct componentname *a_tcnp;
81765963Spendry 	} */ *ap;
81865963Spendry {
81965963Spendry 	int error;
82065963Spendry 
82165963Spendry 	struct vnode *fdvp = ap->a_fdvp;
82265963Spendry 	struct vnode *fvp = ap->a_fvp;
82365963Spendry 	struct vnode *tdvp = ap->a_tdvp;
82465963Spendry 	struct vnode *tvp = ap->a_tvp;
82565963Spendry 
82665963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
82765963Spendry 		struct union_node *un = VTOUNION(fdvp);
82865963Spendry 		if (un->un_uppervp == 0) {
82965963Spendry 			error = EROFS;
83065963Spendry 			goto bad;
83165963Spendry 		}
83265963Spendry 
83365963Spendry 		fdvp = un->un_uppervp;
83465963Spendry 		VREF(fdvp);
83565963Spendry 		vrele(ap->a_fdvp);
83665963Spendry 	}
83765963Spendry 
83865963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
83965963Spendry 		struct union_node *un = VTOUNION(fvp);
84065963Spendry 		if (un->un_uppervp == 0) {
84165963Spendry 			error = EROFS;
84265963Spendry 			goto bad;
84365963Spendry 		}
84465963Spendry 
84565963Spendry 		fvp = un->un_uppervp;
84665963Spendry 		VREF(fvp);
84765963Spendry 		vrele(ap->a_fvp);
84865963Spendry 	}
84965963Spendry 
85065963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
85165963Spendry 		struct union_node *un = VTOUNION(tdvp);
85265963Spendry 		if (un->un_uppervp == 0) {
85365963Spendry 			error = EROFS;
85465963Spendry 			goto bad;
85565963Spendry 		}
85665963Spendry 
85765963Spendry 		tdvp = un->un_uppervp;
85865963Spendry 		VREF(tdvp);
85965963Spendry 		VOP_LOCK(tdvp);
86065963Spendry 		vput(ap->a_fdvp);
86165963Spendry 	}
86265963Spendry 
86365963Spendry 	if (tvp && tvp->v_op == union_vnodeop_p) {
86465963Spendry 		struct union_node *un = VTOUNION(tvp);
86565963Spendry 		if (un->un_uppervp == 0) {
86665963Spendry 			error = EROFS;
86765963Spendry 			goto bad;
86865963Spendry 		}
86965963Spendry 
87065963Spendry 		tvp = un->un_uppervp;
87165963Spendry 		VREF(tvp);
87265963Spendry 		VOP_LOCK(tvp);
87365963Spendry 		vput(ap->a_tvp);
87465963Spendry 	}
87565963Spendry 
87665963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
87765963Spendry 
87865963Spendry bad:
87965963Spendry 	vrele(fdvp);
88065963Spendry 	vrele(fvp);
88165963Spendry 	vput(tdvp);
88265963Spendry 	if (tvp)
88365963Spendry 		vput(tvp);
88465963Spendry 
88565963Spendry 	return (error);
88665963Spendry }
88765963Spendry 
88865963Spendry int
88965963Spendry union_mkdir(ap)
89065963Spendry 	struct vop_mkdir_args /* {
89165963Spendry 		struct vnode *a_dvp;
89265963Spendry 		struct vnode **a_vpp;
89365963Spendry 		struct componentname *a_cnp;
89465963Spendry 		struct vattr *a_vap;
89565963Spendry 	} */ *ap;
89665963Spendry {
89765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
89865963Spendry 	struct vnode *dvp = un->un_uppervp;
89965963Spendry 
90065963Spendry 	if (dvp) {
90165963Spendry 		int error;
90265963Spendry 		struct vnode *vp;
90365963Spendry 
90465963Spendry 		VREF(dvp);
90565963Spendry 		VOP_LOCK(dvp);
90665963Spendry 		vput(ap->a_dvp);
90765963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
90865963Spendry 		if (error)
90965963Spendry 			return (error);
91065963Spendry 
91165963Spendry 		error = union_allocvp(
91265963Spendry 				ap->a_vpp,
913*65989Spendry 				ap->a_dvp->v_mount,
914*65989Spendry 				ap->a_dvp,
91565965Spendry 				NULLVP,
91665963Spendry 				ap->a_cnp,
91765963Spendry 				vp,
91865963Spendry 				NULLVP);
91965965Spendry 		VOP_UNLOCK(vp);
92065965Spendry 		if (error)
92165965Spendry 			vrele(vp);
92265963Spendry 		return (error);
92365963Spendry 	}
92465963Spendry 
92565963Spendry 	vput(ap->a_dvp);
92665963Spendry 	return (EROFS);
92765963Spendry }
92865963Spendry 
92965963Spendry int
93065963Spendry union_rmdir(ap)
93165963Spendry 	struct vop_rmdir_args /* {
93265963Spendry 		struct vnode *a_dvp;
93365963Spendry 		struct vnode *a_vp;
93465963Spendry 		struct componentname *a_cnp;
93565963Spendry 	} */ *ap;
93665963Spendry {
93765963Spendry 	int error;
93865963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
93965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
94065963Spendry 
94165963Spendry 	if (dun->un_uppervp && un->un_uppervp) {
94265963Spendry 		struct vnode *dvp = dun->un_uppervp;
94365963Spendry 		struct vnode *vp = un->un_uppervp;
94465963Spendry 
94565963Spendry 		VREF(dvp);
94665963Spendry 		VOP_LOCK(dvp);
94765963Spendry 		vput(ap->a_dvp);
94865963Spendry 		VREF(vp);
94965963Spendry 		VOP_LOCK(vp);
95065963Spendry 		vput(ap->a_vp);
95165963Spendry 
95265963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
95365963Spendry 	} else {
95465963Spendry 		/*
95565963Spendry 		 * XXX: should create a whiteout here
95665963Spendry 		 */
95765963Spendry 		vput(ap->a_dvp);
95865963Spendry 		vput(ap->a_vp);
95965963Spendry 		error = EROFS;
96065963Spendry 	}
96165963Spendry 
96265963Spendry 	return (error);
96365963Spendry }
96465963Spendry 
96565963Spendry int
96665963Spendry union_symlink(ap)
96765963Spendry 	struct vop_symlink_args /* {
96865963Spendry 		struct vnode *a_dvp;
96965963Spendry 		struct vnode **a_vpp;
97065963Spendry 		struct componentname *a_cnp;
97165963Spendry 		struct vattr *a_vap;
97265963Spendry 		char *a_target;
97365963Spendry 	} */ *ap;
97465963Spendry {
97565963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
97665963Spendry 	struct vnode *dvp = un->un_uppervp;
97765963Spendry 
97865963Spendry 	if (dvp) {
97965963Spendry 		int error;
98065963Spendry 		struct vnode *vp;
98165963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
98265963Spendry 
98365963Spendry 		VREF(dvp);
98465963Spendry 		VOP_LOCK(dvp);
98565963Spendry 		vput(ap->a_dvp);
98665963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
98765963Spendry 					ap->a_vap, ap->a_target);
98865965Spendry 		*ap->a_vpp = 0;
98965963Spendry 		return (error);
99065963Spendry 	}
99165963Spendry 
99265963Spendry 	vput(ap->a_dvp);
99365963Spendry 	return (EROFS);
99465963Spendry }
99565963Spendry 
99665935Spendry /*
99765935Spendry  * union_readdir works in concert with getdirentries and
99865935Spendry  * readdir(3) to provide a list of entries in the unioned
99965935Spendry  * directories.  getdirentries is responsible for walking
100065935Spendry  * down the union stack.  readdir(3) is responsible for
100165935Spendry  * eliminating duplicate names from the returned data stream.
100265935Spendry  */
100365935Spendry int
100465935Spendry union_readdir(ap)
100565935Spendry 	struct vop_readdir_args /* {
100665935Spendry 		struct vnodeop_desc *a_desc;
100765935Spendry 		struct vnode *a_vp;
100865935Spendry 		struct uio *a_uio;
100965935Spendry 		struct ucred *a_cred;
101065935Spendry 	} */ *ap;
101165935Spendry {
101265963Spendry 	int error = 0;
101365935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
101465935Spendry 
101565963Spendry 	if (un->un_uppervp) {
101665963Spendry 		struct vnode *vp = OTHERVP(ap->a_vp);
101765935Spendry 
101865963Spendry 		VOP_LOCK(vp);
101965963Spendry 		error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
102065963Spendry 		VOP_UNLOCK(vp);
102165963Spendry 	}
102265963Spendry 
102365963Spendry 	return (error);
102465935Spendry }
102565935Spendry 
102665935Spendry int
102765963Spendry union_readlink(ap)
102865963Spendry 	struct vop_readlink_args /* {
102965963Spendry 		struct vnode *a_vp;
103065963Spendry 		struct uio *a_uio;
103165963Spendry 		struct ucred *a_cred;
103265963Spendry 	} */ *ap;
103365963Spendry {
103465963Spendry 	int error;
103565963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
103665963Spendry 
103765963Spendry 	VOP_LOCK(vp);
103865963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
103965963Spendry 	VOP_UNLOCK(vp);
104065963Spendry 
104165963Spendry 	return (error);
104265963Spendry }
104365963Spendry 
104465963Spendry int
104565963Spendry union_abortop(ap)
104665963Spendry 	struct vop_abortop_args /* {
104765963Spendry 		struct vnode *a_dvp;
104865963Spendry 		struct componentname *a_cnp;
104965963Spendry 	} */ *ap;
105065963Spendry {
105165963Spendry 	int error;
105265965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
105365963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
105465963Spendry 	int islocked = un->un_flags & UN_LOCKED;
105565963Spendry 
105665963Spendry 	if (islocked)
105765963Spendry 		VOP_LOCK(vp);
105865963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
105965963Spendry 	if (islocked)
106065963Spendry 		VOP_UNLOCK(vp);
106165963Spendry 
106265963Spendry 	return (error);
106365963Spendry }
106465963Spendry 
106565963Spendry int
106665935Spendry union_inactive(ap)
106765935Spendry 	struct vop_inactive_args /* {
106865935Spendry 		struct vnode *a_vp;
106965935Spendry 	} */ *ap;
107065935Spendry {
107165935Spendry 
107265935Spendry 	/*
107365935Spendry 	 * Do nothing (and _don't_ bypass).
107465935Spendry 	 * Wait to vrele lowervp until reclaim,
107565935Spendry 	 * so that until then our union_node is in the
107665935Spendry 	 * cache and reusable.
107765935Spendry 	 *
107865935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
107965935Spendry 	 * the lowervp and then trying to reactivate it
108065935Spendry 	 * with capabilities (v_id)
108165935Spendry 	 * like they do in the name lookup cache code.
108265935Spendry 	 * That's too much work for now.
108365935Spendry 	 */
1084*65989Spendry 
1085*65989Spendry #ifdef DIAGNOSTIC
1086*65989Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
1087*65989Spendry 
1088*65989Spendry 	if (un->un_flags & UN_LOCKED)
1089*65989Spendry 		panic("union: inactivating locked node");
1090*65989Spendry #endif
1091*65989Spendry 
109265935Spendry 	return (0);
109365935Spendry }
109465935Spendry 
109565935Spendry int
109665935Spendry union_reclaim(ap)
109765935Spendry 	struct vop_reclaim_args /* {
109865935Spendry 		struct vnode *a_vp;
109965935Spendry 	} */ *ap;
110065935Spendry {
110165935Spendry 	struct vnode *vp = ap->a_vp;
110265935Spendry 	struct union_node *un = VTOUNION(vp);
110365935Spendry 	struct vnode *uppervp = un->un_uppervp;
110465935Spendry 	struct vnode *lowervp = un->un_lowervp;
110565935Spendry 	struct vnode *dirvp = un->un_dirvp;
110665935Spendry 	char *path = un->un_path;
110765935Spendry 
110865935Spendry 	/*
110965935Spendry 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
111065935Spendry 	 * so we can't call VOPs on ourself.
111165935Spendry 	 */
111265935Spendry 	/* After this assignment, this node will not be re-used. */
111365935Spendry 	un->un_uppervp = 0;
111465935Spendry 	un->un_lowervp = 0;
111565935Spendry 	un->un_dirvp = 0;
111665935Spendry 	un->un_path = NULL;
111765935Spendry 	union_freevp(vp);
111865935Spendry 	if (uppervp)
111965935Spendry 		vrele(uppervp);
112065935Spendry 	if (lowervp)
112165935Spendry 		vrele(lowervp);
112265935Spendry 	if (dirvp)
112365935Spendry 		vrele(dirvp);
112465935Spendry 	if (path)
112565935Spendry 		free(path, M_TEMP);
112665935Spendry 	return (0);
112765935Spendry }
112865935Spendry 
112965963Spendry int
113065963Spendry union_lock(ap)
113165963Spendry 	struct vop_lock_args *ap;
113265963Spendry {
113365963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
113465935Spendry 
113565965Spendry 	while (un->un_flags & UN_LOCKED) {
113665963Spendry #ifdef DIAGNOSTIC
1137*65989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
1138*65989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
1139*65989Spendry 			panic("union: locking against myself");
114065963Spendry #endif
114165963Spendry 		un->un_flags |= UN_WANT;
114265963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
114365963Spendry 	}
114465963Spendry 	un->un_flags |= UN_LOCKED;
1145*65989Spendry 
114665963Spendry #ifdef DIAGNOSTIC
1147*65989Spendry 	if (curproc)
1148*65989Spendry 		un->un_pid = curproc->p_pid;
1149*65989Spendry 	else
1150*65989Spendry 		un->un_pid = -1;
115165963Spendry #endif
115265963Spendry }
115365963Spendry 
115465935Spendry int
115565963Spendry union_unlock(ap)
115665963Spendry 	struct vop_lock_args *ap;
115765963Spendry {
115865963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
115965963Spendry 
116065963Spendry #ifdef DIAGNOSTIC
116165965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
116265965Spendry 		panic("union: unlock unlocked node");
1163*65989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
1164*65989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
116565963Spendry 		panic("union: unlocking other process's union node");
116665963Spendry #endif
116765963Spendry 
116865963Spendry 	un->un_flags &= ~UN_LOCKED;
116965963Spendry 	if (un->un_flags & UN_WANT) {
117065963Spendry 		un->un_flags &= ~UN_WANT;
117165963Spendry 		wakeup((caddr_t) &un->un_flags);
117265963Spendry 	}
117365963Spendry 
117465963Spendry #ifdef DIAGNOSTIC
117565963Spendry 	un->un_pid = 0;
117665963Spendry #endif
117765963Spendry }
117865963Spendry 
117965963Spendry int
118065963Spendry union_bmap(ap)
118165963Spendry 	struct vop_bmap_args /* {
118265963Spendry 		struct vnode *a_vp;
118365963Spendry 		daddr_t  a_bn;
118465963Spendry 		struct vnode **a_vpp;
118565963Spendry 		daddr_t *a_bnp;
118665963Spendry 		int *a_runp;
118765963Spendry 	} */ *ap;
118865963Spendry {
118965963Spendry 	int error;
119065963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
119165963Spendry 
119265963Spendry 	VOP_LOCK(vp);
119365963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
119465963Spendry 	VOP_UNLOCK(vp);
119565963Spendry 
119665963Spendry 	return (error);
119765963Spendry }
119865963Spendry 
119965963Spendry int
120065935Spendry union_print(ap)
120165935Spendry 	struct vop_print_args /* {
120265935Spendry 		struct vnode *a_vp;
120365935Spendry 	} */ *ap;
120465935Spendry {
120565935Spendry 	struct vnode *vp = ap->a_vp;
120665935Spendry 
120765935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
120865935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
120965935Spendry 	return (0);
121065935Spendry }
121165935Spendry 
121265963Spendry int
121365963Spendry union_islocked(ap)
121465963Spendry 	struct vop_islocked_args /* {
121565963Spendry 		struct vnode *a_vp;
121665963Spendry 	} */ *ap;
121765963Spendry {
121865935Spendry 
121965963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
122065963Spendry }
122165963Spendry 
122265935Spendry int
122365963Spendry union_pathconf(ap)
122465963Spendry 	struct vop_pathconf_args /* {
122565963Spendry 		struct vnode *a_vp;
122665963Spendry 		int a_name;
122765963Spendry 		int *a_retval;
122865935Spendry 	} */ *ap;
122965935Spendry {
123065935Spendry 	int error;
123165963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
123265935Spendry 
123365963Spendry 	VOP_LOCK(vp);
123465963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
123565963Spendry 	VOP_UNLOCK(vp);
123665935Spendry 
123765963Spendry 	return (error);
123865963Spendry }
123965935Spendry 
124065963Spendry int
124165963Spendry union_advlock(ap)
124265963Spendry 	struct vop_advlock_args /* {
124365963Spendry 		struct vnode *a_vp;
124465963Spendry 		caddr_t  a_id;
124565963Spendry 		int  a_op;
124665963Spendry 		struct flock *a_fl;
124765963Spendry 		int  a_flags;
124865963Spendry 	} */ *ap;
124965963Spendry {
125065935Spendry 
125165963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
125265963Spendry 				ap->a_fl, ap->a_flags));
125365935Spendry }
125465935Spendry 
125565935Spendry 
125665935Spendry /*
125765963Spendry  * XXX - vop_strategy must be hand coded because it has no
125865935Spendry  * vnode in its arguments.
125965935Spendry  * This goes away with a merged VM/buffer cache.
126065935Spendry  */
126165935Spendry int
126265963Spendry union_strategy(ap)
126365963Spendry 	struct vop_strategy_args /* {
126465935Spendry 		struct buf *a_bp;
126565935Spendry 	} */ *ap;
126665935Spendry {
126765935Spendry 	struct buf *bp = ap->a_bp;
126865935Spendry 	int error;
126965935Spendry 	struct vnode *savedvp;
127065935Spendry 
127165935Spendry 	savedvp = bp->b_vp;
127265963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
127365935Spendry 
127465935Spendry #ifdef DIAGNOSTIC
127565935Spendry 	if (bp->b_vp == 0)
127665963Spendry 		panic("union_strategy: nil vp");
127765963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
127865963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
127965963Spendry 		panic("union_strategy: writing to lowervp");
128065935Spendry #endif
128165935Spendry 
128265963Spendry 	error = VOP_STRATEGY(bp);
128365935Spendry 	bp->b_vp = savedvp;
128465935Spendry 
128565935Spendry 	return (error);
128665935Spendry }
128765935Spendry 
128865935Spendry /*
128965935Spendry  * Global vfs data structures
129065935Spendry  */
129165935Spendry int (**union_vnodeop_p)();
129265965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
129365963Spendry 	{ &vop_default_desc, vn_default_error },
129465963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
129565963Spendry 	{ &vop_create_desc, union_create },		/* create */
129665963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
129765963Spendry 	{ &vop_open_desc, union_open },			/* open */
129865963Spendry 	{ &vop_close_desc, union_close },		/* close */
129965963Spendry 	{ &vop_access_desc, union_access },		/* access */
130065963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
130165963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
130265963Spendry 	{ &vop_read_desc, union_read },			/* read */
130365963Spendry 	{ &vop_write_desc, union_write },		/* write */
130465963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
130565963Spendry 	{ &vop_select_desc, union_select },		/* select */
130665963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
130765963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
130865963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
130965963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
131065963Spendry 	{ &vop_link_desc, union_link },			/* link */
131165963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
131265963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
131365963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
131465963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
131565963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
131665963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
131765963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
131865963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
131965963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
132065963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
132165963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
132265963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
132365963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
132465963Spendry 	{ &vop_print_desc, union_print },		/* print */
132565963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
132665963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
132765963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
132865963Spendry #ifdef notdef
132965963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
133065963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
133165963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
133265963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
133365963Spendry 	{ &vop_update_desc, union_update },		/* update */
133465963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
133565963Spendry #endif
133665935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
133765935Spendry };
133865935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
133965935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1340