Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.60
      1 /*	$NetBSD: rump_vfs.c,v 1.60 2010/11/21 16:19:19 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.60 2010/11/21 16:19:19 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 
     86 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     87 		desiredvnodes = strtoul(buf, NULL, 10);
     88 	} else {
     89 		desiredvnodes = 1<<10;
     90 	}
     91 
     92 	rumpblk_init();
     93 
     94 	for (i = 0; i < ncpu; i++) {
     95 		struct cpu_info *ci = cpu_lookup(i);
     96 		cache_cpu_init(ci);
     97 	}
     98 
     99 	/* make number of bufpages 5% of total memory limit */
    100 	if (rump_physmemlimit != RUMPMEM_UNLIMITED) {
    101 		extern u_int bufpages;
    102 		bufpages = rump_physmemlimit / (20 * PAGE_SIZE);
    103 	}
    104 
    105 	vfsinit();
    106 	bufinit();
    107 	cwd_sys_init();
    108 	lf_init();
    109 	spec_init();
    110 	fstrans_init();
    111 
    112 	if (rump_threads) {
    113 		if ((rv = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL,
    114 		    rumpuser_biothread, rump_biodone, NULL, "rmpabio")) != 0)
    115 			panic("syncer thread create failed: %d", rv);
    116 	}
    117 
    118 	root_device = &rump_rootdev;
    119 
    120 	/* bootstrap cwdi (rest done in vfs_mountroot() */
    121 	rw_init(&cwdi0.cwdi_lock);
    122 	proc0.p_cwdi = &cwdi0;
    123 	proc0.p_cwdi = cwdinit();
    124 
    125 	vfs_attach(&rumpfs_vfsops);
    126 	vfs_mountroot();
    127 
    128 	/* "mtree": create /dev */
    129 	do_sys_mkdir("/dev", 0777, UIO_SYSSPACE);
    130 	rump_devnull_init();
    131 
    132 	rump_proc_vfs_init = pvfs_init;
    133 	rump_proc_vfs_release = pvfs_rele;
    134 
    135 	if (rump_threads) {
    136 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    137 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    138 			panic("syncer thread create failed: %d", rv);
    139 	} else {
    140 		syncdelay = 0;
    141 	}
    142 
    143 	/*
    144 	 * On archs where the native kernel ABI is supported, map
    145 	 * host module directory to rump.  This means that kernel
    146 	 * modules from the host will be autoloaded to rump kernels.
    147 	 */
    148 #ifdef _RUMP_NATIVE_ABI
    149 	{
    150 	char *mbase;
    151 
    152 	if (rumpuser_getenv("RUMP_MODULEBASE", buf, sizeof(buf), &error) == 0)
    153 		mbase = buf;
    154 	else
    155 		mbase = module_base;
    156 
    157 	if (strlen(mbase) != 0 && *mbase != '0') {
    158 		rump_etfs_register(module_base, mbase, RUMP_ETFS_DIR_SUBDIRS);
    159 	}
    160 	}
    161 #endif
    162 
    163 	module_init_class(MODULE_CLASS_VFS);
    164 
    165 	rump_vfs_builddevs(devsw_conv0, max_devsw_convs);
    166 
    167 	rump_component_init(RUMP_COMPONENT_VFS);
    168 }
    169 
    170 void
    171 rump_vfs_fini(void)
    172 {
    173 
    174 	vfs_shutdown();
    175 }
    176 
    177 struct componentname *
    178 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    179 	kauth_cred_t creds, struct lwp *l)
    180 {
    181 	struct componentname *cnp;
    182 	const char *cp = NULL;
    183 
    184 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    185 
    186 	cnp->cn_nameiop = nameiop;
    187 	cnp->cn_flags = flags;
    188 
    189 	cnp->cn_pnbuf = PNBUF_GET();
    190 	strcpy(cnp->cn_pnbuf, name);
    191 	cnp->cn_nameptr = cnp->cn_pnbuf;
    192 	cnp->cn_namelen = namelen;
    193 	cnp->cn_hash = namei_hash(name, &cp);
    194 
    195 	cnp->cn_cred = creds;
    196 
    197 	return cnp;
    198 }
    199 
    200 void
    201 rump_freecn(struct componentname *cnp, int flags)
    202 {
    203 
    204 	if (flags & RUMPCN_FREECRED)
    205 		rump_cred_put(cnp->cn_cred);
    206 
    207 	if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
    208 		PNBUF_PUT(cnp->cn_pnbuf);
    209 	kmem_free(cnp, sizeof(*cnp));
    210 }
    211 
    212 int
    213 rump_checksavecn(struct componentname *cnp)
    214 {
    215 
    216 	if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
    217 		return 0;
    218 	} else {
    219 		cnp->cn_flags |= HASBUF;
    220 		return 1;
    221 	}
    222 }
    223 
    224 /* hey baby, what's your namei? */
    225 int
    226 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    227 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    228 {
    229 	struct pathbuf *pb;
    230 	struct nameidata nd;
    231 	int rv;
    232 
    233 	pb = pathbuf_create(namep);
    234 	if (pb == NULL) {
    235 		return ENOMEM;
    236 	}
    237 	NDINIT(&nd, op, flags, pb);
    238 	rv = namei(&nd);
    239 	if (rv) {
    240 		pathbuf_destroy(pb);
    241 		return rv;
    242 	}
    243 
    244 	if (dvpp) {
    245 		KASSERT(flags & LOCKPARENT);
    246 		*dvpp = nd.ni_dvp;
    247 	} else {
    248 		KASSERT((flags & LOCKPARENT) == 0);
    249 	}
    250 
    251 	if (vpp) {
    252 		*vpp = nd.ni_vp;
    253 	} else {
    254 		if (nd.ni_vp) {
    255 			if (flags & LOCKLEAF)
    256 				vput(nd.ni_vp);
    257 			else
    258 				vrele(nd.ni_vp);
    259 		}
    260 	}
    261 
    262 	if (cnpp) {
    263 		struct componentname *cnp;
    264 
    265 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    266 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    267 		*cnpp = cnp;
    268 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    269 		panic("%s: pathbuf mismatch", __func__);
    270 	}
    271 	pathbuf_destroy(pb);
    272 
    273 	return rv;
    274 }
    275 
    276 void
    277 rump_getvninfo(struct vnode *vp, enum vtype *vtype,
    278 	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 	vref(vp);
    364 }
    365 
    366 int
    367 rump_vp_getref(struct vnode *vp)
    368 {
    369 
    370 	return vp->v_usecount;
    371 }
    372 
    373 void
    374 rump_vp_rele(struct vnode *vp)
    375 {
    376 
    377 	vrele(vp);
    378 }
    379 
    380 void
    381 rump_vp_interlock(struct vnode *vp)
    382 {
    383 
    384 	mutex_enter(&vp->v_interlock);
    385 }
    386 
    387 int
    388 rump_vfs_unmount(struct mount *mp, int mntflags)
    389 {
    390 
    391 	return VFS_UNMOUNT(mp, mntflags);
    392 }
    393 
    394 int
    395 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    396 {
    397 	int rv;
    398 
    399 	rv = VFS_ROOT(mp, vpp);
    400 	if (rv)
    401 		return rv;
    402 
    403 	if (!lock)
    404 		VOP_UNLOCK(*vpp);
    405 
    406 	return 0;
    407 }
    408 
    409 int
    410 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    411 {
    412 
    413 	return VFS_STATVFS(mp, sbp);
    414 }
    415 
    416 int
    417 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    418 {
    419 
    420 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    421 }
    422 
    423 int
    424 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    425 {
    426 
    427 	return VFS_FHTOVP(mp, fid, vpp);
    428 }
    429 
    430 int
    431 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    432 {
    433 
    434 	return VFS_VPTOFH(vp, fid, fidsize);
    435 }
    436 
    437 int
    438 rump_vfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
    439 	int attrnamespace, const char *attrname)
    440 {
    441 
    442 	return VFS_EXTATTRCTL(mp, cmd, vp, attrnamespace, attrname);
    443 }
    444 
    445 /*ARGSUSED*/
    446 void
    447 rump_vfs_syncwait(struct mount *mp)
    448 {
    449 	int n;
    450 
    451 	n = buf_syncwait();
    452 	if (n)
    453 		printf("syncwait: unsynced buffers: %d\n", n);
    454 }
    455 
    456 /*
    457  * Dump info about mount point.  No locking.
    458  */
    459 void
    460 rump_vfs_mount_print(const char *path, int full)
    461 {
    462 #ifdef DEBUGPRINT
    463 	struct vnode *mvp;
    464 	struct vnode *vp;
    465 	int error;
    466 
    467 	rumpuser_dprintf("\n==== dumping mountpoint at ``%s'' ====\n\n", path);
    468 	if ((error = namei_simple_user(path, NSM_FOLLOW_NOEMULROOT, &mvp))!=0) {
    469 		rumpuser_dprintf("==== lookup error %d ====\n\n", error);
    470 		return;
    471 	}
    472 	vfs_mount_print(mvp->v_mount, full, (void *)rumpuser_dprintf);
    473 	if (full) {
    474 		rumpuser_dprintf("\n== dumping vnodes ==\n\n");
    475 		TAILQ_FOREACH(vp, &mvp->v_mount->mnt_vnodelist, v_mntvnodes) {
    476 			vfs_vnode_print(vp, full, (void *)rumpuser_dprintf);
    477 		}
    478 	}
    479 	vrele(mvp);
    480 	rumpuser_dprintf("\n==== done ====\n\n");
    481 #else
    482 	rumpuser_dprintf("mount dump not supported without DEBUGPRINT\n");
    483 #endif
    484 }
    485 
    486 void
    487 rump_biodone(void *arg, size_t count, int error)
    488 {
    489 	struct buf *bp = arg;
    490 
    491 	bp->b_resid = bp->b_bcount - count;
    492 	KASSERT(bp->b_resid >= 0);
    493 	bp->b_error = error;
    494 
    495 	biodone(bp);
    496 }
    497 
    498 void
    499 rump_vfs_drainbufs(int npages)
    500 {
    501 
    502 	mutex_enter(&bufcache_lock);
    503 	buf_drain(npages);
    504 	mutex_exit(&bufcache_lock);
    505 }
    506