-
Notifications
You must be signed in to change notification settings - Fork 52
/
DldCoveringVnode.cpp
4212 lines (3177 loc) · 149 KB
/
DldCoveringVnode.cpp
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
/*
* Copyright (c) 2011 Slava Imameev. All rights reserved.
*/
#ifdef _DLD_MACOSX_CAWL
#include <vfs/vfs_support.h>
#include <sys/ubc.h>
#include <sys/buf.h>
#include <IOKit/IOMemoryDescriptor.h>
#include "DldIOShadow.h"
#include "DldCoveringVnode.h"
#include "DldVnodeHashTable.h"
#include "DldVNodeHook.h"
#include "DldUndocumentedQuirks.h"
#include "DldDiskCAWL.h"
#include "DldVfsMntHook.h"
#include "md5_hash.h"
//--------------------------------------------------------------------
extern vfs_context_t gVfsContextSuser;
SInt32 gCawlNameGenerator = 0x0;
SInt64 gCawlWriteTimeStampSeed = 0x0;
//--------------------------------------------------------------------
#define super OSObject
OSDefineMetaClassAndStructors( DldCoveringFsd, OSObject )
//--------------------------------------------------------------------
DldCoveringFsd* DldCoveringFsd::GlobalCoveringFsd = NULL;
//--------------------------------------------------------------------
bool DldCoveringFsd::InitCoveringFsd()
{
DldCoveringFsd::GlobalCoveringFsd = DldCoveringFsd::withMount( NULL );
assert( DldCoveringFsd::GlobalCoveringFsd );
if( !DldCoveringFsd::GlobalCoveringFsd ){
DBG_PRINT_ERROR(("withMount( NULL ) failed"));
return false;
}
return true;
}
//--------------------------------------------------------------------
DldCoveringFsd* DldCoveringFsd::GetCoveringFsd( __in mount_t mnt )
{
assert( DldCoveringFsd::GlobalCoveringFsd );
//
// the returned object is not refernced as it is global
//
return DldCoveringFsd::GlobalCoveringFsd;
}
//--------------------------------------------------------------------
void DldCoveringFsd::PutCoveringFsd( __in DldCoveringFsd* coveringFsd )
{
//
// nothing to do, the object has not been referenced
//
}
//--------------------------------------------------------------------
DldCoveringFsd*
DldCoveringFsd::withMount( __in_opt mount_t mnt )
{
DldCoveringFsd* newCoveringFsd;
newCoveringFsd = new DldCoveringFsd();
assert( newCoveringFsd );
if( !newCoveringFsd ){
DBG_PRINT_ERROR(("new newCoveringFsd() failed\n"));
return NULL;
}
if( !newCoveringFsd->init() ){
DBG_PRINT_ERROR(("newCoveringFsd->init() failed\n"));
newCoveringFsd->release();
return NULL;
}
newCoveringFsd->mnt = mnt;
return newCoveringFsd;
}
//--------------------------------------------------------------------
bool DldCoveringFsd::init()
{
this->lock = IORecursiveLockAlloc();
assert( this->lock );
if( !this->lock ){
DBG_PRINT_ERROR(("IORecursiveLockAlloc failed\n"));
return false;
}
return true;
}
//--------------------------------------------------------------------
void DldCoveringFsd::free()
{
if( this->lock )
IORecursiveLockFree( this->lock );
}
//--------------------------------------------------------------------
errno_t
DldCoveringFsd::DldFsdHookCreateCoveringVnodeInternal(
__inout DldVnodCreateParams* ap
)
/*
the returned vnode has the iocount set to 0x1
*/
{
assert( preemption_enabled() );
assert( ap->dvp && ap->coveredVnode );
assert( vnode_isreg( ap->coveredVnode ) );
assert( (VOPFUNC*)DLD_VOP_UNKNOWN_OFFSET != DldGetVnodeOpVector( ap->coveredVnode ) );
errno_t error = 0;
struct vnode_fsparam vfsp;
//
// Zero the FS param structure
//
bzero ( &vfsp, sizeof ( vfsp ) );
//
// it seems okay to use the covered fsd mount structure as all requests
// to the covered fsd will be intercepted so it will be unaware about
// attached vnodes, the system traverses all vnodes for a mounted
// fsd in functions such as vflush() and iterate_vnodes() - the latter
// is the most dangerous as it allows the covered fsd to see covering vnodes
// but there is a trick it skips vnodes with NULL v_data fields, so if we
// set this field to NULL our vnode will not be visible to underlying FSD as
// there is no another offical method to iterate vnodes
//
vfsp.vnfs_mp = vnode_mount( ap->coveredVnode );
vfsp.vnfs_vtype = vnode_vtype( ap->coveredVnode );
vfsp.vnfs_str = "DldCoverVnode"; // this is only for debug purposes
vfsp.vnfs_dvp = ap->dvp;
vfsp.vnfs_fsnode = NULL; // this is the most important trick
vfsp.vnfs_cnp = NULL; // do not add to the name cache to avoid two entries with the same name,
// as the covered FSD have already done this
vfsp.vnfs_vops = DldGetVnodeOpVector( ap->coveredVnode ); // TO DO - get our own opvector!
vfsp.vnfs_rdev = 0;
vfsp.vnfs_flags = ap->vnfs_flags | (VNFS_NOCACHE | VNFS_CANTCACHE); // do not add to the name cache
vfsp.vnfs_marksystem = vnode_issystem( ap->coveredVnode );
vfsp.vnfs_markroot = vnode_isvroot( ap->coveredVnode );
//
// TO DO
// What about file size used for UBC ??
//
//
// Note that vnode_create ( ) returns the vnode with an iocount of +1;
// this routine returns the newly created vnode with this positive iocount.
//
error = vnode_create( VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &ap->coveringVnode );
assert( !error );
if( error != 0 ){
assert ( "DldFsdHookCreateCoverVnode failed" );
goto __exit;
}
__exit:
return error;
}
//--------------------------------------------------------------------
bool
DldCoveringFsd::DldIsCoveringVnode( __in vnode_t vnode )
{
DldIOVnode::VnodeType vnType = DldIOVnode::kVnodeType_Native;
DldIOVnode* dldVnode;
dldVnode = DldVnodeHashTable::sVnodesHashTable->RetrieveReferencedIOVnodByBSDVnode( vnode );
if( dldVnode ){
vnType = dldVnode->dldVnodeType;
dldVnode->release();
}
//
// covering vnodes have NULL private data pointer
//
assert( !( (DldIOVnode::kVnodeType_CoveringFSD == vnType) && NULL != vnode_fsnode( vnode ) ) );
return ( DldIOVnode::kVnodeType_CoveringFSD == vnType );
}
//--------------------------------------------------------------------
vnode_t
DldCoveringFsd::DldGetCoveredVnode( __in vnode_t coveringVnode )
/*
the returned vnode is not referenced, returns NULL if there is no coveredVnode
*/
{
DldIOVnode* dldVnode;
vnode_t coveredVnode = NULL;
dldVnode = DldVnodeHashTable::sVnodesHashTable->RetrieveReferencedIOVnodByBSDVnode( coveringVnode );
if( dldVnode ){
if( DldIOVnode::kVnodeType_CoveringFSD == dldVnode->dldVnodeType ){
assert( dldVnode->coveredVnode );
coveredVnode = dldVnode->coveredVnode;
}
dldVnode->release();
}
return coveredVnode;
}
//--------------------------------------------------------------------
void
DldCoveringFsd::DldSetUbcFileSize(
__in DldCoveredVnode* vnodesDscr,
__in off_t size,
__in bool forceSetSize
)
{
bool purgeCawlData = false;
assert( preemption_enabled() );
assert( vnodesDscr->coveringVnode );
assert( vnodesDscr->dldCoveringVnode );
if( !vnodesDscr->coveringVnode || !vnodesDscr->dldCoveringVnode )
return;
assert( DldIOVnode::kVnodeType_CoveringFSD == vnodesDscr->dldCoveringVnode->dldVnodeType );
vnodesDscr->dldCoveringVnode->LockExclusive();
{ // start of the locked region
if( ( vnodesDscr->dldCoveringVnode->fileDataSize < size ) || forceSetSize ){
//
// if the file is being truncated the CAWL related cached data
// past the new size must be purgedas it will never be flushed
// and so will keep the file marked as dirty ( having unflushed
// CAWL data ) and prevent the FSD unmounting
//
purgeCawlData = ( vnodesDscr->dldCoveringVnode->fileDataSize > size );
ubc_setsize( vnodesDscr->coveringVnode, size );
vnodesDscr->dldCoveringVnode->fileDataSize = size;
} // end if( dldVnode->fileDataSize < size )
} // end of the locked region
vnodesDscr->dldCoveringVnode->UnLockExclusive();
if( purgeCawlData ){
DldSparseFile* sparseFile = NULL;
assert( vnodesDscr->dldCoveringVnode->spinLock );
IOSimpleLockLock( vnodesDscr->dldCoveringVnode->spinLock );
{ // start of the lock
assert( !preemption_enabled() );
sparseFile = vnodesDscr->dldCoveringVnode->sparseFile;
if( NULL != sparseFile )
sparseFile->retain();
} // end of the lock
IOSimpleLockUnlock( vnodesDscr->dldCoveringVnode->spinLock );
if( sparseFile ){
sparseFile->purgeFromOffset( size );
sparseFile->release();
}
} // end if( purgeCawlData )
}
//--------------------------------------------------------------------
//
// BIG TO DO -- REPLACE vnode_get to vnode_getwithref where appropriate in the code!
//
int
DldCoveringFsd::DldReplaceVnodeByCovering(
__inout vnode_t* vnodeToCover,
__in vnode_t dvp
)
/*
vnodeToCover's io reference must be bumped by the caller and released by vnode_put
on covering vnode, normally this is done by the system
*/
{
int error = KERN_SUCCESS;
/*
DldIOVnode* dldVnodeToSetSparseFile = NULL;
bool createSparseFile = false;
*/
assert( preemption_enabled() );
bool repeat;
do{
repeat = false;
//this->LockExclusive();
{// start of the lock
DldIOVnode* dldVnode;
//
// TO DO - remove from the lock!
// TO DO if added at lookup or create the vnode doesn't have the name attached, the name
// and the parent are updated later by vnode_update_identity(), we only can be sure that when
// a KAUTH callback is called a vnode has a valid name and parent
//
dldVnode = DldVnodeHashTable::sVnodesHashTable->CreateAndAddIOVnodByBSDVnode( *vnodeToCover );
assert( dldVnode && 0x1 == dldVnode->flags.vopHooked );
if( dldVnode && 0x1 == dldVnode->flags.vopHooked ){
bool waitForCoveringVnode;
bool createCoveringVnode;
waitForCoveringVnode = false;
createCoveringVnode = false;
IOSimpleLockLock( dldVnode->spinLock );
{ // start of the lock
assert( !preemption_enabled() );
if( 0x1 == dldVnode->flags.coveringVnodeIsBeingCreated ) {
//
// a concurrent thread is creating a covering vnode or has already created one
//
waitForCoveringVnode = ( THREAD_WAITING == assert_wait( &dldVnode->coveringVnode, THREAD_UNINT ) );
} else if( NULL == dldVnode->coveringVnode ){
assert( 0x0 == dldVnode->flags.coveringVnodeIsBeingCreated );
dldVnode->flags.coveringVnodeIsBeingCreated = 0x1;
createCoveringVnode = true;
}
} // end of the lock
IOSimpleLockUnlock( dldVnode->spinLock );
if( waitForCoveringVnode ){
thread_block( THREAD_CONTINUE_NULL );
//
// there is a subtle moment here - the covering vnode reported by another thread
// might be in the reclaiming stage as the system doesn't know that we are going
// to use it here again, so we can get NULL or reclaimed vnode, so the vnode is
// being checked by vnode_getwithref below
//
assert( 0x0 == dldVnode->flags.coveringVnodeIsBeingCreated ); // might fail because of concurrent reclaim and recreation
assert( NULL != dldVnode->coveringVnode || 0x1 == dldVnode->flags.coveringVnodeReclaimed );
}
//
// TO DO - SOLVE RACE CONDITIONS
// - multiple callers!
// - simultaneous lookup and close-inactive-reclaim!
// ?? solved by the lock ???
//
if( createCoveringVnode ){
assert( NULL == dldVnode->coveringVnode );
assert( 0x1 == dldVnode->flags.coveringVnodeIsBeingCreated );
DldVnodCreateParams createParams;
bzero( &createParams, sizeof( createParams ) );
createParams.dvp = dvp;
createParams.coveredVnode = *vnodeToCover;
error = DldFsdHookCreateCoveringVnodeInternal( &createParams );
assert( KERN_SUCCESS == error );
if( KERN_SUCCESS == error ){
assert( createParams.coveringVnode );
//
// add the created vnode in the hash
//
DldIOVnode* dldCoveringVnode;
//
// TO DO - solve the name problem, the created DldIOVnode will have empty name, the covered
// DldIOVnode must be used to retrieve the name for log and etc
//
dldCoveringVnode = DldVnodeHashTable::sVnodesHashTable->CreateAndAddIOVnodByBSDVnode(createParams.coveringVnode,
DldIOVnode::kVnodeType_CoveringFSD );
assert( dldCoveringVnode && 0x1 == dldCoveringVnode->flags.vopHooked );
if( dldCoveringVnode ){
//
// the covered and covering vnodes have been just created
//
assert( NULL == dldVnode->coveringVnode );
dldVnode->coveringVnode = createParams.coveringVnode;
//
// get the covered file size and report it to UBC
//
vnode_attr va = { 0x0 };
VATTR_INIT( &va );
VATTR_WANTED( &va, va_data_size );
//
// TO DO - replace vfs_context_current()
// TO DO - remove from the lock
// TO DO - the call might be replaced with vnode_size()
//
if( 0x0 == vnode_getattr( *vnodeToCover, &va, vfs_context_current() ) ){
dldCoveringVnode->fileDataSize = va.va_data_size;
ubc_setsize( dldCoveringVnode->vnode, dldCoveringVnode->fileDataSize );
}
//
// Mark a vnode as having multiple hard links.
// This will cause the name cache to force a VNOP_LOOKUP on the vnode
// so that covering FSD can post-process the lookup. Also, volfs will call
// VNOP_GETATTR2 to determine the parent, instead of using v_parent.
// So we will not allow the vnode cache to substitue the covered vnode
// instead the covering one.
//
vnode_setmultipath( *vnodeToCover );
//
// increment the user reference count to retain the vnode
//
vnode_ref( *vnodeToCover );
//
// decrement the iocount which if nonzero prevents fsd from unmounting
//
vnode_put( *vnodeToCover );
//
// save the covered vnode before replacement
//
dldCoveringVnode->coveredVnode = *vnodeToCover;
//
// copy some flags from the covered vnode
//
dldCoveringVnode->flags.controlledByCAWL = dldVnode->flags.controlledByCAWL;
//
// replace the vnode
//
*vnodeToCover = createParams.coveringVnode;
#if DBG
{
//
// check that the covering vnode is hooked so the covered FSD will not see it
//
DldVnodeHooksHashTable::sVnodeHooksHashTable->LockShared();
{// start of the lock
assert( DldVnodeHooksHashTable::sVnodeHooksHashTable->RetrieveEntry( DldGetVnodeOpVector( dldCoveringVnode->vnode ), false ) );
}// end of the lock
DldVnodeHooksHashTable::sVnodeHooksHashTable->UnLockShared();
}
#endif//DBG
dldCoveringVnode->release();
} else {
//
// delete the vnode
//
// TO DO - as there is no corresponding DldIOVnode the vnode will be passed
// to the covered FSD's reclaim(), this will result in a crash
//
assert( !"this will crash the system" );
vnode_put( createParams.coveringVnode );
error = ENOMEM;
}// end if( dldCoveringVnode )
}// end if( KERN_SUCCESS == error )
IOSimpleLockLock( dldVnode->spinLock );
{ // start of the lock
dldVnode->flags.coveringVnodeIsBeingCreated = 0x0;
dldVnode->flags.coveringVnodeReclaimed = 0x0;
} // end of the lock
IOSimpleLockUnlock( dldVnode->spinLock );
thread_wakeup( &dldVnode->coveringVnode );
} else {
errno_t error;
//
// the covered and covering vnode have been found,
// replace the vnode to already existing covering vnode
//
vnode_t coveringVnodeWithIOCount = NULL;
//
// vnode_getwithref can block so do a trick to
// have a valid vnode pointer after the lock
// is released - use vnode_put which doesn't block
//
//
// synchronize with the reclaim
//
dldVnode->LockShared();
{ // strat of the locked region
if( dldVnode->coveringVnode )
error = vnode_get( dldVnode->coveringVnode );
else
error = ENOENT;
if( !error )
coveringVnodeWithIOCount = dldVnode->coveringVnode;
} // end of the locked region
dldVnode->UnLockShared();
//
// increment an io count reference yet again with vnode_getwithref ( useless I believe )
//
if( coveringVnodeWithIOCount && KERN_SUCCESS == vnode_getwithref( coveringVnodeWithIOCount ) ){
//
// the covering vnode holds a user reference to a covered vnode
//
assert( vnode_isinuse( *vnodeToCover, 0x0 ) );
//
// release an io reference, we are holding a user reference
//
vnode_put( *vnodeToCover );
//
// replace, the io count has been bumped
//
*vnodeToCover = coveringVnodeWithIOCount;
} else {
//
// vnode is being terminated, open a new one
//
repeat = true;
}
//
// release the user IO count
//
if( coveringVnodeWithIOCount )
vnode_put( coveringVnodeWithIOCount );
}// else if( NULL == dldVnode->coveringVnode )
dldVnode->release();
}// if( dldVnode )
}// end of the lock
//this->UnLockExclusive();
} while( repeat );
return error;
}
//--------------------------------------------------------------------
// TO DO - this only for the test!
#define TEST_CAWL_DIR "/work/cawl/"
#define TEST_CAWL_FILE_NAME TEST_CAWL_DIR"XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX_AAAAAAAA"
errno_t
DldCoveringFsd::createCawlSparseFile( __in DldCoveredVnode* coveredVnode, __in vfs_context_t vfsContext )
{
DldSparseFile* sparseFile;
errno_t error = KERN_SUCCESS;
DldIOVnode* dldCoveringVnode = coveredVnode->dldCoveringVnode;
assert( preemption_enabled() );
assert( dldCoveringVnode );
assert( vfsContext );
if( NULL == dldCoveringVnode->sparseFile ){
bool wait = false;
assert( preemption_enabled() );
//
// spin lock is used only for the sparse file creation synchronization
// as it is impossible to use IORecursiveLock with assert_wait
// because of possible reschedlung after or inside assert_wait
// resulting in the retaining IORecursiveLock by a thread that is
// not in the running queue and a possible deadlock
//
assert( dldCoveringVnode->spinLock );
IOSimpleLockLock( dldCoveringVnode->spinLock );
{ // start of the lock
//
// we should call IOLockUnlock after removing
// the thread from the running queue
//
assert( !preemption_enabled() );
if( NULL == dldCoveringVnode->sparseFile && 0x1 == dldCoveringVnode->flags.sparseFileIsBeingCreated )
wait = (THREAD_WAITING == assert_wait( &dldCoveringVnode->sparseFile, THREAD_UNINT ) );
else if( NULL == dldCoveringVnode->sparseFile )
dldCoveringVnode->flags.sparseFileIsBeingCreated = 0x1;
} // end of the lock
IOSimpleLockUnlock( dldCoveringVnode->spinLock );
if( wait )
thread_block( THREAD_CONTINUE_NULL );
} // end if( NULL == dldCoveringVnode->sparseFile )
if( NULL != dldCoveringVnode->sparseFile )
return KERN_SUCCESS;
//
// do not be adamant and tenacious in file creation
//
if( 0x1 == dldCoveringVnode->flags.sparseFileCreationHasFailed )
return ENOMEM;
//
// get the unique ID for the data stream,
// if the file is of the zero size then
// write one bit of data just to allocate
// the disk space for the filr as some
// FSD generate unique ID using the file
// disk map, if there is no map ( a case
// of zero data file ) the returned value
// is not unique
//
vnode_attr va = { 0x0 };
do{
VATTR_INIT( &va );
VATTR_WANTED( &va, va_data_size );
VATTR_WANTED( &va, va_fileid );
VATTR_WANTED( &va, va_fsid );
error = vnode_getwithref( coveredVnode->coveredVnode );
assert( KERN_SUCCESS == error );
if( KERN_SUCCESS == error ){
error = vnode_getattr( coveredVnode->coveredVnode, &va, vfsContext );
if( KERN_SUCCESS == error && 0x0 == va.va_data_size ){
//
// write one bit of data just to allocate
// the disk space for the filr as some
// FSD generate unique ID using the file
// disk map, if there is no map ( a case
// of zero data file ) the returned value
// is not unique
//
error = DldVnodeSetsize( coveredVnode->coveredVnode, 0x1, 0x0, vfsContext);
assert( KERN_SUCCESS == error );
if( KERN_SUCCESS != error ){
DBG_PRINT_ERROR(("DldVnodeSetsize() failed with an error(%u)\n", error));
}
} else {
DBG_PRINT_ERROR(("vnode_getattr() failed with an error(%u)\n", error));
}
vnode_put( coveredVnode->coveredVnode );
assert( KERN_SUCCESS == error );
if( KERN_SUCCESS != error ){
DBG_PRINT_ERROR(("vnode_getattr() failed with an error(%u)\n", error));
return error;
}
} else {
//
// the vnode is being reclaimed
//
return error;
}
} while( 0x0 == va.va_data_size );
//
// get the name hash
//
OSString* nameStrRef = dldCoveringVnode->getNameRef();
assert( nameStrRef );
if( !nameStrRef )
return ENOMEM;
MD5_CTX mdContext;
bool addInHashTable = false;
bool usersCountIsIncremented = false;
DldMD5Init( &mdContext );
//DldMD5Update ( &mdContext, (unsigned char*)nameStrRef->getCStringNoCopy(), nameStrRef->getLength() );
DldMD5Update( &mdContext, (unsigned char*)&va.va_fileid, sizeof(va.va_fileid) );
DldMD5Update( &mdContext, (unsigned char*)&va.va_fsid, sizeof(va.va_fsid) );
DldMD5Final( &mdContext );
assert( 0x1 == dldCoveringVnode->flags.sparseFileIsBeingCreated );
//
// try to retrieve from the hash
//
DldSparseFilesHashTable::sSparseFilesHashTable->LockShared();
{
sparseFile = DldSparseFilesHashTable::sSparseFilesHashTable->RetrieveEntry( mdContext.digest, true );
}
DldSparseFilesHashTable::sSparseFilesHashTable->UnLockShared();
//
// account for a new possible sparse file user,
// this protects from the race when a covering vnode is being reclaimed
// and another vnode for the same data stream is being created
//
if( sparseFile ){
usersCountIsIncremented = sparseFile->incrementUsersCount();
if( !usersCountIsIncremented ){
//
// the sparse file is being removed and has passed the stage from which it can't be resurected
//
sparseFile->release();
sparseFile = NULL;
}
} // end if( sparseFile )
if( !sparseFile ){
char name[ sizeof(TEST_CAWL_FILE_NAME) ];
assert( !usersCountIsIncremented );
//
// create a new
//
snprintf( name, sizeof( name ), "%s%08x_%08x_%08x_%08x", TEST_CAWL_DIR,
*(int*)&mdContext.digest[0],*(int *)&mdContext.digest[4],*(int *)&mdContext.digest[8],*(int *)&mdContext.digest[12]);
//
// create the sparse file
//
sparseFile = DldSparseFile::withPath( (const char*)name,
coveredVnode->coveringVnode,
mdContext.digest,
nameStrRef->getCStringNoCopy() );
assert( sparseFile );
if( sparseFile ){
addInHashTable = true;
usersCountIsIncremented = true; // the object is created with a 0x1 reference count
} // if( sparseFile )
} // end if( !sparseFile )
if( sparseFile ){
bool deleteSparseFile = false;
//
// mark the mount as controlled by the CAWL, do it before setting the dldCoveringVnode->sparseFile value
//
assert( NULL != vnode_mount( dldCoveringVnode->coveringVnode ) );
assert( vnode_mount( dldCoveringVnode->coveringVnode ) == vnode_mount( dldCoveringVnode->coveredVnode ) );
DldVfsMntHook* mnt = DldVfsMntHook::withVfsMnt( vnode_mount( dldCoveringVnode->coveringVnode ) );
assert( mnt );
if( mnt ){
mnt->setAsControlledByCAWL();
mnt->release();
}
sparseFile->exchangeCawlRelatedVnode( dldCoveringVnode->vnode );
assert( dldCoveringVnode->spinLock );
IOSimpleLockLock( dldCoveringVnode->spinLock );
{ // start of the lock
assert( !preemption_enabled() );
if( NULL == dldCoveringVnode->sparseFile ){
dldCoveringVnode->sparseFile = sparseFile;
//
// take a reference for the sparse file hash
//
if( addInHashTable )
sparseFile->retain();
} else {
deleteSparseFile = true;
}
} // end of the lock
IOSimpleLockUnlock( dldCoveringVnode->spinLock );
if( deleteSparseFile ){
assert( sparseFile != dldCoveringVnode->sparseFile );
if( usersCountIsIncremented ){
sparseFile->decrementUsersCount();
usersCountIsIncremented = false;
} // end if( usersCountIsIncremented )
sparseFile->release();
} else if( addInHashTable ){
//
// insert in the sparse file hash, as here we are guaranteed that
// there is a single instance of the sparse file for the key
//
assert( DldSparseFilesHashTable::sSparseFilesHashTable );
DldSparseFilesHashTable::sSparseFilesHashTable->LockExclusive();
{ // strat of the lock
bool added;
assert( sizeof( mdContext.digest ) == 16 );
assert( NULL == DldSparseFilesHashTable::sSparseFilesHashTable->RetrieveEntry( mdContext.digest, false ) );
added = DldSparseFilesHashTable::sSparseFilesHashTable->AddEntry( mdContext.digest, sparseFile );
assert( added );
if( !added ){
DBG_PRINT_ERROR(( "DldSparseFilesHashTable::sSparseFilesHashTable->AddEntry() failed for %s\n", nameStrRef->getCStringNoCopy() ));
}
} // end of the lock
DldSparseFilesHashTable::sSparseFilesHashTable->UnLockExclusive();
//
// the object was referenced while dldCoveringVnode->spinLock was held
//
sparseFile->release();
}
sparseFile = NULL;
} else {
IOSimpleLockLock( dldCoveringVnode->spinLock );
{ // start of the lock
dldCoveringVnode->flags.sparseFileCreationHasFailed = 0x1;
} // end of the lock
IOSimpleLockUnlock( dldCoveringVnode->spinLock );
error = ENOMEM;
}// end if( sparseFile )
//
// we finished
//
IOSimpleLockLock( dldCoveringVnode->spinLock );
{ // start of the lock
dldCoveringVnode->flags.sparseFileIsBeingCreated = 0x0;
} // end of the lock
IOSimpleLockUnlock( dldCoveringVnode->spinLock );
nameStrRef->release();
DLD_DBG_MAKE_POINTER_INVALID( nameStrRef );
//
// wake up waiters( if any ), the waiter should check
// for NULL sparseFile value after being woken up
//
thread_wakeup( &dldCoveringVnode->sparseFile );
return error;
}
//--------------------------------------------------------------------
int
DldCoveringFsd::DldReclaimCoveringVnode(
__in vnode_t coveringVnode
)
{
//
// free the covered vnode ( it seems the following call to RemoveIOVnodeByBSDVnode
// is also doing this if the DldIOVnode's reference count drops to zero
//
assert( preemption_enabled() );
assert( this->DldIsCoveringVnode( coveringVnode ) );
//
// free the covered vnode ( it seems the following call to RemoveIOVnodeByBSDVnode
// is also doing this if the DldIOVnode's reference count drops to zero
//
vnode_t coveredVnode = NULL;
DldSparseFile* sparseFile = NULL;
DldIOVnode* dldVnode;
dldVnode = DldVnodeHashTable::sVnodesHashTable->RetrieveReferencedIOVnodByBSDVnode( coveringVnode );
assert( dldVnode );
if( dldVnode ){
assert( DldIOVnode::kVnodeType_CoveringFSD == dldVnode->dldVnodeType );
if( dldVnode->coveredVnode ){
coveredVnode = dldVnode->coveredVnode;
dldVnode->coveredVnode = NULL;
//
// remeber - this is a debug flag! do not synchronize on it, do not make any decisions using it!
//
dldVnode->flags.coveringVnodeReclaimed = 0x1;
//
// we are the sole reference holder ( might fail if the vnode had been opened before the driver was loaded )
//
// assert( !vnode_isinuse( coveredVnode, 0x1 ) );
DldIOVnode* dldCoveredVnode;
dldCoveredVnode = DldVnodeHashTable::sVnodesHashTable->RetrieveReferencedIOVnodByBSDVnode( coveredVnode );
if( dldCoveredVnode ){
assert( DldIOVnode::kVnodeType_Native == dldCoveredVnode->dldVnodeType );
//
// synchronize with the create and lookup
//
dldCoveredVnode->LockExclusive();
{
dldCoveredVnode->coveringVnode = NULL;
}