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