Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.30
      1 /*	$NetBSD: rump.c,v 1.30 2008/01/24 17:32:56 ad 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;
     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 
    106 	mutex_init(&rump_atomic_lock, MUTEX_DEFAULT, IPL_NONE);
    107 	rumpvm_init();
    108 
    109 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    110 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    111 
    112 	/* should be "enough" */
    113 	syncdelay = 0;
    114 
    115 	vfsinit();
    116 	bufinit();
    117 	filedesc_init();
    118 	selsysinit();
    119 
    120 	rump_sleepers_init();
    121 	rumpuser_thrinit();
    122 
    123 	rumpuser_mutex_recursive_init(&rump_giantlock.kmtx_mtx);
    124 
    125 #ifndef RUMP_WITHOUT_THREADS
    126 	/* aieeeedondest */
    127 	if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    128 	    rump_aiodone_worker, NULL, 0, 0, 0))
    129 		panic("aiodoned");
    130 #endif /* RUMP_WITHOUT_THREADS */
    131 
    132 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    133 	hostnamelen = strlen(hostname);
    134 
    135 	sigemptyset(&sigcantmask);
    136 
    137 	fdinit1(&rump_filedesc0);
    138 }
    139 
    140 struct mount *
    141 rump_mnt_init(struct vfsops *vfsops, int mntflags)
    142 {
    143 	struct mount *mp;
    144 
    145 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    146 
    147 	mp->mnt_op = vfsops;
    148 	mp->mnt_flag = mntflags;
    149 	TAILQ_INIT(&mp->mnt_vnodelist);
    150 
    151 	mount_initspecific(mp);
    152 
    153 	return mp;
    154 }
    155 
    156 int
    157 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    158 {
    159 	int rv;
    160 
    161 	rv = VFS_MOUNT(mp, path, data, dlen);
    162 	if (rv)
    163 		return rv;
    164 
    165 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    166 	rv = VFS_START(mp, 0);
    167 	if (rv)
    168 		VFS_UNMOUNT(mp, MNT_FORCE);
    169 
    170 	return rv;
    171 }
    172 
    173 void
    174 rump_mnt_destroy(struct mount *mp)
    175 {
    176 
    177 	mount_finispecific(mp);
    178 	kmem_free(mp, sizeof(*mp));
    179 }
    180 
    181 struct componentname *
    182 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    183 	kauth_cred_t creds, struct lwp *l)
    184 {
    185 	struct componentname *cnp;
    186 
    187 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    188 
    189 	cnp->cn_nameiop = nameiop;
    190 	cnp->cn_flags = flags;
    191 
    192 	cnp->cn_pnbuf = PNBUF_GET();
    193 	strcpy(cnp->cn_pnbuf, name);
    194 	cnp->cn_nameptr = cnp->cn_pnbuf;
    195 	cnp->cn_namelen = namelen;
    196 
    197 	cnp->cn_cred = creds;
    198 
    199 	return cnp;
    200 }
    201 
    202 void
    203 rump_freecn(struct componentname *cnp, int flags)
    204 {
    205 
    206 	if (flags & RUMPCN_FREECRED)
    207 		rump_cred_destroy(cnp->cn_cred);
    208 
    209 	if (cnp->cn_flags & SAVENAME) {
    210 		if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
    211 			PNBUF_PUT(cnp->cn_pnbuf);
    212 	} else {
    213 		PNBUF_PUT(cnp->cn_pnbuf);
    214 	}
    215 	kmem_free(cnp, sizeof(*cnp));
    216 }
    217 
    218 int
    219 rump_recyclenode(struct vnode *vp)
    220 {
    221 
    222 	return vrecycle(vp, NULL, curlwp);
    223 }
    224 
    225 static struct fakeblk *
    226 _rump_fakeblk_find(const char *path)
    227 {
    228 	char buf[MAXPATHLEN];
    229 	struct fakeblk *fblk;
    230 	int error;
    231 
    232 	if (rumpuser_realpath(path, buf, &error) == NULL)
    233 		return NULL;
    234 
    235 	LIST_FOREACH(fblk, &fakeblks, entries)
    236 		if (strcmp(fblk->path, buf) == 0)
    237 			return fblk;
    238 
    239 	return NULL;
    240 }
    241 
    242 int
    243 rump_fakeblk_register(const char *path)
    244 {
    245 	char buf[MAXPATHLEN];
    246 	struct fakeblk *fblk;
    247 	int error;
    248 
    249 	if (_rump_fakeblk_find(path))
    250 		return EEXIST;
    251 
    252 	if (rumpuser_realpath(path, buf, &error) == NULL)
    253 		return error;
    254 
    255 	fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
    256 	if (fblk == NULL)
    257 		return ENOMEM;
    258 
    259 	strlcpy(fblk->path, buf, MAXPATHLEN);
    260 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    261 
    262 	return 0;
    263 }
    264 
    265 int
    266 rump_fakeblk_find(const char *path)
    267 {
    268 
    269 	return _rump_fakeblk_find(path) != NULL;
    270 }
    271 
    272 void
    273 rump_fakeblk_deregister(const char *path)
    274 {
    275 	struct fakeblk *fblk;
    276 
    277 	fblk = _rump_fakeblk_find(path);
    278 	if (fblk == NULL)
    279 		return;
    280 
    281 	LIST_REMOVE(fblk, entries);
    282 	kmem_free(fblk, sizeof(*fblk));
    283 }
    284 
    285 void
    286 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    287 {
    288 
    289 	*vtype = vp->v_type;
    290 	*vsize = vp->v_size;
    291 	if (vp->v_specnode)
    292 		*vdev = vp->v_rdev;
    293 	else
    294 		*vdev = 0;
    295 }
    296 
    297 struct vfsops *
    298 rump_vfslist_iterate(struct vfsops *ops)
    299 {
    300 
    301 	if (ops == NULL)
    302 		return LIST_FIRST(&vfs_list);
    303 	else
    304 		return LIST_NEXT(ops, vfs_list);
    305 }
    306 
    307 struct vfsops *
    308 rump_vfs_getopsbyname(const char *name)
    309 {
    310 
    311 	return vfs_getopsbyname(name);
    312 }
    313 
    314 struct vattr*
    315 rump_vattr_init()
    316 {
    317 	struct vattr *vap;
    318 
    319 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    320 	vattr_null(vap);
    321 
    322 	return vap;
    323 }
    324 
    325 void
    326 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    327 {
    328 
    329 	vap->va_type = vt;
    330 }
    331 
    332 void
    333 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    334 {
    335 
    336 	vap->va_mode = mode;
    337 }
    338 
    339 void
    340 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    341 {
    342 
    343 	vap->va_rdev = dev;
    344 }
    345 
    346 void
    347 rump_vattr_free(struct vattr *vap)
    348 {
    349 
    350 	kmem_free(vap, sizeof(*vap));
    351 }
    352 
    353 void
    354 rump_vp_incref(struct vnode *vp)
    355 {
    356 
    357 	++vp->v_usecount;
    358 }
    359 
    360 int
    361 rump_vp_getref(struct vnode *vp)
    362 {
    363 
    364 	return vp->v_usecount;
    365 }
    366 
    367 void
    368 rump_vp_decref(struct vnode *vp)
    369 {
    370 
    371 	--vp->v_usecount;
    372 }
    373 
    374 struct uio *
    375 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    376 {
    377 	struct uio *uio;
    378 	enum uio_rw uiorw;
    379 
    380 	switch (rw) {
    381 	case RUMPUIO_READ:
    382 		uiorw = UIO_READ;
    383 		break;
    384 	case RUMPUIO_WRITE:
    385 		uiorw = UIO_WRITE;
    386 		break;
    387 	default:
    388 		panic("%s: invalid rw %d", __func__, rw);
    389 	}
    390 
    391 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    392 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    393 
    394 	uio->uio_iov->iov_base = buf;
    395 	uio->uio_iov->iov_len = bufsize;
    396 
    397 	uio->uio_iovcnt = 1;
    398 	uio->uio_offset = offset;
    399 	uio->uio_resid = bufsize;
    400 	uio->uio_rw = uiorw;
    401 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    402 
    403 	return uio;
    404 }
    405 
    406 size_t
    407 rump_uio_getresid(struct uio *uio)
    408 {
    409 
    410 	return uio->uio_resid;
    411 }
    412 
    413 off_t
    414 rump_uio_getoff(struct uio *uio)
    415 {
    416 
    417 	return uio->uio_offset;
    418 }
    419 
    420 size_t
    421 rump_uio_free(struct uio *uio)
    422 {
    423 	size_t resid;
    424 
    425 	resid = uio->uio_resid;
    426 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    427 	kmem_free(uio, sizeof(*uio));
    428 
    429 	return resid;
    430 }
    431 
    432 void
    433 rump_vp_lock_exclusive(struct vnode *vp)
    434 {
    435 
    436 	/* we can skip vn_lock() */
    437 	VOP_LOCK(vp, LK_EXCLUSIVE);
    438 }
    439 
    440 void
    441 rump_vp_lock_shared(struct vnode *vp)
    442 {
    443 
    444 	VOP_LOCK(vp, LK_SHARED);
    445 }
    446 
    447 void
    448 rump_vp_unlock(struct vnode *vp)
    449 {
    450 
    451 	VOP_UNLOCK(vp, 0);
    452 }
    453 
    454 int
    455 rump_vp_islocked(struct vnode *vp)
    456 {
    457 
    458 	return VOP_ISLOCKED(vp);
    459 }
    460 
    461 void
    462 rump_vp_interlock(struct vnode *vp)
    463 {
    464 
    465 	mutex_enter(&vp->v_interlock);
    466 }
    467 
    468 int
    469 rump_vfs_unmount(struct mount *mp, int mntflags)
    470 {
    471 
    472 	return VFS_UNMOUNT(mp, mntflags);
    473 }
    474 
    475 int
    476 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    477 {
    478 	int rv;
    479 
    480 	rv = VFS_ROOT(mp, vpp);
    481 	if (rv)
    482 		return rv;
    483 
    484 	if (!lock)
    485 		VOP_UNLOCK(*vpp, 0);
    486 
    487 	return 0;
    488 }
    489 
    490 /* XXX: statvfs is different from system to system */
    491 #if 0
    492 int
    493 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    494 {
    495 
    496 	return VFS_STATVFS(mp, sbp);
    497 }
    498 #endif
    499 
    500 int
    501 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    502 {
    503 
    504 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    505 }
    506 
    507 int
    508 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    509 {
    510 
    511 	return VFS_FHTOVP(mp, fid, vpp);
    512 }
    513 
    514 int
    515 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    516 {
    517 
    518 	return VFS_VPTOFH(vp, fid, fidsize);
    519 }
    520 
    521 /*ARGSUSED*/
    522 void
    523 rump_vfs_syncwait(struct mount *mp)
    524 {
    525 	int n;
    526 
    527 	n = buf_syncwait();
    528 	if (n)
    529 		printf("syncwait: unsynced buffers: %d\n", n);
    530 }
    531 
    532 void
    533 rump_bioops_sync()
    534 {
    535 
    536 	if (bioopsp)
    537 		bioopsp->io_sync(NULL);
    538 }
    539 
    540 struct lwp *
    541 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    542 {
    543 	struct lwp *l;
    544 	struct proc *p;
    545 
    546 	l = kmem_alloc(sizeof(struct lwp), KM_SLEEP);
    547 	p = kmem_alloc(sizeof(struct proc), KM_SLEEP);
    548 	p->p_stats = &rump_stats;
    549 	p->p_cwdi = &rump_cwdi;
    550 	p->p_limit = &rump_limits;
    551         p->p_pid = pid;
    552 	p->p_vmspace = &rump_vmspace;
    553 	l->l_cred = rump_cred;
    554 	l->l_proc = p;
    555         l->l_lid = lid;
    556 
    557 	if (set)
    558 		rumpuser_set_curlwp(l);
    559 
    560 	return l;
    561 }
    562 
    563 void
    564 rump_clear_curlwp()
    565 {
    566 	struct lwp *l;
    567 
    568 	l = rumpuser_get_curlwp();
    569 	kmem_free(l->l_proc, sizeof(struct proc));
    570 	kmem_free(l, sizeof(struct lwp));
    571 	rumpuser_set_curlwp(NULL);
    572 }
    573 
    574 struct lwp *
    575 rump_get_curlwp()
    576 {
    577 	struct lwp *l;
    578 
    579 	l = rumpuser_get_curlwp();
    580 	if (l == NULL)
    581 		l = &lwp0;
    582 
    583 	return l;
    584 }
    585 
    586 int
    587 rump_splfoo()
    588 {
    589 
    590 	if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
    591 		rumpuser_rw_enter(&rumpspl, 0);
    592 		rumpuser_set_ipl(RUMPUSER_IPL_SPLFOO);
    593 	}
    594 
    595 	return 0;
    596 }
    597 
    598 static void
    599 rump_intr_enter(void)
    600 {
    601 
    602 	rumpuser_set_ipl(RUMPUSER_IPL_INTR);
    603 	rumpuser_rw_enter(&rumpspl, 1);
    604 }
    605 
    606 static void
    607 rump_intr_exit(void)
    608 {
    609 
    610 	rumpuser_rw_exit(&rumpspl);
    611 	rumpuser_clear_ipl(RUMPUSER_IPL_INTR);
    612 }
    613 
    614 void
    615 rump_splx(int dummy)
    616 {
    617 
    618 	if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
    619 		rumpuser_clear_ipl(RUMPUSER_IPL_SPLFOO);
    620 		rumpuser_rw_exit(&rumpspl);
    621 	}
    622 }
    623 
    624 void
    625 rump_biodone(void *arg, size_t count, int error)
    626 {
    627 	struct buf *bp = arg;
    628 
    629 	bp->b_resid = bp->b_bcount - count;
    630 	KASSERT(bp->b_resid >= 0);
    631 	bp->b_error = error;
    632 
    633 	rump_intr_enter();
    634 	biodone(bp);
    635 	rump_intr_exit();
    636 }
    637