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.
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/ctype.h>
35 #include <sys/dirent.h>
36 #include <sys/fcntl.h>
37 #include <sys/limits.h>
39 #include <sys/mount.h>
40 #include <sys/mutex.h>
41 #include <sys/namei.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
48 #include <fs/pseudofs/pseudofs.h>
49 #include <fs/pseudofs/pseudofs_internal.h>
52 #define PFS_TRACE(foo) \
54 printf("pseudofs: %s(): line %d: ", __func__, __LINE__); \
58 #define PFS_RETURN(err) \
60 printf("pseudofs: %s(): line %d: returning %d\n", \
61 __func__, __LINE__, err); \
65 #define PFS_TRACE(foo) \
66 do { /* nothing */ } while (0)
67 #define PFS_RETURN(err) \
72 * Returns non-zero if given file is visible to given process
75 pfs_visible(struct thread *td, struct pfs_node *pn, pid_t pid)
80 PFS_TRACE(("%s (pid: %d, req: %d)",
81 pn->pn_name, pid, td->td_proc->p_pid));
83 if (pn->pn_flags & PFS_DISABLED)
88 if ((proc = pfind(pid)) == NULL)
90 if (p_cansee(td, proc) != 0 ||
91 (pn->pn_vis != NULL && !(pn->pn_vis)(td, proc, pn)))
102 pfs_access(struct vop_access_args *va)
104 struct vnode *vn = va->a_vp;
108 PFS_TRACE((((struct pfs_vdata *)vn->v_data)->pvd_pn->pn_name));
110 error = VOP_GETATTR(vn, &vattr, va->a_cred, va->a_td);
113 error = vaccess(vn->v_type, vattr.va_mode, vattr.va_uid,
114 vattr.va_gid, va->a_mode, va->a_cred, NULL);
119 * Close a file or directory
122 pfs_close(struct vop_close_args *va)
124 struct vnode *vn = va->a_vp;
125 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
126 struct pfs_node *pn = pvd->pvd_pn;
130 PFS_TRACE((pn->pn_name));
133 * Do nothing unless this is the last close and the node has a
134 * last-close handler.
136 if (vrefcnt(vn) > 1 || pn->pn_close == NULL)
139 if (pvd->pvd_pid != NO_PID)
140 proc = pfind(pvd->pvd_pid);
144 error = (pn->pn_close)(va->a_td, proc, pn);
153 * Get file attributes
156 pfs_getattr(struct vop_getattr_args *va)
158 struct vnode *vn = va->a_vp;
159 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
160 struct pfs_node *pn = pvd->pvd_pn;
161 struct vattr *vap = va->a_vap;
165 PFS_TRACE((pn->pn_name));
167 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
171 vap->va_type = vn->v_type;
172 vap->va_fileid = pn->pn_fileno;
174 vap->va_blocksize = PAGE_SIZE;
175 vap->va_bytes = vap->va_size = 0;
176 vap->va_fsid = vn->v_mount->mnt_stat.f_fsid.val[0];
178 nanotime(&vap->va_ctime);
179 vap->va_atime = vap->va_mtime = vap->va_ctime;
181 switch (pn->pn_type) {
182 case pfstype_procdir:
188 case pfstype_symlink:
192 printf("shouldn't be here!\n");
197 if (pvd->pvd_pid != NO_PID) {
198 if ((proc = pfind(pvd->pvd_pid)) == NULL)
200 vap->va_uid = proc->p_ucred->cr_ruid;
201 vap->va_gid = proc->p_ucred->cr_rgid;
202 if (pn->pn_attr != NULL)
203 error = (pn->pn_attr)(va->a_td, proc, pn, vap);
217 pfs_ioctl(struct vop_ioctl_args *va)
219 struct vnode *vn = va->a_vp;
220 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
221 struct pfs_node *pn = pvd->pvd_pn;
222 struct proc *proc = NULL;
225 PFS_TRACE(("%s: %lx", pn->pn_name, va->a_command));
227 if (vn->v_type != VREG)
230 if (pn->pn_ioctl == NULL)
234 * This is necessary because process' privileges may
235 * have changed since the open() call.
237 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
240 /* XXX duplicates bits of pfs_visible() */
241 if (pvd->pvd_pid != NO_PID) {
242 if ((proc = pfind(pvd->pvd_pid)) == NULL)
248 error = (pn->pn_ioctl)(curthread, proc, pn, va->a_command, va->a_data);
260 pfs_getextattr(struct vop_getextattr_args *va)
262 struct vnode *vn = va->a_vp;
263 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
264 struct pfs_node *pn = pvd->pvd_pn;
265 struct proc *proc = NULL;
268 PFS_TRACE((pn->pn_name));
270 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
273 if (pn->pn_getextattr == NULL)
274 PFS_RETURN (EOPNOTSUPP);
277 * This is necessary because either process' privileges may
278 * have changed since the open() call.
280 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
283 /* XXX duplicates bits of pfs_visible() */
284 if (pvd->pvd_pid != NO_PID) {
285 if ((proc = pfind(pvd->pvd_pid)) == NULL)
291 error = (pn->pn_getextattr)(curthread, proc, pn, va->a_attrnamespace,
292 va->a_name, va->a_uio, va->a_size, va->a_cred);
301 * Look up a file or directory
303 * XXX NOTE! pfs_lookup() has been hooked into vop_lookup_desc! This
304 * will result in a lookup operation for a vnode which may already be
305 * cached, therefore we have to be careful to purge the VFS cache when
308 * This code will work, but is not really correct. Normally we would hook
309 * vfs_cache_lookup() into vop_lookup_desc and hook pfs_lookup() into
310 * vop_cachedlookup_desc.
313 pfs_lookup(struct vop_lookup_args *va)
315 struct vnode *vn = va->a_dvp;
316 struct vnode **vpp = va->a_vpp;
317 struct componentname *cnp = va->a_cnp;
318 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
319 struct pfs_node *pd = pvd->pvd_pn;
320 struct pfs_node *pn, *pdn = NULL;
321 pid_t pid = pvd->pvd_pid;
325 int error, i, namelen;
327 PFS_TRACE(("%.*s", (int)cnp->cn_namelen, cnp->cn_nameptr));
329 cnp->cn_flags &= ~PDIRUNLOCK;
331 if (vn->v_type != VDIR)
332 PFS_RETURN (ENOTDIR);
335 * Don't support DELETE or RENAME. CREATE is supported so
336 * that O_CREAT will work, but the lookup will still fail if
337 * the file does not exist.
339 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
340 PFS_RETURN (EOPNOTSUPP);
342 /* shortcut: check if the name is too long */
343 if (cnp->cn_namelen >= PFS_NAMELEN)
346 /* check that parent directory is visisble... */
347 if (!pfs_visible(curthread, pd, pvd->pvd_pid))
350 lockparent = cnp->cn_flags & LOCKPARENT;
351 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
355 namelen = cnp->cn_namelen;
356 pname = cnp->cn_nameptr;
357 if (namelen == 1 && *pname == '.') {
365 if (cnp->cn_flags & ISDOTDOT) {
366 if (pd->pn_type == pfstype_root)
368 VOP_UNLOCK(vn, 0, cnp->cn_thread);
369 cnp->cn_flags |= PDIRUNLOCK;
371 KASSERT(pd->pn_parent, ("non-root directory has no parent"));
373 * This one is tricky. Descendents of procdir nodes
374 * inherit their parent's process affinity, but
375 * there's no easy reverse mapping. For simplicity,
376 * we assume that if this node is a procdir, its
377 * parent isn't (which is correct as long as
378 * descendents of procdir nodes are never procdir
381 if (pd->pn_type == pfstype_procdir)
388 for (pn = pd->pn_nodes; pn != NULL; pn = pn->pn_next)
389 if (pn->pn_type == pfstype_procdir)
391 else if (pn->pn_name[namelen] == '\0'
392 && bcmp(pname, pn->pn_name, namelen) == 0)
395 /* process dependent node */
396 if ((pn = pdn) != NULL) {
398 for (pid = 0, i = 0; i < namelen && isdigit(pname[i]); ++i)
399 if ((pid = pid * 10 + pname[i] - '0') > PID_MAX)
401 if (i == cnp->cn_namelen)
407 if (pn != pd->pn_parent && !pn->pn_parent)
409 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) && (cnp->cn_flags & ISLASTCN)
418 vn_lock(vn, LK_EXCLUSIVE|LK_RETRY, cnp->cn_thread);
419 cnp->cn_flags &= ~PDIRUNLOCK;
421 if (!((lockparent && (cnp->cn_flags & ISLASTCN)) ||
422 (cnp->cn_flags & ISDOTDOT)))
423 VOP_UNLOCK(vn, 0, cnp->cn_thread);
426 * XXX See comment at top of the routine.
428 if (cnp->cn_flags & MAKEENTRY)
429 cache_enter(vn, *vpp, cnp);
434 * Open a file or directory.
437 pfs_open(struct vop_open_args *va)
439 struct vnode *vn = va->a_vp;
440 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
441 struct pfs_node *pn = pvd->pvd_pn;
442 int mode = va->a_mode;
444 PFS_TRACE(("%s (mode 0x%x)", pn->pn_name, mode));
447 * check if the file is visible to the caller
449 * XXX Not sure if this is necessary, as the VFS system calls
450 * XXX pfs_lookup() and pfs_access() first, and pfs_lookup()
451 * XXX calls pfs_visible(). There's a race condition here, but
452 * XXX calling pfs_visible() from here doesn't really close it,
453 * XXX and the only consequence of that race is an EIO further
456 if (!pfs_visible(va->a_td, pn, pvd->pvd_pid))
459 /* check if the requested mode is permitted */
460 if (((mode & FREAD) && !(mode & PFS_RD)) ||
461 ((mode & FWRITE) && !(mode & PFS_WR)))
464 /* we don't support locking */
465 if ((mode & O_SHLOCK) || (mode & O_EXLOCK))
466 PFS_RETURN (EOPNOTSUPP);
475 pfs_read(struct vop_read_args *va)
477 struct vnode *vn = va->a_vp;
478 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
479 struct pfs_node *pn = pvd->pvd_pn;
480 struct uio *uio = va->a_uio;
481 struct proc *proc = NULL;
482 struct sbuf *sb = NULL;
484 unsigned int buflen, offset, resid;
486 PFS_TRACE((pn->pn_name));
488 if (vn->v_type != VREG)
491 if (!(pn->pn_flags & PFS_RD))
494 if (pn->pn_func == NULL)
498 * This is necessary because either process' privileges may
499 * have changed since the open() call.
501 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
504 /* XXX duplicates bits of pfs_visible() */
505 if (pvd->pvd_pid != NO_PID) {
506 if ((proc = pfind(pvd->pvd_pid)) == NULL)
512 if (pn->pn_flags & PFS_RAWRD) {
513 error = (pn->pn_func)(curthread, proc, pn, NULL, uio);
519 /* Beaucoup sanity checks so we don't ask for bogus allocation. */
520 if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
521 (offset = uio->uio_offset) != uio->uio_offset ||
522 (resid = uio->uio_resid) != uio->uio_resid ||
523 (buflen = offset + resid) < offset || buflen > INT_MAX) {
528 if (buflen > MAXPHYS) {
533 sb = sbuf_new(sb, NULL, buflen, 0);
540 error = (pn->pn_func)(curthread, proc, pn, sb, uio);
551 error = uiomove_frombuf(sbuf_data(sb), sbuf_len(sb), uio);
557 * Iterate through directory entries
560 pfs_iterate(struct thread *td, pid_t pid, struct pfs_node *pd,
561 struct pfs_node **pn, struct proc **p)
563 sx_assert(&allproc_lock, SX_LOCKED);
568 } else if ((*pn)->pn_type != pfstype_procdir) {
570 *pn = (*pn)->pn_next;
572 if (*pn != NULL && (*pn)->pn_type == pfstype_procdir) {
575 *p = LIST_FIRST(&allproc);
577 *p = LIST_NEXT(*p, p_list);
578 /* out of processes: next node */
580 *pn = (*pn)->pn_next;
586 if (!pfs_visible(td, *pn, *p ? (*p)->p_pid : pid))
593 * Return directory entries.
596 pfs_readdir(struct vop_readdir_args *va)
598 struct vnode *vn = va->a_vp;
599 struct pfs_info *pi = (struct pfs_info *)vn->v_mount->mnt_data;
600 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
601 struct pfs_node *pd = pvd->pvd_pn;
602 pid_t pid = pvd->pvd_pid;
611 PFS_TRACE((pd->pn_name));
613 if (vn->v_type != VDIR)
614 PFS_RETURN (ENOTDIR);
617 /* check if the directory is visible to the caller */
618 if (!pfs_visible(curthread, pd, pid))
621 /* only allow reading entire entries */
622 offset = uio->uio_offset;
623 resid = uio->uio_resid;
624 if (offset < 0 || offset % PFS_DELEN != 0 || resid < PFS_DELEN)
627 if (sbuf_new(&sb, NULL, resid, SBUF_FIXEDLEN) == NULL)
630 /* skip unwanted entries */
631 sx_slock(&allproc_lock);
632 for (pn = NULL, p = NULL; offset > 0; offset -= PFS_DELEN)
633 if (pfs_iterate(curthread, pid, pd, &pn, &p) == -1) {
634 /* nothing left... */
635 sx_sunlock(&allproc_lock);
640 /* fill in entries */
641 entry.d_reclen = PFS_DELEN;
642 while (pfs_iterate(curthread, pid, pd, &pn, &p) != -1 && resid > 0) {
646 pfs_fileno_alloc(pi, pn);
648 entry.d_fileno = pn->pn_fileno * NO_PID + pid;
650 entry.d_fileno = pn->pn_fileno;
651 /* PFS_DELEN was picked to fit PFS_NAMLEN */
652 for (i = 0; i < PFS_NAMELEN - 1 && pn->pn_name[i] != '\0'; ++i)
653 entry.d_name[i] = pn->pn_name[i];
656 switch (pn->pn_type) {
657 case pfstype_procdir:
659 ("reached procdir node with p == NULL"));
660 entry.d_fileno = pn->pn_fileno * NO_PID + p->p_pid;
661 entry.d_namlen = snprintf(entry.d_name,
662 PFS_NAMELEN, "%d", p->p_pid);
668 entry.d_type = DT_DIR;
671 entry.d_type = DT_REG;
673 case pfstype_symlink:
674 entry.d_type = DT_LNK;
677 panic("%s has unexpected node type: %d", pn->pn_name, pn->pn_type);
679 PFS_TRACE((entry.d_name));
680 sbuf_bcat(&sb, &entry, PFS_DELEN);
684 sx_sunlock(&allproc_lock);
686 error = uiomove(sbuf_data(&sb), sbuf_len(&sb), uio);
692 * Read a symbolic link
695 pfs_readlink(struct vop_readlink_args *va)
697 struct vnode *vn = va->a_vp;
698 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
699 struct pfs_node *pn = pvd->pvd_pn;
700 struct uio *uio = va->a_uio;
701 struct proc *proc = NULL;
702 char buf[MAXPATHLEN];
706 PFS_TRACE((pn->pn_name));
708 if (vn->v_type != VLNK)
711 if (pn->pn_func == NULL)
714 if (pvd->pvd_pid != NO_PID) {
715 if ((proc = pfind(pvd->pvd_pid)) == NULL)
721 /* sbuf_new() can't fail with a static buffer */
722 sbuf_new(&sb, buf, sizeof buf, 0);
724 error = (pn->pn_func)(curthread, proc, pn, &sb, NULL);
735 error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
744 pfs_reclaim(struct vop_reclaim_args *va)
746 PFS_TRACE((((struct pfs_vdata *)va->a_vp->v_data)->pvd_pn->pn_name));
748 return (pfs_vncache_free(va->a_vp));
755 pfs_setattr(struct vop_setattr_args *va)
757 PFS_TRACE((((struct pfs_vdata *)va->a_vp->v_data)->pvd_pn->pn_name));
759 PFS_RETURN (EOPNOTSUPP);
766 pfs_write(struct vop_write_args *va)
768 struct vnode *vn = va->a_vp;
769 struct pfs_vdata *pvd = (struct pfs_vdata *)vn->v_data;
770 struct pfs_node *pn = pvd->pvd_pn;
771 struct uio *uio = va->a_uio;
772 struct proc *proc = NULL;
776 PFS_TRACE((pn->pn_name));
778 if (vn->v_type != VREG)
781 if (!(pn->pn_flags & PFS_WR))
784 if (pn->pn_func == NULL)
788 * This is necessary because either process' privileges may
789 * have changed since the open() call.
791 if (!pfs_visible(curthread, pn, pvd->pvd_pid))
794 /* XXX duplicates bits of pfs_visible() */
795 if (pvd->pvd_pid != NO_PID) {
796 if ((proc = pfind(pvd->pvd_pid)) == NULL)
802 if (pn->pn_flags & PFS_RAWWR) {
803 error = (pn->pn_func)(curthread, proc, pn, NULL, uio);
809 sbuf_uionew(&sb, uio, &error);
813 error = (pn->pn_func)(curthread, proc, pn, &sb, uio);
825 struct vop_vector pfs_vnodeops = {
826 .vop_default = &default_vnodeops,
828 .vop_access = pfs_access,
829 .vop_close = pfs_close,
830 .vop_create = VOP_EOPNOTSUPP,
831 .vop_getattr = pfs_getattr,
832 .vop_getextattr = pfs_getextattr,
833 .vop_ioctl = pfs_ioctl,
834 .vop_link = VOP_EOPNOTSUPP,
835 .vop_lookup = pfs_lookup,
836 .vop_mkdir = VOP_EOPNOTSUPP,
837 .vop_mknod = VOP_EOPNOTSUPP,
838 .vop_open = pfs_open,
839 .vop_read = pfs_read,
840 .vop_readdir = pfs_readdir,
841 .vop_readlink = pfs_readlink,
842 .vop_reclaim = pfs_reclaim,
843 .vop_remove = VOP_EOPNOTSUPP,
844 .vop_rename = VOP_EOPNOTSUPP,
845 .vop_rmdir = VOP_EOPNOTSUPP,
846 .vop_setattr = pfs_setattr,
847 .vop_symlink = VOP_EOPNOTSUPP,
848 .vop_write = pfs_write,
849 /* XXX I've probably forgotten a few that need VOP_EOPNOTSUPP */