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