Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.9
      1 /*	$NetBSD: puffs_vfsops.c,v 1.9 2006/11/18 12:39:48 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.9 2006/11/18 12:39:48 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:")+1]; /* do I get a prize? */
     63 	int error;
     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 	error = copyin(data, &args, sizeof(struct puffs_args));
     81 	if (error)
     82 		return error;
     83 
     84 	/* nuke spy bits */
     85 	args.pa_flags &= PUFFSFLAG_MASK;
     86 
     87 	/* build real name */
     88 	(void)strlcpy(namebuf, "puffs:", sizeof(namebuf));
     89 	(void)strlcat(namebuf, args.pa_name, sizeof(namebuf));
     90 
     91 	/* inform user server if it got the max request size it wanted */
     92 	if (args.pa_maxreqlen == 0 || args.pa_maxreqlen > PUFFS_REQ_MAXSIZE)
     93 		args.pa_maxreqlen = PUFFS_REQ_MAXSIZE;
     94 	else if (args.pa_maxreqlen < PUFFS_REQSTRUCT_MAX)
     95 		args.pa_maxreqlen = PUFFS_REQSTRUCT_MAX;
     96 	(void)strlcpy(args.pa_name, namebuf, sizeof(args.pa_name));
     97 
     98 	error = copyout(&args, data, sizeof(struct puffs_args));
     99 	if (error)
    100 		return error;
    101 
    102 	error = set_statvfs_info(path, UIO_USERSPACE, namebuf,
    103 	    UIO_SYSSPACE, mp, l);
    104 	if (error)
    105 		return error;
    106 
    107 	MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
    108 	    M_PUFFS, M_WAITOK | M_ZERO);
    109 
    110 	mp->mnt_fs_bshift = DEV_BSHIFT;
    111 	mp->mnt_dev_bshift = DEV_BSHIFT;
    112 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    113 	mp->mnt_data = pmp;
    114 
    115 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    116 	pmp->pmp_nextreq = 0;
    117 	pmp->pmp_mp = mp;
    118 	pmp->pmp_req_maxsize = args.pa_maxreqlen;
    119 	pmp->pmp_args = args;
    120 
    121 	/*
    122 	 * Inform the fileops processing code that we have a mountpoint.
    123 	 * If it doesn't know about anyone with our pid/fd having the
    124 	 * device open, punt
    125 	 */
    126 	if (puffs_setpmp(l->l_proc->p_pid, args.pa_fd, pmp)) {
    127 		FREE(pmp, M_PUFFS);
    128 		return ENOENT;
    129 	}
    130 
    131 	simple_lock_init(&pmp->pmp_lock);
    132 	TAILQ_INIT(&pmp->pmp_req_touser);
    133 	TAILQ_INIT(&pmp->pmp_req_replywait);
    134 	TAILQ_INIT(&pmp->pmp_req_sizepark);
    135 
    136 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    137 	    mp, MPTOPUFFSMP(mp)));
    138 
    139 	vfs_getnewfsid(mp);
    140 
    141 	return 0;
    142 }
    143 
    144 /*
    145  * This is called from the first "Hello, I'm alive" ioctl
    146  * from userspace.
    147  */
    148 int
    149 puffs_start2(struct puffs_mount *pmp, struct puffs_startreq *sreq)
    150 {
    151 	struct puffs_node *pn;
    152 	struct mount *mp;
    153 
    154 	mp = PMPTOMP(pmp);
    155 
    156 	simple_lock(&pmp->pmp_lock);
    157 
    158 	/*
    159 	 * if someone has issued a VFS_ROOT() already, fill in the
    160 	 * vnode cookie.
    161 	 */
    162 	pn = NULL;
    163 	if (pmp->pmp_root) {
    164 		pn = VPTOPP(pmp->pmp_root);
    165 		pn->pn_cookie = sreq->psr_cookie;
    166 	}
    167 
    168 	/* We're good to fly */
    169 	pmp->pmp_rootcookie = sreq->psr_cookie;
    170 	pmp->pmp_status = PUFFSTAT_RUNNING;
    171 	simple_unlock(&pmp->pmp_lock);
    172 
    173 	/* do the VFS_STATVFS() we missed out on in sys_mount() */
    174 	copy_statvfs_info(&sreq->psr_sb, mp);
    175 	(void)memcpy(&mp->mnt_stat, &sreq->psr_sb, sizeof(mp->mnt_stat));
    176 
    177 	DPRINTF(("puffs_start2: root vp %p, cur root pnode %p, cookie %p\n",
    178 	    pmp->pmp_root, pn, sreq->psr_cookie));
    179 
    180 	return 0;
    181 }
    182 
    183 int
    184 puffs_start(struct mount *mp, int flags, struct lwp *l)
    185 {
    186 
    187 	/*
    188 	 * This cannot travel to userspace, as this is called from
    189 	 * the kernel context of the process doing mount(2).  But
    190 	 * it's probably a safe bet that the process doing mount(2)
    191 	 * realizes it needs to start the filesystem also...
    192 	 */
    193 	return 0;
    194 }
    195 
    196 int
    197 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    198 {
    199 	struct puffs_mount *pmp;
    200 	int error, force;
    201 
    202 	PUFFS_VFSREQ(unmount);
    203 
    204 	error = 0;
    205 	force = mntflags & MNT_FORCE;
    206 	pmp = MPTOPUFFSMP(mp);
    207 
    208 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    209 	    "status 0x%x\n", pmp->pmp_status));
    210 
    211 	/*
    212 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    213 	 * root vnode does not get flushed until unmount.  The
    214 	 * userspace root node cookie is stored in the mount
    215 	 * structure, so we can always re-instantiate a root vnode,
    216 	 * should userspace unmount decide it doesn't want to
    217 	 * cooperate.
    218 	 */
    219 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    220 	if (error)
    221 		goto out;
    222 
    223 	/*
    224 	 * If we are not DYING, we should ask userspace's opinion
    225 	 * about the situation
    226 	 */
    227 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    228 		unmount_arg.pvfsr_flags = mntflags;
    229 		unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
    230 		error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
    231 		     &unmount_arg, sizeof(unmount_arg));
    232 	}
    233 
    234 	/*
    235 	 * if userspace cooperated or we really need to die,
    236 	 * screw what userland thinks and just die.
    237 	 */
    238 	DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    239 	if (error == 0 || force) {
    240 		pmp->pmp_status = PUFFSTAT_DYING;
    241 		puffs_nukebypmp(pmp);
    242 		FREE(pmp, M_PUFFS);
    243 		error = 0;
    244 	}
    245 
    246  out:
    247 	DPRINTF(("puffs_unmount: return %d\n", error));
    248 	return error;
    249 }
    250 
    251 /*
    252  * This doesn't need to travel to userspace
    253  */
    254 int
    255 puffs_root(struct mount *mp, struct vnode **vpp)
    256 {
    257 	struct puffs_mount *pmp;
    258 	struct puffs_node *pn;
    259 	struct vnode *vp;
    260 
    261 	pmp = MPTOPUFFSMP(mp);
    262 
    263 	/*
    264 	 * pmp_lock must be held if vref()'ing or vrele()'ing the
    265 	 * root vnode.
    266 	 */
    267 	simple_lock(&pmp->pmp_lock);
    268 	vp = pmp->pmp_root;
    269 	if (vp) {
    270 		pn = VPTOPP(vp);
    271 		if (pn->pn_stat & PNODE_INACTIVE)  {
    272 			if (vget(vp, LK_NOWAIT)) {
    273 				pmp->pmp_root = NULL;
    274 				goto grabnew;
    275 			}
    276 		} else
    277 			vref(vp);
    278 		simple_unlock(&pmp->pmp_lock);
    279 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    280 		*vpp = vp;
    281 		return 0;
    282 	}
    283  grabnew:
    284 	simple_unlock(&pmp->pmp_lock);
    285 
    286 	/*
    287 	 * So, didn't have the magic root vnode available.
    288 	 * No matter, grab another an stuff it with the cookie.
    289 	 */
    290 	if (puffs_getvnode(mp, pmp->pmp_rootcookie, VDIR, 0, 0, &vp))
    291 		panic("sloppy programming");
    292 
    293 	simple_lock(&pmp->pmp_lock);
    294 	/*
    295 	 * check if by mysterious force someone else created a root
    296 	 * vnode while we were executing.
    297 	 */
    298 	if (pmp->pmp_root) {
    299 		vref(pmp->pmp_root);
    300 		simple_unlock(&pmp->pmp_lock);
    301 		puffs_putvnode(vp);
    302 		vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    303 		*vpp = pmp->pmp_root;
    304 		return 0;
    305 	}
    306 
    307 	/* store cache */
    308 	vp->v_flag = VROOT;
    309 	pmp->pmp_root = vp;
    310 	simple_unlock(&pmp->pmp_lock);
    311 
    312 	vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    313 
    314 	*vpp = vp;
    315 	return 0;
    316 }
    317 
    318 int
    319 puffs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *l)
    320 {
    321 
    322 	return EOPNOTSUPP;
    323 }
    324 
    325 int
    326 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    327 {
    328 	struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
    329 	struct puffs_mount *pmp;
    330 	int error = 0;
    331 
    332 	pmp = MPTOPUFFSMP(mp);
    333 
    334 	/*
    335 	 * If we are mounting, it means that the userspace counterpart
    336 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    337 	 * requesting statvfs from userspace would mean a deadlock.
    338 	 * Compensate.
    339 	 */
    340 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    341 		return EINPROGRESS;
    342 
    343 	/* too big for stack */
    344 	MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
    345 	    sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
    346 	statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
    347 
    348 	error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
    349 	    statvfs_arg, sizeof(*statvfs_arg));
    350 
    351 	/*
    352 	 * Try to produce a sensible result even in the event
    353 	 * of userspace error.
    354 	 *
    355 	 * XXX: cache the copy in non-error case
    356 	 */
    357 	if (!error) {
    358 		copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
    359 		(void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
    360 		    sizeof(struct statvfs));
    361 	} else {
    362 		copy_statvfs_info(sbp, mp);
    363 	}
    364 
    365 	FREE(statvfs_arg, M_PUFFS);
    366 
    367 	return 0;
    368 }
    369 
    370 int
    371 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    372 	struct lwp *l)
    373 {
    374 	int error;
    375 
    376 	PUFFS_VFSREQ(sync);
    377 
    378 	sync_arg.pvfsr_waitfor = waitfor;
    379 	puffs_credcvt(&sync_arg.pvfsr_cred, cred);
    380 	sync_arg.pvfsr_pid = puffs_lwp2pid(l);
    381 
    382 	error = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
    383 	    &sync_arg, sizeof(sync_arg));
    384 
    385 	return error;
    386 }
    387 
    388 int
    389 puffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    390 {
    391 
    392 	return EOPNOTSUPP;
    393 }
    394 
    395 #if 0
    396 /*ARGSUSED*/
    397 int
    398 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    399 {
    400 
    401 	return EOPNOTSUPP;
    402 }
    403 
    404 /*ARGSUSED*/
    405 int
    406 puffs_vptofh(struct vnode *vp, struct fid *fhp)
    407 {
    408 
    409 	return EOPNOTSUPP;
    410 }
    411 #endif
    412 
    413 void
    414 puffs_init()
    415 {
    416 
    417 #ifdef _LKM
    418 	malloc_type_attach(M_PUFFS);
    419 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    420 	    "puffspnpl", &pool_allocator_nointr);
    421 #endif
    422 
    423 	return;
    424 }
    425 
    426 void
    427 puffs_done()
    428 {
    429 
    430 #ifdef _LKM
    431 	pool_destroy(&puffs_pnpool);
    432 	malloc_type_detach(M_PUFFS);
    433 #endif
    434 
    435 	return;
    436 }
    437 
    438 int
    439 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    440 {
    441 
    442 	return EOPNOTSUPP;
    443 }
    444 
    445 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    446 	&puffs_vnodeop_opv_desc,
    447 	&puffs_specop_opv_desc,
    448 	&puffs_fifoop_opv_desc,
    449 	NULL,
    450 };
    451 
    452 struct vfsops puffs_vfsops = {
    453 	MOUNT_PUFFS,
    454 	puffs_mount,		/* mount	*/
    455 	puffs_start,		/* start	*/
    456 	puffs_unmount,		/* unmount	*/
    457 	puffs_root,		/* root		*/
    458 	puffs_quotactl,		/* quotactl	*/
    459 	puffs_statvfs,		/* statvfs	*/
    460 	puffs_sync,		/* sync		*/
    461 	puffs_vget,		/* vget		*/
    462 	NULL,			/* fhtovp	*/
    463 	NULL,			/* vptofh	*/
    464 	puffs_init,		/* init		*/
    465 	NULL,			/* reinit	*/
    466 	puffs_done,		/* done		*/
    467 	NULL,			/* mountroot	*/
    468 	puffs_snapshot,		/* snapshot	*/
    469 	vfs_stdextattrctl,	/* extattrctl	*/
    470 	puffs_vnodeopv_descs,	/* vnodeops	*/
    471 	0,			/* refcount	*/
    472 	{ NULL, NULL }
    473 };
    474 VFS_ATTACH(puffs_vfsops);
    475