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