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