Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.31
      1 /*	$NetBSD: rump_vfs.c,v 1.31 2009/10/14 17:29:20 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.31 2009/10/14 17:29:20 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 void
    124 rump_vfs_fini(void)
    125 {
    126 
    127 	vfs_shutdown();
    128 }
    129 
    130 struct componentname *
    131 rumppriv_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    132 	kauth_cred_t creds, struct lwp *l)
    133 {
    134 	struct componentname *cnp;
    135 	const char *cp = NULL;
    136 
    137 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    138 
    139 	cnp->cn_nameiop = nameiop;
    140 	cnp->cn_flags = flags;
    141 
    142 	cnp->cn_pnbuf = PNBUF_GET();
    143 	strcpy(cnp->cn_pnbuf, name);
    144 	cnp->cn_nameptr = cnp->cn_pnbuf;
    145 	cnp->cn_namelen = namelen;
    146 	cnp->cn_hash = namei_hash(name, &cp);
    147 
    148 	cnp->cn_cred = creds;
    149 
    150 	return cnp;
    151 }
    152 
    153 void
    154 rumppriv_freecn(struct componentname *cnp, int flags)
    155 {
    156 
    157 	if (flags & RUMPCN_FREECRED)
    158 		rumppriv_cred_put(cnp->cn_cred);
    159 
    160 	if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
    161 		PNBUF_PUT(cnp->cn_pnbuf);
    162 	kmem_free(cnp, sizeof(*cnp));
    163 }
    164 
    165 int
    166 rumppriv_checksavecn(struct componentname *cnp)
    167 {
    168 
    169 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    170 		return 0;
    171 	} else {
    172 		cnp->cn_flags |= HASBUF;
    173 		return 1;
    174 	}
    175 }
    176 
    177 /* hey baby, what's your namei? */
    178 int
    179 rumppriv_namei(uint32_t op, uint32_t flags, const char *namep,
    180 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    181 {
    182 	struct nameidata nd;
    183 	int rv;
    184 
    185 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    186 	rv = namei(&nd);
    187 	if (rv)
    188 		return rv;
    189 
    190 	if (dvpp) {
    191 		KASSERT(flags & LOCKPARENT);
    192 		*dvpp = nd.ni_dvp;
    193 	} else {
    194 		KASSERT((flags & LOCKPARENT) == 0);
    195 	}
    196 
    197 	if (vpp) {
    198 		*vpp = nd.ni_vp;
    199 	} else {
    200 		if (nd.ni_vp) {
    201 			if (flags & LOCKLEAF)
    202 				vput(nd.ni_vp);
    203 			else
    204 				vrele(nd.ni_vp);
    205 		}
    206 	}
    207 
    208 	if (cnpp) {
    209 		struct componentname *cnp;
    210 
    211 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    212 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    213 		*cnpp = cnp;
    214 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    215 		panic("%s: pathbuf mismatch", __func__);
    216 	}
    217 
    218 	return rv;
    219 }
    220 
    221 void
    222 rumppriv_getvninfo(struct vnode *vp, enum vtype *vtype,
    223 	voff_t *vsize, dev_t *vdev)
    224 {
    225 
    226 	*vtype = vp->v_type;
    227 	*vsize = vp->v_size;
    228 	if (vp->v_specnode)
    229 		*vdev = vp->v_rdev;
    230 	else
    231 		*vdev = 0;
    232 }
    233 
    234 struct vfsops *
    235 rumppriv_vfslist_iterate(struct vfsops *ops)
    236 {
    237 
    238 	if (ops == NULL)
    239 		return LIST_FIRST(&vfs_list);
    240 	else
    241 		return LIST_NEXT(ops, vfs_list);
    242 }
    243 
    244 struct vfsops *
    245 rumppriv_vfs_getopsbyname(const char *name)
    246 {
    247 
    248 	return vfs_getopsbyname(name);
    249 }
    250 
    251 int
    252 rumppriv_vfs_getmp(const char *path, struct mount **mpp)
    253 {
    254 	struct vnode *vp;
    255 	int rv;
    256 
    257 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    258 		return rv;
    259 
    260 	*mpp = vp->v_mount;
    261 	vrele(vp);
    262 	return 0;
    263 }
    264 
    265 struct vattr*
    266 rumppriv_vattr_init(void)
    267 {
    268 	struct vattr *vap;
    269 
    270 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    271 	vattr_null(vap);
    272 
    273 	return vap;
    274 }
    275 
    276 void
    277 rumppriv_vattr_settype(struct vattr *vap, enum vtype vt)
    278 {
    279 
    280 	vap->va_type = vt;
    281 }
    282 
    283 void
    284 rumppriv_vattr_setmode(struct vattr *vap, mode_t mode)
    285 {
    286 
    287 	vap->va_mode = mode;
    288 }
    289 
    290 void
    291 rumppriv_vattr_setrdev(struct vattr *vap, dev_t dev)
    292 {
    293 
    294 	vap->va_rdev = dev;
    295 }
    296 
    297 void
    298 rumppriv_vattr_free(struct vattr *vap)
    299 {
    300 
    301 	kmem_free(vap, sizeof(*vap));
    302 }
    303 
    304 void
    305 rumppriv_vp_incref(struct vnode *vp)
    306 {
    307 
    308 	vref(vp);
    309 }
    310 
    311 int
    312 rumppriv_vp_getref(struct vnode *vp)
    313 {
    314 
    315 	return vp->v_usecount;
    316 }
    317 
    318 void
    319 rumppriv_vp_rele(struct vnode *vp)
    320 {
    321 
    322 	vrele(vp);
    323 }
    324 
    325 void
    326 rumppriv_vp_interlock(struct vnode *vp)
    327 {
    328 
    329 	mutex_enter(&vp->v_interlock);
    330 }
    331 
    332 int
    333 rumppriv_vfs_unmount(struct mount *mp, int mntflags)
    334 {
    335 #if 0
    336 	struct evcnt *ev;
    337 
    338 	printf("event counters:\n");
    339 	TAILQ_FOREACH(ev, &allevents, ev_list)
    340 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    341 #endif
    342 
    343 	return VFS_UNMOUNT(mp, mntflags);
    344 }
    345 
    346 int
    347 rumppriv_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    348 {
    349 	int rv;
    350 
    351 	rv = VFS_ROOT(mp, vpp);
    352 	if (rv)
    353 		return rv;
    354 
    355 	if (!lock)
    356 		VOP_UNLOCK(*vpp, 0);
    357 
    358 	return 0;
    359 }
    360 
    361 int
    362 rumppriv_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    363 {
    364 
    365 	return VFS_STATVFS(mp, sbp);
    366 }
    367 
    368 int
    369 rumppriv_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    370 {
    371 
    372 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    373 }
    374 
    375 int
    376 rumppriv_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    377 {
    378 
    379 	return VFS_FHTOVP(mp, fid, vpp);
    380 }
    381 
    382 int
    383 rumppriv_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    384 {
    385 
    386 	return VFS_VPTOFH(vp, fid, fidsize);
    387 }
    388 
    389 /*ARGSUSED*/
    390 void
    391 rumppriv_vfs_syncwait(struct mount *mp)
    392 {
    393 	int n;
    394 
    395 	n = buf_syncwait();
    396 	if (n)
    397 		printf("syncwait: unsynced buffers: %d\n", n);
    398 }
    399 
    400 void
    401 rump_biodone(void *arg, size_t count, int error)
    402 {
    403 	struct buf *bp = arg;
    404 
    405 	bp->b_resid = bp->b_bcount - count;
    406 	KASSERT(bp->b_resid >= 0);
    407 	bp->b_error = error;
    408 
    409 	biodone(bp);
    410 }
    411 
    412 static void
    413 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    414 {
    415 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    416 
    417 	KASSERT(cvp);
    418 
    419 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    420 	if (cwdi->cwdi_rdir)
    421 		vrele(cwdi->cwdi_rdir);
    422 	if (rvp)
    423 		vref(rvp);
    424 	cwdi->cwdi_rdir = rvp;
    425 
    426 	vrele(cwdi->cwdi_cdir);
    427 	vref(cvp);
    428 	cwdi->cwdi_cdir = cvp;
    429 	rw_exit(&cwdi->cwdi_lock);
    430 }
    431 
    432 void
    433 rumppriv_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    434 {
    435 
    436 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    437 }
    438 
    439 struct vnode *
    440 rumppriv_cdir_get(void)
    441 {
    442 	struct vnode *vp;
    443 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    444 
    445 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    446 	vp = cwdi->cwdi_cdir;
    447 	rw_exit(&cwdi->cwdi_lock);
    448 	vref(vp);
    449 
    450 	return vp;
    451 }
    452