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