Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.32
      1 /*	$NetBSD: rump.c,v 1.32 2008/01/27 19:07:21 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by Google Summer of Code.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/cpu.h>
     32 #include <sys/filedesc.h>
     33 #include <sys/kauth.h>
     34 #include <sys/kmem.h>
     35 #include <sys/mount.h>
     36 #include <sys/namei.h>
     37 #include <sys/queue.h>
     38 #include <sys/resourcevar.h>
     39 #include <sys/select.h>
     40 #include <sys/vnode.h>
     41 
     42 #include <miscfs/specfs/specdev.h>
     43 
     44 #include "rump_private.h"
     45 #include "rumpuser.h"
     46 
     47 struct proc rump_proc;
     48 struct cwdinfo rump_cwdi;
     49 struct pstats rump_stats;
     50 struct plimit rump_limits;
     51 kauth_cred_t rump_cred = RUMPCRED_SUSER;
     52 struct cpu_info rump_cpu;
     53 struct filedesc0 rump_filedesc0;
     54 
     55 kmutex_t rump_giantlock;
     56 
     57 sigset_t sigcantmask;
     58 
     59 struct fakeblk {
     60 	char path[MAXPATHLEN];
     61 	LIST_ENTRY(fakeblk) entries;
     62 };
     63 
     64 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
     65 
     66 #ifndef RUMP_WITHOUT_THREADS
     67 static void
     68 rump_aiodone_worker(struct work *wk, void *dummy)
     69 {
     70 	struct buf *bp = (struct buf *)wk;
     71 
     72 	KASSERT(&bp->b_work == wk);
     73 	bp->b_iodone(bp);
     74 }
     75 #endif /* RUMP_WITHOUT_THREADS */
     76 
     77 int rump_inited;
     78 
     79 void
     80 rump_init()
     81 {
     82 	extern char hostname[];
     83 	extern size_t hostnamelen;
     84 	extern kmutex_t rump_atomic_lock;
     85 	struct proc *p;
     86 	struct lwp *l;
     87 	int error;
     88 
     89 	/* XXX */
     90 	if (rump_inited)
     91 		return;
     92 	rump_inited = 1;
     93 
     94 	l = &lwp0;
     95 	p = &rump_proc;
     96 	p->p_stats = &rump_stats;
     97 	p->p_cwdi = &rump_cwdi;
     98 	p->p_limit = &rump_limits;
     99 	p->p_pid = 0;
    100 	p->p_fd = &rump_filedesc0.fd_fd;
    101 	p->p_vmspace = &rump_vmspace;
    102 	l->l_cred = rump_cred;
    103 	l->l_proc = p;
    104 	l->l_lid = 1;
    105 	rw_init(&rump_cwdi.cwdi_lock);
    106 
    107 	mutex_init(&rump_atomic_lock, MUTEX_DEFAULT, IPL_NONE);
    108 	rumpvm_init();
    109 
    110 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    111 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    112 
    113 	/* should be "enough" */
    114 	syncdelay = 0;
    115 
    116 	vfsinit();
    117 	bufinit();
    118 	filedesc_init();
    119 	selsysinit();
    120 
    121 	rumpvfs_init();
    122 
    123 	rump_sleepers_init();
    124 	rumpuser_thrinit();
    125 
    126 	rumpuser_mutex_recursive_init(&rump_giantlock.kmtx_mtx);
    127 
    128 #ifndef RUMP_WITHOUT_THREADS
    129 	/* aieeeedondest */
    130 	if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    131 	    rump_aiodone_worker, NULL, 0, 0, 0))
    132 		panic("aiodoned");
    133 #endif /* RUMP_WITHOUT_THREADS */
    134 
    135 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    136 	hostnamelen = strlen(hostname);
    137 
    138 	sigemptyset(&sigcantmask);
    139 
    140 	fdinit1(&rump_filedesc0);
    141 }
    142 
    143 struct mount *
    144 rump_mnt_init(struct vfsops *vfsops, int mntflags)
    145 {
    146 	struct mount *mp;
    147 
    148 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    149 
    150 	mp->mnt_op = vfsops;
    151 	mp->mnt_flag = mntflags;
    152 	TAILQ_INIT(&mp->mnt_vnodelist);
    153 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    154 
    155 	mount_initspecific(mp);
    156 
    157 	return mp;
    158 }
    159 
    160 int
    161 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    162 {
    163 	int rv;
    164 
    165 	rv = VFS_MOUNT(mp, path, data, dlen);
    166 	if (rv)
    167 		return rv;
    168 
    169 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    170 	rv = VFS_START(mp, 0);
    171 	if (rv)
    172 		VFS_UNMOUNT(mp, MNT_FORCE);
    173 
    174 	return rv;
    175 }
    176 
    177 void
    178 rump_mnt_destroy(struct mount *mp)
    179 {
    180 
    181 	mount_finispecific(mp);
    182 	kmem_free(mp, sizeof(*mp));
    183 }
    184 
    185 struct componentname *
    186 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    187 	kauth_cred_t creds, struct lwp *l)
    188 {
    189 	struct componentname *cnp;
    190 	const char *cp = NULL;
    191 
    192 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    193 
    194 	cnp->cn_nameiop = nameiop;
    195 	cnp->cn_flags = flags;
    196 
    197 	cnp->cn_pnbuf = PNBUF_GET();
    198 	strcpy(cnp->cn_pnbuf, name);
    199 	cnp->cn_nameptr = cnp->cn_pnbuf;
    200 	cnp->cn_namelen = namelen;
    201 	cnp->cn_hash = namei_hash(name, &cp);
    202 
    203 	cnp->cn_cred = creds;
    204 
    205 	return cnp;
    206 }
    207 
    208 void
    209 rump_freecn(struct componentname *cnp, int flags)
    210 {
    211 
    212 	if (flags & RUMPCN_FREECRED)
    213 		rump_cred_destroy(cnp->cn_cred);
    214 
    215 	if (cnp->cn_flags & SAVENAME) {
    216 		if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
    217 			PNBUF_PUT(cnp->cn_pnbuf);
    218 	} else {
    219 		PNBUF_PUT(cnp->cn_pnbuf);
    220 	}
    221 	kmem_free(cnp, sizeof(*cnp));
    222 }
    223 
    224 static struct fakeblk *
    225 _rump_fakeblk_find(const char *path)
    226 {
    227 	char buf[MAXPATHLEN];
    228 	struct fakeblk *fblk;
    229 	int error;
    230 
    231 	if (rumpuser_realpath(path, buf, &error) == NULL)
    232 		return NULL;
    233 
    234 	LIST_FOREACH(fblk, &fakeblks, entries)
    235 		if (strcmp(fblk->path, buf) == 0)
    236 			return fblk;
    237 
    238 	return NULL;
    239 }
    240 
    241 int
    242 rump_fakeblk_register(const char *path)
    243 {
    244 	char buf[MAXPATHLEN];
    245 	struct fakeblk *fblk;
    246 	int error;
    247 
    248 	if (_rump_fakeblk_find(path))
    249 		return EEXIST;
    250 
    251 	if (rumpuser_realpath(path, buf, &error) == NULL)
    252 		return error;
    253 
    254 	fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
    255 	if (fblk == NULL)
    256 		return ENOMEM;
    257 
    258 	strlcpy(fblk->path, buf, MAXPATHLEN);
    259 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    260 
    261 	return 0;
    262 }
    263 
    264 int
    265 rump_fakeblk_find(const char *path)
    266 {
    267 
    268 	return _rump_fakeblk_find(path) != NULL;
    269 }
    270 
    271 void
    272 rump_fakeblk_deregister(const char *path)
    273 {
    274 	struct fakeblk *fblk;
    275 
    276 	fblk = _rump_fakeblk_find(path);
    277 	if (fblk == NULL)
    278 		return;
    279 
    280 	LIST_REMOVE(fblk, entries);
    281 	kmem_free(fblk, sizeof(*fblk));
    282 }
    283 
    284 void
    285 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    286 {
    287 
    288 	*vtype = vp->v_type;
    289 	*vsize = vp->v_size;
    290 	if (vp->v_specnode)
    291 		*vdev = vp->v_rdev;
    292 	else
    293 		*vdev = 0;
    294 }
    295 
    296 struct vfsops *
    297 rump_vfslist_iterate(struct vfsops *ops)
    298 {
    299 
    300 	if (ops == NULL)
    301 		return LIST_FIRST(&vfs_list);
    302 	else
    303 		return LIST_NEXT(ops, vfs_list);
    304 }
    305 
    306 struct vfsops *
    307 rump_vfs_getopsbyname(const char *name)
    308 {
    309 
    310 	return vfs_getopsbyname(name);
    311 }
    312 
    313 struct vattr*
    314 rump_vattr_init()
    315 {
    316 	struct vattr *vap;
    317 
    318 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    319 	vattr_null(vap);
    320 
    321 	return vap;
    322 }
    323 
    324 void
    325 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    326 {
    327 
    328 	vap->va_type = vt;
    329 }
    330 
    331 void
    332 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    333 {
    334 
    335 	vap->va_mode = mode;
    336 }
    337 
    338 void
    339 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    340 {
    341 
    342 	vap->va_rdev = dev;
    343 }
    344 
    345 void
    346 rump_vattr_free(struct vattr *vap)
    347 {
    348 
    349 	kmem_free(vap, sizeof(*vap));
    350 }
    351 
    352 void
    353 rump_vp_incref(struct vnode *vp)
    354 {
    355 
    356 	mutex_enter(&vp->v_interlock);
    357 	++vp->v_usecount;
    358 	mutex_exit(&vp->v_interlock);
    359 }
    360 
    361 int
    362 rump_vp_getref(struct vnode *vp)
    363 {
    364 
    365 	return vp->v_usecount;
    366 }
    367 
    368 void
    369 rump_vp_decref(struct vnode *vp)
    370 {
    371 
    372 	mutex_enter(&vp->v_interlock);
    373 	--vp->v_usecount;
    374 	mutex_exit(&vp->v_interlock);
    375 }
    376 
    377 void
    378 rump_vp_recycle(struct vnode *vp)
    379 {
    380 
    381 	mutex_enter(&vp->v_interlock);
    382 	vclean(vp, DOCLOSE);
    383 	vrelel(vp, 0, 0);
    384 }
    385 
    386 void
    387 rump_vp_rele(struct vnode *vp)
    388 {
    389 
    390 	vrele(vp);
    391 }
    392 
    393 struct uio *
    394 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    395 {
    396 	struct uio *uio;
    397 	enum uio_rw uiorw;
    398 
    399 	switch (rw) {
    400 	case RUMPUIO_READ:
    401 		uiorw = UIO_READ;
    402 		break;
    403 	case RUMPUIO_WRITE:
    404 		uiorw = UIO_WRITE;
    405 		break;
    406 	default:
    407 		panic("%s: invalid rw %d", __func__, rw);
    408 	}
    409 
    410 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    411 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    412 
    413 	uio->uio_iov->iov_base = buf;
    414 	uio->uio_iov->iov_len = bufsize;
    415 
    416 	uio->uio_iovcnt = 1;
    417 	uio->uio_offset = offset;
    418 	uio->uio_resid = bufsize;
    419 	uio->uio_rw = uiorw;
    420 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    421 
    422 	return uio;
    423 }
    424 
    425 size_t
    426 rump_uio_getresid(struct uio *uio)
    427 {
    428 
    429 	return uio->uio_resid;
    430 }
    431 
    432 off_t
    433 rump_uio_getoff(struct uio *uio)
    434 {
    435 
    436 	return uio->uio_offset;
    437 }
    438 
    439 size_t
    440 rump_uio_free(struct uio *uio)
    441 {
    442 	size_t resid;
    443 
    444 	resid = uio->uio_resid;
    445 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    446 	kmem_free(uio, sizeof(*uio));
    447 
    448 	return resid;
    449 }
    450 
    451 void
    452 rump_vp_lock_exclusive(struct vnode *vp)
    453 {
    454 
    455 	/* we can skip vn_lock() */
    456 	VOP_LOCK(vp, LK_EXCLUSIVE);
    457 }
    458 
    459 void
    460 rump_vp_lock_shared(struct vnode *vp)
    461 {
    462 
    463 	VOP_LOCK(vp, LK_SHARED);
    464 }
    465 
    466 void
    467 rump_vp_unlock(struct vnode *vp)
    468 {
    469 
    470 	VOP_UNLOCK(vp, 0);
    471 }
    472 
    473 int
    474 rump_vp_islocked(struct vnode *vp)
    475 {
    476 
    477 	return VOP_ISLOCKED(vp);
    478 }
    479 
    480 void
    481 rump_vp_interlock(struct vnode *vp)
    482 {
    483 
    484 	mutex_enter(&vp->v_interlock);
    485 }
    486 
    487 int
    488 rump_vfs_unmount(struct mount *mp, int mntflags)
    489 {
    490 
    491 	return VFS_UNMOUNT(mp, mntflags);
    492 }
    493 
    494 int
    495 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    496 {
    497 	int rv;
    498 
    499 	rv = VFS_ROOT(mp, vpp);
    500 	if (rv)
    501 		return rv;
    502 
    503 	if (!lock)
    504 		VOP_UNLOCK(*vpp, 0);
    505 
    506 	return 0;
    507 }
    508 
    509 /* XXX: statvfs is different from system to system */
    510 #if 0
    511 int
    512 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    513 {
    514 
    515 	return VFS_STATVFS(mp, sbp);
    516 }
    517 #endif
    518 
    519 int
    520 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    521 {
    522 
    523 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    524 }
    525 
    526 int
    527 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    528 {
    529 
    530 	return VFS_FHTOVP(mp, fid, vpp);
    531 }
    532 
    533 int
    534 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    535 {
    536 
    537 	return VFS_VPTOFH(vp, fid, fidsize);
    538 }
    539 
    540 /*ARGSUSED*/
    541 void
    542 rump_vfs_syncwait(struct mount *mp)
    543 {
    544 	int n;
    545 
    546 	n = buf_syncwait();
    547 	if (n)
    548 		printf("syncwait: unsynced buffers: %d\n", n);
    549 }
    550 
    551 void
    552 rump_bioops_sync()
    553 {
    554 
    555 	if (bioopsp)
    556 		bioopsp->io_sync(NULL);
    557 }
    558 
    559 struct lwp *
    560 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    561 {
    562 	struct lwp *l;
    563 	struct proc *p;
    564 
    565 	l = kmem_alloc(sizeof(struct lwp), KM_SLEEP);
    566 	p = kmem_alloc(sizeof(struct proc), KM_SLEEP);
    567 	p->p_stats = &rump_stats;
    568 	p->p_cwdi = &rump_cwdi;
    569 	p->p_limit = &rump_limits;
    570         p->p_pid = pid;
    571 	p->p_vmspace = &rump_vmspace;
    572 	l->l_cred = rump_cred;
    573 	l->l_proc = p;
    574         l->l_lid = lid;
    575 
    576 	if (set)
    577 		rumpuser_set_curlwp(l);
    578 
    579 	return l;
    580 }
    581 
    582 void
    583 rump_clear_curlwp()
    584 {
    585 	struct lwp *l;
    586 
    587 	l = rumpuser_get_curlwp();
    588 	kmem_free(l->l_proc, sizeof(struct proc));
    589 	kmem_free(l, sizeof(struct lwp));
    590 	rumpuser_set_curlwp(NULL);
    591 }
    592 
    593 struct lwp *
    594 rump_get_curlwp()
    595 {
    596 	struct lwp *l;
    597 
    598 	l = rumpuser_get_curlwp();
    599 	if (l == NULL)
    600 		l = &lwp0;
    601 
    602 	return l;
    603 }
    604 
    605 int
    606 rump_splfoo()
    607 {
    608 
    609 	if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
    610 		rumpuser_rw_enter(&rumpspl, 0);
    611 		rumpuser_set_ipl(RUMPUSER_IPL_SPLFOO);
    612 	}
    613 
    614 	return 0;
    615 }
    616 
    617 static void
    618 rump_intr_enter(void)
    619 {
    620 
    621 	rumpuser_set_ipl(RUMPUSER_IPL_INTR);
    622 	rumpuser_rw_enter(&rumpspl, 1);
    623 }
    624 
    625 static void
    626 rump_intr_exit(void)
    627 {
    628 
    629 	rumpuser_rw_exit(&rumpspl);
    630 	rumpuser_clear_ipl(RUMPUSER_IPL_INTR);
    631 }
    632 
    633 void
    634 rump_splx(int dummy)
    635 {
    636 
    637 	if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
    638 		rumpuser_clear_ipl(RUMPUSER_IPL_SPLFOO);
    639 		rumpuser_rw_exit(&rumpspl);
    640 	}
    641 }
    642 
    643 void
    644 rump_biodone(void *arg, size_t count, int error)
    645 {
    646 	struct buf *bp = arg;
    647 
    648 	bp->b_resid = bp->b_bcount - count;
    649 	KASSERT(bp->b_resid >= 0);
    650 	bp->b_error = error;
    651 
    652 	rump_intr_enter();
    653 	biodone(bp);
    654 	rump_intr_exit();
    655 }
    656