Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.30
      1 /*	$NetBSD: rump_vfs.c,v 1.30 2009/10/09 14:41: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.30 2009/10/09 14:41: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 void
    124 rump_vfs_fini(void)
    125 {
    126 
    127 	vfs_shutdown();
    128 }
    129 
    130 struct componentname *
    131 rump_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 rump_freecn(struct componentname *cnp, int flags)
    155 {
    156 
    157 	if (flags & RUMPCN_FREECRED)
    158 		rump_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 rump_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 rump_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 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    223 {
    224 
    225 	*vtype = vp->v_type;
    226 	*vsize = vp->v_size;
    227 	if (vp->v_specnode)
    228 		*vdev = vp->v_rdev;
    229 	else
    230 		*vdev = 0;
    231 }
    232 
    233 struct vfsops *
    234 rump_vfslist_iterate(struct vfsops *ops)
    235 {
    236 
    237 	if (ops == NULL)
    238 		return LIST_FIRST(&vfs_list);
    239 	else
    240 		return LIST_NEXT(ops, vfs_list);
    241 }
    242 
    243 struct vfsops *
    244 rump_vfs_getopsbyname(const char *name)
    245 {
    246 
    247 	return vfs_getopsbyname(name);
    248 }
    249 
    250 int
    251 rump_vfs_getmp(const char *path, struct mount **mpp)
    252 {
    253 	struct vnode *vp;
    254 	int rv;
    255 
    256 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    257 		return rv;
    258 
    259 	*mpp = vp->v_mount;
    260 	vrele(vp);
    261 	return 0;
    262 }
    263 
    264 struct vattr*
    265 rump_vattr_init(void)
    266 {
    267 	struct vattr *vap;
    268 
    269 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    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 	kmem_free(vap, sizeof(*vap));
    301 }
    302 
    303 void
    304 rump_vp_incref(struct vnode *vp)
    305 {
    306 
    307 	vref(vp);
    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_rele(struct vnode *vp)
    319 {
    320 
    321 	vrele(vp);
    322 }
    323 
    324 void
    325 rump_vp_interlock(struct vnode *vp)
    326 {
    327 
    328 	mutex_enter(&vp->v_interlock);
    329 }
    330 
    331 int
    332 rump_vfs_unmount(struct mount *mp, int mntflags)
    333 {
    334 #if 0
    335 	struct evcnt *ev;
    336 
    337 	printf("event counters:\n");
    338 	TAILQ_FOREACH(ev, &allevents, ev_list)
    339 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    340 #endif
    341 
    342 	return VFS_UNMOUNT(mp, mntflags);
    343 }
    344 
    345 int
    346 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    347 {
    348 	int rv;
    349 
    350 	rv = VFS_ROOT(mp, vpp);
    351 	if (rv)
    352 		return rv;
    353 
    354 	if (!lock)
    355 		VOP_UNLOCK(*vpp, 0);
    356 
    357 	return 0;
    358 }
    359 
    360 int
    361 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    362 {
    363 
    364 	return VFS_STATVFS(mp, sbp);
    365 }
    366 
    367 int
    368 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    369 {
    370 
    371 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    372 }
    373 
    374 int
    375 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    376 {
    377 
    378 	return VFS_FHTOVP(mp, fid, vpp);
    379 }
    380 
    381 int
    382 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    383 {
    384 
    385 	return VFS_VPTOFH(vp, fid, fidsize);
    386 }
    387 
    388 /*ARGSUSED*/
    389 void
    390 rump_vfs_syncwait(struct mount *mp)
    391 {
    392 	int n;
    393 
    394 	n = buf_syncwait();
    395 	if (n)
    396 		printf("syncwait: unsynced buffers: %d\n", n);
    397 }
    398 
    399 void
    400 rump_biodone(void *arg, size_t count, int error)
    401 {
    402 	struct buf *bp = arg;
    403 
    404 	bp->b_resid = bp->b_bcount - count;
    405 	KASSERT(bp->b_resid >= 0);
    406 	bp->b_error = error;
    407 
    408 	biodone(bp);
    409 }
    410 
    411 static void
    412 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    413 {
    414 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    415 
    416 	KASSERT(cvp);
    417 
    418 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    419 	if (cwdi->cwdi_rdir)
    420 		vrele(cwdi->cwdi_rdir);
    421 	if (rvp)
    422 		vref(rvp);
    423 	cwdi->cwdi_rdir = rvp;
    424 
    425 	vrele(cwdi->cwdi_cdir);
    426 	vref(cvp);
    427 	cwdi->cwdi_cdir = cvp;
    428 	rw_exit(&cwdi->cwdi_lock);
    429 }
    430 
    431 void
    432 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    433 {
    434 
    435 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    436 }
    437 
    438 struct vnode *
    439 rump_cdir_get(void)
    440 {
    441 	struct vnode *vp;
    442 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    443 
    444 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    445 	vp = cwdi->cwdi_cdir;
    446 	rw_exit(&cwdi->cwdi_lock);
    447 	vref(vp);
    448 
    449 	return vp;
    450 }
    451