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