]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/devfs/devfs_vfsops.c
2 * Copyright (c) 2000-2002 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.h>
63 #include <sys/mount.h>
64 #include <sys/malloc.h>
67 #include "devfsdefs.h"
69 static int devfs_statfs( struct mount
*mp
, struct statfs
*sbp
, struct proc
*p
);
71 static struct vfsconf
* devfs_vfsp
= 0;
72 static int kernel_mount
= 0;
76 * Called from the generic VFS startups.
77 * This is the second stage of DEVFS initialisation.
78 * The probed devices have already been loaded and the
79 * basic structure of the DEVFS created.
80 * We take the oportunity to mount the hidden DEVFS layer, so that
81 * devices from devfs get sync'd.
84 devfs_init(struct vfsconf
*vfsp
)
86 devfs_vfsp
= vfsp
; /* remember this for devfs_kernel_mount below */
90 devfs_make_node(makedev(0, 0), DEVFS_CHAR
,
91 UID_ROOT
, GID_WHEEL
, 0622, "console");
92 devfs_make_node(makedev(2, 0), DEVFS_CHAR
,
93 UID_ROOT
, GID_WHEEL
, 0666, "tty");
94 devfs_make_node(makedev(3, 0), DEVFS_CHAR
,
95 UID_ROOT
, GID_KMEM
, 0640, "mem");
96 devfs_make_node(makedev(3, 1), DEVFS_CHAR
,
97 UID_ROOT
, GID_KMEM
, 0640, "kmem");
98 devfs_make_node(makedev(3, 2), DEVFS_CHAR
,
99 UID_ROOT
, GID_WHEEL
, 0666, "null");
100 devfs_make_node(makedev(3, 3), DEVFS_CHAR
,
101 UID_ROOT
, GID_WHEEL
, 0666, "zero");
102 devfs_make_node(makedev(6, 0), DEVFS_CHAR
,
103 UID_ROOT
, GID_WHEEL
, 0600, "klog");
108 * mp - pointer to 'mount' structure
109 * path - addr in user space of mount point (ie /usr or whatever)
110 * data - addr in user space of mount params including the
111 * name of the block special file to treat as a filesystem.
113 * ndp - namei data pointer (NOT USED)
115 * devfs is special in that it doesn't require any device to be mounted..
116 * It makes up its data as it goes along.
117 * it must be mounted during single user.. until it is, only std{in/out/err}
118 * and the root filesystem are available.
122 devfs_mount(struct mount
*mp
, char *path
, caddr_t data
,
123 struct nameidata
*ndp
, struct proc
*p
)
125 struct devfsmount
*devfs_mp_p
; /* devfs specific mount info */
130 * If they just want to update, we don't need to do anything.
132 if (mp
->mnt_flag
& MNT_UPDATE
)
138 * Well, it's not an update, it's a real mount request.
140 * HERE we should check to see if we are already mounted here.
143 MALLOC(devfs_mp_p
, struct devfsmount
*, sizeof(struct devfsmount
),
144 M_DEVFSMNT
, M_WAITOK
);
145 if (devfs_mp_p
== NULL
)
147 bzero(devfs_mp_p
,sizeof(*devfs_mp_p
));
148 devfs_mp_p
->mount
= mp
;
151 * Fill out some fields
153 mp
->mnt_data
= (qaddr_t
)devfs_mp_p
;
154 mp
->mnt_stat
.f_type
= mp
->mnt_vfc
->vfc_typenum
;
155 mp
->mnt_stat
.f_fsid
.val
[0] = (int32_t)(void *)devfs_mp_p
;
156 mp
->mnt_stat
.f_fsid
.val
[1] = mp
->mnt_stat
.f_type
;
157 mp
->mnt_flag
|= MNT_LOCAL
;
160 error
= dev_dup_plane(devfs_mp_p
);
163 mp
->mnt_data
= (qaddr_t
)0;
164 FREE((caddr_t
)devfs_mp_p
, M_DEVFSMNT
);
169 * Copy in the name of the directory the filesystem
170 * is to be mounted on.
171 * And we clear the remainder of the character strings
176 copyinstr(path
, (caddr_t
)mp
->mnt_stat
.f_mntonname
,
177 sizeof(mp
->mnt_stat
.f_mntonname
)-1, &size
);
178 bzero(mp
->mnt_stat
.f_mntonname
+ size
,
179 sizeof(mp
->mnt_stat
.f_mntonname
) - size
);
181 bzero(mp
->mnt_stat
.f_mntfromname
, MNAMELEN
);
182 bcopy("devfs",mp
->mnt_stat
.f_mntfromname
, 5);
184 (void)devfs_statfs(mp
, &mp
->mnt_stat
, p
);
190 devfs_start(struct mount
*mp
, int flags
, struct proc
*p
)
196 * Unmount the filesystem described by mp.
199 devfs_unmount( struct mount
*mp
, int mntflags
, struct proc
*p
)
201 struct devfsmount
*devfs_mp_p
= (struct devfsmount
*)mp
->mnt_data
;
206 if (mntflags
& MNT_FORCE
) {
210 error
= vflush(mp
, NULLVP
, flags
);
215 devfs_free_plane(devfs_mp_p
);
217 FREE((caddr_t
)devfs_mp_p
, M_DEVFSMNT
);
219 mp
->mnt_data
= (qaddr_t
)0;
220 mp
->mnt_flag
&= ~MNT_LOCAL
;
225 /* return the address of the root vnode in *vpp */
227 devfs_root(struct mount
*mp
, struct vnode
**vpp
)
229 struct devfsmount
*devfs_mp_p
= (struct devfsmount
*)(mp
->mnt_data
);
232 error
= devfs_dntovn(devfs_mp_p
->plane_root
->de_dnp
,vpp
,
238 devfs_quotactl(struct mount
*mp
, int cmds
, uid_t uid
, caddr_t arg
,
245 devfs_statfs( struct mount
*mp
, struct statfs
*sbp
, struct proc
*p
)
247 struct devfsmount
*devfs_mp_p
= (struct devfsmount
*)mp
->mnt_data
;
250 * Fill in the stat block.
252 sbp
->f_type
= mp
->mnt_stat
.f_type
;
253 sbp
->f_flags
= 0; /* XXX */
256 sbp
->f_blocks
= (devfs_stats
.mounts
* sizeof(struct devfsmount
)
257 + devfs_stats
.nodes
* sizeof(devnode_t
)
258 + devfs_stats
.entries
* sizeof(devdirent_t
)
259 + devfs_stats
.stringspace
263 sbp
->f_files
= devfs_stats
.nodes
;
265 sbp
->f_fsid
.val
[0] = (int32_t)(void *)devfs_mp_p
;
266 sbp
->f_fsid
.val
[1] = mp
->mnt_stat
.f_type
;
269 * Copy the mounted on and mounted from names into
270 * the passed in stat block, if it is not the one
271 * in the mount structure.
273 if (sbp
!= &mp
->mnt_stat
) {
274 bcopy((caddr_t
)mp
->mnt_stat
.f_mntonname
,
275 (caddr_t
)&sbp
->f_mntonname
[0], MNAMELEN
);
276 bcopy((caddr_t
)mp
->mnt_stat
.f_mntfromname
,
277 (caddr_t
)&sbp
->f_mntfromname
[0], MNAMELEN
);
283 devfs_sync(struct mount
*mp
, int waitfor
,struct ucred
*cred
,struct proc
*p
)
290 devfs_vget(struct mount
*mp
, void * ino
,struct vnode
**vpp
)
295 /*************************************************************
296 * The concept of exporting a kernel generated devfs is stupid
297 * So don't handle filehandles
301 devfs_fhtovp (struct mount
*mp
, struct fid
*fhp
, struct mbuf
*nam
,
302 struct vnode
**vpp
, int *exflagsp
, struct ucred
**credanonp
)
309 devfs_vptofh (struct vnode
*vp
, struct fid
*fhp
)
315 devfs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
327 #include <sys/namei.h>
330 * Function: devfs_kernel_mount
332 * Mount devfs at the given mount point from within the kernel.
335 devfs_kernel_mount(char * mntname
)
343 if (devfs_vfsp
== NULL
) {
344 printf("devfs_kernel_mount: devfs_vfsp is NULL\n");
347 procp
= current_proc();
350 * Get vnode to be covered
352 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE
,
354 if ((error
= namei(&nd
))) {
355 printf("devfs_kernel_mount: failed to find directory '%s', %d",
360 if ((error
= vinvalbuf(vp
, V_SAVE
, procp
->p_ucred
, procp
, 0, 0))) {
361 printf("devfs_kernel_mount: vinval failed: %d\n", error
);
365 if (vp
->v_type
!= VDIR
) {
366 printf("devfs_kernel_mount: '%s' is not a directory\n", mntname
);
370 if (vp
->v_mountedhere
!= NULL
) {
376 * Allocate and initialize the filesystem.
378 MALLOC_ZONE(mp
, struct mount
*, (u_long
)sizeof(struct mount
),
380 bzero((char *)mp
, (u_long
)sizeof(struct mount
));
382 /* Initialize the default IO constraints */
383 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
384 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
386 lockinit(&mp
->mnt_lock
, PVFS
, "vfslock", 0, 0);
387 (void)vfs_busy(mp
, LK_NOWAIT
, 0, procp
);
388 LIST_INIT(&mp
->mnt_vnodelist
);
389 mp
->mnt_op
= devfs_vfsp
->vfc_vfsops
;
390 mp
->mnt_vfc
= devfs_vfsp
;
391 devfs_vfsp
->vfc_refcount
++;
393 mp
->mnt_flag
|= devfs_vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
394 strncpy(mp
->mnt_stat
.f_fstypename
, devfs_vfsp
->vfc_name
, MFSNAMELEN
);
395 vp
->v_mountedhere
= mp
;
396 mp
->mnt_vnodecovered
= vp
;
397 mp
->mnt_stat
.f_owner
= procp
->p_ucred
->cr_uid
;
398 (void) copystr(mntname
, mp
->mnt_stat
.f_mntonname
, MNAMELEN
- 1, 0);
401 error
= devfs_mount(mp
, mntname
, NULL
, NULL
, procp
);
404 printf("devfs_kernel_mount: mount %s failed: %d", mntname
, error
);
405 mp
->mnt_vfc
->vfc_refcount
--;
407 if (mp
->mnt_kern_flag
& MNTK_IO_XINFO
)
408 FREE(mp
->mnt_xinfo_ptr
, M_TEMP
);
409 vfs_unbusy(mp
, procp
);
411 FREE_ZONE(mp
, sizeof (struct mount
), M_MOUNT
);
415 simple_lock(&mountlist_slock
);
416 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
417 simple_unlock(&mountlist_slock
);
418 VOP_UNLOCK(vp
, 0, procp
);
419 vfs_unbusy(mp
, procp
);
423 struct vfsops devfs_vfsops
= {