Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.8.2.3
      1  1.8.2.3     skrll /*	$NetBSD: union_vfsops.c,v 1.8.2.3 2004/08/24 17:57:37 skrll Exp $	*/
      2      1.1  jdolecek 
      3      1.1  jdolecek /*
      4      1.1  jdolecek  * Copyright (c) 1994 The Regents of the University of California.
      5  1.8.2.2     skrll  * All rights reserved.
      6  1.8.2.2     skrll  *
      7  1.8.2.2     skrll  * This code is derived from software donated to Berkeley by
      8  1.8.2.2     skrll  * Jan-Simon Pendry.
      9  1.8.2.2     skrll  *
     10  1.8.2.2     skrll  * Redistribution and use in source and binary forms, with or without
     11  1.8.2.2     skrll  * modification, are permitted provided that the following conditions
     12  1.8.2.2     skrll  * are met:
     13  1.8.2.2     skrll  * 1. Redistributions of source code must retain the above copyright
     14  1.8.2.2     skrll  *    notice, this list of conditions and the following disclaimer.
     15  1.8.2.2     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8.2.2     skrll  *    notice, this list of conditions and the following disclaimer in the
     17  1.8.2.2     skrll  *    documentation and/or other materials provided with the distribution.
     18  1.8.2.2     skrll  * 3. Neither the name of the University nor the names of its contributors
     19  1.8.2.2     skrll  *    may be used to endorse or promote products derived from this software
     20  1.8.2.2     skrll  *    without specific prior written permission.
     21  1.8.2.2     skrll  *
     22  1.8.2.2     skrll  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.8.2.2     skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.8.2.2     skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.8.2.2     skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.8.2.2     skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.8.2.2     skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.8.2.2     skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.8.2.2     skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.8.2.2     skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.8.2.2     skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.8.2.2     skrll  * SUCH DAMAGE.
     33  1.8.2.2     skrll  *
     34  1.8.2.2     skrll  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
     35  1.8.2.2     skrll  */
     36  1.8.2.2     skrll 
     37  1.8.2.2     skrll /*
     38      1.1  jdolecek  * Copyright (c) 1994 Jan-Simon Pendry.
     39      1.1  jdolecek  * All rights reserved.
     40      1.1  jdolecek  *
     41      1.1  jdolecek  * This code is derived from software donated to Berkeley by
     42      1.1  jdolecek  * Jan-Simon Pendry.
     43      1.1  jdolecek  *
     44      1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     45      1.1  jdolecek  * modification, are permitted provided that the following conditions
     46      1.1  jdolecek  * are met:
     47      1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     48      1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     49      1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     50      1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     51      1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     52      1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     53      1.1  jdolecek  *    must display the following acknowledgement:
     54      1.1  jdolecek  *	This product includes software developed by the University of
     55      1.1  jdolecek  *	California, Berkeley and its contributors.
     56      1.1  jdolecek  * 4. Neither the name of the University nor the names of its contributors
     57      1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     58      1.1  jdolecek  *    without specific prior written permission.
     59      1.1  jdolecek  *
     60      1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61      1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62      1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63      1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64      1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65      1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66      1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67      1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68      1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69      1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70      1.1  jdolecek  * SUCH DAMAGE.
     71      1.1  jdolecek  *
     72      1.1  jdolecek  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
     73      1.1  jdolecek  */
     74      1.1  jdolecek 
     75      1.1  jdolecek /*
     76      1.1  jdolecek  * Union Layer
     77      1.1  jdolecek  */
     78      1.1  jdolecek 
     79      1.1  jdolecek #include <sys/cdefs.h>
     80  1.8.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.8.2.3 2004/08/24 17:57:37 skrll Exp $");
     81      1.1  jdolecek 
     82      1.1  jdolecek #include <sys/param.h>
     83      1.1  jdolecek #include <sys/systm.h>
     84  1.8.2.2     skrll #include <sys/sysctl.h>
     85      1.1  jdolecek #include <sys/time.h>
     86      1.1  jdolecek #include <sys/proc.h>
     87      1.1  jdolecek #include <sys/vnode.h>
     88      1.1  jdolecek #include <sys/mount.h>
     89      1.1  jdolecek #include <sys/namei.h>
     90      1.1  jdolecek #include <sys/malloc.h>
     91      1.1  jdolecek #include <sys/filedesc.h>
     92      1.1  jdolecek #include <sys/queue.h>
     93      1.1  jdolecek #include <sys/stat.h>
     94      1.1  jdolecek 
     95      1.1  jdolecek #include <fs/union/union.h>
     96      1.1  jdolecek 
     97      1.1  jdolecek int union_mount __P((struct mount *, const char *, void *, struct nameidata *,
     98  1.8.2.1  wrstuden 		     struct lwp *));
     99  1.8.2.1  wrstuden int union_start __P((struct mount *, int, struct lwp *));
    100  1.8.2.1  wrstuden int union_unmount __P((struct mount *, int, struct lwp *));
    101  1.8.2.3     skrll int union_root __P((struct mount *, struct vnode **));
    102  1.8.2.2     skrll int union_quotactl __P((struct mount *, int, uid_t, void *, struct lwp *));
    103  1.8.2.2     skrll int union_statvfs __P((struct mount *, struct statvfs *, struct lwp *));
    104  1.8.2.1  wrstuden int union_sync __P((struct mount *, int, struct ucred *, struct lwp *));
    105  1.8.2.3     skrll int union_vget __P((struct mount *, ino_t, struct vnode **));
    106  1.8.2.3     skrll int union_fhtovp __P((struct mount *, struct fid *, struct vnode **));
    107      1.1  jdolecek int union_checkexp __P((struct mount *, struct mbuf *, int *,
    108      1.1  jdolecek 		      struct ucred **));
    109      1.1  jdolecek int union_vptofh __P((struct vnode *, struct fid *));
    110      1.1  jdolecek 
    111      1.1  jdolecek /*
    112      1.1  jdolecek  * Mount union filesystem
    113      1.1  jdolecek  */
    114      1.1  jdolecek int
    115  1.8.2.1  wrstuden union_mount(mp, path, data, ndp, l)
    116      1.1  jdolecek 	struct mount *mp;
    117      1.1  jdolecek 	const char *path;
    118      1.1  jdolecek 	void *data;
    119      1.1  jdolecek 	struct nameidata *ndp;
    120  1.8.2.1  wrstuden 	struct lwp *l;
    121      1.1  jdolecek {
    122      1.1  jdolecek 	int error = 0;
    123      1.1  jdolecek 	struct union_args args;
    124      1.1  jdolecek 	struct vnode *lowerrootvp = NULLVP;
    125      1.1  jdolecek 	struct vnode *upperrootvp = NULLVP;
    126      1.1  jdolecek 	struct union_mount *um = 0;
    127      1.1  jdolecek 	struct ucred *cred = 0;
    128      1.1  jdolecek 	char *cp;
    129      1.1  jdolecek 	int len;
    130      1.1  jdolecek 	size_t size;
    131      1.1  jdolecek 
    132      1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    133      1.1  jdolecek 	printf("union_mount(mp = %p)\n", mp);
    134      1.1  jdolecek #endif
    135      1.1  jdolecek 
    136      1.1  jdolecek 	if (mp->mnt_flag & MNT_GETARGS) {
    137      1.1  jdolecek 		um = MOUNTTOUNIONMOUNT(mp);
    138      1.1  jdolecek 		if (um == NULL)
    139      1.1  jdolecek 			return EIO;
    140      1.1  jdolecek 		args.target = NULL;
    141      1.1  jdolecek 		args.mntflags = um->um_op;
    142      1.1  jdolecek 		return copyout(&args, data, sizeof(args));
    143      1.1  jdolecek 	}
    144      1.1  jdolecek 	/*
    145      1.1  jdolecek 	 * Update is a no-op
    146      1.1  jdolecek 	 */
    147      1.1  jdolecek 	if (mp->mnt_flag & MNT_UPDATE) {
    148      1.1  jdolecek 		/*
    149      1.1  jdolecek 		 * Need to provide.
    150      1.1  jdolecek 		 * 1. a way to convert between rdonly and rdwr mounts.
    151      1.1  jdolecek 		 * 2. support for nfs exports.
    152      1.1  jdolecek 		 */
    153      1.1  jdolecek 		error = EOPNOTSUPP;
    154      1.1  jdolecek 		goto bad;
    155      1.1  jdolecek 	}
    156      1.1  jdolecek 
    157      1.1  jdolecek 	/*
    158      1.1  jdolecek 	 * Get argument
    159      1.1  jdolecek 	 */
    160  1.8.2.2     skrll 	error = copyin(data, &args, sizeof(struct union_args));
    161      1.1  jdolecek 	if (error)
    162      1.1  jdolecek 		goto bad;
    163      1.1  jdolecek 
    164      1.1  jdolecek 	lowerrootvp = mp->mnt_vnodecovered;
    165      1.1  jdolecek 	VREF(lowerrootvp);
    166      1.1  jdolecek 
    167      1.1  jdolecek 	/*
    168      1.1  jdolecek 	 * Find upper node.
    169      1.1  jdolecek 	 */
    170      1.1  jdolecek 	NDINIT(ndp, LOOKUP, FOLLOW,
    171  1.8.2.1  wrstuden 	       UIO_USERSPACE, args.target, l);
    172      1.1  jdolecek 
    173      1.1  jdolecek 	if ((error = namei(ndp)) != 0)
    174      1.1  jdolecek 		goto bad;
    175      1.1  jdolecek 
    176      1.1  jdolecek 	upperrootvp = ndp->ni_vp;
    177      1.1  jdolecek 
    178      1.1  jdolecek 	if (upperrootvp->v_type != VDIR) {
    179      1.1  jdolecek 		error = EINVAL;
    180      1.1  jdolecek 		goto bad;
    181      1.1  jdolecek 	}
    182      1.1  jdolecek 
    183      1.1  jdolecek 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
    184      1.1  jdolecek 				M_UFSMNT, M_WAITOK);	/* XXX */
    185      1.1  jdolecek 
    186      1.1  jdolecek 	/*
    187      1.1  jdolecek 	 * Keep a held reference to the target vnodes.
    188      1.1  jdolecek 	 * They are vrele'd in union_unmount.
    189      1.1  jdolecek 	 *
    190      1.1  jdolecek 	 * Depending on the _BELOW flag, the filesystems are
    191      1.1  jdolecek 	 * viewed in a different order.  In effect, this is the
    192      1.1  jdolecek 	 * same as providing a mount under option to the mount syscall.
    193      1.1  jdolecek 	 */
    194      1.1  jdolecek 
    195      1.1  jdolecek 	um->um_op = args.mntflags & UNMNT_OPMASK;
    196      1.1  jdolecek 	switch (um->um_op) {
    197      1.1  jdolecek 	case UNMNT_ABOVE:
    198      1.1  jdolecek 		um->um_lowervp = lowerrootvp;
    199      1.1  jdolecek 		um->um_uppervp = upperrootvp;
    200      1.1  jdolecek 		break;
    201      1.1  jdolecek 
    202      1.1  jdolecek 	case UNMNT_BELOW:
    203      1.1  jdolecek 		um->um_lowervp = upperrootvp;
    204      1.1  jdolecek 		um->um_uppervp = lowerrootvp;
    205      1.1  jdolecek 		break;
    206      1.1  jdolecek 
    207      1.1  jdolecek 	case UNMNT_REPLACE:
    208      1.1  jdolecek 		vrele(lowerrootvp);
    209      1.1  jdolecek 		lowerrootvp = NULLVP;
    210      1.1  jdolecek 		um->um_uppervp = upperrootvp;
    211      1.1  jdolecek 		um->um_lowervp = lowerrootvp;
    212      1.1  jdolecek 		break;
    213      1.1  jdolecek 
    214      1.1  jdolecek 	default:
    215      1.1  jdolecek 		error = EINVAL;
    216      1.1  jdolecek 		goto bad;
    217      1.1  jdolecek 	}
    218      1.1  jdolecek 
    219      1.1  jdolecek 	/*
    220      1.1  jdolecek 	 * Unless the mount is readonly, ensure that the top layer
    221      1.1  jdolecek 	 * supports whiteout operations
    222      1.1  jdolecek 	 */
    223      1.1  jdolecek 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    224      1.1  jdolecek 		error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
    225      1.1  jdolecek 		if (error)
    226      1.1  jdolecek 			goto bad;
    227      1.1  jdolecek 	}
    228      1.1  jdolecek 
    229  1.8.2.1  wrstuden 	um->um_cred = l->l_proc->p_ucred;
    230      1.1  jdolecek 	crhold(um->um_cred);
    231  1.8.2.1  wrstuden 	um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
    232      1.1  jdolecek 
    233      1.1  jdolecek 	/*
    234      1.1  jdolecek 	 * Depending on what you think the MNT_LOCAL flag might mean,
    235      1.1  jdolecek 	 * you may want the && to be || on the conditional below.
    236      1.1  jdolecek 	 * At the moment it has been defined that the filesystem is
    237      1.1  jdolecek 	 * only local if it is all local, ie the MNT_LOCAL flag implies
    238      1.1  jdolecek 	 * that the entire namespace is local.  If you think the MNT_LOCAL
    239      1.1  jdolecek 	 * flag implies that some of the files might be stored locally
    240      1.1  jdolecek 	 * then you will want to change the conditional.
    241      1.1  jdolecek 	 */
    242      1.1  jdolecek 	if (um->um_op == UNMNT_ABOVE) {
    243      1.1  jdolecek 		if (((um->um_lowervp == NULLVP) ||
    244      1.1  jdolecek 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
    245      1.1  jdolecek 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
    246      1.1  jdolecek 			mp->mnt_flag |= MNT_LOCAL;
    247      1.1  jdolecek 	}
    248      1.1  jdolecek 
    249      1.1  jdolecek 	/*
    250      1.1  jdolecek 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
    251      1.1  jdolecek 	 * of lookup() which explicitly checks the flag, rather than asking
    252      1.1  jdolecek 	 * the filesystem for it's own opinion.  This means, that an update
    253      1.1  jdolecek 	 * mount of the underlying filesystem to go from rdonly to rdwr
    254      1.1  jdolecek 	 * will leave the unioned view as read-only.
    255      1.1  jdolecek 	 */
    256      1.1  jdolecek 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
    257      1.1  jdolecek 
    258      1.1  jdolecek 	mp->mnt_data = um;
    259  1.8.2.2     skrll 	mp->mnt_leaf = um->um_uppervp->v_mount->mnt_leaf;
    260      1.1  jdolecek 	vfs_getnewfsid(mp);
    261      1.1  jdolecek 
    262  1.8.2.2     skrll 	error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    263  1.8.2.1  wrstuden 	    mp, l);
    264      1.4  christos 	if (error)
    265      1.4  christos 		goto bad;
    266      1.1  jdolecek 
    267      1.1  jdolecek 	switch (um->um_op) {
    268      1.1  jdolecek 	case UNMNT_ABOVE:
    269      1.1  jdolecek 		cp = "<above>:";
    270      1.1  jdolecek 		break;
    271      1.1  jdolecek 	case UNMNT_BELOW:
    272      1.1  jdolecek 		cp = "<below>:";
    273      1.1  jdolecek 		break;
    274      1.1  jdolecek 	case UNMNT_REPLACE:
    275      1.1  jdolecek 		cp = "";
    276      1.1  jdolecek 		break;
    277      1.1  jdolecek 	default:
    278      1.1  jdolecek 		cp = "<invalid>:";
    279      1.1  jdolecek #ifdef DIAGNOSTIC
    280      1.1  jdolecek 		panic("union_mount: bad um_op");
    281      1.1  jdolecek #endif
    282      1.1  jdolecek 		break;
    283      1.1  jdolecek 	}
    284      1.1  jdolecek 	len = strlen(cp);
    285      1.1  jdolecek 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    286      1.1  jdolecek 
    287      1.1  jdolecek 	cp = mp->mnt_stat.f_mntfromname + len;
    288      1.1  jdolecek 	len = MNAMELEN - len;
    289      1.1  jdolecek 
    290      1.1  jdolecek 	(void) copyinstr(args.target, cp, len - 1, &size);
    291      1.1  jdolecek 	memset(cp + size, 0, len - size);
    292      1.1  jdolecek 
    293      1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    294      1.1  jdolecek 	printf("union_mount: from %s, on %s\n",
    295      1.1  jdolecek 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    296      1.1  jdolecek #endif
    297      1.2  jdolecek 
    298      1.2  jdolecek 	/* Setup the readdir hook if it's not set already */
    299      1.2  jdolecek 	if (!vn_union_readdir_hook)
    300      1.2  jdolecek 		vn_union_readdir_hook = union_readdirhook;
    301      1.2  jdolecek 
    302      1.1  jdolecek 	return (0);
    303      1.1  jdolecek 
    304      1.1  jdolecek bad:
    305      1.1  jdolecek 	if (um)
    306      1.1  jdolecek 		free(um, M_UFSMNT);
    307      1.1  jdolecek 	if (cred)
    308      1.1  jdolecek 		crfree(cred);
    309      1.1  jdolecek 	if (upperrootvp)
    310      1.1  jdolecek 		vrele(upperrootvp);
    311      1.1  jdolecek 	if (lowerrootvp)
    312      1.1  jdolecek 		vrele(lowerrootvp);
    313      1.1  jdolecek 	return (error);
    314      1.1  jdolecek }
    315      1.1  jdolecek 
    316      1.1  jdolecek /*
    317      1.1  jdolecek  * VFS start.  Nothing needed here - the start routine
    318      1.1  jdolecek  * on the underlying filesystem(s) will have been called
    319      1.1  jdolecek  * when that filesystem was mounted.
    320      1.1  jdolecek  */
    321      1.1  jdolecek  /*ARGSUSED*/
    322      1.1  jdolecek int
    323  1.8.2.1  wrstuden union_start(mp, flags, l)
    324      1.1  jdolecek 	struct mount *mp;
    325      1.1  jdolecek 	int flags;
    326  1.8.2.1  wrstuden 	struct lwp *l;
    327      1.1  jdolecek {
    328      1.1  jdolecek 
    329      1.1  jdolecek 	return (0);
    330      1.1  jdolecek }
    331      1.1  jdolecek 
    332      1.1  jdolecek /*
    333      1.1  jdolecek  * Free reference to union layer
    334      1.1  jdolecek  */
    335      1.1  jdolecek int
    336  1.8.2.1  wrstuden union_unmount(mp, mntflags, l)
    337      1.1  jdolecek 	struct mount *mp;
    338      1.1  jdolecek 	int mntflags;
    339  1.8.2.1  wrstuden 	struct lwp *l;
    340      1.1  jdolecek {
    341      1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    342      1.1  jdolecek 	struct vnode *um_rootvp;
    343      1.1  jdolecek 	int error;
    344      1.1  jdolecek 	int freeing;
    345      1.1  jdolecek 
    346      1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    347      1.1  jdolecek 	printf("union_unmount(mp = %p)\n", mp);
    348      1.1  jdolecek #endif
    349      1.1  jdolecek 
    350  1.8.2.3     skrll 	if ((error = union_root(mp, &um_rootvp)) != 0)
    351      1.1  jdolecek 		return (error);
    352      1.1  jdolecek 
    353      1.1  jdolecek 	/*
    354      1.1  jdolecek 	 * Keep flushing vnodes from the mount list.
    355      1.1  jdolecek 	 * This is needed because of the un_pvp held
    356      1.1  jdolecek 	 * reference to the parent vnode.
    357      1.1  jdolecek 	 * If more vnodes have been freed on a given pass,
    358      1.1  jdolecek 	 * the try again.  The loop will iterate at most
    359      1.1  jdolecek 	 * (d) times, where (d) is the maximum tree depth
    360      1.1  jdolecek 	 * in the filesystem.
    361      1.1  jdolecek 	 */
    362      1.1  jdolecek 	for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
    363      1.1  jdolecek 		struct vnode *vp;
    364      1.1  jdolecek 		int n;
    365      1.1  jdolecek 
    366      1.1  jdolecek 		/* count #vnodes held on mount list */
    367      1.1  jdolecek 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
    368      1.1  jdolecek 				vp != NULLVP;
    369      1.1  jdolecek 				vp = vp->v_mntvnodes.le_next)
    370      1.1  jdolecek 			n++;
    371      1.1  jdolecek 
    372      1.1  jdolecek 		/* if this is unchanged then stop */
    373      1.1  jdolecek 		if (n == freeing)
    374      1.1  jdolecek 			break;
    375      1.1  jdolecek 
    376      1.1  jdolecek 		/* otherwise try once more time */
    377      1.1  jdolecek 		freeing = n;
    378      1.1  jdolecek 	}
    379      1.1  jdolecek 
    380      1.1  jdolecek 	/*
    381      1.1  jdolecek 	 * Ok, now that we've tried doing it gently, get out the hammer.
    382      1.1  jdolecek 	 */
    383      1.1  jdolecek 
    384      1.1  jdolecek 	if (mntflags & MNT_FORCE)
    385      1.1  jdolecek 		vflush(mp, um_rootvp, FORCECLOSE);
    386      1.1  jdolecek 
    387      1.1  jdolecek 
    388      1.1  jdolecek 	/* At this point the root vnode should have a single reference */
    389      1.1  jdolecek 	if (um_rootvp->v_usecount > 1) {
    390      1.1  jdolecek 		vput(um_rootvp);
    391      1.1  jdolecek 		return (EBUSY);
    392      1.1  jdolecek 	}
    393      1.1  jdolecek 
    394      1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    395      1.1  jdolecek 	vprint("union root", um_rootvp);
    396      1.1  jdolecek #endif
    397      1.1  jdolecek 	/*
    398      1.1  jdolecek 	 * Discard references to upper and lower target vnodes.
    399      1.1  jdolecek 	 */
    400      1.1  jdolecek 	if (um->um_lowervp)
    401      1.1  jdolecek 		vrele(um->um_lowervp);
    402      1.1  jdolecek 	vrele(um->um_uppervp);
    403      1.1  jdolecek 	crfree(um->um_cred);
    404      1.1  jdolecek 	/*
    405      1.1  jdolecek 	 * Release reference on underlying root vnode
    406      1.1  jdolecek 	 */
    407      1.1  jdolecek 	vput(um_rootvp);
    408      1.1  jdolecek 	/*
    409      1.1  jdolecek 	 * And blow it away for future re-use
    410      1.1  jdolecek 	 */
    411      1.1  jdolecek 	vgone(um_rootvp);
    412      1.1  jdolecek 	/*
    413      1.1  jdolecek 	 * Finally, throw away the union_mount structure
    414      1.1  jdolecek 	 */
    415      1.1  jdolecek 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    416      1.1  jdolecek 	mp->mnt_data = 0;
    417      1.1  jdolecek 	return (0);
    418      1.1  jdolecek }
    419      1.1  jdolecek 
    420      1.1  jdolecek int
    421  1.8.2.3     skrll union_root(mp, vpp)
    422      1.1  jdolecek 	struct mount *mp;
    423      1.1  jdolecek 	struct vnode **vpp;
    424      1.1  jdolecek {
    425      1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    426      1.1  jdolecek 	int error;
    427      1.1  jdolecek 	int loselock;
    428      1.1  jdolecek 
    429      1.1  jdolecek 	/*
    430      1.1  jdolecek 	 * Return locked reference to root.
    431      1.1  jdolecek 	 */
    432      1.1  jdolecek 	VREF(um->um_uppervp);
    433      1.1  jdolecek 	if ((um->um_op == UNMNT_BELOW) &&
    434      1.1  jdolecek 	     VOP_ISLOCKED(um->um_uppervp)) {
    435      1.1  jdolecek 		loselock = 1;
    436      1.1  jdolecek 	} else {
    437      1.1  jdolecek 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    438      1.1  jdolecek 		loselock = 0;
    439      1.1  jdolecek 	}
    440      1.1  jdolecek 	if (um->um_lowervp)
    441      1.1  jdolecek 		VREF(um->um_lowervp);
    442      1.1  jdolecek 	error = union_allocvp(vpp, mp,
    443      1.1  jdolecek 			      (struct vnode *) 0,
    444      1.1  jdolecek 			      (struct vnode *) 0,
    445      1.1  jdolecek 			      (struct componentname *) 0,
    446      1.1  jdolecek 			      um->um_uppervp,
    447      1.1  jdolecek 			      um->um_lowervp,
    448  1.8.2.3     skrll 			      1);
    449      1.1  jdolecek 
    450      1.1  jdolecek 	if (error) {
    451      1.1  jdolecek 		if (!loselock)
    452      1.1  jdolecek 			VOP_UNLOCK(um->um_uppervp, 0);
    453      1.1  jdolecek 		vrele(um->um_uppervp);
    454      1.1  jdolecek 		if (um->um_lowervp)
    455      1.1  jdolecek 			vrele(um->um_lowervp);
    456      1.1  jdolecek 	} else {
    457      1.1  jdolecek 		if (loselock)
    458      1.1  jdolecek 			VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
    459      1.1  jdolecek 	}
    460      1.1  jdolecek 
    461      1.1  jdolecek 	return (error);
    462      1.1  jdolecek }
    463      1.1  jdolecek 
    464      1.1  jdolecek /*ARGSUSED*/
    465      1.1  jdolecek int
    466  1.8.2.1  wrstuden union_quotactl(mp, cmd, uid, arg, l)
    467      1.1  jdolecek 	struct mount *mp;
    468      1.1  jdolecek 	int cmd;
    469      1.1  jdolecek 	uid_t uid;
    470  1.8.2.2     skrll 	void *arg;
    471  1.8.2.1  wrstuden 	struct lwp *l;
    472      1.1  jdolecek {
    473      1.1  jdolecek 
    474      1.1  jdolecek 	return (EOPNOTSUPP);
    475      1.1  jdolecek }
    476      1.1  jdolecek 
    477      1.1  jdolecek int
    478  1.8.2.2     skrll union_statvfs(mp, sbp, l)
    479      1.1  jdolecek 	struct mount *mp;
    480  1.8.2.2     skrll 	struct statvfs *sbp;
    481  1.8.2.1  wrstuden 	struct lwp *l;
    482      1.1  jdolecek {
    483      1.1  jdolecek 	int error;
    484      1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    485  1.8.2.2     skrll 	struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
    486  1.8.2.2     skrll 	unsigned long lbsize;
    487      1.1  jdolecek 
    488      1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    489  1.8.2.2     skrll 	printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
    490      1.1  jdolecek 	    um->um_lowervp, um->um_uppervp);
    491      1.1  jdolecek #endif
    492      1.1  jdolecek 
    493      1.1  jdolecek 	if (um->um_lowervp) {
    494  1.8.2.2     skrll 		error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf, l);
    495      1.1  jdolecek 		if (error)
    496  1.8.2.2     skrll 			goto done;
    497      1.1  jdolecek 	}
    498      1.1  jdolecek 
    499      1.1  jdolecek 	/* now copy across the "interesting" information and fake the rest */
    500  1.8.2.2     skrll 	lbsize = sbuf->f_bsize;
    501  1.8.2.2     skrll 	sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
    502  1.8.2.2     skrll 	sbp->f_files = sbuf->f_files - sbuf->f_ffree;
    503      1.1  jdolecek 
    504  1.8.2.2     skrll 	error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf, l);
    505      1.1  jdolecek 	if (error)
    506  1.8.2.2     skrll 		goto done;
    507      1.1  jdolecek 
    508  1.8.2.2     skrll 	sbp->f_flag = sbuf->f_flag;
    509  1.8.2.2     skrll 	sbp->f_bsize = sbuf->f_bsize;
    510  1.8.2.2     skrll 	sbp->f_frsize = sbuf->f_frsize;
    511  1.8.2.2     skrll 	sbp->f_iosize = sbuf->f_iosize;
    512      1.1  jdolecek 
    513      1.1  jdolecek 	/*
    514      1.1  jdolecek 	 * The "total" fields count total resources in all layers,
    515      1.1  jdolecek 	 * the "free" fields count only those resources which are
    516      1.1  jdolecek 	 * free in the upper layer (since only the upper layer
    517      1.1  jdolecek 	 * is writable).
    518      1.1  jdolecek 	 */
    519      1.1  jdolecek 
    520  1.8.2.2     skrll 	if (sbuf->f_bsize != lbsize)
    521  1.8.2.2     skrll 		sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
    522  1.8.2.2     skrll 	sbp->f_blocks += sbuf->f_blocks;
    523  1.8.2.2     skrll 	sbp->f_bfree = sbuf->f_bfree;
    524  1.8.2.2     skrll 	sbp->f_bavail = sbuf->f_bavail;
    525  1.8.2.2     skrll 	sbp->f_bresvd = sbuf->f_bresvd;
    526  1.8.2.2     skrll 	sbp->f_files += sbuf->f_files;
    527  1.8.2.2     skrll 	sbp->f_ffree = sbuf->f_ffree;
    528  1.8.2.2     skrll 	sbp->f_favail = sbuf->f_favail;
    529  1.8.2.2     skrll 	sbp->f_fresvd = sbuf->f_fresvd;
    530  1.8.2.2     skrll 
    531  1.8.2.2     skrll 	copy_statvfs_info(sbp, mp);
    532  1.8.2.2     skrll done:
    533  1.8.2.2     skrll 	free(sbuf, M_TEMP);
    534  1.8.2.2     skrll 	return error;
    535      1.1  jdolecek }
    536      1.1  jdolecek 
    537      1.1  jdolecek /*ARGSUSED*/
    538      1.1  jdolecek int
    539  1.8.2.1  wrstuden union_sync(mp, waitfor, cred, l)
    540      1.1  jdolecek 	struct mount *mp;
    541      1.1  jdolecek 	int waitfor;
    542      1.1  jdolecek 	struct ucred *cred;
    543  1.8.2.1  wrstuden 	struct lwp *l;
    544      1.1  jdolecek {
    545      1.1  jdolecek 
    546      1.1  jdolecek 	/*
    547      1.1  jdolecek 	 * XXX - Assumes no data cached at union layer.
    548      1.1  jdolecek 	 */
    549      1.1  jdolecek 	return (0);
    550      1.1  jdolecek }
    551      1.1  jdolecek 
    552      1.1  jdolecek /*ARGSUSED*/
    553      1.1  jdolecek int
    554  1.8.2.3     skrll union_vget(mp, ino, vpp)
    555      1.1  jdolecek 	struct mount *mp;
    556      1.1  jdolecek 	ino_t ino;
    557      1.1  jdolecek 	struct vnode **vpp;
    558      1.1  jdolecek {
    559      1.1  jdolecek 
    560      1.1  jdolecek 	return (EOPNOTSUPP);
    561      1.1  jdolecek }
    562      1.1  jdolecek 
    563      1.1  jdolecek /*ARGSUSED*/
    564      1.1  jdolecek int
    565  1.8.2.3     skrll union_fhtovp(mp, fidp, vpp)
    566      1.1  jdolecek 	struct mount *mp;
    567      1.1  jdolecek 	struct fid *fidp;
    568      1.1  jdolecek 	struct vnode **vpp;
    569      1.1  jdolecek {
    570      1.1  jdolecek 
    571      1.1  jdolecek 	return (EOPNOTSUPP);
    572      1.1  jdolecek }
    573      1.1  jdolecek 
    574      1.1  jdolecek /*ARGSUSED*/
    575      1.1  jdolecek int
    576      1.1  jdolecek union_checkexp(mp, nam, exflagsp, credanonp)
    577      1.1  jdolecek 	struct mount *mp;
    578      1.1  jdolecek 	struct mbuf *nam;
    579      1.1  jdolecek 	int *exflagsp;
    580      1.1  jdolecek 	struct ucred **credanonp;
    581      1.1  jdolecek {
    582      1.1  jdolecek 
    583      1.1  jdolecek 	return (EOPNOTSUPP);
    584      1.1  jdolecek }
    585      1.1  jdolecek 
    586      1.1  jdolecek /*ARGSUSED*/
    587      1.1  jdolecek int
    588      1.1  jdolecek union_vptofh(vp, fhp)
    589      1.1  jdolecek 	struct vnode *vp;
    590      1.1  jdolecek 	struct fid *fhp;
    591      1.1  jdolecek {
    592      1.1  jdolecek 
    593      1.1  jdolecek 	return (EOPNOTSUPP);
    594      1.1  jdolecek }
    595      1.1  jdolecek 
    596  1.8.2.2     skrll SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
    597      1.1  jdolecek {
    598  1.8.2.2     skrll 
    599  1.8.2.2     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    600  1.8.2.2     skrll 		       CTLFLAG_PERMANENT,
    601  1.8.2.2     skrll 		       CTLTYPE_NODE, "vfs", NULL,
    602  1.8.2.2     skrll 		       NULL, 0, NULL, 0,
    603  1.8.2.2     skrll 		       CTL_VFS, CTL_EOL);
    604  1.8.2.2     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    605  1.8.2.2     skrll 		       CTLFLAG_PERMANENT,
    606  1.8.2.2     skrll 		       CTLTYPE_NODE, "union",
    607  1.8.2.2     skrll 		       SYSCTL_DESCR("Union file system"),
    608  1.8.2.2     skrll 		       NULL, 0, NULL, 0,
    609  1.8.2.2     skrll 		       CTL_VFS, 15, CTL_EOL);
    610  1.8.2.2     skrll 	/*
    611  1.8.2.2     skrll 	 * XXX the "15" above could be dynamic, thereby eliminating
    612  1.8.2.2     skrll 	 * one more instance of the "number to vfs" mapping problem,
    613  1.8.2.2     skrll 	 * but "15" is the order as taken from sys/mount.h
    614  1.8.2.2     skrll 	 */
    615      1.1  jdolecek }
    616      1.1  jdolecek 
    617      1.1  jdolecek extern const struct vnodeopv_desc union_vnodeop_opv_desc;
    618      1.1  jdolecek 
    619      1.1  jdolecek const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
    620      1.1  jdolecek 	&union_vnodeop_opv_desc,
    621      1.1  jdolecek 	NULL,
    622      1.1  jdolecek };
    623      1.1  jdolecek 
    624      1.1  jdolecek struct vfsops union_vfsops = {
    625      1.1  jdolecek 	MOUNT_UNION,
    626      1.1  jdolecek 	union_mount,
    627      1.1  jdolecek 	union_start,
    628      1.1  jdolecek 	union_unmount,
    629      1.1  jdolecek 	union_root,
    630      1.1  jdolecek 	union_quotactl,
    631  1.8.2.2     skrll 	union_statvfs,
    632      1.1  jdolecek 	union_sync,
    633      1.1  jdolecek 	union_vget,
    634      1.1  jdolecek 	union_fhtovp,
    635      1.1  jdolecek 	union_vptofh,
    636      1.1  jdolecek 	union_init,
    637      1.3  jdolecek 	NULL,				/* vfs_reinit */
    638      1.1  jdolecek 	union_done,
    639  1.8.2.2     skrll 	NULL,
    640      1.1  jdolecek 	NULL,				/* vfs_mountroot */
    641      1.1  jdolecek 	union_checkexp,
    642  1.8.2.2     skrll 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    643      1.1  jdolecek 	union_vnodeopv_descs,
    644      1.1  jdolecek };
    645