2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_pseudofs.h"
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>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/namei.h>
51 #include <sys/sysctl.h>
52 #include <sys/vnode.h>
54 #include <fs/pseudofs/pseudofs.h>
55 #include <fs/pseudofs/pseudofs_internal.h>
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__))
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__))
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__))
72 * Returns the fileno, adjusted for target pid
75 pn_fileno(struct pfs_node *pn, pid_t pid)
78 KASSERT(pn->pn_fileno > 0,
79 ("%s(): no fileno allocated", __func__));
81 return (pn->pn_fileno * NO_PID + pid);
82 return (pn->pn_fileno);
86 * Returns non-zero if given file is visible to given thread.
89 pfs_visible_proc(struct thread *td, struct pfs_node *pn, struct proc *proc)
96 PROC_LOCK_ASSERT(proc, MA_OWNED);
98 visible = ((proc->p_flag & P_WEXIT) == 0);
100 visible = (p_cansee(td, proc) == 0);
101 if (visible && pn->pn_vis != NULL)
102 visible = pn_vis(td, proc, pn);
109 pfs_visible(struct thread *td, struct pfs_node *pn, pid_t pid,
110 bool allproc_locked, struct proc **p)
114 PFS_TRACE(("%s (pid: %d, req: %d)",
115 pn->pn_name, pid, td->td_proc->p_pid));
121 proc = allproc_locked ? pfind_locked(pid) : pfind(pid);
124 if (pfs_visible_proc(td, pn, proc)) {
139 pfs_access(struct vop_access_args *va)
141 struct vnode *vn = va->a_vp;
142 struct pfs_vdata *pvd = vn->v_data;
146 PFS_TRACE(("%s", pvd->pvd_pn->pn_name));
149 error = VOP_GETATTR(vn, &vattr, va->a_cred);
152 error = vaccess(vn->v_type, vattr.va_mode, vattr.va_uid,
153 vattr.va_gid, va->a_accmode, va->a_cred, NULL);
158 * Close a file or directory
161 pfs_close(struct vop_close_args *va)
163 struct vnode *vn = va->a_vp;
164 struct pfs_vdata *pvd = vn->v_data;
165 struct pfs_node *pn = pvd->pvd_pn;
169 PFS_TRACE(("%s", pn->pn_name));
170 pfs_assert_not_owned(pn);
173 * Do nothing unless this is the last close and the node has a
174 * last-close handler.
176 if (vrefcnt(vn) > 1 || pn->pn_close == NULL)
179 if (pvd->pvd_pid != NO_PID) {
180 proc = pfind(pvd->pvd_pid);
185 error = pn_close(va->a_td, proc, pn);
194 * Get file attributes
197 pfs_getattr(struct vop_getattr_args *va)
199 struct vnode *vn = va->a_vp;
200 struct pfs_vdata *pvd = vn->v_data;
201 struct pfs_node *pn = pvd->pvd_pn;
202 struct vattr *vap = va->a_vap;
206 PFS_TRACE(("%s", pn->pn_name));
207 pfs_assert_not_owned(pn);
209 if (!pfs_visible(curthread, pn, pvd->pvd_pid, false, &proc))
212 vap->va_type = vn->v_type;
213 vap->va_fileid = pn_fileno(pn, pvd->pvd_pid);
215 vap->va_blocksize = PAGE_SIZE;
216 vap->va_bytes = vap->va_size = 0;
218 vap->va_fsid = vn->v_mount->mnt_stat.f_fsid.val[0];
220 nanotime(&vap->va_ctime);
221 vap->va_atime = vap->va_mtime = vap->va_ctime;
223 switch (pn->pn_type) {
224 case pfstype_procdir:
229 /* compute link count */
235 case pfstype_symlink:
239 printf("shouldn't be here!\n");
245 vap->va_uid = proc->p_ucred->cr_ruid;
246 vap->va_gid = proc->p_ucred->cr_rgid;
252 if (pn->pn_attr != NULL)
253 error = pn_attr(curthread, proc, pn, vap);
265 pfs_ioctl(struct vop_ioctl_args *va)
268 struct pfs_vdata *pvd;
274 vn_lock(vn, LK_SHARED | LK_RETRY);
275 if (vn->v_iflag & VI_DOOMED) {
282 PFS_TRACE(("%s: %lx", pn->pn_name, va->a_command));
283 pfs_assert_not_owned(pn);
285 if (vn->v_type != VREG) {
289 KASSERT_PN_IS_FILE(pn);
291 if (pn->pn_ioctl == NULL) {
297 * This is necessary because process' privileges may
298 * have changed since the open() call.
300 if (!pfs_visible(curthread, pn, pvd->pvd_pid, false, &proc)) {
305 error = pn_ioctl(curthread, proc, pn, va->a_command, va->a_data);
318 pfs_getextattr(struct vop_getextattr_args *va)
320 struct vnode *vn = va->a_vp;
321 struct pfs_vdata *pvd = vn->v_data;
322 struct pfs_node *pn = pvd->pvd_pn;
326 PFS_TRACE(("%s", pn->pn_name));
327 pfs_assert_not_owned(pn);
330 * This is necessary because either process' privileges may
331 * have changed since the open() call.
333 if (!pfs_visible(curthread, pn, pvd->pvd_pid, false, &proc))
336 if (pn->pn_getextattr == NULL)
339 error = pn_getextattr(curthread, proc, pn,
340 va->a_attrnamespace, va->a_name, va->a_uio,
341 va->a_size, va->a_cred);
350 * Convert a vnode to its component name
353 pfs_vptocnp(struct vop_vptocnp_args *ap)
355 struct vnode *vp = ap->a_vp;
356 struct vnode **dvp = ap->a_vpp;
357 struct pfs_vdata *pvd = vp->v_data;
358 struct pfs_node *pd = pvd->pvd_pn;
361 char *buf = ap->a_buf;
362 int *buflen = ap->a_buflen;
363 char pidbuf[PFS_NAMELEN];
364 pid_t pid = pvd->pvd_pid;
365 int len, i, error, locked;
372 if (vp->v_type == VDIR && pd->pn_type == pfstype_root) {
377 } else if (vp->v_type == VDIR && pd->pn_type == pfstype_procdir) {
378 len = snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
384 bcopy(pidbuf, buf + i, len);
386 len = strlen(pd->pn_name);
392 bcopy(pd->pn_name, buf + i, len);
399 error = vfs_busy(mp, 0);
404 * vp is held by caller.
406 locked = VOP_ISLOCKED(vp);
409 error = pfs_vncache_alloc(mp, dvp, pn, pid);
411 vn_lock(vp, locked | LK_RETRY);
418 vn_lock(vp, locked | LK_RETRY);
428 * Look up a file or directory
431 pfs_lookup(struct vop_cachedlookup_args *va)
433 struct vnode *vn = va->a_dvp;
434 struct vnode **vpp = va->a_vpp;
435 struct componentname *cnp = va->a_cnp;
436 struct pfs_vdata *pvd = vn->v_data;
437 struct pfs_node *pd = pvd->pvd_pn;
438 struct pfs_node *pn, *pdn = NULL;
440 pid_t pid = pvd->pvd_pid;
442 int error, i, namelen, visible;
444 PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr));
445 pfs_assert_not_owned(pd);
447 if (vn->v_type != VDIR)
448 PFS_RETURN (ENOTDIR);
449 KASSERT_PN_IS_DIR(pd);
451 error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread);
456 * Don't support DELETE or RENAME. CREATE is supported so
457 * that O_CREAT will work, but the lookup will still fail if
458 * the file does not exist.
460 if ((cnp->cn_flags & ISLASTCN) &&
461 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
462 PFS_RETURN (EOPNOTSUPP);
464 /* shortcut: check if the name is too long */
465 if (cnp->cn_namelen >= PFS_NAMELEN)
468 /* check that parent directory is visible... */
469 if (!pfs_visible(curthread, pd, pvd->pvd_pid, false, NULL))
473 namelen = cnp->cn_namelen;
474 pname = cnp->cn_nameptr;
475 if (namelen == 1 && pname[0] == '.') {
485 if (cnp->cn_flags & ISDOTDOT) {
486 if (pd->pn_type == pfstype_root)
488 error = vfs_busy(mp, MBF_NOWAIT);
492 error = vfs_busy(mp, 0);
493 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
497 if (vn->v_iflag & VI_DOOMED) {
503 KASSERT(pd->pn_parent != NULL,
504 ("%s(): non-root directory has no parent", __func__));
506 * This one is tricky. Descendents of procdir nodes
507 * inherit their parent's process affinity, but
508 * there's no easy reverse mapping. For simplicity,
509 * we assume that if this node is a procdir, its
510 * parent isn't (which is correct as long as
511 * descendents of procdir nodes are never procdir
514 if (pd->pn_type == pfstype_procdir)
525 for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
526 if (pn->pn_type == pfstype_procdir)
528 else if (pn->pn_name[namelen] == '\0' &&
529 bcmp(pname, pn->pn_name, namelen) == 0) {
534 /* process dependent node */
535 if ((pn = pdn) != NULL) {
537 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
538 if ((pid = pid * 10 + pname[i] - '0') > PID_MAX)
540 if (i == cnp->cn_namelen) {
551 pfs_assert_not_owned(pd);
552 pfs_assert_not_owned(pn);
553 visible = pfs_visible(curthread, pn, pid, false, NULL);
559 error = pfs_vncache_alloc(mp, vpp, pn, pid);
563 if (cnp->cn_flags & ISDOTDOT) {
565 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
566 if (vn->v_iflag & VI_DOOMED) {
572 if (cnp->cn_flags & MAKEENTRY && !(vn->v_iflag & VI_DOOMED))
573 cache_enter(vn, *vpp, cnp);
576 if (cnp->cn_flags & ISDOTDOT) {
578 vn_lock(vn, LK_EXCLUSIVE | LK_RETRY);
585 * Open a file or directory.
588 pfs_open(struct vop_open_args *va)
590 struct vnode *vn = va->a_vp;
591 struct pfs_vdata *pvd = vn->v_data;
592 struct pfs_node *pn = pvd->pvd_pn;
593 int mode = va->a_mode;
595 PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
596 pfs_assert_not_owned(pn);
598 /* check if the requested mode is permitted */
599 if (((mode & FREAD) && !(mode & PFS_RD)) ||
600 ((mode & FWRITE) && !(mode & PFS_WR)))
603 /* we don't support locking */
604 if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
605 PFS_RETURN (EOPNOTSUPP);
614 pfs_read(struct vop_read_args *va)
616 struct vnode *vn = va->a_vp;
617 struct pfs_vdata *pvd = vn->v_data;
618 struct pfs_node *pn = pvd->pvd_pn;
619 struct uio *uio = va->a_uio;
621 struct sbuf *sb = NULL;
625 PFS_TRACE(("%s", pn->pn_name));
626 pfs_assert_not_owned(pn);
628 if (vn->v_type != VREG)
630 KASSERT_PN_IS_FILE(pn);
632 if (!(pn->pn_flags & PFS_RD))
635 if (pn->pn_fill == NULL)
639 * This is necessary because either process' privileges may
640 * have changed since the open() call.
642 if (!pfs_visible(curthread, pn, pvd->pvd_pid, false, &proc))
650 locked = VOP_ISLOCKED(vn);
653 if (pn->pn_flags & PFS_RAWRD) {
654 PFS_TRACE(("%zd resid", uio->uio_resid));
655 error = pn_fill(curthread, proc, pn, NULL, uio);
656 PFS_TRACE(("%zd resid", uio->uio_resid));
660 if (uio->uio_resid < 0 || uio->uio_offset < 0 ||
661 uio->uio_resid > OFF_MAX - uio->uio_offset) {
665 buflen = uio->uio_offset + uio->uio_resid;
666 if (buflen > MAXPHYS)
669 sb = sbuf_new(sb, NULL, buflen + 1, 0);
675 error = pn_fill(curthread, proc, pn, sb, uio);
683 * XXX: If the buffer overflowed, sbuf_len() will not return
684 * the data length. Then just use the full length because an
685 * overflowed sbuf must be full.
687 if (sbuf_finish(sb) == 0)
688 buflen = sbuf_len(sb);
689 error = uiomove_frombuf(sbuf_data(sb), buflen, uio);
692 vn_lock(vn, locked | LK_RETRY);
700 * Iterate through directory entries
703 pfs_iterate(struct thread *td, struct proc *proc, struct pfs_node *pd,
704 struct pfs_node **pn, struct proc **p)
708 sx_assert(&allproc_lock, SX_SLOCKED);
709 pfs_assert_owned(pd);
714 } else if ((*pn)->pn_type != pfstype_procdir) {
716 *pn = (*pn)->pn_next;
718 if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
721 *p = LIST_FIRST(&allproc);
723 *p = LIST_NEXT(*p, p_list);
724 /* out of processes: next node */
726 *pn = (*pn)->pn_next;
735 visible = pfs_visible_proc(td, *pn, *p);
737 } else if (proc != NULL) {
738 visible = pfs_visible_proc(td, *pn, proc);
748 /* Directory entry list */
750 STAILQ_ENTRY(pfsentry) link;
753 STAILQ_HEAD(pfsdirentlist, pfsentry);
756 * Return directory entries.
759 pfs_readdir(struct vop_readdir_args *va)
761 struct vnode *vn = va->a_vp;
762 struct pfs_vdata *pvd = vn->v_data;
763 struct pfs_node *pd = pvd->pvd_pn;
764 pid_t pid = pvd->pvd_pid;
765 struct proc *p, *proc;
768 struct pfsentry *pfsent, *pfsent2;
769 struct pfsdirentlist lst;
775 KASSERT(pd->pn_info == vn->v_mount->mnt_data,
776 ("%s(): pn_info does not match mountpoint", __func__));
777 PFS_TRACE(("%s pid %lu", pd->pn_name, (unsigned long)pid));
778 pfs_assert_not_owned(pd);
780 if (vn->v_type != VDIR)
781 PFS_RETURN (ENOTDIR);
782 KASSERT_PN_IS_DIR(pd);
785 /* only allow reading entire entries */
786 offset = uio->uio_offset;
787 resid = uio->uio_resid;
788 if (offset < 0 || offset % PFS_DELEN != 0 ||
789 (resid && resid < PFS_DELEN))
794 sx_slock(&allproc_lock);
797 /* check if the directory is visible to the caller */
798 if (!pfs_visible(curthread, pd, pid, true, &proc)) {
799 sx_sunlock(&allproc_lock);
803 KASSERT(pid == NO_PID || proc != NULL,
804 ("%s(): no process for pid %lu", __func__, (unsigned long)pid));
806 /* skip unwanted entries */
807 for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN) {
808 if (pfs_iterate(curthread, proc, pd, &pn, &p) == -1) {
809 /* nothing left... */
813 sx_sunlock(&allproc_lock);
818 /* fill in entries */
819 while (pfs_iterate(curthread, proc, pd, &pn, &p) != -1 &&
820 resid >= PFS_DELEN) {
821 if ((pfsent = malloc(sizeof(struct pfsentry), M_IOV,
822 M_NOWAIT | M_ZERO)) == NULL) {
826 pfsent->entry.d_reclen = PFS_DELEN;
827 pfsent->entry.d_fileno = pn_fileno(pn, pid);
828 /* PFS_DELEN was picked to fit PFS_NAMLEN */
829 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
830 pfsent->entry.d_name[i] = pn->pn_name[i];
831 pfsent->entry.d_name[i] = 0;
832 pfsent->entry.d_namlen = i;
833 switch (pn->pn_type) {
834 case pfstype_procdir:
836 ("reached procdir node with p == NULL"));
837 pfsent->entry.d_namlen = snprintf(pfsent->entry.d_name,
838 PFS_NAMELEN, "%d", p->p_pid);
844 pfsent->entry.d_type = DT_DIR;
847 pfsent->entry.d_type = DT_REG;
849 case pfstype_symlink:
850 pfsent->entry.d_type = DT_LNK;
853 panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
855 PFS_TRACE(("%s", pfsent->entry.d_name));
856 STAILQ_INSERT_TAIL(&lst, pfsent, link);
863 sx_sunlock(&allproc_lock);
865 STAILQ_FOREACH_SAFE(pfsent, &lst, link, pfsent2) {
867 error = uiomove(&pfsent->entry, PFS_DELEN, uio);
871 PFS_TRACE(("%ju bytes", (uintmax_t)(i * PFS_DELEN)));
876 * Read a symbolic link
879 pfs_readlink(struct vop_readlink_args *va)
881 struct vnode *vn = va->a_vp;
882 struct pfs_vdata *pvd = vn->v_data;
883 struct pfs_node *pn = pvd->pvd_pn;
884 struct uio *uio = va->a_uio;
885 struct proc *proc = NULL;
890 PFS_TRACE(("%s", pn->pn_name));
891 pfs_assert_not_owned(pn);
893 if (vn->v_type != VLNK)
895 KASSERT_PN_IS_LINK(pn);
897 if (pn->pn_fill == NULL)
900 if (pvd->pvd_pid != NO_PID) {
901 if ((proc = pfind(pvd->pvd_pid)) == NULL)
903 if (proc->p_flag & P_WEXIT) {
911 locked = VOP_ISLOCKED(vn);
914 /* sbuf_new() can't fail with a static buffer */
915 sbuf_new(&sb, buf, sizeof buf, 0);
917 error = pn_fill(curthread, proc, pn, &sb, NULL);
921 vn_lock(vn, locked | LK_RETRY);
929 if (sbuf_finish(&sb) != 0) {
931 PFS_RETURN (ENAMETOOLONG);
934 error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
943 pfs_reclaim(struct vop_reclaim_args *va)
945 struct vnode *vn = va->a_vp;
946 struct pfs_vdata *pvd = vn->v_data;
947 struct pfs_node *pn = pvd->pvd_pn;
949 PFS_TRACE(("%s", pn->pn_name));
950 pfs_assert_not_owned(pn);
952 return (pfs_vncache_free(va->a_vp));
959 pfs_setattr(struct vop_setattr_args *va)
961 struct vnode *vn = va->a_vp;
962 struct pfs_vdata *pvd = vn->v_data;
963 struct pfs_node *pn = pvd->pvd_pn;
965 PFS_TRACE(("%s", pn->pn_name));
966 pfs_assert_not_owned(pn);
968 PFS_RETURN (EOPNOTSUPP);
975 pfs_write(struct vop_write_args *va)
977 struct vnode *vn = va->a_vp;
978 struct pfs_vdata *pvd = vn->v_data;
979 struct pfs_node *pn = pvd->pvd_pn;
980 struct uio *uio = va->a_uio;
985 PFS_TRACE(("%s", pn->pn_name));
986 pfs_assert_not_owned(pn);
988 if (vn->v_type != VREG)
990 KASSERT_PN_IS_FILE(pn);
992 if (!(pn->pn_flags & PFS_WR))
995 if (pn->pn_fill == NULL)
999 * This is necessary because either process' privileges may
1000 * have changed since the open() call.
1002 if (!pfs_visible(curthread, pn, pvd->pvd_pid, false, &proc))
1009 if (pn->pn_flags & PFS_RAWWR) {
1010 error = pn_fill(curthread, proc, pn, NULL, uio);
1016 sbuf_uionew(&sb, uio, &error);
1023 error = pn_fill(curthread, proc, pn, &sb, uio);
1034 struct vop_vector pfs_vnodeops = {
1035 .vop_default = &default_vnodeops,
1037 .vop_access = pfs_access,
1038 .vop_cachedlookup = pfs_lookup,
1039 .vop_close = pfs_close,
1040 .vop_create = VOP_EOPNOTSUPP,
1041 .vop_getattr = pfs_getattr,
1042 .vop_getextattr = pfs_getextattr,
1043 .vop_ioctl = pfs_ioctl,
1044 .vop_link = VOP_EOPNOTSUPP,
1045 .vop_lookup = vfs_cache_lookup,
1046 .vop_mkdir = VOP_EOPNOTSUPP,
1047 .vop_mknod = VOP_EOPNOTSUPP,
1048 .vop_open = pfs_open,
1049 .vop_read = pfs_read,
1050 .vop_readdir = pfs_readdir,
1051 .vop_readlink = pfs_readlink,
1052 .vop_reclaim = pfs_reclaim,
1053 .vop_remove = VOP_EOPNOTSUPP,
1054 .vop_rename = VOP_EOPNOTSUPP,
1055 .vop_rmdir = VOP_EOPNOTSUPP,
1056 .vop_setattr = pfs_setattr,
1057 .vop_symlink = VOP_EOPNOTSUPP,
1058 .vop_vptocnp = pfs_vptocnp,
1059 .vop_write = pfs_write,
1060 /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */