forked from cellularmitosis/ADC-reference-library-2009-july
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting1.html
executable file
·2096 lines (1736 loc) · 79.5 KB
/
listing1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>EmptyFS - /EmptyFS.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Darwin/index.html">Darwin</a> > <a href="../../samplecode/Darwin/idxFileManagement-date.html">File Management</a> > <A HREF="javascript:location.replace('index.html');">EmptyFS</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">EmptyFS</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/EmptyFS.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/EmptyFS.c</option>
<option value="listing2.html">/EmptyFSMountArgs.h</option>
<option value="listing3.html">/MountEmptyFS.c</option>
<option value="listing4.html">/Read Me About EmptyFS.txt</option></select>
</p>
</form>
<p><strong><a href="EmptyFS.zip">Download Sample</a></strong> (“EmptyFS.zip”, 79.2K)<BR>
<strong><a href="EmptyFS.dmg">Download Sample</a></strong> (“EmptyFS.dmg”, 144.2K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: EmptyFS.c
Contains: A basic VFS plug-in example.
Written by: DTS
Copyright: Copyright (c) 2006 by Apple Computer, Inc., All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
$Log: EmptyFS.c,v $
Revision 1.4 2006/10/31 16:27:46
Updated some comments based on review feedback (and corrected the AssertKnownFlags in VFSOPStart).
Revision 1.3 2006/07/25 16:38:06
Disable all name caching. Added uiomove_atomic that checks to see whether we have enough space to copy out the entire dirent.
Revision 1.2 2006/07/25 16:27:07
Rolled in changes based on experience from MFSLives. Almost all of these were updated comments.
Revision 1.1 2006/07/04 14:03:52
First checked in.
*/
/////////////////////////////////////////////////////////////////////
#include "EmptyFSMountArgs.h"
#include <kern/assert.h>
#include <libkern/libkern.h>
#include <libkern/OSMalloc.h>
#include <libkern/locks.h>
#include <mach/mach_types.h>
#include <sys/errno.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/vnode_if.h>
#include <sys/kernel_types.h>
#include <sys/stat.h>
#include <sys/dirent.h>
#include <sys/proc.h>
#include <sys/fcntl.h>
/////////////////////////////////////////////////////////////////////
#pragma mark ***** Source Code Notes
/*
Bit Fields
----------
In places where I initialise a bit field, I include both the active bits
and the inactive bits (commented out). This lets you quickly see all of
the options that are available and the options that I've specifically enabled.
Terminology
-----------
Each volume is made up of a set of file system objects (fsobjs). These objects
are stored on disk (or in some other way, such as across the network). To speed
things up, the system caches information about these file system objects in
memory. The objects in this cache are called vnodes. The cache is managed by
the VFS layer and the VFS plug-in, working in concert.
This cache is /not/ the disk cache (in the traditional sense of the phrase).
A disk cache typically caches the contents of blocks on the disk. Here we're
referring to a cache of information about the file system objects on the volume.
Mac OS X does have a disk cache (called the Unified Buffer Cache, UBC), and this
example interacts with it when it needs to read directory blocks (using the
buf_meta_bread call) and when it reads files (using the cluster_read and
cluster_pagein calls).
A vnode is a virtual representation of a file system object. It's virtual in
the sense that it has no information about the concrete implementation of the
object on disk (or across the network). Rather, it's the handle which the
higher levels of the system use to learn about and manipulate a given file
system object. The only concrete information about the file system object
that stored in the vnode is a reference to the corresponding FSNode.
An FSNode is the in-memory representation of a file system object. An FSNode
is managed by the VFS plug-in, and contains all of the concrete information
needed to manage that file system object. For example, on HFS Plus the FSNode
would store the CNID of the file system object.
We don't use "inode" at all, for two reasons:
o Traditionally, the term "inode" has been used to describe both the
on-disk representation of a file system object /and/ the
in-memory representation of that object (if it's being cached in memory).
That's just confusing (-:
o The term "inode" implies a certain style of on-disk organisation, which is
not universally applicable (for an obvious example, consider a network
file system), and is certainly not applicable to MFS.
Traditionally there is a one-to-one correspondence between vnodes and FSNodes.
However, this not true in the presence of multi-fork files, where there is
one vnode for each fork but all of these refer to the same FSNode.
FSNode Hash
-----------
It's important to realise that the vnode cache is managed globally by the
VFS layer. The VFS plug-in is expected to following along with decisions
made by the VFS layer. However, vnodes are created by the VFS plug-ins,
as they respond to incoming requests.
The most common situation where a VFS plug-in needs to create a vnode is
in VNOPLookup. In this case, the plug-in has information about the file
system object in question (in this example, we have the file number) and
needs to create a vnode for to return as the result of the lookup.
The critical point is that the VFS plug-in MUST NOT create two vnodes
for the same file. Therefore the plug-in must maintain some data structure
that:
o can be accessed quickly based on the information in the file system
object's directory entry (that is, the file number)
o tells the VFS plug-in which file system objects are currently in memory
o can return the vnode, if any, associated with that FSNode
This is typically done using a hash table that indexes all of the FSNodes.
This is keyed by the file system object's raw device number (dev_t) and
inode number (file number in the case of MFS). Getting the mechanics of
this table right is the most difficult part of implementing a VFS plug-in.
In the case of EmptyFS, there can only be one possible vnode (the root
vnode) and thus we don't need a hash table. Rather, we store information
about the root vnode in the mount point itself. Also, we don't actually
need an FSNode data structure, because we don't need any state for our
file system objects.
*/
/////////////////////////////////////////////////////////////////////
#pragma mark ***** More Asserts
// We use the system assert macro (from <kern/assert.h>) for standard asserts.
// In some cases we also want to assert that an incoming 'flags' parameter
// has only the bits that we know about set. In this case we use the
// AssertKnownFlags macro. As getting an unknown flag is more of a warning
// than an error, we just print a message and continue execution.
#if MACH_ASSERT
static void AssertKnownFlagsCore(
uint64_t flags,
uint64_t knownFlags,
boolean_t * havePrintedPtr,
const char * fileStr,
int lineNumber,
const char * flagsStr,
const char * knownFlagsStr
)
// Core implementation of AssertKnownFlags.
{
// Check to see if we have any unknown flags.
if ( (flags & ~knownFlags) != 0 ) {
// If so, have we already printed a warning.
if ( (havePrintedPtr == NULL) || ! *havePrintedPtr ) {
// If not, print it.
printf("%s:%d: AssertKnownFlags(%s, %s) saw unknown flags 0x%llx.\n",
fileStr,
lineNumber,
flagsStr,
knownFlagsStr,
flags & ~knownFlags
);
}
// And record that we did.
if (havePrintedPtr != NULL) {
*havePrintedPtr = TRUE;
}
}
}
// In AssertKnownFlags macro, flags is the incoming flags and
// knownFlags is the set of all flags that we knew about when we
// wrote the code.
#define AssertKnownFlags(flags, knownFlags) \
do { \
static boolean_t sHavePrinted; \
AssertKnownFlagsCore((flags), (knownFlags), &sHavePrinted, __FILE__, __LINE__, # flags, # knownFlags); \
} while (0)
#else
#define AssertKnownFlags(flags, knownFlags) do { } while (0)
#endif
/////////////////////////////////////////////////////////////////////
#pragma mark ***** Error Conversion
static errno_t ErrnoFromKernReturn(kern_return_t kernErr)
// Maps a kern_return_t-style error into an errno_t-style error.
{
errno_t err;
if (kernErr == KERN_SUCCESS) {
err = 0;
} else {
err = EINVAL;
}
return err;
}
static kern_return_t KernReturnFromErrno(errno_t err)
// Maps an errno_t-style error into a kern_return_t-style error.
{
kern_return_t kernErr;
if (err == 0) {
kernErr = KERN_SUCCESS;
} else {
kernErr = KERN_FAILURE;
}
return err;
}
/////////////////////////////////////////////////////////////////////
#pragma mark ***** Memory and Locks
// gOSMallocTag is used for all of our allocations.
static OSMallocTag gOSMallocTag = NULL;
// gLockGroup is used for all of our locks.
static lck_grp_t * gLockGroup = NULL;
static void TermMemoryAndLocks(void)
// Disposes of gOSMallocTag and gLockGroup.
{
if (gLockGroup != NULL) {
lck_grp_free(gLockGroup);
gLockGroup = NULL;
}
if (gOSMallocTag != NULL) {
OSMalloc_Tagfree(gOSMallocTag);
gOSMallocTag = NULL;
}
}
static kern_return_t InitMemoryAndLocks(void)
// Initialises of gOSMallocTag and gLockGroup.
{
kern_return_t err;
err = KERN_SUCCESS;
gOSMallocTag = OSMalloc_Tagalloc("com.apple.dts.kext.EmptyFS", OSMT_DEFAULT);
if (gOSMallocTag == NULL) {
err = KERN_FAILURE;
}
if (err == KERN_SUCCESS) {
gLockGroup = lck_grp_alloc_init("com.apple.dts.kext.EmptyFS", LCK_GRP_ATTR_NULL);
if (gLockGroup == NULL) {
err = KERN_FAILURE;
}
}
// Clean up.
if (err != KERN_SUCCESS) {
TermMemoryAndLocks();
}
assert( (err == KERN_SUCCESS) == (gOSMallocTag != NULL) );
assert( (err == KERN_SUCCESS) == (gLockGroup != NULL) );
return err;
}
/////////////////////////////////////////////////////////////////////
#pragma mark ***** Core Data Structures
// gVNodeOperations is set up when we register the VFS plug-in with vfs_fsadd.
// It holds a pointer to the array of vnode operation functions for this
// VFS plug-in. We have to declare it early in this file because it's referenced
// by the code that creates vnodes.
static errno_t (**gVNodeOperations)(void *);
// EmptyFSMount holds the file system specific data that we need per mount point.
// We attach this to the kernel mount_t by calling vfs_setfsprivate in VFSOPMount.
// There is no reference count on this structure; it lives and dies along with the
// corresponding mount_t.
enum {
kEmptyFSMountMagic = 'MtMn',
kEmptyFSMountBadMagic = 'M!Mn'
};
struct EmptyFSMount {
uint32_t fMagic; // [1] must be kEmptyFSMountMagic
mount_t fMountPoint; // [1] back pointer to the mount_t
uint32_t fDebugLevel; // [1] [3] debug level from mount arguments
dev_t fBlockRDevNum; // [1] raw dev_t of the device we're mounted on
vnode_t fBlockDevVNode; // [1] a vnode for the above; we have a use count reference on this
char fVolumeName[30]; // [1] volume name (UTF-8)
struct vfs_attr fAttr; // [1] pre-calculate volume attributes
lck_mtx_t * fRootMutex; // [1] protects following fields
boolean_t fRootAttaching; // [2] true if someone is attaching a root vnode
boolean_t fRootWaiting; // [2] true if someone is waiting for such an attach to complete
vnode_t fRootVNode; // [2] the root vnode; we hold /no/ proper references to this,
// and must reconfirm its existance each time
};
typedef struct EmptyFSMount EmptyFSMount;
// Root VNode Notes
// ----------------
// In a typical VFS plug-in, the root vnode is accessed via the hash layer, exactly
// like any other vnode. In this trivial file system, I haven't implemented a hash
// layer (simply because I don't need it), thus I store the root vnode information
// in the mount point.
// Other Notes
// -----------
// [1] This field is immutable. That is, it's set up as part of the initialisation
// process, and is not modified after that. Thus, it doesn't need to be
// protected from concurrent access.
//
// [2] This field is protected by the fRootMutex lock.
//
// [3] fDebugLevel isn't really used. I've included it for two reasons:
// a) if you use EmptyFS as a template for your own VFS plug-in, it will be useful
// to have a handy debug switch
// b) it's a good example of how to pass information from your mount tool to your
// KEXT
static EmptyFSMount * EmptyFSMountFromMount(mount_t mp)
// Gets the EmptyFSMount from a mount_t.
{
EmptyFSMount * result;
assert(mp != NULL);
result = vfs_fsprivate(mp);
assert(result != NULL);
assert(result->fMagic == kEmptyFSMountMagic);
assert(result->fMountPoint == mp);
return result;
}
static void EmptyFSMountInitGetAttrListGoop(EmptyFSMount *mtmp)
// Initialises the f_capabilities and f_attributes fields of the
// fAttr field of the EmptyFSMount with the appropriate static values.
// This is in a separate routine because it's so big; I didn't want
// to confuse EmptyFSInitAttr with all of this stuff.
{
mtmp->fAttr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] = 0
// | VOL_CAP_FMT_PERSISTENTOBJECTIDS
// | VOL_CAP_FMT_SYMBOLICLINKS
// | VOL_CAP_FMT_HARDLINKS
// | VOL_CAP_FMT_JOURNAL
// | VOL_CAP_FMT_JOURNAL_ACTIVE
| VOL_CAP_FMT_NO_ROOT_TIMES
// | VOL_CAP_FMT_SPARSE_FILES
// | VOL_CAP_FMT_ZERO_RUNS
| VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
| VOL_CAP_FMT_FAST_STATFS
| VOL_CAP_FMT_2TB_FILESIZE
;
mtmp->fAttr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] = 0
| VOL_CAP_FMT_PERSISTENTOBJECTIDS
| VOL_CAP_FMT_SYMBOLICLINKS
| VOL_CAP_FMT_HARDLINKS
| VOL_CAP_FMT_JOURNAL
| VOL_CAP_FMT_JOURNAL_ACTIVE
| VOL_CAP_FMT_NO_ROOT_TIMES
| VOL_CAP_FMT_SPARSE_FILES
| VOL_CAP_FMT_ZERO_RUNS
| VOL_CAP_FMT_CASE_SENSITIVE
| VOL_CAP_FMT_CASE_PRESERVING
| VOL_CAP_FMT_FAST_STATFS
| VOL_CAP_FMT_2TB_FILESIZE
;
mtmp->fAttr.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] = 0
// | VOL_CAP_INT_SEARCHFS
| VOL_CAP_INT_ATTRLIST
// | VOL_CAP_INT_NFSEXPORT
// | VOL_CAP_INT_READDIRATTR
// | VOL_CAP_INT_EXCHANGEDATA
// | VOL_CAP_INT_COPYFILE
// | VOL_CAP_INT_ALLOCATE
// | VOL_CAP_INT_VOL_RENAME
// | VOL_CAP_INT_ADVLOCK
// | VOL_CAP_INT_FLOCK
// | VOL_CAP_INT_EXTENDED_SECURITY
// | VOL_CAP_INT_USERACCESS
;
mtmp->fAttr.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] = 0
| VOL_CAP_INT_SEARCHFS
| VOL_CAP_INT_ATTRLIST
| VOL_CAP_INT_NFSEXPORT
| VOL_CAP_INT_READDIRATTR
| VOL_CAP_INT_EXCHANGEDATA
| VOL_CAP_INT_COPYFILE
| VOL_CAP_INT_ALLOCATE
| VOL_CAP_INT_VOL_RENAME
| VOL_CAP_INT_ADVLOCK
| VOL_CAP_INT_FLOCK
| VOL_CAP_INT_EXTENDED_SECURITY
| VOL_CAP_INT_USERACCESS
;
mtmp->fAttr.f_attributes.validattr.commonattr = 0
| ATTR_CMN_NAME
| ATTR_CMN_DEVID
| ATTR_CMN_FSID
| ATTR_CMN_OBJTYPE
// | ATTR_CMN_OBJTAG
| ATTR_CMN_OBJID
// | ATTR_CMN_OBJPERMANENTID
| ATTR_CMN_PAROBJID
// | ATTR_CMN_SCRIPT
| ATTR_CMN_CRTIME
// | ATTR_CMN_MODTIME
// | ATTR_CMN_CHGTIME
// | ATTR_CMN_ACCTIME
// | ATTR_CMN_BKUPTIME
// | ATTR_CMN_FNDRINFO
| ATTR_CMN_OWNERID
| ATTR_CMN_GRPID
| ATTR_CMN_ACCESSMASK
| ATTR_CMN_FLAGS
// | ATTR_CMN_USERACCESS
// | ATTR_CMN_EXTENDED_SECURITY
// | ATTR_CMN_UUID
// | ATTR_CMN_GRPUUID
;
mtmp->fAttr.f_attributes.validattr.volattr = 0
| ATTR_VOL_FSTYPE
// | ATTR_VOL_SIGNATURE
| ATTR_VOL_SIZE
| ATTR_VOL_SPACEFREE
| ATTR_VOL_SPACEAVAIL
// | ATTR_VOL_MINALLOCATION
// | ATTR_VOL_ALLOCATIONCLUMP
| ATTR_VOL_IOBLOCKSIZE
| ATTR_VOL_OBJCOUNT
| ATTR_VOL_FILECOUNT
| ATTR_VOL_DIRCOUNT
| ATTR_VOL_MAXOBJCOUNT
| ATTR_VOL_MOUNTPOINT
| ATTR_VOL_NAME
| ATTR_VOL_MOUNTFLAGS
| ATTR_VOL_MOUNTEDDEVICE
// | ATTR_VOL_ENCODINGSUSED
| ATTR_VOL_CAPABILITIES
| ATTR_VOL_ATTRIBUTES
;
mtmp->fAttr.f_attributes.validattr.dirattr = 0
// | ATTR_DIR_LINKCOUNT
// | ATTR_DIR_ENTRYCOUNT
// | ATTR_DIR_MOUNTSTATUS
;
mtmp->fAttr.f_attributes.validattr.fileattr = 0
// | ATTR_FILE_LINKCOUNT
| ATTR_FILE_TOTALSIZE
// | ATTR_FILE_ALLOCSIZE
| ATTR_FILE_IOBLOCKSIZE
// | ATTR_FILE_DEVTYPE
// | ATTR_FILE_FORKCOUNT
// | ATTR_FILE_FORKLIST
| ATTR_FILE_DATALENGTH
| ATTR_FILE_DATAALLOCSIZE
// | ATTR_FILE_RSRCLENGTH
// | ATTR_FILE_RSRCALLOCSIZE
;
mtmp->fAttr.f_attributes.validattr.forkattr = 0;
// All attributes that we do support, we support natively.
mtmp->fAttr.f_attributes.nativeattr.commonattr = mtmp->fAttr.f_attributes.validattr.commonattr;
mtmp->fAttr.f_attributes.nativeattr.volattr = mtmp->fAttr.f_attributes.validattr.volattr;
mtmp->fAttr.f_attributes.nativeattr.dirattr = mtmp->fAttr.f_attributes.validattr.dirattr;
mtmp->fAttr.f_attributes.nativeattr.fileattr = mtmp->fAttr.f_attributes.validattr.fileattr;
mtmp->fAttr.f_attributes.nativeattr.forkattr = mtmp->fAttr.f_attributes.validattr.forkattr;
}
static void EmptyFSInitAttr(EmptyFSMount *mtmp)
// Initialises the fAttr field of the EmptyFSMount with the appropriate
// static values. This is done at initialisation time, so we don't have
// to worry about concurrency.
{
mtmp->fAttr.f_objcount = 1;
mtmp->fAttr.f_filecount = 0;
mtmp->fAttr.f_dircount = 1;
mtmp->fAttr.f_maxobjcount = 1;
mtmp->fAttr.f_bsize = 4096;
mtmp->fAttr.f_iosize = 4096;
mtmp->fAttr.f_blocks = 1;
mtmp->fAttr.f_bfree = 0;
mtmp->fAttr.f_bavail = 0;
mtmp->fAttr.f_bused = 1;
mtmp->fAttr.f_files = 1;
mtmp->fAttr.f_ffree = 0;
mtmp->fAttr.f_fsid.val[0] = mtmp->fBlockRDevNum;
mtmp->fAttr.f_fsid.val[1] = vfs_typenum(mtmp->fMountPoint);
// mtmp->fAttr.f_owner = xxx;
EmptyFSMountInitGetAttrListGoop(mtmp); // f_capabilities and f_attributes
nanotime(&mtmp->fAttr.f_create_time);
// mtmp->fAttr.f_modify_time = xxx;
// mtmp->fAttr.f_access_time = xxx;
// mtmp->fAttr.f_backup_time = xxx;
mtmp->fAttr.f_fssubtype = 0;
mtmp->fAttr.f_vol_name = mtmp->fVolumeName;
// mtmp->fAttr.f_signature = xxx;
// mtmp->fAttr.f_carbon_fsid = xxx;
}
static errno_t EmptyFSMountGetRootVNodeCreatingIfNecessary(EmptyFSMount *mtmp, vnode_t *vnPtr)
// Returns the root vnode for the volume, creating it if necessary. The resulting
// vnode has a I/O reference count, which the caller is responsible for releasing
// (using vnode_put) or passing along to its caller.
{
errno_t err;
errno_t junk;
vnode_t resultVN;
uint32_t vid;
// Pre-conditions
assert(mtmp != NULL);
assert( vnPtr != NULL);
assert(*vnPtr == NULL);
// resultVN holds vnode we're going to return in *vnPtr. If this ever goes non-NULL,
// we're done.
resultVN = NULL;
// First lock the revelant fields of the mount point.
lck_mtx_lock(mtmp->fRootMutex);
do {
// Loop invariants (-:
assert(resultVN == NULL); // no point looping if we already have a result
// lck_mtx_assert is only available in the "com.apple.kpi.unsupported" KPI, so
// we only use it in debug builds. Our "Info.plist" file is preprocessed to
// require the "com.apple.kpi.unsupported" KPI in this case.
#if MACH_ASSSERT
lck_mtx_assert(mtmp->fRootMutex, LCK_MTX_ASSERT_OWNED);
#endif
if (mtmp->fRootAttaching) {
// If someone else is already trying to create the root vnode, wait for
// them to get done. Note that msleep will unlock and relock mtmp->fRootMutex,
// so once it returns we have to loop and start again from scratch.
mtmp->fRootWaiting = TRUE;
(void) msleep(&mtmp->fRootVNode, mtmp->fRootMutex, PINOD, "EmptyFSMountGetRootVNodeCreatingIfNecessary", NULL);
err = EAGAIN;
} else if (mtmp->fRootVNode == NULL) {
vnode_t newVN;
struct vnode_fsparam params;
// There is no root vnode, so create it. While we're creating it, we
// drop our lock (to avoid the possibility of deadlock), so we set
// fRootAttaching to stall anyone else entering the code (and eliminate
// the possibility of two people trying to create the same vnode).
mtmp->fRootAttaching = TRUE;
lck_mtx_unlock(mtmp->fRootMutex);
newVN = NULL;
params.vnfs_mp = mtmp->fMountPoint;
params.vnfs_vtype = VDIR;
params.vnfs_str = NULL;
params.vnfs_dvp = NULL;
params.vnfs_fsnode = NULL;
params.vnfs_vops = gVNodeOperations;
params.vnfs_markroot = TRUE;
params.vnfs_marksystem = FALSE;
params.vnfs_rdev = 0; // we don't currently support VBLK or VCHR
params.vnfs_filesize = 0; // not relevant for a directory
params.vnfs_cnp = NULL;
params.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE; // do no vnode name caching
err = vnode_create(VNCREATE_FLAVOR, sizeof(params), &params, &newVN);
assert( (err == 0) == (newVN != NULL) );
lck_mtx_lock(mtmp->fRootMutex);
if (err == 0) {
// If we successfully create the vnode, it's time to install it as
// the root. No one else should have been able to get here, so
// mtmp->fRootVNode should still be NULL. If it's not, that's bad.
assert(mtmp->fRootVNode == NULL);
mtmp->fRootVNode = newVN;
// Also let the VFS layer know that we have a soft reference to
// the vnode.
junk = vnode_addfsref(newVN);
assert(junk == 0);
// If anyone got hung up on mtmp->fRootAttaching, unblock them.
assert(mtmp->fRootAttaching);
mtmp->fRootAttaching = FALSE;
if (mtmp->fRootWaiting) {
wakeup(&mtmp->fRootVNode);
mtmp->fRootWaiting = FALSE;
}
// Set up the function result. Note that vnode_create creates the
// vnode with an I/O reference count, so we can just return it
// directly.
resultVN = mtmp->fRootVNode;
err = 0;
}
} else {
vnode_t candidateVN;
// We already have a root vnode. Drop our lock (again, to avoid deadlocks)
// and get a reference on it, using the vnode ID (vid) to confirm that it's
// still valid. If that works, we're all set. Otherwise, let's just start
// again from scratch.
candidateVN = mtmp->fRootVNode;
vid = vnode_vid(candidateVN);
lck_mtx_unlock(mtmp->fRootMutex);
err = vnode_getwithvid(candidateVN, vid);
if (err == 0) {
// All ok. vnode_getwithvid has taken an I/O reference count on the
// vnode, so we can just return it to the caller. This reference
// prevents the vnode from being reclaimed in the interim.
resultVN = candidateVN;
assert(err == 0);
} else {
// vnode_getwithvid failed. This is most likely because the vnode
// has been reclaimed between dropping the lock and calling vnode_getwithvid.
// That's cool. We just loop again, and this time we'll get the updated
// results (hopefully).
err = EAGAIN;
}
// We need to reacquire the lock because that's the loop invariant.
// Strictly speaking we don't need to do this in the 'success' case,
// but it makes the code simpler (and I don't care about the trivial
// performance cost in this sample).
lck_mtx_lock(mtmp->fRootMutex);
}
// resultVN should only be set if everything is OK.
assert( (err == 0) == (resultVN != NULL) );
} while (err == EAGAIN);
lck_mtx_unlock(mtmp->fRootMutex);
if (err == 0) {
*vnPtr = resultVN;
}
// Post-conditions
assert( (err == 0) == (*vnPtr != NULL) );
return err;
}
static void EmptyFSMountDetachRootVNode(EmptyFSMount *mtmp, vnode_t vn)
// Called by higher-level code within our VFS plug-in to reclaim a vnode,
// that is, for us to 'forget' about it. We only 'know' about one vnode,
// the root vnode, so this code is much easier than it would be in a
// real file system.
{
int junk;
assert(mtmp != NULL);
assert(vn != NULL);
lck_mtx_lock(mtmp->fRootMutex);
// We can ignore mtmp->fRootAttaching here because, if it's set, mtmp->fRootVNode
// will be NULL. And, if that's the case, we just do nothing and return. That's
// exactly the correct behaviour if the system tries to reclaim the vnode while
// some other thread is in the process of attaching it.
//
// The following assert checks the assumption that makes this all work.
assert( ! mtmp->fRootAttaching || (mtmp->fRootVNode == NULL) );
if (mtmp->fRootVNode == NULL) {
// Do nothing; someone beat us to the reclaim; nothing to do.
} else {
// The vnode we're reclaiming should be the root vnode. If it isn't,
// I want to know about it.
assert(mtmp->fRootVNode == vn);
// Tell VFS that we're removing our soft reference to the vnode.
junk = vnode_removefsref(mtmp->fRootVNode);
assert(junk == 0);
mtmp->fRootVNode = NULL;
}
lck_mtx_unlock(mtmp->fRootMutex);
}
#if MACH_ASSERT
static boolean_t ValidVNode(vnode_t vn)
// Returns true if the vnode is valid on our file system.
// In this case, the only valid vnode is the root vnode,
// so the implementation is trivial.
{
boolean_t result;
EmptyFSMount * mtmp;
assert(vn != NULL);
mtmp = EmptyFSMountFromMount( vnode_mount(vn) );
lck_mtx_lock(mtmp->fRootMutex);
result = (vn == mtmp->fRootVNode);
lck_mtx_unlock(mtmp->fRootMutex);
return result;
}
#endif
/////////////////////////////////////////////////////////////////////
#pragma mark ***** VNode Operations
static errno_t VNOPLookup(struct vnop_lookup_args *ap)
// This is called by VFS to do a directory lookup.
//
// dvp is the directory to search.
//
// cnp describes the name to search for. This is kinda complicated, although
// the comments in <sys/vnode.h> are pretty helpful.
//
// vpp is a pointer to a vnode where we return the found item. The
// returned vnode must have an I/O reference, and the caller is responsible
// for releasing it.
//
// context identifies the calling process.
{
errno_t err;
vnode_t dvp;
vnode_t * vpp;
struct componentname * cnp;
vfs_context_t context;
vnode_t vn;
// Unpack arguments
dvp = ap->a_dvp;
vpp = ap->a_vpp;
cnp = ap->a_cnp;
context = ap->a_context;
// Pre-conditions
assert(dvp != NULL);
assert(vnode_isdir(dvp));
assert( ValidVNode(dvp) );
assert(vpp != NULL);
assert(cnp != NULL);
assert(context != NULL);
// Prepare for failure.
vn = NULL;
// Trivial implementation
if (cnp->cn_flags & ISDOTDOT) {
// Implement lookup for ".." (that is, the parent directory). As we currently
// only support one directory (the root directory) and the parent of the root
// is always the root, this is trivial (and, incidentally, exactly the same
// as the code for ".", but that wouldn't be true in a more general VFS plug-in).
// We just get an I/O reference on dvp and return that.
err = vnode_get(dvp);
if (err == 0) {
vn = dvp;
}
} else if ( (cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.') ) {
// Implement lookup for "." (that is, this directory). Just get an I/O reference
// to dvp and return that.
err = vnode_get(dvp);
if (err == 0) {
vn = dvp;
}
} else {
err = ENOENT;
}
// Under all circumstances we set *vpp to vn. That way, we satisfy the
// post-condition, regardless of what VFS uses as the initial value for
// *vpp.
*vpp = vn;
// Post-conditions
assert( (err == 0) == (*vpp != NULL) );
return err;
}
static errno_t VNOPOpen(struct vnop_open_args *ap)
// Called by VFS to open a vnode for access.
//
// vp is the vnode that's being opened.
//
// mode contains the flags passed to open (things like FREAD).
//
// context identifies the calling process.
//
// This entry is rarely useful because VFS can read a file vnode without ever
// opening it, thus any work that you'd usually do here you have to do lazily in
// your read/write entry points.
//
// Regardless, in our implementation we have nothing to do.
{
vnode_t vp;
int mode;
vfs_context_t context;
// Unpack arguments
vp = ap->a_vp;
mode = ap->a_mode;
context = ap->a_context;
// Pre-conditions