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