Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.26
      1 /*	$NetBSD: rump_vfs.c,v 1.26 2009/10/04 13:29:36 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.26 2009/10/04 13:29:36 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 | HASBUF;
    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) {
    217 		if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
    218 			PNBUF_PUT(cnp->cn_pnbuf);
    219 	} else {
    220 		PNBUF_PUT(cnp->cn_pnbuf);
    221 	}
    222 	kmem_free(cnp, sizeof(*cnp));
    223 }
    224 
    225 /* hey baby, what's your namei? */
    226 int
    227 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    228 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    229 {
    230 	struct nameidata nd;
    231 	int rv;
    232 
    233 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    234 	rv = namei(&nd);
    235 	if (rv)
    236 		return rv;
    237 
    238 	if (dvpp) {
    239 		KASSERT(flags & LOCKPARENT);
    240 		*dvpp = nd.ni_dvp;
    241 	} else {
    242 		KASSERT((flags & LOCKPARENT) == 0);
    243 	}
    244 
    245 	if (vpp) {
    246 		*vpp = nd.ni_vp;
    247 	} else {
    248 		if (nd.ni_vp) {
    249 			if (flags & LOCKLEAF)
    250 				vput(nd.ni_vp);
    251 			else
    252 				vrele(nd.ni_vp);
    253 		}
    254 	}
    255 
    256 	if (cnpp) {
    257 		struct componentname *cnp;
    258 
    259 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    260 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    261 		*cnpp = cnp;
    262 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    263 		panic("%s: pathbuf mismatch", __func__);
    264 	}
    265 
    266 	return rv;
    267 }
    268 
    269 void
    270 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    271 {
    272 
    273 	*vtype = vp->v_type;
    274 	*vsize = vp->v_size;
    275 	if (vp->v_specnode)
    276 		*vdev = vp->v_rdev;
    277 	else
    278 		*vdev = 0;
    279 }
    280 
    281 struct vfsops *
    282 rump_vfslist_iterate(struct vfsops *ops)
    283 {
    284 
    285 	if (ops == NULL)
    286 		return LIST_FIRST(&vfs_list);
    287 	else
    288 		return LIST_NEXT(ops, vfs_list);
    289 }
    290 
    291 struct vfsops *
    292 rump_vfs_getopsbyname(const char *name)
    293 {
    294 
    295 	return vfs_getopsbyname(name);
    296 }
    297 
    298 int
    299 rump_vfs_getmp(const char *path, struct mount **mpp)
    300 {
    301 	struct vnode *vp;
    302 	int rv;
    303 
    304 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    305 		return rv;
    306 
    307 	*mpp = vp->v_mount;
    308 	vrele(vp);
    309 	return 0;
    310 }
    311 
    312 struct vattr*
    313 rump_vattr_init(void)
    314 {
    315 	struct vattr *vap;
    316 
    317 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    318 	vattr_null(vap);
    319 
    320 	return vap;
    321 }
    322 
    323 void
    324 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    325 {
    326 
    327 	vap->va_type = vt;
    328 }
    329 
    330 void
    331 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    332 {
    333 
    334 	vap->va_mode = mode;
    335 }
    336 
    337 void
    338 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    339 {
    340 
    341 	vap->va_rdev = dev;
    342 }
    343 
    344 void
    345 rump_vattr_free(struct vattr *vap)
    346 {
    347 
    348 	kmem_free(vap, sizeof(*vap));
    349 }
    350 
    351 void
    352 rump_vp_incref(struct vnode *vp)
    353 {
    354 
    355 	mutex_enter(&vp->v_interlock);
    356 	++vp->v_usecount;
    357 	mutex_exit(&vp->v_interlock);
    358 }
    359 
    360 int
    361 rump_vp_getref(struct vnode *vp)
    362 {
    363 
    364 	return vp->v_usecount;
    365 }
    366 
    367 void
    368 rump_vp_decref(struct vnode *vp)
    369 {
    370 
    371 	mutex_enter(&vp->v_interlock);
    372 	--vp->v_usecount;
    373 	mutex_exit(&vp->v_interlock);
    374 }
    375 
    376 /*
    377  * Really really recycle with a cherry on top.  We should be
    378  * extra-sure we can do this.  For example with p2k there is
    379  * no problem, since puffs in the kernel takes care of refcounting
    380  * for us.
    381  */
    382 void
    383 rump_vp_recycle_nokidding(struct vnode *vp)
    384 {
    385 
    386 	mutex_enter(&vp->v_interlock);
    387 	vp->v_usecount = 1;
    388 	/*
    389 	 * XXX: NFS holds a reference to the root vnode, so don't clean
    390 	 * it out.  This is very wrong, but fixing it properly would
    391 	 * take too much effort for now
    392 	 */
    393 	if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
    394 		mutex_exit(&vp->v_interlock);
    395 		return;
    396 	}
    397 	vclean(vp, DOCLOSE);
    398 	vrelel(vp, 0);
    399 }
    400 
    401 void
    402 rump_vp_rele(struct vnode *vp)
    403 {
    404 
    405 	vrele(vp);
    406 }
    407 
    408 void
    409 rump_vp_interlock(struct vnode *vp)
    410 {
    411 
    412 	mutex_enter(&vp->v_interlock);
    413 }
    414 
    415 int
    416 rump_vfs_unmount(struct mount *mp, int mntflags)
    417 {
    418 #if 0
    419 	struct evcnt *ev;
    420 
    421 	printf("event counters:\n");
    422 	TAILQ_FOREACH(ev, &allevents, ev_list)
    423 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    424 #endif
    425 
    426 	return VFS_UNMOUNT(mp, mntflags);
    427 }
    428 
    429 int
    430 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    431 {
    432 	int rv;
    433 
    434 	rv = VFS_ROOT(mp, vpp);
    435 	if (rv)
    436 		return rv;
    437 
    438 	if (!lock)
    439 		VOP_UNLOCK(*vpp, 0);
    440 
    441 	return 0;
    442 }
    443 
    444 int
    445 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    446 {
    447 
    448 	return VFS_STATVFS(mp, sbp);
    449 }
    450 
    451 int
    452 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    453 {
    454 
    455 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    456 }
    457 
    458 int
    459 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    460 {
    461 
    462 	return VFS_FHTOVP(mp, fid, vpp);
    463 }
    464 
    465 int
    466 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    467 {
    468 
    469 	return VFS_VPTOFH(vp, fid, fidsize);
    470 }
    471 
    472 /*ARGSUSED*/
    473 void
    474 rump_vfs_syncwait(struct mount *mp)
    475 {
    476 	int n;
    477 
    478 	n = buf_syncwait();
    479 	if (n)
    480 		printf("syncwait: unsynced buffers: %d\n", n);
    481 }
    482 
    483 void
    484 rump_biodone(void *arg, size_t count, int error)
    485 {
    486 	struct buf *bp = arg;
    487 
    488 	bp->b_resid = bp->b_bcount - count;
    489 	KASSERT(bp->b_resid >= 0);
    490 	bp->b_error = error;
    491 
    492 	biodone(bp);
    493 }
    494 
    495 static void
    496 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    497 {
    498 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    499 
    500 	KASSERT(cvp);
    501 
    502 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    503 	if (cwdi->cwdi_rdir)
    504 		vrele(cwdi->cwdi_rdir);
    505 	if (rvp)
    506 		vref(rvp);
    507 	cwdi->cwdi_rdir = rvp;
    508 
    509 	vrele(cwdi->cwdi_cdir);
    510 	vref(cvp);
    511 	cwdi->cwdi_cdir = cvp;
    512 	rw_exit(&cwdi->cwdi_lock);
    513 }
    514 
    515 void
    516 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    517 {
    518 
    519 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    520 }
    521 
    522 struct vnode *
    523 rump_cdir_get(void)
    524 {
    525 	struct vnode *vp;
    526 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    527 
    528 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    529 	vp = cwdi->cwdi_cdir;
    530 	rw_exit(&cwdi->cwdi_lock);
    531 	vref(vp);
    532 
    533 	return vp;
    534 }
    535