2 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_pseudofs.h"
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>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
49 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
52 #include <fs/pseudofs/pseudofs.h>
53 #include <fs/pseudofs/pseudofs_internal.h>
57 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0,
58 "enable tracing of pseudofs vnode operations");
60 #define PFS_TRACE(foo) \
63 printf("%s(): line %d: ", __func__, __LINE__); \
68 #define PFS_RETURN(err) \
71 printf("%s(): line %d: returning %d\n", \
72 __func__, __LINE__, err); \
77 #define PFS_TRACE(foo) \
78 do { /* nothing */ } while (0)
79 #define PFS_RETURN(err) \
84 * Returns non-zero if given file is visible to given process
87 pfs_visible(struct thread *td, struct pfs_node *pn, pid_t pid)
92 PFS_TRACE(("%s (pid: %d, req: %d)",
93 pn->pn_name, pid, td->td_proc->p_pid));
95 if (pn->pn_flags & PFS_DISABLED)
100 if ((proc = pfind(pid)) == NULL)
102 if (p_cansee(td, proc) != 0 ||
103 (pn->pn_vis != NULL && !(pn->pn_vis)(td, proc, pn)))
114 pfs_access(struct vop_access_args *va)
116 struct vnode *vn = va->a_vp;
120 PFS_TRACE((((struct pfs_vdata *)vn->v_data)->pvd_pn->pn_name));
122 error = VOP_GETATTR(vn, &vattr, va->a_cred, va->a_td);
125 error = vaccess(vn->v_type, vattr.va_mode, vattr.va_uid,
126 vattr.va_gid, va->a_mode, va->a_cred, NULL);
131 * Close a file or directory
134 pfs_close(struct vop_close_args *va)
136 struct vnode *vn = va->a_vp;
137 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
138 struct pfs_node *pn = pvd->pvd_pn;
142 PFS_TRACE((pn->pn_name));
145 * Do nothing unless this is the last close and the node has a
146 * last-close handler.
148 if (vrefcnt(vn) > 1 || pn->pn_close == NULL)
151 if (pvd->pvd_pid != NO_PID)
152 proc = pfind(pvd->pvd_pid);
156 error = (pn->pn_close)(va->a_td, proc, pn);
165 * Get file attributes
168 pfs_getattr(struct vop_getattr_args *va)
170 struct vnode *vn = va->a_vp;
171 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
172 struct pfs_node *pn = pvd->pvd_pn;
173 struct vattr *vap = va->a_vap;
177 PFS_TRACE((pn->pn_name));
179 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
183 vap->va_type = vn->v_type;
184 vap->va_fileid = pn->pn_fileno;
186 vap->va_blocksize = PAGE_SIZE;
187 vap->va_bytes = vap->va_size = 0;
188 vap->va_fsid = vn->v_mount->mnt_stat.f_fsid.val[0];
190 nanotime(&vap->va_ctime);
191 vap->va_atime = vap->va_mtime = vap->va_ctime;
193 switch (pn->pn_type) {
194 case pfstype_procdir:
200 case pfstype_symlink:
204 printf("shouldn't be here!\n");
209 if (pvd->pvd_pid != NO_PID) {
210 if ((proc = pfind(pvd->pvd_pid)) == NULL)
212 vap->va_uid = proc->p_ucred->cr_ruid;
213 vap->va_gid = proc->p_ucred->cr_rgid;
214 if (pn->pn_attr != NULL)
215 error = (pn->pn_attr)(va->a_td, proc, pn, vap);
229 pfs_ioctl(struct vop_ioctl_args *va)
231 struct vnode *vn = va->a_vp;
232 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
233 struct pfs_node *pn = pvd->pvd_pn;
234 struct proc *proc = NULL;
237 PFS_TRACE(("%s: %lx", pn->pn_name, va->a_command));
239 if (vn->v_type != VREG)
242 if (pn->pn_ioctl == NULL)
246 * This is necessary because process' privileges may
247 * have changed since the open() call.
249 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
252 /* XXX duplicates bits of pfs_visible() */
253 if (pvd->pvd_pid != NO_PID) {
254 if ((proc = pfind(pvd->pvd_pid)) == NULL)
260 error = (pn->pn_ioctl)(curthread, proc, pn, va->a_command, va->a_data);
272 pfs_getextattr(struct vop_getextattr_args *va)
274 struct vnode *vn = va->a_vp;
275 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
276 struct pfs_node *pn = pvd->pvd_pn;
277 struct proc *proc = NULL;
280 PFS_TRACE((pn->pn_name));
282 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
285 if (pn->pn_getextattr == NULL)
286 PFS_RETURN (EOPNOTSUPP);
289 * This is necessary because either process' privileges may
290 * have changed since the open() call.
292 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
295 /* XXX duplicates bits of pfs_visible() */
296 if (pvd->pvd_pid != NO_PID) {
297 if ((proc = pfind(pvd->pvd_pid)) == NULL)
303 error = (pn->pn_getextattr)(curthread, proc, pn, va->a_attrnamespace,
304 va->a_name, va->a_uio, va->a_size, va->a_cred);
313 * Look up a file or directory
316 pfs_lookup(struct vop_cachedlookup_args *va)
318 struct vnode *vn = va->a_dvp;
319 struct vnode **vpp = va->a_vpp;
320 struct componentname *cnp = va->a_cnp;
321 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
322 struct pfs_node *pd = pvd->pvd_pn;
323 struct pfs_node *pn, *pdn = NULL;
324 pid_t pid = pvd->pvd_pid;
326 int error, i, namelen;
328 PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr));
330 if (vn->v_type != VDIR)
331 PFS_RETURN (ENOTDIR);
333 error = VOP_ACCESS(vn, VEXEC, cnp->cn_cred, cnp->cn_thread);
338 * Don't support DELETE or RENAME. CREATE is supported so
339 * that O_CREAT will work, but the lookup will still fail if
340 * the file does not exist.
342 if ((cnp->cn_flags & ISLASTCN) &&
343 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
344 PFS_RETURN (EOPNOTSUPP);
346 /* shortcut: check if the name is too long */
347 if (cnp->cn_namelen >= PFS_NAMELEN)
350 /* check that parent directory is visisble... */
351 if (!pfs_visible(curthread, pd, pvd->pvd_pid))
355 namelen = cnp->cn_namelen;
356 pname = cnp->cn_nameptr;
357 if (namelen == 1 && pname[0] == '.') {
365 if (cnp->cn_flags & ISDOTDOT) {
366 if (pd->pn_type == pfstype_root)
368 VOP_UNLOCK(vn, 0, cnp->cn_thread);
369 KASSERT(pd->pn_parent, ("non-root directory has no parent"));
371 * This one is tricky. Descendents of procdir nodes
372 * inherit their parent's process affinity, but
373 * there's no easy reverse mapping. For simplicity,
374 * we assume that if this node is a procdir, its
375 * parent isn't (which is correct as long as
376 * descendents of procdir nodes are never procdir
379 if (pd->pn_type == pfstype_procdir)
386 for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
387 if (pn->pn_type == pfstype_procdir)
389 else if (pn->pn_name[namelen] == '\0' &&
390 bcmp(pname, pn->pn_name, namelen) == 0)
393 /* process dependent node */
394 if ((pn = pdn) != NULL) {
396 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
397 if ((pid = pid * 10 + pname[i] - '0') > PID_MAX)
399 if (i == cnp->cn_namelen)
405 if (pn != pd->pn_parent && !pn->pn_parent)
407 if (!pfs_visible(curthread, pn, pvd->pvd_pid)) {
412 error = pfs_vncache_alloc(vn->v_mount, vpp, pn, pid);
416 if (cnp->cn_flags & ISDOTDOT)
417 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY, cnp->cn_thread);
418 if (cnp->cn_flags & MAKEENTRY)
419 cache_enter(vn, *vpp, cnp);
422 if (cnp->cn_flags & ISDOTDOT)
423 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY, cnp->cn_thread);
428 * Open a file or directory.
431 pfs_open(struct vop_open_args *va)
433 struct vnode *vn = va->a_vp;
434 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
435 struct pfs_node *pn = pvd->pvd_pn;
436 int mode = va->a_mode;
438 PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
441 * check if the file is visible to the caller
443 * XXX Not sure if this is necessary, as the VFS system calls
444 * XXX pfs_lookup() and pfs_access() first, and pfs_lookup()
445 * XXX calls pfs_visible(). There's a race condition here, but
446 * XXX calling pfs_visible() from here doesn't really close it,
447 * XXX and the only consequence of that race is an EIO further
450 if (!pfs_visible(va->a_td, pn, pvd->pvd_pid))
453 /* check if the requested mode is permitted */
454 if (((mode & FREAD) && !(mode & PFS_RD)) ||
455 ((mode & FWRITE) && !(mode & PFS_WR)))
458 /* we don't support locking */
459 if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
460 PFS_RETURN (EOPNOTSUPP);
469 pfs_read(struct vop_read_args *va)
471 struct vnode *vn = va->a_vp;
472 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
473 struct pfs_node *pn = pvd->pvd_pn;
474 struct uio *uio = va->a_uio;
475 struct proc *proc = NULL;
476 struct sbuf *sb = NULL;
478 unsigned int buflen, offset, resid;
480 PFS_TRACE((pn->pn_name));
482 if (vn->v_type != VREG)
485 if (!(pn->pn_flags & PFS_RD))
488 if (pn->pn_func == NULL)
492 * This is necessary because either process' privileges may
493 * have changed since the open() call.
495 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
498 /* XXX duplicates bits of pfs_visible() */
499 if (pvd->pvd_pid != NO_PID) {
500 if ((proc = pfind(pvd->pvd_pid)) == NULL)
506 if (pn->pn_flags & PFS_RAWRD) {
507 error = (pn->pn_func)(curthread, proc, pn, NULL, uio);
513 /* Beaucoup sanity checks so we don't ask for bogus allocation. */
514 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
515 (offset = uio->uio_offset) != uio->uio_offset ||
516 (resid = uio->uio_resid) != uio->uio_resid ||
517 (buflen = offset + resid) < offset || buflen > INT_MAX) {
522 if (buflen > MAXPHYS) {
527 sb = sbuf_new(sb, NULL, buflen, 0);
534 error = (pn->pn_func)(curthread, proc, pn, sb, uio);
545 error = uiomove_frombuf(sbuf_data(sb), sbuf_len(sb), uio);
551 * Iterate through directory entries
554 pfs_iterate(struct thread *td, pid_t pid, struct pfs_node *pd,
555 struct pfs_node **pn, struct proc **p)
557 sx_assert(&allproc_lock, SX_LOCKED);
562 } else if ((*pn)->pn_type != pfstype_procdir) {
564 *pn = (*pn)->pn_next;
566 if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
569 *p = LIST_FIRST(&allproc);
571 *p = LIST_NEXT(*p, p_list);
572 /* out of processes: next node */
574 *pn = (*pn)->pn_next;
580 if (!pfs_visible(td, *pn, *p ? (*p)->p_pid : pid))
587 * Return directory entries.
590 pfs_readdir(struct vop_readdir_args *va)
592 struct vnode *vn = va->a_vp;
593 struct pfs_info *pi = (struct pfs_info *)vn->v_mount->mnt_data;
594 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
595 struct pfs_node *pd = pvd->pvd_pn;
596 pid_t pid = pvd->pvd_pid;
605 PFS_TRACE((pd->pn_name));
607 if (vn->v_type != VDIR)
608 PFS_RETURN (ENOTDIR);
611 /* check if the directory is visible to the caller */
612 if (!pfs_visible(curthread, pd, pid))
615 /* only allow reading entire entries */
616 offset = uio->uio_offset;
617 resid = uio->uio_resid;
618 if (offset < 0 || offset % PFS_DELEN != 0 ||
619 (resid && resid < PFS_DELEN))
624 /* skip unwanted entries */
625 sx_slock(&allproc_lock);
626 for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN)
627 if (pfs_iterate(curthread, pid, pd, &pn, &p) == -1) {
628 /* nothing left... */
629 sx_sunlock(&allproc_lock);
633 /* fill in entries */
634 ent = buf = malloc(resid, M_IOV, M_WAITOK);
635 entry.d_reclen = PFS_DELEN;
636 while (pfs_iterate(curthread, pid, pd, &pn, &p) != -1 &&
637 resid >= PFS_DELEN) {
641 pfs_fileno_alloc(pi, pn);
643 entry.d_fileno = pn->pn_fileno * NO_PID + pid;
645 entry.d_fileno = pn->pn_fileno;
646 /* PFS_DELEN was picked to fit PFS_NAMLEN */
647 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
648 entry.d_name[i] = pn->pn_name[i];
651 switch (pn->pn_type) {
652 case pfstype_procdir:
654 ("reached procdir node with p == NULL"));
655 entry.d_fileno = pn->pn_fileno * NO_PID + p->p_pid;
656 entry.d_namlen = snprintf(entry.d_name,
657 PFS_NAMELEN, "%d", p->p_pid);
663 entry.d_type = DT_DIR;
666 entry.d_type = DT_REG;
668 case pfstype_symlink:
669 entry.d_type = DT_LNK;
672 panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
674 PFS_TRACE((entry.d_name));
675 bcopy(&entry, ent, PFS_DELEN); /* XXX waste of cycles */
680 sx_sunlock(&allproc_lock);
681 error = uiomove(buf, ent - buf, uio);
687 * Read a symbolic link
690 pfs_readlink(struct vop_readlink_args *va)
692 struct vnode *vn = va->a_vp;
693 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
694 struct pfs_node *pn = pvd->pvd_pn;
695 struct uio *uio = va->a_uio;
696 struct proc *proc = NULL;
697 char buf[MAXPATHLEN];
701 PFS_TRACE((pn->pn_name));
703 if (vn->v_type != VLNK)
706 if (pn->pn_func == NULL)
709 if (pvd->pvd_pid != NO_PID) {
710 if ((proc = pfind(pvd->pvd_pid)) == NULL)
716 /* sbuf_new() can't fail with a static buffer */
717 sbuf_new(&sb, buf, sizeof buf, 0);
719 error = (pn->pn_func)(curthread, proc, pn, &sb, NULL);
730 error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
739 pfs_reclaim(struct vop_reclaim_args *va)
741 PFS_TRACE((((struct pfs_vdata *)va->a_vp->v_data)->pvd_pn->pn_name));
743 return (pfs_vncache_free(va->a_vp));
750 pfs_setattr(struct vop_setattr_args *va)
752 PFS_TRACE((((struct pfs_vdata *)va->a_vp->v_data)->pvd_pn->pn_name));
754 PFS_RETURN (EOPNOTSUPP);
761 pfs_write(struct vop_write_args *va)
763 struct vnode *vn = va->a_vp;
764 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
765 struct pfs_node *pn = pvd->pvd_pn;
766 struct uio *uio = va->a_uio;
767 struct proc *proc = NULL;
771 PFS_TRACE((pn->pn_name));
773 if (vn->v_type != VREG)
776 if (!(pn->pn_flags & PFS_WR))
779 if (pn->pn_func == NULL)
783 * This is necessary because either process' privileges may
784 * have changed since the open() call.
786 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
789 /* XXX duplicates bits of pfs_visible() */
790 if (pvd->pvd_pid != NO_PID) {
791 if ((proc = pfind(pvd->pvd_pid)) == NULL)
797 if (pn->pn_flags & PFS_RAWWR) {
798 error = (pn->pn_func)(curthread, proc, pn, NULL, uio);
804 sbuf_uionew(&sb, uio, &error);
808 error = (pn->pn_func)(curthread, proc, pn, &sb, uio);
820 struct vop_vector pfs_vnodeops = {
821 .vop_default = &default_vnodeops,
823 .vop_access = pfs_access,
824 .vop_cachedlookup = pfs_lookup,
825 .vop_close = pfs_close,
826 .vop_create = VOP_EOPNOTSUPP,
827 .vop_getattr = pfs_getattr,
828 .vop_getextattr = pfs_getextattr,
829 .vop_ioctl = pfs_ioctl,
830 .vop_link = VOP_EOPNOTSUPP,
831 .vop_lookup = vfs_cache_lookup,
832 .vop_mkdir = VOP_EOPNOTSUPP,
833 .vop_mknod = VOP_EOPNOTSUPP,
834 .vop_open = pfs_open,
835 .vop_read = pfs_read,
836 .vop_readdir = pfs_readdir,
837 .vop_readlink = pfs_readlink,
838 .vop_reclaim = pfs_reclaim,
839 .vop_remove = VOP_EOPNOTSUPP,
840 .vop_rename = VOP_EOPNOTSUPP,
841 .vop_rmdir = VOP_EOPNOTSUPP,
842 .vop_setattr = pfs_setattr,
843 .vop_symlink = VOP_EOPNOTSUPP,
844 .vop_write = pfs_write,
845 /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */