2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1997,1998 Julian Elischer. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright notice,
32 * this list of conditions and the following disclaimer in the documentation
33 * and/or other materials provided with the distribution.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
36 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
39 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
53 * - modified devfs_statfs() to use devfs_stats to calculate the
54 * amount of memory used by devfs
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/vnode_internal.h>
63 #include <sys/kauth.h>
64 #include <sys/mount_internal.h>
65 #include <sys/malloc.h>
68 #include "devfsdefs.h"
70 static int devfs_statfs( struct mount
*mp
, struct vfsstatfs
*sbp
, vfs_context_t context
);
71 static int devfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
);
73 static struct vfstable
* devfs_vfsp
= 0;
74 extern int setup_kmem
;
78 * Called from the generic VFS startups.
79 * This is the second stage of DEVFS initialisation.
80 * The probed devices have already been loaded and the
81 * basic structure of the DEVFS created.
82 * We take the oportunity to mount the hidden DEVFS layer, so that
83 * devices from devfs get sync'd.
86 devfs_init(struct vfsconf
*vfsp
)
88 devfs_vfsp
= (struct vfstable
*)vfsp
; /* remember this for devfs_kernel_mount below */
92 devfs_make_node(makedev(0, 0), DEVFS_CHAR
,
93 UID_ROOT
, GID_WHEEL
, 0622, "console");
94 devfs_make_node(makedev(2, 0), DEVFS_CHAR
,
95 UID_ROOT
, GID_WHEEL
, 0666, "tty");
99 devfs_make_node(makedev(3, 2), DEVFS_CHAR
,
100 UID_ROOT
, GID_WHEEL
, 0666, "null");
101 devfs_make_node(makedev(3, 3), DEVFS_CHAR
,
102 UID_ROOT
, GID_WHEEL
, 0666, "zero");
103 devfs_make_node(makedev(6, 0), DEVFS_CHAR
,
104 UID_ROOT
, GID_WHEEL
, 0600, "klog");
108 __private_extern__
void
109 devfs_setup_kmem(void)
111 devfs_make_node(makedev(3, 0), DEVFS_CHAR
,
112 UID_ROOT
, GID_KMEM
, 0640, "mem");
113 devfs_make_node(makedev(3, 1), DEVFS_CHAR
,
114 UID_ROOT
, GID_KMEM
, 0640, "kmem");
119 * mp - pointer to 'mount' structure
120 * path - addr in user space of mount point (ie /usr or whatever)
121 * data - addr in user space of mount params including the
122 * name of the block special file to treat as a filesystem.
124 * ndp - namei data pointer (NOT USED)
126 * devfs is special in that it doesn't require any device to be mounted..
127 * It makes up its data as it goes along.
128 * it must be mounted during single user.. until it is, only std{in/out/err}
129 * and the root filesystem are available.
133 devfs_mount(struct mount
*mp
, __unused vnode_t devvp
, __unused user_addr_t data
, vfs_context_t context
)
135 struct devfsmount
*devfs_mp_p
; /* devfs specific mount info */
139 * If they just want to update, we don't need to do anything.
141 if (mp
->mnt_flag
& MNT_UPDATE
)
146 /* Advisory locking should be handled at the VFS layer */
147 vfs_setlocklocal(mp
);
150 * Well, it's not an update, it's a real mount request.
152 * HERE we should check to see if we are already mounted here.
155 MALLOC(devfs_mp_p
, struct devfsmount
*, sizeof(struct devfsmount
),
156 M_DEVFSMNT
, M_WAITOK
);
157 if (devfs_mp_p
== NULL
)
159 bzero(devfs_mp_p
,sizeof(*devfs_mp_p
));
160 devfs_mp_p
->mount
= mp
;
163 * Fill out some fields
165 mp
->mnt_data
= (qaddr_t
)devfs_mp_p
;
166 mp
->mnt_vfsstat
.f_fsid
.val
[0] = (int32_t)(void *)devfs_mp_p
;
167 mp
->mnt_vfsstat
.f_fsid
.val
[1] = vfs_typenum(mp
);
168 mp
->mnt_flag
|= MNT_LOCAL
;
171 error
= dev_dup_plane(devfs_mp_p
);
175 mp
->mnt_data
= (qaddr_t
)0;
176 FREE((caddr_t
)devfs_mp_p
, M_DEVFSMNT
);
182 * Copy in the name of the directory the filesystem
183 * is to be mounted on.
184 * And we clear the remainder of the character strings
188 bzero(mp
->mnt_vfsstat
.f_mntfromname
, MAXPATHLEN
);
189 bcopy("devfs",mp
->mnt_vfsstat
.f_mntfromname
, 5);
190 (void)devfs_statfs(mp
, &mp
->mnt_vfsstat
, context
);
197 devfs_start(__unused
struct mount
*mp
, __unused
int flags
, __unused vfs_context_t context
)
203 * Unmount the filesystem described by mp.
206 devfs_unmount( struct mount
*mp
, int mntflags
, __unused vfs_context_t context
)
208 struct devfsmount
*devfs_mp_p
= (struct devfsmount
*)mp
->mnt_data
;
213 if (mntflags
& MNT_FORCE
) {
217 error
= vflush(mp
, NULLVP
, flags
);
222 devfs_free_plane(devfs_mp_p
);
227 FREE((caddr_t
)devfs_mp_p
, M_DEVFSMNT
);
228 mp
->mnt_data
= (qaddr_t
)0;
229 mp
->mnt_flag
&= ~MNT_LOCAL
;
234 /* return the address of the root vnode in *vpp */
236 devfs_root(struct mount
*mp
, struct vnode
**vpp
, vfs_context_t context
)
238 struct devfsmount
*devfs_mp_p
= (struct devfsmount
*)(mp
->mnt_data
);
242 error
= devfs_dntovn(devfs_mp_p
->plane_root
->de_dnp
, vpp
, context
->vc_proc
);
249 devfs_statfs( struct mount
*mp
, struct vfsstatfs
*sbp
, __unused vfs_context_t context
)
251 struct devfsmount
*devfs_mp_p
= (struct devfsmount
*)mp
->mnt_data
;
254 * Fill in the stat block.
256 //sbp->f_type = mp->mnt_vfsstat.f_type;
257 sbp
->f_flags
= 0; /* XXX */
260 sbp
->f_blocks
= (devfs_stats
.mounts
* sizeof(struct devfsmount
)
261 + devfs_stats
.nodes
* sizeof(devnode_t
)
262 + devfs_stats
.entries
* sizeof(devdirent_t
)
263 + devfs_stats
.stringspace
267 sbp
->f_files
= devfs_stats
.nodes
;
269 sbp
->f_fsid
.val
[0] = (int32_t)(void *)devfs_mp_p
;
270 sbp
->f_fsid
.val
[1] = vfs_typenum(mp
);
276 devfs_vfs_getattr(mount_t mp
, struct vfs_attr
*fsap
, vfs_context_t context
)
278 VFSATTR_RETURN(fsap
, f_objcount
, devfs_stats
.nodes
);
279 VFSATTR_RETURN(fsap
, f_maxobjcount
, devfs_stats
.nodes
);
280 VFSATTR_RETURN(fsap
, f_bsize
, 512);
281 VFSATTR_RETURN(fsap
, f_iosize
, 512);
282 if (VFSATTR_IS_ACTIVE(fsap
, f_blocks
) || VFSATTR_IS_ACTIVE(fsap
, f_bused
)) {
283 fsap
->f_blocks
= (devfs_stats
.mounts
* sizeof(struct devfsmount
)
284 + devfs_stats
.nodes
* sizeof(devnode_t
)
285 + devfs_stats
.entries
* sizeof(devdirent_t
)
286 + devfs_stats
.stringspace
288 fsap
->f_bused
= fsap
->f_blocks
;
289 VFSATTR_SET_SUPPORTED(fsap
, f_blocks
);
290 VFSATTR_SET_SUPPORTED(fsap
, f_bused
);
292 VFSATTR_RETURN(fsap
, f_bfree
, 0);
293 VFSATTR_RETURN(fsap
, f_bavail
, 0);
294 VFSATTR_RETURN(fsap
, f_files
, devfs_stats
.nodes
);
295 VFSATTR_RETURN(fsap
, f_ffree
, 0);
296 VFSATTR_RETURN(fsap
, f_fssubtype
, 0);
302 devfs_sync(__unused
struct mount
*mp
, __unused
int waitfor
, __unused vfs_context_t context
)
309 devfs_vget(__unused
struct mount
*mp
, __unused ino64_t ino
, __unused
struct vnode
**vpp
, __unused vfs_context_t context
)
314 /*************************************************************
315 * The concept of exporting a kernel generated devfs is stupid
316 * So don't handle filehandles
320 devfs_fhtovp (__unused
struct mount
*mp
, __unused
int fhlen
, __unused
unsigned char *fhp
, __unused
struct vnode
**vpp
, __unused vfs_context_t context
)
327 devfs_vptofh (__unused
struct vnode
*vp
, __unused
int *fhlenp
, __unused
unsigned char *fhp
, __unused vfs_context_t context
)
333 devfs_sysctl(__unused
int *name
, __unused u_int namelen
, __unused user_addr_t oldp
,
334 __unused
size_t *oldlenp
, __unused user_addr_t newp
,
335 __unused
size_t newlen
, __unused vfs_context_t context
)
340 #include <sys/namei.h>
343 * Function: devfs_kernel_mount
345 * Mount devfs at the given mount point from within the kernel.
348 devfs_kernel_mount(char * mntname
)
354 struct vfs_context context
;
356 if (devfs_vfsp
== NULL
) {
357 printf("devfs_kernel_mount: devfs_vfsp is NULL\n");
360 context
.vc_proc
= current_proc();
361 context
.vc_ucred
= kauth_cred_get();
364 * Get vnode to be covered
366 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
,
367 CAST_USER_ADDR_T(mntname
), &context
);
368 if ((error
= namei(&nd
))) {
369 printf("devfs_kernel_mount: failed to find directory '%s', %d",
376 if ((error
= VNOP_FSYNC(vp
, MNT_WAIT
, &context
))) {
377 printf("devfs_kernel_mount: vnop_fsync failed: %d\n", error
);
381 if ((error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0))) {
382 printf("devfs_kernel_mount: buf_invalidateblks failed: %d\n", error
);
386 if (vnode_isdir(vp
) == 0) {
387 printf("devfs_kernel_mount: '%s' is not a directory\n", mntname
);
391 if ((vnode_mountedhere(vp
))) {
397 * Allocate and initialize the filesystem.
399 MALLOC_ZONE(mp
, struct mount
*, (u_long
)sizeof(struct mount
),
401 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
403 /* Initialize the default IO constraints */
404 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
405 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
408 TAILQ_INIT(&mp
->mnt_vnodelist
);
409 TAILQ_INIT(&mp
->mnt_workerqueue
);
410 TAILQ_INIT(&mp
->mnt_newvnodes
);
412 (void)vfs_busy(mp
, LK_NOWAIT
);
413 mp
->mnt_op
= devfs_vfsp
->vfc_vfsops
;
414 mp
->mnt_vtable
= devfs_vfsp
;
415 devfs_vfsp
->vfc_refcount
++;
416 devfs_vfsp
->vfc_threadsafe
= TRUE
;
417 devfs_vfsp
->vfc_64bitready
= TRUE
;
419 mp
->mnt_flag
|= devfs_vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
420 strncpy(mp
->mnt_vfsstat
.f_fstypename
, devfs_vfsp
->vfc_name
, MFSTYPENAMELEN
);
421 vp
->v_mountedhere
= mp
;
422 mp
->mnt_vnodecovered
= vp
;
423 mp
->mnt_vfsstat
.f_owner
= kauth_cred_getuid(kauth_cred_get());
424 (void) copystr(mntname
, mp
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
- 1, 0);
426 error
= devfs_mount(mp
, NULL
, NULL
, &context
);
429 printf("devfs_kernel_mount: mount %s failed: %d", mntname
, error
);
430 mp
->mnt_vtable
->vfc_refcount
--;
434 mount_lock_destroy(mp
);
435 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
446 struct vfsops devfs_vfsops
= {