Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.8
      1 /*	$NetBSD: rump.c,v 1.8 2007/08/20 15:58: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/filedesc.h>
     32 #include <sys/kauth.h>
     33 #include <sys/mount.h>
     34 #include <sys/namei.h>
     35 #include <sys/queue.h>
     36 #include <sys/resourcevar.h>
     37 #include <sys/vnode.h>
     38 
     39 #include <machine/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 struct fakeblk {
     54 	char path[MAXPATHLEN];
     55 	LIST_ENTRY(fakeblk) entries;
     56 };
     57 
     58 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
     59 
     60 void
     61 rump_init()
     62 {
     63 	extern char hostname[];
     64 	extern size_t hostnamelen;
     65 	int error;
     66 
     67 	curlwp = &lwp0;
     68 	rumpvm_init();
     69 
     70 	curlwp->l_proc = &rump_proc;
     71 	curlwp->l_cred = rump_cred;
     72 	rump_proc.p_stats = &rump_stats;
     73 	rump_proc.p_cwdi = &rump_cwdi;
     74 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
     75 	rump_proc.p_limit = &rump_limits;
     76 
     77 	vfsinit();
     78 	bufinit();
     79 
     80 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
     81 	hostnamelen = strlen(hostname);
     82 }
     83 
     84 struct mount *
     85 rump_mnt_init(struct vfsops *vfsops, int mntflags)
     86 {
     87 	struct mount *mp;
     88 
     89 	mp = rumpuser_malloc(sizeof(struct mount), 0);
     90 	memset(mp, 0, sizeof(struct mount));
     91 
     92 	mp->mnt_op = vfsops;
     93 	mp->mnt_flag = mntflags;
     94 	TAILQ_INIT(&mp->mnt_vnodelist);
     95 
     96 	return mp;
     97 }
     98 
     99 int
    100 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen,
    101 	struct lwp *l)
    102 {
    103 	int rv;
    104 
    105 	rv = VFS_MOUNT(mp, path, data, dlen, l);
    106 	if (rv)
    107 		return rv;
    108 
    109 	rv = VFS_STATVFS(mp, &mp->mnt_stat, l);
    110 	if (rv) {
    111 		VFS_UNMOUNT(mp, MNT_FORCE, l);
    112 		return rv;
    113 	}
    114 
    115 	rv =  VFS_START(mp, 0, l);
    116 	if (rv)
    117 		VFS_UNMOUNT(mp, MNT_FORCE, l);
    118 
    119 	return rv;
    120 }
    121 
    122 void
    123 rump_mnt_destroy(struct mount *mp)
    124 {
    125 
    126 	rumpuser_free(mp);
    127 }
    128 
    129 struct componentname *
    130 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    131 	struct lwp *l)
    132 {
    133 	struct componentname *cnp;
    134 
    135 	cnp = rumpuser_malloc(sizeof(struct componentname), 0);
    136 	memset(cnp, 0, sizeof(struct componentname));
    137 
    138 	cnp->cn_nameiop = nameiop;
    139 	cnp->cn_flags = flags;
    140 
    141 	cnp->cn_pnbuf = PNBUF_GET();
    142 	strcpy(cnp->cn_pnbuf, name);
    143 	cnp->cn_nameptr = cnp->cn_pnbuf;
    144 	cnp->cn_namelen = namelen;
    145 
    146 	cnp->cn_cred = l->l_cred;
    147 	cnp->cn_lwp = l;
    148 
    149 	return cnp;
    150 }
    151 
    152 void
    153 rump_freecn(struct componentname *cnp, int islookup)
    154 {
    155 
    156 	if (cnp->cn_flags & SAVENAME) {
    157 		if (islookup || cnp->cn_flags & SAVESTART)
    158 			PNBUF_PUT(cnp->cn_pnbuf);
    159 	} else {
    160 		PNBUF_PUT(cnp->cn_pnbuf);
    161 	}
    162 	rumpuser_free(cnp);
    163 }
    164 
    165 int
    166 rump_recyclenode(struct vnode *vp)
    167 {
    168 
    169 	return vrecycle(vp, NULL, curlwp);
    170 }
    171 
    172 static struct fakeblk *
    173 _rump_fakeblk_find(const char *path)
    174 {
    175 	char buf[MAXPATHLEN];
    176 	struct fakeblk *fblk;
    177 	int error;
    178 
    179 	if (rumpuser_realpath(path, buf, &error) == NULL)
    180 		return NULL;
    181 
    182 	LIST_FOREACH(fblk, &fakeblks, entries)
    183 		if (strcmp(fblk->path, buf) == 0)
    184 			return fblk;
    185 
    186 	return NULL;
    187 }
    188 
    189 int
    190 rump_fakeblk_register(const char *path)
    191 {
    192 	char buf[MAXPATHLEN];
    193 	struct fakeblk *fblk;
    194 	int error;
    195 
    196 	if (_rump_fakeblk_find(path))
    197 		return EEXIST;
    198 
    199 	if (rumpuser_realpath(path, buf, &error) == NULL)
    200 		return error;
    201 
    202 	fblk = rumpuser_malloc(sizeof(struct fakeblk), 1);
    203 	if (fblk == NULL)
    204 		return ENOMEM;
    205 
    206 	strlcpy(fblk->path, buf, MAXPATHLEN);
    207 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    208 
    209 	return 0;
    210 }
    211 
    212 int
    213 rump_fakeblk_find(const char *path)
    214 {
    215 
    216 	return _rump_fakeblk_find(path) != NULL;
    217 }
    218 
    219 void
    220 rump_fakeblk_deregister(const char *path)
    221 {
    222 	struct fakeblk *fblk;
    223 
    224 	fblk = _rump_fakeblk_find(path);
    225 	if (fblk == NULL)
    226 		return;
    227 
    228 	LIST_REMOVE(fblk, entries);
    229 	rumpuser_free(fblk);
    230 }
    231 
    232 void
    233 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    234 {
    235 
    236 	*vtype = vp->v_type;
    237 	*vsize = vp->v_size;
    238 	if (vp->v_specinfo)
    239 		*vdev = vp->v_rdev;
    240 	else
    241 		*vdev = 0;
    242 }
    243 
    244 struct vfsops *
    245 rump_vfslist_iterate(struct vfsops *ops)
    246 {
    247 
    248 	if (ops == NULL)
    249 		return LIST_FIRST(&vfs_list);
    250 	else
    251 		return LIST_NEXT(ops, vfs_list);
    252 }
    253 
    254 struct vfsops *
    255 rump_vfs_getopsbyname(const char *name)
    256 {
    257 
    258 	return vfs_getopsbyname(name);
    259 }
    260 
    261 struct vattr*
    262 rump_vattr_init()
    263 {
    264 	struct vattr *vap;
    265 
    266 	vap = rumpuser_malloc(sizeof(struct vattr), 0);
    267 	vattr_null(vap);
    268 
    269 	return vap;
    270 }
    271 
    272 void
    273 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    274 {
    275 
    276 	vap->va_type = vt;
    277 }
    278 
    279 void
    280 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    281 {
    282 
    283 	vap->va_mode = mode;
    284 }
    285 
    286 void
    287 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    288 {
    289 
    290 	vap->va_rdev = dev;
    291 }
    292 
    293 void
    294 rump_vattr_free(struct vattr *vap)
    295 {
    296 
    297 	rumpuser_free(vap);
    298 }
    299 
    300 void
    301 rump_vp_incref(struct vnode *vp)
    302 {
    303 
    304 	++vp->v_usecount;
    305 }
    306 
    307 int
    308 rump_vp_getref(struct vnode *vp)
    309 {
    310 
    311 	return vp->v_usecount;
    312 }
    313 
    314 void
    315 rump_vp_decref(struct vnode *vp)
    316 {
    317 
    318 	--vp->v_usecount;
    319 }
    320 
    321 struct uio *
    322 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    323 {
    324 	struct uio *uio;
    325 	enum uio_rw uiorw;
    326 
    327 	switch (rw) {
    328 	case RUMPUIO_READ:
    329 		uiorw = UIO_READ;
    330 		break;
    331 	case RUMPUIO_WRITE:
    332 		uiorw = UIO_WRITE;
    333 		break;
    334 	}
    335 
    336 	uio = rumpuser_malloc(sizeof(struct uio), 0);
    337 	uio->uio_iov = rumpuser_malloc(sizeof(struct iovec), 0);
    338 
    339 	uio->uio_iov->iov_base = buf;
    340 	uio->uio_iov->iov_len = bufsize;
    341 
    342 	uio->uio_iovcnt = 1;
    343 	uio->uio_offset = offset;
    344 	uio->uio_resid = bufsize;
    345 	uio->uio_rw = uiorw;
    346 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    347 
    348 	return uio;
    349 }
    350 
    351 size_t
    352 rump_uio_getresid(struct uio *uio)
    353 {
    354 
    355 	return uio->uio_resid;
    356 }
    357 
    358 off_t
    359 rump_uio_getoff(struct uio *uio)
    360 {
    361 
    362 	return uio->uio_offset;
    363 }
    364 
    365 size_t
    366 rump_uio_free(struct uio *uio)
    367 {
    368 	size_t resid;
    369 
    370 	resid = uio->uio_resid;
    371 	rumpuser_free(uio->uio_iov);
    372 	rumpuser_free(uio);
    373 
    374 	return resid;
    375 }
    376 
    377 void
    378 rump_vp_lock_exclusive(struct vnode *vp)
    379 {
    380 
    381 	/* we can skip vn_lock() */
    382 	VOP_LOCK(vp, LK_EXCLUSIVE);
    383 }
    384 
    385 void
    386 rump_vp_lock_shared(struct vnode *vp)
    387 {
    388 
    389 	VOP_LOCK(vp, LK_SHARED);
    390 }
    391 
    392 void
    393 rump_vp_unlock(struct vnode *vp)
    394 {
    395 
    396 	VOP_UNLOCK(vp, 0);
    397 }
    398 
    399 int
    400 rump_vp_islocked(struct vnode *vp)
    401 {
    402 
    403 	return VOP_ISLOCKED(vp);
    404 }
    405 
    406 int
    407 rump_vfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    408 {
    409 
    410 	return VFS_UNMOUNT(mp, mntflags, l);
    411 }
    412 
    413 int
    414 rump_vfs_root(struct mount *mp, struct vnode **vpp)
    415 {
    416 
    417 	return VFS_ROOT(mp, vpp);
    418 }
    419 
    420 /* XXX: statvfs is different from system to system */
    421 #if 0
    422 int
    423 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    424 {
    425 
    426 	return VFS_STATVFS(mp, sbp, l);
    427 }
    428 #endif
    429 
    430 int
    431 rump_vfs_sync(struct mount *mp, int wait, rump_kauth_cred_t cred,
    432 	struct lwp *l)
    433 {
    434 
    435 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, (kauth_cred_t)cred,l);
    436 }
    437 
    438 int
    439 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    440 {
    441 
    442 	return VFS_FHTOVP(mp, fid, vpp);
    443 }
    444 
    445 int
    446 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    447 {
    448 
    449 	return VFS_VPTOFH(vp, fid, fidsize);
    450 }
    451