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