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