Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.55
      1 /*	$NetBSD: rump_vfs.c,v 1.55 2010/06/24 13:03:18 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Finnish Cultural Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: rump_vfs.c,v 1.55 2010/06/24 13:03:18 hannken Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/buf.h>
     36 #include <sys/conf.h>
     37 #include <sys/evcnt.h>
     38 #include <sys/filedesc.h>
     39 #include <sys/fstrans.h>
     40 #include <sys/lockf.h>
     41 #include <sys/kthread.h>
     42 #include <sys/module.h>
     43 #include <sys/namei.h>
     44 #include <sys/queue.h>
     45 #include <sys/stat.h>
     46 #include <sys/vfs_syscalls.h>
     47 #include <sys/vnode.h>
     48 #include <sys/wapbl.h>
     49 
     50 #include <miscfs/specfs/specdev.h>
     51 #include <miscfs/syncfs/syncfs.h>
     52 
     53 #include <rump/rump.h>
     54 #include <rump/rumpuser.h>
     55 
     56 #include "rump_private.h"
     57 #include "rump_vfs_private.h"
     58 
     59 struct cwdinfo cwdi0;
     60 const char *rootfstype = ROOT_FSTYPE_ANY;
     61 
     62 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
     63 
     64 static void
     65 pvfs_init(struct proc *p)
     66 {
     67 
     68 	p->p_cwdi = cwdinit();
     69 }
     70 
     71 static void
     72 pvfs_rele(struct proc *p)
     73 {
     74 
     75 	cwdfree(p->p_cwdi);
     76 }
     77 
     78 void
     79 rump_vfs_init(void)
     80 {
     81 	extern struct devsw_conv devsw_conv0[];
     82 	extern int max_devsw_convs;
     83 	extern struct vfsops rumpfs_vfsops;
     84 	char buf[64];
     85 	int error;
     86 	int rv, i;
     87 	extern int dovfsusermount; /* XXX */
     88 
     89 	dovfsusermount = 1; /* XXX */
     90 
     91 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     92 		desiredvnodes = strtoul(buf, NULL, 10);
     93 	} else {
     94 		desiredvnodes = 1<<10;
     95 	}
     96 
     97 	rumpblk_init();
     98 
     99 	for (i = 0; i < ncpu; i++) {
    100 		struct cpu_info *ci = cpu_lookup(i);
    101 		cache_cpu_init(ci);
    102 	}
    103 	vfsinit();
    104 	bufinit();
    105 	cwd_sys_init();
    106 	lf_init();
    107 	spec_init();
    108 	fstrans_init();
    109 
    110 	if (rump_threads) {
    111 		if ((rv = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL,
    112 		    rumpuser_biothread, rump_biodone, NULL, "rmpabio")) != 0)
    113 			panic("syncer thread create failed: %d", rv);
    114 	}
    115 
    116 	root_device = &rump_rootdev;
    117 
    118 	/* bootstrap cwdi (rest done in vfs_mountroot() */
    119 	rw_init(&cwdi0.cwdi_lock);
    120 	proc0.p_cwdi = &cwdi0;
    121 	proc0.p_cwdi = cwdinit();
    122 
    123 	vfs_attach(&rumpfs_vfsops);
    124 	vfs_mountroot();
    125 
    126 	/* "mtree": create /dev */
    127 	do_sys_mkdir("/dev", 0777, UIO_SYSSPACE);
    128 	rump_devnull_init();
    129 
    130 	rump_proc_vfs_init = pvfs_init;
    131 	rump_proc_vfs_release = pvfs_rele;
    132 
    133 	if (rump_threads) {
    134 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    135 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    136 			panic("syncer thread create failed: %d", rv);
    137 	} else {
    138 		syncdelay = 0;
    139 	}
    140 
    141 	/*
    142 	 * On archs where the native kernel ABI is supported, map
    143 	 * host module directory to rump.  This means that kernel
    144 	 * modules from the host will be autoloaded to rump kernels.
    145 	 */
    146 #ifdef _RUMP_NATIVE_ABI
    147 	{
    148 	char *mbase;
    149 
    150 	if (rumpuser_getenv("RUMP_MODULEBASE", buf, sizeof(buf), &error) == 0)
    151 		mbase = buf;
    152 	else
    153 		mbase = module_base;
    154 
    155 	if (strlen(mbase) != 0 && *mbase != '0') {
    156 		rump_etfs_register(module_base, mbase, RUMP_ETFS_DIR_SUBDIRS);
    157 	}
    158 	}
    159 #endif
    160 
    161 	module_init_class(MODULE_CLASS_VFS);
    162 
    163 	rump_vfs_builddevs(devsw_conv0, max_devsw_convs);
    164 }
    165 
    166 void
    167 rump_vfs_fini(void)
    168 {
    169 
    170 	vfs_shutdown();
    171 }
    172 
    173 struct componentname *
    174 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    175 	kauth_cred_t creds, struct lwp *l)
    176 {
    177 	struct componentname *cnp;
    178 	const char *cp = NULL;
    179 
    180 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    181 
    182 	cnp->cn_nameiop = nameiop;
    183 	cnp->cn_flags = flags;
    184 
    185 	cnp->cn_pnbuf = PNBUF_GET();
    186 	strcpy(cnp->cn_pnbuf, name);
    187 	cnp->cn_nameptr = cnp->cn_pnbuf;
    188 	cnp->cn_namelen = namelen;
    189 	cnp->cn_hash = namei_hash(name, &cp);
    190 
    191 	cnp->cn_cred = creds;
    192 
    193 	return cnp;
    194 }
    195 
    196 void
    197 rump_freecn(struct componentname *cnp, int flags)
    198 {
    199 
    200 	if (flags & RUMPCN_FREECRED)
    201 		rump_cred_put(cnp->cn_cred);
    202 
    203 	if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
    204 		PNBUF_PUT(cnp->cn_pnbuf);
    205 	kmem_free(cnp, sizeof(*cnp));
    206 }
    207 
    208 int
    209 rump_checksavecn(struct componentname *cnp)
    210 {
    211 
    212 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    213 		return 0;
    214 	} else {
    215 		cnp->cn_flags |= HASBUF;
    216 		return 1;
    217 	}
    218 }
    219 
    220 /* hey baby, what's your namei? */
    221 int
    222 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    223 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    224 {
    225 	struct nameidata nd;
    226 	int rv;
    227 
    228 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    229 	rv = namei(&nd);
    230 	if (rv)
    231 		return rv;
    232 
    233 	if (dvpp) {
    234 		KASSERT(flags & LOCKPARENT);
    235 		*dvpp = nd.ni_dvp;
    236 	} else {
    237 		KASSERT((flags & LOCKPARENT) == 0);
    238 	}
    239 
    240 	if (vpp) {
    241 		*vpp = nd.ni_vp;
    242 	} else {
    243 		if (nd.ni_vp) {
    244 			if (flags & LOCKLEAF)
    245 				vput(nd.ni_vp);
    246 			else
    247 				vrele(nd.ni_vp);
    248 		}
    249 	}
    250 
    251 	if (cnpp) {
    252 		struct componentname *cnp;
    253 
    254 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    255 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    256 		*cnpp = cnp;
    257 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    258 		panic("%s: pathbuf mismatch", __func__);
    259 	}
    260 
    261 	return rv;
    262 }
    263 
    264 void
    265 rump_getvninfo(struct vnode *vp, enum vtype *vtype,
    266 	voff_t *vsize, dev_t *vdev)
    267 {
    268 
    269 	*vtype = vp->v_type;
    270 	*vsize = vp->v_size;
    271 	if (vp->v_specnode)
    272 		*vdev = vp->v_rdev;
    273 	else
    274 		*vdev = 0;
    275 }
    276 
    277 struct vfsops *
    278 rump_vfslist_iterate(struct vfsops *ops)
    279 {
    280 
    281 	if (ops == NULL)
    282 		return LIST_FIRST(&vfs_list);
    283 	else
    284 		return LIST_NEXT(ops, vfs_list);
    285 }
    286 
    287 struct vfsops *
    288 rump_vfs_getopsbyname(const char *name)
    289 {
    290 
    291 	return vfs_getopsbyname(name);
    292 }
    293 
    294 int
    295 rump_vfs_getmp(const char *path, struct mount **mpp)
    296 {
    297 	struct vnode *vp;
    298 	int rv;
    299 
    300 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    301 		return rv;
    302 
    303 	*mpp = vp->v_mount;
    304 	vrele(vp);
    305 	return 0;
    306 }
    307 
    308 struct vattr*
    309 rump_vattr_init(void)
    310 {
    311 	struct vattr *vap;
    312 
    313 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    314 	vattr_null(vap);
    315 
    316 	return vap;
    317 }
    318 
    319 void
    320 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    321 {
    322 
    323 	vap->va_type = vt;
    324 }
    325 
    326 void
    327 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    328 {
    329 
    330 	vap->va_mode = mode;
    331 }
    332 
    333 void
    334 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    335 {
    336 
    337 	vap->va_rdev = dev;
    338 }
    339 
    340 void
    341 rump_vattr_free(struct vattr *vap)
    342 {
    343 
    344 	kmem_free(vap, sizeof(*vap));
    345 }
    346 
    347 void
    348 rump_vp_incref(struct vnode *vp)
    349 {
    350 
    351 	vref(vp);
    352 }
    353 
    354 int
    355 rump_vp_getref(struct vnode *vp)
    356 {
    357 
    358 	return vp->v_usecount;
    359 }
    360 
    361 void
    362 rump_vp_rele(struct vnode *vp)
    363 {
    364 
    365 	vrele(vp);
    366 }
    367 
    368 void
    369 rump_vp_interlock(struct vnode *vp)
    370 {
    371 
    372 	mutex_enter(&vp->v_interlock);
    373 }
    374 
    375 int
    376 rump_vfs_unmount(struct mount *mp, int mntflags)
    377 {
    378 
    379 	return VFS_UNMOUNT(mp, mntflags);
    380 }
    381 
    382 int
    383 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    384 {
    385 	int rv;
    386 
    387 	rv = VFS_ROOT(mp, vpp);
    388 	if (rv)
    389 		return rv;
    390 
    391 	if (!lock)
    392 		VOP_UNLOCK(*vpp);
    393 
    394 	return 0;
    395 }
    396 
    397 int
    398 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    399 {
    400 
    401 	return VFS_STATVFS(mp, sbp);
    402 }
    403 
    404 int
    405 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    406 {
    407 
    408 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    409 }
    410 
    411 int
    412 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    413 {
    414 
    415 	return VFS_FHTOVP(mp, fid, vpp);
    416 }
    417 
    418 int
    419 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    420 {
    421 
    422 	return VFS_VPTOFH(vp, fid, fidsize);
    423 }
    424 
    425 int
    426 rump_vfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
    427 	int attrnamespace, const char *attrname)
    428 {
    429 
    430 	return VFS_EXTATTRCTL(mp, cmd, vp, attrnamespace, attrname);
    431 }
    432 
    433 /*ARGSUSED*/
    434 void
    435 rump_vfs_syncwait(struct mount *mp)
    436 {
    437 	int n;
    438 
    439 	n = buf_syncwait();
    440 	if (n)
    441 		printf("syncwait: unsynced buffers: %d\n", n);
    442 }
    443 
    444 /*
    445  * Dump info about mount point.  No locking.
    446  */
    447 void
    448 rump_vfs_mount_print(const char *path, int full)
    449 {
    450 #ifdef DEBUGPRINT
    451 	struct vnode *mvp;
    452 	struct vnode *vp;
    453 	int error;
    454 
    455 	rumpuser_dprintf("\n==== dumping mountpoint at ``%s'' ====\n\n", path);
    456 	if ((error = namei_simple_user(path, NSM_FOLLOW_NOEMULROOT, &mvp))!=0) {
    457 		rumpuser_dprintf("==== lookup error %d ====\n\n", error);
    458 		return;
    459 	}
    460 	vfs_mount_print(mvp->v_mount, full, (void *)rumpuser_dprintf);
    461 	if (full) {
    462 		rumpuser_dprintf("\n== dumping vnodes ==\n\n");
    463 		TAILQ_FOREACH(vp, &mvp->v_mount->mnt_vnodelist, v_mntvnodes) {
    464 			vfs_vnode_print(vp, full, (void *)rumpuser_dprintf);
    465 		}
    466 	}
    467 	vrele(mvp);
    468 	rumpuser_dprintf("\n==== done ====\n\n");
    469 #else
    470 	rumpuser_dprintf("mount dump not supported without DEBUGPRINT\n");
    471 #endif
    472 }
    473 
    474 void
    475 rump_biodone(void *arg, size_t count, int error)
    476 {
    477 	struct buf *bp = arg;
    478 
    479 	bp->b_resid = bp->b_bcount - count;
    480 	KASSERT(bp->b_resid >= 0);
    481 	bp->b_error = error;
    482 
    483 	biodone(bp);
    484 }
    485 
    486 static void
    487 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    488 {
    489 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    490 
    491 	KASSERT(cvp);
    492 
    493 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    494 	if (cwdi->cwdi_rdir)
    495 		vrele(cwdi->cwdi_rdir);
    496 	if (rvp)
    497 		vref(rvp);
    498 	cwdi->cwdi_rdir = rvp;
    499 
    500 	vrele(cwdi->cwdi_cdir);
    501 	vref(cvp);
    502 	cwdi->cwdi_cdir = cvp;
    503 	rw_exit(&cwdi->cwdi_lock);
    504 }
    505 
    506 void
    507 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    508 {
    509 
    510 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    511 }
    512 
    513 struct vnode *
    514 rump_cdir_get(void)
    515 {
    516 	struct vnode *vp;
    517 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    518 
    519 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    520 	vp = cwdi->cwdi_cdir;
    521 	rw_exit(&cwdi->cwdi_lock);
    522 	vref(vp);
    523 
    524 	return vp;
    525 }
    526