xref: /freebsd/sys/fs/pseudofs/pseudofs_vnops.c (revision 70e0bbedef95258a4dadc996d641a9bebd3f107d)
1 /*-
2  * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_pseudofs.h"
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/ctype.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
40 #include <sys/limits.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/sbuf.h>
48 #include <sys/sx.h>
49 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
51 
52 #include <fs/pseudofs/pseudofs.h>
53 #include <fs/pseudofs/pseudofs_internal.h>
54 
55 #define KASSERT_PN_IS_DIR(pn)						\
56 	KASSERT((pn)->pn_type == pfstype_root ||			\
57 	    (pn)->pn_type == pfstype_dir ||				\
58 	    (pn)->pn_type == pfstype_procdir,				\
59 	    ("%s(): VDIR vnode refers to non-directory pfs_node", __func__))
60 
61 #define KASSERT_PN_IS_FILE(pn)						\
62 	KASSERT((pn)->pn_type == pfstype_file,				\
63 	    ("%s(): VREG vnode refers to non-file pfs_node", __func__))
64 
65 #define KASSERT_PN_IS_LINK(pn)						\
66 	KASSERT((pn)->pn_type == pfstype_symlink,			\
67 	    ("%s(): VLNK vnode refers to non-link pfs_node", __func__))
68 
69 /*
70  * Returns the fileno, adjusted for target pid
71  */
72 static uint32_t
73 pn_fileno(struct pfs_node *pn, pid_t pid)
74 {
75 
76 	KASSERT(pn->pn_fileno > 0,
77 	    ("%s(): no fileno allocated", __func__));
78 	if (pid != NO_PID)
79 		return (pn->pn_fileno * NO_PID + pid);
80 	return (pn->pn_fileno);
81 }
82 
83 /*
84  * Returns non-zero if given file is visible to given thread.
85  */
86 static int
87 pfs_visible_proc(struct thread *td, struct pfs_node *pn, struct proc *proc)
88 {
89 	int visible;
90 
91 	if (proc == NULL)
92 		return (0);
93 
94 	PROC_LOCK_ASSERT(proc, MA_OWNED);
95 
96 	visible = ((proc->p_flag & P_WEXIT) == 0);
97 	if (visible)
98 		visible = (p_cansee(td, proc) == 0);
99 	if (visible && pn->pn_vis != NULL)
100 		visible = pn_vis(td, proc, pn);
101 	if (!visible)
102 		return (0);
103 	return (1);
104 }
105 
106 static int
107 pfs_visible(struct thread *td, struct pfs_node *pn, pid_t pid, struct proc **p)
108 {
109 	struct proc *proc;
110 
111 	PFS_TRACE(("%s (pid: %d, req: %d)",
112 	    pn->pn_name, pid, td->td_proc->p_pid));
113 
114 	if (p)
115 		*p = NULL;
116 	if (pid == NO_PID)
117 		PFS_RETURN (1);
118 	if ((proc = pfind(pid)) == NULL)
119 		PFS_RETURN (0);
120 	if (pfs_visible_proc(td, pn, proc)) {
121 		if (p)
122 			*p = proc;
123 		else
124 			PROC_UNLOCK(proc);
125 		PFS_RETURN (1);
126 	}
127 	PROC_UNLOCK(proc);
128 	PFS_RETURN (0);
129 }
130 
131 /*
132  * Verify permissions
133  */
134 static int
135 pfs_access(struct vop_access_args *va)
136 {
137 	struct vnode *vn = va->a_vp;
138 	struct pfs_vdata *pvd = vn->v_data;
139 	struct vattr vattr;
140 	int error;
141 
142 	PFS_TRACE(("%s", pvd->pvd_pn->pn_name));
143 	(void)pvd;
144 
145 	error = VOP_GETATTR(vn, &vattr, va->a_cred);
146 	if (error)
147 		PFS_RETURN (error);
148 	error = vaccess(vn->v_type, vattr.va_mode, vattr.va_uid,
149 	    vattr.va_gid, va->a_accmode, va->a_cred, NULL);
150 	PFS_RETURN (error);
151 }
152 
153 /*
154  * Close a file or directory
155  */
156 static int
157 pfs_close(struct vop_close_args *va)
158 {
159 	struct vnode *vn = va->a_vp;
160 	struct pfs_vdata *pvd = vn->v_data;
161 	struct pfs_node *pn = pvd->pvd_pn;
162 	struct proc *proc;
163 	int error;
164 
165 	PFS_TRACE(("%s", pn->pn_name));
166 	pfs_assert_not_owned(pn);
167 
168 	/*
169 	 * Do nothing unless this is the last close and the node has a
170 	 * last-close handler.
171 	 */
172 	if (vrefcnt(vn) > 1 || pn->pn_close == NULL)
173 		PFS_RETURN (0);
174 
175 	if (pvd->pvd_pid != NO_PID) {
176 		proc = pfind(pvd->pvd_pid);
177 	} else {
178 		proc = NULL;
179 	}
180 
181 	error = pn_close(va->a_td, proc, pn);
182 
183 	if (proc != NULL)
184 		PROC_UNLOCK(proc);
185 
186 	PFS_RETURN (error);
187 }
188 
189 /*
190  * Get file attributes
191  */
192 static int
193 pfs_getattr(struct vop_getattr_args *va)
194 {
195 	struct vnode *vn = va->a_vp;
196 	struct pfs_vdata *pvd = vn->v_data;
197 	struct pfs_node *pn = pvd->pvd_pn;
198 	struct vattr *vap = va->a_vap;
199 	struct proc *proc;
200 	int error = 0;
201 
202 	PFS_TRACE(("%s", pn->pn_name));
203 	pfs_assert_not_owned(pn);
204 
205 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
206 		PFS_RETURN (ENOENT);
207 
208 	vap->va_type = vn->v_type;
209 	vap->va_fileid = pn_fileno(pn, pvd->pvd_pid);
210 	vap->va_flags = 0;
211 	vap->va_blocksize = PAGE_SIZE;
212 	vap->va_bytes = vap->va_size = 0;
213 	vap->va_filerev = 0;
214 	vap->va_fsid = vn->v_mount->mnt_stat.f_fsid.val[0];
215 	vap->va_nlink = 1;
216 	nanotime(&vap->va_ctime);
217 	vap->va_atime = vap->va_mtime = vap->va_ctime;
218 
219 	switch (pn->pn_type) {
220 	case pfstype_procdir:
221 	case pfstype_root:
222 	case pfstype_dir:
223 #if 0
224 		pfs_lock(pn);
225 		/* compute link count */
226 		pfs_unlock(pn);
227 #endif
228 		vap->va_mode = 0555;
229 		break;
230 	case pfstype_file:
231 	case pfstype_symlink:
232 		vap->va_mode = 0444;
233 		break;
234 	default:
235 		printf("shouldn't be here!\n");
236 		vap->va_mode = 0;
237 		break;
238 	}
239 
240 	if (proc != NULL) {
241 		vap->va_uid = proc->p_ucred->cr_ruid;
242 		vap->va_gid = proc->p_ucred->cr_rgid;
243 	} else {
244 		vap->va_uid = 0;
245 		vap->va_gid = 0;
246 	}
247 
248 	if (pn->pn_attr != NULL)
249 		error = pn_attr(curthread, proc, pn, vap);
250 
251 	if(proc != NULL)
252 		PROC_UNLOCK(proc);
253 
254 	PFS_RETURN (error);
255 }
256 
257 /*
258  * Perform an ioctl
259  */
260 static int
261 pfs_ioctl(struct vop_ioctl_args *va)
262 {
263 	struct vnode *vn;
264 	struct pfs_vdata *pvd;
265 	struct pfs_node *pn;
266 	struct proc *proc;
267 	int error;
268 
269 	vn = va->a_vp;
270 	vn_lock(vn, LK_SHARED | LK_RETRY);
271 	if (vn->v_iflag & VI_DOOMED) {
272 		VOP_UNLOCK(vn, 0);
273 		return (EBADF);
274 	}
275 	pvd = vn->v_data;
276 	pn = pvd->pvd_pn;
277 
278 	PFS_TRACE(("%s: %lx", pn->pn_name, va->a_command));
279 	pfs_assert_not_owned(pn);
280 
281 	if (vn->v_type != VREG) {
282 		VOP_UNLOCK(vn, 0);
283 		PFS_RETURN (EINVAL);
284 	}
285 	KASSERT_PN_IS_FILE(pn);
286 
287 	if (pn->pn_ioctl == NULL) {
288 		VOP_UNLOCK(vn, 0);
289 		PFS_RETURN (ENOTTY);
290 	}
291 
292 	/*
293 	 * This is necessary because process' privileges may
294 	 * have changed since the open() call.
295 	 */
296 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc)) {
297 		VOP_UNLOCK(vn, 0);
298 		PFS_RETURN (EIO);
299 	}
300 
301 	error = pn_ioctl(curthread, proc, pn, va->a_command, va->a_data);
302 
303 	if (proc != NULL)
304 		PROC_UNLOCK(proc);
305 
306 	VOP_UNLOCK(vn, 0);
307 	PFS_RETURN (error);
308 }
309 
310 /*
311  * Perform getextattr
312  */
313 static int
314 pfs_getextattr(struct vop_getextattr_args *va)
315 {
316 	struct vnode *vn = va->a_vp;
317 	struct pfs_vdata *pvd = vn->v_data;
318 	struct pfs_node *pn = pvd->pvd_pn;
319 	struct proc *proc;
320 	int error;
321 
322 	PFS_TRACE(("%s", pn->pn_name));
323 	pfs_assert_not_owned(pn);
324 
325 	/*
326 	 * This is necessary because either process' privileges may
327 	 * have changed since the open() call.
328 	 */
329 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
330 		PFS_RETURN (EIO);
331 
332 	if (pn->pn_getextattr == NULL)
333 		error = EOPNOTSUPP;
334 	else
335 		error = pn_getextattr(curthread, proc, pn,
336 		    va->a_attrnamespace, va->a_name, va->a_uio,
337 		    va->a_size, va->a_cred);
338 
339 	if (proc != NULL)
340 		PROC_UNLOCK(proc);
341 
342 	PFS_RETURN (error);
343 }
344 
345 /*
346  * Convert a vnode to its component name
347  */
348 static int
349 pfs_vptocnp(struct vop_vptocnp_args *ap)
350 {
351 	struct vnode *vp = ap->a_vp;
352 	struct vnode **dvp = ap->a_vpp;
353 	struct pfs_vdata *pvd = vp->v_data;
354 	struct pfs_node *pd = pvd->pvd_pn;
355 	struct pfs_node *pn;
356 	struct mount *mp;
357 	char *buf = ap->a_buf;
358 	int *buflen = ap->a_buflen;
359 	char pidbuf[PFS_NAMELEN];
360 	pid_t pid = pvd->pvd_pid;
361 	int len, i, error, locked;
362 
363 	i = *buflen;
364 	error = 0;
365 
366 	pfs_lock(pd);
367 
368 	if (vp->v_type == VDIR && pd->pn_type == pfstype_root) {
369 		*dvp = vp;
370 		vhold(*dvp);
371 		pfs_unlock(pd);
372 		PFS_RETURN (0);
373 	} else if (vp->v_type == VDIR && pd->pn_type == pfstype_procdir) {
374 		len = snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
375 		i -= len;
376 		if (i < 0) {
377 			error = ENOMEM;
378 			goto failed;
379 		}
380 		bcopy(pidbuf, buf + i, len);
381 	} else {
382 		len = strlen(pd->pn_name);
383 		i -= len;
384 		if (i < 0) {
385 			error = ENOMEM;
386 			goto failed;
387 		}
388 		bcopy(pd->pn_name, buf + i, len);
389 	}
390 
391 	pn = pd->pn_parent;
392 	pfs_unlock(pd);
393 
394 	mp = vp->v_mount;
395 	error = vfs_busy(mp, 0);
396 	if (error)
397 		return (error);
398 
399 	/*
400 	 * vp is held by caller.
401 	 */
402 	locked = VOP_ISLOCKED(vp);
403 	VOP_UNLOCK(vp, 0);
404 
405 	error = pfs_vncache_alloc(mp, dvp, pn, pid);
406 	if (error) {
407 		vn_lock(vp, locked | LK_RETRY);
408 		vfs_unbusy(mp);
409 		PFS_RETURN(error);
410 	}
411 
412 	*buflen = i;
413 	VOP_UNLOCK(*dvp, 0);
414 	vn_lock(vp, locked | LK_RETRY);
415 	vfs_unbusy(mp);
416 
417 	PFS_RETURN (0);
418 failed:
419 	pfs_unlock(pd);
420 	PFS_RETURN(error);
421 }
422 
423 /*
424  * Look up a file or directory
425  */
426 static int
427 pfs_lookup(struct vop_cachedlookup_args *va)
428 {
429 	struct vnode *vn = va->a_dvp;
430 	struct vnode **vpp = va->a_vpp;
431 	struct componentname *cnp = va->a_cnp;
432 	struct pfs_vdata *pvd = vn->v_data;
433 	struct pfs_node *pd = pvd->pvd_pn;
434 	struct pfs_node *pn, *pdn = NULL;
435 	pid_t pid = pvd->pvd_pid;
436 	char *pname;
437 	int error, i, namelen, visible;
438 
439 	PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr));
440 	pfs_assert_not_owned(pd);
441 
442 	if (vn->v_type != VDIR)
443 		PFS_RETURN (ENOTDIR);
444 	KASSERT_PN_IS_DIR(pd);
445 
446 	error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread);
447 	if (error)
448 		PFS_RETURN (error);
449 
450 	/*
451 	 * Don't support DELETE or RENAME.  CREATE is supported so
452 	 * that O_CREAT will work, but the lookup will still fail if
453 	 * the file does not exist.
454 	 */
455 	if ((cnp->cn_flags & ISLASTCN) &&
456 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
457 		PFS_RETURN (EOPNOTSUPP);
458 
459 	/* shortcut: check if the name is too long */
460 	if (cnp->cn_namelen >= PFS_NAMELEN)
461 		PFS_RETURN (ENOENT);
462 
463 	/* check that parent directory is visible... */
464 	if (!pfs_visible(curthread, pd, pvd->pvd_pid, NULL))
465 		PFS_RETURN (ENOENT);
466 
467 	/* self */
468 	namelen = cnp->cn_namelen;
469 	pname = cnp->cn_nameptr;
470 	if (namelen == 1 && pname[0] == '.') {
471 		pn = pd;
472 		*vpp = vn;
473 		VREF(vn);
474 		PFS_RETURN (0);
475 	}
476 
477 	/* parent */
478 	if (cnp->cn_flags & ISDOTDOT) {
479 		if (pd->pn_type == pfstype_root)
480 			PFS_RETURN (EIO);
481 		VOP_UNLOCK(vn, 0);
482 		KASSERT(pd->pn_parent != NULL,
483 		    ("%s(): non-root directory has no parent", __func__));
484 		/*
485 		 * This one is tricky.  Descendents of procdir nodes
486 		 * inherit their parent's process affinity, but
487 		 * there's no easy reverse mapping.  For simplicity,
488 		 * we assume that if this node is a procdir, its
489 		 * parent isn't (which is correct as long as
490 		 * descendents of procdir nodes are never procdir
491 		 * nodes themselves)
492 		 */
493 		if (pd->pn_type == pfstype_procdir)
494 			pid = NO_PID;
495 		pfs_lock(pd);
496 		pn = pd->pn_parent;
497 		pfs_unlock(pd);
498 		goto got_pnode;
499 	}
500 
501 	pfs_lock(pd);
502 
503 	/* named node */
504 	for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
505 		if (pn->pn_type == pfstype_procdir)
506 			pdn = pn;
507 		else if (pn->pn_name[namelen] == '\0' &&
508 		    bcmp(pname, pn->pn_name, namelen) == 0) {
509 			pfs_unlock(pd);
510 			goto got_pnode;
511 		}
512 
513 	/* process dependent node */
514 	if ((pn = pdn) != NULL) {
515 		pid = 0;
516 		for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
517 			if ((pid = pid * 10 + pname[i] - '0') > PID_MAX)
518 				break;
519 		if (i == cnp->cn_namelen) {
520 			pfs_unlock(pd);
521 			goto got_pnode;
522 		}
523 	}
524 
525 	pfs_unlock(pd);
526 
527 	PFS_RETURN (ENOENT);
528 
529  got_pnode:
530 	pfs_assert_not_owned(pd);
531 	pfs_assert_not_owned(pn);
532 	visible = pfs_visible(curthread, pn, pid, NULL);
533 	if (!visible) {
534 		error = ENOENT;
535 		goto failed;
536 	}
537 
538 	error = pfs_vncache_alloc(vn->v_mount, vpp, pn, pid);
539 	if (error)
540 		goto failed;
541 
542 	if (cnp->cn_flags & ISDOTDOT)
543 		vn_lock(vn, LK_EXCLUSIVE|LK_RETRY);
544 	if (cnp->cn_flags & MAKEENTRY && !(vn->v_iflag & VI_DOOMED))
545 		cache_enter(vn, *vpp, cnp);
546 	PFS_RETURN (0);
547  failed:
548 	if (cnp->cn_flags & ISDOTDOT)
549 		vn_lock(vn, LK_EXCLUSIVE|LK_RETRY);
550 	PFS_RETURN(error);
551 }
552 
553 /*
554  * Open a file or directory.
555  */
556 static int
557 pfs_open(struct vop_open_args *va)
558 {
559 	struct vnode *vn = va->a_vp;
560 	struct pfs_vdata *pvd = vn->v_data;
561 	struct pfs_node *pn = pvd->pvd_pn;
562 	int mode = va->a_mode;
563 
564 	PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
565 	pfs_assert_not_owned(pn);
566 
567 	/* check if the requested mode is permitted */
568 	if (((mode & FREAD) && !(mode & PFS_RD)) ||
569 	    ((mode & FWRITE) && !(mode & PFS_WR)))
570 		PFS_RETURN (EPERM);
571 
572 	/* we don't support locking */
573 	if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
574 		PFS_RETURN (EOPNOTSUPP);
575 
576 	PFS_RETURN (0);
577 }
578 
579 /*
580  * Read from a file
581  */
582 static int
583 pfs_read(struct vop_read_args *va)
584 {
585 	struct vnode *vn = va->a_vp;
586 	struct pfs_vdata *pvd = vn->v_data;
587 	struct pfs_node *pn = pvd->pvd_pn;
588 	struct uio *uio = va->a_uio;
589 	struct proc *proc;
590 	struct sbuf *sb = NULL;
591 	int error, locked;
592 	unsigned int buflen, offset, resid;
593 
594 	PFS_TRACE(("%s", pn->pn_name));
595 	pfs_assert_not_owned(pn);
596 
597 	if (vn->v_type != VREG)
598 		PFS_RETURN (EINVAL);
599 	KASSERT_PN_IS_FILE(pn);
600 
601 	if (!(pn->pn_flags & PFS_RD))
602 		PFS_RETURN (EBADF);
603 
604 	if (pn->pn_fill == NULL)
605 		PFS_RETURN (EIO);
606 
607 	/*
608 	 * This is necessary because either process' privileges may
609 	 * have changed since the open() call.
610 	 */
611 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
612 		PFS_RETURN (EIO);
613 	if (proc != NULL) {
614 		_PHOLD(proc);
615 		PROC_UNLOCK(proc);
616 	}
617 
618 	vhold(vn);
619 	locked = VOP_ISLOCKED(vn);
620 	VOP_UNLOCK(vn, 0);
621 
622 	if (pn->pn_flags & PFS_RAWRD) {
623 		PFS_TRACE(("%zd resid", uio->uio_resid));
624 		error = pn_fill(curthread, proc, pn, NULL, uio);
625 		PFS_TRACE(("%zd resid", uio->uio_resid));
626 		goto ret;
627 	}
628 
629 	/* beaucoup sanity checks so we don't ask for bogus allocation */
630 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
631 	    (offset = uio->uio_offset) != uio->uio_offset ||
632 	    (resid = uio->uio_resid) != uio->uio_resid ||
633 	    (buflen = offset + resid) < offset || buflen >= INT_MAX) {
634 		error = EINVAL;
635 		goto ret;
636 	}
637 	if (buflen > MAXPHYS)
638 		buflen = MAXPHYS;
639 
640 	sb = sbuf_new(sb, NULL, buflen + 1, 0);
641 	if (sb == NULL) {
642 		error = EIO;
643 		goto ret;
644 	}
645 
646 	error = pn_fill(curthread, proc, pn, sb, uio);
647 
648 	if (error) {
649 		sbuf_delete(sb);
650 		goto ret;
651 	}
652 
653 	/*
654 	 * XXX: If the buffer overflowed, sbuf_len() will not return
655 	 * the data length. Then just use the full length because an
656 	 * overflowed sbuf must be full.
657 	 */
658 	if (sbuf_finish(sb) == 0)
659 		buflen = sbuf_len(sb);
660 	error = uiomove_frombuf(sbuf_data(sb), buflen, uio);
661 	sbuf_delete(sb);
662 ret:
663 	vn_lock(vn, locked | LK_RETRY);
664 	vdrop(vn);
665 	if (proc != NULL)
666 		PRELE(proc);
667 	PFS_RETURN (error);
668 }
669 
670 /*
671  * Iterate through directory entries
672  */
673 static int
674 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd,
675 	    struct pfs_node **pn, struct proc **p)
676 {
677 	int visible;
678 
679 	sx_assert(&allproc_lock, SX_SLOCKED);
680 	pfs_assert_owned(pd);
681  again:
682 	if (*pn == NULL) {
683 		/* first node */
684 		*pn = pd->pn_nodes;
685 	} else if ((*pn)->pn_type != pfstype_procdir) {
686 		/* next node */
687 		*pn = (*pn)->pn_next;
688 	}
689 	if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
690 		/* next process */
691 		if (*p == NULL)
692 			*p = LIST_FIRST(&allproc);
693 		else
694 			*p = LIST_NEXT(*p, p_list);
695 		/* out of processes: next node */
696 		if (*p == NULL)
697 			*pn = (*pn)->pn_next;
698 		else
699 			PROC_LOCK(*p);
700 	}
701 
702 	if ((*pn) == NULL)
703 		return (-1);
704 
705 	if (*p != NULL) {
706 		visible = pfs_visible_proc(td, *pn, *p);
707 		PROC_UNLOCK(*p);
708 	} else if (proc != NULL) {
709 		visible = pfs_visible_proc(td, *pn, proc);
710 	} else {
711 		visible = 1;
712 	}
713 	if (!visible)
714 		goto again;
715 
716 	return (0);
717 }
718 
719 /* Directory entry list */
720 struct pfsentry {
721 	STAILQ_ENTRY(pfsentry)	link;
722 	struct dirent		entry;
723 };
724 STAILQ_HEAD(pfsdirentlist, pfsentry);
725 
726 /*
727  * Return directory entries.
728  */
729 static int
730 pfs_readdir(struct vop_readdir_args *va)
731 {
732 	struct vnode *vn = va->a_vp;
733 	struct pfs_vdata *pvd = vn->v_data;
734 	struct pfs_node *pd = pvd->pvd_pn;
735 	pid_t pid = pvd->pvd_pid;
736 	struct proc *p, *proc;
737 	struct pfs_node *pn;
738 	struct uio *uio;
739 	struct pfsentry *pfsent, *pfsent2;
740 	struct pfsdirentlist lst;
741 	off_t offset;
742 	int error, i, resid;
743 
744 	STAILQ_INIT(&lst);
745 	error = 0;
746 	KASSERT(pd->pn_info == vn->v_mount->mnt_data,
747 	    ("%s(): pn_info does not match mountpoint", __func__));
748 	PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid));
749 	pfs_assert_not_owned(pd);
750 
751 	if (vn->v_type != VDIR)
752 		PFS_RETURN (ENOTDIR);
753 	KASSERT_PN_IS_DIR(pd);
754 	uio = va->a_uio;
755 
756 	/* only allow reading entire entries */
757 	offset = uio->uio_offset;
758 	resid = uio->uio_resid;
759 	if (offset < 0 || offset % PFS_DELEN != 0 ||
760 	    (resid && resid < PFS_DELEN))
761 		PFS_RETURN (EINVAL);
762 	if (resid == 0)
763 		PFS_RETURN (0);
764 
765 	sx_slock(&allproc_lock);
766 	pfs_lock(pd);
767 
768         /* check if the directory is visible to the caller */
769         if (!pfs_visible(curthread, pd, pid, &proc)) {
770 		sx_sunlock(&allproc_lock);
771 		pfs_unlock(pd);
772                 PFS_RETURN (ENOENT);
773 	}
774 	KASSERT(pid == NO_PID || proc != NULL,
775 	    ("%s(): no process for pid %lu", __func__, (unsigned long)pid));
776 
777 	/* skip unwanted entries */
778 	for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) {
779 		if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) {
780 			/* nothing left... */
781 			if (proc != NULL)
782 				PROC_UNLOCK(proc);
783 			pfs_unlock(pd);
784 			sx_sunlock(&allproc_lock);
785 			PFS_RETURN (0);
786 		}
787 	}
788 
789 	/* fill in entries */
790 	while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 &&
791 	    resid >= PFS_DELEN) {
792 		if ((pfsent = malloc(sizeof(struct pfsentry), M_IOV,
793 		    M_NOWAIT | M_ZERO)) == NULL) {
794 			error = ENOMEM;
795 			break;
796 		}
797 		pfsent->entry.d_reclen = PFS_DELEN;
798 		pfsent->entry.d_fileno = pn_fileno(pn, pid);
799 		/* PFS_DELEN was picked to fit PFS_NAMLEN */
800 		for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
801 			pfsent->entry.d_name[i] = pn->pn_name[i];
802 		pfsent->entry.d_name[i] = 0;
803 		pfsent->entry.d_namlen = i;
804 		switch (pn->pn_type) {
805 		case pfstype_procdir:
806 			KASSERT(p != NULL,
807 			    ("reached procdir node with p == NULL"));
808 			pfsent->entry.d_namlen = snprintf(pfsent->entry.d_name,
809 			    PFS_NAMELEN, "%d", p->p_pid);
810 			/* fall through */
811 		case pfstype_root:
812 		case pfstype_dir:
813 		case pfstype_this:
814 		case pfstype_parent:
815 			pfsent->entry.d_type = DT_DIR;
816 			break;
817 		case pfstype_file:
818 			pfsent->entry.d_type = DT_REG;
819 			break;
820 		case pfstype_symlink:
821 			pfsent->entry.d_type = DT_LNK;
822 			break;
823 		default:
824 			panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
825 		}
826 		PFS_TRACE(("%s", pfsent->entry.d_name));
827 		STAILQ_INSERT_TAIL(&lst, pfsent, link);
828 		offset += PFS_DELEN;
829 		resid -= PFS_DELEN;
830 	}
831 	if (proc != NULL)
832 		PROC_UNLOCK(proc);
833 	pfs_unlock(pd);
834 	sx_sunlock(&allproc_lock);
835 	i = 0;
836 	STAILQ_FOREACH_SAFE(pfsent, &lst, link, pfsent2) {
837 		if (error == 0)
838 			error = uiomove(&pfsent->entry, PFS_DELEN, uio);
839 		free(pfsent, M_IOV);
840 		i++;
841 	}
842 	PFS_TRACE(("%d bytes", i * PFS_DELEN));
843 	PFS_RETURN (error);
844 }
845 
846 /*
847  * Read a symbolic link
848  */
849 static int
850 pfs_readlink(struct vop_readlink_args *va)
851 {
852 	struct vnode *vn = va->a_vp;
853 	struct pfs_vdata *pvd = vn->v_data;
854 	struct pfs_node *pn = pvd->pvd_pn;
855 	struct uio *uio = va->a_uio;
856 	struct proc *proc = NULL;
857 	char buf[PATH_MAX];
858 	struct sbuf sb;
859 	int error, locked;
860 
861 	PFS_TRACE(("%s", pn->pn_name));
862 	pfs_assert_not_owned(pn);
863 
864 	if (vn->v_type != VLNK)
865 		PFS_RETURN (EINVAL);
866 	KASSERT_PN_IS_LINK(pn);
867 
868 	if (pn->pn_fill == NULL)
869 		PFS_RETURN (EIO);
870 
871 	if (pvd->pvd_pid != NO_PID) {
872 		if ((proc = pfind(pvd->pvd_pid)) == NULL)
873 			PFS_RETURN (EIO);
874 		if (proc->p_flag & P_WEXIT) {
875 			PROC_UNLOCK(proc);
876 			PFS_RETURN (EIO);
877 		}
878 		_PHOLD(proc);
879 		PROC_UNLOCK(proc);
880 	}
881 	vhold(vn);
882 	locked = VOP_ISLOCKED(vn);
883 	VOP_UNLOCK(vn, 0);
884 
885 	/* sbuf_new() can't fail with a static buffer */
886 	sbuf_new(&sb, buf, sizeof buf, 0);
887 
888 	error = pn_fill(curthread, proc, pn, &sb, NULL);
889 
890 	if (proc != NULL)
891 		PRELE(proc);
892 	vn_lock(vn, locked | LK_RETRY);
893 	vdrop(vn);
894 
895 	if (error) {
896 		sbuf_delete(&sb);
897 		PFS_RETURN (error);
898 	}
899 
900 	if (sbuf_finish(&sb) != 0) {
901 		sbuf_delete(&sb);
902 		PFS_RETURN (ENAMETOOLONG);
903 	}
904 
905 	error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
906 	sbuf_delete(&sb);
907 	PFS_RETURN (error);
908 }
909 
910 /*
911  * Reclaim a vnode
912  */
913 static int
914 pfs_reclaim(struct vop_reclaim_args *va)
915 {
916 	struct vnode *vn = va->a_vp;
917 	struct pfs_vdata *pvd = vn->v_data;
918 	struct pfs_node *pn = pvd->pvd_pn;
919 
920 	PFS_TRACE(("%s", pn->pn_name));
921 	pfs_assert_not_owned(pn);
922 
923 	return (pfs_vncache_free(va->a_vp));
924 }
925 
926 /*
927  * Set attributes
928  */
929 static int
930 pfs_setattr(struct vop_setattr_args *va)
931 {
932 	struct vnode *vn = va->a_vp;
933 	struct pfs_vdata *pvd = vn->v_data;
934 	struct pfs_node *pn = pvd->pvd_pn;
935 
936 	PFS_TRACE(("%s", pn->pn_name));
937 	pfs_assert_not_owned(pn);
938 
939 	PFS_RETURN (EOPNOTSUPP);
940 }
941 
942 /*
943  * Write to a file
944  */
945 static int
946 pfs_write(struct vop_write_args *va)
947 {
948 	struct vnode *vn = va->a_vp;
949 	struct pfs_vdata *pvd = vn->v_data;
950 	struct pfs_node *pn = pvd->pvd_pn;
951 	struct uio *uio = va->a_uio;
952 	struct proc *proc;
953 	struct sbuf sb;
954 	int error;
955 
956 	PFS_TRACE(("%s", pn->pn_name));
957 	pfs_assert_not_owned(pn);
958 
959 	if (vn->v_type != VREG)
960 		PFS_RETURN (EINVAL);
961 	KASSERT_PN_IS_FILE(pn);
962 
963 	if (!(pn->pn_flags & PFS_WR))
964 		PFS_RETURN (EBADF);
965 
966 	if (pn->pn_fill == NULL)
967 		PFS_RETURN (EIO);
968 
969 	/*
970 	 * This is necessary because either process' privileges may
971 	 * have changed since the open() call.
972 	 */
973 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
974 		PFS_RETURN (EIO);
975 	if (proc != NULL) {
976 		_PHOLD(proc);
977 		PROC_UNLOCK(proc);
978 	}
979 
980 	if (pn->pn_flags & PFS_RAWWR) {
981 		error = pn_fill(curthread, proc, pn, NULL, uio);
982 		if (proc != NULL)
983 			PRELE(proc);
984 		PFS_RETURN (error);
985 	}
986 
987 	sbuf_uionew(&sb, uio, &error);
988 	if (error) {
989 		if (proc != NULL)
990 			PRELE(proc);
991 		PFS_RETURN (error);
992 	}
993 
994 	error = pn_fill(curthread, proc, pn, &sb, uio);
995 
996 	sbuf_delete(&sb);
997 	if (proc != NULL)
998 		PRELE(proc);
999 	PFS_RETURN (error);
1000 }
1001 
1002 /*
1003  * Vnode operations
1004  */
1005 struct vop_vector pfs_vnodeops = {
1006 	.vop_default =		&default_vnodeops,
1007 
1008 	.vop_access =		pfs_access,
1009 	.vop_cachedlookup =	pfs_lookup,
1010 	.vop_close =		pfs_close,
1011 	.vop_create =		VOP_EOPNOTSUPP,
1012 	.vop_getattr =		pfs_getattr,
1013 	.vop_getextattr =	pfs_getextattr,
1014 	.vop_ioctl =		pfs_ioctl,
1015 	.vop_link =		VOP_EOPNOTSUPP,
1016 	.vop_lookup =		vfs_cache_lookup,
1017 	.vop_mkdir =		VOP_EOPNOTSUPP,
1018 	.vop_mknod =		VOP_EOPNOTSUPP,
1019 	.vop_open =		pfs_open,
1020 	.vop_read =		pfs_read,
1021 	.vop_readdir =		pfs_readdir,
1022 	.vop_readlink =		pfs_readlink,
1023 	.vop_reclaim =		pfs_reclaim,
1024 	.vop_remove =		VOP_EOPNOTSUPP,
1025 	.vop_rename =		VOP_EOPNOTSUPP,
1026 	.vop_rmdir =		VOP_EOPNOTSUPP,
1027 	.vop_setattr =		pfs_setattr,
1028 	.vop_symlink =		VOP_EOPNOTSUPP,
1029 	.vop_vptocnp =		pfs_vptocnp,
1030 	.vop_write =		pfs_write,
1031 	/* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */
1032 };
1033