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