Home | History | Annotate | Line # | Download | only in overlay
overlay_vfsops.c revision 1.5.2.3
      1  1.5.2.3  bouyer /*	$NetBSD: overlay_vfsops.c,v 1.5.2.3 2000/11/22 16:05:45 bouyer Exp $	*/
      2  1.5.2.2  bouyer 
      3  1.5.2.2  bouyer /*
      4  1.5.2.2  bouyer  * Copyright (c) 1999, 2000 National Aeronautics & Space Administration
      5  1.5.2.2  bouyer  * All rights reserved.
      6  1.5.2.2  bouyer  *
      7  1.5.2.2  bouyer  * This software was written by William Studenmund of the
      8  1.5.2.2  bouyer  * Numerical Aerospace Similation Facility, NASA Ames Research Center.
      9  1.5.2.2  bouyer  *
     10  1.5.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.5.2.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.5.2.2  bouyer  * are met:
     13  1.5.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.5.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.5.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.5.2.2  bouyer  * 3. Neither the name of the National Aeronautics & Space Administration
     19  1.5.2.2  bouyer  *    nor the names of its contributors may be used to endorse or promote
     20  1.5.2.2  bouyer  *    products derived from this software without specific prior written
     21  1.5.2.2  bouyer  *    permission.
     22  1.5.2.2  bouyer  *
     23  1.5.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24  1.5.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.5.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.5.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27  1.5.2.2  bouyer  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28  1.5.2.2  bouyer  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.5.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.5.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.5.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.5.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.5.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     34  1.5.2.2  bouyer  */
     35  1.5.2.2  bouyer /*
     36  1.5.2.2  bouyer  * Copyright (c) 1992, 1993, 1995
     37  1.5.2.2  bouyer  *	The Regents of the University of California.  All rights reserved.
     38  1.5.2.2  bouyer  *
     39  1.5.2.2  bouyer  * This code is derived from software donated to Berkeley by
     40  1.5.2.2  bouyer  * Jan-Simon Pendry.
     41  1.5.2.2  bouyer  *
     42  1.5.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     43  1.5.2.2  bouyer  * modification, are permitted provided that the following conditions
     44  1.5.2.2  bouyer  * are met:
     45  1.5.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     46  1.5.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     47  1.5.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.5.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     49  1.5.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     50  1.5.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     51  1.5.2.2  bouyer  *    must display the following acknowledgement:
     52  1.5.2.2  bouyer  *	This product includes software developed by the University of
     53  1.5.2.2  bouyer  *	California, Berkeley and its contributors.
     54  1.5.2.2  bouyer  * 4. Neither the name of the University nor the names of its contributors
     55  1.5.2.2  bouyer  *    may be used to endorse or promote products derived from this software
     56  1.5.2.2  bouyer  *    without specific prior written permission.
     57  1.5.2.2  bouyer  *
     58  1.5.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.5.2.2  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.5.2.2  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.5.2.2  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.5.2.2  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.5.2.2  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.5.2.2  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.5.2.2  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.5.2.2  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.5.2.2  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.5.2.2  bouyer  * SUCH DAMAGE.
     69  1.5.2.2  bouyer  *
     70  1.5.2.2  bouyer  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
     71  1.5.2.2  bouyer  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
     72  1.5.2.2  bouyer  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
     73  1.5.2.2  bouyer  */
     74  1.5.2.2  bouyer 
     75  1.5.2.2  bouyer /*
     76  1.5.2.2  bouyer  * Null Layer
     77  1.5.2.2  bouyer  * (See null_vnops.c for a description of what this does.)
     78  1.5.2.2  bouyer  */
     79  1.5.2.2  bouyer 
     80  1.5.2.2  bouyer #include <sys/param.h>
     81  1.5.2.2  bouyer #include <sys/systm.h>
     82  1.5.2.2  bouyer #include <sys/time.h>
     83  1.5.2.2  bouyer #include <sys/proc.h>
     84  1.5.2.2  bouyer #include <sys/types.h>
     85  1.5.2.2  bouyer #include <sys/vnode.h>
     86  1.5.2.2  bouyer #include <sys/mount.h>
     87  1.5.2.2  bouyer #include <sys/namei.h>
     88  1.5.2.2  bouyer #include <sys/malloc.h>
     89  1.5.2.2  bouyer #include <miscfs/overlay/overlay.h>
     90  1.5.2.2  bouyer #include <miscfs/genfs/layer_extern.h>
     91  1.5.2.2  bouyer 
     92  1.5.2.2  bouyer int	ov_mount __P((struct mount *, const char *, void *,
     93  1.5.2.2  bouyer 			  struct nameidata *, struct proc *));
     94  1.5.2.2  bouyer int	ov_unmount __P((struct mount *, int, struct proc *));
     95  1.5.2.2  bouyer 
     96  1.5.2.2  bouyer #define	NOVERLAYNODECACHE	16
     97  1.5.2.2  bouyer 
     98  1.5.2.2  bouyer /*
     99  1.5.2.2  bouyer  * Mount overlay layer
    100  1.5.2.2  bouyer  */
    101  1.5.2.2  bouyer int
    102  1.5.2.2  bouyer ov_mount(mp, path, data, ndp, p)
    103  1.5.2.2  bouyer 	struct mount *mp;
    104  1.5.2.2  bouyer 	const char *path;
    105  1.5.2.2  bouyer 	void *data;
    106  1.5.2.2  bouyer 	struct nameidata *ndp;
    107  1.5.2.2  bouyer 	struct proc *p;
    108  1.5.2.2  bouyer {
    109  1.5.2.2  bouyer 	int error = 0;
    110  1.5.2.2  bouyer 	struct overlay_args args;
    111  1.5.2.2  bouyer 	struct vnode *lowerrootvp, *vp;
    112  1.5.2.2  bouyer 	struct overlay_mount *nmp;
    113  1.5.2.2  bouyer 	struct layer_mount *lmp;
    114  1.5.2.2  bouyer 	size_t size;
    115  1.5.2.2  bouyer 
    116  1.5.2.2  bouyer #ifdef OVERLAYFS_DIAGNOSTIC
    117  1.5.2.2  bouyer 	printf("ov_mount(mp = %p)\n", mp);
    118  1.5.2.2  bouyer #endif
    119  1.5.2.2  bouyer 
    120  1.5.2.2  bouyer 	/*
    121  1.5.2.2  bouyer 	 * Get argument
    122  1.5.2.2  bouyer 	 */
    123  1.5.2.2  bouyer 	error = copyin(data, (caddr_t)&args, sizeof(struct overlay_args));
    124  1.5.2.2  bouyer 	if (error)
    125  1.5.2.2  bouyer 		return (error);
    126  1.5.2.2  bouyer 
    127  1.5.2.2  bouyer 	/*
    128  1.5.2.2  bouyer 	 * Update only does export updating.
    129  1.5.2.2  bouyer 	 */
    130  1.5.2.2  bouyer 	if (mp->mnt_flag & MNT_UPDATE) {
    131  1.5.2.2  bouyer 		lmp = MOUNTTOLAYERMOUNT(mp);
    132  1.5.2.2  bouyer 		if (args.ova_target == 0)
    133  1.5.2.2  bouyer 			return (vfs_export(mp, &lmp->layerm_export,
    134  1.5.2.2  bouyer 					&args.la.export));
    135  1.5.2.2  bouyer 		else
    136  1.5.2.2  bouyer 			return (EOPNOTSUPP);
    137  1.5.2.2  bouyer 	}
    138  1.5.2.2  bouyer 
    139  1.5.2.2  bouyer 	/*
    140  1.5.2.2  bouyer 	 * Find lower node
    141  1.5.2.2  bouyer 	 */
    142  1.5.2.2  bouyer 	lowerrootvp = mp->mnt_vnodecovered;
    143  1.5.2.2  bouyer 	if ((error = vget(lowerrootvp, LK_EXCLUSIVE | LK_RETRY)))
    144  1.5.2.2  bouyer 		return (error);
    145  1.5.2.2  bouyer 
    146  1.5.2.2  bouyer 	/*
    147  1.5.2.2  bouyer 	 * First cut at fixing up upper mount point
    148  1.5.2.2  bouyer 	 */
    149  1.5.2.2  bouyer 	nmp = (struct overlay_mount *) malloc(sizeof(struct overlay_mount),
    150  1.5.2.2  bouyer 				M_UFSMNT, M_WAITOK);	/* XXX */
    151  1.5.2.2  bouyer 	memset((caddr_t)nmp, 0, sizeof(struct overlay_mount));
    152  1.5.2.2  bouyer 
    153  1.5.2.2  bouyer 	mp->mnt_data = (qaddr_t) nmp;
    154  1.5.2.2  bouyer 	nmp->ovm_vfs = lowerrootvp->v_mount;
    155  1.5.2.2  bouyer 	if (nmp->ovm_vfs->mnt_flag & MNT_LOCAL)
    156  1.5.2.2  bouyer 		mp->mnt_flag |= MNT_LOCAL;
    157  1.5.2.2  bouyer 
    158  1.5.2.2  bouyer 	/*
    159  1.5.2.2  bouyer 	 * Make sure that the mount point is sufficiently initialized
    160  1.5.2.2  bouyer 	 * that the node create call will work.
    161  1.5.2.2  bouyer 	 */
    162  1.5.2.2  bouyer 	vfs_getnewfsid(mp);
    163  1.5.2.2  bouyer 
    164  1.5.2.2  bouyer 	nmp->ovm_size = sizeof (struct overlay_node);
    165  1.5.2.2  bouyer 	nmp->ovm_tag = VT_OVERLAY;
    166  1.5.2.2  bouyer 	nmp->ovm_bypass = layer_bypass;
    167  1.5.2.2  bouyer 	nmp->ovm_alloc = layer_node_alloc;	/* the default alloc is fine */
    168  1.5.2.2  bouyer 	nmp->ovm_vnodeop_p = overlay_vnodeop_p;
    169  1.5.2.2  bouyer 	simple_lock_init(&nmp->ovm_hashlock);
    170  1.5.2.3  bouyer 	nmp->ovm_node_hashtbl = hashinit(NOVERLAYNODECACHE, HASH_LIST, M_CACHE,
    171  1.5.2.3  bouyer 	    M_WAITOK, &nmp->ovm_node_hash);
    172  1.5.2.2  bouyer 
    173  1.5.2.2  bouyer 	/*
    174  1.5.2.2  bouyer 	 * Fix up overlay node for root vnode
    175  1.5.2.2  bouyer 	 */
    176  1.5.2.2  bouyer 	error = layer_node_create(mp, lowerrootvp, &vp);
    177  1.5.2.2  bouyer 	/*
    178  1.5.2.2  bouyer 	 * Make sure the fixup worked
    179  1.5.2.2  bouyer 	 */
    180  1.5.2.2  bouyer 	if (error) {
    181  1.5.2.2  bouyer 		vput(lowerrootvp);
    182  1.5.2.2  bouyer 		free(nmp, M_UFSMNT);	/* XXX */
    183  1.5.2.2  bouyer 		return (error);
    184  1.5.2.2  bouyer 	}
    185  1.5.2.2  bouyer 	/*
    186  1.5.2.2  bouyer 	 * Unlock the node
    187  1.5.2.2  bouyer 	 */
    188  1.5.2.2  bouyer 	VOP_UNLOCK(vp, 0);
    189  1.5.2.2  bouyer 
    190  1.5.2.2  bouyer 	/*
    191  1.5.2.2  bouyer 	 * Keep a held reference to the root vnode.
    192  1.5.2.2  bouyer 	 * It is vrele'd in ov_unmount.
    193  1.5.2.2  bouyer 	 */
    194  1.5.2.2  bouyer 	vp->v_flag |= VROOT;
    195  1.5.2.2  bouyer 	nmp->ovm_rootvp = vp;
    196  1.5.2.2  bouyer 
    197  1.5.2.2  bouyer 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    198  1.5.2.2  bouyer 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
    199  1.5.2.2  bouyer 	(void) copyinstr(args.la.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    200  1.5.2.2  bouyer 	    &size);
    201  1.5.2.2  bouyer 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    202  1.5.2.2  bouyer #ifdef OVERLAYFS_DIAGNOSTIC
    203  1.5.2.2  bouyer 	printf("ov_mount: lower %s, alias at %s\n",
    204  1.5.2.2  bouyer 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    205  1.5.2.2  bouyer #endif
    206  1.5.2.2  bouyer 	return (0);
    207  1.5.2.2  bouyer }
    208  1.5.2.2  bouyer 
    209  1.5.2.2  bouyer /*
    210  1.5.2.2  bouyer  * Free reference to overlay layer
    211  1.5.2.2  bouyer  */
    212  1.5.2.2  bouyer int
    213  1.5.2.2  bouyer ov_unmount(mp, mntflags, p)
    214  1.5.2.2  bouyer 	struct mount *mp;
    215  1.5.2.2  bouyer 	int mntflags;
    216  1.5.2.2  bouyer 	struct proc *p;
    217  1.5.2.2  bouyer {
    218  1.5.2.2  bouyer 	struct vnode *overlay_rootvp = MOUNTTOOVERLAYMOUNT(mp)->ovm_rootvp;
    219  1.5.2.2  bouyer 	int error;
    220  1.5.2.2  bouyer 	int flags = 0;
    221  1.5.2.2  bouyer 
    222  1.5.2.2  bouyer #ifdef OVERLAYFS_DIAGNOSTIC
    223  1.5.2.2  bouyer 	printf("ov_unmount(mp = %p)\n", mp);
    224  1.5.2.2  bouyer #endif
    225  1.5.2.2  bouyer 
    226  1.5.2.2  bouyer 	if (mntflags & MNT_FORCE)
    227  1.5.2.2  bouyer 		flags |= FORCECLOSE;
    228  1.5.2.2  bouyer 
    229  1.5.2.2  bouyer 	/*
    230  1.5.2.2  bouyer 	 * Clear out buffer cache.  I don't think we
    231  1.5.2.2  bouyer 	 * ever get anything cached at this level at the
    232  1.5.2.2  bouyer 	 * moment, but who knows...
    233  1.5.2.2  bouyer 	 */
    234  1.5.2.2  bouyer #if 0
    235  1.5.2.2  bouyer 	mntflushbuf(mp, 0);
    236  1.5.2.2  bouyer 	if (mntinvalbuf(mp, 1))
    237  1.5.2.2  bouyer 		return (EBUSY);
    238  1.5.2.2  bouyer #endif
    239  1.5.2.2  bouyer 	if (overlay_rootvp->v_usecount > 1)
    240  1.5.2.2  bouyer 		return (EBUSY);
    241  1.5.2.2  bouyer 	if ((error = vflush(mp, overlay_rootvp, flags)) != 0)
    242  1.5.2.2  bouyer 		return (error);
    243  1.5.2.2  bouyer 
    244  1.5.2.2  bouyer #ifdef OVERLAYFS_DIAGNOSTIC
    245  1.5.2.2  bouyer 	vprint("alias root of lower", overlay_rootvp);
    246  1.5.2.2  bouyer #endif
    247  1.5.2.2  bouyer 	/*
    248  1.5.2.2  bouyer 	 * Release reference on underlying root vnode
    249  1.5.2.2  bouyer 	 */
    250  1.5.2.2  bouyer 	vrele(overlay_rootvp);
    251  1.5.2.2  bouyer 	/*
    252  1.5.2.2  bouyer 	 * And blow it away for future re-use
    253  1.5.2.2  bouyer 	 */
    254  1.5.2.2  bouyer 	vgone(overlay_rootvp);
    255  1.5.2.2  bouyer 	/*
    256  1.5.2.2  bouyer 	 * Finally, throw away the overlay_mount structure
    257  1.5.2.2  bouyer 	 */
    258  1.5.2.2  bouyer 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    259  1.5.2.2  bouyer 	mp->mnt_data = 0;
    260  1.5.2.2  bouyer 	return 0;
    261  1.5.2.2  bouyer }
    262  1.5.2.2  bouyer 
    263  1.5.2.2  bouyer extern struct vnodeopv_desc overlay_vnodeop_opv_desc;
    264  1.5.2.2  bouyer 
    265  1.5.2.2  bouyer struct vnodeopv_desc *ov_vnodeopv_descs[] = {
    266  1.5.2.2  bouyer 	&overlay_vnodeop_opv_desc,
    267  1.5.2.2  bouyer 	NULL,
    268  1.5.2.2  bouyer };
    269  1.5.2.2  bouyer 
    270  1.5.2.2  bouyer struct vfsops overlay_vfsops = {
    271  1.5.2.2  bouyer 	MOUNT_OVERLAY,
    272  1.5.2.2  bouyer 	ov_mount,
    273  1.5.2.2  bouyer 	layerfs_start,
    274  1.5.2.2  bouyer 	ov_unmount,
    275  1.5.2.2  bouyer 	layerfs_root,
    276  1.5.2.2  bouyer 	layerfs_quotactl,
    277  1.5.2.2  bouyer 	layerfs_statfs,
    278  1.5.2.2  bouyer 	layerfs_sync,
    279  1.5.2.2  bouyer 	layerfs_vget,
    280  1.5.2.2  bouyer 	layerfs_fhtovp,
    281  1.5.2.2  bouyer 	layerfs_vptofh,
    282  1.5.2.2  bouyer 	layerfs_init,
    283  1.5.2.2  bouyer 	layerfs_done,
    284  1.5.2.2  bouyer 	layerfs_sysctl,
    285  1.5.2.2  bouyer 	NULL,				/* vfs_mountroot */
    286  1.5.2.2  bouyer 	layerfs_checkexp,
    287  1.5.2.2  bouyer 	ov_vnodeopv_descs,
    288  1.5.2.2  bouyer };
    289