Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.106.2.3
      1 /*	$NetBSD: procfs_vnops.c,v 1.106.2.3 2004/08/24 17:57:41 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     35  */
     36 
     37 /*
     38  * Copyright (c) 1993 Jan-Simon Pendry
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Jan-Simon Pendry.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     72  */
     73 
     74 /*
     75  * procfs vnode interface
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.106.2.3 2004/08/24 17:57:41 skrll Exp $");
     80 
     81 #include <sys/param.h>
     82 #include <sys/systm.h>
     83 #include <sys/time.h>
     84 #include <sys/kernel.h>
     85 #include <sys/file.h>
     86 #include <sys/filedesc.h>
     87 #include <sys/proc.h>
     88 #include <sys/vnode.h>
     89 #include <sys/namei.h>
     90 #include <sys/malloc.h>
     91 #include <sys/mount.h>
     92 #include <sys/dirent.h>
     93 #include <sys/resourcevar.h>
     94 #include <sys/stat.h>
     95 #include <sys/ptrace.h>
     96 
     97 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
     98 
     99 #include <machine/reg.h>
    100 
    101 #include <miscfs/genfs/genfs.h>
    102 #include <miscfs/procfs/procfs.h>
    103 
    104 /*
    105  * Vnode Operations.
    106  *
    107  */
    108 
    109 static int procfs_validfile_linux __P((struct lwp *, struct mount *));
    110 
    111 /*
    112  * This is a list of the valid names in the
    113  * process-specific sub-directories.  It is
    114  * used in procfs_lookup and procfs_readdir
    115  */
    116 static const struct proc_target {
    117 	u_char	pt_type;
    118 	u_char	pt_namlen;
    119 	const char	*pt_name;
    120 	pfstype	pt_pfstype;
    121 	int	(*pt_valid) __P((struct lwp *, struct mount *));
    122 } proc_targets[] = {
    123 #define N(s) sizeof(s)-1, s
    124 	/*	  name		type		validp */
    125 	{ DT_DIR, N("."),	PFSproc,		NULL },
    126 	{ DT_DIR, N(".."),	PFSroot,		NULL },
    127 	{ DT_DIR, N("fd"),	PFSfd,		NULL },
    128 	{ DT_REG, N("file"),	PFSfile,		procfs_validfile },
    129 	{ DT_REG, N("mem"),	PFSmem,		NULL },
    130 	{ DT_REG, N("regs"),	PFSregs,		procfs_validregs },
    131 	{ DT_REG, N("fpregs"),	PFSfpregs,	procfs_validfpregs },
    132 	{ DT_REG, N("ctl"),	PFSctl,		NULL },
    133 	{ DT_REG, N("stat"),	PFSstat,		procfs_validfile_linux },
    134 	{ DT_REG, N("status"),	PFSstatus,	NULL },
    135 	{ DT_REG, N("note"),	PFSnote,		NULL },
    136 	{ DT_REG, N("notepg"),	PFSnotepg,	NULL },
    137 	{ DT_REG, N("map"),	PFSmap,		procfs_validmap },
    138 	{ DT_REG, N("maps"),	PFSmaps,		procfs_validmap },
    139 	{ DT_REG, N("cmdline"), PFScmdline,	NULL },
    140 	{ DT_REG, N("exe"),	PFSfile,		procfs_validfile_linux },
    141 #ifdef __HAVE_PROCFS_MACHDEP
    142 	PROCFS_MACHDEP_NODETYPE_DEFNS
    143 #endif
    144 #undef N
    145 };
    146 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
    147 
    148 /*
    149  * List of files in the root directory. Note: the validate function will
    150  * be called with p == NULL for these ones.
    151  */
    152 static const struct proc_target proc_root_targets[] = {
    153 #define N(s) sizeof(s)-1, s
    154 	/*	  name		    type	    validp */
    155 	{ DT_REG, N("meminfo"),     PFSmeminfo,        procfs_validfile_linux },
    156 	{ DT_REG, N("cpuinfo"),     PFScpuinfo,        procfs_validfile_linux },
    157 	{ DT_REG, N("uptime"),      PFSuptime,         procfs_validfile_linux },
    158 #undef N
    159 };
    160 static const int nproc_root_targets =
    161     sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
    162 
    163 int	procfs_lookup	__P((void *));
    164 #define	procfs_create	genfs_eopnotsupp
    165 #define	procfs_mknod	genfs_eopnotsupp
    166 int	procfs_open	__P((void *));
    167 int	procfs_close	__P((void *));
    168 int	procfs_access	__P((void *));
    169 int	procfs_getattr	__P((void *));
    170 int	procfs_setattr	__P((void *));
    171 #define	procfs_read	procfs_rw
    172 #define	procfs_write	procfs_rw
    173 #define	procfs_fcntl	genfs_fcntl
    174 #define	procfs_ioctl	genfs_enoioctl
    175 #define	procfs_poll	genfs_poll
    176 #define procfs_revoke	genfs_revoke
    177 #define	procfs_fsync	genfs_nullop
    178 #define	procfs_seek	genfs_nullop
    179 #define	procfs_remove	genfs_eopnotsupp
    180 int	procfs_link	__P((void *));
    181 #define	procfs_rename	genfs_eopnotsupp
    182 #define	procfs_mkdir	genfs_eopnotsupp
    183 #define	procfs_rmdir	genfs_eopnotsupp
    184 int	procfs_symlink	__P((void *));
    185 int	procfs_readdir	__P((void *));
    186 int	procfs_readlink	__P((void *));
    187 #define	procfs_abortop	genfs_abortop
    188 int	procfs_inactive	__P((void *));
    189 int	procfs_reclaim	__P((void *));
    190 #define	procfs_lock	genfs_lock
    191 #define	procfs_unlock	genfs_unlock
    192 #define	procfs_bmap	genfs_badop
    193 #define	procfs_strategy	genfs_badop
    194 int	procfs_print	__P((void *));
    195 int	procfs_pathconf	__P((void *));
    196 #define	procfs_islocked	genfs_islocked
    197 #define	procfs_advlock	genfs_einval
    198 #define	procfs_blkatoff	genfs_eopnotsupp
    199 #define	procfs_valloc	genfs_eopnotsupp
    200 #define	procfs_vfree	genfs_nullop
    201 #define	procfs_truncate	genfs_eopnotsupp
    202 #define	procfs_update	genfs_nullop
    203 #define	procfs_bwrite	genfs_eopnotsupp
    204 #define procfs_putpages	genfs_null_putpages
    205 
    206 static int atoi __P((const char *, size_t));
    207 
    208 /*
    209  * procfs vnode operations.
    210  */
    211 int (**procfs_vnodeop_p) __P((void *));
    212 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
    213 	{ &vop_default_desc, vn_default_error },
    214 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
    215 	{ &vop_create_desc, procfs_create },		/* create */
    216 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
    217 	{ &vop_open_desc, procfs_open },		/* open */
    218 	{ &vop_close_desc, procfs_close },		/* close */
    219 	{ &vop_access_desc, procfs_access },		/* access */
    220 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
    221 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
    222 	{ &vop_read_desc, procfs_read },		/* read */
    223 	{ &vop_write_desc, procfs_write },		/* write */
    224 	{ &vop_fcntl_desc, procfs_fcntl },		/* fcntl */
    225 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
    226 	{ &vop_poll_desc, procfs_poll },		/* poll */
    227 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
    228 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
    229 	{ &vop_seek_desc, procfs_seek },		/* seek */
    230 	{ &vop_remove_desc, procfs_remove },		/* remove */
    231 	{ &vop_link_desc, procfs_link },		/* link */
    232 	{ &vop_rename_desc, procfs_rename },		/* rename */
    233 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
    234 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
    235 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
    236 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
    237 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
    238 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
    239 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
    240 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
    241 	{ &vop_lock_desc, procfs_lock },		/* lock */
    242 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
    243 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
    244 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
    245 	{ &vop_print_desc, procfs_print },		/* print */
    246 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
    247 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
    248 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
    249 	{ &vop_blkatoff_desc, procfs_blkatoff },	/* blkatoff */
    250 	{ &vop_valloc_desc, procfs_valloc },		/* valloc */
    251 	{ &vop_vfree_desc, procfs_vfree },		/* vfree */
    252 	{ &vop_truncate_desc, procfs_truncate },	/* truncate */
    253 	{ &vop_update_desc, procfs_update },		/* update */
    254 	{ &vop_putpages_desc, procfs_putpages },	/* putpages */
    255 	{ NULL, NULL }
    256 };
    257 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
    258 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
    259 /*
    260  * set things up for doing i/o on
    261  * the pfsnode (vp).  (vp) is locked
    262  * on entry, and should be left locked
    263  * on exit.
    264  *
    265  * for procfs we don't need to do anything
    266  * in particular for i/o.  all that is done
    267  * is to support exclusive open on process
    268  * memory images.
    269  */
    270 int
    271 procfs_open(v)
    272 	void *v;
    273 {
    274 	struct vop_open_args /* {
    275 		struct vnode *a_vp;
    276 		int  a_mode;
    277 		struct ucred *a_cred;
    278 		struct lwp *a_l;
    279 	} */ *ap = v;
    280 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    281 	struct lwp *l1;
    282 	struct proc *p2;
    283 	int error;
    284 
    285 	l1 = ap->a_l;				/* tracer */
    286 	p2 = PFIND(pfs->pfs_pid);		/* traced */
    287 
    288 	if (p2 == NULL)
    289 		return (ENOENT);		/* was ESRCH, jsp */
    290 
    291 	switch (pfs->pfs_type) {
    292 	case PFSmem:
    293 		if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
    294 		    ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
    295 			return (EBUSY);
    296 
    297 		if ((error = process_checkioperm(l1, p2)) != 0)
    298 			return (error);
    299 
    300 		if (ap->a_mode & FWRITE)
    301 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
    302 
    303 		return (0);
    304 
    305 	default:
    306 		break;
    307 	}
    308 
    309 	return (0);
    310 }
    311 
    312 /*
    313  * close the pfsnode (vp) after doing i/o.
    314  * (vp) is not locked on entry or exit.
    315  *
    316  * nothing to do for procfs other than undo
    317  * any exclusive open flag (see _open above).
    318  */
    319 int
    320 procfs_close(v)
    321 	void *v;
    322 {
    323 	struct vop_close_args /* {
    324 		struct vnode *a_vp;
    325 		int  a_fflag;
    326 		struct ucred *a_cred;
    327 		struct proc *a_p;
    328 	} */ *ap = v;
    329 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    330 
    331 	switch (pfs->pfs_type) {
    332 	case PFSmem:
    333 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
    334 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
    335 		break;
    336 
    337 	default:
    338 		break;
    339 	}
    340 
    341 	return (0);
    342 }
    343 
    344 /*
    345  * _inactive is called when the pfsnode
    346  * is vrele'd and the reference count goes
    347  * to zero.  (vp) will be on the vnode free
    348  * list, so to get it back vget() must be
    349  * used.
    350  *
    351  * for procfs, check if the process is still
    352  * alive and if it isn't then just throw away
    353  * the vnode by calling vgone().  this may
    354  * be overkill and a waste of time since the
    355  * chances are that the process will still be
    356  * there and PFIND is not free.
    357  *
    358  * (vp) is locked on entry, but must be unlocked on exit.
    359  */
    360 int
    361 procfs_inactive(v)
    362 	void *v;
    363 {
    364 	struct vop_inactive_args /* {
    365 		struct vnode *a_vp;
    366 		struct proc *a_p;
    367 	} */ *ap = v;
    368 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    369 
    370 	VOP_UNLOCK(ap->a_vp, 0);
    371 	if (PFIND(pfs->pfs_pid) == NULL)
    372 		vgone(ap->a_vp);
    373 
    374 	return (0);
    375 }
    376 
    377 /*
    378  * _reclaim is called when getnewvnode()
    379  * wants to make use of an entry on the vnode
    380  * free list.  at this time the filesystem needs
    381  * to free any private data and remove the node
    382  * from any private lists.
    383  */
    384 int
    385 procfs_reclaim(v)
    386 	void *v;
    387 {
    388 	struct vop_reclaim_args /* {
    389 		struct vnode *a_vp;
    390 	} */ *ap = v;
    391 
    392 	return (procfs_freevp(ap->a_vp));
    393 }
    394 
    395 /*
    396  * Return POSIX pathconf information applicable to special devices.
    397  */
    398 int
    399 procfs_pathconf(v)
    400 	void *v;
    401 {
    402 	struct vop_pathconf_args /* {
    403 		struct vnode *a_vp;
    404 		int a_name;
    405 		register_t *a_retval;
    406 	} */ *ap = v;
    407 
    408 	switch (ap->a_name) {
    409 	case _PC_LINK_MAX:
    410 		*ap->a_retval = LINK_MAX;
    411 		return (0);
    412 	case _PC_MAX_CANON:
    413 		*ap->a_retval = MAX_CANON;
    414 		return (0);
    415 	case _PC_MAX_INPUT:
    416 		*ap->a_retval = MAX_INPUT;
    417 		return (0);
    418 	case _PC_PIPE_BUF:
    419 		*ap->a_retval = PIPE_BUF;
    420 		return (0);
    421 	case _PC_CHOWN_RESTRICTED:
    422 		*ap->a_retval = 1;
    423 		return (0);
    424 	case _PC_VDISABLE:
    425 		*ap->a_retval = _POSIX_VDISABLE;
    426 		return (0);
    427 	case _PC_SYNC_IO:
    428 		*ap->a_retval = 1;
    429 		return (0);
    430 	default:
    431 		return (EINVAL);
    432 	}
    433 	/* NOTREACHED */
    434 }
    435 
    436 /*
    437  * _print is used for debugging.
    438  * just print a readable description
    439  * of (vp).
    440  */
    441 int
    442 procfs_print(v)
    443 	void *v;
    444 {
    445 	struct vop_print_args /* {
    446 		struct vnode *a_vp;
    447 	} */ *ap = v;
    448 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    449 
    450 	printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
    451 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
    452 	return 0;
    453 }
    454 
    455 int
    456 procfs_link(v)
    457 	void *v;
    458 {
    459 	struct vop_link_args /* {
    460 		struct vnode *a_dvp;
    461 		struct vnode *a_vp;
    462 		struct componentname *a_cnp;
    463 	} */ *ap = v;
    464 
    465 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    466 	vput(ap->a_dvp);
    467 	return (EROFS);
    468 }
    469 
    470 int
    471 procfs_symlink(v)
    472 	void *v;
    473 {
    474 	struct vop_symlink_args /* {
    475 		struct vnode *a_dvp;
    476 		struct vnode **a_vpp;
    477 		struct componentname *a_cnp;
    478 		struct vattr *a_vap;
    479 		char *a_target;
    480 	} */ *ap = v;
    481 
    482 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    483 	vput(ap->a_dvp);
    484 	return (EROFS);
    485 }
    486 
    487 /*
    488  * Invent attributes for pfsnode (vp) and store
    489  * them in (vap).
    490  * Directories lengths are returned as zero since
    491  * any real length would require the genuine size
    492  * to be computed, and nothing cares anyway.
    493  *
    494  * this is relatively minimal for procfs.
    495  */
    496 int
    497 procfs_getattr(v)
    498 	void *v;
    499 {
    500 	struct vop_getattr_args /* {
    501 		struct vnode *a_vp;
    502 		struct vattr *a_vap;
    503 		struct ucred *a_cred;
    504 		struct proc *a_p;
    505 	} */ *ap = v;
    506 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    507 	struct vattr *vap = ap->a_vap;
    508 	struct proc *procp;
    509 	int error;
    510 
    511 	/* first check the process still exists */
    512 	switch (pfs->pfs_type) {
    513 	case PFSroot:
    514 	case PFScurproc:
    515 	case PFSself:
    516 		procp = 0;
    517 		break;
    518 
    519 	default:
    520 		procp = PFIND(pfs->pfs_pid);
    521 		if (procp == NULL)
    522 			return (ENOENT);
    523 		break;
    524 	}
    525 
    526 	error = 0;
    527 
    528 	/* start by zeroing out the attributes */
    529 	VATTR_NULL(vap);
    530 
    531 	/* next do all the common fields */
    532 	vap->va_type = ap->a_vp->v_type;
    533 	vap->va_mode = pfs->pfs_mode;
    534 	vap->va_fileid = pfs->pfs_fileno;
    535 	vap->va_flags = 0;
    536 	vap->va_blocksize = PAGE_SIZE;
    537 
    538 	/*
    539 	 * Make all times be current TOD.  Avoid microtime(9), it's slow.
    540 	 * We don't guard the read from time(9) with splclock(9) since we
    541 	 * don't actually need to be THAT sure the access is atomic.
    542 	 *
    543 	 * It would be possible to get the process start
    544 	 * time from the p_stat structure, but there's
    545 	 * no "file creation" time stamp anyway, and the
    546 	 * p_stat structure is not addressible if u. gets
    547 	 * swapped out for that process.
    548 	 */
    549 	TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime);
    550 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    551 
    552 	switch (pfs->pfs_type) {
    553 	case PFSmem:
    554 	case PFSregs:
    555 	case PFSfpregs:
    556 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    557 	PROCFS_MACHDEP_PROTECT_CASES
    558 #endif
    559 		/*
    560 		 * If the process has exercised some setuid or setgid
    561 		 * privilege, then rip away read/write permission so
    562 		 * that only root can gain access.
    563 		 */
    564 		if (procp->p_flag & P_SUGID)
    565 			vap->va_mode &= ~(S_IRUSR|S_IWUSR);
    566 		/* FALLTHROUGH */
    567 	case PFSctl:
    568 	case PFSstatus:
    569 	case PFSstat:
    570 	case PFSnote:
    571 	case PFSnotepg:
    572 	case PFSmap:
    573 	case PFSmaps:
    574 	case PFScmdline:
    575 		vap->va_nlink = 1;
    576 		vap->va_uid = procp->p_ucred->cr_uid;
    577 		vap->va_gid = procp->p_ucred->cr_gid;
    578 		break;
    579 	case PFSmeminfo:
    580 	case PFScpuinfo:
    581 	case PFSuptime:
    582 		vap->va_nlink = 1;
    583 		vap->va_uid = vap->va_gid = 0;
    584 		break;
    585 
    586 	default:
    587 		break;
    588 	}
    589 
    590 	/*
    591 	 * now do the object specific fields
    592 	 *
    593 	 * The size could be set from struct reg, but it's hardly
    594 	 * worth the trouble, and it puts some (potentially) machine
    595 	 * dependent data into this machine-independent code.  If it
    596 	 * becomes important then this function should break out into
    597 	 * a per-file stat function in the corresponding .c file.
    598 	 */
    599 
    600 	switch (pfs->pfs_type) {
    601 	case PFSroot:
    602 		/*
    603 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    604 		 */
    605 		vap->va_nlink = 1;
    606 		vap->va_uid = 0;
    607 		vap->va_gid = 0;
    608 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    609 		break;
    610 
    611 	case PFScurproc: {
    612 		char buf[16];		/* should be enough */
    613 		vap->va_nlink = 1;
    614 		vap->va_uid = 0;
    615 		vap->va_gid = 0;
    616 		vap->va_bytes = vap->va_size =
    617 		    snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
    618 		break;
    619 	}
    620 
    621 	case PFSself:
    622 		vap->va_nlink = 1;
    623 		vap->va_uid = 0;
    624 		vap->va_gid = 0;
    625 		vap->va_bytes = vap->va_size = sizeof("curproc");
    626 		break;
    627 
    628 	case PFSfd:
    629 		if (pfs->pfs_fd != -1) {
    630 			struct file *fp;
    631 			struct proc *pown;
    632 
    633 			if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
    634 				return error;
    635 			FILE_USE(fp);
    636 			vap->va_nlink = 1;
    637 			vap->va_uid = fp->f_cred->cr_uid;
    638 			vap->va_gid = fp->f_cred->cr_gid;
    639 			switch (fp->f_type) {
    640 			case DTYPE_VNODE:
    641 				vap->va_bytes = vap->va_size =
    642 				    ((struct vnode *)fp->f_data)->v_size;
    643 				break;
    644 			default:
    645 				vap->va_bytes = vap->va_size = 0;
    646 				break;
    647 			}
    648 			FILE_UNUSE(fp, proc_representative_lwp(pown));
    649 			break;
    650 		}
    651 		/*FALLTHROUGH*/
    652 	case PFSproc:
    653 		vap->va_nlink = 2;
    654 		vap->va_uid = procp->p_ucred->cr_uid;
    655 		vap->va_gid = procp->p_ucred->cr_gid;
    656 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    657 		break;
    658 
    659 	case PFSfile:
    660 		error = EOPNOTSUPP;
    661 		break;
    662 
    663 	case PFSmem:
    664 		vap->va_bytes = vap->va_size =
    665 			ctob(procp->p_vmspace->vm_tsize +
    666 				    procp->p_vmspace->vm_dsize +
    667 				    procp->p_vmspace->vm_ssize);
    668 		break;
    669 
    670 #if defined(PT_GETREGS) || defined(PT_SETREGS)
    671 	case PFSregs:
    672 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    673 		break;
    674 #endif
    675 
    676 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    677 	case PFSfpregs:
    678 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    679 		break;
    680 #endif
    681 
    682 	case PFSctl:
    683 	case PFSstatus:
    684 	case PFSstat:
    685 	case PFSnote:
    686 	case PFSnotepg:
    687 	case PFScmdline:
    688 	case PFSmeminfo:
    689 	case PFScpuinfo:
    690 	case PFSuptime:
    691 		vap->va_bytes = vap->va_size = 0;
    692 		break;
    693 	case PFSmap:
    694 	case PFSmaps:
    695 		/*
    696 		 * Advise a larger blocksize for the map files, so that
    697 		 * they may be read in one pass.
    698 		 */
    699 		vap->va_blocksize = 4 * PAGE_SIZE;
    700 		vap->va_bytes = vap->va_size = 0;
    701 		break;
    702 
    703 #ifdef __HAVE_PROCFS_MACHDEP
    704 	PROCFS_MACHDEP_NODETYPE_CASES
    705 		error = procfs_machdep_getattr(ap->a_vp, vap, procp);
    706 		break;
    707 #endif
    708 
    709 	default:
    710 		panic("procfs_getattr");
    711 	}
    712 
    713 	return (error);
    714 }
    715 
    716 /*ARGSUSED*/
    717 int
    718 procfs_setattr(v)
    719 	void *v;
    720 {
    721 	/*
    722 	 * just fake out attribute setting
    723 	 * it's not good to generate an error
    724 	 * return, otherwise things like creat()
    725 	 * will fail when they try to set the
    726 	 * file length to 0.  worse, this means
    727 	 * that echo $note > /proc/$pid/note will fail.
    728 	 */
    729 
    730 	return (0);
    731 }
    732 
    733 /*
    734  * implement access checking.
    735  *
    736  * actually, the check for super-user is slightly
    737  * broken since it will allow read access to write-only
    738  * objects.  this doesn't cause any particular trouble
    739  * but does mean that the i/o entry points need to check
    740  * that the operation really does make sense.
    741  */
    742 int
    743 procfs_access(v)
    744 	void *v;
    745 {
    746 	struct vop_access_args /* {
    747 		struct vnode *a_vp;
    748 		int a_mode;
    749 		struct ucred *a_cred;
    750 		struct lwp *a_l;
    751 	} */ *ap = v;
    752 	struct vattr va;
    753 	int error;
    754 
    755 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
    756 		return (error);
    757 
    758 	return (vaccess(va.va_type, va.va_mode,
    759 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
    760 }
    761 
    762 /*
    763  * lookup.  this is incredibly complicated in the
    764  * general case, however for most pseudo-filesystems
    765  * very little needs to be done.
    766  *
    767  * Locking isn't hard here, just poorly documented.
    768  *
    769  * If we're looking up ".", just vref the parent & return it.
    770  *
    771  * If we're looking up "..", unlock the parent, and lock "..". If everything
    772  * went ok, and we're on the last component and the caller requested the
    773  * parent locked, try to re-lock the parent. We do this to prevent lock
    774  * races.
    775  *
    776  * For anything else, get the needed node. Then unlock the parent if not
    777  * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
    778  * parent in the .. case).
    779  *
    780  * We try to exit with the parent locked in error cases.
    781  */
    782 int
    783 procfs_lookup(v)
    784 	void *v;
    785 {
    786 	struct vop_lookup_args /* {
    787 		struct vnode * a_dvp;
    788 		struct vnode ** a_vpp;
    789 		struct componentname * a_cnp;
    790 	} */ *ap = v;
    791 	struct componentname *cnp = ap->a_cnp;
    792 	struct vnode **vpp = ap->a_vpp;
    793 	struct vnode *dvp = ap->a_dvp;
    794 	const char *pname = cnp->cn_nameptr;
    795 	const struct proc_target *pt = NULL;
    796 	struct vnode *fvp;
    797 	pid_t pid;
    798 	struct pfsnode *pfs;
    799 	struct proc *p = NULL;
    800 	struct lwp *l = NULL;
    801 	int i, error, wantpunlock, iscurproc = 0, isself = 0;
    802 
    803 	*vpp = NULL;
    804 	cnp->cn_flags &= ~PDIRUNLOCK;
    805 
    806 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    807 		return (EROFS);
    808 
    809 	if (cnp->cn_namelen == 1 && *pname == '.') {
    810 		*vpp = dvp;
    811 		VREF(dvp);
    812 		return (0);
    813 	}
    814 
    815 	wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
    816 	pfs = VTOPFS(dvp);
    817 	switch (pfs->pfs_type) {
    818 	case PFSroot:
    819 		/*
    820 		 * Shouldn't get here with .. in the root node.
    821 		 */
    822 		if (cnp->cn_flags & ISDOTDOT)
    823 			return (EIO);
    824 
    825 		iscurproc = CNEQ(cnp, "curproc", 7);
    826 		isself = CNEQ(cnp, "self", 4);
    827 
    828 		if (iscurproc || isself) {
    829 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
    830 			    iscurproc ? PFScurproc : PFSself, -1);
    831 			if ((error == 0) && (wantpunlock)) {
    832 				VOP_UNLOCK(dvp, 0);
    833 				cnp->cn_flags |= PDIRUNLOCK;
    834 			}
    835 			return (error);
    836 		}
    837 
    838 		for (i = 0; i < nproc_root_targets; i++) {
    839 			pt = &proc_root_targets[i];
    840 			if (cnp->cn_namelen == pt->pt_namlen &&
    841 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    842 			    (pt->pt_valid == NULL ||
    843 			     (*pt->pt_valid)(cnp->cn_lwp, dvp->v_mount)))
    844 				break;
    845 		}
    846 
    847 		if (i != nproc_root_targets) {
    848 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
    849 			    pt->pt_pfstype, -1);
    850 			if ((error == 0) && (wantpunlock)) {
    851 				VOP_UNLOCK(dvp, 0);
    852 				cnp->cn_flags |= PDIRUNLOCK;
    853 			}
    854 			return (error);
    855 		}
    856 
    857 		pid = (pid_t)atoi(pname, cnp->cn_namelen);
    858 
    859 		p = PFIND(pid);
    860 		if (p == NULL)
    861 			break;
    862 
    863 		error = procfs_allocvp(dvp->v_mount, vpp, pid, PFSproc, -1);
    864 		if ((error == 0) && (wantpunlock)) {
    865 			VOP_UNLOCK(dvp, 0);
    866 			cnp->cn_flags |= PDIRUNLOCK;
    867 		}
    868 		return (error);
    869 
    870 	case PFSproc:
    871 		/*
    872 		 * do the .. dance. We unlock the directory, and then
    873 		 * get the root dir. That will automatically return ..
    874 		 * locked. Then if the caller wanted dvp locked, we
    875 		 * re-lock.
    876 		 */
    877 		if (cnp->cn_flags & ISDOTDOT) {
    878 			VOP_UNLOCK(dvp, 0);
    879 			cnp->cn_flags |= PDIRUNLOCK;
    880 			error = procfs_root(dvp->v_mount, vpp);
    881 			if ((error == 0) && (wantpunlock == 0) &&
    882 				    ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    883 				cnp->cn_flags &= ~PDIRUNLOCK;
    884 			return (error);
    885 		}
    886 
    887 		p = PFIND(pfs->pfs_pid);
    888 		if (p == NULL)
    889 			break;
    890 		l = proc_representative_lwp(p);
    891 
    892 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
    893 			if (cnp->cn_namelen == pt->pt_namlen &&
    894 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    895 			    (pt->pt_valid == NULL ||
    896 			     (*pt->pt_valid)(cnp->cn_lwp, dvp->v_mount)))
    897 				goto found;
    898 		}
    899 		break;
    900 
    901 	found:
    902 		if (pt->pt_pfstype == PFSfile) {
    903 			fvp = p->p_textvp;
    904 			/* We already checked that it exists. */
    905 			VREF(fvp);
    906 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
    907 			if (wantpunlock) {
    908 				VOP_UNLOCK(dvp, 0);
    909 				cnp->cn_flags |= PDIRUNLOCK;
    910 			}
    911 			*vpp = fvp;
    912 			return (0);
    913 		}
    914 
    915 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    916 		    pt->pt_pfstype, -1);
    917 		if ((error == 0) && (wantpunlock)) {
    918 			VOP_UNLOCK(dvp, 0);
    919 			cnp->cn_flags |= PDIRUNLOCK;
    920 		}
    921 		return (error);
    922 
    923 	case PFSfd: {
    924 		int fd;
    925 		struct file *fp;
    926 		/*
    927 		 * do the .. dance. We unlock the directory, and then
    928 		 * get the proc dir. That will automatically return ..
    929 		 * locked. Then if the caller wanted dvp locked, we
    930 		 * re-lock.
    931 		 */
    932 		if (cnp->cn_flags & ISDOTDOT) {
    933 			VOP_UNLOCK(dvp, 0);
    934 			cnp->cn_flags |= PDIRUNLOCK;
    935 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    936 			    PFSproc, -1);
    937 			if ((error == 0) && (wantpunlock == 0) &&
    938 				    ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    939 				cnp->cn_flags &= ~PDIRUNLOCK;
    940 			return (error);
    941 		}
    942 		fd = atoi(pname, cnp->cn_namelen);
    943 		p = PFIND(pfs->pfs_pid);
    944 		if (p == NULL || (fp = fd_getfile(p->p_fd, fd)) == NULL)
    945 			return ENOENT;
    946 		FILE_USE(fp);
    947 
    948 		switch (fp->f_type) {
    949 		case DTYPE_VNODE:
    950 			fvp = (struct vnode *)fp->f_data;
    951 
    952 			/* Don't show directories */
    953 			if (fvp->v_type == VDIR)
    954 				goto symlink;
    955 
    956 			VREF(fvp);
    957 			FILE_UNUSE(fp, l);
    958 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
    959 			    (p == curproc ? LK_CANRECURSE : 0));
    960 			*vpp = fvp;
    961 			error = 0;
    962 			break;
    963 		default:
    964 		symlink:
    965 			FILE_UNUSE(fp, l);
    966 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    967 			    PFSfd, fd);
    968 			break;
    969 		}
    970 		if ((error == 0) && (wantpunlock)) {
    971 			VOP_UNLOCK(dvp, 0);
    972 			cnp->cn_flags |= PDIRUNLOCK;
    973 		}
    974 		return error;
    975 	}
    976 	default:
    977 		return (ENOTDIR);
    978 	}
    979 
    980 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    981 }
    982 
    983 int
    984 procfs_validfile(l, mp)
    985 	struct lwp *l;
    986 	struct mount *mp;
    987 {
    988 	return (l->l_proc->p_textvp != NULL);
    989 }
    990 
    991 static int
    992 procfs_validfile_linux(l, mp)
    993 	struct lwp *l;
    994 	struct mount *mp;
    995 {
    996 	int flags;
    997 
    998 	flags = VFSTOPROC(mp)->pmnt_flags;
    999 	return ((flags & PROCFSMNT_LINUXCOMPAT) &&
   1000 	    (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp)));
   1001 }
   1002 
   1003 /*
   1004  * readdir returns directory entries from pfsnode (vp).
   1005  *
   1006  * the strategy here with procfs is to generate a single
   1007  * directory entry at a time (struct dirent) and then
   1008  * copy that out to userland using uiomove.  a more efficent
   1009  * though more complex implementation, would try to minimize
   1010  * the number of calls to uiomove().  for procfs, this is
   1011  * hardly worth the added code complexity.
   1012  *
   1013  * this should just be done through read()
   1014  */
   1015 int
   1016 procfs_readdir(v)
   1017 	void *v;
   1018 {
   1019 	struct vop_readdir_args /* {
   1020 		struct vnode *a_vp;
   1021 		struct uio *a_uio;
   1022 		struct ucred *a_cred;
   1023 		int *a_eofflag;
   1024 		off_t **a_cookies;
   1025 		int *a_ncookies;
   1026 	} */ *ap = v;
   1027 	struct uio *uio = ap->a_uio;
   1028 	struct dirent d;
   1029 	struct pfsnode *pfs;
   1030 	off_t i;
   1031 	int error;
   1032 	off_t *cookies = NULL;
   1033 	int ncookies, left, skip, j;
   1034 	struct vnode *vp;
   1035 	const struct proc_target *pt;
   1036 
   1037 	vp = ap->a_vp;
   1038 	pfs = VTOPFS(vp);
   1039 
   1040 	if (uio->uio_resid < UIO_MX)
   1041 		return (EINVAL);
   1042 	if (uio->uio_offset < 0)
   1043 		return (EINVAL);
   1044 
   1045 	error = 0;
   1046 	i = uio->uio_offset;
   1047 	memset(&d, 0, UIO_MX);
   1048 	d.d_reclen = UIO_MX;
   1049 	ncookies = uio->uio_resid / UIO_MX;
   1050 
   1051 	switch (pfs->pfs_type) {
   1052 	/*
   1053 	 * this is for the process-specific sub-directories.
   1054 	 * all that is needed to is copy out all the entries
   1055 	 * from the procent[] table (top of this file).
   1056 	 */
   1057 	case PFSproc: {
   1058 		struct proc *p;
   1059 
   1060 		if (i >= nproc_targets)
   1061 			return 0;
   1062 
   1063 		p = PFIND(pfs->pfs_pid);
   1064 		if (p == NULL)
   1065 			break;
   1066 
   1067 		if (ap->a_ncookies) {
   1068 			ncookies = min(ncookies, (nproc_targets - i));
   1069 			cookies = malloc(ncookies * sizeof (off_t),
   1070 			    M_TEMP, M_WAITOK);
   1071 			*ap->a_cookies = cookies;
   1072 		}
   1073 
   1074 		for (pt = &proc_targets[i];
   1075 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
   1076 			if (pt->pt_valid &&
   1077 			    (*pt->pt_valid)(proc_representative_lwp(p), vp->v_mount) == 0)
   1078 				continue;
   1079 
   1080 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
   1081 			    pt->pt_pfstype, -1);
   1082 			d.d_namlen = pt->pt_namlen;
   1083 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1084 			d.d_type = pt->pt_type;
   1085 
   1086 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1087 				break;
   1088 			if (cookies)
   1089 				*cookies++ = i + 1;
   1090 		}
   1091 
   1092 	    	break;
   1093 	}
   1094 	case PFSfd: {
   1095 		struct proc *p;
   1096 		struct filedesc	*fdp;
   1097 		struct file *fp;
   1098 		int lim, nc = 0;
   1099 
   1100 		p = PFIND(pfs->pfs_pid);
   1101 		if (p == NULL)
   1102 			return ESRCH;
   1103 
   1104 		fdp = p->p_fd;
   1105 
   1106 		lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
   1107 		if (i >= lim)
   1108 			return 0;
   1109 
   1110 		if (ap->a_ncookies) {
   1111 			ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
   1112 			cookies = malloc(ncookies * sizeof (off_t),
   1113 			    M_TEMP, M_WAITOK);
   1114 			*ap->a_cookies = cookies;
   1115 		}
   1116 
   1117 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
   1118 			pt = &proc_targets[i];
   1119 			d.d_namlen = pt->pt_namlen;
   1120 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
   1121 			    pt->pt_pfstype, -1);
   1122 			(void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1123 			d.d_type = pt->pt_type;
   1124 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1125 				break;
   1126 			if (cookies)
   1127 				*cookies++ = i + 1;
   1128 			nc++;
   1129 		}
   1130 		if (error) {
   1131 			ncookies = nc;
   1132 			break;
   1133 		}
   1134 		for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
   1135 			/* check the descriptor exists */
   1136 			if ((fp = fd_getfile(fdp, i - 2)) == NULL)
   1137 				continue;
   1138 			simple_unlock(&fp->f_slock);
   1139 
   1140 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
   1141 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
   1142 			    "%lld", (long long)(i - 2));
   1143 			d.d_type = VREG;
   1144 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1145 				break;
   1146 			if (cookies)
   1147 				*cookies++ = i + 1;
   1148 			nc++;
   1149 		}
   1150 		ncookies = nc;
   1151 		break;
   1152 	}
   1153 
   1154 	/*
   1155 	 * this is for the root of the procfs filesystem
   1156 	 * what is needed are special entries for "curproc"
   1157 	 * and "self" followed by an entry for each process
   1158 	 * on allproc
   1159 #ifdef PROCFS_ZOMBIE
   1160 	 * and deadproc and zombproc.
   1161 #endif
   1162 	 */
   1163 
   1164 	case PFSroot: {
   1165 		int pcnt = i, nc = 0;
   1166 		const struct proclist_desc *pd;
   1167 		volatile struct proc *p;
   1168 
   1169 		if (pcnt > 3)
   1170 			pcnt = 3;
   1171 		if (ap->a_ncookies) {
   1172 			/*
   1173 			 * XXX Potentially allocating too much space here,
   1174 			 * but I'm lazy. This loop needs some work.
   1175 			 */
   1176 			cookies = malloc(ncookies * sizeof (off_t),
   1177 			    M_TEMP, M_WAITOK);
   1178 			*ap->a_cookies = cookies;
   1179 		}
   1180 		/*
   1181 		 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
   1182 		 * PROCLIST IN THE proclists!
   1183 		 */
   1184 		proclist_lock_read();
   1185 		pd = proclists;
   1186 #ifdef PROCFS_ZOMBIE
   1187 	again:
   1188 #endif
   1189 		for (p = LIST_FIRST(pd->pd_list);
   1190 		     p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
   1191 			switch (i) {
   1192 			case 0:		/* `.' */
   1193 			case 1:		/* `..' */
   1194 				d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
   1195 				d.d_namlen = i + 1;
   1196 				memcpy(d.d_name, "..", d.d_namlen);
   1197 				d.d_name[i + 1] = '\0';
   1198 				d.d_type = DT_DIR;
   1199 				break;
   1200 
   1201 			case 2:
   1202 				d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
   1203 				d.d_namlen = sizeof("curproc") - 1;
   1204 				memcpy(d.d_name, "curproc", sizeof("curproc"));
   1205 				d.d_type = DT_LNK;
   1206 				break;
   1207 
   1208 			case 3:
   1209 				d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
   1210 				d.d_namlen = sizeof("self") - 1;
   1211 				memcpy(d.d_name, "self", sizeof("self"));
   1212 				d.d_type = DT_LNK;
   1213 				break;
   1214 
   1215 			default:
   1216 				while (pcnt < i) {
   1217 					pcnt++;
   1218 					p = LIST_NEXT(p, p_list);
   1219 					if (!p)
   1220 						goto done;
   1221 				}
   1222 				d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
   1223 				d.d_namlen = snprintf(d.d_name,
   1224 				    sizeof(d.d_name), "%ld", (long)p->p_pid);
   1225 				d.d_type = DT_DIR;
   1226 				p = p->p_list.le_next;
   1227 				break;
   1228 			}
   1229 
   1230 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1231 				break;
   1232 			nc++;
   1233 			if (cookies)
   1234 				*cookies++ = i + 1;
   1235 		}
   1236 	done:
   1237 
   1238 #ifdef PROCFS_ZOMBIE
   1239 		pd++;
   1240 		if (p == NULL && pd->pd_list != NULL)
   1241 			goto again;
   1242 #endif
   1243 		proclist_unlock_read();
   1244 
   1245 		skip = i - pcnt;
   1246 		if (skip >= nproc_root_targets)
   1247 			break;
   1248 		left = nproc_root_targets - skip;
   1249 		for (j = 0, pt = &proc_root_targets[0];
   1250 		     uio->uio_resid >= UIO_MX && j < left;
   1251 		     pt++, j++, i++) {
   1252 			if (pt->pt_valid &&
   1253 			    (*pt->pt_valid)(NULL, vp->v_mount) == 0)
   1254 				continue;
   1255 			d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
   1256 			d.d_namlen = pt->pt_namlen;
   1257 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1258 			d.d_type = pt->pt_type;
   1259 
   1260 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1261 				break;
   1262 			nc++;
   1263 			if (cookies)
   1264 				*cookies++ = i + 1;
   1265 		}
   1266 
   1267 		ncookies = nc;
   1268 		break;
   1269 	}
   1270 
   1271 	default:
   1272 		error = ENOTDIR;
   1273 		break;
   1274 	}
   1275 
   1276 	if (ap->a_ncookies) {
   1277 		if (error) {
   1278 			if (cookies)
   1279 				free(*ap->a_cookies, M_TEMP);
   1280 			*ap->a_ncookies = 0;
   1281 			*ap->a_cookies = NULL;
   1282 		} else
   1283 			*ap->a_ncookies = ncookies;
   1284 	}
   1285 	uio->uio_offset = i;
   1286 	return (error);
   1287 }
   1288 
   1289 /*
   1290  * readlink reads the link of `curproc'
   1291  */
   1292 int
   1293 procfs_readlink(v)
   1294 	void *v;
   1295 {
   1296 	struct vop_readlink_args *ap = v;
   1297 	char buf[16];		/* should be enough */
   1298 	char *bp = buf;
   1299 	char *path = NULL;
   1300 	int len;
   1301 	int error = 0;
   1302 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
   1303 
   1304 	if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
   1305 		len = snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
   1306 	else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
   1307 		len = snprintf(buf, sizeof(buf), "%s", "curproc");
   1308 	else {
   1309 		struct file *fp;
   1310 		struct proc *pown;
   1311 		struct vnode *vxp, *vp;
   1312 
   1313 		if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
   1314 			return error;
   1315 		FILE_USE(fp);
   1316 		switch (fp->f_type) {
   1317 		case DTYPE_VNODE:
   1318 			vxp = (struct vnode *)fp->f_data;
   1319 			if (vxp->v_type != VDIR) {
   1320 				FILE_UNUSE(fp, proc_representative_lwp(pown));
   1321 				return EINVAL;
   1322 			}
   1323 			if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
   1324 			    == NULL) {
   1325 				FILE_UNUSE(fp, proc_representative_lwp(pown));
   1326 				return ENOMEM;
   1327 			}
   1328 			bp = path + MAXPATHLEN;
   1329 			*--bp = '\0';
   1330 			vp = curproc->p_cwdi->cwdi_rdir;
   1331 			if (vp == NULL)
   1332 				vp = rootvnode;
   1333 			error = getcwd_common(vxp, vp, &bp, path,
   1334 			    MAXPATHLEN / 2, 0, curlwp);
   1335 			FILE_UNUSE(fp, proc_representative_lwp(pown));
   1336 			if (error) {
   1337 				free(path, M_TEMP);
   1338 				return error;
   1339 			}
   1340 			len = strlen(bp);
   1341 			break;
   1342 
   1343 		case DTYPE_MISC:
   1344 			len = snprintf(buf, sizeof(buf), "%s", "[misc]");
   1345 			break;
   1346 
   1347 		case DTYPE_KQUEUE:
   1348 			len = snprintf(buf, sizeof(buf), "%s", "[kqueue]");
   1349 			break;
   1350 
   1351 		default:
   1352 			return EINVAL;
   1353 		}
   1354 	}
   1355 
   1356 	error = uiomove(bp, len, ap->a_uio);
   1357 	if (path)
   1358 		free(path, M_TEMP);
   1359 	return error;
   1360 }
   1361 
   1362 /*
   1363  * convert decimal ascii to int
   1364  */
   1365 static int
   1366 atoi(b, len)
   1367 	const char *b;
   1368 	size_t len;
   1369 {
   1370 	int p = 0;
   1371 
   1372 	while (len--) {
   1373 		char c = *b++;
   1374 		if (c < '0' || c > '9')
   1375 			return -1;
   1376 		p = 10 * p + (c - '0');
   1377 	}
   1378 
   1379 	return p;
   1380 }
   1381