Home | History | Annotate | Line # | Download | only in union
union_subr.c revision 1.46
      1 /*	$NetBSD: union_subr.c,v 1.46 2011/08/10 15:56:01 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
     35  */
     36 
     37 /*
     38  * Copyright (c) 1994 Jan-Simon Pendry
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Jan-Simon Pendry.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.46 2011/08/10 15:56:01 hannken Exp $");
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/proc.h>
     80 #include <sys/time.h>
     81 #include <sys/kernel.h>
     82 #include <sys/vnode.h>
     83 #include <sys/namei.h>
     84 #include <sys/malloc.h>
     85 #include <sys/dirent.h>
     86 #include <sys/file.h>
     87 #include <sys/filedesc.h>
     88 #include <sys/queue.h>
     89 #include <sys/mount.h>
     90 #include <sys/stat.h>
     91 #include <sys/kauth.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <fs/union/union.h>
     96 
     97 /* must be power of two, otherwise change UNION_HASH() */
     98 #define NHASH 32
     99 
    100 /* unsigned int ... */
    101 #define UNION_HASH(u, l) \
    102 	(((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
    103 
    104 static LIST_HEAD(unhead, union_node) unhead[NHASH];
    105 static int unvplock[NHASH];
    106 
    107 static int union_list_lock(int);
    108 static void union_list_unlock(int);
    109 void union_updatevp(struct union_node *, struct vnode *, struct vnode *);
    110 static int union_relookup(struct union_mount *, struct vnode *,
    111 			       struct vnode **, struct componentname *,
    112 			       struct componentname *, char **,
    113 			       const char *, int);
    114 int union_vn_close(struct vnode *, int, kauth_cred_t, struct lwp *);
    115 static void union_dircache_r(struct vnode *, struct vnode ***, int *);
    116 struct vnode *union_dircache(struct vnode *, struct lwp *);
    117 
    118 void
    119 union_init(void)
    120 {
    121 	int i;
    122 
    123 	for (i = 0; i < NHASH; i++)
    124 		LIST_INIT(&unhead[i]);
    125 	memset(unvplock, 0, sizeof(unvplock));
    126 }
    127 
    128 /*
    129  * Free global unionfs resources.
    130  */
    131 void
    132 union_done(void)
    133 {
    134 
    135 	/* Make sure to unset the readdir hook. */
    136 	vn_union_readdir_hook = NULL;
    137 }
    138 
    139 static int
    140 union_list_lock(int ix)
    141 {
    142 
    143 	if (unvplock[ix] & UN_LOCKED) {
    144 		unvplock[ix] |= UN_WANTED;
    145 		(void) tsleep(&unvplock[ix], PINOD, "unionlk", 0);
    146 		return (1);
    147 	}
    148 
    149 	unvplock[ix] |= UN_LOCKED;
    150 
    151 	return (0);
    152 }
    153 
    154 static void
    155 union_list_unlock(int ix)
    156 {
    157 
    158 	unvplock[ix] &= ~UN_LOCKED;
    159 
    160 	if (unvplock[ix] & UN_WANTED) {
    161 		unvplock[ix] &= ~UN_WANTED;
    162 		wakeup(&unvplock[ix]);
    163 	}
    164 }
    165 
    166 void
    167 union_updatevp(struct union_node *un, struct vnode *uppervp,
    168 	struct vnode *lowervp)
    169 {
    170 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
    171 	int nhash = UNION_HASH(uppervp, lowervp);
    172 	int docache = (lowervp != NULLVP || uppervp != NULLVP);
    173 	int lhash, uhash;
    174 
    175 	/*
    176 	 * Ensure locking is ordered from lower to higher
    177 	 * to avoid deadlocks.
    178 	 */
    179 	if (nhash < ohash) {
    180 		lhash = nhash;
    181 		uhash = ohash;
    182 	} else {
    183 		lhash = ohash;
    184 		uhash = nhash;
    185 	}
    186 
    187 	if (lhash != uhash)
    188 		while (union_list_lock(lhash))
    189 			continue;
    190 
    191 	while (union_list_lock(uhash))
    192 		continue;
    193 
    194 	if (ohash != nhash || !docache) {
    195 		if (un->un_flags & UN_CACHED) {
    196 			un->un_flags &= ~UN_CACHED;
    197 			LIST_REMOVE(un, un_cache);
    198 		}
    199 	}
    200 
    201 	if (ohash != nhash)
    202 		union_list_unlock(ohash);
    203 
    204 	if (un->un_lowervp != lowervp) {
    205 		if (un->un_lowervp) {
    206 			vrele(un->un_lowervp);
    207 			if (un->un_path) {
    208 				free(un->un_path, M_TEMP);
    209 				un->un_path = 0;
    210 			}
    211 			if (un->un_dirvp) {
    212 				vrele(un->un_dirvp);
    213 				un->un_dirvp = NULLVP;
    214 			}
    215 		}
    216 		un->un_lowervp = lowervp;
    217 		un->un_lowersz = VNOVAL;
    218 	}
    219 
    220 	if (un->un_uppervp != uppervp) {
    221 		if (un->un_uppervp)
    222 			vrele(un->un_uppervp);
    223 
    224 		un->un_uppervp = uppervp;
    225 		un->un_uppersz = VNOVAL;
    226 		/* Update union vnode interlock. */
    227 		if (uppervp != NULL) {
    228 			mutex_obj_hold(uppervp->v_interlock);
    229 			uvm_obj_setlock(&UNIONTOV(un)->v_uobj,
    230 			    uppervp->v_interlock);
    231 		}
    232 	}
    233 
    234 	if (docache && (ohash != nhash)) {
    235 		LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
    236 		un->un_flags |= UN_CACHED;
    237 	}
    238 
    239 	union_list_unlock(nhash);
    240 }
    241 
    242 void
    243 union_newlower(struct union_node *un, struct vnode *lowervp)
    244 {
    245 
    246 	union_updatevp(un, un->un_uppervp, lowervp);
    247 }
    248 
    249 void
    250 union_newupper(struct union_node *un, struct vnode *uppervp)
    251 {
    252 
    253 	union_updatevp(un, uppervp, un->un_lowervp);
    254 }
    255 
    256 /*
    257  * Keep track of size changes in the underlying vnodes.
    258  * If the size changes, then callback to the vm layer
    259  * giving priority to the upper layer size.
    260  */
    261 void
    262 union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz)
    263 {
    264 	struct union_node *un;
    265 	off_t sz;
    266 
    267 	/* only interested in regular files */
    268 	if (vp->v_type != VREG) {
    269 		uvm_vnp_setsize(vp, 0);
    270 		return;
    271 	}
    272 
    273 	un = VTOUNION(vp);
    274 	sz = VNOVAL;
    275 
    276 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
    277 		un->un_uppersz = uppersz;
    278 		if (sz == VNOVAL)
    279 			sz = un->un_uppersz;
    280 	}
    281 
    282 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
    283 		un->un_lowersz = lowersz;
    284 		if (sz == VNOVAL)
    285 			sz = un->un_lowersz;
    286 	}
    287 
    288 	if (sz != VNOVAL) {
    289 #ifdef UNION_DIAGNOSTIC
    290 		printf("union: %s size now %qd\n",
    291 		    uppersz != VNOVAL ? "upper" : "lower", sz);
    292 #endif
    293 		uvm_vnp_setsize(vp, sz);
    294 	}
    295 }
    296 
    297 /*
    298  * allocate a union_node/vnode pair.  the vnode is
    299  * referenced and locked.  the new vnode is returned
    300  * via (vpp).  (mp) is the mountpoint of the union filesystem,
    301  * (dvp) is the parent directory where the upper layer object
    302  * should exist (but doesn't) and (cnp) is the componentname
    303  * information which is partially copied to allow the upper
    304  * layer object to be created at a later time.  (uppervp)
    305  * and (lowervp) reference the upper and lower layer objects
    306  * being mapped.  either, but not both, can be nil.
    307  * if supplied, (uppervp) is locked.
    308  * the reference is either maintained in the new union_node
    309  * object which is allocated, or they are vrele'd.
    310  *
    311  * all union_nodes are maintained on a singly-linked
    312  * list.  new nodes are only allocated when they cannot
    313  * be found on this list.  entries on the list are
    314  * removed when the vfs reclaim entry is called.
    315  *
    316  * a single lock is kept for the entire list.  this is
    317  * needed because the getnewvnode() function can block
    318  * waiting for a vnode to become free, in which case there
    319  * may be more than one process trying to get the same
    320  * vnode.  this lock is only taken if we are going to
    321  * call getnewvnode, since the kernel itself is single-threaded.
    322  *
    323  * if an entry is found on the list, then call vget() to
    324  * take a reference.  this is done because there may be
    325  * zero references to it and so it needs to removed from
    326  * the vnode free list.
    327  */
    328 int
    329 union_allocvp(
    330 	struct vnode **vpp,
    331 	struct mount *mp,
    332 	struct vnode *undvp,		/* parent union vnode */
    333 	struct vnode *dvp,		/* may be null */
    334 	struct componentname *cnp,	/* may be null */
    335 	struct vnode *uppervp,		/* may be null */
    336 	struct vnode *lowervp,		/* may be null */
    337 	int docache)
    338 {
    339 	int error;
    340 	struct vattr va;
    341 	struct union_node *un = NULL, *un1;
    342 	struct vnode *vp, *xlowervp = NULLVP;
    343 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    344 	voff_t uppersz, lowersz;
    345 	int hash = 0;
    346 	int vflag, iflag;
    347 	int try;
    348 
    349 	if (uppervp == NULLVP && lowervp == NULLVP)
    350 		panic("union: unidentifiable allocation");
    351 
    352 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
    353 		xlowervp = lowervp;
    354 		lowervp = NULLVP;
    355 	}
    356 
    357 	/* detect the root vnode (and aliases) */
    358 	iflag = VI_LAYER;
    359 	vflag = 0;
    360 	if ((uppervp == um->um_uppervp) &&
    361 	    ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
    362 		if (lowervp == NULLVP) {
    363 			lowervp = um->um_lowervp;
    364 			if (lowervp != NULLVP)
    365 				vref(lowervp);
    366 		}
    367 		iflag = 0;
    368 		vflag = VV_ROOT;
    369 	}
    370 
    371 loop:
    372 	if (!docache) {
    373 		un = 0;
    374 	} else for (try = 0; try < 3; try++) {
    375 		switch (try) {
    376 		case 0:
    377 			if (lowervp == NULLVP)
    378 				continue;
    379 			hash = UNION_HASH(uppervp, lowervp);
    380 			break;
    381 
    382 		case 1:
    383 			if (uppervp == NULLVP)
    384 				continue;
    385 			hash = UNION_HASH(uppervp, NULLVP);
    386 			break;
    387 
    388 		case 2:
    389 			if (lowervp == NULLVP)
    390 				continue;
    391 			hash = UNION_HASH(NULLVP, lowervp);
    392 			break;
    393 		}
    394 
    395 		while (union_list_lock(hash))
    396 			continue;
    397 
    398 		for (un = unhead[hash].lh_first; un != 0;
    399 					un = un->un_cache.le_next) {
    400 			if ((un->un_lowervp == lowervp ||
    401 			     un->un_lowervp == NULLVP) &&
    402 			    (un->un_uppervp == uppervp ||
    403 			     un->un_uppervp == NULLVP) &&
    404 			    (UNIONTOV(un)->v_mount == mp)) {
    405 				vp = UNIONTOV(un);
    406 				mutex_enter(vp->v_interlock);
    407 				if (vget(vp, 0)) {
    408 					union_list_unlock(hash);
    409 					goto loop;
    410 				}
    411 				break;
    412 			}
    413 		}
    414 
    415 		union_list_unlock(hash);
    416 
    417 		if (un)
    418 			break;
    419 	}
    420 
    421 	if (un) {
    422 		/*
    423 		 * Obtain a lock on the union_node.
    424 		 * uppervp is locked, though un->un_uppervp
    425 		 * may not be.  this doesn't break the locking
    426 		 * hierarchy since in the case that un->un_uppervp
    427 		 * is not yet locked it will be vrele'd and replaced
    428 		 * with uppervp.
    429 		 */
    430 
    431 		if ((dvp != NULLVP) && (uppervp == dvp)) {
    432 			/*
    433 			 * Access ``.'', so (un) will already
    434 			 * be locked.  Since this process has
    435 			 * the lock on (uppervp) no other
    436 			 * process can hold the lock on (un).
    437 			 */
    438 #ifdef DIAGNOSTIC
    439 			if ((un->un_flags & UN_LOCKED) == 0)
    440 				panic("union: . not locked");
    441 			else if (curproc && un->un_pid != curproc->p_pid &&
    442 				    un->un_pid > -1 && curproc->p_pid > -1)
    443 				panic("union: allocvp not lock owner");
    444 #endif
    445 		} else {
    446 			if (un->un_flags & UN_LOCKED) {
    447 				vrele(UNIONTOV(un));
    448 				un->un_flags |= UN_WANTED;
    449 				(void) tsleep(&un->un_flags, PINOD,
    450 				    "unionalloc", 0);
    451 				goto loop;
    452 			}
    453 			un->un_flags |= UN_LOCKED;
    454 
    455 #ifdef DIAGNOSTIC
    456 			if (curproc)
    457 				un->un_pid = curproc->p_pid;
    458 			else
    459 				un->un_pid = -1;
    460 #endif
    461 		}
    462 
    463 		/*
    464 		 * At this point, the union_node is locked,
    465 		 * un->un_uppervp may not be locked, and uppervp
    466 		 * is locked or nil.
    467 		 */
    468 
    469 		/*
    470 		 * Save information about the upper layer.
    471 		 */
    472 		if (uppervp != un->un_uppervp) {
    473 			union_newupper(un, uppervp);
    474 		} else if (uppervp) {
    475 			vrele(uppervp);
    476 		}
    477 
    478 		if (un->un_uppervp) {
    479 			un->un_flags |= UN_ULOCK;
    480 			un->un_flags &= ~UN_KLOCK;
    481 		}
    482 
    483 		/*
    484 		 * Save information about the lower layer.
    485 		 * This needs to keep track of pathname
    486 		 * and directory information which union_vn_create
    487 		 * might need.
    488 		 */
    489 		if (lowervp != un->un_lowervp) {
    490 			union_newlower(un, lowervp);
    491 			if (cnp && (lowervp != NULLVP)) {
    492 				un->un_hash = cnp->cn_hash;
    493 				un->un_path = malloc(cnp->cn_namelen+1,
    494 						M_TEMP, M_WAITOK);
    495 				memcpy(un->un_path, cnp->cn_nameptr,
    496 						cnp->cn_namelen);
    497 				un->un_path[cnp->cn_namelen] = '\0';
    498 				vref(dvp);
    499 				un->un_dirvp = dvp;
    500 			}
    501 		} else if (lowervp) {
    502 			vrele(lowervp);
    503 		}
    504 		*vpp = UNIONTOV(un);
    505 		return (0);
    506 	}
    507 
    508 	uppersz = lowersz = VNOVAL;
    509 	if (uppervp != NULLVP)
    510 		if (VOP_GETATTR(uppervp, &va, FSCRED) == 0)
    511 			uppersz = va.va_size;
    512 	if (lowervp != NULLVP)
    513 		if (VOP_GETATTR(lowervp, &va, FSCRED) == 0)
    514 			lowersz = va.va_size;
    515 	hash = UNION_HASH(uppervp, lowervp);
    516 
    517 	/*
    518 	 * Get a new vnode and share the lock with upper layer vnode,
    519 	 * unless layers are inverted.
    520 	 */
    521 	vnode_t *svp = (uppervp != NULLVP) ? uppervp : lowervp;
    522 	error = getnewvnode(VT_UNION, mp, union_vnodeop_p,
    523 	    svp->v_interlock, vpp);
    524 	if (error) {
    525 		if (uppervp) {
    526 			if (dvp == uppervp)
    527 				vrele(uppervp);
    528 			else
    529 				vput(uppervp);
    530 		}
    531 		if (lowervp)
    532 			vrele(lowervp);
    533 
    534 		goto out;
    535 	}
    536 
    537 	if (docache) {
    538 		while (union_list_lock(hash))
    539 			continue;
    540 		LIST_FOREACH(un1, &unhead[hash], un_cache) {
    541 			if (un1->un_lowervp == lowervp &&
    542 			    un1->un_uppervp == uppervp &&
    543 			    UNIONTOV(un1)->v_mount == mp) {
    544 				/*
    545 				 * Another thread beat us, push back freshly
    546 				 * allocated vnode and retry.
    547 				 */
    548 				union_list_unlock(hash);
    549 				ungetnewvnode(*vpp);
    550 				goto loop;
    551 			}
    552 		}
    553 	}
    554 
    555 	(*vpp)->v_data = malloc(sizeof(struct union_node), M_TEMP, M_WAITOK);
    556 
    557 	(*vpp)->v_vflag |= vflag;
    558 	(*vpp)->v_iflag |= iflag;
    559 	if (uppervp)
    560 		(*vpp)->v_type = uppervp->v_type;
    561 	else
    562 		(*vpp)->v_type = lowervp->v_type;
    563 	un = VTOUNION(*vpp);
    564 	un->un_vnode = *vpp;
    565 	un->un_uppervp = uppervp;
    566 	un->un_lowervp = lowervp;
    567 	un->un_pvp = undvp;
    568 	if (undvp != NULLVP)
    569 		vref(undvp);
    570 	un->un_dircache = 0;
    571 	un->un_openl = 0;
    572 	un->un_flags = UN_LOCKED;
    573 
    574 	un->un_uppersz = VNOVAL;
    575 	un->un_lowersz = VNOVAL;
    576 	union_newsize(*vpp, uppersz, lowersz);
    577 
    578 	if (un->un_uppervp)
    579 		un->un_flags |= UN_ULOCK;
    580 #ifdef DIAGNOSTIC
    581 	if (curproc)
    582 		un->un_pid = curproc->p_pid;
    583 	else
    584 		un->un_pid = -1;
    585 #endif
    586 	if (dvp && cnp && (lowervp != NULLVP)) {
    587 		un->un_hash = cnp->cn_hash;
    588 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
    589 		memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen);
    590 		un->un_path[cnp->cn_namelen] = '\0';
    591 		vref(dvp);
    592 		un->un_dirvp = dvp;
    593 	} else {
    594 		un->un_hash = 0;
    595 		un->un_path = 0;
    596 		un->un_dirvp = 0;
    597 	}
    598 
    599 	if (docache) {
    600 		LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
    601 		un->un_flags |= UN_CACHED;
    602 	}
    603 
    604 	if (xlowervp)
    605 		vrele(xlowervp);
    606 
    607 out:
    608 	if (docache)
    609 		union_list_unlock(hash);
    610 
    611 	return (error);
    612 }
    613 
    614 int
    615 union_freevp(struct vnode *vp)
    616 {
    617 	int hash;
    618 	struct union_node *un = VTOUNION(vp);
    619 
    620 	hash = UNION_HASH(un->un_uppervp, un->un_lowervp);
    621 
    622 	while (union_list_lock(hash))
    623 		continue;
    624 	if (un->un_flags & UN_CACHED) {
    625 		un->un_flags &= ~UN_CACHED;
    626 		LIST_REMOVE(un, un_cache);
    627 	}
    628 	union_list_unlock(hash);
    629 
    630 	if (un->un_pvp != NULLVP)
    631 		vrele(un->un_pvp);
    632 	if (un->un_uppervp != NULLVP)
    633 		vrele(un->un_uppervp);
    634 	if (un->un_lowervp != NULLVP)
    635 		vrele(un->un_lowervp);
    636 	if (un->un_dirvp != NULLVP)
    637 		vrele(un->un_dirvp);
    638 	if (un->un_path)
    639 		free(un->un_path, M_TEMP);
    640 
    641 	free(vp->v_data, M_TEMP);
    642 	vp->v_data = NULL;
    643 
    644 	return (0);
    645 }
    646 
    647 /*
    648  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
    649  * using a sequence of reads and writes.  both (fvp)
    650  * and (tvp) are locked on entry and exit.
    651  */
    652 int
    653 union_copyfile(struct vnode *fvp, struct vnode *tvp, kauth_cred_t cred,
    654 	struct lwp *l)
    655 {
    656 	char *tbuf;
    657 	struct uio uio;
    658 	struct iovec iov;
    659 	int error = 0;
    660 
    661 	/*
    662 	 * strategy:
    663 	 * allocate a buffer of size MAXBSIZE.
    664 	 * loop doing reads and writes, keeping track
    665 	 * of the current uio offset.
    666 	 * give up at the first sign of trouble.
    667 	 */
    668 
    669 	uio.uio_offset = 0;
    670 	UIO_SETUP_SYSSPACE(&uio);
    671 
    672 	VOP_UNLOCK(fvp);			/* XXX */
    673 	vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    674 	VOP_UNLOCK(tvp);			/* XXX */
    675 	vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    676 
    677 	tbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
    678 
    679 	/* ugly loop follows... */
    680 	do {
    681 		off_t offset = uio.uio_offset;
    682 
    683 		uio.uio_iov = &iov;
    684 		uio.uio_iovcnt = 1;
    685 		iov.iov_base = tbuf;
    686 		iov.iov_len = MAXBSIZE;
    687 		uio.uio_resid = iov.iov_len;
    688 		uio.uio_rw = UIO_READ;
    689 		error = VOP_READ(fvp, &uio, 0, cred);
    690 
    691 		if (error == 0) {
    692 			uio.uio_iov = &iov;
    693 			uio.uio_iovcnt = 1;
    694 			iov.iov_base = tbuf;
    695 			iov.iov_len = MAXBSIZE - uio.uio_resid;
    696 			uio.uio_offset = offset;
    697 			uio.uio_rw = UIO_WRITE;
    698 			uio.uio_resid = iov.iov_len;
    699 
    700 			if (uio.uio_resid == 0)
    701 				break;
    702 
    703 			do {
    704 				error = VOP_WRITE(tvp, &uio, 0, cred);
    705 			} while ((uio.uio_resid > 0) && (error == 0));
    706 		}
    707 
    708 	} while (error == 0);
    709 
    710 	free(tbuf, M_TEMP);
    711 	return (error);
    712 }
    713 
    714 /*
    715  * (un) is assumed to be locked on entry and remains
    716  * locked on exit.
    717  */
    718 int
    719 union_copyup(struct union_node *un, int docopy, kauth_cred_t cred,
    720 	struct lwp *l)
    721 {
    722 	int error;
    723 	struct vnode *lvp, *uvp;
    724 	struct vattr lvattr, uvattr;
    725 
    726 	error = union_vn_create(&uvp, un, l);
    727 	if (error)
    728 		return (error);
    729 
    730 	/* at this point, uppervp is locked */
    731 	union_newupper(un, uvp);
    732 	un->un_flags |= UN_ULOCK;
    733 
    734 	lvp = un->un_lowervp;
    735 
    736 	if (docopy) {
    737 		/*
    738 		 * XX - should not ignore errors
    739 		 * from VOP_CLOSE
    740 		 */
    741 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    742 
    743         	error = VOP_GETATTR(lvp, &lvattr, cred);
    744 		if (error == 0)
    745 			error = VOP_OPEN(lvp, FREAD, cred);
    746 		if (error == 0) {
    747 			error = union_copyfile(lvp, uvp, cred, l);
    748 			(void) VOP_CLOSE(lvp, FREAD, cred);
    749 		}
    750 		if (error == 0) {
    751 			/* Copy permissions up too */
    752 			vattr_null(&uvattr);
    753 			uvattr.va_mode = lvattr.va_mode;
    754 			uvattr.va_flags = lvattr.va_flags;
    755         		error = VOP_SETATTR(uvp, &uvattr, cred);
    756 		}
    757 		VOP_UNLOCK(lvp);
    758 #ifdef UNION_DIAGNOSTIC
    759 		if (error == 0)
    760 			uprintf("union: copied up %s\n", un->un_path);
    761 #endif
    762 
    763 	}
    764 	union_vn_close(uvp, FWRITE, cred, l);
    765 
    766 	/*
    767 	 * Subsequent IOs will go to the top layer, so
    768 	 * call close on the lower vnode and open on the
    769 	 * upper vnode to ensure that the filesystem keeps
    770 	 * its references counts right.  This doesn't do
    771 	 * the right thing with (cred) and (FREAD) though.
    772 	 * Ignoring error returns is not right, either.
    773 	 */
    774 	if (error == 0) {
    775 		int i;
    776 
    777 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    778 		for (i = 0; i < un->un_openl; i++) {
    779 			(void) VOP_CLOSE(lvp, FREAD, cred);
    780 			(void) VOP_OPEN(uvp, FREAD, cred);
    781 		}
    782 		un->un_openl = 0;
    783 		VOP_UNLOCK(lvp);
    784 	}
    785 
    786 	return (error);
    787 
    788 }
    789 
    790 static int
    791 union_relookup(
    792 	struct union_mount *um,
    793 	struct vnode *dvp,
    794 	struct vnode **vpp,
    795 	struct componentname *cnp,
    796 	struct componentname *cn,
    797 	char **pnbuf_ret,
    798 	const char *path,
    799 	int pathlen)
    800 {
    801 	int error;
    802 	char *pnbuf;
    803 
    804 	/*
    805 	 * A new componentname structure must be faked up because
    806 	 * there is no way to know where the upper level cnp came
    807 	 * from or what it is being used for.  This must duplicate
    808 	 * some of the work done by NDINIT, some of the work done
    809 	 * by namei, some of the work done by lookup and some of
    810 	 * the work done by VOP_LOOKUP when given a CREATE flag.
    811 	 * Conclusion: Horrible.
    812 	 */
    813 	cn->cn_namelen = pathlen;
    814 	if ((cn->cn_namelen + 1) > MAXPATHLEN)
    815 		return (ENAMETOOLONG);
    816 	pnbuf = PNBUF_GET();
    817 	memcpy(pnbuf, path, cn->cn_namelen);
    818 	pnbuf[cn->cn_namelen] = '\0';
    819 	*pnbuf_ret = pnbuf;
    820 
    821 	cn->cn_nameiop = CREATE;
    822 	cn->cn_flags = (LOCKPARENT|ISLASTCN);
    823 	if (um->um_op == UNMNT_ABOVE)
    824 		cn->cn_cred = cnp->cn_cred;
    825 	else
    826 		cn->cn_cred = um->um_cred;
    827 	cn->cn_nameptr = pnbuf;
    828 	cn->cn_hash = cnp->cn_hash;
    829 	cn->cn_consume = cnp->cn_consume;
    830 
    831 	error = relookup(dvp, vpp, cn, 0);
    832 	if (error) {
    833 		PNBUF_PUT(pnbuf);
    834 		*pnbuf_ret = NULL;
    835 	}
    836 
    837 	return (error);
    838 }
    839 
    840 /*
    841  * Create a shadow directory in the upper layer.
    842  * The new vnode is returned locked.
    843  *
    844  * (um) points to the union mount structure for access to the
    845  * the mounting process's credentials.
    846  * (dvp) is the directory in which to create the shadow directory.
    847  * it is unlocked on entry and exit.
    848  * (cnp) is the componentname to be created.
    849  * (vpp) is the returned newly created shadow directory, which
    850  * is returned locked.
    851  *
    852  * N.B. We still attempt to create shadow directories even if the union
    853  * is mounted read-only, which is a little nonintuitive.
    854  */
    855 int
    856 union_mkshadow(struct union_mount *um, struct vnode *dvp,
    857 	struct componentname *cnp, struct vnode **vpp)
    858 {
    859 	int error;
    860 	struct vattr va;
    861 	struct componentname cn;
    862 	char *pnbuf;
    863 
    864 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    865 	error = union_relookup(um, dvp, vpp, cnp, &cn, &pnbuf,
    866 			cnp->cn_nameptr, cnp->cn_namelen);
    867 	if (error) {
    868 		VOP_UNLOCK(dvp);
    869 		return (error);
    870 	}
    871 
    872 	if (*vpp) {
    873 		VOP_ABORTOP(dvp, &cn);
    874 		PNBUF_PUT(pnbuf);
    875 		if (dvp != *vpp)
    876 			VOP_UNLOCK(dvp);
    877 		vput(*vpp);
    878 		*vpp = NULLVP;
    879 		return (EEXIST);
    880 	}
    881 
    882 	/*
    883 	 * policy: when creating the shadow directory in the
    884 	 * upper layer, create it owned by the user who did
    885 	 * the mount, group from parent directory, and mode
    886 	 * 777 modified by umask (ie mostly identical to the
    887 	 * mkdir syscall).  (jsp, kb)
    888 	 */
    889 
    890 	vattr_null(&va);
    891 	va.va_type = VDIR;
    892 	va.va_mode = um->um_cmode;
    893 
    894 	vref(dvp);
    895 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
    896 	PNBUF_PUT(pnbuf);
    897 	return (error);
    898 }
    899 
    900 /*
    901  * Create a whiteout entry in the upper layer.
    902  *
    903  * (um) points to the union mount structure for access to the
    904  * the mounting process's credentials.
    905  * (dvp) is the directory in which to create the whiteout.
    906  * it is locked on entry and exit.
    907  * (cnp) is the componentname to be created.
    908  */
    909 int
    910 union_mkwhiteout(struct union_mount *um, struct vnode *dvp,
    911 	struct componentname *cnp, char *path)
    912 {
    913 	int error;
    914 	struct vnode *wvp;
    915 	struct componentname cn;
    916 	char *pnbuf;
    917 
    918 	VOP_UNLOCK(dvp);
    919 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    920 	error = union_relookup(um, dvp, &wvp, cnp, &cn, &pnbuf,
    921 			       path, strlen(path));
    922 	if (error)
    923 		return (error);
    924 
    925 	if (wvp) {
    926 		VOP_ABORTOP(dvp, &cn);
    927 		PNBUF_PUT(pnbuf);
    928 		if (dvp != wvp)
    929 			VOP_UNLOCK(dvp);
    930 		vput(wvp);
    931 		return (EEXIST);
    932 	}
    933 
    934 	error = VOP_WHITEOUT(dvp, &cn, CREATE);
    935 	if (error) {
    936 		VOP_ABORTOP(dvp, &cn);
    937 	}
    938 
    939 	PNBUF_PUT(pnbuf);
    940 	return (error);
    941 }
    942 
    943 /*
    944  * union_vn_create: creates and opens a new shadow file
    945  * on the upper union layer.  this function is similar
    946  * in spirit to calling vn_open but it avoids calling namei().
    947  * the problem with calling namei is that a) it locks too many
    948  * things, and b) it doesn't start at the "right" directory,
    949  * whereas relookup is told where to start.
    950  */
    951 int
    952 union_vn_create(struct vnode **vpp, struct union_node *un, struct lwp *l)
    953 {
    954 	struct vnode *vp;
    955 	kauth_cred_t cred = l->l_cred;
    956 	struct vattr vat;
    957 	struct vattr *vap = &vat;
    958 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
    959 	int error;
    960 	int cmode = UN_FILEMODE & ~l->l_proc->p_cwdi->cwdi_cmask;
    961 	struct componentname cn;
    962 	char *pnbuf;
    963 
    964 	*vpp = NULLVP;
    965 
    966 	/*
    967 	 * Build a new componentname structure (for the same
    968 	 * reasons outlines in union_mkshadow).
    969 	 * The difference here is that the file is owned by
    970 	 * the current user, rather than by the person who
    971 	 * did the mount, since the current user needs to be
    972 	 * able to write the file (that's why it is being
    973 	 * copied in the first place).
    974 	 */
    975 	cn.cn_namelen = strlen(un->un_path);
    976 	if ((cn.cn_namelen + 1) > MAXPATHLEN)
    977 		return (ENAMETOOLONG);
    978 	pnbuf = PNBUF_GET();
    979 	memcpy(pnbuf, un->un_path, cn.cn_namelen+1);
    980 	cn.cn_nameiop = CREATE;
    981 	cn.cn_flags = (LOCKPARENT|ISLASTCN);
    982 	cn.cn_cred = l->l_cred;
    983 	cn.cn_nameptr = pnbuf;
    984 	cn.cn_hash = un->un_hash;
    985 	cn.cn_consume = 0;
    986 
    987 	vn_lock(un->un_dirvp, LK_EXCLUSIVE | LK_RETRY);
    988 	error = relookup(un->un_dirvp, &vp, &cn, 0);
    989 	if (error) {
    990 		PNBUF_PUT(pnbuf);
    991 		VOP_UNLOCK(un->un_dirvp);
    992 		return (error);
    993 	}
    994 
    995 	if (vp) {
    996 		VOP_ABORTOP(un->un_dirvp, &cn);
    997 		PNBUF_PUT(pnbuf);
    998 		if (un->un_dirvp != vp)
    999 			VOP_UNLOCK(un->un_dirvp);
   1000 		vput(vp);
   1001 		return (EEXIST);
   1002 	}
   1003 
   1004 	/*
   1005 	 * Good - there was no race to create the file
   1006 	 * so go ahead and create it.  The permissions
   1007 	 * on the file will be 0666 modified by the
   1008 	 * current user's umask.  Access to the file, while
   1009 	 * it is unioned, will require access to the top *and*
   1010 	 * bottom files.  Access when not unioned will simply
   1011 	 * require access to the top-level file.
   1012 	 * TODO: confirm choice of access permissions.
   1013 	 */
   1014 	vattr_null(vap);
   1015 	vap->va_type = VREG;
   1016 	vap->va_mode = cmode;
   1017 	vref(un->un_dirvp);
   1018 	if ((error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) != 0) {
   1019 		PNBUF_PUT(pnbuf);
   1020 		return (error);
   1021 	}
   1022 
   1023 	if ((error = VOP_OPEN(vp, fmode, cred)) != 0) {
   1024 		vput(vp);
   1025 		PNBUF_PUT(pnbuf);
   1026 		return (error);
   1027 	}
   1028 
   1029 	vp->v_writecount++;
   1030 	*vpp = vp;
   1031 	PNBUF_PUT(pnbuf);
   1032 	return (0);
   1033 }
   1034 
   1035 int
   1036 union_vn_close(struct vnode *vp, int fmode, kauth_cred_t cred, struct lwp *l)
   1037 {
   1038 
   1039 	if (fmode & FWRITE)
   1040 		--vp->v_writecount;
   1041 	return (VOP_CLOSE(vp, fmode, cred));
   1042 }
   1043 
   1044 void
   1045 union_removed_upper(struct union_node *un)
   1046 {
   1047 	int hash;
   1048 
   1049 #if 1
   1050 	/*
   1051 	 * We do not set the uppervp to NULLVP here, because lowervp
   1052 	 * may also be NULLVP, so this routine would end up creating
   1053 	 * a bogus union node with no upper or lower VP (that causes
   1054 	 * pain in many places that assume at least one VP exists).
   1055 	 * Since we've removed this node from the cache hash chains,
   1056 	 * it won't be found again.  When all current holders
   1057 	 * release it, union_inactive() will vgone() it.
   1058 	 */
   1059 	union_diruncache(un);
   1060 #else
   1061 	union_newupper(un, NULLVP);
   1062 #endif
   1063 
   1064 	hash = UNION_HASH(un->un_uppervp, un->un_lowervp);
   1065 
   1066 	while (union_list_lock(hash))
   1067 		continue;
   1068 	if (un->un_flags & UN_CACHED) {
   1069 		un->un_flags &= ~UN_CACHED;
   1070 		LIST_REMOVE(un, un_cache);
   1071 	}
   1072 	union_list_unlock(hash);
   1073 
   1074 	if (un->un_flags & UN_ULOCK) {
   1075 		un->un_flags &= ~UN_ULOCK;
   1076 		VOP_UNLOCK(un->un_uppervp);
   1077 	}
   1078 }
   1079 
   1080 #if 0
   1081 struct vnode *
   1082 union_lowervp(struct vnode *vp)
   1083 {
   1084 	struct union_node *un = VTOUNION(vp);
   1085 
   1086 	if ((un->un_lowervp != NULLVP) &&
   1087 	    (vp->v_type == un->un_lowervp->v_type)) {
   1088 		if (vget(un->un_lowervp, 0) == 0)
   1089 			return (un->un_lowervp);
   1090 	}
   1091 
   1092 	return (NULLVP);
   1093 }
   1094 #endif
   1095 
   1096 /*
   1097  * determine whether a whiteout is needed
   1098  * during a remove/rmdir operation.
   1099  */
   1100 int
   1101 union_dowhiteout(struct union_node *un, kauth_cred_t cred)
   1102 {
   1103 	struct vattr va;
   1104 
   1105 	if (un->un_lowervp != NULLVP)
   1106 		return (1);
   1107 
   1108 	if (VOP_GETATTR(un->un_uppervp, &va, cred) == 0 &&
   1109 	    (va.va_flags & OPAQUE))
   1110 		return (1);
   1111 
   1112 	return (0);
   1113 }
   1114 
   1115 static void
   1116 union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp)
   1117 {
   1118 	struct union_node *un;
   1119 
   1120 	if (vp->v_op != union_vnodeop_p) {
   1121 		if (vppp) {
   1122 			vref(vp);
   1123 			*(*vppp)++ = vp;
   1124 			if (--(*cntp) == 0)
   1125 				panic("union: dircache table too small");
   1126 		} else {
   1127 			(*cntp)++;
   1128 		}
   1129 
   1130 		return;
   1131 	}
   1132 
   1133 	un = VTOUNION(vp);
   1134 	if (un->un_uppervp != NULLVP)
   1135 		union_dircache_r(un->un_uppervp, vppp, cntp);
   1136 	if (un->un_lowervp != NULLVP)
   1137 		union_dircache_r(un->un_lowervp, vppp, cntp);
   1138 }
   1139 
   1140 struct vnode *
   1141 union_dircache(struct vnode *vp, struct lwp *l)
   1142 {
   1143 	int cnt;
   1144 	struct vnode *nvp = NULLVP;
   1145 	struct vnode **vpp;
   1146 	struct vnode **dircache;
   1147 	int error;
   1148 
   1149 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1150 	dircache = VTOUNION(vp)->un_dircache;
   1151 
   1152 	nvp = NULLVP;
   1153 
   1154 	if (dircache == 0) {
   1155 		cnt = 0;
   1156 		union_dircache_r(vp, 0, &cnt);
   1157 		cnt++;
   1158 		dircache = (struct vnode **)
   1159 				malloc(cnt * sizeof(struct vnode *),
   1160 					M_TEMP, M_WAITOK);
   1161 		vpp = dircache;
   1162 		union_dircache_r(vp, &vpp, &cnt);
   1163 		VTOUNION(vp)->un_dircache = dircache;
   1164 		*vpp = NULLVP;
   1165 		vpp = dircache + 1;
   1166 	} else {
   1167 		vpp = dircache;
   1168 		do {
   1169 			if (*vpp++ == VTOUNION(vp)->un_uppervp)
   1170 				break;
   1171 		} while (*vpp != NULLVP);
   1172 	}
   1173 
   1174 	if (*vpp == NULLVP)
   1175 		goto out;
   1176 
   1177 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
   1178 	vref(*vpp);
   1179 	error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0);
   1180 	if (!error) {
   1181 		VTOUNION(vp)->un_dircache = 0;
   1182 		VTOUNION(nvp)->un_dircache = dircache;
   1183 	}
   1184 
   1185 out:
   1186 	VOP_UNLOCK(vp);
   1187 	return (nvp);
   1188 }
   1189 
   1190 void
   1191 union_diruncache(struct union_node *un)
   1192 {
   1193 	struct vnode **vpp;
   1194 
   1195 	if (un->un_dircache != 0) {
   1196 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
   1197 			vrele(*vpp);
   1198 		free(un->un_dircache, M_TEMP);
   1199 		un->un_dircache = 0;
   1200 	}
   1201 }
   1202 
   1203 /*
   1204  * Check whether node can rmdir (check empty).
   1205  */
   1206 int
   1207 union_check_rmdir(struct union_node *un, kauth_cred_t cred)
   1208 {
   1209 	int dirlen, eofflag, error;
   1210 	char *dirbuf;
   1211 	struct vattr va;
   1212 	struct vnode *tvp;
   1213 	struct dirent *dp, *edp;
   1214 	struct componentname cn;
   1215 	struct iovec aiov;
   1216 	struct uio auio;
   1217 
   1218 	KASSERT(un->un_uppervp != NULL);
   1219 
   1220 	/* Check upper for being opaque. */
   1221 	KASSERT(VOP_ISLOCKED(un->un_uppervp));
   1222 	error = VOP_GETATTR(un->un_uppervp, &va, cred);
   1223 	if (error || (va.va_flags & OPAQUE))
   1224 		return error;
   1225 
   1226 	if (un->un_lowervp == NULL)
   1227 		return 0;
   1228 
   1229 	/* Check lower for being empty. */
   1230 	vn_lock(un->un_lowervp, LK_SHARED | LK_RETRY);
   1231 	error = VOP_GETATTR(un->un_lowervp, &va, cred);
   1232 	if (error) {
   1233 		VOP_UNLOCK(un->un_lowervp);
   1234 		return error;
   1235 	}
   1236 	dirlen = va.va_blocksize;
   1237 	dirbuf = kmem_alloc(dirlen, KM_SLEEP);
   1238 	if (dirbuf == NULL) {
   1239 		VOP_UNLOCK(un->un_lowervp);
   1240 		return ENOMEM;
   1241 	}
   1242 	/* error = 0; */
   1243 	eofflag = 0;
   1244 	auio.uio_offset = 0;
   1245 	do {
   1246 		aiov.iov_len = dirlen;
   1247 		aiov.iov_base = dirbuf;
   1248 		auio.uio_iov = &aiov;
   1249 		auio.uio_iovcnt = 1;
   1250 		auio.uio_resid = aiov.iov_len;
   1251 		auio.uio_rw = UIO_READ;
   1252 		UIO_SETUP_SYSSPACE(&auio);
   1253 		error = VOP_READDIR(un->un_lowervp, &auio, cred, &eofflag,
   1254 		    NULL, NULL);
   1255 		if (error)
   1256 			break;
   1257 		edp = (struct dirent *)&dirbuf[dirlen - auio.uio_resid];
   1258 		for (dp = (struct dirent *)dirbuf;
   1259 		    error == 0 && dp < edp;
   1260 		    dp = (struct dirent *)((char *)dp + dp->d_reclen)) {
   1261 			if (dp->d_reclen == 0) {
   1262 				error = ENOTEMPTY;
   1263 				break;
   1264 			}
   1265 			if (dp->d_type == DT_WHT ||
   1266 			    (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
   1267 			    (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2)))
   1268 				continue;
   1269 			/* Check for presence in the upper layer. */
   1270 			cn.cn_nameiop = LOOKUP;
   1271 			cn.cn_flags = ISLASTCN | RDONLY;
   1272 			cn.cn_cred = cred;
   1273 			cn.cn_nameptr = dp->d_name;
   1274 			cn.cn_namelen = dp->d_namlen;
   1275 			cn.cn_hash = 0;
   1276 			cn.cn_consume = 0;
   1277 			error = VOP_LOOKUP(un->un_uppervp, &tvp, &cn);
   1278 			if (error == ENOENT && (cn.cn_flags & ISWHITEOUT)) {
   1279 				error = 0;
   1280 				continue;
   1281 			}
   1282 			if (error == 0)
   1283 				vput(tvp);
   1284 			error = ENOTEMPTY;
   1285 		}
   1286 	} while (error == 0 && !eofflag);
   1287 	kmem_free(dirbuf, dirlen);
   1288 	VOP_UNLOCK(un->un_lowervp);
   1289 
   1290 	return error;
   1291 }
   1292 
   1293 /*
   1294  * This hook is called from vn_readdir() to switch to lower directory
   1295  * entry after the upper directory is read.
   1296  */
   1297 int
   1298 union_readdirhook(struct vnode **vpp, struct file *fp, struct lwp *l)
   1299 {
   1300 	struct vnode *vp = *vpp, *lvp;
   1301 	struct vattr va;
   1302 	int error;
   1303 
   1304 	if (vp->v_op != union_vnodeop_p)
   1305 		return (0);
   1306 
   1307 	if ((lvp = union_dircache(vp, l)) == NULLVP)
   1308 		return (0);
   1309 
   1310 	/*
   1311 	 * If the directory is opaque,
   1312 	 * then don't show lower entries
   1313 	 */
   1314 	error = VOP_GETATTR(vp, &va, fp->f_cred);
   1315 	if (error || (va.va_flags & OPAQUE)) {
   1316 		vput(lvp);
   1317 		return (error);
   1318 	}
   1319 
   1320 	error = VOP_OPEN(lvp, FREAD, fp->f_cred);
   1321 	if (error) {
   1322 		vput(lvp);
   1323 		return (error);
   1324 	}
   1325 	VOP_UNLOCK(lvp);
   1326 	fp->f_data = lvp;
   1327 	fp->f_offset = 0;
   1328 	error = vn_close(vp, FREAD, fp->f_cred);
   1329 	if (error)
   1330 		return (error);
   1331 	*vpp = lvp;
   1332 	return (0);
   1333 }
   1334