puffs_subr.c revision 1.22.2.5 1 /* $NetBSD: puffs_subr.c,v 1.22.2.5 2007/06/08 14:15:00 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.5 2007/06/08 14:15:00 ad Exp $");
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/hash.h>
41 #include <sys/kauth.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/poll.h>
46 #include <sys/socketvar.h>
47 #include <sys/vnode.h>
48 #include <sys/proc.h>
49
50 #include <fs/puffs/puffs_msgif.h>
51 #include <fs/puffs/puffs_sys.h>
52
53 #include <miscfs/genfs/genfs_node.h>
54 #include <miscfs/specfs/specdev.h>
55
56 struct pool puffs_pnpool;
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 /* default size */
139 uvm_vnp_setsize(vp, 0);
140
141 /* dances based on vnode type. almost ufs_vinit(), but not quite */
142 switch (type) {
143 case VCHR:
144 case VBLK:
145 /*
146 * replace vnode operation vector with the specops vector.
147 * our user server has very little control over the node
148 * if it decides its a character or block special file
149 */
150 vp->v_op = puffs_specop_p;
151
152 /* do the standard checkalias-dance */
153 if ((nvp = checkalias(vp, rdev, mp)) != NULL) {
154 /*
155 * found: release & unallocate aliased
156 * old (well, actually, new) node
157 */
158 vp->v_op = spec_vnodeop_p;
159 vp->v_flag &= ~VLOCKSWORK;
160 vrele(vp);
161 vgone(vp); /* cya */
162
163 /* init "new" vnode */
164 vp = nvp;
165 vp->v_vnlock = NULL;
166 vp->v_mount = mp;
167 }
168 break;
169
170 case VFIFO:
171 vp->v_op = puffs_fifoop_p;
172 break;
173
174 case VREG:
175 uvm_vnp_setsize(vp, vsize);
176 break;
177
178 case VDIR:
179 case VLNK:
180 case VSOCK:
181 break;
182 default:
183 #ifdef DIAGNOSTIC
184 panic("puffs_getvnode: invalid vtype %d", type);
185 #endif
186 break;
187 }
188
189 pnode = pool_get(&puffs_pnpool, PR_WAITOK);
190 pnode->pn_cookie = cookie;
191 pnode->pn_stat = 0;
192 pnode->pn_refcount = 1;
193
194 mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
195 SLIST_INIT(&pnode->pn_sel.sel_klist);
196 pnode->pn_revents = 0;
197
198 plist = puffs_cookie2hashlist(pmp, cookie);
199 LIST_INSERT_HEAD(plist, pnode, pn_hashent);
200 vp->v_data = pnode;
201 vp->v_type = type;
202 pnode->pn_vp = vp;
203
204 genfs_node_init(vp, &puffs_genfsops);
205 *vpp = vp;
206
207 DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
208 pnode, pnode->pn_cookie));
209
210 return 0;
211 }
212
213 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
214 int
215 puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
216 void *cookie, struct componentname *cnp, enum vtype type, dev_t rdev)
217 {
218 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
219 struct vnode *vp;
220 int error;
221
222 /* userspace probably has this as a NULL op */
223 if (cookie == NULL) {
224 error = EOPNOTSUPP;
225 return error;
226 }
227
228 /*
229 * Check for previous node with the same designation.
230 * Explicitly check the root node cookie, since it might be
231 * reclaimed from the kernel when this check is made.
232 *
233 * XXX: technically this error check should punish the fs,
234 * not the caller.
235 */
236 mutex_enter(&pmp->pmp_lock);
237 if (cookie == pmp->pmp_root_cookie
238 || puffs_cookie2pnode(pmp, cookie) != NULL) {
239 mutex_exit(&pmp->pmp_lock);
240 error = EEXIST;
241 return error;
242 }
243 mutex_exit(&pmp->pmp_lock);
244
245 error = puffs_getvnode(dvp->v_mount, cookie, type, 0, rdev, &vp);
246 if (error)
247 return error;
248
249 vp->v_type = type;
250 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
251 *vpp = vp;
252
253 if ((cnp->cn_flags & MAKEENTRY) && PUFFS_DOCACHE(pmp))
254 cache_enter(dvp, vp, cnp);
255
256 return 0;
257 }
258
259 /*
260 * Release pnode structure which dealing with references to the
261 * puffs_node instead of the vnode. Can't use vref()/vrele() on
262 * the vnode there, since that causes the lovely VOP_INACTIVE(),
263 * which in turn causes the lovely deadlock when called by the one
264 * who is supposed to handle it.
265 */
266 void
267 puffs_releasenode(struct puffs_node *pn)
268 {
269
270 mutex_enter(&pn->pn_mtx);
271 if (--pn->pn_refcount == 0) {
272 mutex_exit(&pn->pn_mtx);
273 mutex_destroy(&pn->pn_mtx);
274 pool_put(&puffs_pnpool, pn);
275 } else {
276 mutex_exit(&pn->pn_mtx);
277 }
278 }
279
280 /*
281 * Add reference to node.
282 * mutex held on entry and return
283 */
284 void
285 puffs_referencenode(struct puffs_node *pn)
286 {
287
288 KASSERT(mutex_owned(&pn->pn_mtx));
289 pn->pn_refcount++;
290 }
291
292 void
293 puffs_putvnode(struct vnode *vp)
294 {
295 struct puffs_mount *pmp;
296 struct puffs_node *pnode;
297
298 pmp = VPTOPUFFSMP(vp);
299 pnode = VPTOPP(vp);
300
301 #ifdef DIAGNOSTIC
302 if (vp->v_tag != VT_PUFFS)
303 panic("puffs_putvnode: %p not a puffs vnode", vp);
304 #endif
305
306 LIST_REMOVE(pnode, pn_hashent);
307 genfs_node_destroy(vp);
308 puffs_releasenode(pnode);
309 vp->v_data = NULL;
310
311 return;
312 }
313
314 static __inline struct puffs_node_hashlist *
315 puffs_cookie2hashlist(struct puffs_mount *pmp, void *cookie)
316 {
317 uint32_t hash;
318
319 hash = hash32_buf(&cookie, sizeof(void *), HASH32_BUF_INIT);
320 return &pmp->pmp_pnodehash[hash % pmp->pmp_npnodehash];
321 }
322
323 /*
324 * Translate cookie to puffs_node. Caller must hold mountpoint
325 * lock and it will be held upon return.
326 */
327 static struct puffs_node *
328 puffs_cookie2pnode(struct puffs_mount *pmp, void *cookie)
329 {
330 struct puffs_node_hashlist *plist;
331 struct puffs_node *pnode;
332
333 plist = puffs_cookie2hashlist(pmp, cookie);
334 LIST_FOREACH(pnode, plist, pn_hashent) {
335 if (pnode->pn_cookie == cookie)
336 break;
337 }
338
339 return pnode;
340 }
341
342 /*
343 * Locate the in-kernel vnode based on the cookie received given
344 * from userspace. Returns a vnode, if found, NULL otherwise.
345 * The parameter "lock" control whether to lock the possible or
346 * not. Locking always might cause us to lock against ourselves
347 * in situations where we want the vnode but don't care for the
348 * vnode lock, e.g. file server issued putpages.
349 */
350 struct vnode *
351 puffs_pnode2vnode(struct puffs_mount *pmp, void *cookie, int lock)
352 {
353 struct puffs_node *pnode;
354 struct vnode *vp;
355 int vgetflags;
356
357 /*
358 * If we're trying to get the root vnode, return it through
359 * puffs_root() to get all the right things set. Lock must
360 * be set, since VFS_ROOT() always locks the returned vnode.
361 */
362 if (cookie == pmp->pmp_root_cookie) {
363 if (!lock)
364 return NULL;
365 if (VFS_ROOT(pmp->pmp_mp, &vp))
366 return NULL;
367
368 return vp;
369 }
370
371 vgetflags = LK_INTERLOCK;
372 if (lock)
373 vgetflags |= LK_EXCLUSIVE | LK_RETRY;
374
375 mutex_enter(&pmp->pmp_lock);
376 pnode = puffs_cookie2pnode(pmp, cookie);
377
378 if (pnode == NULL) {
379 mutex_exit(&pmp->pmp_lock);
380 return NULL;
381 }
382 vp = pnode->pn_vp;
383
384 mutex_enter(&vp->v_interlock);
385 mutex_exit(&pmp->pmp_lock);
386
387 if (vget(vp, vgetflags))
388 return NULL;
389
390 return vp;
391 }
392
393 void
394 puffs_makecn(struct puffs_kcn *pkcn, const struct componentname *cn)
395 {
396
397 pkcn->pkcn_nameiop = cn->cn_nameiop;
398 pkcn->pkcn_flags = cn->cn_flags;
399 pkcn->pkcn_pid = cn->cn_lwp->l_proc->p_pid;
400 puffs_credcvt(&pkcn->pkcn_cred, cn->cn_cred);
401
402 (void)memcpy(&pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
403 pkcn->pkcn_name[cn->cn_namelen] = '\0';
404 pkcn->pkcn_namelen = cn->cn_namelen;
405 }
406
407 /*
408 * Convert given credentials to struct puffs_cred for userspace.
409 */
410 void
411 puffs_credcvt(struct puffs_cred *pcr, const kauth_cred_t cred)
412 {
413
414 memset(pcr, 0, sizeof(struct puffs_cred));
415
416 if (cred == NOCRED || cred == FSCRED) {
417 pcr->pcr_type = PUFFCRED_TYPE_INTERNAL;
418 if (cred == NOCRED)
419 pcr->pcr_internal = PUFFCRED_CRED_NOCRED;
420 if (cred == FSCRED)
421 pcr->pcr_internal = PUFFCRED_CRED_FSCRED;
422 } else {
423 pcr->pcr_type = PUFFCRED_TYPE_UUC;
424 kauth_cred_to_uucred(&pcr->pcr_uuc, cred);
425 }
426 }
427
428 /*
429 * Return pid. In case the operation is coming from within the
430 * kernel without any process context, borrow the swapper's pid.
431 */
432 pid_t
433 puffs_lwp2pid(struct lwp *l)
434 {
435
436 return l ? l->l_proc->p_pid : 0;
437 }
438
439
440 static void
441 puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
442 int flags)
443 {
444
445 *eobp = size;
446 }
447
448 static void
449 puffs_gop_markupdate(struct vnode *vp, int flags)
450 {
451 int uflags = 0;
452
453 if (flags & GOP_UPDATE_ACCESSED)
454 uflags |= PUFFS_UPDATEATIME;
455 if (flags & GOP_UPDATE_MODIFIED)
456 uflags |= PUFFS_UPDATEMTIME;
457
458 puffs_updatenode(vp, uflags);
459 }
460
461 void
462 puffs_updatenode(struct vnode *vp, int flags)
463 {
464 struct puffs_node *pn;
465 struct timespec ts;
466
467 if (flags == 0)
468 return;
469
470 pn = VPTOPP(vp);
471 nanotime(&ts);
472
473 if (flags & PUFFS_UPDATEATIME) {
474 pn->pn_mc_atime = ts;
475 pn->pn_stat |= PNODE_METACACHE_ATIME;
476 }
477 if (flags & PUFFS_UPDATECTIME) {
478 pn->pn_mc_ctime = ts;
479 pn->pn_stat |= PNODE_METACACHE_CTIME;
480 }
481 if (flags & PUFFS_UPDATEMTIME) {
482 pn->pn_mc_mtime = ts;
483 pn->pn_stat |= PNODE_METACACHE_MTIME;
484 }
485 if (flags & PUFFS_UPDATESIZE) {
486 pn->pn_mc_size = vp->v_size;
487 pn->pn_stat |= PNODE_METACACHE_SIZE;
488 }
489 }
490
491 void
492 puffs_updatevpsize(struct vnode *vp)
493 {
494 struct vattr va;
495
496 if (VOP_GETATTR(vp, &va, FSCRED, NULL))
497 return;
498
499 if (va.va_size != VNOVAL)
500 vp->v_size = va.va_size;
501 }
502
503 void
504 puffs_parkdone_asyncbioread(struct puffs_req *preq, void *arg)
505 {
506 struct puffs_vnreq_read *read_argp = (void *)preq;
507 struct buf *bp = arg;
508 size_t moved;
509
510 bp->b_error = preq->preq_rv;
511 if (bp->b_error == 0) {
512 moved = bp->b_bcount - read_argp->pvnr_resid;
513 bp->b_resid = read_argp->pvnr_resid;
514
515 memcpy(bp->b_data, read_argp->pvnr_data, moved);
516 } else {
517 bp->b_flags |= B_ERROR;
518 }
519
520 biodone(bp);
521 free(preq, M_PUFFS);
522 }
523
524 void
525 puffs_parkdone_poll(struct puffs_req *preq, void *arg)
526 {
527 struct puffs_vnreq_poll *poll_argp = (void *)preq;
528 struct puffs_node *pn = arg;
529 int revents;
530
531 if (preq->preq_rv == 0)
532 revents = poll_argp->pvnr_events;
533 else
534 revents = POLLERR;
535
536 mutex_enter(&pn->pn_mtx);
537 pn->pn_revents |= revents;
538 mutex_exit(&pn->pn_mtx);
539
540 selnotify(&pn->pn_sel, 0);
541 free(preq, M_PUFFS);
542
543 puffs_releasenode(pn);
544 }
545
546 void
547 puffs_mp_reference(struct puffs_mount *pmp)
548 {
549
550 KASSERT(mutex_owned(&pmp->pmp_lock));
551 pmp->pmp_refcount++;
552 }
553
554 void
555 puffs_mp_release(struct puffs_mount *pmp)
556 {
557
558 KASSERT(mutex_owned(&pmp->pmp_lock));
559 if (--pmp->pmp_refcount == 0)
560 cv_broadcast(&pmp->pmp_refcount_cv);
561 }
562