Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.58
      1 /*	$NetBSD: rump_vfs.c,v 1.58 2010/09/07 21:11:10 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.58 2010/09/07 21:11:10 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/fstrans.h>
     40 #include <sys/lockf.h>
     41 #include <sys/kthread.h>
     42 #include <sys/module.h>
     43 #include <sys/namei.h>
     44 #include <sys/queue.h>
     45 #include <sys/stat.h>
     46 #include <sys/vfs_syscalls.h>
     47 #include <sys/vnode.h>
     48 #include <sys/wapbl.h>
     49 
     50 #include <miscfs/specfs/specdev.h>
     51 #include <miscfs/syncfs/syncfs.h>
     52 
     53 #include <rump/rump.h>
     54 #include <rump/rumpuser.h>
     55 
     56 #include "rump_private.h"
     57 #include "rump_vfs_private.h"
     58 
     59 struct cwdinfo cwdi0;
     60 const char *rootfstype = ROOT_FSTYPE_ANY;
     61 
     62 static void
     63 pvfs_init(struct proc *p)
     64 {
     65 
     66 	p->p_cwdi = cwdinit();
     67 }
     68 
     69 static void
     70 pvfs_rele(struct proc *p)
     71 {
     72 
     73 	cwdfree(p->p_cwdi);
     74 }
     75 
     76 void
     77 rump_vfs_init(void)
     78 {
     79 	extern struct devsw_conv devsw_conv0[];
     80 	extern int max_devsw_convs;
     81 	extern struct vfsops rumpfs_vfsops;
     82 	char buf[64];
     83 	int error;
     84 	int rv, i;
     85 	extern int dovfsusermount; /* XXX */
     86 
     87 	dovfsusermount = 1; /* XXX */
     88 
     89 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     90 		desiredvnodes = strtoul(buf, NULL, 10);
     91 	} else {
     92 		desiredvnodes = 1<<10;
     93 	}
     94 
     95 	rumpblk_init();
     96 
     97 	for (i = 0; i < ncpu; i++) {
     98 		struct cpu_info *ci = cpu_lookup(i);
     99 		cache_cpu_init(ci);
    100 	}
    101 
    102 	/* make number of bufpages 5% of total memory limit */
    103 	if (rump_physmemlimit != RUMPMEM_UNLIMITED) {
    104 		extern u_int bufpages;
    105 		bufpages = rump_physmemlimit / (20 * PAGE_SIZE);
    106 	}
    107 
    108 	vfsinit();
    109 	bufinit();
    110 	cwd_sys_init();
    111 	lf_init();
    112 	spec_init();
    113 	fstrans_init();
    114 
    115 	if (rump_threads) {
    116 		if ((rv = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL,
    117 		    rumpuser_biothread, rump_biodone, NULL, "rmpabio")) != 0)
    118 			panic("syncer thread create failed: %d", rv);
    119 	}
    120 
    121 	root_device = &rump_rootdev;
    122 
    123 	/* bootstrap cwdi (rest done in vfs_mountroot() */
    124 	rw_init(&cwdi0.cwdi_lock);
    125 	proc0.p_cwdi = &cwdi0;
    126 	proc0.p_cwdi = cwdinit();
    127 
    128 	vfs_attach(&rumpfs_vfsops);
    129 	vfs_mountroot();
    130 
    131 	/* "mtree": create /dev */
    132 	do_sys_mkdir("/dev", 0777, UIO_SYSSPACE);
    133 	rump_devnull_init();
    134 
    135 	rump_proc_vfs_init = pvfs_init;
    136 	rump_proc_vfs_release = pvfs_rele;
    137 
    138 	if (rump_threads) {
    139 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    140 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    141 			panic("syncer thread create failed: %d", rv);
    142 	} else {
    143 		syncdelay = 0;
    144 	}
    145 
    146 	/*
    147 	 * On archs where the native kernel ABI is supported, map
    148 	 * host module directory to rump.  This means that kernel
    149 	 * modules from the host will be autoloaded to rump kernels.
    150 	 */
    151 #ifdef _RUMP_NATIVE_ABI
    152 	{
    153 	char *mbase;
    154 
    155 	if (rumpuser_getenv("RUMP_MODULEBASE", buf, sizeof(buf), &error) == 0)
    156 		mbase = buf;
    157 	else
    158 		mbase = module_base;
    159 
    160 	if (strlen(mbase) != 0 && *mbase != '0') {
    161 		rump_etfs_register(module_base, mbase, RUMP_ETFS_DIR_SUBDIRS);
    162 	}
    163 	}
    164 #endif
    165 
    166 	module_init_class(MODULE_CLASS_VFS);
    167 
    168 	rump_vfs_builddevs(devsw_conv0, max_devsw_convs);
    169 
    170 	rump_component_init(RUMP_COMPONENT_VFS);
    171 }
    172 
    173 void
    174 rump_vfs_fini(void)
    175 {
    176 
    177 	vfs_shutdown();
    178 }
    179 
    180 struct componentname *
    181 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    182 	kauth_cred_t creds, struct lwp *l)
    183 {
    184 	struct componentname *cnp;
    185 	const char *cp = NULL;
    186 
    187 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    188 
    189 	cnp->cn_nameiop = nameiop;
    190 	cnp->cn_flags = flags;
    191 
    192 	cnp->cn_pnbuf = PNBUF_GET();
    193 	strcpy(cnp->cn_pnbuf, name);
    194 	cnp->cn_nameptr = cnp->cn_pnbuf;
    195 	cnp->cn_namelen = namelen;
    196 	cnp->cn_hash = namei_hash(name, &cp);
    197 
    198 	cnp->cn_cred = creds;
    199 
    200 	return cnp;
    201 }
    202 
    203 void
    204 rump_freecn(struct componentname *cnp, int flags)
    205 {
    206 
    207 	if (flags & RUMPCN_FREECRED)
    208 		rump_cred_put(cnp->cn_cred);
    209 
    210 	if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
    211 		PNBUF_PUT(cnp->cn_pnbuf);
    212 	kmem_free(cnp, sizeof(*cnp));
    213 }
    214 
    215 int
    216 rump_checksavecn(struct componentname *cnp)
    217 {
    218 
    219 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    220 		return 0;
    221 	} else {
    222 		cnp->cn_flags |= HASBUF;
    223 		return 1;
    224 	}
    225 }
    226 
    227 /* hey baby, what's your namei? */
    228 int
    229 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    230 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    231 {
    232 	struct nameidata nd;
    233 	int rv;
    234 
    235 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    236 	rv = namei(&nd);
    237 	if (rv)
    238 		return rv;
    239 
    240 	if (dvpp) {
    241 		KASSERT(flags & LOCKPARENT);
    242 		*dvpp = nd.ni_dvp;
    243 	} else {
    244 		KASSERT((flags & LOCKPARENT) == 0);
    245 	}
    246 
    247 	if (vpp) {
    248 		*vpp = nd.ni_vp;
    249 	} else {
    250 		if (nd.ni_vp) {
    251 			if (flags & LOCKLEAF)
    252 				vput(nd.ni_vp);
    253 			else
    254 				vrele(nd.ni_vp);
    255 		}
    256 	}
    257 
    258 	if (cnpp) {
    259 		struct componentname *cnp;
    260 
    261 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    262 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    263 		*cnpp = cnp;
    264 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    265 		panic("%s: pathbuf mismatch", __func__);
    266 	}
    267 
    268 	return rv;
    269 }
    270 
    271 void
    272 rump_getvninfo(struct vnode *vp, enum vtype *vtype,
    273 	voff_t *vsize, dev_t *vdev)
    274 {
    275 
    276 	*vtype = vp->v_type;
    277 	*vsize = vp->v_size;
    278 	if (vp->v_specnode)
    279 		*vdev = vp->v_rdev;
    280 	else
    281 		*vdev = 0;
    282 }
    283 
    284 struct vfsops *
    285 rump_vfslist_iterate(struct vfsops *ops)
    286 {
    287 
    288 	if (ops == NULL)
    289 		return LIST_FIRST(&vfs_list);
    290 	else
    291 		return LIST_NEXT(ops, vfs_list);
    292 }
    293 
    294 struct vfsops *
    295 rump_vfs_getopsbyname(const char *name)
    296 {
    297 
    298 	return vfs_getopsbyname(name);
    299 }
    300 
    301 int
    302 rump_vfs_getmp(const char *path, struct mount **mpp)
    303 {
    304 	struct vnode *vp;
    305 	int rv;
    306 
    307 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    308 		return rv;
    309 
    310 	*mpp = vp->v_mount;
    311 	vrele(vp);
    312 	return 0;
    313 }
    314 
    315 struct vattr*
    316 rump_vattr_init(void)
    317 {
    318 	struct vattr *vap;
    319 
    320 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    321 	vattr_null(vap);
    322 
    323 	return vap;
    324 }
    325 
    326 void
    327 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    328 {
    329 
    330 	vap->va_type = vt;
    331 }
    332 
    333 void
    334 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    335 {
    336 
    337 	vap->va_mode = mode;
    338 }
    339 
    340 void
    341 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    342 {
    343 
    344 	vap->va_rdev = dev;
    345 }
    346 
    347 void
    348 rump_vattr_free(struct vattr *vap)
    349 {
    350 
    351 	kmem_free(vap, sizeof(*vap));
    352 }
    353 
    354 void
    355 rump_vp_incref(struct vnode *vp)
    356 {
    357 
    358 	vref(vp);
    359 }
    360 
    361 int
    362 rump_vp_getref(struct vnode *vp)
    363 {
    364 
    365 	return vp->v_usecount;
    366 }
    367 
    368 void
    369 rump_vp_rele(struct vnode *vp)
    370 {
    371 
    372 	vrele(vp);
    373 }
    374 
    375 void
    376 rump_vp_interlock(struct vnode *vp)
    377 {
    378 
    379 	mutex_enter(&vp->v_interlock);
    380 }
    381 
    382 int
    383 rump_vfs_unmount(struct mount *mp, int mntflags)
    384 {
    385 
    386 	return VFS_UNMOUNT(mp, mntflags);
    387 }
    388 
    389 int
    390 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    391 {
    392 	int rv;
    393 
    394 	rv = VFS_ROOT(mp, vpp);
    395 	if (rv)
    396 		return rv;
    397 
    398 	if (!lock)
    399 		VOP_UNLOCK(*vpp);
    400 
    401 	return 0;
    402 }
    403 
    404 int
    405 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    406 {
    407 
    408 	return VFS_STATVFS(mp, sbp);
    409 }
    410 
    411 int
    412 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    413 {
    414 
    415 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    416 }
    417 
    418 int
    419 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    420 {
    421 
    422 	return VFS_FHTOVP(mp, fid, vpp);
    423 }
    424 
    425 int
    426 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    427 {
    428 
    429 	return VFS_VPTOFH(vp, fid, fidsize);
    430 }
    431 
    432 int
    433 rump_vfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
    434 	int attrnamespace, const char *attrname)
    435 {
    436 
    437 	return VFS_EXTATTRCTL(mp, cmd, vp, attrnamespace, attrname);
    438 }
    439 
    440 /*ARGSUSED*/
    441 void
    442 rump_vfs_syncwait(struct mount *mp)
    443 {
    444 	int n;
    445 
    446 	n = buf_syncwait();
    447 	if (n)
    448 		printf("syncwait: unsynced buffers: %d\n", n);
    449 }
    450 
    451 /*
    452  * Dump info about mount point.  No locking.
    453  */
    454 void
    455 rump_vfs_mount_print(const char *path, int full)
    456 {
    457 #ifdef DEBUGPRINT
    458 	struct vnode *mvp;
    459 	struct vnode *vp;
    460 	int error;
    461 
    462 	rumpuser_dprintf("\n==== dumping mountpoint at ``%s'' ====\n\n", path);
    463 	if ((error = namei_simple_user(path, NSM_FOLLOW_NOEMULROOT, &mvp))!=0) {
    464 		rumpuser_dprintf("==== lookup error %d ====\n\n", error);
    465 		return;
    466 	}
    467 	vfs_mount_print(mvp->v_mount, full, (void *)rumpuser_dprintf);
    468 	if (full) {
    469 		rumpuser_dprintf("\n== dumping vnodes ==\n\n");
    470 		TAILQ_FOREACH(vp, &mvp->v_mount->mnt_vnodelist, v_mntvnodes) {
    471 			vfs_vnode_print(vp, full, (void *)rumpuser_dprintf);
    472 		}
    473 	}
    474 	vrele(mvp);
    475 	rumpuser_dprintf("\n==== done ====\n\n");
    476 #else
    477 	rumpuser_dprintf("mount dump not supported without DEBUGPRINT\n");
    478 #endif
    479 }
    480 
    481 void
    482 rump_biodone(void *arg, size_t count, int error)
    483 {
    484 	struct buf *bp = arg;
    485 
    486 	bp->b_resid = bp->b_bcount - count;
    487 	KASSERT(bp->b_resid >= 0);
    488 	bp->b_error = error;
    489 
    490 	biodone(bp);
    491 }
    492 
    493 void
    494 rump_vfs_drainbufs(int npages)
    495 {
    496 
    497 	mutex_enter(&bufcache_lock);
    498 	buf_drain(npages);
    499 	mutex_exit(&bufcache_lock);
    500 }
    501