Home | History | Annotate | Line # | Download | only in puffs
puffs_subr.c revision 1.1
      1 /*	$NetBSD: puffs_subr.c,v 1.1 2006/10/22 22:43: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_subr.c,v 1.1 2006/10/22 22:43:23 pooka Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/conf.h>
     40 #include <sys/malloc.h>
     41 #include <sys/mount.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/vnode.h>
     44 #include <sys/kauth.h>
     45 #include <sys/namei.h>
     46 
     47 #include <fs/puffs/puffs_msgif.h>
     48 #include <fs/puffs/puffs_sys.h>
     49 
     50 POOL_INIT(puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0, "puffspnpl",
     51     &pool_allocator_nointr);
     52 
     53 /*
     54  * Grab a vnode, intialize all the puffs-dependant stuff.
     55  */
     56 int
     57 puffs_getvnode(struct mount *mp, void *cookie, struct vnode **vpp)
     58 {
     59 	struct puffs_mount *pmp;
     60 	struct vnode *vp;
     61 	struct puffs_node *pnode;
     62 	int error;
     63 
     64 	pmp = MPTOPUFFSMP(mp);
     65 
     66 	pnode = pool_get(&puffs_pnpool, PR_WAITOK);
     67 	error = getnewvnode(VT_PUFFS, mp, puffs_vnodeop_p, &vp);
     68 	if (error) {
     69 		pool_put(&puffs_pnpool, pnode);
     70 		return error;
     71 	}
     72 	vp->v_vnlock = NULL;
     73 
     74 	pnode->pn_cookie = cookie;
     75 	pnode->pn_stat = 0;
     76 	LIST_INSERT_HEAD(&pmp->pmp_pnodelist, pnode, pn_entries);
     77 	vp->v_data = pnode;
     78 	pnode->pn_vp = vp;
     79 
     80 	*vpp = vp;
     81 
     82 	DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
     83 	    pnode, pnode->pn_cookie));
     84 
     85 	return 0;
     86 }
     87 
     88 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
     89 int
     90 puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
     91 	void *cookie, struct componentname *cnp, enum vtype type)
     92 {
     93 	struct vnode *vp;
     94 	int error;
     95 
     96 	/* userspace probably has this as a NULL op */
     97 	if (cookie == NULL) {
     98 		error = EOPNOTSUPP;
     99 		goto out;
    100 	}
    101 
    102 	error = puffs_getvnode(dvp->v_mount, cookie, &vp);
    103 	if (error)
    104 		goto out;
    105 
    106 	vp->v_type = type;
    107 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    108 	*vpp = vp;
    109 
    110  out:
    111 	if (error || (cnp->cn_flags & SAVESTART) == 0)
    112 		PNBUF_PUT(cnp->cn_pnbuf);
    113 	vput(dvp);
    114 
    115 	return error;
    116 }
    117 
    118 void
    119 puffs_putvnode(struct vnode *vp)
    120 {
    121 	struct puffs_mount *pmp;
    122 	struct puffs_node *pnode;
    123 
    124 	pmp = VPTOPUFFSMP(vp);
    125 	pnode = VPTOPP(vp);
    126 
    127 #ifdef DIAGNOSTIC
    128 	if (vp->v_tag != VT_PUFFS)
    129 		panic("puffs_putvnode: %p not a puffs vnode", vp);
    130 #endif
    131 
    132 	LIST_REMOVE(pnode, pn_entries);
    133 	pool_put(&puffs_pnpool, vp->v_data);
    134 	vp->v_data = NULL;
    135 
    136 	return;
    137 }
    138 
    139 /*
    140  * Locate the in-kernel vnode based on the cookie received given
    141  * from userspace.  Returns a locked & referenced vnode, if found,
    142  * NULL otherwise.
    143  *
    144  * XXX: lists, although lookup cache mostly shields us from this
    145  */
    146 struct vnode *
    147 puffs_pnode2vnode(struct puffs_mount *pmp, void *cookie)
    148 {
    149 	struct puffs_node *pnode;
    150 	struct vnode *vp;
    151 
    152 	simple_lock(&pmp->pmp_lock);
    153 	LIST_FOREACH(pnode, &pmp->pmp_pnodelist, pn_entries) {
    154 		if (pnode->pn_cookie == cookie)
    155 			break;
    156 	}
    157 	simple_unlock(&pmp->pmp_lock);
    158 	if (!pnode)
    159 		return NULL;
    160 	vp = pnode->pn_vp;
    161 
    162 	if (pnode->pn_stat & PNODE_INACTIVE) {
    163 		if (vget(vp, LK_EXCLUSIVE | LK_RETRY))
    164 			return NULL;
    165 	} else {
    166 		vref(vp);
    167 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    168 	}
    169 	return vp;
    170 }
    171 
    172 void
    173 puffs_makecn(struct puffs_cn *pcn, const struct componentname *cn)
    174 {
    175 
    176 	pcn->pcn_nameiop = cn->cn_nameiop;
    177 	pcn->pcn_flags = cn->cn_flags;
    178 	pcn->pcn_pid = cn->cn_lwp->l_proc->p_pid;
    179 	puffs_credcvt(&pcn->pcn_cred, cn->cn_cred);
    180 
    181 	(void)memcpy(&pcn->pcn_name, cn->cn_nameptr, cn->cn_namelen);
    182 	pcn->pcn_name[cn->cn_namelen] = '\0';
    183 	pcn->pcn_namelen = cn->cn_namelen;
    184 }
    185 
    186 /*
    187  * Convert given credentials to struct puffs_cred for userspace.
    188  */
    189 void
    190 puffs_credcvt(struct puffs_cred *pcr, const kauth_cred_t cred)
    191 {
    192 
    193 	memset(pcr, 0, sizeof(struct puffs_cred));
    194 
    195 	if (cred == NOCRED || cred == FSCRED) {
    196 		pcr->pcr_type = PUFFCRED_TYPE_INTERNAL;
    197 		if (cred == NOCRED)
    198 			pcr->pcr_internal = PUFFCRED_CRED_NOCRED;
    199 		if (cred == FSCRED)
    200 			pcr->pcr_internal = PUFFCRED_CRED_FSCRED;
    201  	} else {
    202 		pcr->pcr_type = PUFFCRED_TYPE_UUC;
    203 		kauth_cred_to_uucred(&pcr->pcr_uuc, cred);
    204 	}
    205 }
    206 
    207 /*
    208  * Return pid.  In case the operation is coming from within the
    209  * kernel without any process context, borrow the swapper's pid.
    210  */
    211 pid_t
    212 puffs_lwp2pid(struct lwp *l)
    213 {
    214 
    215 	return l ? l->l_proc->p_pid : 0;
    216 }
    217