Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.21
      1 /*	$NetBSD: puffs_vfsops.c,v 1.21 2007/01/09 23:10:23 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  * The Google SoC project was mentored by Bill Studenmund.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.21 2007/01/09 23:10:23 pooka Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/mount.h>
     40 #include <sys/malloc.h>
     41 #include <sys/extattr.h>
     42 #include <sys/queue.h>
     43 #include <sys/vnode.h>
     44 #include <sys/dirent.h>
     45 #include <sys/kauth.h>
     46 
     47 #include <lib/libkern/libkern.h>
     48 
     49 #include <fs/puffs/puffs_msgif.h>
     50 #include <fs/puffs/puffs_sys.h>
     51 
     52 VFS_PROTOS(puffs);
     53 
     54 MALLOC_DEFINE(M_PUFFS, "puffs", "pass-to-userspace file system structures");
     55 
     56 int
     57 puffs_mount(struct mount *mp, const char *path, void *data,
     58 	    struct nameidata *ndp, struct lwp *l)
     59 {
     60 	struct puffs_mount *pmp;
     61 	struct puffs_args *args;
     62 	char namebuf[PUFFSNAMESIZE+sizeof(PUFFS_NAMEPREFIX)+1]; /* spooky */
     63 	int error = 0;
     64 
     65 	if (mp->mnt_flag & MNT_GETARGS) {
     66 		pmp = MPTOPUFFSMP(mp);
     67 		return copyout(&pmp->pmp_args, data, sizeof(struct puffs_args));
     68 	}
     69 
     70 	/* update is not supported currently */
     71 	if (mp->mnt_flag & MNT_UPDATE)
     72 		return EOPNOTSUPP;
     73 
     74 	/*
     75 	 * We need the file system name
     76 	 */
     77 	if (!data)
     78 		return EINVAL;
     79 
     80 	MALLOC(args, struct puffs_args *, sizeof(struct puffs_args),
     81 	    M_PUFFS, M_WAITOK);
     82 
     83 	error = copyin(data, args, sizeof(struct puffs_args));
     84 	if (error)
     85 		goto out;
     86 
     87 	/* devel phase */
     88 	if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
     89 		printf("puffs_mount: development version mismatch\n");
     90 		error = EINVAL;
     91 		goto out;
     92 	}
     93 
     94 	/* nuke spy bits */
     95 	args->pa_flags &= PUFFS_KFLAG_MASK;
     96 
     97 	/* build real name */
     98 	(void)strlcpy(namebuf, PUFFS_NAMEPREFIX, sizeof(namebuf));
     99 	(void)strlcat(namebuf, args->pa_name, sizeof(namebuf));
    100 
    101 	/* inform user server if it got the max request size it wanted */
    102 	if (args->pa_maxreqlen == 0 || args->pa_maxreqlen > PUFFS_REQ_MAXSIZE)
    103 		args->pa_maxreqlen = PUFFS_REQ_MAXSIZE;
    104 	else if (args->pa_maxreqlen < PUFFS_REQSTRUCT_MAX)
    105 		args->pa_maxreqlen = PUFFS_REQSTRUCT_MAX;
    106 	(void)strlcpy(args->pa_name, namebuf, sizeof(args->pa_name));
    107 
    108 	error = copyout(args, data, sizeof(struct puffs_args));
    109 	if (error)
    110 		goto out;
    111 
    112 	error = set_statvfs_info(path, UIO_USERSPACE, namebuf,
    113 	    UIO_SYSSPACE, mp, l);
    114 	if (error)
    115 		goto out;
    116 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    117 
    118 	MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
    119 	    M_PUFFS, M_WAITOK | M_ZERO);
    120 
    121 	mp->mnt_fs_bshift = DEV_BSHIFT;
    122 	mp->mnt_dev_bshift = DEV_BSHIFT;
    123 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    124 	mp->mnt_data = pmp;
    125 
    126 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    127 	pmp->pmp_nextreq = 0;
    128 	pmp->pmp_mp = mp;
    129 	pmp->pmp_req_maxsize = args->pa_maxreqlen;
    130 	pmp->pmp_args = *args;
    131 
    132 	/*
    133 	 * Inform the fileops processing code that we have a mountpoint.
    134 	 * If it doesn't know about anyone with our pid/fd having the
    135 	 * device open, punt
    136 	 */
    137 	if (puffs_setpmp(l->l_proc->p_pid, args->pa_fd, pmp)) {
    138 		FREE(pmp, M_PUFFS);
    139 		error = ENOENT;
    140 		goto out;
    141 	}
    142 
    143 	simple_lock_init(&pmp->pmp_lock);
    144 	TAILQ_INIT(&pmp->pmp_req_touser);
    145 	TAILQ_INIT(&pmp->pmp_req_replywait);
    146 	TAILQ_INIT(&pmp->pmp_req_sizepark);
    147 
    148 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    149 	    mp, MPTOPUFFSMP(mp)));
    150 
    151 	vfs_getnewfsid(mp);
    152 
    153  out:
    154 	FREE(args, M_PUFFS);
    155 	return error;
    156 }
    157 
    158 /*
    159  * This is called from the first "Hello, I'm alive" ioctl
    160  * from userspace.
    161  */
    162 int
    163 puffs_start2(struct puffs_mount *pmp, struct puffs_startreq *sreq)
    164 {
    165 	struct puffs_node *pn;
    166 	struct mount *mp;
    167 
    168 	mp = PMPTOMP(pmp);
    169 
    170 	simple_lock(&pmp->pmp_lock);
    171 
    172 	/*
    173 	 * if someone has issued a VFS_ROOT() already, fill in the
    174 	 * vnode cookie.
    175 	 */
    176 	pn = NULL;
    177 	if (pmp->pmp_root) {
    178 		pn = VPTOPP(pmp->pmp_root);
    179 		pn->pn_cookie = sreq->psr_cookie;
    180 	}
    181 
    182 	/* We're good to fly */
    183 	pmp->pmp_rootcookie = sreq->psr_cookie;
    184 	pmp->pmp_status = PUFFSTAT_RUNNING;
    185 	simple_unlock(&pmp->pmp_lock);
    186 
    187 	/* do the VFS_STATVFS() we missed out on in sys_mount() */
    188 	copy_statvfs_info(&sreq->psr_sb, mp);
    189 	(void)memcpy(&mp->mnt_stat, &sreq->psr_sb, sizeof(mp->mnt_stat));
    190 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    191 
    192 	DPRINTF(("puffs_start2: root vp %p, cur root pnode %p, cookie %p\n",
    193 	    pmp->pmp_root, pn, sreq->psr_cookie));
    194 
    195 	return 0;
    196 }
    197 
    198 int
    199 puffs_start(struct mount *mp, int flags, struct lwp *l)
    200 {
    201 
    202 	/*
    203 	 * This cannot travel to userspace, as this is called from
    204 	 * the kernel context of the process doing mount(2).  But
    205 	 * it's probably a safe bet that the process doing mount(2)
    206 	 * realizes it needs to start the filesystem also...
    207 	 */
    208 	return 0;
    209 }
    210 
    211 int
    212 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    213 {
    214 	struct puffs_mount *pmp;
    215 	int error, force;
    216 
    217 	PUFFS_VFSREQ(unmount);
    218 
    219 	error = 0;
    220 	force = mntflags & MNT_FORCE;
    221 	pmp = MPTOPUFFSMP(mp);
    222 
    223 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    224 	    "status 0x%x\n", pmp->pmp_status));
    225 
    226 	/*
    227 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    228 	 * root vnode does not get flushed until unmount.  The
    229 	 * userspace root node cookie is stored in the mount
    230 	 * structure, so we can always re-instantiate a root vnode,
    231 	 * should userspace unmount decide it doesn't want to
    232 	 * cooperate.
    233 	 */
    234 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    235 	if (error)
    236 		goto out;
    237 
    238 	/*
    239 	 * If we are not DYING, we should ask userspace's opinion
    240 	 * about the situation
    241 	 */
    242 	simple_lock(&pmp->pmp_lock);
    243 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    244 		pmp->pmp_unmounting = 1;
    245 		simple_unlock(&pmp->pmp_lock);
    246 
    247 		unmount_arg.pvfsr_flags = mntflags;
    248 		unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
    249 
    250 		error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
    251 		     &unmount_arg, sizeof(unmount_arg));
    252 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    253 
    254 		simple_lock(&pmp->pmp_lock);
    255 		pmp->pmp_unmounting = 0;
    256 		wakeup(&pmp->pmp_unmounting);
    257 	}
    258 
    259 	/*
    260 	 * if userspace cooperated or we really need to die,
    261 	 * screw what userland thinks and just die.
    262 	 */
    263 	if (error == 0 || force) {
    264 		pmp->pmp_status = PUFFSTAT_DYING;
    265 		puffs_nukebypmp(pmp);
    266 		simple_unlock(&pmp->pmp_lock);
    267 		FREE(pmp, M_PUFFS);
    268 		error = 0;
    269 	} else {
    270 		simple_unlock(&pmp->pmp_lock);
    271 	}
    272 
    273  out:
    274 	DPRINTF(("puffs_unmount: return %d\n", error));
    275 	return error;
    276 }
    277 
    278 /*
    279  * This doesn't need to travel to userspace
    280  */
    281 int
    282 puffs_root(struct mount *mp, struct vnode **vpp)
    283 {
    284 	struct puffs_mount *pmp;
    285 	struct puffs_node *pn;
    286 	struct vnode *vp;
    287 
    288 	pmp = MPTOPUFFSMP(mp);
    289 
    290 	/*
    291 	 * pmp_lock must be held if vref()'ing or vrele()'ing the
    292 	 * root vnode.  the latter is controlled by puffs_inactive().
    293 	 */
    294 	simple_lock(&pmp->pmp_lock);
    295 	vp = pmp->pmp_root;
    296 	if (vp) {
    297 		pn = VPTOPP(vp);
    298 		if (vget(vp, LK_EXCLUSIVE | LK_RETRY)) {
    299 			pmp->pmp_root = NULL;
    300 			goto grabnew;
    301 		}
    302 		simple_unlock(&pmp->pmp_lock);
    303 		*vpp = vp;
    304 		return 0;
    305 	}
    306  grabnew:
    307 	simple_unlock(&pmp->pmp_lock);
    308 
    309 	/*
    310 	 * So, didn't have the magic root vnode available.
    311 	 * No matter, grab another an stuff it with the cookie.
    312 	 */
    313 	if (puffs_getvnode(mp, pmp->pmp_rootcookie, VDIR, 0, 0, &vp))
    314 		panic("sloppy programming");
    315 
    316 	simple_lock(&pmp->pmp_lock);
    317 	/*
    318 	 * check if by mysterious force someone else created a root
    319 	 * vnode while we were executing.
    320 	 */
    321 	if (pmp->pmp_root) {
    322 		vref(pmp->pmp_root);
    323 		simple_unlock(&pmp->pmp_lock);
    324 		puffs_putvnode(vp);
    325 		vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    326 		*vpp = pmp->pmp_root;
    327 		return 0;
    328 	}
    329 
    330 	/* store cache */
    331 	vp->v_flag = VROOT;
    332 	pmp->pmp_root = vp;
    333 	simple_unlock(&pmp->pmp_lock);
    334 
    335 	vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    336 
    337 	*vpp = vp;
    338 	return 0;
    339 }
    340 
    341 int
    342 puffs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *l)
    343 {
    344 
    345 	return EOPNOTSUPP;
    346 }
    347 
    348 int
    349 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    350 {
    351 	struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
    352 	struct puffs_mount *pmp;
    353 	int error = 0;
    354 
    355 	pmp = MPTOPUFFSMP(mp);
    356 
    357 	/*
    358 	 * If we are mounting, it means that the userspace counterpart
    359 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    360 	 * requesting statvfs from userspace would mean a deadlock.
    361 	 * Compensate.
    362 	 */
    363 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    364 		return EINPROGRESS;
    365 
    366 	/* too big for stack */
    367 	MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
    368 	    sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
    369 	statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
    370 
    371 	error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
    372 	    statvfs_arg, sizeof(*statvfs_arg));
    373 	statvfs_arg->pvfsr_sb.f_iosize = DEV_BSIZE;
    374 
    375 	/*
    376 	 * Try to produce a sensible result even in the event
    377 	 * of userspace error.
    378 	 *
    379 	 * XXX: cache the copy in non-error case
    380 	 */
    381 	if (!error) {
    382 		copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
    383 		(void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
    384 		    sizeof(struct statvfs));
    385 	} else {
    386 		copy_statvfs_info(sbp, mp);
    387 	}
    388 
    389 	FREE(statvfs_arg, M_PUFFS);
    390 	return error;
    391 }
    392 
    393 int
    394 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    395 	struct lwp *l)
    396 {
    397 	struct vnode *vp, *nvp;
    398 	int error, rv;
    399 	int ppflags;
    400 
    401 	PUFFS_VFSREQ(sync);
    402 
    403 	error = 0;
    404 	ppflags = PGO_CLEANIT | PGO_ALLPAGES;
    405 	if (waitfor == MNT_WAIT)
    406 		ppflags |= PGO_SYNCIO;
    407 
    408 	/*
    409 	 * Sync all data from nodes.  The user server can still cache
    410 	 * metadata and control its syncing with VFS_SYNC.  However,
    411 	 * we just push all data with VOP_FSYNC already here to avoid
    412 	 * an extra pingpong query from userspace requesting that
    413 	 * data (and besides, there's no framework yet to handle it).
    414 	 */
    415 	simple_lock(&mntvnode_slock);
    416  loop:
    417 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    418 		/* check if we're on the right list */
    419 		if (vp->v_mount != mp)
    420 			goto loop;
    421 
    422 		simple_lock(&vp->v_interlock);
    423 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    424 
    425 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    426 			simple_unlock(&vp->v_interlock);
    427 			continue;
    428 		}
    429 
    430 		simple_unlock(&mntvnode_slock);
    431 
    432 		/*
    433 		 * Here we try to get a reference to the vnode and to
    434 		 * lock it.  This is mostly cargo-culted, but I will
    435 		 * offer an explanation to why I believe this might
    436 		 * actually do the right thing.
    437 		 *
    438 		 * If the vnode is a goner, we quite obviously don't need
    439 		 * to sync it.
    440 		 *
    441 		 * If the vnode was busy, we don't need to sync it because
    442 		 * this is never called with MNT_WAIT except from
    443 		 * dounmount(), when we are wait-flushing all the dirty
    444 		 * vnodes through other routes in any case.  So there,
    445 		 * sync() doesn't actually sync.  Happy now?
    446 		 */
    447 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    448 		if (rv) {
    449 			simple_lock(&mntvnode_slock);
    450 			if (rv == ENOENT)
    451 				goto loop;
    452 			continue;
    453 		}
    454 
    455 		simple_lock(&vp->v_interlock);
    456 		rv = VOP_PUTPAGES(vp, 0, 0, ppflags);
    457 		if (rv)
    458 			error = rv;
    459 		vput(vp);
    460 		simple_lock(&mntvnode_slock);
    461 	}
    462 	simple_unlock(&mntvnode_slock);
    463 
    464 	/* sync fs */
    465 	sync_arg.pvfsr_waitfor = waitfor;
    466 	puffs_credcvt(&sync_arg.pvfsr_cred, cred);
    467 	sync_arg.pvfsr_pid = puffs_lwp2pid(l);
    468 
    469 	rv = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
    470 	    &sync_arg, sizeof(sync_arg));
    471 	if (rv)
    472 		error = rv;
    473 
    474 	return error;
    475 }
    476 
    477 int
    478 puffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    479 {
    480 
    481 	return EOPNOTSUPP;
    482 }
    483 
    484 #if 0
    485 /*ARGSUSED*/
    486 int
    487 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    488 {
    489 
    490 	return EOPNOTSUPP;
    491 }
    492 
    493 /*ARGSUSED*/
    494 int
    495 puffs_vptofh(struct vnode *vp, struct fid *fhp)
    496 {
    497 
    498 	return EOPNOTSUPP;
    499 }
    500 #endif
    501 
    502 void
    503 puffs_init()
    504 {
    505 
    506 #ifdef _LKM
    507 	malloc_type_attach(M_PUFFS);
    508 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    509 	    "puffspnpl", &pool_allocator_nointr);
    510 #endif
    511 
    512 	return;
    513 }
    514 
    515 void
    516 puffs_done()
    517 {
    518 
    519 #ifdef _LKM
    520 	pool_destroy(&puffs_pnpool);
    521 	malloc_type_detach(M_PUFFS);
    522 #endif
    523 
    524 	return;
    525 }
    526 
    527 int
    528 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    529 {
    530 
    531 	return EOPNOTSUPP;
    532 }
    533 
    534 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    535 	&puffs_vnodeop_opv_desc,
    536 	&puffs_specop_opv_desc,
    537 	&puffs_fifoop_opv_desc,
    538 	&puffs_msgop_opv_desc,
    539 	NULL,
    540 };
    541 
    542 struct vfsops puffs_vfsops = {
    543 	MOUNT_PUFFS,
    544 	puffs_mount,		/* mount	*/
    545 	puffs_start,		/* start	*/
    546 	puffs_unmount,		/* unmount	*/
    547 	puffs_root,		/* root		*/
    548 	puffs_quotactl,		/* quotactl	*/
    549 	puffs_statvfs,		/* statvfs	*/
    550 	puffs_sync,		/* sync		*/
    551 	puffs_vget,		/* vget		*/
    552 	(void *)eopnotsupp,	/* fhtovp	*/
    553 	(void *)eopnotsupp,	/* vptofh	*/
    554 	puffs_init,		/* init		*/
    555 	NULL,			/* reinit	*/
    556 	puffs_done,		/* done		*/
    557 	NULL,			/* mountroot	*/
    558 	puffs_snapshot,		/* snapshot	*/
    559 	vfs_stdextattrctl,	/* extattrctl	*/
    560 	puffs_vnodeopv_descs,	/* vnodeops	*/
    561 	0,			/* refcount	*/
    562 	{ NULL, NULL }
    563 };
    564 VFS_ATTACH(puffs_vfsops);
    565