2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <miscfs/mockfs/mockfs.h>
30 #include <miscfs/mockfs/mockfs_fsnode.h>
31 #include <miscfs/mockfs/mockfs_vnops.h>
32 #include <miscfs/specfs/specdev.h>
34 #include <sys/mount_internal.h>
35 #include <sys/ubc_internal.h>
36 #include <sys/vnode_internal.h>
37 #include <vm/vm_protos.h>
39 #include <libkern/libkern.h>
42 * For the moment, most operations that change the fsnode will be called only in the context of
43 * mockfs_mountroot, so they should not need to use a mutex. The exceptions are mockfs_fsnode_vnode,
44 * and mockfs_fsnode_drop_vnode, which will use a tree-wide mutex (that lives in the mockfs_mount_t
47 * mockfs_fsnode_child_by_type doesn't require locking right now (we're only looking at the structure of
48 * the node tree, which should not change during VNOP operations.
51 /* mockfs_fsnode_create:
52 * Given a mount (mp) and mockfs node type (type), creates a new fsnode for that mountpoint (*fsnpp).
53 * For the moment (while type == fileid) we should have at most one node of any given type.
55 * Returns 0 on success, or an error.
57 int mockfs_fsnode_create(mount_t mp
, uint8_t type
, mockfs_fsnode_t
* fsnpp
)
77 * For a regular file, size is meaningful, but it will always be equal to the
78 * size of the backing device.
80 new_size
= mp
->mnt_devvp
->v_specinfo
->si_devsize
;
87 MALLOC(*fsnpp
, typeof(*fsnpp
), sizeof(**fsnpp
), M_TEMP
, M_WAITOK
| M_ZERO
);
94 (*fsnpp
)->size
= new_size
;
95 (*fsnpp
)->type
= type
;
103 * mockfs_fsnode_destroy:
104 * Given a node (fsnp), tears down and deallocates that node and the entire subtree that it is the
105 * root of (deallocates you, and your children, and your children's children! ...for three months).
107 * Returns 0 on success, or an error.
109 int mockfs_fsnode_destroy(mockfs_fsnode_t fsnp
)
116 * We will not destroy a root node that is actively pointed to by the mount structure; the
117 * mount must drop the reference to the mockfs tree before we can deallocate it.
119 if (!fsnp
|| (((mockfs_mount_t
)fsnp
->mnt
->mnt_data
)->mockfs_root
== fsnp
)) {
125 * For now, panic in this case; I don't expect anyone to ask us to destroy a node with a live
126 * vfs reference, but this will tell me if that assumption is untrue.
129 panic("mockfs_fsnode_destroy called on node with live vnode; fsnp = %p (in case gdb is screwing with you)", fsnp
);
132 * If this node has children, we need to destroy them.
134 * At least for now, we aren't guaranteeing destroy will be clean; we may get partway through
135 * and encounter an error, in which case we will panic (we may still have a sane tree, but
136 * we've failed to destroy the subtree, which means someone called destroy when they should
140 if ((rvalue
= mockfs_fsnode_destroy(fsnp
->child_a
)))
141 panic("mockfs_fsnode_destroy failed on child_a; fsnp = %p (in case gdb is screwing with you), rvalue = %d", fsnp
, rvalue
);
144 if ((rvalue
= mockfs_fsnode_destroy(fsnp
->child_b
)))
145 panic("mockfs_fsnode_destroy failed on child_b; fsnp = %p (in case gdb is screwing with you), rvalue = %d", fsnp
, rvalue
);
148 * We need to orphan this node before we destroy it.
151 if ((rvalue
= mockfs_fsnode_orphan(fsnp
)))
152 panic("mockfs_fsnode_orphan failed during destroy; fsnp = %p (in case gdb is screwing with you), rvalue = %d", fsnp
, rvalue
);
160 * mockfs_fsnode_adopt:
161 * Given two nodes (parent, child), makes one node the child of the other node.
163 * Returns 0 on success, or an error.
165 int mockfs_fsnode_adopt(mockfs_fsnode_t parent
, mockfs_fsnode_t child
)
172 * The child must be an orphan, and the parent cannot be the child.
174 if ((!parent
|| !child
|| child
->parent
) && (parent
!= child
)) {
180 * Nodes are actually tied to a specific mount, so assert that both nodes belong to the same mount.
182 if (parent
->mnt
!= child
->mnt
) {
188 * TODO: Get rid of this check if I ever get around to making the tree non-binary.
189 * TODO: Enforce that the parent cannot have two children of the same type (for the moment, this is
190 * implicit in the structure of the tree constructed by mockfs_mountroot, so we don't need to
193 * Can the parent support another child (food, shelter, unused pointers)?
195 if (!parent
->child_a
) {
196 parent
->child_a
= child
;
197 child
->parent
= parent
;
199 else if (!parent
->child_b
) {
200 parent
->child_b
= child
;
201 child
->parent
= parent
;
212 * mockfs_fsnode_orphan:
214 * Returns 0 on success, or an error.
216 int mockfs_fsnode_orphan(mockfs_fsnode_t fsnp
)
219 mockfs_fsnode_t parent
;
223 if (!fsnp
|| !fsnp
->parent
) {
229 * Disallow orphaning a node with a live vnode for now.
232 panic("mockfs_fsnode_orphan called on node with live vnode; fsnp = %p (in case gdb is screwing with you)", fsnp
);
234 parent
= fsnp
->parent
;
236 if (parent
->child_a
== fsnp
) {
237 parent
->child_a
= NULL
;
240 else if (parent
->child_b
== fsnp
) {
241 parent
->child_b
= NULL
;
245 panic("mockfs_fsnode_orphan insanity, fsnp->parent != parent->child; fsnp = %p (in case gdb is screwing with you)", fsnp
);
252 * mockfs_fsnode_child_by_type:
253 * Given a node (parent) and a type (type), returns the first child (*child) found corresponding to the
254 * requested type. This method exists to support lookup (which is responsible for mapping names, which
255 * we have no conception of currently, onto vnodes).
257 * This should be safe, as we are walking the read-only parts of the filesystem structure (not touching
260 * Returns 0 on success, or an error.
262 int mockfs_fsnode_child_by_type(mockfs_fsnode_t parent
, uint8_t type
, mockfs_fsnode_t
* child
)
268 if (!parent
|| !child
) {
273 if ((parent
->child_a
) && (parent
->child_a
->type
== type
))
274 *child
= parent
->child_a
;
275 else if ((parent
->child_b
) && (parent
->child_b
->type
== type
))
276 *child
= parent
->child_b
;
285 * mockfs_fsnode_vnode:
286 * Given a mockfs node (fsnp), returns a vnode (*vpp) corresponding to the mockfs node; the vnode will
287 * have an iocount on it.
289 * Returns 0 on success, or an error.
291 int mockfs_fsnode_vnode(mockfs_fsnode_t fsnp
, vnode_t
* vpp
)
294 memory_object_control_t ubc_mem_object
;
295 mockfs_mount_t mockfs_mnt
;
296 struct vnode_fsparam vnfs_param
;
298 if ((!fsnp
) || (!vpp
)) {
303 mockfs_mnt
= ((mockfs_mount_t
) fsnp
->mnt
->mnt_data
);
304 lck_mtx_lock(&mockfs_mnt
->mockfs_mnt_mtx
);
308 * The vnode already exists; this should be easy.
310 rvalue
= vnode_get(fsnp
->vp
);
317 * We need to create the vnode; this will be unpleasant.
319 vnfs_param
.vnfs_mp
= fsnp
->mnt
;
320 vnfs_param
.vnfs_vtype
= (fsnp
->type
== MOCKFS_FILE
) ? VREG
: VDIR
;
321 vnfs_param
.vnfs_str
= "mockfs";
322 vnfs_param
.vnfs_dvp
= (fsnp
->type
== MOCKFS_ROOT
) ? NULL
: fsnp
->parent
->vp
;
323 vnfs_param
.vnfs_fsnode
= fsnp
;
324 vnfs_param
.vnfs_vops
= mockfs_vnodeop_p
;
325 vnfs_param
.vnfs_markroot
= (fsnp
->type
== MOCKFS_ROOT
);
326 vnfs_param
.vnfs_marksystem
= 0;
327 vnfs_param
.vnfs_rdev
= 0;
328 vnfs_param
.vnfs_filesize
= fsnp
->size
;
329 vnfs_param
.vnfs_cnp
= NULL
;
330 vnfs_param
.vnfs_flags
= VNFS_CANTCACHE
| VNFS_NOCACHE
;
331 rvalue
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vnfs_param
, &fsnp
->vp
);
333 if ((!rvalue
) && (fsnp
->type
== MOCKFS_FILE
) && (mockfs_mnt
->mockfs_memory_backed
)) {
335 * We're memory backed; point the pager towards the backing store of the device.
337 ubc_mem_object
= ubc_getobject(fsnp
->vp
, 0);
340 panic("mockfs_fsvnode failed to get ubc_mem_object for a new vnode");
342 rvalue
= pager_map_to_phys_contiguous(ubc_mem_object
, 0, (mockfs_mnt
->mockfs_memdev_base
<< PAGE_SHIFT
), fsnp
->size
);
345 panic("mockfs_fsnode_vnode failed to create fictitious pages for a memory-backed device; rvalue = %d", rvalue
);
352 lck_mtx_unlock(&mockfs_mnt
->mockfs_mnt_mtx
);
359 * mockfs_fsnode_vnode:
360 * Given a mockfs node (fsnp) that has a vnode associated with it, causes them to drop their
361 * references to each other. This exists to support mockfs_reclaim. This method will grab the tree
362 * mutex, as this will mutate the tree.
364 * Returns 0 on success, or an error.
366 int mockfs_fsnode_drop_vnode(mockfs_fsnode_t fsnp
)
369 mockfs_mount_t mockfs_mnt
;
379 mockfs_mnt
= ((mockfs_mount_t
) fsnp
->mnt
->mnt_data
);
380 lck_mtx_lock(&mockfs_mnt
->mockfs_mnt_mtx
);
383 panic("mock_fsnode_drop_vnode: target fsnode does not have an associated vnode");
388 vnode_clearfsnode(vp
);
390 lck_mtx_unlock(&mockfs_mnt
->mockfs_mnt_mtx
);