xref: /xnu-11417.140.69/bsd/miscfs/devfs/devfs_fdesc_support.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * Copyright (c) 1992, 1993
31*43a90889SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
32*43a90889SApple OSS Distributions  *
33*43a90889SApple OSS Distributions  * This code is derived from software donated to Berkeley by
34*43a90889SApple OSS Distributions  * Jan-Simon Pendry.
35*43a90889SApple OSS Distributions  *
36*43a90889SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
37*43a90889SApple OSS Distributions  * modification, are permitted provided that the following conditions
38*43a90889SApple OSS Distributions  * are met:
39*43a90889SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
40*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
41*43a90889SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
42*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
43*43a90889SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
44*43a90889SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
45*43a90889SApple OSS Distributions  *    must display the following acknowledgement:
46*43a90889SApple OSS Distributions  *	This product includes software developed by the University of
47*43a90889SApple OSS Distributions  *	California, Berkeley and its contributors.
48*43a90889SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
49*43a90889SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
50*43a90889SApple OSS Distributions  *    without specific prior written permission.
51*43a90889SApple OSS Distributions  *
52*43a90889SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53*43a90889SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54*43a90889SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55*43a90889SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56*43a90889SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57*43a90889SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58*43a90889SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59*43a90889SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60*43a90889SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61*43a90889SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62*43a90889SApple OSS Distributions  * SUCH DAMAGE.
63*43a90889SApple OSS Distributions  *
64*43a90889SApple OSS Distributions  *	@(#)fdesc_vnops.c	8.17 (Berkeley) 5/22/95
65*43a90889SApple OSS Distributions  *
66*43a90889SApple OSS Distributions  */
67*43a90889SApple OSS Distributions 
68*43a90889SApple OSS Distributions /*
69*43a90889SApple OSS Distributions  * /dev/fd Filesystem
70*43a90889SApple OSS Distributions  */
71*43a90889SApple OSS Distributions 
72*43a90889SApple OSS Distributions #include <sys/param.h>
73*43a90889SApple OSS Distributions #include <sys/systm.h>
74*43a90889SApple OSS Distributions #include <sys/types.h>
75*43a90889SApple OSS Distributions #include <sys/time.h>
76*43a90889SApple OSS Distributions #include <sys/proc_internal.h>
77*43a90889SApple OSS Distributions #include <sys/kernel.h> /* boottime */
78*43a90889SApple OSS Distributions #include <sys/resourcevar.h>
79*43a90889SApple OSS Distributions #include <sys/filedesc.h>
80*43a90889SApple OSS Distributions #include <sys/kauth.h>
81*43a90889SApple OSS Distributions #include <sys/vnode_internal.h>
82*43a90889SApple OSS Distributions #include <sys/malloc.h>
83*43a90889SApple OSS Distributions #include <sys/file_internal.h>
84*43a90889SApple OSS Distributions #include <sys/stat.h>
85*43a90889SApple OSS Distributions #include <sys/mount_internal.h>
86*43a90889SApple OSS Distributions #include <sys/namei.h>
87*43a90889SApple OSS Distributions #include <sys/dirent.h>
88*43a90889SApple OSS Distributions #include <sys/ubc.h>
89*43a90889SApple OSS Distributions #include <sys/socketvar.h>
90*43a90889SApple OSS Distributions #include <sys/pipe.h>
91*43a90889SApple OSS Distributions #include <sys/uio_internal.h>
92*43a90889SApple OSS Distributions #include <vfs/vfs_support.h>
93*43a90889SApple OSS Distributions #include <pexpert/pexpert.h>
94*43a90889SApple OSS Distributions #include <miscfs/devfs/fdesc.h>
95*43a90889SApple OSS Distributions #include <miscfs/devfs/devfs.h>
96*43a90889SApple OSS Distributions #include <miscfs/devfs/devfsdefs.h>
97*43a90889SApple OSS Distributions 
98*43a90889SApple OSS Distributions #define FDL_WANT        0x01
99*43a90889SApple OSS Distributions #define FDL_LOCKED      0x02
100*43a90889SApple OSS Distributions static int fdcache_lock;
101*43a90889SApple OSS Distributions 
102*43a90889SApple OSS Distributions 
103*43a90889SApple OSS Distributions #if (FD_STDIN != FD_STDOUT - 1) || (FD_STDOUT != FD_STDERR - 1)
104*43a90889SApple OSS Distributions FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n + 1, n + 2
105*43a90889SApple OSS Distributions #endif
106*43a90889SApple OSS Distributions 
107*43a90889SApple OSS Distributions #define NFDCACHE 3
108*43a90889SApple OSS Distributions 
109*43a90889SApple OSS Distributions #define FD_NHASH(ix) \
110*43a90889SApple OSS Distributions 	(&fdhashtbl[(ix) & fdhash])
111*43a90889SApple OSS Distributions LIST_HEAD(fdhashhead, fdescnode) * fdhashtbl;
112*43a90889SApple OSS Distributions u_long fdhash;
113*43a90889SApple OSS Distributions 
114*43a90889SApple OSS Distributions static int fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context);
115*43a90889SApple OSS Distributions 
116*43a90889SApple OSS Distributions static LCK_GRP_DECLARE(fdesc_lckgrp, "fdesc");
117*43a90889SApple OSS Distributions static LCK_MTX_DECLARE(fdesc_mtx, &fdesc_lckgrp);
118*43a90889SApple OSS Distributions 
119*43a90889SApple OSS Distributions static void
fdesc_lock(void)120*43a90889SApple OSS Distributions fdesc_lock(void)
121*43a90889SApple OSS Distributions {
122*43a90889SApple OSS Distributions 	lck_mtx_lock(&fdesc_mtx);
123*43a90889SApple OSS Distributions }
124*43a90889SApple OSS Distributions 
125*43a90889SApple OSS Distributions static void
fdesc_unlock(void)126*43a90889SApple OSS Distributions fdesc_unlock(void)
127*43a90889SApple OSS Distributions {
128*43a90889SApple OSS Distributions 	lck_mtx_unlock(&fdesc_mtx);
129*43a90889SApple OSS Distributions }
130*43a90889SApple OSS Distributions 
131*43a90889SApple OSS Distributions 
132*43a90889SApple OSS Distributions /*
133*43a90889SApple OSS Distributions  * Initialise cache headers, create the devfs node
134*43a90889SApple OSS Distributions  */
135*43a90889SApple OSS Distributions int
devfs_fdesc_init()136*43a90889SApple OSS Distributions devfs_fdesc_init()
137*43a90889SApple OSS Distributions {
138*43a90889SApple OSS Distributions 	int error = 0;
139*43a90889SApple OSS Distributions 	devnode_t *rootdir = dev_root->de_dnp;
140*43a90889SApple OSS Distributions 	devdirent_t *direntp;
141*43a90889SApple OSS Distributions 
142*43a90889SApple OSS Distributions 	/* XXX Make sure you have the right path... */
143*43a90889SApple OSS Distributions 	fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
144*43a90889SApple OSS Distributions 
145*43a90889SApple OSS Distributions 	DEVFS_LOCK();
146*43a90889SApple OSS Distributions 	dev_add_entry("fd", rootdir, DEV_DEVFD, NULL, NULL, NULL, &direntp);
147*43a90889SApple OSS Distributions 	devfs_fdesc_makelinks();
148*43a90889SApple OSS Distributions 	DEVFS_UNLOCK();
149*43a90889SApple OSS Distributions 
150*43a90889SApple OSS Distributions 	return error;
151*43a90889SApple OSS Distributions }
152*43a90889SApple OSS Distributions 
153*43a90889SApple OSS Distributions /*
154*43a90889SApple OSS Distributions  * Called during early startup, no need to synchronize
155*43a90889SApple OSS Distributions  */
156*43a90889SApple OSS Distributions int
devfs_fdesc_makelinks()157*43a90889SApple OSS Distributions devfs_fdesc_makelinks()
158*43a90889SApple OSS Distributions {
159*43a90889SApple OSS Distributions 	int error = 0;
160*43a90889SApple OSS Distributions 	devdirent_t *stdin_ent = NULL, *stdout_ent = NULL, *stderr_ent = NULL;
161*43a90889SApple OSS Distributions 	devnode_t *root_devnode = dev_root->de_dnp;
162*43a90889SApple OSS Distributions 
163*43a90889SApple OSS Distributions 	/* We do this ugliness to get around some "const" warnings */
164*43a90889SApple OSS Distributions 	char in[] = "stdin";
165*43a90889SApple OSS Distributions 	char out[] = "stdout";
166*43a90889SApple OSS Distributions 	char err[] = "stderr";
167*43a90889SApple OSS Distributions 	char zero[] = "fd/0";
168*43a90889SApple OSS Distributions 	char one[] = "fd/1";
169*43a90889SApple OSS Distributions 	char two[] = "fd/2";
170*43a90889SApple OSS Distributions 
171*43a90889SApple OSS Distributions 	if ((error = devfs_make_symlink(root_devnode, in, 0555, zero, &stdin_ent))) {
172*43a90889SApple OSS Distributions 		printf("Couldn't make stdin, err %d.\n", error);
173*43a90889SApple OSS Distributions 		goto bad;
174*43a90889SApple OSS Distributions 	}
175*43a90889SApple OSS Distributions 
176*43a90889SApple OSS Distributions 	if ((error = devfs_make_symlink(root_devnode, out, 0555, one, &stdout_ent))) {
177*43a90889SApple OSS Distributions 		printf("Couldn't make stdout, err %d.\n", error);
178*43a90889SApple OSS Distributions 		goto bad;
179*43a90889SApple OSS Distributions 	}
180*43a90889SApple OSS Distributions 
181*43a90889SApple OSS Distributions 	if ((error = devfs_make_symlink(root_devnode, err, 0555, two, &stderr_ent))) {
182*43a90889SApple OSS Distributions 		printf("Couldn't make stderr, err %d.\n", error);
183*43a90889SApple OSS Distributions 		goto bad;
184*43a90889SApple OSS Distributions 	}
185*43a90889SApple OSS Distributions 
186*43a90889SApple OSS Distributions 	return 0;
187*43a90889SApple OSS Distributions 
188*43a90889SApple OSS Distributions bad:
189*43a90889SApple OSS Distributions 	if (stdin_ent) {
190*43a90889SApple OSS Distributions 		dev_free_name(stdin_ent);
191*43a90889SApple OSS Distributions 	}
192*43a90889SApple OSS Distributions 	if (stdout_ent) {
193*43a90889SApple OSS Distributions 		dev_free_name(stdout_ent);
194*43a90889SApple OSS Distributions 	}
195*43a90889SApple OSS Distributions 	if (stderr_ent) {
196*43a90889SApple OSS Distributions 		dev_free_name(stderr_ent);
197*43a90889SApple OSS Distributions 	}
198*43a90889SApple OSS Distributions 
199*43a90889SApple OSS Distributions 	return error;
200*43a90889SApple OSS Distributions }
201*43a90889SApple OSS Distributions 
202*43a90889SApple OSS Distributions int
fdesc_allocvp(fdntype ftype,int ix,struct mount * mp,struct vnode ** vpp,enum vtype vtype,int fdno)203*43a90889SApple OSS Distributions fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp, enum vtype vtype, int fdno)
204*43a90889SApple OSS Distributions {
205*43a90889SApple OSS Distributions 	struct fdhashhead *fc;
206*43a90889SApple OSS Distributions 	struct fdescnode *fd;
207*43a90889SApple OSS Distributions 	int error = 0;
208*43a90889SApple OSS Distributions 	int vid = 0;
209*43a90889SApple OSS Distributions 	struct vnode_fsparam vfsp;
210*43a90889SApple OSS Distributions 	struct vnode *vp;
211*43a90889SApple OSS Distributions 
212*43a90889SApple OSS Distributions 	fdesc_lock();
213*43a90889SApple OSS Distributions 
214*43a90889SApple OSS Distributions 	fc = FD_NHASH(ix);
215*43a90889SApple OSS Distributions loop:
216*43a90889SApple OSS Distributions 	for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
217*43a90889SApple OSS Distributions 		if (fd->fd_ix == ix && vnode_mount(fd->fd_vnode) == mp) {
218*43a90889SApple OSS Distributions 			vid = vnode_vid(fd->fd_vnode);
219*43a90889SApple OSS Distributions 			vp = fd->fd_vnode;
220*43a90889SApple OSS Distributions 			vnode_hold(vp);
221*43a90889SApple OSS Distributions 			fdesc_unlock();
222*43a90889SApple OSS Distributions 
223*43a90889SApple OSS Distributions 			if (vnode_getwithvid(vp, vid)) {
224*43a90889SApple OSS Distributions 				vnode_drop(vp);
225*43a90889SApple OSS Distributions 				fdesc_lock();
226*43a90889SApple OSS Distributions 				goto loop;
227*43a90889SApple OSS Distributions 			}
228*43a90889SApple OSS Distributions 
229*43a90889SApple OSS Distributions 			vnode_drop(vp);
230*43a90889SApple OSS Distributions 			*vpp = vp;
231*43a90889SApple OSS Distributions 			(*vpp)->v_type = (uint16_t)vtype;
232*43a90889SApple OSS Distributions 
233*43a90889SApple OSS Distributions 			return error;
234*43a90889SApple OSS Distributions 		}
235*43a90889SApple OSS Distributions 	}
236*43a90889SApple OSS Distributions 
237*43a90889SApple OSS Distributions 	/* Only one thread can add to the hash at a time */
238*43a90889SApple OSS Distributions 	if (fdcache_lock & FDL_LOCKED) {
239*43a90889SApple OSS Distributions 		fdcache_lock |= FDL_WANT;
240*43a90889SApple OSS Distributions 		msleep((caddr_t) &fdcache_lock, &fdesc_mtx, PINOD, "fdesc_allocvp", NULL);
241*43a90889SApple OSS Distributions 		goto loop;
242*43a90889SApple OSS Distributions 	}
243*43a90889SApple OSS Distributions 
244*43a90889SApple OSS Distributions 	fdcache_lock |= FDL_LOCKED;
245*43a90889SApple OSS Distributions 	fdesc_unlock();
246*43a90889SApple OSS Distributions 
247*43a90889SApple OSS Distributions 	fd = kalloc_type(struct fdescnode, Z_WAITOK);
248*43a90889SApple OSS Distributions 
249*43a90889SApple OSS Distributions 	vfsp.vnfs_mp = mp;
250*43a90889SApple OSS Distributions 	vfsp.vnfs_vtype = vtype;
251*43a90889SApple OSS Distributions 	vfsp.vnfs_str = "fdesc";
252*43a90889SApple OSS Distributions 	vfsp.vnfs_dvp = NULL;
253*43a90889SApple OSS Distributions 	vfsp.vnfs_fsnode = fd;
254*43a90889SApple OSS Distributions 	vfsp.vnfs_cnp = NULL;
255*43a90889SApple OSS Distributions 	vfsp.vnfs_vops = fdesc_vnodeop_p;
256*43a90889SApple OSS Distributions 	vfsp.vnfs_rdev = 0;
257*43a90889SApple OSS Distributions 	vfsp.vnfs_filesize = 0;
258*43a90889SApple OSS Distributions 	vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
259*43a90889SApple OSS Distributions 	vfsp.vnfs_marksystem = 0;
260*43a90889SApple OSS Distributions 	vfsp.vnfs_markroot = 0;
261*43a90889SApple OSS Distributions 
262*43a90889SApple OSS Distributions 	error = vnode_create_ext(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, vpp,
263*43a90889SApple OSS Distributions 	    VNODE_CREATE_DEFAULT);
264*43a90889SApple OSS Distributions 	if (error) {
265*43a90889SApple OSS Distributions 		kfree_type(struct fdescnode, fd);
266*43a90889SApple OSS Distributions 		fdesc_lock();
267*43a90889SApple OSS Distributions 		goto out;
268*43a90889SApple OSS Distributions 	}
269*43a90889SApple OSS Distributions 
270*43a90889SApple OSS Distributions 	(*vpp)->v_tag = VT_FDESC;
271*43a90889SApple OSS Distributions 	fd->fd_vnode = *vpp;
272*43a90889SApple OSS Distributions 	fd->fd_type = ftype;
273*43a90889SApple OSS Distributions 	fd->fd_fd = fdno;
274*43a90889SApple OSS Distributions 	fd->fd_link = NULL;
275*43a90889SApple OSS Distributions 	fd->fd_ix = ix;
276*43a90889SApple OSS Distributions 
277*43a90889SApple OSS Distributions 	fdesc_lock();
278*43a90889SApple OSS Distributions 
279*43a90889SApple OSS Distributions 	LIST_INSERT_HEAD(fc, fd, fd_hash);
280*43a90889SApple OSS Distributions out:
281*43a90889SApple OSS Distributions 	/* Hold the lock when we get here */
282*43a90889SApple OSS Distributions 	fdcache_lock &= ~FDL_LOCKED;
283*43a90889SApple OSS Distributions 
284*43a90889SApple OSS Distributions 	if (fdcache_lock & FDL_WANT) {
285*43a90889SApple OSS Distributions 		fdcache_lock &= ~FDL_WANT;
286*43a90889SApple OSS Distributions 		wakeup((caddr_t) &fdcache_lock);
287*43a90889SApple OSS Distributions 	}
288*43a90889SApple OSS Distributions 
289*43a90889SApple OSS Distributions 	fdesc_unlock();
290*43a90889SApple OSS Distributions 
291*43a90889SApple OSS Distributions 	return error;
292*43a90889SApple OSS Distributions }
293*43a90889SApple OSS Distributions 
294*43a90889SApple OSS Distributions /*
295*43a90889SApple OSS Distributions  * vp is the current namei directory
296*43a90889SApple OSS Distributions  * ndp is the name to locate in that directory...
297*43a90889SApple OSS Distributions  *
298*43a90889SApple OSS Distributions  * This vnop should only be called on the special directory /dev/fd.
299*43a90889SApple OSS Distributions  */
300*43a90889SApple OSS Distributions int
devfs_devfd_lookup(struct vnop_lookup_args * ap)301*43a90889SApple OSS Distributions devfs_devfd_lookup(struct vnop_lookup_args *ap)
302*43a90889SApple OSS Distributions {
303*43a90889SApple OSS Distributions 	struct vnode **vpp = ap->a_vpp;
304*43a90889SApple OSS Distributions 	struct vnode *dvp = ap->a_dvp;
305*43a90889SApple OSS Distributions 	struct componentname *cnp = ap->a_cnp;
306*43a90889SApple OSS Distributions 	char *pname = cnp->cn_nameptr;
307*43a90889SApple OSS Distributions 	struct proc *p = vfs_context_proc(ap->a_context);
308*43a90889SApple OSS Distributions 	int numfiles = p->p_fd.fd_nfiles;
309*43a90889SApple OSS Distributions 	int fd;
310*43a90889SApple OSS Distributions 	int error;
311*43a90889SApple OSS Distributions 	struct vnode *fvp;
312*43a90889SApple OSS Distributions 
313*43a90889SApple OSS Distributions 	if (cnp->cn_namelen == 1 && *pname == '.') {
314*43a90889SApple OSS Distributions 		*vpp = dvp;
315*43a90889SApple OSS Distributions 
316*43a90889SApple OSS Distributions 		if ((error = vnode_get(dvp))) {
317*43a90889SApple OSS Distributions 			goto bad;
318*43a90889SApple OSS Distributions 		}
319*43a90889SApple OSS Distributions 		return 0;
320*43a90889SApple OSS Distributions 	}
321*43a90889SApple OSS Distributions 
322*43a90889SApple OSS Distributions 	fd = 0;
323*43a90889SApple OSS Distributions 	while (*pname >= '0' && *pname <= '9') {
324*43a90889SApple OSS Distributions 		if (os_mul_and_add_overflow(fd, 10, *pname++ - '0', &fd)) {
325*43a90889SApple OSS Distributions 			break;
326*43a90889SApple OSS Distributions 		}
327*43a90889SApple OSS Distributions 	}
328*43a90889SApple OSS Distributions 
329*43a90889SApple OSS Distributions 	if (*pname != '\0') {
330*43a90889SApple OSS Distributions 		error = ENOENT;
331*43a90889SApple OSS Distributions 		goto bad;
332*43a90889SApple OSS Distributions 	}
333*43a90889SApple OSS Distributions 
334*43a90889SApple OSS Distributions 	if (fd < 0 || fd >= numfiles ||
335*43a90889SApple OSS Distributions 	    *fdfile(p, fd) == NULL ||
336*43a90889SApple OSS Distributions 	    (*fdflags(p, fd) & UF_RESERVED)) {
337*43a90889SApple OSS Distributions 		error = EBADF;
338*43a90889SApple OSS Distributions 		goto bad;
339*43a90889SApple OSS Distributions 	}
340*43a90889SApple OSS Distributions 
341*43a90889SApple OSS Distributions 	/*
342*43a90889SApple OSS Distributions 	 * For OP_SETATTR lookups, resolve the backing vnode now so we do
343*43a90889SApple OSS Distributions 	 * MACF checks on the right thing.
344*43a90889SApple OSS Distributions 	 *
345*43a90889SApple OSS Distributions 	 * If there is no vnode (i.e. something else is open at that fd) then
346*43a90889SApple OSS Distributions 	 * just return an fdescnode.
347*43a90889SApple OSS Distributions 	 */
348*43a90889SApple OSS Distributions 	if ((cnp->cn_ndp->ni_op == OP_SETATTR) &&
349*43a90889SApple OSS Distributions 	    (0 == vnode_getfromfd(ap->a_context, fd, &fvp))) {
350*43a90889SApple OSS Distributions 		cnp->cn_flags &= ~MAKEENTRY;
351*43a90889SApple OSS Distributions 		*vpp = fvp;
352*43a90889SApple OSS Distributions 		return 0;
353*43a90889SApple OSS Distributions 	}
354*43a90889SApple OSS Distributions 
355*43a90889SApple OSS Distributions 	error = fdesc_allocvp(Fdesc, FD_DESC + fd, dvp->v_mount, &fvp, VNON, fd);
356*43a90889SApple OSS Distributions 	if (error) {
357*43a90889SApple OSS Distributions 		goto bad;
358*43a90889SApple OSS Distributions 	}
359*43a90889SApple OSS Distributions 	*vpp = fvp;
360*43a90889SApple OSS Distributions 	return 0;
361*43a90889SApple OSS Distributions 
362*43a90889SApple OSS Distributions bad:
363*43a90889SApple OSS Distributions 	*vpp = NULL;
364*43a90889SApple OSS Distributions 	return error;
365*43a90889SApple OSS Distributions }
366*43a90889SApple OSS Distributions 
367*43a90889SApple OSS Distributions int
fdesc_open(struct vnop_open_args * ap)368*43a90889SApple OSS Distributions fdesc_open(struct vnop_open_args *ap)
369*43a90889SApple OSS Distributions {
370*43a90889SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
371*43a90889SApple OSS Distributions 	thread_t thr = vfs_context_thread(ap->a_context);
372*43a90889SApple OSS Distributions 	uthread_t uu;
373*43a90889SApple OSS Distributions 	int error = 0;
374*43a90889SApple OSS Distributions 
375*43a90889SApple OSS Distributions 	if (thr == NULL) {
376*43a90889SApple OSS Distributions 		return EINVAL;
377*43a90889SApple OSS Distributions 	}
378*43a90889SApple OSS Distributions 
379*43a90889SApple OSS Distributions 	uu = get_bsdthread_info(thr);
380*43a90889SApple OSS Distributions 
381*43a90889SApple OSS Distributions 	switch (VTOFDESC(vp)->fd_type) {
382*43a90889SApple OSS Distributions 	case Fdesc:
383*43a90889SApple OSS Distributions 		/*
384*43a90889SApple OSS Distributions 		 * XXX Kludge: set uu->uu_dupfd to contain the value of the
385*43a90889SApple OSS Distributions 		 * the file descriptor being sought for duplication. The error
386*43a90889SApple OSS Distributions 		 * return ensures that the vnode for this device will be
387*43a90889SApple OSS Distributions 		 * released by vn_open. Open will detect this special error and
388*43a90889SApple OSS Distributions 		 * take the actions in dupfdopen.  Other callers of vn_open or
389*43a90889SApple OSS Distributions 		 * vnop_open will simply report the error.
390*43a90889SApple OSS Distributions 		 */
391*43a90889SApple OSS Distributions 		uu->uu_dupfd = VTOFDESC(vp)->fd_fd;     /* XXX */
392*43a90889SApple OSS Distributions 		error = ENODEV;
393*43a90889SApple OSS Distributions 		break;
394*43a90889SApple OSS Distributions 	default:
395*43a90889SApple OSS Distributions 		panic("Invalid type for fdesc node!");
396*43a90889SApple OSS Distributions 		break;
397*43a90889SApple OSS Distributions 	}
398*43a90889SApple OSS Distributions 
399*43a90889SApple OSS Distributions 	return error;
400*43a90889SApple OSS Distributions }
401*43a90889SApple OSS Distributions 
402*43a90889SApple OSS Distributions static int
fdesc_attr(int fd,struct vnode_attr * vap,vfs_context_t a_context)403*43a90889SApple OSS Distributions fdesc_attr(int fd, struct vnode_attr *vap, vfs_context_t a_context)
404*43a90889SApple OSS Distributions {
405*43a90889SApple OSS Distributions 	struct fileproc *fp;
406*43a90889SApple OSS Distributions 	struct proc *p = vfs_context_proc(a_context);
407*43a90889SApple OSS Distributions 	struct stat stb;
408*43a90889SApple OSS Distributions 	int error;
409*43a90889SApple OSS Distributions 
410*43a90889SApple OSS Distributions 	if ((error = fp_lookup(p, fd, &fp, 0))) {
411*43a90889SApple OSS Distributions 		return error;
412*43a90889SApple OSS Distributions 	}
413*43a90889SApple OSS Distributions 	switch (FILEGLOB_DTYPE(fp->fp_glob)) {
414*43a90889SApple OSS Distributions 	case DTYPE_VNODE:
415*43a90889SApple OSS Distributions 		if ((error = vnode_getwithref((struct vnode *)fp_get_data(fp))) != 0) {
416*43a90889SApple OSS Distributions 			break;
417*43a90889SApple OSS Distributions 		}
418*43a90889SApple OSS Distributions 		if ((error = vnode_authorize((struct vnode *)fp_get_data(fp),
419*43a90889SApple OSS Distributions 		    NULL,
420*43a90889SApple OSS Distributions 		    KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY,
421*43a90889SApple OSS Distributions 		    a_context)) == 0) {
422*43a90889SApple OSS Distributions 			error = vnode_getattr((struct vnode *)fp_get_data(fp), vap, a_context);
423*43a90889SApple OSS Distributions 		}
424*43a90889SApple OSS Distributions 		if (error == 0 && vap->va_type == VDIR) {
425*43a90889SApple OSS Distributions 			/*
426*43a90889SApple OSS Distributions 			 * directories can cause loops in the namespace,
427*43a90889SApple OSS Distributions 			 * so turn off the 'x' bits to avoid trouble.
428*43a90889SApple OSS Distributions 			 *
429*43a90889SApple OSS Distributions 			 * XXX ACLs break this, of course
430*43a90889SApple OSS Distributions 			 */
431*43a90889SApple OSS Distributions 			vap->va_mode &= ~((VEXEC) | (VEXEC >> 3) | (VEXEC >> 6));
432*43a90889SApple OSS Distributions 		}
433*43a90889SApple OSS Distributions 		(void)vnode_put((struct vnode *)fp_get_data(fp));
434*43a90889SApple OSS Distributions 		break;
435*43a90889SApple OSS Distributions 
436*43a90889SApple OSS Distributions 	case DTYPE_SOCKET:
437*43a90889SApple OSS Distributions 	case DTYPE_PIPE:
438*43a90889SApple OSS Distributions #if SOCKETS
439*43a90889SApple OSS Distributions 		if (FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) {
440*43a90889SApple OSS Distributions 			error = soo_stat((struct socket *)fp_get_data(fp), (void *)&stb, 0);
441*43a90889SApple OSS Distributions 		} else
442*43a90889SApple OSS Distributions #endif /* SOCKETS */
443*43a90889SApple OSS Distributions 		error = pipe_stat((struct pipe *)fp_get_data(fp), (void *)&stb, 0);
444*43a90889SApple OSS Distributions 
445*43a90889SApple OSS Distributions 		if (error == 0) {
446*43a90889SApple OSS Distributions 			if (FILEGLOB_DTYPE(fp->fp_glob) == DTYPE_SOCKET) {
447*43a90889SApple OSS Distributions 				VATTR_RETURN(vap, va_type, VSOCK);
448*43a90889SApple OSS Distributions 			} else {
449*43a90889SApple OSS Distributions 				VATTR_RETURN(vap, va_type, VFIFO);
450*43a90889SApple OSS Distributions 			}
451*43a90889SApple OSS Distributions 
452*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_mode, stb.st_mode);
453*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_nlink, stb.st_nlink);
454*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_uid, stb.st_uid);
455*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_gid, stb.st_gid);
456*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_fsid, stb.st_dev);
457*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_fileid, stb.st_ino);
458*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_data_size, stb.st_size);
459*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_access_time, stb.st_atimespec);
460*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_modify_time, stb.st_mtimespec);
461*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_change_time, stb.st_ctimespec);
462*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_gen, stb.st_gen);
463*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_flags, stb.st_flags);
464*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_rdev, stb.st_rdev);
465*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_total_alloc, stb.st_blocks * stb.st_blksize);
466*43a90889SApple OSS Distributions 			VATTR_RETURN(vap, va_acl, NULL);
467*43a90889SApple OSS Distributions 		}
468*43a90889SApple OSS Distributions 		break;
469*43a90889SApple OSS Distributions 
470*43a90889SApple OSS Distributions 	default:
471*43a90889SApple OSS Distributions 		error = EBADF;
472*43a90889SApple OSS Distributions 	}
473*43a90889SApple OSS Distributions 
474*43a90889SApple OSS Distributions 	fp_drop(p, fd, fp, 0);
475*43a90889SApple OSS Distributions 	return error;
476*43a90889SApple OSS Distributions }
477*43a90889SApple OSS Distributions 
478*43a90889SApple OSS Distributions int
fdesc_getattr(struct vnop_getattr_args * ap)479*43a90889SApple OSS Distributions fdesc_getattr(struct vnop_getattr_args *ap)
480*43a90889SApple OSS Distributions {
481*43a90889SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
482*43a90889SApple OSS Distributions 	struct vnode_attr *vap = ap->a_vap;
483*43a90889SApple OSS Distributions 	unsigned fd;
484*43a90889SApple OSS Distributions 	int error = 0;
485*43a90889SApple OSS Distributions 
486*43a90889SApple OSS Distributions 	switch (VTOFDESC(vp)->fd_type) {
487*43a90889SApple OSS Distributions 	case Fdesc:
488*43a90889SApple OSS Distributions 		fd = VTOFDESC(vp)->fd_fd;
489*43a90889SApple OSS Distributions 		error = fdesc_attr(fd, vap, ap->a_context);
490*43a90889SApple OSS Distributions 		break;
491*43a90889SApple OSS Distributions 
492*43a90889SApple OSS Distributions 	default:
493*43a90889SApple OSS Distributions 		panic("Invalid type for an fdesc node!");
494*43a90889SApple OSS Distributions 		break;
495*43a90889SApple OSS Distributions 	}
496*43a90889SApple OSS Distributions 
497*43a90889SApple OSS Distributions 	/*
498*43a90889SApple OSS Distributions 	 * Yes, we do this without locking, but this value is always just
499*43a90889SApple OSS Distributions 	 * a snapshot.
500*43a90889SApple OSS Distributions 	 */
501*43a90889SApple OSS Distributions 	if (error == 0) {
502*43a90889SApple OSS Distributions 		vp->v_type = (uint16_t)vap->va_type;
503*43a90889SApple OSS Distributions 
504*43a90889SApple OSS Distributions 		/* We need an inactive to reset type to VNON */
505*43a90889SApple OSS Distributions 		vnode_setneedinactive(vp);
506*43a90889SApple OSS Distributions 	}
507*43a90889SApple OSS Distributions 
508*43a90889SApple OSS Distributions 	return error;
509*43a90889SApple OSS Distributions }
510*43a90889SApple OSS Distributions 
511*43a90889SApple OSS Distributions int
fdesc_setattr(struct vnop_setattr_args * ap)512*43a90889SApple OSS Distributions fdesc_setattr(struct vnop_setattr_args *ap)
513*43a90889SApple OSS Distributions {
514*43a90889SApple OSS Distributions 	struct fileproc *fp;
515*43a90889SApple OSS Distributions 	unsigned fd;
516*43a90889SApple OSS Distributions 	int error;
517*43a90889SApple OSS Distributions 	struct proc * p = vfs_context_proc(ap->a_context);
518*43a90889SApple OSS Distributions 
519*43a90889SApple OSS Distributions 	/*
520*43a90889SApple OSS Distributions 	 * Can't mess with the root vnode
521*43a90889SApple OSS Distributions 	 */
522*43a90889SApple OSS Distributions 	switch (VTOFDESC(ap->a_vp)->fd_type) {
523*43a90889SApple OSS Distributions 	case Fdesc:
524*43a90889SApple OSS Distributions 		break;
525*43a90889SApple OSS Distributions 	default:
526*43a90889SApple OSS Distributions 		panic("Invalid type for an fdesc node!");
527*43a90889SApple OSS Distributions 		return EACCES;
528*43a90889SApple OSS Distributions 	}
529*43a90889SApple OSS Distributions 
530*43a90889SApple OSS Distributions 	fd = VTOFDESC(ap->a_vp)->fd_fd;
531*43a90889SApple OSS Distributions 	if ((error = fp_lookup(vfs_context_proc(ap->a_context), fd, &fp, 0))) {
532*43a90889SApple OSS Distributions 		return error;
533*43a90889SApple OSS Distributions 	}
534*43a90889SApple OSS Distributions 
535*43a90889SApple OSS Distributions 	switch (FILEGLOB_DTYPE(fp->fp_glob)) {
536*43a90889SApple OSS Distributions 	case DTYPE_VNODE:
537*43a90889SApple OSS Distributions 	/*
538*43a90889SApple OSS Distributions 	 * We shouldn't get here unless we were raced with close/open.
539*43a90889SApple OSS Distributions 	 * In that case, shenanigans are happening and it's unsafe to
540*43a90889SApple OSS Distributions 	 * take action on the `setattr`; the MACF and UNIX permission
541*43a90889SApple OSS Distributions 	 * checks are no longer valid (TOCTOU).
542*43a90889SApple OSS Distributions 	 *
543*43a90889SApple OSS Distributions 	 * Fall through to report success.
544*43a90889SApple OSS Distributions 	 */
545*43a90889SApple OSS Distributions 	case DTYPE_SOCKET:
546*43a90889SApple OSS Distributions 	case DTYPE_PIPE:
547*43a90889SApple OSS Distributions 		error = 0;
548*43a90889SApple OSS Distributions 		break;
549*43a90889SApple OSS Distributions 
550*43a90889SApple OSS Distributions 	default:
551*43a90889SApple OSS Distributions 		error = EBADF;
552*43a90889SApple OSS Distributions 		break;
553*43a90889SApple OSS Distributions 	}
554*43a90889SApple OSS Distributions 
555*43a90889SApple OSS Distributions 	fp_drop(p, fd, fp, 0);
556*43a90889SApple OSS Distributions 	return error;
557*43a90889SApple OSS Distributions }
558*43a90889SApple OSS Distributions 
559*43a90889SApple OSS Distributions #define UIO_MX 16
560*43a90889SApple OSS Distributions 
561*43a90889SApple OSS Distributions /*
562*43a90889SApple OSS Distributions  *  static struct dirtmp {
563*43a90889SApple OSS Distributions  *       u_int32_t d_fileno;
564*43a90889SApple OSS Distributions  *       u_short d_reclen;
565*43a90889SApple OSS Distributions  *       u_short d_namlen;
566*43a90889SApple OSS Distributions  *       char d_name[8];
567*43a90889SApple OSS Distributions  *  } rootent[] = {
568*43a90889SApple OSS Distributions  *       { FD_DEVFD, UIO_MX, 2, "fd" },
569*43a90889SApple OSS Distributions  *       { FD_STDIN, UIO_MX, 5, "stdin" },
570*43a90889SApple OSS Distributions  *       { FD_STDOUT, UIO_MX, 6, "stdout" },
571*43a90889SApple OSS Distributions  *       { FD_STDERR, UIO_MX, 6, "stderr" },
572*43a90889SApple OSS Distributions  *       { 0, 0, 0, "" }
573*43a90889SApple OSS Distributions  *  };
574*43a90889SApple OSS Distributions  */
575*43a90889SApple OSS Distributions 
576*43a90889SApple OSS Distributions /* Only called on /dev/fd */
577*43a90889SApple OSS Distributions int
devfs_devfd_readdir(struct vnop_readdir_args * ap)578*43a90889SApple OSS Distributions devfs_devfd_readdir(struct vnop_readdir_args *ap)
579*43a90889SApple OSS Distributions {
580*43a90889SApple OSS Distributions 	struct uio *uio = ap->a_uio;
581*43a90889SApple OSS Distributions 	struct proc *p = current_proc();
582*43a90889SApple OSS Distributions 	off_t i;
583*43a90889SApple OSS Distributions 	int error;
584*43a90889SApple OSS Distributions 
585*43a90889SApple OSS Distributions 	/*
586*43a90889SApple OSS Distributions 	 * We don't allow exporting fdesc mounts, and currently local
587*43a90889SApple OSS Distributions 	 * requests do not need cookies.
588*43a90889SApple OSS Distributions 	 */
589*43a90889SApple OSS Distributions 	if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF)) {
590*43a90889SApple OSS Distributions 		return EINVAL;
591*43a90889SApple OSS Distributions 	}
592*43a90889SApple OSS Distributions 
593*43a90889SApple OSS Distributions 	/*
594*43a90889SApple OSS Distributions 	 * There needs to be space for at least one entry.
595*43a90889SApple OSS Distributions 	 */
596*43a90889SApple OSS Distributions 	if (uio_resid(uio) < UIO_MX) {
597*43a90889SApple OSS Distributions 		return EINVAL;
598*43a90889SApple OSS Distributions 	}
599*43a90889SApple OSS Distributions 
600*43a90889SApple OSS Distributions 	i = uio->uio_offset / UIO_MX;
601*43a90889SApple OSS Distributions 	error = 0;
602*43a90889SApple OSS Distributions 	while (uio_resid(uio) >= UIO_MX) {
603*43a90889SApple OSS Distributions 		if (i >= p->p_fd.fd_nfiles || i < 0) {
604*43a90889SApple OSS Distributions 			break;
605*43a90889SApple OSS Distributions 		}
606*43a90889SApple OSS Distributions 
607*43a90889SApple OSS Distributions 		if (*fdfile(p, i) != NULL && !(*fdflags(p, i) & UF_RESERVED)) {
608*43a90889SApple OSS Distributions 			struct dirent d;
609*43a90889SApple OSS Distributions 			struct dirent *dp = &d;
610*43a90889SApple OSS Distributions 
611*43a90889SApple OSS Distributions 			bzero((caddr_t) dp, UIO_MX);
612*43a90889SApple OSS Distributions 
613*43a90889SApple OSS Distributions 			dp->d_namlen = (__uint8_t)scnprintf(dp->d_name, sizeof(dp->d_name),
614*43a90889SApple OSS Distributions 			    "%lld", i);
615*43a90889SApple OSS Distributions 			dp->d_reclen = UIO_MX;
616*43a90889SApple OSS Distributions 			dp->d_type = DT_UNKNOWN;
617*43a90889SApple OSS Distributions 			dp->d_fileno = (ino_t)i + FD_STDIN;
618*43a90889SApple OSS Distributions 			/*
619*43a90889SApple OSS Distributions 			 * And ship to userland
620*43a90889SApple OSS Distributions 			 */
621*43a90889SApple OSS Distributions 			error = uiomove((caddr_t) dp, UIO_MX, uio);
622*43a90889SApple OSS Distributions 			if (error) {
623*43a90889SApple OSS Distributions 				break;
624*43a90889SApple OSS Distributions 			}
625*43a90889SApple OSS Distributions 		}
626*43a90889SApple OSS Distributions 		i++;
627*43a90889SApple OSS Distributions 	}
628*43a90889SApple OSS Distributions 
629*43a90889SApple OSS Distributions 	uio->uio_offset = i * UIO_MX;
630*43a90889SApple OSS Distributions 	return error;
631*43a90889SApple OSS Distributions }
632*43a90889SApple OSS Distributions 
633*43a90889SApple OSS Distributions int
fdesc_read(__unused struct vnop_read_args * ap)634*43a90889SApple OSS Distributions fdesc_read(__unused struct vnop_read_args *ap)
635*43a90889SApple OSS Distributions {
636*43a90889SApple OSS Distributions 	return ENOTSUP;
637*43a90889SApple OSS Distributions }
638*43a90889SApple OSS Distributions 
639*43a90889SApple OSS Distributions int
fdesc_write(__unused struct vnop_write_args * ap)640*43a90889SApple OSS Distributions fdesc_write(__unused struct vnop_write_args *ap)
641*43a90889SApple OSS Distributions {
642*43a90889SApple OSS Distributions 	return ENOTSUP;
643*43a90889SApple OSS Distributions }
644*43a90889SApple OSS Distributions 
645*43a90889SApple OSS Distributions int
fdesc_ioctl(__unused struct vnop_ioctl_args * ap)646*43a90889SApple OSS Distributions fdesc_ioctl(__unused struct vnop_ioctl_args *ap)
647*43a90889SApple OSS Distributions {
648*43a90889SApple OSS Distributions 	return ENOTSUP;
649*43a90889SApple OSS Distributions }
650*43a90889SApple OSS Distributions 
651*43a90889SApple OSS Distributions int
fdesc_select(__unused struct vnop_select_args * ap)652*43a90889SApple OSS Distributions fdesc_select(__unused struct vnop_select_args *ap)
653*43a90889SApple OSS Distributions {
654*43a90889SApple OSS Distributions 	return ENOTSUP;
655*43a90889SApple OSS Distributions }
656*43a90889SApple OSS Distributions 
657*43a90889SApple OSS Distributions int
fdesc_inactive(struct vnop_inactive_args * ap)658*43a90889SApple OSS Distributions fdesc_inactive(struct vnop_inactive_args *ap)
659*43a90889SApple OSS Distributions {
660*43a90889SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
661*43a90889SApple OSS Distributions 
662*43a90889SApple OSS Distributions 	/*
663*43a90889SApple OSS Distributions 	 * Clear out the v_type field to avoid
664*43a90889SApple OSS Distributions 	 * nasty things happening in vgone().
665*43a90889SApple OSS Distributions 	 */
666*43a90889SApple OSS Distributions 	vp->v_type = VNON;
667*43a90889SApple OSS Distributions 
668*43a90889SApple OSS Distributions 	return 0;
669*43a90889SApple OSS Distributions }
670*43a90889SApple OSS Distributions 
671*43a90889SApple OSS Distributions int
fdesc_reclaim(struct vnop_reclaim_args * ap)672*43a90889SApple OSS Distributions fdesc_reclaim(struct vnop_reclaim_args *ap)
673*43a90889SApple OSS Distributions {
674*43a90889SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
675*43a90889SApple OSS Distributions 	struct fdescnode *fd = VTOFDESC(vp);
676*43a90889SApple OSS Distributions 
677*43a90889SApple OSS Distributions 	fdesc_lock();
678*43a90889SApple OSS Distributions 
679*43a90889SApple OSS Distributions 	LIST_REMOVE(fd, fd_hash);
680*43a90889SApple OSS Distributions 	kfree_type(struct fdescnode, vp->v_data);
681*43a90889SApple OSS Distributions 
682*43a90889SApple OSS Distributions 	fdesc_unlock();
683*43a90889SApple OSS Distributions 
684*43a90889SApple OSS Distributions 	return 0;
685*43a90889SApple OSS Distributions }
686*43a90889SApple OSS Distributions 
687*43a90889SApple OSS Distributions /*
688*43a90889SApple OSS Distributions  * Return POSIX pathconf information applicable to special devices.
689*43a90889SApple OSS Distributions  */
690*43a90889SApple OSS Distributions int
fdesc_pathconf(struct vnop_pathconf_args * ap)691*43a90889SApple OSS Distributions fdesc_pathconf(struct vnop_pathconf_args *ap)
692*43a90889SApple OSS Distributions {
693*43a90889SApple OSS Distributions 	switch (ap->a_name) {
694*43a90889SApple OSS Distributions 	case _PC_LINK_MAX:
695*43a90889SApple OSS Distributions 		*ap->a_retval = LINK_MAX;
696*43a90889SApple OSS Distributions 		return 0;
697*43a90889SApple OSS Distributions 	case _PC_MAX_CANON:
698*43a90889SApple OSS Distributions 		*ap->a_retval = MAX_CANON;
699*43a90889SApple OSS Distributions 		return 0;
700*43a90889SApple OSS Distributions 	case _PC_MAX_INPUT:
701*43a90889SApple OSS Distributions 		*ap->a_retval = MAX_INPUT;
702*43a90889SApple OSS Distributions 		return 0;
703*43a90889SApple OSS Distributions 	case _PC_PIPE_BUF:
704*43a90889SApple OSS Distributions 		*ap->a_retval = PIPE_BUF;
705*43a90889SApple OSS Distributions 		return 0;
706*43a90889SApple OSS Distributions 	case _PC_CHOWN_RESTRICTED:
707*43a90889SApple OSS Distributions 		*ap->a_retval = 200112;         /* _POSIX_CHOWN_RESTRICTED */
708*43a90889SApple OSS Distributions 		return 0;
709*43a90889SApple OSS Distributions 	case _PC_VDISABLE:
710*43a90889SApple OSS Distributions 		*ap->a_retval = _POSIX_VDISABLE;
711*43a90889SApple OSS Distributions 		return 0;
712*43a90889SApple OSS Distributions 	default:
713*43a90889SApple OSS Distributions 		return EINVAL;
714*43a90889SApple OSS Distributions 	}
715*43a90889SApple OSS Distributions 	/* NOTREACHED */
716*43a90889SApple OSS Distributions }
717*43a90889SApple OSS Distributions 
718*43a90889SApple OSS Distributions /*
719*43a90889SApple OSS Distributions  * /dev/fd "should never get here" operation
720*43a90889SApple OSS Distributions  */
721*43a90889SApple OSS Distributions int
fdesc_badop(void)722*43a90889SApple OSS Distributions fdesc_badop(void)
723*43a90889SApple OSS Distributions {
724*43a90889SApple OSS Distributions 	return ENOTSUP;
725*43a90889SApple OSS Distributions 	/* NOTREACHED */
726*43a90889SApple OSS Distributions }
727*43a90889SApple OSS Distributions 
728*43a90889SApple OSS Distributions #define VOPFUNC int (*)(void *)
729*43a90889SApple OSS Distributions 
730*43a90889SApple OSS Distributions #define fdesc_revoke nop_revoke
731*43a90889SApple OSS Distributions #define fdesc_strategy (void (*)(void))fdesc_badop
732*43a90889SApple OSS Distributions 
733*43a90889SApple OSS Distributions #define fdesc_nullop (void (*)(void ))nullop
734*43a90889SApple OSS Distributions #define fdesc_error (void (*)(void ))vn_default_error
735*43a90889SApple OSS Distributions #define fdesc_notsupp (void (*)(void ))eopnotsupp
736*43a90889SApple OSS Distributions 
737*43a90889SApple OSS Distributions int(**fdesc_vnodeop_p)(void *);
738*43a90889SApple OSS Distributions const struct vnodeopv_entry_desc devfs_fdesc_vnodeop_entries[] = {
739*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)fdesc_error },
740*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)fdesc_error},        /* lookup */
741*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },   /* create */
742*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },     /* mknod */
743*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)fdesc_open },       /* open */
744*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)fdesc_nullop },     /* close */
745*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)fdesc_nullop },   /* access */
746*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)fdesc_getattr }, /* getattr */
747*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)fdesc_setattr }, /* setattr */
748*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)fdesc_read },       /* read */
749*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)fdesc_write },     /* write */
750*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)fdesc_ioctl },     /* ioctl */
751*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)fdesc_select },   /* select */
752*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)fdesc_revoke },   /* revoke */
753*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* mmap */
754*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)fdesc_nullop },     /* fsync */
755*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },   /* remove */
756*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* link */
757*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_rename_desc, .opve_impl =  (VOPFUNC)fdesc_notsupp },   /* rename */
758*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },     /* mkdir */
759*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },     /* rmdir */
760*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)fdesc_notsupp }, /* symlink */
761*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)fdesc_error},/* readdir */
762*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink}, /* readlink */
763*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)fdesc_inactive },/* inactive */
764*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)fdesc_reclaim }, /* reclaim */
765*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)fdesc_strategy },       /* strategy */
766*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)fdesc_pathconf },       /* pathconf */
767*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)fdesc_notsupp }, /* advlock */
768*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },   /* bwrite */
769*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein },     /* pagein */
770*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout },   /* pageout */
771*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */
772*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* blktooff */
773*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* offtoblk */
774*43a90889SApple OSS Distributions 	{ .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)fdesc_notsupp },       /* blockmap */
775*43a90889SApple OSS Distributions 	{ .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (VOPFUNC)NULL }
776*43a90889SApple OSS Distributions };
777*43a90889SApple OSS Distributions 
778*43a90889SApple OSS Distributions const struct vnodeopv_desc devfs_fdesc_vnodeop_opv_desc =
779*43a90889SApple OSS Distributions { .opv_desc_vector_p = &fdesc_vnodeop_p, .opv_desc_ops = devfs_fdesc_vnodeop_entries };
780