Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.11
      1 /*	$NetBSD: rump_vfs.c,v 1.11 2009/01/13 11:40:55 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Finnish Cultural Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD");
     33 
     34 #include <sys/param.h>
     35 #include <sys/buf.h>
     36 #include <sys/conf.h>
     37 #include <sys/filedesc.h>
     38 #include <sys/lockf.h>
     39 #include <sys/module.h>
     40 #include <sys/namei.h>
     41 #include <sys/queue.h>
     42 #include <sys/vfs_syscalls.h>
     43 #include <sys/vnode.h>
     44 #include <sys/wapbl.h>
     45 
     46 #include <miscfs/specfs/specdev.h>
     47 
     48 #include <rump/rump.h>
     49 #include <rump/rumpuser.h>
     50 
     51 #include "rump_private.h"
     52 #include "rump_vfs_private.h"
     53 
     54 struct fakeblk {
     55 	char path[MAXPATHLEN];
     56 	LIST_ENTRY(fakeblk) entries;
     57 };
     58 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
     59 
     60 static struct cwdinfo rump_cwdi;
     61 
     62 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
     63 
     64 static void
     65 pvfs_init(struct proc *p)
     66 {
     67 
     68 	p->p_cwdi = cwdinit();
     69 }
     70 
     71 static void
     72 pvfs_rele(struct proc *p)
     73 {
     74 
     75 	cwdfree(p->p_cwdi);
     76 }
     77 
     78 void
     79 rump_vfs_init()
     80 {
     81 	char buf[64];
     82 	int error;
     83 
     84 	syncdelay = 0;
     85 	dovfsusermount = 1;
     86 
     87 	rumpblk_init();
     88 
     89 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     90 		desiredvnodes = strtoul(buf, NULL, 10);
     91 	} else {
     92 		desiredvnodes = 1<<16;
     93 	}
     94 
     95 	cache_cpu_init(&rump_cpu);
     96 	vfsinit();
     97 	bufinit();
     98 	wapbl_init();
     99 	cwd_sys_init();
    100 	lf_init();
    101 
    102 	rumpuser_bioinit(rump_biodone);
    103 	rumpfs_init();
    104 
    105 	rump_proc_vfs_init = pvfs_init;
    106 	rump_proc_vfs_release = pvfs_rele;
    107 
    108 	rw_init(&rump_cwdi.cwdi_lock);
    109 	rump_cwdi.cwdi_cdir = rootvnode;
    110 	vref(rump_cwdi.cwdi_cdir);
    111 	proc0.p_cwdi = &rump_cwdi;
    112 }
    113 
    114 struct mount *
    115 rump_mnt_init(struct vfsops *vfsops, int mntflags)
    116 {
    117 	struct mount *mp;
    118 
    119 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    120 
    121 	mp->mnt_op = vfsops;
    122 	mp->mnt_flag = mntflags;
    123 	TAILQ_INIT(&mp->mnt_vnodelist);
    124 	rw_init(&mp->mnt_unmounting);
    125 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    126 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    127 	mp->mnt_refcnt = 1;
    128 	mp->mnt_vnodecovered = rootvnode;
    129 
    130 	mount_initspecific(mp);
    131 
    132 	return mp;
    133 }
    134 
    135 int
    136 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    137 {
    138 	struct vnode *rvp;
    139 	int rv;
    140 
    141 	rv = VFS_MOUNT(mp, path, data, dlen);
    142 	if (rv)
    143 		return rv;
    144 
    145 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    146 	rv = VFS_START(mp, 0);
    147 	if (rv)
    148 		VFS_UNMOUNT(mp, MNT_FORCE);
    149 
    150 	/*
    151 	 * XXX: set a root for lwp0.  This is strictly not correct,
    152 	 * but makes things work for single fs case without having
    153 	 * to manually call rump_rcvp_set().
    154 	 */
    155 	VFS_ROOT(mp, &rvp);
    156 	rump_rcvp_lwpset(rvp, rvp, &lwp0);
    157 	vput(rvp);
    158 
    159 	return rv;
    160 }
    161 
    162 void
    163 rump_mnt_destroy(struct mount *mp)
    164 {
    165 
    166 	/* See rcvp XXX above */
    167 	rump_cwdi.cwdi_rdir = NULL;
    168 	vref(rootvnode);
    169 	rump_cwdi.cwdi_cdir = rootvnode;
    170 
    171 	mount_finispecific(mp);
    172 	kmem_free(mp, sizeof(*mp));
    173 }
    174 
    175 struct componentname *
    176 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    177 	kauth_cred_t creds, struct lwp *l)
    178 {
    179 	struct componentname *cnp;
    180 	const char *cp = NULL;
    181 
    182 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    183 
    184 	cnp->cn_nameiop = nameiop;
    185 	cnp->cn_flags = flags | HASBUF;
    186 
    187 	cnp->cn_pnbuf = PNBUF_GET();
    188 	strcpy(cnp->cn_pnbuf, name);
    189 	cnp->cn_nameptr = cnp->cn_pnbuf;
    190 	cnp->cn_namelen = namelen;
    191 	cnp->cn_hash = namei_hash(name, &cp);
    192 
    193 	cnp->cn_cred = creds;
    194 
    195 	return cnp;
    196 }
    197 
    198 void
    199 rump_freecn(struct componentname *cnp, int flags)
    200 {
    201 
    202 	if (flags & RUMPCN_FREECRED)
    203 		rump_cred_destroy(cnp->cn_cred);
    204 
    205 	if ((flags & RUMPCN_HASNTBUF) == 0) {
    206 		if (cnp->cn_flags & SAVENAME) {
    207 			if (flags & RUMPCN_ISLOOKUP ||cnp->cn_flags & SAVESTART)
    208 				PNBUF_PUT(cnp->cn_pnbuf);
    209 		} else {
    210 			PNBUF_PUT(cnp->cn_pnbuf);
    211 		}
    212 	}
    213 	kmem_free(cnp, sizeof(*cnp));
    214 }
    215 
    216 /* hey baby, what's your namei? */
    217 int
    218 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    219 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    220 {
    221 	struct nameidata nd;
    222 	int rv;
    223 
    224 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    225 	rv = namei(&nd);
    226 	if (rv)
    227 		return rv;
    228 
    229 	if (dvpp) {
    230 		KASSERT(flags & LOCKPARENT);
    231 		*dvpp = nd.ni_dvp;
    232 	} else {
    233 		KASSERT((flags & LOCKPARENT) == 0);
    234 	}
    235 
    236 	if (vpp) {
    237 		*vpp = nd.ni_vp;
    238 	} else {
    239 		if (nd.ni_vp) {
    240 			if (flags & LOCKLEAF)
    241 				vput(nd.ni_vp);
    242 			else
    243 				vrele(nd.ni_vp);
    244 		}
    245 	}
    246 
    247 	if (cnpp) {
    248 		struct componentname *cnp;
    249 
    250 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    251 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    252 		*cnpp = cnp;
    253 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    254 		panic("%s: pathbuf mismatch", __func__);
    255 	}
    256 
    257 	return rv;
    258 }
    259 
    260 static struct fakeblk *
    261 _rump_fakeblk_find(const char *path)
    262 {
    263 	char buf[MAXPATHLEN];
    264 	struct fakeblk *fblk;
    265 	int error;
    266 
    267 	if (rumpuser_realpath(path, buf, &error) == NULL)
    268 		return NULL;
    269 
    270 	LIST_FOREACH(fblk, &fakeblks, entries)
    271 		if (strcmp(fblk->path, buf) == 0)
    272 			return fblk;
    273 
    274 	return NULL;
    275 }
    276 
    277 int
    278 rump_fakeblk_register(const char *path)
    279 {
    280 	char buf[MAXPATHLEN];
    281 	struct fakeblk *fblk;
    282 	int error;
    283 
    284 	if (_rump_fakeblk_find(path))
    285 		return EEXIST;
    286 
    287 	if (rumpuser_realpath(path, buf, &error) == NULL)
    288 		return error;
    289 
    290 	fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
    291 	if (fblk == NULL)
    292 		return ENOMEM;
    293 
    294 	strlcpy(fblk->path, buf, MAXPATHLEN);
    295 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    296 
    297 	return 0;
    298 }
    299 
    300 int
    301 rump_fakeblk_find(const char *path)
    302 {
    303 
    304 	return _rump_fakeblk_find(path) != NULL;
    305 }
    306 
    307 void
    308 rump_fakeblk_deregister(const char *path)
    309 {
    310 	struct fakeblk *fblk;
    311 
    312 	fblk = _rump_fakeblk_find(path);
    313 	if (fblk == NULL)
    314 		return;
    315 
    316 	LIST_REMOVE(fblk, entries);
    317 	kmem_free(fblk, sizeof(*fblk));
    318 }
    319 
    320 void
    321 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    322 {
    323 
    324 	*vtype = vp->v_type;
    325 	*vsize = vp->v_size;
    326 	if (vp->v_specnode)
    327 		*vdev = vp->v_rdev;
    328 	else
    329 		*vdev = 0;
    330 }
    331 
    332 struct vfsops *
    333 rump_vfslist_iterate(struct vfsops *ops)
    334 {
    335 
    336 	if (ops == NULL)
    337 		return LIST_FIRST(&vfs_list);
    338 	else
    339 		return LIST_NEXT(ops, vfs_list);
    340 }
    341 
    342 struct vfsops *
    343 rump_vfs_getopsbyname(const char *name)
    344 {
    345 
    346 	return vfs_getopsbyname(name);
    347 }
    348 
    349 struct vattr*
    350 rump_vattr_init()
    351 {
    352 	struct vattr *vap;
    353 
    354 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    355 	vattr_null(vap);
    356 
    357 	return vap;
    358 }
    359 
    360 void
    361 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    362 {
    363 
    364 	vap->va_type = vt;
    365 }
    366 
    367 void
    368 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    369 {
    370 
    371 	vap->va_mode = mode;
    372 }
    373 
    374 void
    375 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    376 {
    377 
    378 	vap->va_rdev = dev;
    379 }
    380 
    381 void
    382 rump_vattr_free(struct vattr *vap)
    383 {
    384 
    385 	kmem_free(vap, sizeof(*vap));
    386 }
    387 
    388 void
    389 rump_vp_incref(struct vnode *vp)
    390 {
    391 
    392 	mutex_enter(&vp->v_interlock);
    393 	++vp->v_usecount;
    394 	mutex_exit(&vp->v_interlock);
    395 }
    396 
    397 int
    398 rump_vp_getref(struct vnode *vp)
    399 {
    400 
    401 	return vp->v_usecount;
    402 }
    403 
    404 void
    405 rump_vp_decref(struct vnode *vp)
    406 {
    407 
    408 	mutex_enter(&vp->v_interlock);
    409 	--vp->v_usecount;
    410 	mutex_exit(&vp->v_interlock);
    411 }
    412 
    413 /*
    414  * Really really recycle with a cherry on top.  We should be
    415  * extra-sure we can do this.  For example with p2k there is
    416  * no problem, since puffs in the kernel takes care of refcounting
    417  * for us.
    418  */
    419 void
    420 rump_vp_recycle_nokidding(struct vnode *vp)
    421 {
    422 
    423 	mutex_enter(&vp->v_interlock);
    424 	vp->v_usecount = 1;
    425 	/*
    426 	 * XXX: NFS holds a reference to the root vnode, so don't clean
    427 	 * it out.  This is very wrong, but fixing it properly would
    428 	 * take too much effort for now
    429 	 */
    430 	if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
    431 		mutex_exit(&vp->v_interlock);
    432 		return;
    433 	}
    434 	vclean(vp, DOCLOSE);
    435 	vrelel(vp, 0);
    436 }
    437 
    438 void
    439 rump_vp_rele(struct vnode *vp)
    440 {
    441 
    442 	vrele(vp);
    443 }
    444 
    445 void
    446 rump_vp_interlock(struct vnode *vp)
    447 {
    448 
    449 	mutex_enter(&vp->v_interlock);
    450 }
    451 
    452 int
    453 rump_vfs_unmount(struct mount *mp, int mntflags)
    454 {
    455 
    456 	return VFS_UNMOUNT(mp, mntflags);
    457 }
    458 
    459 int
    460 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    461 {
    462 	int rv;
    463 
    464 	rv = VFS_ROOT(mp, vpp);
    465 	if (rv)
    466 		return rv;
    467 
    468 	if (!lock)
    469 		VOP_UNLOCK(*vpp, 0);
    470 
    471 	return 0;
    472 }
    473 
    474 int
    475 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    476 {
    477 
    478 	return VFS_STATVFS(mp, sbp);
    479 }
    480 
    481 int
    482 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    483 {
    484 
    485 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    486 }
    487 
    488 int
    489 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    490 {
    491 
    492 	return VFS_FHTOVP(mp, fid, vpp);
    493 }
    494 
    495 int
    496 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    497 {
    498 
    499 	return VFS_VPTOFH(vp, fid, fidsize);
    500 }
    501 
    502 /*ARGSUSED*/
    503 void
    504 rump_vfs_syncwait(struct mount *mp)
    505 {
    506 	int n;
    507 
    508 	n = buf_syncwait();
    509 	if (n)
    510 		printf("syncwait: unsynced buffers: %d\n", n);
    511 }
    512 
    513 void
    514 rump_bioops_sync()
    515 {
    516 
    517 	if (bioopsp)
    518 		bioopsp->io_sync(NULL);
    519 }
    520 
    521 void
    522 rump_biodone(void *arg, size_t count, int error)
    523 {
    524 	struct buf *bp = arg;
    525 
    526 	bp->b_resid = bp->b_bcount - count;
    527 	KASSERT(bp->b_resid >= 0);
    528 	bp->b_error = error;
    529 
    530 	rump_intr_enter();
    531 	biodone(bp);
    532 	rump_intr_exit();
    533 }
    534 
    535 static void
    536 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    537 {
    538 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    539 
    540 	KASSERT(cvp);
    541 
    542 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    543 	if (cwdi->cwdi_rdir)
    544 		vrele(cwdi->cwdi_rdir);
    545 	if (rvp)
    546 		vref(rvp);
    547 	cwdi->cwdi_rdir = rvp;
    548 
    549 	vrele(cwdi->cwdi_cdir);
    550 	vref(cvp);
    551 	cwdi->cwdi_cdir = cvp;
    552 	rw_exit(&cwdi->cwdi_lock);
    553 }
    554 
    555 void
    556 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    557 {
    558 
    559 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    560 }
    561 
    562 struct vnode *
    563 rump_cdir_get()
    564 {
    565 	struct vnode *vp;
    566 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    567 
    568 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    569 	vp = cwdi->cwdi_cdir;
    570 	rw_exit(&cwdi->cwdi_lock);
    571 	vref(vp);
    572 
    573 	return vp;
    574 }
    575