]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/devfs/devfs_vfsops.c
c97601787bfb936bcb4d5a8c966807ad5340e4eb
[apple/xnu.git] / bsd / miscfs / devfs / devfs_vfsops.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 */
56 /*
57 * HISTORY
58 * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
59 * - modified devfs_statfs() to use devfs_stats to calculate the
60 * amount of memory used by devfs
61 */
62
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/vnode_internal.h>
68 #include <sys/proc.h>
69 #include <sys/kauth.h>
70 #include <sys/mount_internal.h>
71 #include <sys/malloc.h>
72
73 #include "devfs.h"
74 #include "devfsdefs.h"
75
76 static int devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, vfs_context_t context);
77 static int devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context);
78
79 static struct vfstable * devfs_vfsp = 0;
80
81
82 /*-
83 * Called from the generic VFS startups.
84 * This is the second stage of DEVFS initialisation.
85 * The probed devices have already been loaded and the
86 * basic structure of the DEVFS created.
87 * We take the oportunity to mount the hidden DEVFS layer, so that
88 * devices from devfs get sync'd.
89 */
90 static int
91 devfs_init(struct vfsconf *vfsp)
92 {
93 devfs_vfsp = (struct vfstable *)vfsp; /* remember this for devfs_kernel_mount below */
94
95 if (devfs_sinit())
96 return (ENOTSUP);
97 devfs_make_node(makedev(0, 0), DEVFS_CHAR,
98 UID_ROOT, GID_WHEEL, 0622, "console");
99 devfs_make_node(makedev(2, 0), DEVFS_CHAR,
100 UID_ROOT, GID_WHEEL, 0666, "tty");
101 devfs_make_node(makedev(3, 0), DEVFS_CHAR,
102 UID_ROOT, GID_KMEM, 0640, "mem");
103 devfs_make_node(makedev(3, 1), DEVFS_CHAR,
104 UID_ROOT, GID_KMEM, 0640, "kmem");
105 devfs_make_node(makedev(3, 2), DEVFS_CHAR,
106 UID_ROOT, GID_WHEEL, 0666, "null");
107 devfs_make_node(makedev(3, 3), DEVFS_CHAR,
108 UID_ROOT, GID_WHEEL, 0666, "zero");
109 devfs_make_node(makedev(6, 0), DEVFS_CHAR,
110 UID_ROOT, GID_WHEEL, 0600, "klog");
111 return 0;
112 }
113
114 /*-
115 * mp - pointer to 'mount' structure
116 * path - addr in user space of mount point (ie /usr or whatever)
117 * data - addr in user space of mount params including the
118 * name of the block special file to treat as a filesystem.
119 * (NOT USED)
120 * ndp - namei data pointer (NOT USED)
121 * p - proc pointer
122 * devfs is special in that it doesn't require any device to be mounted..
123 * It makes up its data as it goes along.
124 * it must be mounted during single user.. until it is, only std{in/out/err}
125 * and the root filesystem are available.
126 */
127 /*proto*/
128 int
129 devfs_mount(struct mount *mp, __unused vnode_t devvp, __unused user_addr_t data, vfs_context_t context)
130 {
131 struct devfsmount *devfs_mp_p; /* devfs specific mount info */
132 int error;
133
134 /*-
135 * If they just want to update, we don't need to do anything.
136 */
137 if (mp->mnt_flag & MNT_UPDATE)
138 {
139 return 0;
140 }
141
142 /* Advisory locking should be handled at the VFS layer */
143 vfs_setlocklocal(mp);
144
145 /*-
146 * Well, it's not an update, it's a real mount request.
147 * Time to get dirty.
148 * HERE we should check to see if we are already mounted here.
149 */
150
151 MALLOC(devfs_mp_p, struct devfsmount *, sizeof(struct devfsmount),
152 M_DEVFSMNT, M_WAITOK);
153 if (devfs_mp_p == NULL)
154 return (ENOMEM);
155 bzero(devfs_mp_p,sizeof(*devfs_mp_p));
156 devfs_mp_p->mount = mp;
157
158 /*-
159 * Fill out some fields
160 */
161 mp->mnt_data = (qaddr_t)devfs_mp_p;
162 mp->mnt_vfsstat.f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
163 mp->mnt_vfsstat.f_fsid.val[1] = vfs_typenum(mp);
164 mp->mnt_flag |= MNT_LOCAL;
165
166 DEVFS_LOCK();
167 error = dev_dup_plane(devfs_mp_p);
168 DEVFS_UNLOCK();
169
170 if (error) {
171 mp->mnt_data = (qaddr_t)0;
172 FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
173 return (error);
174 } else
175 DEVFS_INCR_MOUNTS();
176
177 /*-
178 * Copy in the name of the directory the filesystem
179 * is to be mounted on.
180 * And we clear the remainder of the character strings
181 * to be tidy.
182 */
183
184 bzero(mp->mnt_vfsstat.f_mntfromname, MAXPATHLEN);
185 bcopy("devfs",mp->mnt_vfsstat.f_mntfromname, 5);
186 (void)devfs_statfs(mp, &mp->mnt_vfsstat, context);
187
188 return 0;
189 }
190
191
192 static int
193 devfs_start(__unused struct mount *mp, __unused int flags, __unused vfs_context_t context)
194 {
195 return 0;
196 }
197
198 /*-
199 * Unmount the filesystem described by mp.
200 */
201 static int
202 devfs_unmount( struct mount *mp, int mntflags, __unused vfs_context_t context)
203 {
204 struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
205 int flags = 0;
206 int force = 0;
207 int error;
208
209 if (mntflags & MNT_FORCE) {
210 flags |= FORCECLOSE;
211 force = 1;
212 }
213 error = vflush(mp, NULLVP, flags);
214 if (error && !force)
215 return error;
216
217 DEVFS_LOCK();
218 devfs_free_plane(devfs_mp_p);
219 DEVFS_UNLOCK();
220
221 DEVFS_DECR_MOUNTS();
222
223 FREE((caddr_t)devfs_mp_p, M_DEVFSMNT);
224 mp->mnt_data = (qaddr_t)0;
225 mp->mnt_flag &= ~MNT_LOCAL;
226
227 return 0;
228 }
229
230 /* return the address of the root vnode in *vpp */
231 static int
232 devfs_root(struct mount *mp, struct vnode **vpp, vfs_context_t context)
233 {
234 struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data);
235 int error;
236
237 DEVFS_LOCK();
238 error = devfs_dntovn(devfs_mp_p->plane_root->de_dnp, vpp, context->vc_proc);
239 DEVFS_UNLOCK();
240
241 return error;
242 }
243
244 static int
245 devfs_statfs( struct mount *mp, struct vfsstatfs *sbp, __unused vfs_context_t context)
246 {
247 struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data;
248
249 /*-
250 * Fill in the stat block.
251 */
252 //sbp->f_type = mp->mnt_vfsstat.f_type;
253 sbp->f_flags = 0; /* XXX */
254 sbp->f_bsize = 512;
255 sbp->f_iosize = 512;
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
260 ) / sbp->f_bsize;
261 sbp->f_bfree = 0;
262 sbp->f_bavail = 0;
263 sbp->f_files = devfs_stats.nodes;
264 sbp->f_ffree = 0;
265 sbp->f_fsid.val[0] = (int32_t)(void *)devfs_mp_p;
266 sbp->f_fsid.val[1] = vfs_typenum(mp);
267
268 return 0;
269 }
270
271 static int
272 devfs_vfs_getattr(mount_t mp, struct vfs_attr *fsap, vfs_context_t context)
273 {
274 VFSATTR_RETURN(fsap, f_objcount, devfs_stats.nodes);
275 VFSATTR_RETURN(fsap, f_maxobjcount, devfs_stats.nodes);
276 VFSATTR_RETURN(fsap, f_bsize, 512);
277 VFSATTR_RETURN(fsap, f_iosize, 512);
278 if (VFSATTR_IS_ACTIVE(fsap, f_blocks) || VFSATTR_IS_ACTIVE(fsap, f_bused)) {
279 fsap->f_blocks = (devfs_stats.mounts * sizeof(struct devfsmount)
280 + devfs_stats.nodes * sizeof(devnode_t)
281 + devfs_stats.entries * sizeof(devdirent_t)
282 + devfs_stats.stringspace
283 ) / fsap->f_bsize;
284 fsap->f_bused = fsap->f_blocks;
285 VFSATTR_SET_SUPPORTED(fsap, f_blocks);
286 VFSATTR_SET_SUPPORTED(fsap, f_bused);
287 }
288 VFSATTR_RETURN(fsap, f_bfree, 0);
289 VFSATTR_RETURN(fsap, f_bavail, 0);
290 VFSATTR_RETURN(fsap, f_files, devfs_stats.nodes);
291 VFSATTR_RETURN(fsap, f_ffree, 0);
292 VFSATTR_RETURN(fsap, f_fssubtype, 0);
293
294 return 0;
295 }
296
297 static int
298 devfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t context)
299 {
300 return (0);
301 }
302
303
304 static int
305 devfs_vget(__unused struct mount *mp, __unused ino64_t ino, __unused struct vnode **vpp, __unused vfs_context_t context)
306 {
307 return ENOTSUP;
308 }
309
310 /*************************************************************
311 * The concept of exporting a kernel generated devfs is stupid
312 * So don't handle filehandles
313 */
314
315 static int
316 devfs_fhtovp (__unused struct mount *mp, __unused int fhlen, __unused unsigned char *fhp, __unused struct vnode **vpp, __unused vfs_context_t context)
317 {
318 return (EINVAL);
319 }
320
321
322 static int
323 devfs_vptofh (__unused struct vnode *vp, __unused int *fhlenp, __unused unsigned char *fhp, __unused vfs_context_t context)
324 {
325 return (EINVAL);
326 }
327
328 static int
329 devfs_sysctl(__unused int *name, __unused u_int namelen, __unused user_addr_t oldp,
330 __unused size_t *oldlenp, __unused user_addr_t newp,
331 __unused size_t newlen, __unused vfs_context_t context)
332 {
333 return (ENOTSUP);
334 }
335
336 #include <sys/namei.h>
337
338 /*
339 * Function: devfs_kernel_mount
340 * Purpose:
341 * Mount devfs at the given mount point from within the kernel.
342 */
343 int
344 devfs_kernel_mount(char * mntname)
345 {
346 struct mount *mp;
347 int error;
348 struct nameidata nd;
349 struct vnode * vp;
350 struct vfs_context context;
351
352 if (devfs_vfsp == NULL) {
353 printf("devfs_kernel_mount: devfs_vfsp is NULL\n");
354 return (EINVAL);
355 }
356 context.vc_proc = current_proc();
357 context.vc_ucred = kauth_cred_get();
358
359 /*
360 * Get vnode to be covered
361 */
362 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32,
363 CAST_USER_ADDR_T(mntname), &context);
364 if ((error = namei(&nd))) {
365 printf("devfs_kernel_mount: failed to find directory '%s', %d",
366 mntname, error);
367 return (error);
368 }
369 nameidone(&nd);
370 vp = nd.ni_vp;
371
372 if ((error = VNOP_FSYNC(vp, MNT_WAIT, &context))) {
373 printf("devfs_kernel_mount: vnop_fsync failed: %d\n", error);
374 vnode_put(vp);
375 return (error);
376 }
377 if ((error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0))) {
378 printf("devfs_kernel_mount: buf_invalidateblks failed: %d\n", error);
379 vnode_put(vp);
380 return (error);
381 }
382 if (vnode_isdir(vp) == 0) {
383 printf("devfs_kernel_mount: '%s' is not a directory\n", mntname);
384 vnode_put(vp);
385 return (ENOTDIR);
386 }
387 if ((vnode_mountedhere(vp))) {
388 vnode_put(vp);
389 return (EBUSY);
390 }
391
392 /*
393 * Allocate and initialize the filesystem.
394 */
395 MALLOC_ZONE(mp, struct mount *, (u_long)sizeof(struct mount),
396 M_MOUNT, M_WAITOK);
397 bzero((char *)mp, (u_long)sizeof(struct mount));
398
399 /* Initialize the default IO constraints */
400 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
401 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
402
403 mount_lock_init(mp);
404 TAILQ_INIT(&mp->mnt_vnodelist);
405 TAILQ_INIT(&mp->mnt_workerqueue);
406 TAILQ_INIT(&mp->mnt_newvnodes);
407
408 (void)vfs_busy(mp, LK_NOWAIT);
409 mp->mnt_op = devfs_vfsp->vfc_vfsops;
410 mp->mnt_vtable = devfs_vfsp;
411 devfs_vfsp->vfc_refcount++;
412 devfs_vfsp->vfc_threadsafe = TRUE;
413 devfs_vfsp->vfc_64bitready = TRUE;
414 mp->mnt_flag = 0;
415 mp->mnt_flag |= devfs_vfsp->vfc_flags & MNT_VISFLAGMASK;
416 strncpy(mp->mnt_vfsstat.f_fstypename, devfs_vfsp->vfc_name, MFSTYPENAMELEN);
417 vp->v_mountedhere = mp;
418 mp->mnt_vnodecovered = vp;
419 mp->mnt_vfsstat.f_owner = kauth_cred_getuid(kauth_cred_get());
420 (void) copystr(mntname, mp->mnt_vfsstat.f_mntonname, MAXPATHLEN - 1, 0);
421
422 error = devfs_mount(mp, NULL, NULL, &context);
423
424 if (error) {
425 printf("devfs_kernel_mount: mount %s failed: %d", mntname, error);
426 mp->mnt_vtable->vfc_refcount--;
427
428 vfs_unbusy(mp);
429
430 mount_lock_destroy(mp);
431 FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
432 vnode_put(vp);
433 return (error);
434 }
435 vnode_ref(vp);
436 vnode_put(vp);
437 vfs_unbusy(mp);
438 mount_list_add(mp);
439 return (0);
440 }
441
442 struct vfsops devfs_vfsops = {
443 devfs_mount,
444 devfs_start,
445 devfs_unmount,
446 devfs_root,
447 NULL, /* quotactl */
448 devfs_vfs_getattr,
449 devfs_sync,
450 devfs_vget,
451 devfs_fhtovp,
452 devfs_vptofh,
453 devfs_init,
454 devfs_sysctl
455 };