]> git.saurik.com Git - apple/xnu.git/blame - bsd/miscfs/mockfs/mockfs_vfsops.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / miscfs / mockfs / mockfs_vfsops.c
CommitLineData
39236c6e
A
1/*
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
39236c6e
A
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.
0a7de745 14 *
39236c6e
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
39236c6e
A
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.
0a7de745 25 *
39236c6e
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <kern/assert.h>
30#include <kern/debug.h>
31#include <libkern/libkern.h>
32#include <miscfs/mockfs/mockfs.h>
33#include <miscfs/mockfs/mockfs_fsnode.h>
34#include <miscfs/mockfs/mockfs_vnops.h>
35#include <miscfs/specfs/specdev.h>
36#include <sys/disk.h>
37#include <sys/errno.h>
38#include <sys/malloc.h>
39#include <sys/mount_internal.h>
40#include <sys/vnode_internal.h>
41
42lck_attr_t * mockfs_mtx_attr = (lck_attr_t *) 0;
43lck_grp_attr_t * mockfs_grp_attr = (lck_grp_attr_t *) 0;
44lck_grp_t * mockfs_mtx_grp = (lck_grp_t *) 0;
45
46int mockfs_mountroot(mount_t mp, vnode_t rvp, __unused vfs_context_t ctx);
47
48/*
49 * Functions that are part of the mockfs_vfsops structure.
50 */
51int mockfs_unmount(__unused struct mount *mp, __unused int mntflags, __unused vfs_context_t ctx);
52int mockfs_root(mount_t mp, vnode_t * vpp, __unused vfs_context_t ctx);
53int mockfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t ctx);
54int mockfs_init(__unused struct vfsconf * vfsc);
55
56/*
57 * mockfs_mountroot:
58 * Given a mount (mp) and a vnode for the root device (rvp), builds a fake filesystem for rvp. This consists
59 * of three nodes; a directory node (to serve as a mountpoint for devfs), a file node meant to serve as an
60 * executable frontend for rootvp (we will assume that rootvp is an executable, that the kernel can subsequently
61 * run), and the root node for the mockfs filesystem. The structure of mockfs is memory-backed; only the
62 * contents of the file node refer to the backing device.
63 *
64 * Returns 0 on success, or an error.
65 */
0a7de745
A
66int
67mockfs_mountroot(mount_t mp, vnode_t rvp, __unused vfs_context_t ctx)
39236c6e
A
68{
69 int rvalue = 0;
70 mockfs_fsnode_t root_fsnode = NULL;
71 mockfs_fsnode_t dev_fsnode = NULL;
72 mockfs_fsnode_t file_fsnode = NULL;
73 mockfs_mount_t mockfs_mount_data = NULL;
74 dk_memdev_info_t memdev_info;
75
76 /*
77 * TODO: Validate that the device at least LOOKS like a mach-o (has a sane header); this would prevent us
78 * from causing EBADMACHO panics further along the boot path.
79 */
80
81 /*
82 * There are no M_MOCKFS* definitions at the moment, just use M_TEMP.
83 */
84
85 MALLOC(mockfs_mount_data, mockfs_mount_t, sizeof(*mockfs_mount_data), M_TEMP, M_WAITOK | M_ZERO);
86 mockfs_fsnode_create(mp, MOCKFS_ROOT, &root_fsnode);
87 mockfs_fsnode_create(mp, MOCKFS_DEV, &dev_fsnode);
88 mockfs_fsnode_create(mp, MOCKFS_FILE, &file_fsnode);
89
90 if (!mockfs_mount_data || !root_fsnode || !dev_fsnode || !file_fsnode) {
91 rvalue = ENOMEM;
92 goto done;
93 }
94
95 /*
96 * If rvp is a memory device (with a few caveats), we can point to the same physical memory as the device
97 * and avoid pointless paging/copying; query the device node for the information we need to determine
98 * if we can do this.
99 */
100 bzero(&memdev_info, sizeof(memdev_info));
101
102 if (!VNOP_IOCTL(rvp, DKIOCGETMEMDEVINFO, (caddr_t)&memdev_info, 0, NULL)) {
103 /*
104 * For the moment, we won't try to optimize when mi_phys is true.
105 */
106 if (!mockfs_mount_data->mockfs_physical_memory) {
107 mockfs_mount_data->mockfs_memory_backed = memdev_info.mi_mdev;
108 mockfs_mount_data->mockfs_physical_memory = memdev_info.mi_phys;
109 mockfs_mount_data->mockfs_memdev_base = memdev_info.mi_base;
110 mockfs_mount_data->mockfs_memdev_size = memdev_info.mi_size;
111 }
112 }
113
114 lck_mtx_init(&mockfs_mount_data->mockfs_mnt_mtx, mockfs_mtx_grp, mockfs_mtx_attr);
115
116 /*
117 * All of the needed nodes/structures have been set up; now we just need to establish the relationships
118 * between the various mockfs nodes.
119 */
0a7de745 120 if ((rvalue = mockfs_fsnode_adopt(root_fsnode, dev_fsnode))) {
39236c6e 121 goto done;
0a7de745 122 }
39236c6e 123
0a7de745 124 if ((rvalue = mockfs_fsnode_adopt(root_fsnode, file_fsnode))) {
39236c6e 125 goto done;
0a7de745 126 }
39236c6e
A
127
128 mockfs_mount_data->mockfs_root = root_fsnode;
0a7de745 129 mp->mnt_data = (typeof(mp->mnt_data))mockfs_mount_data;
39236c6e
A
130
131done:
132 if (rvalue) {
0a7de745 133 if (file_fsnode) {
39236c6e 134 mockfs_fsnode_destroy(file_fsnode);
0a7de745
A
135 }
136 if (dev_fsnode) {
39236c6e 137 mockfs_fsnode_destroy(dev_fsnode);
0a7de745
A
138 }
139 if (root_fsnode) {
39236c6e 140 mockfs_fsnode_destroy(root_fsnode);
0a7de745 141 }
39236c6e
A
142 if (mockfs_mount_data) {
143 lck_mtx_destroy(&mockfs_mount_data->mockfs_mnt_mtx, mockfs_mtx_grp);
144 FREE(mockfs_mount_data, M_TEMP);
145 }
146 }
147
148 return rvalue;
149}
150
151/*
152 * mockfs_unmount:
153 * Given a mount (mp), and associated flags (mntflags), performs the necessary teardown to destroy the mount.
154 *
155 * Returns 0 on success, or an error.
156 */
0a7de745
A
157int
158mockfs_unmount(struct mount *mp, int mntflags, __unused vfs_context_t ctx)
39236c6e
A
159{
160 int rvalue;
161 int vflush_flags;
162 mockfs_fsnode_t root_fsnode;
163 mockfs_mount_t mockfs_mnt;
164
165 vflush_flags = 0;
166 mockfs_mnt = (mockfs_mount_t) mp->mnt_data;
167
168 /*
169 * Reclaim the vnodes for the mount (forcibly, if requested; given that mockfs only support mountroot
0a7de745 170 * at the moment, this should ALWAYS be forced),
39236c6e
A
171 */
172 if (mntflags & MNT_FORCE) {
173 vflush_flags |= FORCECLOSE;
174 }
175
176 rvalue = vflush(mp, NULL, vflush_flags);
177
0a7de745 178 if (rvalue) {
39236c6e 179 return rvalue;
0a7de745 180 }
39236c6e
A
181
182 /*
183 * Past this point, errors are likely to be unrecoverable, so panic if we're given any excuse; we
184 * need to teardown the mockfs_mnt data now, so that VFS can cleanup the mount structure. Note
185 * that clearing mockfs_root before destroying the fsnode tree is related to an implementation
186 * detail of mockfs_fsnode_destroy (which will refuse to destroy the root node).
187 */
188 root_fsnode = mockfs_mnt->mockfs_root;
189 mockfs_mnt->mockfs_root = NULL;
190 rvalue = mockfs_fsnode_destroy(root_fsnode);
191
0a7de745 192 if (rvalue) {
39236c6e 193 panic("mockfs_unmount: Failed to destroy the fsnode tree");
0a7de745 194 }
39236c6e
A
195
196 lck_mtx_destroy(&mockfs_mnt->mockfs_mnt_mtx, mockfs_mtx_grp);
197 FREE(mockfs_mnt, M_TEMP);
198 mp->mnt_data = NULL;
199
200 return rvalue;
201}
202
203/*
204 * mockfs_root:
205 * Given a mount (mp), returns the root vnode (*vpp) for that mount with an iocount.
206 *
207 * Returns 0 on success, or an error.
208 */
0a7de745
A
209int
210mockfs_root(mount_t mp, vnode_t * vpp, __unused vfs_context_t ctx)
39236c6e
A
211{
212 int rvalue;
213
214 rvalue = mockfs_fsnode_vnode(((mockfs_mount_t) mp->mnt_data)->mockfs_root, vpp);
215 return rvalue;
216}
217
218/*
219 * mockfs_sync:
220 * Returns success because we're a read-only filesystem.
221 *
222 * Returns 0.
223 */
0a7de745
A
224int
225mockfs_sync(__unused struct mount *mp, __unused int waitfor, __unused vfs_context_t ctx)
39236c6e 226{
0a7de745 227 return 0;
39236c6e
A
228}
229
230/*
231 * mockfs_init:
232 * Run once (during VFS initialization); takes care of generic mockfs initialization (which for now, means
233 * global lock information).
234 *
235 * Returns 0 on success, or an error.
236 */
0a7de745
A
237int
238mockfs_init(__unused struct vfsconf * vfsc)
39236c6e
A
239{
240 mockfs_mtx_attr = lck_attr_alloc_init();
241 mockfs_grp_attr = lck_grp_attr_alloc_init();
242 mockfs_mtx_grp = lck_grp_alloc_init("mockfs-mutex", mockfs_grp_attr);
243
244 /*
245 * If we've failed to allocate this early in boot, something is horrendously wrong; it should be fine to
246 * panic (for now).
247 */
248 if (!mockfs_mtx_attr || !mockfs_grp_attr || !mockfs_mtx_grp) {
249 panic("mockfs_init failed to allocate lock information");
250 }
251
0a7de745 252 return 0;
39236c6e
A
253}
254
255struct vfsops mockfs_vfsops = {
39037602
A
256 .vfs_unmount = mockfs_unmount,
257 .vfs_root = mockfs_root,
258 .vfs_sync = mockfs_sync,
259 .vfs_init = mockfs_init,
39236c6e 260};