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