Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.11.2.1.4.1
      1  1.11.2.1.4.1       riz /*	$NetBSD: union_vfsops.c,v 1.11.2.1.4.1 2006/01/06 23:53:22 riz 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.9       agc  * All rights reserved.
      6           1.9       agc  *
      7           1.9       agc  * This code is derived from software donated to Berkeley by
      8           1.9       agc  * Jan-Simon Pendry.
      9           1.9       agc  *
     10           1.9       agc  * Redistribution and use in source and binary forms, with or without
     11           1.9       agc  * modification, are permitted provided that the following conditions
     12           1.9       agc  * are met:
     13           1.9       agc  * 1. Redistributions of source code must retain the above copyright
     14           1.9       agc  *    notice, this list of conditions and the following disclaimer.
     15           1.9       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16           1.9       agc  *    notice, this list of conditions and the following disclaimer in the
     17           1.9       agc  *    documentation and/or other materials provided with the distribution.
     18           1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     19           1.9       agc  *    may be used to endorse or promote products derived from this software
     20           1.9       agc  *    without specific prior written permission.
     21           1.9       agc  *
     22           1.9       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23           1.9       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24           1.9       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25           1.9       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26           1.9       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27           1.9       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28           1.9       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29           1.9       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30           1.9       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31           1.9       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32           1.9       agc  * SUCH DAMAGE.
     33           1.9       agc  *
     34           1.9       agc  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
     35           1.9       agc  */
     36           1.9       agc 
     37           1.9       agc /*
     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.11.2.1.4.1       riz __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.11.2.1.4.1 2006/01/06 23:53:22 riz Exp $");
     81           1.1  jdolecek 
     82           1.1  jdolecek #include <sys/param.h>
     83           1.1  jdolecek #include <sys/systm.h>
     84          1.10    atatat #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      fvdl 		     struct proc *));
     99           1.8      fvdl int union_start __P((struct mount *, int, struct proc *));
    100           1.8      fvdl int union_unmount __P((struct mount *, int, struct proc *));
    101           1.7   thorpej int union_root __P((struct mount *, struct vnode **));
    102           1.8      fvdl int union_quotactl __P((struct mount *, int, uid_t, caddr_t, struct proc *));
    103           1.8      fvdl int union_statfs __P((struct mount *, struct statfs *, struct proc *));
    104           1.8      fvdl int union_sync __P((struct mount *, int, struct ucred *, struct proc *));
    105           1.7   thorpej int union_vget __P((struct mount *, ino_t, struct vnode **));
    106           1.7   thorpej 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      fvdl union_mount(mp, path, data, ndp, p)
    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      fvdl 	struct proc *p;
    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.1  jdolecek 	error = copyin(data, (caddr_t)&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      fvdl 	       UIO_USERSPACE, args.target, p);
    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.1  jdolecek 	um->um_cred = p->p_ucred;
    230           1.1  jdolecek 	crhold(um->um_cred);
    231           1.1  jdolecek 	um->um_cmode = UN_DIRMODE &~ p->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.1  jdolecek 	vfs_getnewfsid(mp);
    260           1.1  jdolecek 
    261           1.4  christos 	error = set_statfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    262           1.8      fvdl 	    mp, p);
    263           1.4  christos 	if (error)
    264           1.4  christos 		goto bad;
    265           1.1  jdolecek 
    266           1.1  jdolecek 	switch (um->um_op) {
    267           1.1  jdolecek 	case UNMNT_ABOVE:
    268           1.1  jdolecek 		cp = "<above>:";
    269           1.1  jdolecek 		break;
    270           1.1  jdolecek 	case UNMNT_BELOW:
    271           1.1  jdolecek 		cp = "<below>:";
    272           1.1  jdolecek 		break;
    273           1.1  jdolecek 	case UNMNT_REPLACE:
    274           1.1  jdolecek 		cp = "";
    275           1.1  jdolecek 		break;
    276           1.1  jdolecek 	default:
    277           1.1  jdolecek 		cp = "<invalid>:";
    278           1.1  jdolecek #ifdef DIAGNOSTIC
    279           1.1  jdolecek 		panic("union_mount: bad um_op");
    280           1.1  jdolecek #endif
    281           1.1  jdolecek 		break;
    282           1.1  jdolecek 	}
    283           1.1  jdolecek 	len = strlen(cp);
    284           1.1  jdolecek 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    285           1.1  jdolecek 
    286           1.1  jdolecek 	cp = mp->mnt_stat.f_mntfromname + len;
    287           1.1  jdolecek 	len = MNAMELEN - len;
    288           1.1  jdolecek 
    289           1.1  jdolecek 	(void) copyinstr(args.target, cp, len - 1, &size);
    290           1.1  jdolecek 	memset(cp + size, 0, len - size);
    291           1.1  jdolecek 
    292           1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    293           1.1  jdolecek 	printf("union_mount: from %s, on %s\n",
    294           1.1  jdolecek 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    295           1.1  jdolecek #endif
    296           1.2  jdolecek 
    297           1.2  jdolecek 	/* Setup the readdir hook if it's not set already */
    298           1.2  jdolecek 	if (!vn_union_readdir_hook)
    299           1.2  jdolecek 		vn_union_readdir_hook = union_readdirhook;
    300           1.2  jdolecek 
    301           1.1  jdolecek 	return (0);
    302           1.1  jdolecek 
    303           1.1  jdolecek bad:
    304           1.1  jdolecek 	if (um)
    305           1.1  jdolecek 		free(um, M_UFSMNT);
    306           1.1  jdolecek 	if (cred)
    307           1.1  jdolecek 		crfree(cred);
    308           1.1  jdolecek 	if (upperrootvp)
    309           1.1  jdolecek 		vrele(upperrootvp);
    310           1.1  jdolecek 	if (lowerrootvp)
    311           1.1  jdolecek 		vrele(lowerrootvp);
    312           1.1  jdolecek 	return (error);
    313           1.1  jdolecek }
    314           1.1  jdolecek 
    315           1.1  jdolecek /*
    316           1.1  jdolecek  * VFS start.  Nothing needed here - the start routine
    317           1.1  jdolecek  * on the underlying filesystem(s) will have been called
    318           1.1  jdolecek  * when that filesystem was mounted.
    319           1.1  jdolecek  */
    320           1.1  jdolecek  /*ARGSUSED*/
    321           1.1  jdolecek int
    322           1.8      fvdl union_start(mp, flags, p)
    323           1.1  jdolecek 	struct mount *mp;
    324           1.1  jdolecek 	int flags;
    325           1.8      fvdl 	struct proc *p;
    326           1.1  jdolecek {
    327           1.1  jdolecek 
    328           1.1  jdolecek 	return (0);
    329           1.1  jdolecek }
    330           1.1  jdolecek 
    331           1.1  jdolecek /*
    332           1.1  jdolecek  * Free reference to union layer
    333           1.1  jdolecek  */
    334           1.1  jdolecek int
    335           1.8      fvdl union_unmount(mp, mntflags, p)
    336           1.1  jdolecek 	struct mount *mp;
    337           1.1  jdolecek 	int mntflags;
    338           1.8      fvdl 	struct proc *p;
    339           1.1  jdolecek {
    340           1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    341           1.1  jdolecek 	int freeing;
    342           1.1  jdolecek 
    343           1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    344           1.1  jdolecek 	printf("union_unmount(mp = %p)\n", mp);
    345           1.1  jdolecek #endif
    346           1.1  jdolecek 
    347           1.1  jdolecek 	/*
    348           1.1  jdolecek 	 * Keep flushing vnodes from the mount list.
    349           1.1  jdolecek 	 * This is needed because of the un_pvp held
    350           1.1  jdolecek 	 * reference to the parent vnode.
    351           1.1  jdolecek 	 * If more vnodes have been freed on a given pass,
    352           1.1  jdolecek 	 * the try again.  The loop will iterate at most
    353           1.1  jdolecek 	 * (d) times, where (d) is the maximum tree depth
    354           1.1  jdolecek 	 * in the filesystem.
    355           1.1  jdolecek 	 */
    356  1.11.2.1.4.1       riz 	for (freeing = 0; vflush(mp, NULL, 0) != 0;) {
    357           1.1  jdolecek 		struct vnode *vp;
    358           1.1  jdolecek 		int n;
    359           1.1  jdolecek 
    360           1.1  jdolecek 		/* count #vnodes held on mount list */
    361           1.1  jdolecek 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
    362           1.1  jdolecek 				vp != NULLVP;
    363           1.1  jdolecek 				vp = vp->v_mntvnodes.le_next)
    364           1.1  jdolecek 			n++;
    365           1.1  jdolecek 
    366           1.1  jdolecek 		/* if this is unchanged then stop */
    367           1.1  jdolecek 		if (n == freeing)
    368           1.1  jdolecek 			break;
    369           1.1  jdolecek 
    370           1.1  jdolecek 		/* otherwise try once more time */
    371           1.1  jdolecek 		freeing = n;
    372           1.1  jdolecek 	}
    373           1.1  jdolecek 
    374           1.1  jdolecek 	/*
    375           1.1  jdolecek 	 * Ok, now that we've tried doing it gently, get out the hammer.
    376           1.1  jdolecek 	 */
    377           1.1  jdolecek 
    378           1.1  jdolecek 	if (mntflags & MNT_FORCE)
    379  1.11.2.1.4.1       riz 		vflush(mp, NULL, FORCECLOSE);
    380           1.1  jdolecek 
    381           1.1  jdolecek 
    382           1.1  jdolecek 	/*
    383           1.1  jdolecek 	 * Discard references to upper and lower target vnodes.
    384           1.1  jdolecek 	 */
    385           1.1  jdolecek 	if (um->um_lowervp)
    386           1.1  jdolecek 		vrele(um->um_lowervp);
    387           1.1  jdolecek 	vrele(um->um_uppervp);
    388           1.1  jdolecek 	crfree(um->um_cred);
    389           1.1  jdolecek 	/*
    390           1.1  jdolecek 	 * Finally, throw away the union_mount structure
    391           1.1  jdolecek 	 */
    392           1.1  jdolecek 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    393           1.1  jdolecek 	mp->mnt_data = 0;
    394           1.1  jdolecek 	return (0);
    395           1.1  jdolecek }
    396           1.1  jdolecek 
    397           1.1  jdolecek int
    398           1.7   thorpej union_root(mp, vpp)
    399           1.1  jdolecek 	struct mount *mp;
    400           1.1  jdolecek 	struct vnode **vpp;
    401           1.1  jdolecek {
    402           1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    403           1.1  jdolecek 	int error;
    404           1.1  jdolecek 	int loselock;
    405           1.1  jdolecek 
    406           1.1  jdolecek 	/*
    407           1.1  jdolecek 	 * Return locked reference to root.
    408           1.1  jdolecek 	 */
    409           1.1  jdolecek 	VREF(um->um_uppervp);
    410           1.1  jdolecek 	if ((um->um_op == UNMNT_BELOW) &&
    411           1.1  jdolecek 	     VOP_ISLOCKED(um->um_uppervp)) {
    412           1.1  jdolecek 		loselock = 1;
    413           1.1  jdolecek 	} else {
    414           1.1  jdolecek 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    415           1.1  jdolecek 		loselock = 0;
    416           1.1  jdolecek 	}
    417           1.1  jdolecek 	if (um->um_lowervp)
    418           1.1  jdolecek 		VREF(um->um_lowervp);
    419           1.1  jdolecek 	error = union_allocvp(vpp, mp,
    420           1.1  jdolecek 			      (struct vnode *) 0,
    421           1.1  jdolecek 			      (struct vnode *) 0,
    422           1.1  jdolecek 			      (struct componentname *) 0,
    423           1.1  jdolecek 			      um->um_uppervp,
    424           1.1  jdolecek 			      um->um_lowervp,
    425           1.7   thorpej 			      1);
    426           1.1  jdolecek 
    427           1.1  jdolecek 	if (error) {
    428           1.1  jdolecek 		if (!loselock)
    429           1.1  jdolecek 			VOP_UNLOCK(um->um_uppervp, 0);
    430           1.1  jdolecek 		vrele(um->um_uppervp);
    431           1.1  jdolecek 		if (um->um_lowervp)
    432           1.1  jdolecek 			vrele(um->um_lowervp);
    433           1.1  jdolecek 	} else {
    434           1.1  jdolecek 		if (loselock)
    435           1.1  jdolecek 			VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
    436           1.1  jdolecek 	}
    437           1.1  jdolecek 
    438           1.1  jdolecek 	return (error);
    439           1.1  jdolecek }
    440           1.1  jdolecek 
    441           1.1  jdolecek /*ARGSUSED*/
    442           1.1  jdolecek int
    443           1.8      fvdl union_quotactl(mp, cmd, uid, arg, p)
    444           1.1  jdolecek 	struct mount *mp;
    445           1.1  jdolecek 	int cmd;
    446           1.1  jdolecek 	uid_t uid;
    447           1.1  jdolecek 	caddr_t arg;
    448           1.8      fvdl 	struct proc *p;
    449           1.1  jdolecek {
    450           1.1  jdolecek 
    451           1.1  jdolecek 	return (EOPNOTSUPP);
    452           1.1  jdolecek }
    453           1.1  jdolecek 
    454           1.1  jdolecek int
    455           1.8      fvdl union_statfs(mp, sbp, p)
    456           1.1  jdolecek 	struct mount *mp;
    457           1.1  jdolecek 	struct statfs *sbp;
    458           1.8      fvdl 	struct proc *p;
    459           1.1  jdolecek {
    460           1.1  jdolecek 	int error;
    461           1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    462           1.1  jdolecek 	struct statfs mstat;
    463           1.1  jdolecek 	int lbsize;
    464           1.1  jdolecek 
    465           1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    466           1.1  jdolecek 	printf("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
    467           1.1  jdolecek 	    um->um_lowervp, um->um_uppervp);
    468           1.1  jdolecek #endif
    469           1.1  jdolecek 
    470           1.1  jdolecek 	memset(&mstat, 0, sizeof(mstat));
    471           1.1  jdolecek 
    472           1.1  jdolecek 	if (um->um_lowervp) {
    473           1.8      fvdl 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
    474           1.1  jdolecek 		if (error)
    475           1.1  jdolecek 			return (error);
    476           1.1  jdolecek 	}
    477           1.1  jdolecek 
    478           1.1  jdolecek 	/* now copy across the "interesting" information and fake the rest */
    479           1.1  jdolecek 	lbsize = mstat.f_bsize;
    480           1.1  jdolecek 	sbp->f_blocks = mstat.f_blocks - mstat.f_bfree;
    481           1.1  jdolecek 	sbp->f_files = mstat.f_files - mstat.f_ffree;
    482           1.1  jdolecek 
    483           1.8      fvdl 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
    484           1.1  jdolecek 	if (error)
    485           1.1  jdolecek 		return (error);
    486           1.1  jdolecek 
    487           1.1  jdolecek 	sbp->f_type = 0;
    488           1.1  jdolecek 	sbp->f_flags = mstat.f_flags;
    489           1.1  jdolecek 	sbp->f_bsize = mstat.f_bsize;
    490           1.1  jdolecek 	sbp->f_iosize = mstat.f_iosize;
    491           1.1  jdolecek 
    492           1.1  jdolecek 	/*
    493           1.1  jdolecek 	 * The "total" fields count total resources in all layers,
    494           1.1  jdolecek 	 * the "free" fields count only those resources which are
    495           1.1  jdolecek 	 * free in the upper layer (since only the upper layer
    496           1.1  jdolecek 	 * is writable).
    497           1.1  jdolecek 	 */
    498           1.1  jdolecek 
    499           1.1  jdolecek 	if (mstat.f_bsize != lbsize)
    500           1.1  jdolecek 		sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize;
    501           1.1  jdolecek 	sbp->f_blocks += mstat.f_blocks;
    502           1.1  jdolecek 	sbp->f_bfree = mstat.f_bfree;
    503           1.1  jdolecek 	sbp->f_bavail = mstat.f_bavail;
    504           1.1  jdolecek 	sbp->f_files += mstat.f_files;
    505           1.1  jdolecek 	sbp->f_ffree = mstat.f_ffree;
    506           1.1  jdolecek 
    507           1.4  christos 	copy_statfs_info(sbp, mp);
    508           1.1  jdolecek 	return (0);
    509           1.1  jdolecek }
    510           1.1  jdolecek 
    511           1.1  jdolecek /*ARGSUSED*/
    512           1.1  jdolecek int
    513           1.8      fvdl union_sync(mp, waitfor, cred, p)
    514           1.1  jdolecek 	struct mount *mp;
    515           1.1  jdolecek 	int waitfor;
    516           1.1  jdolecek 	struct ucred *cred;
    517           1.8      fvdl 	struct proc *p;
    518           1.1  jdolecek {
    519           1.1  jdolecek 
    520           1.1  jdolecek 	/*
    521           1.1  jdolecek 	 * XXX - Assumes no data cached at union layer.
    522           1.1  jdolecek 	 */
    523           1.1  jdolecek 	return (0);
    524           1.1  jdolecek }
    525           1.1  jdolecek 
    526           1.1  jdolecek /*ARGSUSED*/
    527           1.1  jdolecek int
    528           1.7   thorpej union_vget(mp, ino, vpp)
    529           1.1  jdolecek 	struct mount *mp;
    530           1.1  jdolecek 	ino_t ino;
    531           1.1  jdolecek 	struct vnode **vpp;
    532           1.1  jdolecek {
    533           1.1  jdolecek 
    534           1.1  jdolecek 	return (EOPNOTSUPP);
    535           1.1  jdolecek }
    536           1.1  jdolecek 
    537           1.1  jdolecek /*ARGSUSED*/
    538           1.1  jdolecek int
    539           1.7   thorpej union_fhtovp(mp, fidp, vpp)
    540           1.1  jdolecek 	struct mount *mp;
    541           1.1  jdolecek 	struct fid *fidp;
    542           1.1  jdolecek 	struct vnode **vpp;
    543           1.1  jdolecek {
    544           1.1  jdolecek 
    545           1.1  jdolecek 	return (EOPNOTSUPP);
    546           1.1  jdolecek }
    547           1.1  jdolecek 
    548           1.1  jdolecek /*ARGSUSED*/
    549           1.1  jdolecek int
    550           1.1  jdolecek union_checkexp(mp, nam, exflagsp, credanonp)
    551           1.1  jdolecek 	struct mount *mp;
    552           1.1  jdolecek 	struct mbuf *nam;
    553           1.1  jdolecek 	int *exflagsp;
    554           1.1  jdolecek 	struct ucred **credanonp;
    555           1.1  jdolecek {
    556           1.1  jdolecek 
    557           1.1  jdolecek 	return (EOPNOTSUPP);
    558           1.1  jdolecek }
    559           1.1  jdolecek 
    560           1.1  jdolecek /*ARGSUSED*/
    561           1.1  jdolecek int
    562           1.1  jdolecek union_vptofh(vp, fhp)
    563           1.1  jdolecek 	struct vnode *vp;
    564           1.1  jdolecek 	struct fid *fhp;
    565           1.1  jdolecek {
    566           1.1  jdolecek 
    567           1.1  jdolecek 	return (EOPNOTSUPP);
    568           1.1  jdolecek }
    569           1.1  jdolecek 
    570          1.10    atatat SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
    571           1.1  jdolecek {
    572          1.10    atatat 
    573          1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    574          1.11    atatat 		       CTLFLAG_PERMANENT,
    575          1.10    atatat 		       CTLTYPE_NODE, "vfs", NULL,
    576          1.10    atatat 		       NULL, 0, NULL, 0,
    577          1.10    atatat 		       CTL_VFS, CTL_EOL);
    578          1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    579          1.11    atatat 		       CTLFLAG_PERMANENT,
    580      1.11.2.1      tron 		       CTLTYPE_NODE, "union",
    581      1.11.2.1      tron 		       SYSCTL_DESCR("Union file system"),
    582          1.10    atatat 		       NULL, 0, NULL, 0,
    583          1.10    atatat 		       CTL_VFS, 15, CTL_EOL);
    584          1.10    atatat 	/*
    585          1.10    atatat 	 * XXX the "15" above could be dynamic, thereby eliminating
    586          1.10    atatat 	 * one more instance of the "number to vfs" mapping problem,
    587          1.10    atatat 	 * but "15" is the order as taken from sys/mount.h
    588          1.10    atatat 	 */
    589           1.1  jdolecek }
    590           1.1  jdolecek 
    591           1.1  jdolecek extern const struct vnodeopv_desc union_vnodeop_opv_desc;
    592           1.1  jdolecek 
    593           1.1  jdolecek const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
    594           1.1  jdolecek 	&union_vnodeop_opv_desc,
    595           1.1  jdolecek 	NULL,
    596           1.1  jdolecek };
    597           1.1  jdolecek 
    598           1.1  jdolecek struct vfsops union_vfsops = {
    599           1.1  jdolecek 	MOUNT_UNION,
    600           1.1  jdolecek 	union_mount,
    601           1.1  jdolecek 	union_start,
    602           1.1  jdolecek 	union_unmount,
    603           1.1  jdolecek 	union_root,
    604           1.1  jdolecek 	union_quotactl,
    605           1.1  jdolecek 	union_statfs,
    606           1.1  jdolecek 	union_sync,
    607           1.1  jdolecek 	union_vget,
    608           1.1  jdolecek 	union_fhtovp,
    609           1.1  jdolecek 	union_vptofh,
    610           1.1  jdolecek 	union_init,
    611           1.3  jdolecek 	NULL,				/* vfs_reinit */
    612           1.1  jdolecek 	union_done,
    613          1.10    atatat 	NULL,
    614           1.1  jdolecek 	NULL,				/* vfs_mountroot */
    615           1.1  jdolecek 	union_checkexp,
    616           1.1  jdolecek 	union_vnodeopv_descs,
    617           1.1  jdolecek };
    618