Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.68.2.2
      1  1.68.2.1       tls /*	$NetBSD: union_vfsops.c,v 1.68.2.2 2017/12/03 11:38:44 jdolecek 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.68.2.1       tls __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.68.2.2 2017/12/03 11:38:44 jdolecek 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.35      elad #include <sys/kauth.h>
     95      1.54    rumble #include <sys/module.h>
     96       1.1  jdolecek 
     97  1.68.2.2  jdolecek #include <miscfs/genfs/genfs.h>
     98       1.1  jdolecek #include <fs/union/union.h>
     99       1.1  jdolecek 
    100      1.63     pooka MODULE(MODULE_CLASS_VFS, union, NULL);
    101      1.54    rumble 
    102      1.57    rumble static struct sysctllog *union_sysctl_log;
    103      1.57    rumble 
    104       1.1  jdolecek /*
    105       1.1  jdolecek  * Mount union filesystem
    106       1.1  jdolecek  */
    107       1.1  jdolecek int
    108      1.52      matt union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    109       1.1  jdolecek {
    110      1.49     pooka 	struct lwp *l = curlwp;
    111       1.1  jdolecek 	int error = 0;
    112      1.45       dsl 	struct union_args *args = data;
    113       1.1  jdolecek 	struct vnode *lowerrootvp = NULLVP;
    114       1.1  jdolecek 	struct vnode *upperrootvp = NULLVP;
    115       1.1  jdolecek 	struct union_mount *um = 0;
    116      1.27  christos 	const char *cp;
    117      1.27  christos 	char *xp;
    118       1.1  jdolecek 	int len;
    119       1.1  jdolecek 	size_t size;
    120       1.1  jdolecek 
    121  1.68.2.1       tls 	if (args == NULL)
    122  1.68.2.1       tls 		return EINVAL;
    123      1.45       dsl 	if (*data_len < sizeof *args)
    124      1.45       dsl 		return EINVAL;
    125      1.45       dsl 
    126       1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    127       1.1  jdolecek 	printf("union_mount(mp = %p)\n", mp);
    128       1.1  jdolecek #endif
    129       1.1  jdolecek 
    130       1.1  jdolecek 	if (mp->mnt_flag & MNT_GETARGS) {
    131       1.1  jdolecek 		um = MOUNTTOUNIONMOUNT(mp);
    132       1.1  jdolecek 		if (um == NULL)
    133       1.1  jdolecek 			return EIO;
    134      1.45       dsl 		args->target = NULL;
    135      1.45       dsl 		args->mntflags = um->um_op;
    136      1.45       dsl 		*data_len = sizeof *args;
    137      1.45       dsl 		return 0;
    138       1.1  jdolecek 	}
    139       1.1  jdolecek 	/*
    140       1.1  jdolecek 	 * Update is a no-op
    141       1.1  jdolecek 	 */
    142       1.1  jdolecek 	if (mp->mnt_flag & MNT_UPDATE) {
    143       1.1  jdolecek 		/*
    144       1.1  jdolecek 		 * Need to provide.
    145       1.1  jdolecek 		 * 1. a way to convert between rdonly and rdwr mounts.
    146       1.1  jdolecek 		 * 2. support for nfs exports.
    147       1.1  jdolecek 		 */
    148       1.1  jdolecek 		error = EOPNOTSUPP;
    149       1.1  jdolecek 		goto bad;
    150       1.1  jdolecek 	}
    151       1.1  jdolecek 
    152       1.1  jdolecek 	lowerrootvp = mp->mnt_vnodecovered;
    153      1.61     pooka 	vref(lowerrootvp);
    154       1.1  jdolecek 
    155       1.1  jdolecek 	/*
    156       1.1  jdolecek 	 * Find upper node.
    157       1.1  jdolecek 	 */
    158      1.60  dholland 	error = namei_simple_user(args->target,
    159      1.60  dholland 				NSM_FOLLOW_NOEMULROOT, &upperrootvp);
    160      1.60  dholland 	if (error != 0)
    161       1.1  jdolecek 		goto bad;
    162       1.1  jdolecek 
    163       1.1  jdolecek 	if (upperrootvp->v_type != VDIR) {
    164       1.1  jdolecek 		error = EINVAL;
    165       1.1  jdolecek 		goto bad;
    166       1.1  jdolecek 	}
    167      1.25     perry 
    168      1.68     rmind 	um = kmem_zalloc(sizeof(struct union_mount), KM_SLEEP);
    169       1.1  jdolecek 
    170       1.1  jdolecek 	/*
    171       1.1  jdolecek 	 * Keep a held reference to the target vnodes.
    172       1.1  jdolecek 	 * They are vrele'd in union_unmount.
    173       1.1  jdolecek 	 *
    174       1.1  jdolecek 	 * Depending on the _BELOW flag, the filesystems are
    175       1.1  jdolecek 	 * viewed in a different order.  In effect, this is the
    176       1.1  jdolecek 	 * same as providing a mount under option to the mount syscall.
    177       1.1  jdolecek 	 */
    178       1.1  jdolecek 
    179      1.45       dsl 	um->um_op = args->mntflags & UNMNT_OPMASK;
    180       1.1  jdolecek 	switch (um->um_op) {
    181       1.1  jdolecek 	case UNMNT_ABOVE:
    182       1.1  jdolecek 		um->um_lowervp = lowerrootvp;
    183       1.1  jdolecek 		um->um_uppervp = upperrootvp;
    184       1.1  jdolecek 		break;
    185       1.1  jdolecek 
    186       1.1  jdolecek 	case UNMNT_BELOW:
    187       1.1  jdolecek 		um->um_lowervp = upperrootvp;
    188       1.1  jdolecek 		um->um_uppervp = lowerrootvp;
    189       1.1  jdolecek 		break;
    190       1.1  jdolecek 
    191       1.1  jdolecek 	case UNMNT_REPLACE:
    192       1.1  jdolecek 		vrele(lowerrootvp);
    193       1.1  jdolecek 		lowerrootvp = NULLVP;
    194       1.1  jdolecek 		um->um_uppervp = upperrootvp;
    195       1.1  jdolecek 		um->um_lowervp = lowerrootvp;
    196       1.1  jdolecek 		break;
    197       1.1  jdolecek 
    198       1.1  jdolecek 	default:
    199       1.1  jdolecek 		error = EINVAL;
    200       1.1  jdolecek 		goto bad;
    201       1.1  jdolecek 	}
    202       1.1  jdolecek 
    203      1.65   hannken 	mp->mnt_iflag |= IMNT_MPSAFE;
    204      1.65   hannken 
    205       1.1  jdolecek 	/*
    206       1.1  jdolecek 	 * Unless the mount is readonly, ensure that the top layer
    207       1.1  jdolecek 	 * supports whiteout operations
    208       1.1  jdolecek 	 */
    209       1.1  jdolecek 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    210      1.62   hannken 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    211      1.62   hannken 		error = VOP_WHITEOUT(um->um_uppervp,
    212      1.62   hannken 		    (struct componentname *) 0, LOOKUP);
    213      1.62   hannken 		VOP_UNLOCK(um->um_uppervp);
    214       1.1  jdolecek 		if (error)
    215       1.1  jdolecek 			goto bad;
    216       1.1  jdolecek 	}
    217       1.1  jdolecek 
    218      1.36        ad 	um->um_cred = l->l_cred;
    219      1.35      elad 	kauth_cred_hold(um->um_cred);
    220      1.31  christos 	um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
    221       1.1  jdolecek 
    222       1.1  jdolecek 	/*
    223       1.1  jdolecek 	 * Depending on what you think the MNT_LOCAL flag might mean,
    224       1.1  jdolecek 	 * you may want the && to be || on the conditional below.
    225       1.1  jdolecek 	 * At the moment it has been defined that the filesystem is
    226       1.1  jdolecek 	 * only local if it is all local, ie the MNT_LOCAL flag implies
    227       1.1  jdolecek 	 * that the entire namespace is local.  If you think the MNT_LOCAL
    228       1.1  jdolecek 	 * flag implies that some of the files might be stored locally
    229       1.1  jdolecek 	 * then you will want to change the conditional.
    230       1.1  jdolecek 	 */
    231       1.1  jdolecek 	if (um->um_op == UNMNT_ABOVE) {
    232       1.1  jdolecek 		if (((um->um_lowervp == NULLVP) ||
    233       1.1  jdolecek 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
    234       1.1  jdolecek 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
    235       1.1  jdolecek 			mp->mnt_flag |= MNT_LOCAL;
    236       1.1  jdolecek 	}
    237       1.1  jdolecek 
    238       1.1  jdolecek 	/*
    239       1.1  jdolecek 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
    240       1.1  jdolecek 	 * of lookup() which explicitly checks the flag, rather than asking
    241  1.68.2.2  jdolecek 	 * the filesystem for its own opinion.  This means, that an update
    242       1.1  jdolecek 	 * mount of the underlying filesystem to go from rdonly to rdwr
    243       1.1  jdolecek 	 * will leave the unioned view as read-only.
    244       1.1  jdolecek 	 */
    245       1.1  jdolecek 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
    246       1.1  jdolecek 
    247       1.1  jdolecek 	mp->mnt_data = um;
    248       1.1  jdolecek 	vfs_getnewfsid(mp);
    249       1.1  jdolecek 
    250      1.12  christos 	error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    251      1.46     pooka 	    mp->mnt_op->vfs_name, mp, l);
    252       1.4  christos 	if (error)
    253       1.4  christos 		goto bad;
    254       1.1  jdolecek 
    255  1.68.2.2  jdolecek 	mp->mnt_lower = um->um_uppervp->v_mount;
    256  1.68.2.2  jdolecek 
    257       1.1  jdolecek 	switch (um->um_op) {
    258       1.1  jdolecek 	case UNMNT_ABOVE:
    259       1.1  jdolecek 		cp = "<above>:";
    260       1.1  jdolecek 		break;
    261       1.1  jdolecek 	case UNMNT_BELOW:
    262       1.1  jdolecek 		cp = "<below>:";
    263       1.1  jdolecek 		break;
    264       1.1  jdolecek 	case UNMNT_REPLACE:
    265       1.1  jdolecek 		cp = "";
    266       1.1  jdolecek 		break;
    267       1.1  jdolecek 	default:
    268       1.1  jdolecek 		cp = "<invalid>:";
    269       1.1  jdolecek #ifdef DIAGNOSTIC
    270       1.1  jdolecek 		panic("union_mount: bad um_op");
    271       1.1  jdolecek #endif
    272       1.1  jdolecek 		break;
    273       1.1  jdolecek 	}
    274       1.1  jdolecek 	len = strlen(cp);
    275       1.1  jdolecek 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    276       1.1  jdolecek 
    277      1.27  christos 	xp = mp->mnt_stat.f_mntfromname + len;
    278       1.1  jdolecek 	len = MNAMELEN - len;
    279       1.1  jdolecek 
    280      1.45       dsl 	(void) copyinstr(args->target, xp, len - 1, &size);
    281      1.27  christos 	memset(xp + size, 0, len - size);
    282       1.1  jdolecek 
    283       1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    284       1.1  jdolecek 	printf("union_mount: from %s, on %s\n",
    285       1.1  jdolecek 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    286       1.1  jdolecek #endif
    287       1.2  jdolecek 
    288       1.2  jdolecek 	/* Setup the readdir hook if it's not set already */
    289       1.2  jdolecek 	if (!vn_union_readdir_hook)
    290       1.2  jdolecek 		vn_union_readdir_hook = union_readdirhook;
    291       1.2  jdolecek 
    292       1.1  jdolecek 	return (0);
    293       1.1  jdolecek 
    294       1.1  jdolecek bad:
    295       1.1  jdolecek 	if (um)
    296      1.68     rmind 		kmem_free(um, sizeof(struct union_mount));
    297       1.1  jdolecek 	if (upperrootvp)
    298       1.1  jdolecek 		vrele(upperrootvp);
    299       1.1  jdolecek 	if (lowerrootvp)
    300       1.1  jdolecek 		vrele(lowerrootvp);
    301       1.1  jdolecek 	return (error);
    302       1.1  jdolecek }
    303       1.1  jdolecek 
    304       1.1  jdolecek /*
    305       1.1  jdolecek  * VFS start.  Nothing needed here - the start routine
    306       1.1  jdolecek  * on the underlying filesystem(s) will have been called
    307       1.1  jdolecek  * when that filesystem was mounted.
    308       1.1  jdolecek  */
    309       1.1  jdolecek  /*ARGSUSED*/
    310       1.1  jdolecek int
    311      1.49     pooka union_start(struct mount *mp, int flags)
    312       1.1  jdolecek {
    313       1.1  jdolecek 
    314       1.1  jdolecek 	return (0);
    315       1.1  jdolecek }
    316       1.1  jdolecek 
    317       1.1  jdolecek /*
    318       1.1  jdolecek  * Free reference to union layer
    319       1.1  jdolecek  */
    320  1.68.2.2  jdolecek static bool
    321  1.68.2.2  jdolecek union_unmount_selector(void *cl, struct vnode *vp)
    322  1.68.2.2  jdolecek {
    323  1.68.2.2  jdolecek 	int *count = cl;
    324  1.68.2.2  jdolecek 
    325  1.68.2.2  jdolecek 	KASSERT(mutex_owned(vp->v_interlock));
    326  1.68.2.2  jdolecek 
    327  1.68.2.2  jdolecek 	*count += 1;
    328  1.68.2.2  jdolecek 	return false;
    329  1.68.2.2  jdolecek }
    330  1.68.2.2  jdolecek 
    331       1.1  jdolecek int
    332      1.49     pooka union_unmount(struct mount *mp, int mntflags)
    333       1.1  jdolecek {
    334       1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    335       1.1  jdolecek 	int freeing;
    336      1.33  christos 	int error;
    337       1.1  jdolecek 
    338       1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    339       1.1  jdolecek 	printf("union_unmount(mp = %p)\n", mp);
    340       1.1  jdolecek #endif
    341       1.1  jdolecek 
    342       1.1  jdolecek 	/*
    343       1.1  jdolecek 	 * Keep flushing vnodes from the mount list.
    344       1.1  jdolecek 	 * This is needed because of the un_pvp held
    345       1.1  jdolecek 	 * reference to the parent vnode.
    346       1.1  jdolecek 	 * If more vnodes have been freed on a given pass,
    347       1.1  jdolecek 	 * the try again.  The loop will iterate at most
    348       1.1  jdolecek 	 * (d) times, where (d) is the maximum tree depth
    349       1.1  jdolecek 	 * in the filesystem.
    350       1.1  jdolecek 	 */
    351      1.33  christos 	for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
    352  1.68.2.2  jdolecek 		struct vnode_iterator *marker;
    353       1.1  jdolecek 		int n;
    354       1.1  jdolecek 
    355       1.1  jdolecek 		/* count #vnodes held on mount list */
    356      1.39   reinoud 		n = 0;
    357  1.68.2.2  jdolecek 		vfs_vnode_iterator_init(mp, &marker);
    358  1.68.2.2  jdolecek 		vfs_vnode_iterator_next(marker, union_unmount_selector, &n);
    359  1.68.2.2  jdolecek 		vfs_vnode_iterator_destroy(marker);
    360       1.1  jdolecek 
    361       1.1  jdolecek 		/* if this is unchanged then stop */
    362       1.1  jdolecek 		if (n == freeing)
    363       1.1  jdolecek 			break;
    364       1.1  jdolecek 
    365       1.1  jdolecek 		/* otherwise try once more time */
    366       1.1  jdolecek 		freeing = n;
    367       1.1  jdolecek 	}
    368       1.1  jdolecek 
    369       1.1  jdolecek 	/*
    370       1.1  jdolecek 	 * Ok, now that we've tried doing it gently, get out the hammer.
    371       1.1  jdolecek 	 */
    372       1.1  jdolecek 
    373       1.1  jdolecek 	if (mntflags & MNT_FORCE)
    374      1.33  christos 		error = vflush(mp, NULL, FORCECLOSE);
    375      1.25     perry 
    376      1.33  christos 	if (error)
    377      1.33  christos 		return error;
    378       1.1  jdolecek 
    379       1.1  jdolecek 	/*
    380       1.1  jdolecek 	 * Discard references to upper and lower target vnodes.
    381       1.1  jdolecek 	 */
    382       1.1  jdolecek 	if (um->um_lowervp)
    383       1.1  jdolecek 		vrele(um->um_lowervp);
    384       1.1  jdolecek 	vrele(um->um_uppervp);
    385      1.35      elad 	kauth_cred_free(um->um_cred);
    386       1.1  jdolecek 	/*
    387       1.1  jdolecek 	 * Finally, throw away the union_mount structure
    388       1.1  jdolecek 	 */
    389      1.68     rmind 	kmem_free(um, sizeof(struct union_mount));
    390      1.55    simonb 	mp->mnt_data = NULL;
    391      1.68     rmind 	return 0;
    392       1.1  jdolecek }
    393       1.1  jdolecek 
    394       1.1  jdolecek int
    395      1.52      matt union_root(struct mount *mp, struct vnode **vpp)
    396       1.1  jdolecek {
    397       1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    398       1.1  jdolecek 	int error;
    399       1.1  jdolecek 
    400       1.1  jdolecek 	/*
    401       1.1  jdolecek 	 * Return locked reference to root.
    402       1.1  jdolecek 	 */
    403      1.61     pooka 	vref(um->um_uppervp);
    404       1.1  jdolecek 	if (um->um_lowervp)
    405      1.61     pooka 		vref(um->um_lowervp);
    406      1.43       chs 	error = union_allocvp(vpp, mp, NULL, NULL, NULL,
    407      1.43       chs 			      um->um_uppervp, um->um_lowervp, 1);
    408       1.1  jdolecek 
    409       1.1  jdolecek 	if (error) {
    410  1.68.2.1       tls 		vrele(um->um_uppervp);
    411       1.1  jdolecek 		if (um->um_lowervp)
    412       1.1  jdolecek 			vrele(um->um_lowervp);
    413  1.68.2.1       tls 		return error;
    414       1.1  jdolecek 	}
    415       1.1  jdolecek 
    416  1.68.2.1       tls 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
    417  1.68.2.1       tls 
    418  1.68.2.1       tls 	return 0;
    419       1.1  jdolecek }
    420       1.1  jdolecek 
    421       1.1  jdolecek int
    422      1.52      matt union_statvfs(struct mount *mp, struct statvfs *sbp)
    423       1.1  jdolecek {
    424       1.1  jdolecek 	int error;
    425       1.1  jdolecek 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    426      1.28  nakayama 	struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
    427      1.17  christos 	unsigned long lbsize;
    428       1.1  jdolecek 
    429       1.1  jdolecek #ifdef UNION_DIAGNOSTIC
    430      1.12  christos 	printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
    431       1.1  jdolecek 	    um->um_lowervp, um->um_uppervp);
    432       1.1  jdolecek #endif
    433       1.1  jdolecek 
    434       1.1  jdolecek 	if (um->um_lowervp) {
    435      1.49     pooka 		error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
    436       1.1  jdolecek 		if (error)
    437      1.17  christos 			goto done;
    438       1.1  jdolecek 	}
    439       1.1  jdolecek 
    440      1.17  christos 	/* now copy across the "interesting" information and fake the rest */
    441      1.17  christos 	lbsize = sbuf->f_bsize;
    442      1.17  christos 	sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
    443      1.17  christos 	sbp->f_files = sbuf->f_files - sbuf->f_ffree;
    444      1.17  christos 
    445      1.49     pooka 	error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
    446       1.1  jdolecek 	if (error)
    447      1.17  christos 		goto done;
    448      1.17  christos 
    449      1.17  christos 	sbp->f_flag = sbuf->f_flag;
    450      1.17  christos 	sbp->f_bsize = sbuf->f_bsize;
    451      1.17  christos 	sbp->f_frsize = sbuf->f_frsize;
    452      1.17  christos 	sbp->f_iosize = sbuf->f_iosize;
    453      1.17  christos 
    454       1.1  jdolecek 	/*
    455       1.1  jdolecek 	 * The "total" fields count total resources in all layers,
    456       1.1  jdolecek 	 * the "free" fields count only those resources which are
    457       1.1  jdolecek 	 * free in the upper layer (since only the upper layer
    458       1.1  jdolecek 	 * is writable).
    459       1.1  jdolecek 	 */
    460      1.17  christos 
    461      1.17  christos 	if (sbuf->f_bsize != lbsize)
    462      1.17  christos 		sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
    463      1.17  christos 	sbp->f_blocks += sbuf->f_blocks;
    464      1.17  christos 	sbp->f_bfree = sbuf->f_bfree;
    465      1.17  christos 	sbp->f_bavail = sbuf->f_bavail;
    466      1.17  christos 	sbp->f_bresvd = sbuf->f_bresvd;
    467      1.17  christos 	sbp->f_files += sbuf->f_files;
    468      1.17  christos 	sbp->f_ffree = sbuf->f_ffree;
    469      1.17  christos 	sbp->f_favail = sbuf->f_favail;
    470      1.17  christos 	sbp->f_fresvd = sbuf->f_fresvd;
    471       1.1  jdolecek 
    472      1.12  christos 	copy_statvfs_info(sbp, mp);
    473      1.17  christos done:
    474      1.17  christos 	free(sbuf, M_TEMP);
    475      1.17  christos 	return error;
    476       1.1  jdolecek }
    477       1.1  jdolecek 
    478       1.1  jdolecek /*ARGSUSED*/
    479       1.1  jdolecek int
    480      1.40  christos union_sync(struct mount *mp, int waitfor,
    481      1.49     pooka     kauth_cred_t cred)
    482       1.1  jdolecek {
    483       1.1  jdolecek 
    484       1.1  jdolecek 	/*
    485       1.1  jdolecek 	 * XXX - Assumes no data cached at union layer.
    486       1.1  jdolecek 	 */
    487       1.1  jdolecek 	return (0);
    488       1.1  jdolecek }
    489       1.1  jdolecek 
    490       1.1  jdolecek /*ARGSUSED*/
    491       1.1  jdolecek int
    492      1.40  christos union_vget(struct mount *mp, ino_t ino,
    493      1.40  christos     struct vnode **vpp)
    494       1.1  jdolecek {
    495      1.25     perry 
    496       1.1  jdolecek 	return (EOPNOTSUPP);
    497       1.1  jdolecek }
    498       1.1  jdolecek 
    499      1.51  dholland static int
    500      1.51  dholland union_renamelock_enter(struct mount *mp)
    501      1.51  dholland {
    502      1.51  dholland 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    503      1.51  dholland 
    504      1.51  dholland 	/* Lock just the upper fs, where the action happens. */
    505      1.51  dholland 	return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
    506      1.51  dholland }
    507      1.51  dholland 
    508      1.51  dholland static void
    509      1.51  dholland union_renamelock_exit(struct mount *mp)
    510      1.51  dholland {
    511      1.51  dholland 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    512      1.51  dholland 
    513      1.51  dholland 	VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
    514      1.51  dholland }
    515      1.51  dholland 
    516       1.1  jdolecek extern const struct vnodeopv_desc union_vnodeop_opv_desc;
    517       1.1  jdolecek 
    518       1.1  jdolecek const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
    519       1.1  jdolecek 	&union_vnodeop_opv_desc,
    520       1.1  jdolecek 	NULL,
    521       1.1  jdolecek };
    522       1.1  jdolecek 
    523       1.1  jdolecek struct vfsops union_vfsops = {
    524  1.68.2.1       tls 	.vfs_name = MOUNT_UNION,
    525  1.68.2.1       tls 	.vfs_min_mount_data = sizeof (struct union_args),
    526  1.68.2.1       tls 	.vfs_mount = union_mount,
    527  1.68.2.1       tls 	.vfs_start = union_start,
    528  1.68.2.1       tls 	.vfs_unmount = union_unmount,
    529  1.68.2.1       tls 	.vfs_root = union_root,
    530  1.68.2.1       tls 	.vfs_quotactl = (void *)eopnotsupp,
    531  1.68.2.1       tls 	.vfs_statvfs = union_statvfs,
    532  1.68.2.1       tls 	.vfs_sync = union_sync,
    533  1.68.2.1       tls 	.vfs_vget = union_vget,
    534  1.68.2.2  jdolecek 	.vfs_loadvnode = union_loadvnode,
    535  1.68.2.1       tls 	.vfs_fhtovp = (void *)eopnotsupp,
    536  1.68.2.1       tls 	.vfs_vptofh = (void *)eopnotsupp,
    537  1.68.2.1       tls 	.vfs_init = union_init,
    538  1.68.2.1       tls 	.vfs_reinit = union_reinit,
    539  1.68.2.1       tls 	.vfs_done = union_done,
    540  1.68.2.1       tls 	.vfs_snapshot = (void *)eopnotsupp,
    541  1.68.2.1       tls 	.vfs_extattrctl = vfs_stdextattrctl,
    542  1.68.2.2  jdolecek 	.vfs_suspendctl = genfs_suspendctl,
    543  1.68.2.1       tls 	.vfs_renamelock_enter = union_renamelock_enter,
    544  1.68.2.1       tls 	.vfs_renamelock_exit = union_renamelock_exit,
    545  1.68.2.1       tls 	.vfs_fsync = (void *)eopnotsupp,
    546  1.68.2.1       tls 	.vfs_opv_descs = union_vnodeopv_descs
    547       1.1  jdolecek };
    548      1.54    rumble 
    549      1.54    rumble static int
    550      1.54    rumble union_modcmd(modcmd_t cmd, void *arg)
    551      1.54    rumble {
    552      1.57    rumble 	int error;
    553      1.54    rumble 
    554      1.54    rumble 	switch (cmd) {
    555      1.54    rumble 	case MODULE_CMD_INIT:
    556      1.57    rumble 		error = vfs_attach(&union_vfsops);
    557      1.57    rumble 		if (error != 0)
    558      1.57    rumble 			break;
    559      1.57    rumble 		sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
    560      1.57    rumble 			       CTLFLAG_PERMANENT,
    561      1.57    rumble 			       CTLTYPE_NODE, "union",
    562      1.57    rumble 			       SYSCTL_DESCR("Union file system"),
    563      1.57    rumble 			       NULL, 0, NULL, 0,
    564      1.57    rumble 			       CTL_VFS, 15, CTL_EOL);
    565      1.57    rumble 		/*
    566      1.57    rumble 		 * XXX the "15" above could be dynamic, thereby eliminating
    567      1.57    rumble 		 * one more instance of the "number to vfs" mapping problem,
    568      1.57    rumble 		 * but "15" is the order as taken from sys/mount.h
    569      1.57    rumble 		 */
    570      1.57    rumble 		break;
    571      1.54    rumble 	case MODULE_CMD_FINI:
    572      1.57    rumble 		error = vfs_detach(&union_vfsops);
    573      1.57    rumble 		if (error != 0)
    574      1.57    rumble 			break;
    575      1.57    rumble 		sysctl_teardown(&union_sysctl_log);
    576      1.57    rumble 		break;
    577      1.54    rumble 	default:
    578      1.57    rumble 		error = ENOTTY;
    579      1.57    rumble 		break;
    580      1.54    rumble 	}
    581      1.57    rumble 
    582      1.57    rumble 	return (error);
    583      1.54    rumble }
    584