xref: /freebsd/sys/fs/unionfs/union_vnops.c (revision 06be2aaa8320ba839a837d20e35f35c3efdf7587)
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_lock(struct vop_lock_args *ap);
8411caded3SAlfred Perlstein static int	union_lookup(struct vop_lookup_args *ap);
8511caded3SAlfred Perlstein static int	union_lookup1(struct vnode *udvp, struct vnode **dvp,
869b5e8b3aSBruce Evans 				   struct vnode **vpp,
8711caded3SAlfred Perlstein 				   struct componentname *cnp);
8811caded3SAlfred Perlstein static int	union_mkdir(struct vop_mkdir_args *ap);
8911caded3SAlfred Perlstein static int	union_mknod(struct vop_mknod_args *ap);
9011caded3SAlfred Perlstein static int	union_open(struct vop_open_args *ap);
9111caded3SAlfred Perlstein static int	union_pathconf(struct vop_pathconf_args *ap);
9211caded3SAlfred Perlstein static int	union_print(struct vop_print_args *ap);
9311caded3SAlfred Perlstein static int	union_read(struct vop_read_args *ap);
9411caded3SAlfred Perlstein static int	union_readdir(struct vop_readdir_args *ap);
9511caded3SAlfred Perlstein static int	union_readlink(struct vop_readlink_args *ap);
9611caded3SAlfred Perlstein static int	union_getwritemount(struct vop_getwritemount_args *ap);
9711caded3SAlfred Perlstein static int	union_reclaim(struct vop_reclaim_args *ap);
9811caded3SAlfred Perlstein static int	union_remove(struct vop_remove_args *ap);
9911caded3SAlfred Perlstein static int	union_rename(struct vop_rename_args *ap);
10011caded3SAlfred Perlstein static int	union_revoke(struct vop_revoke_args *ap);
10111caded3SAlfred Perlstein static int	union_rmdir(struct vop_rmdir_args *ap);
10211caded3SAlfred Perlstein static int	union_poll(struct vop_poll_args *ap);
10311caded3SAlfred Perlstein static int	union_setattr(struct vop_setattr_args *ap);
10411caded3SAlfred Perlstein static int	union_strategy(struct vop_strategy_args *ap);
10511caded3SAlfred Perlstein static int	union_symlink(struct vop_symlink_args *ap);
10611caded3SAlfred Perlstein static int	union_unlock(struct vop_unlock_args *ap);
10711caded3SAlfred Perlstein static int	union_whiteout(struct vop_whiteout_args *ap);
10811caded3SAlfred Perlstein static int	union_write(struct vop_read_args *ap);
1099b5e8b3aSBruce Evans 
1102a31267eSMatthew Dillon static __inline
1112a31267eSMatthew Dillon struct vnode *
112b40ce416SJulian Elischer union_lock_upper(struct union_node *un, struct thread *td)
113df8bae1dSRodney W. Grimes {
1142a31267eSMatthew Dillon 	struct vnode *uppervp;
115df8bae1dSRodney W. Grimes 
1162a31267eSMatthew Dillon 	if ((uppervp = un->un_uppervp) != NULL) {
1172a31267eSMatthew Dillon 		VREF(uppervp);
118b40ce416SJulian Elischer 		vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
1192a31267eSMatthew Dillon 	}
1202a31267eSMatthew Dillon 	KASSERT((uppervp == NULL || uppervp->v_usecount > 0), ("uppervp usecount is 0"));
1212a31267eSMatthew Dillon 	return(uppervp);
122df8bae1dSRodney W. Grimes }
123df8bae1dSRodney W. Grimes 
1242a31267eSMatthew Dillon static __inline
1252a31267eSMatthew Dillon void
126b40ce416SJulian Elischer union_unlock_upper(struct vnode *uppervp, struct thread *td)
1272a31267eSMatthew Dillon {
1282a31267eSMatthew Dillon 	vput(uppervp);
1292a31267eSMatthew Dillon }
1302a31267eSMatthew Dillon 
1312a31267eSMatthew Dillon static __inline
1322a31267eSMatthew Dillon struct vnode *
133b40ce416SJulian Elischer union_lock_other(struct union_node *un, struct thread *td)
1342a31267eSMatthew Dillon {
1352a31267eSMatthew Dillon 	struct vnode *vp;
1362a31267eSMatthew Dillon 
1372a31267eSMatthew Dillon 	if (un->un_uppervp != NULL) {
138b40ce416SJulian Elischer 		vp = union_lock_upper(un, td);
1392a31267eSMatthew Dillon 	} else if ((vp = un->un_lowervp) != NULL) {
1402a31267eSMatthew Dillon 		VREF(vp);
141b40ce416SJulian Elischer 		vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
1422a31267eSMatthew Dillon 	}
1432a31267eSMatthew Dillon 	return(vp);
1442a31267eSMatthew Dillon }
1452a31267eSMatthew Dillon 
1462a31267eSMatthew Dillon static __inline
1472a31267eSMatthew Dillon void
148b40ce416SJulian Elischer union_unlock_other(struct vnode *vp, struct thread *td)
1492a31267eSMatthew Dillon {
1502a31267eSMatthew Dillon 	vput(vp);
1512a31267eSMatthew Dillon }
1522a31267eSMatthew Dillon 
1532a31267eSMatthew Dillon /*
1542a31267eSMatthew Dillon  *	union_lookup:
1552a31267eSMatthew Dillon  *
1562a31267eSMatthew Dillon  *	udvp	must be exclusively locked on call and will remain
1572a31267eSMatthew Dillon  *		exclusively locked on return.  This is the mount point
1582a31267eSMatthew Dillon  *		for out filesystem.
1592a31267eSMatthew Dillon  *
1602a31267eSMatthew Dillon  *	dvp	Our base directory, locked and referenced.
1612a31267eSMatthew Dillon  *		The passed dvp will be dereferenced and unlocked on return
1622a31267eSMatthew Dillon  *		and a new dvp will be returned which is locked and
1632a31267eSMatthew Dillon  *		referenced in the same variable.
1642a31267eSMatthew Dillon  *
1652a31267eSMatthew Dillon  *	vpp	is filled in with the result if no error occured,
1662a31267eSMatthew Dillon  *		locked and ref'd.
1672a31267eSMatthew Dillon  *
1682a31267eSMatthew Dillon  *		If an error is returned, *vpp is set to NULLVP.  If no
1692a31267eSMatthew Dillon  *		error occurs, *vpp is returned with a reference and an
1702a31267eSMatthew Dillon  *		exclusive lock.
1712a31267eSMatthew Dillon  */
1722a31267eSMatthew Dillon 
173df8bae1dSRodney W. Grimes static int
1742a31267eSMatthew Dillon union_lookup1(udvp, pdvp, vpp, cnp)
175df8bae1dSRodney W. Grimes 	struct vnode *udvp;
1762a31267eSMatthew Dillon 	struct vnode **pdvp;
177df8bae1dSRodney W. Grimes 	struct vnode **vpp;
178df8bae1dSRodney W. Grimes 	struct componentname *cnp;
179df8bae1dSRodney W. Grimes {
180df8bae1dSRodney W. Grimes 	int error;
181b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
1822a31267eSMatthew Dillon 	struct vnode *dvp = *pdvp;
183df8bae1dSRodney W. Grimes 	struct vnode *tdvp;
184df8bae1dSRodney W. Grimes 	struct mount *mp;
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes 	/*
187df8bae1dSRodney W. Grimes 	 * If stepping up the directory tree, check for going
188df8bae1dSRodney W. Grimes 	 * back across the mount point, in which case do what
189df8bae1dSRodney W. Grimes 	 * lookup would do by stepping back down the mount
190df8bae1dSRodney W. Grimes 	 * hierarchy.
191df8bae1dSRodney W. Grimes 	 */
192df8bae1dSRodney W. Grimes 	if (cnp->cn_flags & ISDOTDOT) {
193e6e370a7SJeff Roberson 		while ((dvp != udvp) && (dvp->v_vflag & VV_ROOT)) {
194df8bae1dSRodney W. Grimes 			/*
195df8bae1dSRodney W. Grimes 			 * Don't do the NOCROSSMOUNT check
196df8bae1dSRodney W. Grimes 			 * at this level.  By definition,
197df8bae1dSRodney W. Grimes 			 * union fs deals with namespaces, not
198df8bae1dSRodney W. Grimes 			 * filesystems.
199df8bae1dSRodney W. Grimes 			 */
200df8bae1dSRodney W. Grimes 			tdvp = dvp;
2012a31267eSMatthew Dillon 			dvp = dvp->v_mount->mnt_vnodecovered;
202df8bae1dSRodney W. Grimes 			VREF(dvp);
2032a31267eSMatthew Dillon 			vput(tdvp);
204b40ce416SJulian Elischer 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
205df8bae1dSRodney W. Grimes 		}
206df8bae1dSRodney W. Grimes 	}
207df8bae1dSRodney W. Grimes 
2082a31267eSMatthew Dillon 	/*
2092a31267eSMatthew Dillon 	 * Set return dvp to be the upperdvp 'parent directory.
2102a31267eSMatthew Dillon 	 */
2112a31267eSMatthew Dillon 	*pdvp = dvp;
2122a31267eSMatthew Dillon 
2132a31267eSMatthew Dillon 	/*
2142a31267eSMatthew Dillon 	 * If the VOP_LOOKUP call generates an error, tdvp is invalid and no
2152a31267eSMatthew Dillon 	 * changes will have been made to dvp, so we are set to return.
2162a31267eSMatthew Dillon 	 */
2172a31267eSMatthew Dillon 
218df8bae1dSRodney W. Grimes         error = VOP_LOOKUP(dvp, &tdvp, cnp);
2192a31267eSMatthew Dillon 	if (error) {
2202a31267eSMatthew Dillon 		UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags));
2212a31267eSMatthew Dillon 		*vpp = NULL;
222df8bae1dSRodney W. Grimes 		return (error);
2232a31267eSMatthew Dillon 	}
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes 	/*
226df8bae1dSRodney W. Grimes 	 * The parent directory will have been unlocked, unless lookup
2272a31267eSMatthew Dillon 	 * found the last component or if dvp == tdvp (tdvp must be locked).
2282a31267eSMatthew Dillon 	 *
2292a31267eSMatthew Dillon 	 * We want our dvp to remain locked and ref'd.  We also want tdvp
2302a31267eSMatthew Dillon 	 * to remain locked and ref'd.
231df8bae1dSRodney W. Grimes 	 */
2322a31267eSMatthew Dillon 	UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags));
233df8bae1dSRodney W. Grimes 
2342a31267eSMatthew Dillon 	if (dvp != tdvp && (cnp->cn_flags & ISLASTCN) == 0)
235b40ce416SJulian Elischer 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes 	/*
238df8bae1dSRodney W. Grimes 	 * Lastly check if the current node is a mount point in
239df8bae1dSRodney W. Grimes 	 * which case walk up the mount hierarchy making sure not to
240df8bae1dSRodney W. Grimes 	 * bump into the root of the mount tree (ie. dvp != udvp).
2412a31267eSMatthew Dillon 	 *
2422a31267eSMatthew Dillon 	 * We use dvp as a temporary variable here, it is no longer related
2432a31267eSMatthew Dillon 	 * to the dvp above.  However, we have to ensure that both *pdvp and
2442a31267eSMatthew Dillon 	 * tdvp are locked on return.
245df8bae1dSRodney W. Grimes 	 */
2462a31267eSMatthew Dillon 
2472a31267eSMatthew Dillon 	dvp = tdvp;
2482a31267eSMatthew Dillon 	while (
2492a31267eSMatthew Dillon 	    dvp != udvp &&
2502a31267eSMatthew Dillon 	    (dvp->v_type == VDIR) &&
2512a31267eSMatthew Dillon 	    (mp = dvp->v_mountedhere)
2522a31267eSMatthew Dillon 	) {
2532a31267eSMatthew Dillon 		int relock_pdvp = 0;
254df8bae1dSRodney W. Grimes 
255b40ce416SJulian Elischer 		if (vfs_busy(mp, 0, 0, td))
256df8bae1dSRodney W. Grimes 			continue;
257df8bae1dSRodney W. Grimes 
2582a31267eSMatthew Dillon 		if (dvp == *pdvp)
2592a31267eSMatthew Dillon 			relock_pdvp = 1;
260df8bae1dSRodney W. Grimes 		vput(dvp);
2612a31267eSMatthew Dillon 		dvp = NULL;
2622a31267eSMatthew Dillon 		error = VFS_ROOT(mp, &dvp);
2632a31267eSMatthew Dillon 
264b40ce416SJulian Elischer 		vfs_unbusy(mp, td);
2652a31267eSMatthew Dillon 
2662a31267eSMatthew Dillon 		if (relock_pdvp)
267b40ce416SJulian Elischer 			vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, td);
2682a31267eSMatthew Dillon 
2692a31267eSMatthew Dillon 		if (error) {
2702a31267eSMatthew Dillon 			*vpp = NULL;
271df8bae1dSRodney W. Grimes 			return (error);
272df8bae1dSRodney W. Grimes 		}
273df8bae1dSRodney W. Grimes 	}
274df8bae1dSRodney W. Grimes 	*vpp = dvp;
275df8bae1dSRodney W. Grimes 	return (0);
276df8bae1dSRodney W. Grimes }
277df8bae1dSRodney W. Grimes 
278c9bf0111SKATO Takenori static int
279df8bae1dSRodney W. Grimes union_lookup(ap)
280df8bae1dSRodney W. Grimes 	struct vop_lookup_args /* {
281df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
282df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
283df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
284df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
285df8bae1dSRodney W. Grimes 	} */ *ap;
286df8bae1dSRodney W. Grimes {
287df8bae1dSRodney W. Grimes 	int error;
288df8bae1dSRodney W. Grimes 	int uerror, lerror;
289df8bae1dSRodney W. Grimes 	struct vnode *uppervp, *lowervp;
290df8bae1dSRodney W. Grimes 	struct vnode *upperdvp, *lowerdvp;
2912a31267eSMatthew Dillon 	struct vnode *dvp = ap->a_dvp;		/* starting dir */
2922a31267eSMatthew Dillon 	struct union_node *dun = VTOUNION(dvp);	/* associated union node */
293df8bae1dSRodney W. Grimes 	struct componentname *cnp = ap->a_cnp;
294b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
295df8bae1dSRodney W. Grimes 	int lockparent = cnp->cn_flags & LOCKPARENT;
296df8bae1dSRodney W. Grimes 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
29727ed09c2SMatthew Dillon 	struct ucred *saved_cred = NULL;
298996c772fSJohn Dyson 	int iswhiteout;
299996c772fSJohn Dyson 	struct vattr va;
300996c772fSJohn Dyson 
3012a31267eSMatthew Dillon 	*ap->a_vpp = NULLVP;
3026ca02614SKATO Takenori 
3036ca02614SKATO Takenori 	/*
3046ca02614SKATO Takenori 	 * Disallow write attemps to the filesystem mounted read-only.
3056ca02614SKATO Takenori 	 */
3062a31267eSMatthew Dillon 	if ((cnp->cn_flags & ISLASTCN) &&
3072a31267eSMatthew Dillon 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3082a31267eSMatthew Dillon 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
3096ca02614SKATO Takenori 		return (EROFS);
310996c772fSJohn Dyson 	}
311df8bae1dSRodney W. Grimes 
3122a31267eSMatthew Dillon 	/*
3132a31267eSMatthew Dillon 	 * For any lookup's we do, always return with the parent locked
3142a31267eSMatthew Dillon 	 */
315df8bae1dSRodney W. Grimes 	cnp->cn_flags |= LOCKPARENT;
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes 	lowerdvp = dun->un_lowervp;
318df8bae1dSRodney W. Grimes 	uppervp = NULLVP;
319df8bae1dSRodney W. Grimes 	lowervp = NULLVP;
320996c772fSJohn Dyson 	iswhiteout = 0;
321df8bae1dSRodney W. Grimes 
3222a31267eSMatthew Dillon 	uerror = ENOENT;
3232a31267eSMatthew Dillon 	lerror = ENOENT;
3242a31267eSMatthew Dillon 
3252a31267eSMatthew Dillon 	/*
3262a31267eSMatthew Dillon 	 * Get a private lock on uppervp and a reference, effectively
3272a31267eSMatthew Dillon 	 * taking it out of the union_node's control.
3282a31267eSMatthew Dillon 	 *
3292a31267eSMatthew Dillon 	 * We must lock upperdvp while holding our lock on dvp
3302a31267eSMatthew Dillon 	 * to avoid a deadlock.
3312a31267eSMatthew Dillon 	 */
332b40ce416SJulian Elischer 	upperdvp = union_lock_upper(dun, td);
333afc2a558SKATO Takenori 
334df8bae1dSRodney W. Grimes 	/*
335df8bae1dSRodney W. Grimes 	 * do the lookup in the upper level.
336df8bae1dSRodney W. Grimes 	 * if that level comsumes additional pathnames,
337df8bae1dSRodney W. Grimes 	 * then assume that something special is going
338df8bae1dSRodney W. Grimes 	 * on and just return that vnode.
339df8bae1dSRodney W. Grimes 	 */
340996c772fSJohn Dyson 	if (upperdvp != NULLVP) {
341ee582cdfSKATO Takenori 		/*
3422a31267eSMatthew Dillon 		 * We do not have to worry about the DOTDOT case, we've
3432a31267eSMatthew Dillon 		 * already unlocked dvp.
344ee582cdfSKATO Takenori 		 */
3452a31267eSMatthew Dillon 		UDEBUG(("A %p\n", upperdvp));
3462a31267eSMatthew Dillon 
3472a31267eSMatthew Dillon 		/*
3482a31267eSMatthew Dillon 		 * Do the lookup.   We must supply a locked and referenced
3492a31267eSMatthew Dillon 		 * upperdvp to the function and will get a new locked and
3502a31267eSMatthew Dillon 		 * referenced upperdvp back with the old having been
3512a31267eSMatthew Dillon 		 * dereferenced.
3522a31267eSMatthew Dillon 		 *
3532a31267eSMatthew Dillon 		 * If an error is returned, uppervp will be NULLVP.  If no
3542a31267eSMatthew Dillon 		 * error occurs, uppervp will be the locked and referenced
3552a31267eSMatthew Dillon 		 * return vnode or possibly NULL, depending on what is being
3562a31267eSMatthew Dillon 		 * requested.  It is possible that the returned uppervp
3572a31267eSMatthew Dillon 		 * will be the same as upperdvp.
3582a31267eSMatthew Dillon 		 */
3592a31267eSMatthew Dillon 		uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
3602a31267eSMatthew Dillon 		UDEBUG((
3612a31267eSMatthew Dillon 		    "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
3622a31267eSMatthew Dillon 		    uerror,
3632a31267eSMatthew Dillon 		    upperdvp,
3642a31267eSMatthew Dillon 		    upperdvp->v_usecount,
3656bdfe06aSEivind Eklund 		    VOP_ISLOCKED(upperdvp, NULL),
3662a31267eSMatthew Dillon 		    uppervp,
3672a31267eSMatthew Dillon 		    (uppervp ? uppervp->v_usecount : -99),
3686bdfe06aSEivind Eklund 		    (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99)
3692a31267eSMatthew Dillon 		));
3702a31267eSMatthew Dillon 
3716ca02614SKATO Takenori 		/*
3726ca02614SKATO Takenori 		 * Disallow write attemps to the filesystem mounted read-only.
3736ca02614SKATO Takenori 		 */
3746ca02614SKATO Takenori 		if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) &&
3756ca02614SKATO Takenori 		    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3766ca02614SKATO Takenori 		    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {
3772a31267eSMatthew Dillon 			error = EROFS;
378afc2a558SKATO Takenori 			goto out;
379df8bae1dSRodney W. Grimes 		}
3802a31267eSMatthew Dillon 
3812a31267eSMatthew Dillon 		/*
3822a31267eSMatthew Dillon 		 * Special case.  If cn_consume != 0 skip out.  The result
3832a31267eSMatthew Dillon 		 * of the lookup is transfered to our return variable.  If
3842a31267eSMatthew Dillon 		 * an error occured we have to throw away the results.
3852a31267eSMatthew Dillon 		 */
3862a31267eSMatthew Dillon 
3872a31267eSMatthew Dillon 		if (cnp->cn_consume != 0) {
3882a31267eSMatthew Dillon 			if ((error = uerror) == 0) {
3892a31267eSMatthew Dillon 				*ap->a_vpp = uppervp;
3902a31267eSMatthew Dillon 				uppervp = NULL;
3912a31267eSMatthew Dillon 			}
3922a31267eSMatthew Dillon 			goto out;
3932a31267eSMatthew Dillon 		}
3942a31267eSMatthew Dillon 
3952a31267eSMatthew Dillon 		/*
3962a31267eSMatthew Dillon 		 * Calculate whiteout, fall through
3972a31267eSMatthew Dillon 		 */
3982a31267eSMatthew Dillon 
399996c772fSJohn Dyson 		if (uerror == ENOENT || uerror == EJUSTRETURN) {
400996c772fSJohn Dyson 			if (cnp->cn_flags & ISWHITEOUT) {
401996c772fSJohn Dyson 				iswhiteout = 1;
402996c772fSJohn Dyson 			} else if (lowerdvp != NULLVP) {
403e3a285c7SMatthew Dillon 				int terror;
404e3a285c7SMatthew Dillon 
405e3a285c7SMatthew Dillon 				terror = VOP_GETATTR(upperdvp, &va,
406b40ce416SJulian Elischer 					cnp->cn_cred, cnp->cn_thread);
407e3a285c7SMatthew Dillon 				if (terror == 0 && (va.va_flags & OPAQUE))
408996c772fSJohn Dyson 					iswhiteout = 1;
409996c772fSJohn Dyson 			}
410996c772fSJohn Dyson 		}
411df8bae1dSRodney W. Grimes 	}
412df8bae1dSRodney W. Grimes 
413df8bae1dSRodney W. Grimes 	/*
414df8bae1dSRodney W. Grimes 	 * in a similar way to the upper layer, do the lookup
415df8bae1dSRodney W. Grimes 	 * in the lower layer.   this time, if there is some
416df8bae1dSRodney W. Grimes 	 * component magic going on, then vput whatever we got
417df8bae1dSRodney W. Grimes 	 * back from the upper layer and return the lower vnode
418df8bae1dSRodney W. Grimes 	 * instead.
419df8bae1dSRodney W. Grimes 	 */
4202a31267eSMatthew Dillon 
421996c772fSJohn Dyson 	if (lowerdvp != NULLVP && !iswhiteout) {
422df8bae1dSRodney W. Grimes 		int nameiop;
423df8bae1dSRodney W. Grimes 
4242a31267eSMatthew Dillon 		UDEBUG(("B %p\n", lowerdvp));
425df8bae1dSRodney W. Grimes 
426df8bae1dSRodney W. Grimes 		/*
4272a31267eSMatthew Dillon 		 * Force only LOOKUPs on the lower node, since
428df8bae1dSRodney W. Grimes 		 * we won't be making changes to it anyway.
429df8bae1dSRodney W. Grimes 		 */
430df8bae1dSRodney W. Grimes 		nameiop = cnp->cn_nameiop;
431df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = LOOKUP;
432df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW) {
433df8bae1dSRodney W. Grimes 			saved_cred = cnp->cn_cred;
434df8bae1dSRodney W. Grimes 			cnp->cn_cred = um->um_cred;
435df8bae1dSRodney W. Grimes 		}
4362a31267eSMatthew Dillon 
437ee582cdfSKATO Takenori 		/*
438ee582cdfSKATO Takenori 		 * We shouldn't have to worry about locking interactions
439ee582cdfSKATO Takenori 		 * between the lower layer and our union layer (w.r.t.
440ee582cdfSKATO Takenori 		 * `..' processing) because we don't futz with lowervp
441ee582cdfSKATO Takenori 		 * locks in the union-node instantiation code path.
4422a31267eSMatthew Dillon 		 *
4432a31267eSMatthew Dillon 		 * union_lookup1() requires lowervp to be locked on entry,
4442a31267eSMatthew Dillon 		 * and it will be unlocked on return.  The ref count will
4452a31267eSMatthew Dillon 		 * not change.  On return lowervp doesn't represent anything
4462a31267eSMatthew Dillon 		 * to us so we NULL it out.
447ee582cdfSKATO Takenori 		 */
4482a31267eSMatthew Dillon 		VREF(lowerdvp);
449b40ce416SJulian Elischer 		vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, td);
4502a31267eSMatthew Dillon 		lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
4512a31267eSMatthew Dillon 		if (lowerdvp == lowervp)
4522a31267eSMatthew Dillon 			vrele(lowerdvp);
4532a31267eSMatthew Dillon 		else
4542a31267eSMatthew Dillon 			vput(lowerdvp);
4552a31267eSMatthew Dillon 		lowerdvp = NULL;	/* lowerdvp invalid after vput */
4562a31267eSMatthew Dillon 
457df8bae1dSRodney W. Grimes 		if (um->um_op == UNMNT_BELOW)
458df8bae1dSRodney W. Grimes 			cnp->cn_cred = saved_cred;
459df8bae1dSRodney W. Grimes 		cnp->cn_nameiop = nameiop;
460df8bae1dSRodney W. Grimes 
461f8fc96b5SKATO Takenori 		if (cnp->cn_consume != 0 || lerror == EACCES) {
4622a31267eSMatthew Dillon 			if ((error = lerror) == 0) {
463df8bae1dSRodney W. Grimes 				*ap->a_vpp = lowervp;
4642a31267eSMatthew Dillon 				lowervp = NULL;
4652a31267eSMatthew Dillon 			}
466afc2a558SKATO Takenori 			goto out;
467df8bae1dSRodney W. Grimes 		}
468df8bae1dSRodney W. Grimes 	} else {
4692a31267eSMatthew Dillon 		UDEBUG(("C %p\n", lowerdvp));
470996c772fSJohn Dyson 		if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
4712a31267eSMatthew Dillon 			if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
472996c772fSJohn Dyson 				VREF(lowervp);
473b40ce416SJulian Elischer 				vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, td);
474996c772fSJohn Dyson 				lerror = 0;
475996c772fSJohn Dyson 			}
476996c772fSJohn Dyson 		}
477df8bae1dSRodney W. Grimes 	}
478df8bae1dSRodney W. Grimes 
479df8bae1dSRodney W. Grimes 	/*
4802a31267eSMatthew Dillon 	 * Ok.  Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
481df8bae1dSRodney W. Grimes 	 *
4822a31267eSMatthew Dillon 	 * 1. If both layers returned an error, select the upper layer.
483df8bae1dSRodney W. Grimes 	 *
4842a31267eSMatthew Dillon 	 * 2. If the upper layer faile and the bottom layer succeeded,
4852a31267eSMatthew Dillon 	 *    two subcases occur:
486df8bae1dSRodney W. Grimes 	 *
4872a31267eSMatthew Dillon 	 *	a.  The bottom vnode is not a directory, in which case
4882a31267eSMatthew Dillon 	 *	    just return a new union vnode referencing an
4892a31267eSMatthew Dillon 	 *	    empty top layer and the existing bottom layer.
4902a31267eSMatthew Dillon 	 *
4912a31267eSMatthew Dillon 	 *	b.  The button vnode is a directory, in which case
4922a31267eSMatthew Dillon 	 *	    create a new directory in the top layer and
4932a31267eSMatthew Dillon 	 *	    and fall through to case 3.
4942a31267eSMatthew Dillon 	 *
4952a31267eSMatthew Dillon 	 * 3. If the top layer succeeded then return a new union
496df8bae1dSRodney W. Grimes 	 *    vnode referencing whatever the new top layer and
497df8bae1dSRodney W. Grimes 	 *    whatever the bottom layer returned.
498df8bae1dSRodney W. Grimes 	 */
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 	/* case 1. */
501df8bae1dSRodney W. Grimes 	if ((uerror != 0) && (lerror != 0)) {
502afc2a558SKATO Takenori 		error = uerror;
503afc2a558SKATO Takenori 		goto out;
504df8bae1dSRodney W. Grimes 	}
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 	/* case 2. */
507df8bae1dSRodney W. Grimes 	if (uerror != 0 /* && (lerror == 0) */ ) {
508df8bae1dSRodney W. Grimes 		if (lowervp->v_type == VDIR) { /* case 2b. */
5092a31267eSMatthew Dillon 			KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
5102a31267eSMatthew Dillon 			/*
5112a31267eSMatthew Dillon 			 * oops, uppervp has a problem, we may have to shadow.
5122a31267eSMatthew Dillon 			 */
513df8bae1dSRodney W. Grimes 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
514df8bae1dSRodney W. Grimes 			if (uerror) {
515afc2a558SKATO Takenori 				error = uerror;
516afc2a558SKATO Takenori 				goto out;
517df8bae1dSRodney W. Grimes 			}
518df8bae1dSRodney W. Grimes 		}
519df8bae1dSRodney W. Grimes 	}
520df8bae1dSRodney W. Grimes 
5212a31267eSMatthew Dillon 	/*
5222a31267eSMatthew Dillon 	 * Must call union_allocvp with both the upper and lower vnodes
5232a31267eSMatthew Dillon 	 * referenced and the upper vnode locked.   ap->a_vpp is returned
5242a31267eSMatthew Dillon 	 * referenced and locked.  lowervp, uppervp, and upperdvp are
5252a31267eSMatthew Dillon 	 * absorbed by union_allocvp() whether it succeeds or fails.
5262a31267eSMatthew Dillon 	 *
5272a31267eSMatthew Dillon 	 * upperdvp is the parent directory of uppervp which may be
5282a31267eSMatthew Dillon 	 * different, depending on the path, from dvp->un_uppervp.  That's
5292a31267eSMatthew Dillon 	 * why it is a separate argument.  Note that it must be unlocked.
5302a31267eSMatthew Dillon 	 *
5312a31267eSMatthew Dillon 	 * dvp must be locked on entry to the call and will be locked on
5322a31267eSMatthew Dillon 	 * return.
5332a31267eSMatthew Dillon 	 */
5342a31267eSMatthew Dillon 
5352a31267eSMatthew Dillon 	if (uppervp && uppervp != upperdvp)
536b40ce416SJulian Elischer 		VOP_UNLOCK(uppervp, 0, td);
5372a31267eSMatthew Dillon 	if (lowervp)
538b40ce416SJulian Elischer 		VOP_UNLOCK(lowervp, 0, td);
5392a31267eSMatthew Dillon 	if (upperdvp)
540b40ce416SJulian Elischer 		VOP_UNLOCK(upperdvp, 0, td);
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
543996c772fSJohn Dyson 			      uppervp, lowervp, 1);
544df8bae1dSRodney W. Grimes 
5452a31267eSMatthew Dillon 	UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? ((*ap->a_vpp)->v_usecount) : -99));
5462a31267eSMatthew Dillon 
5472a31267eSMatthew Dillon 	uppervp = NULL;
5482a31267eSMatthew Dillon 	upperdvp = NULL;
5492a31267eSMatthew Dillon 	lowervp = NULL;
5502a31267eSMatthew Dillon 
5512a31267eSMatthew Dillon 	/*
5522a31267eSMatthew Dillon 	 *	Termination Code
5532a31267eSMatthew Dillon 	 *
5542a31267eSMatthew Dillon 	 *	- put away any extra junk laying around.  Note that lowervp
5552a31267eSMatthew Dillon 	 *	  (if not NULL) will never be the same as *ap->a_vp and
5562a31267eSMatthew Dillon 	 *	  neither will uppervp, because when we set that state we
5572a31267eSMatthew Dillon 	 *	  NULL-out lowervp or uppervp.  On the otherhand, upperdvp
5582a31267eSMatthew Dillon 	 *	  may match uppervp or *ap->a_vpp.
5592a31267eSMatthew Dillon 	 *
5602a31267eSMatthew Dillon 	 *	- relock/unlock dvp if appropriate.
5612a31267eSMatthew Dillon 	 */
5622a31267eSMatthew Dillon 
5632a31267eSMatthew Dillon out:
5642a31267eSMatthew Dillon 	if (upperdvp) {
5652a31267eSMatthew Dillon 		if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
5662a31267eSMatthew Dillon 			vrele(upperdvp);
5672a31267eSMatthew Dillon 		else
5682a31267eSMatthew Dillon 			vput(upperdvp);
5692a31267eSMatthew Dillon 	}
5702a31267eSMatthew Dillon 
5712a31267eSMatthew Dillon 	if (uppervp)
572df8bae1dSRodney W. Grimes 		vput(uppervp);
5732a31267eSMatthew Dillon 
5742a31267eSMatthew Dillon 	if (lowervp)
5752a31267eSMatthew Dillon 		vput(lowervp);
5762a31267eSMatthew Dillon 
5772a31267eSMatthew Dillon 	/*
5782a31267eSMatthew Dillon 	 * Restore LOCKPARENT state
5792a31267eSMatthew Dillon 	 */
5802a31267eSMatthew Dillon 
5812a31267eSMatthew Dillon 	if (!lockparent)
5822a31267eSMatthew Dillon 		cnp->cn_flags &= ~LOCKPARENT;
5832a31267eSMatthew Dillon 
5842a31267eSMatthew Dillon 	UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
5852a31267eSMatthew Dillon 		((*ap->a_vpp) ? (*ap->a_vpp)->v_usecount : -99),
5862a31267eSMatthew Dillon 		lowervp, uppervp));
5872a31267eSMatthew Dillon 
5882a31267eSMatthew Dillon 	/*
5892a31267eSMatthew Dillon 	 * dvp lock state, determine whether to relock dvp.  dvp is expected
5902a31267eSMatthew Dillon 	 * to be locked on return if:
5912a31267eSMatthew Dillon 	 *
5922a31267eSMatthew Dillon 	 *	- there was an error (except not EJUSTRETURN), or
5932a31267eSMatthew Dillon 	 *	- we hit the last component and lockparent is true
5942a31267eSMatthew Dillon 	 *
5952a31267eSMatthew Dillon 	 * dvp_is_locked is the current state of the dvp lock, not counting
5962a31267eSMatthew Dillon 	 * the possibility that *ap->a_vpp == dvp (in which case it is locked
5972a31267eSMatthew Dillon 	 * anyway).  Note that *ap->a_vpp == dvp only if no error occured.
5982a31267eSMatthew Dillon 	 */
5992a31267eSMatthew Dillon 
6002a31267eSMatthew Dillon 	if (*ap->a_vpp != dvp) {
6012a31267eSMatthew Dillon 		if ((error == 0 || error == EJUSTRETURN) &&
6022a31267eSMatthew Dillon 		    (!lockparent || (cnp->cn_flags & ISLASTCN) == 0)) {
603b40ce416SJulian Elischer 			VOP_UNLOCK(dvp, 0, td);
6042a31267eSMatthew Dillon 		}
6052a31267eSMatthew Dillon 	}
6062a31267eSMatthew Dillon 
6072a31267eSMatthew Dillon 	/*
6082a31267eSMatthew Dillon 	 * Diagnostics
6092a31267eSMatthew Dillon 	 */
6102a31267eSMatthew Dillon 
611f5a5311eSKATO Takenori #ifdef DIAGNOSTIC
612f5a5311eSKATO Takenori 	if (cnp->cn_namelen == 1 &&
613f5a5311eSKATO Takenori 	    cnp->cn_nameptr[0] == '.' &&
614f5a5311eSKATO Takenori 	    *ap->a_vpp != dvp) {
6152a31267eSMatthew Dillon 		panic("union_lookup returning . (%p) not same as startdir (%p)", ap->a_vpp, dvp);
616f5a5311eSKATO Takenori 	}
617f5a5311eSKATO Takenori #endif
618afc2a558SKATO Takenori 
619df8bae1dSRodney W. Grimes 	return (error);
620df8bae1dSRodney W. Grimes }
621df8bae1dSRodney W. Grimes 
6222a31267eSMatthew Dillon /*
6232a31267eSMatthew Dillon  * 	union_create:
6242a31267eSMatthew Dillon  *
6252a31267eSMatthew Dillon  * a_dvp is locked on entry and remains locked on return.  a_vpp is returned
6262a31267eSMatthew Dillon  * locked if no error occurs, otherwise it is garbage.
6272a31267eSMatthew Dillon  */
6282a31267eSMatthew Dillon 
629c9bf0111SKATO Takenori static int
630df8bae1dSRodney W. Grimes union_create(ap)
631df8bae1dSRodney W. Grimes 	struct vop_create_args /* {
632df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
633df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
634df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
635df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
636df8bae1dSRodney W. Grimes 	} */ *ap;
637df8bae1dSRodney W. Grimes {
6387be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
639996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
640b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
6412a31267eSMatthew Dillon 	struct vnode *dvp;
6422a31267eSMatthew Dillon 	int error = EROFS;
643df8bae1dSRodney W. Grimes 
644b40ce416SJulian Elischer 	if ((dvp = union_lock_upper(dun, td)) != NULL) {
645df8bae1dSRodney W. Grimes 		struct vnode *vp;
646996c772fSJohn Dyson 		struct mount *mp;
647df8bae1dSRodney W. Grimes 
648996c772fSJohn Dyson 		error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
6492a31267eSMatthew Dillon 		if (error == 0) {
6507be2d300SMike Smith 			mp = ap->a_dvp->v_mount;
651b40ce416SJulian Elischer 			VOP_UNLOCK(vp, 0, td);
6522a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vp->v_usecount));
6532a31267eSMatthew Dillon 			error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
6542a31267eSMatthew Dillon 				cnp, vp, NULLVP, 1);
6552a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
656df8bae1dSRodney W. Grimes 		}
657b40ce416SJulian Elischer 		union_unlock_upper(dvp, td);
6582a31267eSMatthew Dillon 	}
6592a31267eSMatthew Dillon 	return (error);
660df8bae1dSRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
662c9bf0111SKATO Takenori static int
663996c772fSJohn Dyson union_whiteout(ap)
664996c772fSJohn Dyson 	struct vop_whiteout_args /* {
665996c772fSJohn Dyson 		struct vnode *a_dvp;
666996c772fSJohn Dyson 		struct componentname *a_cnp;
667996c772fSJohn Dyson 		int a_flags;
668996c772fSJohn Dyson 	} */ *ap;
669996c772fSJohn Dyson {
670996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_dvp);
671996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
6722a31267eSMatthew Dillon 	struct vnode *uppervp;
6732a31267eSMatthew Dillon 	int error = EOPNOTSUPP;
674996c772fSJohn Dyson 
675b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, cnp->cn_thread)) != NULLVP) {
6762a31267eSMatthew Dillon 		error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags);
677b40ce416SJulian Elischer 		union_unlock_upper(uppervp, cnp->cn_thread);
678996c772fSJohn Dyson 	}
6792a31267eSMatthew Dillon 	return(error);
6802a31267eSMatthew Dillon }
6812a31267eSMatthew Dillon 
6822a31267eSMatthew Dillon /*
6832a31267eSMatthew Dillon  * 	union_mknod:
6842a31267eSMatthew Dillon  *
6852a31267eSMatthew Dillon  *	a_dvp is locked on entry and should remain locked on return.
6862a31267eSMatthew Dillon  *	a_vpp is garbagre whether an error occurs or not.
6872a31267eSMatthew Dillon  */
688996c772fSJohn Dyson 
689c9bf0111SKATO Takenori static int
690df8bae1dSRodney W. Grimes union_mknod(ap)
691df8bae1dSRodney W. Grimes 	struct vop_mknod_args /* {
692df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
693df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
694df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
695df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
696df8bae1dSRodney W. Grimes 	} */ *ap;
697df8bae1dSRodney W. Grimes {
6987be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
699996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
7002a31267eSMatthew Dillon 	struct vnode *dvp;
7012a31267eSMatthew Dillon 	int error = EROFS;
702df8bae1dSRodney W. Grimes 
703b40ce416SJulian Elischer 	if ((dvp = union_lock_upper(dun, cnp->cn_thread)) != NULL) {
704edfe736dSEivind Eklund 		error = VOP_MKNOD(dvp, ap->a_vpp, cnp, ap->a_vap);
705b40ce416SJulian Elischer 		union_unlock_upper(dvp, cnp->cn_thread);
706df8bae1dSRodney W. Grimes 	}
707df8bae1dSRodney W. Grimes 	return (error);
708df8bae1dSRodney W. Grimes }
709df8bae1dSRodney W. Grimes 
7102a31267eSMatthew Dillon /*
7112a31267eSMatthew Dillon  *	union_open:
7122a31267eSMatthew Dillon  *
7132a31267eSMatthew Dillon  *	run open VOP.  When opening the underlying vnode we have to mimic
7142a31267eSMatthew Dillon  *	vn_open.  What we *really* need to do to avoid screwups if the
7152a31267eSMatthew Dillon  *	open semantics change is to call vn_open().  For example, ufs blows
7162a31267eSMatthew Dillon  *	up if you open a file but do not vmio it prior to writing.
7172a31267eSMatthew Dillon  */
718df8bae1dSRodney W. Grimes 
719c9bf0111SKATO Takenori static int
720df8bae1dSRodney W. Grimes union_open(ap)
721df8bae1dSRodney W. Grimes 	struct vop_open_args /* {
722df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
723df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
724df8bae1dSRodney W. Grimes 		int a_mode;
725df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
726b40ce416SJulian Elischer 		struct thread *a_td;
727df8bae1dSRodney W. Grimes 	} */ *ap;
728df8bae1dSRodney W. Grimes {
729df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
730df8bae1dSRodney W. Grimes 	struct vnode *tvp;
731df8bae1dSRodney W. Grimes 	int mode = ap->a_mode;
732df8bae1dSRodney W. Grimes 	struct ucred *cred = ap->a_cred;
733b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
7342a31267eSMatthew Dillon 	int error = 0;
7352a31267eSMatthew Dillon 	int tvpisupper = 1;
736df8bae1dSRodney W. Grimes 
737df8bae1dSRodney W. Grimes 	/*
738df8bae1dSRodney W. Grimes 	 * If there is an existing upper vp then simply open that.
7392a31267eSMatthew Dillon 	 * The upper vp takes precedence over the lower vp.  When opening
7402a31267eSMatthew Dillon 	 * a lower vp for writing copy it to the uppervp and then open the
7412a31267eSMatthew Dillon 	 * uppervp.
7422a31267eSMatthew Dillon 	 *
7432a31267eSMatthew Dillon 	 * At the end of this section tvp will be left locked.
744df8bae1dSRodney W. Grimes 	 */
745b40ce416SJulian Elischer 	if ((tvp = union_lock_upper(un, td)) == NULLVP) {
746df8bae1dSRodney W. Grimes 		/*
747df8bae1dSRodney W. Grimes 		 * If the lower vnode is being opened for writing, then
748df8bae1dSRodney W. Grimes 		 * copy the file contents to the upper vnode and open that,
749df8bae1dSRodney W. Grimes 		 * otherwise can simply open the lower vnode.
750df8bae1dSRodney W. Grimes 		 */
751df8bae1dSRodney W. Grimes 		tvp = un->un_lowervp;
752df8bae1dSRodney W. Grimes 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
7532a31267eSMatthew Dillon 			int docopy = !(mode & O_TRUNC);
754b40ce416SJulian Elischer 			error = union_copyup(un, docopy, cred, td);
755b40ce416SJulian Elischer 			tvp = union_lock_upper(un, td);
7562a31267eSMatthew Dillon 		} else {
7572a31267eSMatthew Dillon 			un->un_openl++;
7582a31267eSMatthew Dillon 			VREF(tvp);
759b40ce416SJulian Elischer 			vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td);
7602a31267eSMatthew Dillon 			tvpisupper = 0;
7612a31267eSMatthew Dillon 		}
7622a31267eSMatthew Dillon 	}
7632a31267eSMatthew Dillon 
7642a31267eSMatthew Dillon 	/*
7652a31267eSMatthew Dillon 	 * We are holding the correct vnode, open it
7662a31267eSMatthew Dillon 	 */
7672a31267eSMatthew Dillon 
768df8bae1dSRodney W. Grimes 	if (error == 0)
769b40ce416SJulian Elischer 		error = VOP_OPEN(tvp, mode, cred, td);
7702a31267eSMatthew Dillon 
7712a31267eSMatthew Dillon 	/*
7722a31267eSMatthew Dillon 	 * Absolutely necessary or UFS will blowup
7732a31267eSMatthew Dillon 	 */
7742a31267eSMatthew Dillon         if (error == 0 && vn_canvmio(tvp) == TRUE) {
775b40ce416SJulian Elischer                 error = vfs_object_create(tvp, td, cred);
7762a31267eSMatthew Dillon         }
7772a31267eSMatthew Dillon 
7782a31267eSMatthew Dillon 	/*
7792a31267eSMatthew Dillon 	 * Release any locks held
7802a31267eSMatthew Dillon 	 */
7812a31267eSMatthew Dillon 	if (tvpisupper) {
7822a31267eSMatthew Dillon 		if (tvp)
783b40ce416SJulian Elischer 			union_unlock_upper(tvp, td);
7842a31267eSMatthew Dillon 	} else {
7852a31267eSMatthew Dillon 		vput(tvp);
7862a31267eSMatthew Dillon 	}
787df8bae1dSRodney W. Grimes 	return (error);
788df8bae1dSRodney W. Grimes }
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes /*
7912a31267eSMatthew Dillon  *	union_close:
7922a31267eSMatthew Dillon  *
7932a31267eSMatthew Dillon  *	It is unclear whether a_vp is passed locked or unlocked.  Whatever
7942a31267eSMatthew Dillon  *	the case we do not change it.
795df8bae1dSRodney W. Grimes  */
796df8bae1dSRodney W. Grimes 
797c9bf0111SKATO Takenori static int
798df8bae1dSRodney W. Grimes union_close(ap)
799df8bae1dSRodney W. Grimes 	struct vop_close_args /* {
800df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
801df8bae1dSRodney W. Grimes 		int  a_fflag;
802df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
803b40ce416SJulian Elischer 		struct thread *a_td;
804df8bae1dSRodney W. Grimes 	} */ *ap;
805df8bae1dSRodney W. Grimes {
806df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
807df8bae1dSRodney W. Grimes 	struct vnode *vp;
808df8bae1dSRodney W. Grimes 
809996c772fSJohn Dyson 	if ((vp = un->un_uppervp) == NULLVP) {
810df8bae1dSRodney W. Grimes #ifdef UNION_DIAGNOSTIC
811df8bae1dSRodney W. Grimes 		if (un->un_openl <= 0)
812df8bae1dSRodney W. Grimes 			panic("union: un_openl cnt");
813df8bae1dSRodney W. Grimes #endif
814df8bae1dSRodney W. Grimes 		--un->un_openl;
815df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
816df8bae1dSRodney W. Grimes 	}
817996c772fSJohn Dyson 	ap->a_vp = vp;
818996c772fSJohn Dyson 	return (VCALL(vp, VOFFSET(vop_close), ap));
819df8bae1dSRodney W. Grimes }
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes /*
822df8bae1dSRodney W. Grimes  * Check access permission on the union vnode.
823df8bae1dSRodney W. Grimes  * The access check being enforced is to check
824df8bae1dSRodney W. Grimes  * against both the underlying vnode, and any
825df8bae1dSRodney W. Grimes  * copied vnode.  This ensures that no additional
826df8bae1dSRodney W. Grimes  * file permissions are given away simply because
827df8bae1dSRodney W. Grimes  * the user caused an implicit file copy.
828df8bae1dSRodney W. Grimes  */
829c9bf0111SKATO Takenori static int
830df8bae1dSRodney W. Grimes union_access(ap)
831df8bae1dSRodney W. Grimes 	struct vop_access_args /* {
832df8bae1dSRodney W. Grimes 		struct vnodeop_desc *a_desc;
833df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
834df8bae1dSRodney W. Grimes 		int a_mode;
835df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
836b40ce416SJulian Elischer 		struct thread *a_td;
837df8bae1dSRodney W. Grimes 	} */ *ap;
838df8bae1dSRodney W. Grimes {
839df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
840b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
841df8bae1dSRodney W. Grimes 	int error = EACCES;
842df8bae1dSRodney W. Grimes 	struct vnode *vp;
843df8bae1dSRodney W. Grimes 
8446ca02614SKATO Takenori 	/*
8456ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
8466ca02614SKATO Takenori 	 */
8472a31267eSMatthew Dillon 	if ((ap->a_mode & VWRITE) &&
8482a31267eSMatthew Dillon 	    (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
8496ca02614SKATO Takenori 		switch (ap->a_vp->v_type) {
850831a80b0SMatthew Dillon 		case VREG:
851831a80b0SMatthew Dillon 		case VDIR:
852831a80b0SMatthew Dillon 		case VLNK:
8536ca02614SKATO Takenori 			return (EROFS);
854831a80b0SMatthew Dillon 		default:
855831a80b0SMatthew Dillon 			break;
8566ca02614SKATO Takenori 		}
8576ca02614SKATO Takenori 	}
8582a31267eSMatthew Dillon 
859b40ce416SJulian Elischer 	if ((vp = union_lock_upper(un, td)) != NULLVP) {
860996c772fSJohn Dyson 		ap->a_vp = vp;
8612a31267eSMatthew Dillon 		error = VCALL(vp, VOFFSET(vop_access), ap);
862b40ce416SJulian Elischer 		union_unlock_upper(vp, td);
8632a31267eSMatthew Dillon 		return(error);
864df8bae1dSRodney W. Grimes 	}
865df8bae1dSRodney W. Grimes 
866996c772fSJohn Dyson 	if ((vp = un->un_lowervp) != NULLVP) {
867b40ce416SJulian Elischer 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
868996c772fSJohn Dyson 		ap->a_vp = vp;
8692a31267eSMatthew Dillon 
8702a31267eSMatthew Dillon 		/*
8712a31267eSMatthew Dillon 		 * Remove VWRITE from a_mode if our mount point is RW, because
8722a31267eSMatthew Dillon 		 * we want to allow writes and lowervp may be read-only.
8732a31267eSMatthew Dillon 		 */
8742a31267eSMatthew Dillon 		if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
8752a31267eSMatthew Dillon 			ap->a_mode &= ~VWRITE;
8762a31267eSMatthew Dillon 
877996c772fSJohn Dyson 		error = VCALL(vp, VOFFSET(vop_access), ap);
878df8bae1dSRodney W. Grimes 		if (error == 0) {
8792a31267eSMatthew Dillon 			struct union_mount *um;
8802a31267eSMatthew Dillon 
8812a31267eSMatthew Dillon 			um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
882df8bae1dSRodney W. Grimes 
883996c772fSJohn Dyson 			if (um->um_op == UNMNT_BELOW) {
884996c772fSJohn Dyson 				ap->a_cred = um->um_cred;
885996c772fSJohn Dyson 				error = VCALL(vp, VOFFSET(vop_access), ap);
886df8bae1dSRodney W. Grimes 			}
887996c772fSJohn Dyson 		}
888b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
889df8bae1dSRodney W. Grimes 	}
890df8bae1dSRodney W. Grimes 	return(error);
891df8bae1dSRodney W. Grimes }
892df8bae1dSRodney W. Grimes 
893df8bae1dSRodney W. Grimes /*
894996c772fSJohn Dyson  * We handle getattr only to change the fsid and
895996c772fSJohn Dyson  * track object sizes
8962a31267eSMatthew Dillon  *
8972a31267eSMatthew Dillon  * It's not clear whether VOP_GETATTR is to be
8982a31267eSMatthew Dillon  * called with the vnode locked or not.  stat() calls
8992a31267eSMatthew Dillon  * it with (vp) locked, and fstat calls it with
9002a31267eSMatthew Dillon  * (vp) unlocked.
9012a31267eSMatthew Dillon  *
9022a31267eSMatthew Dillon  * Because of this we cannot use our normal locking functions
9032a31267eSMatthew Dillon  * if we do not intend to lock the main a_vp node.  At the moment
9042a31267eSMatthew Dillon  * we are running without any specific locking at all, but beware
9052a31267eSMatthew Dillon  * to any programmer that care must be taken if locking is added
9062a31267eSMatthew Dillon  * to this function.
907df8bae1dSRodney W. Grimes  */
9082a31267eSMatthew Dillon 
909c9bf0111SKATO Takenori static int
910df8bae1dSRodney W. Grimes union_getattr(ap)
911df8bae1dSRodney W. Grimes 	struct vop_getattr_args /* {
912df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
913df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
914df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
915b40ce416SJulian Elischer 		struct thread *a_td;
916df8bae1dSRodney W. Grimes 	} */ *ap;
917df8bae1dSRodney W. Grimes {
918df8bae1dSRodney W. Grimes 	int error;
919df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
9202a31267eSMatthew Dillon 	struct vnode *vp;
921df8bae1dSRodney W. Grimes 	struct vattr *vap;
922df8bae1dSRodney W. Grimes 	struct vattr va;
923df8bae1dSRodney W. Grimes 
924df8bae1dSRodney W. Grimes 	/*
925df8bae1dSRodney W. Grimes 	 * Some programs walk the filesystem hierarchy by counting
926df8bae1dSRodney W. Grimes 	 * links to directories to avoid stat'ing all the time.
927df8bae1dSRodney W. Grimes 	 * This means the link count on directories needs to be "correct".
928df8bae1dSRodney W. Grimes 	 * The only way to do that is to call getattr on both layers
929df8bae1dSRodney W. Grimes 	 * and fix up the link count.  The link count will not necessarily
930df8bae1dSRodney W. Grimes 	 * be accurate but will be large enough to defeat the tree walkers.
931df8bae1dSRodney W. Grimes 	 */
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	vap = ap->a_vap;
934df8bae1dSRodney W. Grimes 
9352a31267eSMatthew Dillon 	if ((vp = un->un_uppervp) != NULLVP) {
936b40ce416SJulian Elischer 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
937df8bae1dSRodney W. Grimes 		if (error)
938df8bae1dSRodney W. Grimes 			return (error);
9392a31267eSMatthew Dillon 		/* XXX isn't this dangerouso without a lock? */
940996c772fSJohn Dyson 		union_newsize(ap->a_vp, vap->va_size, VNOVAL);
941df8bae1dSRodney W. Grimes 	}
942df8bae1dSRodney W. Grimes 
943df8bae1dSRodney W. Grimes 	if (vp == NULLVP) {
944df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
945b2bde4cbSKATO Takenori 	} else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
946df8bae1dSRodney W. Grimes 		vp = un->un_lowervp;
947df8bae1dSRodney W. Grimes 		vap = &va;
948df8bae1dSRodney W. Grimes 	} else {
949df8bae1dSRodney W. Grimes 		vp = NULLVP;
950df8bae1dSRodney W. Grimes 	}
951df8bae1dSRodney W. Grimes 
952df8bae1dSRodney W. Grimes 	if (vp != NULLVP) {
953b40ce416SJulian Elischer 		error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
954df8bae1dSRodney W. Grimes 		if (error)
955df8bae1dSRodney W. Grimes 			return (error);
9562a31267eSMatthew Dillon 		/* XXX isn't this dangerous without a lock? */
957996c772fSJohn Dyson 		union_newsize(ap->a_vp, VNOVAL, vap->va_size);
958df8bae1dSRodney W. Grimes 	}
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	if ((vap != ap->a_vap) && (vap->va_type == VDIR))
961df8bae1dSRodney W. Grimes 		ap->a_vap->va_nlink += vap->va_nlink;
962df8bae1dSRodney W. Grimes 	return (0);
963df8bae1dSRodney W. Grimes }
964df8bae1dSRodney W. Grimes 
965c9bf0111SKATO Takenori static int
966df8bae1dSRodney W. Grimes union_setattr(ap)
967df8bae1dSRodney W. Grimes 	struct vop_setattr_args /* {
968df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
969df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
970df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
971b40ce416SJulian Elischer 		struct thread *a_td;
972df8bae1dSRodney W. Grimes 	} */ *ap;
973df8bae1dSRodney W. Grimes {
974df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
975b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
9766ca02614SKATO Takenori 	struct vattr *vap = ap->a_vap;
9772a31267eSMatthew Dillon 	struct vnode *uppervp;
978df8bae1dSRodney W. Grimes 	int error;
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 	/*
9816ca02614SKATO Takenori 	 * Disallow write attempts on filesystems mounted read-only.
9826ca02614SKATO Takenori 	 */
9836ca02614SKATO Takenori 	if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
9846ca02614SKATO Takenori 	    (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
9856ca02614SKATO Takenori 	     vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
9862a31267eSMatthew Dillon 	     vap->va_mtime.tv_sec != VNOVAL ||
9872a31267eSMatthew Dillon 	     vap->va_mode != (mode_t)VNOVAL)) {
9886ca02614SKATO Takenori 		return (EROFS);
9892a31267eSMatthew Dillon 	}
9906ca02614SKATO Takenori 
9916ca02614SKATO Takenori 	/*
992df8bae1dSRodney W. Grimes 	 * Handle case of truncating lower object to zero size,
993df8bae1dSRodney W. Grimes 	 * by creating a zero length upper object.  This is to
994df8bae1dSRodney W. Grimes 	 * handle the case of open with O_TRUNC and O_CREAT.
995df8bae1dSRodney W. Grimes 	 */
9962a31267eSMatthew Dillon 	if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
997996c772fSJohn Dyson 		error = union_copyup(un, (ap->a_vap->va_size != 0),
998b40ce416SJulian Elischer 			    ap->a_cred, ap->a_td);
999df8bae1dSRodney W. Grimes 		if (error)
1000df8bae1dSRodney W. Grimes 			return (error);
1001df8bae1dSRodney W. Grimes 	}
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 	/*
1004df8bae1dSRodney W. Grimes 	 * Try to set attributes in upper layer,
1005df8bae1dSRodney W. Grimes 	 * otherwise return read-only filesystem error.
1006df8bae1dSRodney W. Grimes 	 */
10072a31267eSMatthew Dillon 	error = EROFS;
1008b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1009df8bae1dSRodney W. Grimes 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
1010b40ce416SJulian Elischer 					ap->a_cred, ap->a_td);
1011996c772fSJohn Dyson 		if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
1012996c772fSJohn Dyson 			union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
1013b40ce416SJulian Elischer 		union_unlock_upper(uppervp, td);
10142a31267eSMatthew Dillon 	}
10152a31267eSMatthew Dillon 	return (error);
1016df8bae1dSRodney W. Grimes }
1017df8bae1dSRodney W. Grimes 
1018c9bf0111SKATO Takenori static int
1019df8bae1dSRodney W. Grimes union_read(ap)
1020df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
1021df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1022df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1023df8bae1dSRodney W. Grimes 		int  a_ioflag;
1024df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1025df8bae1dSRodney W. Grimes 	} */ *ap;
1026df8bae1dSRodney W. Grimes {
10272a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1028b40ce416SJulian Elischer 	struct thread *td = ap->a_uio->uio_td;
10292a31267eSMatthew Dillon 	struct vnode *uvp;
10302a31267eSMatthew Dillon 	int error;
1031df8bae1dSRodney W. Grimes 
1032b40ce416SJulian Elischer 	uvp = union_lock_other(un, td);
10332a31267eSMatthew Dillon 	KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
10342a31267eSMatthew Dillon 
10352a31267eSMatthew Dillon 	error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1036b40ce416SJulian Elischer 	union_unlock_other(uvp, td);
1037996c772fSJohn Dyson 
1038996c772fSJohn Dyson 	/*
1039996c772fSJohn Dyson 	 * XXX
1040996c772fSJohn Dyson 	 * perhaps the size of the underlying object has changed under
1041996c772fSJohn Dyson 	 * our feet.  take advantage of the offset information present
1042996c772fSJohn Dyson 	 * in the uio structure.
1043996c772fSJohn Dyson 	 */
1044996c772fSJohn Dyson 	if (error == 0) {
1045996c772fSJohn Dyson 		struct union_node *un = VTOUNION(ap->a_vp);
1046996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
1047996c772fSJohn Dyson 
10482a31267eSMatthew Dillon 		if (uvp == un->un_uppervp) {
1049996c772fSJohn Dyson 			if (cur > un->un_uppersz)
1050996c772fSJohn Dyson 				union_newsize(ap->a_vp, cur, VNOVAL);
1051996c772fSJohn Dyson 		} else {
1052996c772fSJohn Dyson 			if (cur > un->un_lowersz)
1053996c772fSJohn Dyson 				union_newsize(ap->a_vp, VNOVAL, cur);
1054996c772fSJohn Dyson 		}
1055996c772fSJohn Dyson 	}
1056df8bae1dSRodney W. Grimes 	return (error);
1057df8bae1dSRodney W. Grimes }
1058df8bae1dSRodney W. Grimes 
1059c9bf0111SKATO Takenori static int
1060df8bae1dSRodney W. Grimes union_write(ap)
1061df8bae1dSRodney W. Grimes 	struct vop_read_args /* {
1062df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1063df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1064df8bae1dSRodney W. Grimes 		int  a_ioflag;
1065df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1066df8bae1dSRodney W. Grimes 	} */ *ap;
1067df8bae1dSRodney W. Grimes {
1068996c772fSJohn Dyson 	struct union_node *un = VTOUNION(ap->a_vp);
1069b40ce416SJulian Elischer 	struct thread *td = ap->a_uio->uio_td;
10702a31267eSMatthew Dillon 	struct vnode *uppervp;
10712a31267eSMatthew Dillon 	int error;
1072df8bae1dSRodney W. Grimes 
1073b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) == NULLVP)
1074996c772fSJohn Dyson 		panic("union: missing upper layer in write");
1075996c772fSJohn Dyson 
10762a31267eSMatthew Dillon 	error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1077996c772fSJohn Dyson 
1078996c772fSJohn Dyson 	/*
1079996c772fSJohn Dyson 	 * the size of the underlying object may be changed by the
1080996c772fSJohn Dyson 	 * write.
1081996c772fSJohn Dyson 	 */
1082996c772fSJohn Dyson 	if (error == 0) {
1083996c772fSJohn Dyson 		off_t cur = ap->a_uio->uio_offset;
1084996c772fSJohn Dyson 
1085996c772fSJohn Dyson 		if (cur > un->un_uppersz)
1086996c772fSJohn Dyson 			union_newsize(ap->a_vp, cur, VNOVAL);
1087996c772fSJohn Dyson 	}
1088b40ce416SJulian Elischer 	union_unlock_upper(uppervp, td);
1089df8bae1dSRodney W. Grimes 	return (error);
1090df8bae1dSRodney W. Grimes }
1091df8bae1dSRodney W. Grimes 
1092c9bf0111SKATO Takenori static int
1093996c772fSJohn Dyson union_lease(ap)
1094996c772fSJohn Dyson 	struct vop_lease_args /* {
1095996c772fSJohn Dyson 		struct vnode *a_vp;
1096b40ce416SJulian Elischer 		struct thread *a_td;
1097996c772fSJohn Dyson 		struct ucred *a_cred;
1098996c772fSJohn Dyson 		int a_flag;
1099996c772fSJohn Dyson 	} */ *ap;
1100996c772fSJohn Dyson {
11012a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1102996c772fSJohn Dyson 
1103996c772fSJohn Dyson 	ap->a_vp = ovp;
1104996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_lease), ap));
1105996c772fSJohn Dyson }
1106996c772fSJohn Dyson 
1107c9bf0111SKATO Takenori static int
1108df8bae1dSRodney W. Grimes union_ioctl(ap)
1109df8bae1dSRodney W. Grimes 	struct vop_ioctl_args /* {
1110df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1111df8bae1dSRodney W. Grimes 		int  a_command;
1112df8bae1dSRodney W. Grimes 		caddr_t  a_data;
1113df8bae1dSRodney W. Grimes 		int  a_fflag;
1114df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1115b40ce416SJulian Elischer 		struct thread *a_td;
1116df8bae1dSRodney W. Grimes 	} */ *ap;
1117df8bae1dSRodney W. Grimes {
11182a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1119df8bae1dSRodney W. Grimes 
1120996c772fSJohn Dyson 	ap->a_vp = ovp;
1121996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
1122df8bae1dSRodney W. Grimes }
1123df8bae1dSRodney W. Grimes 
1124c9bf0111SKATO Takenori static int
1125a6aeade2SPeter Wemm union_poll(ap)
1126a6aeade2SPeter Wemm 	struct vop_poll_args /* {
1127df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1128a6aeade2SPeter Wemm 		int  a_events;
1129df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1130b40ce416SJulian Elischer 		struct thread *a_td;
1131df8bae1dSRodney W. Grimes 	} */ *ap;
1132df8bae1dSRodney W. Grimes {
11332a31267eSMatthew Dillon 	struct vnode *ovp = OTHERVP(ap->a_vp);
1134df8bae1dSRodney W. Grimes 
1135996c772fSJohn Dyson 	ap->a_vp = ovp;
1136a6aeade2SPeter Wemm 	return (VCALL(ovp, VOFFSET(vop_poll), ap));
1137996c772fSJohn Dyson }
1138996c772fSJohn Dyson 
1139c9bf0111SKATO Takenori static int
1140996c772fSJohn Dyson union_revoke(ap)
1141996c772fSJohn Dyson 	struct vop_revoke_args /* {
1142996c772fSJohn Dyson 		struct vnode *a_vp;
1143996c772fSJohn Dyson 		int a_flags;
1144b40ce416SJulian Elischer 		struct thread *a_td;
1145996c772fSJohn Dyson 	} */ *ap;
1146996c772fSJohn Dyson {
1147996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1148996c772fSJohn Dyson 
1149996c772fSJohn Dyson 	if (UPPERVP(vp))
1150996c772fSJohn Dyson 		VOP_REVOKE(UPPERVP(vp), ap->a_flags);
1151996c772fSJohn Dyson 	if (LOWERVP(vp))
1152996c772fSJohn Dyson 		VOP_REVOKE(LOWERVP(vp), ap->a_flags);
1153996c772fSJohn Dyson 	vgone(vp);
1154996c772fSJohn Dyson 	return (0);
1155df8bae1dSRodney W. Grimes }
1156df8bae1dSRodney W. Grimes 
1157c9bf0111SKATO Takenori static int
1158df8bae1dSRodney W. Grimes union_fsync(ap)
1159df8bae1dSRodney W. Grimes 	struct vop_fsync_args /* {
1160df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1161df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1162df8bae1dSRodney W. Grimes 		int  a_waitfor;
1163b40ce416SJulian Elischer 		struct thread *a_td;
1164df8bae1dSRodney W. Grimes 	} */ *ap;
1165df8bae1dSRodney W. Grimes {
1166df8bae1dSRodney W. Grimes 	int error = 0;
1167b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
11682a31267eSMatthew Dillon 	struct vnode *targetvp;
11692a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1170df8bae1dSRodney W. Grimes 
1171b40ce416SJulian Elischer 	if ((targetvp = union_lock_other(un, td)) != NULLVP) {
1172b40ce416SJulian Elischer 		error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_waitfor, td);
1173b40ce416SJulian Elischer 		union_unlock_other(targetvp, td);
1174df8bae1dSRodney W. Grimes 	}
1175df8bae1dSRodney W. Grimes 
1176df8bae1dSRodney W. Grimes 	return (error);
1177df8bae1dSRodney W. Grimes }
1178df8bae1dSRodney W. Grimes 
11792a31267eSMatthew Dillon /*
11802a31267eSMatthew Dillon  *	union_remove:
11812a31267eSMatthew Dillon  *
11822a31267eSMatthew Dillon  *	Remove the specified cnp.  The dvp and vp are passed to us locked
11832a31267eSMatthew Dillon  *	and must remain locked on return.
11842a31267eSMatthew Dillon  */
11852a31267eSMatthew Dillon 
1186c9bf0111SKATO Takenori static int
1187df8bae1dSRodney W. Grimes union_remove(ap)
1188df8bae1dSRodney W. Grimes 	struct vop_remove_args /* {
1189df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1190df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1191df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1192df8bae1dSRodney W. Grimes 	} */ *ap;
1193df8bae1dSRodney W. Grimes {
1194df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1195df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1196996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1197b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
11982a31267eSMatthew Dillon 	struct vnode *uppervp;
11992a31267eSMatthew Dillon 	struct vnode *upperdvp;
12007be2d300SMike Smith 	int error;
1201df8bae1dSRodney W. Grimes 
1202b40ce416SJulian Elischer 	if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1203996c772fSJohn Dyson 		panic("union remove: null upper vnode");
1204996c772fSJohn Dyson 
1205b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1206b40ce416SJulian Elischer 		if (union_dowhiteout(un, cnp->cn_cred, td))
1207996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
12082a31267eSMatthew Dillon 		error = VOP_REMOVE(upperdvp, uppervp, cnp);
120987884aadSKATO Takenori #if 0
121087884aadSKATO Takenori 		/* XXX */
1211df8bae1dSRodney W. Grimes 		if (!error)
1212df8bae1dSRodney W. Grimes 			union_removed_upper(un);
121387884aadSKATO Takenori #endif
1214b40ce416SJulian Elischer 		union_unlock_upper(uppervp, td);
1215df8bae1dSRodney W. Grimes 	} else {
1216996c772fSJohn Dyson 		error = union_mkwhiteout(
12172a31267eSMatthew Dillon 			    MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
12182a31267eSMatthew Dillon 			    upperdvp, ap->a_cnp, un->un_path);
1219df8bae1dSRodney W. Grimes 	}
1220b40ce416SJulian Elischer 	union_unlock_upper(upperdvp, td);
1221df8bae1dSRodney W. Grimes 	return (error);
1222df8bae1dSRodney W. Grimes }
1223df8bae1dSRodney W. Grimes 
12242a31267eSMatthew Dillon /*
12252a31267eSMatthew Dillon  *	union_link:
12262a31267eSMatthew Dillon  *
12272a31267eSMatthew Dillon  *	tdvp will be locked on entry, vp will not be locked on entry.
12282a31267eSMatthew Dillon  *	tdvp should remain locked on return and vp should remain unlocked
12292a31267eSMatthew Dillon  *	on return.
12302a31267eSMatthew Dillon  */
12312a31267eSMatthew Dillon 
1232c9bf0111SKATO Takenori static int
1233df8bae1dSRodney W. Grimes union_link(ap)
1234df8bae1dSRodney W. Grimes 	struct vop_link_args /* {
1235df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
123647777413SDavid Greenman 		struct vnode *a_vp;
1237df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1238df8bae1dSRodney W. Grimes 	} */ *ap;
1239df8bae1dSRodney W. Grimes {
1240996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1241b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
12427be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_tdvp);
1243996c772fSJohn Dyson 	struct vnode *vp;
1244996c772fSJohn Dyson 	struct vnode *tdvp;
12457be2d300SMike Smith 	int error = 0;
1246df8bae1dSRodney W. Grimes 
1247996c772fSJohn Dyson 	if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1248996c772fSJohn Dyson 		vp = ap->a_vp;
1249df8bae1dSRodney W. Grimes 	} else {
1250996c772fSJohn Dyson 		struct union_node *tun = VTOUNION(ap->a_vp);
12512a31267eSMatthew Dillon 
1252996c772fSJohn Dyson 		if (tun->un_uppervp == NULLVP) {
1253b40ce416SJulian Elischer 			vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
12542a31267eSMatthew Dillon #if 0
12557be2d300SMike Smith 			if (dun->un_uppervp == tun->un_dirvp) {
12562a31267eSMatthew Dillon 				if (dun->un_flags & UN_ULOCK) {
12577be2d300SMike Smith 					dun->un_flags &= ~UN_ULOCK;
1258b40ce416SJulian Elischer 					VOP_UNLOCK(dun->un_uppervp, 0, td);
1259996c772fSJohn Dyson 				}
12602a31267eSMatthew Dillon 			}
12612a31267eSMatthew Dillon #endif
1262b40ce416SJulian Elischer 			error = union_copyup(tun, 1, cnp->cn_cred, td);
12632a31267eSMatthew Dillon #if 0
12647be2d300SMike Smith 			if (dun->un_uppervp == tun->un_dirvp) {
12657be2d300SMike Smith 				vn_lock(dun->un_uppervp,
1266b40ce416SJulian Elischer 					    LK_EXCLUSIVE | LK_RETRY, td);
12677be2d300SMike Smith 				dun->un_flags |= UN_ULOCK;
1268996c772fSJohn Dyson 			}
12692a31267eSMatthew Dillon #endif
1270b40ce416SJulian Elischer 			VOP_UNLOCK(ap->a_vp, 0, td);
1271996c772fSJohn Dyson 		}
1272996c772fSJohn Dyson 		vp = tun->un_uppervp;
1273df8bae1dSRodney W. Grimes 	}
1274df8bae1dSRodney W. Grimes 
12757be2d300SMike Smith 	if (error)
1276df8bae1dSRodney W. Grimes 		return (error);
1277df8bae1dSRodney W. Grimes 
12782a31267eSMatthew Dillon 	/*
12792a31267eSMatthew Dillon 	 * Make sure upper is locked, then unlock the union directory we were
12802a31267eSMatthew Dillon 	 * called with to avoid a deadlock while we are calling VOP_LINK on
12812a31267eSMatthew Dillon 	 * the upper (with tdvp locked and vp not locked).  Our ap->a_tdvp
12822a31267eSMatthew Dillon 	 * is expected to be locked on return.
12832a31267eSMatthew Dillon 	 */
1284996c772fSJohn Dyson 
1285b40ce416SJulian Elischer 	if ((tdvp = union_lock_upper(dun, td)) == NULLVP)
12862a31267eSMatthew Dillon 		return (EROFS);
12877be2d300SMike Smith 
1288b40ce416SJulian Elischer 	VOP_UNLOCK(ap->a_tdvp, 0, td);		/* unlock calling node */
12892a31267eSMatthew Dillon 	error = VOP_LINK(tdvp, vp, cnp);	/* call link on upper */
12907be2d300SMike Smith 
12912a31267eSMatthew Dillon 	/*
12922a31267eSMatthew Dillon 	 * We have to unlock tdvp prior to relocking our calling node in
12932a31267eSMatthew Dillon 	 * order to avoid a deadlock.
12942a31267eSMatthew Dillon 	 */
1295b40ce416SJulian Elischer 	union_unlock_upper(tdvp, td);
1296b40ce416SJulian Elischer 	vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, td);
12977be2d300SMike Smith 	return (error);
1298996c772fSJohn Dyson }
1299996c772fSJohn Dyson 
1300c9bf0111SKATO Takenori static int
1301df8bae1dSRodney W. Grimes union_rename(ap)
1302df8bae1dSRodney W. Grimes 	struct vop_rename_args  /* {
1303df8bae1dSRodney W. Grimes 		struct vnode *a_fdvp;
1304df8bae1dSRodney W. Grimes 		struct vnode *a_fvp;
1305df8bae1dSRodney W. Grimes 		struct componentname *a_fcnp;
1306df8bae1dSRodney W. Grimes 		struct vnode *a_tdvp;
1307df8bae1dSRodney W. Grimes 		struct vnode *a_tvp;
1308df8bae1dSRodney W. Grimes 		struct componentname *a_tcnp;
1309df8bae1dSRodney W. Grimes 	} */ *ap;
1310df8bae1dSRodney W. Grimes {
1311df8bae1dSRodney W. Grimes 	int error;
1312df8bae1dSRodney W. Grimes 	struct vnode *fdvp = ap->a_fdvp;
1313df8bae1dSRodney W. Grimes 	struct vnode *fvp = ap->a_fvp;
1314df8bae1dSRodney W. Grimes 	struct vnode *tdvp = ap->a_tdvp;
1315df8bae1dSRodney W. Grimes 	struct vnode *tvp = ap->a_tvp;
1316df8bae1dSRodney W. Grimes 
13172a31267eSMatthew Dillon 	/*
13182a31267eSMatthew Dillon 	 * Figure out what fdvp to pass to our upper or lower vnode.  If we
13192a31267eSMatthew Dillon 	 * replace the fdvp, release the original one and ref the new one.
13202a31267eSMatthew Dillon 	 */
13212a31267eSMatthew Dillon 
1322df8bae1dSRodney W. Grimes 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
1323df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fdvp);
1324df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1325996c772fSJohn Dyson 			/*
1326996c772fSJohn Dyson 			 * this should never happen in normal
1327996c772fSJohn Dyson 			 * operation but might if there was
1328996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1329996c772fSJohn Dyson 			 * directory.
1330996c772fSJohn Dyson 			 */
1331996c772fSJohn Dyson 			error = EXDEV;
1332df8bae1dSRodney W. Grimes 			goto bad;
1333df8bae1dSRodney W. Grimes 		}
1334df8bae1dSRodney W. Grimes 		fdvp = un->un_uppervp;
1335df8bae1dSRodney W. Grimes 		VREF(fdvp);
1336df8bae1dSRodney W. Grimes 		vrele(ap->a_fdvp);
1337df8bae1dSRodney W. Grimes 	}
1338df8bae1dSRodney W. Grimes 
13392a31267eSMatthew Dillon 	/*
13402a31267eSMatthew Dillon 	 * Figure out what fvp to pass to our upper or lower vnode.  If we
13412a31267eSMatthew Dillon 	 * replace the fvp, release the original one and ref the new one.
13422a31267eSMatthew Dillon 	 */
13432a31267eSMatthew Dillon 
1344df8bae1dSRodney W. Grimes 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
1345df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(fvp);
13462a31267eSMatthew Dillon #if 0
13472a31267eSMatthew Dillon 		struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
13482a31267eSMatthew Dillon #endif
13492a31267eSMatthew Dillon 
1350df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
13512a31267eSMatthew Dillon 			switch(fvp->v_type) {
13522a31267eSMatthew Dillon 			case VREG:
1353b40ce416SJulian Elischer 				vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread);
1354b40ce416SJulian Elischer 				error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_thread);
1355b40ce416SJulian Elischer 				VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_thread);
13562a31267eSMatthew Dillon 				if (error)
13572a31267eSMatthew Dillon 					goto bad;
13582a31267eSMatthew Dillon 				break;
13592a31267eSMatthew Dillon 			case VDIR:
13602a31267eSMatthew Dillon 				/*
13612a31267eSMatthew Dillon 				 * XXX not yet.
13622a31267eSMatthew Dillon 				 *
13632a31267eSMatthew Dillon 				 * There is only one way to rename a directory
13642a31267eSMatthew Dillon 				 * based in the lowervp, and that is to copy
13652a31267eSMatthew Dillon 				 * the entire directory hierarchy.  Otherwise
13662a31267eSMatthew Dillon 				 * it would not last across a reboot.
13672a31267eSMatthew Dillon 				 */
13682a31267eSMatthew Dillon #if 0
13692a31267eSMatthew Dillon 				vrele(fvp);
13702a31267eSMatthew Dillon 				fvp = NULL;
1371b40ce416SJulian Elischer 				vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread);
13722a31267eSMatthew Dillon 				error = union_mkshadow(um, fdvp,
13732a31267eSMatthew Dillon 					    ap->a_fcnp, &un->un_uppervp);
1374b40ce416SJulian Elischer 				VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_thread);
13752a31267eSMatthew Dillon 				if (un->un_uppervp)
1376b40ce416SJulian Elischer 					VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_thread);
13772a31267eSMatthew Dillon 				if (error)
13782a31267eSMatthew Dillon 					goto bad;
13792a31267eSMatthew Dillon 				break;
13802a31267eSMatthew Dillon #endif
13812a31267eSMatthew Dillon 			default:
1382996c772fSJohn Dyson 				error = EXDEV;
1383df8bae1dSRodney W. Grimes 				goto bad;
1384df8bae1dSRodney W. Grimes 			}
13852a31267eSMatthew Dillon 		}
1386df8bae1dSRodney W. Grimes 
1387996c772fSJohn Dyson 		if (un->un_lowervp != NULLVP)
1388996c772fSJohn Dyson 			ap->a_fcnp->cn_flags |= DOWHITEOUT;
1389df8bae1dSRodney W. Grimes 		fvp = un->un_uppervp;
1390df8bae1dSRodney W. Grimes 		VREF(fvp);
1391df8bae1dSRodney W. Grimes 		vrele(ap->a_fvp);
1392df8bae1dSRodney W. Grimes 	}
1393df8bae1dSRodney W. Grimes 
13942a31267eSMatthew Dillon 	/*
13952a31267eSMatthew Dillon 	 * Figure out what tdvp (destination directory) to pass to the
13962a31267eSMatthew Dillon 	 * lower level.  If we replace it with uppervp, we need to vput the
13972a31267eSMatthew Dillon 	 * old one.  The exclusive lock is transfered to what we will pass
13982a31267eSMatthew Dillon 	 * down in the VOP_RENAME and we replace uppervp with a simple
13992a31267eSMatthew Dillon 	 * reference.
14002a31267eSMatthew Dillon 	 */
14012a31267eSMatthew Dillon 
1402df8bae1dSRodney W. Grimes 	if (tdvp->v_op == union_vnodeop_p) {
1403df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tdvp);
14042a31267eSMatthew Dillon 
1405df8bae1dSRodney W. Grimes 		if (un->un_uppervp == NULLVP) {
1406996c772fSJohn Dyson 			/*
1407996c772fSJohn Dyson 			 * this should never happen in normal
1408996c772fSJohn Dyson 			 * operation but might if there was
1409996c772fSJohn Dyson 			 * a problem creating the top-level shadow
1410996c772fSJohn Dyson 			 * directory.
1411996c772fSJohn Dyson 			 */
1412996c772fSJohn Dyson 			error = EXDEV;
1413df8bae1dSRodney W. Grimes 			goto bad;
1414df8bae1dSRodney W. Grimes 		}
1415df8bae1dSRodney W. Grimes 
14162a31267eSMatthew Dillon 		/*
14172a31267eSMatthew Dillon 		 * new tdvp is a lock and reference on uppervp, put away
14182a31267eSMatthew Dillon 		 * the old tdvp.
14192a31267eSMatthew Dillon 		 */
1420b40ce416SJulian Elischer 		tdvp = union_lock_upper(un, ap->a_tcnp->cn_thread);
1421df8bae1dSRodney W. Grimes 		vput(ap->a_tdvp);
1422df8bae1dSRodney W. Grimes 	}
1423df8bae1dSRodney W. Grimes 
14242a31267eSMatthew Dillon 	/*
14252a31267eSMatthew Dillon 	 * Figure out what tvp (destination file) to pass to the
14262a31267eSMatthew Dillon 	 * lower level.
14272a31267eSMatthew Dillon 	 *
14282a31267eSMatthew Dillon 	 * If the uppervp file does not exist put away the (wrong)
14292a31267eSMatthew Dillon 	 * file and change tvp to NULL.
14302a31267eSMatthew Dillon 	 */
14312a31267eSMatthew Dillon 
1432996c772fSJohn Dyson 	if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1433df8bae1dSRodney W. Grimes 		struct union_node *un = VTOUNION(tvp);
1434df8bae1dSRodney W. Grimes 
1435b40ce416SJulian Elischer 		tvp = union_lock_upper(un, ap->a_tcnp->cn_thread);
1436df8bae1dSRodney W. Grimes 		vput(ap->a_tvp);
14372a31267eSMatthew Dillon 		/* note: tvp may be NULL */
1438df8bae1dSRodney W. Grimes 	}
1439df8bae1dSRodney W. Grimes 
14402a31267eSMatthew Dillon 	/*
14412a31267eSMatthew Dillon 	 * VOP_RENAME releases/vputs prior to returning, so we have no
14422a31267eSMatthew Dillon 	 * cleanup to do.
14432a31267eSMatthew Dillon 	 */
14442a31267eSMatthew Dillon 
1445df8bae1dSRodney W. Grimes 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1446df8bae1dSRodney W. Grimes 
14472a31267eSMatthew Dillon 	/*
14482a31267eSMatthew Dillon 	 * Error.  We still have to release / vput the various elements.
14492a31267eSMatthew Dillon 	 */
14502a31267eSMatthew Dillon 
1451df8bae1dSRodney W. Grimes bad:
1452df8bae1dSRodney W. Grimes 	vrele(fdvp);
14532a31267eSMatthew Dillon 	if (fvp)
1454df8bae1dSRodney W. Grimes 		vrele(fvp);
1455df8bae1dSRodney W. Grimes 	vput(tdvp);
14562a31267eSMatthew Dillon 	if (tvp != NULLVP) {
14572a31267eSMatthew Dillon 		if (tvp != tdvp)
1458df8bae1dSRodney W. Grimes 			vput(tvp);
14592a31267eSMatthew Dillon 		else
14602a31267eSMatthew Dillon 			vrele(tvp);
14612a31267eSMatthew Dillon 	}
1462df8bae1dSRodney W. Grimes 	return (error);
1463df8bae1dSRodney W. Grimes }
1464df8bae1dSRodney W. Grimes 
1465c9bf0111SKATO Takenori static int
1466df8bae1dSRodney W. Grimes union_mkdir(ap)
1467df8bae1dSRodney W. Grimes 	struct vop_mkdir_args /* {
1468df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1469df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1470df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1471df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1472df8bae1dSRodney W. Grimes 	} */ *ap;
1473df8bae1dSRodney W. Grimes {
14747be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
1475996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1476b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
14772a31267eSMatthew Dillon 	struct vnode *upperdvp;
14782a31267eSMatthew Dillon 	int error = EROFS;
1479df8bae1dSRodney W. Grimes 
1480b40ce416SJulian Elischer 	if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) {
1481df8bae1dSRodney W. Grimes 		struct vnode *vp;
1482df8bae1dSRodney W. Grimes 
14832a31267eSMatthew Dillon 		error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap);
1484b40ce416SJulian Elischer 		union_unlock_upper(upperdvp, td);
14852a31267eSMatthew Dillon 
14862a31267eSMatthew Dillon 		if (error == 0) {
1487b40ce416SJulian Elischer 			VOP_UNLOCK(vp, 0, td);
14882a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vp->v_usecount));
14892a31267eSMatthew Dillon 			error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
14902a31267eSMatthew Dillon 				ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
14912a31267eSMatthew Dillon 			UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
1492996c772fSJohn Dyson 		}
1493df8bae1dSRodney W. Grimes 	}
14942a31267eSMatthew Dillon 	return (error);
1495df8bae1dSRodney W. Grimes }
1496df8bae1dSRodney W. Grimes 
1497c9bf0111SKATO Takenori static int
1498df8bae1dSRodney W. Grimes union_rmdir(ap)
1499df8bae1dSRodney W. Grimes 	struct vop_rmdir_args /* {
1500df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1501df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1502df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1503df8bae1dSRodney W. Grimes 	} */ *ap;
1504df8bae1dSRodney W. Grimes {
1505df8bae1dSRodney W. Grimes 	struct union_node *dun = VTOUNION(ap->a_dvp);
1506df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1507996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1508b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
15092a31267eSMatthew Dillon 	struct vnode *upperdvp;
15102a31267eSMatthew Dillon 	struct vnode *uppervp;
15117be2d300SMike Smith 	int error;
1512df8bae1dSRodney W. Grimes 
1513b40ce416SJulian Elischer 	if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1514996c772fSJohn Dyson 		panic("union rmdir: null upper vnode");
1515996c772fSJohn Dyson 
1516b40ce416SJulian Elischer 	if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1517b40ce416SJulian Elischer 		if (union_dowhiteout(un, cnp->cn_cred, td))
1518996c772fSJohn Dyson 			cnp->cn_flags |= DOWHITEOUT;
15192a31267eSMatthew Dillon 		error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp);
1520b40ce416SJulian Elischer 		union_unlock_upper(uppervp, td);
1521df8bae1dSRodney W. Grimes 	} else {
1522996c772fSJohn Dyson 		error = union_mkwhiteout(
15232a31267eSMatthew Dillon 			    MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1524996c772fSJohn Dyson 			    dun->un_uppervp, ap->a_cnp, un->un_path);
1525df8bae1dSRodney W. Grimes 	}
1526b40ce416SJulian Elischer 	union_unlock_upper(upperdvp, td);
1527df8bae1dSRodney W. Grimes 	return (error);
1528df8bae1dSRodney W. Grimes }
1529df8bae1dSRodney W. Grimes 
15302a31267eSMatthew Dillon /*
15312a31267eSMatthew Dillon  *	union_symlink:
15322a31267eSMatthew Dillon  *
15332a31267eSMatthew Dillon  *	dvp is locked on entry and remains locked on return.  a_vpp is garbage
15342a31267eSMatthew Dillon  *	(unused).
15352a31267eSMatthew Dillon  */
15362a31267eSMatthew Dillon 
1537c9bf0111SKATO Takenori static int
1538df8bae1dSRodney W. Grimes union_symlink(ap)
1539df8bae1dSRodney W. Grimes 	struct vop_symlink_args /* {
1540df8bae1dSRodney W. Grimes 		struct vnode *a_dvp;
1541df8bae1dSRodney W. Grimes 		struct vnode **a_vpp;
1542df8bae1dSRodney W. Grimes 		struct componentname *a_cnp;
1543df8bae1dSRodney W. Grimes 		struct vattr *a_vap;
1544df8bae1dSRodney W. Grimes 		char *a_target;
1545df8bae1dSRodney W. Grimes 	} */ *ap;
1546df8bae1dSRodney W. Grimes {
15477be2d300SMike Smith 	struct union_node *dun = VTOUNION(ap->a_dvp);
1548996c772fSJohn Dyson 	struct componentname *cnp = ap->a_cnp;
1549b40ce416SJulian Elischer 	struct thread *td = cnp->cn_thread;
15502a31267eSMatthew Dillon 	struct vnode *dvp;
15512a31267eSMatthew Dillon 	int error = EROFS;
1552df8bae1dSRodney W. Grimes 
1553b40ce416SJulian Elischer 	if ((dvp = union_lock_upper(dun, td)) != NULLVP) {
1554dd8c04f4SEivind Eklund 		error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap,
1555dd8c04f4SEivind Eklund 			    ap->a_target);
1556b40ce416SJulian Elischer 		union_unlock_upper(dvp, td);
1557df8bae1dSRodney W. Grimes 	}
15582a31267eSMatthew Dillon 	return (error);
1559df8bae1dSRodney W. Grimes }
1560df8bae1dSRodney W. Grimes 
1561df8bae1dSRodney W. Grimes /*
1562df8bae1dSRodney W. Grimes  * union_readdir works in concert with getdirentries and
1563df8bae1dSRodney W. Grimes  * readdir(3) to provide a list of entries in the unioned
1564df8bae1dSRodney W. Grimes  * directories.  getdirentries is responsible for walking
1565df8bae1dSRodney W. Grimes  * down the union stack.  readdir(3) is responsible for
1566df8bae1dSRodney W. Grimes  * eliminating duplicate names from the returned data stream.
1567df8bae1dSRodney W. Grimes  */
1568c9bf0111SKATO Takenori static int
1569df8bae1dSRodney W. Grimes union_readdir(ap)
1570df8bae1dSRodney W. Grimes 	struct vop_readdir_args /* {
1571df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1572df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1573df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1574996c772fSJohn Dyson 		int *a_eofflag;
1575996c772fSJohn Dyson 		u_long *a_cookies;
1576996c772fSJohn Dyson 		int a_ncookies;
1577df8bae1dSRodney W. Grimes 	} */ *ap;
1578df8bae1dSRodney W. Grimes {
1579df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1580b40ce416SJulian Elischer 	struct thread *td = ap->a_uio->uio_td;
15812a31267eSMatthew Dillon 	struct vnode *uvp;
15822a31267eSMatthew Dillon 	int error = 0;
1583df8bae1dSRodney W. Grimes 
1584b40ce416SJulian Elischer 	if ((uvp = union_lock_upper(un, td)) != NULLVP) {
1585996c772fSJohn Dyson 		ap->a_vp = uvp;
15862a31267eSMatthew Dillon 		error = VCALL(uvp, VOFFSET(vop_readdir), ap);
1587b40ce416SJulian Elischer 		union_unlock_upper(uvp, td);
15882a31267eSMatthew Dillon 	}
15892a31267eSMatthew Dillon 	return(error);
1590df8bae1dSRodney W. Grimes }
1591df8bae1dSRodney W. Grimes 
1592c9bf0111SKATO Takenori static int
1593df8bae1dSRodney W. Grimes union_readlink(ap)
1594df8bae1dSRodney W. Grimes 	struct vop_readlink_args /* {
1595df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1596df8bae1dSRodney W. Grimes 		struct uio *a_uio;
1597df8bae1dSRodney W. Grimes 		struct ucred *a_cred;
1598df8bae1dSRodney W. Grimes 	} */ *ap;
1599df8bae1dSRodney W. Grimes {
1600df8bae1dSRodney W. Grimes 	int error;
16012a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
1602996c772fSJohn Dyson 	struct uio *uio = ap->a_uio;
1603b40ce416SJulian Elischer 	struct thread *td = uio->uio_td;
16042a31267eSMatthew Dillon 	struct vnode *vp;
1605df8bae1dSRodney W. Grimes 
1606b40ce416SJulian Elischer 	vp = union_lock_other(un, td);
16072a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
16082a31267eSMatthew Dillon 
1609996c772fSJohn Dyson 	ap->a_vp = vp;
1610996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_readlink), ap);
1611b40ce416SJulian Elischer 	union_unlock_other(vp, td);
1612df8bae1dSRodney W. Grimes 
1613df8bae1dSRodney W. Grimes 	return (error);
1614df8bae1dSRodney W. Grimes }
1615df8bae1dSRodney W. Grimes 
1616f2a2857bSKirk McKusick static int
1617f2a2857bSKirk McKusick union_getwritemount(ap)
1618f2a2857bSKirk McKusick 	struct vop_getwritemount_args /* {
1619f2a2857bSKirk McKusick 		struct vnode *a_vp;
1620f2a2857bSKirk McKusick 		struct mount **a_mpp;
1621f2a2857bSKirk McKusick 	} */ *ap;
1622f2a2857bSKirk McKusick {
1623f3d1ec67SBoris Popov 	struct vnode *vp = ap->a_vp;
1624f3d1ec67SBoris Popov 	struct vnode *uvp = UPPERVP(vp);
1625f2a2857bSKirk McKusick 
1626f3d1ec67SBoris Popov 	if (uvp == NULL) {
1627f3d1ec67SBoris Popov 		VI_LOCK(vp);
1628e6e370a7SJeff Roberson 		if (vp->v_iflag & VI_FREE) {
1629f3d1ec67SBoris Popov 			VI_UNLOCK(vp);
1630f3d1ec67SBoris Popov 			return (EOPNOTSUPP);
1631f3d1ec67SBoris Popov 		}
1632f3d1ec67SBoris Popov 		VI_UNLOCK(vp);
163310fa1684SBoris Popov 		return (EACCES);
1634f3d1ec67SBoris Popov 	}
1635f3d1ec67SBoris Popov 	return(VOP_GETWRITEMOUNT(uvp, ap->a_mpp));
1636f2a2857bSKirk McKusick }
1637f2a2857bSKirk McKusick 
16382a31267eSMatthew Dillon /*
16392a31267eSMatthew Dillon  *	union_inactive:
16402a31267eSMatthew Dillon  *
16412a31267eSMatthew Dillon  *	Called with the vnode locked.  We are expected to unlock the vnode.
16422a31267eSMatthew Dillon  */
16432a31267eSMatthew Dillon 
1644c9bf0111SKATO Takenori static int
1645df8bae1dSRodney W. Grimes union_inactive(ap)
1646df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
1647df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1648b40ce416SJulian Elischer 		struct thread *a_td;
1649df8bae1dSRodney W. Grimes 	} */ *ap;
1650df8bae1dSRodney W. Grimes {
1651996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
1652b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
1653996c772fSJohn Dyson 	struct union_node *un = VTOUNION(vp);
1654996c772fSJohn Dyson 	struct vnode **vpp;
1655df8bae1dSRodney W. Grimes 
1656df8bae1dSRodney W. Grimes 	/*
1657df8bae1dSRodney W. Grimes 	 * Do nothing (and _don't_ bypass).
1658df8bae1dSRodney W. Grimes 	 * Wait to vrele lowervp until reclaim,
1659df8bae1dSRodney W. Grimes 	 * so that until then our union_node is in the
1660df8bae1dSRodney W. Grimes 	 * cache and reusable.
1661df8bae1dSRodney W. Grimes 	 *
1662df8bae1dSRodney W. Grimes 	 */
1663df8bae1dSRodney W. Grimes 
1664996c772fSJohn Dyson 	if (un->un_dircache != 0) {
1665996c772fSJohn Dyson 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1666996c772fSJohn Dyson 			vrele(*vpp);
1667996c772fSJohn Dyson 		free (un->un_dircache, M_TEMP);
1668996c772fSJohn Dyson 		un->un_dircache = 0;
1669996c772fSJohn Dyson 	}
1670df8bae1dSRodney W. Grimes 
16712a31267eSMatthew Dillon #if 0
16722a31267eSMatthew Dillon 	if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
16732a31267eSMatthew Dillon 		un->un_flags &= ~UN_ULOCK;
1674b40ce416SJulian Elischer 		VOP_UNLOCK(un->un_uppervp, 0, td);
16752a31267eSMatthew Dillon 	}
16762a31267eSMatthew Dillon #endif
16772a31267eSMatthew Dillon 
1678b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
1679996c772fSJohn Dyson 
1680996c772fSJohn Dyson 	if ((un->un_flags & UN_CACHED) == 0)
1681996c772fSJohn Dyson 		vgone(vp);
1682df8bae1dSRodney W. Grimes 
1683df8bae1dSRodney W. Grimes 	return (0);
1684df8bae1dSRodney W. Grimes }
1685df8bae1dSRodney W. Grimes 
1686c9bf0111SKATO Takenori static int
1687df8bae1dSRodney W. Grimes union_reclaim(ap)
1688df8bae1dSRodney W. Grimes 	struct vop_reclaim_args /* {
1689df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1690df8bae1dSRodney W. Grimes 	} */ *ap;
1691df8bae1dSRodney W. Grimes {
1692df8bae1dSRodney W. Grimes 	union_freevp(ap->a_vp);
1693df8bae1dSRodney W. Grimes 
1694df8bae1dSRodney W. Grimes 	return (0);
1695df8bae1dSRodney W. Grimes }
1696df8bae1dSRodney W. Grimes 
1697c9bf0111SKATO Takenori static int
1698df8bae1dSRodney W. Grimes union_lock(ap)
1699df8bae1dSRodney W. Grimes 	struct vop_lock_args *ap;
1700df8bae1dSRodney W. Grimes {
17012a31267eSMatthew Dillon #if 0
1702df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1703b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
1704996c772fSJohn Dyson 	int flags = ap->a_flags;
1705df8bae1dSRodney W. Grimes 	struct union_node *un;
17062a31267eSMatthew Dillon #endif
1707996c772fSJohn Dyson 	int error;
1708996c772fSJohn Dyson 
17092a31267eSMatthew Dillon 	error = vop_stdlock(ap);
17102a31267eSMatthew Dillon #if 0
1711df8bae1dSRodney W. Grimes 	un = VTOUNION(vp);
1712df8bae1dSRodney W. Grimes 
17132a31267eSMatthew Dillon 	if (error == 0) {
17142a31267eSMatthew Dillon 		/*
17152a31267eSMatthew Dillon 		 * Lock the upper if it exists and this is an exclusive lock
17162a31267eSMatthew Dillon 		 * request.
17172a31267eSMatthew Dillon 		 */
17182a31267eSMatthew Dillon 		if (un->un_uppervp != NULLVP &&
17192a31267eSMatthew Dillon 		    (flags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
17202a31267eSMatthew Dillon 			if ((un->un_flags & UN_ULOCK) == 0 && vp->v_usecount) {
1721b40ce416SJulian Elischer 				error = vn_lock(un->un_uppervp, flags, td);
17222a31267eSMatthew Dillon 				if (error) {
17232a31267eSMatthew Dillon 					struct vop_unlock_args uap = { 0 };
17242a31267eSMatthew Dillon 					uap.a_vp = ap->a_vp;
17252a31267eSMatthew Dillon 					uap.a_flags = ap->a_flags;
1726b40ce416SJulian Elischer 					uap.a_td = ap->a_td;
17272a31267eSMatthew Dillon 					vop_stdunlock(&uap);
1728996c772fSJohn Dyson 					return (error);
17292a31267eSMatthew Dillon 				}
1730df8bae1dSRodney W. Grimes 				un->un_flags |= UN_ULOCK;
1731df8bae1dSRodney W. Grimes 			}
17322a31267eSMatthew Dillon 		}
1733d6476adaSKATO Takenori 	}
1734d6476adaSKATO Takenori #endif
17352a31267eSMatthew Dillon 	return (error);
1736df8bae1dSRodney W. Grimes }
1737df8bae1dSRodney W. Grimes 
1738996c772fSJohn Dyson /*
17392a31267eSMatthew Dillon  *	union_unlock:
1740996c772fSJohn Dyson  *
17412a31267eSMatthew Dillon  *	Unlock our union node.  This also unlocks uppervp.
1742996c772fSJohn Dyson  */
1743c9bf0111SKATO Takenori static int
1744df8bae1dSRodney W. Grimes union_unlock(ap)
1745996c772fSJohn Dyson 	struct vop_unlock_args /* {
1746996c772fSJohn Dyson 		struct vnode *a_vp;
1747996c772fSJohn Dyson 		int a_flags;
1748b40ce416SJulian Elischer 		struct thread *a_td;
1749996c772fSJohn Dyson 	} */ *ap;
1750df8bae1dSRodney W. Grimes {
1751b7ef0b12SGarrett Wollman #if 0
1752df8bae1dSRodney W. Grimes 	struct union_node *un = VTOUNION(ap->a_vp);
1753b7ef0b12SGarrett Wollman #endif
17542a31267eSMatthew Dillon 	int error;
1755df8bae1dSRodney W. Grimes 
175610cf882bSPeter Wemm #if 0
17572a31267eSMatthew Dillon 	KASSERT((un->un_uppervp == NULL || un->un_uppervp->v_usecount > 0), ("uppervp usecount is 0"));
175810cf882bSPeter Wemm #endif
17592a31267eSMatthew Dillon 
17602a31267eSMatthew Dillon 	error = vop_stdunlock(ap);
17612a31267eSMatthew Dillon #if 0
17622a31267eSMatthew Dillon 
17632a31267eSMatthew Dillon 	/*
17642a31267eSMatthew Dillon 	 * If no exclusive locks remain and we are holding an uppervp lock,
17652a31267eSMatthew Dillon 	 * remove the uppervp lock.
17662a31267eSMatthew Dillon 	 */
17672a31267eSMatthew Dillon 
17682a31267eSMatthew Dillon 	if ((un->un_flags & UN_ULOCK) &&
17696bdfe06aSEivind Eklund 	    lockstatus(&un->un_lock, NULL) != LK_EXCLUSIVE) {
17702a31267eSMatthew Dillon 		un->un_flags &= ~UN_ULOCK;
1771b40ce416SJulian Elischer 		VOP_UNLOCK(un->un_uppervp, LK_EXCLUSIVE, td);
17722a31267eSMatthew Dillon 	}
1773df8bae1dSRodney W. Grimes #endif
17742a31267eSMatthew Dillon 	return(error);
1775df8bae1dSRodney W. Grimes }
1776df8bae1dSRodney W. Grimes 
17773413421bSBoris Popov /*
17783413421bSBoris Popov  * unionvp do not hold a VM object and there is no need to create one for
17793413421bSBoris Popov  * upper or lower vp because it is done in the union_open()
17803413421bSBoris Popov  */
17813413421bSBoris Popov static int
17823413421bSBoris Popov union_createvobject(ap)
17833413421bSBoris Popov 	struct vop_createvobject_args /* {
17843413421bSBoris Popov 		struct vnode *vp;
17853413421bSBoris Popov 		struct ucred *cred;
1786b40ce416SJulian Elischer 		struct thread *td;
17873413421bSBoris Popov 	} */ *ap;
17883413421bSBoris Popov {
17893413421bSBoris Popov 	struct vnode *vp = ap->a_vp;
17903413421bSBoris Popov 
1791e6e370a7SJeff Roberson 	vp->v_vflag |= VV_OBJBUF;
17923413421bSBoris Popov 	return (0);
17933413421bSBoris Popov }
17943413421bSBoris Popov 
17953413421bSBoris Popov /*
17963413421bSBoris Popov  * We have nothing to destroy and this operation shouldn't be bypassed.
17973413421bSBoris Popov  */
17983413421bSBoris Popov static int
17993413421bSBoris Popov union_destroyvobject(ap)
18003413421bSBoris Popov 	struct vop_destroyvobject_args /* {
18013413421bSBoris Popov 		struct vnode *vp;
18023413421bSBoris Popov 	} */ *ap;
18033413421bSBoris Popov {
18043413421bSBoris Popov 	struct vnode *vp = ap->a_vp;
18053413421bSBoris Popov 
1806e6e370a7SJeff Roberson 	vp->v_vflag &= ~VV_OBJBUF;
18073413421bSBoris Popov 	return (0);
18083413421bSBoris Popov }
18093413421bSBoris Popov 
18103413421bSBoris Popov /*
18113413421bSBoris Popov  * Get VM object from the upper or lower vp
18123413421bSBoris Popov  */
18133413421bSBoris Popov static int
18143413421bSBoris Popov union_getvobject(ap)
18153413421bSBoris Popov 	struct vop_getvobject_args /* {
18163413421bSBoris Popov 		struct vnode *vp;
18173413421bSBoris Popov 		struct vm_object **objpp;
18183413421bSBoris Popov 	} */ *ap;
18193413421bSBoris Popov {
18203413421bSBoris Popov 	struct vnode *ovp = OTHERVP(ap->a_vp);
18213413421bSBoris Popov 
18223413421bSBoris Popov 	if (ovp == NULL)
18233413421bSBoris Popov 		return EINVAL;
18243413421bSBoris Popov 	return (VOP_GETVOBJECT(ovp, ap->a_objpp));
18253413421bSBoris Popov }
18263413421bSBoris Popov 
1827c9bf0111SKATO Takenori static int
1828df8bae1dSRodney W. Grimes union_print(ap)
1829df8bae1dSRodney W. Grimes 	struct vop_print_args /* {
1830df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1831df8bae1dSRodney W. Grimes 	} */ *ap;
1832df8bae1dSRodney W. Grimes {
1833df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1834df8bae1dSRodney W. Grimes 
183506be2aaaSNate Lawson 	printf("\ttag %s, vp=%p, uppervp=%p, lowervp=%p\n", vp->v_tag,
1836df8bae1dSRodney W. Grimes 	       vp, UPPERVP(vp), LOWERVP(vp));
1837996c772fSJohn Dyson 	if (UPPERVP(vp) != NULLVP)
1838996c772fSJohn Dyson 		vprint("union: upper", UPPERVP(vp));
1839996c772fSJohn Dyson 	if (LOWERVP(vp) != NULLVP)
1840996c772fSJohn Dyson 		vprint("union: lower", LOWERVP(vp));
1841996c772fSJohn Dyson 
1842df8bae1dSRodney W. Grimes 	return (0);
1843df8bae1dSRodney W. Grimes }
1844df8bae1dSRodney W. Grimes 
1845c9bf0111SKATO Takenori static int
1846df8bae1dSRodney W. Grimes union_pathconf(ap)
1847df8bae1dSRodney W. Grimes 	struct vop_pathconf_args /* {
1848df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1849df8bae1dSRodney W. Grimes 		int a_name;
1850df8bae1dSRodney W. Grimes 		int *a_retval;
1851df8bae1dSRodney W. Grimes 	} */ *ap;
1852df8bae1dSRodney W. Grimes {
1853df8bae1dSRodney W. Grimes 	int error;
1854b40ce416SJulian Elischer 	struct thread *td = curthread;		/* XXX */
18552a31267eSMatthew Dillon 	struct union_node *un = VTOUNION(ap->a_vp);
18562a31267eSMatthew Dillon 	struct vnode *vp;
1857df8bae1dSRodney W. Grimes 
1858b40ce416SJulian Elischer 	vp = union_lock_other(un, td);
18592a31267eSMatthew Dillon 	KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
18602a31267eSMatthew Dillon 
1861996c772fSJohn Dyson 	ap->a_vp = vp;
1862996c772fSJohn Dyson 	error = VCALL(vp, VOFFSET(vop_pathconf), ap);
1863b40ce416SJulian Elischer 	union_unlock_other(vp, td);
1864df8bae1dSRodney W. Grimes 
1865df8bae1dSRodney W. Grimes 	return (error);
1866df8bae1dSRodney W. Grimes }
1867df8bae1dSRodney W. Grimes 
1868c9bf0111SKATO Takenori static int
1869df8bae1dSRodney W. Grimes union_advlock(ap)
1870df8bae1dSRodney W. Grimes 	struct vop_advlock_args /* {
1871df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
1872df8bae1dSRodney W. Grimes 		caddr_t  a_id;
1873df8bae1dSRodney W. Grimes 		int  a_op;
1874df8bae1dSRodney W. Grimes 		struct flock *a_fl;
1875df8bae1dSRodney W. Grimes 		int  a_flags;
1876df8bae1dSRodney W. Grimes 	} */ *ap;
1877df8bae1dSRodney W. Grimes {
1878996c772fSJohn Dyson 	register struct vnode *ovp = OTHERVP(ap->a_vp);
1879df8bae1dSRodney W. Grimes 
1880996c772fSJohn Dyson 	ap->a_vp = ovp;
1881996c772fSJohn Dyson 	return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1882df8bae1dSRodney W. Grimes }
1883df8bae1dSRodney W. Grimes 
1884df8bae1dSRodney W. Grimes 
1885df8bae1dSRodney W. Grimes /*
1886df8bae1dSRodney W. Grimes  * XXX - vop_strategy must be hand coded because it has no
18872a31267eSMatthew Dillon  * YYY - and it is not coherent with anything
18882a31267eSMatthew Dillon  *
1889df8bae1dSRodney W. Grimes  * vnode in its arguments.
1890df8bae1dSRodney W. Grimes  * This goes away with a merged VM/buffer cache.
1891df8bae1dSRodney W. Grimes  */
1892c9bf0111SKATO Takenori static int
1893df8bae1dSRodney W. Grimes union_strategy(ap)
1894df8bae1dSRodney W. Grimes 	struct vop_strategy_args /* {
1895fd5d1124SJulian Elischer 		struct vnode *a_vp;
1896df8bae1dSRodney W. Grimes 		struct buf *a_bp;
1897df8bae1dSRodney W. Grimes 	} */ *ap;
1898df8bae1dSRodney W. Grimes {
1899df8bae1dSRodney W. Grimes 	struct buf *bp = ap->a_bp;
1900f9c8cab5SKirk McKusick 	struct vnode *othervp = OTHERVP(bp->b_vp);
1901df8bae1dSRodney W. Grimes 
1902df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
1903f9c8cab5SKirk McKusick 	if (othervp == NULLVP)
1904df8bae1dSRodney W. Grimes 		panic("union_strategy: nil vp");
190521144e3bSPoul-Henning Kamp 	if ((bp->b_iocmd == BIO_WRITE) &&
1906f9c8cab5SKirk McKusick 	    (othervp == LOWERVP(bp->b_vp)))
1907df8bae1dSRodney W. Grimes 		panic("union_strategy: writing to lowervp");
1908df8bae1dSRodney W. Grimes #endif
1909f9c8cab5SKirk McKusick 	return (VOP_STRATEGY(othervp, bp));
1910df8bae1dSRodney W. Grimes }
1911df8bae1dSRodney W. Grimes 
1912df8bae1dSRodney W. Grimes /*
1913df8bae1dSRodney W. Grimes  * Global vfs data structures
1914df8bae1dSRodney W. Grimes  */
1915f57e6547SBruce Evans vop_t **union_vnodeop_p;
1916c109c577SBruce Evans static struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1917dba3870cSPoul-Henning Kamp 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1918539ef70cSPoul-Henning Kamp 	{ &vop_access_desc,		(vop_t *) union_access },
1919539ef70cSPoul-Henning Kamp 	{ &vop_advlock_desc,		(vop_t *) union_advlock },
1920b7ebffbcSPoul-Henning Kamp 	{ &vop_bmap_desc,		(vop_t *) vop_eopnotsupp },
1921539ef70cSPoul-Henning Kamp 	{ &vop_close_desc,		(vop_t *) union_close },
1922539ef70cSPoul-Henning Kamp 	{ &vop_create_desc,		(vop_t *) union_create },
19233413421bSBoris Popov 	{ &vop_createvobject_desc,	(vop_t *) union_createvobject },
19243413421bSBoris Popov 	{ &vop_destroyvobject_desc,	(vop_t *) union_destroyvobject },
1925539ef70cSPoul-Henning Kamp 	{ &vop_fsync_desc,		(vop_t *) union_fsync },
1926539ef70cSPoul-Henning Kamp 	{ &vop_getattr_desc,		(vop_t *) union_getattr },
19273413421bSBoris Popov 	{ &vop_getvobject_desc,		(vop_t *) union_getvobject },
1928539ef70cSPoul-Henning Kamp 	{ &vop_inactive_desc,		(vop_t *) union_inactive },
1929539ef70cSPoul-Henning Kamp 	{ &vop_ioctl_desc,		(vop_t *) union_ioctl },
19302a31267eSMatthew Dillon 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
1931539ef70cSPoul-Henning Kamp 	{ &vop_lease_desc,		(vop_t *) union_lease },
1932539ef70cSPoul-Henning Kamp 	{ &vop_link_desc,		(vop_t *) union_link },
1933539ef70cSPoul-Henning Kamp 	{ &vop_lock_desc,		(vop_t *) union_lock },
1934539ef70cSPoul-Henning Kamp 	{ &vop_lookup_desc,		(vop_t *) union_lookup },
1935539ef70cSPoul-Henning Kamp 	{ &vop_mkdir_desc,		(vop_t *) union_mkdir },
1936539ef70cSPoul-Henning Kamp 	{ &vop_mknod_desc,		(vop_t *) union_mknod },
1937539ef70cSPoul-Henning Kamp 	{ &vop_open_desc,		(vop_t *) union_open },
1938539ef70cSPoul-Henning Kamp 	{ &vop_pathconf_desc,		(vop_t *) union_pathconf },
1939539ef70cSPoul-Henning Kamp 	{ &vop_poll_desc,		(vop_t *) union_poll },
1940539ef70cSPoul-Henning Kamp 	{ &vop_print_desc,		(vop_t *) union_print },
1941539ef70cSPoul-Henning Kamp 	{ &vop_read_desc,		(vop_t *) union_read },
1942539ef70cSPoul-Henning Kamp 	{ &vop_readdir_desc,		(vop_t *) union_readdir },
1943539ef70cSPoul-Henning Kamp 	{ &vop_readlink_desc,		(vop_t *) union_readlink },
1944f2a2857bSKirk McKusick 	{ &vop_getwritemount_desc,	(vop_t *) union_getwritemount },
1945539ef70cSPoul-Henning Kamp 	{ &vop_reclaim_desc,		(vop_t *) union_reclaim },
1946539ef70cSPoul-Henning Kamp 	{ &vop_remove_desc,		(vop_t *) union_remove },
1947539ef70cSPoul-Henning Kamp 	{ &vop_rename_desc,		(vop_t *) union_rename },
1948539ef70cSPoul-Henning Kamp 	{ &vop_revoke_desc,		(vop_t *) union_revoke },
1949539ef70cSPoul-Henning Kamp 	{ &vop_rmdir_desc,		(vop_t *) union_rmdir },
1950539ef70cSPoul-Henning Kamp 	{ &vop_setattr_desc,		(vop_t *) union_setattr },
1951539ef70cSPoul-Henning Kamp 	{ &vop_strategy_desc,		(vop_t *) union_strategy },
1952539ef70cSPoul-Henning Kamp 	{ &vop_symlink_desc,		(vop_t *) union_symlink },
1953539ef70cSPoul-Henning Kamp 	{ &vop_unlock_desc,		(vop_t *) union_unlock },
1954539ef70cSPoul-Henning Kamp 	{ &vop_whiteout_desc,		(vop_t *) union_whiteout },
1955539ef70cSPoul-Henning Kamp 	{ &vop_write_desc,		(vop_t *) union_write },
1956f57e6547SBruce Evans 	{ NULL, NULL }
1957df8bae1dSRodney W. Grimes };
1958c109c577SBruce Evans static struct vnodeopv_desc union_vnodeop_opv_desc =
1959df8bae1dSRodney W. Grimes 	{ &union_vnodeop_p, union_vnodeop_entries };
1960c901836cSGarrett Wollman 
1961c901836cSGarrett Wollman VNODEOP_SET(union_vnodeop_opv_desc);
1962