Home | History | Annotate | Line # | Download | only in union
union_subr.c revision 1.80
      1 /*	$NetBSD: union_subr.c,v 1.80 2022/03/19 13:48:04 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.80 2022/03/19 13:48:04 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 #include <miscfs/genfs/genfs.h>
     97 #include <miscfs/specfs/specdev.h>
     98 
     99 static LIST_HEAD(uhashhead, union_node) *uhashtbl;
    100 static u_long uhash_mask;		/* size of hash table - 1 */
    101 #define UNION_HASH(u, l) \
    102 	((((u_long) (u) + (u_long) (l)) >> 8) & uhash_mask)
    103 #define NOHASH	((u_long)-1)
    104 
    105 static kmutex_t uhash_lock;
    106 
    107 static void union_newupper(struct union_node *, struct vnode *);
    108 static void union_newlower(struct union_node *, struct vnode *);
    109 static void union_ref(struct union_node *);
    110 static void union_rele(struct union_node *);
    111 static int union_do_lookup(struct vnode *, struct componentname *, kauth_cred_t,    const char *);
    112 int union_vn_close(struct vnode *, int, kauth_cred_t, struct lwp *);
    113 static void union_dircache_r(struct vnode *, struct vnode ***, int *);
    114 struct vnode *union_dircache(struct vnode *, struct lwp *);
    115 
    116 void
    117 union_init(void)
    118 {
    119 
    120 	mutex_init(&uhash_lock, MUTEX_DEFAULT, IPL_NONE);
    121 	uhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &uhash_mask);
    122 }
    123 
    124 void
    125 union_reinit(void)
    126 {
    127 	struct union_node *un;
    128 	struct uhashhead *oldhash, *hash;
    129 	u_long oldmask, mask, val;
    130 	int i;
    131 
    132 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    133 	mutex_enter(&uhash_lock);
    134 	oldhash = uhashtbl;
    135 	oldmask = uhash_mask;
    136 	uhashtbl = hash;
    137 	uhash_mask = mask;
    138 	for (i = 0; i <= oldmask; i++) {
    139 		while ((un = LIST_FIRST(&oldhash[i])) != NULL) {
    140 			LIST_REMOVE(un, un_cache);
    141 			val = UNION_HASH(un->un_uppervp, un->un_lowervp);
    142 			LIST_INSERT_HEAD(&hash[val], un, un_cache);
    143 		}
    144 	}
    145 	mutex_exit(&uhash_lock);
    146 	hashdone(oldhash, HASH_LIST, oldmask);
    147 }
    148 
    149 /*
    150  * Free global unionfs resources.
    151  */
    152 void
    153 union_done(void)
    154 {
    155 
    156 	hashdone(uhashtbl, HASH_LIST, uhash_mask);
    157 	mutex_destroy(&uhash_lock);
    158 
    159 	/* Make sure to unset the readdir hook. */
    160 	vn_union_readdir_hook = NULL;
    161 }
    162 
    163 void
    164 union_newlower(struct union_node *un, struct vnode *lowervp)
    165 {
    166 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
    167 	int nhash = UNION_HASH(un->un_uppervp, lowervp);
    168 
    169 	if (un->un_lowervp == lowervp)
    170 		return;
    171 
    172 	KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE);
    173 	KASSERT(un->un_lowervp == NULL);
    174 
    175 	mutex_enter(&uhash_lock);
    176 
    177 	if (ohash != nhash && (un->un_cflags & UN_CACHED)) {
    178 		un->un_cflags &= ~UN_CACHED;
    179 		LIST_REMOVE(un, un_cache);
    180 	}
    181 	mutex_enter(&un->un_lock);
    182 	un->un_lowervp = lowervp;
    183 	un->un_lowersz = VNOVAL;
    184 	mutex_exit(&un->un_lock);
    185 	if (ohash != nhash) {
    186 		LIST_INSERT_HEAD(&uhashtbl[nhash], un, un_cache);
    187 		un->un_cflags |= UN_CACHED;
    188 	}
    189 
    190 	mutex_exit(&uhash_lock);
    191 }
    192 
    193 void
    194 union_newupper(struct union_node *un, struct vnode *uppervp)
    195 {
    196 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
    197 	int nhash = UNION_HASH(uppervp, un->un_lowervp);
    198 	struct vop_lock_args lock_ap;
    199 	struct vop_unlock_args unlock_ap;
    200 	int error __diagused;
    201 
    202 	if (un->un_uppervp == uppervp)
    203 		return;
    204 
    205 	KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE);
    206 	KASSERT(un->un_uppervp == NULL);
    207 
    208 	/*
    209 	 * We have to transfer the vnode lock from the union vnode to
    210 	 * the upper vnode.  Lock the upper vnode first.  We cannot use
    211 	 * VOP_LOCK() here as it would break the fstrans state.
    212 	 */
    213 	lock_ap.a_desc = VDESC(vop_lock);
    214 	lock_ap.a_vp = uppervp;
    215 	lock_ap.a_flags = LK_EXCLUSIVE;
    216 	error = VCALL(lock_ap.a_vp,  VOFFSET(vop_lock), &lock_ap);
    217 	KASSERT(error == 0);
    218 
    219 	mutex_enter(&uhash_lock);
    220 
    221 	if (ohash != nhash && (un->un_cflags & UN_CACHED)) {
    222 		un->un_cflags &= ~UN_CACHED;
    223 		LIST_REMOVE(un, un_cache);
    224 	}
    225 	mutex_enter(&un->un_lock);
    226 	un->un_uppervp = uppervp;
    227 	un->un_uppersz = VNOVAL;
    228 	/*
    229 	 * With the upper vnode in place unlock the union vnode to
    230 	 * finalize the lock transfer.
    231 	 */
    232 	unlock_ap.a_desc = VDESC(vop_unlock);
    233 	unlock_ap.a_vp = UNIONTOV(un);
    234 	genfs_unlock(&unlock_ap);
    235 	/* Update union vnode interlock & vmobjlock. */
    236 	vshareilock(UNIONTOV(un), uppervp);
    237 	rw_obj_hold(uppervp->v_uobj.vmobjlock);
    238 	uvm_obj_setlock(&UNIONTOV(un)->v_uobj, uppervp->v_uobj.vmobjlock);
    239 	mutex_exit(&un->un_lock);
    240 	if (ohash != nhash) {
    241 		LIST_INSERT_HEAD(&uhashtbl[nhash], un, un_cache);
    242 		un->un_cflags |= UN_CACHED;
    243 	}
    244 
    245 	mutex_exit(&uhash_lock);
    246 }
    247 
    248 /*
    249  * Keep track of size changes in the underlying vnodes.
    250  * If the size changes, then callback to the vm layer
    251  * giving priority to the upper layer size.
    252  *
    253  * Mutex un_lock hold on entry and released on return.
    254  */
    255 void
    256 union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz)
    257 {
    258 	struct union_node *un = VTOUNION(vp);
    259 	off_t sz;
    260 
    261 	KASSERT(mutex_owned(&un->un_lock));
    262 	/* only interested in regular files */
    263 	if (vp->v_type != VREG) {
    264 		mutex_exit(&un->un_lock);
    265 		uvm_vnp_setsize(vp, 0);
    266 		return;
    267 	}
    268 
    269 	sz = VNOVAL;
    270 
    271 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
    272 		un->un_uppersz = uppersz;
    273 		if (sz == VNOVAL)
    274 			sz = un->un_uppersz;
    275 	}
    276 
    277 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
    278 		un->un_lowersz = lowersz;
    279 		if (sz == VNOVAL)
    280 			sz = un->un_lowersz;
    281 	}
    282 	mutex_exit(&un->un_lock);
    283 
    284 	if (sz != VNOVAL) {
    285 #ifdef UNION_DIAGNOSTIC
    286 		printf("union: %s size now %qd\n",
    287 		    uppersz != VNOVAL ? "upper" : "lower", sz);
    288 #endif
    289 		uvm_vnp_setsize(vp, sz);
    290 	}
    291 }
    292 
    293 static void
    294 union_ref(struct union_node *un)
    295 {
    296 
    297 	KASSERT(mutex_owned(&uhash_lock));
    298 	un->un_refs++;
    299 }
    300 
    301 static void
    302 union_rele(struct union_node *un)
    303 {
    304 
    305 	mutex_enter(&uhash_lock);
    306 	un->un_refs--;
    307 	if (un->un_refs > 0) {
    308 		mutex_exit(&uhash_lock);
    309 		return;
    310 	}
    311 	if (un->un_cflags & UN_CACHED) {
    312 		un->un_cflags &= ~UN_CACHED;
    313 		LIST_REMOVE(un, un_cache);
    314 	}
    315 	mutex_exit(&uhash_lock);
    316 
    317 	if (un->un_pvp != NULLVP)
    318 		vrele(un->un_pvp);
    319 	if (un->un_uppervp != NULLVP)
    320 		vrele(un->un_uppervp);
    321 	if (un->un_lowervp != NULLVP)
    322 		vrele(un->un_lowervp);
    323 	if (un->un_dirvp != NULLVP)
    324 		vrele(un->un_dirvp);
    325 	if (un->un_path)
    326 		free(un->un_path, M_TEMP);
    327 	mutex_destroy(&un->un_lock);
    328 
    329 	free(un, M_TEMP);
    330 }
    331 
    332 /*
    333  * allocate a union_node/vnode pair.  the vnode is
    334  * referenced and unlocked.  the new vnode is returned
    335  * via (vpp).  (mp) is the mountpoint of the union filesystem,
    336  * (dvp) is the parent directory where the upper layer object
    337  * should exist (but doesn't) and (cnp) is the componentname
    338  * information which is partially copied to allow the upper
    339  * layer object to be created at a later time.  (uppervp)
    340  * and (lowervp) reference the upper and lower layer objects
    341  * being mapped.  either, but not both, can be nil.
    342  * both, if supplied, are unlocked.
    343  * the reference is either maintained in the new union_node
    344  * object which is allocated, or they are vrele'd.
    345  *
    346  * all union_nodes are maintained on a hash
    347  * list.  new nodes are only allocated when they cannot
    348  * be found on this list.  entries on the list are
    349  * removed when the vfs reclaim entry is called.
    350  *
    351  * the vnode gets attached or referenced with vcache_get().
    352  */
    353 int
    354 union_allocvp(
    355 	struct vnode **vpp,
    356 	struct mount *mp,
    357 	struct vnode *undvp,		/* parent union vnode */
    358 	struct vnode *dvp,		/* may be null */
    359 	struct componentname *cnp,	/* may be null */
    360 	struct vnode *uppervp,		/* may be null */
    361 	struct vnode *lowervp,		/* may be null */
    362 	int docache)
    363 {
    364 	int error;
    365 	struct union_node *un = NULL, *un1;
    366 	struct vnode *vp, *xlowervp = NULLVP;
    367 	u_long hash[3];
    368 	int try;
    369 	bool is_dotdot;
    370 
    371 	is_dotdot = (dvp != NULL && cnp != NULL && (cnp->cn_flags & ISDOTDOT));
    372 
    373 	if (uppervp == NULLVP && lowervp == NULLVP)
    374 		panic("union: unidentifiable allocation");
    375 
    376 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
    377 		xlowervp = lowervp;
    378 		lowervp = NULLVP;
    379 	}
    380 
    381 	/*
    382 	 * If both uppervp and lowervp are not NULL we have to
    383 	 * search union nodes with one vnode as NULL too.
    384 	 */
    385 	hash[0] = UNION_HASH(uppervp, lowervp);
    386 	if (uppervp == NULL || lowervp == NULL) {
    387 		hash[1] = hash[2] = NOHASH;
    388 	} else {
    389 		hash[1] = UNION_HASH(uppervp, NULLVP);
    390 		hash[2] = UNION_HASH(NULLVP, lowervp);
    391 	}
    392 
    393 	if (!docache) {
    394 		un = NULL;
    395 		goto found;
    396 	}
    397 
    398 loop:
    399 	mutex_enter(&uhash_lock);
    400 
    401 	for (try = 0; try < 3; try++) {
    402 		if (hash[try] == NOHASH)
    403 			continue;
    404 		LIST_FOREACH(un, &uhashtbl[hash[try]], un_cache) {
    405 			if ((un->un_lowervp && un->un_lowervp != lowervp) ||
    406 			    (un->un_uppervp && un->un_uppervp != uppervp) ||
    407 			    un->un_mount != mp)
    408 				continue;
    409 
    410 			union_ref(un);
    411 			mutex_exit(&uhash_lock);
    412 			error = vcache_get(mp, &un, sizeof(un), &vp);
    413 			KASSERT(error != 0 || UNIONTOV(un) == vp);
    414 			union_rele(un);
    415 			if (error == ENOENT)
    416 				goto loop;
    417 			else if (error)
    418 				goto out;
    419 			goto found;
    420 		}
    421 	}
    422 
    423 	mutex_exit(&uhash_lock);
    424 
    425 found:
    426 	if (un) {
    427 		if (uppervp != dvp) {
    428 			if (is_dotdot)
    429 				VOP_UNLOCK(dvp);
    430 			vn_lock(UNIONTOV(un), LK_EXCLUSIVE | LK_RETRY);
    431 			if (is_dotdot)
    432 				vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    433 		}
    434 		/*
    435 		 * Save information about the upper layer.
    436 		 */
    437 		if (uppervp != un->un_uppervp) {
    438 			union_newupper(un, uppervp);
    439 		} else if (uppervp) {
    440 			vrele(uppervp);
    441 		}
    442 
    443 		/*
    444 		 * Save information about the lower layer.
    445 		 * This needs to keep track of pathname
    446 		 * and directory information which union_vn_create
    447 		 * might need.
    448 		 */
    449 		if (lowervp != un->un_lowervp) {
    450 			union_newlower(un, lowervp);
    451 			if (cnp && (lowervp != NULLVP)) {
    452 				un->un_path = malloc(cnp->cn_namelen+1,
    453 						M_TEMP, M_WAITOK);
    454 				memcpy(un->un_path, cnp->cn_nameptr,
    455 						cnp->cn_namelen);
    456 				un->un_path[cnp->cn_namelen] = '\0';
    457 				vref(dvp);
    458 				un->un_dirvp = dvp;
    459 			}
    460 		} else if (lowervp) {
    461 			vrele(lowervp);
    462 		}
    463 		*vpp = UNIONTOV(un);
    464 		if (uppervp != dvp)
    465 			VOP_UNLOCK(*vpp);
    466 		error = 0;
    467 		goto out;
    468 	}
    469 
    470 	un = malloc(sizeof(struct union_node), M_TEMP, M_WAITOK);
    471 	mutex_init(&un->un_lock, MUTEX_DEFAULT, IPL_NONE);
    472 	un->un_refs = 1;
    473 	un->un_mount = mp;
    474 	un->un_vnode = NULL;
    475 	un->un_uppervp = uppervp;
    476 	un->un_lowervp = lowervp;
    477 	un->un_pvp = undvp;
    478 	if (undvp != NULLVP)
    479 		vref(undvp);
    480 	un->un_dircache = 0;
    481 	un->un_openl = 0;
    482 	un->un_cflags = 0;
    483 	un->un_hooknode = false;
    484 
    485 	un->un_uppersz = VNOVAL;
    486 	un->un_lowersz = VNOVAL;
    487 
    488 	if (dvp && cnp && (lowervp != NULLVP)) {
    489 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
    490 		memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen);
    491 		un->un_path[cnp->cn_namelen] = '\0';
    492 		vref(dvp);
    493 		un->un_dirvp = dvp;
    494 	} else {
    495 		un->un_path = 0;
    496 		un->un_dirvp = 0;
    497 	}
    498 
    499 	if (docache) {
    500 		mutex_enter(&uhash_lock);
    501 		LIST_FOREACH(un1, &uhashtbl[hash[0]], un_cache) {
    502 			if (un1->un_lowervp == lowervp &&
    503 			    un1->un_uppervp == uppervp &&
    504 			    un1->un_mount == mp) {
    505 				/*
    506 				 * Another thread beat us, push back freshly
    507 				 * allocated node and retry.
    508 				 */
    509 				mutex_exit(&uhash_lock);
    510 				union_rele(un);
    511 				goto loop;
    512 			}
    513 		}
    514 		LIST_INSERT_HEAD(&uhashtbl[hash[0]], un, un_cache);
    515 		un->un_cflags |= UN_CACHED;
    516 		mutex_exit(&uhash_lock);
    517 	}
    518 
    519 	error = vcache_get(mp, &un, sizeof(un), vpp);
    520 	KASSERT(error != 0 || UNIONTOV(un) == *vpp);
    521 	union_rele(un);
    522 	if (error == ENOENT)
    523 		goto loop;
    524 
    525 out:
    526 	if (xlowervp)
    527 		vrele(xlowervp);
    528 
    529 	return error;
    530 }
    531 
    532 int
    533 union_freevp(struct vnode *vp)
    534 {
    535 	struct union_node *un = VTOUNION(vp);
    536 
    537 	/* Detach vnode from union node. */
    538 	un->un_vnode = NULL;
    539 	un->un_uppersz = VNOVAL;
    540 	un->un_lowersz = VNOVAL;
    541 
    542 	/* Detach union node from vnode. */
    543 	mutex_enter(vp->v_interlock);
    544 	vp->v_data = NULL;
    545 	mutex_exit(vp->v_interlock);
    546 
    547 	union_rele(un);
    548 
    549 	return 0;
    550 }
    551 
    552 int
    553 union_loadvnode(struct mount *mp, struct vnode *vp,
    554     const void *key, size_t key_len, const void **new_key)
    555 {
    556 	struct vattr va;
    557 	struct vnode *svp;
    558 	struct union_node *un;
    559 	struct union_mount *um;
    560 	voff_t uppersz, lowersz;
    561 
    562 	KASSERT(key_len == sizeof(un));
    563 	memcpy(&un, key, key_len);
    564 
    565 	um = MOUNTTOUNIONMOUNT(mp);
    566 	svp = (un->un_uppervp != NULLVP) ? un->un_uppervp : un->un_lowervp;
    567 
    568 	vp->v_tag = VT_UNION;
    569 	vp->v_op = union_vnodeop_p;
    570 	vp->v_vflag |= VV_LOCKSWORK;
    571 	vp->v_data = un;
    572 	un->un_vnode = vp;
    573 
    574 	vp->v_type = svp->v_type;
    575 	if (svp->v_type == VCHR || svp->v_type == VBLK)
    576 		spec_node_init(vp, svp->v_rdev);
    577 
    578 	vshareilock(vp, svp);
    579 	rw_obj_hold(svp->v_uobj.vmobjlock);
    580 	uvm_obj_setlock(&vp->v_uobj, svp->v_uobj.vmobjlock);
    581 
    582 	/* detect the root vnode (and aliases) */
    583 	if ((un->un_uppervp == um->um_uppervp) &&
    584 	    ((un->un_lowervp == NULLVP) || un->un_lowervp == um->um_lowervp)) {
    585 		if (un->un_lowervp == NULLVP) {
    586 			un->un_lowervp = um->um_lowervp;
    587 			if (un->un_lowervp != NULLVP)
    588 				vref(un->un_lowervp);
    589 		}
    590 		vp->v_vflag |= VV_ROOT;
    591 	}
    592 
    593 	uppersz = lowersz = VNOVAL;
    594 	if (un->un_uppervp != NULLVP) {
    595 		if (vn_lock(un->un_uppervp, LK_SHARED) == 0) {
    596 			if (VOP_GETATTR(un->un_uppervp, &va, FSCRED) == 0)
    597 				uppersz = va.va_size;
    598 			VOP_UNLOCK(un->un_uppervp);
    599 		}
    600 	}
    601 	if (un->un_lowervp != NULLVP) {
    602 		if (vn_lock(un->un_lowervp, LK_SHARED) == 0) {
    603 			if (VOP_GETATTR(un->un_lowervp, &va, FSCRED) == 0)
    604 				lowersz = va.va_size;
    605 			VOP_UNLOCK(un->un_lowervp);
    606 		}
    607 	}
    608 
    609 	mutex_enter(&un->un_lock);
    610 	union_newsize(vp, uppersz, lowersz);
    611 
    612 	mutex_enter(&uhash_lock);
    613 	union_ref(un);
    614 	mutex_exit(&uhash_lock);
    615 
    616 	*new_key = &vp->v_data;
    617 
    618 	return 0;
    619 }
    620 
    621 /*
    622  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
    623  * using a sequence of reads and writes.  both (fvp)
    624  * and (tvp) are locked on entry and exit.
    625  */
    626 int
    627 union_copyfile(struct vnode *fvp, struct vnode *tvp, kauth_cred_t cred,
    628 	struct lwp *l)
    629 {
    630 	char *tbuf;
    631 	struct uio uio;
    632 	struct iovec iov;
    633 	int error = 0;
    634 
    635 	/*
    636 	 * strategy:
    637 	 * allocate a buffer of size MAXBSIZE.
    638 	 * loop doing reads and writes, keeping track
    639 	 * of the current uio offset.
    640 	 * give up at the first sign of trouble.
    641 	 */
    642 
    643 	uio.uio_offset = 0;
    644 	UIO_SETUP_SYSSPACE(&uio);
    645 
    646 	tbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
    647 
    648 	/* ugly loop follows... */
    649 	do {
    650 		off_t offset = uio.uio_offset;
    651 
    652 		uio.uio_iov = &iov;
    653 		uio.uio_iovcnt = 1;
    654 		iov.iov_base = tbuf;
    655 		iov.iov_len = MAXBSIZE;
    656 		uio.uio_resid = iov.iov_len;
    657 		uio.uio_rw = UIO_READ;
    658 		error = VOP_READ(fvp, &uio, 0, cred);
    659 
    660 		if (error == 0) {
    661 			uio.uio_iov = &iov;
    662 			uio.uio_iovcnt = 1;
    663 			iov.iov_base = tbuf;
    664 			iov.iov_len = MAXBSIZE - uio.uio_resid;
    665 			uio.uio_offset = offset;
    666 			uio.uio_rw = UIO_WRITE;
    667 			uio.uio_resid = iov.iov_len;
    668 
    669 			if (uio.uio_resid == 0)
    670 				break;
    671 
    672 			do {
    673 				error = VOP_WRITE(tvp, &uio, 0, cred);
    674 			} while ((uio.uio_resid > 0) && (error == 0));
    675 		}
    676 
    677 	} while (error == 0);
    678 
    679 	free(tbuf, M_TEMP);
    680 	return (error);
    681 }
    682 
    683 /*
    684  * (un) is assumed to be locked on entry and remains
    685  * locked on exit.
    686  */
    687 int
    688 union_copyup(struct union_node *un, int docopy, kauth_cred_t cred,
    689 	struct lwp *l)
    690 {
    691 	int error;
    692 	struct vnode *lvp, *uvp;
    693 	struct vattr lvattr, uvattr;
    694 
    695 	error = union_vn_create(&uvp, un, l);
    696 	if (error)
    697 		return (error);
    698 
    699 	union_newupper(un, uvp);
    700 
    701 	lvp = un->un_lowervp;
    702 
    703 	if (docopy) {
    704 		/*
    705 		 * XX - should not ignore errors
    706 		 * from VOP_CLOSE
    707 		 */
    708 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    709 
    710         	error = VOP_GETATTR(lvp, &lvattr, cred);
    711 		if (error == 0)
    712 			error = VOP_OPEN(lvp, FREAD, cred);
    713 		if (error == 0) {
    714 			error = union_copyfile(lvp, uvp, cred, l);
    715 			(void) VOP_CLOSE(lvp, FREAD, cred);
    716 		}
    717 		if (error == 0) {
    718 			/* Copy permissions up too */
    719 			vattr_null(&uvattr);
    720 			uvattr.va_mode = lvattr.va_mode;
    721 			uvattr.va_flags = lvattr.va_flags;
    722         		error = VOP_SETATTR(uvp, &uvattr, cred);
    723 		}
    724 		VOP_UNLOCK(lvp);
    725 #ifdef UNION_DIAGNOSTIC
    726 		if (error == 0)
    727 			uprintf("union: copied up %s\n", un->un_path);
    728 #endif
    729 
    730 	}
    731 	union_vn_close(uvp, FWRITE, cred, l);
    732 
    733 	/*
    734 	 * Subsequent IOs will go to the top layer, so
    735 	 * call close on the lower vnode and open on the
    736 	 * upper vnode to ensure that the filesystem keeps
    737 	 * its references counts right.  This doesn't do
    738 	 * the right thing with (cred) and (FREAD) though.
    739 	 * Ignoring error returns is not right, either.
    740 	 */
    741 	if (error == 0) {
    742 		int i;
    743 
    744 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    745 		for (i = 0; i < un->un_openl; i++) {
    746 			(void) VOP_CLOSE(lvp, FREAD, cred);
    747 			(void) VOP_OPEN(uvp, FREAD, cred);
    748 		}
    749 		un->un_openl = 0;
    750 		VOP_UNLOCK(lvp);
    751 	}
    752 
    753 	return (error);
    754 
    755 }
    756 
    757 /*
    758  * Prepare the creation of a new node in the upper layer.
    759  *
    760  * (dvp) is the directory in which to create the new node.
    761  * it is locked on entry and exit.
    762  * (cnp) is the componentname to be created.
    763  * (cred, path, hash) are credentials, path and its hash to fill (cnp).
    764  */
    765 static int
    766 union_do_lookup(struct vnode *dvp, struct componentname *cnp, kauth_cred_t cred,
    767     const char *path)
    768 {
    769 	int error;
    770 	struct vnode *vp;
    771 
    772 	cnp->cn_nameiop = CREATE;
    773 	cnp->cn_flags = LOCKPARENT | ISLASTCN;
    774 	cnp->cn_cred = cred;
    775 	cnp->cn_nameptr = path;
    776 	cnp->cn_namelen = strlen(path);
    777 
    778 	error = VOP_LOOKUP(dvp, &vp, cnp);
    779 
    780 	if (error == 0) {
    781 		KASSERT(vp != NULL);
    782 		VOP_ABORTOP(dvp, cnp);
    783 		vrele(vp);
    784 		error = EEXIST;
    785 	} else if (error == EJUSTRETURN) {
    786 		error = 0;
    787 	}
    788 
    789 	return error;
    790 }
    791 
    792 /*
    793  * Create a shadow directory in the upper layer.
    794  * The new vnode is returned locked.
    795  *
    796  * (um) points to the union mount structure for access to the
    797  * the mounting process's credentials.
    798  * (dvp) is the directory in which to create the shadow directory.
    799  * it is unlocked on entry and exit.
    800  * (cnp) is the componentname to be created.
    801  * (vpp) is the returned newly created shadow directory, which
    802  * is returned locked.
    803  *
    804  * N.B. We still attempt to create shadow directories even if the union
    805  * is mounted read-only, which is a little nonintuitive.
    806  */
    807 int
    808 union_mkshadow(struct union_mount *um, struct vnode *dvp,
    809 	struct componentname *cnp, struct vnode **vpp)
    810 {
    811 	int error;
    812 	struct vattr va;
    813 	struct componentname cn;
    814 	char *pnbuf;
    815 
    816 	if (cnp->cn_namelen + 1 > MAXPATHLEN)
    817 		return ENAMETOOLONG;
    818 	pnbuf = PNBUF_GET();
    819 	memcpy(pnbuf, cnp->cn_nameptr, cnp->cn_namelen);
    820 	pnbuf[cnp->cn_namelen] = '\0';
    821 
    822 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    823 
    824 	error = union_do_lookup(dvp, &cn,
    825 	    (um->um_op == UNMNT_ABOVE ? cnp->cn_cred : um->um_cred), pnbuf);
    826 	if (error) {
    827 		VOP_UNLOCK(dvp);
    828 		PNBUF_PUT(pnbuf);
    829 		return error;
    830 	}
    831 
    832 	/*
    833 	 * policy: when creating the shadow directory in the
    834 	 * upper layer, create it owned by the user who did
    835 	 * the mount, group from parent directory, and mode
    836 	 * 777 modified by umask (ie mostly identical to the
    837 	 * mkdir syscall).  (jsp, kb)
    838 	 */
    839 
    840 	vattr_null(&va);
    841 	va.va_type = VDIR;
    842 	va.va_mode = um->um_cmode;
    843 
    844 	KASSERT(*vpp == NULL);
    845 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
    846 	VOP_UNLOCK(dvp);
    847 	PNBUF_PUT(pnbuf);
    848 	return error;
    849 }
    850 
    851 /*
    852  * Create a whiteout entry in the upper layer.
    853  *
    854  * (um) points to the union mount structure for access to the
    855  * the mounting process's credentials.
    856  * (dvp) is the directory in which to create the whiteout.
    857  * it is locked on entry and exit.
    858  * (cnp) is the componentname to be created.
    859  * (un) holds the path and its hash to be created.
    860  */
    861 int
    862 union_mkwhiteout(struct union_mount *um, struct vnode *dvp,
    863 	struct componentname *cnp, struct union_node *un)
    864 {
    865 	int error;
    866 	struct componentname cn;
    867 
    868 	error = union_do_lookup(dvp, &cn,
    869 	    (um->um_op == UNMNT_ABOVE ? cnp->cn_cred : um->um_cred),
    870 	    un->un_path);
    871 	if (error)
    872 		return error;
    873 
    874 	error = VOP_WHITEOUT(dvp, &cn, CREATE);
    875 	return error;
    876 }
    877 
    878 /*
    879  * union_vn_create: creates and opens a new shadow file
    880  * on the upper union layer.  this function is similar
    881  * in spirit to calling vn_open but it avoids calling namei().
    882  * the problem with calling namei is that a) it locks too many
    883  * things, and b) it doesn't start at the "right" directory,
    884  * whereas union_do_lookup is told where to start.
    885  */
    886 int
    887 union_vn_create(struct vnode **vpp, struct union_node *un, struct lwp *l)
    888 {
    889 	struct vnode *vp;
    890 	kauth_cred_t cred = l->l_cred;
    891 	struct vattr vat;
    892 	struct vattr *vap = &vat;
    893 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
    894 	int error;
    895 	int cmode = UN_FILEMODE & ~l->l_proc->p_cwdi->cwdi_cmask;
    896 	struct componentname cn;
    897 
    898 	*vpp = NULLVP;
    899 
    900 	vn_lock(un->un_dirvp, LK_EXCLUSIVE | LK_RETRY);
    901 
    902 	error = union_do_lookup(un->un_dirvp, &cn, l->l_cred,
    903 	    un->un_path);
    904 	if (error) {
    905 		VOP_UNLOCK(un->un_dirvp);
    906 		return error;
    907 	}
    908 
    909 	/*
    910 	 * Good - there was no race to create the file
    911 	 * so go ahead and create it.  The permissions
    912 	 * on the file will be 0666 modified by the
    913 	 * current user's umask.  Access to the file, while
    914 	 * it is unioned, will require access to the top *and*
    915 	 * bottom files.  Access when not unioned will simply
    916 	 * require access to the top-level file.
    917 	 * TODO: confirm choice of access permissions.
    918 	 */
    919 	vattr_null(vap);
    920 	vap->va_type = VREG;
    921 	vap->va_mode = cmode;
    922 	vp = NULL;
    923 	error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap);
    924 	if (error) {
    925 		VOP_UNLOCK(un->un_dirvp);
    926 		return error;
    927 	}
    928 
    929 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    930 	VOP_UNLOCK(un->un_dirvp);
    931 	error = VOP_OPEN(vp, fmode, cred);
    932 	if (error) {
    933 		vput(vp);
    934 		return error;
    935 	}
    936 
    937 	vp->v_writecount++;
    938 	VOP_UNLOCK(vp);
    939 	*vpp = vp;
    940 	return 0;
    941 }
    942 
    943 int
    944 union_vn_close(struct vnode *vp, int fmode, kauth_cred_t cred, struct lwp *l)
    945 {
    946 
    947 	if (fmode & FWRITE)
    948 		--vp->v_writecount;
    949 	return (VOP_CLOSE(vp, fmode, cred));
    950 }
    951 
    952 void
    953 union_removed_upper(struct union_node *un)
    954 {
    955 	struct vnode *vp = UNIONTOV(un);
    956 
    957 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    958 #if 1
    959 	/*
    960 	 * We do not set the uppervp to NULLVP here, because lowervp
    961 	 * may also be NULLVP, so this routine would end up creating
    962 	 * a bogus union node with no upper or lower VP (that causes
    963 	 * pain in many places that assume at least one VP exists).
    964 	 * Since we've removed this node from the cache hash chains,
    965 	 * it won't be found again.  When all current holders
    966 	 * release it, union_inactive() will vgone() it.
    967 	 */
    968 	union_diruncache(un);
    969 #else
    970 	union_newupper(un, NULLVP);
    971 #endif
    972 
    973 	VOP_UNLOCK(vp);
    974 
    975 	mutex_enter(&uhash_lock);
    976 	if (un->un_cflags & UN_CACHED) {
    977 		un->un_cflags &= ~UN_CACHED;
    978 		LIST_REMOVE(un, un_cache);
    979 	}
    980 	mutex_exit(&uhash_lock);
    981 }
    982 
    983 #if 0
    984 struct vnode *
    985 union_lowervp(struct vnode *vp)
    986 {
    987 	struct union_node *un = VTOUNION(vp);
    988 
    989 	if ((un->un_lowervp != NULLVP) &&
    990 	    (vp->v_type == un->un_lowervp->v_type)) {
    991 		if (vget(un->un_lowervp, 0, true /* wait */) == 0)
    992 			return (un->un_lowervp);
    993 	}
    994 
    995 	return (NULLVP);
    996 }
    997 #endif
    998 
    999 /*
   1000  * determine whether a whiteout is needed
   1001  * during a remove/rmdir operation.
   1002  */
   1003 int
   1004 union_dowhiteout(struct union_node *un, kauth_cred_t cred)
   1005 {
   1006 	struct vattr va;
   1007 
   1008 	if (un->un_lowervp != NULLVP)
   1009 		return (1);
   1010 
   1011 	if (VOP_GETATTR(un->un_uppervp, &va, cred) == 0 &&
   1012 	    (va.va_flags & OPAQUE))
   1013 		return (1);
   1014 
   1015 	return (0);
   1016 }
   1017 
   1018 static void
   1019 union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp)
   1020 {
   1021 	struct union_node *un;
   1022 
   1023 	if (vp->v_op != union_vnodeop_p) {
   1024 		if (vppp) {
   1025 			vref(vp);
   1026 			*(*vppp)++ = vp;
   1027 			if (--(*cntp) == 0)
   1028 				panic("union: dircache table too small");
   1029 		} else {
   1030 			(*cntp)++;
   1031 		}
   1032 
   1033 		return;
   1034 	}
   1035 
   1036 	un = VTOUNION(vp);
   1037 	if (un->un_uppervp != NULLVP)
   1038 		union_dircache_r(un->un_uppervp, vppp, cntp);
   1039 	if (un->un_lowervp != NULLVP)
   1040 		union_dircache_r(un->un_lowervp, vppp, cntp);
   1041 }
   1042 
   1043 struct vnode *
   1044 union_dircache(struct vnode *vp, struct lwp *l)
   1045 {
   1046 	int cnt;
   1047 	struct vnode *nvp = NULLVP;
   1048 	struct vnode **vpp;
   1049 	struct vnode **dircache;
   1050 	int error;
   1051 
   1052 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1053 	dircache = VTOUNION(vp)->un_dircache;
   1054 
   1055 	nvp = NULLVP;
   1056 
   1057 	if (dircache == 0) {
   1058 		cnt = 0;
   1059 		union_dircache_r(vp, 0, &cnt);
   1060 		cnt++;
   1061 		dircache = (struct vnode **)
   1062 				malloc(cnt * sizeof(struct vnode *),
   1063 					M_TEMP, M_WAITOK);
   1064 		vpp = dircache;
   1065 		union_dircache_r(vp, &vpp, &cnt);
   1066 		VTOUNION(vp)->un_dircache = dircache;
   1067 		*vpp = NULLVP;
   1068 		vpp = dircache + 1;
   1069 	} else {
   1070 		vpp = dircache;
   1071 		do {
   1072 			if (*vpp++ == VTOUNION(vp)->un_lowervp)
   1073 				break;
   1074 		} while (*vpp != NULLVP);
   1075 	}
   1076 
   1077 	if (*vpp == NULLVP)
   1078 		goto out;
   1079 
   1080 	vref(*vpp);
   1081 	error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0,
   1082 	    NULLVP, *vpp, 0);
   1083 	if (!error) {
   1084 		vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
   1085 		VTOUNION(vp)->un_dircache = 0;
   1086 		VTOUNION(nvp)->un_hooknode = true;
   1087 		VTOUNION(nvp)->un_dircache = dircache;
   1088 	}
   1089 
   1090 out:
   1091 	VOP_UNLOCK(vp);
   1092 	return (nvp);
   1093 }
   1094 
   1095 void
   1096 union_diruncache(struct union_node *un)
   1097 {
   1098 	struct vnode **vpp;
   1099 
   1100 	KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE);
   1101 	if (un->un_dircache != 0) {
   1102 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
   1103 			vrele(*vpp);
   1104 		free(un->un_dircache, M_TEMP);
   1105 		un->un_dircache = 0;
   1106 	}
   1107 }
   1108 
   1109 /*
   1110  * Check whether node can rmdir (check empty).
   1111  */
   1112 int
   1113 union_check_rmdir(struct union_node *un, kauth_cred_t cred)
   1114 {
   1115 	int dirlen, eofflag, error;
   1116 	char *dirbuf;
   1117 	struct vattr va;
   1118 	struct vnode *tvp;
   1119 	struct dirent *dp, *edp;
   1120 	struct componentname cn;
   1121 	struct iovec aiov;
   1122 	struct uio auio;
   1123 
   1124 	KASSERT(un->un_uppervp != NULL);
   1125 
   1126 	/* Check upper for being opaque. */
   1127 	KASSERT(VOP_ISLOCKED(un->un_uppervp));
   1128 	error = VOP_GETATTR(un->un_uppervp, &va, cred);
   1129 	if (error || (va.va_flags & OPAQUE))
   1130 		return error;
   1131 
   1132 	if (un->un_lowervp == NULL)
   1133 		return 0;
   1134 
   1135 	/* Check lower for being empty. */
   1136 	vn_lock(un->un_lowervp, LK_SHARED | LK_RETRY);
   1137 	error = VOP_GETATTR(un->un_lowervp, &va, cred);
   1138 	if (error) {
   1139 		VOP_UNLOCK(un->un_lowervp);
   1140 		return error;
   1141 	}
   1142 	dirlen = va.va_blocksize;
   1143 	dirbuf = kmem_alloc(dirlen, KM_SLEEP);
   1144 	/* error = 0; */
   1145 	eofflag = 0;
   1146 	auio.uio_offset = 0;
   1147 	do {
   1148 		aiov.iov_len = dirlen;
   1149 		aiov.iov_base = dirbuf;
   1150 		auio.uio_iov = &aiov;
   1151 		auio.uio_iovcnt = 1;
   1152 		auio.uio_resid = aiov.iov_len;
   1153 		auio.uio_rw = UIO_READ;
   1154 		UIO_SETUP_SYSSPACE(&auio);
   1155 		error = VOP_READDIR(un->un_lowervp, &auio, cred, &eofflag,
   1156 		    NULL, NULL);
   1157 		if (error)
   1158 			break;
   1159 		edp = (struct dirent *)&dirbuf[dirlen - auio.uio_resid];
   1160 		for (dp = (struct dirent *)dirbuf;
   1161 		    error == 0 && dp < edp;
   1162 		    dp = (struct dirent *)((char *)dp + dp->d_reclen)) {
   1163 			if (dp->d_reclen == 0) {
   1164 				error = ENOTEMPTY;
   1165 				break;
   1166 			}
   1167 			if (dp->d_type == DT_WHT ||
   1168 			    (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
   1169 			    (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2)))
   1170 				continue;
   1171 			/* Check for presence in the upper layer. */
   1172 			cn.cn_nameiop = LOOKUP;
   1173 			cn.cn_flags = ISLASTCN | RDONLY;
   1174 			cn.cn_cred = cred;
   1175 			cn.cn_nameptr = dp->d_name;
   1176 			cn.cn_namelen = dp->d_namlen;
   1177 			error = VOP_LOOKUP(un->un_uppervp, &tvp, &cn);
   1178 			if (error == ENOENT && (cn.cn_flags & ISWHITEOUT)) {
   1179 				error = 0;
   1180 				continue;
   1181 			}
   1182 			if (error == 0)
   1183 				vrele(tvp);
   1184 			error = ENOTEMPTY;
   1185 		}
   1186 	} while (error == 0 && !eofflag);
   1187 	kmem_free(dirbuf, dirlen);
   1188 	VOP_UNLOCK(un->un_lowervp);
   1189 
   1190 	return error;
   1191 }
   1192 
   1193 /*
   1194  * This hook is called from vn_readdir() to switch to lower directory
   1195  * entry after the upper directory is read.
   1196  */
   1197 int
   1198 union_readdirhook(struct vnode **vpp, struct file *fp, struct lwp *l)
   1199 {
   1200 	struct vnode *vp = *vpp, *lvp;
   1201 	struct vattr va;
   1202 	int error;
   1203 
   1204 	if (vp->v_op != union_vnodeop_p)
   1205 		return (0);
   1206 
   1207 	/*
   1208 	 * If the directory is opaque,
   1209 	 * then don't show lower entries
   1210 	 */
   1211 	vn_lock(vp, LK_SHARED | LK_RETRY);
   1212 	error = VOP_GETATTR(vp, &va, fp->f_cred);
   1213 	VOP_UNLOCK(vp);
   1214 	if (error || (va.va_flags & OPAQUE))
   1215 		return error;
   1216 
   1217 	if ((lvp = union_dircache(vp, l)) == NULLVP)
   1218 		return (0);
   1219 
   1220 	error = VOP_OPEN(lvp, FREAD, fp->f_cred);
   1221 	if (error) {
   1222 		vput(lvp);
   1223 		return (error);
   1224 	}
   1225 	VOP_UNLOCK(lvp);
   1226 	fp->f_vnode = lvp;
   1227 	fp->f_offset = 0;
   1228 	error = vn_close(vp, FREAD, fp->f_cred);
   1229 	if (error)
   1230 		return (error);
   1231 	*vpp = lvp;
   1232 	return (0);
   1233 }
   1234