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