xref: /xnu-11417.121.6/bsd/miscfs/devfs/devfs_vfsops.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions /*
2*a1e26a70SApple OSS Distributions  * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3*a1e26a70SApple OSS Distributions  *
4*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*a1e26a70SApple OSS Distributions  *
6*a1e26a70SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a1e26a70SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a1e26a70SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a1e26a70SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*a1e26a70SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*a1e26a70SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*a1e26a70SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*a1e26a70SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*a1e26a70SApple OSS Distributions  *
15*a1e26a70SApple OSS Distributions  * Please obtain a copy of the License at
16*a1e26a70SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*a1e26a70SApple OSS Distributions  *
18*a1e26a70SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*a1e26a70SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*a1e26a70SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*a1e26a70SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*a1e26a70SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*a1e26a70SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*a1e26a70SApple OSS Distributions  * limitations under the License.
25*a1e26a70SApple OSS Distributions  *
26*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*a1e26a70SApple OSS Distributions  */
28*a1e26a70SApple OSS Distributions /*-
29*a1e26a70SApple OSS Distributions  * Copyright 1997,1998 Julian Elischer.  All rights reserved.
30*a1e26a70SApple OSS Distributions  * [email protected]
31*a1e26a70SApple OSS Distributions  *
32*a1e26a70SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*a1e26a70SApple OSS Distributions  * modification, are permitted provided that the following conditions are
34*a1e26a70SApple OSS Distributions  * met:
35*a1e26a70SApple OSS Distributions  *  1. Redistributions of source code must retain the above copyright
36*a1e26a70SApple OSS Distributions  *     notice, this list of conditions and the following disclaimer.
37*a1e26a70SApple OSS Distributions  *  2. Redistributions in binary form must reproduce the above copyright notice,
38*a1e26a70SApple OSS Distributions  *     this list of conditions and the following disclaimer in the documentation
39*a1e26a70SApple OSS Distributions  *     and/or other materials provided with the distribution.
40*a1e26a70SApple OSS Distributions  *
41*a1e26a70SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
42*a1e26a70SApple OSS Distributions  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43*a1e26a70SApple OSS Distributions  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44*a1e26a70SApple OSS Distributions  * DISCLAIMED.  IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
45*a1e26a70SApple OSS Distributions  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*a1e26a70SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47*a1e26a70SApple OSS Distributions  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48*a1e26a70SApple OSS Distributions  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*a1e26a70SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*a1e26a70SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*a1e26a70SApple OSS Distributions  * SUCH DAMAGE.
52*a1e26a70SApple OSS Distributions  *
53*a1e26a70SApple OSS Distributions  * devfs_vfsops.c
54*a1e26a70SApple OSS Distributions  *
55*a1e26a70SApple OSS Distributions  */
56*a1e26a70SApple OSS Distributions /*
57*a1e26a70SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
58*a1e26a70SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
59*a1e26a70SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
60*a1e26a70SApple OSS Distributions  * Version 2.0.
61*a1e26a70SApple OSS Distributions  */
62*a1e26a70SApple OSS Distributions /*
63*a1e26a70SApple OSS Distributions  * HISTORY
64*a1e26a70SApple OSS Distributions  *  Dieter Siegmund ([email protected]) Wed Jul 14 13:37:59 PDT 1999
65*a1e26a70SApple OSS Distributions  *  - modified devfs_statfs() to use devfs_stats to calculate the
66*a1e26a70SApple OSS Distributions  *    amount of memory used by devfs
67*a1e26a70SApple OSS Distributions  */
68*a1e26a70SApple OSS Distributions 
69*a1e26a70SApple OSS Distributions 
70*a1e26a70SApple OSS Distributions #include <sys/param.h>
71*a1e26a70SApple OSS Distributions #include <sys/systm.h>
72*a1e26a70SApple OSS Distributions #include <sys/kernel.h>
73*a1e26a70SApple OSS Distributions #include <sys/vnode_internal.h>
74*a1e26a70SApple OSS Distributions #include <sys/proc.h>
75*a1e26a70SApple OSS Distributions #include <sys/kauth.h>
76*a1e26a70SApple OSS Distributions #include <sys/mount_internal.h>
77*a1e26a70SApple OSS Distributions #include <sys/malloc.h>
78*a1e26a70SApple OSS Distributions #include <sys/conf.h>
79*a1e26a70SApple OSS Distributions #include <libkern/OSAtomic.h>
80*a1e26a70SApple OSS Distributions #include <atm/atm_internal.h>
81*a1e26a70SApple OSS Distributions 
82*a1e26a70SApple OSS Distributions #if CONFIG_MACF
83*a1e26a70SApple OSS Distributions #include <security/mac_framework.h>
84*a1e26a70SApple OSS Distributions #endif
85*a1e26a70SApple OSS Distributions 
86*a1e26a70SApple OSS Distributions #include "devfs.h"
87*a1e26a70SApple OSS Distributions #include "devfsdefs.h"
88*a1e26a70SApple OSS Distributions 
89*a1e26a70SApple OSS Distributions #if FDESC
90*a1e26a70SApple OSS Distributions #include "fdesc.h"
91*a1e26a70SApple OSS Distributions #endif /* FDESC */
92*a1e26a70SApple OSS Distributions 
93*a1e26a70SApple OSS Distributions 
94*a1e26a70SApple OSS Distributions static int devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, vfs_context_t ctx);
95*a1e26a70SApple OSS Distributions static int devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t ctx);
96*a1e26a70SApple OSS Distributions 
97*a1e26a70SApple OSS Distributions #if CONFIG_DEV_KMEM
98*a1e26a70SApple OSS Distributions extern boolean_t dev_kmem_enabled;
99*a1e26a70SApple OSS Distributions #endif
100*a1e26a70SApple OSS Distributions 
101*a1e26a70SApple OSS Distributions /*-
102*a1e26a70SApple OSS Distributions  * Called from the generic VFS startups.
103*a1e26a70SApple OSS Distributions  * This is the second stage of DEVFS initialisation.
104*a1e26a70SApple OSS Distributions  * The probed devices have already been loaded and the
105*a1e26a70SApple OSS Distributions  * basic structure of the DEVFS created.
106*a1e26a70SApple OSS Distributions  * We take the oportunity to mount the hidden DEVFS layer, so that
107*a1e26a70SApple OSS Distributions  * devices from devfs get sync'd.
108*a1e26a70SApple OSS Distributions  */
109*a1e26a70SApple OSS Distributions static int
devfs_init(__unused struct vfsconf * vfsp)110*a1e26a70SApple OSS Distributions devfs_init(__unused struct vfsconf *vfsp)
111*a1e26a70SApple OSS Distributions {
112*a1e26a70SApple OSS Distributions 	if (devfs_sinit()) {
113*a1e26a70SApple OSS Distributions 		return ENOTSUP;
114*a1e26a70SApple OSS Distributions 	}
115*a1e26a70SApple OSS Distributions 	devfs_make_node(makedev(0, 0), DEVFS_CHAR,
116*a1e26a70SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0622, "console");
117*a1e26a70SApple OSS Distributions 	devfs_make_node(makedev(2, 0), DEVFS_CHAR,
118*a1e26a70SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0666, "tty");
119*a1e26a70SApple OSS Distributions #if CONFIG_DEV_KMEM
120*a1e26a70SApple OSS Distributions 	if (dev_kmem_enabled) {
121*a1e26a70SApple OSS Distributions 		/* (3,0) reserved for /dev/mem physical memory */
122*a1e26a70SApple OSS Distributions 		devfs_make_node(makedev(3, 1), DEVFS_CHAR,
123*a1e26a70SApple OSS Distributions 		    UID_ROOT, GID_KMEM, 0640, "kmem");
124*a1e26a70SApple OSS Distributions 	}
125*a1e26a70SApple OSS Distributions #endif
126*a1e26a70SApple OSS Distributions 	devfs_make_node(makedev(3, 2), DEVFS_CHAR,
127*a1e26a70SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0666, "null");
128*a1e26a70SApple OSS Distributions 	devfs_make_node(makedev(3, 3), DEVFS_CHAR,
129*a1e26a70SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0666, "zero");
130*a1e26a70SApple OSS Distributions 	uint32_t logging_config = atm_get_diagnostic_config();
131*a1e26a70SApple OSS Distributions 
132*a1e26a70SApple OSS Distributions 	devfs_make_node(makedev(6, 0), DEVFS_CHAR,
133*a1e26a70SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0600, "klog");
134*a1e26a70SApple OSS Distributions 
135*a1e26a70SApple OSS Distributions 	if (!(logging_config & ATM_TRACE_DISABLE)) {
136*a1e26a70SApple OSS Distributions 		devfs_make_node(makedev(7, 0), DEVFS_CHAR,
137*a1e26a70SApple OSS Distributions 		    UID_LOGD, GID_LOGD, 0600, "oslog");
138*a1e26a70SApple OSS Distributions 		if (cdevsw_setkqueueok(7, (&(cdevsw[7])), 0) == -1) {
139*a1e26a70SApple OSS Distributions 			return ENOTSUP;
140*a1e26a70SApple OSS Distributions 		}
141*a1e26a70SApple OSS Distributions 
142*a1e26a70SApple OSS Distributions 		devfs_make_node(makedev(8, 0), DEVFS_CHAR,
143*a1e26a70SApple OSS Distributions 		    UID_ROOT, GID_WHEEL, 0600, "oslog_stream");
144*a1e26a70SApple OSS Distributions 		if (cdevsw_setkqueueok(8, (&(cdevsw[8])), 0) == -1) {
145*a1e26a70SApple OSS Distributions 			return ENOTSUP;
146*a1e26a70SApple OSS Distributions 		}
147*a1e26a70SApple OSS Distributions 	}
148*a1e26a70SApple OSS Distributions 
149*a1e26a70SApple OSS Distributions 
150*a1e26a70SApple OSS Distributions #if  FDESC
151*a1e26a70SApple OSS Distributions 	devfs_fdesc_init();
152*a1e26a70SApple OSS Distributions #endif
153*a1e26a70SApple OSS Distributions 
154*a1e26a70SApple OSS Distributions 	return 0;
155*a1e26a70SApple OSS Distributions }
156*a1e26a70SApple OSS Distributions 
157*a1e26a70SApple OSS Distributions /*-
158*a1e26a70SApple OSS Distributions  *  mp	 - pointer to 'mount' structure
159*a1e26a70SApple OSS Distributions  *  path - addr in user space of mount point (ie /usr or whatever)
160*a1e26a70SApple OSS Distributions  *  data - addr in user space of mount params including the
161*a1e26a70SApple OSS Distributions  *         name of the block special file to treat as a filesystem.
162*a1e26a70SApple OSS Distributions  *         (NOT USED)
163*a1e26a70SApple OSS Distributions  *  ndp  - namei data pointer (NOT USED)
164*a1e26a70SApple OSS Distributions  *  p    - proc pointer
165*a1e26a70SApple OSS Distributions  * devfs is special in that it doesn't require any device to be mounted..
166*a1e26a70SApple OSS Distributions  * It makes up its data as it goes along.
167*a1e26a70SApple OSS Distributions  * it must be mounted during single user.. until it is, only std{in/out/err}
168*a1e26a70SApple OSS Distributions  * and the root filesystem are available.
169*a1e26a70SApple OSS Distributions  */
170*a1e26a70SApple OSS Distributions /*proto*/
171*a1e26a70SApple OSS Distributions int
devfs_mount(struct mount * mp,__unused vnode_t devvp,__unused user_addr_t data,vfs_context_t ctx)172*a1e26a70SApple OSS Distributions devfs_mount(struct mount *mp, __unused vnode_t devvp, __unused user_addr_t data, vfs_context_t ctx)
173*a1e26a70SApple OSS Distributions {
174*a1e26a70SApple OSS Distributions 	struct devfsmount *devfs_mp_p;  /* devfs specific mount info */
175*a1e26a70SApple OSS Distributions 	int error;
176*a1e26a70SApple OSS Distributions 
177*a1e26a70SApple OSS Distributions 	/*-
178*a1e26a70SApple OSS Distributions 	 *  If they just want to update, we don't need to do anything.
179*a1e26a70SApple OSS Distributions 	 */
180*a1e26a70SApple OSS Distributions 	if (mp->mnt_flag & MNT_UPDATE) {
181*a1e26a70SApple OSS Distributions 		return 0;
182*a1e26a70SApple OSS Distributions 	}
183*a1e26a70SApple OSS Distributions 
184*a1e26a70SApple OSS Distributions 	/* Advisory locking should be handled at the VFS layer */
185*a1e26a70SApple OSS Distributions 	vfs_setlocklocal(mp);
186*a1e26a70SApple OSS Distributions 
187*a1e26a70SApple OSS Distributions 	/*-
188*a1e26a70SApple OSS Distributions 	 *  Well, it's not an update, it's a real mount request.
189*a1e26a70SApple OSS Distributions 	 *  Time to get dirty.
190*a1e26a70SApple OSS Distributions 	 * HERE we should check to see if we are already mounted here.
191*a1e26a70SApple OSS Distributions 	 */
192*a1e26a70SApple OSS Distributions 
193*a1e26a70SApple OSS Distributions 	devfs_mp_p = kalloc_type(struct devfsmount,
194*a1e26a70SApple OSS Distributions 	    Z_WAITOK | Z_ZERO | Z_NOFAIL);
195*a1e26a70SApple OSS Distributions 	devfs_mp_p->mount = mp;
196*a1e26a70SApple OSS Distributions 
197*a1e26a70SApple OSS Distributions 	/*-
198*a1e26a70SApple OSS Distributions 	 *  Fill out some fields
199*a1e26a70SApple OSS Distributions 	 */
200*a1e26a70SApple OSS Distributions 	__IGNORE_WCASTALIGN(mp->mnt_data = (qaddr_t)devfs_mp_p);
201*a1e26a70SApple OSS Distributions 	mp->mnt_vfsstat.f_fsid.val[0] = (int32_t)VM_KERNEL_ADDRHASH(devfs_mp_p);
202*a1e26a70SApple OSS Distributions 	mp->mnt_vfsstat.f_fsid.val[1] = vfs_typenum(mp);
203*a1e26a70SApple OSS Distributions 	mp->mnt_flag |= MNT_LOCAL;
204*a1e26a70SApple OSS Distributions 
205*a1e26a70SApple OSS Distributions 	DEVFS_LOCK();
206*a1e26a70SApple OSS Distributions 	error = dev_dup_plane(devfs_mp_p);
207*a1e26a70SApple OSS Distributions 	DEVFS_UNLOCK();
208*a1e26a70SApple OSS Distributions 
209*a1e26a70SApple OSS Distributions 	if (error) {
210*a1e26a70SApple OSS Distributions 		mp->mnt_data = (qaddr_t)0;
211*a1e26a70SApple OSS Distributions 		kfree_type(struct devfsmount, devfs_mp_p);
212*a1e26a70SApple OSS Distributions 		return error;
213*a1e26a70SApple OSS Distributions 	} else {
214*a1e26a70SApple OSS Distributions 		DEVFS_INCR_MOUNTS();
215*a1e26a70SApple OSS Distributions 	}
216*a1e26a70SApple OSS Distributions 
217*a1e26a70SApple OSS Distributions 	/*-
218*a1e26a70SApple OSS Distributions 	 *  Copy in the name of the directory the filesystem
219*a1e26a70SApple OSS Distributions 	 *  is to be mounted on.
220*a1e26a70SApple OSS Distributions 	 *  And we clear the remainder of the character strings
221*a1e26a70SApple OSS Distributions 	 *  to be tidy.
222*a1e26a70SApple OSS Distributions 	 */
223*a1e26a70SApple OSS Distributions 
224*a1e26a70SApple OSS Distributions 	bzero(mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN);
225*a1e26a70SApple OSS Distributions 	bcopy("devfs", mp->mnt_vfsstat.f_mntfromname, 5);
226*a1e26a70SApple OSS Distributions 	(void)devfs_statfs(mp, &mp->mnt_vfsstat, ctx);
227*a1e26a70SApple OSS Distributions 
228*a1e26a70SApple OSS Distributions 	return 0;
229*a1e26a70SApple OSS Distributions }
230*a1e26a70SApple OSS Distributions 
231*a1e26a70SApple OSS Distributions 
232*a1e26a70SApple OSS Distributions static int
devfs_start(__unused struct mount * mp,__unused int flags,__unused vfs_context_t ctx)233*a1e26a70SApple OSS Distributions devfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t ctx)
234*a1e26a70SApple OSS Distributions {
235*a1e26a70SApple OSS Distributions 	return 0;
236*a1e26a70SApple OSS Distributions }
237*a1e26a70SApple OSS Distributions 
238*a1e26a70SApple OSS Distributions /*-
239*a1e26a70SApple OSS Distributions  *  Unmount the filesystem described by mp.
240*a1e26a70SApple OSS Distributions  */
241*a1e26a70SApple OSS Distributions static int
devfs_unmount(struct mount * mp,int mntflags,__unused vfs_context_t ctx)242*a1e26a70SApple OSS Distributions devfs_unmount( struct mount *mp, int mntflags, __unused vfs_context_t ctx)
243*a1e26a70SApple OSS Distributions {
244*a1e26a70SApple OSS Distributions 	struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
245*a1e26a70SApple OSS Distributions 	int flags = 0;
246*a1e26a70SApple OSS Distributions 	int force = 0;
247*a1e26a70SApple OSS Distributions 	int error;
248*a1e26a70SApple OSS Distributions 
249*a1e26a70SApple OSS Distributions 	if (mntflags & MNT_FORCE) {
250*a1e26a70SApple OSS Distributions 		flags |= FORCECLOSE;
251*a1e26a70SApple OSS Distributions 		force = 1;
252*a1e26a70SApple OSS Distributions 	}
253*a1e26a70SApple OSS Distributions 	error = vflush(mp, NULLVP, flags);
254*a1e26a70SApple OSS Distributions 	if (error && !force) {
255*a1e26a70SApple OSS Distributions 		return error;
256*a1e26a70SApple OSS Distributions 	}
257*a1e26a70SApple OSS Distributions 
258*a1e26a70SApple OSS Distributions 	DEVFS_LOCK();
259*a1e26a70SApple OSS Distributions 	devfs_free_plane(devfs_mp_p);
260*a1e26a70SApple OSS Distributions 	DEVFS_UNLOCK();
261*a1e26a70SApple OSS Distributions 
262*a1e26a70SApple OSS Distributions 	DEVFS_DECR_MOUNTS();
263*a1e26a70SApple OSS Distributions 
264*a1e26a70SApple OSS Distributions 	kfree_type(struct devfsmount, devfs_mp_p);
265*a1e26a70SApple OSS Distributions 	mp->mnt_data = (qaddr_t)0;
266*a1e26a70SApple OSS Distributions 	mp->mnt_flag &= ~MNT_LOCAL;
267*a1e26a70SApple OSS Distributions 
268*a1e26a70SApple OSS Distributions 	return 0;
269*a1e26a70SApple OSS Distributions }
270*a1e26a70SApple OSS Distributions 
271*a1e26a70SApple OSS Distributions /* return the address of the root vnode  in *vpp */
272*a1e26a70SApple OSS Distributions static int
devfs_root(struct mount * mp,struct vnode ** vpp,__unused vfs_context_t ctx)273*a1e26a70SApple OSS Distributions devfs_root(struct mount *mp, struct vnode **vpp, __unused vfs_context_t ctx)
274*a1e26a70SApple OSS Distributions {
275*a1e26a70SApple OSS Distributions 	struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data);
276*a1e26a70SApple OSS Distributions 	int error;
277*a1e26a70SApple OSS Distributions 
278*a1e26a70SApple OSS Distributions 	DEVFS_LOCK();
279*a1e26a70SApple OSS Distributions 	/* last parameter to devfs_dntovn() is ignored */
280*a1e26a70SApple OSS Distributions 	error = devfs_dntovn(devfs_mp_p->plane_root->de_dnp, vpp, NULL);
281*a1e26a70SApple OSS Distributions 	DEVFS_UNLOCK();
282*a1e26a70SApple OSS Distributions 
283*a1e26a70SApple OSS Distributions 	return error;
284*a1e26a70SApple OSS Distributions }
285*a1e26a70SApple OSS Distributions 
286*a1e26a70SApple OSS Distributions static int
devfs_statfs(struct mount * mp,struct vfsstatfs * sbp,__unused vfs_context_t ctx)287*a1e26a70SApple OSS Distributions devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, __unused vfs_context_t ctx)
288*a1e26a70SApple OSS Distributions {
289*a1e26a70SApple OSS Distributions 	struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
290*a1e26a70SApple OSS Distributions 
291*a1e26a70SApple OSS Distributions 	/*-
292*a1e26a70SApple OSS Distributions 	 *  Fill in the stat block.
293*a1e26a70SApple OSS Distributions 	 */
294*a1e26a70SApple OSS Distributions 	//sbp->f_type   = mp->mnt_vfsstat.f_type;
295*a1e26a70SApple OSS Distributions 	sbp->f_flags  = 0;              /* XXX */
296*a1e26a70SApple OSS Distributions 	sbp->f_bsize  = 512;
297*a1e26a70SApple OSS Distributions 	sbp->f_iosize = 512;
298*a1e26a70SApple OSS Distributions 	sbp->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
299*a1e26a70SApple OSS Distributions 	    + devfs_stats.nodes * sizeof(devnode_t)
300*a1e26a70SApple OSS Distributions 	    + devfs_stats.entries * sizeof(devdirent_t)
301*a1e26a70SApple OSS Distributions 	    + devfs_stats.stringspace
302*a1e26a70SApple OSS Distributions 	    ) / sbp->f_bsize;
303*a1e26a70SApple OSS Distributions 	sbp->f_bfree  = 0;
304*a1e26a70SApple OSS Distributions 	sbp->f_bavail = 0;
305*a1e26a70SApple OSS Distributions 	sbp->f_files  = devfs_stats.nodes;
306*a1e26a70SApple OSS Distributions 	sbp->f_ffree  = 0;
307*a1e26a70SApple OSS Distributions 	sbp->f_fsid.val[0] = (int32_t)VM_KERNEL_ADDRHASH(devfs_mp_p);
308*a1e26a70SApple OSS Distributions 	sbp->f_fsid.val[1] = vfs_typenum(mp);
309*a1e26a70SApple OSS Distributions 
310*a1e26a70SApple OSS Distributions 	return 0;
311*a1e26a70SApple OSS Distributions }
312*a1e26a70SApple OSS Distributions 
313*a1e26a70SApple OSS Distributions static int
devfs_vfs_getattr(__unused mount_t mp,struct vfs_attr * fsap,__unused vfs_context_t ctx)314*a1e26a70SApple OSS Distributions devfs_vfs_getattr(__unused mount_t mp, struct vfs_attr *fsap, __unused vfs_context_t ctx)
315*a1e26a70SApple OSS Distributions {
316*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_objcount, devfs_stats.nodes);
317*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_maxobjcount, devfs_stats.nodes);
318*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_bsize, 512);
319*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_iosize, 512);
320*a1e26a70SApple OSS Distributions 	if (VFSATTR_IS_ACTIVE(fsap, f_blocks) || VFSATTR_IS_ACTIVE(fsap, f_bused)) {
321*a1e26a70SApple OSS Distributions 		fsap->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
322*a1e26a70SApple OSS Distributions 		    + devfs_stats.nodes * sizeof(devnode_t)
323*a1e26a70SApple OSS Distributions 		    + devfs_stats.entries * sizeof(devdirent_t)
324*a1e26a70SApple OSS Distributions 		    + devfs_stats.stringspace
325*a1e26a70SApple OSS Distributions 		    ) / fsap->f_bsize;
326*a1e26a70SApple OSS Distributions 		fsap->f_bused = fsap->f_blocks;
327*a1e26a70SApple OSS Distributions 		VFSATTR_SET_SUPPORTED(fsap, f_blocks);
328*a1e26a70SApple OSS Distributions 		VFSATTR_SET_SUPPORTED(fsap, f_bused);
329*a1e26a70SApple OSS Distributions 	}
330*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_bfree, 0);
331*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_bavail, 0);
332*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_files, devfs_stats.nodes);
333*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_ffree, 0);
334*a1e26a70SApple OSS Distributions 	VFSATTR_RETURN(fsap, f_fssubtype, 0);
335*a1e26a70SApple OSS Distributions 
336*a1e26a70SApple OSS Distributions 	if (VFSATTR_IS_ACTIVE(fsap, f_capabilities)) {
337*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] =
338*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_SYMBOLICLINKS |
339*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_HARDLINKS |
340*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_NO_ROOT_TIMES |
341*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_CASE_SENSITIVE |
342*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_CASE_PRESERVING |
343*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_FAST_STATFS |
344*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_2TB_FILESIZE |
345*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_HIDDEN_FILES;
346*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] =
347*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_ATTRLIST;
348*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
349*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
350*a1e26a70SApple OSS Distributions 
351*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.valid[VOL_CAPABILITIES_FORMAT] =
352*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_PERSISTENTOBJECTIDS |
353*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_SYMBOLICLINKS |
354*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_HARDLINKS |
355*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_JOURNAL |
356*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_JOURNAL_ACTIVE |
357*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_NO_ROOT_TIMES |
358*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_SPARSE_FILES |
359*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_ZERO_RUNS |
360*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_CASE_SENSITIVE |
361*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_CASE_PRESERVING |
362*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_FAST_STATFS |
363*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_2TB_FILESIZE |
364*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_OPENDENYMODES |
365*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_HIDDEN_FILES |
366*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_PATH_FROM_ID |
367*a1e26a70SApple OSS Distributions 		    VOL_CAP_FMT_NO_VOLUME_SIZES;
368*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] =
369*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_SEARCHFS |
370*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_ATTRLIST |
371*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_NFSEXPORT |
372*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_READDIRATTR |
373*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_EXCHANGEDATA |
374*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_COPYFILE |
375*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_ALLOCATE |
376*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_VOL_RENAME |
377*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_ADVLOCK |
378*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_FLOCK |
379*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_EXTENDED_SECURITY |
380*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_USERACCESS |
381*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_MANLOCK |
382*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_EXTENDED_ATTR |
383*a1e26a70SApple OSS Distributions 		    VOL_CAP_INT_NAMEDSTREAMS;
384*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED1] = 0;
385*a1e26a70SApple OSS Distributions 		fsap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED2] = 0;
386*a1e26a70SApple OSS Distributions 
387*a1e26a70SApple OSS Distributions 		VFSATTR_SET_SUPPORTED(fsap, f_capabilities);
388*a1e26a70SApple OSS Distributions 	}
389*a1e26a70SApple OSS Distributions 
390*a1e26a70SApple OSS Distributions 	if (VFSATTR_IS_ACTIVE(fsap, f_attributes)) {
391*a1e26a70SApple OSS Distributions 		fsap->f_attributes.validattr.commonattr =
392*a1e26a70SApple OSS Distributions 		    ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_FSID |
393*a1e26a70SApple OSS Distributions 		    ATTR_CMN_OBJTYPE | ATTR_CMN_OBJTAG | ATTR_CMN_OBJID |
394*a1e26a70SApple OSS Distributions 		    ATTR_CMN_PAROBJID |
395*a1e26a70SApple OSS Distributions 		    ATTR_CMN_MODTIME | ATTR_CMN_CHGTIME | ATTR_CMN_ACCTIME |
396*a1e26a70SApple OSS Distributions 		    ATTR_CMN_OWNERID | ATTR_CMN_GRPID | ATTR_CMN_ACCESSMASK |
397*a1e26a70SApple OSS Distributions 		    ATTR_CMN_FLAGS | ATTR_CMN_USERACCESS | ATTR_CMN_FILEID;
398*a1e26a70SApple OSS Distributions 		fsap->f_attributes.validattr.volattr =
399*a1e26a70SApple OSS Distributions 		    ATTR_VOL_FSTYPE | ATTR_VOL_SIZE | ATTR_VOL_SPACEFREE |
400*a1e26a70SApple OSS Distributions 		    ATTR_VOL_SPACEAVAIL | ATTR_VOL_MINALLOCATION |
401*a1e26a70SApple OSS Distributions 		    ATTR_VOL_OBJCOUNT | ATTR_VOL_MAXOBJCOUNT |
402*a1e26a70SApple OSS Distributions 		    ATTR_VOL_MOUNTPOINT | ATTR_VOL_MOUNTFLAGS |
403*a1e26a70SApple OSS Distributions 		    ATTR_VOL_MOUNTEDDEVICE | ATTR_VOL_CAPABILITIES |
404*a1e26a70SApple OSS Distributions 		    ATTR_VOL_ATTRIBUTES;
405*a1e26a70SApple OSS Distributions 		fsap->f_attributes.validattr.dirattr =
406*a1e26a70SApple OSS Distributions 		    ATTR_DIR_LINKCOUNT | ATTR_DIR_MOUNTSTATUS;
407*a1e26a70SApple OSS Distributions 		fsap->f_attributes.validattr.fileattr =
408*a1e26a70SApple OSS Distributions 		    ATTR_FILE_LINKCOUNT | ATTR_FILE_TOTALSIZE |
409*a1e26a70SApple OSS Distributions 		    ATTR_FILE_IOBLOCKSIZE | ATTR_FILE_DEVTYPE |
410*a1e26a70SApple OSS Distributions 		    ATTR_FILE_DATALENGTH;
411*a1e26a70SApple OSS Distributions 		fsap->f_attributes.validattr.forkattr = 0;
412*a1e26a70SApple OSS Distributions 
413*a1e26a70SApple OSS Distributions 		fsap->f_attributes.nativeattr.commonattr =
414*a1e26a70SApple OSS Distributions 		    ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_FSID |
415*a1e26a70SApple OSS Distributions 		    ATTR_CMN_OBJTYPE | ATTR_CMN_OBJTAG | ATTR_CMN_OBJID |
416*a1e26a70SApple OSS Distributions 		    ATTR_CMN_PAROBJID |
417*a1e26a70SApple OSS Distributions 		    ATTR_CMN_MODTIME | ATTR_CMN_CHGTIME | ATTR_CMN_ACCTIME |
418*a1e26a70SApple OSS Distributions 		    ATTR_CMN_OWNERID | ATTR_CMN_GRPID | ATTR_CMN_ACCESSMASK |
419*a1e26a70SApple OSS Distributions 		    ATTR_CMN_FLAGS | ATTR_CMN_USERACCESS | ATTR_CMN_FILEID;
420*a1e26a70SApple OSS Distributions 		fsap->f_attributes.nativeattr.volattr =
421*a1e26a70SApple OSS Distributions 		    ATTR_VOL_FSTYPE | ATTR_VOL_SIZE | ATTR_VOL_SPACEFREE |
422*a1e26a70SApple OSS Distributions 		    ATTR_VOL_SPACEAVAIL | ATTR_VOL_MINALLOCATION |
423*a1e26a70SApple OSS Distributions 		    ATTR_VOL_OBJCOUNT | ATTR_VOL_MAXOBJCOUNT |
424*a1e26a70SApple OSS Distributions 		    ATTR_VOL_MOUNTPOINT | ATTR_VOL_MOUNTFLAGS |
425*a1e26a70SApple OSS Distributions 		    ATTR_VOL_MOUNTEDDEVICE | ATTR_VOL_CAPABILITIES |
426*a1e26a70SApple OSS Distributions 		    ATTR_VOL_ATTRIBUTES;
427*a1e26a70SApple OSS Distributions 		fsap->f_attributes.nativeattr.dirattr =
428*a1e26a70SApple OSS Distributions 		    ATTR_DIR_MOUNTSTATUS;
429*a1e26a70SApple OSS Distributions 		fsap->f_attributes.nativeattr.fileattr =
430*a1e26a70SApple OSS Distributions 		    ATTR_FILE_LINKCOUNT | ATTR_FILE_TOTALSIZE |
431*a1e26a70SApple OSS Distributions 		    ATTR_FILE_IOBLOCKSIZE | ATTR_FILE_DEVTYPE |
432*a1e26a70SApple OSS Distributions 		    ATTR_FILE_DATALENGTH;
433*a1e26a70SApple OSS Distributions 		fsap->f_attributes.nativeattr.forkattr = 0;
434*a1e26a70SApple OSS Distributions 
435*a1e26a70SApple OSS Distributions 		VFSATTR_SET_SUPPORTED(fsap, f_attributes);
436*a1e26a70SApple OSS Distributions 	}
437*a1e26a70SApple OSS Distributions 
438*a1e26a70SApple OSS Distributions 	return 0;
439*a1e26a70SApple OSS Distributions }
440*a1e26a70SApple OSS Distributions 
441*a1e26a70SApple OSS Distributions static int
devfs_sync(__unused struct mount * mp,__unused int waitfor,__unused vfs_context_t ctx)442*a1e26a70SApple OSS Distributions devfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t ctx)
443*a1e26a70SApple OSS Distributions {
444*a1e26a70SApple OSS Distributions 	return 0;
445*a1e26a70SApple OSS Distributions }
446*a1e26a70SApple OSS Distributions 
447*a1e26a70SApple OSS Distributions 
448*a1e26a70SApple OSS Distributions static int
devfs_vget(__unused struct mount * mp,__unused ino64_t ino,__unused struct vnode ** vpp,__unused vfs_context_t ctx)449*a1e26a70SApple OSS Distributions devfs_vget(__unused struct mount *mp, __unused ino64_t ino, __unused struct vnode **vpp, __unused vfs_context_t ctx)
450*a1e26a70SApple OSS Distributions {
451*a1e26a70SApple OSS Distributions 	return ENOTSUP;
452*a1e26a70SApple OSS Distributions }
453*a1e26a70SApple OSS Distributions 
454*a1e26a70SApple OSS Distributions /*************************************************************
455*a1e26a70SApple OSS Distributions  * The concept of exporting a kernel generated devfs is stupid
456*a1e26a70SApple OSS Distributions  * So don't handle filehandles
457*a1e26a70SApple OSS Distributions  */
458*a1e26a70SApple OSS Distributions 
459*a1e26a70SApple OSS Distributions static int
devfs_fhtovp(__unused struct mount * mp,__unused int fhlen,__unused unsigned char * fhp,__unused struct vnode ** vpp,__unused vfs_context_t ctx)460*a1e26a70SApple OSS Distributions devfs_fhtovp(__unused struct mount *mp, __unused int fhlen, __unused unsigned char *fhp, __unused struct vnode **vpp, __unused vfs_context_t ctx)
461*a1e26a70SApple OSS Distributions {
462*a1e26a70SApple OSS Distributions 	return EINVAL;
463*a1e26a70SApple OSS Distributions }
464*a1e26a70SApple OSS Distributions 
465*a1e26a70SApple OSS Distributions 
466*a1e26a70SApple OSS Distributions static int
devfs_vptofh(__unused struct vnode * vp,__unused int * fhlenp,__unused unsigned char * fhp,__unused vfs_context_t ctx)467*a1e26a70SApple OSS Distributions devfs_vptofh(__unused struct vnode *vp, __unused int *fhlenp, __unused unsigned char *fhp, __unused vfs_context_t ctx)
468*a1e26a70SApple OSS Distributions {
469*a1e26a70SApple OSS Distributions 	return EINVAL;
470*a1e26a70SApple OSS Distributions }
471*a1e26a70SApple OSS Distributions 
472*a1e26a70SApple OSS Distributions static int
devfs_sysctl(__unused int * name,__unused u_int namelen,__unused user_addr_t oldp,__unused size_t * oldlenp,__unused user_addr_t newp,__unused size_t newlen,__unused vfs_context_t ctx)473*a1e26a70SApple OSS Distributions devfs_sysctl(__unused int *name, __unused u_int namelen, __unused user_addr_t oldp,
474*a1e26a70SApple OSS Distributions     __unused size_t *oldlenp, __unused user_addr_t newp,
475*a1e26a70SApple OSS Distributions     __unused size_t newlen, __unused vfs_context_t ctx)
476*a1e26a70SApple OSS Distributions {
477*a1e26a70SApple OSS Distributions 	return ENOTSUP;
478*a1e26a70SApple OSS Distributions }
479*a1e26a70SApple OSS Distributions 
480*a1e26a70SApple OSS Distributions #include <sys/namei.h>
481*a1e26a70SApple OSS Distributions 
482*a1e26a70SApple OSS Distributions /*
483*a1e26a70SApple OSS Distributions  * Function: devfs_kernel_mount
484*a1e26a70SApple OSS Distributions  * Purpose:
485*a1e26a70SApple OSS Distributions  *   Mount devfs at the given mount point from within the kernel.
486*a1e26a70SApple OSS Distributions  */
487*a1e26a70SApple OSS Distributions int
devfs_kernel_mount(char * mntname)488*a1e26a70SApple OSS Distributions devfs_kernel_mount(char * mntname)
489*a1e26a70SApple OSS Distributions {
490*a1e26a70SApple OSS Distributions 	int error;
491*a1e26a70SApple OSS Distributions 	vfs_context_t ctx = vfs_context_kernel();
492*a1e26a70SApple OSS Distributions 	char fsname[] = "devfs";
493*a1e26a70SApple OSS Distributions 
494*a1e26a70SApple OSS Distributions 	error = kernel_mount(fsname, NULLVP, NULLVP, mntname, NULL, 0, MNT_DONTBROWSE, KERNEL_MOUNT_NOAUTH | KERNEL_MOUNT_DEVFS, ctx);
495*a1e26a70SApple OSS Distributions 	if (error) {
496*a1e26a70SApple OSS Distributions 		printf("devfs_kernel_mount: kernel_mount failed: %d\n", error);
497*a1e26a70SApple OSS Distributions 		return error;
498*a1e26a70SApple OSS Distributions 	}
499*a1e26a70SApple OSS Distributions 
500*a1e26a70SApple OSS Distributions 	return 0;
501*a1e26a70SApple OSS Distributions }
502*a1e26a70SApple OSS Distributions 
503*a1e26a70SApple OSS Distributions const struct vfsops devfs_vfsops = {
504*a1e26a70SApple OSS Distributions 	.vfs_mount   = devfs_mount,
505*a1e26a70SApple OSS Distributions 	.vfs_start   = devfs_start,
506*a1e26a70SApple OSS Distributions 	.vfs_unmount = devfs_unmount,
507*a1e26a70SApple OSS Distributions 	.vfs_root    = devfs_root,
508*a1e26a70SApple OSS Distributions 	.vfs_getattr = devfs_vfs_getattr,
509*a1e26a70SApple OSS Distributions 	.vfs_sync    = devfs_sync,
510*a1e26a70SApple OSS Distributions 	.vfs_vget    = devfs_vget,
511*a1e26a70SApple OSS Distributions 	.vfs_fhtovp  = devfs_fhtovp,
512*a1e26a70SApple OSS Distributions 	.vfs_vptofh  = devfs_vptofh,
513*a1e26a70SApple OSS Distributions 	.vfs_init    = devfs_init,
514*a1e26a70SApple OSS Distributions 	.vfs_sysctl  = devfs_sysctl,
515*a1e26a70SApple OSS Distributions 	// There are other VFS ops that we do not support
516*a1e26a70SApple OSS Distributions };
517