Home | History | Annotate | Line # | Download | only in rumpvfs
rumpfs.c revision 1.6.6.2
      1 /*	$NetBSD: rumpfs.c,v 1.6.6.2 2009/07/23 23:32:55 jym Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by Google Summer of Code.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: rumpfs.c,v 1.6.6.2 2009/07/23 23:32:55 jym Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/mount.h>
     35 #include <sys/vnode.h>
     36 #include <sys/errno.h>
     37 #include <sys/kauth.h>
     38 #include <sys/lock.h>
     39 #include <sys/lockf.h>
     40 #include <sys/stat.h>
     41 #include <sys/namei.h>
     42 #include <sys/queue.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/syscallargs.h>
     45 #include <sys/atomic.h>
     46 
     47 #include <miscfs/fifofs/fifo.h>
     48 #include <miscfs/specfs/specdev.h>
     49 #include <miscfs/genfs/genfs.h>
     50 
     51 #include <rump/rumpuser.h>
     52 
     53 #include "rump_private.h"
     54 #include "rump_vfs_private.h"
     55 
     56 static int rump_vop_lookup(void *);
     57 static int rump_vop_getattr(void *);
     58 static int rump_vop_mkdir(void *);
     59 static int rump_vop_mknod(void *);
     60 static int rump_vop_inactive(void *);
     61 static int rump_vop_reclaim(void *);
     62 static int rump_vop_success(void *);
     63 static int rump_vop_spec(void *);
     64 
     65 int (**fifo_vnodeop_p)(void *);
     66 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
     67 	{ &vop_default_desc, vn_default_error },
     68 	{ NULL, NULL }
     69 };
     70 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
     71 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
     72 
     73 int (**rump_vnodeop_p)(void *);
     74 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
     75 	{ &vop_default_desc, vn_default_error },
     76 	{ &vop_lookup_desc, rump_vop_lookup },
     77 	{ &vop_getattr_desc, rump_vop_getattr },
     78 	{ &vop_mkdir_desc, rump_vop_mkdir },
     79 	{ &vop_mknod_desc, rump_vop_mknod },
     80 	{ &vop_access_desc, rump_vop_success },
     81 	{ &vop_putpages_desc, genfs_null_putpages },
     82 	{ &vop_fsync_desc, rump_vop_success },
     83 	{ &vop_lock_desc, genfs_lock },
     84 	{ &vop_unlock_desc, genfs_unlock },
     85 	{ &vop_inactive_desc, rump_vop_inactive },
     86 	{ &vop_reclaim_desc, rump_vop_reclaim },
     87 	{ NULL, NULL }
     88 };
     89 const struct vnodeopv_desc rump_vnodeop_opv_desc =
     90 	{ &rump_vnodeop_p, rump_vnodeop_entries };
     91 
     92 int (**rump_specop_p)(void *);
     93 const struct vnodeopv_entry_desc rump_specop_entries[] = {
     94 	{ &vop_default_desc, rump_vop_spec },
     95 	{ NULL, NULL }
     96 };
     97 const struct vnodeopv_desc rump_specop_opv_desc =
     98 	{ &rump_specop_p, rump_specop_entries };
     99 
    100 const struct vnodeopv_desc * const rump_opv_descs[] = {
    101 	&rump_vnodeop_opv_desc,
    102 	&rump_specop_opv_desc,
    103 	NULL
    104 };
    105 
    106 static struct mount rump_mnt;
    107 static int lastino = 1;
    108 static kmutex_t reclock;
    109 
    110 struct rumpfs_dent {
    111 	char *rd_name;
    112 	struct rumpfs_node *rd_node;
    113 
    114 	LIST_ENTRY(rumpfs_dent) rd_entries;
    115 };
    116 
    117 struct rumpfs_node {
    118 	struct vattr rn_va;
    119 	struct vnode *rn_vp;
    120 
    121 	/* only for VDIR */
    122 	LIST_HEAD(, rumpfs_dent) rn_dir;
    123 };
    124 
    125 static struct rumpfs_node *
    126 makeprivate(enum vtype vt, dev_t rdev, voff_t size)
    127 {
    128 	struct rumpfs_node *rn;
    129 	struct vattr *va;
    130 	struct timespec ts;
    131 
    132 	rn = kmem_alloc(sizeof(*rn), KM_SLEEP);
    133 	LIST_INIT(&rn->rn_dir);
    134 	nanotime(&ts);
    135 
    136 	va = &rn->rn_va;
    137 	va->va_type = vt;
    138 	va->va_mode = 0755;
    139 	if (vt == VDIR)
    140 		va->va_nlink = 2;
    141 	else
    142 		va->va_nlink = 1;
    143 	va->va_uid = 0;
    144 	va->va_gid = 0;
    145 	va->va_fsid =
    146 	va->va_fileid = atomic_inc_uint_nv(&lastino);
    147 	va->va_size = size;
    148 	va->va_blocksize = 512;
    149 	va->va_atime = ts;
    150 	va->va_mtime = ts;
    151 	va->va_ctime = ts;
    152 	va->va_birthtime = ts;
    153 	va->va_gen = 0;
    154 	va->va_flags = 0;
    155 	va->va_rdev = rdev;
    156 	va->va_bytes = 512;
    157 	va->va_filerev = 0;
    158 	va->va_vaflags = 0;
    159 
    160 	return rn;
    161 }
    162 
    163 static int
    164 rump_makevnode(const char *path, voff_t size, enum vtype vt, dev_t rdev,
    165 	struct vnode **vpp, bool regrumpblk)
    166 {
    167 	struct vnode *vp;
    168 	struct rumpfs_node *rn;
    169 	int (**vpops)(void *);
    170 	int rv;
    171 
    172 	if (vt == VREG || vt == VCHR || vt == VBLK) {
    173 		vt = VBLK;
    174 		vpops = rump_specop_p;
    175 	} else {
    176 		vpops = rump_vnodeop_p;
    177 	}
    178 	if (vt != VBLK && vt != VDIR)
    179 		panic("rump_makevnode: only VBLK/VDIR vnodes supported");
    180 
    181 	rv = getnewvnode(VT_RUMP, &rump_mnt, vpops, &vp);
    182 	if (rv)
    183 		return rv;
    184 
    185 	vp->v_size = vp->v_writesize = size;
    186 	vp->v_type = vt;
    187 
    188 	if (vp->v_type == VBLK) {
    189 		if (regrumpblk) {
    190 			rv = rumpblk_register(path);
    191 			if (rv == -1)
    192 				panic("rump_makevnode: lazy bum");
    193 			rdev = makedev(RUMPBLK, rv);
    194 			spec_node_init(vp, rdev);
    195 		} else {
    196 			spec_node_init(vp, rdev);
    197 		}
    198 	}
    199 	rn = makeprivate(vp->v_type, rdev, size);
    200 	rn->rn_vp = vp;
    201 	vp->v_data = rn;
    202 
    203 	vn_lock(vp, LK_RETRY | LK_EXCLUSIVE);
    204 	*vpp = vp;
    205 
    206 	return 0;
    207 }
    208 
    209 /*
    210  * Simple lookup for faking lookup of device entry for rump file systems
    211  * and for locating/creating directories.  Yes, this will panic if you
    212  * call it with the wrong arguments.
    213  */
    214 static int
    215 rump_vop_lookup(void *v)
    216 {
    217 	struct vop_lookup_args /* {
    218 		struct vnode *a_dvp;
    219 		struct vnode **a_vpp;
    220 		struct componentname *a_cnp;
    221 	}; */ *ap = v;
    222 	struct componentname *cnp = ap->a_cnp;
    223 	struct vnode *dvp = ap->a_dvp;
    224 	struct vnode **vpp = ap->a_vpp;
    225 	struct vnode *vp;
    226 	struct rumpfs_node *rn = dvp->v_data;
    227 	struct rumpfs_dent *rd;
    228 	uint64_t fsize;
    229 	enum vtype vt;
    230 	int rv, error, ft;
    231 
    232 	/* we handle only some "non-special" cases */
    233 	if (!(((cnp->cn_flags & ISLASTCN) == 0)
    234 	    || (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == CREATE)))
    235 		return EOPNOTSUPP;
    236 	if (!((cnp->cn_flags & ISDOTDOT) == 0))
    237 		return EOPNOTSUPP;
    238 	if (!(cnp->cn_namelen != 0 && cnp->cn_pnbuf[0] != '.'))
    239 		return EOPNOTSUPP;
    240 
    241 	/* check if we are returning a faked block device */
    242 	if (dvp == rootvnode && cnp->cn_nameiop == LOOKUP) {
    243 		if (rump_fakeblk_find(cnp->cn_pnbuf)) {
    244 			rv = rumpuser_getfileinfo(cnp->cn_pnbuf, &fsize,
    245 			    &ft, &error);
    246 			if (rv)
    247 				return rv;
    248 			switch (ft) {
    249 			case RUMPUSER_FT_DIR:
    250 				vt = VDIR;
    251 				break;
    252 			case RUMPUSER_FT_REG:
    253 				vt = VREG;
    254 				break;
    255 			case RUMPUSER_FT_BLK:
    256 				vt = VBLK;
    257 				break;
    258 			case RUMPUSER_FT_CHR:
    259 				vt = VCHR;
    260 				break;
    261 			default:
    262 				vt = VBAD;
    263 				break;
    264 			}
    265 			error = rump_makevnode(cnp->cn_pnbuf, fsize, vt, -1,
    266 			    vpp, true);
    267 			if (error)
    268 				return error;
    269 			cnp->cn_consume = strlen(cnp->cn_nameptr
    270 			    + cnp->cn_namelen);
    271 			cnp->cn_flags &= ~REQUIREDIR;
    272 
    273 			return 0;
    274 		}
    275 	}
    276 
    277 	LIST_FOREACH(rd, &rn->rn_dir, rd_entries) {
    278 		if (strncmp(rd->rd_name, cnp->cn_nameptr,
    279 		    cnp->cn_namelen) == 0)
    280 			break;
    281 	}
    282 
    283 	if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
    284 		return ENOENT;
    285 
    286 	if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
    287 		cnp->cn_flags |= SAVENAME;
    288 		return EJUSTRETURN;
    289 	}
    290 	KASSERT(rd);
    291 
    292  retry:
    293 	mutex_enter(&reclock);
    294 	if ((vp = rd->rd_node->rn_vp)) {
    295 		mutex_enter(&vp->v_interlock);
    296 		mutex_exit(&reclock);
    297 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    298 			goto retry;
    299 		*vpp = vp;
    300 	} else {
    301 		rv = rump_makevnode(cnp->cn_nameptr, DEV_BSIZE, VDIR, -1,
    302 		    vpp, false);
    303 		if (rv)
    304 			return rv;
    305 	}
    306 
    307 	return 0;
    308 }
    309 
    310 static int
    311 rump_vop_getattr(void *v)
    312 {
    313 	struct vop_getattr_args /* {
    314 		struct vnode *a_vp;
    315 		struct vattr *a_vap;
    316 		kauth_cred_t a_cred;
    317 	} */ *ap = v;
    318 	struct rumpfs_node *rn = ap->a_vp->v_data;
    319 
    320 	memcpy(ap->a_vap, &rn->rn_va, sizeof(struct vattr));
    321 	return 0;
    322 }
    323 
    324 static int
    325 rump_vop_mkdir(void *v)
    326 {
    327 	struct vop_mkdir_args /* {
    328 		struct vnode *a_dvp;
    329 		struct vnode **a_vpp;
    330 		struct componentname *a_cnp;
    331 		struct vattr *a_vap;
    332 	}; */ *ap = v;
    333 	struct vnode *dvp = ap->a_dvp;
    334 	struct vnode **vpp = ap->a_vpp;
    335 	struct componentname *cnp = ap->a_cnp;
    336 	struct rumpfs_node *rnd = dvp->v_data;
    337 	struct rumpfs_dent *rdent;
    338 	int rv = 0;
    339 
    340 	if ((rv = rump_makevnode(cnp->cn_nameptr, DEV_BSIZE, VDIR, -1,
    341 	    vpp, false)) != 0)
    342 		goto out;
    343 
    344 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    345 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    346 	rdent->rd_node = (*vpp)->v_data;
    347 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    348 
    349 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    350 
    351  out:
    352 	vput(dvp);
    353 	return rv;
    354 }
    355 
    356 static int
    357 rump_vop_mknod(void *v)
    358 {
    359 	struct vop_mknod_args /* {
    360 		struct vnode *a_dvp;
    361 		struct vnode **a_vpp;
    362 		struct componentname *a_cnp;
    363 		struct vattr *a_vap;
    364 	}; */ *ap = v;
    365 	struct vnode *dvp = ap->a_dvp;
    366 	struct vnode **vpp = ap->a_vpp;
    367 	struct componentname *cnp = ap->a_cnp;
    368 	struct vattr *va = ap->a_vap;
    369 	struct rumpfs_node *rnd = dvp->v_data;
    370 	struct rumpfs_dent *rdent;
    371 	int rv;
    372 
    373 	if ((rv = rump_makevnode(cnp->cn_nameptr, DEV_BSIZE, va->va_type,
    374 	    va->va_rdev, vpp, false)) != 0)
    375 		goto out;
    376 
    377 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    378 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    379 	rdent->rd_node = (*vpp)->v_data;
    380 	rdent->rd_node->rn_va.va_rdev = va->va_rdev;
    381 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    382 
    383 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    384 
    385  out:
    386 	vput(dvp);
    387 	return rv;
    388 }
    389 
    390 static int
    391 rump_vop_success(void *v)
    392 {
    393 
    394 	return 0;
    395 }
    396 
    397 static int
    398 rump_vop_inactive(void *v)
    399 {
    400 	struct vop_inactive_args *ap = v;
    401 
    402 	VOP_UNLOCK(ap->a_vp, 0);
    403 	return 0;
    404 }
    405 
    406 static int
    407 rump_vop_reclaim(void *v)
    408 {
    409 	struct vop_reclaim_args /* {
    410 		struct vnode *a_vp;
    411 	} */ *ap = v;
    412 	struct vnode *vp = ap->a_vp;
    413 	struct rumpfs_node *rn = vp->v_data;
    414 
    415 	mutex_enter(&reclock);
    416 	rn->rn_vp = NULL;
    417 	mutex_exit(&reclock);
    418 	vp->v_data = NULL;
    419 
    420 	return 0;
    421 }
    422 
    423 static int
    424 rump_vop_spec(void *v)
    425 {
    426 	struct vop_generic_args *ap = v;
    427 	int (**opvec)(void *);
    428 
    429 	switch (ap->a_desc->vdesc_offset) {
    430 	case VOP_ACCESS_DESCOFFSET:
    431 	case VOP_GETATTR_DESCOFFSET:
    432 		opvec = rump_vnodeop_p;
    433 		break;
    434 	default:
    435 		opvec = spec_vnodeop_p;
    436 		break;
    437 	}
    438 
    439 	return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
    440 }
    441 
    442 void
    443 rumpfs_init(void)
    444 {
    445 	int rv;
    446 
    447 	mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
    448 
    449 	/* XXX: init properly instead of this crap */
    450 	rump_mnt.mnt_refcnt = 1;
    451 	rump_mnt.mnt_flag = MNT_ROOTFS;
    452 	rw_init(&rump_mnt.mnt_unmounting);
    453 	TAILQ_INIT(&rump_mnt.mnt_vnodelist);
    454 
    455 	vfs_opv_init(rump_opv_descs);
    456 	rv = rump_makevnode("/", 0, VDIR, -1, &rootvnode, false);
    457 	if (rv)
    458 		panic("could not create root vnode: %d", rv);
    459 	rootvnode->v_vflag |= VV_ROOT;
    460 	VOP_UNLOCK(rootvnode, 0);
    461 }
    462