xref: /freebsd/sys/fs/unionfs/union_vnops.c (revision cefb5754dda2cc46efd2d00ec3373b7eca7a0c76)
1df8bae1dSRodney W. Grimes /*
2996c772fSJohn Dyson  * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
3996c772fSJohn Dyson  * Copyright (c) 1992, 1993, 1994, 1995
4996c772fSJohn Dyson  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37996c772fSJohn Dyson  *	@(#)union_vnops.c	8.32 (Berkeley) 6/23/95
38c3aac50fSPeter Wemm  * $FreeBSD$
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
433ac4d1efSBruce Evans #include <sys/fcntl.h>
44996c772fSJohn Dyson #include <sys/stat.h>
459e67ea79SBruce Evans #include <sys/kernel.h>
46df8bae1dSRodney W. Grimes #include <sys/vnode.h>
47df8bae1dSRodney W. Grimes #include <sys/mount.h>
48df8bae1dSRodney W. Grimes #include <sys/namei.h>
49df8bae1dSRodney W. Grimes #include <sys/malloc.h>
509626b608SPoul-Henning Kamp #include <sys/bio.h>
51df8bae1dSRodney W. Grimes #include <sys/buf.h>
52996c772fSJohn Dyson #include <sys/lock.h>
532a31267eSMatthew Dillon #include <sys/sysctl.h>
5499d300a1SRuslan Ermilov #include <fs/unionfs/union.h>
55df8bae1dSRodney W. Grimes 
562a31267eSMatthew Dillon #include <vm/vm.h>
572a31267eSMatthew Dillon #include <vm/vnode_pager.h>
582a31267eSMatthew Dillon 
592a31267eSMatthew Dillon #include <vm/vm_page.h>
602a31267eSMatthew Dillon #include <vm/vm_object.h>
612a31267eSMatthew Dillon 
622a31267eSMatthew Dillon int uniondebug = 0;
632a31267eSMatthew Dillon 
642a31267eSMatthew Dillon #if UDEBUG_ENABLED
652a31267eSMatthew Dillon SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, "");
662a31267eSMatthew Dillon #else
672a31267eSMatthew Dillon SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, "");
682a31267eSMatthew Dillon #endif
69df8bae1dSRodney W. Grimes 
7011caded3SAlfred Perlstein static int	union_access(struct vop_access_args *ap);
7111caded3SAlfred Perlstein static int	union_advlock(struct vop_advlock_args *ap);
7211caded3SAlfred Perlstein static int	union_close(struct vop_close_args *ap);
7311caded3SAlfred Perlstein static int	union_create(struct vop_create_args *ap);
7411caded3SAlfred Perlstein static int	union_createvobject(struct vop_createvobject_args *ap);
7511caded3SAlfred Perlstein static int	union_destroyvobject(struct vop_destroyvobject_args *ap);
7611caded3SAlfred Perlstein static int	union_fsync(struct vop_fsync_args *ap);
7711caded3SAlfred Perlstein static int	union_getattr(struct vop_getattr_args *ap);
7811caded3SAlfred Perlstein static int	union_getvobject(struct vop_getvobject_args *ap);
7911caded3SAlfred Perlstein static int	union_inactive(struct vop_inactive_args *ap);
8011caded3SAlfred Perlstein static int	union_ioctl(struct vop_ioctl_args *ap);
8111caded3SAlfred Perlstein static int	union_lease(struct vop_lease_args *ap);
8211caded3SAlfred Perlstein static int	union_link(struct vop_link_args *ap);
8311caded3SAlfred Perlstein static int	union_lookup(struct vop_lookup_args *ap);
8411caded3SAlfred Perlstein static int	union_lookup1(struct vnode *udvp, struct vnode **dvp,
859b5e8b3aSBruce Evans 				   struct vnode **vpp,
8611caded3SAlfred Perlstein 				   struct componentname *cnp);
8711caded3SAlfred Perlstein static int	union_mkdir(struct vop_mkdir_args *ap);
8811caded3SAlfred Perlstein static int	union_mknod(struct vop_mknod_args *ap);
8911caded3SAlfred Perlstein static int	union_open(struct vop_open_args *ap);
9011caded3SAlfred Perlstein static int	union_pathconf(struct vop_pathconf_args *ap);
9111caded3SAlfred Perlstein static int	union_print(struct vop_print_args *ap);
9211caded3SAlfred Perlstein static int	union_read(struct vop_read_args *ap);
9311caded3SAlfred Perlstein static int	union_readdir(struct vop_readdir_args *ap);
9411caded3SAlfred Perlstein static int	union_readlink(struct vop_readlink_args *ap);
9511caded3SAlfred Perlstein static int	union_getwritemount(struct vop_getwritemount_args *ap);
9611caded3SAlfred Perlstein static int	union_reclaim(struct vop_reclaim_args *ap);
9711caded3SAlfred Perlstein static int	union_remove(struct vop_remove_args *ap);
9811caded3SAlfred Perlstein static int	union_rename(struct vop_rename_args *ap);
9911caded3SAlfred Perlstein static int	union_revoke(struct vop_revoke_args *ap);
10011caded3SAlfred Perlstein static int	union_rmdir(struct vop_rmdir_args *ap);
10111caded3SAlfred Perlstein static int	union_poll(struct vop_poll_args *ap);
10211caded3SAlfred Perlstein static int	union_setattr(struct vop_setattr_args *ap);
10311caded3SAlfred Perlstein static int	union_strategy(struct vop_strategy_args *ap);
10411caded3SAlfred Perlstein static int	union_symlink(struct vop_symlink_args *ap);
10511caded3SAlfred Perlstein static int	union_whiteout(struct vop_whiteout_args *ap);
10611caded3SAlfred Perlstein static int	union_write(struct vop_read_args *ap);
1079b5e8b3aSBruce Evans 
1082a31267eSMatthew Dillon static __inline
1092a31267eSMatthew Dillon struct vnode *
110b40ce416SJulian Elischer union_lock_upper(struct union_node *un, struct thread *td)
111df8bae1dSRodney W. Grimes {
1122a31267eSMatthew Dillon 	struct vnode *uppervp;
113df8bae1dSRodney W. Grimes 
1142a31267eSMatthew Dillon 	if ((uppervp = un->un_uppervp) != NULL) {
1152a31267eSMatthew Dillon 		VREF(uppervp);
116b40ce416SJulian Elischer 		vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
1172a31267eSMatthew Dillon 	}
1184d93c0beSJeff Roberson 	KASSERT((uppervp == NULL || vrefcnt(uppervp) > 0), ("uppervp usecount is 0"));
1192a31267eSMatthew Dillon 	return(uppervp);
120df8bae1dSRodney W. Grimes }
121df8bae1dSRodney W. Grimes 
1222a31267eSMatthew Dillon static __inline
1232a31267eSMatthew Dillon void
124b40ce416SJulian Elischer union_unlock_upper(struct vnode *uppervp, struct thread *td)
1252a31267eSMatthew Dillon {
1262a31267eSMatthew Dillon 	vput(uppervp);
1272a31267eSMatthew Dillon }
1282a31267eSMatthew Dillon 
1292a31267eSMatthew Dillon static __inline
1302a31267eSMatthew Dillon struct vnode *
131b40ce416SJulian Elischer union_lock_other(struct union_node *un, struct thread *td)
1322a31267eSMatthew Dillon {
1332a31267eSMatthew Dillon 	struct vnode *vp;
1342a31267eSMatthew Dillon 
1352a31267eSMatthew Dillon 	if (un->un_uppervp != NULL) {
136b40ce416SJulian Elischer 		vp = union_lock_upper(un, td);
1372a31267eSMatthew Dillon 	} else if ((vp = un->un_lowervp) != NULL) {
1382a31267eSMatthew Dillon 		VREF(vp);
139b40ce416SJulian Elischer 		vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
1402a31267eSMatthew Dillon 	}
1412a31267eSMatthew Dillon 	return(vp);
1422a31267eSMatthew Dillon }
1432a31267eSMatthew Dillon 
1442a31267eSMatthew Dillon static __inline
1452a31267eSMatthew Dillon void
146b40ce416SJulian Elischer union_unlock_other(struct vnode *vp, struct thread *td)
1472a31267eSMatthew Dillon {
1482a31267eSMatthew Dillon 	vput(vp);
1492a31267eSMatthew Dillon }
1502a31267eSMatthew Dillon 
1512a31267eSMatthew Dillon /*
1522a31267eSMatthew Dillon  *	union_lookup:
1532a31267eSMatthew Dillon  *
1542a31267eSMatthew Dillon  *	udvp	must be exclusively locked on call and will remain
1552a31267eSMatthew Dillon  *		exclusively locked on return.  This is the mount point
1561c4ccf09SDon Lewis  *		for our filesystem.
1572a31267eSMatthew Dillon  *
1582a31267eSMatthew Dillon  *	dvp	Our base directory, locked and referenced.
1592a31267eSMatthew Dillon  *		The passed dvp will be dereferenced and unlocked on return
1602a31267eSMatthew Dillon  *		and a new dvp will be returned which is locked and
1612a31267eSMatthew Dillon  *		referenced in the same variable.
1622a31267eSMatthew Dillon  *
1632a31267eSMatthew Dillon  *	vpp	is filled in with the result if no error occured,
1642a31267eSMatthew Dillon  *		locked and ref'd.
1652a31267eSMatthew Dillon  *
1662a31267eSMatthew Dillon  *		If an error is returned, *vpp is set to NULLVP.  If no
1672a31267eSMatthew Dillon  *		error occurs, *vpp is returned with a reference and an
1682a31267eSMatthew Dillon  *		exclusive lock.
1692a31267eSMatthew Dillon  */
1702a31267eSMatthew Dillon 
171df8bae1dSRodney W. Grimes static int
1722a31267eSMatthew Dillon union_lookup1(udvp, pdvp, vpp, cnp)
173df8bae1dSRodney W. Grimes 	struct vnode *udvp;
1742a31267eSMatthew Dillon 	struct vnode **pdvp;
175df8bae1dSRodney W. Grimes 	struct vnode **vpp;
176df8bae1dSRodney W. Grimes 	struct componentname *cnp;
177df8bae1dSRodney W. Grimes {
178df8bae1dSRodney W. Grimes 	int error;
179b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
1802a31267eSMatthew Dillon 	struct vnode *dvp = *pdvp;
181df8bae1dSRodney W. Grimes 	struct vnode *tdvp;
182df8bae1dSRodney W. Grimes 	struct mount *mp;
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes 	/*
185df8bae1dSRodney W. Grimes 	 * If stepping up the directory tree, check for going
186df8bae1dSRodney W. Grimes 	 * back across the mount point, in which case do what
187df8bae1dSRodney W. Grimes 	 * lookup would do by stepping back down the mount
188df8bae1dSRodney W. Grimes 	 * hierarchy.
189df8bae1dSRodney W. Grimes 	 */
190df8bae1dSRodney W. Grimes 	if (cnp->cn_flags & ISDOTDOT) {
191e6e370a7SJeff Roberson 		while ((dvp != udvp) && (dvp->v_vflag & VV_ROOT)) {
192df8bae1dSRodney W. Grimes 			/*
193df8bae1dSRodney W. Grimes 			 * Don't do the NOCROSSMOUNT check
194df8bae1dSRodney W. Grimes 			 * at this level.  By definition,
195df8bae1dSRodney W. Grimes 			 * union fs deals with namespaces, not
196df8bae1dSRodney W. Grimes 			 * filesystems.
197df8bae1dSRodney W. Grimes 			 */
198df8bae1dSRodney W. Grimes 			tdvp = dvp;
1992a31267eSMatthew Dillon 			dvp = dvp->v_mount->mnt_vnodecovered;
200df8bae1dSRodney W. Grimes 			VREF(dvp);
2012a31267eSMatthew Dillon 			vput(tdvp);
202b40ce416SJulian Elischer 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
203df8bae1dSRodney W. Grimes 		}
204df8bae1dSRodney W. Grimes 	}
205df8bae1dSRodney W. Grimes 
2062a31267eSMatthew Dillon 	/*
2072a31267eSMatthew Dillon 	 * Set return dvp to be the upperdvp 'parent directory.
2082a31267eSMatthew Dillon 	 */
2092a31267eSMatthew Dillon 	*pdvp = dvp;
2102a31267eSMatthew Dillon 
2112a31267eSMatthew Dillon 	/*
2121c4ccf09SDon Lewis 	 * If the VOP_LOOKUP() call generates an error, tdvp is invalid and
2131c4ccf09SDon Lewis 	 * no changes will have been made to dvp, so we are set to return.
2142a31267eSMatthew Dillon 	 */
2152a31267eSMatthew Dillon 
216df8bae1dSRodney W. Grimes         error = VOP_LOOKUP(dvp, &tdvp, cnp);
2172a31267eSMatthew Dillon 	if (error) {
2182a31267eSMatthew Dillon 		UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags));
2192a31267eSMatthew Dillon 		*vpp = NULL;
220df8bae1dSRodney W. Grimes 		return (error);
2212a31267eSMatthew Dillon 	}
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes 	/*
224df8bae1dSRodney W. Grimes 	 * The parent directory will have been unlocked, unless lookup
2252a31267eSMatthew Dillon 	 * found the last component or if dvp == tdvp (tdvp must be locked).
2262a31267eSMatthew Dillon 	 *
2272a31267eSMatthew Dillon 	 * We want our dvp to remain locked and ref'd.  We also want tdvp
2282a31267eSMatthew Dillon 	 * to remain locked and ref'd.
229df8bae1dSRodney W. Grimes 	 */
2302a31267eSMatthew Dillon 	UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags));
231df8bae1dSRodney W. Grimes 
2322a31267eSMatthew Dillon 	if (dvp != tdvp && (cnp->cn_flags & ISLASTCN) == 0)
233b40ce416SJulian Elischer 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
234df8bae1dSRodney W. Grimes 
235df8bae1dSRodney W. Grimes 	/*
236df8bae1dSRodney W. Grimes 	 * Lastly check if the current node is a mount point in
237df8bae1dSRodney W. Grimes 	 * which case walk up the mount hierarchy making sure not to
238df8bae1dSRodney W. Grimes 	 * bump into the root of the mount tree (ie. dvp != udvp).
2392a31267eSMatthew Dillon 	 *
2402a31267eSMatthew Dillon 	 * We use dvp as a temporary variable here, it is no longer related
2412a31267eSMatthew Dillon 	 * to the dvp above.  However, we have to ensure that both *pdvp and
2422a31267eSMatthew Dillon 	 * tdvp are locked on return.
243df8bae1dSRodney W. Grimes 	 */
2442a31267eSMatthew Dillon 
2452a31267eSMatthew Dillon 	dvp = tdvp;
2462a31267eSMatthew Dillon 	while (
2472a31267eSMatthew Dillon 	    dvp != udvp &&
2482a31267eSMatthew Dillon 	    (dvp->v_type == VDIR) &&
2492a31267eSMatthew Dillon 	    (mp = dvp->v_mountedhere)
2502a31267eSMatthew Dillon 	) {
2512a31267eSMatthew Dillon 		int relock_pdvp = 0;
252df8bae1dSRodney W. Grimes 
253b40ce416SJulian Elischer 		if (vfs_busy(mp, 0, 0, td))
254df8bae1dSRodney W. Grimes 			continue;
255df8bae1dSRodney W. Grimes 
2562a31267eSMatthew Dillon 		if (dvp == *pdvp)
2572a31267eSMatthew Dillon 			relock_pdvp = 1;
258df8bae1dSRodney W. Grimes 		vput(dvp);
2592a31267eSMatthew Dillon 		dvp = NULL;
2602a31267eSMatthew Dillon 		error = VFS_ROOT(mp, &dvp);
2612a31267eSMatthew Dillon 
262b40ce416SJulian Elischer 		vfs_unbusy(mp, td);
2632a31267eSMatthew Dillon 
2642a31267eSMatthew Dillon 		if (relock_pdvp)
265b40ce416SJulian Elischer 			vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, td);
2662a31267eSMatthew Dillon 
2672a31267eSMatthew Dillon 		if (error) {
2682a31267eSMatthew Dillon 			*vpp = NULL;
269df8bae1dSRodney W. Grimes 			return (error);
270df8bae1dSRodney W. Grimes 		}
271df8bae1dSRodney W. Grimes 	}
272df8bae1dSRodney W. Grimes 	*vpp = dvp;
273df8bae1dSRodney W. Grimes 	return (0);
274df8bae1dSRodney W. Grimes }
275df8bae1dSRodney W. Grimes 
276c9bf0111SKATO Takenori static int
277df8bae1dSRodney W. Grimes union_lookup(ap)
278df8bae1dSRodney W. Grimes 	struct vop_lookup_args /* {
279df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
280df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
281df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
282df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
283df8bae1dSRodney W. Grimes 	} */ *ap;
284df8bae1dSRodney W. Grimes {
285df8bae1dSRodney W. Grimes 	int error;
286df8bae1dSRodney W. Grimes 	int uerror, lerror;
287df8bae1dSRodney W. Grimes 	struct vnode *uppervp, *lowervp;
288df8bae1dSRodney W. Grimes 	struct vnode *upperdvp, *lowerdvp;
2892a31267eSMatthew Dillon 	struct vnode *dvp = ap->a_dvp;		/* starting dir */
2902a31267eSMatthew Dillon 	struct union_node *dun = VTOUNION(dvp);	/* associated union node */
291df8bae1dSRodney W. Grimes 	struct componentname *cnp = ap->a_cnp;
292b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
293df8bae1dSRodney W. Grimes 	int lockparent = cnp->cn_flags & LOCKPARENT;
294df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
29527ed09c2SMatthew Dillon 	struct ucred *saved_cred = NULL;
296996c772fSJohn Dyson 	int iswhiteout;
297996c772fSJohn Dyson 	struct vattr va;
298996c772fSJohn Dyson 
2992a31267eSMatthew Dillon 	*ap->a_vpp = NULLVP;
3006ca02614SKATO Takenori 
3016ca02614SKATO Takenori 	/*
3021c4ccf09SDon Lewis 	 * Disallow write attempts to the filesystem mounted read-only.
3036ca02614SKATO Takenori 	 */
3042a31267eSMatthew Dillon 	if ((cnp->cn_flags & ISLASTCN) &&
3052a31267eSMatthew Dillon 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3062a31267eSMatthew Dillon 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
3076ca02614SKATO Takenori 		return (EROFS);
308996c772fSJohn Dyson 	}
309df8bae1dSRodney W. Grimes 
3102a31267eSMatthew Dillon 	/*
3111c4ccf09SDon Lewis 	 * For any lookups we do, always return with the parent locked.
3122a31267eSMatthew Dillon 	 */
313df8bae1dSRodney W. Grimes 	cnp->cn_flags |= LOCKPARENT;
314df8bae1dSRodney W. Grimes 
315df8bae1dSRodney W. Grimes 	lowerdvp = dun->un_lowervp;
316df8bae1dSRodney W. Grimes 	uppervp = NULLVP;
317df8bae1dSRodney W. Grimes 	lowervp = NULLVP;
318996c772fSJohn Dyson 	iswhiteout = 0;
319df8bae1dSRodney W. Grimes 
3202a31267eSMatthew Dillon 	uerror = ENOENT;
3212a31267eSMatthew Dillon 	lerror = ENOENT;
3222a31267eSMatthew Dillon 
3232a31267eSMatthew Dillon 	/*
3242a31267eSMatthew Dillon 	 * Get a private lock on uppervp and a reference, effectively
3252a31267eSMatthew Dillon 	 * taking it out of the union_node's control.
3262a31267eSMatthew Dillon 	 *
3272a31267eSMatthew Dillon 	 * We must lock upperdvp while holding our lock on dvp
3282a31267eSMatthew Dillon 	 * to avoid a deadlock.
3292a31267eSMatthew Dillon 	 */
330b40ce416SJulian Elischer 	upperdvp = union_lock_upper(dun, td);
331afc2a558SKATO Takenori 
332df8bae1dSRodney W. Grimes 	/*
3331c4ccf09SDon Lewis 	 * Do the lookup in the upper level.
3341c4ccf09SDon Lewis 	 * If that level consumes additional pathnames,
335df8bae1dSRodney W. Grimes 	 * then assume that something special is going
336df8bae1dSRodney W. Grimes 	 * on and just return that vnode.
337df8bae1dSRodney W. Grimes 	 */
338996c772fSJohn Dyson 	if (upperdvp != NULLVP) {
339ee582cdfSKATO Takenori 		/*
3402a31267eSMatthew Dillon 		 * We do not have to worry about the DOTDOT case, we've
3412a31267eSMatthew Dillon 		 * already unlocked dvp.
342ee582cdfSKATO Takenori 		 */
3432a31267eSMatthew Dillon 		UDEBUG(("A %p\n", upperdvp));
3442a31267eSMatthew Dillon 
3452a31267eSMatthew Dillon 		/*
3462a31267eSMatthew Dillon 		 * Do the lookup.   We must supply a locked and referenced
3472a31267eSMatthew Dillon 		 * upperdvp to the function and will get a new locked and
3481c4ccf09SDon Lewis 		 * referenced upperdvp back, with the old having been
3492a31267eSMatthew Dillon 		 * dereferenced.
3502a31267eSMatthew Dillon 		 *
3512a31267eSMatthew Dillon 		 * If an error is returned, uppervp will be NULLVP.  If no
3521c4ccf09SDon Lewis 		 * error occurs, uppervp will be the locked and referenced.
3531c4ccf09SDon Lewis 		 * Return vnode, or possibly NULL, depending on what is being
3542a31267eSMatthew Dillon 		 * requested.  It is possible that the returned uppervp
3552a31267eSMatthew Dillon 		 * will be the same as upperdvp.
3562a31267eSMatthew Dillon 		 */
3572a31267eSMatthew Dillon 		uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
3582a31267eSMatthew Dillon 		UDEBUG((
3592a31267eSMatthew Dillon 		    "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
3602a31267eSMatthew Dillon 		    uerror,
3612a31267eSMatthew Dillon 		    upperdvp,
3624d93c0beSJeff Roberson 		    vrefcnt(upperdvp),
3636bdfe06aSEivind Eklund 		    VOP_ISLOCKED(upperdvp, NULL),
3642a31267eSMatthew Dillon 		    uppervp,
3654d93c0beSJeff Roberson 		    (uppervp ? vrefcnt(uppervp) : -99),
3666bdfe06aSEivind Eklund 		    (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99)
3672a31267eSMatthew Dillon 		));
3682a31267eSMatthew Dillon 
3696ca02614SKATO Takenori 		/*
3701c4ccf09SDon Lewis 		 * Disallow write attempts to the filesystem mounted read-only.
3716ca02614SKATO Takenori 		 */
3726ca02614SKATO Takenori 		if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) &&
3736ca02614SKATO Takenori 		    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3746ca02614SKATO Takenori 		    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {
3752a31267eSMatthew Dillon 			error = EROFS;
376afc2a558SKATO Takenori 			goto out;
377df8bae1dSRodney W. Grimes 		}
3782a31267eSMatthew Dillon 
3792a31267eSMatthew Dillon 		/*
3801c4ccf09SDon Lewis 		 * Special case: If cn_consume != 0 then skip out.  The result
3812a31267eSMatthew Dillon 		 * of the lookup is transfered to our return variable.  If
3822a31267eSMatthew Dillon 		 * an error occured we have to throw away the results.
3832a31267eSMatthew Dillon 		 */
3842a31267eSMatthew Dillon 
3852a31267eSMatthew Dillon 		if (cnp->cn_consume != 0) {
3862a31267eSMatthew Dillon 			if ((error = uerror) == 0) {
3872a31267eSMatthew Dillon 				*ap->a_vpp = uppervp;
3882a31267eSMatthew Dillon 				uppervp = NULL;
3892a31267eSMatthew Dillon 			}
3902a31267eSMatthew Dillon 			goto out;
3912a31267eSMatthew Dillon 		}
3922a31267eSMatthew Dillon 
3932a31267eSMatthew Dillon 		/*
3941c4ccf09SDon Lewis 		 * Calculate whiteout, fall through.
3952a31267eSMatthew Dillon 		 */
3962a31267eSMatthew Dillon 
397996c772fSJohn Dyson 		if (uerror == ENOENT || uerror == EJUSTRETURN) {
398996c772fSJohn Dyson 			if (cnp->cn_flags & ISWHITEOUT) {
399996c772fSJohn Dyson 				iswhiteout = 1;
400996c772fSJohn Dyson 			} else if (lowerdvp != NULLVP) {
401e3a285c7SMatthew Dillon 				int terror;
402e3a285c7SMatthew Dillon 
403e3a285c7SMatthew Dillon 				terror = VOP_GETATTR(upperdvp, &va,
404b40ce416SJulian Elischer 					cnp->cn_cred, cnp->cn_thread);
405e3a285c7SMatthew Dillon 				if (terror == 0 && (va.va_flags & OPAQUE))
406996c772fSJohn Dyson 					iswhiteout = 1;
407996c772fSJohn Dyson 			}
408996c772fSJohn Dyson 		}
409df8bae1dSRodney W. Grimes 	}
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	/*
4121c4ccf09SDon Lewis 	 * In a similar way to the upper layer, do the lookup
4131c4ccf09SDon Lewis 	 * in the lower layer.   This time, if there is some
414df8bae1dSRodney W. Grimes 	 * component magic going on, then vput whatever we got
415df8bae1dSRodney W. Grimes 	 * back from the upper layer and return the lower vnode
416df8bae1dSRodney W. Grimes 	 * instead.
417df8bae1dSRodney W. Grimes 	 */
4182a31267eSMatthew Dillon 
419996c772fSJohn Dyson 	if (lowerdvp != NULLVP && !iswhiteout) {
420df8bae1dSRodney W. Grimes 		int nameiop;
421df8bae1dSRodney W. Grimes 
4222a31267eSMatthew Dillon 		UDEBUG(("B %p\n", lowerdvp));
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 		/*
4252a31267eSMatthew Dillon 		 * Force only LOOKUPs on the lower node, since
426df8bae1dSRodney W. Grimes 		 * we won't be making changes to it anyway.
427df8bae1dSRodney W. Grimes 		 */
428df8bae1dSRodney W. Grimes 		nameiop = cnp->cn_nameiop;
429df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = LOOKUP;
430df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW) {
431df8bae1dSRodney W. Grimes 			saved_cred = cnp->cn_cred;
432df8bae1dSRodney W. Grimes 			cnp->cn_cred = um->um_cred;
433df8bae1dSRodney W. Grimes 		}
4342a31267eSMatthew Dillon 
435ee582cdfSKATO Takenori 		/*
436ee582cdfSKATO Takenori 		 * We shouldn't have to worry about locking interactions
437ee582cdfSKATO Takenori 		 * between the lower layer and our union layer (w.r.t.
438ee582cdfSKATO Takenori 		 * `..' processing) because we don't futz with lowervp
439ee582cdfSKATO Takenori 		 * locks in the union-node instantiation code path.
4402a31267eSMatthew Dillon 		 *
4412a31267eSMatthew Dillon 		 * union_lookup1() requires lowervp to be locked on entry,
4422a31267eSMatthew Dillon 		 * and it will be unlocked on return.  The ref count will
4432a31267eSMatthew Dillon 		 * not change.  On return lowervp doesn't represent anything
4442a31267eSMatthew Dillon 		 * to us so we NULL it out.
445ee582cdfSKATO Takenori 		 */
4462a31267eSMatthew Dillon 		VREF(lowerdvp);
447b40ce416SJulian Elischer 		vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, td);
4482a31267eSMatthew Dillon 		lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
4492a31267eSMatthew Dillon 		if (lowerdvp == lowervp)
4502a31267eSMatthew Dillon 			vrele(lowerdvp);
4512a31267eSMatthew Dillon 		else
4522a31267eSMatthew Dillon 			vput(lowerdvp);
4532a31267eSMatthew Dillon 		lowerdvp = NULL;	/* lowerdvp invalid after vput */
4542a31267eSMatthew Dillon 
455df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW)
456df8bae1dSRodney W. Grimes 			cnp->cn_cred = saved_cred;
457df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = nameiop;
458df8bae1dSRodney W. Grimes 
459f8fc96b5SKATO Takenori 		if (cnp->cn_consume != 0 || lerror == EACCES) {
4602a31267eSMatthew Dillon 			if ((error = lerror) == 0) {
461df8bae1dSRodney W. Grimes 				*ap->a_vpp = lowervp;
4622a31267eSMatthew Dillon 				lowervp = NULL;
4632a31267eSMatthew Dillon 			}
464afc2a558SKATO Takenori 			goto out;
465df8bae1dSRodney W. Grimes 		}
466df8bae1dSRodney W. Grimes 	} else {
4672a31267eSMatthew Dillon 		UDEBUG(("C %p\n", lowerdvp));
468996c772fSJohn Dyson 		if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
4692a31267eSMatthew Dillon 			if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
470996c772fSJohn Dyson 				VREF(lowervp);
471b40ce416SJulian Elischer 				vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, td);
472996c772fSJohn Dyson 				lerror = 0;
473996c772fSJohn Dyson 			}
474996c772fSJohn Dyson 		}
475df8bae1dSRodney W. Grimes 	}
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes 	/*
4782a31267eSMatthew Dillon 	 * Ok.  Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
479df8bae1dSRodney W. Grimes 	 *
4802a31267eSMatthew Dillon 	 * 1. If both layers returned an error, select the upper layer.
481df8bae1dSRodney W. Grimes 	 *
4821c4ccf09SDon Lewis 	 * 2. If the upper layer failed and the bottom layer succeeded,
4832a31267eSMatthew Dillon 	 *    two subcases occur:
484df8bae1dSRodney W. Grimes 	 *
4852a31267eSMatthew Dillon 	 *	a.  The bottom vnode is not a directory, in which case
4862a31267eSMatthew Dillon 	 *	    just return a new union vnode referencing an
4872a31267eSMatthew Dillon 	 *	    empty top layer and the existing bottom layer.
4882a31267eSMatthew Dillon 	 *
4891c4ccf09SDon Lewis 	 *	b.  The bottom vnode is a directory, in which case
4902a31267eSMatthew Dillon 	 *	    create a new directory in the top layer and
4912a31267eSMatthew Dillon 	 *	    and fall through to case 3.
4922a31267eSMatthew Dillon 	 *
4931c4ccf09SDon Lewis 	 * 3. If the top layer succeeded, then return a new union
494df8bae1dSRodney W. Grimes 	 *    vnode referencing whatever the new top layer and
495df8bae1dSRodney W. Grimes 	 *    whatever the bottom layer returned.
496df8bae1dSRodney W. Grimes 	 */
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 	/* case 1. */
499df8bae1dSRodney W. Grimes 	if ((uerror != 0) && (lerror != 0)) {
500afc2a558SKATO Takenori 		error = uerror;
501afc2a558SKATO Takenori 		goto out;
502df8bae1dSRodney W. Grimes 	}
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	/* case 2. */
505df8bae1dSRodney W. Grimes 	if (uerror != 0 /* && (lerror == 0) */ ) {
506df8bae1dSRodney W. Grimes 		if (lowervp->v_type == VDIR) { /* case 2b. */
5072a31267eSMatthew Dillon 			KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
5082a31267eSMatthew Dillon 			/*
5091c4ccf09SDon Lewis 			 * Oops, uppervp has a problem, we may have to shadow.
5102a31267eSMatthew Dillon 			 */
511df8bae1dSRodney W. Grimes 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
512df8bae1dSRodney W. Grimes 			if (uerror) {
513afc2a558SKATO Takenori 				error = uerror;
514afc2a558SKATO Takenori 				goto out;
515df8bae1dSRodney W. Grimes 			}
516df8bae1dSRodney W. Grimes 		}
517df8bae1dSRodney W. Grimes 	}
518df8bae1dSRodney W. Grimes 
5192a31267eSMatthew Dillon 	/*
5201c4ccf09SDon Lewis 	 * Must call union_allocvp() with both the upper and lower vnodes
5212a31267eSMatthew Dillon 	 * referenced and the upper vnode locked.   ap->a_vpp is returned
5222a31267eSMatthew Dillon 	 * referenced and locked.  lowervp, uppervp, and upperdvp are
5232a31267eSMatthew Dillon 	 * absorbed by union_allocvp() whether it succeeds or fails.
5242a31267eSMatthew Dillon 	 *
5252a31267eSMatthew Dillon 	 * upperdvp is the parent directory of uppervp which may be
5262a31267eSMatthew Dillon 	 * different, depending on the path, from dvp->un_uppervp.  That's
5272a31267eSMatthew Dillon 	 * why it is a separate argument.  Note that it must be unlocked.
5282a31267eSMatthew Dillon 	 *
5292a31267eSMatthew Dillon 	 * dvp must be locked on entry to the call and will be locked on
5302a31267eSMatthew Dillon 	 * return.
5312a31267eSMatthew Dillon 	 */
5322a31267eSMatthew Dillon 
5332a31267eSMatthew Dillon 	if (uppervp && uppervp != upperdvp)
534b40ce416SJulian Elischer 		VOP_UNLOCK(uppervp, 0, td);
5352a31267eSMatthew Dillon 	if (lowervp)
536b40ce416SJulian Elischer 		VOP_UNLOCK(lowervp, 0, td);
5372a31267eSMatthew Dillon 	if (upperdvp)
538b40ce416SJulian Elischer 		VOP_UNLOCK(upperdvp, 0, td);
539df8bae1dSRodney W. Grimes 
540df8bae1dSRodney W. Grimes 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
541996c772fSJohn Dyson 			      uppervp, lowervp, 1);
542df8bae1dSRodney W. Grimes 
5434d93c0beSJeff Roberson 	UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99));
5442a31267eSMatthew Dillon 
5452a31267eSMatthew Dillon 	uppervp = NULL;
5462a31267eSMatthew Dillon 	upperdvp = NULL;
5472a31267eSMatthew Dillon 	lowervp = NULL;
5482a31267eSMatthew Dillon 
5492a31267eSMatthew Dillon 	/*
5502a31267eSMatthew Dillon 	 *	Termination Code
5512a31267eSMatthew Dillon 	 *
5522a31267eSMatthew Dillon 	 *	- put away any extra junk laying around.  Note that lowervp
5532a31267eSMatthew Dillon 	 *	  (if not NULL) will never be the same as *ap->a_vp and
5542a31267eSMatthew Dillon 	 *	  neither will uppervp, because when we set that state we
5552a31267eSMatthew Dillon 	 *	  NULL-out lowervp or uppervp.  On the otherhand, upperdvp
5562a31267eSMatthew Dillon 	 *	  may match uppervp or *ap->a_vpp.
5572a31267eSMatthew Dillon 	 *
5582a31267eSMatthew Dillon 	 *	- relock/unlock dvp if appropriate.
5592a31267eSMatthew Dillon 	 */
5602a31267eSMatthew Dillon 
5612a31267eSMatthew Dillon out:
5622a31267eSMatthew Dillon 	if (upperdvp) {
5632a31267eSMatthew Dillon 		if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
5642a31267eSMatthew Dillon 			vrele(upperdvp);
5652a31267eSMatthew Dillon 		else
5662a31267eSMatthew Dillon 			vput(upperdvp);
5672a31267eSMatthew Dillon 	}
5682a31267eSMatthew Dillon 
5692a31267eSMatthew Dillon 	if (uppervp)
570df8bae1dSRodney W. Grimes 		vput(uppervp);
5712a31267eSMatthew Dillon 
5722a31267eSMatthew Dillon 	if (lowervp)
5732a31267eSMatthew Dillon 		vput(lowervp);
5742a31267eSMatthew Dillon 
5752a31267eSMatthew Dillon 	/*
5762a31267eSMatthew Dillon 	 * Restore LOCKPARENT state
5772a31267eSMatthew Dillon 	 */
5782a31267eSMatthew Dillon 
5792a31267eSMatthew Dillon 	if (!lockparent)
5802a31267eSMatthew Dillon 		cnp->cn_flags &= ~LOCKPARENT;
5812a31267eSMatthew Dillon 
5822a31267eSMatthew Dillon 	UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
5834d93c0beSJeff Roberson 		((*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99),
5842a31267eSMatthew Dillon 		lowervp, uppervp));
5852a31267eSMatthew Dillon 
5862a31267eSMatthew Dillon 	/*
5872a31267eSMatthew Dillon 	 * dvp lock state, determine whether to relock dvp.  dvp is expected
5882a31267eSMatthew Dillon 	 * to be locked on return if:
5892a31267eSMatthew Dillon 	 *
5902a31267eSMatthew Dillon 	 *	- there was an error (except not EJUSTRETURN), or
5912a31267eSMatthew Dillon 	 *	- we hit the last component and lockparent is true
5922a31267eSMatthew Dillon 	 *
5932a31267eSMatthew Dillon 	 * dvp_is_locked is the current state of the dvp lock, not counting
5942a31267eSMatthew Dillon 	 * the possibility that *ap->a_vpp == dvp (in which case it is locked
5952a31267eSMatthew Dillon 	 * anyway).  Note that *ap->a_vpp == dvp only if no error occured.
5962a31267eSMatthew Dillon 	 */
5972a31267eSMatthew Dillon 
5982a31267eSMatthew Dillon 	if (*ap->a_vpp != dvp) {
5992a31267eSMatthew Dillon 		if ((error == 0 || error == EJUSTRETURN) &&
6002a31267eSMatthew Dillon 		    (!lockparent || (cnp->cn_flags & ISLASTCN) == 0)) {
601b40ce416SJulian Elischer 			VOP_UNLOCK(dvp, 0, td);
6022a31267eSMatthew Dillon 		}
6032a31267eSMatthew Dillon 	}
6042a31267eSMatthew Dillon 
6052a31267eSMatthew Dillon 	/*
6062a31267eSMatthew Dillon 	 * Diagnostics
6072a31267eSMatthew Dillon 	 */
6082a31267eSMatthew Dillon 
609f5a5311eSKATO Takenori #ifdef DIAGNOSTIC
610f5a5311eSKATO Takenori 	if (cnp->cn_namelen == 1 &&
611f5a5311eSKATO Takenori 	    cnp->cn_nameptr[0] == '.' &&
612f5a5311eSKATO Takenori 	    *ap->a_vpp != dvp) {
6132a31267eSMatthew Dillon 		panic("union_lookup returning . (%p) not same as startdir (%p)", ap->a_vpp, dvp);
614f5a5311eSKATO Takenori 	}
615f5a5311eSKATO Takenori #endif
616afc2a558SKATO Takenori 
617df8bae1dSRodney W. Grimes 	return (error);
618df8bae1dSRodney W. Grimes }
619df8bae1dSRodney W. Grimes 
6202a31267eSMatthew Dillon /*
6212a31267eSMatthew Dillon  * 	union_create:
6222a31267eSMatthew Dillon  *
6232a31267eSMatthew Dillon  * a_dvp is locked on entry and remains locked on return.  a_vpp is returned
6242a31267eSMatthew Dillon  * locked if no error occurs, otherwise it is garbage.
6252a31267eSMatthew Dillon  */
6262a31267eSMatthew Dillon 
627c9bf0111SKATO Takenori static int
628df8bae1dSRodney W. Grimes union_create(ap)
629df8bae1dSRodney W. Grimes 	struct vop_create_args /* {
630df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
631df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
632df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
633df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
634df8bae1dSRodney W. Grimes 	} */ *ap;
635df8bae1dSRodney W. Grimes {
6367be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
637996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
638b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
6392a31267eSMatthew Dillon 	struct vnode *dvp;
6402a31267eSMatthew Dillon 	int error = EROFS;
641df8bae1dSRodney W. Grimes 
642b40ce416SJulian Elischer 	if ((dvp = union_lock_upper(dun, td)) != NULL) {
643df8bae1dSRodney W. Grimes 		struct vnode *vp;
644996c772fSJohn Dyson 		struct mount *mp;
645df8bae1dSRodney W. Grimes 
646996c772fSJohn Dyson 		error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
6472a31267eSMatthew Dillon 		if (error == 0) {
6487be2d300SMike Smith 			mp = ap->a_dvp->v_mount;
649b40ce416SJulian Elischer 			VOP_UNLOCK(vp, 0, td);
6504d93c0beSJeff Roberson 			UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vrefcnt(vp)));
6512a31267eSMatthew Dillon 			error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
6522a31267eSMatthew Dillon 				cnp, vp, NULLVP, 1);
6534d93c0beSJeff Roberson 			UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp)));
654df8bae1dSRodney W. Grimes 		}
655b40ce416SJulian Elischer 		union_unlock_upper(dvp, td);
6562a31267eSMatthew Dillon 	}
6572a31267eSMatthew Dillon 	return (error);
658df8bae1dSRodney W. Grimes }
659df8bae1dSRodney W. Grimes 
660c9bf0111SKATO Takenori static int
661996c772fSJohn Dyson union_whiteout(ap)
662996c772fSJohn Dyson 	struct vop_whiteout_args /* {
663996c772fSJohn Dyson 		struct vnode *a_dvp;
664996c772fSJohn Dyson 		struct componentname *a_cnp;
665996c772fSJohn Dyson 		int a_flags;
666996c772fSJohn Dyson 	} */ *ap;
667996c772fSJohn Dyson {
668996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_dvp);
669996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
6702a31267eSMatthew Dillon 	struct vnode *uppervp;
6713bb3827fSDavid Schultz 	int error;
672996c772fSJohn Dyson 
673ac092fb3SDavid Schultz 	switch (ap->a_flags) {
674ac092fb3SDavid Schultz 	case CREATE:
675ac092fb3SDavid Schultz 	case DELETE:
6763bb3827fSDavid Schultz 		uppervp = union_lock_upper(un, cnp->cn_thread);
6773bb3827fSDavid Schultz 		if (uppervp != NULLVP) {
6782a31267eSMatthew Dillon 			error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags);
679b40ce416SJulian Elischer 			union_unlock_upper(uppervp, cnp->cn_thread);
6803bb3827fSDavid Schultz 		} else
6813bb3827fSDavid Schultz 			error = EOPNOTSUPP;
6823bb3827fSDavid Schultz 		break;
6833bb3827fSDavid Schultz 	case LOOKUP:
6843bb3827fSDavid Schultz 		error = EOPNOTSUPP;
685ac092fb3SDavid Schultz 		break;
686ac092fb3SDavid Schultz 	default:
687ac092fb3SDavid Schultz 		panic("union_whiteout: unknown op");
688ac092fb3SDavid Schultz 	}
6892a31267eSMatthew Dillon 	return (error);
6902a31267eSMatthew Dillon }
6912a31267eSMatthew Dillon 
6922a31267eSMatthew Dillon /*
6932a31267eSMatthew Dillon  * 	union_mknod:
6942a31267eSMatthew Dillon  *
6952a31267eSMatthew Dillon  *	a_dvp is locked on entry and should remain locked on return.
6962a31267eSMatthew Dillon  *	a_vpp is garbagre whether an error occurs or not.
6972a31267eSMatthew Dillon  */
698996c772fSJohn Dyson 
699c9bf0111SKATO Takenori static int
700df8bae1dSRodney W. Grimes union_mknod(ap)
701df8bae1dSRodney W. Grimes 	struct vop_mknod_args /* {
702df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
703df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
704df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
705df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
706df8bae1dSRodney W. Grimes 	} */ *ap;
707df8bae1dSRodney W. Grimes {
7087be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
709996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
7102a31267eSMatthew Dillon 	struct vnode *dvp;
7112a31267eSMatthew Dillon 	int error = EROFS;
712df8bae1dSRodney W. Grimes 
713b40ce416SJulian Elischer 	if ((dvp = union_lock_upper(dun, cnp->cn_thread)) != NULL) {
714edfe736dSEivind Eklund 		error = VOP_MKNOD(dvp, ap->a_vpp, cnp, ap->a_vap);
715b40ce416SJulian Elischer 		union_unlock_upper(dvp, cnp->cn_thread);
716df8bae1dSRodney W. Grimes 	}
717df8bae1dSRodney W. Grimes 	return (error);
718df8bae1dSRodney W. Grimes }
719df8bae1dSRodney W. Grimes 
7202a31267eSMatthew Dillon /*
7212a31267eSMatthew Dillon  *	union_open:
7222a31267eSMatthew Dillon  *
7232a31267eSMatthew Dillon  *	run open VOP.  When opening the underlying vnode we have to mimic
7241c4ccf09SDon Lewis  *	vn_open().  What we *really* need to do to avoid screwups if the
7252a31267eSMatthew Dillon  *	open semantics change is to call vn_open().  For example, ufs blows
7262a31267eSMatthew Dillon  *	up if you open a file but do not vmio it prior to writing.
7272a31267eSMatthew Dillon  */
728df8bae1dSRodney W. Grimes 
729c9bf0111SKATO Takenori static int
730df8bae1dSRodney W. Grimes union_open(ap)
731df8bae1dSRodney W. Grimes 	struct vop_open_args /* {
732df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
733df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
734df8bae1dSRodney W. Grimes 		int a_mode;
735df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
736b40ce416SJulian Elischer 		struct thread *a_td;
737df8bae1dSRodney W. Grimes 	} */ *ap;
738df8bae1dSRodney W. Grimes {
739df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
740df8bae1dSRodney W. Grimes 	struct vnode *tvp;
741df8bae1dSRodney W. Grimes 	int mode = ap->a_mode;
742df8bae1dSRodney W. Grimes 	struct ucred *cred = ap->a_cred;
743b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
7442a31267eSMatthew Dillon 	int error = 0;
7452a31267eSMatthew Dillon 	int tvpisupper = 1;
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	/*
748df8bae1dSRodney W. Grimes 	 * If there is an existing upper vp then simply open that.
7492a31267eSMatthew Dillon 	 * The upper vp takes precedence over the lower vp.  When opening
7502a31267eSMatthew Dillon 	 * a lower vp for writing copy it to the uppervp and then open the
7512a31267eSMatthew Dillon 	 * uppervp.
7522a31267eSMatthew Dillon 	 *
7532a31267eSMatthew Dillon 	 * At the end of this section tvp will be left locked.
754df8bae1dSRodney W. Grimes 	 */
755b40ce416SJulian Elischer 	if ((tvp = union_lock_upper(un, td)) == NULLVP) {
756df8bae1dSRodney W. Grimes 		/*
757df8bae1dSRodney W. Grimes 		 * If the lower vnode is being opened for writing, then
758df8bae1dSRodney W. Grimes 		 * copy the file contents to the upper vnode and open that,
759df8bae1dSRodney W. Grimes 		 * otherwise can simply open the lower vnode.
760df8bae1dSRodney W. Grimes 		 */
761df8bae1dSRodney W. Grimes 		tvp = un->un_lowervp;
762df8bae1dSRodney W. Grimes 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
7632a31267eSMatthew Dillon 			int docopy = !(mode & O_TRUNC);
764b40ce416SJulian Elischer 			error = union_copyup(un, docopy, cred, td);
765b40ce416SJulian Elischer 			tvp = union_lock_upper(un, td);
7662a31267eSMatthew Dillon 		} else {
7672a31267eSMatthew Dillon 			un->un_openl++;
7682a31267eSMatthew Dillon 			VREF(tvp);
769b40ce416SJulian Elischer 			vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td);
7702a31267eSMatthew Dillon 			tvpisupper = 0;
7712a31267eSMatthew Dillon 		}
7722a31267eSMatthew Dillon 	}
7732a31267eSMatthew Dillon 
7742a31267eSMatthew Dillon 	/*
7751c4ccf09SDon Lewis 	 * We are holding the correct vnode, open it.
7762a31267eSMatthew Dillon 	 */
7772a31267eSMatthew Dillon 
778df8bae1dSRodney W. Grimes 	if (error == 0)
779b40ce416SJulian Elischer 		error = VOP_OPEN(tvp, mode, cred, td);
7802a31267eSMatthew Dillon 
7812a31267eSMatthew Dillon 	/*
7821c4ccf09SDon Lewis 	 * This is absolutely necessary or UFS will blow up.
7832a31267eSMatthew Dillon 	 */
7842a31267eSMatthew Dillon         if (error == 0 && vn_canvmio(tvp) == TRUE) {
785b40ce416SJulian Elischer                 error = vfs_object_create(tvp, td, cred);
7862a31267eSMatthew Dillon         }
7872a31267eSMatthew Dillon 
7882a31267eSMatthew Dillon 	/*
7891c4ccf09SDon Lewis 	 * Release any locks held.
7902a31267eSMatthew Dillon 	 */
7912a31267eSMatthew Dillon 	if (tvpisupper) {
7922a31267eSMatthew Dillon 		if (tvp)
793b40ce416SJulian Elischer 			union_unlock_upper(tvp, td);
7942a31267eSMatthew Dillon 	} else {
7952a31267eSMatthew Dillon 		vput(tvp);
7962a31267eSMatthew Dillon 	}
797df8bae1dSRodney W. Grimes 	return (error);
798df8bae1dSRodney W. Grimes }
799df8bae1dSRodney W. Grimes 
800df8bae1dSRodney W. Grimes /*
8012a31267eSMatthew Dillon  *	union_close:
8022a31267eSMatthew Dillon  *
8032a31267eSMatthew Dillon  *	It is unclear whether a_vp is passed locked or unlocked.  Whatever
8042a31267eSMatthew Dillon  *	the case we do not change it.
805df8bae1dSRodney W. Grimes  */
806df8bae1dSRodney W. Grimes 
807c9bf0111SKATO Takenori static int
808df8bae1dSRodney W. Grimes union_close(ap)
809df8bae1dSRodney W. Grimes 	struct vop_close_args /* {
810df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
811df8bae1dSRodney W. Grimes 		int  a_fflag;
812df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
813b40ce416SJulian Elischer 		struct thread *a_td;
814df8bae1dSRodney W. Grimes 	} */ *ap;
815df8bae1dSRodney W. Grimes {
816df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
817df8bae1dSRodney W. Grimes 	struct vnode *vp;
818df8bae1dSRodney W. Grimes 
819996c772fSJohn Dyson 	if ((vp = un->un_uppervp) == NULLVP) {
820df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
821df8bae1dSRodney W. Grimes 		if (un->un_openl <= 0)
822df8bae1dSRodney W. Grimes 			panic("union: un_openl cnt");
823df8bae1dSRodney W. Grimes #endif
824df8bae1dSRodney W. Grimes 		--un->un_openl;
825df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
826df8bae1dSRodney W. Grimes 	}
827996c772fSJohn Dyson 	ap->a_vp = vp;
828996c772fSJohn Dyson 	return (VCALL(vp, VOFFSET(vop_close), ap));
829df8bae1dSRodney W. Grimes }
830df8bae1dSRodney W. Grimes 
831df8bae1dSRodney W. Grimes /*
832df8bae1dSRodney W. Grimes  * Check access permission on the union vnode.
833df8bae1dSRodney W. Grimes  * The access check being enforced is to check
834df8bae1dSRodney W. Grimes  * against both the underlying vnode, and any
835df8bae1dSRodney W. Grimes  * copied vnode.  This ensures that no additional
836df8bae1dSRodney W. Grimes  * file permissions are given away simply because
837df8bae1dSRodney W. Grimes  * the user caused an implicit file copy.
838df8bae1dSRodney W. Grimes  */
839c9bf0111SKATO Takenori static int
840df8bae1dSRodney W. Grimes union_access(ap)
841df8bae1dSRodney W. Grimes 	struct vop_access_args /* {
842df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
843df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
844df8bae1dSRodney W. Grimes 		int a_mode;
845df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
846b40ce416SJulian Elischer 		struct thread *a_td;
847df8bae1dSRodney W. Grimes 	} */ *ap;
848df8bae1dSRodney W. Grimes {
849df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
850b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
851df8bae1dSRodney W. Grimes 	int error = EACCES;
852df8bae1dSRodney W. Grimes 	struct vnode *vp;
853df8bae1dSRodney W. Grimes 
8546ca02614SKATO Takenori 	/*
8556ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
8566ca02614SKATO Takenori 	 */
8572a31267eSMatthew Dillon 	if ((ap->a_mode & VWRITE) &&
8582a31267eSMatthew Dillon 	    (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
8596ca02614SKATO Takenori 		switch (ap->a_vp->v_type) {
860831a80b0SMatthew Dillon 		case VREG:
861831a80b0SMatthew Dillon 		case VDIR:
862831a80b0SMatthew Dillon 		case VLNK:
8636ca02614SKATO Takenori 			return (EROFS);
864831a80b0SMatthew Dillon 		default:
865831a80b0SMatthew Dillon 			break;
8666ca02614SKATO Takenori 		}
8676ca02614SKATO Takenori 	}
8682a31267eSMatthew Dillon 
869b40ce416SJulian Elischer 	if ((vp = union_lock_upper(un, td)) != NULLVP) {
870996c772fSJohn Dyson 		ap->a_vp = vp;
8712a31267eSMatthew Dillon 		error = VCALL(vp, VOFFSET(vop_access), ap);
872b40ce416SJulian Elischer 		union_unlock_upper(vp, td);
8732a31267eSMatthew Dillon 		return(error);
874df8bae1dSRodney W. Grimes 	}
875df8bae1dSRodney W. Grimes 
876996c772fSJohn Dyson 	if ((vp = un->un_lowervp) != NULLVP) {
877b40ce416SJulian Elischer 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
878996c772fSJohn Dyson 		ap->a_vp = vp;
8792a31267eSMatthew Dillon 
8802a31267eSMatthew Dillon 		/*
8812a31267eSMatthew Dillon 		 * Remove VWRITE from a_mode if our mount point is RW, because
8822a31267eSMatthew Dillon 		 * we want to allow writes and lowervp may be read-only.
8832a31267eSMatthew Dillon 		 */
8842a31267eSMatthew Dillon 		if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
8852a31267eSMatthew Dillon 			ap->a_mode &= ~VWRITE;
8862a31267eSMatthew Dillon 
887996c772fSJohn Dyson 		error = VCALL(vp, VOFFSET(vop_access), ap);
888df8bae1dSRodney W. Grimes 		if (error == 0) {
8892a31267eSMatthew Dillon 			struct union_mount *um;
8902a31267eSMatthew Dillon 
8912a31267eSMatthew Dillon 			um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
892df8bae1dSRodney W. Grimes 
893996c772fSJohn Dyson 			if (um->um_op == UNMNT_BELOW) {
894996c772fSJohn Dyson 				ap->a_cred = um->um_cred;
895996c772fSJohn Dyson 				error = VCALL(vp, VOFFSET(vop_access), ap);
896df8bae1dSRodney W. Grimes 			}
897996c772fSJohn Dyson 		}
898b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
899df8bae1dSRodney W. Grimes 	}
900df8bae1dSRodney W. Grimes 	return(error);
901df8bae1dSRodney W. Grimes }
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes /*
904996c772fSJohn Dyson  * We handle getattr only to change the fsid and
905996c772fSJohn Dyson  * track object sizes
9062a31267eSMatthew Dillon  *
9072a31267eSMatthew Dillon  * It's not clear whether VOP_GETATTR is to be
9082a31267eSMatthew Dillon  * called with the vnode locked or not.  stat() calls
9091c4ccf09SDon Lewis  * it with (vp) locked, and fstat() calls it with
9102a31267eSMatthew Dillon  * (vp) unlocked.
9112a31267eSMatthew Dillon  *
9122a31267eSMatthew Dillon  * Because of this we cannot use our normal locking functions
9132a31267eSMatthew Dillon  * if we do not intend to lock the main a_vp node.  At the moment
9142a31267eSMatthew Dillon  * we are running without any specific locking at all, but beware
9152a31267eSMatthew Dillon  * to any programmer that care must be taken if locking is added
9162a31267eSMatthew Dillon  * to this function.
917df8bae1dSRodney W. Grimes  */
9182a31267eSMatthew Dillon 
919c9bf0111SKATO Takenori static int
920df8bae1dSRodney W. Grimes union_getattr(ap)
921df8bae1dSRodney W. Grimes 	struct vop_getattr_args /* {
922df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
923df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
924df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
925b40ce416SJulian Elischer 		struct thread *a_td;
926df8bae1dSRodney W. Grimes 	} */ *ap;
927df8bae1dSRodney W. Grimes {
928df8bae1dSRodney W. Grimes 	int error;
929df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
9302a31267eSMatthew Dillon 	struct vnode *vp;
931df8bae1dSRodney W. Grimes 	struct vattr *vap;
932df8bae1dSRodney W. Grimes 	struct vattr va;
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	/*
935df8bae1dSRodney W. Grimes 	 * Some programs walk the filesystem hierarchy by counting
936df8bae1dSRodney W. Grimes 	 * links to directories to avoid stat'ing all the time.
937df8bae1dSRodney W. Grimes 	 * This means the link count on directories needs to be "correct".
938df8bae1dSRodney W. Grimes 	 * The only way to do that is to call getattr on both layers
939df8bae1dSRodney W. Grimes 	 * and fix up the link count.  The link count will not necessarily
940df8bae1dSRodney W. Grimes 	 * be accurate but will be large enough to defeat the tree walkers.
941df8bae1dSRodney W. Grimes 	 */
942df8bae1dSRodney W. Grimes 
943df8bae1dSRodney W. Grimes 	vap = ap->a_vap;
944df8bae1dSRodney W. Grimes 
9452a31267eSMatthew Dillon 	if ((vp = un->un_uppervp) != NULLVP) {
946b40ce416SJulian Elischer 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
947df8bae1dSRodney W. Grimes 		if (error)
948df8bae1dSRodney W. Grimes 			return (error);
9491c4ccf09SDon Lewis 		/* XXX isn't this dangerous without a lock? */
950996c772fSJohn Dyson 		union_newsize(ap->a_vp, vap->va_size, VNOVAL);
951df8bae1dSRodney W. Grimes 	}
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 	if (vp == NULLVP) {
954df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
955b2bde4cbSKATO Takenori 	} else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
956df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
957df8bae1dSRodney W. Grimes 		vap = &va;
958df8bae1dSRodney W. Grimes 	} else {
959df8bae1dSRodney W. Grimes 		vp = NULLVP;
960df8bae1dSRodney W. Grimes 	}
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
963b40ce416SJulian Elischer 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
964df8bae1dSRodney W. Grimes 		if (error)
965df8bae1dSRodney W. Grimes 			return (error);
9662a31267eSMatthew Dillon 		/* XXX isn't this dangerous without a lock? */
967996c772fSJohn Dyson 		union_newsize(ap->a_vp, VNOVAL, vap->va_size);
968df8bae1dSRodney W. Grimes 	}
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
971df8bae1dSRodney W. Grimes 		ap->a_vap->va_nlink += vap->va_nlink;
972df8bae1dSRodney W. Grimes 	return (0);
973df8bae1dSRodney W. Grimes }
974df8bae1dSRodney W. Grimes 
975c9bf0111SKATO Takenori static int
976df8bae1dSRodney W. Grimes union_setattr(ap)
977df8bae1dSRodney W. Grimes 	struct vop_setattr_args /* {
978df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
979df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
980df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
981b40ce416SJulian Elischer 		struct thread *a_td;
982df8bae1dSRodney W. Grimes 	} */ *ap;
983df8bae1dSRodney W. Grimes {
984df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
985b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
9866ca02614SKATO Takenori 	struct vattr *vap = ap->a_vap;
9872a31267eSMatthew Dillon 	struct vnode *uppervp;
988df8bae1dSRodney W. Grimes 	int error;
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes 	/*
9916ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
9926ca02614SKATO Takenori 	 */
9936ca02614SKATO Takenori 	if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
9946ca02614SKATO Takenori 	    (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
9956ca02614SKATO Takenori 	     vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
9962a31267eSMatthew Dillon 	     vap->va_mtime.tv_sec != VNOVAL ||
9972a31267eSMatthew Dillon 	     vap->va_mode != (mode_t)VNOVAL)) {
9986ca02614SKATO Takenori 		return (EROFS);
9992a31267eSMatthew Dillon 	}
10006ca02614SKATO Takenori 
10016ca02614SKATO Takenori 	/*
10021c4ccf09SDon Lewis 	 * Handle case of truncating lower object to zero size
1003df8bae1dSRodney W. Grimes 	 * by creating a zero length upper object.  This is to
1004df8bae1dSRodney W. Grimes 	 * handle the case of open with O_TRUNC and O_CREAT.
1005df8bae1dSRodney W. Grimes 	 */
10062a31267eSMatthew Dillon 	if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
1007996c772fSJohn Dyson 		error = union_copyup(un, (ap->a_vap->va_size != 0),
1008b40ce416SJulian Elischer 			    ap->a_cred, ap->a_td);
1009df8bae1dSRodney W. Grimes 		if (error)
1010df8bae1dSRodney W. Grimes 			return (error);
1011df8bae1dSRodney W. Grimes 	}
1012df8bae1dSRodney W. Grimes 
1013df8bae1dSRodney W. Grimes 	/*
1014df8bae1dSRodney W. Grimes 	 * Try to set attributes in upper layer,
1015df8bae1dSRodney W. Grimes 	 * otherwise return read-only filesystem error.
1016df8bae1dSRodney W. Grimes 	 */
10172a31267eSMatthew Dillon 	error = EROFS;
1018b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1019df8bae1dSRodney W. Grimes 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
1020b40ce416SJulian Elischer 					ap->a_cred, ap->a_td);
1021996c772fSJohn Dyson 		if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
1022996c772fSJohn Dyson 			union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
1023b40ce416SJulian Elischer 		union_unlock_upper(uppervp, td);
10242a31267eSMatthew Dillon 	}
10252a31267eSMatthew Dillon 	return (error);
1026df8bae1dSRodney W. Grimes }
1027df8bae1dSRodney W. Grimes 
1028c9bf0111SKATO Takenori static int
1029df8bae1dSRodney W. Grimes union_read(ap)
1030df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
1031df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1032df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1033df8bae1dSRodney W. Grimes 		int  a_ioflag;
1034df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1035df8bae1dSRodney W. Grimes 	} */ *ap;
1036df8bae1dSRodney W. Grimes {
10372a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1038b40ce416SJulian Elischer 	struct thread *td = ap->a_uio->uio_td;
10392a31267eSMatthew Dillon 	struct vnode *uvp;
10402a31267eSMatthew Dillon 	int error;
1041df8bae1dSRodney W. Grimes 
1042b40ce416SJulian Elischer 	uvp = union_lock_other(un, td);
10432a31267eSMatthew Dillon 	KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
10442a31267eSMatthew Dillon 
10452a31267eSMatthew Dillon 	error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1046b40ce416SJulian Elischer 	union_unlock_other(uvp, td);
1047996c772fSJohn Dyson 
1048996c772fSJohn Dyson 	/*
1049996c772fSJohn Dyson 	 * XXX
10501c4ccf09SDon Lewis 	 * Perhaps the size of the underlying object has changed under
10511c4ccf09SDon Lewis 	 * our feet.  Take advantage of the offset information present
1052996c772fSJohn Dyson 	 * in the uio structure.
1053996c772fSJohn Dyson 	 */
1054996c772fSJohn Dyson 	if (error == 0) {
1055996c772fSJohn Dyson 		struct union_node *un = VTOUNION(ap->a_vp);
1056996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
1057996c772fSJohn Dyson 
10582a31267eSMatthew Dillon 		if (uvp == un->un_uppervp) {
1059996c772fSJohn Dyson 			if (cur > un->un_uppersz)
1060996c772fSJohn Dyson 				union_newsize(ap->a_vp, cur, VNOVAL);
1061996c772fSJohn Dyson 		} else {
1062996c772fSJohn Dyson 			if (cur > un->un_lowersz)
1063996c772fSJohn Dyson 				union_newsize(ap->a_vp, VNOVAL, cur);
1064996c772fSJohn Dyson 		}
1065996c772fSJohn Dyson 	}
1066df8bae1dSRodney W. Grimes 	return (error);
1067df8bae1dSRodney W. Grimes }
1068df8bae1dSRodney W. Grimes 
1069c9bf0111SKATO Takenori static int
1070df8bae1dSRodney W. Grimes union_write(ap)
1071df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
1072df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1073df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1074df8bae1dSRodney W. Grimes 		int  a_ioflag;
1075df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1076df8bae1dSRodney W. Grimes 	} */ *ap;
1077df8bae1dSRodney W. Grimes {
1078996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_vp);
1079b40ce416SJulian Elischer 	struct thread *td = ap->a_uio->uio_td;
10802a31267eSMatthew Dillon 	struct vnode *uppervp;
10812a31267eSMatthew Dillon 	int error;
1082df8bae1dSRodney W. Grimes 
1083b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) == NULLVP)
1084996c772fSJohn Dyson 		panic("union: missing upper layer in write");
1085996c772fSJohn Dyson 
10862a31267eSMatthew Dillon 	error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1087996c772fSJohn Dyson 
1088996c772fSJohn Dyson 	/*
10891c4ccf09SDon Lewis 	 * The size of the underlying object may be changed by the
1090996c772fSJohn Dyson 	 * write.
1091996c772fSJohn Dyson 	 */
1092996c772fSJohn Dyson 	if (error == 0) {
1093996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
1094996c772fSJohn Dyson 
1095996c772fSJohn Dyson 		if (cur > un->un_uppersz)
1096996c772fSJohn Dyson 			union_newsize(ap->a_vp, cur, VNOVAL);
1097996c772fSJohn Dyson 	}
1098b40ce416SJulian Elischer 	union_unlock_upper(uppervp, td);
1099df8bae1dSRodney W. Grimes 	return (error);
1100df8bae1dSRodney W. Grimes }
1101df8bae1dSRodney W. Grimes 
1102c9bf0111SKATO Takenori static int
1103996c772fSJohn Dyson union_lease(ap)
1104996c772fSJohn Dyson 	struct vop_lease_args /* {
1105996c772fSJohn Dyson 		struct vnode *a_vp;
1106b40ce416SJulian Elischer 		struct thread *a_td;
1107996c772fSJohn Dyson 		struct ucred *a_cred;
1108996c772fSJohn Dyson 		int a_flag;
1109996c772fSJohn Dyson 	} */ *ap;
1110996c772fSJohn Dyson {
11112a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1112996c772fSJohn Dyson 
1113996c772fSJohn Dyson 	ap->a_vp = ovp;
1114996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_lease), ap));
1115996c772fSJohn Dyson }
1116996c772fSJohn Dyson 
1117c9bf0111SKATO Takenori static int
1118df8bae1dSRodney W. Grimes union_ioctl(ap)
1119df8bae1dSRodney W. Grimes 	struct vop_ioctl_args /* {
1120df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1121bc9d8a9aSPoul-Henning Kamp 		u_long  a_command;
1122df8bae1dSRodney W. Grimes 		caddr_t  a_data;
1123df8bae1dSRodney W. Grimes 		int  a_fflag;
1124df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1125b40ce416SJulian Elischer 		struct thread *a_td;
1126df8bae1dSRodney W. Grimes 	} */ *ap;
1127df8bae1dSRodney W. Grimes {
11282a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1129df8bae1dSRodney W. Grimes 
1130996c772fSJohn Dyson 	ap->a_vp = ovp;
1131996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
1134c9bf0111SKATO Takenori static int
1135a6aeade2SPeter Wemm union_poll(ap)
1136a6aeade2SPeter Wemm 	struct vop_poll_args /* {
1137df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1138a6aeade2SPeter Wemm 		int  a_events;
1139df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1140b40ce416SJulian Elischer 		struct thread *a_td;
1141df8bae1dSRodney W. Grimes 	} */ *ap;
1142df8bae1dSRodney W. Grimes {
11432a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1144df8bae1dSRodney W. Grimes 
1145996c772fSJohn Dyson 	ap->a_vp = ovp;
1146a6aeade2SPeter Wemm 	return (VCALL(ovp, VOFFSET(vop_poll), ap));
1147996c772fSJohn Dyson }
1148996c772fSJohn Dyson 
1149c9bf0111SKATO Takenori static int
1150996c772fSJohn Dyson union_revoke(ap)
1151996c772fSJohn Dyson 	struct vop_revoke_args /* {
1152996c772fSJohn Dyson 		struct vnode *a_vp;
1153996c772fSJohn Dyson 		int a_flags;
1154b40ce416SJulian Elischer 		struct thread *a_td;
1155996c772fSJohn Dyson 	} */ *ap;
1156996c772fSJohn Dyson {
1157996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1158996c772fSJohn Dyson 
1159996c772fSJohn Dyson 	if (UPPERVP(vp))
1160996c772fSJohn Dyson 		VOP_REVOKE(UPPERVP(vp), ap->a_flags);
1161996c772fSJohn Dyson 	if (LOWERVP(vp))
1162996c772fSJohn Dyson 		VOP_REVOKE(LOWERVP(vp), ap->a_flags);
1163996c772fSJohn Dyson 	vgone(vp);
1164996c772fSJohn Dyson 	return (0);
1165df8bae1dSRodney W. Grimes }
1166df8bae1dSRodney W. Grimes 
1167c9bf0111SKATO Takenori static int
1168df8bae1dSRodney W. Grimes union_fsync(ap)
1169df8bae1dSRodney W. Grimes 	struct vop_fsync_args /* {
1170df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1171df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1172df8bae1dSRodney W. Grimes 		int  a_waitfor;
1173b40ce416SJulian Elischer 		struct thread *a_td;
1174df8bae1dSRodney W. Grimes 	} */ *ap;
1175df8bae1dSRodney W. Grimes {
1176df8bae1dSRodney W. Grimes 	int error = 0;
1177b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
11782a31267eSMatthew Dillon 	struct vnode *targetvp;
11792a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1180df8bae1dSRodney W. Grimes 
1181b40ce416SJulian Elischer 	if ((targetvp = union_lock_other(un, td)) != NULLVP) {
1182b40ce416SJulian Elischer 		error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, td);
1183b40ce416SJulian Elischer 		union_unlock_other(targetvp, td);
1184df8bae1dSRodney W. Grimes 	}
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 	return (error);
1187df8bae1dSRodney W. Grimes }
1188df8bae1dSRodney W. Grimes 
11892a31267eSMatthew Dillon /*
11902a31267eSMatthew Dillon  *	union_remove:
11912a31267eSMatthew Dillon  *
11922a31267eSMatthew Dillon  *	Remove the specified cnp.  The dvp and vp are passed to us locked
11932a31267eSMatthew Dillon  *	and must remain locked on return.
11942a31267eSMatthew Dillon  */
11952a31267eSMatthew Dillon 
1196c9bf0111SKATO Takenori static int
1197df8bae1dSRodney W. Grimes union_remove(ap)
1198df8bae1dSRodney W. Grimes 	struct vop_remove_args /* {
1199df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1200df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1201df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1202df8bae1dSRodney W. Grimes 	} */ *ap;
1203df8bae1dSRodney W. Grimes {
1204df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1205df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1206996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1207b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
12082a31267eSMatthew Dillon 	struct vnode *uppervp;
12092a31267eSMatthew Dillon 	struct vnode *upperdvp;
12107be2d300SMike Smith 	int error;
1211df8bae1dSRodney W. Grimes 
1212b40ce416SJulian Elischer 	if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1213996c772fSJohn Dyson 		panic("union remove: null upper vnode");
1214996c772fSJohn Dyson 
1215b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1216b40ce416SJulian Elischer 		if (union_dowhiteout(un, cnp->cn_cred, td))
1217996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
12182a31267eSMatthew Dillon 		error = VOP_REMOVE(upperdvp, uppervp, cnp);
121987884aadSKATO Takenori #if 0
122087884aadSKATO Takenori 		/* XXX */
1221df8bae1dSRodney W. Grimes 		if (!error)
1222df8bae1dSRodney W. Grimes 			union_removed_upper(un);
122387884aadSKATO Takenori #endif
1224b40ce416SJulian Elischer 		union_unlock_upper(uppervp, td);
1225df8bae1dSRodney W. Grimes 	} else {
1226996c772fSJohn Dyson 		error = union_mkwhiteout(
12272a31267eSMatthew Dillon 			    MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
12282a31267eSMatthew Dillon 			    upperdvp, ap->a_cnp, un->un_path);
1229df8bae1dSRodney W. Grimes 	}
1230b40ce416SJulian Elischer 	union_unlock_upper(upperdvp, td);
1231df8bae1dSRodney W. Grimes 	return (error);
1232df8bae1dSRodney W. Grimes }
1233df8bae1dSRodney W. Grimes 
12342a31267eSMatthew Dillon /*
12352a31267eSMatthew Dillon  *	union_link:
12362a31267eSMatthew Dillon  *
1237fa288043SDon Lewis  *	tdvp and vp will be locked on entry.
1238fa288043SDon Lewis  *	tdvp and vp should remain locked on return.
12392a31267eSMatthew Dillon  */
12402a31267eSMatthew Dillon 
1241c9bf0111SKATO Takenori static int
1242df8bae1dSRodney W. Grimes union_link(ap)
1243df8bae1dSRodney W. Grimes 	struct vop_link_args /* {
1244df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
124547777413SDavid Greenman 		struct vnode *a_vp;
1246df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1247df8bae1dSRodney W. Grimes 	} */ *ap;
1248df8bae1dSRodney W. Grimes {
1249996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1250b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
12517be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_tdvp);
1252996c772fSJohn Dyson 	struct vnode *vp;
1253996c772fSJohn Dyson 	struct vnode *tdvp;
12547be2d300SMike Smith 	int error = 0;
1255df8bae1dSRodney W. Grimes 
1256996c772fSJohn Dyson 	if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1257996c772fSJohn Dyson 		vp = ap->a_vp;
1258df8bae1dSRodney W. Grimes 	} else {
1259996c772fSJohn Dyson 		struct union_node *tun = VTOUNION(ap->a_vp);
12602a31267eSMatthew Dillon 
1261996c772fSJohn Dyson 		if (tun->un_uppervp == NULLVP) {
12622a31267eSMatthew Dillon #if 0
12637be2d300SMike Smith 			if (dun->un_uppervp == tun->un_dirvp) {
12642a31267eSMatthew Dillon 				if (dun->un_flags & UN_ULOCK) {
12657be2d300SMike Smith 					dun->un_flags &= ~UN_ULOCK;
1266b40ce416SJulian Elischer 					VOP_UNLOCK(dun->un_uppervp, 0, td);
1267996c772fSJohn Dyson 				}
12682a31267eSMatthew Dillon 			}
12692a31267eSMatthew Dillon #endif
1270b40ce416SJulian Elischer 			error = union_copyup(tun, 1, cnp->cn_cred, td);
12712a31267eSMatthew Dillon #if 0
12727be2d300SMike Smith 			if (dun->un_uppervp == tun->un_dirvp) {
12737be2d300SMike Smith 				vn_lock(dun->un_uppervp,
1274b40ce416SJulian Elischer 					    LK_EXCLUSIVE | LK_RETRY, td);
12757be2d300SMike Smith 				dun->un_flags |= UN_ULOCK;
1276996c772fSJohn Dyson 			}
12772a31267eSMatthew Dillon #endif
12787be2d300SMike Smith 			if (error)
1279df8bae1dSRodney W. Grimes 				return (error);
1280fa288043SDon Lewis 		}
1281fa288043SDon Lewis 		vp = tun->un_uppervp;
1282fa288043SDon Lewis 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1283fa288043SDon Lewis 	}
1284df8bae1dSRodney W. Grimes 
12852a31267eSMatthew Dillon 	/*
12862a31267eSMatthew Dillon 	 * Make sure upper is locked, then unlock the union directory we were
12871c4ccf09SDon Lewis 	 * called with to avoid a deadlock while we are calling VOP_LINK() on
12882a31267eSMatthew Dillon 	 * the upper (with tdvp locked and vp not locked).  Our ap->a_tdvp
12892a31267eSMatthew Dillon 	 * is expected to be locked on return.
12902a31267eSMatthew Dillon 	 */
1291996c772fSJohn Dyson 
1292b40ce416SJulian Elischer 	if ((tdvp = union_lock_upper(dun, td)) == NULLVP)
12932a31267eSMatthew Dillon 		return (EROFS);
12947be2d300SMike Smith 
1295b40ce416SJulian Elischer 	VOP_UNLOCK(ap->a_tdvp, 0, td);		/* unlock calling node */
12962a31267eSMatthew Dillon 	error = VOP_LINK(tdvp, vp, cnp);	/* call link on upper */
12977be2d300SMike Smith 
12982a31267eSMatthew Dillon 	/*
1299fa288043SDon Lewis 	 * Unlock tun->un_uppervp if we locked it above.
13002a31267eSMatthew Dillon 	 */
1301fa288043SDon Lewis 	if (ap->a_tdvp->v_op == ap->a_vp->v_op)
1302fa288043SDon Lewis 		VOP_UNLOCK(vp, 0, td);
1303fa288043SDon Lewis 	/*
1304fa288043SDon Lewis 	 * We have to unlock tdvp prior to relocking our calling node in
1305fa288043SDon Lewis 	 * order to avoid a deadlock.  We also have to unlock ap->a_vp
1306fa288043SDon Lewis 	 * before relocking the directory, but then we have to relock
1307fa288043SDon Lewis 	 * ap->a_vp as our caller expects.
1308fa288043SDon Lewis 	 */
1309fa288043SDon Lewis 	VOP_UNLOCK(ap->a_vp, 0, td);
1310b40ce416SJulian Elischer 	union_unlock_upper(tdvp, td);
1311b40ce416SJulian Elischer 	vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, td);
1312fa288043SDon Lewis 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
13137be2d300SMike Smith 	return (error);
1314996c772fSJohn Dyson }
1315996c772fSJohn Dyson 
1316c9bf0111SKATO Takenori static int
1317df8bae1dSRodney W. Grimes union_rename(ap)
1318df8bae1dSRodney W. Grimes 	struct vop_rename_args  /* {
1319df8bae1dSRodney W. Grimes 		struct vnode *a_fdvp;
1320df8bae1dSRodney W. Grimes 		struct vnode *a_fvp;
1321df8bae1dSRodney W. Grimes 		struct componentname *a_fcnp;
1322df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
1323df8bae1dSRodney W. Grimes 		struct vnode *a_tvp;
1324df8bae1dSRodney W. Grimes 		struct componentname *a_tcnp;
1325df8bae1dSRodney W. Grimes 	} */ *ap;
1326df8bae1dSRodney W. Grimes {
1327df8bae1dSRodney W. Grimes 	int error;
1328df8bae1dSRodney W. Grimes 	struct vnode *fdvp = ap->a_fdvp;
1329df8bae1dSRodney W. Grimes 	struct vnode *fvp = ap->a_fvp;
1330df8bae1dSRodney W. Grimes 	struct vnode *tdvp = ap->a_tdvp;
1331df8bae1dSRodney W. Grimes 	struct vnode *tvp = ap->a_tvp;
1332df8bae1dSRodney W. Grimes 
13332a31267eSMatthew Dillon 	/*
13342a31267eSMatthew Dillon 	 * Figure out what fdvp to pass to our upper or lower vnode.  If we
13352a31267eSMatthew Dillon 	 * replace the fdvp, release the original one and ref the new one.
13362a31267eSMatthew Dillon 	 */
13372a31267eSMatthew Dillon 
1338df8bae1dSRodney W. Grimes 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
1339df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fdvp);
1340df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1341996c772fSJohn Dyson 			/*
1342996c772fSJohn Dyson 			 * this should never happen in normal
1343996c772fSJohn Dyson 			 * operation but might if there was
1344996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1345996c772fSJohn Dyson 			 * directory.
1346996c772fSJohn Dyson 			 */
1347996c772fSJohn Dyson 			error = EXDEV;
1348df8bae1dSRodney W. Grimes 			goto bad;
1349df8bae1dSRodney W. Grimes 		}
1350df8bae1dSRodney W. Grimes 		fdvp = un->un_uppervp;
1351df8bae1dSRodney W. Grimes 		VREF(fdvp);
1352df8bae1dSRodney W. Grimes 		vrele(ap->a_fdvp);
1353df8bae1dSRodney W. Grimes 	}
1354df8bae1dSRodney W. Grimes 
13552a31267eSMatthew Dillon 	/*
13562a31267eSMatthew Dillon 	 * Figure out what fvp to pass to our upper or lower vnode.  If we
13572a31267eSMatthew Dillon 	 * replace the fvp, release the original one and ref the new one.
13582a31267eSMatthew Dillon 	 */
13592a31267eSMatthew Dillon 
1360df8bae1dSRodney W. Grimes 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
1361df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fvp);
13622a31267eSMatthew Dillon #if 0
13632a31267eSMatthew Dillon 		struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
13642a31267eSMatthew Dillon #endif
13652a31267eSMatthew Dillon 
1366df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
13672a31267eSMatthew Dillon 			switch(fvp->v_type) {
13682a31267eSMatthew Dillon 			case VREG:
1369b40ce416SJulian Elischer 				vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread);
1370b40ce416SJulian Elischer 				error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_thread);
1371b40ce416SJulian Elischer 				VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_thread);
13722a31267eSMatthew Dillon 				if (error)
13732a31267eSMatthew Dillon 					goto bad;
13742a31267eSMatthew Dillon 				break;
13752a31267eSMatthew Dillon 			case VDIR:
13762a31267eSMatthew Dillon 				/*
13772a31267eSMatthew Dillon 				 * XXX not yet.
13782a31267eSMatthew Dillon 				 *
13792a31267eSMatthew Dillon 				 * There is only one way to rename a directory
13802a31267eSMatthew Dillon 				 * based in the lowervp, and that is to copy
13812a31267eSMatthew Dillon 				 * the entire directory hierarchy.  Otherwise
13822a31267eSMatthew Dillon 				 * it would not last across a reboot.
13832a31267eSMatthew Dillon 				 */
13842a31267eSMatthew Dillon #if 0
13852a31267eSMatthew Dillon 				vrele(fvp);
13862a31267eSMatthew Dillon 				fvp = NULL;
1387b40ce416SJulian Elischer 				vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread);
13882a31267eSMatthew Dillon 				error = union_mkshadow(um, fdvp,
13892a31267eSMatthew Dillon 					    ap->a_fcnp, &un->un_uppervp);
1390b40ce416SJulian Elischer 				VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_thread);
13912a31267eSMatthew Dillon 				if (un->un_uppervp)
1392b40ce416SJulian Elischer 					VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_thread);
13932a31267eSMatthew Dillon 				if (error)
13942a31267eSMatthew Dillon 					goto bad;
13952a31267eSMatthew Dillon 				break;
13962a31267eSMatthew Dillon #endif
13972a31267eSMatthew Dillon 			default:
1398996c772fSJohn Dyson 				error = EXDEV;
1399df8bae1dSRodney W. Grimes 				goto bad;
1400df8bae1dSRodney W. Grimes 			}
14012a31267eSMatthew Dillon 		}
1402df8bae1dSRodney W. Grimes 
1403996c772fSJohn Dyson 		if (un->un_lowervp != NULLVP)
1404996c772fSJohn Dyson 			ap->a_fcnp->cn_flags |= DOWHITEOUT;
1405df8bae1dSRodney W. Grimes 		fvp = un->un_uppervp;
1406df8bae1dSRodney W. Grimes 		VREF(fvp);
1407df8bae1dSRodney W. Grimes 		vrele(ap->a_fvp);
1408df8bae1dSRodney W. Grimes 	}
1409df8bae1dSRodney W. Grimes 
14102a31267eSMatthew Dillon 	/*
14112a31267eSMatthew Dillon 	 * Figure out what tdvp (destination directory) to pass to the
14122a31267eSMatthew Dillon 	 * lower level.  If we replace it with uppervp, we need to vput the
14132a31267eSMatthew Dillon 	 * old one.  The exclusive lock is transfered to what we will pass
14141c4ccf09SDon Lewis 	 * down in the VOP_RENAME() and we replace uppervp with a simple
14152a31267eSMatthew Dillon 	 * reference.
14162a31267eSMatthew Dillon 	 */
14172a31267eSMatthew Dillon 
1418df8bae1dSRodney W. Grimes 	if (tdvp->v_op == union_vnodeop_p) {
1419df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tdvp);
14202a31267eSMatthew Dillon 
1421df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1422996c772fSJohn Dyson 			/*
14231c4ccf09SDon Lewis 			 * This should never happen in normal
1424996c772fSJohn Dyson 			 * operation but might if there was
1425996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1426996c772fSJohn Dyson 			 * directory.
1427996c772fSJohn Dyson 			 */
1428996c772fSJohn Dyson 			error = EXDEV;
1429df8bae1dSRodney W. Grimes 			goto bad;
1430df8bae1dSRodney W. Grimes 		}
1431df8bae1dSRodney W. Grimes 
14322a31267eSMatthew Dillon 		/*
14331c4ccf09SDon Lewis 		 * New tdvp is a lock and reference on uppervp.
14341c4ccf09SDon Lewis 		 * Put away the old tdvp.
14352a31267eSMatthew Dillon 		 */
1436b40ce416SJulian Elischer 		tdvp = union_lock_upper(un, ap->a_tcnp->cn_thread);
1437df8bae1dSRodney W. Grimes 		vput(ap->a_tdvp);
1438df8bae1dSRodney W. Grimes 	}
1439df8bae1dSRodney W. Grimes 
14402a31267eSMatthew Dillon 	/*
14412a31267eSMatthew Dillon 	 * Figure out what tvp (destination file) to pass to the
14422a31267eSMatthew Dillon 	 * lower level.
14432a31267eSMatthew Dillon 	 *
14441c4ccf09SDon Lewis 	 * If the uppervp file does not exist, put away the (wrong)
14452a31267eSMatthew Dillon 	 * file and change tvp to NULL.
14462a31267eSMatthew Dillon 	 */
14472a31267eSMatthew Dillon 
1448996c772fSJohn Dyson 	if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1449df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tvp);
1450df8bae1dSRodney W. Grimes 
1451b40ce416SJulian Elischer 		tvp = union_lock_upper(un, ap->a_tcnp->cn_thread);
1452df8bae1dSRodney W. Grimes 		vput(ap->a_tvp);
14532a31267eSMatthew Dillon 		/* note: tvp may be NULL */
1454df8bae1dSRodney W. Grimes 	}
1455df8bae1dSRodney W. Grimes 
14562a31267eSMatthew Dillon 	/*
14571c4ccf09SDon Lewis 	 * VOP_RENAME() releases/vputs prior to returning, so we have no
14582a31267eSMatthew Dillon 	 * cleanup to do.
14592a31267eSMatthew Dillon 	 */
14602a31267eSMatthew Dillon 
1461df8bae1dSRodney W. Grimes 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1462df8bae1dSRodney W. Grimes 
14632a31267eSMatthew Dillon 	/*
14642a31267eSMatthew Dillon 	 * Error.  We still have to release / vput the various elements.
14652a31267eSMatthew Dillon 	 */
14662a31267eSMatthew Dillon 
1467df8bae1dSRodney W. Grimes bad:
1468df8bae1dSRodney W. Grimes 	vrele(fdvp);
14692a31267eSMatthew Dillon 	if (fvp)
1470df8bae1dSRodney W. Grimes 		vrele(fvp);
1471df8bae1dSRodney W. Grimes 	vput(tdvp);
14722a31267eSMatthew Dillon 	if (tvp != NULLVP) {
14732a31267eSMatthew Dillon 		if (tvp != tdvp)
1474df8bae1dSRodney W. Grimes 			vput(tvp);
14752a31267eSMatthew Dillon 		else
14762a31267eSMatthew Dillon 			vrele(tvp);
14772a31267eSMatthew Dillon 	}
1478df8bae1dSRodney W. Grimes 	return (error);
1479df8bae1dSRodney W. Grimes }
1480df8bae1dSRodney W. Grimes 
1481c9bf0111SKATO Takenori static int
1482df8bae1dSRodney W. Grimes union_mkdir(ap)
1483df8bae1dSRodney W. Grimes 	struct vop_mkdir_args /* {
1484df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1485df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1486df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1487df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1488df8bae1dSRodney W. Grimes 	} */ *ap;
1489df8bae1dSRodney W. Grimes {
14907be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
1491996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1492b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
14932a31267eSMatthew Dillon 	struct vnode *upperdvp;
14942a31267eSMatthew Dillon 	int error = EROFS;
1495df8bae1dSRodney W. Grimes 
1496b40ce416SJulian Elischer 	if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) {
1497df8bae1dSRodney W. Grimes 		struct vnode *vp;
1498df8bae1dSRodney W. Grimes 
14992a31267eSMatthew Dillon 		error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap);
1500b40ce416SJulian Elischer 		union_unlock_upper(upperdvp, td);
15012a31267eSMatthew Dillon 
15022a31267eSMatthew Dillon 		if (error == 0) {
1503b40ce416SJulian Elischer 			VOP_UNLOCK(vp, 0, td);
15044d93c0beSJeff Roberson 			UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vrefcnt(vp)));
15052a31267eSMatthew Dillon 			error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
15062a31267eSMatthew Dillon 				ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
15074d93c0beSJeff Roberson 			UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp)));
1508996c772fSJohn Dyson 		}
1509df8bae1dSRodney W. Grimes 	}
15102a31267eSMatthew Dillon 	return (error);
1511df8bae1dSRodney W. Grimes }
1512df8bae1dSRodney W. Grimes 
1513c9bf0111SKATO Takenori static int
1514df8bae1dSRodney W. Grimes union_rmdir(ap)
1515df8bae1dSRodney W. Grimes 	struct vop_rmdir_args /* {
1516df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1517df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1518df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1519df8bae1dSRodney W. Grimes 	} */ *ap;
1520df8bae1dSRodney W. Grimes {
1521df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1522df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1523996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1524b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
15252a31267eSMatthew Dillon 	struct vnode *upperdvp;
15262a31267eSMatthew Dillon 	struct vnode *uppervp;
15277be2d300SMike Smith 	int error;
1528df8bae1dSRodney W. Grimes 
1529b40ce416SJulian Elischer 	if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1530996c772fSJohn Dyson 		panic("union rmdir: null upper vnode");
1531996c772fSJohn Dyson 
1532b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1533b40ce416SJulian Elischer 		if (union_dowhiteout(un, cnp->cn_cred, td))
1534996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
15352a31267eSMatthew Dillon 		error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp);
1536b40ce416SJulian Elischer 		union_unlock_upper(uppervp, td);
1537df8bae1dSRodney W. Grimes 	} else {
1538996c772fSJohn Dyson 		error = union_mkwhiteout(
15392a31267eSMatthew Dillon 			    MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1540996c772fSJohn Dyson 			    dun->un_uppervp, ap->a_cnp, un->un_path);
1541df8bae1dSRodney W. Grimes 	}
1542b40ce416SJulian Elischer 	union_unlock_upper(upperdvp, td);
1543df8bae1dSRodney W. Grimes 	return (error);
1544df8bae1dSRodney W. Grimes }
1545df8bae1dSRodney W. Grimes 
15462a31267eSMatthew Dillon /*
15472a31267eSMatthew Dillon  *	union_symlink:
15482a31267eSMatthew Dillon  *
15492a31267eSMatthew Dillon  *	dvp is locked on entry and remains locked on return.  a_vpp is garbage
15502a31267eSMatthew Dillon  *	(unused).
15512a31267eSMatthew Dillon  */
15522a31267eSMatthew Dillon 
1553c9bf0111SKATO Takenori static int
1554df8bae1dSRodney W. Grimes union_symlink(ap)
1555df8bae1dSRodney W. Grimes 	struct vop_symlink_args /* {
1556df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1557df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1558df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1559df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1560df8bae1dSRodney W. Grimes 		char *a_target;
1561df8bae1dSRodney W. Grimes 	} */ *ap;
1562df8bae1dSRodney W. Grimes {
15637be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
1564996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1565b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
15662a31267eSMatthew Dillon 	struct vnode *dvp;
15672a31267eSMatthew Dillon 	int error = EROFS;
1568df8bae1dSRodney W. Grimes 
1569b40ce416SJulian Elischer 	if ((dvp = union_lock_upper(dun, td)) != NULLVP) {
1570dd8c04f4SEivind Eklund 		error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap,
1571dd8c04f4SEivind Eklund 			    ap->a_target);
1572b40ce416SJulian Elischer 		union_unlock_upper(dvp, td);
1573df8bae1dSRodney W. Grimes 	}
15742a31267eSMatthew Dillon 	return (error);
1575df8bae1dSRodney W. Grimes }
1576df8bae1dSRodney W. Grimes 
1577df8bae1dSRodney W. Grimes /*
15781c4ccf09SDon Lewis  * union_readdir ()works in concert with getdirentries() and
1579df8bae1dSRodney W. Grimes  * readdir(3) to provide a list of entries in the unioned
15801c4ccf09SDon Lewis  * directories.  getdirentries()  is responsible for walking
1581df8bae1dSRodney W. Grimes  * down the union stack.  readdir(3) is responsible for
1582df8bae1dSRodney W. Grimes  * eliminating duplicate names from the returned data stream.
1583df8bae1dSRodney W. Grimes  */
1584c9bf0111SKATO Takenori static int
1585df8bae1dSRodney W. Grimes union_readdir(ap)
1586df8bae1dSRodney W. Grimes 	struct vop_readdir_args /* {
1587df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1588df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1589df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1590996c772fSJohn Dyson 		int *a_eofflag;
1591996c772fSJohn Dyson 		u_long *a_cookies;
1592996c772fSJohn Dyson 		int a_ncookies;
1593df8bae1dSRodney W. Grimes 	} */ *ap;
1594df8bae1dSRodney W. Grimes {
1595df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1596b40ce416SJulian Elischer 	struct thread *td = ap->a_uio->uio_td;
15972a31267eSMatthew Dillon 	struct vnode *uvp;
15982a31267eSMatthew Dillon 	int error = 0;
1599df8bae1dSRodney W. Grimes 
1600b40ce416SJulian Elischer 	if ((uvp = union_lock_upper(un, td)) != NULLVP) {
1601996c772fSJohn Dyson 		ap->a_vp = uvp;
16022a31267eSMatthew Dillon 		error = VCALL(uvp, VOFFSET(vop_readdir), ap);
1603b40ce416SJulian Elischer 		union_unlock_upper(uvp, td);
16042a31267eSMatthew Dillon 	}
16052a31267eSMatthew Dillon 	return(error);
1606df8bae1dSRodney W. Grimes }
1607df8bae1dSRodney W. Grimes 
1608c9bf0111SKATO Takenori static int
1609df8bae1dSRodney W. Grimes union_readlink(ap)
1610df8bae1dSRodney W. Grimes 	struct vop_readlink_args /* {
1611df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1612df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1613df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1614df8bae1dSRodney W. Grimes 	} */ *ap;
1615df8bae1dSRodney W. Grimes {
1616df8bae1dSRodney W. Grimes 	int error;
16172a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1618996c772fSJohn Dyson 	struct uio *uio = ap->a_uio;
1619b40ce416SJulian Elischer 	struct thread *td = uio->uio_td;
16202a31267eSMatthew Dillon 	struct vnode *vp;
1621df8bae1dSRodney W. Grimes 
1622b40ce416SJulian Elischer 	vp = union_lock_other(un, td);
16232a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
16242a31267eSMatthew Dillon 
1625996c772fSJohn Dyson 	ap->a_vp = vp;
1626996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_readlink), ap);
1627b40ce416SJulian Elischer 	union_unlock_other(vp, td);
1628df8bae1dSRodney W. Grimes 
1629df8bae1dSRodney W. Grimes 	return (error);
1630df8bae1dSRodney W. Grimes }
1631df8bae1dSRodney W. Grimes 
1632f2a2857bSKirk McKusick static int
1633f2a2857bSKirk McKusick union_getwritemount(ap)
1634f2a2857bSKirk McKusick 	struct vop_getwritemount_args /* {
1635f2a2857bSKirk McKusick 		struct vnode *a_vp;
1636f2a2857bSKirk McKusick 		struct mount **a_mpp;
1637f2a2857bSKirk McKusick 	} */ *ap;
1638f2a2857bSKirk McKusick {
1639f3d1ec67SBoris Popov 	struct vnode *vp = ap->a_vp;
1640f3d1ec67SBoris Popov 	struct vnode *uvp = UPPERVP(vp);
1641f2a2857bSKirk McKusick 
1642f3d1ec67SBoris Popov 	if (uvp == NULL) {
1643f3d1ec67SBoris Popov 		VI_LOCK(vp);
1644e6e370a7SJeff Roberson 		if (vp->v_iflag & VI_FREE) {
1645f3d1ec67SBoris Popov 			VI_UNLOCK(vp);
1646f3d1ec67SBoris Popov 			return (EOPNOTSUPP);
1647f3d1ec67SBoris Popov 		}
1648f3d1ec67SBoris Popov 		VI_UNLOCK(vp);
164910fa1684SBoris Popov 		return (EACCES);
1650f3d1ec67SBoris Popov 	}
1651f3d1ec67SBoris Popov 	return(VOP_GETWRITEMOUNT(uvp, ap->a_mpp));
1652f2a2857bSKirk McKusick }
1653f2a2857bSKirk McKusick 
16542a31267eSMatthew Dillon /*
16552a31267eSMatthew Dillon  *	union_inactive:
16562a31267eSMatthew Dillon  *
16572a31267eSMatthew Dillon  *	Called with the vnode locked.  We are expected to unlock the vnode.
16582a31267eSMatthew Dillon  */
16592a31267eSMatthew Dillon 
1660c9bf0111SKATO Takenori static int
1661df8bae1dSRodney W. Grimes union_inactive(ap)
1662df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
1663df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1664b40ce416SJulian Elischer 		struct thread *a_td;
1665df8bae1dSRodney W. Grimes 	} */ *ap;
1666df8bae1dSRodney W. Grimes {
1667996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1668b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
1669996c772fSJohn Dyson 	struct union_node *un = VTOUNION(vp);
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes 	/*
1672df8bae1dSRodney W. Grimes 	 * Do nothing (and _don't_ bypass).
1673df8bae1dSRodney W. Grimes 	 * Wait to vrele lowervp until reclaim,
1674df8bae1dSRodney W. Grimes 	 * so that until then our union_node is in the
1675df8bae1dSRodney W. Grimes 	 * cache and reusable.
1676df8bae1dSRodney W. Grimes 	 *
1677df8bae1dSRodney W. Grimes 	 */
1678df8bae1dSRodney W. Grimes 
1679d8c6e674SDavid Schultz 	if (un->un_dircache != NULL)
1680d8c6e674SDavid Schultz 		union_dircache_free(un);
1681df8bae1dSRodney W. Grimes 
16822a31267eSMatthew Dillon #if 0
16832a31267eSMatthew Dillon 	if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
16842a31267eSMatthew Dillon 		un->un_flags &= ~UN_ULOCK;
1685b40ce416SJulian Elischer 		VOP_UNLOCK(un->un_uppervp, 0, td);
16862a31267eSMatthew Dillon 	}
16872a31267eSMatthew Dillon #endif
16882a31267eSMatthew Dillon 
1689b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
1690996c772fSJohn Dyson 
1691996c772fSJohn Dyson 	if ((un->un_flags & UN_CACHED) == 0)
1692996c772fSJohn Dyson 		vgone(vp);
1693df8bae1dSRodney W. Grimes 
1694df8bae1dSRodney W. Grimes 	return (0);
1695df8bae1dSRodney W. Grimes }
1696df8bae1dSRodney W. Grimes 
1697c9bf0111SKATO Takenori static int
1698df8bae1dSRodney W. Grimes union_reclaim(ap)
1699df8bae1dSRodney W. Grimes 	struct vop_reclaim_args /* {
1700df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1701df8bae1dSRodney W. Grimes 	} */ *ap;
1702df8bae1dSRodney W. Grimes {
1703df8bae1dSRodney W. Grimes 	union_freevp(ap->a_vp);
1704df8bae1dSRodney W. Grimes 
1705df8bae1dSRodney W. Grimes 	return (0);
1706df8bae1dSRodney W. Grimes }
1707df8bae1dSRodney W. Grimes 
17083413421bSBoris Popov /*
17093413421bSBoris Popov  * unionvp do not hold a VM object and there is no need to create one for
17103413421bSBoris Popov  * upper or lower vp because it is done in the union_open()
17113413421bSBoris Popov  */
17123413421bSBoris Popov static int
17133413421bSBoris Popov union_createvobject(ap)
17143413421bSBoris Popov 	struct vop_createvobject_args /* {
17153413421bSBoris Popov 		struct vnode *vp;
17163413421bSBoris Popov 		struct ucred *cred;
1717b40ce416SJulian Elischer 		struct thread *td;
17183413421bSBoris Popov 	} */ *ap;
17193413421bSBoris Popov {
17203413421bSBoris Popov 	struct vnode *vp = ap->a_vp;
17213413421bSBoris Popov 
1722e6e370a7SJeff Roberson 	vp->v_vflag |= VV_OBJBUF;
17233413421bSBoris Popov 	return (0);
17243413421bSBoris Popov }
17253413421bSBoris Popov 
17263413421bSBoris Popov /*
17273413421bSBoris Popov  * We have nothing to destroy and this operation shouldn't be bypassed.
17283413421bSBoris Popov  */
17293413421bSBoris Popov static int
17303413421bSBoris Popov union_destroyvobject(ap)
17313413421bSBoris Popov 	struct vop_destroyvobject_args /* {
17323413421bSBoris Popov 		struct vnode *vp;
17333413421bSBoris Popov 	} */ *ap;
17343413421bSBoris Popov {
17353413421bSBoris Popov 	struct vnode *vp = ap->a_vp;
17363413421bSBoris Popov 
1737e6e370a7SJeff Roberson 	vp->v_vflag &= ~VV_OBJBUF;
17383413421bSBoris Popov 	return (0);
17393413421bSBoris Popov }
17403413421bSBoris Popov 
17413413421bSBoris Popov /*
17423413421bSBoris Popov  * Get VM object from the upper or lower vp
17433413421bSBoris Popov  */
17443413421bSBoris Popov static int
17453413421bSBoris Popov union_getvobject(ap)
17463413421bSBoris Popov 	struct vop_getvobject_args /* {
17473413421bSBoris Popov 		struct vnode *vp;
17483413421bSBoris Popov 		struct vm_object **objpp;
17493413421bSBoris Popov 	} */ *ap;
17503413421bSBoris Popov {
17513413421bSBoris Popov 	struct vnode *ovp = OTHERVP(ap->a_vp);
17523413421bSBoris Popov 
17533413421bSBoris Popov 	if (ovp == NULL)
17543413421bSBoris Popov 		return EINVAL;
17553413421bSBoris Popov 	return (VOP_GETVOBJECT(ovp, ap->a_objpp));
17563413421bSBoris Popov }
17573413421bSBoris Popov 
1758c9bf0111SKATO Takenori static int
1759df8bae1dSRodney W. Grimes union_print(ap)
1760df8bae1dSRodney W. Grimes 	struct vop_print_args /* {
1761df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1762df8bae1dSRodney W. Grimes 	} */ *ap;
1763df8bae1dSRodney W. Grimes {
1764df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1765df8bae1dSRodney W. Grimes 
176699648386SNate Lawson 	printf("\tvp=%p, uppervp=%p, lowervp=%p\n",
1767df8bae1dSRodney W. Grimes 	       vp, UPPERVP(vp), LOWERVP(vp));
1768996c772fSJohn Dyson 	if (UPPERVP(vp) != NULLVP)
1769996c772fSJohn Dyson 		vprint("union: upper", UPPERVP(vp));
1770996c772fSJohn Dyson 	if (LOWERVP(vp) != NULLVP)
1771996c772fSJohn Dyson 		vprint("union: lower", LOWERVP(vp));
1772996c772fSJohn Dyson 
1773df8bae1dSRodney W. Grimes 	return (0);
1774df8bae1dSRodney W. Grimes }
1775df8bae1dSRodney W. Grimes 
1776c9bf0111SKATO Takenori static int
1777df8bae1dSRodney W. Grimes union_pathconf(ap)
1778df8bae1dSRodney W. Grimes 	struct vop_pathconf_args /* {
1779df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1780df8bae1dSRodney W. Grimes 		int a_name;
1781df8bae1dSRodney W. Grimes 		int *a_retval;
1782df8bae1dSRodney W. Grimes 	} */ *ap;
1783df8bae1dSRodney W. Grimes {
1784df8bae1dSRodney W. Grimes 	int error;
1785b40ce416SJulian Elischer 	struct thread *td = curthread;		/* XXX */
17862a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
17872a31267eSMatthew Dillon 	struct vnode *vp;
1788df8bae1dSRodney W. Grimes 
1789b40ce416SJulian Elischer 	vp = union_lock_other(un, td);
17902a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
17912a31267eSMatthew Dillon 
1792996c772fSJohn Dyson 	ap->a_vp = vp;
1793996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_pathconf), ap);
1794b40ce416SJulian Elischer 	union_unlock_other(vp, td);
1795df8bae1dSRodney W. Grimes 
1796df8bae1dSRodney W. Grimes 	return (error);
1797df8bae1dSRodney W. Grimes }
1798df8bae1dSRodney W. Grimes 
1799c9bf0111SKATO Takenori static int
1800df8bae1dSRodney W. Grimes union_advlock(ap)
1801df8bae1dSRodney W. Grimes 	struct vop_advlock_args /* {
1802df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1803df8bae1dSRodney W. Grimes 		caddr_t  a_id;
1804df8bae1dSRodney W. Grimes 		int  a_op;
1805df8bae1dSRodney W. Grimes 		struct flock *a_fl;
1806df8bae1dSRodney W. Grimes 		int  a_flags;
1807df8bae1dSRodney W. Grimes 	} */ *ap;
1808df8bae1dSRodney W. Grimes {
1809996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1810df8bae1dSRodney W. Grimes 
1811996c772fSJohn Dyson 	ap->a_vp = ovp;
1812996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1813df8bae1dSRodney W. Grimes }
1814df8bae1dSRodney W. Grimes 
1815df8bae1dSRodney W. Grimes 
1816df8bae1dSRodney W. Grimes /*
1817df8bae1dSRodney W. Grimes  * XXX - vop_strategy must be hand coded because it has no
18182a31267eSMatthew Dillon  * YYY - and it is not coherent with anything
18192a31267eSMatthew Dillon  *
1820df8bae1dSRodney W. Grimes  * vnode in its arguments.
1821df8bae1dSRodney W. Grimes  * This goes away with a merged VM/buffer cache.
1822df8bae1dSRodney W. Grimes  */
1823c9bf0111SKATO Takenori static int
1824df8bae1dSRodney W. Grimes union_strategy(ap)
1825df8bae1dSRodney W. Grimes 	struct vop_strategy_args /* {
1826fd5d1124SJulian Elischer 		struct vnode *a_vp;
1827df8bae1dSRodney W. Grimes 		struct buf *a_bp;
1828df8bae1dSRodney W. Grimes 	} */ *ap;
1829df8bae1dSRodney W. Grimes {
1830df8bae1dSRodney W. Grimes 	struct buf *bp = ap->a_bp;
1831f9c8cab5SKirk McKusick 	struct vnode *othervp = OTHERVP(bp->b_vp);
1832df8bae1dSRodney W. Grimes 
1833cefb5754SPoul-Henning Kamp 	KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)",
1834cefb5754SPoul-Henning Kamp 	    __func__, ap->a_vp, ap->a_bp->b_vp));
1835df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1836f9c8cab5SKirk McKusick 	if (othervp == NULLVP)
1837df8bae1dSRodney W. Grimes 		panic("union_strategy: nil vp");
183821144e3bSPoul-Henning Kamp 	if ((bp->b_iocmd == BIO_WRITE) &&
1839f9c8cab5SKirk McKusick 	    (othervp == LOWERVP(bp->b_vp)))
1840df8bae1dSRodney W. Grimes 		panic("union_strategy: writing to lowervp");
1841df8bae1dSRodney W. Grimes #endif
1842f9c8cab5SKirk McKusick 	return (VOP_STRATEGY(othervp, bp));
1843df8bae1dSRodney W. Grimes }
1844df8bae1dSRodney W. Grimes 
1845df8bae1dSRodney W. Grimes /*
1846df8bae1dSRodney W. Grimes  * Global vfs data structures
1847df8bae1dSRodney W. Grimes  */
1848f57e6547SBruce Evans vop_t **union_vnodeop_p;
1849c109c577SBruce Evans static struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1850dba3870cSPoul-Henning Kamp 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1851539ef70cSPoul-Henning Kamp 	{ &vop_access_desc,		(vop_t *) union_access },
1852539ef70cSPoul-Henning Kamp 	{ &vop_advlock_desc,		(vop_t *) union_advlock },
1853b7ebffbcSPoul-Henning Kamp 	{ &vop_bmap_desc,		(vop_t *) vop_eopnotsupp },
1854539ef70cSPoul-Henning Kamp 	{ &vop_close_desc,		(vop_t *) union_close },
1855539ef70cSPoul-Henning Kamp 	{ &vop_create_desc,		(vop_t *) union_create },
18563413421bSBoris Popov 	{ &vop_createvobject_desc,	(vop_t *) union_createvobject },
18573413421bSBoris Popov 	{ &vop_destroyvobject_desc,	(vop_t *) union_destroyvobject },
1858539ef70cSPoul-Henning Kamp 	{ &vop_fsync_desc,		(vop_t *) union_fsync },
1859539ef70cSPoul-Henning Kamp 	{ &vop_getattr_desc,		(vop_t *) union_getattr },
18603413421bSBoris Popov 	{ &vop_getvobject_desc,		(vop_t *) union_getvobject },
1861539ef70cSPoul-Henning Kamp 	{ &vop_inactive_desc,		(vop_t *) union_inactive },
1862539ef70cSPoul-Henning Kamp 	{ &vop_ioctl_desc,		(vop_t *) union_ioctl },
1863539ef70cSPoul-Henning Kamp 	{ &vop_lease_desc,		(vop_t *) union_lease },
1864539ef70cSPoul-Henning Kamp 	{ &vop_link_desc,		(vop_t *) union_link },
1865539ef70cSPoul-Henning Kamp 	{ &vop_lookup_desc,		(vop_t *) union_lookup },
1866539ef70cSPoul-Henning Kamp 	{ &vop_mkdir_desc,		(vop_t *) union_mkdir },
1867539ef70cSPoul-Henning Kamp 	{ &vop_mknod_desc,		(vop_t *) union_mknod },
1868539ef70cSPoul-Henning Kamp 	{ &vop_open_desc,		(vop_t *) union_open },
1869539ef70cSPoul-Henning Kamp 	{ &vop_pathconf_desc,		(vop_t *) union_pathconf },
1870539ef70cSPoul-Henning Kamp 	{ &vop_poll_desc,		(vop_t *) union_poll },
1871539ef70cSPoul-Henning Kamp 	{ &vop_print_desc,		(vop_t *) union_print },
1872539ef70cSPoul-Henning Kamp 	{ &vop_read_desc,		(vop_t *) union_read },
1873539ef70cSPoul-Henning Kamp 	{ &vop_readdir_desc,		(vop_t *) union_readdir },
1874539ef70cSPoul-Henning Kamp 	{ &vop_readlink_desc,		(vop_t *) union_readlink },
1875f2a2857bSKirk McKusick 	{ &vop_getwritemount_desc,	(vop_t *) union_getwritemount },
1876539ef70cSPoul-Henning Kamp 	{ &vop_reclaim_desc,		(vop_t *) union_reclaim },
1877539ef70cSPoul-Henning Kamp 	{ &vop_remove_desc,		(vop_t *) union_remove },
1878539ef70cSPoul-Henning Kamp 	{ &vop_rename_desc,		(vop_t *) union_rename },
1879539ef70cSPoul-Henning Kamp 	{ &vop_revoke_desc,		(vop_t *) union_revoke },
1880539ef70cSPoul-Henning Kamp 	{ &vop_rmdir_desc,		(vop_t *) union_rmdir },
1881539ef70cSPoul-Henning Kamp 	{ &vop_setattr_desc,		(vop_t *) union_setattr },
1882539ef70cSPoul-Henning Kamp 	{ &vop_strategy_desc,		(vop_t *) union_strategy },
1883539ef70cSPoul-Henning Kamp 	{ &vop_symlink_desc,		(vop_t *) union_symlink },
1884539ef70cSPoul-Henning Kamp 	{ &vop_whiteout_desc,		(vop_t *) union_whiteout },
1885539ef70cSPoul-Henning Kamp 	{ &vop_write_desc,		(vop_t *) union_write },
1886f57e6547SBruce Evans 	{ NULL, NULL }
1887df8bae1dSRodney W. Grimes };
1888c109c577SBruce Evans static struct vnodeopv_desc union_vnodeop_opv_desc =
1889df8bae1dSRodney W. Grimes 	{ &union_vnodeop_p, union_vnodeop_entries };
1890c901836cSGarrett Wollman 
1891c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc);
1892