Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.20.2.2
      1 /*	$NetBSD: rump_vfs.c,v 1.20.2.2 2009/05/04 08:14:31 yamt 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");
     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 fakeblk {
     58 	char path[MAXPATHLEN];
     59 	LIST_ENTRY(fakeblk) entries;
     60 };
     61 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
     62 
     63 static struct cwdinfo rump_cwdi;
     64 
     65 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
     66 
     67 static void
     68 pvfs_init(struct proc *p)
     69 {
     70 
     71 	p->p_cwdi = cwdinit();
     72 }
     73 
     74 static void
     75 pvfs_rele(struct proc *p)
     76 {
     77 
     78 	cwdfree(p->p_cwdi);
     79 }
     80 
     81 void
     82 rump_vfs_init(void)
     83 {
     84 	char buf[64];
     85 	int error;
     86 
     87 	dovfsusermount = 1;
     88 
     89 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     90 		desiredvnodes = strtoul(buf, NULL, 10);
     91 	} else {
     92 		desiredvnodes = 1<<16;
     93 	}
     94 
     95 	rumpblk_init();
     96 
     97 	cache_cpu_init(&rump_cpu);
     98 	vfsinit();
     99 	bufinit();
    100 	wapbl_init();
    101 	cwd_sys_init();
    102 	lf_init();
    103 
    104 	rumpuser_bioinit(rump_biodone);
    105 	rumpfs_init();
    106 
    107 	rump_proc_vfs_init = pvfs_init;
    108 	rump_proc_vfs_release = pvfs_rele;
    109 
    110 	/* bootstrap cwdi */
    111 	rw_init(&rump_cwdi.cwdi_lock);
    112 	rump_cwdi.cwdi_cdir = rootvnode;
    113 	vref(rump_cwdi.cwdi_cdir);
    114 	proc0.p_cwdi = &rump_cwdi;
    115 	proc0.p_cwdi = cwdinit();
    116 
    117 	if (rump_threads) {
    118 		int rv;
    119 
    120 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    121 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    122 			panic("syncer thread create failed: %d", rv);
    123 	} else {
    124 		syncdelay = 0;
    125 	}
    126 }
    127 
    128 struct mount *
    129 rump_mnt_init(struct vfsops *vfsops, int mntflags)
    130 {
    131 	struct mount *mp;
    132 
    133 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    134 
    135 	mp->mnt_op = vfsops;
    136 	mp->mnt_flag = mntflags;
    137 	TAILQ_INIT(&mp->mnt_vnodelist);
    138 	rw_init(&mp->mnt_unmounting);
    139 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    140 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    141 	mp->mnt_refcnt = 1;
    142 	mp->mnt_vnodecovered = rootvnode;
    143 
    144 	mount_initspecific(mp);
    145 
    146 	return mp;
    147 }
    148 
    149 int
    150 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    151 {
    152 	struct vnode *rvp;
    153 	int rv;
    154 
    155 	rv = VFS_MOUNT(mp, path, data, dlen);
    156 	if (rv)
    157 		return rv;
    158 
    159 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    160 	rv = VFS_START(mp, 0);
    161 	if (rv) {
    162 		VFS_UNMOUNT(mp, MNT_FORCE);
    163 		return rv;
    164 	}
    165 
    166 	/*
    167 	 * XXX: set a root for lwp0.  This is strictly not correct,
    168 	 * but makes things work for single fs case without having
    169 	 * to manually call rump_rcvp_set().
    170 	 */
    171 	VFS_ROOT(mp, &rvp);
    172 	rump_rcvp_lwpset(rvp, rvp, &lwp0);
    173 	vput(rvp);
    174 
    175 	return rv;
    176 }
    177 
    178 void
    179 rump_mnt_destroy(struct mount *mp)
    180 {
    181 
    182 	/* See rcvp XXX above */
    183 	rump_cwdi.cwdi_rdir = NULL;
    184 	vref(rootvnode);
    185 	rump_cwdi.cwdi_cdir = rootvnode;
    186 
    187 	mount_finispecific(mp);
    188 	kmem_free(mp, sizeof(*mp));
    189 }
    190 
    191 struct componentname *
    192 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    193 	kauth_cred_t creds, struct lwp *l)
    194 {
    195 	struct componentname *cnp;
    196 	const char *cp = NULL;
    197 
    198 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    199 
    200 	cnp->cn_nameiop = nameiop;
    201 	cnp->cn_flags = flags | HASBUF;
    202 
    203 	cnp->cn_pnbuf = PNBUF_GET();
    204 	strcpy(cnp->cn_pnbuf, name);
    205 	cnp->cn_nameptr = cnp->cn_pnbuf;
    206 	cnp->cn_namelen = namelen;
    207 	cnp->cn_hash = namei_hash(name, &cp);
    208 
    209 	cnp->cn_cred = creds;
    210 
    211 	return cnp;
    212 }
    213 
    214 void
    215 rump_freecn(struct componentname *cnp, int flags)
    216 {
    217 
    218 	if (flags & RUMPCN_FREECRED)
    219 		rump_cred_put(cnp->cn_cred);
    220 
    221 	if ((flags & RUMPCN_HASNTBUF) == 0) {
    222 		if (cnp->cn_flags & SAVENAME) {
    223 			if (flags & RUMPCN_ISLOOKUP ||cnp->cn_flags & SAVESTART)
    224 				PNBUF_PUT(cnp->cn_pnbuf);
    225 		} else {
    226 			PNBUF_PUT(cnp->cn_pnbuf);
    227 		}
    228 	}
    229 	kmem_free(cnp, sizeof(*cnp));
    230 }
    231 
    232 /* hey baby, what's your namei? */
    233 int
    234 rump_namei(uint32_t op, uint32_t flags, const char *namep,
    235 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    236 {
    237 	struct nameidata nd;
    238 	int rv;
    239 
    240 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    241 	rv = namei(&nd);
    242 	if (rv)
    243 		return rv;
    244 
    245 	if (dvpp) {
    246 		KASSERT(flags & LOCKPARENT);
    247 		*dvpp = nd.ni_dvp;
    248 	} else {
    249 		KASSERT((flags & LOCKPARENT) == 0);
    250 	}
    251 
    252 	if (vpp) {
    253 		*vpp = nd.ni_vp;
    254 	} else {
    255 		if (nd.ni_vp) {
    256 			if (flags & LOCKLEAF)
    257 				vput(nd.ni_vp);
    258 			else
    259 				vrele(nd.ni_vp);
    260 		}
    261 	}
    262 
    263 	if (cnpp) {
    264 		struct componentname *cnp;
    265 
    266 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    267 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    268 		*cnpp = cnp;
    269 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    270 		panic("%s: pathbuf mismatch", __func__);
    271 	}
    272 
    273 	return rv;
    274 }
    275 
    276 static struct fakeblk *
    277 _rump_fakeblk_find(const char *path)
    278 {
    279 	char buf[MAXPATHLEN];
    280 	struct fakeblk *fblk;
    281 	int error;
    282 
    283 	if (rumpuser_realpath(path, buf, &error) == NULL)
    284 		return NULL;
    285 
    286 	LIST_FOREACH(fblk, &fakeblks, entries)
    287 		if (strcmp(fblk->path, buf) == 0)
    288 			return fblk;
    289 
    290 	return NULL;
    291 }
    292 
    293 int
    294 rump_fakeblk_register(const char *path)
    295 {
    296 	char buf[MAXPATHLEN];
    297 	struct fakeblk *fblk;
    298 	int error;
    299 
    300 	if (_rump_fakeblk_find(path))
    301 		return EEXIST;
    302 
    303 	if (rumpuser_realpath(path, buf, &error) == NULL)
    304 		return error;
    305 
    306 	fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
    307 	if (fblk == NULL)
    308 		return ENOMEM;
    309 
    310 	strlcpy(fblk->path, buf, MAXPATHLEN);
    311 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    312 
    313 	return 0;
    314 }
    315 
    316 int
    317 rump_fakeblk_find(const char *path)
    318 {
    319 
    320 	return _rump_fakeblk_find(path) != NULL;
    321 }
    322 
    323 void
    324 rump_fakeblk_deregister(const char *path)
    325 {
    326 	struct fakeblk *fblk;
    327 
    328 	fblk = _rump_fakeblk_find(path);
    329 	if (fblk == NULL)
    330 		return;
    331 
    332 	LIST_REMOVE(fblk, entries);
    333 	kmem_free(fblk, sizeof(*fblk));
    334 }
    335 
    336 void
    337 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    338 {
    339 
    340 	*vtype = vp->v_type;
    341 	*vsize = vp->v_size;
    342 	if (vp->v_specnode)
    343 		*vdev = vp->v_rdev;
    344 	else
    345 		*vdev = 0;
    346 }
    347 
    348 struct vfsops *
    349 rump_vfslist_iterate(struct vfsops *ops)
    350 {
    351 
    352 	if (ops == NULL)
    353 		return LIST_FIRST(&vfs_list);
    354 	else
    355 		return LIST_NEXT(ops, vfs_list);
    356 }
    357 
    358 struct vfsops *
    359 rump_vfs_getopsbyname(const char *name)
    360 {
    361 
    362 	return vfs_getopsbyname(name);
    363 }
    364 
    365 int
    366 rump_vfs_getmp(const char *path, struct mount **mpp)
    367 {
    368 	struct nameidata nd;
    369 	struct vnode *vp;
    370 	int rv;
    371 
    372 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path);
    373 	if ((rv = namei(&nd)) != 0)
    374 		return rv;
    375 	vp = nd.ni_vp;
    376 
    377 	*mpp = vp->v_mount;
    378 	vrele(vp);
    379 	return 0;
    380 }
    381 
    382 struct vattr*
    383 rump_vattr_init(void)
    384 {
    385 	struct vattr *vap;
    386 
    387 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    388 	vattr_null(vap);
    389 
    390 	return vap;
    391 }
    392 
    393 void
    394 rump_vattr_settype(struct vattr *vap, enum vtype vt)
    395 {
    396 
    397 	vap->va_type = vt;
    398 }
    399 
    400 void
    401 rump_vattr_setmode(struct vattr *vap, mode_t mode)
    402 {
    403 
    404 	vap->va_mode = mode;
    405 }
    406 
    407 void
    408 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    409 {
    410 
    411 	vap->va_rdev = dev;
    412 }
    413 
    414 void
    415 rump_vattr_free(struct vattr *vap)
    416 {
    417 
    418 	kmem_free(vap, sizeof(*vap));
    419 }
    420 
    421 void
    422 rump_vp_incref(struct vnode *vp)
    423 {
    424 
    425 	mutex_enter(&vp->v_interlock);
    426 	++vp->v_usecount;
    427 	mutex_exit(&vp->v_interlock);
    428 }
    429 
    430 int
    431 rump_vp_getref(struct vnode *vp)
    432 {
    433 
    434 	return vp->v_usecount;
    435 }
    436 
    437 void
    438 rump_vp_decref(struct vnode *vp)
    439 {
    440 
    441 	mutex_enter(&vp->v_interlock);
    442 	--vp->v_usecount;
    443 	mutex_exit(&vp->v_interlock);
    444 }
    445 
    446 /*
    447  * Really really recycle with a cherry on top.  We should be
    448  * extra-sure we can do this.  For example with p2k there is
    449  * no problem, since puffs in the kernel takes care of refcounting
    450  * for us.
    451  */
    452 void
    453 rump_vp_recycle_nokidding(struct vnode *vp)
    454 {
    455 
    456 	mutex_enter(&vp->v_interlock);
    457 	vp->v_usecount = 1;
    458 	/*
    459 	 * XXX: NFS holds a reference to the root vnode, so don't clean
    460 	 * it out.  This is very wrong, but fixing it properly would
    461 	 * take too much effort for now
    462 	 */
    463 	if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
    464 		mutex_exit(&vp->v_interlock);
    465 		return;
    466 	}
    467 	vclean(vp, DOCLOSE);
    468 	vrelel(vp, 0);
    469 }
    470 
    471 void
    472 rump_vp_rele(struct vnode *vp)
    473 {
    474 
    475 	vrele(vp);
    476 }
    477 
    478 void
    479 rump_vp_interlock(struct vnode *vp)
    480 {
    481 
    482 	mutex_enter(&vp->v_interlock);
    483 }
    484 
    485 int
    486 rump_vfs_unmount(struct mount *mp, int mntflags)
    487 {
    488 #if 0
    489 	struct evcnt *ev;
    490 
    491 	printf("event counters:\n");
    492 	TAILQ_FOREACH(ev, &allevents, ev_list)
    493 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    494 #endif
    495 
    496 	return VFS_UNMOUNT(mp, mntflags);
    497 }
    498 
    499 int
    500 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    501 {
    502 	int rv;
    503 
    504 	rv = VFS_ROOT(mp, vpp);
    505 	if (rv)
    506 		return rv;
    507 
    508 	if (!lock)
    509 		VOP_UNLOCK(*vpp, 0);
    510 
    511 	return 0;
    512 }
    513 
    514 int
    515 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    516 {
    517 
    518 	return VFS_STATVFS(mp, sbp);
    519 }
    520 
    521 int
    522 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    523 {
    524 
    525 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    526 }
    527 
    528 int
    529 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    530 {
    531 
    532 	return VFS_FHTOVP(mp, fid, vpp);
    533 }
    534 
    535 int
    536 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    537 {
    538 
    539 	return VFS_VPTOFH(vp, fid, fidsize);
    540 }
    541 
    542 /*ARGSUSED*/
    543 void
    544 rump_vfs_syncwait(struct mount *mp)
    545 {
    546 	int n;
    547 
    548 	n = buf_syncwait();
    549 	if (n)
    550 		printf("syncwait: unsynced buffers: %d\n", n);
    551 }
    552 
    553 void
    554 rump_biodone(void *arg, size_t count, int error)
    555 {
    556 	struct buf *bp = arg;
    557 
    558 	bp->b_resid = bp->b_bcount - count;
    559 	KASSERT(bp->b_resid >= 0);
    560 	bp->b_error = error;
    561 
    562 	biodone(bp);
    563 }
    564 
    565 static void
    566 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    567 {
    568 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    569 
    570 	KASSERT(cvp);
    571 
    572 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    573 	if (cwdi->cwdi_rdir)
    574 		vrele(cwdi->cwdi_rdir);
    575 	if (rvp)
    576 		vref(rvp);
    577 	cwdi->cwdi_rdir = rvp;
    578 
    579 	vrele(cwdi->cwdi_cdir);
    580 	vref(cvp);
    581 	cwdi->cwdi_cdir = cvp;
    582 	rw_exit(&cwdi->cwdi_lock);
    583 }
    584 
    585 void
    586 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    587 {
    588 
    589 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    590 }
    591 
    592 struct vnode *
    593 rump_cdir_get(void)
    594 {
    595 	struct vnode *vp;
    596 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    597 
    598 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    599 	vp = cwdi->cwdi_cdir;
    600 	rw_exit(&cwdi->cwdi_lock);
    601 	vref(vp);
    602 
    603 	return vp;
    604 }
    605