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