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