Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.70
      1  1.70   pooka /*	$NetBSD: rump.c,v 1.70 2008/10/13 19:41:13 pooka Exp $	*/
      2   1.1   pooka 
      3   1.1   pooka /*
      4   1.1   pooka  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5   1.1   pooka  *
      6   1.1   pooka  * Development of this software was supported by Google Summer of Code.
      7   1.1   pooka  *
      8   1.1   pooka  * Redistribution and use in source and binary forms, with or without
      9   1.1   pooka  * modification, are permitted provided that the following conditions
     10   1.1   pooka  * are met:
     11   1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     12   1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     13   1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   pooka  *    documentation and/or other materials provided with the distribution.
     16   1.1   pooka  *
     17   1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18   1.1   pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19   1.1   pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20   1.1   pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21   1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22   1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23   1.1   pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24   1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25   1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26   1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27   1.1   pooka  * SUCH DAMAGE.
     28   1.1   pooka  */
     29   1.1   pooka 
     30   1.1   pooka #include <sys/param.h>
     31  1.64   pooka #include <sys/atomic.h>
     32  1.66   pooka #include <sys/callout.h>
     33  1.27   pooka #include <sys/cpu.h>
     34   1.1   pooka #include <sys/filedesc.h>
     35   1.1   pooka #include <sys/kauth.h>
     36  1.14   pooka #include <sys/kmem.h>
     37  1.49  simonb #include <sys/module.h>
     38   1.1   pooka #include <sys/mount.h>
     39   1.1   pooka #include <sys/namei.h>
     40  1.65   pooka #include <sys/percpu.h>
     41   1.1   pooka #include <sys/queue.h>
     42   1.1   pooka #include <sys/resourcevar.h>
     43  1.27   pooka #include <sys/select.h>
     44  1.68   pooka #include <sys/uidinfo.h>
     45   1.1   pooka #include <sys/vnode.h>
     46  1.36   pooka #include <sys/vfs_syscalls.h>
     47  1.49  simonb #include <sys/wapbl.h>
     48  1.51   pooka #include <sys/sysctl.h>
     49   1.1   pooka 
     50   1.4   pooka #include <miscfs/specfs/specdev.h>
     51   1.4   pooka 
     52  1.48   pooka #include <rump/rumpuser.h>
     53  1.48   pooka 
     54   1.8   pooka #include "rump_private.h"
     55   1.1   pooka 
     56  1.40      ad struct proc proc0;
     57   1.1   pooka struct cwdinfo rump_cwdi;
     58   1.1   pooka struct pstats rump_stats;
     59   1.1   pooka struct plimit rump_limits;
     60   1.1   pooka struct cpu_info rump_cpu;
     61  1.40      ad struct filedesc rump_filedesc0;
     62  1.36   pooka struct proclist allproc;
     63  1.46      ad char machine[] = "rump";
     64  1.59   pooka static kauth_cred_t rump_susercred;
     65   1.1   pooka 
     66  1.19   pooka kmutex_t rump_giantlock;
     67  1.19   pooka 
     68  1.24   pooka sigset_t sigcantmask;
     69  1.24   pooka 
     70  1.54   pooka #ifdef RUMP_WITHOUT_THREADS
     71  1.54   pooka int rump_threads = 0;
     72  1.54   pooka #else
     73  1.54   pooka int rump_threads = 1;
     74  1.54   pooka #endif
     75  1.54   pooka 
     76   1.2   pooka struct fakeblk {
     77   1.2   pooka 	char path[MAXPATHLEN];
     78   1.2   pooka 	LIST_ENTRY(fakeblk) entries;
     79   1.2   pooka };
     80   1.2   pooka 
     81   1.2   pooka static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
     82   1.2   pooka 
     83  1.14   pooka static void
     84  1.14   pooka rump_aiodone_worker(struct work *wk, void *dummy)
     85  1.14   pooka {
     86  1.14   pooka 	struct buf *bp = (struct buf *)wk;
     87  1.14   pooka 
     88  1.14   pooka 	KASSERT(&bp->b_work == wk);
     89  1.14   pooka 	bp->b_iodone(bp);
     90  1.14   pooka }
     91  1.14   pooka 
     92  1.51   pooka static int rump_inited;
     93  1.51   pooka static struct emul emul_rump;
     94  1.27   pooka 
     95  1.61   pooka int
     96  1.61   pooka _rump_init(int rump_version)
     97   1.1   pooka {
     98   1.1   pooka 	extern char hostname[];
     99   1.1   pooka 	extern size_t hostnamelen;
    100  1.36   pooka 	char buf[256];
    101  1.14   pooka 	struct proc *p;
    102  1.14   pooka 	struct lwp *l;
    103   1.1   pooka 	int error;
    104   1.1   pooka 
    105  1.27   pooka 	/* XXX */
    106  1.27   pooka 	if (rump_inited)
    107  1.61   pooka 		return 0;
    108  1.27   pooka 	rump_inited = 1;
    109  1.27   pooka 
    110  1.61   pooka 	if (rump_version != RUMP_VERSION) {
    111  1.61   pooka 		printf("rump version mismatch, %d vs. %d\n",
    112  1.61   pooka 		    rump_version, RUMP_VERSION);
    113  1.61   pooka 		return EPROGMISMATCH;
    114  1.61   pooka 	}
    115  1.61   pooka 
    116  1.36   pooka 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
    117  1.36   pooka 		desiredvnodes = strtoul(buf, NULL, 10);
    118  1.36   pooka 	} else {
    119  1.36   pooka 		desiredvnodes = 1<<16;
    120  1.36   pooka 	}
    121  1.54   pooka 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    122  1.54   pooka 		rump_threads = *buf != '0';
    123  1.54   pooka 	}
    124  1.36   pooka 
    125  1.63   pooka 	rumpuser_mutex_recursive_init(&rump_giantlock.kmtx_mtx);
    126  1.63   pooka 
    127  1.52   pooka 	rumpvm_init();
    128  1.52   pooka 	rump_sleepers_init();
    129  1.52   pooka #ifdef RUMP_USE_REAL_KMEM
    130  1.52   pooka 	kmem_init();
    131  1.52   pooka #endif
    132  1.52   pooka 
    133  1.59   pooka 	kauth_init();
    134  1.59   pooka 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    135  1.59   pooka 
    136  1.47      ad 	cache_cpu_init(&rump_cpu);
    137  1.39   pooka 	rw_init(&rump_cwdi.cwdi_lock);
    138  1.59   pooka 
    139  1.14   pooka 	l = &lwp0;
    140  1.40      ad 	p = &proc0;
    141  1.14   pooka 	p->p_stats = &rump_stats;
    142  1.14   pooka 	p->p_cwdi = &rump_cwdi;
    143  1.14   pooka 	p->p_limit = &rump_limits;
    144  1.14   pooka 	p->p_pid = 0;
    145  1.40      ad 	p->p_fd = &rump_filedesc0;
    146  1.27   pooka 	p->p_vmspace = &rump_vmspace;
    147  1.51   pooka 	p->p_emul = &emul_rump;
    148  1.59   pooka 	l->l_cred = rump_cred_suserget();
    149  1.14   pooka 	l->l_proc = p;
    150  1.14   pooka 	l->l_lid = 1;
    151  1.36   pooka 	LIST_INSERT_HEAD(&allproc, p, p_list);
    152  1.36   pooka 
    153   1.1   pooka 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    154  1.27   pooka 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    155  1.70   pooka 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    156   1.1   pooka 
    157  1.12   pooka 	syncdelay = 0;
    158  1.36   pooka 	dovfsusermount = 1;
    159  1.12   pooka 
    160  1.43   pooka 	rumpuser_thrinit();
    161  1.66   pooka 	callout_startup();
    162  1.66   pooka 	callout_init_cpu(&rump_cpu);
    163  1.43   pooka 
    164  1.68   pooka 	uid_init();
    165  1.65   pooka 	percpu_init();
    166  1.43   pooka 	fd_sys_init();
    167  1.44      ad 	module_init();
    168  1.51   pooka 	sysctl_init();
    169   1.1   pooka 	vfsinit();
    170   1.5   pooka 	bufinit();
    171  1.49  simonb 	wapbl_init();
    172  1.63   pooka 	softint_init(&rump_cpu);
    173   1.1   pooka 
    174  1.31   pooka 	rumpvfs_init();
    175  1.31   pooka 
    176  1.14   pooka 	/* aieeeedondest */
    177  1.54   pooka 	if (rump_threads) {
    178  1.54   pooka 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    179  1.54   pooka 		    rump_aiodone_worker, NULL, 0, 0, 0))
    180  1.54   pooka 			panic("aiodoned");
    181  1.54   pooka 	}
    182  1.14   pooka 
    183   1.1   pooka 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    184   1.1   pooka 	hostnamelen = strlen(hostname);
    185  1.24   pooka 
    186  1.24   pooka 	sigemptyset(&sigcantmask);
    187  1.27   pooka 
    188  1.56   pooka 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    189  1.37   pooka 	rump_cwdi.cwdi_cdir = rootvnode;
    190  1.61   pooka 
    191  1.61   pooka 	return 0;
    192   1.1   pooka }
    193   1.1   pooka 
    194   1.8   pooka struct mount *
    195   1.8   pooka rump_mnt_init(struct vfsops *vfsops, int mntflags)
    196   1.1   pooka {
    197   1.1   pooka 	struct mount *mp;
    198   1.1   pooka 
    199  1.28   pooka 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    200   1.1   pooka 
    201   1.1   pooka 	mp->mnt_op = vfsops;
    202   1.8   pooka 	mp->mnt_flag = mntflags;
    203   1.1   pooka 	TAILQ_INIT(&mp->mnt_vnodelist);
    204  1.45      ad 	rw_init(&mp->mnt_unmounting);
    205  1.45      ad 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    206  1.36   pooka 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    207  1.35      ad 	mp->mnt_refcnt = 1;
    208   1.8   pooka 
    209  1.20   pooka 	mount_initspecific(mp);
    210  1.20   pooka 
    211   1.8   pooka 	return mp;
    212   1.8   pooka }
    213   1.8   pooka 
    214   1.8   pooka int
    215  1.22   pooka rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    216   1.8   pooka {
    217  1.56   pooka 	struct vnode *rvp;
    218   1.8   pooka 	int rv;
    219   1.8   pooka 
    220  1.22   pooka 	rv = VFS_MOUNT(mp, path, data, dlen);
    221   1.8   pooka 	if (rv)
    222   1.8   pooka 		return rv;
    223   1.8   pooka 
    224  1.27   pooka 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    225  1.27   pooka 	rv = VFS_START(mp, 0);
    226   1.8   pooka 	if (rv)
    227  1.22   pooka 		VFS_UNMOUNT(mp, MNT_FORCE);
    228   1.8   pooka 
    229  1.56   pooka 	/*
    230  1.56   pooka 	 * XXX: set a root for lwp0.  This is strictly not correct,
    231  1.56   pooka 	 * but makes things works for single fs case without having
    232  1.56   pooka 	 * to manually call rump_rcvp_set().
    233  1.56   pooka 	 */
    234  1.56   pooka 	VFS_ROOT(mp, &rvp);
    235  1.56   pooka 	rump_rcvp_set(rvp, rvp);
    236  1.56   pooka 	vput(rvp);
    237  1.56   pooka 
    238   1.8   pooka 	return rv;
    239   1.1   pooka }
    240   1.1   pooka 
    241   1.1   pooka void
    242   1.8   pooka rump_mnt_destroy(struct mount *mp)
    243   1.1   pooka {
    244   1.1   pooka 
    245  1.20   pooka 	mount_finispecific(mp);
    246  1.28   pooka 	kmem_free(mp, sizeof(*mp));
    247   1.1   pooka }
    248   1.1   pooka 
    249   1.1   pooka struct componentname *
    250   1.1   pooka rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    251  1.10   pooka 	kauth_cred_t creds, struct lwp *l)
    252   1.1   pooka {
    253   1.1   pooka 	struct componentname *cnp;
    254  1.31   pooka 	const char *cp = NULL;
    255   1.1   pooka 
    256  1.28   pooka 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    257   1.1   pooka 
    258   1.1   pooka 	cnp->cn_nameiop = nameiop;
    259  1.58   pooka 	cnp->cn_flags = flags | HASBUF;
    260   1.1   pooka 
    261   1.1   pooka 	cnp->cn_pnbuf = PNBUF_GET();
    262   1.1   pooka 	strcpy(cnp->cn_pnbuf, name);
    263   1.1   pooka 	cnp->cn_nameptr = cnp->cn_pnbuf;
    264   1.1   pooka 	cnp->cn_namelen = namelen;
    265  1.31   pooka 	cnp->cn_hash = namei_hash(name, &cp);
    266   1.1   pooka 
    267  1.10   pooka 	cnp->cn_cred = creds;
    268   1.1   pooka 
    269   1.1   pooka 	return cnp;
    270   1.1   pooka }
    271   1.1   pooka 
    272   1.1   pooka void
    273   1.9   pooka rump_freecn(struct componentname *cnp, int flags)
    274   1.1   pooka {
    275   1.1   pooka 
    276   1.9   pooka 	if (flags & RUMPCN_FREECRED)
    277  1.10   pooka 		rump_cred_destroy(cnp->cn_cred);
    278   1.9   pooka 
    279  1.36   pooka 	if ((flags & RUMPCN_HASNTBUF) == 0) {
    280  1.36   pooka 		if (cnp->cn_flags & SAVENAME) {
    281  1.36   pooka 			if (flags & RUMPCN_ISLOOKUP ||cnp->cn_flags & SAVESTART)
    282  1.36   pooka 				PNBUF_PUT(cnp->cn_pnbuf);
    283  1.36   pooka 		} else {
    284   1.1   pooka 			PNBUF_PUT(cnp->cn_pnbuf);
    285  1.36   pooka 		}
    286  1.36   pooka 	}
    287  1.36   pooka 	kmem_free(cnp, sizeof(*cnp));
    288  1.36   pooka }
    289  1.36   pooka 
    290  1.36   pooka /* hey baby, what's your namei? */
    291  1.36   pooka int
    292  1.36   pooka rump_namei(uint32_t op, uint32_t flags, const char *namep,
    293  1.36   pooka 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    294  1.36   pooka {
    295  1.36   pooka 	struct nameidata nd;
    296  1.36   pooka 	int rv;
    297  1.36   pooka 
    298  1.36   pooka 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    299  1.36   pooka 	rv = namei(&nd);
    300  1.36   pooka 	if (rv)
    301  1.36   pooka 		return rv;
    302  1.36   pooka 
    303  1.36   pooka 	if (dvpp) {
    304  1.36   pooka 		KASSERT(flags & LOCKPARENT);
    305  1.36   pooka 		*dvpp = nd.ni_dvp;
    306  1.36   pooka 	} else {
    307  1.36   pooka 		KASSERT((flags & LOCKPARENT) == 0);
    308  1.36   pooka 	}
    309  1.36   pooka 
    310  1.36   pooka 	if (vpp) {
    311  1.36   pooka 		*vpp = nd.ni_vp;
    312   1.1   pooka 	} else {
    313  1.36   pooka 		if (nd.ni_vp) {
    314  1.36   pooka 			if (flags & LOCKLEAF)
    315  1.36   pooka 				vput(nd.ni_vp);
    316  1.36   pooka 			else
    317  1.36   pooka 				vrele(nd.ni_vp);
    318  1.36   pooka 		}
    319   1.1   pooka 	}
    320  1.36   pooka 
    321  1.36   pooka 	if (cnpp) {
    322  1.36   pooka 		struct componentname *cnp;
    323  1.36   pooka 
    324  1.36   pooka 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    325  1.36   pooka 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    326  1.36   pooka 		*cnpp = cnp;
    327  1.36   pooka 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    328  1.36   pooka 		panic("%s: pathbuf mismatch", __func__);
    329  1.36   pooka 	}
    330  1.36   pooka 
    331  1.36   pooka 	return rv;
    332   1.1   pooka }
    333   1.1   pooka 
    334   1.2   pooka static struct fakeblk *
    335   1.2   pooka _rump_fakeblk_find(const char *path)
    336   1.2   pooka {
    337   1.2   pooka 	char buf[MAXPATHLEN];
    338   1.2   pooka 	struct fakeblk *fblk;
    339   1.3   pooka 	int error;
    340   1.2   pooka 
    341   1.3   pooka 	if (rumpuser_realpath(path, buf, &error) == NULL)
    342   1.3   pooka 		return NULL;
    343   1.2   pooka 
    344   1.2   pooka 	LIST_FOREACH(fblk, &fakeblks, entries)
    345   1.2   pooka 		if (strcmp(fblk->path, buf) == 0)
    346   1.2   pooka 			return fblk;
    347   1.2   pooka 
    348   1.2   pooka 	return NULL;
    349   1.2   pooka }
    350   1.2   pooka 
    351   1.2   pooka int
    352   1.2   pooka rump_fakeblk_register(const char *path)
    353   1.2   pooka {
    354   1.2   pooka 	char buf[MAXPATHLEN];
    355   1.2   pooka 	struct fakeblk *fblk;
    356   1.3   pooka 	int error;
    357   1.2   pooka 
    358   1.2   pooka 	if (_rump_fakeblk_find(path))
    359   1.2   pooka 		return EEXIST;
    360   1.2   pooka 
    361   1.3   pooka 	if (rumpuser_realpath(path, buf, &error) == NULL)
    362   1.3   pooka 		return error;
    363   1.3   pooka 
    364  1.28   pooka 	fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
    365   1.2   pooka 	if (fblk == NULL)
    366   1.2   pooka 		return ENOMEM;
    367   1.2   pooka 
    368   1.3   pooka 	strlcpy(fblk->path, buf, MAXPATHLEN);
    369   1.2   pooka 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    370   1.2   pooka 
    371   1.2   pooka 	return 0;
    372   1.2   pooka }
    373   1.2   pooka 
    374   1.2   pooka int
    375   1.2   pooka rump_fakeblk_find(const char *path)
    376   1.2   pooka {
    377   1.2   pooka 
    378   1.2   pooka 	return _rump_fakeblk_find(path) != NULL;
    379   1.2   pooka }
    380   1.2   pooka 
    381   1.2   pooka void
    382   1.2   pooka rump_fakeblk_deregister(const char *path)
    383   1.2   pooka {
    384   1.2   pooka 	struct fakeblk *fblk;
    385   1.2   pooka 
    386   1.2   pooka 	fblk = _rump_fakeblk_find(path);
    387   1.2   pooka 	if (fblk == NULL)
    388   1.3   pooka 		return;
    389   1.2   pooka 
    390   1.2   pooka 	LIST_REMOVE(fblk, entries);
    391  1.28   pooka 	kmem_free(fblk, sizeof(*fblk));
    392   1.2   pooka }
    393   1.4   pooka 
    394   1.4   pooka void
    395   1.4   pooka rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    396   1.4   pooka {
    397   1.4   pooka 
    398   1.4   pooka 	*vtype = vp->v_type;
    399   1.4   pooka 	*vsize = vp->v_size;
    400  1.30      ad 	if (vp->v_specnode)
    401   1.4   pooka 		*vdev = vp->v_rdev;
    402   1.4   pooka 	else
    403   1.4   pooka 		*vdev = 0;
    404   1.4   pooka }
    405   1.6   pooka 
    406   1.6   pooka struct vfsops *
    407   1.6   pooka rump_vfslist_iterate(struct vfsops *ops)
    408   1.6   pooka {
    409   1.6   pooka 
    410   1.6   pooka 	if (ops == NULL)
    411   1.6   pooka 		return LIST_FIRST(&vfs_list);
    412   1.6   pooka 	else
    413   1.6   pooka 		return LIST_NEXT(ops, vfs_list);
    414   1.6   pooka }
    415   1.6   pooka 
    416   1.6   pooka struct vfsops *
    417   1.6   pooka rump_vfs_getopsbyname(const char *name)
    418   1.6   pooka {
    419   1.6   pooka 
    420   1.6   pooka 	return vfs_getopsbyname(name);
    421   1.6   pooka }
    422   1.7   pooka 
    423   1.8   pooka struct vattr*
    424   1.8   pooka rump_vattr_init()
    425   1.8   pooka {
    426   1.8   pooka 	struct vattr *vap;
    427   1.8   pooka 
    428  1.28   pooka 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    429   1.8   pooka 	vattr_null(vap);
    430   1.8   pooka 
    431   1.8   pooka 	return vap;
    432   1.8   pooka }
    433   1.8   pooka 
    434   1.8   pooka void
    435   1.8   pooka rump_vattr_settype(struct vattr *vap, enum vtype vt)
    436   1.8   pooka {
    437   1.8   pooka 
    438   1.8   pooka 	vap->va_type = vt;
    439   1.8   pooka }
    440   1.8   pooka 
    441   1.7   pooka void
    442   1.8   pooka rump_vattr_setmode(struct vattr *vap, mode_t mode)
    443   1.7   pooka {
    444   1.7   pooka 
    445   1.8   pooka 	vap->va_mode = mode;
    446   1.8   pooka }
    447   1.8   pooka 
    448   1.8   pooka void
    449   1.8   pooka rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    450   1.8   pooka {
    451   1.8   pooka 
    452   1.8   pooka 	vap->va_rdev = dev;
    453   1.8   pooka }
    454   1.8   pooka 
    455   1.8   pooka void
    456   1.8   pooka rump_vattr_free(struct vattr *vap)
    457   1.8   pooka {
    458   1.8   pooka 
    459  1.28   pooka 	kmem_free(vap, sizeof(*vap));
    460   1.8   pooka }
    461   1.8   pooka 
    462   1.8   pooka void
    463   1.8   pooka rump_vp_incref(struct vnode *vp)
    464   1.8   pooka {
    465   1.8   pooka 
    466  1.32   pooka 	mutex_enter(&vp->v_interlock);
    467   1.8   pooka 	++vp->v_usecount;
    468  1.32   pooka 	mutex_exit(&vp->v_interlock);
    469   1.8   pooka }
    470   1.8   pooka 
    471   1.8   pooka int
    472   1.8   pooka rump_vp_getref(struct vnode *vp)
    473   1.8   pooka {
    474   1.8   pooka 
    475   1.8   pooka 	return vp->v_usecount;
    476   1.8   pooka }
    477   1.8   pooka 
    478   1.8   pooka void
    479   1.8   pooka rump_vp_decref(struct vnode *vp)
    480   1.8   pooka {
    481   1.8   pooka 
    482  1.32   pooka 	mutex_enter(&vp->v_interlock);
    483   1.8   pooka 	--vp->v_usecount;
    484  1.32   pooka 	mutex_exit(&vp->v_interlock);
    485  1.32   pooka }
    486  1.32   pooka 
    487  1.33   pooka /*
    488  1.33   pooka  * Really really recycle with a cherry on top.  We should be
    489  1.33   pooka  * extra-sure we can do this.  For example with p2k there is
    490  1.33   pooka  * no problem, since puffs in the kernel takes care of refcounting
    491  1.33   pooka  * for us.
    492  1.33   pooka  */
    493  1.32   pooka void
    494  1.33   pooka rump_vp_recycle_nokidding(struct vnode *vp)
    495  1.32   pooka {
    496  1.32   pooka 
    497  1.32   pooka 	mutex_enter(&vp->v_interlock);
    498  1.33   pooka 	vp->v_usecount = 1;
    499  1.57   pooka 	/*
    500  1.57   pooka 	 * XXX: NFS holds a reference to the root vnode, so don't clean
    501  1.57   pooka 	 * it out.  This is very wrong, but fixing it properly would
    502  1.57   pooka 	 * take too much effort for now
    503  1.57   pooka 	 */
    504  1.60   pooka 	if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
    505  1.57   pooka 		mutex_exit(&vp->v_interlock);
    506  1.57   pooka 		return;
    507  1.57   pooka 	}
    508  1.32   pooka 	vclean(vp, DOCLOSE);
    509  1.34   pooka 	vrelel(vp, 0);
    510  1.32   pooka }
    511  1.32   pooka 
    512  1.32   pooka void
    513  1.32   pooka rump_vp_rele(struct vnode *vp)
    514  1.32   pooka {
    515  1.32   pooka 
    516  1.32   pooka 	vrele(vp);
    517   1.8   pooka }
    518   1.8   pooka 
    519   1.8   pooka struct uio *
    520   1.8   pooka rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    521   1.8   pooka {
    522   1.8   pooka 	struct uio *uio;
    523   1.8   pooka 	enum uio_rw uiorw;
    524   1.8   pooka 
    525   1.8   pooka 	switch (rw) {
    526   1.8   pooka 	case RUMPUIO_READ:
    527   1.8   pooka 		uiorw = UIO_READ;
    528   1.8   pooka 		break;
    529   1.8   pooka 	case RUMPUIO_WRITE:
    530   1.8   pooka 		uiorw = UIO_WRITE;
    531   1.8   pooka 		break;
    532  1.11   pooka 	default:
    533  1.11   pooka 		panic("%s: invalid rw %d", __func__, rw);
    534   1.8   pooka 	}
    535   1.8   pooka 
    536  1.28   pooka 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    537  1.28   pooka 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    538   1.8   pooka 
    539   1.8   pooka 	uio->uio_iov->iov_base = buf;
    540   1.8   pooka 	uio->uio_iov->iov_len = bufsize;
    541   1.8   pooka 
    542   1.8   pooka 	uio->uio_iovcnt = 1;
    543   1.8   pooka 	uio->uio_offset = offset;
    544   1.8   pooka 	uio->uio_resid = bufsize;
    545   1.8   pooka 	uio->uio_rw = uiorw;
    546   1.8   pooka 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    547   1.8   pooka 
    548   1.8   pooka 	return uio;
    549   1.8   pooka }
    550   1.8   pooka 
    551   1.8   pooka size_t
    552   1.8   pooka rump_uio_getresid(struct uio *uio)
    553   1.8   pooka {
    554   1.8   pooka 
    555   1.8   pooka 	return uio->uio_resid;
    556   1.8   pooka }
    557   1.8   pooka 
    558   1.8   pooka off_t
    559   1.8   pooka rump_uio_getoff(struct uio *uio)
    560   1.8   pooka {
    561   1.8   pooka 
    562   1.8   pooka 	return uio->uio_offset;
    563   1.8   pooka }
    564   1.8   pooka 
    565   1.8   pooka size_t
    566   1.8   pooka rump_uio_free(struct uio *uio)
    567   1.8   pooka {
    568   1.8   pooka 	size_t resid;
    569   1.8   pooka 
    570   1.8   pooka 	resid = uio->uio_resid;
    571  1.28   pooka 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    572  1.28   pooka 	kmem_free(uio, sizeof(*uio));
    573   1.8   pooka 
    574   1.8   pooka 	return resid;
    575   1.8   pooka }
    576   1.8   pooka 
    577   1.8   pooka void
    578   1.8   pooka rump_vp_lock_exclusive(struct vnode *vp)
    579   1.8   pooka {
    580   1.8   pooka 
    581   1.8   pooka 	/* we can skip vn_lock() */
    582   1.8   pooka 	VOP_LOCK(vp, LK_EXCLUSIVE);
    583   1.8   pooka }
    584   1.8   pooka 
    585   1.8   pooka void
    586   1.8   pooka rump_vp_lock_shared(struct vnode *vp)
    587   1.8   pooka {
    588   1.8   pooka 
    589   1.8   pooka 	VOP_LOCK(vp, LK_SHARED);
    590   1.8   pooka }
    591   1.8   pooka 
    592   1.8   pooka void
    593   1.8   pooka rump_vp_unlock(struct vnode *vp)
    594   1.8   pooka {
    595   1.8   pooka 
    596   1.8   pooka 	VOP_UNLOCK(vp, 0);
    597   1.8   pooka }
    598   1.8   pooka 
    599   1.8   pooka int
    600   1.8   pooka rump_vp_islocked(struct vnode *vp)
    601   1.8   pooka {
    602   1.8   pooka 
    603   1.8   pooka 	return VOP_ISLOCKED(vp);
    604   1.8   pooka }
    605   1.8   pooka 
    606  1.26   pooka void
    607  1.26   pooka rump_vp_interlock(struct vnode *vp)
    608  1.26   pooka {
    609  1.26   pooka 
    610  1.26   pooka 	mutex_enter(&vp->v_interlock);
    611  1.26   pooka }
    612  1.26   pooka 
    613   1.8   pooka int
    614  1.22   pooka rump_vfs_unmount(struct mount *mp, int mntflags)
    615   1.8   pooka {
    616   1.8   pooka 
    617  1.22   pooka 	return VFS_UNMOUNT(mp, mntflags);
    618   1.8   pooka }
    619   1.8   pooka 
    620   1.8   pooka int
    621  1.12   pooka rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    622   1.8   pooka {
    623  1.12   pooka 	int rv;
    624  1.12   pooka 
    625  1.12   pooka 	rv = VFS_ROOT(mp, vpp);
    626  1.12   pooka 	if (rv)
    627  1.12   pooka 		return rv;
    628  1.12   pooka 
    629  1.12   pooka 	if (!lock)
    630  1.12   pooka 		VOP_UNLOCK(*vpp, 0);
    631   1.8   pooka 
    632  1.12   pooka 	return 0;
    633   1.8   pooka }
    634   1.8   pooka 
    635   1.8   pooka int
    636  1.22   pooka rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    637   1.8   pooka {
    638   1.8   pooka 
    639  1.22   pooka 	return VFS_STATVFS(mp, sbp);
    640   1.8   pooka }
    641   1.8   pooka 
    642   1.8   pooka int
    643  1.22   pooka rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    644   1.8   pooka {
    645   1.8   pooka 
    646  1.22   pooka 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    647   1.8   pooka }
    648   1.8   pooka 
    649   1.8   pooka int
    650   1.8   pooka rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    651   1.8   pooka {
    652   1.8   pooka 
    653   1.8   pooka 	return VFS_FHTOVP(mp, fid, vpp);
    654   1.8   pooka }
    655   1.8   pooka 
    656   1.8   pooka int
    657   1.8   pooka rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    658   1.8   pooka {
    659   1.8   pooka 
    660   1.8   pooka 	return VFS_VPTOFH(vp, fid, fidsize);
    661   1.7   pooka }
    662  1.12   pooka 
    663  1.16   pooka /*ARGSUSED*/
    664  1.16   pooka void
    665  1.16   pooka rump_vfs_syncwait(struct mount *mp)
    666  1.16   pooka {
    667  1.16   pooka 	int n;
    668  1.16   pooka 
    669  1.16   pooka 	n = buf_syncwait();
    670  1.16   pooka 	if (n)
    671  1.16   pooka 		printf("syncwait: unsynced buffers: %d\n", n);
    672  1.16   pooka }
    673  1.16   pooka 
    674  1.50   pooka int
    675  1.50   pooka rump_vfs_load(struct modinfo **mi)
    676  1.50   pooka {
    677  1.50   pooka 
    678  1.50   pooka 	if (!module_compatible((*mi)->mi_version, __NetBSD_Version__))
    679  1.50   pooka 		return EPROGMISMATCH;
    680  1.50   pooka 
    681  1.50   pooka 	return (*mi)->mi_modcmd(MODULE_CMD_INIT, NULL);
    682  1.50   pooka }
    683  1.50   pooka 
    684  1.12   pooka void
    685  1.12   pooka rump_bioops_sync()
    686  1.12   pooka {
    687  1.12   pooka 
    688  1.12   pooka 	if (bioopsp)
    689  1.12   pooka 		bioopsp->io_sync(NULL);
    690  1.12   pooka }
    691  1.14   pooka 
    692  1.14   pooka struct lwp *
    693  1.14   pooka rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    694  1.14   pooka {
    695  1.14   pooka 	struct lwp *l;
    696  1.14   pooka 	struct proc *p;
    697  1.14   pooka 
    698  1.37   pooka 	l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
    699  1.53   pooka 	if (pid != 0) {
    700  1.53   pooka 		p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
    701  1.53   pooka 		p->p_cwdi = cwdinit();
    702  1.53   pooka 
    703  1.53   pooka 		p->p_stats = &rump_stats;
    704  1.53   pooka 		p->p_limit = &rump_limits;
    705  1.53   pooka 		p->p_pid = pid;
    706  1.53   pooka 		p->p_vmspace = &rump_vmspace;
    707  1.53   pooka 		p->p_fd = fd_init(NULL);
    708  1.53   pooka 	} else {
    709  1.53   pooka 		p = &proc0;
    710  1.53   pooka 	}
    711  1.37   pooka 
    712  1.59   pooka 	l->l_cred = rump_cred_suserget();
    713  1.14   pooka 	l->l_proc = p;
    714  1.53   pooka 	l->l_lid = lid;
    715  1.53   pooka 	l->l_fd = p->p_fd;
    716  1.64   pooka 	l->l_mutex = RUMP_LMUTEX_MAGIC;
    717  1.38   pooka 
    718  1.14   pooka 	if (set)
    719  1.14   pooka 		rumpuser_set_curlwp(l);
    720  1.14   pooka 
    721  1.14   pooka 	return l;
    722  1.14   pooka }
    723  1.14   pooka 
    724  1.14   pooka void
    725  1.14   pooka rump_clear_curlwp()
    726  1.14   pooka {
    727  1.14   pooka 	struct lwp *l;
    728  1.14   pooka 
    729  1.14   pooka 	l = rumpuser_get_curlwp();
    730  1.53   pooka 	if (l->l_proc->p_pid != 0) {
    731  1.53   pooka 		fd_free();
    732  1.53   pooka 		cwdfree(l->l_proc->p_cwdi);
    733  1.59   pooka 		rump_cred_destroy(l->l_cred);
    734  1.53   pooka 		kmem_free(l->l_proc, sizeof(*l->l_proc));
    735  1.53   pooka 	}
    736  1.37   pooka 	kmem_free(l, sizeof(*l));
    737  1.14   pooka 	rumpuser_set_curlwp(NULL);
    738  1.14   pooka }
    739  1.14   pooka 
    740  1.14   pooka struct lwp *
    741  1.14   pooka rump_get_curlwp()
    742  1.14   pooka {
    743  1.14   pooka 	struct lwp *l;
    744  1.14   pooka 
    745  1.14   pooka 	l = rumpuser_get_curlwp();
    746  1.14   pooka 	if (l == NULL)
    747  1.14   pooka 		l = &lwp0;
    748  1.14   pooka 
    749  1.14   pooka 	return l;
    750  1.14   pooka }
    751  1.15   pooka 
    752  1.18   pooka int
    753  1.18   pooka rump_splfoo()
    754  1.18   pooka {
    755  1.18   pooka 
    756  1.21   pooka 	if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
    757  1.18   pooka 		rumpuser_rw_enter(&rumpspl, 0);
    758  1.21   pooka 		rumpuser_set_ipl(RUMPUSER_IPL_SPLFOO);
    759  1.21   pooka 	}
    760  1.18   pooka 
    761  1.18   pooka 	return 0;
    762  1.18   pooka }
    763  1.18   pooka 
    764  1.18   pooka static void
    765  1.18   pooka rump_intr_enter(void)
    766  1.18   pooka {
    767  1.18   pooka 
    768  1.21   pooka 	rumpuser_set_ipl(RUMPUSER_IPL_INTR);
    769  1.18   pooka 	rumpuser_rw_enter(&rumpspl, 1);
    770  1.18   pooka }
    771  1.18   pooka 
    772  1.18   pooka static void
    773  1.18   pooka rump_intr_exit(void)
    774  1.18   pooka {
    775  1.18   pooka 
    776  1.18   pooka 	rumpuser_rw_exit(&rumpspl);
    777  1.21   pooka 	rumpuser_clear_ipl(RUMPUSER_IPL_INTR);
    778  1.18   pooka }
    779  1.18   pooka 
    780  1.18   pooka void
    781  1.18   pooka rump_splx(int dummy)
    782  1.18   pooka {
    783  1.18   pooka 
    784  1.21   pooka 	if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
    785  1.21   pooka 		rumpuser_clear_ipl(RUMPUSER_IPL_SPLFOO);
    786  1.18   pooka 		rumpuser_rw_exit(&rumpspl);
    787  1.21   pooka 	}
    788  1.18   pooka }
    789  1.18   pooka 
    790  1.15   pooka void
    791  1.15   pooka rump_biodone(void *arg, size_t count, int error)
    792  1.15   pooka {
    793  1.15   pooka 	struct buf *bp = arg;
    794  1.15   pooka 
    795  1.15   pooka 	bp->b_resid = bp->b_bcount - count;
    796  1.15   pooka 	KASSERT(bp->b_resid >= 0);
    797  1.15   pooka 	bp->b_error = error;
    798  1.18   pooka 
    799  1.18   pooka 	rump_intr_enter();
    800  1.15   pooka 	biodone(bp);
    801  1.18   pooka 	rump_intr_exit();
    802  1.15   pooka }
    803  1.55   pooka 
    804  1.59   pooka kauth_cred_t
    805  1.59   pooka rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    806  1.59   pooka {
    807  1.59   pooka 	kauth_cred_t cred;
    808  1.59   pooka 	int rv;
    809  1.59   pooka 
    810  1.59   pooka 	cred = kauth_cred_alloc();
    811  1.59   pooka 	kauth_cred_setuid(cred, uid);
    812  1.59   pooka 	kauth_cred_seteuid(cred, uid);
    813  1.59   pooka 	kauth_cred_setsvuid(cred, uid);
    814  1.59   pooka 	kauth_cred_setgid(cred, gid);
    815  1.59   pooka 	kauth_cred_setgid(cred, gid);
    816  1.59   pooka 	kauth_cred_setegid(cred, gid);
    817  1.59   pooka 	kauth_cred_setsvgid(cred, gid);
    818  1.59   pooka 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    819  1.59   pooka 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    820  1.59   pooka 	assert(rv == 0);
    821  1.59   pooka 
    822  1.59   pooka 	return cred;
    823  1.59   pooka }
    824  1.59   pooka 
    825  1.59   pooka void
    826  1.59   pooka rump_cred_destroy(kauth_cred_t cred)
    827  1.59   pooka {
    828  1.59   pooka 
    829  1.59   pooka 	kauth_cred_free(cred);
    830  1.59   pooka }
    831  1.59   pooka 
    832  1.59   pooka kauth_cred_t
    833  1.59   pooka rump_cred_suserget()
    834  1.59   pooka {
    835  1.59   pooka 
    836  1.59   pooka 	kauth_cred_hold(rump_susercred);
    837  1.59   pooka 	return rump_susercred;
    838  1.59   pooka }
    839  1.59   pooka 
    840  1.64   pooka /* XXX: if they overflow, we're screwed */
    841  1.64   pooka lwpid_t
    842  1.64   pooka rump_nextlid()
    843  1.64   pooka {
    844  1.64   pooka 	static unsigned lwpid = 2;
    845  1.64   pooka 
    846  1.64   pooka 	do {
    847  1.64   pooka 		lwpid = atomic_inc_uint_nv(&lwpid);
    848  1.64   pooka 	} while (lwpid == 0);
    849  1.64   pooka 
    850  1.64   pooka 	return (lwpid_t)lwpid;
    851  1.64   pooka }
    852  1.64   pooka 
    853  1.55   pooka int _syspuffs_stub(int, int *);
    854  1.55   pooka int
    855  1.55   pooka _syspuffs_stub(int fd, int *newfd)
    856  1.55   pooka {
    857  1.55   pooka 
    858  1.55   pooka 	return ENODEV;
    859  1.55   pooka }
    860  1.67   pooka __weak_alias(syspuffs_glueinit,_syspuffs_stub);
    861