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