Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.86.12.2
      1  1.86.12.1       mrg /*	$NetBSD: umap_vfsops.c,v 1.86.12.2 2012/06/02 11:09:37 mrg Exp $	*/
      2        1.2       cgd 
      3        1.1   mycroft /*
      4        1.1   mycroft  * Copyright (c) 1992, 1993
      5        1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1   mycroft  *
      7        1.1   mycroft  * This code is derived from software donated to Berkeley by
      8        1.1   mycroft  * the UCLA Ficus project.
      9        1.1   mycroft  *
     10        1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11        1.1   mycroft  * modification, are permitted provided that the following conditions
     12        1.1   mycroft  * are met:
     13        1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14        1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15        1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17        1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.40       agc  * 3. Neither the name of the University nor the names of its contributors
     19        1.1   mycroft  *    may be used to endorse or promote products derived from this software
     20        1.1   mycroft  *    without specific prior written permission.
     21        1.1   mycroft  *
     22        1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23        1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24        1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25        1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26        1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27        1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28        1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29        1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30        1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31        1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32        1.1   mycroft  * SUCH DAMAGE.
     33        1.1   mycroft  *
     34        1.1   mycroft  *	from: @(#)null_vfsops.c       1.5 (Berkeley) 7/10/92
     35       1.18      fvdl  *	@(#)umap_vfsops.c	8.8 (Berkeley) 5/14/95
     36        1.1   mycroft  */
     37        1.1   mycroft 
     38        1.1   mycroft /*
     39        1.1   mycroft  * Umap Layer
     40        1.1   mycroft  * (See mount_umap(8) for a description of this layer.)
     41        1.1   mycroft  */
     42       1.32     lukem 
     43       1.32     lukem #include <sys/cdefs.h>
     44  1.86.12.1       mrg __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.86.12.2 2012/06/02 11:09:37 mrg Exp $");
     45        1.1   mycroft 
     46        1.1   mycroft #include <sys/param.h>
     47        1.1   mycroft #include <sys/systm.h>
     48       1.41    atatat #include <sys/sysctl.h>
     49       1.18      fvdl #include <sys/proc.h>
     50        1.1   mycroft #include <sys/time.h>
     51        1.1   mycroft #include <sys/vnode.h>
     52        1.1   mycroft #include <sys/mount.h>
     53        1.1   mycroft #include <sys/namei.h>
     54        1.1   mycroft #include <sys/malloc.h>
     55       1.57      elad #include <sys/kauth.h>
     56       1.78    rumble #include <sys/module.h>
     57       1.57      elad 
     58        1.1   mycroft #include <miscfs/umapfs/umap.h>
     59       1.23  wrstuden #include <miscfs/genfs/layer_extern.h>
     60        1.1   mycroft 
     61       1.83     pooka MODULE(MODULE_CLASS_VFS, umap, "layerfs");
     62       1.78    rumble 
     63       1.69     pooka VFS_PROTOS(umapfs);
     64        1.9  christos 
     65       1.80    rumble static struct sysctllog *umapfs_sysctl_log;
     66       1.80    rumble 
     67        1.1   mycroft /*
     68        1.1   mycroft  * Mount umap layer
     69        1.1   mycroft  */
     70        1.1   mycroft int
     71       1.82       dsl umapfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     72        1.1   mycroft {
     73       1.72     pooka 	struct lwp *l = curlwp;
     74       1.86  dholland 	struct pathbuf *pb;
     75       1.69     pooka 	struct nameidata nd;
     76       1.66       dsl 	struct umap_args *args = data;
     77        1.1   mycroft 	struct vnode *lowerrootvp, *vp;
     78        1.1   mycroft 	struct umap_mount *amp;
     79        1.1   mycroft 	int error;
     80       1.22  perseant #ifdef UMAPFS_DIAGNOSTIC
     81       1.22  perseant 	int i;
     82       1.22  perseant #endif
     83       1.56  christos 
     84       1.66       dsl 	if (*data_len < sizeof *args)
     85       1.66       dsl 		return EINVAL;
     86       1.66       dsl 
     87       1.35  christos 	if (mp->mnt_flag & MNT_GETARGS) {
     88       1.35  christos 		amp = MOUNTTOUMAPMOUNT(mp);
     89       1.35  christos 		if (amp == NULL)
     90       1.35  christos 			return EIO;
     91       1.66       dsl 		args->la.target = NULL;
     92       1.66       dsl 		args->nentries = amp->info_nentries;
     93       1.66       dsl 		args->gnentries = amp->info_gnentries;
     94       1.66       dsl 		*data_len = sizeof *args;
     95       1.66       dsl 		return 0;
     96       1.35  christos 	}
     97       1.21    bouyer 
     98       1.21    bouyer 	/* only for root */
     99  1.86.12.1       mrg 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    100  1.86.12.1       mrg 	    KAUTH_REQ_SYSTEM_MOUNT_UMAP, NULL, NULL, NULL);
    101  1.86.12.1       mrg 	if (error)
    102       1.21    bouyer 		return error;
    103        1.1   mycroft 
    104        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    105       1.15  christos 	printf("umapfs_mount(mp = %p)\n", mp);
    106        1.1   mycroft #endif
    107        1.1   mycroft 
    108        1.1   mycroft 	/*
    109       1.55      jmmv 	 * Update is not supported
    110       1.23  wrstuden 	 */
    111       1.55      jmmv 	if (mp->mnt_flag & MNT_UPDATE)
    112       1.55      jmmv 		return EOPNOTSUPP;
    113       1.23  wrstuden 
    114       1.23  wrstuden 	/*
    115        1.1   mycroft 	 * Find lower node
    116        1.1   mycroft 	 */
    117       1.86  dholland 	error = pathbuf_copyin(args->umap_target, &pb);
    118       1.86  dholland 	if (error) {
    119       1.86  dholland 		return error;
    120       1.86  dholland 	}
    121       1.86  dholland 	NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
    122       1.86  dholland 	if ((error = namei(&nd)) != 0) {
    123       1.86  dholland 		pathbuf_destroy(pb);
    124       1.86  dholland 		return error;
    125       1.86  dholland 	}
    126        1.1   mycroft 
    127        1.1   mycroft 	/*
    128        1.1   mycroft 	 * Sanity check on lower vnode
    129        1.1   mycroft 	 */
    130       1.69     pooka 	lowerrootvp = nd.ni_vp;
    131       1.86  dholland 	pathbuf_destroy(pb);
    132        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    133       1.15  christos 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
    134        1.1   mycroft #endif
    135        1.1   mycroft 
    136        1.1   mycroft 	if (lowerrootvp->v_type != VDIR) {
    137        1.1   mycroft 		vput(lowerrootvp);
    138        1.1   mycroft 		return (EINVAL);
    139        1.1   mycroft 	}
    140        1.1   mycroft 
    141        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    142       1.15  christos 	printf("mp = %p\n", mp);
    143        1.1   mycroft #endif
    144        1.1   mycroft 
    145  1.86.12.2       mrg 	amp = kmem_zalloc(sizeof(struct umap_mount), KM_SLEEP);
    146       1.34     soren 	mp->mnt_data = amp;
    147        1.1   mycroft 	amp->umapm_vfs = lowerrootvp->v_mount;
    148       1.23  wrstuden 	if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
    149       1.23  wrstuden 		mp->mnt_flag |= MNT_LOCAL;
    150        1.1   mycroft 
    151       1.51     perry 	/*
    152        1.1   mycroft 	 * Now copy in the number of entries and maps for umap mapping.
    153        1.1   mycroft 	 */
    154       1.66       dsl 	if (args->nentries > MAPFILEENTRIES || args->gnentries > GMAPFILEENTRIES) {
    155       1.29  jdolecek 		vput(lowerrootvp);
    156       1.29  jdolecek 		return (error);
    157       1.29  jdolecek 	}
    158       1.29  jdolecek 
    159       1.66       dsl 	amp->info_nentries = args->nentries;
    160       1.66       dsl 	amp->info_gnentries = args->gnentries;
    161       1.66       dsl 	error = copyin(args->mapdata, amp->info_mapdata,
    162       1.66       dsl 	    2*sizeof(u_long)*args->nentries);
    163       1.23  wrstuden 	if (error) {
    164       1.23  wrstuden 		vput(lowerrootvp);
    165        1.1   mycroft 		return (error);
    166       1.23  wrstuden 	}
    167        1.1   mycroft 
    168       1.22  perseant #ifdef UMAPFS_DIAGNOSTIC
    169       1.66       dsl 	printf("umap_mount:nentries %d\n",args->nentries);
    170       1.66       dsl 	for (i = 0; i < args->nentries; i++)
    171       1.22  perseant 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
    172        1.1   mycroft 	 	    amp->info_mapdata[i][1]);
    173        1.1   mycroft #endif
    174        1.1   mycroft 
    175       1.66       dsl 	error = copyin(args->gmapdata, amp->info_gmapdata,
    176       1.66       dsl 	    2*sizeof(u_long)*args->gnentries);
    177       1.23  wrstuden 	if (error) {
    178       1.23  wrstuden 		vput(lowerrootvp);
    179        1.1   mycroft 		return (error);
    180       1.23  wrstuden 	}
    181        1.1   mycroft 
    182       1.22  perseant #ifdef UMAPFS_DIAGNOSTIC
    183       1.66       dsl 	printf("umap_mount:gnentries %d\n",args->gnentries);
    184       1.66       dsl 	for (i = 0; i < args->gnentries; i++)
    185       1.51     perry 		printf("\tgroup %ld maps to %ld\n",
    186        1.1   mycroft 		    amp->info_gmapdata[i][0],
    187        1.1   mycroft 	 	    amp->info_gmapdata[i][1]);
    188        1.1   mycroft #endif
    189        1.1   mycroft 
    190        1.1   mycroft 	/*
    191       1.23  wrstuden 	 * Make sure the mount point's sufficiently initialized
    192       1.23  wrstuden 	 * that the node create call will work.
    193        1.1   mycroft 	 */
    194       1.25     assar 	vfs_getnewfsid(mp);
    195       1.23  wrstuden 	amp->umapm_size = sizeof(struct umap_node);
    196       1.23  wrstuden 	amp->umapm_tag = VT_UMAP;
    197       1.23  wrstuden 	amp->umapm_bypass = umap_bypass;
    198       1.23  wrstuden 	amp->umapm_alloc = layer_node_alloc;	/* the default alloc is fine */
    199       1.23  wrstuden 	amp->umapm_vnodeop_p = umap_vnodeop_p;
    200       1.71        ad 	mutex_init(&amp->umapm_hashlock, MUTEX_DEFAULT, IPL_NONE);
    201       1.77        ad 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, true,
    202       1.77        ad 	    &amp->umapm_node_hash);
    203       1.23  wrstuden 
    204       1.23  wrstuden 
    205        1.1   mycroft 	/*
    206       1.23  wrstuden 	 * fix up umap node for root vnode.
    207        1.1   mycroft 	 */
    208       1.23  wrstuden 	error = layer_node_create(mp, lowerrootvp, &vp);
    209        1.1   mycroft 	/*
    210        1.1   mycroft 	 * Make sure the node alias worked
    211        1.1   mycroft 	 */
    212        1.1   mycroft 	if (error) {
    213       1.23  wrstuden 		vput(lowerrootvp);
    214       1.77        ad 		hashdone(amp->umapm_node_hashtbl, HASH_LIST,
    215       1.77        ad 		    amp->umapm_node_hash);
    216  1.86.12.2       mrg 		kmem_free(amp, sizeof(struct umap_mount));
    217  1.86.12.2       mrg 		return error;
    218        1.1   mycroft 	}
    219       1.23  wrstuden 	/*
    220       1.23  wrstuden 	 * Unlock the node (either the lower or the alias)
    221       1.23  wrstuden 	 */
    222       1.70        ad 	vp->v_vflag |= VV_ROOT;
    223       1.85   hannken 	VOP_UNLOCK(vp);
    224        1.1   mycroft 
    225        1.1   mycroft 	/*
    226        1.1   mycroft 	 * Keep a held reference to the root vnode.
    227        1.1   mycroft 	 * It is vrele'd in umapfs_unmount.
    228        1.1   mycroft 	 */
    229       1.23  wrstuden 	amp->umapm_rootvp = vp;
    230        1.1   mycroft 
    231       1.66       dsl 	error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target,
    232       1.67     pooka 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    233        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    234       1.11  christos 	printf("umapfs_mount: lower %s, alias at %s\n",
    235        1.1   mycroft 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    236        1.1   mycroft #endif
    237       1.36  christos 	return error;
    238        1.1   mycroft }
    239        1.1   mycroft 
    240        1.1   mycroft /*
    241        1.1   mycroft  * Free reference to umap layer
    242        1.1   mycroft  */
    243        1.1   mycroft int
    244       1.72     pooka umapfs_unmount(struct mount *mp, int mntflags)
    245        1.1   mycroft {
    246       1.71        ad 	struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp);
    247       1.71        ad 	struct vnode *rtvp = amp->umapm_rootvp;
    248        1.1   mycroft 	int error;
    249        1.1   mycroft 	int flags = 0;
    250        1.1   mycroft 
    251        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    252       1.15  christos 	printf("umapfs_unmount(mp = %p)\n", mp);
    253        1.1   mycroft #endif
    254        1.1   mycroft 
    255       1.18      fvdl 	if (mntflags & MNT_FORCE)
    256        1.1   mycroft 		flags |= FORCECLOSE;
    257        1.1   mycroft 
    258       1.65     pooka 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    259        1.1   mycroft 		return (EBUSY);
    260       1.53  christos 	if ((error = vflush(mp, rtvp, flags)) != 0)
    261        1.1   mycroft 		return (error);
    262        1.1   mycroft 
    263        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    264       1.53  christos 	vprint("alias root of lower", rtvp);
    265       1.51     perry #endif
    266        1.1   mycroft 	/*
    267       1.74        ad 	 * Blow it away for future re-use
    268        1.1   mycroft 	 */
    269       1.53  christos 	vgone(rtvp);
    270        1.1   mycroft 	/*
    271        1.1   mycroft 	 * Finally, throw away the umap_mount structure
    272        1.1   mycroft 	 */
    273       1.71        ad 	mutex_destroy(&amp->umapm_hashlock);
    274       1.77        ad 	hashdone(amp->umapm_node_hashtbl, HASH_LIST, amp->umapm_node_hash);
    275  1.86.12.2       mrg 	kmem_free(amp, sizeof(struct umap_mount));
    276       1.79    simonb 	mp->mnt_data = NULL;
    277  1.86.12.2       mrg 	return 0;
    278        1.1   mycroft }
    279        1.1   mycroft 
    280       1.27  jdolecek extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    281       1.17   thorpej 
    282       1.27  jdolecek const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
    283       1.17   thorpej 	&umapfs_vnodeop_opv_desc,
    284       1.17   thorpej 	NULL,
    285       1.17   thorpej };
    286       1.17   thorpej 
    287       1.16   thorpej struct vfsops umapfs_vfsops = {
    288        1.1   mycroft 	MOUNT_UMAP,
    289       1.66       dsl 	sizeof (struct umap_args),
    290        1.1   mycroft 	umapfs_mount,
    291       1.23  wrstuden 	layerfs_start,
    292        1.1   mycroft 	umapfs_unmount,
    293       1.23  wrstuden 	layerfs_root,
    294       1.23  wrstuden 	layerfs_quotactl,
    295       1.44  christos 	layerfs_statvfs,
    296       1.23  wrstuden 	layerfs_sync,
    297       1.23  wrstuden 	layerfs_vget,
    298       1.23  wrstuden 	layerfs_fhtovp,
    299       1.23  wrstuden 	layerfs_vptofh,
    300       1.23  wrstuden 	layerfs_init,
    301       1.31       chs 	NULL,
    302       1.24  jdolecek 	layerfs_done,
    303       1.17   thorpej 	NULL,				/* vfs_mountroot */
    304       1.48  wrstuden 	layerfs_snapshot,
    305       1.50   thorpej 	vfs_stdextattrctl,
    306       1.68     pooka 	(void *)eopnotsupp,		/* vfs_suspendctl */
    307       1.75  dholland 	layerfs_renamelock_enter,
    308       1.75  dholland 	layerfs_renamelock_exit,
    309       1.76        ad 	(void *)eopnotsupp,
    310       1.17   thorpej 	umapfs_vnodeopv_descs,
    311       1.59  christos 	0,				/* vfs_refcount */
    312       1.59  christos 	{ NULL, NULL },
    313        1.1   mycroft };
    314       1.78    rumble 
    315       1.78    rumble static int
    316       1.84   mlelstv umap_modcmd(modcmd_t cmd, void *arg)
    317       1.78    rumble {
    318       1.80    rumble 	int error;
    319       1.78    rumble 
    320       1.78    rumble 	switch (cmd) {
    321       1.78    rumble 	case MODULE_CMD_INIT:
    322       1.80    rumble 		error = vfs_attach(&umapfs_vfsops);
    323       1.80    rumble 		if (error != 0)
    324       1.80    rumble 			break;
    325       1.80    rumble 		sysctl_createv(&umapfs_sysctl_log, 0, NULL, NULL,
    326       1.80    rumble 			       CTLFLAG_PERMANENT,
    327       1.80    rumble 			       CTLTYPE_NODE, "vfs", NULL,
    328       1.80    rumble 			       NULL, 0, NULL, 0,
    329       1.80    rumble 			       CTL_VFS, CTL_EOL);
    330       1.80    rumble 		sysctl_createv(&umapfs_sysctl_log, 0, NULL, NULL,
    331       1.80    rumble 			       CTLFLAG_PERMANENT,
    332       1.80    rumble 			       CTLTYPE_NODE, "umap",
    333       1.80    rumble 			       SYSCTL_DESCR("UID/GID remapping file system"),
    334       1.80    rumble 			       NULL, 0, NULL, 0,
    335       1.80    rumble 			       CTL_VFS, 10, CTL_EOL);
    336       1.80    rumble 		/*
    337       1.80    rumble 		 * XXX the "10" above could be dynamic, thereby eliminating
    338       1.80    rumble 		 * one more instance of the "number to vfs" mapping problem,
    339       1.80    rumble 		 * but "10" is the order as taken from sys/mount.h
    340       1.80    rumble 		 */
    341       1.80    rumble 		break;
    342       1.78    rumble 	case MODULE_CMD_FINI:
    343       1.80    rumble 		error = vfs_detach(&umapfs_vfsops);
    344       1.80    rumble 		if (error != 0)
    345       1.80    rumble 			break;
    346       1.80    rumble 		sysctl_teardown(&umapfs_sysctl_log);
    347       1.80    rumble 		break;
    348       1.78    rumble 	default:
    349       1.80    rumble 		error = ENOTTY;
    350       1.80    rumble 		break;
    351       1.78    rumble 	}
    352       1.80    rumble 
    353       1.80    rumble 	return (error);
    354       1.78    rumble }
    355