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