Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.42.2.1
      1 /*	$NetBSD: rump_vfs.c,v 1.42.2.1 2010/04/30 14:44:30 uebayasi 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.42.2.1 2010/04/30 14:44:30 uebayasi 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/vfs_syscalls.h>
     46 #include <sys/vnode.h>
     47 #include <sys/wapbl.h>
     48 
     49 #include <miscfs/specfs/specdev.h>
     50 #include <miscfs/syncfs/syncfs.h>
     51 
     52 #include <rump/rump.h>
     53 #include <rump/rumpuser.h>
     54 
     55 #include "rump_private.h"
     56 #include "rump_vfs_private.h"
     57 
     58 struct cwdinfo cwdi0;
     59 const char *rootfstype = ROOT_FSTYPE_ANY;
     60 
     61 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
     62 
     63 static void
     64 pvfs_init(struct proc *p)
     65 {
     66 
     67 	p->p_cwdi = cwdinit();
     68 }
     69 
     70 static void
     71 pvfs_rele(struct proc *p)
     72 {
     73 
     74 	cwdfree(p->p_cwdi);
     75 }
     76 
     77 void
     78 rump_vfs_init(void)
     79 {
     80 	extern struct vfsops rumpfs_vfsops;
     81 	char buf[64];
     82 	int error;
     83 	int rv, i;
     84 	extern int dovfsusermount; /* XXX */
     85 
     86 	dovfsusermount = 1; /* XXX */
     87 
     88 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     89 		desiredvnodes = strtoul(buf, NULL, 10);
     90 	} else {
     91 		desiredvnodes = 1<<16;
     92 	}
     93 
     94 	rumpblk_init();
     95 
     96 	for (i = 0; i < ncpu; i++) {
     97 		struct cpu_info *ci = cpu_lookup(i);
     98 		cache_cpu_init(ci);
     99 	}
    100 	vfsinit();
    101 	bufinit();
    102 	cwd_sys_init();
    103 	lf_init();
    104 	spec_init();
    105 	fstrans_init();
    106 
    107 	if (rump_threads) {
    108 		if ((rv = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL,
    109 		    rumpuser_biothread, rump_biodone, NULL, "rmpabio")) != 0)
    110 			panic("syncer thread create failed: %d", rv);
    111 	}
    112 
    113 	root_device = &rump_rootdev;
    114 
    115 	/* bootstrap cwdi (rest done in vfs_mountroot() */
    116 	rw_init(&cwdi0.cwdi_lock);
    117 	proc0.p_cwdi = &cwdi0;
    118 	proc0.p_cwdi = cwdinit();
    119 
    120 	vfs_attach(&rumpfs_vfsops);
    121 	vfs_mountroot();
    122 
    123 	/* "mtree": create /dev */
    124 	do_sys_mkdir("/dev", 0777, UIO_SYSSPACE);
    125 	rump_devnull_init();
    126 
    127 	rump_proc_vfs_init = pvfs_init;
    128 	rump_proc_vfs_release = pvfs_rele;
    129 
    130 	if (rump_threads) {
    131 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    132 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    133 			panic("syncer thread create failed: %d", rv);
    134 	} else {
    135 		syncdelay = 0;
    136 	}
    137 
    138 	/*
    139 	 * On archs where the native kernel ABI is supported, map
    140 	 * host module directory to rump.  This means that kernel
    141 	 * modules from the host will be autoloaded to rump kernels.
    142 	 */
    143 #ifdef _RUMP_NATIVE_ABI
    144 	rump_etfs_register(module_base, module_base, RUMP_ETFS_DIR_SUBDIRS);
    145 #endif
    146 
    147 	module_init_class(MODULE_CLASS_VFS);
    148 }
    149 
    150 void
    151 rump_vfs_fini(void)
    152 {
    153 
    154 	vfs_shutdown();
    155 }
    156 
    157 struct componentname *
    158 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    159 	kauth_cred_t creds, struct lwp *l)
    160 {
    161 	struct componentname *cnp;
    162 	const char *cp = NULL;
    163 
    164 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    165 
    166 	cnp->cn_nameiop = nameiop;
    167 	cnp->cn_flags = flags;
    168 
    169 	cnp->cn_pnbuf = PNBUF_GET();
    170 	strcpy(cnp->cn_pnbuf, name);
    171 	cnp->cn_nameptr = cnp->cn_pnbuf;
    172 	cnp->cn_namelen = namelen;
    173 	cnp->cn_hash = namei_hash(name, &cp);
    174 
    175 	cnp->cn_cred = creds;
    176 
    177 	return cnp;
    178 }
    179 
    180 void
    181 rump_freecn(struct componentname *cnp, int flags)
    182 {
    183 
    184 	if (flags & RUMPCN_FREECRED)
    185 		rump_cred_put(cnp->cn_cred);
    186 
    187 	if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
    188 		PNBUF_PUT(cnp->cn_pnbuf);
    189 	kmem_free(cnp, sizeof(*cnp));
    190 }
    191 
    192 int
    193 rump_checksavecn(struct componentname *cnp)
    194 {
    195 
    196 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    197 		return 0;
    198 	} else {
    199 		cnp->cn_flags |= HASBUF;
    200 		return 1;
    201 	}
    202 }
    203 
    204 /* hey baby, what's your namei? */
    205 int
    206 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    207 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    208 {
    209 	struct nameidata nd;
    210 	int rv;
    211 
    212 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    213 	rv = namei(&nd);
    214 	if (rv)
    215 		return rv;
    216 
    217 	if (dvpp) {
    218 		KASSERT(flags & LOCKPARENT);
    219 		*dvpp = nd.ni_dvp;
    220 	} else {
    221 		KASSERT((flags & LOCKPARENT) == 0);
    222 	}
    223 
    224 	if (vpp) {
    225 		*vpp = nd.ni_vp;
    226 	} else {
    227 		if (nd.ni_vp) {
    228 			if (flags & LOCKLEAF)
    229 				vput(nd.ni_vp);
    230 			else
    231 				vrele(nd.ni_vp);
    232 		}
    233 	}
    234 
    235 	if (cnpp) {
    236 		struct componentname *cnp;
    237 
    238 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    239 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    240 		*cnpp = cnp;
    241 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    242 		panic("%s: pathbuf mismatch", __func__);
    243 	}
    244 
    245 	return rv;
    246 }
    247 
    248 void
    249 rump_getvninfo(struct vnode *vp, enum vtype *vtype,
    250 	voff_t *vsize, dev_t *vdev)
    251 {
    252 
    253 	*vtype = vp->v_type;
    254 	*vsize = vp->v_size;
    255 	if (vp->v_specnode)
    256 		*vdev = vp->v_rdev;
    257 	else
    258 		*vdev = 0;
    259 }
    260 
    261 struct vfsops *
    262 rump_vfslist_iterate(struct vfsops *ops)
    263 {
    264 
    265 	if (ops == NULL)
    266 		return LIST_FIRST(&vfs_list);
    267 	else
    268 		return LIST_NEXT(ops, vfs_list);
    269 }
    270 
    271 struct vfsops *
    272 rump_vfs_getopsbyname(const char *name)
    273 {
    274 
    275 	return vfs_getopsbyname(name);
    276 }
    277 
    278 int
    279 rump_vfs_getmp(const char *path, struct mount **mpp)
    280 {
    281 	struct vnode *vp;
    282 	int rv;
    283 
    284 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    285 		return rv;
    286 
    287 	*mpp = vp->v_mount;
    288 	vrele(vp);
    289 	return 0;
    290 }
    291 
    292 struct vattr*
    293 rump_vattr_init(void)
    294 {
    295 	struct vattr *vap;
    296 
    297 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    298 	vattr_null(vap);
    299 
    300 	return vap;
    301 }
    302 
    303 void
    304 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    305 {
    306 
    307 	vap->va_type = vt;
    308 }
    309 
    310 void
    311 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    312 {
    313 
    314 	vap->va_mode = mode;
    315 }
    316 
    317 void
    318 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    319 {
    320 
    321 	vap->va_rdev = dev;
    322 }
    323 
    324 void
    325 rump_vattr_free(struct vattr *vap)
    326 {
    327 
    328 	kmem_free(vap, sizeof(*vap));
    329 }
    330 
    331 void
    332 rump_vp_incref(struct vnode *vp)
    333 {
    334 
    335 	vref(vp);
    336 }
    337 
    338 int
    339 rump_vp_getref(struct vnode *vp)
    340 {
    341 
    342 	return vp->v_usecount;
    343 }
    344 
    345 void
    346 rump_vp_rele(struct vnode *vp)
    347 {
    348 
    349 	vrele(vp);
    350 }
    351 
    352 void
    353 rump_vp_interlock(struct vnode *vp)
    354 {
    355 
    356 	mutex_enter(&vp->v_interlock);
    357 }
    358 
    359 int
    360 rump_vfs_unmount(struct mount *mp, int mntflags)
    361 {
    362 #if 0
    363 	struct evcnt *ev;
    364 
    365 	printf("event counters:\n");
    366 	TAILQ_FOREACH(ev, &allevents, ev_list)
    367 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    368 #endif
    369 
    370 	return VFS_UNMOUNT(mp, mntflags);
    371 }
    372 
    373 int
    374 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    375 {
    376 	int rv;
    377 
    378 	rv = VFS_ROOT(mp, vpp);
    379 	if (rv)
    380 		return rv;
    381 
    382 	if (!lock)
    383 		VOP_UNLOCK(*vpp, 0);
    384 
    385 	return 0;
    386 }
    387 
    388 int
    389 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    390 {
    391 
    392 	return VFS_STATVFS(mp, sbp);
    393 }
    394 
    395 int
    396 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    397 {
    398 
    399 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    400 }
    401 
    402 int
    403 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    404 {
    405 
    406 	return VFS_FHTOVP(mp, fid, vpp);
    407 }
    408 
    409 int
    410 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    411 {
    412 
    413 	return VFS_VPTOFH(vp, fid, fidsize);
    414 }
    415 
    416 /*ARGSUSED*/
    417 void
    418 rump_vfs_syncwait(struct mount *mp)
    419 {
    420 	int n;
    421 
    422 	n = buf_syncwait();
    423 	if (n)
    424 		printf("syncwait: unsynced buffers: %d\n", n);
    425 }
    426 
    427 void
    428 rump_biodone(void *arg, size_t count, int error)
    429 {
    430 	struct buf *bp = arg;
    431 
    432 	bp->b_resid = bp->b_bcount - count;
    433 	KASSERT(bp->b_resid >= 0);
    434 	bp->b_error = error;
    435 
    436 	biodone(bp);
    437 }
    438 
    439 static void
    440 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    441 {
    442 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    443 
    444 	KASSERT(cvp);
    445 
    446 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    447 	if (cwdi->cwdi_rdir)
    448 		vrele(cwdi->cwdi_rdir);
    449 	if (rvp)
    450 		vref(rvp);
    451 	cwdi->cwdi_rdir = rvp;
    452 
    453 	vrele(cwdi->cwdi_cdir);
    454 	vref(cvp);
    455 	cwdi->cwdi_cdir = cvp;
    456 	rw_exit(&cwdi->cwdi_lock);
    457 }
    458 
    459 void
    460 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    461 {
    462 
    463 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    464 }
    465 
    466 struct vnode *
    467 rump_cdir_get(void)
    468 {
    469 	struct vnode *vp;
    470 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    471 
    472 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    473 	vp = cwdi->cwdi_cdir;
    474 	rw_exit(&cwdi->cwdi_lock);
    475 	vref(vp);
    476 
    477 	return vp;
    478 }
    479