]> git.saurik.com Git - apple/xnu.git/blame - bsd/miscfs/devfs/devfs_vfsops.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / miscfs / devfs / devfs_vfsops.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*-
29 * Copyright 1997,1998 Julian Elischer. All rights reserved.
30 * julian@freebsd.org
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions are
34 * met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
39 * and/or other materials provided with the distribution.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
42 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
45 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * devfs_vfsops.c
54 *
55 */
2d21ac55
A
56/*
57 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 */
1c79356b
A
62/*
63 * HISTORY
64 * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
65 * - modified devfs_statfs() to use devfs_stats to calculate the
66 * amount of memory used by devfs
67 */
68
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/kernel.h>
91447636 73#include <sys/vnode_internal.h>
1c79356b 74#include <sys/proc.h>
91447636
A
75#include <sys/kauth.h>
76#include <sys/mount_internal.h>
1c79356b
A
77#include <sys/malloc.h>
78
2d21ac55
A
79#include <libkern/OSAtomic.h>
80
81#if CONFIG_MACF
82#include <security/mac_framework.h>
83#endif
84
1c79356b
A
85#include "devfs.h"
86#include "devfsdefs.h"
87
2d21ac55
A
88static int devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, vfs_context_t ctx);
89static int devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t ctx);
1c79356b 90
91447636 91static struct vfstable * devfs_vfsp = 0;
0c530ab8 92extern int setup_kmem;
2d21ac55 93__private_extern__ void devfs_setup_kmem(void);
1c79356b
A
94
95
96/*-
97 * Called from the generic VFS startups.
98 * This is the second stage of DEVFS initialisation.
99 * The probed devices have already been loaded and the
100 * basic structure of the DEVFS created.
101 * We take the oportunity to mount the hidden DEVFS layer, so that
102 * devices from devfs get sync'd.
103 */
104static int
105devfs_init(struct vfsconf *vfsp)
106{
91447636 107 devfs_vfsp = (struct vfstable *)vfsp; /* remember this for devfs_kernel_mount below */
1c79356b
A
108
109 if (devfs_sinit())
91447636 110 return (ENOTSUP);
1c79356b
A
111 devfs_make_node(makedev(0, 0), DEVFS_CHAR,
112 UID_ROOT, GID_WHEEL, 0622, "console");
113 devfs_make_node(makedev(2, 0), DEVFS_CHAR,
114 UID_ROOT, GID_WHEEL, 0666, "tty");
0c530ab8
A
115 if (setup_kmem) {
116 devfs_setup_kmem();
117 }
1c79356b
A
118 devfs_make_node(makedev(3, 2), DEVFS_CHAR,
119 UID_ROOT, GID_WHEEL, 0666, "null");
120 devfs_make_node(makedev(3, 3), DEVFS_CHAR,
121 UID_ROOT, GID_WHEEL, 0666, "zero");
122 devfs_make_node(makedev(6, 0), DEVFS_CHAR,
123 UID_ROOT, GID_WHEEL, 0600, "klog");
124 return 0;
125}
126
0c530ab8
A
127__private_extern__ void
128devfs_setup_kmem(void)
129{
130 devfs_make_node(makedev(3, 0), DEVFS_CHAR,
131 UID_ROOT, GID_KMEM, 0640, "mem");
132 devfs_make_node(makedev(3, 1), DEVFS_CHAR,
133 UID_ROOT, GID_KMEM, 0640, "kmem");
134}
135
136
1c79356b
A
137/*-
138 * mp - pointer to 'mount' structure
139 * path - addr in user space of mount point (ie /usr or whatever)
140 * data - addr in user space of mount params including the
141 * name of the block special file to treat as a filesystem.
142 * (NOT USED)
143 * ndp - namei data pointer (NOT USED)
144 * p - proc pointer
145 * devfs is special in that it doesn't require any device to be mounted..
146 * It makes up its data as it goes along.
147 * it must be mounted during single user.. until it is, only std{in/out/err}
148 * and the root filesystem are available.
149 */
150/*proto*/
151int
2d21ac55 152devfs_mount(struct mount *mp, __unused vnode_t devvp, __unused user_addr_t data, vfs_context_t ctx)
1c79356b
A
153{
154 struct devfsmount *devfs_mp_p; /* devfs specific mount info */
155 int error;
1c79356b
A
156
157 /*-
158 * If they just want to update, we don't need to do anything.
159 */
160 if (mp->mnt_flag & MNT_UPDATE)
161 {
162 return 0;
163 }
164
91447636
A
165 /* Advisory locking should be handled at the VFS layer */
166 vfs_setlocklocal(mp);
167
1c79356b
A
168 /*-
169 * Well, it's not an update, it's a real mount request.
170 * Time to get dirty.
171 * HERE we should check to see if we are already mounted here.
172 */
173
174 MALLOC(devfs_mp_p, struct devfsmount *, sizeof(struct devfsmount),
175 M_DEVFSMNT, M_WAITOK);
176 if (devfs_mp_p == NULL)
177 return (ENOMEM);
178 bzero(devfs_mp_p,sizeof(*devfs_mp_p));
179 devfs_mp_p->mount = mp;
180
181 /*-
182 * Fill out some fields
183 */
184 mp->mnt_data = (qaddr_t)devfs_mp_p;
91447636
A
185 mp->mnt_vfsstat.f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
186 mp->mnt_vfsstat.f_fsid.val[1] = vfs_typenum(mp);
1c79356b
A
187 mp->mnt_flag |= MNT_LOCAL;
188
91447636 189 DEVFS_LOCK();
1c79356b 190 error = dev_dup_plane(devfs_mp_p);
91447636
A
191 DEVFS_UNLOCK();
192
1c79356b
A
193 if (error) {
194 mp->mnt_data = (qaddr_t)0;
195 FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
196 return (error);
91447636
A
197 } else
198 DEVFS_INCR_MOUNTS();
1c79356b
A
199
200 /*-
201 * Copy in the name of the directory the filesystem
202 * is to be mounted on.
203 * And we clear the remainder of the character strings
204 * to be tidy.
205 */
206
91447636
A
207 bzero(mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN);
208 bcopy("devfs",mp->mnt_vfsstat.f_mntfromname, 5);
2d21ac55 209 (void)devfs_statfs(mp, &mp->mnt_vfsstat, ctx);
91447636 210
1c79356b
A
211 return 0;
212}
213
214
215static int
2d21ac55 216devfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t ctx)
1c79356b
A
217{
218 return 0;
219}
220
221/*-
222 * Unmount the filesystem described by mp.
223 */
224static int
2d21ac55 225devfs_unmount( struct mount *mp, int mntflags, __unused vfs_context_t ctx)
1c79356b
A
226{
227 struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
228 int flags = 0;
9bccf70c 229 int force = 0;
1c79356b
A
230 int error;
231
232 if (mntflags & MNT_FORCE) {
233 flags |= FORCECLOSE;
9bccf70c 234 force = 1;
1c79356b
A
235 }
236 error = vflush(mp, NULLVP, flags);
9bccf70c 237 if (error && !force)
1c79356b
A
238 return error;
239
91447636 240 DEVFS_LOCK();
1c79356b 241 devfs_free_plane(devfs_mp_p);
91447636
A
242 DEVFS_UNLOCK();
243
1c79356b 244 DEVFS_DECR_MOUNTS();
91447636
A
245
246 FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
1c79356b
A
247 mp->mnt_data = (qaddr_t)0;
248 mp->mnt_flag &= ~MNT_LOCAL;
249
250 return 0;
251}
252
253/* return the address of the root vnode in *vpp */
254static int
2d21ac55 255devfs_root(struct mount *mp, struct vnode **vpp, __unused vfs_context_t ctx)
1c79356b
A
256{
257 struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data);
258 int error;
259
91447636 260 DEVFS_LOCK();
2d21ac55
A
261 /* last parameter to devfs_dntovn() is ignored */
262 error = devfs_dntovn(devfs_mp_p->plane_root->de_dnp, vpp, NULL);
91447636 263 DEVFS_UNLOCK();
1c79356b 264
91447636 265 return error;
1c79356b
A
266}
267
268static int
2d21ac55 269devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, __unused vfs_context_t ctx)
1c79356b
A
270{
271 struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
272
273 /*-
274 * Fill in the stat block.
275 */
91447636 276 //sbp->f_type = mp->mnt_vfsstat.f_type;
1c79356b
A
277 sbp->f_flags = 0; /* XXX */
278 sbp->f_bsize = 512;
279 sbp->f_iosize = 512;
280 sbp->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
281 + devfs_stats.nodes * sizeof(devnode_t)
282 + devfs_stats.entries * sizeof(devdirent_t)
283 + devfs_stats.stringspace
284 ) / sbp->f_bsize;
285 sbp->f_bfree = 0;
286 sbp->f_bavail = 0;
287 sbp->f_files = devfs_stats.nodes;
288 sbp->f_ffree = 0;
289 sbp->f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
91447636 290 sbp->f_fsid.val[1] = vfs_typenum(mp);
1c79356b 291
91447636
A
292 return 0;
293}
294
295static int
2d21ac55 296devfs_vfs_getattr(__unused mount_t mp, struct vfs_attr *fsap, __unused vfs_context_t ctx)
91447636
A
297{
298 VFSATTR_RETURN(fsap, f_objcount, devfs_stats.nodes);
299 VFSATTR_RETURN(fsap, f_maxobjcount, devfs_stats.nodes);
300 VFSATTR_RETURN(fsap, f_bsize, 512);
301 VFSATTR_RETURN(fsap, f_iosize, 512);
302 if (VFSATTR_IS_ACTIVE(fsap, f_blocks) || VFSATTR_IS_ACTIVE(fsap, f_bused)) {
303 fsap->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
304 + devfs_stats.nodes * sizeof(devnode_t)
305 + devfs_stats.entries * sizeof(devdirent_t)
306 + devfs_stats.stringspace
307 ) / fsap->f_bsize;
308 fsap->f_bused = fsap->f_blocks;
309 VFSATTR_SET_SUPPORTED(fsap, f_blocks);
310 VFSATTR_SET_SUPPORTED(fsap, f_bused);
1c79356b 311 }
91447636
A
312 VFSATTR_RETURN(fsap, f_bfree, 0);
313 VFSATTR_RETURN(fsap, f_bavail, 0);
314 VFSATTR_RETURN(fsap, f_files, devfs_stats.nodes);
315 VFSATTR_RETURN(fsap, f_ffree, 0);
316 VFSATTR_RETURN(fsap, f_fssubtype, 0);
317
1c79356b
A
318 return 0;
319}
320
321static int
2d21ac55 322devfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t ctx)
1c79356b
A
323{
324 return (0);
325}
326
327
328static int
2d21ac55 329devfs_vget(__unused struct mount *mp, __unused ino64_t ino, __unused struct vnode **vpp, __unused vfs_context_t ctx)
1c79356b 330{
91447636 331 return ENOTSUP;
1c79356b
A
332}
333
334/*************************************************************
335 * The concept of exporting a kernel generated devfs is stupid
336 * So don't handle filehandles
337 */
338
339static int
2d21ac55 340devfs_fhtovp (__unused struct mount *mp, __unused int fhlen, __unused unsigned char *fhp, __unused struct vnode **vpp, __unused vfs_context_t ctx)
1c79356b
A
341{
342 return (EINVAL);
343}
344
345
346static int
2d21ac55 347devfs_vptofh (__unused struct vnode *vp, __unused int *fhlenp, __unused unsigned char *fhp, __unused vfs_context_t ctx)
1c79356b
A
348{
349 return (EINVAL);
350}
351
352static int
91447636
A
353devfs_sysctl(__unused int *name, __unused u_int namelen, __unused user_addr_t oldp,
354 __unused size_t *oldlenp, __unused user_addr_t newp,
2d21ac55 355 __unused size_t newlen, __unused vfs_context_t ctx)
1c79356b 356{
91447636 357 return (ENOTSUP);
1c79356b
A
358}
359
360#include <sys/namei.h>
361
362/*
363 * Function: devfs_kernel_mount
364 * Purpose:
365 * Mount devfs at the given mount point from within the kernel.
366 */
367int
368devfs_kernel_mount(char * mntname)
369{
370 struct mount *mp;
371 int error;
1c79356b
A
372 struct nameidata nd;
373 struct vnode * vp;
2d21ac55 374 vfs_context_t ctx = vfs_context_kernel();
1c79356b
A
375
376 if (devfs_vfsp == NULL) {
377 printf("devfs_kernel_mount: devfs_vfsp is NULL\n");
378 return (EINVAL);
379 }
1c79356b
A
380
381 /*
382 * Get vnode to be covered
383 */
91447636 384 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32,
2d21ac55 385 CAST_USER_ADDR_T(mntname), ctx);
1c79356b
A
386 if ((error = namei(&nd))) {
387 printf("devfs_kernel_mount: failed to find directory '%s', %d",
388 mntname, error);
389 return (error);
390 }
91447636 391 nameidone(&nd);
1c79356b 392 vp = nd.ni_vp;
91447636 393
2d21ac55 394 if ((error = VNOP_FSYNC(vp, MNT_WAIT, ctx))) {
91447636
A
395 printf("devfs_kernel_mount: vnop_fsync failed: %d\n", error);
396 vnode_put(vp);
1c79356b
A
397 return (error);
398 }
91447636
A
399 if ((error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
400 printf("devfs_kernel_mount: buf_invalidateblks failed: %d\n", error);
401 vnode_put(vp);
402 return (error);
403 }
404 if (vnode_isdir(vp) == 0) {
1c79356b 405 printf("devfs_kernel_mount: '%s' is not a directory\n", mntname);
91447636 406 vnode_put(vp);
1c79356b
A
407 return (ENOTDIR);
408 }
91447636
A
409 if ((vnode_mountedhere(vp))) {
410 vnode_put(vp);
1c79356b
A
411 return (EBUSY);
412 }
413
414 /*
415 * Allocate and initialize the filesystem.
416 */
55e303ae
A
417 MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount),
418 M_MOUNT, M_WAITOK);
1c79356b 419 bzero((char *)mp, (u_long)sizeof(struct mount));
0b4e3aa0 420
91447636
A
421 /* Initialize the default IO constraints */
422 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
423 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
2d21ac55
A
424 mp->mnt_ioflags = 0;
425 mp->mnt_realrootvp = NULLVP;
426 mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
91447636
A
427
428 mount_lock_init(mp);
429 TAILQ_INIT(&mp->mnt_vnodelist);
430 TAILQ_INIT(&mp->mnt_workerqueue);
431 TAILQ_INIT(&mp->mnt_newvnodes);
0b4e3aa0 432
91447636 433 (void)vfs_busy(mp, LK_NOWAIT);
1c79356b 434 mp->mnt_op = devfs_vfsp->vfc_vfsops;
91447636 435 mp->mnt_vtable = devfs_vfsp;
1c79356b 436 devfs_vfsp->vfc_refcount++;
91447636
A
437 devfs_vfsp->vfc_threadsafe = TRUE;
438 devfs_vfsp->vfc_64bitready = TRUE;
1c79356b
A
439 mp->mnt_flag = 0;
440 mp->mnt_flag |= devfs_vfsp->vfc_flags & MNT_VISFLAGMASK;
2d21ac55 441 strlcpy(mp->mnt_vfsstat.f_fstypename, devfs_vfsp->vfc_name, MFSTYPENAMELEN);
1c79356b
A
442 vp->v_mountedhere = mp;
443 mp->mnt_vnodecovered = vp;
91447636
A
444 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
445 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN - 1, 0);
2d21ac55
A
446#if CONFIG_MACF
447 mac_mount_label_init(mp);
448 mac_mount_label_associate(ctx, mp);
449#endif
91447636 450
2d21ac55 451 error = devfs_mount(mp, NULL, USER_ADDR_NULL, ctx);
1c79356b 452
1c79356b
A
453 if (error) {
454 printf("devfs_kernel_mount: mount %s failed: %d", mntname, error);
91447636 455 mp->mnt_vtable->vfc_refcount--;
55e303ae 456
91447636 457 vfs_unbusy(mp);
55e303ae 458
91447636 459 mount_lock_destroy(mp);
2d21ac55
A
460#if CONFIG_MACF
461 mac_mount_label_destroy(mp);
462#endif
55e303ae 463 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
91447636 464 vnode_put(vp);
1c79356b
A
465 return (error);
466 }
91447636
A
467 vnode_ref(vp);
468 vnode_put(vp);
469 vfs_unbusy(mp);
470 mount_list_add(mp);
1c79356b
A
471 return (0);
472}
473
474struct vfsops devfs_vfsops = {
475 devfs_mount,
476 devfs_start,
477 devfs_unmount,
478 devfs_root,
91447636
A
479 NULL, /* quotactl */
480 devfs_vfs_getattr,
1c79356b
A
481 devfs_sync,
482 devfs_vget,
483 devfs_fhtovp,
484 devfs_vptofh,
485 devfs_init,
2d21ac55
A
486 devfs_sysctl,
487 NULL,
488 {NULL}
1c79356b 489};