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