2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1998 Apple Computer, Inc. All Rights Reserved */
29 * 29-May-1998 Pat Dirks Changed to cache pointer to root vnode until unmount.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/namei.h>
37 #include <sys/kernel.h>
38 #include <mach/machine/vm_types.h>
39 #include <sys/vnode.h>
40 #include <sys/socket.h>
41 #include <sys/mount.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
51 #include <miscfs/specfs/specdev.h>
54 struct vfsops volfs_vfsops
= {
69 static char volfs_fs_name
[MFSNAMELEN
] = "volfs";
70 extern struct vnodeopv_desc volfs_vnodeop_opv_desc
;
72 /* The following refer to kernel global variables used in the loading/initialization: */
73 extern int maxvfsslots
; /* Total number of slots in the system's vfsconf table */
74 extern int maxvfsconf
; /* The highest fs type number [old-style ID] in use [dispite its name] */
75 extern int vfs_opv_numops
; /* The total number of defined vnode operations */
79 volfs_load(int loadArgument
) {
80 struct vfsconf
*vfsconflistentry
;
82 struct vfsconf
*newvfsconf
= NULL
;
83 struct vfsconf
*lastentry
= NULL
;
85 int (***opv_desc_vector_p
)();
86 int (**opv_desc_vector
)();
87 struct vnodeopv_entry_desc
*opve_descp
;
89 #pragma unused(loadArgument)
92 * This routine is responsible for all the initialization that would
93 * ordinarily be done as part of the system startup; it calls volfs_init
94 * to do the initialization that is strictly volfs-specific.
98 prevvfsconf is supposed to be the entry preceding the new entry.
99 To make sure we can always get hooked in SOMEWHERE in the list,
100 start it out at the first entry of the list. This assumes the
101 first entry in the list will be non-empty and not volfs.
103 This becomes irrelevant when volfs is compiled into the list.
105 DBG_VOP(("load_volfs: Scanning vfsconf list...\n"));
106 vfsconflistentry
= vfsconf
;
107 for (entriesRemaining
= maxvfsslots
; entriesRemaining
> 0; --entriesRemaining
) {
108 if (vfsconflistentry
->vfc_vfsops
!= NULL
) {
110 * Check to see if we're reloading a new version of volfs during debugging
111 * and overwrite the previously assigned entry if we find one:
113 if (strcmp(vfsconflistentry
->vfc_name
, volfs_fs_name
) == 0) {
114 newvfsconf
= vfsconflistentry
;
117 lastentry
= vfsconflistentry
;
121 * This is at least a POSSIBLE place to insert the new entry...
123 newvfsconf
= vfsconflistentry
;
129 DBG_VOP(("load_volfs: filling in vfsconf entry at 0x%08lX; lastentry = 0x%08lX.\n", (long)newvfsconf
, (long)lastentry
));
130 newvfsconf
->vfc_vfsops
= &volfs_vfsops
;
131 strncpy(&newvfsconf
->vfc_name
[0], "volfs", MFSNAMELEN
);
132 newvfsconf
->vfc_typenum
= maxvfsconf
++;
133 newvfsconf
->vfc_refcount
= 0;
134 newvfsconf
->vfc_flags
= 0;
135 newvfsconf
->vfc_mountroot
= NULL
; /* Can't mount root of file system [yet] */
137 /* Hook into the list: */
138 newvfsconf
->vfc_next
= NULL
;
140 newvfsconf
->vfc_next
= lastentry
->vfc_next
;
141 lastentry
->vfc_next
= newvfsconf
;
144 /* Based on vfs_op_init and ... */
145 opv_desc_vector_p
= volfs_vnodeop_opv_desc
.opv_desc_vector_p
;
147 DBG_VOP(("load_volfs: Allocating and initializing VNode ops vector...\n"));
150 * Allocate and init the vector.
151 * Also handle backwards compatibility.
153 MALLOC(*opv_desc_vector_p
, PFI
*, vfs_opv_numops
*sizeof(PFI
), M_TEMP
, M_WAITOK
);
155 bzero (*opv_desc_vector_p
, vfs_opv_numops
*sizeof(PFI
));
157 opv_desc_vector
= *opv_desc_vector_p
;
158 for (j
=0; volfs_vnodeop_opv_desc
.opv_desc_ops
[j
].opve_op
; j
++) {
159 opve_descp
= &(volfs_vnodeop_opv_desc
.opv_desc_ops
[j
]);
162 * Sanity check: is this operation listed
163 * in the list of operations? We check this
164 * by seeing if its offest is zero. Since
165 * the default routine should always be listed
166 * first, it should be the only one with a zero
167 * offset. Any other operation with a zero
168 * offset is probably not listed in
169 * vfs_op_descs, and so is probably an error.
171 * A panic here means the layer programmer
172 * has committed the all-too common bug
173 * of adding a new operation to the layer's
174 * list of vnode operations but
175 * not adding the operation to the system-wide
176 * list of supported operations.
178 if (opve_descp
->opve_op
->vdesc_offset
== 0 &&
179 opve_descp
->opve_op
->vdesc_offset
!= VOFFSET(vop_default
)) {
180 DBG_VOP(("load_volfs: operation %s not listed in %s.\n",
181 opve_descp
->opve_op
->vdesc_name
,
183 panic ("load_volfs: bad operation");
186 * Fill in this entry.
188 opv_desc_vector
[opve_descp
->opve_op
->vdesc_offset
] =
189 opve_descp
->opve_impl
;
193 * Finally, go back and replace unfilled routines
194 * with their default. (Sigh, an O(n^3) algorithm. I
195 * could make it better, but that'd be work, and n is small.)
197 opv_desc_vector_p
= volfs_vnodeop_opv_desc
.opv_desc_vector_p
;
200 * Force every operations vector to have a default routine.
202 opv_desc_vector
= *opv_desc_vector_p
;
203 if (opv_desc_vector
[VOFFSET(vop_default
)]==NULL
) {
204 panic("load_vp;fs: operation vector without default routine.");
206 for (j
= 0;j
<vfs_opv_numops
; j
++)
207 if (opv_desc_vector
[j
] == NULL
)
209 opv_desc_vector
[VOFFSET(vop_default
)];
211 DBG_VOP(("load_volfs: calling volfs_init()...\n"));
212 volfs_init(newvfsconf
);
222 volfs_mount(mp
, path
, data
, ndp
, p
)
223 register struct mount
*mp
;
226 struct nameidata
*ndp
;
229 struct volfs_mntdata
*priv_mnt_data
;
230 struct vnode
*root_vp
;
231 struct volfs_vndata
*priv_vn_data
;
235 DBG_VOP(("volfs_mount called\n"));
236 MALLOC(priv_mnt_data
, struct volfs_mntdata
*, sizeof(struct volfs_mntdata
),
237 M_VOLFSMNT
, M_WAITOK
);
238 DBG_VOP(("MALLOC succeeded\n"));
239 LIST_INIT(&priv_mnt_data
->volfs_fsvnodes
);
240 DBG_VOP(("LIST_INIT succeeded\n"));
242 mp
->mnt_data
= (void *)priv_mnt_data
;
243 strcpy(mp
->mnt_stat
.f_fstypename
, "volfs");
244 (void) copyinstr(path
, mp
->mnt_stat
.f_mntonname
, sizeof(mp
->mnt_stat
.f_mntonname
) - 1, &size
);
245 strcpy(mp
->mnt_stat
.f_mntfromname
, "<volfs>");
247 /* Set up the root vnode for fast reference in the future.
248 Note that the root is maintained unlocked but with a pos. ref count until unmount. */
250 MALLOC(priv_vn_data
, struct volfs_vndata
*, sizeof(struct volfs_vndata
), M_VOLFSNODE
, M_WAITOK
);
251 error
= getnewvnode(VT_VOLFS
, mp
, volfs_vnodeop_p
, &root_vp
);
254 FREE(priv_mnt_data
, M_VOLFSMNT
);
255 FREE(priv_vn_data
, M_VOLFSNODE
);
256 DBG_VOP(("getnewvnode failed with error code %d\n", error
));
259 root_vp
->v_type
= VDIR
;
260 root_vp
->v_flag
|= VROOT
;
261 lockinit(&priv_vn_data
->lock
, PINOD
, "volfsnode", 0, 0);
262 priv_vn_data
->vnode_type
= VOLFS_ROOT
;
263 priv_vn_data
->nodeID
= 0;
264 priv_vn_data
->fs_mount
= mp
;
265 root_vp
->v_data
= priv_vn_data
;
267 priv_mnt_data
->volfs_rootvp
= root_vp
;
273 volfs_start(mp
, flags
, p
)
278 DBG_VOP(("volfs_start called\n"));
283 * Return the root of a filesystem. For volfs the root vnode is a directory
284 * containing the list of all filesystems volfs can work with.
291 struct volfs_mntdata
*priv_data
;
292 // struct volfs_vndata *priv_vn_data;
295 DBG_VOP(("volfs_root called\n"));
296 priv_data
= (struct volfs_mntdata
*)mp
->mnt_data
;
298 if (priv_data
->volfs_rootvp
) {
299 vref(priv_data
->volfs_rootvp
);
300 VOP_LOCK(priv_data
->volfs_rootvp
, LK_EXCLUSIVE
, current_proc());
301 *vpp
= priv_data
->volfs_rootvp
;
303 panic("volfs: root vnode missing!");
306 DBG_VOP(("volfs_root returned with "));
307 DBG_VOP_PRINT_VNODE_INFO(*vpp
);DBG_VOP(("\n"));
313 volfs_quotactl(mp
, cmds
, uid
, arg
, p
)
320 DBG_VOP(("volfs_quotactl called\n"));
325 * unmount system call
328 volfs_unmount(mp
, mntflags
, p
)
333 struct volfs_mntdata
*priv_data
;
334 struct vnode
*root_vp
;
337 DBG_VOP(("volfs_unmount called\n"));
338 priv_data
= (struct volfs_mntdata
*)mp
->mnt_data
;
340 root_vp
= priv_data
->volfs_rootvp
;
341 retval
= vflush(mp
, root_vp
, 0);
342 if (retval
) goto Err_Exit
;
344 /* Free the root vnode.
345 Note that there's no need to vget() or vref() it before locking it here:
346 the ref. count has been maintained at +1 ever since mount time. */
348 retval
= vn_lock(root_vp
, LK_EXCLUSIVE
, p
);
349 if (retval
) goto Err_Exit
;
350 if (root_vp
->v_usecount
> 1) {
351 DBG_VOP(("VOLFS ERROR: root vnode = %x, usecount = %d\n", (int)root_vp
, priv_data
->volfs_rootvp
->v_usecount
));
352 VOP_UNLOCK(root_vp
, 0, p
);
357 priv_data
->volfs_rootvp
= NULL
;
358 vput(root_vp
); /* This drops volfs's own refcount */
362 /* All vnodes should be gone, and no errors, clean up the last */
363 /* XXX DBG_ASSERT(mp->mnt_vnodelist.lh_first == NULL); */
364 /* XXX DBG_ASSERT(retval == 0); */
367 FREE(priv_data
, M_VOLFSMNT
);
375 * Get file system statistics.
378 volfs_statfs(mp
, sbp
, p
)
380 register struct statfs
*sbp
;
383 DBG_VOP(("volfs_statfs called\n"));
386 sbp
->f_blocks
= 1024; // lies, darn lies and virtual file systems
387 sbp
->f_bfree
= 0; // Nope, can't write here!
389 sbp
->f_files
= 0; // Hmmm...maybe later
395 * volfs doesn't have any data and you can't write into any of the volfs
396 * structures, so don't do anything
399 volfs_sync(mp
, waitfor
, cred
, p
)
405 // DBG_VOP(("volfs_sync called\n"));
409 * Look up a FFS dinode number to find its incore vnode, otherwise read it
410 * in from disk. If it is in core, wait for the lock bit to clear, then
411 * return the inode locked. Detection and handling of mount points must be
412 * done by the calling routine.
415 volfs_vget(mp
, ino
, vpp
)
420 // DBG_VOP(("volfs_vget called\n"));
424 * File handle to vnode
426 * Have to be really careful about stale file handles:
427 * - check that the inode number is valid
428 * - call ffs_vget() to get the locked inode
429 * - check for an unallocated inode (i_mode == 0)
430 * - check that the given client host has export rights and return
431 * those rights via. exflagsp and credanonp
434 volfs_fhtovp(mp
, fhp
, nam
, vpp
, exflagsp
, credanonp
)
435 register struct mount
*mp
;
440 struct ucred
**credanonp
;
442 DBG_VOP(("volfs_fhtovp called\n"));
446 * Vnode pointer to File handle
450 volfs_vptofh(vp
, fhp
)
454 DBG_VOP(("volfs_vptofh called\n"));
458 * Initialize the filesystem
462 struct vfsconf
*vfsp
;
464 DBG_VOP(("volfs_init called\n"));
469 * fast filesystem related variables.
472 volfs_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
, p
)
481 DBG_VOP(("volfs_sysctl called\n"));