Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.27
      1 /*	$NetBSD: rump_vfs.c,v 1.27 2009/10/06 16:23:03 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: rump_vfs.c,v 1.27 2009/10/06 16:23:03 pooka Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/buf.h>
     36 #include <sys/conf.h>
     37 #include <sys/evcnt.h>
     38 #include <sys/filedesc.h>
     39 #include <sys/lockf.h>
     40 #include <sys/kthread.h>
     41 #include <sys/module.h>
     42 #include <sys/namei.h>
     43 #include <sys/queue.h>
     44 #include <sys/vfs_syscalls.h>
     45 #include <sys/vnode.h>
     46 #include <sys/wapbl.h>
     47 
     48 #include <miscfs/specfs/specdev.h>
     49 #include <miscfs/syncfs/syncfs.h>
     50 
     51 #include <rump/rump.h>
     52 #include <rump/rumpuser.h>
     53 
     54 #include "rump_private.h"
     55 #include "rump_vfs_private.h"
     56 
     57 struct cwdinfo cwdi0;
     58 
     59 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
     60 
     61 static void
     62 pvfs_init(struct proc *p)
     63 {
     64 
     65 	p->p_cwdi = cwdinit();
     66 }
     67 
     68 static void
     69 pvfs_rele(struct proc *p)
     70 {
     71 
     72 	cwdfree(p->p_cwdi);
     73 }
     74 
     75 void
     76 rump_vfs_init(void)
     77 {
     78 	char buf[64];
     79 	int error;
     80 	extern int dovfsusermount; /* XXX */
     81 
     82 	dovfsusermount = 1; /* XXX */
     83 
     84 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     85 		desiredvnodes = strtoul(buf, NULL, 10);
     86 	} else {
     87 		desiredvnodes = 1<<16;
     88 	}
     89 
     90 	rumpblk_init();
     91 
     92 	cache_cpu_init(&rump_cpu);
     93 	vfsinit();
     94 	bufinit();
     95 	wapbl_init();
     96 	cwd_sys_init();
     97 	lf_init();
     98 
     99 	rumpuser_bioinit(rump_biodone);
    100 	rumpfs_init();
    101 
    102 	rump_proc_vfs_init = pvfs_init;
    103 	rump_proc_vfs_release = pvfs_rele;
    104 
    105 	/* bootstrap cwdi */
    106 	rw_init(&cwdi0.cwdi_lock);
    107 	cwdi0.cwdi_cdir = rootvnode;
    108 	vref(cwdi0.cwdi_cdir);
    109 	proc0.p_cwdi = &cwdi0;
    110 	proc0.p_cwdi = cwdinit();
    111 
    112 	if (rump_threads) {
    113 		int rv;
    114 
    115 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    116 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    117 			panic("syncer thread create failed: %d", rv);
    118 	} else {
    119 		syncdelay = 0;
    120 	}
    121 }
    122 
    123 struct mount *
    124 rump_mnt_init(struct vfsops *vfsops, int mntflags)
    125 {
    126 	struct mount *mp;
    127 
    128 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    129 
    130 	mp->mnt_op = vfsops;
    131 	mp->mnt_flag = mntflags;
    132 	TAILQ_INIT(&mp->mnt_vnodelist);
    133 	rw_init(&mp->mnt_unmounting);
    134 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    135 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    136 	mp->mnt_refcnt = 1;
    137 	mp->mnt_vnodecovered = rootvnode;
    138 
    139 	mount_initspecific(mp);
    140 
    141 	return mp;
    142 }
    143 
    144 int
    145 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    146 {
    147 	struct vnode *rvp;
    148 	int rv;
    149 
    150 	rv = VFS_MOUNT(mp, path, data, dlen);
    151 	if (rv)
    152 		return rv;
    153 
    154 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    155 	rv = VFS_START(mp, 0);
    156 	if (rv) {
    157 		VFS_UNMOUNT(mp, MNT_FORCE);
    158 		return rv;
    159 	}
    160 
    161 	/*
    162 	 * XXX: set a root for lwp0.  This is strictly not correct,
    163 	 * but makes things work for single fs case without having
    164 	 * to manually call rump_rcvp_set().
    165 	 */
    166 	VFS_ROOT(mp, &rvp);
    167 	rump_rcvp_lwpset(rvp, rvp, &lwp0);
    168 	vput(rvp);
    169 
    170 	return rv;
    171 }
    172 
    173 void
    174 rump_mnt_destroy(struct mount *mp)
    175 {
    176 
    177 	/* See rcvp XXX above */
    178 	cwdi0.cwdi_rdir = NULL;
    179 	vref(rootvnode);
    180 	cwdi0.cwdi_cdir = rootvnode;
    181 
    182 	mount_finispecific(mp);
    183 	kmem_free(mp, sizeof(*mp));
    184 }
    185 
    186 struct componentname *
    187 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    188 	kauth_cred_t creds, struct lwp *l)
    189 {
    190 	struct componentname *cnp;
    191 	const char *cp = NULL;
    192 
    193 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    194 
    195 	cnp->cn_nameiop = nameiop;
    196 	cnp->cn_flags = flags;
    197 
    198 	cnp->cn_pnbuf = PNBUF_GET();
    199 	strcpy(cnp->cn_pnbuf, name);
    200 	cnp->cn_nameptr = cnp->cn_pnbuf;
    201 	cnp->cn_namelen = namelen;
    202 	cnp->cn_hash = namei_hash(name, &cp);
    203 
    204 	cnp->cn_cred = creds;
    205 
    206 	return cnp;
    207 }
    208 
    209 void
    210 rump_freecn(struct componentname *cnp, int flags)
    211 {
    212 
    213 	if (flags & RUMPCN_FREECRED)
    214 		rump_cred_put(cnp->cn_cred);
    215 
    216 	if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
    217 		PNBUF_PUT(cnp->cn_pnbuf);
    218 	kmem_free(cnp, sizeof(*cnp));
    219 }
    220 
    221 int
    222 rump_checksavecn(struct componentname *cnp)
    223 {
    224 
    225 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    226 		return 0;
    227 	} else {
    228 		cnp->cn_flags |= HASBUF;
    229 		return 1;
    230 	}
    231 }
    232 
    233 /* hey baby, what's your namei? */
    234 int
    235 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    236 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    237 {
    238 	struct nameidata nd;
    239 	int rv;
    240 
    241 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    242 	rv = namei(&nd);
    243 	if (rv)
    244 		return rv;
    245 
    246 	if (dvpp) {
    247 		KASSERT(flags & LOCKPARENT);
    248 		*dvpp = nd.ni_dvp;
    249 	} else {
    250 		KASSERT((flags & LOCKPARENT) == 0);
    251 	}
    252 
    253 	if (vpp) {
    254 		*vpp = nd.ni_vp;
    255 	} else {
    256 		if (nd.ni_vp) {
    257 			if (flags & LOCKLEAF)
    258 				vput(nd.ni_vp);
    259 			else
    260 				vrele(nd.ni_vp);
    261 		}
    262 	}
    263 
    264 	if (cnpp) {
    265 		struct componentname *cnp;
    266 
    267 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    268 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    269 		*cnpp = cnp;
    270 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    271 		panic("%s: pathbuf mismatch", __func__);
    272 	}
    273 
    274 	return rv;
    275 }
    276 
    277 void
    278 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    279 {
    280 
    281 	*vtype = vp->v_type;
    282 	*vsize = vp->v_size;
    283 	if (vp->v_specnode)
    284 		*vdev = vp->v_rdev;
    285 	else
    286 		*vdev = 0;
    287 }
    288 
    289 struct vfsops *
    290 rump_vfslist_iterate(struct vfsops *ops)
    291 {
    292 
    293 	if (ops == NULL)
    294 		return LIST_FIRST(&vfs_list);
    295 	else
    296 		return LIST_NEXT(ops, vfs_list);
    297 }
    298 
    299 struct vfsops *
    300 rump_vfs_getopsbyname(const char *name)
    301 {
    302 
    303 	return vfs_getopsbyname(name);
    304 }
    305 
    306 int
    307 rump_vfs_getmp(const char *path, struct mount **mpp)
    308 {
    309 	struct vnode *vp;
    310 	int rv;
    311 
    312 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    313 		return rv;
    314 
    315 	*mpp = vp->v_mount;
    316 	vrele(vp);
    317 	return 0;
    318 }
    319 
    320 struct vattr*
    321 rump_vattr_init(void)
    322 {
    323 	struct vattr *vap;
    324 
    325 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    326 	vattr_null(vap);
    327 
    328 	return vap;
    329 }
    330 
    331 void
    332 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    333 {
    334 
    335 	vap->va_type = vt;
    336 }
    337 
    338 void
    339 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    340 {
    341 
    342 	vap->va_mode = mode;
    343 }
    344 
    345 void
    346 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    347 {
    348 
    349 	vap->va_rdev = dev;
    350 }
    351 
    352 void
    353 rump_vattr_free(struct vattr *vap)
    354 {
    355 
    356 	kmem_free(vap, sizeof(*vap));
    357 }
    358 
    359 void
    360 rump_vp_incref(struct vnode *vp)
    361 {
    362 
    363 	mutex_enter(&vp->v_interlock);
    364 	++vp->v_usecount;
    365 	mutex_exit(&vp->v_interlock);
    366 }
    367 
    368 int
    369 rump_vp_getref(struct vnode *vp)
    370 {
    371 
    372 	return vp->v_usecount;
    373 }
    374 
    375 void
    376 rump_vp_decref(struct vnode *vp)
    377 {
    378 
    379 	mutex_enter(&vp->v_interlock);
    380 	--vp->v_usecount;
    381 	mutex_exit(&vp->v_interlock);
    382 }
    383 
    384 /*
    385  * Really really recycle with a cherry on top.  We should be
    386  * extra-sure we can do this.  For example with p2k there is
    387  * no problem, since puffs in the kernel takes care of refcounting
    388  * for us.
    389  */
    390 void
    391 rump_vp_recycle_nokidding(struct vnode *vp)
    392 {
    393 
    394 	mutex_enter(&vp->v_interlock);
    395 	vp->v_usecount = 1;
    396 	/*
    397 	 * XXX: NFS holds a reference to the root vnode, so don't clean
    398 	 * it out.  This is very wrong, but fixing it properly would
    399 	 * take too much effort for now
    400 	 */
    401 	if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
    402 		mutex_exit(&vp->v_interlock);
    403 		return;
    404 	}
    405 	vclean(vp, DOCLOSE);
    406 	vrelel(vp, 0);
    407 }
    408 
    409 void
    410 rump_vp_rele(struct vnode *vp)
    411 {
    412 
    413 	vrele(vp);
    414 }
    415 
    416 void
    417 rump_vp_interlock(struct vnode *vp)
    418 {
    419 
    420 	mutex_enter(&vp->v_interlock);
    421 }
    422 
    423 int
    424 rump_vfs_unmount(struct mount *mp, int mntflags)
    425 {
    426 #if 0
    427 	struct evcnt *ev;
    428 
    429 	printf("event counters:\n");
    430 	TAILQ_FOREACH(ev, &allevents, ev_list)
    431 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    432 #endif
    433 
    434 	return VFS_UNMOUNT(mp, mntflags);
    435 }
    436 
    437 int
    438 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    439 {
    440 	int rv;
    441 
    442 	rv = VFS_ROOT(mp, vpp);
    443 	if (rv)
    444 		return rv;
    445 
    446 	if (!lock)
    447 		VOP_UNLOCK(*vpp, 0);
    448 
    449 	return 0;
    450 }
    451 
    452 int
    453 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    454 {
    455 
    456 	return VFS_STATVFS(mp, sbp);
    457 }
    458 
    459 int
    460 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    461 {
    462 
    463 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    464 }
    465 
    466 int
    467 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    468 {
    469 
    470 	return VFS_FHTOVP(mp, fid, vpp);
    471 }
    472 
    473 int
    474 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    475 {
    476 
    477 	return VFS_VPTOFH(vp, fid, fidsize);
    478 }
    479 
    480 /*ARGSUSED*/
    481 void
    482 rump_vfs_syncwait(struct mount *mp)
    483 {
    484 	int n;
    485 
    486 	n = buf_syncwait();
    487 	if (n)
    488 		printf("syncwait: unsynced buffers: %d\n", n);
    489 }
    490 
    491 void
    492 rump_biodone(void *arg, size_t count, int error)
    493 {
    494 	struct buf *bp = arg;
    495 
    496 	bp->b_resid = bp->b_bcount - count;
    497 	KASSERT(bp->b_resid >= 0);
    498 	bp->b_error = error;
    499 
    500 	biodone(bp);
    501 }
    502 
    503 static void
    504 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    505 {
    506 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    507 
    508 	KASSERT(cvp);
    509 
    510 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    511 	if (cwdi->cwdi_rdir)
    512 		vrele(cwdi->cwdi_rdir);
    513 	if (rvp)
    514 		vref(rvp);
    515 	cwdi->cwdi_rdir = rvp;
    516 
    517 	vrele(cwdi->cwdi_cdir);
    518 	vref(cvp);
    519 	cwdi->cwdi_cdir = cvp;
    520 	rw_exit(&cwdi->cwdi_lock);
    521 }
    522 
    523 void
    524 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    525 {
    526 
    527 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    528 }
    529 
    530 struct vnode *
    531 rump_cdir_get(void)
    532 {
    533 	struct vnode *vp;
    534 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    535 
    536 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    537 	vp = cwdi->cwdi_cdir;
    538 	rw_exit(&cwdi->cwdi_lock);
    539 	vref(vp);
    540 
    541 	return vp;
    542 }
    543