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