xref: /csrg-svn/sys/miscfs/union/union_vnops.c (revision 67446)
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*67446Spendry  *	@(#)union_vnops.c	8.18 (Berkeley) 06/24/94
1265935Spendry  */
1365935Spendry 
1465935Spendry #include <sys/param.h>
1565935Spendry #include <sys/systm.h>
1665935Spendry #include <sys/proc.h>
1765935Spendry #include <sys/file.h>
1865935Spendry #include <sys/time.h>
1965935Spendry #include <sys/types.h>
2065935Spendry #include <sys/vnode.h>
2165935Spendry #include <sys/mount.h>
2265935Spendry #include <sys/namei.h>
2365935Spendry #include <sys/malloc.h>
2465935Spendry #include <sys/buf.h>
2566053Spendry #include <sys/queue.h>
2666055Spendry #include <miscfs/union/union.h>
2765935Spendry 
2866152Spendry #define FIXUP(un) { \
2966152Spendry 	if (((un)->un_flags & UN_ULOCK) == 0) { \
3066152Spendry 		union_fixup(un); \
3166152Spendry 	} \
3266152Spendry }
3366152Spendry 
3466152Spendry static void
3566152Spendry union_fixup(un)
3666152Spendry 	struct union_node *un;
3766152Spendry {
3866152Spendry 
3966152Spendry 	VOP_LOCK(un->un_uppervp);
4066152Spendry 	un->un_flags |= UN_ULOCK;
4166152Spendry }
4266152Spendry 
4365935Spendry static int
4467064Spendry union_lookup1(udvp, dvpp, vpp, cnp)
4565989Spendry 	struct vnode *udvp;
4667064Spendry 	struct vnode **dvpp;
4765935Spendry 	struct vnode **vpp;
4865935Spendry 	struct componentname *cnp;
4965935Spendry {
5065935Spendry 	int error;
5165935Spendry 	struct vnode *tdvp;
5267064Spendry 	struct vnode *dvp;
5365935Spendry 	struct mount *mp;
5465935Spendry 
5567064Spendry 	dvp = *dvpp;
5667064Spendry 
5765994Spendry 	/*
5865994Spendry 	 * If stepping up the directory tree, check for going
5965994Spendry 	 * back across the mount point, in which case do what
6065994Spendry 	 * lookup would do by stepping back down the mount
6165994Spendry 	 * hierarchy.
6265994Spendry 	 */
6365935Spendry 	if (cnp->cn_flags & ISDOTDOT) {
6467064Spendry 		while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
6566034Spendry 			/*
6666034Spendry 			 * Don't do the NOCROSSMOUNT check
6766034Spendry 			 * at this level.  By definition,
6866034Spendry 			 * union fs deals with namespaces, not
6966034Spendry 			 * filesystems.
7066034Spendry 			 */
7165935Spendry 			tdvp = dvp;
7267064Spendry 			*dvpp = dvp = dvp->v_mount->mnt_vnodecovered;
7365935Spendry 			vput(tdvp);
7465935Spendry 			VREF(dvp);
7565935Spendry 			VOP_LOCK(dvp);
7665935Spendry 		}
7765935Spendry 	}
7866027Spendry 
7965935Spendry         error = VOP_LOOKUP(dvp, &tdvp, cnp);
8065935Spendry 	if (error)
8165935Spendry 		return (error);
8265935Spendry 
8365994Spendry 	/*
8466027Spendry 	 * The parent directory will have been unlocked, unless lookup
8566027Spendry 	 * found the last component.  In which case, re-lock the node
8666027Spendry 	 * here to allow it to be unlocked again (phew) in union_lookup.
8765994Spendry 	 */
8866027Spendry 	if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
8965994Spendry 		VOP_LOCK(dvp);
9065994Spendry 
9165935Spendry 	dvp = tdvp;
9265994Spendry 
9365994Spendry 	/*
9465994Spendry 	 * Lastly check if the current node is a mount point in
9566034Spendry 	 * which case walk up the mount hierarchy making sure not to
9665994Spendry 	 * bump into the root of the mount tree (ie. dvp != udvp).
9765994Spendry 	 */
9865989Spendry 	while (dvp != udvp && (dvp->v_type == VDIR) &&
9966034Spendry 	       (mp = dvp->v_mountedhere)) {
10065935Spendry 
10165935Spendry 		if (mp->mnt_flag & MNT_MLOCK) {
10265935Spendry 			mp->mnt_flag |= MNT_MWAIT;
10365935Spendry 			sleep((caddr_t) mp, PVFS);
10465935Spendry 			continue;
10565935Spendry 		}
10665935Spendry 
10765935Spendry 		if (error = VFS_ROOT(mp, &tdvp)) {
10865935Spendry 			vput(dvp);
10965935Spendry 			return (error);
11065935Spendry 		}
11165935Spendry 
11265965Spendry 		vput(dvp);
11365935Spendry 		dvp = tdvp;
11465935Spendry 	}
11565935Spendry 
11665935Spendry 	*vpp = dvp;
11765935Spendry 	return (0);
11865935Spendry }
11965935Spendry 
12065935Spendry int
12165935Spendry union_lookup(ap)
12265935Spendry 	struct vop_lookup_args /* {
12365935Spendry 		struct vnodeop_desc *a_desc;
12465935Spendry 		struct vnode *a_dvp;
12565935Spendry 		struct vnode **a_vpp;
12665935Spendry 		struct componentname *a_cnp;
12765935Spendry 	} */ *ap;
12865935Spendry {
12965965Spendry 	int error;
13065935Spendry 	int uerror, lerror;
13165935Spendry 	struct vnode *uppervp, *lowervp;
13265935Spendry 	struct vnode *upperdvp, *lowerdvp;
13365935Spendry 	struct vnode *dvp = ap->a_dvp;
13465989Spendry 	struct union_node *dun = VTOUNION(dvp);
13565935Spendry 	struct componentname *cnp = ap->a_cnp;
13665935Spendry 	int lockparent = cnp->cn_flags & LOCKPARENT;
13765994Spendry 	int rdonly = cnp->cn_flags & RDONLY;
13865997Spendry 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
13966152Spendry 	struct ucred *saved_cred;
14065935Spendry 
141*67446Spendry #ifdef notyet
142*67446Spendry 	if (cnp->cn_namelen == 3 &&
143*67446Spendry 			cnp->cn_nameptr[2] == '.' &&
144*67446Spendry 			cnp->cn_nameptr[1] == '.' &&
145*67446Spendry 			cnp->cn_nameptr[0] == '.') {
146*67446Spendry 		dvp = *ap->a_vpp = LOWERVP(ap->a_dvp);
147*67446Spendry 		if (dvp == NULLVP)
148*67446Spendry 			return (ENOENT);
149*67446Spendry 		VREF(dvp);
150*67446Spendry 		VOP_LOCK(dvp);
151*67446Spendry 		if (!lockparent || !(cnp->cn_flags & ISLASTCN))
152*67446Spendry 			VOP_UNLOCK(ap->a_dvp);
153*67446Spendry 		return (0);
154*67446Spendry 	}
155*67446Spendry #endif
156*67446Spendry 
15765965Spendry 	cnp->cn_flags |= LOCKPARENT;
15865965Spendry 
15965935Spendry 	upperdvp = dun->un_uppervp;
16065935Spendry 	lowerdvp = dun->un_lowervp;
16165997Spendry 	uppervp = NULLVP;
16265997Spendry 	lowervp = NULLVP;
16365935Spendry 
16465935Spendry 	/*
16565935Spendry 	 * do the lookup in the upper level.
16665935Spendry 	 * if that level comsumes additional pathnames,
16765935Spendry 	 * then assume that something special is going
16865935Spendry 	 * on and just return that vnode.
16965935Spendry 	 */
17067076Spendry 	if (upperdvp != NULLVP) {
17166152Spendry 		FIXUP(dun);
17267064Spendry 		uerror = union_lookup1(um->um_uppervp, &upperdvp,
17365997Spendry 					&uppervp, cnp);
17466051Spendry 		/*if (uppervp == upperdvp)
17566051Spendry 			dun->un_flags |= UN_KLOCK;*/
17665965Spendry 
17765935Spendry 		if (cnp->cn_consume != 0) {
17865935Spendry 			*ap->a_vpp = uppervp;
17965965Spendry 			if (!lockparent)
18065965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
18165935Spendry 			return (uerror);
18265935Spendry 		}
18365935Spendry 	} else {
18465935Spendry 		uerror = ENOENT;
18565935Spendry 	}
18665935Spendry 
18765935Spendry 	/*
18865935Spendry 	 * in a similar way to the upper layer, do the lookup
18965935Spendry 	 * in the lower layer.   this time, if there is some
19065935Spendry 	 * component magic going on, then vput whatever we got
19165935Spendry 	 * back from the upper layer and return the lower vnode
19265935Spendry 	 * instead.
19365935Spendry 	 */
19467076Spendry 	if (lowerdvp != NULLVP) {
19566051Spendry 		int nameiop;
19666051Spendry 
19765965Spendry 		VOP_LOCK(lowerdvp);
19866051Spendry 
19966051Spendry 		/*
20066051Spendry 		 * Only do a LOOKUP on the bottom node, since
20166051Spendry 		 * we won't be making changes to it anyway.
20266051Spendry 		 */
20366051Spendry 		nameiop = cnp->cn_nameiop;
20466051Spendry 		cnp->cn_nameiop = LOOKUP;
20566152Spendry 		if (um->um_op == UNMNT_BELOW) {
20666152Spendry 			saved_cred = cnp->cn_cred;
20766152Spendry 			cnp->cn_cred = um->um_cred;
20866152Spendry 		}
20967064Spendry 		lerror = union_lookup1(um->um_lowervp, &lowerdvp,
21066051Spendry 				&lowervp, cnp);
21166152Spendry 		if (um->um_op == UNMNT_BELOW)
21266152Spendry 			cnp->cn_cred = saved_cred;
21366051Spendry 		cnp->cn_nameiop = nameiop;
21466051Spendry 
21565989Spendry 		if (lowervp != lowerdvp)
21665989Spendry 			VOP_UNLOCK(lowerdvp);
21765965Spendry 
21865935Spendry 		if (cnp->cn_consume != 0) {
21967076Spendry 			if (uppervp != NULLVP) {
22066051Spendry 				if (uppervp == upperdvp)
22166051Spendry 					vrele(uppervp);
22266051Spendry 				else
22366051Spendry 					vput(uppervp);
22465997Spendry 				uppervp = NULLVP;
22565935Spendry 			}
22665935Spendry 			*ap->a_vpp = lowervp;
22765965Spendry 			if (!lockparent)
22865965Spendry 				cnp->cn_flags &= ~LOCKPARENT;
22965935Spendry 			return (lerror);
23065935Spendry 		}
23165935Spendry 	} else {
23265935Spendry 		lerror = ENOENT;
23367416Spendry 		if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
23467416Spendry 			lowervp = LOWERVP(dun->un_pvp);
23567416Spendry 			if (lowervp != NULLVP) {
23667416Spendry 				VREF(lowervp);
23767416Spendry 				VOP_LOCK(lowervp);
23867416Spendry 				lerror = 0;
23967416Spendry 			}
24067416Spendry 		}
24165935Spendry 	}
24265935Spendry 
24365965Spendry 	if (!lockparent)
24465965Spendry 		cnp->cn_flags &= ~LOCKPARENT;
24565965Spendry 
24665935Spendry 	/*
24765935Spendry 	 * at this point, we have uerror and lerror indicating
24865935Spendry 	 * possible errors with the lookups in the upper and lower
24965935Spendry 	 * layers.  additionally, uppervp and lowervp are (locked)
25065935Spendry 	 * references to existing vnodes in the upper and lower layers.
25165935Spendry 	 *
25265935Spendry 	 * there are now three cases to consider.
25365935Spendry 	 * 1. if both layers returned an error, then return whatever
25465935Spendry 	 *    error the upper layer generated.
25565935Spendry 	 *
25665935Spendry 	 * 2. if the top layer failed and the bottom layer succeeded
25765935Spendry 	 *    then two subcases occur.
25865935Spendry 	 *    a.  the bottom vnode is not a directory, in which
25965935Spendry 	 *	  case just return a new union vnode referencing
26065935Spendry 	 *	  an empty top layer and the existing bottom layer.
26165935Spendry 	 *    b.  the bottom vnode is a directory, in which case
26265935Spendry 	 *	  create a new directory in the top-level and
26365935Spendry 	 *	  continue as in case 3.
26465935Spendry 	 *
26565935Spendry 	 * 3. if the top layer succeeded then return a new union
26665935Spendry 	 *    vnode referencing whatever the new top layer and
26765935Spendry 	 *    whatever the bottom layer returned.
26865935Spendry 	 */
26965935Spendry 
27066027Spendry 	*ap->a_vpp = NULLVP;
27166027Spendry 
27265935Spendry 	/* case 1. */
27365935Spendry 	if ((uerror != 0) && (lerror != 0)) {
27465935Spendry 		return (uerror);
27565935Spendry 	}
27665935Spendry 
27765935Spendry 	/* case 2. */
27865935Spendry 	if (uerror != 0 /* && (lerror == 0) */ ) {
27965935Spendry 		if (lowervp->v_type == VDIR) { /* case 2b. */
28066051Spendry 			dun->un_flags &= ~UN_ULOCK;
28166051Spendry 			VOP_UNLOCK(upperdvp);
28265997Spendry 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
28366051Spendry 			VOP_LOCK(upperdvp);
28466051Spendry 			dun->un_flags |= UN_ULOCK;
28566051Spendry 
28665935Spendry 			if (uerror) {
28767076Spendry 				if (lowervp != NULLVP) {
28865935Spendry 					vput(lowervp);
28965997Spendry 					lowervp = NULLVP;
29065935Spendry 				}
29165935Spendry 				return (uerror);
29265935Spendry 			}
29365935Spendry 		}
29465935Spendry 	}
29565935Spendry 
29667076Spendry 	if (lowervp != NULLVP)
29765965Spendry 		VOP_UNLOCK(lowervp);
29865965Spendry 
29965997Spendry 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
30065997Spendry 			      uppervp, lowervp);
30165997Spendry 
30265965Spendry 	if (error) {
30367076Spendry 		if (uppervp != NULLVP)
30466051Spendry 			vput(uppervp);
30567076Spendry 		if (lowervp != NULLVP)
30665965Spendry 			vrele(lowervp);
30765965Spendry 	} else {
30865994Spendry 		if (*ap->a_vpp != dvp)
30965994Spendry 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
31065994Spendry 				VOP_UNLOCK(dvp);
31165965Spendry 	}
31265965Spendry 
31365965Spendry 	return (error);
31465935Spendry }
31565935Spendry 
31665963Spendry int
31765963Spendry union_create(ap)
31865963Spendry 	struct vop_create_args /* {
31965963Spendry 		struct vnode *a_dvp;
32065963Spendry 		struct vnode **a_vpp;
32165963Spendry 		struct componentname *a_cnp;
32265963Spendry 		struct vattr *a_vap;
32365963Spendry 	} */ *ap;
32465963Spendry {
32565963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
32665963Spendry 	struct vnode *dvp = un->un_uppervp;
32765963Spendry 
32867076Spendry 	if (dvp != NULLVP) {
32965963Spendry 		int error;
33065963Spendry 		struct vnode *vp;
33165963Spendry 
33266152Spendry 		FIXUP(un);
33366152Spendry 
33465963Spendry 		VREF(dvp);
33566051Spendry 		un->un_flags |= UN_KLOCK;
33665963Spendry 		vput(ap->a_dvp);
33765963Spendry 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
33865963Spendry 		if (error)
33965963Spendry 			return (error);
34065963Spendry 
34165963Spendry 		error = union_allocvp(
34265963Spendry 				ap->a_vpp,
34365989Spendry 				ap->a_dvp->v_mount,
34465989Spendry 				ap->a_dvp,
34565965Spendry 				NULLVP,
34665963Spendry 				ap->a_cnp,
34765963Spendry 				vp,
34865963Spendry 				NULLVP);
34965965Spendry 		if (error)
35066051Spendry 			vput(vp);
35165963Spendry 		return (error);
35265963Spendry 	}
35365963Spendry 
35465963Spendry 	vput(ap->a_dvp);
35565963Spendry 	return (EROFS);
35665963Spendry }
35765963Spendry 
35865963Spendry int
35965963Spendry union_mknod(ap)
36065963Spendry 	struct vop_mknod_args /* {
36165963Spendry 		struct vnode *a_dvp;
36265963Spendry 		struct vnode **a_vpp;
36365963Spendry 		struct componentname *a_cnp;
36465963Spendry 		struct vattr *a_vap;
36565963Spendry 	} */ *ap;
36665963Spendry {
36765963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
36865963Spendry 	struct vnode *dvp = un->un_uppervp;
36965963Spendry 
37067076Spendry 	if (dvp != NULLVP) {
37165963Spendry 		int error;
37265963Spendry 		struct vnode *vp;
37365963Spendry 
37466152Spendry 		FIXUP(un);
37566152Spendry 
37665963Spendry 		VREF(dvp);
37766051Spendry 		un->un_flags |= UN_KLOCK;
37865963Spendry 		vput(ap->a_dvp);
37965963Spendry 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
38065963Spendry 		if (error)
38165963Spendry 			return (error);
38265963Spendry 
38367076Spendry 		if (vp != NULLVP) {
38465965Spendry 			error = union_allocvp(
38565965Spendry 					ap->a_vpp,
38665989Spendry 					ap->a_dvp->v_mount,
38765989Spendry 					ap->a_dvp,
38865965Spendry 					NULLVP,
38965965Spendry 					ap->a_cnp,
39065965Spendry 					vp,
39165965Spendry 					NULLVP);
39265965Spendry 			if (error)
39366051Spendry 				vput(vp);
39465965Spendry 		}
39565963Spendry 		return (error);
39665963Spendry 	}
39765963Spendry 
39865963Spendry 	vput(ap->a_dvp);
39965963Spendry 	return (EROFS);
40065963Spendry }
40165963Spendry 
40265935Spendry int
40365935Spendry union_open(ap)
40465935Spendry 	struct vop_open_args /* {
40565935Spendry 		struct vnodeop_desc *a_desc;
40665935Spendry 		struct vnode *a_vp;
40765935Spendry 		int a_mode;
40865935Spendry 		struct ucred *a_cred;
40965935Spendry 		struct proc *a_p;
41065935Spendry 	} */ *ap;
41165935Spendry {
41265935Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
41365965Spendry 	struct vnode *tvp;
41465935Spendry 	int mode = ap->a_mode;
41565935Spendry 	struct ucred *cred = ap->a_cred;
41665935Spendry 	struct proc *p = ap->a_p;
41765965Spendry 	int error;
41865935Spendry 
41965935Spendry 	/*
42065935Spendry 	 * If there is an existing upper vp then simply open that.
42165935Spendry 	 */
42265965Spendry 	tvp = un->un_uppervp;
42365965Spendry 	if (tvp == NULLVP) {
42465935Spendry 		/*
42565965Spendry 		 * If the lower vnode is being opened for writing, then
42665965Spendry 		 * copy the file contents to the upper vnode and open that,
42765965Spendry 		 * otherwise can simply open the lower vnode.
42865935Spendry 		 */
42965965Spendry 		tvp = un->un_lowervp;
43065965Spendry 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
43167169Spendry 			error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p);
43265965Spendry 			if (error == 0)
43365965Spendry 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
43465965Spendry 			return (error);
43565935Spendry 		}
43666051Spendry 
43766051Spendry 		/*
43866051Spendry 		 * Just open the lower vnode
43966051Spendry 		 */
44066027Spendry 		un->un_openl++;
44166051Spendry 		VOP_LOCK(tvp);
44266051Spendry 		error = VOP_OPEN(tvp, mode, cred, p);
44366051Spendry 		VOP_UNLOCK(tvp);
44466051Spendry 
44566051Spendry 		return (error);
44665935Spendry 	}
44765935Spendry 
44866152Spendry 	FIXUP(un);
44966152Spendry 
45065965Spendry 	error = VOP_OPEN(tvp, mode, cred, p);
45165965Spendry 
45265965Spendry 	return (error);
45365935Spendry }
45465935Spendry 
45565963Spendry int
45665963Spendry union_close(ap)
45765963Spendry 	struct vop_close_args /* {
45865963Spendry 		struct vnode *a_vp;
45965963Spendry 		int  a_fflag;
46065963Spendry 		struct ucred *a_cred;
46165963Spendry 		struct proc *a_p;
46265963Spendry 	} */ *ap;
46365963Spendry {
46465997Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
46565997Spendry 	struct vnode *vp;
46665963Spendry 
46767076Spendry 	if (un->un_uppervp != NULLVP) {
46865997Spendry 		vp = un->un_uppervp;
46965997Spendry 	} else {
47066027Spendry #ifdef UNION_DIAGNOSTIC
47166027Spendry 		if (un->un_openl <= 0)
47266027Spendry 			panic("union: un_openl cnt");
47365997Spendry #endif
47466027Spendry 		--un->un_openl;
47565997Spendry 		vp = un->un_lowervp;
47665997Spendry 	}
47766027Spendry 
47865997Spendry 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
47965963Spendry }
48065963Spendry 
48165935Spendry /*
48265963Spendry  * Check access permission on the union vnode.
48365963Spendry  * The access check being enforced is to check
48465963Spendry  * against both the underlying vnode, and any
48565963Spendry  * copied vnode.  This ensures that no additional
48665963Spendry  * file permissions are given away simply because
48765963Spendry  * the user caused an implicit file copy.
48865963Spendry  */
48965963Spendry int
49065963Spendry union_access(ap)
49165963Spendry 	struct vop_access_args /* {
49265963Spendry 		struct vnodeop_desc *a_desc;
49365963Spendry 		struct vnode *a_vp;
49465963Spendry 		int a_mode;
49565963Spendry 		struct ucred *a_cred;
49665963Spendry 		struct proc *a_p;
49765963Spendry 	} */ *ap;
49865963Spendry {
49965963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
50066149Spendry 	int error = EACCES;
50165963Spendry 	struct vnode *vp;
50265963Spendry 
50367076Spendry 	if ((vp = un->un_uppervp) != NULLVP) {
50466152Spendry 		FIXUP(un);
50566152Spendry 		return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p));
50666152Spendry 	}
50766152Spendry 
50867076Spendry 	if ((vp = un->un_lowervp) != NULLVP) {
50965965Spendry 		VOP_LOCK(vp);
51065963Spendry 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
51166152Spendry 		if (error == 0) {
51266152Spendry 			struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount);
51366152Spendry 
51466152Spendry 			if (um->um_op == UNMNT_BELOW)
51566152Spendry 				error = VOP_ACCESS(vp, ap->a_mode,
51666152Spendry 						um->um_cred, ap->a_p);
51766152Spendry 		}
51865965Spendry 		VOP_UNLOCK(vp);
51965963Spendry 		if (error)
52065963Spendry 			return (error);
52165963Spendry 	}
52265963Spendry 
52365965Spendry 	return (error);
52465963Spendry }
52565963Spendry 
52665963Spendry /*
52767109Spendry  * We handle getattr only to change the fsid and
52867109Spendry  * track object sizes
52965935Spendry  */
53065935Spendry int
53165935Spendry union_getattr(ap)
53265935Spendry 	struct vop_getattr_args /* {
53365935Spendry 		struct vnode *a_vp;
53465935Spendry 		struct vattr *a_vap;
53565935Spendry 		struct ucred *a_cred;
53665935Spendry 		struct proc *a_p;
53765935Spendry 	} */ *ap;
53865935Spendry {
53965935Spendry 	int error;
54066062Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
54166062Spendry 	struct vnode *vp = un->un_uppervp;
54266062Spendry 	struct vattr *vap;
54366062Spendry 	struct vattr va;
54465935Spendry 
54566062Spendry 
54666062Spendry 	/*
54766062Spendry 	 * Some programs walk the filesystem hierarchy by counting
54866062Spendry 	 * links to directories to avoid stat'ing all the time.
54966062Spendry 	 * This means the link count on directories needs to be "correct".
55066062Spendry 	 * The only way to do that is to call getattr on both layers
55166062Spendry 	 * and fix up the link count.  The link count will not necessarily
55266062Spendry 	 * be accurate but will be large enough to defeat the tree walkers.
55366062Spendry 	 */
55466062Spendry 
55566062Spendry 	vap = ap->a_vap;
55666062Spendry 
55766062Spendry 	vp = un->un_uppervp;
55866062Spendry 	if (vp != NULLVP) {
55967073Spendry 		/*
56067073Spendry 		 * It's not clear whether VOP_GETATTR is to be
56167073Spendry 		 * called with the vnode locked or not.  stat() calls
56267073Spendry 		 * it with (vp) locked, and fstat calls it with
56367073Spendry 		 * (vp) unlocked.
56467073Spendry 		 * In the mean time, compensate here by checking
56567073Spendry 		 * the union_node's lock flag.
56667073Spendry 		 */
56767073Spendry 		if (un->un_flags & UN_LOCKED)
56867073Spendry 			FIXUP(un);
56967073Spendry 
57066062Spendry 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
57166062Spendry 		if (error)
57266062Spendry 			return (error);
57367109Spendry 		union_newsize(ap->a_vp, vap->va_size, VNOVAL);
57466062Spendry 	}
57566062Spendry 
57666062Spendry 	if (vp == NULLVP) {
57766062Spendry 		vp = un->un_lowervp;
57866062Spendry 	} else if (vp->v_type == VDIR) {
57966062Spendry 		vp = un->un_lowervp;
58066062Spendry 		vap = &va;
58166062Spendry 	} else {
58266062Spendry 		vp = NULLVP;
58366062Spendry 	}
58466062Spendry 
58566062Spendry 	if (vp != NULLVP) {
58666051Spendry 		VOP_LOCK(vp);
58766062Spendry 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
58866051Spendry 		VOP_UNLOCK(vp);
58966062Spendry 		if (error)
59066062Spendry 			return (error);
59167109Spendry 		union_newsize(ap->a_vp, VNOVAL, vap->va_size);
59266062Spendry 	}
59365965Spendry 
59466062Spendry 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
59566062Spendry 		ap->a_vap->va_nlink += vap->va_nlink;
59666062Spendry 
59767400Spendry 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
59865935Spendry 	return (0);
59965935Spendry }
60065935Spendry 
60165963Spendry int
60265965Spendry union_setattr(ap)
60365963Spendry 	struct vop_setattr_args /* {
60465963Spendry 		struct vnode *a_vp;
60565963Spendry 		struct vattr *a_vap;
60665963Spendry 		struct ucred *a_cred;
60765963Spendry 		struct proc *a_p;
60865963Spendry 	} */ *ap;
60965963Spendry {
61065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
61165963Spendry 	int error;
61265963Spendry 
61366057Spendry 	/*
61466057Spendry 	 * Handle case of truncating lower object to zero size,
61566057Spendry 	 * by creating a zero length upper object.  This is to
61666057Spendry 	 * handle the case of open with O_TRUNC and O_CREAT.
61766057Spendry 	 */
61866057Spendry 	if ((un->un_uppervp == NULLVP) &&
61966057Spendry 	    /* assert(un->un_lowervp != NULLVP) */
62066057Spendry 	    (un->un_lowervp->v_type == VREG) &&
62166057Spendry 	    (ap->a_vap->va_size == 0)) {
62266057Spendry 		struct vnode *vp;
62366057Spendry 
62466057Spendry 		error = union_vn_create(&vp, un, ap->a_p);
62566057Spendry 		if (error)
62666057Spendry 			return (error);
62766057Spendry 
62866057Spendry 		/* at this point, uppervp is locked */
62966057Spendry 		union_newupper(un, vp);
63066057Spendry 
63166057Spendry 		VOP_UNLOCK(vp);
63266057Spendry 		union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p);
63366057Spendry 		VOP_LOCK(vp);
63466057Spendry 		un->un_flags |= UN_ULOCK;
63566057Spendry 	}
63666057Spendry 
63766057Spendry 	/*
63866057Spendry 	 * Try to set attributes in upper layer,
63966057Spendry 	 * otherwise return read-only filesystem error.
64066057Spendry 	 */
64166057Spendry 	if (un->un_uppervp != NULLVP) {
64266152Spendry 		FIXUP(un);
64365963Spendry 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
64465963Spendry 					ap->a_cred, ap->a_p);
64567109Spendry 		if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
64667109Spendry 			union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
64765963Spendry 	} else {
64865963Spendry 		error = EROFS;
64965963Spendry 	}
65065963Spendry 
65165963Spendry 	return (error);
65265963Spendry }
65365963Spendry 
65465963Spendry int
65565963Spendry union_read(ap)
65665963Spendry 	struct vop_read_args /* {
65765963Spendry 		struct vnode *a_vp;
65865963Spendry 		struct uio *a_uio;
65965963Spendry 		int  a_ioflag;
66065963Spendry 		struct ucred *a_cred;
66165963Spendry 	} */ *ap;
66265963Spendry {
66365963Spendry 	int error;
66465963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
66566051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
66665963Spendry 
66766051Spendry 	if (dolock)
66866051Spendry 		VOP_LOCK(vp);
66966152Spendry 	else
67066152Spendry 		FIXUP(VTOUNION(ap->a_vp));
67165963Spendry 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
67266051Spendry 	if (dolock)
67366051Spendry 		VOP_UNLOCK(vp);
67465963Spendry 
67567109Spendry 	/*
67667109Spendry 	 * XXX
67767109Spendry 	 * perhaps the size of the underlying object has changed under
67867109Spendry 	 * our feet.  take advantage of the offset information present
67967109Spendry 	 * in the uio structure.
68067109Spendry 	 */
68167109Spendry 	if (error == 0) {
68267109Spendry 		struct union_node *un = VTOUNION(ap->a_vp);
68367109Spendry 		off_t cur = ap->a_uio->uio_offset;
68467109Spendry 
68567109Spendry 		if (vp == un->un_uppervp) {
68667109Spendry 			if (cur > un->un_uppersz)
68767109Spendry 				union_newsize(ap->a_vp, cur, VNOVAL);
68867109Spendry 		} else {
68967109Spendry 			if (cur > un->un_lowersz)
69067109Spendry 				union_newsize(ap->a_vp, VNOVAL, cur);
69167109Spendry 		}
69267109Spendry 	}
69367109Spendry 
69465963Spendry 	return (error);
69565963Spendry }
69665963Spendry 
69765963Spendry int
69865963Spendry union_write(ap)
69965963Spendry 	struct vop_read_args /* {
70065963Spendry 		struct vnode *a_vp;
70165963Spendry 		struct uio *a_uio;
70265963Spendry 		int  a_ioflag;
70365963Spendry 		struct ucred *a_cred;
70465963Spendry 	} */ *ap;
70565963Spendry {
70665963Spendry 	int error;
70765963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
70866051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
70965963Spendry 
71066051Spendry 	if (dolock)
71166051Spendry 		VOP_LOCK(vp);
71266152Spendry 	else
71366152Spendry 		FIXUP(VTOUNION(ap->a_vp));
71465963Spendry 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
71566051Spendry 	if (dolock)
71666051Spendry 		VOP_UNLOCK(vp);
71765963Spendry 
71867109Spendry 	/*
71967109Spendry 	 * the size of the underlying object may be changed by the
72067109Spendry 	 * write.
72167109Spendry 	 */
72267109Spendry 	if (error == 0) {
72367109Spendry 		struct union_node *un = VTOUNION(ap->a_vp);
72467109Spendry 		off_t cur = ap->a_uio->uio_offset;
72567109Spendry 
72667109Spendry 		if (vp == un->un_uppervp) {
72767109Spendry 			if (cur > un->un_uppersz)
72867109Spendry 				union_newsize(ap->a_vp, cur, VNOVAL);
72967109Spendry 		} else {
73067109Spendry 			if (cur > un->un_lowersz)
73167109Spendry 				union_newsize(ap->a_vp, VNOVAL, cur);
73267109Spendry 		}
73367109Spendry 	}
73467109Spendry 
73565963Spendry 	return (error);
73665963Spendry }
73765963Spendry 
73865963Spendry int
73965963Spendry union_ioctl(ap)
74065963Spendry 	struct vop_ioctl_args /* {
74165963Spendry 		struct vnode *a_vp;
74265963Spendry 		int  a_command;
74365963Spendry 		caddr_t  a_data;
74465963Spendry 		int  a_fflag;
74565963Spendry 		struct ucred *a_cred;
74665963Spendry 		struct proc *a_p;
74765963Spendry 	} */ *ap;
74865963Spendry {
74965963Spendry 
75065963Spendry 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
75165963Spendry 				ap->a_fflag, ap->a_cred, ap->a_p));
75265963Spendry }
75365963Spendry 
75465963Spendry int
75565963Spendry union_select(ap)
75665963Spendry 	struct vop_select_args /* {
75765963Spendry 		struct vnode *a_vp;
75865963Spendry 		int  a_which;
75965963Spendry 		int  a_fflags;
76065963Spendry 		struct ucred *a_cred;
76165963Spendry 		struct proc *a_p;
76265963Spendry 	} */ *ap;
76365963Spendry {
76465963Spendry 
76565963Spendry 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
76665963Spendry 				ap->a_cred, ap->a_p));
76765963Spendry }
76865963Spendry 
76965963Spendry int
77065963Spendry union_mmap(ap)
77165963Spendry 	struct vop_mmap_args /* {
77265963Spendry 		struct vnode *a_vp;
77365963Spendry 		int  a_fflags;
77465963Spendry 		struct ucred *a_cred;
77565963Spendry 		struct proc *a_p;
77665963Spendry 	} */ *ap;
77765963Spendry {
77865963Spendry 
77965963Spendry 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
78065963Spendry 				ap->a_cred, ap->a_p));
78165963Spendry }
78265963Spendry 
78365963Spendry int
78465963Spendry union_fsync(ap)
78565963Spendry 	struct vop_fsync_args /* {
78665963Spendry 		struct vnode *a_vp;
78765963Spendry 		struct ucred *a_cred;
78865963Spendry 		int  a_waitfor;
78965963Spendry 		struct proc *a_p;
79065963Spendry 	} */ *ap;
79165963Spendry {
79265963Spendry 	int error = 0;
79365963Spendry 	struct vnode *targetvp = OTHERVP(ap->a_vp);
79465963Spendry 
79567076Spendry 	if (targetvp != NULLVP) {
79666051Spendry 		int dolock = (targetvp == LOWERVP(ap->a_vp));
79766051Spendry 
79866051Spendry 		if (dolock)
79966051Spendry 			VOP_LOCK(targetvp);
80066152Spendry 		else
80166152Spendry 			FIXUP(VTOUNION(ap->a_vp));
80265963Spendry 		error = VOP_FSYNC(targetvp, ap->a_cred,
80365963Spendry 					ap->a_waitfor, ap->a_p);
80466051Spendry 		if (dolock)
80566051Spendry 			VOP_UNLOCK(targetvp);
80665963Spendry 	}
80765963Spendry 
80865963Spendry 	return (error);
80965963Spendry }
81065963Spendry 
81165963Spendry int
81265963Spendry union_seek(ap)
81365963Spendry 	struct vop_seek_args /* {
81465963Spendry 		struct vnode *a_vp;
81565963Spendry 		off_t  a_oldoff;
81665963Spendry 		off_t  a_newoff;
81765963Spendry 		struct ucred *a_cred;
81865963Spendry 	} */ *ap;
81965963Spendry {
82065963Spendry 
82165963Spendry 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
82265963Spendry }
82365963Spendry 
82465963Spendry int
82565963Spendry union_remove(ap)
82665963Spendry 	struct vop_remove_args /* {
82765963Spendry 		struct vnode *a_dvp;
82865963Spendry 		struct vnode *a_vp;
82965963Spendry 		struct componentname *a_cnp;
83065963Spendry 	} */ *ap;
83165963Spendry {
83265963Spendry 	int error;
83365963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
83465963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
83565963Spendry 
83667076Spendry 	if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) {
83765963Spendry 		struct vnode *dvp = dun->un_uppervp;
83865963Spendry 		struct vnode *vp = un->un_uppervp;
83965963Spendry 
84066152Spendry 		FIXUP(dun);
84165963Spendry 		VREF(dvp);
84266051Spendry 		dun->un_flags |= UN_KLOCK;
84365963Spendry 		vput(ap->a_dvp);
84466152Spendry 		FIXUP(un);
84565963Spendry 		VREF(vp);
84666051Spendry 		un->un_flags |= UN_KLOCK;
84765963Spendry 		vput(ap->a_vp);
84865963Spendry 
84965963Spendry 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
85066027Spendry 		if (!error)
85166027Spendry 			union_removed_upper(un);
85266027Spendry 
85366027Spendry 		/*
85466027Spendry 		 * XXX: should create a whiteout here
85566027Spendry 		 */
85665963Spendry 	} else {
85765963Spendry 		/*
85865963Spendry 		 * XXX: should create a whiteout here
85965963Spendry 		 */
86065963Spendry 		vput(ap->a_dvp);
86165963Spendry 		vput(ap->a_vp);
86265963Spendry 		error = EROFS;
86365963Spendry 	}
86465963Spendry 
86565963Spendry 	return (error);
86665963Spendry }
86765963Spendry 
86865963Spendry int
86965963Spendry union_link(ap)
87065963Spendry 	struct vop_link_args /* {
87165963Spendry 		struct vnode *a_vp;
87265963Spendry 		struct vnode *a_tdvp;
87365963Spendry 		struct componentname *a_cnp;
87465963Spendry 	} */ *ap;
87565963Spendry {
87667169Spendry 	int error = 0;
87767169Spendry 	struct union_node *un;
87867169Spendry 	struct vnode *vp;
87967169Spendry 	struct vnode *tdvp;
88065963Spendry 
88167169Spendry 	un = VTOUNION(ap->a_vp);
88265963Spendry 
88367169Spendry 	if (ap->a_vp->v_op != ap->a_tdvp->v_op) {
88467169Spendry 		tdvp = ap->a_tdvp;
88567169Spendry 	} else {
88667169Spendry 		struct union_node *tdun = VTOUNION(ap->a_tdvp);
88767169Spendry 		if (tdun->un_uppervp == NULLVP) {
88867169Spendry 			VOP_LOCK(ap->a_tdvp);
88967169Spendry 			if (un->un_uppervp == tdun->un_dirvp) {
89067169Spendry 				un->un_flags &= ~UN_ULOCK;
89167169Spendry 				VOP_UNLOCK(un->un_uppervp);
89267169Spendry 			}
89367169Spendry 			error = union_copyup(tdun, 1, ap->a_cnp->cn_cred,
89467169Spendry 						ap->a_cnp->cn_proc);
89567169Spendry 			if (un->un_uppervp == tdun->un_dirvp) {
89667169Spendry 				VOP_LOCK(un->un_uppervp);
89767169Spendry 				un->un_flags |= UN_ULOCK;
89867169Spendry 			}
89967169Spendry 			VOP_UNLOCK(ap->a_tdvp);
90067169Spendry 		}
90167169Spendry 		tdvp = tdun->un_uppervp;
90267169Spendry 	}
90365963Spendry 
90467169Spendry 	vp = un->un_uppervp;
90567169Spendry 	if (vp == NULLVP)
90667169Spendry 		error = EROFS;
90767169Spendry 
90867169Spendry 	if (error) {
90965963Spendry 		vput(ap->a_vp);
91067169Spendry 		return (error);
91165963Spendry 	}
91265963Spendry 
91367169Spendry 	FIXUP(un);
91467169Spendry 	VREF(vp);
91567169Spendry 	un->un_flags |= UN_KLOCK;
91667169Spendry 	vput(ap->a_vp);
91767169Spendry 
91867169Spendry 	return (VOP_LINK(vp, tdvp, ap->a_cnp));
91965963Spendry }
92065963Spendry 
92165963Spendry int
92265963Spendry union_rename(ap)
92365963Spendry 	struct vop_rename_args  /* {
92465963Spendry 		struct vnode *a_fdvp;
92565963Spendry 		struct vnode *a_fvp;
92665963Spendry 		struct componentname *a_fcnp;
92765963Spendry 		struct vnode *a_tdvp;
92865963Spendry 		struct vnode *a_tvp;
92965963Spendry 		struct componentname *a_tcnp;
93065963Spendry 	} */ *ap;
93165963Spendry {
93265963Spendry 	int error;
93365963Spendry 
93465963Spendry 	struct vnode *fdvp = ap->a_fdvp;
93565963Spendry 	struct vnode *fvp = ap->a_fvp;
93665963Spendry 	struct vnode *tdvp = ap->a_tdvp;
93765963Spendry 	struct vnode *tvp = ap->a_tvp;
93865963Spendry 
93965963Spendry 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
94065963Spendry 		struct union_node *un = VTOUNION(fdvp);
94165997Spendry 		if (un->un_uppervp == NULLVP) {
94265963Spendry 			error = EROFS;
94365963Spendry 			goto bad;
94465963Spendry 		}
94565963Spendry 
94665963Spendry 		fdvp = un->un_uppervp;
94765963Spendry 		VREF(fdvp);
94865963Spendry 		vrele(ap->a_fdvp);
94965963Spendry 	}
95065963Spendry 
95165963Spendry 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
95265963Spendry 		struct union_node *un = VTOUNION(fvp);
95365997Spendry 		if (un->un_uppervp == NULLVP) {
95465963Spendry 			error = EROFS;
95565963Spendry 			goto bad;
95665963Spendry 		}
95765963Spendry 
95865963Spendry 		fvp = un->un_uppervp;
95965963Spendry 		VREF(fvp);
96065963Spendry 		vrele(ap->a_fvp);
96165963Spendry 	}
96265963Spendry 
96365963Spendry 	if (tdvp->v_op == union_vnodeop_p) {
96465963Spendry 		struct union_node *un = VTOUNION(tdvp);
96565997Spendry 		if (un->un_uppervp == NULLVP) {
96667076Spendry 			/*
96767076Spendry 			 * this should never happen in normal
96867076Spendry 			 * operation but might if there was
96967076Spendry 			 * a problem creating the top-level shadow
97067076Spendry 			 * directory.
97167076Spendry 			 */
97265963Spendry 			error = EROFS;
97365963Spendry 			goto bad;
97465963Spendry 		}
97565963Spendry 
97665963Spendry 		tdvp = un->un_uppervp;
97765963Spendry 		VREF(tdvp);
97866051Spendry 		un->un_flags |= UN_KLOCK;
97965997Spendry 		vput(ap->a_tdvp);
98065963Spendry 	}
98165963Spendry 
98267076Spendry 	if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
98365963Spendry 		struct union_node *un = VTOUNION(tvp);
98465963Spendry 
98565963Spendry 		tvp = un->un_uppervp;
98667076Spendry 		if (tvp != NULLVP) {
98767076Spendry 			VREF(tvp);
98867076Spendry 			un->un_flags |= UN_KLOCK;
98967076Spendry 		}
99065963Spendry 		vput(ap->a_tvp);
99165963Spendry 	}
99265963Spendry 
99365963Spendry 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
99465963Spendry 
99565963Spendry bad:
99665963Spendry 	vrele(fdvp);
99765963Spendry 	vrele(fvp);
99865963Spendry 	vput(tdvp);
99967076Spendry 	if (tvp != NULLVP)
100065963Spendry 		vput(tvp);
100165963Spendry 
100265963Spendry 	return (error);
100365963Spendry }
100465963Spendry 
100565963Spendry int
100665963Spendry union_mkdir(ap)
100765963Spendry 	struct vop_mkdir_args /* {
100865963Spendry 		struct vnode *a_dvp;
100965963Spendry 		struct vnode **a_vpp;
101065963Spendry 		struct componentname *a_cnp;
101165963Spendry 		struct vattr *a_vap;
101265963Spendry 	} */ *ap;
101365963Spendry {
101465963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
101565963Spendry 	struct vnode *dvp = un->un_uppervp;
101665963Spendry 
101767076Spendry 	if (dvp != NULLVP) {
101865963Spendry 		int error;
101965963Spendry 		struct vnode *vp;
102065963Spendry 
102166152Spendry 		FIXUP(un);
102265963Spendry 		VREF(dvp);
102366051Spendry 		un->un_flags |= UN_KLOCK;
102465963Spendry 		vput(ap->a_dvp);
102565963Spendry 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
102665963Spendry 		if (error)
102765963Spendry 			return (error);
102865963Spendry 
102965963Spendry 		error = union_allocvp(
103065963Spendry 				ap->a_vpp,
103165989Spendry 				ap->a_dvp->v_mount,
103265989Spendry 				ap->a_dvp,
103365965Spendry 				NULLVP,
103465963Spendry 				ap->a_cnp,
103565963Spendry 				vp,
103665963Spendry 				NULLVP);
103765965Spendry 		if (error)
103866051Spendry 			vput(vp);
103965963Spendry 		return (error);
104065963Spendry 	}
104165963Spendry 
104265963Spendry 	vput(ap->a_dvp);
104365963Spendry 	return (EROFS);
104465963Spendry }
104565963Spendry 
104665963Spendry int
104765963Spendry union_rmdir(ap)
104865963Spendry 	struct vop_rmdir_args /* {
104965963Spendry 		struct vnode *a_dvp;
105065963Spendry 		struct vnode *a_vp;
105165963Spendry 		struct componentname *a_cnp;
105265963Spendry 	} */ *ap;
105365963Spendry {
105465963Spendry 	int error;
105565963Spendry 	struct union_node *dun = VTOUNION(ap->a_dvp);
105665963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
105765963Spendry 
105867076Spendry 	if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) {
105965963Spendry 		struct vnode *dvp = dun->un_uppervp;
106065963Spendry 		struct vnode *vp = un->un_uppervp;
106165963Spendry 
106266152Spendry 		FIXUP(dun);
106365963Spendry 		VREF(dvp);
106466051Spendry 		dun->un_flags |= UN_KLOCK;
106565963Spendry 		vput(ap->a_dvp);
106666152Spendry 		FIXUP(un);
106765963Spendry 		VREF(vp);
106866051Spendry 		un->un_flags |= UN_KLOCK;
106965963Spendry 		vput(ap->a_vp);
107065963Spendry 
107166051Spendry 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
107266027Spendry 		if (!error)
107366027Spendry 			union_removed_upper(un);
107466027Spendry 
107566027Spendry 		/*
107666027Spendry 		 * XXX: should create a whiteout here
107766027Spendry 		 */
107865963Spendry 	} else {
107965963Spendry 		/*
108065963Spendry 		 * XXX: should create a whiteout here
108165963Spendry 		 */
108265963Spendry 		vput(ap->a_dvp);
108365963Spendry 		vput(ap->a_vp);
108465963Spendry 		error = EROFS;
108565963Spendry 	}
108665963Spendry 
108765963Spendry 	return (error);
108865963Spendry }
108965963Spendry 
109065963Spendry int
109165963Spendry union_symlink(ap)
109265963Spendry 	struct vop_symlink_args /* {
109365963Spendry 		struct vnode *a_dvp;
109465963Spendry 		struct vnode **a_vpp;
109565963Spendry 		struct componentname *a_cnp;
109665963Spendry 		struct vattr *a_vap;
109765963Spendry 		char *a_target;
109865963Spendry 	} */ *ap;
109965963Spendry {
110065963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
110165963Spendry 	struct vnode *dvp = un->un_uppervp;
110265963Spendry 
110367076Spendry 	if (dvp != NULLVP) {
110465963Spendry 		int error;
110565963Spendry 		struct vnode *vp;
110665963Spendry 		struct mount *mp = ap->a_dvp->v_mount;
110765963Spendry 
110866152Spendry 		FIXUP(un);
110965963Spendry 		VREF(dvp);
111066051Spendry 		un->un_flags |= UN_KLOCK;
111165963Spendry 		vput(ap->a_dvp);
111265963Spendry 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
111365963Spendry 					ap->a_vap, ap->a_target);
111465997Spendry 		*ap->a_vpp = NULLVP;
111565963Spendry 		return (error);
111665963Spendry 	}
111765963Spendry 
111865963Spendry 	vput(ap->a_dvp);
111965963Spendry 	return (EROFS);
112065963Spendry }
112165963Spendry 
112265935Spendry /*
112365935Spendry  * union_readdir works in concert with getdirentries and
112465935Spendry  * readdir(3) to provide a list of entries in the unioned
112565935Spendry  * directories.  getdirentries is responsible for walking
112665935Spendry  * down the union stack.  readdir(3) is responsible for
112765935Spendry  * eliminating duplicate names from the returned data stream.
112865935Spendry  */
112965935Spendry int
113065935Spendry union_readdir(ap)
113165935Spendry 	struct vop_readdir_args /* {
113265935Spendry 		struct vnodeop_desc *a_desc;
113365935Spendry 		struct vnode *a_vp;
113465935Spendry 		struct uio *a_uio;
113565935Spendry 		struct ucred *a_cred;
113667369Smckusick 		int *a_eofflag;
113767369Smckusick 		u_long *a_cookies;
113867369Smckusick 		int a_ncookies;
113965935Spendry 	} */ *ap;
114065935Spendry {
114167369Smckusick 	register struct union_node *un = VTOUNION(ap->a_vp);
114267369Smckusick 	register struct vnode *uvp = un->un_uppervp;
114365935Spendry 
114467369Smckusick 	if (uvp == NULLVP)
114567369Smckusick 		return (0);
114665935Spendry 
114767369Smckusick 	FIXUP(un);
114867369Smckusick 	ap->a_vp = uvp;
114967369Smckusick 	return (VOCALL(uvp->v_op, VOFFSET(vop_readdir), ap));
115065935Spendry }
115165935Spendry 
115265935Spendry int
115365963Spendry union_readlink(ap)
115465963Spendry 	struct vop_readlink_args /* {
115565963Spendry 		struct vnode *a_vp;
115665963Spendry 		struct uio *a_uio;
115765963Spendry 		struct ucred *a_cred;
115865963Spendry 	} */ *ap;
115965963Spendry {
116065963Spendry 	int error;
116165963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
116266051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
116365963Spendry 
116466051Spendry 	if (dolock)
116566051Spendry 		VOP_LOCK(vp);
116666152Spendry 	else
116766152Spendry 		FIXUP(VTOUNION(ap->a_vp));
116865963Spendry 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
116966051Spendry 	if (dolock)
117066051Spendry 		VOP_UNLOCK(vp);
117165963Spendry 
117265963Spendry 	return (error);
117365963Spendry }
117465963Spendry 
117565963Spendry int
117665963Spendry union_abortop(ap)
117765963Spendry 	struct vop_abortop_args /* {
117865963Spendry 		struct vnode *a_dvp;
117965963Spendry 		struct componentname *a_cnp;
118065963Spendry 	} */ *ap;
118165963Spendry {
118265963Spendry 	int error;
118365965Spendry 	struct vnode *vp = OTHERVP(ap->a_dvp);
118465963Spendry 	struct union_node *un = VTOUNION(ap->a_dvp);
118565963Spendry 	int islocked = un->un_flags & UN_LOCKED;
118666051Spendry 	int dolock = (vp == LOWERVP(ap->a_dvp));
118765963Spendry 
118866152Spendry 	if (islocked) {
118966152Spendry 		if (dolock)
119066152Spendry 			VOP_LOCK(vp);
119166152Spendry 		else
119266152Spendry 			FIXUP(VTOUNION(ap->a_dvp));
119366152Spendry 	}
119465963Spendry 	error = VOP_ABORTOP(vp, ap->a_cnp);
119566051Spendry 	if (islocked && dolock)
119665963Spendry 		VOP_UNLOCK(vp);
119765963Spendry 
119865963Spendry 	return (error);
119965963Spendry }
120065963Spendry 
120165963Spendry int
120265935Spendry union_inactive(ap)
120365935Spendry 	struct vop_inactive_args /* {
120465935Spendry 		struct vnode *a_vp;
120565935Spendry 	} */ *ap;
120665935Spendry {
120767073Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
120865935Spendry 
120965935Spendry 	/*
121065935Spendry 	 * Do nothing (and _don't_ bypass).
121165935Spendry 	 * Wait to vrele lowervp until reclaim,
121265935Spendry 	 * so that until then our union_node is in the
121365935Spendry 	 * cache and reusable.
121465935Spendry 	 *
121565935Spendry 	 * NEEDSWORK: Someday, consider inactive'ing
121665935Spendry 	 * the lowervp and then trying to reactivate it
121765935Spendry 	 * with capabilities (v_id)
121865935Spendry 	 * like they do in the name lookup cache code.
121965935Spendry 	 * That's too much work for now.
122065935Spendry 	 */
122165989Spendry 
122266027Spendry #ifdef UNION_DIAGNOSTIC
122365989Spendry 	if (un->un_flags & UN_LOCKED)
122465989Spendry 		panic("union: inactivating locked node");
122567073Spendry 	if (un->un_flags & UN_ULOCK)
122667073Spendry 		panic("union: inactivating w/locked upper node");
122765989Spendry #endif
122865989Spendry 
122967073Spendry 	if ((un->un_flags & UN_CACHED) == 0)
123067073Spendry 		vgone(ap->a_vp);
123167073Spendry 
123265935Spendry 	return (0);
123365935Spendry }
123465935Spendry 
123565935Spendry int
123665935Spendry union_reclaim(ap)
123765935Spendry 	struct vop_reclaim_args /* {
123865935Spendry 		struct vnode *a_vp;
123965935Spendry 	} */ *ap;
124065935Spendry {
124165935Spendry 
124266053Spendry 	union_freevp(ap->a_vp);
124366053Spendry 
124465935Spendry 	return (0);
124565935Spendry }
124665935Spendry 
124765963Spendry int
124865963Spendry union_lock(ap)
124965963Spendry 	struct vop_lock_args *ap;
125065963Spendry {
125166149Spendry 	struct vnode *vp = ap->a_vp;
125266149Spendry 	struct union_node *un;
125365935Spendry 
125466149Spendry start:
125566149Spendry 	while (vp->v_flag & VXLOCK) {
125666149Spendry 		vp->v_flag |= VXWANT;
125766149Spendry 		sleep((caddr_t)vp, PINOD);
125866149Spendry 	}
125966149Spendry 
126066149Spendry 	un = VTOUNION(vp);
126166149Spendry 
126267076Spendry 	if (un->un_uppervp != NULLVP) {
126367230Spendry 		if (((un->un_flags & UN_ULOCK) == 0) &&
126467230Spendry 		    (vp->v_usecount != 0)) {
126566149Spendry 			un->un_flags |= UN_ULOCK;
126666051Spendry 			VOP_LOCK(un->un_uppervp);
126766051Spendry 		}
126866051Spendry #ifdef DIAGNOSTIC
126966051Spendry 		if (un->un_flags & UN_KLOCK)
127066051Spendry 			panic("union: dangling upper lock");
127166051Spendry #endif
127266051Spendry 	}
127366051Spendry 
127466149Spendry 	if (un->un_flags & UN_LOCKED) {
127565963Spendry #ifdef DIAGNOSTIC
127665989Spendry 		if (curproc && un->un_pid == curproc->p_pid &&
127765989Spendry 			    un->un_pid > -1 && curproc->p_pid > -1)
127865989Spendry 			panic("union: locking against myself");
127965963Spendry #endif
128065963Spendry 		un->un_flags |= UN_WANT;
128165963Spendry 		sleep((caddr_t) &un->un_flags, PINOD);
128266149Spendry 		goto start;
128365963Spendry 	}
128465989Spendry 
128565963Spendry #ifdef DIAGNOSTIC
128665989Spendry 	if (curproc)
128765989Spendry 		un->un_pid = curproc->p_pid;
128865989Spendry 	else
128965989Spendry 		un->un_pid = -1;
129065963Spendry #endif
129166028Spendry 
129266149Spendry 	un->un_flags |= UN_LOCKED;
129366028Spendry 	return (0);
129465963Spendry }
129565963Spendry 
129665935Spendry int
129765963Spendry union_unlock(ap)
129865963Spendry 	struct vop_lock_args *ap;
129965963Spendry {
130065963Spendry 	struct union_node *un = VTOUNION(ap->a_vp);
130165963Spendry 
130265963Spendry #ifdef DIAGNOSTIC
130365965Spendry 	if ((un->un_flags & UN_LOCKED) == 0)
130465965Spendry 		panic("union: unlock unlocked node");
130565989Spendry 	if (curproc && un->un_pid != curproc->p_pid &&
130665989Spendry 			curproc->p_pid > -1 && un->un_pid > -1)
130765963Spendry 		panic("union: unlocking other process's union node");
130865963Spendry #endif
130965963Spendry 
131065963Spendry 	un->un_flags &= ~UN_LOCKED;
131166051Spendry 
131266051Spendry 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
131366051Spendry 		VOP_UNLOCK(un->un_uppervp);
131466051Spendry 
131566051Spendry 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
131666051Spendry 
131765963Spendry 	if (un->un_flags & UN_WANT) {
131865963Spendry 		un->un_flags &= ~UN_WANT;
131965963Spendry 		wakeup((caddr_t) &un->un_flags);
132065963Spendry 	}
132165963Spendry 
132265963Spendry #ifdef DIAGNOSTIC
132365963Spendry 	un->un_pid = 0;
132465963Spendry #endif
132566028Spendry 
132666028Spendry 	return (0);
132765963Spendry }
132865963Spendry 
132965963Spendry int
133065963Spendry union_bmap(ap)
133165963Spendry 	struct vop_bmap_args /* {
133265963Spendry 		struct vnode *a_vp;
133365963Spendry 		daddr_t  a_bn;
133465963Spendry 		struct vnode **a_vpp;
133565963Spendry 		daddr_t *a_bnp;
133665963Spendry 		int *a_runp;
133765963Spendry 	} */ *ap;
133865963Spendry {
133965963Spendry 	int error;
134065963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
134166051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
134265963Spendry 
134366051Spendry 	if (dolock)
134466051Spendry 		VOP_LOCK(vp);
134566152Spendry 	else
134666152Spendry 		FIXUP(VTOUNION(ap->a_vp));
134765963Spendry 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
134866051Spendry 	if (dolock)
134966051Spendry 		VOP_UNLOCK(vp);
135065963Spendry 
135165963Spendry 	return (error);
135265963Spendry }
135365963Spendry 
135465963Spendry int
135565935Spendry union_print(ap)
135665935Spendry 	struct vop_print_args /* {
135765935Spendry 		struct vnode *a_vp;
135865935Spendry 	} */ *ap;
135965935Spendry {
136065935Spendry 	struct vnode *vp = ap->a_vp;
136165935Spendry 
136265935Spendry 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
136365935Spendry 			vp, UPPERVP(vp), LOWERVP(vp));
136465935Spendry 	return (0);
136565935Spendry }
136665935Spendry 
136765963Spendry int
136865963Spendry union_islocked(ap)
136965963Spendry 	struct vop_islocked_args /* {
137065963Spendry 		struct vnode *a_vp;
137165963Spendry 	} */ *ap;
137265963Spendry {
137365935Spendry 
137465963Spendry 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
137565963Spendry }
137665963Spendry 
137765935Spendry int
137865963Spendry union_pathconf(ap)
137965963Spendry 	struct vop_pathconf_args /* {
138065963Spendry 		struct vnode *a_vp;
138165963Spendry 		int a_name;
138265963Spendry 		int *a_retval;
138365935Spendry 	} */ *ap;
138465935Spendry {
138565935Spendry 	int error;
138665963Spendry 	struct vnode *vp = OTHERVP(ap->a_vp);
138766051Spendry 	int dolock = (vp == LOWERVP(ap->a_vp));
138865935Spendry 
138966051Spendry 	if (dolock)
139066051Spendry 		VOP_LOCK(vp);
139166152Spendry 	else
139266152Spendry 		FIXUP(VTOUNION(ap->a_vp));
139365963Spendry 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
139466051Spendry 	if (dolock)
139566051Spendry 		VOP_UNLOCK(vp);
139665935Spendry 
139765963Spendry 	return (error);
139865963Spendry }
139965935Spendry 
140065963Spendry int
140165963Spendry union_advlock(ap)
140265963Spendry 	struct vop_advlock_args /* {
140365963Spendry 		struct vnode *a_vp;
140465963Spendry 		caddr_t  a_id;
140565963Spendry 		int  a_op;
140665963Spendry 		struct flock *a_fl;
140765963Spendry 		int  a_flags;
140865963Spendry 	} */ *ap;
140965963Spendry {
141065935Spendry 
141165963Spendry 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
141265963Spendry 				ap->a_fl, ap->a_flags));
141365935Spendry }
141465935Spendry 
141565935Spendry 
141665935Spendry /*
141765963Spendry  * XXX - vop_strategy must be hand coded because it has no
141865935Spendry  * vnode in its arguments.
141965935Spendry  * This goes away with a merged VM/buffer cache.
142065935Spendry  */
142165935Spendry int
142265963Spendry union_strategy(ap)
142365963Spendry 	struct vop_strategy_args /* {
142465935Spendry 		struct buf *a_bp;
142565935Spendry 	} */ *ap;
142665935Spendry {
142765935Spendry 	struct buf *bp = ap->a_bp;
142865935Spendry 	int error;
142965935Spendry 	struct vnode *savedvp;
143065935Spendry 
143165935Spendry 	savedvp = bp->b_vp;
143265963Spendry 	bp->b_vp = OTHERVP(bp->b_vp);
143365935Spendry 
143465935Spendry #ifdef DIAGNOSTIC
143565997Spendry 	if (bp->b_vp == NULLVP)
143665963Spendry 		panic("union_strategy: nil vp");
143765963Spendry 	if (((bp->b_flags & B_READ) == 0) &&
143865963Spendry 	    (bp->b_vp == LOWERVP(savedvp)))
143965963Spendry 		panic("union_strategy: writing to lowervp");
144065935Spendry #endif
144165935Spendry 
144265963Spendry 	error = VOP_STRATEGY(bp);
144365935Spendry 	bp->b_vp = savedvp;
144465935Spendry 
144565935Spendry 	return (error);
144665935Spendry }
144765935Spendry 
144865935Spendry /*
144965935Spendry  * Global vfs data structures
145065935Spendry  */
145165935Spendry int (**union_vnodeop_p)();
145265965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = {
145365963Spendry 	{ &vop_default_desc, vn_default_error },
145465963Spendry 	{ &vop_lookup_desc, union_lookup },		/* lookup */
145565963Spendry 	{ &vop_create_desc, union_create },		/* create */
145665963Spendry 	{ &vop_mknod_desc, union_mknod },		/* mknod */
145765963Spendry 	{ &vop_open_desc, union_open },			/* open */
145865963Spendry 	{ &vop_close_desc, union_close },		/* close */
145965963Spendry 	{ &vop_access_desc, union_access },		/* access */
146065963Spendry 	{ &vop_getattr_desc, union_getattr },		/* getattr */
146165963Spendry 	{ &vop_setattr_desc, union_setattr },		/* setattr */
146265963Spendry 	{ &vop_read_desc, union_read },			/* read */
146365963Spendry 	{ &vop_write_desc, union_write },		/* write */
146465963Spendry 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
146565963Spendry 	{ &vop_select_desc, union_select },		/* select */
146665963Spendry 	{ &vop_mmap_desc, union_mmap },			/* mmap */
146765963Spendry 	{ &vop_fsync_desc, union_fsync },		/* fsync */
146865963Spendry 	{ &vop_seek_desc, union_seek },			/* seek */
146965963Spendry 	{ &vop_remove_desc, union_remove },		/* remove */
147065963Spendry 	{ &vop_link_desc, union_link },			/* link */
147165963Spendry 	{ &vop_rename_desc, union_rename },		/* rename */
147265963Spendry 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
147365963Spendry 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
147465963Spendry 	{ &vop_symlink_desc, union_symlink },		/* symlink */
147565963Spendry 	{ &vop_readdir_desc, union_readdir },		/* readdir */
147665963Spendry 	{ &vop_readlink_desc, union_readlink },		/* readlink */
147765963Spendry 	{ &vop_abortop_desc, union_abortop },		/* abortop */
147865963Spendry 	{ &vop_inactive_desc, union_inactive },		/* inactive */
147965963Spendry 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
148065963Spendry 	{ &vop_lock_desc, union_lock },			/* lock */
148165963Spendry 	{ &vop_unlock_desc, union_unlock },		/* unlock */
148265963Spendry 	{ &vop_bmap_desc, union_bmap },			/* bmap */
148365963Spendry 	{ &vop_strategy_desc, union_strategy },		/* strategy */
148465963Spendry 	{ &vop_print_desc, union_print },		/* print */
148565963Spendry 	{ &vop_islocked_desc, union_islocked },		/* islocked */
148665963Spendry 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
148765963Spendry 	{ &vop_advlock_desc, union_advlock },		/* advlock */
148865963Spendry #ifdef notdef
148965963Spendry 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
149065963Spendry 	{ &vop_valloc_desc, union_valloc },		/* valloc */
149165963Spendry 	{ &vop_vfree_desc, union_vfree },		/* vfree */
149265963Spendry 	{ &vop_truncate_desc, union_truncate },		/* truncate */
149365963Spendry 	{ &vop_update_desc, union_update },		/* update */
149465963Spendry 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
149565963Spendry #endif
149665935Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
149765935Spendry };
149865935Spendry struct vnodeopv_desc union_vnodeop_opv_desc =
149965935Spendry 	{ &union_vnodeop_p, union_vnodeop_entries };
1500