Home | History | Annotate | Line # | Download | only in puffs
puffs_subr.c revision 1.20.2.6
      1 /*	$NetBSD: puffs_subr.c,v 1.20.2.6 2007/05/17 13:41:44 yamt 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_subr.c,v 1.20.2.6 2007/05/17 13:41:44 yamt Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/conf.h>
     40 #include <sys/hash.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mount.h>
     43 #include <sys/socketvar.h>
     44 #include <sys/vnode.h>
     45 #include <sys/kauth.h>
     46 #include <sys/namei.h>
     47 
     48 #include <fs/puffs/puffs_msgif.h>
     49 #include <fs/puffs/puffs_sys.h>
     50 
     51 #include <miscfs/genfs/genfs_node.h>
     52 #include <miscfs/specfs/specdev.h>
     53 
     54 struct pool puffs_pnpool;
     55 
     56 #ifdef PUFFSDEBUG
     57 int puffsdebug;
     58 #endif
     59 
     60 static __inline struct puffs_node_hashlist
     61 	*puffs_cookie2hashlist(struct puffs_mount *, void *);
     62 static struct puffs_node *puffs_cookie2pnode(struct puffs_mount *, void *);
     63 
     64 static void puffs_gop_size(struct vnode *, off_t, off_t *, int);
     65 static void puffs_gop_markupdate(struct vnode *, int);
     66 
     67 static const struct genfs_ops puffs_genfsops = {
     68 	.gop_size = puffs_gop_size,
     69 	.gop_write = genfs_gop_write,
     70 	.gop_markupdate = puffs_gop_markupdate,
     71 #if 0
     72 	.gop_alloc, should ask userspace
     73 #endif
     74 };
     75 
     76 /*
     77  * Grab a vnode, intialize all the puffs-dependant stuff.
     78  */
     79 int
     80 puffs_getvnode(struct mount *mp, void *cookie, enum vtype type,
     81 	voff_t vsize, dev_t rdev, struct vnode **vpp)
     82 {
     83 	struct puffs_mount *pmp;
     84 	struct vnode *vp, *nvp;
     85 	struct puffs_node *pnode;
     86 	struct puffs_node_hashlist *plist;
     87 	int error;
     88 
     89 	pmp = MPTOPUFFSMP(mp);
     90 
     91 	/*
     92 	 * XXX: there is a deadlock condition between vfs_busy() and
     93 	 * vnode locks.  For an unmounting file system the mountpoint
     94 	 * is frozen, but in unmount(FORCE) vflush() wants to access all
     95 	 * of the vnodes.  If we are here waiting for the mountpoint
     96 	 * lock while holding on to a vnode lock, well, we ain't
     97 	 * just pining for the fjords anymore.  If we release the
     98 	 * vnode lock, we will be in the situation "mount point
     99 	 * is dying" and panic() will ensue in insmntque.  So as a
    100 	 * temporary workaround, get a vnode without putting it on
    101 	 * the mount point list, check if mount point is still alive
    102 	 * and kicking and only then add the vnode to the list.
    103 	 */
    104 	error = getnewvnode(VT_PUFFS, NULL, puffs_vnodeop_p, &vp);
    105 	if (error)
    106 		return error;
    107 	vp->v_vnlock = NULL;
    108 	vp->v_type = type;
    109 
    110 	/*
    111 	 * Check what mount point isn't going away.  This will work
    112 	 * until we decide to remove biglock or make the kernel
    113 	 * preemptive.  But hopefully the real problem will be fixed
    114 	 * by then.
    115 	 *
    116 	 * XXX: yes, should call vfs_busy(), but thar be rabbits with
    117 	 * vicious streaks a mile wide ...
    118 	 */
    119 	if (mp->mnt_iflag & IMNT_UNMOUNT) {
    120 		DPRINTF(("puffs_getvnode: mp %p unmount, unable to create "
    121 		    "vnode for cookie %p\n", mp, cookie));
    122 		ungetnewvnode(vp);
    123 		return ENXIO;
    124 	}
    125 
    126 	/* So it's not dead yet.. good.. inform new vnode of its master */
    127 	simple_lock(&mntvnode_slock);
    128 	TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
    129 	simple_unlock(&mntvnode_slock);
    130 	vp->v_mount = mp;
    131 
    132 	/*
    133 	 * clerical tasks & footwork
    134 	 */
    135 
    136 	/* default size */
    137 	uvm_vnp_setsize(vp, 0);
    138 
    139 	/* dances based on vnode type. almost ufs_vinit(), but not quite */
    140 	switch (type) {
    141 	case VCHR:
    142 	case VBLK:
    143 		/*
    144 		 * replace vnode operation vector with the specops vector.
    145 		 * our user server has very little control over the node
    146 		 * if it decides its a character or block special file
    147 		 */
    148 		vp->v_op = puffs_specop_p;
    149 
    150 		/* do the standard checkalias-dance */
    151 		if ((nvp = checkalias(vp, rdev, mp)) != NULL) {
    152 			/*
    153 			 * found: release & unallocate aliased
    154 			 * old (well, actually, new) node
    155 			 */
    156 			vp->v_op = spec_vnodeop_p;
    157 			vp->v_flag &= ~VLOCKSWORK;
    158 			vrele(vp);
    159 			vgone(vp); /* cya */
    160 
    161 			/* init "new" vnode */
    162 			vp = nvp;
    163 			vp->v_vnlock = NULL;
    164 			vp->v_mount = mp;
    165 		}
    166 		break;
    167 
    168 	case VFIFO:
    169 		vp->v_op = puffs_fifoop_p;
    170 		break;
    171 
    172 	case VREG:
    173 		uvm_vnp_setsize(vp, vsize);
    174 		break;
    175 
    176 	case VDIR:
    177 	case VLNK:
    178 	case VSOCK:
    179 		break;
    180 	default:
    181 #ifdef DIAGNOSTIC
    182 		panic("puffs_getvnode: invalid vtype %d", type);
    183 #endif
    184 		break;
    185 	}
    186 
    187 	pnode = pool_get(&puffs_pnpool, PR_WAITOK);
    188 	pnode->pn_cookie = cookie;
    189 	pnode->pn_stat = 0;
    190 	plist = puffs_cookie2hashlist(pmp, cookie);
    191 	LIST_INSERT_HEAD(plist, pnode, pn_hashent);
    192 	vp->v_data = pnode;
    193 	vp->v_type = type;
    194 	pnode->pn_vp = vp;
    195 
    196 	genfs_node_init(vp, &puffs_genfsops);
    197 	*vpp = vp;
    198 
    199 	DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
    200 	    pnode, pnode->pn_cookie));
    201 
    202 	return 0;
    203 }
    204 
    205 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
    206 int
    207 puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
    208 	void *cookie, struct componentname *cnp, enum vtype type, dev_t rdev)
    209 {
    210 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    211 	struct vnode *vp;
    212 	int error;
    213 
    214 	/* userspace probably has this as a NULL op */
    215 	if (cookie == NULL) {
    216 		error = EOPNOTSUPP;
    217 		return error;
    218 	}
    219 
    220 	/*
    221 	 * Check for previous node with the same designation.
    222 	 * Explicitly check the root node cookie, since it might be
    223 	 * reclaimed from the kernel when this check is made.
    224 	 *
    225 	 * XXX: technically this error check should punish the fs,
    226 	 * not the caller.
    227 	 */
    228 	mutex_enter(&pmp->pmp_lock);
    229 	if (cookie == pmp->pmp_rootcookie
    230 	    || puffs_cookie2pnode(pmp, cookie) != NULL) {
    231 		mutex_exit(&pmp->pmp_lock);
    232 		error = EEXIST;
    233 		return error;
    234 	}
    235 	mutex_exit(&pmp->pmp_lock);
    236 
    237 	error = puffs_getvnode(dvp->v_mount, cookie, type, 0, rdev, &vp);
    238 	if (error)
    239 		return error;
    240 
    241 	vp->v_type = type;
    242 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    243 	*vpp = vp;
    244 
    245 	if ((cnp->cn_flags & MAKEENTRY) && PUFFS_DOCACHE(pmp))
    246 		cache_enter(dvp, vp, cnp);
    247 
    248 	return 0;
    249 }
    250 
    251 void
    252 puffs_putvnode(struct vnode *vp)
    253 {
    254 	struct puffs_mount *pmp;
    255 	struct puffs_node *pnode;
    256 
    257 	pmp = VPTOPUFFSMP(vp);
    258 	pnode = VPTOPP(vp);
    259 
    260 #ifdef DIAGNOSTIC
    261 	if (vp->v_tag != VT_PUFFS)
    262 		panic("puffs_putvnode: %p not a puffs vnode", vp);
    263 #endif
    264 
    265 	LIST_REMOVE(pnode, pn_hashent);
    266 	genfs_node_destroy(vp);
    267 	pool_put(&puffs_pnpool, vp->v_data);
    268 	vp->v_data = NULL;
    269 
    270 	return;
    271 }
    272 
    273 static __inline struct puffs_node_hashlist *
    274 puffs_cookie2hashlist(struct puffs_mount *pmp, void *cookie)
    275 {
    276 	uint32_t hash;
    277 
    278 	hash = hash32_buf(&cookie, sizeof(void *), HASH32_BUF_INIT);
    279 	return &pmp->pmp_pnodehash[hash % pmp->pmp_npnodehash];
    280 }
    281 
    282 /*
    283  * Translate cookie to puffs_node.  Caller must hold mountpoint
    284  * lock and it will be held upon return.
    285  */
    286 static struct puffs_node *
    287 puffs_cookie2pnode(struct puffs_mount *pmp, void *cookie)
    288 {
    289 	struct puffs_node_hashlist *plist;
    290 	struct puffs_node *pnode;
    291 
    292 	plist = puffs_cookie2hashlist(pmp, cookie);
    293 	LIST_FOREACH(pnode, plist, pn_hashent) {
    294 		if (pnode->pn_cookie == cookie)
    295 			break;
    296 	}
    297 
    298 	return pnode;
    299 }
    300 
    301 /*
    302  * Locate the in-kernel vnode based on the cookie received given
    303  * from userspace.  Returns a vnode, if found, NULL otherwise.
    304  * The parameter "lock" control whether to lock the possible or
    305  * not.  Locking always might cause us to lock against ourselves
    306  * in situations where we want the vnode but don't care for the
    307  * vnode lock, e.g. file server issued putpages.
    308  */
    309 struct vnode *
    310 puffs_pnode2vnode(struct puffs_mount *pmp, void *cookie, int lock)
    311 {
    312 	struct puffs_node *pnode;
    313 	struct vnode *vp;
    314 	int vgetflags;
    315 
    316 	/*
    317 	 * If we're trying to get the root vnode, return it through
    318 	 * puffs_root() to get all the right things set.  Lock must
    319 	 * be set, since VFS_ROOT() always locks the returned vnode.
    320 	 */
    321 	if (cookie == pmp->pmp_rootcookie) {
    322 		if (!lock)
    323 			return NULL;
    324 		if (VFS_ROOT(pmp->pmp_mp, &vp))
    325 			return NULL;
    326 
    327 		return vp;
    328 	}
    329 
    330 	vgetflags = LK_INTERLOCK;
    331 	if (lock)
    332 		vgetflags |= LK_EXCLUSIVE | LK_RETRY;
    333 
    334 	mutex_enter(&pmp->pmp_lock);
    335 	pnode = puffs_cookie2pnode(pmp, cookie);
    336 
    337 	if (pnode == NULL) {
    338 		mutex_exit(&pmp->pmp_lock);
    339 		return NULL;
    340 	}
    341 	vp = pnode->pn_vp;
    342 
    343 	simple_lock(&vp->v_interlock);
    344 	mutex_exit(&pmp->pmp_lock);
    345 
    346 	if (vget(vp, vgetflags))
    347 		return NULL;
    348 
    349 	return vp;
    350 }
    351 
    352 void
    353 puffs_makecn(struct puffs_kcn *pkcn, const struct componentname *cn)
    354 {
    355 
    356 	pkcn->pkcn_nameiop = cn->cn_nameiop;
    357 	pkcn->pkcn_flags = cn->cn_flags;
    358 	pkcn->pkcn_pid = cn->cn_lwp->l_proc->p_pid;
    359 	puffs_credcvt(&pkcn->pkcn_cred, cn->cn_cred);
    360 
    361 	(void)memcpy(&pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
    362 	pkcn->pkcn_name[cn->cn_namelen] = '\0';
    363 	pkcn->pkcn_namelen = cn->cn_namelen;
    364 }
    365 
    366 /*
    367  * Convert given credentials to struct puffs_cred for userspace.
    368  */
    369 void
    370 puffs_credcvt(struct puffs_cred *pcr, const kauth_cred_t cred)
    371 {
    372 
    373 	memset(pcr, 0, sizeof(struct puffs_cred));
    374 
    375 	if (cred == NOCRED || cred == FSCRED) {
    376 		pcr->pcr_type = PUFFCRED_TYPE_INTERNAL;
    377 		if (cred == NOCRED)
    378 			pcr->pcr_internal = PUFFCRED_CRED_NOCRED;
    379 		if (cred == FSCRED)
    380 			pcr->pcr_internal = PUFFCRED_CRED_FSCRED;
    381  	} else {
    382 		pcr->pcr_type = PUFFCRED_TYPE_UUC;
    383 		kauth_cred_to_uucred(&pcr->pcr_uuc, cred);
    384 	}
    385 }
    386 
    387 /*
    388  * Return pid.  In case the operation is coming from within the
    389  * kernel without any process context, borrow the swapper's pid.
    390  */
    391 pid_t
    392 puffs_lwp2pid(struct lwp *l)
    393 {
    394 
    395 	return l ? l->l_proc->p_pid : 0;
    396 }
    397 
    398 
    399 static void
    400 puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
    401 	int flags)
    402 {
    403 
    404 	*eobp = size;
    405 }
    406 
    407 static void
    408 puffs_gop_markupdate(struct vnode *vp, int flags)
    409 {
    410 	int uflags = 0;
    411 
    412 	if (flags & GOP_UPDATE_ACCESSED)
    413 		uflags |= PUFFS_UPDATEATIME;
    414 	if (flags & GOP_UPDATE_MODIFIED)
    415 		uflags |= PUFFS_UPDATEMTIME;
    416 
    417 	puffs_updatenode(vp, uflags);
    418 }
    419 
    420 void
    421 puffs_updatenode(struct vnode *vp, int flags)
    422 {
    423 	struct puffs_node *pn;
    424 	struct timespec ts;
    425 
    426 	if (flags == 0)
    427 		return;
    428 
    429 	pn = VPTOPP(vp);
    430 	nanotime(&ts);
    431 
    432 	if (flags & PUFFS_UPDATEATIME) {
    433 		pn->pn_mc_atime = ts;
    434 		pn->pn_stat |= PNODE_METACACHE_ATIME;
    435 	}
    436 	if (flags & PUFFS_UPDATECTIME) {
    437 		pn->pn_mc_ctime = ts;
    438 		pn->pn_stat |= PNODE_METACACHE_CTIME;
    439 	}
    440 	if (flags & PUFFS_UPDATEMTIME) {
    441 		pn->pn_mc_mtime = ts;
    442 		pn->pn_stat |= PNODE_METACACHE_MTIME;
    443 	}
    444 	if (flags & PUFFS_UPDATESIZE) {
    445 		pn->pn_mc_size = vp->v_size;
    446 		pn->pn_stat |= PNODE_METACACHE_SIZE;
    447 	}
    448 }
    449 
    450 void
    451 puffs_updatevpsize(struct vnode *vp)
    452 {
    453 	struct vattr va;
    454 
    455 	if (VOP_GETATTR(vp, &va, FSCRED, NULL))
    456 		return;
    457 
    458 	if (va.va_size != VNOVAL)
    459 		vp->v_size = va.va_size;
    460 }
    461 
    462 void
    463 puffs_parkdone_asyncbioread(struct puffs_req *preq, void *arg)
    464 {
    465 	struct puffs_vnreq_read *read_argp = (void *)preq;
    466 	struct buf *bp = arg;
    467 	size_t moved;
    468 
    469 	bp->b_error = preq->preq_rv;
    470 	if (bp->b_error == 0) {
    471 		moved = bp->b_bcount - read_argp->pvnr_resid;
    472 		bp->b_resid = read_argp->pvnr_resid;
    473 
    474 		memcpy(bp->b_data, read_argp->pvnr_data, moved);
    475 	} else {
    476 		bp->b_flags |= B_ERROR;
    477 	}
    478 
    479 	biodone(bp);
    480 	free(preq, M_PUFFS);
    481 }
    482 
    483 void
    484 puffs_mp_reference(struct puffs_mount *pmp)
    485 {
    486 
    487 	KASSERT(mutex_owned(&pmp->pmp_lock));
    488 	pmp->pmp_refcount++;
    489 }
    490 
    491 void
    492 puffs_mp_release(struct puffs_mount *pmp)
    493 {
    494 
    495 	KASSERT(mutex_owned(&pmp->pmp_lock));
    496 	if (--pmp->pmp_refcount == 0)
    497 		cv_broadcast(&pmp->pmp_refcount_cv);
    498 }
    499