Home | History | Annotate | Line # | Download | only in nullfs
null_vfsops.c revision 1.98.2.1
      1  1.98.2.1    bouyer /*	$NetBSD: null_vfsops.c,v 1.98.2.1 2020/04/20 11:29:11 bouyer Exp $	*/
      2       1.2       cgd 
      3       1.1   mycroft /*
      4      1.24  wrstuden  * Copyright (c) 1999 National Aeronautics & Space Administration
      5      1.24  wrstuden  * All rights reserved.
      6      1.24  wrstuden  *
      7      1.24  wrstuden  * This software was written by William Studenmund of the
      8      1.30       wiz  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
      9      1.24  wrstuden  *
     10      1.24  wrstuden  * Redistribution and use in source and binary forms, with or without
     11      1.24  wrstuden  * modification, are permitted provided that the following conditions
     12      1.24  wrstuden  * are met:
     13      1.24  wrstuden  * 1. Redistributions of source code must retain the above copyright
     14      1.24  wrstuden  *    notice, this list of conditions and the following disclaimer.
     15      1.24  wrstuden  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.24  wrstuden  *    notice, this list of conditions and the following disclaimer in the
     17      1.24  wrstuden  *    documentation and/or other materials provided with the distribution.
     18      1.25     soren  * 3. Neither the name of the National Aeronautics & Space Administration
     19      1.24  wrstuden  *    nor the names of its contributors may be used to endorse or promote
     20      1.24  wrstuden  *    products derived from this software without specific prior written
     21      1.24  wrstuden  *    permission.
     22      1.24  wrstuden  *
     23      1.24  wrstuden  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24      1.24  wrstuden  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25      1.24  wrstuden  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26      1.24  wrstuden  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27      1.24  wrstuden  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28      1.24  wrstuden  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29      1.24  wrstuden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30      1.24  wrstuden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31      1.24  wrstuden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32      1.24  wrstuden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33      1.24  wrstuden  * POSSIBILITY OF SUCH DAMAGE.
     34      1.24  wrstuden  */
     35      1.82     rmind 
     36      1.24  wrstuden /*
     37      1.19      fvdl  * Copyright (c) 1992, 1993, 1995
     38       1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     39       1.1   mycroft  *
     40       1.1   mycroft  * This code is derived from software donated to Berkeley by
     41       1.1   mycroft  * Jan-Simon Pendry.
     42       1.1   mycroft  *
     43       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     44       1.1   mycroft  * modification, are permitted provided that the following conditions
     45       1.1   mycroft  * are met:
     46       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     47       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     48       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     49       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     50       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     51      1.43       agc  * 3. Neither the name of the University nor the names of its contributors
     52       1.1   mycroft  *    may be used to endorse or promote products derived from this software
     53       1.1   mycroft  *    without specific prior written permission.
     54       1.1   mycroft  *
     55       1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56       1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57       1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58       1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59       1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60       1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61       1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62       1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63       1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64       1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65       1.1   mycroft  * SUCH DAMAGE.
     66       1.1   mycroft  *
     67       1.1   mycroft  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
     68       1.1   mycroft  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
     69      1.19      fvdl  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
     70       1.1   mycroft  */
     71       1.1   mycroft 
     72       1.1   mycroft /*
     73      1.82     rmind  * Null file-system: VFS operations.
     74      1.82     rmind  *
     75      1.82     rmind  * See null_vnops.c for a description.
     76       1.1   mycroft  */
     77      1.36     lukem 
     78      1.36     lukem #include <sys/cdefs.h>
     79  1.98.2.1    bouyer __KERNEL_RCSID(0, "$NetBSD: null_vfsops.c,v 1.98.2.1 2020/04/20 11:29:11 bouyer Exp $");
     80       1.1   mycroft 
     81       1.1   mycroft #include <sys/param.h>
     82       1.1   mycroft #include <sys/systm.h>
     83      1.44    atatat #include <sys/sysctl.h>
     84       1.1   mycroft #include <sys/vnode.h>
     85       1.1   mycroft #include <sys/mount.h>
     86       1.1   mycroft #include <sys/namei.h>
     87      1.76    rumble #include <sys/module.h>
     88      1.33     enami 
     89       1.1   mycroft #include <miscfs/nullfs/null.h>
     90      1.24  wrstuden #include <miscfs/genfs/layer_extern.h>
     91       1.1   mycroft 
     92      1.80       jld MODULE(MODULE_CLASS_VFS, null, "layerfs");
     93      1.76    rumble 
     94      1.68     pooka VFS_PROTOS(nullfs);
     95      1.24  wrstuden 
     96       1.1   mycroft int
     97      1.79       dsl nullfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     98       1.1   mycroft {
     99      1.82     rmind 	struct vnode *lowerrootvp, *vp;
    100      1.65       dsl 	struct null_args *args = data;
    101      1.24  wrstuden 	struct null_mount *nmp;
    102      1.24  wrstuden 	struct layer_mount *lmp;
    103      1.83  dholland 	struct pathbuf *pb;
    104      1.82     rmind 	struct nameidata nd;
    105      1.82     rmind 	int error;
    106       1.1   mycroft 
    107      1.88      maxv 	if (args == NULL)
    108      1.88      maxv 		return EINVAL;
    109      1.82     rmind 	if (*data_len < sizeof(*args))
    110      1.65       dsl 		return EINVAL;
    111      1.65       dsl 
    112      1.38  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    113      1.38  christos 		lmp = MOUNTTOLAYERMOUNT(mp);
    114      1.38  christos 		if (lmp == NULL)
    115      1.38  christos 			return EIO;
    116      1.65       dsl 		args->la.target = NULL;
    117      1.82     rmind 		*data_len = sizeof(*args);
    118      1.65       dsl 		return 0;
    119      1.38  christos 	}
    120       1.1   mycroft 
    121      1.82     rmind 	/* Update is not supported. */
    122      1.57      jmmv 	if (mp->mnt_flag & MNT_UPDATE)
    123      1.57      jmmv 		return EOPNOTSUPP;
    124      1.24  wrstuden 
    125      1.82     rmind 	/* Find the lower vnode and lock it. */
    126      1.83  dholland 	error = pathbuf_copyin(args->la.target, &pb);
    127      1.83  dholland 	if (error) {
    128      1.83  dholland 		return error;
    129      1.83  dholland 	}
    130      1.83  dholland 	NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
    131      1.82     rmind 	if ((error = namei(&nd)) != 0) {
    132      1.83  dholland 		pathbuf_destroy(pb);
    133      1.82     rmind 		return error;
    134      1.82     rmind 	}
    135      1.68     pooka 	lowerrootvp = nd.ni_vp;
    136      1.83  dholland 	pathbuf_destroy(pb);
    137       1.1   mycroft 
    138      1.82     rmind 	/* Create the mount point. */
    139      1.84     rmind 	nmp = kmem_zalloc(sizeof(struct null_mount), KM_SLEEP);
    140      1.37     soren 	mp->mnt_data = nmp;
    141      1.96     joerg 	mp->mnt_iflag |= IMNT_MPSAFE;
    142      1.98        ad 	mp->mnt_iflag |= lowerrootvp->v_mount->mnt_iflag & IMNT_SHRLOOKUP;
    143       1.1   mycroft 
    144       1.1   mycroft 	/*
    145      1.24  wrstuden 	 * Make sure that the mount point is sufficiently initialized
    146      1.24  wrstuden 	 * that the node create call will work.
    147       1.1   mycroft 	 */
    148      1.27     assar 	vfs_getnewfsid(mp);
    149      1.95   hannken 	mp->mnt_lower = lowerrootvp->v_mount;
    150      1.24  wrstuden 
    151      1.33     enami 	nmp->nullm_size = sizeof(struct null_node);
    152      1.24  wrstuden 	nmp->nullm_tag = VT_NULL;
    153      1.24  wrstuden 	nmp->nullm_bypass = layer_bypass;
    154      1.24  wrstuden 	nmp->nullm_vnodeop_p = null_vnodeop_p;
    155       1.1   mycroft 
    156      1.82     rmind 	/* Setup a null node for root vnode. */
    157      1.85   hannken 	VOP_UNLOCK(lowerrootvp);
    158      1.24  wrstuden 	error = layer_node_create(mp, lowerrootvp, &vp);
    159       1.1   mycroft 	if (error) {
    160      1.85   hannken 		vrele(lowerrootvp);
    161      1.84     rmind 		kmem_free(nmp, sizeof(struct null_mount));
    162      1.82     rmind 		return error;
    163       1.1   mycroft 	}
    164      1.21  wrstuden 	/*
    165      1.82     rmind 	 * Keep a held reference to the root vnode.  It will be released on
    166      1.82     rmind 	 * umount.  Note: nullfs is MP-safe.
    167      1.21  wrstuden 	 */
    168      1.85   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    169      1.69        ad 	vp->v_vflag |= VV_ROOT;
    170      1.72        ad 	nmp->nullm_rootvp = vp;
    171      1.81   hannken 	VOP_UNLOCK(vp);
    172       1.1   mycroft 
    173      1.65       dsl 	error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
    174      1.82     rmind 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, curlwp);
    175      1.93   hannken 	if (error)
    176      1.93   hannken 		return error;
    177      1.94   hannken 
    178      1.94   hannken 	if (mp->mnt_lower->mnt_flag & MNT_LOCAL)
    179      1.94   hannken 		mp->mnt_flag |= MNT_LOCAL;
    180      1.93   hannken 	return 0;
    181       1.1   mycroft }
    182       1.1   mycroft 
    183       1.1   mycroft int
    184      1.70     pooka nullfs_unmount(struct mount *mp, int mntflags)
    185       1.1   mycroft {
    186      1.34     enami 	struct null_mount *nmp = MOUNTTONULLMOUNT(mp);
    187      1.34     enami 	struct vnode *null_rootvp = nmp->nullm_rootvp;
    188      1.82     rmind 	int error, flags = 0;
    189       1.1   mycroft 
    190      1.19      fvdl 	if (mntflags & MNT_FORCE)
    191       1.1   mycroft 		flags |= FORCECLOSE;
    192       1.1   mycroft 
    193  1.98.2.1    bouyer 	if (vrefcnt(null_rootvp) > 1 && (mntflags & MNT_FORCE) == 0)
    194      1.82     rmind 		return EBUSY;
    195      1.82     rmind 
    196      1.24  wrstuden 	if ((error = vflush(mp, null_rootvp, flags)) != 0)
    197      1.82     rmind 		return error;
    198       1.1   mycroft 
    199      1.82     rmind 	/* Eliminate all activity and release the vnode. */
    200      1.24  wrstuden 	vgone(null_rootvp);
    201      1.33     enami 
    202      1.82     rmind 	/* Finally, destroy the mount point structures. */
    203      1.84     rmind 	kmem_free(mp->mnt_data, sizeof(struct null_mount));
    204      1.33     enami 	mp->mnt_data = NULL;
    205      1.82     rmind 	return 0;
    206       1.1   mycroft }
    207       1.1   mycroft 
    208      1.29  jdolecek extern const struct vnodeopv_desc null_vnodeop_opv_desc;
    209      1.18   thorpej 
    210      1.29  jdolecek const struct vnodeopv_desc * const nullfs_vnodeopv_descs[] = {
    211      1.19      fvdl 	&null_vnodeop_opv_desc,
    212      1.18   thorpej 	NULL,
    213      1.18   thorpej };
    214      1.18   thorpej 
    215      1.17   thorpej struct vfsops nullfs_vfsops = {
    216      1.87   hannken 	.vfs_name = MOUNT_NULL,
    217      1.87   hannken 	.vfs_min_mount_data = sizeof (struct null_args),
    218      1.87   hannken 	.vfs_mount = nullfs_mount,
    219      1.87   hannken 	.vfs_start = layerfs_start,
    220      1.87   hannken 	.vfs_unmount = nullfs_unmount,
    221      1.87   hannken 	.vfs_root = layerfs_root,
    222      1.87   hannken 	.vfs_quotactl = layerfs_quotactl,
    223      1.87   hannken 	.vfs_statvfs = layerfs_statvfs,
    224      1.87   hannken 	.vfs_sync = layerfs_sync,
    225      1.89   hannken 	.vfs_loadvnode = layerfs_loadvnode,
    226      1.87   hannken 	.vfs_vget = layerfs_vget,
    227      1.87   hannken 	.vfs_fhtovp = layerfs_fhtovp,
    228      1.87   hannken 	.vfs_vptofh = layerfs_vptofh,
    229      1.87   hannken 	.vfs_init = layerfs_init,
    230      1.87   hannken 	.vfs_done = layerfs_done,
    231      1.87   hannken 	.vfs_snapshot = layerfs_snapshot,
    232      1.87   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    233      1.91   hannken 	.vfs_suspendctl = layerfs_suspendctl,
    234      1.87   hannken 	.vfs_renamelock_enter = layerfs_renamelock_enter,
    235      1.87   hannken 	.vfs_renamelock_exit = layerfs_renamelock_exit,
    236      1.87   hannken 	.vfs_fsync = (void *)eopnotsupp,
    237      1.87   hannken 	.vfs_opv_descs = nullfs_vnodeopv_descs
    238       1.1   mycroft };
    239      1.76    rumble 
    240      1.97  pgoyette SYSCTL_SETUP(nullfs_sysctl_setup, "nullfs sysctl")
    241      1.97  pgoyette {
    242      1.97  pgoyette 
    243      1.97  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    244      1.97  pgoyette 	    CTLFLAG_PERMANENT,
    245      1.97  pgoyette 	    CTLTYPE_NODE, "null",
    246      1.97  pgoyette 	    SYSCTL_DESCR("Loopback file system"),
    247      1.97  pgoyette 	    NULL, 0, NULL, 0,
    248      1.97  pgoyette 	    CTL_VFS, 9, CTL_EOL);
    249      1.97  pgoyette 	/*
    250      1.97  pgoyette 	 * XXX the "9" above could be dynamic, thereby eliminating
    251      1.97  pgoyette 	 * one more instance of the "number to vfs" mapping problem,
    252      1.97  pgoyette 	 * but "9" is the order as taken from sys/mount.h
    253      1.97  pgoyette 	 */
    254      1.97  pgoyette }
    255      1.97  pgoyette 
    256      1.76    rumble static int
    257      1.80       jld null_modcmd(modcmd_t cmd, void *arg)
    258      1.76    rumble {
    259      1.77        ad 	int error;
    260      1.76    rumble 
    261      1.76    rumble 	switch (cmd) {
    262      1.76    rumble 	case MODULE_CMD_INIT:
    263      1.77        ad 		error = vfs_attach(&nullfs_vfsops);
    264      1.77        ad 		if (error != 0)
    265      1.77        ad 			break;
    266      1.77        ad 		break;
    267      1.76    rumble 	case MODULE_CMD_FINI:
    268      1.77        ad 		error = vfs_detach(&nullfs_vfsops);
    269      1.77        ad 		if (error != 0)
    270      1.77        ad 			break;
    271      1.77        ad 		break;
    272      1.76    rumble 	default:
    273      1.77        ad 		error = ENOTTY;
    274      1.77        ad 		break;
    275      1.76    rumble 	}
    276      1.77        ad 	return error;
    277      1.76    rumble }
    278