puffs_subr.c revision 1.46 1 /* $NetBSD: puffs_subr.c,v 1.46 2007/09/24 19:15:42 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.46 2007/09/24 19:15:42 pooka Exp $");
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/hash.h>
38 #include <sys/kauth.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/poll.h>
43 #include <sys/socketvar.h>
44 #include <sys/vnode.h>
45 #include <sys/proc.h>
46
47 #include <fs/puffs/puffs_msgif.h>
48 #include <fs/puffs/puffs_sys.h>
49
50 #include <miscfs/genfs/genfs_node.h>
51 #include <miscfs/specfs/specdev.h>
52
53 struct pool puffs_pnpool;
54
55 #ifdef PUFFSDEBUG
56 int puffsdebug;
57 #endif
58
59 static __inline struct puffs_node_hashlist
60 *puffs_cookie2hashlist(struct puffs_mount *, void *);
61 static struct puffs_node *puffs_cookie2pnode(struct puffs_mount *, void *);
62
63 static void puffs_gop_size(struct vnode *, off_t, off_t *, int);
64 static void puffs_gop_markupdate(struct vnode *, int);
65
66 static const struct genfs_ops puffs_genfsops = {
67 .gop_size = puffs_gop_size,
68 .gop_write = genfs_gop_write,
69 .gop_markupdate = puffs_gop_markupdate,
70 #if 0
71 .gop_alloc, should ask userspace
72 #endif
73 };
74
75 /*
76 * Grab a vnode, intialize all the puffs-dependant stuff.
77 */
78 int
79 puffs_getvnode(struct mount *mp, void *cookie, enum vtype type,
80 voff_t vsize, dev_t rdev, struct vnode **vpp)
81 {
82 struct puffs_mount *pmp;
83 struct vnode *vp, *nvp;
84 struct puffs_node *pnode;
85 struct puffs_node_hashlist *plist;
86 int error;
87
88 if (type <= VNON || type >= VBAD)
89 return EINVAL;
90 if (vsize == VSIZENOTSET)
91 return EINVAL;
92
93 pmp = MPTOPUFFSMP(mp);
94
95 /*
96 * XXX: there is a deadlock condition between vfs_busy() and
97 * vnode locks. For an unmounting file system the mountpoint
98 * is frozen, but in unmount(FORCE) vflush() wants to access all
99 * of the vnodes. If we are here waiting for the mountpoint
100 * lock while holding on to a vnode lock, well, we ain't
101 * just pining for the fjords anymore. If we release the
102 * vnode lock, we will be in the situation "mount point
103 * is dying" and panic() will ensue in insmntque. So as a
104 * temporary workaround, get a vnode without putting it on
105 * the mount point list, check if mount point is still alive
106 * and kicking and only then add the vnode to the list.
107 */
108 error = getnewvnode(VT_PUFFS, NULL, puffs_vnodeop_p, &vp);
109 if (error)
110 return error;
111 vp->v_vnlock = NULL;
112 vp->v_type = type;
113
114 /*
115 * Check what mount point isn't going away. This will work
116 * until we decide to remove biglock or make the kernel
117 * preemptive. But hopefully the real problem will be fixed
118 * by then.
119 *
120 * XXX: yes, should call vfs_busy(), but thar be rabbits with
121 * vicious streaks a mile wide ...
122 *
123 * XXX: there is a transient failure here: if someone is unmounting
124 * the file system but doesn't succeed (due to it being busy),
125 * we incorrectly fail new vnode allocation. This is *very*
126 * hard to fix with the current structure of file system unmounting.
127 */
128 if (mp->mnt_iflag & IMNT_UNMOUNT) {
129 DPRINTF(("puffs_getvnode: mp %p unmount, unable to create "
130 "vnode for cookie %p\n", mp, cookie));
131 ungetnewvnode(vp);
132 return ENXIO;
133 }
134
135 /* So it's not dead yet.. good.. inform new vnode of its master */
136 simple_lock(&mntvnode_slock);
137 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
138 simple_unlock(&mntvnode_slock);
139 vp->v_mount = mp;
140
141 /*
142 * clerical tasks & footwork
143 */
144
145 /* default size */
146 uvm_vnp_setsize(vp, 0);
147
148 /* dances based on vnode type. almost ufs_vinit(), but not quite */
149 switch (type) {
150 case VCHR:
151 case VBLK:
152 /*
153 * replace vnode operation vector with the specops vector.
154 * our user server has very little control over the node
155 * if it decides its a character or block special file
156 */
157 vp->v_op = puffs_specop_p;
158
159 /* do the standard checkalias-dance */
160 if ((nvp = checkalias(vp, rdev, mp)) != NULL) {
161 /*
162 * found: release & unallocate aliased
163 * old (well, actually, new) node
164 */
165 vp->v_op = spec_vnodeop_p;
166 vp->v_flag &= ~VLOCKSWORK;
167 vrele(vp);
168 vgone(vp); /* cya */
169
170 /* init "new" vnode */
171 vp = nvp;
172 vp->v_vnlock = NULL;
173 vp->v_mount = mp;
174 }
175 break;
176
177 case VFIFO:
178 vp->v_op = puffs_fifoop_p;
179 break;
180
181 case VREG:
182 uvm_vnp_setsize(vp, vsize);
183 break;
184
185 case VDIR:
186 case VLNK:
187 case VSOCK:
188 break;
189 default:
190 #ifdef DIAGNOSTIC
191 panic("puffs_getvnode: invalid vtype %d", type);
192 #endif
193 break;
194 }
195
196 pnode = pool_get(&puffs_pnpool, PR_WAITOK);
197 memset(pnode, 0, sizeof(struct puffs_node));
198
199 pnode->pn_cookie = cookie;
200 pnode->pn_refcount = 1;
201
202 mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
203 SLIST_INIT(&pnode->pn_sel.sel_klist);
204
205 plist = puffs_cookie2hashlist(pmp, cookie);
206 LIST_INSERT_HEAD(plist, pnode, pn_hashent);
207 vp->v_data = pnode;
208 vp->v_type = type;
209 pnode->pn_vp = vp;
210 pnode->pn_serversize = vsize;
211
212 genfs_node_init(vp, &puffs_genfsops);
213 *vpp = vp;
214
215 DPRINTF(("new vnode at %p, pnode %p, cookie %p\n", vp,
216 pnode, pnode->pn_cookie));
217
218 return 0;
219 }
220
221 /* new node creating for creative vop ops (create, symlink, mkdir, mknod) */
222 int
223 puffs_newnode(struct mount *mp, struct vnode *dvp, struct vnode **vpp,
224 void *cookie, struct componentname *cnp, enum vtype type, dev_t rdev)
225 {
226 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
227 struct vnode *vp;
228 int error;
229
230 /* userspace probably has this as a NULL op */
231 if (cookie == NULL) {
232 error = EOPNOTSUPP;
233 return error;
234 }
235
236 /*
237 * Check for previous node with the same designation.
238 * Explicitly check the root node cookie, since it might be
239 * reclaimed from the kernel when this check is made.
240 *
241 * XXX: technically this error check should punish the fs,
242 * not the caller.
243 */
244 mutex_enter(&pmp->pmp_lock);
245 if (cookie == pmp->pmp_root_cookie
246 || puffs_cookie2pnode(pmp, cookie) != NULL) {
247 mutex_exit(&pmp->pmp_lock);
248 error = EEXIST;
249 return error;
250 }
251 /*
252 * XXX: there is a race here. Nothing prevents another cookie
253 * from being inserted. Solution: insert the cookie and back
254 * it out in case of failure?
255 */
256 mutex_exit(&pmp->pmp_lock);
257
258 error = puffs_getvnode(dvp->v_mount, cookie, type, 0, rdev, &vp);
259 if (error)
260 return error;
261
262 vp->v_type = type;
263 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
264 *vpp = vp;
265
266 if ((cnp->cn_flags & MAKEENTRY) && PUFFS_USE_NAMECACHE(pmp))
267 cache_enter(dvp, vp, cnp);
268
269 return 0;
270 }
271
272 /*
273 * Release pnode structure which dealing with references to the
274 * puffs_node instead of the vnode. Can't use vref()/vrele() on
275 * the vnode there, since that causes the lovely VOP_INACTIVE(),
276 * which in turn causes the lovely deadlock when called by the one
277 * who is supposed to handle it.
278 */
279 void
280 puffs_releasenode(struct puffs_node *pn)
281 {
282
283 mutex_enter(&pn->pn_mtx);
284 if (--pn->pn_refcount == 0) {
285 mutex_exit(&pn->pn_mtx);
286 mutex_destroy(&pn->pn_mtx);
287 pool_put(&puffs_pnpool, pn);
288 } else {
289 mutex_exit(&pn->pn_mtx);
290 }
291 }
292
293 /*
294 * Add reference to node.
295 * mutex held on entry and return
296 */
297 void
298 puffs_referencenode(struct puffs_node *pn)
299 {
300
301 KASSERT(mutex_owned(&pn->pn_mtx));
302 pn->pn_refcount++;
303 }
304
305 void
306 puffs_putvnode(struct vnode *vp)
307 {
308 struct puffs_mount *pmp;
309 struct puffs_node *pnode;
310
311 pmp = VPTOPUFFSMP(vp);
312 pnode = VPTOPP(vp);
313
314 #ifdef DIAGNOSTIC
315 if (vp->v_tag != VT_PUFFS)
316 panic("puffs_putvnode: %p not a puffs vnode", vp);
317 #endif
318
319 LIST_REMOVE(pnode, pn_hashent);
320 genfs_node_destroy(vp);
321 puffs_releasenode(pnode);
322 vp->v_data = NULL;
323
324 return;
325 }
326
327 static __inline struct puffs_node_hashlist *
328 puffs_cookie2hashlist(struct puffs_mount *pmp, void *cookie)
329 {
330 uint32_t hash;
331
332 hash = hash32_buf(&cookie, sizeof(void *), HASH32_BUF_INIT);
333 return &pmp->pmp_pnodehash[hash % pmp->pmp_npnodehash];
334 }
335
336 /*
337 * Translate cookie to puffs_node. Caller must hold mountpoint
338 * lock and it will be held upon return.
339 */
340 static struct puffs_node *
341 puffs_cookie2pnode(struct puffs_mount *pmp, void *cookie)
342 {
343 struct puffs_node_hashlist *plist;
344 struct puffs_node *pnode;
345
346 plist = puffs_cookie2hashlist(pmp, cookie);
347 LIST_FOREACH(pnode, plist, pn_hashent) {
348 if (pnode->pn_cookie == cookie)
349 break;
350 }
351
352 return pnode;
353 }
354
355 /*
356 * Make sure root vnode exists and reference it. Does NOT lock.
357 */
358 static int
359 puffs_makeroot(struct puffs_mount *pmp)
360 {
361 struct vnode *vp;
362 int rv;
363
364 /*
365 * pmp_lock must be held if vref()'ing or vrele()'ing the
366 * root vnode. the latter is controlled by puffs_inactive().
367 *
368 * pmp_root is set here and cleared in puffs_reclaim().
369 */
370 retry:
371 mutex_enter(&pmp->pmp_lock);
372 vp = pmp->pmp_root;
373 if (vp) {
374 simple_lock(&vp->v_interlock);
375 mutex_exit(&pmp->pmp_lock);
376 if (vget(vp, LK_INTERLOCK) == 0)
377 return 0;
378 } else
379 mutex_exit(&pmp->pmp_lock);
380
381 /*
382 * So, didn't have the magic root vnode available.
383 * No matter, grab another an stuff it with the cookie.
384 */
385 if ((rv = puffs_getvnode(pmp->pmp_mp, pmp->pmp_root_cookie,
386 pmp->pmp_root_vtype, pmp->pmp_root_vsize, pmp->pmp_root_rdev, &vp)))
387 return rv;
388
389 /*
390 * Someone magically managed to race us into puffs_getvnode?
391 * Put our previous new vnode back and retry.
392 */
393 mutex_enter(&pmp->pmp_lock);
394 if (pmp->pmp_root) {
395 mutex_exit(&pmp->pmp_lock);
396 puffs_putvnode(vp);
397 goto retry;
398 }
399
400 /* store cache */
401 vp->v_flag = VROOT;
402 pmp->pmp_root = vp;
403 mutex_exit(&pmp->pmp_lock);
404
405 return 0;
406 }
407
408 /*
409 * Locate the in-kernel vnode based on the cookie received given
410 * from userspace. Returns a vnode, if found, NULL otherwise.
411 * The parameter "lock" control whether to lock the possible or
412 * not. Locking always might cause us to lock against ourselves
413 * in situations where we want the vnode but don't care for the
414 * vnode lock, e.g. file server issued putpages.
415 */
416 int
417 puffs_pnode2vnode(struct puffs_mount *pmp, void *cookie, int lock,
418 struct vnode **vpp)
419 {
420 struct puffs_node *pnode;
421 struct vnode *vp;
422 int vgetflags, rv;
423
424 /*
425 * Handle root in a special manner, since we want to make sure
426 * pmp_root is properly set.
427 */
428 if (cookie == pmp->pmp_root_cookie) {
429 if ((rv = puffs_makeroot(pmp)))
430 return rv;
431 if (lock)
432 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
433
434 *vpp = pmp->pmp_root;
435 return 0;
436 }
437
438 mutex_enter(&pmp->pmp_lock);
439 pnode = puffs_cookie2pnode(pmp, cookie);
440
441 if (pnode == NULL) {
442 mutex_exit(&pmp->pmp_lock);
443 return ENOENT;
444 }
445
446 vp = pnode->pn_vp;
447 simple_lock(&vp->v_interlock);
448 mutex_exit(&pmp->pmp_lock);
449
450 vgetflags = LK_INTERLOCK;
451 if (lock)
452 vgetflags |= LK_EXCLUSIVE | LK_RETRY;
453 if ((rv = vget(vp, vgetflags)))
454 return rv;
455
456 *vpp = vp;
457 return 0;
458 }
459
460 void
461 puffs_makecn(struct puffs_kcn *pkcn, struct puffs_kcred *pkcr,
462 struct puffs_kcid *pkcid, const struct componentname *cn, int full)
463 {
464
465 pkcn->pkcn_nameiop = cn->cn_nameiop;
466 pkcn->pkcn_flags = cn->cn_flags;
467 puffs_cidcvt(pkcid, cn->cn_lwp);
468
469 if (full) {
470 (void)strcpy(pkcn->pkcn_name, cn->cn_nameptr);
471 } else {
472 (void)memcpy(pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
473 pkcn->pkcn_name[cn->cn_namelen] = '\0';
474 }
475 pkcn->pkcn_namelen = cn->cn_namelen;
476 pkcn->pkcn_consume = 0;
477
478 puffs_credcvt(pkcr, cn->cn_cred);
479 }
480
481 /*
482 * Convert given credentials to struct puffs_kcred for userspace.
483 */
484 void
485 puffs_credcvt(struct puffs_kcred *pkcr, const kauth_cred_t cred)
486 {
487
488 memset(pkcr, 0, sizeof(struct puffs_kcred));
489
490 if (cred == NOCRED || cred == FSCRED) {
491 pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL;
492 if (cred == NOCRED)
493 pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED;
494 if (cred == FSCRED)
495 pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED;
496 } else {
497 pkcr->pkcr_type = PUFFCRED_TYPE_UUC;
498 kauth_cred_to_uucred(&pkcr->pkcr_uuc, cred);
499 }
500 }
501
502 void
503 puffs_cidcvt(struct puffs_kcid *pkcid, const struct lwp *l)
504 {
505
506 if (l) {
507 pkcid->pkcid_type = PUFFCID_TYPE_REAL;
508 pkcid->pkcid_pid = l->l_proc->p_pid;
509 pkcid->pkcid_lwpid = l->l_lid;
510 } else {
511 pkcid->pkcid_type = PUFFCID_TYPE_FAKE;
512 pkcid->pkcid_pid = 0;
513 pkcid->pkcid_lwpid = 0;
514 }
515 }
516
517 static void
518 puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
519 int flags)
520 {
521
522 *eobp = size;
523 }
524
525 static void
526 puffs_gop_markupdate(struct vnode *vp, int flags)
527 {
528 int uflags = 0;
529
530 if (flags & GOP_UPDATE_ACCESSED)
531 uflags |= PUFFS_UPDATEATIME;
532 if (flags & GOP_UPDATE_MODIFIED)
533 uflags |= PUFFS_UPDATEMTIME;
534
535 puffs_updatenode(vp, uflags);
536 }
537
538 void
539 puffs_updatenode(struct vnode *vp, int flags)
540 {
541 struct puffs_node *pn;
542 struct timespec ts;
543
544 if (flags == 0)
545 return;
546
547 pn = VPTOPP(vp);
548 nanotime(&ts);
549
550 if (flags & PUFFS_UPDATEATIME) {
551 pn->pn_mc_atime = ts;
552 pn->pn_stat |= PNODE_METACACHE_ATIME;
553 }
554 if (flags & PUFFS_UPDATECTIME) {
555 pn->pn_mc_ctime = ts;
556 pn->pn_stat |= PNODE_METACACHE_CTIME;
557 }
558 if (flags & PUFFS_UPDATEMTIME) {
559 pn->pn_mc_mtime = ts;
560 pn->pn_stat |= PNODE_METACACHE_MTIME;
561 }
562 if (flags & PUFFS_UPDATESIZE) {
563 pn->pn_mc_size = vp->v_size;
564 pn->pn_stat |= PNODE_METACACHE_SIZE;
565 }
566 }
567
568 void
569 puffs_parkdone_asyncbioread(struct puffs_req *preq, void *arg)
570 {
571 struct puffs_vnreq_read *read_argp = (void *)preq;
572 struct buf *bp = arg;
573 size_t moved;
574
575 bp->b_error = preq->preq_rv;
576 if (bp->b_error == 0) {
577 moved = bp->b_bcount - read_argp->pvnr_resid;
578 bp->b_resid = read_argp->pvnr_resid;
579
580 memcpy(bp->b_data, read_argp->pvnr_data, moved);
581 }
582
583 biodone(bp);
584 free(preq, M_PUFFS);
585 }
586
587 /* XXX: userspace can leak kernel resources */
588 void
589 puffs_parkdone_poll(struct puffs_req *preq, void *arg)
590 {
591 struct puffs_vnreq_poll *poll_argp = (void *)preq;
592 struct puffs_node *pn = arg;
593 int revents;
594
595 if (preq->preq_rv == 0)
596 revents = poll_argp->pvnr_events;
597 else
598 revents = POLLERR;
599
600 mutex_enter(&pn->pn_mtx);
601 pn->pn_revents |= revents;
602 mutex_exit(&pn->pn_mtx);
603
604 selnotify(&pn->pn_sel, 0);
605 free(preq, M_PUFFS);
606
607 puffs_releasenode(pn);
608 }
609
610 void
611 puffs_mp_reference(struct puffs_mount *pmp)
612 {
613
614 KASSERT(mutex_owned(&pmp->pmp_lock));
615 pmp->pmp_refcount++;
616 }
617
618 void
619 puffs_mp_release(struct puffs_mount *pmp)
620 {
621
622 KASSERT(mutex_owned(&pmp->pmp_lock));
623 if (--pmp->pmp_refcount == 0)
624 cv_broadcast(&pmp->pmp_refcount_cv);
625 }
626