xref: /freebsd/sys/fs/unionfs/union_subr.c (revision dce5f3abed7181cc533ca5ed3de44517775e78dd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994 Jan-Simon Pendry
5  * Copyright (c) 1994
6  *	The Regents of the University of California.  All rights reserved.
7  * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
8  * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
38  * $FreeBSD$
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/vnode.h>
52 #include <sys/dirent.h>
53 #include <sys/fcntl.h>
54 #include <sys/filedesc.h>
55 #include <sys/stat.h>
56 #include <sys/sysctl.h>
57 #include <sys/taskqueue.h>
58 #include <sys/resourcevar.h>
59 
60 #include <security/mac/mac_framework.h>
61 
62 #include <vm/uma.h>
63 
64 #include <fs/unionfs/union.h>
65 
66 #define NUNIONFSNODECACHE 16
67 #define UNIONFSHASHMASK (NUNIONFSNODECACHE - 1)
68 
69 static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table");
70 MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part");
71 MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
72 
73 static struct task unionfs_deferred_rele_task;
74 static struct mtx unionfs_deferred_rele_lock;
75 static STAILQ_HEAD(, unionfs_node) unionfs_deferred_rele_list =
76     STAILQ_HEAD_INITIALIZER(unionfs_deferred_rele_list);
77 static TASKQUEUE_DEFINE_THREAD(unionfs_rele);
78 
79 unsigned int unionfs_ndeferred = 0;
80 SYSCTL_UINT(_vfs, OID_AUTO, unionfs_ndeferred, CTLFLAG_RD,
81     &unionfs_ndeferred, 0, "unionfs deferred vnode release");
82 
83 static void unionfs_deferred_rele(void *, int);
84 
85 /*
86  * Initialize
87  */
88 int
89 unionfs_init(struct vfsconf *vfsp)
90 {
91 	UNIONFSDEBUG("unionfs_init\n");	/* printed during system boot */
92 	TASK_INIT(&unionfs_deferred_rele_task, 0, unionfs_deferred_rele, NULL);
93 	mtx_init(&unionfs_deferred_rele_lock, "uniondefr", NULL, MTX_DEF);
94 	return (0);
95 }
96 
97 /*
98  * Uninitialize
99  */
100 int
101 unionfs_uninit(struct vfsconf *vfsp)
102 {
103 	taskqueue_quiesce(taskqueue_unionfs_rele);
104 	taskqueue_free(taskqueue_unionfs_rele);
105 	mtx_destroy(&unionfs_deferred_rele_lock);
106 	return (0);
107 }
108 
109 static void
110 unionfs_deferred_rele(void *arg __unused, int pending __unused)
111 {
112 	STAILQ_HEAD(, unionfs_node) local_rele_list;
113 	struct unionfs_node *unp, *tunp;
114 	unsigned int ndeferred;
115 
116 	ndeferred = 0;
117 	STAILQ_INIT(&local_rele_list);
118 	mtx_lock(&unionfs_deferred_rele_lock);
119 	STAILQ_CONCAT(&local_rele_list, &unionfs_deferred_rele_list);
120 	mtx_unlock(&unionfs_deferred_rele_lock);
121 	STAILQ_FOREACH_SAFE(unp, &local_rele_list, un_rele, tunp) {
122 		++ndeferred;
123 		MPASS(unp->un_dvp != NULL);
124 		vrele(unp->un_dvp);
125 		free(unp, M_UNIONFSNODE);
126 	}
127 
128 	/* We expect this function to be single-threaded, thus no atomic */
129 	unionfs_ndeferred += ndeferred;
130 }
131 
132 static struct unionfs_node_hashhead *
133 unionfs_get_hashhead(struct vnode *dvp, struct vnode *lookup)
134 {
135 	struct unionfs_node *unp;
136 
137 	unp = VTOUNIONFS(dvp);
138 
139 	return (&(unp->un_hashtbl[vfs_hash_index(lookup) & UNIONFSHASHMASK]));
140 }
141 
142 /*
143  * Attempt to lookup a cached unionfs vnode by upper/lower vp
144  * from dvp, with dvp's interlock held.
145  */
146 static struct vnode *
147 unionfs_get_cached_vnode_locked(struct vnode *lookup, struct vnode *dvp)
148 {
149 	struct unionfs_node *unp;
150 	struct unionfs_node_hashhead *hd;
151 	struct vnode *vp;
152 
153 	hd = unionfs_get_hashhead(dvp, lookup);
154 
155 	LIST_FOREACH(unp, hd, un_hash) {
156 		if ((unp->un_uppervp == lookup) ||
157 		    (unp->un_lowervp == lookup)) {
158 			vp = UNIONFSTOV(unp);
159 			VI_LOCK_FLAGS(vp, MTX_DUPOK);
160 			vp->v_iflag &= ~VI_OWEINACT;
161 			if (VN_IS_DOOMED(vp) ||
162 			    ((vp->v_iflag & VI_DOINGINACT) != 0)) {
163 				VI_UNLOCK(vp);
164 				vp = NULLVP;
165 			} else {
166 				vrefl(vp);
167 				VI_UNLOCK(vp);
168 			}
169 			return (vp);
170 		}
171 	}
172 
173 	return (NULLVP);
174 }
175 
176 
177 /*
178  * Get the cached vnode.
179  */
180 static struct vnode *
181 unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp,
182     struct vnode *dvp)
183 {
184 	struct vnode *vp;
185 
186 	KASSERT((uvp == NULLVP || uvp->v_type == VDIR),
187 	    ("unionfs_get_cached_vnode: v_type != VDIR"));
188 	KASSERT((lvp == NULLVP || lvp->v_type == VDIR),
189 	    ("unionfs_get_cached_vnode: v_type != VDIR"));
190 
191 	vp = NULLVP;
192 	VI_LOCK(dvp);
193 	if (uvp != NULLVP)
194 		vp = unionfs_get_cached_vnode_locked(uvp, dvp);
195 	else if (lvp != NULLVP)
196 		vp = unionfs_get_cached_vnode_locked(lvp, dvp);
197 	VI_UNLOCK(dvp);
198 
199 	return (vp);
200 }
201 
202 /*
203  * Add the new vnode into cache.
204  */
205 static struct vnode *
206 unionfs_ins_cached_vnode(struct unionfs_node *uncp,
207     struct vnode *dvp)
208 {
209 	struct unionfs_node_hashhead *hd;
210 	struct vnode *vp;
211 
212 	KASSERT((uncp->un_uppervp==NULLVP || uncp->un_uppervp->v_type==VDIR),
213 	    ("unionfs_ins_cached_vnode: v_type != VDIR"));
214 	KASSERT((uncp->un_lowervp==NULLVP || uncp->un_lowervp->v_type==VDIR),
215 	    ("unionfs_ins_cached_vnode: v_type != VDIR"));
216 
217 	vp = NULLVP;
218 	VI_LOCK(dvp);
219 	if (uncp->un_uppervp != NULL)
220 		vp = unionfs_get_cached_vnode_locked(uncp->un_uppervp, dvp);
221 	else if (uncp->un_lowervp != NULL)
222 		vp = unionfs_get_cached_vnode_locked(uncp->un_lowervp, dvp);
223 	if (vp == NULLVP) {
224 		hd = unionfs_get_hashhead(dvp, (uncp->un_uppervp != NULLVP ?
225 		    uncp->un_uppervp : uncp->un_lowervp));
226 		LIST_INSERT_HEAD(hd, uncp, un_hash);
227 	}
228 	VI_UNLOCK(dvp);
229 
230 	return (vp);
231 }
232 
233 /*
234  * Remove the vnode.
235  */
236 static void
237 unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp)
238 {
239 	KASSERT((unp != NULL), ("unionfs_rem_cached_vnode: null node"));
240 	KASSERT((dvp != NULLVP),
241 	    ("unionfs_rem_cached_vnode: null parent vnode"));
242 
243 	VI_LOCK(dvp);
244 	if (unp->un_hash.le_prev != NULL) {
245 		LIST_REMOVE(unp, un_hash);
246 		unp->un_hash.le_next = NULL;
247 		unp->un_hash.le_prev = NULL;
248 	}
249 	VI_UNLOCK(dvp);
250 }
251 
252 /*
253  * Common cleanup handling for unionfs_nodeget
254  * Upper, lower, and parent directory vnodes are expected to be referenced by
255  * the caller.  Upper and lower vnodes, if non-NULL, are also expected to be
256  * exclusively locked by the caller.
257  * This function will return with the caller's locks and references undone.
258  */
259 static void
260 unionfs_nodeget_cleanup(struct vnode *vp, void *arg)
261 {
262 	struct unionfs_node *unp;
263 
264 	/*
265 	 * Lock and reset the default vnode lock; vgone() expects a locked
266 	 * vnode, and we're going to reset the vnode ops.
267 	 */
268 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
269 
270 	/*
271 	 * Clear out private data and reset the vnode ops to avoid use of
272 	 * unionfs vnode ops on a partially constructed vnode.
273 	 */
274 	VI_LOCK(vp);
275 	vp->v_data = NULL;
276 	vp->v_vnlock = &vp->v_lock;
277 	vp->v_op = &dead_vnodeops;
278 	VI_UNLOCK(vp);
279 	vgone(vp);
280 	vput(vp);
281 
282 	unp = arg;
283 	if (unp->un_dvp != NULLVP)
284 		vrele(unp->un_dvp);
285 	if (unp->un_uppervp != NULLVP)
286 		vput(unp->un_uppervp);
287 	if (unp->un_lowervp != NULLVP)
288 		vput(unp->un_lowervp);
289 	if (unp->un_hashtbl != NULL)
290 		hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
291 	free(unp->un_path, M_UNIONFSPATH);
292 	free(unp, M_UNIONFSNODE);
293 }
294 
295 /*
296  * Make a new or get existing unionfs node.
297  *
298  * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
299  * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
300  * you should not lock plurality simultaneously.
301  */
302 int
303 unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
304     struct vnode *lowervp, struct vnode *dvp, struct vnode **vpp,
305     struct componentname *cnp, struct thread *td)
306 {
307 	char	       *path;
308 	struct unionfs_mount *ump;
309 	struct unionfs_node *unp;
310 	struct vnode   *vp;
311 	u_long		hashmask;
312 	int		error;
313 	int		lkflags;
314 	enum vtype	vt;
315 
316 	error = 0;
317 	ump = MOUNTTOUNIONFSMOUNT(mp);
318 	lkflags = (cnp ? cnp->cn_lkflags : 0);
319 	path = (cnp ? cnp->cn_nameptr : NULL);
320 	*vpp = NULLVP;
321 
322 	if (uppervp == NULLVP && lowervp == NULLVP)
323 		panic("unionfs_nodeget: upper and lower is null");
324 
325 	vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
326 
327 	/* If it has no ISLASTCN flag, path check is skipped. */
328 	if (cnp && !(cnp->cn_flags & ISLASTCN))
329 		path = NULL;
330 
331 	/* check the cache */
332 	if (dvp != NULLVP && vt == VDIR) {
333 		vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp);
334 		if (vp != NULLVP) {
335 			*vpp = vp;
336 			goto unionfs_nodeget_out;
337 		}
338 	}
339 
340 	if ((uppervp == NULLVP || ump->um_uppervp != uppervp) ||
341 	    (lowervp == NULLVP || ump->um_lowervp != lowervp)) {
342 		/* dvp will be NULLVP only in case of root vnode. */
343 		if (dvp == NULLVP)
344 			return (EINVAL);
345 	}
346 	unp = malloc(sizeof(struct unionfs_node),
347 	    M_UNIONFSNODE, M_WAITOK | M_ZERO);
348 
349 	error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp);
350 	if (error != 0) {
351 		free(unp, M_UNIONFSNODE);
352 		return (error);
353 	}
354 	if (dvp != NULLVP)
355 		vref(dvp);
356 	if (uppervp != NULLVP)
357 		vref(uppervp);
358 	if (lowervp != NULLVP)
359 		vref(lowervp);
360 
361 	if (vt == VDIR) {
362 		unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH,
363 		    &hashmask);
364 		KASSERT(hashmask == UNIONFSHASHMASK,
365 		    ("unexpected unionfs hash mask 0x%lx", hashmask));
366 	}
367 
368 	unp->un_vnode = vp;
369 	unp->un_uppervp = uppervp;
370 	unp->un_lowervp = lowervp;
371 	unp->un_dvp = dvp;
372 	if (uppervp != NULLVP)
373 		vp->v_vnlock = uppervp->v_vnlock;
374 	else
375 		vp->v_vnlock = lowervp->v_vnlock;
376 
377 	if (path != NULL) {
378 		unp->un_path = malloc(cnp->cn_namelen + 1,
379 		    M_UNIONFSPATH, M_WAITOK | M_ZERO);
380 		bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen);
381 		unp->un_path[cnp->cn_namelen] = '\0';
382 		unp->un_pathlen = cnp->cn_namelen;
383 	}
384 	vp->v_type = vt;
385 	vp->v_data = unp;
386 
387 	if ((uppervp != NULLVP && ump->um_uppervp == uppervp) &&
388 	    (lowervp != NULLVP && ump->um_lowervp == lowervp))
389 		vp->v_vflag |= VV_ROOT;
390 
391 	vn_lock_pair(lowervp, false, uppervp, false);
392 	error = insmntque1(vp, mp, unionfs_nodeget_cleanup, unp);
393 	if (error != 0)
394 		return (error);
395 	if (lowervp != NULL && VN_IS_DOOMED(lowervp)) {
396 		vput(lowervp);
397 		unp->un_lowervp = NULL;
398 	}
399 	if (uppervp != NULL && VN_IS_DOOMED(uppervp)) {
400 		vput(uppervp);
401 		unp->un_uppervp = NULL;
402 	}
403 	if (unp->un_lowervp == NULL && unp->un_uppervp == NULL) {
404 		unionfs_nodeget_cleanup(vp, unp);
405 		return (ENOENT);
406 	}
407 
408 	if (dvp != NULLVP && vt == VDIR)
409 		*vpp = unionfs_ins_cached_vnode(unp, dvp);
410 	if (*vpp != NULLVP) {
411 		unionfs_nodeget_cleanup(vp, unp);
412 		vp = *vpp;
413 	} else {
414 		if (uppervp != NULL)
415 			VOP_UNLOCK(uppervp);
416 		if (lowervp != NULL)
417 			VOP_UNLOCK(lowervp);
418 		*vpp = vp;
419 	}
420 
421 unionfs_nodeget_out:
422 	if (lkflags & LK_TYPE_MASK)
423 		vn_lock(vp, lkflags | LK_RETRY);
424 
425 	return (0);
426 }
427 
428 /*
429  * Clean up the unionfs node.
430  */
431 void
432 unionfs_noderem(struct vnode *vp, struct thread *td)
433 {
434 	struct unionfs_node *unp, *unp_t1, *unp_t2;
435 	struct unionfs_node_hashhead *hd;
436 	struct unionfs_node_status *unsp, *unsp_tmp;
437 	struct vnode   *lvp;
438 	struct vnode   *uvp;
439 	struct vnode   *dvp;
440 	int		count;
441 
442 	/*
443 	 * Use the interlock to protect the clearing of v_data to
444 	 * prevent faults in unionfs_lock().
445 	 */
446 	VI_LOCK(vp);
447 	unp = VTOUNIONFS(vp);
448 	lvp = unp->un_lowervp;
449 	uvp = unp->un_uppervp;
450 	dvp = unp->un_dvp;
451 	unp->un_lowervp = unp->un_uppervp = NULLVP;
452 	vp->v_vnlock = &(vp->v_lock);
453 	vp->v_data = NULL;
454 	vp->v_object = NULL;
455 	if (vp->v_writecount > 0) {
456 		if (uvp != NULL)
457 			VOP_ADD_WRITECOUNT(uvp, -vp->v_writecount);
458 		else if (lvp != NULL)
459 			VOP_ADD_WRITECOUNT(lvp, -vp->v_writecount);
460 	} else if (vp->v_writecount < 0)
461 		vp->v_writecount = 0;
462 	VI_UNLOCK(vp);
463 
464 	if (lvp != NULLVP)
465 		VOP_UNLOCK(lvp);
466 	if (uvp != NULLVP)
467 		VOP_UNLOCK(uvp);
468 
469 	if (dvp != NULLVP)
470 		unionfs_rem_cached_vnode(unp, dvp);
471 
472 	if (lockmgr(vp->v_vnlock, LK_EXCLUSIVE, VI_MTX(vp)) != 0)
473 		panic("the lock for deletion is unacquirable.");
474 
475 	if (lvp != NULLVP)
476 		vrele(lvp);
477 	if (uvp != NULLVP)
478 		vrele(uvp);
479 	if (unp->un_path != NULL) {
480 		free(unp->un_path, M_UNIONFSPATH);
481 		unp->un_path = NULL;
482 		unp->un_pathlen = 0;
483 	}
484 
485 	if (unp->un_hashtbl != NULL) {
486 		for (count = 0; count <= UNIONFSHASHMASK; count++) {
487 			hd = unp->un_hashtbl + count;
488 			LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) {
489 				LIST_REMOVE(unp_t1, un_hash);
490 				unp_t1->un_hash.le_next = NULL;
491 				unp_t1->un_hash.le_prev = NULL;
492 			}
493 		}
494 		hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
495 	}
496 
497 	LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) {
498 		LIST_REMOVE(unsp, uns_list);
499 		free(unsp, M_TEMP);
500 	}
501 	if (dvp != NULLVP) {
502 		mtx_lock(&unionfs_deferred_rele_lock);
503 		STAILQ_INSERT_TAIL(&unionfs_deferred_rele_list, unp, un_rele);
504 		mtx_unlock(&unionfs_deferred_rele_lock);
505 		taskqueue_enqueue(taskqueue_unionfs_rele,
506 		    &unionfs_deferred_rele_task);
507 	} else
508 		free(unp, M_UNIONFSNODE);
509 }
510 
511 /*
512  * Get the unionfs node status.
513  * You need exclusive lock this vnode.
514  */
515 void
516 unionfs_get_node_status(struct unionfs_node *unp, struct thread *td,
517     struct unionfs_node_status **unspp)
518 {
519 	struct unionfs_node_status *unsp;
520 	pid_t pid;
521 
522 	pid = td->td_proc->p_pid;
523 
524 	KASSERT(NULL != unspp, ("null pointer"));
525 	ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status");
526 
527 	LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
528 		if (unsp->uns_pid == pid) {
529 			*unspp = unsp;
530 			return;
531 		}
532 	}
533 
534 	/* create a new unionfs node status */
535 	unsp = malloc(sizeof(struct unionfs_node_status),
536 	    M_TEMP, M_WAITOK | M_ZERO);
537 
538 	unsp->uns_pid = pid;
539 	LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
540 
541 	*unspp = unsp;
542 }
543 
544 /*
545  * Remove the unionfs node status, if you can.
546  * You need exclusive lock this vnode.
547  */
548 void
549 unionfs_tryrem_node_status(struct unionfs_node *unp,
550     struct unionfs_node_status *unsp)
551 {
552 	KASSERT(NULL != unsp, ("null pointer"));
553 	ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status");
554 
555 	if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
556 		return;
557 
558 	LIST_REMOVE(unsp, uns_list);
559 	free(unsp, M_TEMP);
560 }
561 
562 /*
563  * Create upper node attr.
564  */
565 void
566 unionfs_create_uppervattr_core(struct unionfs_mount *ump, struct vattr *lva,
567     struct vattr *uva, struct thread *td)
568 {
569 	VATTR_NULL(uva);
570 	uva->va_type = lva->va_type;
571 	uva->va_atime = lva->va_atime;
572 	uva->va_mtime = lva->va_mtime;
573 	uva->va_ctime = lva->va_ctime;
574 
575 	switch (ump->um_copymode) {
576 	case UNIONFS_TRANSPARENT:
577 		uva->va_mode = lva->va_mode;
578 		uva->va_uid = lva->va_uid;
579 		uva->va_gid = lva->va_gid;
580 		break;
581 	case UNIONFS_MASQUERADE:
582 		if (ump->um_uid == lva->va_uid) {
583 			uva->va_mode = lva->va_mode & 077077;
584 			uva->va_mode |= (lva->va_type == VDIR ?
585 			    ump->um_udir : ump->um_ufile) & 0700;
586 			uva->va_uid = lva->va_uid;
587 			uva->va_gid = lva->va_gid;
588 		} else {
589 			uva->va_mode = (lva->va_type == VDIR ?
590 			    ump->um_udir : ump->um_ufile);
591 			uva->va_uid = ump->um_uid;
592 			uva->va_gid = ump->um_gid;
593 		}
594 		break;
595 	default:		/* UNIONFS_TRADITIONAL */
596 		uva->va_mode = 0777 & ~td->td_proc->p_pd->pd_cmask;
597 		uva->va_uid = ump->um_uid;
598 		uva->va_gid = ump->um_gid;
599 		break;
600 	}
601 }
602 
603 /*
604  * Create upper node attr.
605  */
606 int
607 unionfs_create_uppervattr(struct unionfs_mount *ump, struct vnode *lvp,
608     struct vattr *uva, struct ucred *cred, struct thread *td)
609 {
610 	struct vattr	lva;
611 	int		error;
612 
613 	if ((error = VOP_GETATTR(lvp, &lva, cred)))
614 		return (error);
615 
616 	unionfs_create_uppervattr_core(ump, &lva, uva, td);
617 
618 	return (error);
619 }
620 
621 /*
622  * relookup
623  *
624  * dvp should be locked on entry and will be locked on return.
625  *
626  * If an error is returned, *vpp will be invalid, otherwise it will hold a
627  * locked, referenced vnode. If *vpp == dvp then remember that only one
628  * LK_EXCLUSIVE lock is held.
629  */
630 int
631 unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
632     struct componentname *cnp, struct componentname *cn, struct thread *td,
633     char *path, int pathlen, u_long nameiop)
634 {
635 	int error;
636 
637 	cn->cn_namelen = pathlen;
638 	cn->cn_pnbuf = path;
639 	cn->cn_nameiop = nameiop;
640 	cn->cn_flags = (LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | ISLASTCN);
641 	cn->cn_lkflags = LK_EXCLUSIVE;
642 	cn->cn_cred = cnp->cn_cred;
643 	cn->cn_nameptr = cn->cn_pnbuf;
644 
645 	if (nameiop == DELETE)
646 		cn->cn_flags |= (cnp->cn_flags & (DOWHITEOUT | SAVESTART));
647 	else if (RENAME == nameiop)
648 		cn->cn_flags |= (cnp->cn_flags & SAVESTART);
649 	else if (nameiop == CREATE)
650 		cn->cn_flags |= NOCACHE;
651 
652 	vref(dvp);
653 	VOP_UNLOCK(dvp);
654 
655 	if ((error = relookup(dvp, vpp, cn))) {
656 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
657 	} else
658 		vrele(dvp);
659 
660 	KASSERT((cn->cn_flags & HASBUF) != 0,
661 	    ("%s: HASBUF cleared", __func__));
662 	KASSERT((cn->cn_flags & SAVENAME) != 0,
663 	    ("%s: SAVENAME cleared", __func__));
664 	KASSERT(cn->cn_pnbuf == path, ("%s: cn_pnbuf changed", __func__));
665 
666 	return (error);
667 }
668 
669 /*
670  * relookup for CREATE namei operation.
671  *
672  * dvp is unionfs vnode. dvp should be locked.
673  *
674  * If it called 'unionfs_copyfile' function by unionfs_link etc,
675  * VOP_LOOKUP information is broken.
676  * So it need relookup in order to create link etc.
677  */
678 int
679 unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp,
680     struct thread *td)
681 {
682 	struct vnode *udvp;
683 	struct vnode *vp;
684 	struct componentname cn;
685 	int error;
686 
687 	udvp = UNIONFSVPTOUPPERVP(dvp);
688 	vp = NULLVP;
689 
690 	KASSERT((cnp->cn_flags & HASBUF) != 0,
691 	    ("%s called without HASBUF", __func__));
692 	error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
693 	    cnp->cn_namelen, CREATE);
694 	if (error)
695 		return (error);
696 
697 	if (vp != NULLVP) {
698 		if (udvp == vp)
699 			vrele(vp);
700 		else
701 			vput(vp);
702 
703 		error = EEXIST;
704 	}
705 
706 	return (error);
707 }
708 
709 /*
710  * relookup for DELETE namei operation.
711  *
712  * dvp is unionfs vnode. dvp should be locked.
713  */
714 int
715 unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp,
716     struct thread *td)
717 {
718 	struct vnode *udvp;
719 	struct vnode *vp;
720 	struct componentname cn;
721 	int error;
722 
723 	udvp = UNIONFSVPTOUPPERVP(dvp);
724 	vp = NULLVP;
725 
726 	KASSERT((cnp->cn_flags & HASBUF) != 0,
727 	    ("%s called without HASBUF", __func__));
728 	error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
729 	    cnp->cn_namelen, DELETE);
730 	if (error)
731 		return (error);
732 
733 	if (vp == NULLVP)
734 		error = ENOENT;
735 	else {
736 		if (udvp == vp)
737 			vrele(vp);
738 		else
739 			vput(vp);
740 	}
741 
742 	return (error);
743 }
744 
745 /*
746  * relookup for RENAME namei operation.
747  *
748  * dvp is unionfs vnode. dvp should be locked.
749  */
750 int
751 unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp,
752     struct thread *td)
753 {
754 	struct vnode *udvp;
755 	struct vnode *vp;
756 	struct componentname cn;
757 	int error;
758 
759 	udvp = UNIONFSVPTOUPPERVP(dvp);
760 	vp = NULLVP;
761 
762 	KASSERT((cnp->cn_flags & HASBUF) != 0,
763 	    ("%s called without HASBUF", __func__));
764 	error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
765 	    cnp->cn_namelen, RENAME);
766 	if (error)
767 		return (error);
768 
769 	if (vp != NULLVP) {
770 		if (udvp == vp)
771 			vrele(vp);
772 		else
773 			vput(vp);
774 	}
775 
776 	return (error);
777 }
778 
779 /*
780  * Update the unionfs_node.
781  *
782  * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
783  * uvp's lock and lower's lock will be unlocked.
784  */
785 static void
786 unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp,
787     struct thread *td)
788 {
789 	struct unionfs_node_hashhead *hd;
790 	struct vnode   *vp;
791 	struct vnode   *lvp;
792 	struct vnode   *dvp;
793 	unsigned	count, lockrec;
794 
795 	vp = UNIONFSTOV(unp);
796 	lvp = unp->un_lowervp;
797 	ASSERT_VOP_ELOCKED(lvp, "unionfs_node_update");
798 	dvp = unp->un_dvp;
799 
800 	/*
801 	 * lock update
802 	 */
803 	VI_LOCK(vp);
804 	unp->un_uppervp = uvp;
805 	vp->v_vnlock = uvp->v_vnlock;
806 	VI_UNLOCK(vp);
807 	lockrec = lvp->v_vnlock->lk_recurse;
808 	for (count = 0; count < lockrec; count++)
809 		vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
810 
811 	/*
812 	 * Re-cache the unionfs vnode against the upper vnode
813 	 */
814 	if (dvp != NULLVP && vp->v_type == VDIR) {
815 		VI_LOCK(dvp);
816 		if (unp->un_hash.le_prev != NULL) {
817 			LIST_REMOVE(unp, un_hash);
818 			hd = unionfs_get_hashhead(dvp, uvp);
819 			LIST_INSERT_HEAD(hd, unp, un_hash);
820 		}
821 		VI_UNLOCK(unp->un_dvp);
822 	}
823 }
824 
825 /*
826  * Create a new shadow dir.
827  *
828  * udvp should be locked on entry and will be locked on return.
829  *
830  * If no error returned, unp will be updated.
831  */
832 int
833 unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
834     struct unionfs_node *unp, struct componentname *cnp, struct thread *td)
835 {
836 	struct vnode   *lvp;
837 	struct vnode   *uvp;
838 	struct vattr	va;
839 	struct vattr	lva;
840 	struct nameidata nd;
841 	struct mount   *mp;
842 	struct ucred   *cred;
843 	struct ucred   *credbk;
844 	struct uidinfo *rootinfo;
845 	int		error;
846 
847 	if (unp->un_uppervp != NULLVP)
848 		return (EEXIST);
849 
850 	lvp = unp->un_lowervp;
851 	uvp = NULLVP;
852 	credbk = cnp->cn_cred;
853 
854 	/* Authority change to root */
855 	rootinfo = uifind((uid_t)0);
856 	cred = crdup(cnp->cn_cred);
857 	/*
858 	 * The calls to chgproccnt() are needed to compensate for change_ruid()
859 	 * calling chgproccnt().
860 	 */
861 	chgproccnt(cred->cr_ruidinfo, 1, 0);
862 	change_euid(cred, rootinfo);
863 	change_ruid(cred, rootinfo);
864 	change_svuid(cred, (uid_t)0);
865 	uifree(rootinfo);
866 	cnp->cn_cred = cred;
867 
868 	memset(&nd.ni_cnd, 0, sizeof(struct componentname));
869 	NDPREINIT(&nd);
870 
871 	if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
872 		goto unionfs_mkshadowdir_abort;
873 
874 	if ((error = unionfs_relookup(udvp, &uvp, cnp, &nd.ni_cnd, td,
875 	    cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
876 		goto unionfs_mkshadowdir_abort;
877 	if (uvp != NULLVP) {
878 		if (udvp == uvp)
879 			vrele(uvp);
880 		else
881 			vput(uvp);
882 
883 		error = EEXIST;
884 		goto unionfs_mkshadowdir_abort;
885 	}
886 
887 	if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)))
888 		goto unionfs_mkshadowdir_abort;
889 	unionfs_create_uppervattr_core(ump, &lva, &va, td);
890 
891 	error = VOP_MKDIR(udvp, &uvp, &nd.ni_cnd, &va);
892 
893 	if (!error) {
894 		unionfs_node_update(unp, uvp, td);
895 
896 		/*
897 		 * XXX The bug which cannot set uid/gid was corrected.
898 		 * Ignore errors.
899 		 */
900 		va.va_type = VNON;
901 		VOP_SETATTR(uvp, &va, nd.ni_cnd.cn_cred);
902 	}
903 	vn_finished_write(mp);
904 
905 unionfs_mkshadowdir_abort:
906 	cnp->cn_cred = credbk;
907 	chgproccnt(cred->cr_ruidinfo, -1, 0);
908 	crfree(cred);
909 
910 	return (error);
911 }
912 
913 /*
914  * Create a new whiteout.
915  *
916  * dvp should be locked on entry and will be locked on return.
917  */
918 int
919 unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp,
920     struct thread *td, char *path, int pathlen)
921 {
922 	struct vnode   *wvp;
923 	struct nameidata nd;
924 	struct mount   *mp;
925 	int		error;
926 
927 	wvp = NULLVP;
928 	NDPREINIT(&nd);
929 	if ((error = unionfs_relookup(dvp, &wvp, cnp, &nd.ni_cnd, td, path,
930 	    pathlen, CREATE))) {
931 		return (error);
932 	}
933 	if (wvp != NULLVP) {
934 		if (dvp == wvp)
935 			vrele(wvp);
936 		else
937 			vput(wvp);
938 
939 		return (EEXIST);
940 	}
941 
942 	if ((error = vn_start_write(dvp, &mp, V_WAIT | PCATCH)))
943 		goto unionfs_mkwhiteout_free_out;
944 	error = VOP_WHITEOUT(dvp, &nd.ni_cnd, CREATE);
945 
946 	vn_finished_write(mp);
947 
948 unionfs_mkwhiteout_free_out:
949 	return (error);
950 }
951 
952 /*
953  * Create a new vnode for create a new shadow file.
954  *
955  * If an error is returned, *vpp will be invalid, otherwise it will hold a
956  * locked, referenced and opened vnode.
957  *
958  * unp is never updated.
959  */
960 static int
961 unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
962     struct unionfs_node *unp, struct vattr *uvap, struct thread *td)
963 {
964 	struct unionfs_mount *ump;
965 	struct vnode   *vp;
966 	struct vnode   *lvp;
967 	struct ucred   *cred;
968 	struct vattr	lva;
969 	struct nameidata nd;
970 	int		fmode;
971 	int		error;
972 
973 	ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
974 	vp = NULLVP;
975 	lvp = unp->un_lowervp;
976 	cred = td->td_ucred;
977 	fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
978 	error = 0;
979 
980 	if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
981 		return (error);
982 	unionfs_create_uppervattr_core(ump, &lva, uvap, td);
983 
984 	if (unp->un_path == NULL)
985 		panic("unionfs: un_path is null");
986 
987 	nd.ni_cnd.cn_namelen = unp->un_pathlen;
988 	nd.ni_cnd.cn_pnbuf = unp->un_path;
989 	nd.ni_cnd.cn_nameiop = CREATE;
990 	nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME |
991 	    ISLASTCN;
992 	nd.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
993 	nd.ni_cnd.cn_cred = cred;
994 	nd.ni_cnd.cn_nameptr = nd.ni_cnd.cn_pnbuf;
995 	NDPREINIT(&nd);
996 
997 	vref(udvp);
998 	if ((error = relookup(udvp, &vp, &nd.ni_cnd)) != 0)
999 		goto unionfs_vn_create_on_upper_free_out2;
1000 	vrele(udvp);
1001 
1002 	if (vp != NULLVP) {
1003 		if (vp == udvp)
1004 			vrele(vp);
1005 		else
1006 			vput(vp);
1007 		error = EEXIST;
1008 		goto unionfs_vn_create_on_upper_free_out1;
1009 	}
1010 
1011 	if ((error = VOP_CREATE(udvp, &vp, &nd.ni_cnd, uvap)) != 0)
1012 		goto unionfs_vn_create_on_upper_free_out1;
1013 
1014 	if ((error = VOP_OPEN(vp, fmode, cred, td, NULL)) != 0) {
1015 		vput(vp);
1016 		goto unionfs_vn_create_on_upper_free_out1;
1017 	}
1018 	error = VOP_ADD_WRITECOUNT(vp, 1);
1019 	CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1020 	    __func__, vp, vp->v_writecount);
1021 	if (error == 0) {
1022 		*vpp = vp;
1023 	} else {
1024 		VOP_CLOSE(vp, fmode, cred, td);
1025 	}
1026 
1027 unionfs_vn_create_on_upper_free_out1:
1028 	VOP_UNLOCK(udvp);
1029 
1030 unionfs_vn_create_on_upper_free_out2:
1031 	KASSERT((nd.ni_cnd.cn_flags & HASBUF) != 0,
1032 	    ("%s: HASBUF cleared", __func__));
1033 	KASSERT((nd.ni_cnd.cn_flags & SAVENAME) != 0,
1034 	    ("%s: SAVENAME cleared", __func__));
1035 	KASSERT(nd.ni_cnd.cn_pnbuf == unp->un_path,
1036 	    ("%s: cn_pnbuf changed", __func__));
1037 
1038 	return (error);
1039 }
1040 
1041 /*
1042  * Copy from lvp to uvp.
1043  *
1044  * lvp and uvp should be locked and opened on entry and will be locked and
1045  * opened on return.
1046  */
1047 static int
1048 unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
1049     struct ucred *cred, struct thread *td)
1050 {
1051 	char           *buf;
1052 	struct uio	uio;
1053 	struct iovec	iov;
1054 	off_t		offset;
1055 	int		count;
1056 	int		error;
1057 	int		bufoffset;
1058 
1059 	error = 0;
1060 	memset(&uio, 0, sizeof(uio));
1061 
1062 	uio.uio_td = td;
1063 	uio.uio_segflg = UIO_SYSSPACE;
1064 	uio.uio_offset = 0;
1065 
1066 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
1067 
1068 	while (error == 0) {
1069 		offset = uio.uio_offset;
1070 
1071 		uio.uio_iov = &iov;
1072 		uio.uio_iovcnt = 1;
1073 		iov.iov_base = buf;
1074 		iov.iov_len = MAXBSIZE;
1075 		uio.uio_resid = iov.iov_len;
1076 		uio.uio_rw = UIO_READ;
1077 
1078 		if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
1079 			break;
1080 		if ((count = MAXBSIZE - uio.uio_resid) == 0)
1081 			break;
1082 
1083 		bufoffset = 0;
1084 		while (bufoffset < count) {
1085 			uio.uio_iov = &iov;
1086 			uio.uio_iovcnt = 1;
1087 			iov.iov_base = buf + bufoffset;
1088 			iov.iov_len = count - bufoffset;
1089 			uio.uio_offset = offset + bufoffset;
1090 			uio.uio_resid = iov.iov_len;
1091 			uio.uio_rw = UIO_WRITE;
1092 
1093 			if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
1094 				break;
1095 
1096 			bufoffset += (count - bufoffset) - uio.uio_resid;
1097 		}
1098 
1099 		uio.uio_offset = offset + bufoffset;
1100 	}
1101 
1102 	free(buf, M_TEMP);
1103 
1104 	return (error);
1105 }
1106 
1107 /*
1108  * Copy file from lower to upper.
1109  *
1110  * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
1111  * docopy.
1112  *
1113  * If no error returned, unp will be updated.
1114  */
1115 int
1116 unionfs_copyfile(struct unionfs_node *unp, int docopy, struct ucred *cred,
1117     struct thread *td)
1118 {
1119 	struct mount   *mp;
1120 	struct vnode   *udvp;
1121 	struct vnode   *lvp;
1122 	struct vnode   *uvp;
1123 	struct vattr	uva;
1124 	int		error;
1125 
1126 	lvp = unp->un_lowervp;
1127 	uvp = NULLVP;
1128 
1129 	if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
1130 		return (EROFS);
1131 	if (unp->un_dvp == NULLVP)
1132 		return (EINVAL);
1133 	if (unp->un_uppervp != NULLVP)
1134 		return (EEXIST);
1135 	udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
1136 	if (udvp == NULLVP)
1137 		return (EROFS);
1138 	if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
1139 		return (EROFS);
1140 
1141 	error = VOP_ACCESS(lvp, VREAD, cred, td);
1142 	if (error != 0)
1143 		return (error);
1144 
1145 	if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)) != 0)
1146 		return (error);
1147 	error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva, td);
1148 	if (error != 0) {
1149 		vn_finished_write(mp);
1150 		return (error);
1151 	}
1152 
1153 	if (docopy != 0) {
1154 		error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
1155 		if (error == 0) {
1156 			error = unionfs_copyfile_core(lvp, uvp, cred, td);
1157 			VOP_CLOSE(lvp, FREAD, cred, td);
1158 		}
1159 	}
1160 	VOP_CLOSE(uvp, FWRITE, cred, td);
1161 	VOP_ADD_WRITECOUNT_CHECKED(uvp, -1);
1162 	CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1163 	    __func__, uvp, uvp->v_writecount);
1164 
1165 	vn_finished_write(mp);
1166 
1167 	if (error == 0) {
1168 		/* Reset the attributes. Ignore errors. */
1169 		uva.va_type = VNON;
1170 		VOP_SETATTR(uvp, &uva, cred);
1171 	}
1172 
1173 	unionfs_node_update(unp, uvp, td);
1174 
1175 	return (error);
1176 }
1177 
1178 /*
1179  * It checks whether vp can rmdir. (check empty)
1180  *
1181  * vp is unionfs vnode.
1182  * vp should be locked.
1183  */
1184 int
1185 unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td)
1186 {
1187 	struct vnode   *uvp;
1188 	struct vnode   *lvp;
1189 	struct vnode   *tvp;
1190 	struct dirent  *dp;
1191 	struct dirent  *edp;
1192 	struct componentname cn;
1193 	struct iovec	iov;
1194 	struct uio	uio;
1195 	struct vattr	va;
1196 	int		error;
1197 	int		eofflag;
1198 	int		lookuperr;
1199 
1200 	/*
1201 	 * The size of buf needs to be larger than DIRBLKSIZ.
1202 	 */
1203 	char		buf[256 * 6];
1204 
1205 	ASSERT_VOP_ELOCKED(vp, "unionfs_check_rmdir");
1206 
1207 	eofflag = 0;
1208 	uvp = UNIONFSVPTOUPPERVP(vp);
1209 	lvp = UNIONFSVPTOLOWERVP(vp);
1210 
1211 	/* check opaque */
1212 	if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
1213 		return (error);
1214 	if (va.va_flags & OPAQUE)
1215 		return (0);
1216 
1217 	/* open vnode */
1218 #ifdef MAC
1219 	if ((error = mac_vnode_check_open(cred, vp, VEXEC|VREAD)) != 0)
1220 		return (error);
1221 #endif
1222 	if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred, td)) != 0)
1223 		return (error);
1224 	if ((error = VOP_OPEN(vp, FREAD, cred, td, NULL)) != 0)
1225 		return (error);
1226 
1227 	uio.uio_rw = UIO_READ;
1228 	uio.uio_segflg = UIO_SYSSPACE;
1229 	uio.uio_td = td;
1230 	uio.uio_offset = 0;
1231 
1232 #ifdef MAC
1233 	error = mac_vnode_check_readdir(td->td_ucred, lvp);
1234 #endif
1235 	while (!error && !eofflag) {
1236 		iov.iov_base = buf;
1237 		iov.iov_len = sizeof(buf);
1238 		uio.uio_iov = &iov;
1239 		uio.uio_iovcnt = 1;
1240 		uio.uio_resid = iov.iov_len;
1241 
1242 		error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
1243 		if (error != 0)
1244 			break;
1245 		if (eofflag == 0 && uio.uio_resid == sizeof(buf)) {
1246 #ifdef DIAGNOSTIC
1247 			panic("bad readdir response from lower FS.");
1248 #endif
1249 			break;
1250 		}
1251 
1252 		edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
1253 		for (dp = (struct dirent*)buf; !error && dp < edp;
1254 		     dp = (struct dirent*)((caddr_t)dp + dp->d_reclen)) {
1255 			if (dp->d_type == DT_WHT || dp->d_fileno == 0 ||
1256 			    (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1257 			    (dp->d_namlen == 2 && !bcmp(dp->d_name, "..", 2)))
1258 				continue;
1259 
1260 			cn.cn_namelen = dp->d_namlen;
1261 			cn.cn_pnbuf = NULL;
1262 			cn.cn_nameptr = dp->d_name;
1263 			cn.cn_nameiop = LOOKUP;
1264 			cn.cn_flags = LOCKPARENT | LOCKLEAF | SAVENAME |
1265 			    RDONLY | ISLASTCN;
1266 			cn.cn_lkflags = LK_EXCLUSIVE;
1267 			cn.cn_cred = cred;
1268 
1269 			/*
1270 			 * check entry in lower.
1271 			 * Sometimes, readdir function returns
1272 			 * wrong entry.
1273 			 */
1274 			lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
1275 
1276 			if (!lookuperr)
1277 				vput(tvp);
1278 			else
1279 				continue; /* skip entry */
1280 
1281 			/*
1282 			 * check entry
1283 			 * If it has no exist/whiteout entry in upper,
1284 			 * directory is not empty.
1285 			 */
1286 			cn.cn_flags = LOCKPARENT | LOCKLEAF | SAVENAME |
1287 			    RDONLY | ISLASTCN;
1288 			lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
1289 
1290 			if (!lookuperr)
1291 				vput(tvp);
1292 
1293 			/* ignore exist or whiteout entry */
1294 			if (!lookuperr ||
1295 			    (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
1296 				continue;
1297 
1298 			error = ENOTEMPTY;
1299 		}
1300 	}
1301 
1302 	/* close vnode */
1303 	VOP_CLOSE(vp, FREAD, cred, td);
1304 
1305 	return (error);
1306 }
1307 
1308 #ifdef DIAGNOSTIC
1309 
1310 struct vnode   *
1311 unionfs_checkuppervp(struct vnode *vp, char *fil, int lno)
1312 {
1313 	struct unionfs_node *unp;
1314 
1315 	unp = VTOUNIONFS(vp);
1316 
1317 #ifdef notyet
1318 	if (vp->v_op != unionfs_vnodeop_p) {
1319 		printf("unionfs_checkuppervp: on non-unionfs-node.\n");
1320 #ifdef KDB
1321 		kdb_enter(KDB_WHY_UNIONFS,
1322 		    "unionfs_checkuppervp: on non-unionfs-node.\n");
1323 #endif
1324 		panic("unionfs_checkuppervp");
1325 	}
1326 #endif
1327 	return (unp->un_uppervp);
1328 }
1329 
1330 struct vnode   *
1331 unionfs_checklowervp(struct vnode *vp, char *fil, int lno)
1332 {
1333 	struct unionfs_node *unp;
1334 
1335 	unp = VTOUNIONFS(vp);
1336 
1337 #ifdef notyet
1338 	if (vp->v_op != unionfs_vnodeop_p) {
1339 		printf("unionfs_checklowervp: on non-unionfs-node.\n");
1340 #ifdef KDB
1341 		kdb_enter(KDB_WHY_UNIONFS,
1342 		    "unionfs_checklowervp: on non-unionfs-node.\n");
1343 #endif
1344 		panic("unionfs_checklowervp");
1345 	}
1346 #endif
1347 	return (unp->un_lowervp);
1348 }
1349 #endif
1350