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