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