Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.56.10.2
      1  1.56.10.2      elad /*	$NetBSD: umap_vfsops.c,v 1.56.10.2 2006/03/10 13:57:49 elad 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.56.10.2      elad __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.56.10.2 2006/03/10 13:57:49 elad 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.1   mycroft #include <miscfs/umapfs/umap.h>
     56       1.23  wrstuden #include <miscfs/genfs/layer_extern.h>
     57        1.1   mycroft 
     58       1.54   xtraeme int	umapfs_mount(struct mount *, const char *, void *,
     59       1.56  christos 			  struct nameidata *, struct lwp *);
     60       1.56  christos int	umapfs_unmount(struct mount *, int, struct lwp *);
     61        1.9  christos 
     62        1.1   mycroft /*
     63        1.1   mycroft  * Mount umap layer
     64        1.1   mycroft  */
     65        1.1   mycroft int
     66       1.56  christos umapfs_mount(mp, path, data, ndp, l)
     67        1.1   mycroft 	struct mount *mp;
     68       1.12       cgd 	const char *path;
     69       1.12       cgd 	void *data;
     70        1.1   mycroft 	struct nameidata *ndp;
     71       1.56  christos 	struct lwp *l;
     72        1.1   mycroft {
     73        1.1   mycroft 	struct umap_args args;
     74        1.1   mycroft 	struct vnode *lowerrootvp, *vp;
     75        1.1   mycroft 	struct umap_mount *amp;
     76       1.56  christos 	struct proc *p = l->l_proc;
     77        1.1   mycroft 	int error;
     78       1.22  perseant #ifdef UMAPFS_DIAGNOSTIC
     79       1.22  perseant 	int i;
     80       1.22  perseant #endif
     81       1.56  christos 
     82       1.35  christos 	if (mp->mnt_flag & MNT_GETARGS) {
     83       1.35  christos 		amp = MOUNTTOUMAPMOUNT(mp);
     84       1.35  christos 		if (amp == NULL)
     85       1.35  christos 			return EIO;
     86       1.35  christos 		args.la.target = NULL;
     87       1.35  christos 		args.nentries = amp->info_nentries;
     88       1.35  christos 		args.gnentries = amp->info_gnentries;
     89       1.35  christos 		return copyout(&args, data, sizeof(args));
     90       1.35  christos 	}
     91       1.21    bouyer 
     92       1.21    bouyer 	/* only for root */
     93  1.56.10.2      elad 	if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
     94  1.56.10.2      elad 	    &p->p_acflag)) != 0)
     95       1.21    bouyer 		return error;
     96        1.1   mycroft 
     97        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
     98       1.15  christos 	printf("umapfs_mount(mp = %p)\n", mp);
     99        1.1   mycroft #endif
    100        1.1   mycroft 
    101        1.1   mycroft 	/*
    102        1.1   mycroft 	 * Get argument
    103        1.1   mycroft 	 */
    104       1.45       jrf 	error = copyin(data, &args, sizeof(struct umap_args));
    105        1.9  christos 	if (error)
    106        1.1   mycroft 		return (error);
    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.28     assar 	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF,
    118       1.56  christos 		UIO_USERSPACE, args.umap_target, l);
    119        1.9  christos 	if ((error = namei(ndp)) != 0)
    120        1.1   mycroft 		return (error);
    121        1.1   mycroft 
    122        1.1   mycroft 	/*
    123        1.1   mycroft 	 * Sanity check on lower vnode
    124        1.1   mycroft 	 */
    125        1.1   mycroft 	lowerrootvp = ndp->ni_vp;
    126        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    127       1.15  christos 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
    128        1.1   mycroft #endif
    129        1.1   mycroft 
    130        1.1   mycroft 	if (lowerrootvp->v_type != VDIR) {
    131        1.1   mycroft 		vput(lowerrootvp);
    132        1.1   mycroft 		return (EINVAL);
    133        1.1   mycroft 	}
    134        1.1   mycroft 
    135        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    136       1.15  christos 	printf("mp = %p\n", mp);
    137        1.1   mycroft #endif
    138        1.1   mycroft 
    139        1.1   mycroft 	amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
    140        1.1   mycroft 				M_UFSMNT, M_WAITOK);	/* XXX */
    141       1.45       jrf 	memset(amp, 0, sizeof(struct umap_mount));
    142        1.1   mycroft 
    143       1.34     soren 	mp->mnt_data = amp;
    144       1.49   hannken 	mp->mnt_leaf = lowerrootvp->v_mount->mnt_leaf;
    145        1.1   mycroft 	amp->umapm_vfs = lowerrootvp->v_mount;
    146       1.23  wrstuden 	if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
    147       1.23  wrstuden 		mp->mnt_flag |= MNT_LOCAL;
    148        1.1   mycroft 
    149       1.51     perry 	/*
    150        1.1   mycroft 	 * Now copy in the number of entries and maps for umap mapping.
    151        1.1   mycroft 	 */
    152       1.30        tv 	if (args.nentries > MAPFILEENTRIES || args.gnentries > GMAPFILEENTRIES) {
    153       1.29  jdolecek 		vput(lowerrootvp);
    154       1.29  jdolecek 		return (error);
    155       1.29  jdolecek 	}
    156       1.29  jdolecek 
    157        1.1   mycroft 	amp->info_nentries = args.nentries;
    158        1.1   mycroft 	amp->info_gnentries = args.gnentries;
    159       1.51     perry 	error = copyin(args.mapdata, amp->info_mapdata,
    160        1.1   mycroft 	    2*sizeof(u_long)*args.nentries);
    161       1.23  wrstuden 	if (error) {
    162       1.23  wrstuden 		vput(lowerrootvp);
    163        1.1   mycroft 		return (error);
    164       1.23  wrstuden 	}
    165        1.1   mycroft 
    166       1.22  perseant #ifdef UMAPFS_DIAGNOSTIC
    167       1.11  christos 	printf("umap_mount:nentries %d\n",args.nentries);
    168        1.1   mycroft 	for (i = 0; i < args.nentries; i++)
    169       1.22  perseant 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
    170        1.1   mycroft 	 	    amp->info_mapdata[i][1]);
    171        1.1   mycroft #endif
    172        1.1   mycroft 
    173       1.51     perry 	error = copyin(args.gmapdata, amp->info_gmapdata,
    174       1.13     mikel 	    2*sizeof(u_long)*args.gnentries);
    175       1.23  wrstuden 	if (error) {
    176       1.23  wrstuden 		vput(lowerrootvp);
    177        1.1   mycroft 		return (error);
    178       1.23  wrstuden 	}
    179        1.1   mycroft 
    180       1.22  perseant #ifdef UMAPFS_DIAGNOSTIC
    181       1.11  christos 	printf("umap_mount:gnentries %d\n",args.gnentries);
    182        1.1   mycroft 	for (i = 0; i < args.gnentries; i++)
    183       1.51     perry 		printf("\tgroup %ld maps to %ld\n",
    184        1.1   mycroft 		    amp->info_gmapdata[i][0],
    185        1.1   mycroft 	 	    amp->info_gmapdata[i][1]);
    186        1.1   mycroft #endif
    187        1.1   mycroft 
    188        1.1   mycroft 	/*
    189       1.23  wrstuden 	 * Make sure the mount point's sufficiently initialized
    190       1.23  wrstuden 	 * that the node create call will work.
    191        1.1   mycroft 	 */
    192       1.25     assar 	vfs_getnewfsid(mp);
    193       1.23  wrstuden 	amp->umapm_size = sizeof(struct umap_node);
    194       1.23  wrstuden 	amp->umapm_tag = VT_UMAP;
    195       1.23  wrstuden 	amp->umapm_bypass = umap_bypass;
    196       1.23  wrstuden 	amp->umapm_alloc = layer_node_alloc;	/* the default alloc is fine */
    197       1.23  wrstuden 	amp->umapm_vnodeop_p = umap_vnodeop_p;
    198       1.23  wrstuden 	simple_lock_init(&amp->umapm_hashlock);
    199       1.26        ad 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE,
    200       1.26        ad 	    M_WAITOK, &amp->umapm_node_hash);
    201       1.23  wrstuden 
    202       1.23  wrstuden 
    203        1.1   mycroft 	/*
    204       1.23  wrstuden 	 * fix up umap node for root vnode.
    205        1.1   mycroft 	 */
    206       1.23  wrstuden 	error = layer_node_create(mp, lowerrootvp, &vp);
    207        1.1   mycroft 	/*
    208        1.1   mycroft 	 * Make sure the node alias worked
    209        1.1   mycroft 	 */
    210        1.1   mycroft 	if (error) {
    211       1.23  wrstuden 		vput(lowerrootvp);
    212        1.1   mycroft 		free(amp, M_UFSMNT);	/* XXX */
    213        1.1   mycroft 		return (error);
    214        1.1   mycroft 	}
    215       1.23  wrstuden 	/*
    216       1.23  wrstuden 	 * Unlock the node (either the lower or the alias)
    217       1.23  wrstuden 	 */
    218       1.23  wrstuden 	VOP_UNLOCK(vp, 0);
    219        1.1   mycroft 
    220        1.1   mycroft 	/*
    221        1.1   mycroft 	 * Keep a held reference to the root vnode.
    222        1.1   mycroft 	 * It is vrele'd in umapfs_unmount.
    223        1.1   mycroft 	 */
    224       1.23  wrstuden 	vp->v_flag |= VROOT;
    225       1.23  wrstuden 	amp->umapm_rootvp = vp;
    226        1.1   mycroft 
    227       1.44  christos 	error = set_statvfs_info(path, UIO_USERSPACE, args.umap_target,
    228       1.56  christos 	    UIO_USERSPACE, mp, l);
    229        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    230       1.11  christos 	printf("umapfs_mount: lower %s, alias at %s\n",
    231        1.1   mycroft 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    232        1.1   mycroft #endif
    233       1.36  christos 	return error;
    234        1.1   mycroft }
    235        1.1   mycroft 
    236        1.1   mycroft /*
    237        1.1   mycroft  * Free reference to umap layer
    238        1.1   mycroft  */
    239        1.1   mycroft int
    240       1.56  christos umapfs_unmount(mp, mntflags, l)
    241        1.1   mycroft 	struct mount *mp;
    242        1.1   mycroft 	int mntflags;
    243       1.56  christos 	struct lwp *l;
    244        1.1   mycroft {
    245       1.53  christos 	struct vnode *rtvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
    246        1.1   mycroft 	int error;
    247        1.1   mycroft 	int flags = 0;
    248        1.1   mycroft 
    249        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    250       1.15  christos 	printf("umapfs_unmount(mp = %p)\n", mp);
    251        1.1   mycroft #endif
    252        1.1   mycroft 
    253       1.18      fvdl 	if (mntflags & MNT_FORCE)
    254        1.1   mycroft 		flags |= FORCECLOSE;
    255        1.1   mycroft 
    256        1.1   mycroft 	/*
    257        1.1   mycroft 	 * Clear out buffer cache.  I don't think we
    258        1.1   mycroft 	 * ever get anything cached at this level at the
    259        1.1   mycroft 	 * moment, but who knows...
    260        1.1   mycroft 	 */
    261        1.1   mycroft #ifdef notyet
    262       1.51     perry 	mntflushbuf(mp, 0);
    263        1.1   mycroft 	if (mntinvalbuf(mp, 1))
    264        1.1   mycroft 		return (EBUSY);
    265        1.1   mycroft #endif
    266       1.53  christos 	if (rtvp->v_usecount > 1)
    267        1.1   mycroft 		return (EBUSY);
    268       1.53  christos 	if ((error = vflush(mp, rtvp, flags)) != 0)
    269        1.1   mycroft 		return (error);
    270        1.1   mycroft 
    271        1.1   mycroft #ifdef UMAPFS_DIAGNOSTIC
    272       1.53  christos 	vprint("alias root of lower", rtvp);
    273       1.51     perry #endif
    274        1.1   mycroft 	/*
    275        1.1   mycroft 	 * Release reference on underlying root vnode
    276        1.1   mycroft 	 */
    277       1.53  christos 	vrele(rtvp);
    278        1.1   mycroft 	/*
    279        1.1   mycroft 	 * And blow it away for future re-use
    280        1.1   mycroft 	 */
    281       1.53  christos 	vgone(rtvp);
    282        1.1   mycroft 	/*
    283        1.1   mycroft 	 * Finally, throw away the umap_mount structure
    284        1.1   mycroft 	 */
    285        1.1   mycroft 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    286        1.1   mycroft 	mp->mnt_data = 0;
    287        1.1   mycroft 	return (0);
    288        1.1   mycroft }
    289        1.1   mycroft 
    290       1.41    atatat SYSCTL_SETUP(sysctl_vfs_umap_setup, "sysctl vfs.umap subtree setup")
    291       1.41    atatat {
    292       1.41    atatat 
    293       1.43    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    294       1.43    atatat 		       CTLFLAG_PERMANENT,
    295       1.41    atatat 		       CTLTYPE_NODE, "vfs", NULL,
    296       1.41    atatat 		       NULL, 0, NULL, 0,
    297       1.41    atatat 		       CTL_VFS, CTL_EOL);
    298       1.43    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    299       1.43    atatat 		       CTLFLAG_PERMANENT,
    300       1.46    atatat 		       CTLTYPE_NODE, "umap",
    301       1.46    atatat 		       SYSCTL_DESCR("UID/GID remapping file system"),
    302       1.41    atatat 		       NULL, 0, NULL, 0,
    303       1.41    atatat 		       CTL_VFS, 10, CTL_EOL);
    304       1.41    atatat 	/*
    305       1.41    atatat 	 * XXX the "10" above could be dynamic, thereby eliminating
    306       1.41    atatat 	 * one more instance of the "number to vfs" mapping problem,
    307       1.41    atatat 	 * but "10" is the order as taken from sys/mount.h
    308       1.41    atatat 	 */
    309       1.41    atatat }
    310       1.41    atatat 
    311       1.27  jdolecek extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    312       1.17   thorpej 
    313       1.27  jdolecek const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
    314       1.17   thorpej 	&umapfs_vnodeop_opv_desc,
    315       1.17   thorpej 	NULL,
    316       1.17   thorpej };
    317       1.17   thorpej 
    318       1.16   thorpej struct vfsops umapfs_vfsops = {
    319        1.1   mycroft 	MOUNT_UMAP,
    320        1.1   mycroft 	umapfs_mount,
    321       1.23  wrstuden 	layerfs_start,
    322        1.1   mycroft 	umapfs_unmount,
    323       1.23  wrstuden 	layerfs_root,
    324       1.23  wrstuden 	layerfs_quotactl,
    325       1.44  christos 	layerfs_statvfs,
    326       1.23  wrstuden 	layerfs_sync,
    327       1.23  wrstuden 	layerfs_vget,
    328       1.23  wrstuden 	layerfs_fhtovp,
    329       1.23  wrstuden 	layerfs_vptofh,
    330       1.23  wrstuden 	layerfs_init,
    331       1.31       chs 	NULL,
    332       1.24  jdolecek 	layerfs_done,
    333       1.17   thorpej 	NULL,				/* vfs_mountroot */
    334       1.48  wrstuden 	layerfs_snapshot,
    335       1.50   thorpej 	vfs_stdextattrctl,
    336       1.17   thorpej 	umapfs_vnodeopv_descs,
    337        1.1   mycroft };
    338       1.52   thorpej VFS_ATTACH(umapfs_vfsops);
    339