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