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_vnops.h>
32 #include <sys/mount_internal.h>
33 #include <sys/vnode_internal.h>
34 #include <vfs/vfs_support.h>
36 #include <libkern/libkern.h>
37 #include <kern/debug.h>
40 * VOPFUNC macro; why do we have so many distinct definitions of this?
42 #define VOPFUNC int (*)(void *)
45 * VNOP functions that mockfs implements. See xnu/bsd/sys/vnode_if.h for information on what
46 * each function does in generic terms.
48 int mockfs_lookup(struct vnop_lookup_args
* ap
);
49 int mockfs_getattr(struct vnop_getattr_args
* ap
);
50 int mockfs_read(struct vnop_read_args
* ap
);
51 int mockfs_strategy(struct vnop_strategy_args
* ap
);
52 int mockfs_pagein(struct vnop_pagein_args
* ap
);
53 int mockfs_reclaim(__unused
struct vnop_reclaim_args
* ap
);
54 int mockfs_blockmap(struct vnop_blockmap_args
* ap
);
57 * struct vnop_lookup_args {
58 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
59 * vnode_t a_dvp; // vnode for the directory we are performing the lookup in
60 * vnode_t *a_vpp; // Return parameter: the vnode we matched the lookup to
61 * struct componentname *a_cnp; // Description of the file we are looking for
62 * vfs_context_t a_context; // We don't care about this (for now)
66 * Given a vnode for a directory (a_dvp) and a file description (a_cnp), looks for a file matching
67 * the description in the directory, and give a vnode with an iocount for the file (*a_vpp), if the
68 * file was found. For mockfs, because we realistically have 3 vnodes, the filesystem information
69 * is extremely sparse, so the details on naming are all implemented in mockfs_lookup; the generic VFS
70 * information is enough for us to distinguish between all 3 files. Any lookup not done in the root
71 * vnode fails, by definition. Each vnode has the following names in relation to the root vnode:
79 * The executable vnode
82 * Returns 0 on success, or an error.
84 int mockfs_lookup(struct vnop_lookup_args
* ap
)
89 mockfs_fsnode_t fsnode
;
90 mockfs_fsnode_t target_fsnode
;
94 struct componentname
* cnp
;
101 op
= cnp
->cn_nameiop
;
102 fsnode
= (mockfs_fsnode_t
) dvp
->v_data
;
103 target_fsnode
= NULL
;
105 if ((op
== LOOKUP
) && (fsnode
->type
== MOCKFS_ROOT
)) {
107 * Okay, we're looking in the root directory, so we aren't necessarily
108 * going to fail. What are we looking for?
111 held_char
= cnp
->cn_nameptr
[cnp
->cn_namelen
];
112 cnp
->cn_nameptr
[cnp
->cn_namelen
] = '\0';
115 * We'll resolve sbin to /, and launchd to the executable for the moment, so that I don't
116 * accidentally commit a change to the init_process pathname. We map from name to node type
117 * here, as mockfs doesn't current use names; just unique types.
119 if (!strncmp(cnp
->cn_nameptr
, "sbin", 5))
120 target_fsnode
= fsnode
;
121 else if (!strncmp(cnp
->cn_nameptr
, "dev", 4))
122 mockfs_fsnode_child_by_type(fsnode
, MOCKFS_DEV
, &target_fsnode
);
123 else if (!strncmp(cnp
->cn_nameptr
, "launchd", 8))
124 mockfs_fsnode_child_by_type(fsnode
, MOCKFS_FILE
, &target_fsnode
);
128 cnp
->cn_nameptr
[cnp
->cn_namelen
] = held_char
;
131 rvalue
= mockfs_fsnode_vnode(target_fsnode
, vpp
);
135 * We aren't looking in root; the query may actually be reasonable, but we're not
136 * going to support it.
145 * struct vnop_getattr_args {
146 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
147 * vnode_t a_vp; // Pointer to the vnode we are interested in
148 * struct vnode_attr *a_vap; // Details the requested attributes, and used to return attributes
149 * vfs_context_t a_context; // We don't care about this (for now)
153 * Given a vnode (a_vp), returns the attributes requested for that vnode (*a_vap). For mockfs, we don't care
154 * about the majority of attributes (we are not a fully featured filesystem). We will return a minimal set of
155 * attributes for any request, regardless of which attributes were requested, to ensure that we look like a sane
156 * file, and so that permissions are set appropriately to allow execution of the executable vnode.
158 * Returns 0 on success, or an error.
160 int mockfs_getattr(struct vnop_getattr_args
* ap
)
163 * For the moment, we don't actually care about most attributes. We'll
164 * deal with actually managing attributes as part of the general cleanup.
167 mockfs_fsnode_t fsnode
;
168 struct vnode_attr
* vap
;
171 fsnode
= (mockfs_fsnode_t
)vp
->v_data
;
173 bzero(vap
, sizeof(*vap
));
174 VATTR_RETURN(vap
, va_nlink
, 1); /* Simply assert that someone has at least one link to us */
175 VATTR_RETURN(vap
, va_mode
, VREAD
| VWRITE
| VEXEC
);
176 VATTR_RETURN(vap
, va_fileid
, fsnode
->type
);
177 VATTR_RETURN(vap
, va_total_size
, fsnode
->size
);
178 VATTR_RETURN(vap
, va_total_alloc
, fsnode
->size
);
179 VATTR_RETURN(vap
, va_data_size
, fsnode
->size
);
180 VATTR_RETURN(vap
, va_data_alloc
, fsnode
->size
);
186 * struct vnop_read_args {
187 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
188 * vnode_t a_vp; // Pointer to the vnode we are interested in
189 * struct uio *a_uio; // Description of the request
190 * int a_ioflag; // IO flags (we don't care about these)
191 * vfs_context_t a_context; // We don't care about this (for now)
195 * Given a vnode (a_vp), a set of flags (a_ioflag), and a description of a read request (a_uio), executes the read
196 * request and returns the resulting data through the description (a_uio). mockfs has very little to do here; we
197 * merely mandate that any read attempt MUST be on VREG (our MOCKFS_FILE object), as it is the only vnode that has
198 * a backing store that can support a read (the other node types being purely in-memory hacks). Because we do not
199 * support VNOP_OPEN, we can probably assume that the kernel is the only entity that will ever issue a VNOP_READ
200 * (as part of the exec path) to a mockfs vnode.
202 * Returns 0 on success, or an error.
204 int mockfs_read(struct vnop_read_args
* ap
)
208 mockfs_fsnode_t fsnode
;
211 fsnode
= (mockfs_fsnode_t
) vp
->v_data
;
214 * We're just an ugly frontend for the devnode, so we shouldn't need to do much for reads;
215 * pass the work to cluster_read.
217 if (vp
->v_type
== VREG
) {
218 rvalue
= cluster_read(vp
, ap
->a_uio
, fsnode
->size
, ap
->a_ioflag
);
222 * You've tried to read from a nonregular file; I hate you.
231 * struct vnop_reclaim_args {
232 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
233 * vnode_t a_vp; // Pointer to the vnode we are reclaiming
234 * vfs_context_t a_context; // We don't care about this (for now)
238 * Given a vnode (a_vp), performs any cleanup needed to allow VFS to reclaim the vnode. Because the mockfs tree
239 * is always in memory, we have very little to do as part of reclaim, so we'll just zero a few pointers and let
240 * VFS reclaim the vnode.
242 int mockfs_reclaim(struct vnop_reclaim_args
* ap
)
246 mockfs_fsnode_t fsnode
;
249 fsnode
= (mockfs_fsnode_t
) vnode_fsnode(vp
);
250 rvalue
= mockfs_fsnode_drop_vnode(fsnode
);
256 * struct vnop_strategy_args {
257 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
258 * struct buf *a_bp; // Description of the desired IO
262 * Given an IO description (a_bp), does any preparations required by the filesystem, and then passes the IO off to
263 * the appropriate device. mockfs doesn't need to do anything to prepare for the IO, so we simply pass it off to
264 * our backing device.
266 * Returns 0 on success, or an error.
268 int mockfs_strategy(struct vnop_strategy_args
* ap
)
274 * We'll avoid checking for a memory-backed device here; we already do this for blockmap, which will be
275 * called as part of the IO path.
278 dvp
= vfs_devvp(buf_vnode(ap
->a_bp
)->v_mount
);
281 rvalue
= buf_strategy(dvp
, ap
);
286 * I'm not certain this is the BEST error to return for this case.
295 * struct vnop_pagein_args {
296 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
297 * vnode_t a_vp; // Pointer to the vnode we are interested in
298 * upl_t a_pl; // Describes the pages that need to be paged in
299 * upl_offset_t a_pl_offset; // Offset in the UPL to start placing data at
300 * off_t a_f_offset; // File offset to begin paging in at
301 * size_t a_size; // Bytes of data to page in
302 * int a_flags; // UPL flags (we don't care about these)
303 * vfs_context_t a_context; // We don't care about this (for now)
307 * Given a vnode (a_vp), and a region, described by an offset (a_f_offset) and a size (a_size), pages the region
308 * into the given UPL (a_pl), starting at the UPL offset (a_pl_offset). For mockfs, we don't have anything significant
309 * to do for pagein, so we largely serve as a wrapper to the cluster_pagein routine.
311 * Returns 0 on success, or an error.
313 int mockfs_pagein(struct vnop_pagein_args
* ap
)
315 mockfs_fsnode_t fsnode
;
316 mockfs_mount_t mockfs_mnt
;
319 * Nothing special needed from us; just nab the filesize and kick the work over to cluster_pagein.
321 fsnode
= (mockfs_fsnode_t
) ap
->a_vp
->v_data
;
322 mockfs_mnt
= ((mockfs_mount_t
) fsnode
->mnt
->mnt_data
);
325 * If we represent a memory backed device, we should be pointing directly to the backing store; we should never
326 * see a pagein in this case.
328 if (mockfs_mnt
->mockfs_memory_backed
)
329 panic("mockfs_pagein called for a memory-backed device");
331 return cluster_pagein(ap
->a_vp
, ap
->a_pl
, ap
->a_pl_offset
, ap
->a_f_offset
, ap
->a_size
, fsnode
->size
, ap
->a_flags
);
335 * struct vnop_blockmap_args {
336 * struct vnodeop_desc *a_desc; // We don't care about this (for now)
337 * vnode_t a_vp; // Pointer to the vnode we are interested in
338 * off_t a_foffset; // File offset we are interested in
339 * size_t a_size; // Size of the region we are interested in
340 * daddr64_t *a_bpn; // Return parameter: physical block number the region we are interest in starts at
341 * size_t *a_run; // Return parameter: number of contiguous bytes of data
342 * void *a_poff; // Unused, as far as I know
343 * int a_flags; // Used to distinguish reads and writes; we don't care
344 * vfs_context_t a_context; // We don't care about this (for now)
348 * Given a vnode (a_vp), and a region, described by an offset (a_foffset), and a size (a_size), tells the caller
349 * which physical block (on the backing device) the region begins at (*a_bpn), and how many bytes can be read
350 * before the first discontinuity (*a_run). For mockfs, because only VREG files are eligible for IO, and because
351 * all VREG files are simply a frontend for the backing device, this mapping will always be one to one, and all we
352 * need to do is convert the physical offset to the physical block number.
354 * Returns 0 on success, or an error.
356 int mockfs_blockmap(struct vnop_blockmap_args
* ap
)
364 mockfs_fsnode_t fsnode
;
367 foffset
= ap
->a_foffset
;
371 fsnode
= (mockfs_fsnode_t
) vp
->v_data
;
372 blksize
= vp
->v_mount
->mnt_devblocksize
;
375 * If we represent a memory backed device, we should be pointing directly to the backing store; all IO should
376 * be satisfied from the UBC, and any called to blockmap (inidicating an attempted IO to the backing store)
377 * is therefore disallowed.
379 if (((mockfs_mount_t
) fsnode
->mnt
->mnt_data
)->mockfs_memory_backed
)
380 printf("mockfs_blockmap called for a memory-backed device\n");
383 * This will ultimately be simple; the vnode must be VREG (init), and the mapping will be 1 to 1.
384 * This also means that their request should always be contiguous, so the run calculation is easy!
386 if (vp
->v_type
== VREG
) {
387 *bpn
= foffset
/ blksize
;
388 *run
= fsnode
->size
- foffset
;
390 if (ap
->a_size
> *run
) {
391 /* We've been asked for more data than the backing device can provide; we're done. */
392 panic("mockfs_blockmap was asked for a region that extended past the end of the backing device");
402 int (**mockfs_vnodeop_p
)(void *);
403 struct vnodeopv_entry_desc mockfs_vnodeop_entries
[] = {
404 { &vnop_default_desc
, (VOPFUNC
) vn_default_error
}, /* default */
405 { &vnop_lookup_desc
, (VOPFUNC
) mockfs_lookup
}, /* lookup */
406 { &vnop_create_desc
, (VOPFUNC
) err_create
},/* create */
407 { &vnop_open_desc
, (VOPFUNC
) err_open
}, /* open */
408 { &vnop_mknod_desc
, (VOPFUNC
) err_mknod
}, /* mknod */
409 { &vnop_close_desc
, (VOPFUNC
) err_close
}, /* close */
410 { &vnop_access_desc
, (VOPFUNC
) err_access
}, /* access */
411 { &vnop_getattr_desc
, (VOPFUNC
) mockfs_getattr
}, /* getattr */
412 { &vnop_setattr_desc
, (VOPFUNC
) err_setattr
}, /* setattr */
413 { &vnop_read_desc
, (VOPFUNC
) mockfs_read
}, /* read */
414 { &vnop_write_desc
, (VOPFUNC
) err_write
}, /* write */
415 { &vnop_ioctl_desc
, (VOPFUNC
) err_ioctl
}, /* ioctl */
416 { &vnop_select_desc
, (VOPFUNC
) err_select
}, /* select */
417 { &vnop_mmap_desc
, (VOPFUNC
) err_mmap
}, /* mmap */
418 { &vnop_fsync_desc
, (VOPFUNC
) nop_fsync
}, /* fsync */
419 { &vnop_remove_desc
, (VOPFUNC
) err_remove
}, /* remove */
420 { &vnop_link_desc
, (VOPFUNC
) err_link
}, /* link */
421 { &vnop_rename_desc
, (VOPFUNC
) err_rename
}, /* rename */
422 { &vnop_mkdir_desc
, (VOPFUNC
) err_mkdir
}, /* mkdir */
423 { &vnop_rmdir_desc
, (VOPFUNC
) err_rmdir
}, /* rmdir */
424 { &vnop_symlink_desc
, (VOPFUNC
) err_symlink
}, /* symlink */
425 { &vnop_readdir_desc
, (VOPFUNC
) err_readdir
}, /* readdir */
426 { &vnop_readlink_desc
, (VOPFUNC
) err_readlink
}, /* readlink */
427 { &vnop_inactive_desc
, (VOPFUNC
) err_inactive
}, /* inactive */
428 { &vnop_reclaim_desc
, (VOPFUNC
) mockfs_reclaim
}, /* reclaim */
429 { &vnop_strategy_desc
, (VOPFUNC
) mockfs_strategy
}, /* strategy */
430 { &vnop_pathconf_desc
, (VOPFUNC
) err_pathconf
}, /* pathconf */
431 { &vnop_advlock_desc
, (VOPFUNC
) err_advlock
}, /* advlock */
432 { &vnop_bwrite_desc
, (VOPFUNC
) err_bwrite
}, /* bwrite */
433 { &vnop_pagein_desc
, (VOPFUNC
) mockfs_pagein
}, /* pagein */
434 { &vnop_pageout_desc
, (VOPFUNC
) err_pageout
}, /* pageout */
435 { &vnop_copyfile_desc
, (VOPFUNC
) err_copyfile
}, /* copyfile */
436 { &vnop_blktooff_desc
, (VOPFUNC
) err_blktooff
}, /* blktooff */
437 { &vnop_offtoblk_desc
, (VOPFUNC
) err_offtoblk
}, /* offtoblk */
438 { &vnop_blockmap_desc
, (VOPFUNC
) mockfs_blockmap
}, /* blockmap */
439 { (struct vnodeop_desc
*) NULL
, (VOPFUNC
) NULL
}
442 struct vnodeopv_desc mockfs_vnodeop_opv_desc
= {
444 mockfs_vnodeop_entries