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