puffs_subr.c revision 1.5 1 1.5 pooka /* $NetBSD: puffs_subr.c,v 1.5 2006/10/27 12:25:16 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2005, 2006 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Google Summer of Code program and the Ulla Tuominen Foundation.
8 1.1 pooka * The Google SoC project was mentored by Bill Studenmund.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka * 3. The name of the company nor the name of the author may be used to
19 1.1 pooka * endorse or promote products derived from this software without specific
20 1.1 pooka * prior written permission.
21 1.1 pooka *
22 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 pooka * SUCH DAMAGE.
33 1.1 pooka */
34 1.1 pooka
35 1.1 pooka #include <sys/cdefs.h>
36 1.5 pooka __KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.5 2006/10/27 12:25:16 pooka Exp $");
37 1.1 pooka
38 1.1 pooka #include <sys/param.h>
39 1.1 pooka #include <sys/conf.h>
40 1.1 pooka #include <sys/malloc.h>
41 1.1 pooka #include <sys/mount.h>
42 1.1 pooka #include <sys/socketvar.h>
43 1.1 pooka #include <sys/vnode.h>
44 1.1 pooka #include <sys/kauth.h>
45 1.1 pooka #include <sys/namei.h>
46 1.1 pooka
47 1.1 pooka #include <fs/puffs/puffs_msgif.h>
48 1.1 pooka #include <fs/puffs/puffs_sys.h>
49 1.1 pooka
50 1.4 pooka #include <miscfs/specfs/specdev.h>
51 1.4 pooka
52 1.1 pooka POOL_INIT(puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0, "puffspnpl",
53 1.1 pooka &pool_allocator_nointr);
54 1.1 pooka
55 1.1 pooka /*
56 1.1 pooka * Grab a vnode, intialize all the puffs-dependant stuff.
57 1.1 pooka */
58 1.1 pooka int
59 1.4 pooka puffs_getvnode(struct mount *mp, void *cookie, enum vtype type,
60 1.4 pooka dev_t rdev, struct vnode **vpp)
61 1.1 pooka {
62 1.1 pooka struct puffs_mount *pmp;
63 1.4 pooka struct vnode *vp, *nvp;
64 1.1 pooka struct puffs_node *pnode;
65 1.1 pooka int error;
66 1.1 pooka
67 1.1 pooka pmp = MPTOPUFFSMP(mp);
68 1.1 pooka
69 1.2 pooka /*
70 1.2 pooka * XXX: there is a deadlock condition between vfs_busy() and
71 1.2 pooka * vnode locks. For an unmounting file system the mountpoint
72 1.2 pooka * is frozen, but in unmount(FORCE) vflush() wants to access all
73 1.2 pooka * of the vnodes. If we are here waiting for the mountpoint
74 1.2 pooka * lock while holding on to a vnode lock, well, we ain't
75 1.2 pooka * just pining for the fjords anymore. If we release the
76 1.2 pooka * vnode lock, we will be in the situation "mount point
77 1.2 pooka * is dying" and panic() will ensue in insmntque. So as a
78 1.2 pooka * temporary workaround, get a vnode without putting it on
79 1.2 pooka * the mount point list, check if mount point is still alive
80 1.2 pooka * and kicking and only then add the vnode to the list.
81 1.2 pooka */
82 1.2 pooka error = getnewvnode(VT_PUFFS, NULL, puffs_vnodeop_p, &vp);
83 1.2 pooka if (error)
84 1.1 pooka return error;
85 1.2 pooka vp->v_vnlock = NULL;
86 1.4 pooka vp->v_type = type;
87 1.2 pooka
88 1.2 pooka /*
89 1.2 pooka * Check what mount point isn't going away. This will work
90 1.2 pooka * until we decide to remove biglock or make the kernel
91 1.2 pooka * preemptive. But hopefully the real problem will be fixed
92 1.2 pooka * by then.
93 1.2 pooka *
94 1.2 pooka * XXX: yes, should call vfs_busy(), but thar be rabbits with
95 1.2 pooka * vicious streaks a mile wide ...
96 1.2 pooka */
97 1.2 pooka if (mp->mnt_iflag & IMNT_UNMOUNT) {
98 1.2 pooka DPRINTF(("puffs_getvnode: mp %p unmount, unable to create "
99 1.2 pooka "vnode for cookie %p\n", mp, cookie));
100 1.2 pooka ungetnewvnode(vp);
101 1.2 pooka return ENXIO;
102 1.1 pooka }
103 1.1 pooka
104 1.2 pooka /* So it's not dead yet.. good.. inform new vnode of its master */
105 1.2 pooka simple_lock(&mntvnode_slock);
106 1.2 pooka if (TAILQ_EMPTY(&mp->mnt_vnodelist))
107 1.2 pooka TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
108 1.2 pooka else
109 1.2 pooka TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
110 1.2 pooka simple_unlock(&mntvnode_slock);
111 1.2 pooka vp->v_mount = mp;
112 1.2 pooka
113 1.4 pooka /*
114 1.4 pooka * clerical tasks & footwork
115 1.4 pooka */
116 1.4 pooka
117 1.4 pooka /* dances based on vnode type. almost ufs_vinit(), but not quite */
118 1.4 pooka switch (type) {
119 1.4 pooka case VCHR:
120 1.4 pooka case VBLK:
121 1.4 pooka /*
122 1.4 pooka * replace vnode operation vector with the specops vector.
123 1.4 pooka * our user server has very little control over the node
124 1.4 pooka * if it decides its a character or block special file
125 1.4 pooka */
126 1.4 pooka vp->v_op = puffs_specop_p;
127 1.4 pooka
128 1.4 pooka /* do the standard checkalias-dance */
129 1.4 pooka if ((nvp = checkalias(vp, rdev, mp)) != NULL) {
130 1.4 pooka /*
131 1.4 pooka * found, release & unallocate aliased
132 1.4 pooka * old (well, actually, new) node
133 1.4 pooka */
134 1.4 pooka VOP_UNLOCK(vp, 0);
135 1.4 pooka vp->v_op = spec_vnodeop_p;
136 1.4 pooka vp->v_flag &= ~VLOCKSWORK;
137 1.4 pooka vrele(vp);
138 1.4 pooka vgone(vp); /* cya */
139 1.4 pooka
140 1.4 pooka /* init "new" vnode */
141 1.4 pooka vp = nvp;
142 1.4 pooka vp->v_vnlock = NULL;
143 1.4 pooka vp->v_mount = mp;
144 1.4 pooka vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
145 1.4 pooka }
146 1.4 pooka break;
147 1.5 pooka case VFIFO:
148 1.5 pooka vp->v_op = puffs_fifoop_p;
149 1.5 pooka break;
150 1.5 pooka case VDIR:
151 1.5 pooka case VREG:
152 1.5 pooka case VLNK:
153 1.5 pooka case VSOCK:
154 1.5 pooka break;
155 1.4 pooka default:
156 1.5 pooka #ifdef DIAGNOSTIC
157 1.5 pooka panic("puffs_getvnode: invalid vtype %d", type);
158 1.5 pooka #endif
159 1.4 pooka break;
160 1.4 pooka }
161 1.4 pooka
162 1.2 pooka pnode = pool_get(&puffs_pnpool, PR_WAITOK);
163 1.1 pooka pnode->pn_cookie = cookie;
164 1.1 pooka pnode->pn_stat = 0;
165 1.1 pooka LIST_INSERT_HEAD(&pmp->pmp_pnodelist, pnode, pn_entries);
166 1.1 pooka vp->v_data = pnode;
167 1.4 pooka vp->v_type = type;
168 1.1 pooka pnode->pn_vp = vp;
169 1.1 pooka
170 1.1 pooka *vpp = vp;
171 1.1 pooka
172 1.1 pooka DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
173 1.1 pooka pnode, pnode->pn_cookie));
174 1.1 pooka
175 1.1 pooka return 0;
176 1.1 pooka }
177 1.1 pooka
178 1.1 pooka /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
179 1.1 pooka int
180 1.1 pooka puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
181 1.4 pooka void *cookie, struct componentname *cnp, enum vtype type, dev_t rdev)
182 1.1 pooka {
183 1.1 pooka struct vnode *vp;
184 1.1 pooka int error;
185 1.1 pooka
186 1.1 pooka /* userspace probably has this as a NULL op */
187 1.1 pooka if (cookie == NULL) {
188 1.1 pooka error = EOPNOTSUPP;
189 1.3 pooka return error;
190 1.1 pooka }
191 1.1 pooka
192 1.4 pooka error = puffs_getvnode(dvp->v_mount, cookie, type, rdev, &vp);
193 1.1 pooka if (error)
194 1.3 pooka return error;
195 1.1 pooka
196 1.1 pooka vp->v_type = type;
197 1.1 pooka vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
198 1.1 pooka *vpp = vp;
199 1.1 pooka
200 1.3 pooka return 0;
201 1.1 pooka }
202 1.1 pooka
203 1.1 pooka void
204 1.1 pooka puffs_putvnode(struct vnode *vp)
205 1.1 pooka {
206 1.1 pooka struct puffs_mount *pmp;
207 1.1 pooka struct puffs_node *pnode;
208 1.1 pooka
209 1.1 pooka pmp = VPTOPUFFSMP(vp);
210 1.1 pooka pnode = VPTOPP(vp);
211 1.1 pooka
212 1.1 pooka #ifdef DIAGNOSTIC
213 1.1 pooka if (vp->v_tag != VT_PUFFS)
214 1.1 pooka panic("puffs_putvnode: %p not a puffs vnode", vp);
215 1.1 pooka #endif
216 1.1 pooka
217 1.1 pooka LIST_REMOVE(pnode, pn_entries);
218 1.1 pooka pool_put(&puffs_pnpool, vp->v_data);
219 1.1 pooka vp->v_data = NULL;
220 1.1 pooka
221 1.1 pooka return;
222 1.1 pooka }
223 1.1 pooka
224 1.1 pooka /*
225 1.1 pooka * Locate the in-kernel vnode based on the cookie received given
226 1.1 pooka * from userspace. Returns a locked & referenced vnode, if found,
227 1.1 pooka * NULL otherwise.
228 1.1 pooka *
229 1.1 pooka * XXX: lists, although lookup cache mostly shields us from this
230 1.1 pooka */
231 1.1 pooka struct vnode *
232 1.1 pooka puffs_pnode2vnode(struct puffs_mount *pmp, void *cookie)
233 1.1 pooka {
234 1.1 pooka struct puffs_node *pnode;
235 1.1 pooka struct vnode *vp;
236 1.1 pooka
237 1.1 pooka simple_lock(&pmp->pmp_lock);
238 1.1 pooka LIST_FOREACH(pnode, &pmp->pmp_pnodelist, pn_entries) {
239 1.1 pooka if (pnode->pn_cookie == cookie)
240 1.1 pooka break;
241 1.1 pooka }
242 1.1 pooka simple_unlock(&pmp->pmp_lock);
243 1.1 pooka if (!pnode)
244 1.1 pooka return NULL;
245 1.1 pooka vp = pnode->pn_vp;
246 1.1 pooka
247 1.1 pooka if (pnode->pn_stat & PNODE_INACTIVE) {
248 1.1 pooka if (vget(vp, LK_EXCLUSIVE | LK_RETRY))
249 1.1 pooka return NULL;
250 1.1 pooka } else {
251 1.1 pooka vref(vp);
252 1.1 pooka vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
253 1.1 pooka }
254 1.1 pooka return vp;
255 1.1 pooka }
256 1.1 pooka
257 1.1 pooka void
258 1.1 pooka puffs_makecn(struct puffs_cn *pcn, const struct componentname *cn)
259 1.1 pooka {
260 1.1 pooka
261 1.1 pooka pcn->pcn_nameiop = cn->cn_nameiop;
262 1.1 pooka pcn->pcn_flags = cn->cn_flags;
263 1.1 pooka pcn->pcn_pid = cn->cn_lwp->l_proc->p_pid;
264 1.1 pooka puffs_credcvt(&pcn->pcn_cred, cn->cn_cred);
265 1.1 pooka
266 1.1 pooka (void)memcpy(&pcn->pcn_name, cn->cn_nameptr, cn->cn_namelen);
267 1.1 pooka pcn->pcn_name[cn->cn_namelen] = '\0';
268 1.1 pooka pcn->pcn_namelen = cn->cn_namelen;
269 1.1 pooka }
270 1.1 pooka
271 1.1 pooka /*
272 1.1 pooka * Convert given credentials to struct puffs_cred for userspace.
273 1.1 pooka */
274 1.1 pooka void
275 1.1 pooka puffs_credcvt(struct puffs_cred *pcr, const kauth_cred_t cred)
276 1.1 pooka {
277 1.1 pooka
278 1.1 pooka memset(pcr, 0, sizeof(struct puffs_cred));
279 1.1 pooka
280 1.1 pooka if (cred == NOCRED || cred == FSCRED) {
281 1.1 pooka pcr->pcr_type = PUFFCRED_TYPE_INTERNAL;
282 1.1 pooka if (cred == NOCRED)
283 1.1 pooka pcr->pcr_internal = PUFFCRED_CRED_NOCRED;
284 1.1 pooka if (cred == FSCRED)
285 1.1 pooka pcr->pcr_internal = PUFFCRED_CRED_FSCRED;
286 1.1 pooka } else {
287 1.1 pooka pcr->pcr_type = PUFFCRED_TYPE_UUC;
288 1.1 pooka kauth_cred_to_uucred(&pcr->pcr_uuc, cred);
289 1.1 pooka }
290 1.1 pooka }
291 1.1 pooka
292 1.1 pooka /*
293 1.1 pooka * Return pid. In case the operation is coming from within the
294 1.1 pooka * kernel without any process context, borrow the swapper's pid.
295 1.1 pooka */
296 1.1 pooka pid_t
297 1.1 pooka puffs_lwp2pid(struct lwp *l)
298 1.1 pooka {
299 1.1 pooka
300 1.1 pooka return l ? l->l_proc->p_pid : 0;
301 1.1 pooka }
302