]> git.saurik.com Git - apple/xnu.git/blame - bsd/miscfs/devfs/devfs_vfsops.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / miscfs / devfs / devfs_vfsops.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*-
23 * Copyright 1997,1998 Julian Elischer. All rights reserved.
24 * julian@freebsd.org
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are
28 * met:
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.
34 *
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
45 * SUCH DAMAGE.
46 *
47 * devfs_vfsops.c
48 *
49 */
50/*
51 * HISTORY
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
55 */
56
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/kernel.h>
91447636 61#include <sys/vnode_internal.h>
1c79356b 62#include <sys/proc.h>
91447636
A
63#include <sys/kauth.h>
64#include <sys/mount_internal.h>
1c79356b
A
65#include <sys/malloc.h>
66
67#include "devfs.h"
68#include "devfsdefs.h"
69
91447636
A
70static int devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, vfs_context_t context);
71static int devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context);
1c79356b 72
91447636 73static struct vfstable * devfs_vfsp = 0;
c0fea474 74extern int setup_kmem;
1c79356b
A
75
76
77/*-
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.
84 */
85static int
86devfs_init(struct vfsconf *vfsp)
87{
91447636 88 devfs_vfsp = (struct vfstable *)vfsp; /* remember this for devfs_kernel_mount below */
1c79356b
A
89
90 if (devfs_sinit())
91447636 91 return (ENOTSUP);
1c79356b
A
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");
c0fea474
A
96 if (setup_kmem) {
97 devfs_setup_kmem();
98 }
1c79356b
A
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");
105 return 0;
106}
107
c0fea474
A
108__private_extern__ void
109devfs_setup_kmem(void)
110{
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");
115}
116
117
1c79356b
A
118/*-
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.
123 * (NOT USED)
124 * ndp - namei data pointer (NOT USED)
125 * p - proc pointer
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.
130 */
131/*proto*/
132int
91447636 133devfs_mount(struct mount *mp, __unused vnode_t devvp, __unused user_addr_t data, vfs_context_t context)
1c79356b
A
134{
135 struct devfsmount *devfs_mp_p; /* devfs specific mount info */
136 int error;
1c79356b
A
137
138 /*-
139 * If they just want to update, we don't need to do anything.
140 */
141 if (mp->mnt_flag & MNT_UPDATE)
142 {
143 return 0;
144 }
145
91447636
A
146 /* Advisory locking should be handled at the VFS layer */
147 vfs_setlocklocal(mp);
148
1c79356b
A
149 /*-
150 * Well, it's not an update, it's a real mount request.
151 * Time to get dirty.
152 * HERE we should check to see if we are already mounted here.
153 */
154
155 MALLOC(devfs_mp_p, struct devfsmount *, sizeof(struct devfsmount),
156 M_DEVFSMNT, M_WAITOK);
157 if (devfs_mp_p == NULL)
158 return (ENOMEM);
159 bzero(devfs_mp_p,sizeof(*devfs_mp_p));
160 devfs_mp_p->mount = mp;
161
162 /*-
163 * Fill out some fields
164 */
165 mp->mnt_data = (qaddr_t)devfs_mp_p;
91447636
A
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);
1c79356b
A
168 mp->mnt_flag |= MNT_LOCAL;
169
91447636 170 DEVFS_LOCK();
1c79356b 171 error = dev_dup_plane(devfs_mp_p);
91447636
A
172 DEVFS_UNLOCK();
173
1c79356b
A
174 if (error) {
175 mp->mnt_data = (qaddr_t)0;
176 FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
177 return (error);
91447636
A
178 } else
179 DEVFS_INCR_MOUNTS();
1c79356b
A
180
181 /*-
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
185 * to be tidy.
186 */
187
91447636
A
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);
191
1c79356b
A
192 return 0;
193}
194
195
196static int
91447636 197devfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t context)
1c79356b
A
198{
199 return 0;
200}
201
202/*-
203 * Unmount the filesystem described by mp.
204 */
205static int
91447636 206devfs_unmount( struct mount *mp, int mntflags, __unused vfs_context_t context)
1c79356b
A
207{
208 struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
209 int flags = 0;
9bccf70c 210 int force = 0;
1c79356b
A
211 int error;
212
213 if (mntflags & MNT_FORCE) {
214 flags |= FORCECLOSE;
9bccf70c 215 force = 1;
1c79356b
A
216 }
217 error = vflush(mp, NULLVP, flags);
9bccf70c 218 if (error && !force)
1c79356b
A
219 return error;
220
91447636 221 DEVFS_LOCK();
1c79356b 222 devfs_free_plane(devfs_mp_p);
91447636
A
223 DEVFS_UNLOCK();
224
1c79356b 225 DEVFS_DECR_MOUNTS();
91447636
A
226
227 FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
1c79356b
A
228 mp->mnt_data = (qaddr_t)0;
229 mp->mnt_flag &= ~MNT_LOCAL;
230
231 return 0;
232}
233
234/* return the address of the root vnode in *vpp */
235static int
91447636 236devfs_root(struct mount *mp, struct vnode **vpp, vfs_context_t context)
1c79356b
A
237{
238 struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data);
239 int error;
240
91447636
A
241 DEVFS_LOCK();
242 error = devfs_dntovn(devfs_mp_p->plane_root->de_dnp, vpp, context->vc_proc);
243 DEVFS_UNLOCK();
1c79356b 244
91447636 245 return error;
1c79356b
A
246}
247
248static int
91447636 249devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, __unused vfs_context_t context)
1c79356b
A
250{
251 struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
252
253 /*-
254 * Fill in the stat block.
255 */
91447636 256 //sbp->f_type = mp->mnt_vfsstat.f_type;
1c79356b
A
257 sbp->f_flags = 0; /* XXX */
258 sbp->f_bsize = 512;
259 sbp->f_iosize = 512;
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
264 ) / sbp->f_bsize;
265 sbp->f_bfree = 0;
266 sbp->f_bavail = 0;
267 sbp->f_files = devfs_stats.nodes;
268 sbp->f_ffree = 0;
269 sbp->f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
91447636 270 sbp->f_fsid.val[1] = vfs_typenum(mp);
1c79356b 271
91447636
A
272 return 0;
273}
274
275static int
276devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context)
277{
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
287 ) / fsap->f_bsize;
288 fsap->f_bused = fsap->f_blocks;
289 VFSATTR_SET_SUPPORTED(fsap, f_blocks);
290 VFSATTR_SET_SUPPORTED(fsap, f_bused);
1c79356b 291 }
91447636
A
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);
297
1c79356b
A
298 return 0;
299}
300
301static int
91447636 302devfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t context)
1c79356b
A
303{
304 return (0);
305}
306
307
308static int
91447636 309devfs_vget(__unused struct mount *mp, __unused ino64_t ino, __unused struct vnode **vpp, __unused vfs_context_t context)
1c79356b 310{
91447636 311 return ENOTSUP;
1c79356b
A
312}
313
314/*************************************************************
315 * The concept of exporting a kernel generated devfs is stupid
316 * So don't handle filehandles
317 */
318
319static int
91447636 320devfs_fhtovp (__unused struct mount *mp, __unused int fhlen, __unused unsigned char *fhp, __unused struct vnode **vpp, __unused vfs_context_t context)
1c79356b
A
321{
322 return (EINVAL);
323}
324
325
326static int
91447636 327devfs_vptofh (__unused struct vnode *vp, __unused int *fhlenp, __unused unsigned char *fhp, __unused vfs_context_t context)
1c79356b
A
328{
329 return (EINVAL);
330}
331
332static int
91447636
A
333devfs_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)
1c79356b 336{
91447636 337 return (ENOTSUP);
1c79356b
A
338}
339
340#include <sys/namei.h>
341
342/*
343 * Function: devfs_kernel_mount
344 * Purpose:
345 * Mount devfs at the given mount point from within the kernel.
346 */
347int
348devfs_kernel_mount(char * mntname)
349{
350 struct mount *mp;
351 int error;
1c79356b
A
352 struct nameidata nd;
353 struct vnode * vp;
91447636 354 struct vfs_context context;
1c79356b
A
355
356 if (devfs_vfsp == NULL) {
357 printf("devfs_kernel_mount: devfs_vfsp is NULL\n");
358 return (EINVAL);
359 }
91447636
A
360 context.vc_proc = current_proc();
361 context.vc_ucred = kauth_cred_get();
1c79356b
A
362
363 /*
364 * Get vnode to be covered
365 */
91447636
A
366 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32,
367 CAST_USER_ADDR_T(mntname), &context);
1c79356b
A
368 if ((error = namei(&nd))) {
369 printf("devfs_kernel_mount: failed to find directory '%s', %d",
370 mntname, error);
371 return (error);
372 }
91447636 373 nameidone(&nd);
1c79356b 374 vp = nd.ni_vp;
91447636
A
375
376 if ((error = VNOP_FSYNC(vp, MNT_WAIT, &context))) {
377 printf("devfs_kernel_mount: vnop_fsync failed: %d\n", error);
378 vnode_put(vp);
1c79356b
A
379 return (error);
380 }
91447636
A
381 if ((error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
382 printf("devfs_kernel_mount: buf_invalidateblks failed: %d\n", error);
383 vnode_put(vp);
384 return (error);
385 }
386 if (vnode_isdir(vp) == 0) {
1c79356b 387 printf("devfs_kernel_mount: '%s' is not a directory\n", mntname);
91447636 388 vnode_put(vp);
1c79356b
A
389 return (ENOTDIR);
390 }
91447636
A
391 if ((vnode_mountedhere(vp))) {
392 vnode_put(vp);
1c79356b
A
393 return (EBUSY);
394 }
395
396 /*
397 * Allocate and initialize the filesystem.
398 */
55e303ae
A
399 MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount),
400 M_MOUNT, M_WAITOK);
1c79356b 401 bzero((char *)mp, (u_long)sizeof(struct mount));
0b4e3aa0 402
91447636
A
403 /* Initialize the default IO constraints */
404 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
405 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
406
407 mount_lock_init(mp);
408 TAILQ_INIT(&mp->mnt_vnodelist);
409 TAILQ_INIT(&mp->mnt_workerqueue);
410 TAILQ_INIT(&mp->mnt_newvnodes);
0b4e3aa0 411
91447636 412 (void)vfs_busy(mp, LK_NOWAIT);
1c79356b 413 mp->mnt_op = devfs_vfsp->vfc_vfsops;
91447636 414 mp->mnt_vtable = devfs_vfsp;
1c79356b 415 devfs_vfsp->vfc_refcount++;
91447636
A
416 devfs_vfsp->vfc_threadsafe = TRUE;
417 devfs_vfsp->vfc_64bitready = TRUE;
1c79356b
A
418 mp->mnt_flag = 0;
419 mp->mnt_flag |= devfs_vfsp->vfc_flags & MNT_VISFLAGMASK;
91447636 420 strncpy(mp->mnt_vfsstat.f_fstypename, devfs_vfsp->vfc_name, MFSTYPENAMELEN);
1c79356b
A
421 vp->v_mountedhere = mp;
422 mp->mnt_vnodecovered = vp;
91447636
A
423 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
424 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN - 1, 0);
425
426 error = devfs_mount(mp, NULL, NULL, &context);
1c79356b 427
1c79356b
A
428 if (error) {
429 printf("devfs_kernel_mount: mount %s failed: %d", mntname, error);
91447636 430 mp->mnt_vtable->vfc_refcount--;
55e303ae 431
91447636 432 vfs_unbusy(mp);
55e303ae 433
91447636 434 mount_lock_destroy(mp);
55e303ae 435 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
91447636 436 vnode_put(vp);
1c79356b
A
437 return (error);
438 }
91447636
A
439 vnode_ref(vp);
440 vnode_put(vp);
441 vfs_unbusy(mp);
442 mount_list_add(mp);
1c79356b
A
443 return (0);
444}
445
446struct vfsops devfs_vfsops = {
447 devfs_mount,
448 devfs_start,
449 devfs_unmount,
450 devfs_root,
91447636
A
451 NULL, /* quotactl */
452 devfs_vfs_getattr,
1c79356b
A
453 devfs_sync,
454 devfs_vget,
455 devfs_fhtovp,
456 devfs_vptofh,
457 devfs_init,
91447636 458 devfs_sysctl
1c79356b 459};