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