Home | History | Annotate | Line # | Download | only in union
union_subr.c revision 1.77.10.1
      1  1.77.10.1        ad /*	$NetBSD: union_subr.c,v 1.77.10.1 2020/02/29 20:21:02 ad Exp $	*/
      2        1.1  jdolecek 
      3        1.1  jdolecek /*
      4        1.1  jdolecek  * Copyright (c) 1994
      5        1.1  jdolecek  *	The Regents of the University of California.  All rights reserved.
      6        1.1  jdolecek  *
      7        1.1  jdolecek  * This code is derived from software contributed to Berkeley by
      8        1.1  jdolecek  * Jan-Simon Pendry.
      9        1.1  jdolecek  *
     10        1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     11        1.1  jdolecek  * modification, are permitted provided that the following conditions
     12        1.1  jdolecek  * are met:
     13        1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14        1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15        1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17        1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     18        1.7       agc  * 3. Neither the name of the University nor the names of its contributors
     19        1.7       agc  *    may be used to endorse or promote products derived from this software
     20        1.7       agc  *    without specific prior written permission.
     21        1.7       agc  *
     22        1.7       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23        1.7       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24        1.7       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25        1.7       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26        1.7       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27        1.7       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28        1.7       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29        1.7       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30        1.7       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31        1.7       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32        1.7       agc  * SUCH DAMAGE.
     33        1.7       agc  *
     34        1.7       agc  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
     35        1.7       agc  */
     36        1.7       agc 
     37        1.7       agc /*
     38        1.7       agc  * Copyright (c) 1994 Jan-Simon Pendry
     39        1.7       agc  *
     40        1.7       agc  * This code is derived from software contributed to Berkeley by
     41        1.7       agc  * Jan-Simon Pendry.
     42        1.7       agc  *
     43        1.7       agc  * Redistribution and use in source and binary forms, with or without
     44        1.7       agc  * modification, are permitted provided that the following conditions
     45        1.7       agc  * are met:
     46        1.7       agc  * 1. Redistributions of source code must retain the above copyright
     47        1.7       agc  *    notice, this list of conditions and the following disclaimer.
     48        1.7       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49        1.7       agc  *    notice, this list of conditions and the following disclaimer in the
     50        1.7       agc  *    documentation and/or other materials provided with the distribution.
     51        1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     52        1.1  jdolecek  *    must display the following acknowledgement:
     53        1.1  jdolecek  *	This product includes software developed by the University of
     54        1.1  jdolecek  *	California, Berkeley and its contributors.
     55        1.1  jdolecek  * 4. Neither the name of the University nor the names of its contributors
     56        1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     57        1.1  jdolecek  *    without specific prior written permission.
     58        1.1  jdolecek  *
     59        1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60        1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61        1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62        1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63        1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64        1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65        1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66        1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67        1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68        1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69        1.1  jdolecek  * SUCH DAMAGE.
     70        1.1  jdolecek  *
     71        1.1  jdolecek  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
     72        1.1  jdolecek  */
     73        1.1  jdolecek 
     74        1.1  jdolecek #include <sys/cdefs.h>
     75  1.77.10.1        ad __KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.77.10.1 2020/02/29 20:21:02 ad Exp $");
     76        1.1  jdolecek 
     77        1.1  jdolecek #include <sys/param.h>
     78        1.1  jdolecek #include <sys/systm.h>
     79        1.1  jdolecek #include <sys/proc.h>
     80        1.1  jdolecek #include <sys/time.h>
     81        1.1  jdolecek #include <sys/kernel.h>
     82        1.1  jdolecek #include <sys/vnode.h>
     83        1.1  jdolecek #include <sys/namei.h>
     84        1.1  jdolecek #include <sys/malloc.h>
     85       1.44   hannken #include <sys/dirent.h>
     86        1.1  jdolecek #include <sys/file.h>
     87        1.1  jdolecek #include <sys/filedesc.h>
     88        1.1  jdolecek #include <sys/queue.h>
     89        1.1  jdolecek #include <sys/mount.h>
     90        1.1  jdolecek #include <sys/stat.h>
     91       1.18      elad #include <sys/kauth.h>
     92        1.1  jdolecek 
     93        1.1  jdolecek #include <uvm/uvm_extern.h>
     94        1.1  jdolecek 
     95        1.1  jdolecek #include <fs/union/union.h>
     96       1.53   hannken #include <miscfs/genfs/genfs.h>
     97       1.47   hannken #include <miscfs/specfs/specdev.h>
     98        1.1  jdolecek 
     99       1.54   hannken static LIST_HEAD(uhashhead, union_node) *uhashtbl;
    100       1.54   hannken static u_long uhash_mask;		/* size of hash table - 1 */
    101        1.1  jdolecek #define UNION_HASH(u, l) \
    102       1.54   hannken 	((((u_long) (u) + (u_long) (l)) >> 8) & uhash_mask)
    103       1.54   hannken #define NOHASH	((u_long)-1)
    104        1.1  jdolecek 
    105       1.54   hannken static kmutex_t uhash_lock;
    106        1.1  jdolecek 
    107       1.76   hannken static void union_newupper(struct union_node *, struct vnode *);
    108       1.76   hannken static void union_newlower(struct union_node *, struct vnode *);
    109       1.69   hannken static void union_ref(struct union_node *);
    110       1.69   hannken static void union_rele(struct union_node *);
    111       1.56  dholland static int union_do_lookup(struct vnode *, struct componentname *, kauth_cred_t,    const char *);
    112       1.18      elad int union_vn_close(struct vnode *, int, kauth_cred_t, struct lwp *);
    113       1.14   xtraeme static void union_dircache_r(struct vnode *, struct vnode ***, int *);
    114       1.15  christos struct vnode *union_dircache(struct vnode *, struct lwp *);
    115        1.1  jdolecek 
    116        1.1  jdolecek void
    117       1.32      matt union_init(void)
    118        1.1  jdolecek {
    119       1.54   hannken 
    120       1.54   hannken 	mutex_init(&uhash_lock, MUTEX_DEFAULT, IPL_NONE);
    121       1.54   hannken 	uhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &uhash_mask);
    122       1.54   hannken }
    123       1.54   hannken 
    124       1.54   hannken void
    125       1.54   hannken union_reinit(void)
    126       1.54   hannken {
    127       1.54   hannken 	struct union_node *un;
    128       1.54   hannken 	struct uhashhead *oldhash, *hash;
    129       1.54   hannken 	u_long oldmask, mask, val;
    130        1.1  jdolecek 	int i;
    131        1.1  jdolecek 
    132       1.54   hannken 	hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
    133       1.54   hannken 	mutex_enter(&uhash_lock);
    134       1.54   hannken 	oldhash = uhashtbl;
    135       1.54   hannken 	oldmask = uhash_mask;
    136       1.54   hannken 	uhashtbl = hash;
    137       1.54   hannken 	uhash_mask = mask;
    138       1.54   hannken 	for (i = 0; i <= oldmask; i++) {
    139       1.54   hannken 		while ((un = LIST_FIRST(&oldhash[i])) != NULL) {
    140       1.54   hannken 			LIST_REMOVE(un, un_cache);
    141       1.54   hannken 			val = UNION_HASH(un->un_uppervp, un->un_lowervp);
    142       1.54   hannken 			LIST_INSERT_HEAD(&hash[val], un, un_cache);
    143       1.54   hannken 		}
    144       1.49   hannken 	}
    145       1.54   hannken 	mutex_exit(&uhash_lock);
    146       1.54   hannken 	hashdone(oldhash, HASH_LIST, oldmask);
    147        1.1  jdolecek }
    148        1.1  jdolecek 
    149        1.1  jdolecek /*
    150        1.1  jdolecek  * Free global unionfs resources.
    151        1.1  jdolecek  */
    152        1.1  jdolecek void
    153       1.32      matt union_done(void)
    154        1.1  jdolecek {
    155       1.49   hannken 
    156       1.54   hannken 	hashdone(uhashtbl, HASH_LIST, uhash_mask);
    157       1.54   hannken 	mutex_destroy(&uhash_lock);
    158       1.12     perry 
    159        1.2  jdolecek 	/* Make sure to unset the readdir hook. */
    160        1.2  jdolecek 	vn_union_readdir_hook = NULL;
    161        1.1  jdolecek }
    162        1.1  jdolecek 
    163        1.1  jdolecek void
    164       1.76   hannken union_newlower(struct union_node *un, struct vnode *lowervp)
    165        1.1  jdolecek {
    166        1.1  jdolecek 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
    167       1.76   hannken 	int nhash = UNION_HASH(un->un_uppervp, lowervp);
    168       1.76   hannken 
    169       1.76   hannken 	if (un->un_lowervp == lowervp)
    170       1.76   hannken 		return;
    171        1.1  jdolecek 
    172       1.53   hannken 	KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE);
    173       1.76   hannken 	KASSERT(un->un_lowervp == NULL);
    174        1.1  jdolecek 
    175       1.54   hannken 	mutex_enter(&uhash_lock);
    176        1.1  jdolecek 
    177       1.76   hannken 	if (ohash != nhash && (un->un_cflags & UN_CACHED)) {
    178       1.76   hannken 		un->un_cflags &= ~UN_CACHED;
    179       1.76   hannken 		LIST_REMOVE(un, un_cache);
    180        1.1  jdolecek 	}
    181       1.76   hannken 	mutex_enter(&un->un_lock);
    182       1.76   hannken 	un->un_lowervp = lowervp;
    183       1.76   hannken 	un->un_lowersz = VNOVAL;
    184       1.76   hannken 	mutex_exit(&un->un_lock);
    185       1.76   hannken 	if (ohash != nhash) {
    186       1.54   hannken 		LIST_INSERT_HEAD(&uhashtbl[nhash], un, un_cache);
    187       1.53   hannken 		un->un_cflags |= UN_CACHED;
    188        1.1  jdolecek 	}
    189        1.1  jdolecek 
    190       1.54   hannken 	mutex_exit(&uhash_lock);
    191        1.1  jdolecek }
    192        1.1  jdolecek 
    193        1.1  jdolecek void
    194       1.76   hannken union_newupper(struct union_node *un, struct vnode *uppervp)
    195        1.1  jdolecek {
    196       1.76   hannken 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
    197       1.76   hannken 	int nhash = UNION_HASH(uppervp, un->un_lowervp);
    198       1.76   hannken 	struct vop_lock_args lock_ap;
    199       1.76   hannken 	struct vop_unlock_args unlock_ap;
    200       1.76   hannken 	int error __diagused;
    201       1.76   hannken 
    202       1.76   hannken 	if (un->un_uppervp == uppervp)
    203       1.76   hannken 		return;
    204       1.76   hannken 
    205       1.76   hannken 	KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE);
    206       1.76   hannken 	KASSERT(un->un_uppervp == NULL);
    207       1.76   hannken 
    208       1.76   hannken 	/*
    209       1.76   hannken 	 * We have to transfer the vnode lock from the union vnode to
    210       1.76   hannken 	 * the upper vnode.  Lock the upper vnode first.  We cannot use
    211       1.76   hannken 	 * VOP_LOCK() here as it would break the fstrans state.
    212       1.76   hannken 	 */
    213       1.76   hannken 	lock_ap.a_desc = VDESC(vop_lock);
    214       1.76   hannken 	lock_ap.a_vp = uppervp;
    215       1.76   hannken 	lock_ap.a_flags = LK_EXCLUSIVE;
    216       1.76   hannken 	error = VCALL(lock_ap.a_vp,  VOFFSET(vop_lock), &lock_ap);
    217       1.76   hannken 	KASSERT(error == 0);
    218        1.1  jdolecek 
    219       1.76   hannken 	mutex_enter(&uhash_lock);
    220        1.1  jdolecek 
    221       1.76   hannken 	if (ohash != nhash && (un->un_cflags & UN_CACHED)) {
    222       1.76   hannken 		un->un_cflags &= ~UN_CACHED;
    223       1.76   hannken 		LIST_REMOVE(un, un_cache);
    224       1.76   hannken 	}
    225       1.76   hannken 	mutex_enter(&un->un_lock);
    226       1.76   hannken 	un->un_uppervp = uppervp;
    227       1.76   hannken 	un->un_uppersz = VNOVAL;
    228       1.76   hannken 	/*
    229       1.76   hannken 	 * With the upper vnode in place unlock the union vnode to
    230       1.76   hannken 	 * finalize the lock transfer.
    231       1.76   hannken 	 */
    232       1.76   hannken 	unlock_ap.a_desc = VDESC(vop_unlock);
    233       1.76   hannken 	unlock_ap.a_vp = UNIONTOV(un);
    234       1.76   hannken 	genfs_unlock(&unlock_ap);
    235  1.77.10.1        ad 	/* Update union vnode interlock & vmobjlock. */
    236  1.77.10.1        ad 	vshareilock(UNIONTOV(un), uppervp);
    237  1.77.10.1        ad 	rw_obj_hold(uppervp->v_uobj.vmobjlock);
    238  1.77.10.1        ad 	uvm_obj_setlock(&UNIONTOV(un)->v_uobj, uppervp->v_uobj.vmobjlock);
    239       1.76   hannken 	mutex_exit(&un->un_lock);
    240       1.76   hannken 	if (ohash != nhash) {
    241       1.76   hannken 		LIST_INSERT_HEAD(&uhashtbl[nhash], un, un_cache);
    242       1.76   hannken 		un->un_cflags |= UN_CACHED;
    243       1.76   hannken 	}
    244        1.1  jdolecek 
    245       1.76   hannken 	mutex_exit(&uhash_lock);
    246        1.1  jdolecek }
    247        1.1  jdolecek 
    248        1.1  jdolecek /*
    249        1.1  jdolecek  * Keep track of size changes in the underlying vnodes.
    250        1.1  jdolecek  * If the size changes, then callback to the vm layer
    251        1.1  jdolecek  * giving priority to the upper layer size.
    252       1.53   hannken  *
    253       1.53   hannken  * Mutex un_lock hold on entry and released on return.
    254        1.1  jdolecek  */
    255        1.1  jdolecek void
    256       1.32      matt union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz)
    257        1.1  jdolecek {
    258       1.53   hannken 	struct union_node *un = VTOUNION(vp);
    259        1.1  jdolecek 	off_t sz;
    260        1.1  jdolecek 
    261       1.53   hannken 	KASSERT(mutex_owned(&un->un_lock));
    262        1.1  jdolecek 	/* only interested in regular files */
    263       1.26     pooka 	if (vp->v_type != VREG) {
    264       1.53   hannken 		mutex_exit(&un->un_lock);
    265       1.26     pooka 		uvm_vnp_setsize(vp, 0);
    266        1.1  jdolecek 		return;
    267       1.26     pooka 	}
    268        1.1  jdolecek 
    269        1.1  jdolecek 	sz = VNOVAL;
    270        1.1  jdolecek 
    271        1.1  jdolecek 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
    272        1.1  jdolecek 		un->un_uppersz = uppersz;
    273        1.1  jdolecek 		if (sz == VNOVAL)
    274        1.1  jdolecek 			sz = un->un_uppersz;
    275        1.1  jdolecek 	}
    276        1.1  jdolecek 
    277        1.1  jdolecek 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
    278        1.1  jdolecek 		un->un_lowersz = lowersz;
    279        1.1  jdolecek 		if (sz == VNOVAL)
    280        1.1  jdolecek 			sz = un->un_lowersz;
    281        1.1  jdolecek 	}
    282       1.53   hannken 	mutex_exit(&un->un_lock);
    283        1.1  jdolecek 
    284        1.1  jdolecek 	if (sz != VNOVAL) {
    285        1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    286        1.1  jdolecek 		printf("union: %s size now %qd\n",
    287        1.1  jdolecek 		    uppersz != VNOVAL ? "upper" : "lower", sz);
    288        1.1  jdolecek #endif
    289        1.1  jdolecek 		uvm_vnp_setsize(vp, sz);
    290        1.1  jdolecek 	}
    291        1.1  jdolecek }
    292        1.1  jdolecek 
    293       1.69   hannken static void
    294       1.69   hannken union_ref(struct union_node *un)
    295       1.69   hannken {
    296       1.69   hannken 
    297       1.69   hannken 	KASSERT(mutex_owned(&uhash_lock));
    298       1.69   hannken 	un->un_refs++;
    299       1.69   hannken }
    300       1.69   hannken 
    301       1.69   hannken static void
    302       1.69   hannken union_rele(struct union_node *un)
    303       1.69   hannken {
    304       1.69   hannken 
    305       1.69   hannken 	mutex_enter(&uhash_lock);
    306       1.69   hannken 	un->un_refs--;
    307       1.69   hannken 	if (un->un_refs > 0) {
    308       1.69   hannken 		mutex_exit(&uhash_lock);
    309       1.69   hannken 		return;
    310       1.69   hannken 	}
    311       1.69   hannken 	if (un->un_cflags & UN_CACHED) {
    312       1.69   hannken 		un->un_cflags &= ~UN_CACHED;
    313       1.69   hannken 		LIST_REMOVE(un, un_cache);
    314       1.69   hannken 	}
    315       1.69   hannken 	mutex_exit(&uhash_lock);
    316       1.69   hannken 
    317       1.69   hannken 	if (un->un_pvp != NULLVP)
    318       1.69   hannken 		vrele(un->un_pvp);
    319       1.69   hannken 	if (un->un_uppervp != NULLVP)
    320       1.69   hannken 		vrele(un->un_uppervp);
    321       1.69   hannken 	if (un->un_lowervp != NULLVP)
    322       1.69   hannken 		vrele(un->un_lowervp);
    323       1.69   hannken 	if (un->un_dirvp != NULLVP)
    324       1.69   hannken 		vrele(un->un_dirvp);
    325       1.69   hannken 	if (un->un_path)
    326       1.69   hannken 		free(un->un_path, M_TEMP);
    327       1.69   hannken 	mutex_destroy(&un->un_lock);
    328       1.69   hannken 
    329       1.69   hannken 	free(un, M_TEMP);
    330       1.69   hannken }
    331       1.69   hannken 
    332        1.1  jdolecek /*
    333        1.1  jdolecek  * allocate a union_node/vnode pair.  the vnode is
    334       1.63   hannken  * referenced and unlocked.  the new vnode is returned
    335        1.1  jdolecek  * via (vpp).  (mp) is the mountpoint of the union filesystem,
    336        1.1  jdolecek  * (dvp) is the parent directory where the upper layer object
    337        1.1  jdolecek  * should exist (but doesn't) and (cnp) is the componentname
    338        1.1  jdolecek  * information which is partially copied to allow the upper
    339        1.1  jdolecek  * layer object to be created at a later time.  (uppervp)
    340        1.1  jdolecek  * and (lowervp) reference the upper and lower layer objects
    341        1.1  jdolecek  * being mapped.  either, but not both, can be nil.
    342       1.63   hannken  * both, if supplied, are unlocked.
    343        1.1  jdolecek  * the reference is either maintained in the new union_node
    344        1.1  jdolecek  * object which is allocated, or they are vrele'd.
    345        1.1  jdolecek  *
    346       1.70   hannken  * all union_nodes are maintained on a hash
    347        1.1  jdolecek  * list.  new nodes are only allocated when they cannot
    348        1.1  jdolecek  * be found on this list.  entries on the list are
    349        1.1  jdolecek  * removed when the vfs reclaim entry is called.
    350        1.1  jdolecek  *
    351       1.70   hannken  * the vnode gets attached or referenced with vcache_get().
    352        1.1  jdolecek  */
    353        1.1  jdolecek int
    354       1.32      matt union_allocvp(
    355       1.32      matt 	struct vnode **vpp,
    356       1.32      matt 	struct mount *mp,
    357       1.32      matt 	struct vnode *undvp,		/* parent union vnode */
    358       1.32      matt 	struct vnode *dvp,		/* may be null */
    359       1.32      matt 	struct componentname *cnp,	/* may be null */
    360       1.32      matt 	struct vnode *uppervp,		/* may be null */
    361       1.32      matt 	struct vnode *lowervp,		/* may be null */
    362       1.32      matt 	int docache)
    363        1.1  jdolecek {
    364        1.1  jdolecek 	int error;
    365       1.38   hannken 	struct union_node *un = NULL, *un1;
    366       1.38   hannken 	struct vnode *vp, *xlowervp = NULLVP;
    367       1.54   hannken 	u_long hash[3];
    368        1.1  jdolecek 	int try;
    369       1.63   hannken 	bool is_dotdot;
    370        1.1  jdolecek 
    371       1.63   hannken 	is_dotdot = (dvp != NULL && cnp != NULL && (cnp->cn_flags & ISDOTDOT));
    372       1.53   hannken 
    373        1.1  jdolecek 	if (uppervp == NULLVP && lowervp == NULLVP)
    374        1.1  jdolecek 		panic("union: unidentifiable allocation");
    375        1.1  jdolecek 
    376        1.1  jdolecek 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
    377        1.1  jdolecek 		xlowervp = lowervp;
    378        1.1  jdolecek 		lowervp = NULLVP;
    379        1.1  jdolecek 	}
    380        1.1  jdolecek 
    381       1.54   hannken 	/*
    382       1.54   hannken 	 * If both uppervp and lowervp are not NULL we have to
    383       1.54   hannken 	 * search union nodes with one vnode as NULL too.
    384       1.54   hannken 	 */
    385       1.54   hannken 	hash[0] = UNION_HASH(uppervp, lowervp);
    386       1.54   hannken 	if (uppervp == NULL || lowervp == NULL) {
    387       1.54   hannken 		hash[1] = hash[2] = NOHASH;
    388       1.54   hannken 	} else {
    389       1.54   hannken 		hash[1] = UNION_HASH(uppervp, NULLVP);
    390       1.54   hannken 		hash[2] = UNION_HASH(NULLVP, lowervp);
    391       1.54   hannken 	}
    392        1.1  jdolecek 
    393       1.77  christos 	if (!docache) {
    394       1.77  christos 		un = NULL;
    395       1.77  christos 		goto found;
    396       1.77  christos 	}
    397       1.77  christos 
    398       1.54   hannken loop:
    399       1.54   hannken 	mutex_enter(&uhash_lock);
    400        1.1  jdolecek 
    401       1.54   hannken 	for (try = 0; try < 3; try++) {
    402       1.54   hannken 		if (hash[try] == NOHASH)
    403       1.54   hannken 			continue;
    404       1.54   hannken 		LIST_FOREACH(un, &uhashtbl[hash[try]], un_cache) {
    405       1.54   hannken 			if ((un->un_lowervp && un->un_lowervp != lowervp) ||
    406       1.54   hannken 			    (un->un_uppervp && un->un_uppervp != uppervp) ||
    407       1.70   hannken 			    un->un_mount != mp)
    408        1.1  jdolecek 				continue;
    409        1.1  jdolecek 
    410       1.69   hannken 			union_ref(un);
    411       1.54   hannken 			mutex_exit(&uhash_lock);
    412       1.70   hannken 			error = vcache_get(mp, &un, sizeof(un), &vp);
    413       1.70   hannken 			KASSERT(error != 0 || UNIONTOV(un) == vp);
    414       1.69   hannken 			union_rele(un);
    415       1.70   hannken 			if (error == ENOENT)
    416       1.54   hannken 				goto loop;
    417       1.70   hannken 			else if (error)
    418       1.70   hannken 				goto out;
    419       1.54   hannken 			goto found;
    420        1.1  jdolecek 		}
    421       1.54   hannken 	}
    422        1.1  jdolecek 
    423       1.54   hannken 	mutex_exit(&uhash_lock);
    424        1.1  jdolecek 
    425       1.54   hannken found:
    426        1.1  jdolecek 	if (un) {
    427       1.63   hannken 		if (uppervp != dvp) {
    428       1.63   hannken 			if (is_dotdot)
    429       1.63   hannken 				VOP_UNLOCK(dvp);
    430       1.63   hannken 			vn_lock(UNIONTOV(un), LK_EXCLUSIVE | LK_RETRY);
    431       1.63   hannken 			if (is_dotdot)
    432       1.63   hannken 				vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    433       1.63   hannken 		}
    434        1.1  jdolecek 		/*
    435        1.1  jdolecek 		 * Save information about the upper layer.
    436        1.1  jdolecek 		 */
    437        1.1  jdolecek 		if (uppervp != un->un_uppervp) {
    438        1.1  jdolecek 			union_newupper(un, uppervp);
    439        1.1  jdolecek 		} else if (uppervp) {
    440        1.1  jdolecek 			vrele(uppervp);
    441        1.1  jdolecek 		}
    442        1.1  jdolecek 
    443        1.1  jdolecek 		/*
    444        1.1  jdolecek 		 * Save information about the lower layer.
    445        1.1  jdolecek 		 * This needs to keep track of pathname
    446        1.1  jdolecek 		 * and directory information which union_vn_create
    447        1.1  jdolecek 		 * might need.
    448        1.1  jdolecek 		 */
    449        1.1  jdolecek 		if (lowervp != un->un_lowervp) {
    450        1.1  jdolecek 			union_newlower(un, lowervp);
    451        1.1  jdolecek 			if (cnp && (lowervp != NULLVP)) {
    452        1.1  jdolecek 				un->un_path = malloc(cnp->cn_namelen+1,
    453        1.1  jdolecek 						M_TEMP, M_WAITOK);
    454        1.1  jdolecek 				memcpy(un->un_path, cnp->cn_nameptr,
    455        1.1  jdolecek 						cnp->cn_namelen);
    456        1.1  jdolecek 				un->un_path[cnp->cn_namelen] = '\0';
    457       1.35     pooka 				vref(dvp);
    458        1.1  jdolecek 				un->un_dirvp = dvp;
    459        1.1  jdolecek 			}
    460        1.1  jdolecek 		} else if (lowervp) {
    461        1.1  jdolecek 			vrele(lowervp);
    462        1.1  jdolecek 		}
    463        1.1  jdolecek 		*vpp = UNIONTOV(un);
    464       1.63   hannken 		if (uppervp != dvp)
    465       1.63   hannken 			VOP_UNLOCK(*vpp);
    466       1.70   hannken 		error = 0;
    467       1.70   hannken 		goto out;
    468       1.47   hannken 	}
    469       1.47   hannken 
    470       1.70   hannken 	un = malloc(sizeof(struct union_node), M_TEMP, M_WAITOK);
    471       1.53   hannken 	mutex_init(&un->un_lock, MUTEX_DEFAULT, IPL_NONE);
    472       1.69   hannken 	un->un_refs = 1;
    473       1.70   hannken 	un->un_mount = mp;
    474       1.70   hannken 	un->un_vnode = NULL;
    475        1.1  jdolecek 	un->un_uppervp = uppervp;
    476        1.1  jdolecek 	un->un_lowervp = lowervp;
    477        1.1  jdolecek 	un->un_pvp = undvp;
    478        1.1  jdolecek 	if (undvp != NULLVP)
    479       1.35     pooka 		vref(undvp);
    480        1.1  jdolecek 	un->un_dircache = 0;
    481        1.1  jdolecek 	un->un_openl = 0;
    482       1.53   hannken 	un->un_cflags = 0;
    483       1.26     pooka 
    484       1.26     pooka 	un->un_uppersz = VNOVAL;
    485       1.26     pooka 	un->un_lowersz = VNOVAL;
    486       1.26     pooka 
    487       1.17  christos 	if (dvp && cnp && (lowervp != NULLVP)) {
    488        1.1  jdolecek 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
    489        1.1  jdolecek 		memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen);
    490        1.1  jdolecek 		un->un_path[cnp->cn_namelen] = '\0';
    491       1.35     pooka 		vref(dvp);
    492        1.1  jdolecek 		un->un_dirvp = dvp;
    493        1.1  jdolecek 	} else {
    494        1.1  jdolecek 		un->un_path = 0;
    495        1.1  jdolecek 		un->un_dirvp = 0;
    496        1.1  jdolecek 	}
    497        1.1  jdolecek 
    498        1.1  jdolecek 	if (docache) {
    499       1.70   hannken 		mutex_enter(&uhash_lock);
    500       1.70   hannken 		LIST_FOREACH(un1, &uhashtbl[hash[0]], un_cache) {
    501       1.70   hannken 			if (un1->un_lowervp == lowervp &&
    502       1.70   hannken 			    un1->un_uppervp == uppervp &&
    503       1.70   hannken 			    un1->un_mount == mp) {
    504       1.70   hannken 				/*
    505       1.70   hannken 				 * Another thread beat us, push back freshly
    506       1.70   hannken 				 * allocated node and retry.
    507       1.70   hannken 				 */
    508       1.70   hannken 				mutex_exit(&uhash_lock);
    509       1.70   hannken 				union_rele(un);
    510       1.70   hannken 				goto loop;
    511       1.70   hannken 			}
    512       1.70   hannken 		}
    513       1.54   hannken 		LIST_INSERT_HEAD(&uhashtbl[hash[0]], un, un_cache);
    514       1.53   hannken 		un->un_cflags |= UN_CACHED;
    515       1.70   hannken 		mutex_exit(&uhash_lock);
    516        1.1  jdolecek 	}
    517        1.1  jdolecek 
    518       1.70   hannken 	error = vcache_get(mp, &un, sizeof(un), vpp);
    519       1.70   hannken 	KASSERT(error != 0 || UNIONTOV(un) == *vpp);
    520       1.70   hannken 	union_rele(un);
    521       1.70   hannken 	if (error == ENOENT)
    522       1.70   hannken 		goto loop;
    523       1.70   hannken 
    524       1.70   hannken out:
    525        1.1  jdolecek 	if (xlowervp)
    526        1.1  jdolecek 		vrele(xlowervp);
    527        1.1  jdolecek 
    528       1.70   hannken 	return error;
    529        1.1  jdolecek }
    530        1.1  jdolecek 
    531        1.1  jdolecek int
    532       1.32      matt union_freevp(struct vnode *vp)
    533        1.1  jdolecek {
    534        1.1  jdolecek 	struct union_node *un = VTOUNION(vp);
    535        1.1  jdolecek 
    536       1.71   hannken 	/* Detach vnode from union node. */
    537       1.71   hannken 	un->un_vnode = NULL;
    538       1.71   hannken 	un->un_uppersz = VNOVAL;
    539       1.71   hannken 	un->un_lowersz = VNOVAL;
    540       1.71   hannken 
    541       1.71   hannken 	/* Detach union node from vnode. */
    542       1.70   hannken 	mutex_enter(vp->v_interlock);
    543       1.70   hannken 	vp->v_data = NULL;
    544       1.70   hannken 	mutex_exit(vp->v_interlock);
    545       1.70   hannken 
    546       1.69   hannken 	union_rele(un);
    547        1.1  jdolecek 
    548       1.70   hannken 	return 0;
    549       1.70   hannken }
    550       1.70   hannken 
    551       1.70   hannken int
    552       1.70   hannken union_loadvnode(struct mount *mp, struct vnode *vp,
    553       1.70   hannken     const void *key, size_t key_len, const void **new_key)
    554       1.70   hannken {
    555       1.70   hannken 	struct vattr va;
    556       1.70   hannken 	struct vnode *svp;
    557       1.70   hannken 	struct union_node *un;
    558       1.70   hannken 	struct union_mount *um;
    559       1.70   hannken 	voff_t uppersz, lowersz;
    560       1.70   hannken 
    561       1.70   hannken 	KASSERT(key_len == sizeof(un));
    562       1.70   hannken 	memcpy(&un, key, key_len);
    563       1.70   hannken 
    564       1.70   hannken 	um = MOUNTTOUNIONMOUNT(mp);
    565       1.70   hannken 	svp = (un->un_uppervp != NULLVP) ? un->un_uppervp : un->un_lowervp;
    566       1.70   hannken 
    567       1.70   hannken 	vp->v_tag = VT_UNION;
    568       1.70   hannken 	vp->v_op = union_vnodeop_p;
    569       1.70   hannken 	vp->v_data = un;
    570       1.70   hannken 	un->un_vnode = vp;
    571       1.70   hannken 
    572       1.70   hannken 	vp->v_type = svp->v_type;
    573       1.70   hannken 	if (svp->v_type == VCHR || svp->v_type == VBLK)
    574       1.70   hannken 		spec_node_init(vp, svp->v_rdev);
    575       1.70   hannken 
    576  1.77.10.1        ad 	vshareilock(vp, svp);
    577  1.77.10.1        ad 	rw_obj_hold(svp->v_uobj.vmobjlock);
    578  1.77.10.1        ad 	uvm_obj_setlock(&vp->v_uobj, svp->v_uobj.vmobjlock);
    579       1.70   hannken 
    580       1.70   hannken 	/* detect the root vnode (and aliases) */
    581       1.70   hannken 	if ((un->un_uppervp == um->um_uppervp) &&
    582       1.70   hannken 	    ((un->un_lowervp == NULLVP) || un->un_lowervp == um->um_lowervp)) {
    583       1.70   hannken 		if (un->un_lowervp == NULLVP) {
    584       1.70   hannken 			un->un_lowervp = um->um_lowervp;
    585       1.70   hannken 			if (un->un_lowervp != NULLVP)
    586       1.70   hannken 				vref(un->un_lowervp);
    587       1.70   hannken 		}
    588       1.70   hannken 		vp->v_vflag |= VV_ROOT;
    589       1.70   hannken 	}
    590       1.70   hannken 
    591       1.70   hannken 	uppersz = lowersz = VNOVAL;
    592       1.70   hannken 	if (un->un_uppervp != NULLVP) {
    593       1.70   hannken 		if (vn_lock(un->un_uppervp, LK_SHARED) == 0) {
    594       1.70   hannken 			if (VOP_GETATTR(un->un_uppervp, &va, FSCRED) == 0)
    595       1.70   hannken 				uppersz = va.va_size;
    596       1.70   hannken 			VOP_UNLOCK(un->un_uppervp);
    597       1.70   hannken 		}
    598       1.70   hannken 	}
    599       1.70   hannken 	if (un->un_lowervp != NULLVP) {
    600       1.70   hannken 		if (vn_lock(un->un_lowervp, LK_SHARED) == 0) {
    601       1.70   hannken 			if (VOP_GETATTR(un->un_lowervp, &va, FSCRED) == 0)
    602       1.70   hannken 				lowersz = va.va_size;
    603       1.70   hannken 			VOP_UNLOCK(un->un_lowervp);
    604       1.70   hannken 		}
    605       1.70   hannken 	}
    606       1.70   hannken 
    607       1.70   hannken 	mutex_enter(&un->un_lock);
    608       1.70   hannken 	union_newsize(vp, uppersz, lowersz);
    609       1.70   hannken 
    610       1.70   hannken 	mutex_enter(&uhash_lock);
    611       1.70   hannken 	union_ref(un);
    612       1.70   hannken 	mutex_exit(&uhash_lock);
    613       1.70   hannken 
    614       1.70   hannken 	*new_key = &vp->v_data;
    615        1.1  jdolecek 
    616       1.69   hannken 	return 0;
    617        1.1  jdolecek }
    618        1.1  jdolecek 
    619        1.1  jdolecek /*
    620        1.1  jdolecek  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
    621        1.1  jdolecek  * using a sequence of reads and writes.  both (fvp)
    622        1.1  jdolecek  * and (tvp) are locked on entry and exit.
    623        1.1  jdolecek  */
    624        1.1  jdolecek int
    625       1.32      matt union_copyfile(struct vnode *fvp, struct vnode *tvp, kauth_cred_t cred,
    626       1.32      matt 	struct lwp *l)
    627        1.1  jdolecek {
    628       1.13  christos 	char *tbuf;
    629        1.1  jdolecek 	struct uio uio;
    630        1.1  jdolecek 	struct iovec iov;
    631        1.1  jdolecek 	int error = 0;
    632        1.1  jdolecek 
    633        1.1  jdolecek 	/*
    634        1.1  jdolecek 	 * strategy:
    635        1.1  jdolecek 	 * allocate a buffer of size MAXBSIZE.
    636        1.1  jdolecek 	 * loop doing reads and writes, keeping track
    637        1.1  jdolecek 	 * of the current uio offset.
    638        1.1  jdolecek 	 * give up at the first sign of trouble.
    639        1.1  jdolecek 	 */
    640        1.1  jdolecek 
    641        1.1  jdolecek 	uio.uio_offset = 0;
    642       1.16      yamt 	UIO_SETUP_SYSSPACE(&uio);
    643        1.1  jdolecek 
    644       1.13  christos 	tbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
    645        1.1  jdolecek 
    646        1.1  jdolecek 	/* ugly loop follows... */
    647        1.1  jdolecek 	do {
    648        1.1  jdolecek 		off_t offset = uio.uio_offset;
    649        1.1  jdolecek 
    650        1.1  jdolecek 		uio.uio_iov = &iov;
    651        1.1  jdolecek 		uio.uio_iovcnt = 1;
    652       1.13  christos 		iov.iov_base = tbuf;
    653        1.1  jdolecek 		iov.iov_len = MAXBSIZE;
    654        1.1  jdolecek 		uio.uio_resid = iov.iov_len;
    655        1.1  jdolecek 		uio.uio_rw = UIO_READ;
    656        1.1  jdolecek 		error = VOP_READ(fvp, &uio, 0, cred);
    657        1.1  jdolecek 
    658        1.1  jdolecek 		if (error == 0) {
    659        1.1  jdolecek 			uio.uio_iov = &iov;
    660        1.1  jdolecek 			uio.uio_iovcnt = 1;
    661       1.13  christos 			iov.iov_base = tbuf;
    662        1.1  jdolecek 			iov.iov_len = MAXBSIZE - uio.uio_resid;
    663        1.1  jdolecek 			uio.uio_offset = offset;
    664        1.1  jdolecek 			uio.uio_rw = UIO_WRITE;
    665        1.1  jdolecek 			uio.uio_resid = iov.iov_len;
    666        1.1  jdolecek 
    667        1.1  jdolecek 			if (uio.uio_resid == 0)
    668        1.1  jdolecek 				break;
    669        1.1  jdolecek 
    670        1.1  jdolecek 			do {
    671        1.1  jdolecek 				error = VOP_WRITE(tvp, &uio, 0, cred);
    672        1.1  jdolecek 			} while ((uio.uio_resid > 0) && (error == 0));
    673        1.1  jdolecek 		}
    674        1.1  jdolecek 
    675        1.1  jdolecek 	} while (error == 0);
    676        1.1  jdolecek 
    677       1.13  christos 	free(tbuf, M_TEMP);
    678        1.1  jdolecek 	return (error);
    679        1.1  jdolecek }
    680        1.1  jdolecek 
    681        1.1  jdolecek /*
    682        1.1  jdolecek  * (un) is assumed to be locked on entry and remains
    683        1.1  jdolecek  * locked on exit.
    684        1.1  jdolecek  */
    685        1.1  jdolecek int
    686       1.32      matt union_copyup(struct union_node *un, int docopy, kauth_cred_t cred,
    687       1.32      matt 	struct lwp *l)
    688        1.1  jdolecek {
    689        1.1  jdolecek 	int error;
    690        1.1  jdolecek 	struct vnode *lvp, *uvp;
    691        1.1  jdolecek 	struct vattr lvattr, uvattr;
    692        1.1  jdolecek 
    693       1.15  christos 	error = union_vn_create(&uvp, un, l);
    694       1.25   hannken 	if (error)
    695        1.1  jdolecek 		return (error);
    696        1.1  jdolecek 
    697        1.1  jdolecek 	union_newupper(un, uvp);
    698        1.1  jdolecek 
    699        1.1  jdolecek 	lvp = un->un_lowervp;
    700        1.1  jdolecek 
    701        1.1  jdolecek 	if (docopy) {
    702        1.1  jdolecek 		/*
    703        1.1  jdolecek 		 * XX - should not ignore errors
    704        1.1  jdolecek 		 * from VOP_CLOSE
    705        1.1  jdolecek 		 */
    706        1.1  jdolecek 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    707        1.1  jdolecek 
    708       1.29     pooka         	error = VOP_GETATTR(lvp, &lvattr, cred);
    709        1.1  jdolecek 		if (error == 0)
    710       1.29     pooka 			error = VOP_OPEN(lvp, FREAD, cred);
    711        1.1  jdolecek 		if (error == 0) {
    712       1.15  christos 			error = union_copyfile(lvp, uvp, cred, l);
    713       1.29     pooka 			(void) VOP_CLOSE(lvp, FREAD, cred);
    714        1.1  jdolecek 		}
    715        1.1  jdolecek 		if (error == 0) {
    716        1.1  jdolecek 			/* Copy permissions up too */
    717       1.35     pooka 			vattr_null(&uvattr);
    718        1.1  jdolecek 			uvattr.va_mode = lvattr.va_mode;
    719        1.1  jdolecek 			uvattr.va_flags = lvattr.va_flags;
    720       1.29     pooka         		error = VOP_SETATTR(uvp, &uvattr, cred);
    721        1.1  jdolecek 		}
    722       1.37   hannken 		VOP_UNLOCK(lvp);
    723        1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    724        1.1  jdolecek 		if (error == 0)
    725        1.1  jdolecek 			uprintf("union: copied up %s\n", un->un_path);
    726        1.1  jdolecek #endif
    727        1.1  jdolecek 
    728        1.1  jdolecek 	}
    729       1.15  christos 	union_vn_close(uvp, FWRITE, cred, l);
    730        1.1  jdolecek 
    731        1.1  jdolecek 	/*
    732        1.1  jdolecek 	 * Subsequent IOs will go to the top layer, so
    733        1.1  jdolecek 	 * call close on the lower vnode and open on the
    734        1.1  jdolecek 	 * upper vnode to ensure that the filesystem keeps
    735        1.1  jdolecek 	 * its references counts right.  This doesn't do
    736        1.1  jdolecek 	 * the right thing with (cred) and (FREAD) though.
    737        1.1  jdolecek 	 * Ignoring error returns is not right, either.
    738        1.1  jdolecek 	 */
    739        1.1  jdolecek 	if (error == 0) {
    740        1.1  jdolecek 		int i;
    741        1.1  jdolecek 
    742        1.1  jdolecek 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    743        1.1  jdolecek 		for (i = 0; i < un->un_openl; i++) {
    744       1.29     pooka 			(void) VOP_CLOSE(lvp, FREAD, cred);
    745       1.29     pooka 			(void) VOP_OPEN(uvp, FREAD, cred);
    746        1.1  jdolecek 		}
    747        1.1  jdolecek 		un->un_openl = 0;
    748       1.37   hannken 		VOP_UNLOCK(lvp);
    749        1.1  jdolecek 	}
    750        1.1  jdolecek 
    751        1.1  jdolecek 	return (error);
    752        1.1  jdolecek 
    753        1.1  jdolecek }
    754        1.1  jdolecek 
    755       1.50   hannken /*
    756       1.50   hannken  * Prepare the creation of a new node in the upper layer.
    757       1.50   hannken  *
    758       1.50   hannken  * (dvp) is the directory in which to create the new node.
    759       1.50   hannken  * it is locked on entry and exit.
    760       1.50   hannken  * (cnp) is the componentname to be created.
    761       1.50   hannken  * (cred, path, hash) are credentials, path and its hash to fill (cnp).
    762       1.50   hannken  */
    763        1.1  jdolecek static int
    764       1.50   hannken union_do_lookup(struct vnode *dvp, struct componentname *cnp, kauth_cred_t cred,
    765       1.56  dholland     const char *path)
    766        1.1  jdolecek {
    767        1.1  jdolecek 	int error;
    768       1.50   hannken 	struct vnode *vp;
    769       1.50   hannken 
    770       1.50   hannken 	cnp->cn_nameiop = CREATE;
    771       1.50   hannken 	cnp->cn_flags = LOCKPARENT | ISLASTCN;
    772       1.50   hannken 	cnp->cn_cred = cred;
    773       1.50   hannken 	cnp->cn_nameptr = path;
    774       1.50   hannken 	cnp->cn_namelen = strlen(path);
    775        1.1  jdolecek 
    776       1.50   hannken 	error = VOP_LOOKUP(dvp, &vp, cnp);
    777        1.1  jdolecek 
    778       1.50   hannken 	if (error == 0) {
    779       1.50   hannken 		KASSERT(vp != NULL);
    780       1.50   hannken 		VOP_ABORTOP(dvp, cnp);
    781       1.60   hannken 		vrele(vp);
    782       1.50   hannken 		error = EEXIST;
    783       1.50   hannken 	} else if (error == EJUSTRETURN) {
    784       1.50   hannken 		error = 0;
    785        1.1  jdolecek 	}
    786        1.1  jdolecek 
    787       1.50   hannken 	return error;
    788        1.1  jdolecek }
    789        1.1  jdolecek 
    790        1.1  jdolecek /*
    791        1.1  jdolecek  * Create a shadow directory in the upper layer.
    792        1.1  jdolecek  * The new vnode is returned locked.
    793        1.1  jdolecek  *
    794        1.1  jdolecek  * (um) points to the union mount structure for access to the
    795        1.1  jdolecek  * the mounting process's credentials.
    796        1.1  jdolecek  * (dvp) is the directory in which to create the shadow directory.
    797        1.1  jdolecek  * it is unlocked on entry and exit.
    798        1.1  jdolecek  * (cnp) is the componentname to be created.
    799        1.1  jdolecek  * (vpp) is the returned newly created shadow directory, which
    800        1.1  jdolecek  * is returned locked.
    801        1.1  jdolecek  *
    802        1.1  jdolecek  * N.B. We still attempt to create shadow directories even if the union
    803        1.1  jdolecek  * is mounted read-only, which is a little nonintuitive.
    804        1.1  jdolecek  */
    805        1.1  jdolecek int
    806       1.32      matt union_mkshadow(struct union_mount *um, struct vnode *dvp,
    807       1.32      matt 	struct componentname *cnp, struct vnode **vpp)
    808        1.1  jdolecek {
    809        1.1  jdolecek 	int error;
    810        1.1  jdolecek 	struct vattr va;
    811        1.1  jdolecek 	struct componentname cn;
    812       1.40  dholland 	char *pnbuf;
    813        1.1  jdolecek 
    814       1.50   hannken 	if (cnp->cn_namelen + 1 > MAXPATHLEN)
    815       1.50   hannken 		return ENAMETOOLONG;
    816       1.50   hannken 	pnbuf = PNBUF_GET();
    817       1.50   hannken 	memcpy(pnbuf, cnp->cn_nameptr, cnp->cn_namelen);
    818       1.50   hannken 	pnbuf[cnp->cn_namelen] = '\0';
    819       1.50   hannken 
    820       1.22       chs 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    821       1.50   hannken 
    822       1.50   hannken 	error = union_do_lookup(dvp, &cn,
    823       1.56  dholland 	    (um->um_op == UNMNT_ABOVE ? cnp->cn_cred : um->um_cred), pnbuf);
    824        1.8   hannken 	if (error) {
    825       1.37   hannken 		VOP_UNLOCK(dvp);
    826       1.40  dholland 		PNBUF_PUT(pnbuf);
    827       1.50   hannken 		return error;
    828        1.1  jdolecek 	}
    829        1.1  jdolecek 
    830        1.1  jdolecek 	/*
    831        1.1  jdolecek 	 * policy: when creating the shadow directory in the
    832        1.1  jdolecek 	 * upper layer, create it owned by the user who did
    833        1.1  jdolecek 	 * the mount, group from parent directory, and mode
    834        1.1  jdolecek 	 * 777 modified by umask (ie mostly identical to the
    835        1.1  jdolecek 	 * mkdir syscall).  (jsp, kb)
    836        1.1  jdolecek 	 */
    837        1.1  jdolecek 
    838       1.35     pooka 	vattr_null(&va);
    839        1.1  jdolecek 	va.va_type = VDIR;
    840        1.1  jdolecek 	va.va_mode = um->um_cmode;
    841        1.1  jdolecek 
    842       1.65  dholland 	KASSERT(*vpp == NULL);
    843        1.1  jdolecek 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
    844       1.58   hannken 	VOP_UNLOCK(dvp);
    845       1.40  dholland 	PNBUF_PUT(pnbuf);
    846       1.50   hannken 	return error;
    847        1.1  jdolecek }
    848        1.1  jdolecek 
    849        1.1  jdolecek /*
    850        1.1  jdolecek  * Create a whiteout entry in the upper layer.
    851        1.1  jdolecek  *
    852        1.1  jdolecek  * (um) points to the union mount structure for access to the
    853        1.1  jdolecek  * the mounting process's credentials.
    854        1.1  jdolecek  * (dvp) is the directory in which to create the whiteout.
    855        1.1  jdolecek  * it is locked on entry and exit.
    856        1.1  jdolecek  * (cnp) is the componentname to be created.
    857       1.50   hannken  * (un) holds the path and its hash to be created.
    858        1.1  jdolecek  */
    859        1.1  jdolecek int
    860       1.32      matt union_mkwhiteout(struct union_mount *um, struct vnode *dvp,
    861       1.50   hannken 	struct componentname *cnp, struct union_node *un)
    862        1.1  jdolecek {
    863        1.1  jdolecek 	int error;
    864        1.1  jdolecek 	struct componentname cn;
    865        1.1  jdolecek 
    866       1.50   hannken 	error = union_do_lookup(dvp, &cn,
    867       1.50   hannken 	    (um->um_op == UNMNT_ABOVE ? cnp->cn_cred : um->um_cred),
    868       1.56  dholland 	    un->un_path);
    869       1.25   hannken 	if (error)
    870       1.50   hannken 		return error;
    871        1.1  jdolecek 
    872        1.1  jdolecek 	error = VOP_WHITEOUT(dvp, &cn, CREATE);
    873       1.50   hannken 	return error;
    874        1.1  jdolecek }
    875        1.1  jdolecek 
    876        1.1  jdolecek /*
    877        1.1  jdolecek  * union_vn_create: creates and opens a new shadow file
    878        1.1  jdolecek  * on the upper union layer.  this function is similar
    879        1.1  jdolecek  * in spirit to calling vn_open but it avoids calling namei().
    880        1.1  jdolecek  * the problem with calling namei is that a) it locks too many
    881        1.1  jdolecek  * things, and b) it doesn't start at the "right" directory,
    882       1.50   hannken  * whereas union_do_lookup is told where to start.
    883        1.1  jdolecek  */
    884        1.1  jdolecek int
    885       1.32      matt union_vn_create(struct vnode **vpp, struct union_node *un, struct lwp *l)
    886        1.1  jdolecek {
    887        1.1  jdolecek 	struct vnode *vp;
    888       1.19        ad 	kauth_cred_t cred = l->l_cred;
    889        1.1  jdolecek 	struct vattr vat;
    890        1.1  jdolecek 	struct vattr *vap = &vat;
    891        1.1  jdolecek 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
    892        1.1  jdolecek 	int error;
    893       1.15  christos 	int cmode = UN_FILEMODE & ~l->l_proc->p_cwdi->cwdi_cmask;
    894        1.1  jdolecek 	struct componentname cn;
    895        1.1  jdolecek 
    896        1.1  jdolecek 	*vpp = NULLVP;
    897        1.1  jdolecek 
    898       1.50   hannken 	vn_lock(un->un_dirvp, LK_EXCLUSIVE | LK_RETRY);
    899        1.1  jdolecek 
    900       1.50   hannken 	error = union_do_lookup(un->un_dirvp, &cn, l->l_cred,
    901       1.56  dholland 	    un->un_path);
    902       1.22       chs 	if (error) {
    903       1.37   hannken 		VOP_UNLOCK(un->un_dirvp);
    904       1.50   hannken 		return error;
    905        1.1  jdolecek 	}
    906        1.1  jdolecek 
    907        1.1  jdolecek 	/*
    908        1.1  jdolecek 	 * Good - there was no race to create the file
    909        1.1  jdolecek 	 * so go ahead and create it.  The permissions
    910        1.1  jdolecek 	 * on the file will be 0666 modified by the
    911        1.1  jdolecek 	 * current user's umask.  Access to the file, while
    912        1.1  jdolecek 	 * it is unioned, will require access to the top *and*
    913        1.1  jdolecek 	 * bottom files.  Access when not unioned will simply
    914        1.1  jdolecek 	 * require access to the top-level file.
    915        1.1  jdolecek 	 * TODO: confirm choice of access permissions.
    916        1.1  jdolecek 	 */
    917       1.35     pooka 	vattr_null(vap);
    918        1.1  jdolecek 	vap->va_type = VREG;
    919        1.1  jdolecek 	vap->va_mode = cmode;
    920       1.64  dholland 	vp = NULL;
    921       1.50   hannken 	error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap);
    922       1.66   hannken 	if (error) {
    923       1.66   hannken 		VOP_UNLOCK(un->un_dirvp);
    924       1.50   hannken 		return error;
    925       1.66   hannken 	}
    926        1.1  jdolecek 
    927       1.59   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    928       1.66   hannken 	VOP_UNLOCK(un->un_dirvp);
    929       1.50   hannken 	error = VOP_OPEN(vp, fmode, cred);
    930       1.50   hannken 	if (error) {
    931        1.1  jdolecek 		vput(vp);
    932       1.50   hannken 		return error;
    933        1.1  jdolecek 	}
    934        1.1  jdolecek 
    935        1.1  jdolecek 	vp->v_writecount++;
    936       1.76   hannken 	VOP_UNLOCK(vp);
    937        1.1  jdolecek 	*vpp = vp;
    938       1.50   hannken 	return 0;
    939        1.1  jdolecek }
    940        1.1  jdolecek 
    941        1.1  jdolecek int
    942       1.32      matt union_vn_close(struct vnode *vp, int fmode, kauth_cred_t cred, struct lwp *l)
    943        1.1  jdolecek {
    944        1.1  jdolecek 
    945        1.1  jdolecek 	if (fmode & FWRITE)
    946        1.1  jdolecek 		--vp->v_writecount;
    947       1.29     pooka 	return (VOP_CLOSE(vp, fmode, cred));
    948        1.1  jdolecek }
    949        1.1  jdolecek 
    950        1.1  jdolecek void
    951       1.32      matt union_removed_upper(struct union_node *un)
    952        1.1  jdolecek {
    953       1.53   hannken 	struct vnode *vp = UNIONTOV(un);
    954       1.38   hannken 
    955       1.53   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    956        1.1  jdolecek #if 1
    957        1.1  jdolecek 	/*
    958        1.1  jdolecek 	 * We do not set the uppervp to NULLVP here, because lowervp
    959        1.1  jdolecek 	 * may also be NULLVP, so this routine would end up creating
    960        1.1  jdolecek 	 * a bogus union node with no upper or lower VP (that causes
    961        1.1  jdolecek 	 * pain in many places that assume at least one VP exists).
    962        1.1  jdolecek 	 * Since we've removed this node from the cache hash chains,
    963        1.1  jdolecek 	 * it won't be found again.  When all current holders
    964        1.1  jdolecek 	 * release it, union_inactive() will vgone() it.
    965        1.1  jdolecek 	 */
    966        1.1  jdolecek 	union_diruncache(un);
    967        1.1  jdolecek #else
    968        1.1  jdolecek 	union_newupper(un, NULLVP);
    969        1.1  jdolecek #endif
    970        1.1  jdolecek 
    971       1.53   hannken 	VOP_UNLOCK(vp);
    972       1.38   hannken 
    973       1.54   hannken 	mutex_enter(&uhash_lock);
    974       1.53   hannken 	if (un->un_cflags & UN_CACHED) {
    975       1.53   hannken 		un->un_cflags &= ~UN_CACHED;
    976        1.1  jdolecek 		LIST_REMOVE(un, un_cache);
    977        1.1  jdolecek 	}
    978       1.54   hannken 	mutex_exit(&uhash_lock);
    979        1.1  jdolecek }
    980        1.1  jdolecek 
    981        1.1  jdolecek #if 0
    982        1.1  jdolecek struct vnode *
    983       1.32      matt union_lowervp(struct vnode *vp)
    984        1.1  jdolecek {
    985        1.1  jdolecek 	struct union_node *un = VTOUNION(vp);
    986        1.1  jdolecek 
    987        1.1  jdolecek 	if ((un->un_lowervp != NULLVP) &&
    988        1.1  jdolecek 	    (vp->v_type == un->un_lowervp->v_type)) {
    989       1.72  riastrad 		if (vget(un->un_lowervp, 0, true /* wait */) == 0)
    990        1.1  jdolecek 			return (un->un_lowervp);
    991        1.1  jdolecek 	}
    992        1.1  jdolecek 
    993        1.1  jdolecek 	return (NULLVP);
    994        1.1  jdolecek }
    995        1.1  jdolecek #endif
    996        1.1  jdolecek 
    997        1.1  jdolecek /*
    998        1.1  jdolecek  * determine whether a whiteout is needed
    999        1.1  jdolecek  * during a remove/rmdir operation.
   1000        1.1  jdolecek  */
   1001        1.1  jdolecek int
   1002       1.32      matt union_dowhiteout(struct union_node *un, kauth_cred_t cred)
   1003        1.1  jdolecek {
   1004        1.1  jdolecek 	struct vattr va;
   1005        1.1  jdolecek 
   1006        1.1  jdolecek 	if (un->un_lowervp != NULLVP)
   1007        1.1  jdolecek 		return (1);
   1008        1.1  jdolecek 
   1009       1.29     pooka 	if (VOP_GETATTR(un->un_uppervp, &va, cred) == 0 &&
   1010        1.1  jdolecek 	    (va.va_flags & OPAQUE))
   1011        1.1  jdolecek 		return (1);
   1012        1.1  jdolecek 
   1013        1.1  jdolecek 	return (0);
   1014        1.1  jdolecek }
   1015        1.1  jdolecek 
   1016        1.1  jdolecek static void
   1017       1.32      matt union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp)
   1018        1.1  jdolecek {
   1019        1.1  jdolecek 	struct union_node *un;
   1020        1.1  jdolecek 
   1021        1.1  jdolecek 	if (vp->v_op != union_vnodeop_p) {
   1022        1.1  jdolecek 		if (vppp) {
   1023       1.35     pooka 			vref(vp);
   1024        1.1  jdolecek 			*(*vppp)++ = vp;
   1025        1.1  jdolecek 			if (--(*cntp) == 0)
   1026        1.1  jdolecek 				panic("union: dircache table too small");
   1027        1.1  jdolecek 		} else {
   1028        1.1  jdolecek 			(*cntp)++;
   1029        1.1  jdolecek 		}
   1030        1.1  jdolecek 
   1031        1.1  jdolecek 		return;
   1032        1.1  jdolecek 	}
   1033        1.1  jdolecek 
   1034        1.1  jdolecek 	un = VTOUNION(vp);
   1035        1.1  jdolecek 	if (un->un_uppervp != NULLVP)
   1036        1.1  jdolecek 		union_dircache_r(un->un_uppervp, vppp, cntp);
   1037        1.1  jdolecek 	if (un->un_lowervp != NULLVP)
   1038        1.1  jdolecek 		union_dircache_r(un->un_lowervp, vppp, cntp);
   1039        1.1  jdolecek }
   1040        1.1  jdolecek 
   1041        1.1  jdolecek struct vnode *
   1042       1.21  christos union_dircache(struct vnode *vp, struct lwp *l)
   1043        1.1  jdolecek {
   1044        1.1  jdolecek 	int cnt;
   1045        1.1  jdolecek 	struct vnode *nvp = NULLVP;
   1046        1.1  jdolecek 	struct vnode **vpp;
   1047        1.1  jdolecek 	struct vnode **dircache;
   1048        1.1  jdolecek 	int error;
   1049        1.1  jdolecek 
   1050        1.1  jdolecek 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1051        1.1  jdolecek 	dircache = VTOUNION(vp)->un_dircache;
   1052        1.1  jdolecek 
   1053        1.1  jdolecek 	nvp = NULLVP;
   1054        1.1  jdolecek 
   1055        1.1  jdolecek 	if (dircache == 0) {
   1056        1.1  jdolecek 		cnt = 0;
   1057        1.1  jdolecek 		union_dircache_r(vp, 0, &cnt);
   1058        1.1  jdolecek 		cnt++;
   1059        1.1  jdolecek 		dircache = (struct vnode **)
   1060        1.1  jdolecek 				malloc(cnt * sizeof(struct vnode *),
   1061        1.1  jdolecek 					M_TEMP, M_WAITOK);
   1062        1.1  jdolecek 		vpp = dircache;
   1063        1.1  jdolecek 		union_dircache_r(vp, &vpp, &cnt);
   1064        1.1  jdolecek 		VTOUNION(vp)->un_dircache = dircache;
   1065        1.1  jdolecek 		*vpp = NULLVP;
   1066        1.1  jdolecek 		vpp = dircache + 1;
   1067        1.1  jdolecek 	} else {
   1068        1.1  jdolecek 		vpp = dircache;
   1069        1.1  jdolecek 		do {
   1070        1.1  jdolecek 			if (*vpp++ == VTOUNION(vp)->un_uppervp)
   1071        1.1  jdolecek 				break;
   1072        1.1  jdolecek 		} while (*vpp != NULLVP);
   1073        1.1  jdolecek 	}
   1074        1.1  jdolecek 
   1075        1.1  jdolecek 	if (*vpp == NULLVP)
   1076        1.1  jdolecek 		goto out;
   1077        1.1  jdolecek 
   1078       1.35     pooka 	vref(*vpp);
   1079        1.5   thorpej 	error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0);
   1080        1.1  jdolecek 	if (!error) {
   1081       1.63   hannken 		vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
   1082        1.1  jdolecek 		VTOUNION(vp)->un_dircache = 0;
   1083        1.1  jdolecek 		VTOUNION(nvp)->un_dircache = dircache;
   1084        1.1  jdolecek 	}
   1085        1.1  jdolecek 
   1086        1.1  jdolecek out:
   1087       1.37   hannken 	VOP_UNLOCK(vp);
   1088        1.1  jdolecek 	return (nvp);
   1089        1.1  jdolecek }
   1090        1.1  jdolecek 
   1091        1.1  jdolecek void
   1092       1.32      matt union_diruncache(struct union_node *un)
   1093        1.1  jdolecek {
   1094        1.1  jdolecek 	struct vnode **vpp;
   1095        1.1  jdolecek 
   1096       1.53   hannken 	KASSERT(VOP_ISLOCKED(UNIONTOV(un)) == LK_EXCLUSIVE);
   1097        1.1  jdolecek 	if (un->un_dircache != 0) {
   1098        1.1  jdolecek 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
   1099        1.1  jdolecek 			vrele(*vpp);
   1100        1.1  jdolecek 		free(un->un_dircache, M_TEMP);
   1101        1.1  jdolecek 		un->un_dircache = 0;
   1102        1.1  jdolecek 	}
   1103        1.2  jdolecek }
   1104        1.2  jdolecek 
   1105        1.2  jdolecek /*
   1106       1.44   hannken  * Check whether node can rmdir (check empty).
   1107       1.44   hannken  */
   1108       1.44   hannken int
   1109       1.44   hannken union_check_rmdir(struct union_node *un, kauth_cred_t cred)
   1110       1.44   hannken {
   1111       1.44   hannken 	int dirlen, eofflag, error;
   1112       1.44   hannken 	char *dirbuf;
   1113       1.44   hannken 	struct vattr va;
   1114       1.44   hannken 	struct vnode *tvp;
   1115       1.44   hannken 	struct dirent *dp, *edp;
   1116       1.44   hannken 	struct componentname cn;
   1117       1.44   hannken 	struct iovec aiov;
   1118       1.44   hannken 	struct uio auio;
   1119       1.44   hannken 
   1120       1.44   hannken 	KASSERT(un->un_uppervp != NULL);
   1121       1.44   hannken 
   1122       1.44   hannken 	/* Check upper for being opaque. */
   1123       1.44   hannken 	KASSERT(VOP_ISLOCKED(un->un_uppervp));
   1124       1.44   hannken 	error = VOP_GETATTR(un->un_uppervp, &va, cred);
   1125       1.44   hannken 	if (error || (va.va_flags & OPAQUE))
   1126       1.44   hannken 		return error;
   1127       1.44   hannken 
   1128       1.44   hannken 	if (un->un_lowervp == NULL)
   1129       1.44   hannken 		return 0;
   1130       1.44   hannken 
   1131       1.44   hannken 	/* Check lower for being empty. */
   1132       1.45   hannken 	vn_lock(un->un_lowervp, LK_SHARED | LK_RETRY);
   1133       1.44   hannken 	error = VOP_GETATTR(un->un_lowervp, &va, cred);
   1134       1.44   hannken 	if (error) {
   1135       1.44   hannken 		VOP_UNLOCK(un->un_lowervp);
   1136       1.44   hannken 		return error;
   1137       1.44   hannken 	}
   1138       1.44   hannken 	dirlen = va.va_blocksize;
   1139       1.44   hannken 	dirbuf = kmem_alloc(dirlen, KM_SLEEP);
   1140       1.44   hannken 	/* error = 0; */
   1141       1.44   hannken 	eofflag = 0;
   1142       1.44   hannken 	auio.uio_offset = 0;
   1143       1.44   hannken 	do {
   1144       1.44   hannken 		aiov.iov_len = dirlen;
   1145       1.44   hannken 		aiov.iov_base = dirbuf;
   1146       1.44   hannken 		auio.uio_iov = &aiov;
   1147       1.44   hannken 		auio.uio_iovcnt = 1;
   1148       1.44   hannken 		auio.uio_resid = aiov.iov_len;
   1149       1.44   hannken 		auio.uio_rw = UIO_READ;
   1150       1.44   hannken 		UIO_SETUP_SYSSPACE(&auio);
   1151       1.44   hannken 		error = VOP_READDIR(un->un_lowervp, &auio, cred, &eofflag,
   1152       1.44   hannken 		    NULL, NULL);
   1153       1.44   hannken 		if (error)
   1154       1.44   hannken 			break;
   1155       1.44   hannken 		edp = (struct dirent *)&dirbuf[dirlen - auio.uio_resid];
   1156       1.44   hannken 		for (dp = (struct dirent *)dirbuf;
   1157       1.44   hannken 		    error == 0 && dp < edp;
   1158       1.44   hannken 		    dp = (struct dirent *)((char *)dp + dp->d_reclen)) {
   1159       1.44   hannken 			if (dp->d_reclen == 0) {
   1160       1.44   hannken 				error = ENOTEMPTY;
   1161       1.44   hannken 				break;
   1162       1.44   hannken 			}
   1163       1.44   hannken 			if (dp->d_type == DT_WHT ||
   1164       1.44   hannken 			    (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
   1165       1.44   hannken 			    (dp->d_namlen == 2 && !memcmp(dp->d_name, "..", 2)))
   1166       1.44   hannken 				continue;
   1167       1.44   hannken 			/* Check for presence in the upper layer. */
   1168       1.44   hannken 			cn.cn_nameiop = LOOKUP;
   1169       1.44   hannken 			cn.cn_flags = ISLASTCN | RDONLY;
   1170       1.44   hannken 			cn.cn_cred = cred;
   1171       1.44   hannken 			cn.cn_nameptr = dp->d_name;
   1172       1.44   hannken 			cn.cn_namelen = dp->d_namlen;
   1173       1.44   hannken 			error = VOP_LOOKUP(un->un_uppervp, &tvp, &cn);
   1174       1.44   hannken 			if (error == ENOENT && (cn.cn_flags & ISWHITEOUT)) {
   1175       1.44   hannken 				error = 0;
   1176       1.44   hannken 				continue;
   1177       1.44   hannken 			}
   1178       1.44   hannken 			if (error == 0)
   1179       1.60   hannken 				vrele(tvp);
   1180       1.44   hannken 			error = ENOTEMPTY;
   1181       1.44   hannken 		}
   1182       1.44   hannken 	} while (error == 0 && !eofflag);
   1183       1.44   hannken 	kmem_free(dirbuf, dirlen);
   1184       1.44   hannken 	VOP_UNLOCK(un->un_lowervp);
   1185       1.44   hannken 
   1186       1.44   hannken 	return error;
   1187       1.44   hannken }
   1188       1.44   hannken 
   1189       1.44   hannken /*
   1190        1.2  jdolecek  * This hook is called from vn_readdir() to switch to lower directory
   1191        1.2  jdolecek  * entry after the upper directory is read.
   1192        1.2  jdolecek  */
   1193        1.2  jdolecek int
   1194       1.15  christos union_readdirhook(struct vnode **vpp, struct file *fp, struct lwp *l)
   1195        1.2  jdolecek {
   1196        1.2  jdolecek 	struct vnode *vp = *vpp, *lvp;
   1197        1.2  jdolecek 	struct vattr va;
   1198        1.2  jdolecek 	int error;
   1199        1.2  jdolecek 
   1200        1.2  jdolecek 	if (vp->v_op != union_vnodeop_p)
   1201        1.2  jdolecek 		return (0);
   1202        1.2  jdolecek 
   1203        1.2  jdolecek 	/*
   1204        1.2  jdolecek 	 * If the directory is opaque,
   1205        1.2  jdolecek 	 * then don't show lower entries
   1206        1.2  jdolecek 	 */
   1207       1.53   hannken 	vn_lock(vp, LK_SHARED | LK_RETRY);
   1208       1.29     pooka 	error = VOP_GETATTR(vp, &va, fp->f_cred);
   1209       1.53   hannken 	VOP_UNLOCK(vp);
   1210       1.51   hannken 	if (error || (va.va_flags & OPAQUE))
   1211       1.51   hannken 		return error;
   1212       1.51   hannken 
   1213       1.51   hannken 	if ((lvp = union_dircache(vp, l)) == NULLVP)
   1214       1.51   hannken 		return (0);
   1215       1.12     perry 
   1216       1.29     pooka 	error = VOP_OPEN(lvp, FREAD, fp->f_cred);
   1217        1.2  jdolecek 	if (error) {
   1218        1.2  jdolecek 		vput(lvp);
   1219        1.2  jdolecek 		return (error);
   1220        1.2  jdolecek 	}
   1221       1.37   hannken 	VOP_UNLOCK(lvp);
   1222       1.67      matt 	fp->f_vnode = lvp;
   1223        1.2  jdolecek 	fp->f_offset = 0;
   1224       1.33        ad 	error = vn_close(vp, FREAD, fp->f_cred);
   1225        1.2  jdolecek 	if (error)
   1226        1.2  jdolecek 		return (error);
   1227        1.2  jdolecek 	*vpp = lvp;
   1228        1.2  jdolecek 	return (0);
   1229        1.1  jdolecek }
   1230