xref: /freebsd/sys/fs/unionfs/union_subr.c (revision 9b505845a3aefd2ffaef77764aeca092653f0772)
1d167cf6fSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1994 Jan-Simon Pendry
5df8bae1dSRodney W. Grimes  * Copyright (c) 1994
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7cb5736b7SDaichi GOTO  * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
8cb5736b7SDaichi GOTO  * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. 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  */
37df8bae1dSRodney W. Grimes 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40fb919e4dSMark Murray #include <sys/kernel.h>
41daec9284SConrad Meyer #include <sys/ktr.h>
42fb919e4dSMark Murray #include <sys/lock.h>
432178ff8bSJohn Baldwin #include <sys/mutex.h>
44d00947d8SCraig Rodrigues #include <sys/malloc.h>
45d00947d8SCraig Rodrigues #include <sys/mount.h>
46fb919e4dSMark Murray #include <sys/namei.h>
47d00947d8SCraig Rodrigues #include <sys/proc.h>
48fb919e4dSMark Murray #include <sys/vnode.h>
49d00947d8SCraig Rodrigues #include <sys/dirent.h>
50d00947d8SCraig Rodrigues #include <sys/fcntl.h>
51d00947d8SCraig Rodrigues #include <sys/filedesc.h>
52d00947d8SCraig Rodrigues #include <sys/stat.h>
53372691a7SJason A. Harmening #include <sys/sysctl.h>
54372691a7SJason A. Harmening #include <sys/taskqueue.h>
55dce36a01SEdward Tomasz Napierala #include <sys/resourcevar.h>
56fb919e4dSMark Murray 
57d877dd57SJason A. Harmening #include <machine/atomic.h>
58d877dd57SJason A. Harmening 
59bcf11e8dSRobert Watson #include <security/mac/mac_framework.h>
60d00947d8SCraig Rodrigues 
618396dd9eSJeff Roberson #include <vm/uma.h>
62fb919e4dSMark Murray 
6399d300a1SRuslan Ermilov #include <fs/unionfs/union.h>
64df8bae1dSRodney W. Grimes 
65a9b794ffSDaichi GOTO #define NUNIONFSNODECACHE 16
66fd8ad212SJason A. Harmening #define UNIONFSHASHMASK (NUNIONFSNODECACHE - 1)
67a9b794ffSDaichi GOTO 
68a9b794ffSDaichi GOTO static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table");
69d00947d8SCraig Rodrigues MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part");
70d00947d8SCraig Rodrigues MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
71df8bae1dSRodney W. Grimes 
72372691a7SJason A. Harmening static struct task unionfs_deferred_rele_task;
73372691a7SJason A. Harmening static struct mtx unionfs_deferred_rele_lock;
74372691a7SJason A. Harmening static STAILQ_HEAD(, unionfs_node) unionfs_deferred_rele_list =
75372691a7SJason A. Harmening     STAILQ_HEAD_INITIALIZER(unionfs_deferred_rele_list);
76372691a7SJason A. Harmening static TASKQUEUE_DEFINE_THREAD(unionfs_rele);
77372691a7SJason A. Harmening 
78372691a7SJason A. Harmening unsigned int unionfs_ndeferred = 0;
79372691a7SJason A. Harmening SYSCTL_UINT(_vfs, OID_AUTO, unionfs_ndeferred, CTLFLAG_RD,
80372691a7SJason A. Harmening     &unionfs_ndeferred, 0, "unionfs deferred vnode release");
81372691a7SJason A. Harmening 
82372691a7SJason A. Harmening static void unionfs_deferred_rele(void *, int);
83372691a7SJason A. Harmening 
84d00947d8SCraig Rodrigues /*
85dc2dd185SDaichi GOTO  * Initialize
86d00947d8SCraig Rodrigues  */
87df8bae1dSRodney W. Grimes int
unionfs_init(struct vfsconf * vfsp)88d00947d8SCraig Rodrigues unionfs_init(struct vfsconf *vfsp)
89df8bae1dSRodney W. Grimes {
90d00947d8SCraig Rodrigues 	UNIONFSDEBUG("unionfs_init\n");	/* printed during system boot */
91372691a7SJason A. Harmening 	TASK_INIT(&unionfs_deferred_rele_task, 0, unionfs_deferred_rele, NULL);
92372691a7SJason A. Harmening 	mtx_init(&unionfs_deferred_rele_lock, "uniondefr", NULL, MTX_DEF);
9326f9a767SRodney W. Grimes 	return (0);
94df8bae1dSRodney W. Grimes }
95df8bae1dSRodney W. Grimes 
96d00947d8SCraig Rodrigues /*
97dc2dd185SDaichi GOTO  * Uninitialize
98d00947d8SCraig Rodrigues  */
99d00947d8SCraig Rodrigues int
unionfs_uninit(struct vfsconf * vfsp)100d00947d8SCraig Rodrigues unionfs_uninit(struct vfsconf *vfsp)
101df8bae1dSRodney W. Grimes {
102372691a7SJason A. Harmening 	taskqueue_quiesce(taskqueue_unionfs_rele);
103372691a7SJason A. Harmening 	taskqueue_free(taskqueue_unionfs_rele);
104372691a7SJason A. Harmening 	mtx_destroy(&unionfs_deferred_rele_lock);
105df8bae1dSRodney W. Grimes 	return (0);
106df8bae1dSRodney W. Grimes }
107df8bae1dSRodney W. Grimes 
108372691a7SJason A. Harmening static void
unionfs_deferred_rele(void * arg __unused,int pending __unused)109372691a7SJason A. Harmening unionfs_deferred_rele(void *arg __unused, int pending __unused)
110372691a7SJason A. Harmening {
111372691a7SJason A. Harmening 	STAILQ_HEAD(, unionfs_node) local_rele_list;
112372691a7SJason A. Harmening 	struct unionfs_node *unp, *tunp;
113372691a7SJason A. Harmening 	unsigned int ndeferred;
114372691a7SJason A. Harmening 
115372691a7SJason A. Harmening 	ndeferred = 0;
116372691a7SJason A. Harmening 	STAILQ_INIT(&local_rele_list);
117372691a7SJason A. Harmening 	mtx_lock(&unionfs_deferred_rele_lock);
118372691a7SJason A. Harmening 	STAILQ_CONCAT(&local_rele_list, &unionfs_deferred_rele_list);
119372691a7SJason A. Harmening 	mtx_unlock(&unionfs_deferred_rele_lock);
120372691a7SJason A. Harmening 	STAILQ_FOREACH_SAFE(unp, &local_rele_list, un_rele, tunp) {
121372691a7SJason A. Harmening 		++ndeferred;
122372691a7SJason A. Harmening 		MPASS(unp->un_dvp != NULL);
123372691a7SJason A. Harmening 		vrele(unp->un_dvp);
124372691a7SJason A. Harmening 		free(unp, M_UNIONFSNODE);
125372691a7SJason A. Harmening 	}
126372691a7SJason A. Harmening 
127372691a7SJason A. Harmening 	/* We expect this function to be single-threaded, thus no atomic */
128372691a7SJason A. Harmening 	unionfs_ndeferred += ndeferred;
129372691a7SJason A. Harmening }
130372691a7SJason A. Harmening 
131a9b794ffSDaichi GOTO static struct unionfs_node_hashhead *
unionfs_get_hashhead(struct vnode * dvp,struct vnode * lookup)132fd8ad212SJason A. Harmening unionfs_get_hashhead(struct vnode *dvp, struct vnode *lookup)
133a9b794ffSDaichi GOTO {
134312d49efSJason A. Harmening 	struct unionfs_node *unp;
135a9b794ffSDaichi GOTO 
136a9b794ffSDaichi GOTO 	unp = VTOUNIONFS(dvp);
137fd8ad212SJason A. Harmening 
138fd8ad212SJason A. Harmening 	return (&(unp->un_hashtbl[vfs_hash_index(lookup) & UNIONFSHASHMASK]));
139a9b794ffSDaichi GOTO }
140a9b794ffSDaichi GOTO 
141fd8ad212SJason A. Harmening /*
142fd8ad212SJason A. Harmening  * Attempt to lookup a cached unionfs vnode by upper/lower vp
143fd8ad212SJason A. Harmening  * from dvp, with dvp's interlock held.
144fd8ad212SJason A. Harmening  */
145fd8ad212SJason A. Harmening static struct vnode *
unionfs_get_cached_vnode_locked(struct vnode * lookup,struct vnode * dvp)146fd8ad212SJason A. Harmening unionfs_get_cached_vnode_locked(struct vnode *lookup, struct vnode *dvp)
147fd8ad212SJason A. Harmening {
148fd8ad212SJason A. Harmening 	struct unionfs_node *unp;
149fd8ad212SJason A. Harmening 	struct unionfs_node_hashhead *hd;
150fd8ad212SJason A. Harmening 	struct vnode *vp;
151fd8ad212SJason A. Harmening 
152fd8ad212SJason A. Harmening 	hd = unionfs_get_hashhead(dvp, lookup);
153fd8ad212SJason A. Harmening 
154fd8ad212SJason A. Harmening 	LIST_FOREACH(unp, hd, un_hash) {
1553ecefc4aSJason A. Harmening 		if (unp->un_uppervp == lookup ||
1563ecefc4aSJason A. Harmening 		    unp->un_lowervp == lookup) {
157fd8ad212SJason A. Harmening 			vp = UNIONFSTOV(unp);
158fd8ad212SJason A. Harmening 			VI_LOCK_FLAGS(vp, MTX_DUPOK);
159fd8ad212SJason A. Harmening 			vp->v_iflag &= ~VI_OWEINACT;
160fd8ad212SJason A. Harmening 			if (VN_IS_DOOMED(vp) ||
161fd8ad212SJason A. Harmening 			    ((vp->v_iflag & VI_DOINGINACT) != 0)) {
162fd8ad212SJason A. Harmening 				VI_UNLOCK(vp);
163fd8ad212SJason A. Harmening 				vp = NULLVP;
164fd8ad212SJason A. Harmening 			} else {
165fd8ad212SJason A. Harmening 				vrefl(vp);
166fd8ad212SJason A. Harmening 				VI_UNLOCK(vp);
167a9b794ffSDaichi GOTO 			}
168fd8ad212SJason A. Harmening 			return (vp);
169fd8ad212SJason A. Harmening 		}
170fd8ad212SJason A. Harmening 	}
171fd8ad212SJason A. Harmening 
172fd8ad212SJason A. Harmening 	return (NULLVP);
173fd8ad212SJason A. Harmening }
174fd8ad212SJason A. Harmening 
175a9b794ffSDaichi GOTO 
176a9b794ffSDaichi GOTO /*
1773af387c9SDaichi GOTO  * Get the cached vnode.
178a9b794ffSDaichi GOTO  */
179a9b794ffSDaichi GOTO static struct vnode *
unionfs_get_cached_vnode(struct vnode * uvp,struct vnode * lvp,struct vnode * dvp)1803af387c9SDaichi GOTO unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp,
181fd8ad212SJason A. Harmening     struct vnode *dvp)
182a9b794ffSDaichi GOTO {
183a9b794ffSDaichi GOTO 	struct vnode *vp;
184a9b794ffSDaichi GOTO 
185a9b794ffSDaichi GOTO 	vp = NULLVP;
186fd8ad212SJason A. Harmening 	VI_LOCK(dvp);
187fd8ad212SJason A. Harmening 	if (uvp != NULLVP)
188fd8ad212SJason A. Harmening 		vp = unionfs_get_cached_vnode_locked(uvp, dvp);
189fd8ad212SJason A. Harmening 	else if (lvp != NULLVP)
190fd8ad212SJason A. Harmening 		vp = unionfs_get_cached_vnode_locked(lvp, dvp);
191a9b794ffSDaichi GOTO 	VI_UNLOCK(dvp);
192a9b794ffSDaichi GOTO 
193fd8ad212SJason A. Harmening 	return (vp);
194a9b794ffSDaichi GOTO }
195a9b794ffSDaichi GOTO 
196a9b794ffSDaichi GOTO /*
1973af387c9SDaichi GOTO  * Add the new vnode into cache.
198a9b794ffSDaichi GOTO  */
199a9b794ffSDaichi GOTO static struct vnode *
unionfs_ins_cached_vnode(struct unionfs_node * uncp,struct vnode * dvp)2003af387c9SDaichi GOTO unionfs_ins_cached_vnode(struct unionfs_node *uncp,
201fd8ad212SJason A. Harmening     struct vnode *dvp)
202a9b794ffSDaichi GOTO {
203a9b794ffSDaichi GOTO 	struct unionfs_node_hashhead *hd;
204a9b794ffSDaichi GOTO 	struct vnode *vp;
205a9b794ffSDaichi GOTO 
206a9b794ffSDaichi GOTO 	vp = NULLVP;
207fd8ad212SJason A. Harmening 	VI_LOCK(dvp);
208eb60ff1eSJason A. Harmening 	if (uncp->un_uppervp != NULLVP) {
209eb60ff1eSJason A. Harmening 		ASSERT_VOP_ELOCKED(uncp->un_uppervp, __func__);
210eb60ff1eSJason A. Harmening 		KASSERT(uncp->un_uppervp->v_type == VDIR,
211eb60ff1eSJason A. Harmening 		    ("%s: v_type != VDIR", __func__));
212fd8ad212SJason A. Harmening 		vp = unionfs_get_cached_vnode_locked(uncp->un_uppervp, dvp);
213eb60ff1eSJason A. Harmening 	} else if (uncp->un_lowervp != NULLVP) {
214eb60ff1eSJason A. Harmening 		ASSERT_VOP_ELOCKED(uncp->un_lowervp, __func__);
215eb60ff1eSJason A. Harmening 		KASSERT(uncp->un_lowervp->v_type == VDIR,
216eb60ff1eSJason A. Harmening 		    ("%s: v_type != VDIR", __func__));
217fd8ad212SJason A. Harmening 		vp = unionfs_get_cached_vnode_locked(uncp->un_lowervp, dvp);
218eb60ff1eSJason A. Harmening 	}
219fd8ad212SJason A. Harmening 	if (vp == NULLVP) {
220fd8ad212SJason A. Harmening 		hd = unionfs_get_hashhead(dvp, (uncp->un_uppervp != NULLVP ?
221fd8ad212SJason A. Harmening 		    uncp->un_uppervp : uncp->un_lowervp));
222a9b794ffSDaichi GOTO 		LIST_INSERT_HEAD(hd, uncp, un_hash);
223fd8ad212SJason A. Harmening 	}
224a9b794ffSDaichi GOTO 	VI_UNLOCK(dvp);
225a9b794ffSDaichi GOTO 
226fd8ad212SJason A. Harmening 	return (vp);
227a9b794ffSDaichi GOTO }
228a9b794ffSDaichi GOTO 
229a9b794ffSDaichi GOTO /*
2303af387c9SDaichi GOTO  * Remove the vnode.
231a9b794ffSDaichi GOTO  */
232a9b794ffSDaichi GOTO static void
unionfs_rem_cached_vnode(struct unionfs_node * unp,struct vnode * dvp)2333af387c9SDaichi GOTO unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp)
234a9b794ffSDaichi GOTO {
2353ecefc4aSJason A. Harmening 	KASSERT(unp != NULL, ("%s: null node", __func__));
2363ecefc4aSJason A. Harmening 	KASSERT(dvp != NULLVP,
2373ecefc4aSJason A. Harmening 	    ("%s: null parent vnode", __func__));
238a9b794ffSDaichi GOTO 
239a9b794ffSDaichi GOTO 	VI_LOCK(dvp);
240fd8ad212SJason A. Harmening 	if (unp->un_hash.le_prev != NULL) {
241a9b794ffSDaichi GOTO 		LIST_REMOVE(unp, un_hash);
2423af387c9SDaichi GOTO 		unp->un_hash.le_next = NULL;
2433af387c9SDaichi GOTO 		unp->un_hash.le_prev = NULL;
244fd8ad212SJason A. Harmening 	}
245a9b794ffSDaichi GOTO 	VI_UNLOCK(dvp);
246a9b794ffSDaichi GOTO }
247a9b794ffSDaichi GOTO 
248d00947d8SCraig Rodrigues /*
249f9e28f90SJason A. Harmening  * Common cleanup handling for unionfs_nodeget
250f9e28f90SJason A. Harmening  * Upper, lower, and parent directory vnodes are expected to be referenced by
251f9e28f90SJason A. Harmening  * the caller.  Upper and lower vnodes, if non-NULL, are also expected to be
252f9e28f90SJason A. Harmening  * exclusively locked by the caller.
253f9e28f90SJason A. Harmening  * This function will return with the caller's locks and references undone.
254f9e28f90SJason A. Harmening  */
255f9e28f90SJason A. Harmening static void
unionfs_nodeget_cleanup(struct vnode * vp,struct unionfs_node * unp)2563150cf0cSMateusz Guzik unionfs_nodeget_cleanup(struct vnode *vp, struct unionfs_node *unp)
257f9e28f90SJason A. Harmening {
258f9e28f90SJason A. Harmening 
259f9e28f90SJason A. Harmening 	/*
260f9e28f90SJason A. Harmening 	 * Lock and reset the default vnode lock; vgone() expects a locked
261f9e28f90SJason A. Harmening 	 * vnode, and we're going to reset the vnode ops.
262f9e28f90SJason A. Harmening 	 */
263f9e28f90SJason A. Harmening 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
264f9e28f90SJason A. Harmening 
265f9e28f90SJason A. Harmening 	/*
266f9e28f90SJason A. Harmening 	 * Clear out private data and reset the vnode ops to avoid use of
267f9e28f90SJason A. Harmening 	 * unionfs vnode ops on a partially constructed vnode.
268f9e28f90SJason A. Harmening 	 */
269f9e28f90SJason A. Harmening 	VI_LOCK(vp);
270f9e28f90SJason A. Harmening 	vp->v_data = NULL;
271f9e28f90SJason A. Harmening 	vp->v_vnlock = &vp->v_lock;
272f9e28f90SJason A. Harmening 	vp->v_op = &dead_vnodeops;
273f9e28f90SJason A. Harmening 	VI_UNLOCK(vp);
274f9e28f90SJason A. Harmening 	vgone(vp);
275f9e28f90SJason A. Harmening 	vput(vp);
276f9e28f90SJason A. Harmening 
277f9e28f90SJason A. Harmening 	if (unp->un_dvp != NULLVP)
278f9e28f90SJason A. Harmening 		vrele(unp->un_dvp);
279eb60ff1eSJason A. Harmening 	if (unp->un_uppervp != NULLVP) {
280f9e28f90SJason A. Harmening 		vput(unp->un_uppervp);
281f9e28f90SJason A. Harmening 		if (unp->un_lowervp != NULLVP)
282eb60ff1eSJason A. Harmening 			vrele(unp->un_lowervp);
283eb60ff1eSJason A. Harmening 	} else if (unp->un_lowervp != NULLVP)
284f9e28f90SJason A. Harmening 		vput(unp->un_lowervp);
285f9e28f90SJason A. Harmening 	if (unp->un_hashtbl != NULL)
286fd8ad212SJason A. Harmening 		hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
287f9e28f90SJason A. Harmening 	free(unp->un_path, M_UNIONFSPATH);
288f9e28f90SJason A. Harmening 	free(unp, M_UNIONFSNODE);
289f9e28f90SJason A. Harmening }
290f9e28f90SJason A. Harmening 
291f9e28f90SJason A. Harmening /*
292d00947d8SCraig Rodrigues  * Make a new or get existing unionfs node.
2932a31267eSMatthew Dillon  *
294d00947d8SCraig Rodrigues  * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
295d00947d8SCraig Rodrigues  * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
296d00947d8SCraig Rodrigues  * you should not lock plurality simultaneously.
297df8bae1dSRodney W. Grimes  */
298d00947d8SCraig Rodrigues int
unionfs_nodeget(struct mount * mp,struct vnode * uppervp,struct vnode * lowervp,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp)299d00947d8SCraig Rodrigues unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
300312d49efSJason A. Harmening     struct vnode *lowervp, struct vnode *dvp, struct vnode **vpp,
3016d8420d4SJason A. Harmening     struct componentname *cnp)
302d00947d8SCraig Rodrigues {
303312d49efSJason A. Harmening 	char	       *path;
304d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
305d00947d8SCraig Rodrigues 	struct unionfs_node *unp;
306d00947d8SCraig Rodrigues 	struct vnode   *vp;
307fd8ad212SJason A. Harmening 	u_long		hashmask;
308d00947d8SCraig Rodrigues 	int		error;
309d00947d8SCraig Rodrigues 	int		lkflags;
310ba8cc6d7SMateusz Guzik 	__enum_uint8(vtype)	vt;
311df8bae1dSRodney W. Grimes 
312f9e28f90SJason A. Harmening 	error = 0;
313d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(mp);
314d00947d8SCraig Rodrigues 	lkflags = (cnp ? cnp->cn_lkflags : 0);
315dc2dd185SDaichi GOTO 	path = (cnp ? cnp->cn_nameptr : NULL);
316a9b794ffSDaichi GOTO 	*vpp = NULLVP;
3172a31267eSMatthew Dillon 
318d00947d8SCraig Rodrigues 	if (uppervp == NULLVP && lowervp == NULLVP)
319eb60ff1eSJason A. Harmening 		panic("%s: upper and lower are both null", __func__);
320d00947d8SCraig Rodrigues 
321a9b794ffSDaichi GOTO 	vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
322a9b794ffSDaichi GOTO 
323d00947d8SCraig Rodrigues 	/* If it has no ISLASTCN flag, path check is skipped. */
324dc2dd185SDaichi GOTO 	if (cnp && !(cnp->cn_flags & ISLASTCN))
325d00947d8SCraig Rodrigues 		path = NULL;
326d00947d8SCraig Rodrigues 
3273af387c9SDaichi GOTO 	/* check the cache */
328fd8ad212SJason A. Harmening 	if (dvp != NULLVP && vt == VDIR) {
329fd8ad212SJason A. Harmening 		vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp);
330a9b794ffSDaichi GOTO 		if (vp != NULLVP) {
331a9b794ffSDaichi GOTO 			*vpp = vp;
332eb60ff1eSJason A. Harmening 			if (lkflags != 0)
333eb60ff1eSJason A. Harmening 				vn_lock(*vpp, lkflags | LK_RETRY);
334eb60ff1eSJason A. Harmening 			return (0);
335a9b794ffSDaichi GOTO 		}
336a9b794ffSDaichi GOTO 	}
337a9b794ffSDaichi GOTO 
3381ede983cSDag-Erling Smørgrav 	unp = malloc(sizeof(struct unionfs_node),
339d00947d8SCraig Rodrigues 	    M_UNIONFSNODE, M_WAITOK | M_ZERO);
340d00947d8SCraig Rodrigues 
341d00947d8SCraig Rodrigues 	error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp);
342dc2dd185SDaichi GOTO 	if (error != 0) {
3431ede983cSDag-Erling Smørgrav 		free(unp, M_UNIONFSNODE);
344d00947d8SCraig Rodrigues 		return (error);
345d00947d8SCraig Rodrigues 	}
346d00947d8SCraig Rodrigues 	if (dvp != NULLVP)
347d00947d8SCraig Rodrigues 		vref(dvp);
348d00947d8SCraig Rodrigues 	if (uppervp != NULLVP)
349d00947d8SCraig Rodrigues 		vref(uppervp);
350d00947d8SCraig Rodrigues 	if (lowervp != NULLVP)
351d00947d8SCraig Rodrigues 		vref(lowervp);
352d00947d8SCraig Rodrigues 
353fd8ad212SJason A. Harmening 	if (vt == VDIR) {
354a9b794ffSDaichi GOTO 		unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH,
355fd8ad212SJason A. Harmening 		    &hashmask);
356fd8ad212SJason A. Harmening 		KASSERT(hashmask == UNIONFSHASHMASK,
357fd8ad212SJason A. Harmening 		    ("unexpected unionfs hash mask 0x%lx", hashmask));
358fd8ad212SJason A. Harmening 	}
359a9b794ffSDaichi GOTO 
360d00947d8SCraig Rodrigues 	unp->un_vnode = vp;
361d00947d8SCraig Rodrigues 	unp->un_uppervp = uppervp;
362d00947d8SCraig Rodrigues 	unp->un_lowervp = lowervp;
363d00947d8SCraig Rodrigues 	unp->un_dvp = dvp;
364d00947d8SCraig Rodrigues 	if (uppervp != NULLVP)
365d00947d8SCraig Rodrigues 		vp->v_vnlock = uppervp->v_vnlock;
366d00947d8SCraig Rodrigues 	else
367d00947d8SCraig Rodrigues 		vp->v_vnlock = lowervp->v_vnlock;
368d00947d8SCraig Rodrigues 
369dc2dd185SDaichi GOTO 	if (path != NULL) {
370312d49efSJason A. Harmening 		unp->un_path = malloc(cnp->cn_namelen + 1,
371312d49efSJason A. Harmening 		    M_UNIONFSPATH, M_WAITOK | M_ZERO);
372d00947d8SCraig Rodrigues 		bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen);
373d00947d8SCraig Rodrigues 		unp->un_path[cnp->cn_namelen] = '\0';
374abe95116SJason A. Harmening 		unp->un_pathlen = cnp->cn_namelen;
375d00947d8SCraig Rodrigues 	}
376a9b794ffSDaichi GOTO 	vp->v_type = vt;
377d00947d8SCraig Rodrigues 	vp->v_data = unp;
378d00947d8SCraig Rodrigues 
379a01ca46bSJason A. Harmening 	/*
380a01ca46bSJason A. Harmening 	 * TODO: This is an imperfect check, as there's no guarantee that
381a01ca46bSJason A. Harmening 	 * the underlying filesystems will always return vnode pointers
382a01ca46bSJason A. Harmening 	 * for the root inodes that match our cached values.  To reduce
383a01ca46bSJason A. Harmening 	 * the likelihood of failure, for example in the case where either
384a01ca46bSJason A. Harmening 	 * vnode has been forcibly doomed, we check both pointers and set
385a01ca46bSJason A. Harmening 	 * VV_ROOT if either matches.
386a01ca46bSJason A. Harmening 	 */
387a01ca46bSJason A. Harmening 	if (ump->um_uppervp == uppervp || ump->um_lowervp == lowervp)
388d00947d8SCraig Rodrigues 		vp->v_vflag |= VV_ROOT;
389a01ca46bSJason A. Harmening 	KASSERT(dvp != NULL || (vp->v_vflag & VV_ROOT) != 0,
390a01ca46bSJason A. Harmening 	    ("%s: NULL dvp for non-root vp %p", __func__, vp));
391d00947d8SCraig Rodrigues 
392eb60ff1eSJason A. Harmening 
393eb60ff1eSJason A. Harmening 	/*
394eb60ff1eSJason A. Harmening 	 * NOTE: There is still a possibility for cross-filesystem locking here.
395eb60ff1eSJason A. Harmening 	 * If dvp has an upper FS component and is locked, while the new vnode
396eb60ff1eSJason A. Harmening 	 * created here only has a lower-layer FS component, then we will end
397eb60ff1eSJason A. Harmening 	 * up taking a lower-FS lock while holding an upper-FS lock.
398eb60ff1eSJason A. Harmening 	 * That situation could be dealt with here using vn_lock_pair().
399eb60ff1eSJason A. Harmening 	 * However, that would only address one instance out of many in which
400eb60ff1eSJason A. Harmening 	 * a child vnode lock is taken while holding a lock on its parent
401eb60ff1eSJason A. Harmening 	 * directory. This is done in many places in common VFS code, as well as
402eb60ff1eSJason A. Harmening 	 * a few places within unionfs (which could lead to the same cross-FS
403eb60ff1eSJason A. Harmening 	 * locking issue if, for example, the upper FS is another nested unionfs
404eb60ff1eSJason A. Harmening 	 * instance).  Additionally, it is unclear under what circumstances this
405eb60ff1eSJason A. Harmening 	 * specific lock sequence (a directory on one FS followed by a child of
406eb60ff1eSJason A. Harmening 	 * its 'peer' directory on another FS) would present the practical
407eb60ff1eSJason A. Harmening 	 * possibility of deadlock due to some other agent on the system
408eb60ff1eSJason A. Harmening 	 * attempting to lock those two specific vnodes in the opposite order.
409eb60ff1eSJason A. Harmening 	 */
410eb60ff1eSJason A. Harmening 	if (uppervp != NULLVP)
411eb60ff1eSJason A. Harmening 		vn_lock(uppervp, LK_EXCLUSIVE | LK_RETRY);
412eb60ff1eSJason A. Harmening 	else
413eb60ff1eSJason A. Harmening 		vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY);
41466c5fbcaSKonstantin Belousov 	error = insmntque1(vp, mp);
4153150cf0cSMateusz Guzik 	if (error != 0) {
4163150cf0cSMateusz Guzik 		unionfs_nodeget_cleanup(vp, unp);
417f9e28f90SJason A. Harmening 		return (error);
4183150cf0cSMateusz Guzik 	}
419eb60ff1eSJason A. Harmening 	/*
420eb60ff1eSJason A. Harmening 	 * lowervp and uppervp should only be doomed by a forced unmount of
421eb60ff1eSJason A. Harmening 	 * their respective filesystems, but that can only happen if the
422eb60ff1eSJason A. Harmening 	 * unionfs instance is first unmounted.  We also effectively hold the
423eb60ff1eSJason A. Harmening 	 * lock on the new unionfs vnode at this point.  Therefore, if a
424eb60ff1eSJason A. Harmening 	 * unionfs umount has not yet reached the point at which the above
425eb60ff1eSJason A. Harmening 	 * insmntque1() would fail, then its vflush() call will end up
426eb60ff1eSJason A. Harmening 	 * blocked on our vnode lock, effectively also preventing unmount
427eb60ff1eSJason A. Harmening 	 * of the underlying filesystems.
428eb60ff1eSJason A. Harmening 	 */
429eb60ff1eSJason A. Harmening 	VNASSERT(lowervp == NULLVP || !VN_IS_DOOMED(lowervp), vp,
430eb60ff1eSJason A. Harmening 	    ("%s: doomed lowervp %p", __func__, lowervp));
431eb60ff1eSJason A. Harmening 	VNASSERT(uppervp == NULLVP || !VN_IS_DOOMED(uppervp), vp,
432eb60ff1eSJason A. Harmening 	    ("%s: doomed lowervp %p", __func__, uppervp));
433fd8ad212SJason A. Harmening 
434829f0bcbSMateusz Guzik 	vn_set_state(vp, VSTATE_CONSTRUCTED);
435829f0bcbSMateusz Guzik 
436fd8ad212SJason A. Harmening 	if (dvp != NULLVP && vt == VDIR)
437fd8ad212SJason A. Harmening 		*vpp = unionfs_ins_cached_vnode(unp, dvp);
438f9e28f90SJason A. Harmening 	if (*vpp != NULLVP) {
439f9e28f90SJason A. Harmening 		unionfs_nodeget_cleanup(vp, unp);
440eb60ff1eSJason A. Harmening 		if (lkflags != 0)
441eb60ff1eSJason A. Harmening 			vn_lock(*vpp, lkflags | LK_RETRY);
442eb60ff1eSJason A. Harmening 		return (0);
443eb60ff1eSJason A. Harmening 	} else
444a9b794ffSDaichi GOTO 		*vpp = vp;
445a9b794ffSDaichi GOTO 
446eb60ff1eSJason A. Harmening 	if ((lkflags & LK_SHARED) != 0)
447eb60ff1eSJason A. Harmening 		vn_lock(vp, LK_DOWNGRADE);
448eb60ff1eSJason A. Harmening 	else if ((lkflags & LK_EXCLUSIVE) == 0)
449eb60ff1eSJason A. Harmening 		VOP_UNLOCK(vp);
450df8bae1dSRodney W. Grimes 
451d00947d8SCraig Rodrigues 	return (0);
452996c772fSJohn Dyson }
453df8bae1dSRodney W. Grimes 
4542a31267eSMatthew Dillon /*
455dc2dd185SDaichi GOTO  * Clean up the unionfs node.
4562a31267eSMatthew Dillon  */
457d00947d8SCraig Rodrigues void
unionfs_noderem(struct vnode * vp)4586d8420d4SJason A. Harmening unionfs_noderem(struct vnode *vp)
459d00947d8SCraig Rodrigues {
4603af387c9SDaichi GOTO 	struct unionfs_node *unp, *unp_t1, *unp_t2;
4613af387c9SDaichi GOTO 	struct unionfs_node_hashhead *hd;
462acc4bab1SCraig Rodrigues 	struct unionfs_node_status *unsp, *unsp_tmp;
463d00947d8SCraig Rodrigues 	struct vnode   *lvp;
464d00947d8SCraig Rodrigues 	struct vnode   *uvp;
465a9b794ffSDaichi GOTO 	struct vnode   *dvp;
466312d49efSJason A. Harmening 	int		count;
467d877dd57SJason A. Harmening 	int		writerefs;
468eb60ff1eSJason A. Harmening 	bool		unlock_lvp;
4692a31267eSMatthew Dillon 
4706ff167aaSJason A. Harmening 	/*
471974efbb3SJason A. Harmening 	 * The root vnode lock may be recursed during unmount, because
4726ff167aaSJason A. Harmening 	 * it may share the same lock as the unionfs mount's covered vnode,
4736ff167aaSJason A. Harmening 	 * which is locked across VFS_UNMOUNT().  This lock will then be
4746ff167aaSJason A. Harmening 	 * recursively taken during the vflush() issued by unionfs_unmount().
4756ff167aaSJason A. Harmening 	 * But we still only need to lock the unionfs lock once, because only
4766ff167aaSJason A. Harmening 	 * one of those lock operations was taken against a unionfs vnode and
4776ff167aaSJason A. Harmening 	 * will be undone against a unionfs vnode.
4786ff167aaSJason A. Harmening 	 */
4796ff167aaSJason A. Harmening 	KASSERT(vp->v_vnlock->lk_recurse == 0 || (vp->v_vflag & VV_ROOT) != 0,
48066191a76SJason A. Harmening 	    ("%s: vnode %p locked recursively", __func__, vp));
481eb60ff1eSJason A. Harmening 
482eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
483eb60ff1eSJason A. Harmening 	VNASSERT(unp != NULL, vp, ("%s: already reclaimed", __func__));
484eb60ff1eSJason A. Harmening 	lvp = unp->un_lowervp;
485eb60ff1eSJason A. Harmening 	uvp = unp->un_uppervp;
486eb60ff1eSJason A. Harmening 	dvp = unp->un_dvp;
487eb60ff1eSJason A. Harmening 	unlock_lvp = (uvp == NULLVP);
488eb60ff1eSJason A. Harmening 
489eb60ff1eSJason A. Harmening 	/*
490eb60ff1eSJason A. Harmening 	 * Lock the lower vnode in addition to the upper vnode lock in order
491eb60ff1eSJason A. Harmening 	 * to synchronize against any unionfs_lock() operation which may still
492eb60ff1eSJason A. Harmening 	 * hold the lower vnode lock.  We do not need to do this for the root
493eb60ff1eSJason A. Harmening 	 * vnode, as the root vnode should always have both upper and lower
494eb60ff1eSJason A. Harmening 	 * base vnodes for its entire lifecycled, so unionfs_lock() should
495eb60ff1eSJason A. Harmening 	 * never attempt to lock its lower vnode in the first place.
496eb60ff1eSJason A. Harmening 	 * Moreover, during unmount of a non-"below" unionfs mount, the lower
497eb60ff1eSJason A. Harmening 	 * root vnode will already be locked as it is the covered vnode.
498eb60ff1eSJason A. Harmening 	 */
499eb60ff1eSJason A. Harmening 	if (uvp != NULLVP && lvp != NULLVP && (vp->v_vflag & VV_ROOT) == 0) {
500eb60ff1eSJason A. Harmening 		vn_lock_pair(uvp, true, LK_EXCLUSIVE, lvp, false, LK_EXCLUSIVE);
501eb60ff1eSJason A. Harmening 		unlock_lvp = true;
502eb60ff1eSJason A. Harmening 	}
503eb60ff1eSJason A. Harmening 
50466191a76SJason A. Harmening 	if (lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
5053ecefc4aSJason A. Harmening 		panic("%s: failed to acquire lock for vnode lock", __func__);
5062a31267eSMatthew Dillon 	/*
507d00947d8SCraig Rodrigues 	 * Use the interlock to protect the clearing of v_data to
508d00947d8SCraig Rodrigues 	 * prevent faults in unionfs_lock().
5092a31267eSMatthew Dillon 	 */
510d00947d8SCraig Rodrigues 	VI_LOCK(vp);
511d00947d8SCraig Rodrigues 	unp->un_lowervp = unp->un_uppervp = NULLVP;
512d00947d8SCraig Rodrigues 	vp->v_vnlock = &(vp->v_lock);
513d00947d8SCraig Rodrigues 	vp->v_data = NULL;
514d00947d8SCraig Rodrigues 	vp->v_object = NULL;
515866dd633SJason A. Harmening 	if (unp->un_hashtbl != NULL) {
516866dd633SJason A. Harmening 		/*
517866dd633SJason A. Harmening 		 * Clear out any cached child vnodes.  This should only
518866dd633SJason A. Harmening 		 * be necessary during forced unmount, when the vnode may
519866dd633SJason A. Harmening 		 * be reclaimed with a non-zero use count.  Otherwise the
520866dd633SJason A. Harmening 		 * reference held by each child should prevent reclamation.
521866dd633SJason A. Harmening 		 */
522866dd633SJason A. Harmening 		for (count = 0; count <= UNIONFSHASHMASK; count++) {
523866dd633SJason A. Harmening 			hd = unp->un_hashtbl + count;
524866dd633SJason A. Harmening 			LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) {
525866dd633SJason A. Harmening 				LIST_REMOVE(unp_t1, un_hash);
526866dd633SJason A. Harmening 				unp_t1->un_hash.le_next = NULL;
527866dd633SJason A. Harmening 				unp_t1->un_hash.le_prev = NULL;
528866dd633SJason A. Harmening 			}
529866dd633SJason A. Harmening 		}
530866dd633SJason A. Harmening 	}
531cb5736b7SDaichi GOTO 	VI_UNLOCK(vp);
532cb5736b7SDaichi GOTO 
533d877dd57SJason A. Harmening 	writerefs = atomic_load_int(&vp->v_writecount);
534d877dd57SJason A. Harmening 	VNASSERT(writerefs >= 0, vp,
535d877dd57SJason A. Harmening 	    ("%s: write count %d, unexpected text ref", __func__, writerefs));
536d877dd57SJason A. Harmening 	/*
537d877dd57SJason A. Harmening 	 * If we were opened for write, we leased the write reference
538d877dd57SJason A. Harmening 	 * to the lower vnode.  If this is a reclamation due to the
539d877dd57SJason A. Harmening 	 * forced unmount, undo the reference now.
540d877dd57SJason A. Harmening 	 */
541d877dd57SJason A. Harmening 	if (writerefs > 0) {
542d877dd57SJason A. Harmening 		VNASSERT(uvp != NULL, vp,
543d877dd57SJason A. Harmening 		    ("%s: write reference without upper vnode", __func__));
544d877dd57SJason A. Harmening 		VOP_ADD_WRITECOUNT(uvp, -writerefs);
545d877dd57SJason A. Harmening 	}
546cb5736b7SDaichi GOTO 	if (uvp != NULLVP)
547eb60ff1eSJason A. Harmening 		vput(uvp);
548eb60ff1eSJason A. Harmening 	if (unlock_lvp)
549eb60ff1eSJason A. Harmening 		vput(lvp);
550eb60ff1eSJason A. Harmening 	else if (lvp != NULLVP)
551eb60ff1eSJason A. Harmening 		vrele(lvp);
552d00947d8SCraig Rodrigues 
553fd8ad212SJason A. Harmening 	if (dvp != NULLVP)
5543af387c9SDaichi GOTO 		unionfs_rem_cached_vnode(unp, dvp);
555a9b794ffSDaichi GOTO 
556a9b794ffSDaichi GOTO 	if (unp->un_path != NULL) {
557d00947d8SCraig Rodrigues 		free(unp->un_path, M_UNIONFSPATH);
558d00947d8SCraig Rodrigues 		unp->un_path = NULL;
559abe95116SJason A. Harmening 		unp->un_pathlen = 0;
560d00947d8SCraig Rodrigues 	}
561acc4bab1SCraig Rodrigues 
5623af387c9SDaichi GOTO 	if (unp->un_hashtbl != NULL) {
563fd8ad212SJason A. Harmening 		hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
5643af387c9SDaichi GOTO 	}
565a9b794ffSDaichi GOTO 
566acc4bab1SCraig Rodrigues 	LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) {
567d00947d8SCraig Rodrigues 		LIST_REMOVE(unsp, uns_list);
568d00947d8SCraig Rodrigues 		free(unsp, M_TEMP);
569d00947d8SCraig Rodrigues 	}
570372691a7SJason A. Harmening 	if (dvp != NULLVP) {
571372691a7SJason A. Harmening 		mtx_lock(&unionfs_deferred_rele_lock);
572372691a7SJason A. Harmening 		STAILQ_INSERT_TAIL(&unionfs_deferred_rele_list, unp, un_rele);
573372691a7SJason A. Harmening 		mtx_unlock(&unionfs_deferred_rele_lock);
574372691a7SJason A. Harmening 		taskqueue_enqueue(taskqueue_unionfs_rele,
575372691a7SJason A. Harmening 		    &unionfs_deferred_rele_task);
576372691a7SJason A. Harmening 	} else
5771ede983cSDag-Erling Smørgrav 		free(unp, M_UNIONFSNODE);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
580d00947d8SCraig Rodrigues /*
58153a777bbSJason A. Harmening  * Find the unionfs node status object for the vnode corresponding to unp,
58253a777bbSJason A. Harmening  * for the process that owns td.  Return NULL if no such object exists.
58353a777bbSJason A. Harmening  */
58453a777bbSJason A. Harmening struct unionfs_node_status *
unionfs_find_node_status(struct unionfs_node * unp,struct thread * td)58553a777bbSJason A. Harmening unionfs_find_node_status(struct unionfs_node *unp, struct thread *td)
58653a777bbSJason A. Harmening {
58753a777bbSJason A. Harmening 	struct unionfs_node_status *unsp;
58853a777bbSJason A. Harmening 	pid_t pid;
58953a777bbSJason A. Harmening 
59053a777bbSJason A. Harmening 	pid = td->td_proc->p_pid;
59153a777bbSJason A. Harmening 
59253a777bbSJason A. Harmening 	ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), __func__);
59353a777bbSJason A. Harmening 
59453a777bbSJason A. Harmening 	LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
59553a777bbSJason A. Harmening 		if (unsp->uns_pid == pid) {
59653a777bbSJason A. Harmening 			return (unsp);
59753a777bbSJason A. Harmening 		}
59853a777bbSJason A. Harmening 	}
59953a777bbSJason A. Harmening 
60053a777bbSJason A. Harmening 	return (NULL);
60153a777bbSJason A. Harmening }
60253a777bbSJason A. Harmening 
60353a777bbSJason A. Harmening /*
60439a2dc44SJason A. Harmening  * Get the unionfs node status object for the vnode corresponding to unp,
60539a2dc44SJason A. Harmening  * for the process that owns td.  Allocate a new status object if one
60639a2dc44SJason A. Harmening  * does not already exist.
607d00947d8SCraig Rodrigues  */
608d00947d8SCraig Rodrigues void
unionfs_get_node_status(struct unionfs_node * unp,struct thread * td,struct unionfs_node_status ** unspp)609d00947d8SCraig Rodrigues unionfs_get_node_status(struct unionfs_node *unp, struct thread *td,
610d00947d8SCraig Rodrigues     struct unionfs_node_status **unspp)
611d00947d8SCraig Rodrigues {
612d00947d8SCraig Rodrigues 	struct unionfs_node_status *unsp;
613312d49efSJason A. Harmening 	pid_t pid;
614312d49efSJason A. Harmening 
615312d49efSJason A. Harmening 	pid = td->td_proc->p_pid;
616df8bae1dSRodney W. Grimes 
6173ecefc4aSJason A. Harmening 	KASSERT(NULL != unspp, ("%s: NULL status", __func__));
61853a777bbSJason A. Harmening 	unsp = unionfs_find_node_status(unp, td);
61953a777bbSJason A. Harmening 	if (unsp == NULL) {
620d00947d8SCraig Rodrigues 		/* create a new unionfs node status */
621e11e3f18SDag-Erling Smørgrav 		unsp = malloc(sizeof(struct unionfs_node_status),
622e11e3f18SDag-Erling Smørgrav 		    M_TEMP, M_WAITOK | M_ZERO);
6232a31267eSMatthew Dillon 
624fe5f08cdSDaichi GOTO 		unsp->uns_pid = pid;
625d00947d8SCraig Rodrigues 		LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
62653a777bbSJason A. Harmening 	}
6272a31267eSMatthew Dillon 
628d00947d8SCraig Rodrigues 	*unspp = unsp;
629d00947d8SCraig Rodrigues }
630d00947d8SCraig Rodrigues 
631d00947d8SCraig Rodrigues /*
632d00947d8SCraig Rodrigues  * Remove the unionfs node status, if you can.
633d00947d8SCraig Rodrigues  * You need exclusive lock this vnode.
634d00947d8SCraig Rodrigues  */
635d00947d8SCraig Rodrigues void
unionfs_tryrem_node_status(struct unionfs_node * unp,struct unionfs_node_status * unsp)636fe5f08cdSDaichi GOTO unionfs_tryrem_node_status(struct unionfs_node *unp,
637d00947d8SCraig Rodrigues     struct unionfs_node_status *unsp)
638d00947d8SCraig Rodrigues {
6393ecefc4aSJason A. Harmening 	KASSERT(NULL != unsp, ("%s: NULL status", __func__));
6403ecefc4aSJason A. Harmening 	ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), __func__);
641d00947d8SCraig Rodrigues 
642d00947d8SCraig Rodrigues 	if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
643d00947d8SCraig Rodrigues 		return;
644d00947d8SCraig Rodrigues 
645d00947d8SCraig Rodrigues 	LIST_REMOVE(unsp, uns_list);
646d00947d8SCraig Rodrigues 	free(unsp, M_TEMP);
647d00947d8SCraig Rodrigues }
648d00947d8SCraig Rodrigues 
649d00947d8SCraig Rodrigues /*
650d00947d8SCraig Rodrigues  * Create upper node attr.
651d00947d8SCraig Rodrigues  */
652d00947d8SCraig Rodrigues void
unionfs_create_uppervattr_core(struct unionfs_mount * ump,struct vattr * lva,struct vattr * uva,struct thread * td)653312d49efSJason A. Harmening unionfs_create_uppervattr_core(struct unionfs_mount *ump, struct vattr *lva,
654312d49efSJason A. Harmening     struct vattr *uva, struct thread *td)
655d00947d8SCraig Rodrigues {
656d00947d8SCraig Rodrigues 	VATTR_NULL(uva);
657d00947d8SCraig Rodrigues 	uva->va_type = lva->va_type;
658d00947d8SCraig Rodrigues 	uva->va_atime = lva->va_atime;
659d00947d8SCraig Rodrigues 	uva->va_mtime = lva->va_mtime;
660d00947d8SCraig Rodrigues 	uva->va_ctime = lva->va_ctime;
661d00947d8SCraig Rodrigues 
662d00947d8SCraig Rodrigues 	switch (ump->um_copymode) {
663d00947d8SCraig Rodrigues 	case UNIONFS_TRANSPARENT:
664d00947d8SCraig Rodrigues 		uva->va_mode = lva->va_mode;
665d00947d8SCraig Rodrigues 		uva->va_uid = lva->va_uid;
666d00947d8SCraig Rodrigues 		uva->va_gid = lva->va_gid;
667d00947d8SCraig Rodrigues 		break;
668d00947d8SCraig Rodrigues 	case UNIONFS_MASQUERADE:
669d00947d8SCraig Rodrigues 		if (ump->um_uid == lva->va_uid) {
670d00947d8SCraig Rodrigues 			uva->va_mode = lva->va_mode & 077077;
671312d49efSJason A. Harmening 			uva->va_mode |= (lva->va_type == VDIR ?
672312d49efSJason A. Harmening 			    ump->um_udir : ump->um_ufile) & 0700;
673d00947d8SCraig Rodrigues 			uva->va_uid = lva->va_uid;
674d00947d8SCraig Rodrigues 			uva->va_gid = lva->va_gid;
675df8bae1dSRodney W. Grimes 		} else {
676312d49efSJason A. Harmening 			uva->va_mode = (lva->va_type == VDIR ?
677312d49efSJason A. Harmening 			    ump->um_udir : ump->um_ufile);
678d00947d8SCraig Rodrigues 			uva->va_uid = ump->um_uid;
679d00947d8SCraig Rodrigues 			uva->va_gid = ump->um_gid;
680d00947d8SCraig Rodrigues 		}
681d00947d8SCraig Rodrigues 		break;
682d00947d8SCraig Rodrigues 	default:		/* UNIONFS_TRADITIONAL */
68385078b85SConrad Meyer 		uva->va_mode = 0777 & ~td->td_proc->p_pd->pd_cmask;
684d00947d8SCraig Rodrigues 		uva->va_uid = ump->um_uid;
685d00947d8SCraig Rodrigues 		uva->va_gid = ump->um_gid;
686d00947d8SCraig Rodrigues 		break;
687d00947d8SCraig Rodrigues 	}
688df8bae1dSRodney W. Grimes }
689df8bae1dSRodney W. Grimes 
690d00947d8SCraig Rodrigues /*
691d00947d8SCraig Rodrigues  * Create upper node attr.
692d00947d8SCraig Rodrigues  */
693d00947d8SCraig Rodrigues int
unionfs_create_uppervattr(struct unionfs_mount * ump,struct vnode * lvp,struct vattr * uva,struct ucred * cred,struct thread * td)694312d49efSJason A. Harmening unionfs_create_uppervattr(struct unionfs_mount *ump, struct vnode *lvp,
695312d49efSJason A. Harmening     struct vattr *uva, struct ucred *cred, struct thread *td)
696d00947d8SCraig Rodrigues {
697d00947d8SCraig Rodrigues 	struct vattr	lva;
698312d49efSJason A. Harmening 	int		error;
699df8bae1dSRodney W. Grimes 
7000359a12eSAttilio Rao 	if ((error = VOP_GETATTR(lvp, &lva, cred)))
701d00947d8SCraig Rodrigues 		return (error);
702d00947d8SCraig Rodrigues 
703d00947d8SCraig Rodrigues 	unionfs_create_uppervattr_core(ump, &lva, uva, td);
704df8bae1dSRodney W. Grimes 
705df8bae1dSRodney W. Grimes 	return (error);
706df8bae1dSRodney W. Grimes }
707df8bae1dSRodney W. Grimes 
708d00947d8SCraig Rodrigues /*
709d00947d8SCraig Rodrigues  * relookup
710d00947d8SCraig Rodrigues  *
711d00947d8SCraig Rodrigues  * dvp should be locked on entry and will be locked on return.
712d00947d8SCraig Rodrigues  *
713d00947d8SCraig Rodrigues  * If an error is returned, *vpp will be invalid, otherwise it will hold a
714d00947d8SCraig Rodrigues  * locked, referenced vnode. If *vpp == dvp then remember that only one
715d00947d8SCraig Rodrigues  * LK_EXCLUSIVE lock is held.
716d00947d8SCraig Rodrigues  */
7171e5da15aSDaichi GOTO int
unionfs_relookup(struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,struct componentname * cn,struct thread * td,char * path,int pathlen,u_long nameiop)718d00947d8SCraig Rodrigues unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
719312d49efSJason A. Harmening     struct componentname *cnp, struct componentname *cn, struct thread *td,
720312d49efSJason A. Harmening     char *path, int pathlen, u_long nameiop)
721df8bae1dSRodney W. Grimes {
722d00947d8SCraig Rodrigues 	int error;
7238f874e92SMateusz Guzik 	bool refstart;
724df8bae1dSRodney W. Grimes 
725d00947d8SCraig Rodrigues 	cn->cn_namelen = pathlen;
726abe95116SJason A. Harmening 	cn->cn_pnbuf = path;
727d00947d8SCraig Rodrigues 	cn->cn_nameiop = nameiop;
7285b5b7e2cSMateusz Guzik 	cn->cn_flags = (LOCKPARENT | LOCKLEAF | ISLASTCN);
729d00947d8SCraig Rodrigues 	cn->cn_lkflags = LK_EXCLUSIVE;
730d00947d8SCraig Rodrigues 	cn->cn_cred = cnp->cn_cred;
731d00947d8SCraig Rodrigues 	cn->cn_nameptr = cn->cn_pnbuf;
732df8bae1dSRodney W. Grimes 
7338f874e92SMateusz Guzik 	refstart = false;
7348f874e92SMateusz Guzik 	if (nameiop == DELETE) {
7358f874e92SMateusz Guzik 		cn->cn_flags |= (cnp->cn_flags & DOWHITEOUT);
7368f7859e8SMateusz Guzik 	} else if (nameiop == RENAME) {
7378f7859e8SMateusz Guzik 		refstart = true;
7388f874e92SMateusz Guzik 	} else if (nameiop == CREATE) {
7396c21f6edSKonstantin Belousov 		cn->cn_flags |= NOCACHE;
7408f874e92SMateusz Guzik 	}
741d00947d8SCraig Rodrigues 
742d00947d8SCraig Rodrigues 	vref(dvp);
743b249ce48SMateusz Guzik 	VOP_UNLOCK(dvp);
744d00947d8SCraig Rodrigues 
7458f874e92SMateusz Guzik 	if ((error = vfs_relookup(dvp, vpp, cn, refstart))) {
746cb05b60aSAttilio Rao 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
747d00947d8SCraig Rodrigues 	} else
748d00947d8SCraig Rodrigues 		vrele(dvp);
749d00947d8SCraig Rodrigues 
750abe95116SJason A. Harmening 	KASSERT(cn->cn_pnbuf == path, ("%s: cn_pnbuf changed", __func__));
751abe95116SJason A. Harmening 
752d00947d8SCraig Rodrigues 	return (error);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes /*
756d00947d8SCraig Rodrigues  * Update the unionfs_node.
757d00947d8SCraig Rodrigues  *
758d00947d8SCraig Rodrigues  * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
759d00947d8SCraig Rodrigues  * uvp's lock and lower's lock will be unlocked.
760d00947d8SCraig Rodrigues  */
761d00947d8SCraig Rodrigues static void
unionfs_node_update(struct unionfs_node * unp,struct vnode * uvp,struct thread * td)762d00947d8SCraig Rodrigues unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp,
763d00947d8SCraig Rodrigues     struct thread *td)
764d00947d8SCraig Rodrigues {
765fd8ad212SJason A. Harmening 	struct unionfs_node_hashhead *hd;
766d00947d8SCraig Rodrigues 	struct vnode   *vp;
767d00947d8SCraig Rodrigues 	struct vnode   *lvp;
768a9b794ffSDaichi GOTO 	struct vnode   *dvp;
769312d49efSJason A. Harmening 	unsigned	count, lockrec;
770d00947d8SCraig Rodrigues 
771d00947d8SCraig Rodrigues 	vp = UNIONFSTOV(unp);
772d00947d8SCraig Rodrigues 	lvp = unp->un_lowervp;
7733ecefc4aSJason A. Harmening 	ASSERT_VOP_ELOCKED(lvp, __func__);
77466191a76SJason A. Harmening 	ASSERT_VOP_ELOCKED(uvp, __func__);
775a9b794ffSDaichi GOTO 	dvp = unp->un_dvp;
776d00947d8SCraig Rodrigues 
777d877dd57SJason A. Harmening 	VNASSERT(vp->v_writecount == 0, vp,
778d877dd57SJason A. Harmening 	    ("%s: non-zero writecount", __func__));
779d00947d8SCraig Rodrigues 	/*
7806ff167aaSJason A. Harmening 	 * Update the upper vnode's lock state to match the lower vnode,
781866dd633SJason A. Harmening 	 * and then switch the unionfs vnode's lock to the upper vnode.
782d00947d8SCraig Rodrigues 	 */
783866dd633SJason A. Harmening 	lockrec = lvp->v_vnlock->lk_recurse;
784866dd633SJason A. Harmening 	for (count = 0; count < lockrec; count++)
785866dd633SJason A. Harmening 		vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
786d00947d8SCraig Rodrigues 	VI_LOCK(vp);
787d00947d8SCraig Rodrigues 	unp->un_uppervp = uvp;
788d00947d8SCraig Rodrigues 	vp->v_vnlock = uvp->v_vnlock;
789d00947d8SCraig Rodrigues 	VI_UNLOCK(vp);
790a9b794ffSDaichi GOTO 
791eb60ff1eSJason A. Harmening 	for (count = 0; count < lockrec + 1; count++)
792eb60ff1eSJason A. Harmening 		VOP_UNLOCK(lvp);
793a9b794ffSDaichi GOTO 	/*
794fd8ad212SJason A. Harmening 	 * Re-cache the unionfs vnode against the upper vnode
795a9b794ffSDaichi GOTO 	 */
796fd8ad212SJason A. Harmening 	if (dvp != NULLVP && vp->v_type == VDIR) {
797a9b794ffSDaichi GOTO 		VI_LOCK(dvp);
798fd8ad212SJason A. Harmening 		if (unp->un_hash.le_prev != NULL) {
799a9b794ffSDaichi GOTO 			LIST_REMOVE(unp, un_hash);
800fd8ad212SJason A. Harmening 			hd = unionfs_get_hashhead(dvp, uvp);
801a9b794ffSDaichi GOTO 			LIST_INSERT_HEAD(hd, unp, un_hash);
802fd8ad212SJason A. Harmening 		}
803fd8ad212SJason A. Harmening 		VI_UNLOCK(unp->un_dvp);
804a9b794ffSDaichi GOTO 	}
805d00947d8SCraig Rodrigues }
806d00947d8SCraig Rodrigues 
807d00947d8SCraig Rodrigues /*
808eb60ff1eSJason A. Harmening  * Mark a unionfs operation as being in progress, sleeping if the
809eb60ff1eSJason A. Harmening  * same operation is already in progress.
810eb60ff1eSJason A. Harmening  * This is useful, for example, during copy-up operations in which
811eb60ff1eSJason A. Harmening  * we may drop the target vnode lock, but we want to avoid the
812eb60ff1eSJason A. Harmening  * possibility of a concurrent copy-up on the same vnode triggering
813eb60ff1eSJason A. Harmening  * a spurious failure.
814eb60ff1eSJason A. Harmening  */
815eb60ff1eSJason A. Harmening int
unionfs_set_in_progress_flag(struct vnode * vp,unsigned int flag)816eb60ff1eSJason A. Harmening unionfs_set_in_progress_flag(struct vnode *vp, unsigned int flag)
817eb60ff1eSJason A. Harmening {
818eb60ff1eSJason A. Harmening 	struct unionfs_node *unp;
819eb60ff1eSJason A. Harmening 	int error;
820eb60ff1eSJason A. Harmening 
821eb60ff1eSJason A. Harmening 	error = 0;
822eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(vp, __func__);
823eb60ff1eSJason A. Harmening 	VI_LOCK(vp);
824eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
825eb60ff1eSJason A. Harmening 	while (error == 0 && (unp->un_flag & flag) != 0) {
826eb60ff1eSJason A. Harmening 		VOP_UNLOCK(vp);
827eb60ff1eSJason A. Harmening 		error = msleep(vp, VI_MTX(vp), PCATCH | PDROP, "unioncp", 0);
828eb60ff1eSJason A. Harmening 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
829eb60ff1eSJason A. Harmening 		VI_LOCK(vp);
830eb60ff1eSJason A. Harmening 		if (error == 0) {
831eb60ff1eSJason A. Harmening 			/*
832eb60ff1eSJason A. Harmening 			 * If we waited on a concurrent copy-up and that
833eb60ff1eSJason A. Harmening 			 * copy-up was successful, return a non-fatal
834eb60ff1eSJason A. Harmening 			 * indication that the desired operation is already
835eb60ff1eSJason A. Harmening 			 * complete.  If we waited on a concurrent lookup,
836eb60ff1eSJason A. Harmening 			 * return ERELOOKUP to indicate the VFS cache should
837eb60ff1eSJason A. Harmening 			 * be re-queried to avoid creating a duplicate unionfs
838eb60ff1eSJason A. Harmening 			 * vnode.
839eb60ff1eSJason A. Harmening 			 */
840eb60ff1eSJason A. Harmening 			unp = VTOUNIONFS(vp);
841eb60ff1eSJason A. Harmening 			if (unp == NULL)
842eb60ff1eSJason A. Harmening 				error = ENOENT;
843eb60ff1eSJason A. Harmening 			else if (flag == UNIONFS_COPY_IN_PROGRESS &&
844eb60ff1eSJason A. Harmening 			    unp->un_uppervp != NULLVP)
845eb60ff1eSJason A. Harmening 				error = EJUSTRETURN;
846eb60ff1eSJason A. Harmening 			else if (flag == UNIONFS_LOOKUP_IN_PROGRESS)
847eb60ff1eSJason A. Harmening 				error = ERELOOKUP;
848eb60ff1eSJason A. Harmening 		}
849eb60ff1eSJason A. Harmening 	}
850eb60ff1eSJason A. Harmening 	if (error == 0)
851eb60ff1eSJason A. Harmening 		unp->un_flag |= flag;
852eb60ff1eSJason A. Harmening 	VI_UNLOCK(vp);
853eb60ff1eSJason A. Harmening 
854eb60ff1eSJason A. Harmening 	return (error);
855eb60ff1eSJason A. Harmening }
856eb60ff1eSJason A. Harmening 
857eb60ff1eSJason A. Harmening void
unionfs_clear_in_progress_flag(struct vnode * vp,unsigned int flag)858eb60ff1eSJason A. Harmening unionfs_clear_in_progress_flag(struct vnode *vp, unsigned int flag)
859eb60ff1eSJason A. Harmening {
860eb60ff1eSJason A. Harmening 	struct unionfs_node *unp;
861eb60ff1eSJason A. Harmening 
862eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(vp, __func__);
863eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
864eb60ff1eSJason A. Harmening 	VI_LOCK(vp);
865eb60ff1eSJason A. Harmening 	if (unp != NULL) {
866eb60ff1eSJason A. Harmening 		VNASSERT((unp->un_flag & flag) != 0, vp,
867eb60ff1eSJason A. Harmening 		    ("%s: copy not in progress", __func__));
868eb60ff1eSJason A. Harmening 		unp->un_flag &= ~flag;
869eb60ff1eSJason A. Harmening 	}
870eb60ff1eSJason A. Harmening 	wakeup(vp);
871eb60ff1eSJason A. Harmening 	VI_UNLOCK(vp);
872eb60ff1eSJason A. Harmening }
873eb60ff1eSJason A. Harmening 
874eb60ff1eSJason A. Harmening /*
875d00947d8SCraig Rodrigues  * Create a new shadow dir.
876d00947d8SCraig Rodrigues  *
877eb60ff1eSJason A. Harmening  * dvp and vp are unionfs vnodes representing a parent directory and
878eb60ff1eSJason A. Harmening  * child file, should be locked on entry, and will be locked on return.
879d00947d8SCraig Rodrigues  *
880d00947d8SCraig Rodrigues  * If no error returned, unp will be updated.
881d00947d8SCraig Rodrigues  */
882d00947d8SCraig Rodrigues int
unionfs_mkshadowdir(struct vnode * dvp,struct vnode * vp,struct componentname * cnp,struct thread * td)883eb60ff1eSJason A. Harmening unionfs_mkshadowdir(struct vnode *dvp, struct vnode *vp,
884eb60ff1eSJason A. Harmening     struct componentname *cnp, struct thread *td)
885d00947d8SCraig Rodrigues {
886d00947d8SCraig Rodrigues 	struct vnode   *lvp;
887d00947d8SCraig Rodrigues 	struct vnode   *uvp;
888eb60ff1eSJason A. Harmening 	struct vnode   *udvp;
889d00947d8SCraig Rodrigues 	struct vattr	va;
890d00947d8SCraig Rodrigues 	struct vattr	lva;
891190110f2SKonstantin Belousov 	struct nameidata nd;
892d00947d8SCraig Rodrigues 	struct mount   *mp;
893d00947d8SCraig Rodrigues 	struct ucred   *cred;
894d00947d8SCraig Rodrigues 	struct ucred   *credbk;
895d00947d8SCraig Rodrigues 	struct uidinfo *rootinfo;
896eb60ff1eSJason A. Harmening 	struct unionfs_mount *ump;
897eb60ff1eSJason A. Harmening 	struct unionfs_node *dunp;
898eb60ff1eSJason A. Harmening 	struct unionfs_node *unp;
899312d49efSJason A. Harmening 	int		error;
900d00947d8SCraig Rodrigues 
901eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(dvp, __func__);
902eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(vp, __func__);
903eb60ff1eSJason A. Harmening 	ump = MOUNTTOUNIONFSMOUNT(vp->v_mount);
904eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
905d00947d8SCraig Rodrigues 	if (unp->un_uppervp != NULLVP)
906d00947d8SCraig Rodrigues 		return (EEXIST);
907eb60ff1eSJason A. Harmening 	dunp = VTOUNIONFS(dvp);
908eb60ff1eSJason A. Harmening 	udvp = dunp->un_uppervp;
909eb60ff1eSJason A. Harmening 
910eb60ff1eSJason A. Harmening 	error = unionfs_set_in_progress_flag(vp, UNIONFS_COPY_IN_PROGRESS);
911eb60ff1eSJason A. Harmening 	if (error == EJUSTRETURN)
912eb60ff1eSJason A. Harmening 		return (0);
913eb60ff1eSJason A. Harmening 	else if (error != 0)
914eb60ff1eSJason A. Harmening 		return (error);
915d00947d8SCraig Rodrigues 
916d00947d8SCraig Rodrigues 	lvp = unp->un_lowervp;
917d00947d8SCraig Rodrigues 	uvp = NULLVP;
918d00947d8SCraig Rodrigues 	credbk = cnp->cn_cred;
919d00947d8SCraig Rodrigues 
920d00947d8SCraig Rodrigues 	/* Authority change to root */
921d00947d8SCraig Rodrigues 	rootinfo = uifind((uid_t)0);
922d00947d8SCraig Rodrigues 	cred = crdup(cnp->cn_cred);
92381f6480dSEdward Tomasz Napierala 	/*
92481f6480dSEdward Tomasz Napierala 	 * The calls to chgproccnt() are needed to compensate for change_ruid()
92581f6480dSEdward Tomasz Napierala 	 * calling chgproccnt().
92681f6480dSEdward Tomasz Napierala 	 */
92781f6480dSEdward Tomasz Napierala 	chgproccnt(cred->cr_ruidinfo, 1, 0);
928d00947d8SCraig Rodrigues 	change_euid(cred, rootinfo);
929d00947d8SCraig Rodrigues 	change_ruid(cred, rootinfo);
930d00947d8SCraig Rodrigues 	change_svuid(cred, (uid_t)0);
931d00947d8SCraig Rodrigues 	uifree(rootinfo);
932d00947d8SCraig Rodrigues 	cnp->cn_cred = cred;
933d00947d8SCraig Rodrigues 
934190110f2SKonstantin Belousov 	memset(&nd.ni_cnd, 0, sizeof(struct componentname));
935190110f2SKonstantin Belousov 	NDPREINIT(&nd);
936d00947d8SCraig Rodrigues 
9370359a12eSAttilio Rao 	if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
938eb60ff1eSJason A. Harmening 		goto unionfs_mkshadowdir_finish;
939d00947d8SCraig Rodrigues 
940eb60ff1eSJason A. Harmening 	vref(udvp);
941eb60ff1eSJason A. Harmening 	VOP_UNLOCK(vp);
942190110f2SKonstantin Belousov 	if ((error = unionfs_relookup(udvp, &uvp, cnp, &nd.ni_cnd, td,
943eb60ff1eSJason A. Harmening 	    cnp->cn_nameptr, cnp->cn_namelen, CREATE))) {
944eb60ff1eSJason A. Harmening 		/*
945eb60ff1eSJason A. Harmening 		 * When handling error cases here, we drop udvp's lock and
946eb60ff1eSJason A. Harmening 		 * then jump to exit code that relocks dvp, which in most
947eb60ff1eSJason A. Harmening 		 * cases will effectively relock udvp.  However, this is
948eb60ff1eSJason A. Harmening 		 * not guaranteed to be the case, as various calls made
949eb60ff1eSJason A. Harmening 		 * here (such as unionfs_relookup() above and VOP_MKDIR()
950eb60ff1eSJason A. Harmening 		 * below) may unlock and then relock udvp, allowing dvp to
951eb60ff1eSJason A. Harmening 		 * be reclaimed in the meantime.  In such a situation dvp
952eb60ff1eSJason A. Harmening 		 * will no longer share its lock with udvp.  Since
953eb60ff1eSJason A. Harmening 		 * performance isn't a concern for these error cases, it
954eb60ff1eSJason A. Harmening 		 * makes more sense to reuse the common code that locks
955eb60ff1eSJason A. Harmening 		 * dvp on exit than to explicitly check for reclamation
956eb60ff1eSJason A. Harmening 		 * of dvp.
957eb60ff1eSJason A. Harmening 		 */
958eb60ff1eSJason A. Harmening 		vput(udvp);
959eb60ff1eSJason A. Harmening 		goto unionfs_mkshadowdir_relock;
960eb60ff1eSJason A. Harmening 	}
961d00947d8SCraig Rodrigues 	if (uvp != NULLVP) {
962d00947d8SCraig Rodrigues 		if (udvp == uvp)
963d00947d8SCraig Rodrigues 			vrele(uvp);
964d00947d8SCraig Rodrigues 		else
965d00947d8SCraig Rodrigues 			vput(uvp);
966d00947d8SCraig Rodrigues 
967d00947d8SCraig Rodrigues 		error = EEXIST;
968eb60ff1eSJason A. Harmening 		vput(udvp);
969eb60ff1eSJason A. Harmening 		goto unionfs_mkshadowdir_relock;
970d00947d8SCraig Rodrigues 	}
971d00947d8SCraig Rodrigues 
972eb60ff1eSJason A. Harmening 	if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH))) {
973eb60ff1eSJason A. Harmening 		vput(udvp);
974eb60ff1eSJason A. Harmening 		goto unionfs_mkshadowdir_relock;
975eb60ff1eSJason A. Harmening 	}
976d00947d8SCraig Rodrigues 	unionfs_create_uppervattr_core(ump, &lva, &va, td);
977d00947d8SCraig Rodrigues 
978a2ddbe01SJason A. Harmening 	/*
979a2ddbe01SJason A. Harmening 	 * Temporarily NUL-terminate the current pathname component.
980a2ddbe01SJason A. Harmening 	 * This function may be called during lookup operations in which
981a2ddbe01SJason A. Harmening 	 * the current pathname component is not the leaf, meaning that
982a2ddbe01SJason A. Harmening 	 * the NUL terminator is some distance beyond the end of the current
983a2ddbe01SJason A. Harmening 	 * component.  This *should* be fine, as cn_namelen will still
984a2ddbe01SJason A. Harmening 	 * correctly indicate the length of only the current component,
985a2ddbe01SJason A. Harmening 	 * but ZFS in particular does not respect cn_namelen in its VOP_MKDIR
986eb60ff1eSJason A. Harmening 	 * implementation.
987a2ddbe01SJason A. Harmening 	 * Note that this assumes nd.ni_cnd.cn_pnbuf was allocated by
988a2ddbe01SJason A. Harmening 	 * something like a local namei() operation and the temporary
989a2ddbe01SJason A. Harmening 	 * NUL-termination will not have an effect on other threads.
990a2ddbe01SJason A. Harmening 	 */
991a2ddbe01SJason A. Harmening 	char *pathend = &nd.ni_cnd.cn_nameptr[nd.ni_cnd.cn_namelen];
992a2ddbe01SJason A. Harmening 	char pathterm = *pathend;
993a2ddbe01SJason A. Harmening 	*pathend = '\0';
994190110f2SKonstantin Belousov 	error = VOP_MKDIR(udvp, &uvp, &nd.ni_cnd, &va);
995a2ddbe01SJason A. Harmening 	*pathend = pathterm;
996eb60ff1eSJason A. Harmening 	if (error != 0) {
997eb60ff1eSJason A. Harmening 		/*
998eb60ff1eSJason A. Harmening 		 * See the comment after unionfs_relookup() above for an
999eb60ff1eSJason A. Harmening 		 * explanation of why we unlock udvp here only to relock
1000eb60ff1eSJason A. Harmening 		 * dvp on exit.
1001eb60ff1eSJason A. Harmening 		 */
1002eb60ff1eSJason A. Harmening 		vput(udvp);
1003eb60ff1eSJason A. Harmening 		vn_finished_write(mp);
1004eb60ff1eSJason A. Harmening 		goto unionfs_mkshadowdir_relock;
1005eb60ff1eSJason A. Harmening 	}
1006d00947d8SCraig Rodrigues 
1007d00947d8SCraig Rodrigues 	/*
1008d00947d8SCraig Rodrigues 	 * XXX The bug which cannot set uid/gid was corrected.
1009d00947d8SCraig Rodrigues 	 * Ignore errors.
1010d00947d8SCraig Rodrigues 	 */
1011d00947d8SCraig Rodrigues 	va.va_type = VNON;
10126c8ded00SJason A. Harmening 	/*
10136c8ded00SJason A. Harmening 	 * VOP_SETATTR() may transiently drop uvp's lock, so it's
10146c8ded00SJason A. Harmening 	 * important to call it before unionfs_node_update() transfers
10156c8ded00SJason A. Harmening 	 * the unionfs vnode's lock from lvp to uvp; otherwise the
10166c8ded00SJason A. Harmening 	 * unionfs vnode itself would be transiently unlocked and
10176c8ded00SJason A. Harmening 	 * potentially doomed.
10186c8ded00SJason A. Harmening 	 */
1019eb60ff1eSJason A. Harmening 	VOP_SETATTR(uvp, &va, nd.ni_cnd.cn_cred);
1020d00947d8SCraig Rodrigues 
1021eb60ff1eSJason A. Harmening 	/*
1022eb60ff1eSJason A. Harmening 	 * uvp may become doomed during VOP_VPUT_PAIR() if the implementation
1023eb60ff1eSJason A. Harmening 	 * must temporarily drop uvp's lock.  However, since we hold a
1024eb60ff1eSJason A. Harmening 	 * reference to uvp from the VOP_MKDIR() call above, this would require
1025eb60ff1eSJason A. Harmening 	 * a forcible unmount of uvp's filesystem, which in turn can only
1026eb60ff1eSJason A. Harmening 	 * happen if our unionfs instance is first forcibly unmounted.  We'll
1027eb60ff1eSJason A. Harmening 	 * therefore catch this case in the NULL check of unp below.
1028eb60ff1eSJason A. Harmening 	 */
1029eb60ff1eSJason A. Harmening 	VOP_VPUT_PAIR(udvp, &uvp, false);
1030eb60ff1eSJason A. Harmening 	vn_finished_write(mp);
1031eb60ff1eSJason A. Harmening 	vn_lock_pair(vp, false, LK_EXCLUSIVE, uvp, true, LK_EXCLUSIVE);
1032eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
1033eb60ff1eSJason A. Harmening 	if (unp == NULL) {
1034eb60ff1eSJason A. Harmening 		vput(uvp);
1035eb60ff1eSJason A. Harmening 		error = ENOENT;
1036eb60ff1eSJason A. Harmening 	} else
1037eb60ff1eSJason A. Harmening 		unionfs_node_update(unp, uvp, td);
1038eb60ff1eSJason A. Harmening 	VOP_UNLOCK(vp);
1039eb60ff1eSJason A. Harmening 
1040eb60ff1eSJason A. Harmening unionfs_mkshadowdir_relock:
1041eb60ff1eSJason A. Harmening 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1042eb60ff1eSJason A. Harmening 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1043eb60ff1eSJason A. Harmening 	if (error == 0 && (VN_IS_DOOMED(dvp) || VN_IS_DOOMED(vp)))
1044eb60ff1eSJason A. Harmening 		error = ENOENT;
1045eb60ff1eSJason A. Harmening 
1046eb60ff1eSJason A. Harmening unionfs_mkshadowdir_finish:
1047eb60ff1eSJason A. Harmening 	unionfs_clear_in_progress_flag(vp, UNIONFS_COPY_IN_PROGRESS);
1048d00947d8SCraig Rodrigues 	cnp->cn_cred = credbk;
104981f6480dSEdward Tomasz Napierala 	chgproccnt(cred->cr_ruidinfo, -1, 0);
1050d00947d8SCraig Rodrigues 	crfree(cred);
1051d00947d8SCraig Rodrigues 
1052d00947d8SCraig Rodrigues 	return (error);
1053d00947d8SCraig Rodrigues }
1054d00947d8SCraig Rodrigues 
10556c8ded00SJason A. Harmening static inline void
unionfs_forward_vop_ref(struct vnode * basevp,int * lkflags)10566c8ded00SJason A. Harmening unionfs_forward_vop_ref(struct vnode *basevp, int *lkflags)
10576c8ded00SJason A. Harmening {
10586c8ded00SJason A. Harmening 	ASSERT_VOP_LOCKED(basevp, __func__);
10596c8ded00SJason A. Harmening 	*lkflags = VOP_ISLOCKED(basevp);
10606c8ded00SJason A. Harmening 	vref(basevp);
10616c8ded00SJason A. Harmening }
10626c8ded00SJason A. Harmening 
10636c8ded00SJason A. Harmening /*
10646c8ded00SJason A. Harmening  * Prepare unionfs to issue a forwarded VOP to either the upper or lower
10656c8ded00SJason A. Harmening  * FS.  This should be used for any VOP which may drop the vnode lock;
10666c8ded00SJason A. Harmening  * it is not required otherwise.
10676c8ded00SJason A. Harmening  * The unionfs vnode shares its lock with the base-layer vnode(s); if the
10686c8ded00SJason A. Harmening  * base FS must transiently drop its vnode lock, the unionfs vnode may
10696c8ded00SJason A. Harmening  * effectively become unlocked.  During that window, a concurrent forced
10706c8ded00SJason A. Harmening  * unmount may doom the unionfs vnode, which leads to two significant
10716c8ded00SJason A. Harmening  * issues:
10726c8ded00SJason A. Harmening  * 1) Completion of, and return from, the unionfs VOP with the unionfs
10736c8ded00SJason A. Harmening  *    vnode completely unlocked.  When the unionfs vnode becomes doomed
10746c8ded00SJason A. Harmening  *    it stops sharing its lock with the base vnode, so even if the
10756c8ded00SJason A. Harmening  *    forwarded VOP reacquires the base vnode lock the unionfs vnode
10766c8ded00SJason A. Harmening  *    lock will no longer be held.  This can lead to violation of the
10776c8ded00SJason A. Harmening  *    caller's sychronization requirements as well as various failed
10786c8ded00SJason A. Harmening  *    locking assertions when DEBUG_VFS_LOCKS is enabled.
10796c8ded00SJason A. Harmening  * 2) Loss of reference on the base vnode.  The caller is expected to
10806c8ded00SJason A. Harmening  *    hold a v_usecount reference on the unionfs vnode, while the
10816c8ded00SJason A. Harmening  *    unionfs vnode holds a reference on the base-layer vnode(s).  But
10826c8ded00SJason A. Harmening  *    these references are released when the unionfs vnode becomes
10836c8ded00SJason A. Harmening  *    doomed, violating the base layer's expectation that its caller
10846c8ded00SJason A. Harmening  *    must hold a reference to prevent vnode recycling.
10856c8ded00SJason A. Harmening  *
10866c8ded00SJason A. Harmening  * basevp1 and basevp2 represent two base-layer vnodes which are
10876c8ded00SJason A. Harmening  * expected to be locked when this function is called.  basevp2
10886c8ded00SJason A. Harmening  * may be NULL, but if not NULL basevp1 and basevp2 should represent
10896c8ded00SJason A. Harmening  * a parent directory and a filed linked to it, respectively.
10906c8ded00SJason A. Harmening  * lkflags1 and lkflags2 are output parameters that will store the
10916c8ded00SJason A. Harmening  * current lock status of basevp1 and basevp2, respectively.  They
10926c8ded00SJason A. Harmening  * are intended to be passed as the lkflags1 and lkflags2 parameters
10936c8ded00SJason A. Harmening  * in the subsequent call to unionfs_forward_vop_finish_pair().
10946c8ded00SJason A. Harmening  * lkflags2 may be NULL iff basevp2 is NULL.
10956c8ded00SJason A. Harmening  */
10966c8ded00SJason A. Harmening void
unionfs_forward_vop_start_pair(struct vnode * basevp1,int * lkflags1,struct vnode * basevp2,int * lkflags2)10976c8ded00SJason A. Harmening unionfs_forward_vop_start_pair(struct vnode *basevp1, int *lkflags1,
10986c8ded00SJason A. Harmening     struct vnode *basevp2, int *lkflags2)
10996c8ded00SJason A. Harmening {
11006c8ded00SJason A. Harmening 	/*
11016c8ded00SJason A. Harmening 	 * Take an additional reference on the base-layer vnodes to
11026c8ded00SJason A. Harmening 	 * avoid loss of reference if the unionfs vnodes are doomed.
11036c8ded00SJason A. Harmening 	 */
11046c8ded00SJason A. Harmening 	unionfs_forward_vop_ref(basevp1, lkflags1);
11056c8ded00SJason A. Harmening 	if (basevp2 != NULL)
11066c8ded00SJason A. Harmening 		unionfs_forward_vop_ref(basevp2, lkflags2);
11076c8ded00SJason A. Harmening }
11086c8ded00SJason A. Harmening 
11096c8ded00SJason A. Harmening static inline bool
unionfs_forward_vop_rele(struct vnode * unionvp,struct vnode * basevp,int lkflags)11106c8ded00SJason A. Harmening unionfs_forward_vop_rele(struct vnode *unionvp, struct vnode *basevp,
11116c8ded00SJason A. Harmening     int lkflags)
11126c8ded00SJason A. Harmening {
11136c8ded00SJason A. Harmening 	bool unionvp_doomed;
11146c8ded00SJason A. Harmening 
11156c8ded00SJason A. Harmening 	if (__predict_false(VTOUNIONFS(unionvp) == NULL)) {
11166c8ded00SJason A. Harmening 		if ((lkflags & LK_EXCLUSIVE) != 0)
11176c8ded00SJason A. Harmening 			ASSERT_VOP_ELOCKED(basevp, __func__);
11186c8ded00SJason A. Harmening 		else
11196c8ded00SJason A. Harmening 			ASSERT_VOP_LOCKED(basevp, __func__);
11206c8ded00SJason A. Harmening 		unionvp_doomed = true;
11216c8ded00SJason A. Harmening 	} else {
11226c8ded00SJason A. Harmening 		vrele(basevp);
11236c8ded00SJason A. Harmening 		unionvp_doomed = false;
11246c8ded00SJason A. Harmening 	}
11256c8ded00SJason A. Harmening 
11266c8ded00SJason A. Harmening 	return (unionvp_doomed);
11276c8ded00SJason A. Harmening }
11286c8ded00SJason A. Harmening 
11296c8ded00SJason A. Harmening 
11306c8ded00SJason A. Harmening /*
11316c8ded00SJason A. Harmening  * Indicate completion of a forwarded VOP previously prepared by
11326c8ded00SJason A. Harmening  * unionfs_forward_vop_start_pair().
11336c8ded00SJason A. Harmening  * basevp1 and basevp2 must be the same values passed to the prior
11346c8ded00SJason A. Harmening  * call to unionfs_forward_vop_start_pair().  unionvp1 and unionvp2
11356c8ded00SJason A. Harmening  * must be the unionfs vnodes that were initially above basevp1 and
11366c8ded00SJason A. Harmening  * basevp2, respectively.
11376c8ded00SJason A. Harmening  * basevp1 and basevp2 (if not NULL) must be locked when this function
11386c8ded00SJason A. Harmening  * is called, while unionvp1 and/or unionvp2 may be unlocked if either
11396c8ded00SJason A. Harmening  * unionfs vnode has become doomed.
11406c8ded00SJason A. Harmening  * lkflags1 and lkflag2 represent the locking flags that should be
11416c8ded00SJason A. Harmening  * used to re-lock unionvp1 and unionvp2, respectively, if either
11426c8ded00SJason A. Harmening  * vnode has become doomed.
11436c8ded00SJason A. Harmening  *
11446c8ded00SJason A. Harmening  * Returns true if any unionfs vnode was found to be doomed, false
11456c8ded00SJason A. Harmening  * otherwise.
11466c8ded00SJason A. Harmening  */
11476c8ded00SJason A. Harmening bool
unionfs_forward_vop_finish_pair(struct vnode * unionvp1,struct vnode * basevp1,int lkflags1,struct vnode * unionvp2,struct vnode * basevp2,int lkflags2)11486c8ded00SJason A. Harmening unionfs_forward_vop_finish_pair(
11496c8ded00SJason A. Harmening     struct vnode *unionvp1, struct vnode *basevp1, int lkflags1,
11506c8ded00SJason A. Harmening     struct vnode *unionvp2, struct vnode *basevp2, int lkflags2)
11516c8ded00SJason A. Harmening {
11526c8ded00SJason A. Harmening 	bool vp1_doomed, vp2_doomed;
11536c8ded00SJason A. Harmening 
11546c8ded00SJason A. Harmening 	/*
11556c8ded00SJason A. Harmening 	 * If either vnode is found to have been doomed, set
11566c8ded00SJason A. Harmening 	 * a flag indicating that it needs to be re-locked.
11576c8ded00SJason A. Harmening 	 * Otherwise, simply drop the base-vnode reference that
11586c8ded00SJason A. Harmening 	 * was taken in unionfs_forward_vop_start().
11596c8ded00SJason A. Harmening 	 */
11606c8ded00SJason A. Harmening 	vp1_doomed = unionfs_forward_vop_rele(unionvp1, basevp1, lkflags1);
11616c8ded00SJason A. Harmening 
11626c8ded00SJason A. Harmening 	if (unionvp2 != NULL)
11636c8ded00SJason A. Harmening 		vp2_doomed = unionfs_forward_vop_rele(unionvp2, basevp2, lkflags2);
11646c8ded00SJason A. Harmening 	else
11656c8ded00SJason A. Harmening 		vp2_doomed = false;
11666c8ded00SJason A. Harmening 
11676c8ded00SJason A. Harmening 	/*
11686c8ded00SJason A. Harmening 	 * If any of the unionfs vnodes need to be re-locked, that
11696c8ded00SJason A. Harmening 	 * means the unionfs vnode's lock is now de-coupled from the
11706c8ded00SJason A. Harmening 	 * corresponding base vnode.  We therefore need to drop the
11716c8ded00SJason A. Harmening 	 * base vnode lock (since nothing else will after this point),
11726c8ded00SJason A. Harmening 	 * and also release the reference taken in
11736c8ded00SJason A. Harmening 	 * unionfs_forward_vop_start_pair().
11746c8ded00SJason A. Harmening 	 */
11756c8ded00SJason A. Harmening 	if (__predict_false(vp1_doomed && vp2_doomed))
11766c8ded00SJason A. Harmening 		VOP_VPUT_PAIR(basevp1, &basevp2, true);
11776c8ded00SJason A. Harmening 	else if (__predict_false(vp1_doomed)) {
11786c8ded00SJason A. Harmening 		/*
11796c8ded00SJason A. Harmening 		 * If basevp1 needs to be unlocked, then we may not
11806c8ded00SJason A. Harmening 		 * be able to safely unlock it with basevp2 still locked,
11816c8ded00SJason A. Harmening 		 * for the same reason that an ordinary VFS call would
11826c8ded00SJason A. Harmening 		 * need to use VOP_VPUT_PAIR() here.  We might be able
11836c8ded00SJason A. Harmening 		 * to use VOP_VPUT_PAIR(..., false) here, but then we
11846c8ded00SJason A. Harmening 		 * would need to deal with the possibility of basevp2
11856c8ded00SJason A. Harmening 		 * changing out from under us, which could result in
11866c8ded00SJason A. Harmening 		 * either the unionfs vnode becoming doomed or its
11876c8ded00SJason A. Harmening 		 * upper/lower vp no longer matching basevp2.  Either
11886c8ded00SJason A. Harmening 		 * scenario would require at least re-locking the unionfs
11896c8ded00SJason A. Harmening 		 * vnode anyway.
11906c8ded00SJason A. Harmening 		 */
11916c8ded00SJason A. Harmening 		if (unionvp2 != NULL) {
11926c8ded00SJason A. Harmening 			VOP_UNLOCK(unionvp2);
11936c8ded00SJason A. Harmening 			vp2_doomed = true;
11946c8ded00SJason A. Harmening 		}
11956c8ded00SJason A. Harmening 		vput(basevp1);
11966c8ded00SJason A. Harmening 	} else if (__predict_false(vp2_doomed))
11976c8ded00SJason A. Harmening 		vput(basevp2);
11986c8ded00SJason A. Harmening 
11996c8ded00SJason A. Harmening 	if (__predict_false(vp1_doomed || vp2_doomed))
12006c8ded00SJason A. Harmening 		vn_lock_pair(unionvp1, !vp1_doomed, lkflags1,
12016c8ded00SJason A. Harmening 		    unionvp2, !vp2_doomed, lkflags2);
12026c8ded00SJason A. Harmening 
12036c8ded00SJason A. Harmening 	return (vp1_doomed || vp2_doomed);
12046c8ded00SJason A. Harmening }
12056c8ded00SJason A. Harmening 
1206d00947d8SCraig Rodrigues /*
1207d00947d8SCraig Rodrigues  * Create a new whiteout.
1208d00947d8SCraig Rodrigues  *
1209eb60ff1eSJason A. Harmening  * dvp and vp are unionfs vnodes representing a parent directory and
1210eb60ff1eSJason A. Harmening  * child file, should be locked on entry, and will be locked on return.
1211d00947d8SCraig Rodrigues  */
1212d00947d8SCraig Rodrigues int
unionfs_mkwhiteout(struct vnode * dvp,struct vnode * vp,struct componentname * cnp,struct thread * td,char * path,int pathlen)1213eb60ff1eSJason A. Harmening unionfs_mkwhiteout(struct vnode *dvp, struct vnode *vp,
12146c8ded00SJason A. Harmening     struct componentname *cnp, struct thread *td, char *path, int pathlen)
1215d00947d8SCraig Rodrigues {
1216eb60ff1eSJason A. Harmening 	struct vnode   *udvp;
1217d00947d8SCraig Rodrigues 	struct vnode   *wvp;
1218190110f2SKonstantin Belousov 	struct nameidata nd;
1219d00947d8SCraig Rodrigues 	struct mount   *mp;
1220abe95116SJason A. Harmening 	int		error;
1221eb60ff1eSJason A. Harmening 	bool		dvp_locked;
1222d00947d8SCraig Rodrigues 
1223eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(dvp, __func__);
1224eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(vp, __func__);
1225eb60ff1eSJason A. Harmening 
1226eb60ff1eSJason A. Harmening 	udvp = VTOUNIONFS(dvp)->un_uppervp;
1227d00947d8SCraig Rodrigues 	wvp = NULLVP;
1228190110f2SKonstantin Belousov 	NDPREINIT(&nd);
1229eb60ff1eSJason A. Harmening 	vref(udvp);
1230eb60ff1eSJason A. Harmening 	VOP_UNLOCK(vp);
12316c8ded00SJason A. Harmening 	if ((error = unionfs_relookup(udvp, &wvp, cnp, &nd.ni_cnd, td, path,
1232abe95116SJason A. Harmening 	    pathlen, CREATE))) {
1233eb60ff1eSJason A. Harmening 		goto unionfs_mkwhiteout_cleanup;
1234d00947d8SCraig Rodrigues 	}
1235abe95116SJason A. Harmening 	if (wvp != NULLVP) {
12366c8ded00SJason A. Harmening 		if (udvp == wvp)
1237d00947d8SCraig Rodrigues 			vrele(wvp);
1238d00947d8SCraig Rodrigues 		else
1239d00947d8SCraig Rodrigues 			vput(wvp);
1240d00947d8SCraig Rodrigues 
1241eb60ff1eSJason A. Harmening 		if (nd.ni_cnd.cn_flags & ISWHITEOUT)
1242eb60ff1eSJason A. Harmening 			error = 0;
1243eb60ff1eSJason A. Harmening 		else
1244eb60ff1eSJason A. Harmening 			error = EEXIST;
1245eb60ff1eSJason A. Harmening 		goto unionfs_mkwhiteout_cleanup;
1246d00947d8SCraig Rodrigues 	}
1247d00947d8SCraig Rodrigues 
12486c8ded00SJason A. Harmening 	if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)))
1249eb60ff1eSJason A. Harmening 		goto unionfs_mkwhiteout_cleanup;
12506c8ded00SJason A. Harmening 	error = VOP_WHITEOUT(udvp, &nd.ni_cnd, CREATE);
1251d00947d8SCraig Rodrigues 	vn_finished_write(mp);
1252d00947d8SCraig Rodrigues 
1253eb60ff1eSJason A. Harmening unionfs_mkwhiteout_cleanup:
1254eb60ff1eSJason A. Harmening 	if (VTOUNIONFS(dvp) == NULL) {
1255eb60ff1eSJason A. Harmening 		vput(udvp);
1256eb60ff1eSJason A. Harmening 		dvp_locked = false;
1257eb60ff1eSJason A. Harmening 	} else {
1258eb60ff1eSJason A. Harmening 		vrele(udvp);
1259eb60ff1eSJason A. Harmening 		dvp_locked = true;
1260eb60ff1eSJason A. Harmening 	}
1261eb60ff1eSJason A. Harmening 	vn_lock_pair(dvp, dvp_locked, LK_EXCLUSIVE, vp, false, LK_EXCLUSIVE);
1262d00947d8SCraig Rodrigues 	return (error);
1263d00947d8SCraig Rodrigues }
1264d00947d8SCraig Rodrigues 
1265d00947d8SCraig Rodrigues /*
1266d00947d8SCraig Rodrigues  * Create a new vnode for create a new shadow file.
1267d00947d8SCraig Rodrigues  *
1268d00947d8SCraig Rodrigues  * If an error is returned, *vpp will be invalid, otherwise it will hold a
1269d00947d8SCraig Rodrigues  * locked, referenced and opened vnode.
1270d00947d8SCraig Rodrigues  *
1271d00947d8SCraig Rodrigues  * unp is never updated.
1272df8bae1dSRodney W. Grimes  */
127380b301c3SPoul-Henning Kamp static int
unionfs_vn_create_on_upper(struct vnode ** vpp,struct vnode * udvp,struct vnode * vp,struct vattr * uvap,struct thread * td)1274d00947d8SCraig Rodrigues unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
1275eb60ff1eSJason A. Harmening     struct vnode *vp, struct vattr *uvap, struct thread *td)
1276df8bae1dSRodney W. Grimes {
1277d00947d8SCraig Rodrigues 	struct unionfs_mount *ump;
1278eb60ff1eSJason A. Harmening 	struct unionfs_node *unp;
1279eb60ff1eSJason A. Harmening 	struct vnode   *uvp;
1280d00947d8SCraig Rodrigues 	struct vnode   *lvp;
1281d00947d8SCraig Rodrigues 	struct ucred   *cred;
1282d00947d8SCraig Rodrigues 	struct vattr	lva;
1283312d49efSJason A. Harmening 	struct nameidata nd;
1284d00947d8SCraig Rodrigues 	int		fmode;
1285d00947d8SCraig Rodrigues 	int		error;
1286d00947d8SCraig Rodrigues 
1287eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(vp, __func__);
1288eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
1289d00947d8SCraig Rodrigues 	ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
1290eb60ff1eSJason A. Harmening 	uvp = NULLVP;
1291d00947d8SCraig Rodrigues 	lvp = unp->un_lowervp;
1292d00947d8SCraig Rodrigues 	cred = td->td_ucred;
1293d00947d8SCraig Rodrigues 	fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
1294d00947d8SCraig Rodrigues 	error = 0;
1295d00947d8SCraig Rodrigues 
12960359a12eSAttilio Rao 	if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
1297d00947d8SCraig Rodrigues 		return (error);
1298d00947d8SCraig Rodrigues 	unionfs_create_uppervattr_core(ump, &lva, uvap, td);
1299d00947d8SCraig Rodrigues 
1300d00947d8SCraig Rodrigues 	if (unp->un_path == NULL)
13013ecefc4aSJason A. Harmening 		panic("%s: NULL un_path", __func__);
1302d00947d8SCraig Rodrigues 
1303abe95116SJason A. Harmening 	nd.ni_cnd.cn_namelen = unp->un_pathlen;
1304abe95116SJason A. Harmening 	nd.ni_cnd.cn_pnbuf = unp->un_path;
1305190110f2SKonstantin Belousov 	nd.ni_cnd.cn_nameiop = CREATE;
13065b5b7e2cSMateusz Guzik 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | ISLASTCN;
1307190110f2SKonstantin Belousov 	nd.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
1308190110f2SKonstantin Belousov 	nd.ni_cnd.cn_cred = cred;
1309190110f2SKonstantin Belousov 	nd.ni_cnd.cn_nameptr = nd.ni_cnd.cn_pnbuf;
1310190110f2SKonstantin Belousov 	NDPREINIT(&nd);
1311d00947d8SCraig Rodrigues 
1312d00947d8SCraig Rodrigues 	vref(udvp);
1313eb60ff1eSJason A. Harmening 	VOP_UNLOCK(vp);
1314eb60ff1eSJason A. Harmening 	if ((error = vfs_relookup(udvp, &uvp, &nd.ni_cnd, false)) != 0) {
1315d00947d8SCraig Rodrigues 		vrele(udvp);
1316eb60ff1eSJason A. Harmening 		return (error);
1317eb60ff1eSJason A. Harmening 	}
1318d00947d8SCraig Rodrigues 
1319eb60ff1eSJason A. Harmening 	if (uvp != NULLVP) {
1320eb60ff1eSJason A. Harmening 		if (uvp == udvp)
1321eb60ff1eSJason A. Harmening 			vrele(uvp);
1322d00947d8SCraig Rodrigues 		else
1323eb60ff1eSJason A. Harmening 			vput(uvp);
1324d00947d8SCraig Rodrigues 		error = EEXIST;
1325eb60ff1eSJason A. Harmening 		goto unionfs_vn_create_on_upper_cleanup;
1326d00947d8SCraig Rodrigues 	}
1327d00947d8SCraig Rodrigues 
1328eb60ff1eSJason A. Harmening 	if ((error = VOP_CREATE(udvp, &uvp, &nd.ni_cnd, uvap)) != 0)
1329eb60ff1eSJason A. Harmening 		goto unionfs_vn_create_on_upper_cleanup;
1330d00947d8SCraig Rodrigues 
1331eb60ff1eSJason A. Harmening 	if ((error = VOP_OPEN(uvp, fmode, cred, td, NULL)) != 0) {
1332eb60ff1eSJason A. Harmening 		vput(uvp);
1333eb60ff1eSJason A. Harmening 		goto unionfs_vn_create_on_upper_cleanup;
1334d00947d8SCraig Rodrigues 	}
1335eb60ff1eSJason A. Harmening 	error = VOP_ADD_WRITECOUNT(uvp, 1);
1336*9b505845SJason A. Harmening 	CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1337*9b505845SJason A. Harmening 	    __func__, uvp, uvp->v_writecount);
133878022527SKonstantin Belousov 	if (error == 0) {
1339eb60ff1eSJason A. Harmening 		*vpp = uvp;
134078022527SKonstantin Belousov 	} else {
1341eb60ff1eSJason A. Harmening 		VOP_CLOSE(uvp, fmode, cred, td);
134278022527SKonstantin Belousov 	}
1343d00947d8SCraig Rodrigues 
1344eb60ff1eSJason A. Harmening unionfs_vn_create_on_upper_cleanup:
1345eb60ff1eSJason A. Harmening 	vput(udvp);
1346d00947d8SCraig Rodrigues 	return (error);
1347d00947d8SCraig Rodrigues }
1348d00947d8SCraig Rodrigues 
1349d00947d8SCraig Rodrigues /*
1350d00947d8SCraig Rodrigues  * Copy from lvp to uvp.
1351d00947d8SCraig Rodrigues  *
1352d00947d8SCraig Rodrigues  * lvp and uvp should be locked and opened on entry and will be locked and
1353d00947d8SCraig Rodrigues  * opened on return.
1354d00947d8SCraig Rodrigues  */
1355d00947d8SCraig Rodrigues static int
unionfs_copyfile_core(struct vnode * lvp,struct vnode * uvp,struct ucred * cred,struct thread * td)1356d00947d8SCraig Rodrigues unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
1357d00947d8SCraig Rodrigues     struct ucred *cred, struct thread *td)
1358d00947d8SCraig Rodrigues {
1359df8bae1dSRodney W. Grimes 	char           *buf;
1360df8bae1dSRodney W. Grimes 	struct uio	uio;
1361df8bae1dSRodney W. Grimes 	struct iovec	iov;
1362312d49efSJason A. Harmening 	off_t		offset;
1363312d49efSJason A. Harmening 	int		count;
1364312d49efSJason A. Harmening 	int		error;
1365312d49efSJason A. Harmening 	int		bufoffset;
1366df8bae1dSRodney W. Grimes 
1367d00947d8SCraig Rodrigues 	error = 0;
1368d00947d8SCraig Rodrigues 	memset(&uio, 0, sizeof(uio));
13692a31267eSMatthew Dillon 
1370b40ce416SJulian Elischer 	uio.uio_td = td;
1371df8bae1dSRodney W. Grimes 	uio.uio_segflg = UIO_SYSSPACE;
1372df8bae1dSRodney W. Grimes 	uio.uio_offset = 0;
1373df8bae1dSRodney W. Grimes 
1374a163d034SWarner Losh 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
1375df8bae1dSRodney W. Grimes 
1376d00947d8SCraig Rodrigues 	while (error == 0) {
1377d00947d8SCraig Rodrigues 		offset = uio.uio_offset;
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes 		uio.uio_iov = &iov;
1380df8bae1dSRodney W. Grimes 		uio.uio_iovcnt = 1;
1381df8bae1dSRodney W. Grimes 		iov.iov_base = buf;
1382df8bae1dSRodney W. Grimes 		iov.iov_len = MAXBSIZE;
1383df8bae1dSRodney W. Grimes 		uio.uio_resid = iov.iov_len;
1384df8bae1dSRodney W. Grimes 		uio.uio_rw = UIO_READ;
1385df8bae1dSRodney W. Grimes 
1386d00947d8SCraig Rodrigues 		if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
13872a31267eSMatthew Dillon 			break;
13882a31267eSMatthew Dillon 		if ((count = MAXBSIZE - uio.uio_resid) == 0)
13892a31267eSMatthew Dillon 			break;
13902a31267eSMatthew Dillon 
1391d00947d8SCraig Rodrigues 		bufoffset = 0;
13922a31267eSMatthew Dillon 		while (bufoffset < count) {
1393df8bae1dSRodney W. Grimes 			uio.uio_iov = &iov;
1394df8bae1dSRodney W. Grimes 			uio.uio_iovcnt = 1;
13952a31267eSMatthew Dillon 			iov.iov_base = buf + bufoffset;
13962a31267eSMatthew Dillon 			iov.iov_len = count - bufoffset;
13972a31267eSMatthew Dillon 			uio.uio_offset = offset + bufoffset;
1398df8bae1dSRodney W. Grimes 			uio.uio_resid = iov.iov_len;
1399d00947d8SCraig Rodrigues 			uio.uio_rw = UIO_WRITE;
1400df8bae1dSRodney W. Grimes 
1401d00947d8SCraig Rodrigues 			if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
1402df8bae1dSRodney W. Grimes 				break;
1403d00947d8SCraig Rodrigues 
14042a31267eSMatthew Dillon 			bufoffset += (count - bufoffset) - uio.uio_resid;
1405df8bae1dSRodney W. Grimes 		}
1406d00947d8SCraig Rodrigues 
14072a31267eSMatthew Dillon 		uio.uio_offset = offset + bufoffset;
1408d00947d8SCraig Rodrigues 	}
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	free(buf, M_TEMP);
1411d00947d8SCraig Rodrigues 
1412df8bae1dSRodney W. Grimes 	return (error);
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
1415df8bae1dSRodney W. Grimes /*
1416d00947d8SCraig Rodrigues  * Copy file from lower to upper.
14172a31267eSMatthew Dillon  *
1418d00947d8SCraig Rodrigues  * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
1419d00947d8SCraig Rodrigues  * docopy.
1420d00947d8SCraig Rodrigues  *
1421eb60ff1eSJason A. Harmening  * vp is a unionfs vnode that should be locked on entry and will be
1422eb60ff1eSJason A. Harmening  * locked on return.
1423eb60ff1eSJason A. Harmening  *
1424d00947d8SCraig Rodrigues  * If no error returned, unp will be updated.
1425996c772fSJohn Dyson  */
1426996c772fSJohn Dyson int
unionfs_copyfile(struct vnode * vp,int docopy,struct ucred * cred,struct thread * td)1427eb60ff1eSJason A. Harmening unionfs_copyfile(struct vnode *vp, int docopy, struct ucred *cred,
1428d00947d8SCraig Rodrigues     struct thread *td)
1429996c772fSJohn Dyson {
1430eb60ff1eSJason A. Harmening 	struct unionfs_node *unp;
1431eb60ff1eSJason A. Harmening 	struct unionfs_node *dunp;
1432f2a2857bSKirk McKusick 	struct mount   *mp;
1433d00947d8SCraig Rodrigues 	struct vnode   *udvp;
1434d00947d8SCraig Rodrigues 	struct vnode   *lvp;
1435d00947d8SCraig Rodrigues 	struct vnode   *uvp;
1436d00947d8SCraig Rodrigues 	struct vattr	uva;
1437312d49efSJason A. Harmening 	int		error;
1438996c772fSJohn Dyson 
1439eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(vp, __func__);
1440eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
1441d00947d8SCraig Rodrigues 	lvp = unp->un_lowervp;
1442d00947d8SCraig Rodrigues 	uvp = NULLVP;
1443d00947d8SCraig Rodrigues 
1444d00947d8SCraig Rodrigues 	if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
1445d00947d8SCraig Rodrigues 		return (EROFS);
1446d00947d8SCraig Rodrigues 	if (unp->un_dvp == NULLVP)
1447d00947d8SCraig Rodrigues 		return (EINVAL);
1448d00947d8SCraig Rodrigues 	if (unp->un_uppervp != NULLVP)
1449d00947d8SCraig Rodrigues 		return (EEXIST);
1450eb60ff1eSJason A. Harmening 
1451eb60ff1eSJason A. Harmening 	udvp = NULLVP;
1452eb60ff1eSJason A. Harmening 	VI_LOCK(unp->un_dvp);
1453eb60ff1eSJason A. Harmening 	dunp = VTOUNIONFS(unp->un_dvp);
1454eb60ff1eSJason A. Harmening 	if (dunp != NULL)
1455eb60ff1eSJason A. Harmening 		udvp = dunp->un_uppervp;
1456eb60ff1eSJason A. Harmening 	VI_UNLOCK(unp->un_dvp);
1457eb60ff1eSJason A. Harmening 
1458d00947d8SCraig Rodrigues 	if (udvp == NULLVP)
1459d00947d8SCraig Rodrigues 		return (EROFS);
1460d00947d8SCraig Rodrigues 	if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
1461d00947d8SCraig Rodrigues 		return (EROFS);
1462eb60ff1eSJason A. Harmening 	ASSERT_VOP_UNLOCKED(udvp, __func__);
1463eb60ff1eSJason A. Harmening 
1464eb60ff1eSJason A. Harmening 	error = unionfs_set_in_progress_flag(vp, UNIONFS_COPY_IN_PROGRESS);
1465eb60ff1eSJason A. Harmening 	if (error == EJUSTRETURN)
1466eb60ff1eSJason A. Harmening 		return (0);
1467eb60ff1eSJason A. Harmening 	else if (error != 0)
1468eb60ff1eSJason A. Harmening 		return (error);
1469d00947d8SCraig Rodrigues 
1470d00947d8SCraig Rodrigues 	error = VOP_ACCESS(lvp, VREAD, cred, td);
1471d00947d8SCraig Rodrigues 	if (error != 0)
1472eb60ff1eSJason A. Harmening 		goto unionfs_copyfile_cleanup;
14735842d4e5SKATO Takenori 
1474a75d1dddSMateusz Guzik 	if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)) != 0)
1475eb60ff1eSJason A. Harmening 		goto unionfs_copyfile_cleanup;
1476eb60ff1eSJason A. Harmening 	error = unionfs_vn_create_on_upper(&uvp, udvp, vp, &uva, td);
1477d00947d8SCraig Rodrigues 	if (error != 0) {
1478f2a2857bSKirk McKusick 		vn_finished_write(mp);
1479eb60ff1eSJason A. Harmening 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1480eb60ff1eSJason A. Harmening 		goto unionfs_copyfile_cleanup;
1481eb60ff1eSJason A. Harmening 	}
1482eb60ff1eSJason A. Harmening 
1483eb60ff1eSJason A. Harmening 	/*
1484eb60ff1eSJason A. Harmening 	 * Note that it's still possible for e.g. VOP_WRITE to relock
1485eb60ff1eSJason A. Harmening 	 * uvp below while holding vp[=lvp] locked.  Replacing
1486eb60ff1eSJason A. Harmening 	 * unionfs_copyfile_core with vn_generic_copy_file_range() will
1487eb60ff1eSJason A. Harmening 	 * allow us to avoid the problem by moving this vn_lock_pair()
1488eb60ff1eSJason A. Harmening 	 * call much later.
1489eb60ff1eSJason A. Harmening 	 */
1490eb60ff1eSJason A. Harmening 	vn_lock_pair(vp, false, LK_EXCLUSIVE, uvp, true, LK_EXCLUSIVE);
1491eb60ff1eSJason A. Harmening 	unp = VTOUNIONFS(vp);
1492eb60ff1eSJason A. Harmening 	if (unp == NULL) {
1493eb60ff1eSJason A. Harmening 		error = ENOENT;
1494eb60ff1eSJason A. Harmening 		goto unionfs_copyfile_cleanup;
1495f2a2857bSKirk McKusick 	}
1496996c772fSJohn Dyson 
1497d00947d8SCraig Rodrigues 	if (docopy != 0) {
14989e223287SKonstantin Belousov 		error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
1499996c772fSJohn Dyson 		if (error == 0) {
1500d00947d8SCraig Rodrigues 			error = unionfs_copyfile_core(lvp, uvp, cred, td);
1501d00947d8SCraig Rodrigues 			VOP_CLOSE(lvp, FREAD, cred, td);
1502996c772fSJohn Dyson 		}
1503d00947d8SCraig Rodrigues 	}
1504d00947d8SCraig Rodrigues 	VOP_CLOSE(uvp, FWRITE, cred, td);
150578022527SKonstantin Belousov 	VOP_ADD_WRITECOUNT_CHECKED(uvp, -1);
1506312d49efSJason A. Harmening 	CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1507312d49efSJason A. Harmening 	    __func__, uvp, uvp->v_writecount);
1508996c772fSJohn Dyson 
1509f2a2857bSKirk McKusick 	vn_finished_write(mp);
1510d00947d8SCraig Rodrigues 
1511996c772fSJohn Dyson 	if (error == 0) {
1512d00947d8SCraig Rodrigues 		/* Reset the attributes. Ignore errors. */
1513d00947d8SCraig Rodrigues 		uva.va_type = VNON;
15140359a12eSAttilio Rao 		VOP_SETATTR(uvp, &uva, cred);
1515eb60ff1eSJason A. Harmening 		unionfs_node_update(unp, uvp, td);
1516996c772fSJohn Dyson 	}
1517996c772fSJohn Dyson 
1518eb60ff1eSJason A. Harmening unionfs_copyfile_cleanup:
1519eb60ff1eSJason A. Harmening 	unionfs_clear_in_progress_flag(vp, UNIONFS_COPY_IN_PROGRESS);
15202a31267eSMatthew Dillon 	return (error);
1521b422956cSPoul-Henning Kamp }
1522996c772fSJohn Dyson 
15232a31267eSMatthew Dillon /*
1524eb60ff1eSJason A. Harmening  * Determine if the unionfs view of a directory is empty such that
1525eb60ff1eSJason A. Harmening  * an rmdir operation can be permitted.
15262a31267eSMatthew Dillon  *
1527eb60ff1eSJason A. Harmening  * We assume the VOP_RMDIR() against the upper layer vnode will take
1528eb60ff1eSJason A. Harmening  * care of this check for us where the upper FS is concerned, so here
1529eb60ff1eSJason A. Harmening  * we concentrate on the lower FS.  We need to check for the presence
1530eb60ff1eSJason A. Harmening  * of files other than "." and ".." in the lower FS directory and
1531eb60ff1eSJason A. Harmening  * then cross-check any files we find against the upper FS to see if
1532eb60ff1eSJason A. Harmening  * a whiteout is present (in which case we treat the lower file as
1533eb60ff1eSJason A. Harmening  * non-present).
1534eb60ff1eSJason A. Harmening  *
1535eb60ff1eSJason A. Harmening  * The logic here is based heavily on vn_dir_check_empty().
1536eb60ff1eSJason A. Harmening  *
1537eb60ff1eSJason A. Harmening  * vp should be a locked unionfs node, and vp's lowervp should also be
1538eb60ff1eSJason A. Harmening  * locked.
1539df8bae1dSRodney W. Grimes  */
1540df8bae1dSRodney W. Grimes int
unionfs_check_rmdir(struct vnode * vp,struct ucred * cred,struct thread * td)1541d00947d8SCraig Rodrigues unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td)
1542df8bae1dSRodney W. Grimes {
1543d00947d8SCraig Rodrigues 	struct vnode   *uvp;
1544d00947d8SCraig Rodrigues 	struct vnode   *lvp;
1545d00947d8SCraig Rodrigues 	struct vnode   *tvp;
1546eb60ff1eSJason A. Harmening 	char *dirbuf;
1547eb60ff1eSJason A. Harmening 	size_t dirbuflen, len;
1548eb60ff1eSJason A. Harmening 	off_t off;
1549312d49efSJason A. Harmening 	struct dirent  *dp;
1550df8bae1dSRodney W. Grimes 	struct componentname cn;
1551312d49efSJason A. Harmening 	struct vattr	va;
1552312d49efSJason A. Harmening 	int		error;
1553312d49efSJason A. Harmening 	int		eofflag;
1554df8bae1dSRodney W. Grimes 
1555d00947d8SCraig Rodrigues 	eofflag = 0;
1556d00947d8SCraig Rodrigues 	lvp = UNIONFSVPTOLOWERVP(vp);
1557eb60ff1eSJason A. Harmening 	uvp = UNIONFSVPTOUPPERVP(vp);
1558df8bae1dSRodney W. Grimes 
1559eb60ff1eSJason A. Harmening 	/*
1560eb60ff1eSJason A. Harmening 	 * Note that the locking here still isn't ideal: We expect the caller
1561eb60ff1eSJason A. Harmening 	 * to hold both the upper and lower layer locks as well as the upper
1562eb60ff1eSJason A. Harmening 	 * parent directory lock, which it can do in a manner that avoids
1563eb60ff1eSJason A. Harmening 	 * deadlock.  However, if the cross-check logic below needs to call
1564eb60ff1eSJason A. Harmening 	 * VOP_LOOKUP(), that may relock the upper vnode and lock any found
1565eb60ff1eSJason A. Harmening 	 * child vnode in a way that doesn't protect against deadlock given
1566eb60ff1eSJason A. Harmening 	 * the other held locks.  Beyond that, the various other VOPs we issue
1567eb60ff1eSJason A. Harmening 	 * below, such as VOP_OPEN() and VOP_READDIR(), may also re-lock the
1568eb60ff1eSJason A. Harmening 	 * lower vnode.
1569eb60ff1eSJason A. Harmening 	 * We might instead just handoff between the upper vnode lock
1570eb60ff1eSJason A. Harmening 	 * (and its parent directory lock) and the lower vnode lock as needed,
1571eb60ff1eSJason A. Harmening 	 * so that the lower lock is never held at the same time as the upper
1572eb60ff1eSJason A. Harmening 	 * locks, but that opens up a wider window in which the upper
1573eb60ff1eSJason A. Harmening 	 * directory (and also the lower directory if it isn't truly
1574eb60ff1eSJason A. Harmening 	 * read-only) may change while the relevant lock is dropped.  But
1575eb60ff1eSJason A. Harmening 	 * since re-locking may happen here and open up such a window anyway,
1576eb60ff1eSJason A. Harmening 	 * perhaps that is a worthwile tradeoff?  Or perhaps we can ultimately
1577eb60ff1eSJason A. Harmening 	 * do sufficient tracking of empty state within the unionfs vnode
1578eb60ff1eSJason A. Harmening 	 * (in conjunction with upcalls from the lower FSes to notify us
1579eb60ff1eSJason A. Harmening 	 * of out-of-band state changes) that we can avoid these costly checks
1580eb60ff1eSJason A. Harmening 	 * altogether.
1581eb60ff1eSJason A. Harmening 	 */
1582eb60ff1eSJason A. Harmening 	ASSERT_VOP_LOCKED(lvp, __func__);
1583eb60ff1eSJason A. Harmening 	ASSERT_VOP_ELOCKED(uvp, __func__);
1584eb60ff1eSJason A. Harmening 
15850359a12eSAttilio Rao 	if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
1586df8bae1dSRodney W. Grimes 		return (error);
1587d00947d8SCraig Rodrigues 	if (va.va_flags & OPAQUE)
1588d00947d8SCraig Rodrigues 		return (0);
1589df8bae1dSRodney W. Grimes 
15903282e2c4SDaichi GOTO #ifdef MAC
1591eb60ff1eSJason A. Harmening 	if ((error = mac_vnode_check_open(cred, lvp, VEXEC | VREAD)) != 0)
15923282e2c4SDaichi GOTO 		return (error);
15933282e2c4SDaichi GOTO #endif
1594eb60ff1eSJason A. Harmening 	if ((error = VOP_ACCESS(lvp, VEXEC | VREAD, cred, td)) != 0)
15953282e2c4SDaichi GOTO 		return (error);
1596eb60ff1eSJason A. Harmening 	if ((error = VOP_OPEN(lvp, FREAD, cred, td, NULL)) != 0)
1597eb60ff1eSJason A. Harmening 		return (error);
1598eb60ff1eSJason A. Harmening 	if ((error = VOP_GETATTR(lvp, &va, cred)) != 0)
1599996c772fSJohn Dyson 		return (error);
1600996c772fSJohn Dyson 
1601eb60ff1eSJason A. Harmening 	dirbuflen = max(DEV_BSIZE, GENERIC_MAXDIRSIZ);
1602eb60ff1eSJason A. Harmening 	if (dirbuflen < va.va_blocksize)
1603eb60ff1eSJason A. Harmening 		dirbuflen = va.va_blocksize;
1604eb60ff1eSJason A. Harmening 	dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK);
1605996c772fSJohn Dyson 
1606eb60ff1eSJason A. Harmening 	len = 0;
1607eb60ff1eSJason A. Harmening 	off = 0;
1608eb60ff1eSJason A. Harmening 	eofflag = 0;
1609996c772fSJohn Dyson 
1610eb60ff1eSJason A. Harmening 	for (;;) {
1611eb60ff1eSJason A. Harmening 		error = vn_dir_next_dirent(lvp, td, dirbuf, dirbuflen,
1612eb60ff1eSJason A. Harmening 		    &dp, &len, &off, &eofflag);
1613a68ae31cSDaichi GOTO 		if (error != 0)
1614d00947d8SCraig Rodrigues 			break;
1615996c772fSJohn Dyson 
1616eb60ff1eSJason A. Harmening 		if (len == 0) {
1617eb60ff1eSJason A. Harmening 			/* EOF */
1618eb60ff1eSJason A. Harmening 			error = 0;
1619eb60ff1eSJason A. Harmening 			break;
1620eb60ff1eSJason A. Harmening 		}
1621eb60ff1eSJason A. Harmening 
1622eb60ff1eSJason A. Harmening 		if (dp->d_type == DT_WHT)
1623d00947d8SCraig Rodrigues 			continue;
1624df8bae1dSRodney W. Grimes 
1625eb60ff1eSJason A. Harmening 		/*
1626eb60ff1eSJason A. Harmening 		 * Any file in the directory which is not '.' or '..' indicates
1627eb60ff1eSJason A. Harmening 		 * the directory is not empty.
1628eb60ff1eSJason A. Harmening 		 */
1629eb60ff1eSJason A. Harmening 		switch (dp->d_namlen) {
1630eb60ff1eSJason A. Harmening 		case 2:
1631eb60ff1eSJason A. Harmening 			if (dp->d_name[1] != '.') {
1632eb60ff1eSJason A. Harmening 				/* Can't be '..' (nor '.') */
1633eb60ff1eSJason A. Harmening 				break;
1634eb60ff1eSJason A. Harmening 			}
1635eb60ff1eSJason A. Harmening 			/* FALLTHROUGH */
1636eb60ff1eSJason A. Harmening 		case 1:
1637eb60ff1eSJason A. Harmening 			if (dp->d_name[0] != '.') {
1638eb60ff1eSJason A. Harmening 				/* Can't be '..' nor '.' */
1639eb60ff1eSJason A. Harmening 				break;
1640eb60ff1eSJason A. Harmening 			}
1641eb60ff1eSJason A. Harmening 			continue;
1642eb60ff1eSJason A. Harmening 		default:
1643eb60ff1eSJason A. Harmening 			break;
1644eb60ff1eSJason A. Harmening 		}
1645eb60ff1eSJason A. Harmening 
1646d00947d8SCraig Rodrigues 		cn.cn_namelen = dp->d_namlen;
1647d00947d8SCraig Rodrigues 		cn.cn_pnbuf = NULL;
1648d00947d8SCraig Rodrigues 		cn.cn_nameptr = dp->d_name;
1649d00947d8SCraig Rodrigues 		cn.cn_nameiop = LOOKUP;
16505b5b7e2cSMateusz Guzik 		cn.cn_flags = LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN;
1651d00947d8SCraig Rodrigues 		cn.cn_lkflags = LK_EXCLUSIVE;
1652d00947d8SCraig Rodrigues 		cn.cn_cred = cred;
1653df8bae1dSRodney W. Grimes 
1654eb60ff1eSJason A. Harmening 		error = VOP_LOOKUP(uvp, &tvp, &cn);
1655eb60ff1eSJason A. Harmening 		if (tvp != NULLVP)
1656d00947d8SCraig Rodrigues 			vput(tvp);
1657eb60ff1eSJason A. Harmening 		if (error != 0 && error != ENOENT && error != EJUSTRETURN)
1658eb60ff1eSJason A. Harmening 			break;
1659eb60ff1eSJason A. Harmening 		else if ((cn.cn_flags & ISWHITEOUT) == 0) {
1660d00947d8SCraig Rodrigues 			error = ENOTEMPTY;
1661eb60ff1eSJason A. Harmening 			break;
1662eb60ff1eSJason A. Harmening 		} else
1663eb60ff1eSJason A. Harmening 			error = 0;
1664996c772fSJohn Dyson 	}
1665996c772fSJohn Dyson 
1666eb60ff1eSJason A. Harmening 	VOP_CLOSE(lvp, FREAD, cred, td);
1667eb60ff1eSJason A. Harmening 	free(dirbuf, M_TEMP);
1668d00947d8SCraig Rodrigues 	return (error);
1669d00947d8SCraig Rodrigues }
1670