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