xref: /freebsd/sys/fs/pseudofs/pseudofs_vnops.c (revision 6966ac055c3b7a39266fb982493330df7a097997)
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,
171 	    vattr.va_gid, va->a_accmode, va->a_cred, NULL);
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 	int *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 	error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread);
470 	if (error)
471 		PFS_RETURN (error);
472 
473 	/*
474 	 * Don't support DELETE or RENAME.  CREATE is supported so
475 	 * that O_CREAT will work, but the lookup will still fail if
476 	 * the file does not exist.
477 	 */
478 	if ((cnp->cn_flags & ISLASTCN) &&
479 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
480 		PFS_RETURN (EOPNOTSUPP);
481 
482 	/* shortcut: check if the name is too long */
483 	if (cnp->cn_namelen >= PFS_NAMELEN)
484 		PFS_RETURN (ENOENT);
485 
486 	/* check that parent directory is visible... */
487 	if (!pfs_visible(curthread, pd, pvd->pvd_pid, NULL))
488 		PFS_RETURN (ENOENT);
489 
490 	/* self */
491 	namelen = cnp->cn_namelen;
492 	pname = cnp->cn_nameptr;
493 	if (namelen == 1 && pname[0] == '.') {
494 		pn = pd;
495 		*vpp = vn;
496 		VREF(vn);
497 		PFS_RETURN (0);
498 	}
499 
500 	mp = vn->v_mount;
501 
502 	/* parent */
503 	if (cnp->cn_flags & ISDOTDOT) {
504 		if (pd->pn_type == pfstype_root)
505 			PFS_RETURN (EIO);
506 		error = vfs_busy(mp, MBF_NOWAIT);
507 		if (error != 0) {
508 			vfs_ref(mp);
509 			VOP_UNLOCK(vn);
510 			error = vfs_busy(mp, 0);
511 			vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
512 			vfs_rel(mp);
513 			if (error != 0)
514 				PFS_RETURN(ENOENT);
515 			if (VN_IS_DOOMED(vn)) {
516 				vfs_unbusy(mp);
517 				PFS_RETURN(ENOENT);
518 			}
519 		}
520 		VOP_UNLOCK(vn);
521 		KASSERT(pd->pn_parent != NULL,
522 		    ("%s(): non-root directory has no parent", __func__));
523 		/*
524 		 * This one is tricky.  Descendents of procdir nodes
525 		 * inherit their parent's process affinity, but
526 		 * there's no easy reverse mapping.  For simplicity,
527 		 * we assume that if this node is a procdir, its
528 		 * parent isn't (which is correct as long as
529 		 * descendents of procdir nodes are never procdir
530 		 * nodes themselves)
531 		 */
532 		if (pd->pn_type == pfstype_procdir)
533 			pid = NO_PID;
534 		pfs_lock(pd);
535 		pn = pd->pn_parent;
536 		pfs_unlock(pd);
537 		goto got_pnode;
538 	}
539 
540 	pfs_lock(pd);
541 
542 	/* named node */
543 	for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
544 		if (pn->pn_type == pfstype_procdir)
545 			pdn = pn;
546 		else if (pn->pn_name[namelen] == '\0' &&
547 		    bcmp(pname, pn->pn_name, namelen) == 0) {
548 			pfs_unlock(pd);
549 			goto got_pnode;
550 		}
551 
552 	/* process dependent node */
553 	if ((pn = pdn) != NULL) {
554 		pid = 0;
555 		for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
556 			if ((pid = pid * 10 + pname[i] - '0') > PID_MAX)
557 				break;
558 		if (i == cnp->cn_namelen) {
559 			pfs_unlock(pd);
560 			goto got_pnode;
561 		}
562 	}
563 
564 	pfs_unlock(pd);
565 
566 	PFS_RETURN (ENOENT);
567 
568  got_pnode:
569 	pfs_assert_not_owned(pd);
570 	pfs_assert_not_owned(pn);
571 	visible = pfs_visible(curthread, pn, pid, NULL);
572 	if (!visible) {
573 		error = ENOENT;
574 		goto failed;
575 	}
576 
577 	error = pfs_vncache_alloc(mp, vpp, pn, pid);
578 	if (error)
579 		goto failed;
580 
581 	if (cnp->cn_flags & ISDOTDOT) {
582 		vfs_unbusy(mp);
583 		vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
584 		if (VN_IS_DOOMED(vn)) {
585 			vput(*vpp);
586 			*vpp = NULL;
587 			PFS_RETURN(ENOENT);
588 		}
589 	}
590 	if (cnp->cn_flags & MAKEENTRY && !VN_IS_DOOMED(vn))
591 		cache_enter(vn, *vpp, cnp);
592 	PFS_RETURN (0);
593  failed:
594 	if (cnp->cn_flags & ISDOTDOT) {
595 		vfs_unbusy(mp);
596 		vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
597 		*vpp = NULL;
598 	}
599 	PFS_RETURN(error);
600 }
601 
602 /*
603  * Open a file or directory.
604  */
605 static int
606 pfs_open(struct vop_open_args *va)
607 {
608 	struct vnode *vn = va->a_vp;
609 	struct pfs_vdata *pvd = vn->v_data;
610 	struct pfs_node *pn = pvd->pvd_pn;
611 	int mode = va->a_mode;
612 
613 	PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
614 	pfs_assert_not_owned(pn);
615 
616 	/* check if the requested mode is permitted */
617 	if (((mode & FREAD) && !(mode & PFS_RD)) ||
618 	    ((mode & FWRITE) && !(mode & PFS_WR)))
619 		PFS_RETURN (EPERM);
620 
621 	/* we don't support locking */
622 	if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
623 		PFS_RETURN (EOPNOTSUPP);
624 
625 	PFS_RETURN (0);
626 }
627 
628 /*
629  * Read from a file
630  */
631 static int
632 pfs_read(struct vop_read_args *va)
633 {
634 	struct vnode *vn = va->a_vp;
635 	struct pfs_vdata *pvd = vn->v_data;
636 	struct pfs_node *pn = pvd->pvd_pn;
637 	struct uio *uio = va->a_uio;
638 	struct proc *proc;
639 	struct sbuf *sb = NULL;
640 	int error, locked;
641 	off_t buflen;
642 
643 	PFS_TRACE(("%s", pn->pn_name));
644 	pfs_assert_not_owned(pn);
645 
646 	if (vn->v_type != VREG)
647 		PFS_RETURN (EINVAL);
648 	KASSERT_PN_IS_FILE(pn);
649 
650 	if (!(pn->pn_flags & PFS_RD))
651 		PFS_RETURN (EBADF);
652 
653 	if (pn->pn_fill == NULL)
654 		PFS_RETURN (EIO);
655 
656 	/*
657 	 * This is necessary because either process' privileges may
658 	 * have changed since the open() call.
659 	 */
660 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
661 		PFS_RETURN (EIO);
662 	if (proc != NULL) {
663 		_PHOLD(proc);
664 		PROC_UNLOCK(proc);
665 	}
666 
667 	vhold(vn);
668 	locked = VOP_ISLOCKED(vn);
669 	VOP_UNLOCK(vn);
670 
671 	if (pn->pn_flags & PFS_RAWRD) {
672 		PFS_TRACE(("%zd resid", uio->uio_resid));
673 		error = pn_fill(curthread, proc, pn, NULL, uio);
674 		PFS_TRACE(("%zd resid", uio->uio_resid));
675 		goto ret;
676 	}
677 
678 	if (uio->uio_resid < 0 || uio->uio_offset < 0 ||
679 	    uio->uio_resid > OFF_MAX - uio->uio_offset) {
680 		error = EINVAL;
681 		goto ret;
682 	}
683 	buflen = uio->uio_offset + uio->uio_resid;
684 	if (buflen > MAXPHYS)
685 		buflen = MAXPHYS;
686 
687 	sb = sbuf_new(sb, NULL, buflen + 1, 0);
688 	if (sb == NULL) {
689 		error = EIO;
690 		goto ret;
691 	}
692 
693 	error = pn_fill(curthread, proc, pn, sb, uio);
694 
695 	if (error) {
696 		sbuf_delete(sb);
697 		goto ret;
698 	}
699 
700 	/*
701 	 * XXX: If the buffer overflowed, sbuf_len() will not return
702 	 * the data length. Then just use the full length because an
703 	 * overflowed sbuf must be full.
704 	 */
705 	if (sbuf_finish(sb) == 0)
706 		buflen = sbuf_len(sb);
707 	error = uiomove_frombuf(sbuf_data(sb), buflen, uio);
708 	sbuf_delete(sb);
709 ret:
710 	vn_lock(vn, locked | LK_RETRY);
711 	vdrop(vn);
712 	if (proc != NULL)
713 		PRELE(proc);
714 	PFS_RETURN (error);
715 }
716 
717 /*
718  * Iterate through directory entries
719  */
720 static int
721 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd,
722 	    struct pfs_node **pn, struct proc **p)
723 {
724 	int visible;
725 
726 	sx_assert(&allproc_lock, SX_SLOCKED);
727 	pfs_assert_owned(pd);
728  again:
729 	if (*pn == NULL) {
730 		/* first node */
731 		*pn = pd->pn_nodes;
732 	} else if ((*pn)->pn_type != pfstype_procdir) {
733 		/* next node */
734 		*pn = (*pn)->pn_next;
735 	}
736 	if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
737 		/* next process */
738 		if (*p == NULL)
739 			*p = LIST_FIRST(&allproc);
740 		else
741 			*p = LIST_NEXT(*p, p_list);
742 		/* out of processes: next node */
743 		if (*p == NULL)
744 			*pn = (*pn)->pn_next;
745 		else
746 			PROC_LOCK(*p);
747 	}
748 
749 	if ((*pn) == NULL)
750 		return (-1);
751 
752 	if (*p != NULL) {
753 		visible = pfs_visible_proc(td, *pn, *p);
754 		PROC_UNLOCK(*p);
755 	} else if (proc != NULL) {
756 		visible = pfs_visible_proc(td, *pn, proc);
757 	} else {
758 		visible = 1;
759 	}
760 	if (!visible)
761 		goto again;
762 
763 	return (0);
764 }
765 
766 /* Directory entry list */
767 struct pfsentry {
768 	STAILQ_ENTRY(pfsentry)	link;
769 	struct dirent		entry;
770 };
771 STAILQ_HEAD(pfsdirentlist, pfsentry);
772 
773 /*
774  * Return directory entries.
775  */
776 static int
777 pfs_readdir(struct vop_readdir_args *va)
778 {
779 	struct vnode *vn = va->a_vp;
780 	struct pfs_vdata *pvd = vn->v_data;
781 	struct pfs_node *pd = pvd->pvd_pn;
782 	pid_t pid = pvd->pvd_pid;
783 	struct proc *p, *proc;
784 	struct pfs_node *pn;
785 	struct uio *uio;
786 	struct pfsentry *pfsent, *pfsent2;
787 	struct pfsdirentlist lst;
788 	off_t offset;
789 	int error, i, resid;
790 
791 	STAILQ_INIT(&lst);
792 	error = 0;
793 	KASSERT(pd->pn_info == vn->v_mount->mnt_data,
794 	    ("%s(): pn_info does not match mountpoint", __func__));
795 	PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid));
796 	pfs_assert_not_owned(pd);
797 
798 	if (vn->v_type != VDIR)
799 		PFS_RETURN (ENOTDIR);
800 	KASSERT_PN_IS_DIR(pd);
801 	uio = va->a_uio;
802 
803 	/* only allow reading entire entries */
804 	offset = uio->uio_offset;
805 	resid = uio->uio_resid;
806 	if (offset < 0 || offset % PFS_DELEN != 0 ||
807 	    (resid && resid < PFS_DELEN))
808 		PFS_RETURN (EINVAL);
809 	if (resid == 0)
810 		PFS_RETURN (0);
811 
812 	proc = NULL;
813 	if (pid != NO_PID && !pfs_lookup_proc(pid, &proc))
814 		PFS_RETURN (ENOENT);
815 
816 	sx_slock(&allproc_lock);
817 	pfs_lock(pd);
818 
819 	KASSERT(pid == NO_PID || proc != NULL,
820 	    ("%s(): no process for pid %lu", __func__, (unsigned long)pid));
821 
822 	if (pid != NO_PID) {
823 		PROC_LOCK(proc);
824 
825 		/* check if the directory is visible to the caller */
826 		if (!pfs_visible_proc(curthread, pd, proc)) {
827 			_PRELE(proc);
828 			PROC_UNLOCK(proc);
829 			sx_sunlock(&allproc_lock);
830 			pfs_unlock(pd);
831 			PFS_RETURN (ENOENT);
832 		}
833 	}
834 
835 	/* skip unwanted entries */
836 	for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) {
837 		if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) {
838 			/* nothing left... */
839 			if (proc != NULL) {
840 				_PRELE(proc);
841 				PROC_UNLOCK(proc);
842 			}
843 			pfs_unlock(pd);
844 			sx_sunlock(&allproc_lock);
845 			PFS_RETURN (0);
846 		}
847 	}
848 
849 	/* fill in entries */
850 	while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 &&
851 	    resid >= PFS_DELEN) {
852 		if ((pfsent = malloc(sizeof(struct pfsentry), M_IOV,
853 		    M_NOWAIT | M_ZERO)) == NULL) {
854 			error = ENOMEM;
855 			break;
856 		}
857 		pfsent->entry.d_reclen = PFS_DELEN;
858 		pfsent->entry.d_fileno = pn_fileno(pn, pid);
859 		/* PFS_DELEN was picked to fit PFS_NAMLEN */
860 		for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
861 			pfsent->entry.d_name[i] = pn->pn_name[i];
862 		pfsent->entry.d_namlen = i;
863 		/* NOTE: d_off is the offset of the *next* entry. */
864 		pfsent->entry.d_off = offset + PFS_DELEN;
865 		switch (pn->pn_type) {
866 		case pfstype_procdir:
867 			KASSERT(p != NULL,
868 			    ("reached procdir node with p == NULL"));
869 			pfsent->entry.d_namlen = snprintf(pfsent->entry.d_name,
870 			    PFS_NAMELEN, "%d", p->p_pid);
871 			/* fall through */
872 		case pfstype_root:
873 		case pfstype_dir:
874 		case pfstype_this:
875 		case pfstype_parent:
876 			pfsent->entry.d_type = DT_DIR;
877 			break;
878 		case pfstype_file:
879 			pfsent->entry.d_type = DT_REG;
880 			break;
881 		case pfstype_symlink:
882 			pfsent->entry.d_type = DT_LNK;
883 			break;
884 		default:
885 			panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
886 		}
887 		PFS_TRACE(("%s", pfsent->entry.d_name));
888 		dirent_terminate(&pfsent->entry);
889 		STAILQ_INSERT_TAIL(&lst, pfsent, link);
890 		offset += PFS_DELEN;
891 		resid -= PFS_DELEN;
892 	}
893 	if (proc != NULL) {
894 		_PRELE(proc);
895 		PROC_UNLOCK(proc);
896 	}
897 	pfs_unlock(pd);
898 	sx_sunlock(&allproc_lock);
899 	i = 0;
900 	STAILQ_FOREACH_SAFE(pfsent, &lst, link, pfsent2) {
901 		if (error == 0)
902 			error = uiomove(&pfsent->entry, PFS_DELEN, uio);
903 		free(pfsent, M_IOV);
904 		i++;
905 	}
906 	PFS_TRACE(("%ju bytes", (uintmax_t)(i * PFS_DELEN)));
907 	PFS_RETURN (error);
908 }
909 
910 /*
911  * Read a symbolic link
912  */
913 static int
914 pfs_readlink(struct vop_readlink_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 	struct uio *uio = va->a_uio;
920 	struct proc *proc = NULL;
921 	char buf[PATH_MAX];
922 	struct sbuf sb;
923 	int error, locked;
924 
925 	PFS_TRACE(("%s", pn->pn_name));
926 	pfs_assert_not_owned(pn);
927 
928 	if (vn->v_type != VLNK)
929 		PFS_RETURN (EINVAL);
930 	KASSERT_PN_IS_LINK(pn);
931 
932 	if (pn->pn_fill == NULL)
933 		PFS_RETURN (EIO);
934 
935 	if (pvd->pvd_pid != NO_PID) {
936 		if ((proc = pfind(pvd->pvd_pid)) == NULL)
937 			PFS_RETURN (EIO);
938 		if (proc->p_flag & P_WEXIT) {
939 			PROC_UNLOCK(proc);
940 			PFS_RETURN (EIO);
941 		}
942 		_PHOLD(proc);
943 		PROC_UNLOCK(proc);
944 	}
945 	vhold(vn);
946 	locked = VOP_ISLOCKED(vn);
947 	VOP_UNLOCK(vn);
948 
949 	/* sbuf_new() can't fail with a static buffer */
950 	sbuf_new(&sb, buf, sizeof buf, 0);
951 
952 	error = pn_fill(curthread, proc, pn, &sb, NULL);
953 
954 	if (proc != NULL)
955 		PRELE(proc);
956 	vn_lock(vn, locked | LK_RETRY);
957 	vdrop(vn);
958 
959 	if (error) {
960 		sbuf_delete(&sb);
961 		PFS_RETURN (error);
962 	}
963 
964 	if (sbuf_finish(&sb) != 0) {
965 		sbuf_delete(&sb);
966 		PFS_RETURN (ENAMETOOLONG);
967 	}
968 
969 	error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
970 	sbuf_delete(&sb);
971 	PFS_RETURN (error);
972 }
973 
974 /*
975  * Reclaim a vnode
976  */
977 static int
978 pfs_reclaim(struct vop_reclaim_args *va)
979 {
980 	struct vnode *vn = va->a_vp;
981 	struct pfs_vdata *pvd = vn->v_data;
982 	struct pfs_node *pn = pvd->pvd_pn;
983 
984 	PFS_TRACE(("%s", pn->pn_name));
985 	pfs_assert_not_owned(pn);
986 
987 	return (pfs_vncache_free(va->a_vp));
988 }
989 
990 /*
991  * Set attributes
992  */
993 static int
994 pfs_setattr(struct vop_setattr_args *va)
995 {
996 	struct vnode *vn = va->a_vp;
997 	struct pfs_vdata *pvd = vn->v_data;
998 	struct pfs_node *pn = pvd->pvd_pn;
999 
1000 	PFS_TRACE(("%s", pn->pn_name));
1001 	pfs_assert_not_owned(pn);
1002 
1003 	/* Silently ignore unchangeable attributes. */
1004 	PFS_RETURN (0);
1005 }
1006 
1007 /*
1008  * Write to a file
1009  */
1010 static int
1011 pfs_write(struct vop_write_args *va)
1012 {
1013 	struct vnode *vn = va->a_vp;
1014 	struct pfs_vdata *pvd = vn->v_data;
1015 	struct pfs_node *pn = pvd->pvd_pn;
1016 	struct uio *uio = va->a_uio;
1017 	struct proc *proc;
1018 	struct sbuf sb;
1019 	int error;
1020 
1021 	PFS_TRACE(("%s", pn->pn_name));
1022 	pfs_assert_not_owned(pn);
1023 
1024 	if (vn->v_type != VREG)
1025 		PFS_RETURN (EINVAL);
1026 	KASSERT_PN_IS_FILE(pn);
1027 
1028 	if (!(pn->pn_flags & PFS_WR))
1029 		PFS_RETURN (EBADF);
1030 
1031 	if (pn->pn_fill == NULL)
1032 		PFS_RETURN (EIO);
1033 
1034 	/*
1035 	 * This is necessary because either process' privileges may
1036 	 * have changed since the open() call.
1037 	 */
1038 	if (!pfs_visible(curthread, pn, pvd->pvd_pid, &proc))
1039 		PFS_RETURN (EIO);
1040 	if (proc != NULL) {
1041 		_PHOLD(proc);
1042 		PROC_UNLOCK(proc);
1043 	}
1044 
1045 	if (pn->pn_flags & PFS_RAWWR) {
1046 		error = pn_fill(curthread, proc, pn, NULL, uio);
1047 		if (proc != NULL)
1048 			PRELE(proc);
1049 		PFS_RETURN (error);
1050 	}
1051 
1052 	sbuf_uionew(&sb, uio, &error);
1053 	if (error) {
1054 		if (proc != NULL)
1055 			PRELE(proc);
1056 		PFS_RETURN (error);
1057 	}
1058 
1059 	error = pn_fill(curthread, proc, pn, &sb, uio);
1060 
1061 	sbuf_delete(&sb);
1062 	if (proc != NULL)
1063 		PRELE(proc);
1064 	PFS_RETURN (error);
1065 }
1066 
1067 /*
1068  * Vnode operations
1069  */
1070 struct vop_vector pfs_vnodeops = {
1071 	.vop_default =		&default_vnodeops,
1072 
1073 	.vop_access =		pfs_access,
1074 	.vop_cachedlookup =	pfs_lookup,
1075 	.vop_close =		pfs_close,
1076 	.vop_create =		VOP_EOPNOTSUPP,
1077 	.vop_getattr =		pfs_getattr,
1078 	.vop_getextattr =	pfs_getextattr,
1079 	.vop_ioctl =		pfs_ioctl,
1080 	.vop_link =		VOP_EOPNOTSUPP,
1081 	.vop_lookup =		vfs_cache_lookup,
1082 	.vop_mkdir =		VOP_EOPNOTSUPP,
1083 	.vop_mknod =		VOP_EOPNOTSUPP,
1084 	.vop_open =		pfs_open,
1085 	.vop_read =		pfs_read,
1086 	.vop_readdir =		pfs_readdir,
1087 	.vop_readlink =		pfs_readlink,
1088 	.vop_reclaim =		pfs_reclaim,
1089 	.vop_remove =		VOP_EOPNOTSUPP,
1090 	.vop_rename =		VOP_EOPNOTSUPP,
1091 	.vop_rmdir =		VOP_EOPNOTSUPP,
1092 	.vop_setattr =		pfs_setattr,
1093 	.vop_symlink =		VOP_EOPNOTSUPP,
1094 	.vop_vptocnp =		pfs_vptocnp,
1095 	.vop_write =		pfs_write,
1096 	/* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */
1097 };
1098 VFS_VOP_VECTOR_REGISTER(pfs_vnodeops);
1099