Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.10
      1 /*	$NetBSD: rump.c,v 1.10 2007/08/25 10:22:31 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 	kauth_cred_t creds, 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 = creds;
    147 	cnp->cn_lwp = l;
    148 
    149 	return cnp;
    150 }
    151 
    152 void
    153 rump_freecn(struct componentname *cnp, int flags)
    154 {
    155 
    156 	if (flags & RUMPCN_FREECRED)
    157 		rump_cred_destroy(cnp->cn_cred);
    158 
    159 	if (cnp->cn_flags & SAVENAME) {
    160 		if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
    161 			PNBUF_PUT(cnp->cn_pnbuf);
    162 	} else {
    163 		PNBUF_PUT(cnp->cn_pnbuf);
    164 	}
    165 	rumpuser_free(cnp);
    166 }
    167 
    168 int
    169 rump_recyclenode(struct vnode *vp)
    170 {
    171 
    172 	return vrecycle(vp, NULL, curlwp);
    173 }
    174 
    175 static struct fakeblk *
    176 _rump_fakeblk_find(const char *path)
    177 {
    178 	char buf[MAXPATHLEN];
    179 	struct fakeblk *fblk;
    180 	int error;
    181 
    182 	if (rumpuser_realpath(path, buf, &error) == NULL)
    183 		return NULL;
    184 
    185 	LIST_FOREACH(fblk, &fakeblks, entries)
    186 		if (strcmp(fblk->path, buf) == 0)
    187 			return fblk;
    188 
    189 	return NULL;
    190 }
    191 
    192 int
    193 rump_fakeblk_register(const char *path)
    194 {
    195 	char buf[MAXPATHLEN];
    196 	struct fakeblk *fblk;
    197 	int error;
    198 
    199 	if (_rump_fakeblk_find(path))
    200 		return EEXIST;
    201 
    202 	if (rumpuser_realpath(path, buf, &error) == NULL)
    203 		return error;
    204 
    205 	fblk = rumpuser_malloc(sizeof(struct fakeblk), 1);
    206 	if (fblk == NULL)
    207 		return ENOMEM;
    208 
    209 	strlcpy(fblk->path, buf, MAXPATHLEN);
    210 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    211 
    212 	return 0;
    213 }
    214 
    215 int
    216 rump_fakeblk_find(const char *path)
    217 {
    218 
    219 	return _rump_fakeblk_find(path) != NULL;
    220 }
    221 
    222 void
    223 rump_fakeblk_deregister(const char *path)
    224 {
    225 	struct fakeblk *fblk;
    226 
    227 	fblk = _rump_fakeblk_find(path);
    228 	if (fblk == NULL)
    229 		return;
    230 
    231 	LIST_REMOVE(fblk, entries);
    232 	rumpuser_free(fblk);
    233 }
    234 
    235 void
    236 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    237 {
    238 
    239 	*vtype = vp->v_type;
    240 	*vsize = vp->v_size;
    241 	if (vp->v_specinfo)
    242 		*vdev = vp->v_rdev;
    243 	else
    244 		*vdev = 0;
    245 }
    246 
    247 struct vfsops *
    248 rump_vfslist_iterate(struct vfsops *ops)
    249 {
    250 
    251 	if (ops == NULL)
    252 		return LIST_FIRST(&vfs_list);
    253 	else
    254 		return LIST_NEXT(ops, vfs_list);
    255 }
    256 
    257 struct vfsops *
    258 rump_vfs_getopsbyname(const char *name)
    259 {
    260 
    261 	return vfs_getopsbyname(name);
    262 }
    263 
    264 struct vattr*
    265 rump_vattr_init()
    266 {
    267 	struct vattr *vap;
    268 
    269 	vap = rumpuser_malloc(sizeof(struct vattr), 0);
    270 	vattr_null(vap);
    271 
    272 	return vap;
    273 }
    274 
    275 void
    276 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    277 {
    278 
    279 	vap->va_type = vt;
    280 }
    281 
    282 void
    283 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    284 {
    285 
    286 	vap->va_mode = mode;
    287 }
    288 
    289 void
    290 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    291 {
    292 
    293 	vap->va_rdev = dev;
    294 }
    295 
    296 void
    297 rump_vattr_free(struct vattr *vap)
    298 {
    299 
    300 	rumpuser_free(vap);
    301 }
    302 
    303 void
    304 rump_vp_incref(struct vnode *vp)
    305 {
    306 
    307 	++vp->v_usecount;
    308 }
    309 
    310 int
    311 rump_vp_getref(struct vnode *vp)
    312 {
    313 
    314 	return vp->v_usecount;
    315 }
    316 
    317 void
    318 rump_vp_decref(struct vnode *vp)
    319 {
    320 
    321 	--vp->v_usecount;
    322 }
    323 
    324 struct uio *
    325 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    326 {
    327 	struct uio *uio;
    328 	enum uio_rw uiorw;
    329 
    330 	switch (rw) {
    331 	case RUMPUIO_READ:
    332 		uiorw = UIO_READ;
    333 		break;
    334 	case RUMPUIO_WRITE:
    335 		uiorw = UIO_WRITE;
    336 		break;
    337 	}
    338 
    339 	uio = rumpuser_malloc(sizeof(struct uio), 0);
    340 	uio->uio_iov = rumpuser_malloc(sizeof(struct iovec), 0);
    341 
    342 	uio->uio_iov->iov_base = buf;
    343 	uio->uio_iov->iov_len = bufsize;
    344 
    345 	uio->uio_iovcnt = 1;
    346 	uio->uio_offset = offset;
    347 	uio->uio_resid = bufsize;
    348 	uio->uio_rw = uiorw;
    349 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    350 
    351 	return uio;
    352 }
    353 
    354 size_t
    355 rump_uio_getresid(struct uio *uio)
    356 {
    357 
    358 	return uio->uio_resid;
    359 }
    360 
    361 off_t
    362 rump_uio_getoff(struct uio *uio)
    363 {
    364 
    365 	return uio->uio_offset;
    366 }
    367 
    368 size_t
    369 rump_uio_free(struct uio *uio)
    370 {
    371 	size_t resid;
    372 
    373 	resid = uio->uio_resid;
    374 	rumpuser_free(uio->uio_iov);
    375 	rumpuser_free(uio);
    376 
    377 	return resid;
    378 }
    379 
    380 void
    381 rump_vp_lock_exclusive(struct vnode *vp)
    382 {
    383 
    384 	/* we can skip vn_lock() */
    385 	VOP_LOCK(vp, LK_EXCLUSIVE);
    386 }
    387 
    388 void
    389 rump_vp_lock_shared(struct vnode *vp)
    390 {
    391 
    392 	VOP_LOCK(vp, LK_SHARED);
    393 }
    394 
    395 void
    396 rump_vp_unlock(struct vnode *vp)
    397 {
    398 
    399 	VOP_UNLOCK(vp, 0);
    400 }
    401 
    402 int
    403 rump_vp_islocked(struct vnode *vp)
    404 {
    405 
    406 	return VOP_ISLOCKED(vp);
    407 }
    408 
    409 int
    410 rump_vfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    411 {
    412 
    413 	return VFS_UNMOUNT(mp, mntflags, l);
    414 }
    415 
    416 int
    417 rump_vfs_root(struct mount *mp, struct vnode **vpp)
    418 {
    419 
    420 	return VFS_ROOT(mp, vpp);
    421 }
    422 
    423 /* XXX: statvfs is different from system to system */
    424 #if 0
    425 int
    426 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    427 {
    428 
    429 	return VFS_STATVFS(mp, sbp, l);
    430 }
    431 #endif
    432 
    433 int
    434 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred, struct lwp *l)
    435 {
    436 
    437 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred, l);
    438 }
    439 
    440 int
    441 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    442 {
    443 
    444 	return VFS_FHTOVP(mp, fid, vpp);
    445 }
    446 
    447 int
    448 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    449 {
    450 
    451 	return VFS_VPTOFH(vp, fid, fidsize);
    452 }
    453