Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.29
      1  1.29   xtraeme /*	$NetBSD: union_vfsops.c,v 1.29 2005/08/30 19:11:43 xtraeme 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.29   xtraeme __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.29 2005/08/30 19:11:43 xtraeme 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.29   xtraeme int union_mount(struct mount *, const char *, void *, struct nameidata *,
     98  1.29   xtraeme 		     struct proc *);
     99  1.29   xtraeme int union_start(struct mount *, int, struct proc *);
    100  1.29   xtraeme int union_unmount(struct mount *, int, struct proc *);
    101  1.29   xtraeme int union_root(struct mount *, struct vnode **);
    102  1.29   xtraeme int union_quotactl(struct mount *, int, uid_t, void *, struct proc *);
    103  1.29   xtraeme int union_statvfs(struct mount *, struct statvfs *, struct proc *);
    104  1.29   xtraeme int union_sync(struct mount *, int, struct ucred *, struct proc *);
    105  1.29   xtraeme int union_vget(struct mount *, ino_t, struct vnode **);
    106  1.29   xtraeme int union_fhtovp(struct mount *, struct fid *, struct vnode **);
    107  1.29   xtraeme int union_checkexp(struct mount *, struct mbuf *, int *,
    108  1.29   xtraeme 		      struct ucred **);
    109  1.29   xtraeme int union_vptofh(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.27  christos 	const char *cp;
    129  1.27  christos 	char *xp;
    130   1.1  jdolecek 	int len;
    131   1.1  jdolecek 	size_t size;
    132   1.1  jdolecek 
    133   1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    134   1.1  jdolecek 	printf("union_mount(mp = %p)\n", mp);
    135   1.1  jdolecek #endif
    136   1.1  jdolecek 
    137   1.1  jdolecek 	if (mp->mnt_flag & MNT_GETARGS) {
    138   1.1  jdolecek 		um = MOUNTTOUNIONMOUNT(mp);
    139   1.1  jdolecek 		if (um == NULL)
    140   1.1  jdolecek 			return EIO;
    141   1.1  jdolecek 		args.target = NULL;
    142   1.1  jdolecek 		args.mntflags = um->um_op;
    143   1.1  jdolecek 		return copyout(&args, data, sizeof(args));
    144   1.1  jdolecek 	}
    145   1.1  jdolecek 	/*
    146   1.1  jdolecek 	 * Update is a no-op
    147   1.1  jdolecek 	 */
    148   1.1  jdolecek 	if (mp->mnt_flag & MNT_UPDATE) {
    149   1.1  jdolecek 		/*
    150   1.1  jdolecek 		 * Need to provide.
    151   1.1  jdolecek 		 * 1. a way to convert between rdonly and rdwr mounts.
    152   1.1  jdolecek 		 * 2. support for nfs exports.
    153   1.1  jdolecek 		 */
    154   1.1  jdolecek 		error = EOPNOTSUPP;
    155   1.1  jdolecek 		goto bad;
    156   1.1  jdolecek 	}
    157   1.1  jdolecek 
    158   1.1  jdolecek 	/*
    159   1.1  jdolecek 	 * Get argument
    160   1.1  jdolecek 	 */
    161  1.14       jrf 	error = copyin(data, &args, sizeof(struct union_args));
    162   1.1  jdolecek 	if (error)
    163   1.1  jdolecek 		goto bad;
    164   1.1  jdolecek 
    165   1.1  jdolecek 	lowerrootvp = mp->mnt_vnodecovered;
    166   1.1  jdolecek 	VREF(lowerrootvp);
    167   1.1  jdolecek 
    168   1.1  jdolecek 	/*
    169   1.1  jdolecek 	 * Find upper node.
    170   1.1  jdolecek 	 */
    171   1.1  jdolecek 	NDINIT(ndp, LOOKUP, FOLLOW,
    172   1.8      fvdl 	       UIO_USERSPACE, args.target, p);
    173   1.1  jdolecek 
    174   1.1  jdolecek 	if ((error = namei(ndp)) != 0)
    175   1.1  jdolecek 		goto bad;
    176   1.1  jdolecek 
    177   1.1  jdolecek 	upperrootvp = ndp->ni_vp;
    178   1.1  jdolecek 
    179   1.1  jdolecek 	if (upperrootvp->v_type != VDIR) {
    180   1.1  jdolecek 		error = EINVAL;
    181   1.1  jdolecek 		goto bad;
    182   1.1  jdolecek 	}
    183  1.25     perry 
    184   1.1  jdolecek 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
    185   1.1  jdolecek 				M_UFSMNT, M_WAITOK);	/* XXX */
    186   1.1  jdolecek 
    187   1.1  jdolecek 	/*
    188   1.1  jdolecek 	 * Keep a held reference to the target vnodes.
    189   1.1  jdolecek 	 * They are vrele'd in union_unmount.
    190   1.1  jdolecek 	 *
    191   1.1  jdolecek 	 * Depending on the _BELOW flag, the filesystems are
    192   1.1  jdolecek 	 * viewed in a different order.  In effect, this is the
    193   1.1  jdolecek 	 * same as providing a mount under option to the mount syscall.
    194   1.1  jdolecek 	 */
    195   1.1  jdolecek 
    196   1.1  jdolecek 	um->um_op = args.mntflags & UNMNT_OPMASK;
    197   1.1  jdolecek 	switch (um->um_op) {
    198   1.1  jdolecek 	case UNMNT_ABOVE:
    199   1.1  jdolecek 		um->um_lowervp = lowerrootvp;
    200   1.1  jdolecek 		um->um_uppervp = upperrootvp;
    201   1.1  jdolecek 		break;
    202   1.1  jdolecek 
    203   1.1  jdolecek 	case UNMNT_BELOW:
    204   1.1  jdolecek 		um->um_lowervp = upperrootvp;
    205   1.1  jdolecek 		um->um_uppervp = lowerrootvp;
    206   1.1  jdolecek 		break;
    207   1.1  jdolecek 
    208   1.1  jdolecek 	case UNMNT_REPLACE:
    209   1.1  jdolecek 		vrele(lowerrootvp);
    210   1.1  jdolecek 		lowerrootvp = NULLVP;
    211   1.1  jdolecek 		um->um_uppervp = upperrootvp;
    212   1.1  jdolecek 		um->um_lowervp = lowerrootvp;
    213   1.1  jdolecek 		break;
    214   1.1  jdolecek 
    215   1.1  jdolecek 	default:
    216   1.1  jdolecek 		error = EINVAL;
    217   1.1  jdolecek 		goto bad;
    218   1.1  jdolecek 	}
    219   1.1  jdolecek 
    220   1.1  jdolecek 	/*
    221   1.1  jdolecek 	 * Unless the mount is readonly, ensure that the top layer
    222   1.1  jdolecek 	 * supports whiteout operations
    223   1.1  jdolecek 	 */
    224   1.1  jdolecek 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    225   1.1  jdolecek 		error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
    226   1.1  jdolecek 		if (error)
    227   1.1  jdolecek 			goto bad;
    228   1.1  jdolecek 	}
    229   1.1  jdolecek 
    230   1.1  jdolecek 	um->um_cred = p->p_ucred;
    231   1.1  jdolecek 	crhold(um->um_cred);
    232   1.1  jdolecek 	um->um_cmode = UN_DIRMODE &~ p->p_cwdi->cwdi_cmask;
    233   1.1  jdolecek 
    234   1.1  jdolecek 	/*
    235   1.1  jdolecek 	 * Depending on what you think the MNT_LOCAL flag might mean,
    236   1.1  jdolecek 	 * you may want the && to be || on the conditional below.
    237   1.1  jdolecek 	 * At the moment it has been defined that the filesystem is
    238   1.1  jdolecek 	 * only local if it is all local, ie the MNT_LOCAL flag implies
    239   1.1  jdolecek 	 * that the entire namespace is local.  If you think the MNT_LOCAL
    240   1.1  jdolecek 	 * flag implies that some of the files might be stored locally
    241   1.1  jdolecek 	 * then you will want to change the conditional.
    242   1.1  jdolecek 	 */
    243   1.1  jdolecek 	if (um->um_op == UNMNT_ABOVE) {
    244   1.1  jdolecek 		if (((um->um_lowervp == NULLVP) ||
    245   1.1  jdolecek 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
    246   1.1  jdolecek 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
    247   1.1  jdolecek 			mp->mnt_flag |= MNT_LOCAL;
    248   1.1  jdolecek 	}
    249   1.1  jdolecek 
    250   1.1  jdolecek 	/*
    251   1.1  jdolecek 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
    252   1.1  jdolecek 	 * of lookup() which explicitly checks the flag, rather than asking
    253   1.1  jdolecek 	 * the filesystem for it's own opinion.  This means, that an update
    254   1.1  jdolecek 	 * mount of the underlying filesystem to go from rdonly to rdwr
    255   1.1  jdolecek 	 * will leave the unioned view as read-only.
    256   1.1  jdolecek 	 */
    257   1.1  jdolecek 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
    258   1.1  jdolecek 
    259   1.1  jdolecek 	mp->mnt_data = um;
    260  1.23   hannken 	mp->mnt_leaf = um->um_uppervp->v_mount->mnt_leaf;
    261   1.1  jdolecek 	vfs_getnewfsid(mp);
    262   1.1  jdolecek 
    263  1.12  christos 	error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    264   1.8      fvdl 	    mp, p);
    265   1.4  christos 	if (error)
    266   1.4  christos 		goto bad;
    267   1.1  jdolecek 
    268   1.1  jdolecek 	switch (um->um_op) {
    269   1.1  jdolecek 	case UNMNT_ABOVE:
    270   1.1  jdolecek 		cp = "<above>:";
    271   1.1  jdolecek 		break;
    272   1.1  jdolecek 	case UNMNT_BELOW:
    273   1.1  jdolecek 		cp = "<below>:";
    274   1.1  jdolecek 		break;
    275   1.1  jdolecek 	case UNMNT_REPLACE:
    276   1.1  jdolecek 		cp = "";
    277   1.1  jdolecek 		break;
    278   1.1  jdolecek 	default:
    279   1.1  jdolecek 		cp = "<invalid>:";
    280   1.1  jdolecek #ifdef DIAGNOSTIC
    281   1.1  jdolecek 		panic("union_mount: bad um_op");
    282   1.1  jdolecek #endif
    283   1.1  jdolecek 		break;
    284   1.1  jdolecek 	}
    285   1.1  jdolecek 	len = strlen(cp);
    286   1.1  jdolecek 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    287   1.1  jdolecek 
    288  1.27  christos 	xp = mp->mnt_stat.f_mntfromname + len;
    289   1.1  jdolecek 	len = MNAMELEN - len;
    290   1.1  jdolecek 
    291  1.27  christos 	(void) copyinstr(args.target, xp, len - 1, &size);
    292  1.27  christos 	memset(xp + size, 0, len - size);
    293   1.1  jdolecek 
    294   1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    295   1.1  jdolecek 	printf("union_mount: from %s, on %s\n",
    296   1.1  jdolecek 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    297   1.1  jdolecek #endif
    298   1.2  jdolecek 
    299   1.2  jdolecek 	/* Setup the readdir hook if it's not set already */
    300   1.2  jdolecek 	if (!vn_union_readdir_hook)
    301   1.2  jdolecek 		vn_union_readdir_hook = union_readdirhook;
    302   1.2  jdolecek 
    303   1.1  jdolecek 	return (0);
    304   1.1  jdolecek 
    305   1.1  jdolecek bad:
    306   1.1  jdolecek 	if (um)
    307   1.1  jdolecek 		free(um, M_UFSMNT);
    308   1.1  jdolecek 	if (cred)
    309   1.1  jdolecek 		crfree(cred);
    310   1.1  jdolecek 	if (upperrootvp)
    311   1.1  jdolecek 		vrele(upperrootvp);
    312   1.1  jdolecek 	if (lowerrootvp)
    313   1.1  jdolecek 		vrele(lowerrootvp);
    314   1.1  jdolecek 	return (error);
    315   1.1  jdolecek }
    316   1.1  jdolecek 
    317   1.1  jdolecek /*
    318   1.1  jdolecek  * VFS start.  Nothing needed here - the start routine
    319   1.1  jdolecek  * on the underlying filesystem(s) will have been called
    320   1.1  jdolecek  * when that filesystem was mounted.
    321   1.1  jdolecek  */
    322   1.1  jdolecek  /*ARGSUSED*/
    323   1.1  jdolecek int
    324   1.8      fvdl union_start(mp, flags, p)
    325   1.1  jdolecek 	struct mount *mp;
    326   1.1  jdolecek 	int flags;
    327   1.8      fvdl 	struct proc *p;
    328   1.1  jdolecek {
    329   1.1  jdolecek 
    330   1.1  jdolecek 	return (0);
    331   1.1  jdolecek }
    332   1.1  jdolecek 
    333   1.1  jdolecek /*
    334   1.1  jdolecek  * Free reference to union layer
    335   1.1  jdolecek  */
    336   1.1  jdolecek int
    337   1.8      fvdl union_unmount(mp, mntflags, p)
    338   1.1  jdolecek 	struct mount *mp;
    339   1.1  jdolecek 	int mntflags;
    340   1.8      fvdl 	struct proc *p;
    341   1.1  jdolecek {
    342   1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    343   1.1  jdolecek 	struct vnode *um_rootvp;
    344   1.1  jdolecek 	int error;
    345   1.1  jdolecek 	int freeing;
    346   1.1  jdolecek 
    347   1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    348   1.1  jdolecek 	printf("union_unmount(mp = %p)\n", mp);
    349   1.1  jdolecek #endif
    350   1.1  jdolecek 
    351   1.7   thorpej 	if ((error = union_root(mp, &um_rootvp)) != 0)
    352   1.1  jdolecek 		return (error);
    353   1.1  jdolecek 
    354   1.1  jdolecek 	/*
    355   1.1  jdolecek 	 * Keep flushing vnodes from the mount list.
    356   1.1  jdolecek 	 * This is needed because of the un_pvp held
    357   1.1  jdolecek 	 * reference to the parent vnode.
    358   1.1  jdolecek 	 * If more vnodes have been freed on a given pass,
    359   1.1  jdolecek 	 * the try again.  The loop will iterate at most
    360   1.1  jdolecek 	 * (d) times, where (d) is the maximum tree depth
    361   1.1  jdolecek 	 * in the filesystem.
    362   1.1  jdolecek 	 */
    363   1.1  jdolecek 	for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
    364   1.1  jdolecek 		struct vnode *vp;
    365   1.1  jdolecek 		int n;
    366   1.1  jdolecek 
    367   1.1  jdolecek 		/* count #vnodes held on mount list */
    368   1.1  jdolecek 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
    369   1.1  jdolecek 				vp != NULLVP;
    370   1.1  jdolecek 				vp = vp->v_mntvnodes.le_next)
    371   1.1  jdolecek 			n++;
    372   1.1  jdolecek 
    373   1.1  jdolecek 		/* if this is unchanged then stop */
    374   1.1  jdolecek 		if (n == freeing)
    375   1.1  jdolecek 			break;
    376   1.1  jdolecek 
    377   1.1  jdolecek 		/* otherwise try once more time */
    378   1.1  jdolecek 		freeing = n;
    379   1.1  jdolecek 	}
    380   1.1  jdolecek 
    381   1.1  jdolecek 	/*
    382   1.1  jdolecek 	 * Ok, now that we've tried doing it gently, get out the hammer.
    383   1.1  jdolecek 	 */
    384   1.1  jdolecek 
    385   1.1  jdolecek 	if (mntflags & MNT_FORCE)
    386   1.1  jdolecek 		vflush(mp, um_rootvp, FORCECLOSE);
    387  1.25     perry 
    388   1.1  jdolecek 
    389   1.1  jdolecek 	/* At this point the root vnode should have a single reference */
    390   1.1  jdolecek 	if (um_rootvp->v_usecount > 1) {
    391   1.1  jdolecek 		vput(um_rootvp);
    392   1.1  jdolecek 		return (EBUSY);
    393   1.1  jdolecek 	}
    394   1.1  jdolecek 
    395   1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    396   1.1  jdolecek 	vprint("union root", um_rootvp);
    397  1.25     perry #endif
    398   1.1  jdolecek 	/*
    399   1.1  jdolecek 	 * Discard references to upper and lower target vnodes.
    400   1.1  jdolecek 	 */
    401   1.1  jdolecek 	if (um->um_lowervp)
    402   1.1  jdolecek 		vrele(um->um_lowervp);
    403   1.1  jdolecek 	vrele(um->um_uppervp);
    404   1.1  jdolecek 	crfree(um->um_cred);
    405   1.1  jdolecek 	/*
    406   1.1  jdolecek 	 * Release reference on underlying root vnode
    407   1.1  jdolecek 	 */
    408   1.1  jdolecek 	vput(um_rootvp);
    409   1.1  jdolecek 	/*
    410   1.1  jdolecek 	 * And blow it away for future re-use
    411   1.1  jdolecek 	 */
    412   1.1  jdolecek 	vgone(um_rootvp);
    413   1.1  jdolecek 	/*
    414   1.1  jdolecek 	 * Finally, throw away the union_mount structure
    415   1.1  jdolecek 	 */
    416   1.1  jdolecek 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    417   1.1  jdolecek 	mp->mnt_data = 0;
    418   1.1  jdolecek 	return (0);
    419   1.1  jdolecek }
    420   1.1  jdolecek 
    421   1.1  jdolecek int
    422   1.7   thorpej union_root(mp, vpp)
    423   1.1  jdolecek 	struct mount *mp;
    424   1.1  jdolecek 	struct vnode **vpp;
    425   1.1  jdolecek {
    426   1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    427   1.1  jdolecek 	int error;
    428   1.1  jdolecek 	int loselock;
    429   1.1  jdolecek 
    430   1.1  jdolecek 	/*
    431   1.1  jdolecek 	 * Return locked reference to root.
    432   1.1  jdolecek 	 */
    433   1.1  jdolecek 	VREF(um->um_uppervp);
    434   1.1  jdolecek 	if ((um->um_op == UNMNT_BELOW) &&
    435   1.1  jdolecek 	     VOP_ISLOCKED(um->um_uppervp)) {
    436   1.1  jdolecek 		loselock = 1;
    437   1.1  jdolecek 	} else {
    438   1.1  jdolecek 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    439   1.1  jdolecek 		loselock = 0;
    440   1.1  jdolecek 	}
    441   1.1  jdolecek 	if (um->um_lowervp)
    442   1.1  jdolecek 		VREF(um->um_lowervp);
    443   1.1  jdolecek 	error = union_allocvp(vpp, mp,
    444   1.1  jdolecek 			      (struct vnode *) 0,
    445   1.1  jdolecek 			      (struct vnode *) 0,
    446   1.1  jdolecek 			      (struct componentname *) 0,
    447   1.1  jdolecek 			      um->um_uppervp,
    448   1.1  jdolecek 			      um->um_lowervp,
    449   1.7   thorpej 			      1);
    450   1.1  jdolecek 
    451   1.1  jdolecek 	if (error) {
    452   1.1  jdolecek 		if (!loselock)
    453   1.1  jdolecek 			VOP_UNLOCK(um->um_uppervp, 0);
    454   1.1  jdolecek 		vrele(um->um_uppervp);
    455   1.1  jdolecek 		if (um->um_lowervp)
    456   1.1  jdolecek 			vrele(um->um_lowervp);
    457   1.1  jdolecek 	} else {
    458   1.1  jdolecek 		if (loselock)
    459   1.1  jdolecek 			VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
    460   1.1  jdolecek 	}
    461   1.1  jdolecek 
    462   1.1  jdolecek 	return (error);
    463   1.1  jdolecek }
    464   1.1  jdolecek 
    465   1.1  jdolecek /*ARGSUSED*/
    466   1.1  jdolecek int
    467   1.8      fvdl union_quotactl(mp, cmd, uid, arg, p)
    468   1.1  jdolecek 	struct mount *mp;
    469   1.1  jdolecek 	int cmd;
    470   1.1  jdolecek 	uid_t uid;
    471  1.13       jrf 	void *arg;
    472   1.8      fvdl 	struct proc *p;
    473   1.1  jdolecek {
    474   1.1  jdolecek 
    475   1.1  jdolecek 	return (EOPNOTSUPP);
    476   1.1  jdolecek }
    477   1.1  jdolecek 
    478   1.1  jdolecek int
    479  1.12  christos union_statvfs(mp, sbp, p)
    480   1.1  jdolecek 	struct mount *mp;
    481  1.12  christos 	struct statvfs *sbp;
    482   1.8      fvdl 	struct proc *p;
    483   1.1  jdolecek {
    484   1.1  jdolecek 	int error;
    485   1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    486  1.28  nakayama 	struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
    487  1.17  christos 	unsigned long lbsize;
    488   1.1  jdolecek 
    489   1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    490  1.12  christos 	printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
    491   1.1  jdolecek 	    um->um_lowervp, um->um_uppervp);
    492   1.1  jdolecek #endif
    493   1.1  jdolecek 
    494   1.1  jdolecek 	if (um->um_lowervp) {
    495  1.17  christos 		error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf, p);
    496   1.1  jdolecek 		if (error)
    497  1.17  christos 			goto done;
    498   1.1  jdolecek 	}
    499   1.1  jdolecek 
    500  1.17  christos 	/* now copy across the "interesting" information and fake the rest */
    501  1.17  christos 	lbsize = sbuf->f_bsize;
    502  1.17  christos 	sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
    503  1.17  christos 	sbp->f_files = sbuf->f_files - sbuf->f_ffree;
    504  1.17  christos 
    505  1.17  christos 	error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf, p);
    506   1.1  jdolecek 	if (error)
    507  1.17  christos 		goto done;
    508  1.17  christos 
    509  1.17  christos 	sbp->f_flag = sbuf->f_flag;
    510  1.17  christos 	sbp->f_bsize = sbuf->f_bsize;
    511  1.17  christos 	sbp->f_frsize = sbuf->f_frsize;
    512  1.17  christos 	sbp->f_iosize = sbuf->f_iosize;
    513  1.17  christos 
    514   1.1  jdolecek 	/*
    515   1.1  jdolecek 	 * The "total" fields count total resources in all layers,
    516   1.1  jdolecek 	 * the "free" fields count only those resources which are
    517   1.1  jdolecek 	 * free in the upper layer (since only the upper layer
    518   1.1  jdolecek 	 * is writable).
    519   1.1  jdolecek 	 */
    520  1.17  christos 
    521  1.17  christos 	if (sbuf->f_bsize != lbsize)
    522  1.17  christos 		sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
    523  1.17  christos 	sbp->f_blocks += sbuf->f_blocks;
    524  1.17  christos 	sbp->f_bfree = sbuf->f_bfree;
    525  1.17  christos 	sbp->f_bavail = sbuf->f_bavail;
    526  1.17  christos 	sbp->f_bresvd = sbuf->f_bresvd;
    527  1.17  christos 	sbp->f_files += sbuf->f_files;
    528  1.17  christos 	sbp->f_ffree = sbuf->f_ffree;
    529  1.17  christos 	sbp->f_favail = sbuf->f_favail;
    530  1.17  christos 	sbp->f_fresvd = sbuf->f_fresvd;
    531   1.1  jdolecek 
    532  1.12  christos 	copy_statvfs_info(sbp, mp);
    533  1.17  christos done:
    534  1.17  christos 	free(sbuf, M_TEMP);
    535  1.17  christos 	return error;
    536   1.1  jdolecek }
    537   1.1  jdolecek 
    538   1.1  jdolecek /*ARGSUSED*/
    539   1.1  jdolecek int
    540   1.8      fvdl union_sync(mp, waitfor, cred, p)
    541   1.1  jdolecek 	struct mount *mp;
    542   1.1  jdolecek 	int waitfor;
    543   1.1  jdolecek 	struct ucred *cred;
    544   1.8      fvdl 	struct proc *p;
    545   1.1  jdolecek {
    546   1.1  jdolecek 
    547   1.1  jdolecek 	/*
    548   1.1  jdolecek 	 * XXX - Assumes no data cached at union layer.
    549   1.1  jdolecek 	 */
    550   1.1  jdolecek 	return (0);
    551   1.1  jdolecek }
    552   1.1  jdolecek 
    553   1.1  jdolecek /*ARGSUSED*/
    554   1.1  jdolecek int
    555   1.7   thorpej union_vget(mp, ino, vpp)
    556   1.1  jdolecek 	struct mount *mp;
    557   1.1  jdolecek 	ino_t ino;
    558   1.1  jdolecek 	struct vnode **vpp;
    559   1.1  jdolecek {
    560  1.25     perry 
    561   1.1  jdolecek 	return (EOPNOTSUPP);
    562   1.1  jdolecek }
    563   1.1  jdolecek 
    564   1.1  jdolecek /*ARGSUSED*/
    565   1.1  jdolecek int
    566   1.7   thorpej union_fhtovp(mp, fidp, vpp)
    567   1.1  jdolecek 	struct mount *mp;
    568   1.1  jdolecek 	struct fid *fidp;
    569   1.1  jdolecek 	struct vnode **vpp;
    570   1.1  jdolecek {
    571   1.1  jdolecek 
    572   1.1  jdolecek 	return (EOPNOTSUPP);
    573   1.1  jdolecek }
    574   1.1  jdolecek 
    575   1.1  jdolecek /*ARGSUSED*/
    576   1.1  jdolecek int
    577   1.1  jdolecek union_checkexp(mp, nam, exflagsp, credanonp)
    578   1.1  jdolecek 	struct mount *mp;
    579   1.1  jdolecek 	struct mbuf *nam;
    580   1.1  jdolecek 	int *exflagsp;
    581   1.1  jdolecek 	struct ucred **credanonp;
    582   1.1  jdolecek {
    583   1.1  jdolecek 
    584   1.1  jdolecek 	return (EOPNOTSUPP);
    585   1.1  jdolecek }
    586   1.1  jdolecek 
    587   1.1  jdolecek /*ARGSUSED*/
    588   1.1  jdolecek int
    589   1.1  jdolecek union_vptofh(vp, fhp)
    590   1.1  jdolecek 	struct vnode *vp;
    591   1.1  jdolecek 	struct fid *fhp;
    592   1.1  jdolecek {
    593   1.1  jdolecek 
    594   1.1  jdolecek 	return (EOPNOTSUPP);
    595   1.1  jdolecek }
    596   1.1  jdolecek 
    597  1.10    atatat SYSCTL_SETUP(sysctl_vfs_union_setup, "sysctl vfs.union subtree setup")
    598   1.1  jdolecek {
    599  1.10    atatat 
    600  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    601  1.11    atatat 		       CTLFLAG_PERMANENT,
    602  1.10    atatat 		       CTLTYPE_NODE, "vfs", NULL,
    603  1.10    atatat 		       NULL, 0, NULL, 0,
    604  1.10    atatat 		       CTL_VFS, CTL_EOL);
    605  1.11    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    606  1.11    atatat 		       CTLFLAG_PERMANENT,
    607  1.18    atatat 		       CTLTYPE_NODE, "union",
    608  1.18    atatat 		       SYSCTL_DESCR("Union file system"),
    609  1.10    atatat 		       NULL, 0, NULL, 0,
    610  1.10    atatat 		       CTL_VFS, 15, CTL_EOL);
    611  1.10    atatat 	/*
    612  1.10    atatat 	 * XXX the "15" above could be dynamic, thereby eliminating
    613  1.10    atatat 	 * one more instance of the "number to vfs" mapping problem,
    614  1.10    atatat 	 * but "15" is the order as taken from sys/mount.h
    615  1.10    atatat 	 */
    616   1.1  jdolecek }
    617   1.1  jdolecek 
    618   1.1  jdolecek extern const struct vnodeopv_desc union_vnodeop_opv_desc;
    619   1.1  jdolecek 
    620   1.1  jdolecek const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
    621   1.1  jdolecek 	&union_vnodeop_opv_desc,
    622   1.1  jdolecek 	NULL,
    623   1.1  jdolecek };
    624   1.1  jdolecek 
    625   1.1  jdolecek struct vfsops union_vfsops = {
    626   1.1  jdolecek 	MOUNT_UNION,
    627   1.1  jdolecek 	union_mount,
    628   1.1  jdolecek 	union_start,
    629   1.1  jdolecek 	union_unmount,
    630   1.1  jdolecek 	union_root,
    631   1.1  jdolecek 	union_quotactl,
    632  1.12  christos 	union_statvfs,
    633   1.1  jdolecek 	union_sync,
    634   1.1  jdolecek 	union_vget,
    635   1.1  jdolecek 	union_fhtovp,
    636   1.1  jdolecek 	union_vptofh,
    637   1.1  jdolecek 	union_init,
    638   1.3  jdolecek 	NULL,				/* vfs_reinit */
    639   1.1  jdolecek 	union_done,
    640  1.10    atatat 	NULL,
    641   1.1  jdolecek 	NULL,				/* vfs_mountroot */
    642   1.1  jdolecek 	union_checkexp,
    643  1.22  wrstuden 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    644  1.24   thorpej 	vfs_stdextattrctl,
    645   1.1  jdolecek 	union_vnodeopv_descs,
    646   1.1  jdolecek };
    647  1.26   thorpej VFS_ATTACH(union_vfsops);
    648