-
Notifications
You must be signed in to change notification settings - Fork 85
/
X70FsdFileInfo.c
2132 lines (1645 loc) · 55.8 KB
/
X70FsdFileInfo.c
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
#include "X70FsdData.h"
#include "X70FsdFileInfo.h"
extern CACHE_MANAGER_CALLBACKS G_CacheMgrCallbacks;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//完成卷查询接口
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FLT_PREOP_CALLBACK_STATUS
PtPreOperationQueryVolumeInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__deref_out_opt PVOID *CompletionContext
)
{
//用自己的文件对象下发然后完成请求
NTSTATUS Status;
PFLT_CALLBACK_DATA RetNewCallbackData = NULL;
PVOLUME_CONTEXT volCtx = NULL;
PVOID VolumeBuffer;
PFLT_IO_PARAMETER_BLOCK Iopb = Data->Iopb;
FS_INFORMATION_CLASS FsInformationClass;
PFILE_OBJECT FileObject;
PFCB Fcb;
PCCB Ccb;
BOOLEAN AcquireResource = FALSE;
FsRtlEnterFileSystem();
//
if(!IsMyFakeFcb(FltObjects->FileObject))
{
FsRtlExitFileSystem();
return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
Status = FltGetVolumeContext( FltObjects->Filter,
FltObjects->Volume,
&volCtx );
if(!NT_SUCCESS(Status))
{
Data->IoStatus.Status = Status;
Data->IoStatus.Information = 0;
FsRtlExitFileSystem();
return FLT_PREOP_COMPLETE;
}
VolumeBuffer = Iopb->Parameters.SetVolumeInformation.VolumeBuffer;
FsInformationClass = Iopb->Parameters.SetVolumeInformation.FsInformationClass;
FileObject = FltObjects->FileObject;
Fcb = (PFCB)FileObject->FsContext;
Ccb = (PCCB)FileObject->FsContext2;
try{
ExAcquireResourceSharedLite(volCtx->VolResource, TRUE);
AcquireResource = TRUE;
if(Ccb->StreamFileInfo.StreamObject == NULL)
{
try_return(Data->IoStatus.Status = STATUS_FILE_DELETED);
}
Status = FltAllocateCallbackData(FltObjects->Instance,Ccb->StreamFileInfo.StreamObject,&RetNewCallbackData);
if(NT_SUCCESS(Status))
{
RtlCopyMemory(RetNewCallbackData->Iopb,Data->Iopb,sizeof(FLT_IO_PARAMETER_BLOCK));
RetNewCallbackData->Iopb->TargetFileObject = Ccb->StreamFileInfo.StreamObject;
FltPerformSynchronousIo(RetNewCallbackData);
Status = RetNewCallbackData->IoStatus.Status;
Data->IoStatus = RetNewCallbackData->IoStatus;
if(NT_SUCCESS(Status))
{
if(FsInformationClass == FileFsSizeInformation) //更新下扇区大小跟簇大小
{
volCtx->SectorsPerAllocationUnit = ((PFILE_FS_SIZE_INFORMATION)VolumeBuffer)->SectorsPerAllocationUnit;
volCtx->SectorSize = ((PFILE_FS_SIZE_INFORMATION)VolumeBuffer)->BytesPerSector;
}
if(FsInformationClass == FileFsFullSizeInformation)
{
volCtx->SectorsPerAllocationUnit = ((PFILE_FS_FULL_SIZE_INFORMATION)VolumeBuffer)->SectorsPerAllocationUnit;
volCtx->SectorSize = ((PFILE_FS_FULL_SIZE_INFORMATION)VolumeBuffer)->BytesPerSector;
}
}
}
try_exit:NOTHING;
}
finally
{
if(RetNewCallbackData != NULL)
{
FltFreeCallbackData(RetNewCallbackData);
}
if(AcquireResource)
{
ExReleaseResourceLite(volCtx->VolResource);
}
if(volCtx != NULL)
{
FltReleaseContext(volCtx);
}
}
FsRtlExitFileSystem();
return FLT_PREOP_COMPLETE;
}
FLT_PREOP_CALLBACK_STATUS
PtPreOperationSetVolumeInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__deref_out_opt PVOID *CompletionContext
)
{
//用自己的文件对象下发然后完成请求
NTSTATUS Status;
PFLT_CALLBACK_DATA RetNewCallbackData = NULL;
PVOLUME_CONTEXT volCtx = NULL;
PFLT_IO_PARAMETER_BLOCK Iopb = Data->Iopb;
PVOID VolumeBuffer;
PFILE_OBJECT FileObject;
PFCB Fcb;
PCCB Ccb;
BOOLEAN AcquireResource = FALSE;
FsRtlEnterFileSystem();
//
if(!IsMyFakeFcb(FltObjects->FileObject))
{
FsRtlExitFileSystem();
return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
Status = FltGetVolumeContext( FltObjects->Filter,
FltObjects->Volume,
&volCtx );
if(!NT_SUCCESS(Status))
{
Data->IoStatus.Status = Status;
Data->IoStatus.Information = 0;
FsRtlExitFileSystem();
return FLT_PREOP_COMPLETE;
}
FileObject = FltObjects->FileObject;
Fcb = (PFCB)FileObject->FsContext;
Ccb = (PCCB)FileObject->FsContext2;
try
{
ExAcquireResourceExclusiveLite(volCtx->VolResource, TRUE);
AcquireResource = TRUE;
if(Ccb->StreamFileInfo.StreamObject == NULL)
{
try_return(Data->IoStatus.Status = STATUS_FILE_DELETED);
}
Status = FltAllocateCallbackData(FltObjects->Instance,Ccb->StreamFileInfo.StreamObject,&RetNewCallbackData);
if(NT_SUCCESS(Status))
{
RtlCopyMemory(RetNewCallbackData->Iopb,Data->Iopb,sizeof(FLT_IO_PARAMETER_BLOCK));
RetNewCallbackData->Iopb->TargetFileObject = Ccb->StreamFileInfo.StreamObject;
FltPerformSynchronousIo(RetNewCallbackData);
Status = RetNewCallbackData->IoStatus.Status;
Data->IoStatus = RetNewCallbackData->IoStatus;
}
try_exit: NOTHING;
}
finally
{
if(RetNewCallbackData != NULL)
{
FltFreeCallbackData(RetNewCallbackData);
}
if(AcquireResource)
{
ExReleaseResourceLite(volCtx->VolResource);
}
if(volCtx != NULL)
{
FltReleaseContext(volCtx);
}
}
FsRtlExitFileSystem();
return FLT_PREOP_COMPLETE;
}
FLT_POSTOP_CALLBACK_STATUS
PtPostOperationQueryVolumeInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__in_opt PVOID CompletionContext,
__in FLT_POST_OPERATION_FLAGS Flags
)
{
UNREFERENCED_PARAMETER( Data );
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( CompletionContext );
UNREFERENCED_PARAMETER( Flags );
return FLT_POSTOP_FINISHED_PROCESSING;
}
FLT_POSTOP_CALLBACK_STATUS
PtPostOperationSetVolumeInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__in_opt PVOID CompletionContext,
__in FLT_POST_OPERATION_FLAGS Flags
)
{
UNREFERENCED_PARAMETER( Data );
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( CompletionContext );
UNREFERENCED_PARAMETER( Flags );
return FLT_POSTOP_FINISHED_PROCESSING;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//文件查询接口
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FLT_POSTOP_CALLBACK_STATUS
PtPostOperationQueryInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__in_opt PVOID CompletionContext,
__in FLT_POST_OPERATION_FLAGS Flags
)
{
UNREFERENCED_PARAMETER( Data );
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( CompletionContext );
UNREFERENCED_PARAMETER( Flags );
return FLT_POSTOP_FINISHED_PROCESSING;
}
//完成文件查询接口
FLT_PREOP_CALLBACK_STATUS
PtPreOperationQueryInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__deref_out_opt PVOID *CompletionContext
)
{
FLT_PREOP_CALLBACK_STATUS FltStatus;
PIRP_CONTEXT IrpContext = NULL;
BOOLEAN TopLevel = FALSE;
FsRtlEnterFileSystem();
if(!IsMyFakeFcb(FltObjects->FileObject))
{
FsRtlExitFileSystem();
return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
if(FLT_IS_IRP_OPERATION(Data)) //irp query
{
//
//设置顶层组件
TopLevel = X70FsdIsIrpTopLevel( Data );
//创建irp上下文
try
{
IrpContext = X70FsdCreateIrpContext( Data,FltObjects, CanFsdWait( Data ) );
if(IrpContext == NULL)
{
X70FsdRaiseStatus(IrpContext,STATUS_INSUFFICIENT_RESOURCES);
}
FltStatus = X70FsdCommonQueryInformation(Data, FltObjects,IrpContext);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
//出现异常直接返回
DbgPrint("queryinfo出现异常直接返回 \n");
X70FsdProcessException(&IrpContext,&Data,GetExceptionCode());
FltStatus = FLT_PREOP_COMPLETE;
}
if (TopLevel) { IoSetTopLevelIrp( NULL ); }
}
else if(FLT_IS_FASTIO_OPERATION(Data)) //fastio read
{
//DbgPrint("FastIo queryinfo \n");
FltStatus = FLT_PREOP_DISALLOW_FASTIO ;// X70FsdFastIoRead(Data, FltObjects);
}
else
{
Data->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
Data->IoStatus.Information = 0;
FltStatus = FLT_PREOP_COMPLETE;
/*FltStatus = FLT_PREOP_SUCCESS_NO_CALLBACK;*/
DbgPrint("收到其他的queryinfo类型请求 \n");
}
FsRtlExitFileSystem();
return FltStatus;
}
//
FLT_PREOP_CALLBACK_STATUS
X70FsdCommonQueryInformation(
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__in PIRP_CONTEXT IrpContext)
{
//用原始文件对象进行文件的查询
NTSTATUS Status = STATUS_SUCCESS;
FLT_PREOP_CALLBACK_STATUS FltStatus = FLT_PREOP_COMPLETE;
ULONG Length = 0 ,LengthReturned = 0;
FILE_INFORMATION_CLASS FileInformationClass;
PFLT_IO_PARAMETER_BLOCK Iopb = Data->Iopb;
PVOID InfoBuffer ;
//PVOID QueryBuffer = NULL;
PFILE_OBJECT FileObject;
PFCB Fcb;
PCCB Ccb;
PFLT_CALLBACK_DATA RetNewCallbackData = NULL;
BOOLEAN PostIrp = FALSE;
PVOLUME_CONTEXT volCtx = NULL;
BOOLEAN AcquireResource = FALSE;
BOOLEAN AcquireVolResource = FALSE;
BOOLEAN ReleaseScbPaging = FALSE;
PDEVICE_OBJECT pVolumeDevObj,DeviceObject;
BOOLEAN FcbAcquired = FALSE;
BOOLEAN EncryptResourceAcquired = FALSE;
BOOLEAN NonCachedIoPending = FALSE;
if(FltObjects == NULL)
{
FltObjects = &IrpContext->FltObjects;
}
try{
LengthReturned = Length = Iopb->Parameters.QueryFileInformation.Length;
FileInformationClass = Iopb->Parameters.QueryFileInformation.FileInformationClass;
InfoBuffer = Iopb->Parameters.QueryFileInformation.InfoBuffer ;
FileObject = FltObjects->FileObject;
Fcb = (PFCB)FileObject->FsContext;
Ccb = FileObject->FsContext2;
if (!X70FsdAcquireSharedFcb( IrpContext, Fcb ))
{
Status = X70FsdPostRequest(Data, IrpContext );
IrpContext = NULL;
NonCachedIoPending = TRUE;
try_return( Status );
}
FcbAcquired = TRUE;
ExAcquireResourceSharedLite(Fcb->EncryptResource,TRUE);
EncryptResourceAcquired = TRUE;
//DbgPrint("查询文件信息 %d ,%ws \n",FileInformationClass,FileObject->FileName.Buffer);
//QueryBuffer = FltAllocatePoolAlignedWithTag(FltObjects->Instance,NonPagedPool,Length,'qfi');
//if(QueryBuffer == NULL)
//{
// try_return(Status = STATUS_INSUFFICIENT_RESOURCES);
//}
//RtlZeroMemory(QueryBuffer,Length);
//RtlCopyMemory(QueryBuffer,InfoBuffer,Length);
if(Ccb->StreamFileInfo.StreamObject == NULL)
{
try_return(Status = STATUS_FILE_DELETED);
}
Status = FltAllocateCallbackData(FltObjects->Instance,Ccb->StreamFileInfo.StreamObject,&RetNewCallbackData);
if(NT_SUCCESS(Status))
{
RtlCopyMemory(RetNewCallbackData->Iopb,Data->Iopb,sizeof(FLT_IO_PARAMETER_BLOCK));
//RetNewCallbackData->Iopb->Parameters.QueryFileInformation.InfoBuffer = QueryBuffer;
//RetNewCallbackData->Iopb->Parameters.QueryFileInformation.Length = Length;
ClearFlag(RetNewCallbackData->Iopb->IrpFlags,IRP_PAGING_IO);
RetNewCallbackData->Iopb->TargetFileObject = Ccb->StreamFileInfo.StreamObject;
FltPerformSynchronousIo(RetNewCallbackData);
Status = RetNewCallbackData->IoStatus.Status;
Data->IoStatus = RetNewCallbackData->IoStatus;
//RtlCopyMemory(InfoBuffer,QueryBuffer,Length);
if(NT_SUCCESS(Status) ||
(Status == STATUS_BUFFER_OVERFLOW))
{
//处理文件大小等参数
switch(FileInformationClass)
{
case FileAllInformation:
{
// FileAllInformation
PFILE_ALL_INFORMATION fai;
fai = (PFILE_ALL_INFORMATION)InfoBuffer;
if(Ccb->FileAccess == FILE_ONLY_READ && Length >= sizeof(FILE_BASIC_INFORMATION))
{
SetFlag(fai->BasicInformation.FileAttributes,FILE_ATTRIBUTE_READONLY);
}
if(Length >= (sizeof(FILE_BASIC_INFORMATION)+sizeof(FILE_STANDARD_INFORMATION)))
{
fai->StandardInformation.EndOfFile.QuadPart = Fcb->Header.FileSize.QuadPart;
fai->StandardInformation.AllocationSize.QuadPart = Fcb->Header.AllocationSize.QuadPart;
}
if(Length >= (sizeof(FILE_BASIC_INFORMATION)
+sizeof(FILE_STANDARD_INFORMATION)
+sizeof(FILE_INTERNAL_INFORMATION)
+sizeof(FILE_EA_INFORMATION)
+sizeof(FILE_ACCESS_INFORMATION)
+sizeof(FILE_POSITION_INFORMATION)))
{
fai->PositionInformation.CurrentByteOffset = FileObject->CurrentByteOffset;
}
}
break;
case FileBasicInformation:
{
PFILE_BASIC_INFORMATION fbi;
fbi = (PFILE_BASIC_INFORMATION)InfoBuffer;
if(Ccb->FileAccess == FILE_ONLY_READ)
{
SetFlag(fbi->FileAttributes,FILE_ATTRIBUTE_READONLY);
}
}
break;
case FileAttributeTagInformation:
{
PFILE_ATTRIBUTE_TAG_INFORMATION fati;
fati = (PFILE_ATTRIBUTE_TAG_INFORMATION)InfoBuffer;
if(Ccb->FileAccess == FILE_ONLY_READ)
{
SetFlag(fati->FileAttributes,FILE_ATTRIBUTE_READONLY);
}
}
break;
case FileStandardInformation:
{
PFILE_STANDARD_INFORMATION fsi;
fsi = (PFILE_STANDARD_INFORMATION)InfoBuffer;
fsi->EndOfFile.QuadPart = Fcb->Header.FileSize.QuadPart;
// 保留文件标识部分
fsi->AllocationSize.QuadPart = Fcb->Header.AllocationSize.QuadPart;
}
break;
case FilePositionInformation:
{
PFILE_POSITION_INFORMATION fpi;
fpi = (PFILE_POSITION_INFORMATION)InfoBuffer;
fpi->CurrentByteOffset = FltObjects->FileObject->CurrentByteOffset;
}
break;
case FileNetworkOpenInformation:
{
PFILE_NETWORK_OPEN_INFORMATION fnoi;
fnoi = (PFILE_NETWORK_OPEN_INFORMATION)InfoBuffer;
//DbgPrint("查询网络信息 \n");
fnoi->EndOfFile.QuadPart = Fcb->Header.FileSize.QuadPart;
// 保留文件标识部分
fnoi->AllocationSize.QuadPart = Fcb->Header.AllocationSize.QuadPart;
}
break;
case FileStreamInformation: //如果加密文件流的话,这里相关文件流的大小必须返回正确的数据,最好是访问一下设备然后去读一下加密头
{
WCHAR NtfsData[] = L"::$DATA";
WCHAR StreamData[] = L":$DATA";
PFILE_STREAM_INFORMATION fsi;
ULONG offset = 0;
fsi = (PFILE_STREAM_INFORMATION)InfoBuffer;
#ifdef CV
VirtualizerStart();
#endif
do
{
offset = fsi->NextEntryOffset;
//默认流为文件本身
if( (fsi->StreamNameLength == 0 ) ||
((fsi->StreamNameLength == (sizeof(NtfsData)-sizeof(WCHAR))) &&
(RtlCompareMemory(fsi->StreamName,NtfsData,fsi->StreamNameLength) == fsi->StreamNameLength)))
{
fsi->StreamSize.QuadPart = Fcb->Header.FileSize.QuadPart;
// 保留文件标识部分
fsi->StreamAllocationSize.QuadPart = Fcb->Header.AllocationSize.QuadPart;
}
//NTFS文件流暂时不考虑
//else if(fsi->StreamNameLength >= sizeof(StreamData) &&
// (RtlCompareMemory(
// Add2Ptr(fsi->StreamName,fsi->StreamNameLength-sizeof(StreamData)+sizeof(WCHAR)),
// StreamData,
// sizeof(StreamData)-sizeof(WCHAR)
// ) == sizeof(StreamData)-sizeof(WCHAR)))//修改跟这个文件关联流的大小
//{
// BOOLEAN IsEnFile = FALSE;
// NTSTATUS StrStatus;
// //fsi->StreamSize.QuadPart -= Fcb->FileHeaderLength;
// StrStatus = GetFileStreamRealSize( IrpContext,
// FltObjects,
// fsi->StreamName,
// fsi->StreamNameLength,
// &IsEnFile);
// if(NT_SUCCESS(StrStatus) && IsEnFile)
// {
// fsi->StreamSize.QuadPart = IrpContext->CreateInfo.RealFileSize.QuadPart;
// fsi->StreamAllocationSize.QuadPart -= FILE_HEADER_LENGTH;
// }
//}
fsi = (PFILE_STREAM_INFORMATION)Add2Ptr(fsi,offset);
}
while(offset != 0);
#ifdef CV
VirtualizerEnd();
#endif
}
break;
default:
//DbgPrint("收到其他查询请求 %d \n",FileInformationClass);
break;
}
}
}
try_exit: NOTHING;
}
finally
{
if (FcbAcquired)
{
X70FsdReleaseFcb( IrpContext, Fcb );
}
if(AcquireVolResource)
{
ExReleaseResourceLite(volCtx->VolResource);
}
if(volCtx != NULL)
{
FltReleaseContext(volCtx);
}
if(EncryptResourceAcquired)
{
ExReleaseResourceLite( Fcb->EncryptResource );
}
if(RetNewCallbackData != NULL)
{
FltFreeCallbackData(RetNewCallbackData);
}
//if(QueryBuffer != NULL)
//{
// FltFreePoolAlignedWithTag(FltObjects->Instance,QueryBuffer,'qfi');
//}
if(!NT_SUCCESS(Status))
{
//DbgPrint("QueryInformation %ws 请求 %d 出错 ,%x \n ",FileObject->FileName.Buffer,FileInformationClass,Status);
}
if(Status == STATUS_PENDING)
{
FltStatus = FLT_PREOP_PENDING;
}
else
{
Data->IoStatus.Status = Status;
FltStatus = FLT_PREOP_COMPLETE;
}
if(!AbnormalTermination())
{
X70FsdCompleteRequest( &IrpContext, &Data, Data->IoStatus.Status,FALSE );
}
}
return FltStatus;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//文件设置接口
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//对文件信息设置接口
FLT_POSTOP_CALLBACK_STATUS
PtPostOperationSetInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__in_opt PVOID CompletionContext,
__in FLT_POST_OPERATION_FLAGS Flags
)
{
UNREFERENCED_PARAMETER( Data );
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( CompletionContext );
UNREFERENCED_PARAMETER( Flags );
return FLT_POSTOP_FINISHED_PROCESSING;
}
FLT_PREOP_CALLBACK_STATUS
PtPreOperationSetInformation (
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__deref_out_opt PVOID *CompletionContext
)
{
FLT_PREOP_CALLBACK_STATUS FltStatus;
PIRP_CONTEXT IrpContext = NULL;
BOOLEAN TopLevel = FALSE;
PAGED_CODE();
FsRtlEnterFileSystem();
if(!IsMyFakeFcb(FltObjects->FileObject))
{
FsRtlExitFileSystem();
return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
if(FLT_IS_IRP_OPERATION(Data)) //irp query
{
//
//设置顶层组件
TopLevel = X70FsdIsIrpTopLevel( Data );
//创建irp上下文
try
{
IrpContext = X70FsdCreateIrpContext( Data,FltObjects, CanFsdWait( Data ) );
if(IrpContext == NULL)
{
X70FsdRaiseStatus(IrpContext,STATUS_INSUFFICIENT_RESOURCES);
}
FltStatus = X70FsdCommonSetInformation(Data, FltObjects,IrpContext);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
//出现异常直接返回
DbgPrint("setinfo出现异常直接返回 \n");
X70FsdProcessException(&IrpContext,&Data,GetExceptionCode());
FltStatus = FLT_PREOP_COMPLETE;
}
// 恢复Top-Level IRP
if (TopLevel) { IoSetTopLevelIrp( NULL ); }
}
else if(FLT_IS_FASTIO_OPERATION(Data)) //fastio read
{
//DbgPrint("FastIo setinfo \n");
FltStatus = FLT_PREOP_DISALLOW_FASTIO ;//
}
else
{
Data->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
Data->IoStatus.Information = 0;
FltStatus = FLT_PREOP_COMPLETE;
/*FltStatus = FLT_PREOP_SUCCESS_NO_CALLBACK;*/
DbgPrint("收到其他的setinfo类型请求 \n");
}
FsRtlExitFileSystem();
return FltStatus;
}
//对文件信息设置接口
FLT_PREOP_CALLBACK_STATUS
X70FsdCommonSetInformation(
__inout PFLT_CALLBACK_DATA Data,
__in PCFLT_RELATED_OBJECTS FltObjects,
__in PIRP_CONTEXT IrpContext
)
{
NTSTATUS Status;
ULONG Length = 0 ;
FILE_INFORMATION_CLASS FileInformationClass;
PFLT_IO_PARAMETER_BLOCK Iopb = Data->Iopb;
PVOID InfoBuffer = NULL ;
PFILE_OBJECT FileObject;
PFCB Fcb;
PCCB Ccb;
PCCB TargetCcb;
BOOLEAN PostIrp = FALSE;
FLT_PREOP_CALLBACK_STATUS FltStatus;
PFLT_CALLBACK_DATA RetNewCallbackData = NULL;
PVOLUME_CONTEXT volCtx = NULL;
BOOLEAN AcquireVolResource = FALSE;
BOOLEAN ReleaseScbPaging = FALSE;
BOOLEAN FsRtlHeaderLocked = FALSE;
BOOLEAN FcbAcquired = FALSE;
BOOLEAN PagingIoResourceAcquired = FALSE;
BOOLEAN FileSizeChanged = FALSE;
BOOLEAN NonCachedIoPending = FALSE;
BOOLEAN FOResourceAcquired =FALSE;
BOOLEAN EncryptResourceAcquired = FALSE;
BOOLEAN Delete = FALSE;
if(FltObjects == NULL)
{
FltObjects = &IrpContext->FltObjects;
}
try{
Status = FltGetVolumeContext( FltObjects->Filter,
FltObjects->Volume,
&volCtx );
if(!NT_SUCCESS(Status))
{
Data->IoStatus.Status = Status;
Data->IoStatus.Information = 0;
try_return(FltStatus = FLT_PREOP_COMPLETE);
}
Length = Iopb->Parameters.SetFileInformation.Length;
FileInformationClass = Iopb->Parameters.SetFileInformation.FileInformationClass;
InfoBuffer = Iopb->Parameters.SetFileInformation.InfoBuffer ;
FileObject = FltObjects->FileObject;
Fcb = (PFCB)FileObject->FsContext;
Ccb = FileObject->FsContext2;
//DbgPrint("收到SetInfo %ws \n",FileObject->FileName.Buffer);
if(FileInformationClass == FileEndOfFileInformation ||
FileInformationClass == FileValidDataLengthInformation ||
FileInformationClass == FileAllocationInformation)
{
FLT_PREOP_CALLBACK_STATUS FltOplockStatus;
FltOplockStatus = FltCheckOplock( &Fcb->Oplock,
Data,
IrpContext,
NULL,
NULL );
if (FltOplockStatus != FLT_PREOP_SUCCESS_WITH_CALLBACK )
{
try_return( Data->IoStatus.Status );
}
ExAcquireFastMutex(Fcb->Header.FastMutex);
if ( FltOplockIsFastIoPossible(&Fcb->Oplock) )
{
if ( Fcb->FileLock &&
Fcb->FileLock->FastIoIsQuestionable )
{
Fcb->Header.IsFastIoPossible = FastIoIsQuestionable;
}
else
{
Fcb->Header.IsFastIoPossible = FastIoIsPossible;
}
}
else
{
Fcb->Header.IsFastIoPossible = FastIoIsNotPossible;
}
ExReleaseFastMutex(Fcb->Header.FastMutex);
if(!Fcb->IsEnFile && BooleanFlagOn(Fcb->FileType,FILE_ACCESS_WRITE_CHANGE_TO_ENCRYPTION)) //文件修改过需要转变成加密文件
{
//对文件进行加密
DbgPrint("对非加密文件加密\n");
Status = TransformFileToEncrypted(Data,FltObjects,Fcb,Ccb);
if(!NT_SUCCESS(Status))
{
try_return( Status );
}
}
}
if ((FileInformationClass == FileDispositionInformation) ||
(FileInformationClass == FileRenameInformation))
{
//互斥取得资源
if (!ExAcquireResourceExclusiveLite( volCtx->VolResource, BooleanFlagOn(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT)))
{
Status = X70FsdPostRequest( Data,IrpContext);
NonCachedIoPending = TRUE;
IrpContext = NULL;
try_return( Status );
}
AcquireVolResource = TRUE;
}
if (!X70FsdAcquireExclusiveFcb( IrpContext, Fcb )) //
{
Status = X70FsdPostRequest( Data,IrpContext);
NonCachedIoPending = TRUE;
IrpContext = NULL;
try_return( Status );
}
FcbAcquired = TRUE;
ExAcquireResourceSharedLite(Fcb->EncryptResource,TRUE);
EncryptResourceAcquired = TRUE;
if(Ccb->StreamFileInfo.StreamObject == NULL)
{
try_return(Status = STATUS_FILE_DELETED);
}
//DbgPrint("收到SetInformation %ws \n",FileObject->FileName.Buffer);
switch(FileInformationClass)
{
case FileAllocationInformation:
{
Status = X70FsdCommonSetAllocationInfo(Data,FltObjects,IrpContext);
}
break;
case FileEndOfFileInformation:
{
Status = X70FsdCommonSetEndOfFileInfo(Data,FltObjects,IrpContext);
}
break;
case FileValidDataLengthInformation:
{
Status = X70FsdCommonSetValidDataLengthInfo(Data,FltObjects,IrpContext);
}
break;
case FilePositionInformation:
{
Status = X70FsdCommonFilePositionInfo(Data,FltObjects,IrpContext);
}
break;
case FileRenameInformation:
{
Status = X70FsdCommonRenameInfo(Data,FltObjects,IrpContext);
}
break;
case FileDispositionInformation:
{
PFILE_DISPOSITION_INFORMATION fdi;
fdi =(PFILE_DISPOSITION_INFORMATION)InfoBuffer;
if(fdi->DeleteFile )
{
if (!MmFlushImageSection( &Fcb->SectionObjectPointers,
MmFlushForDelete ))
{
Status = STATUS_CANNOT_DELETE;
break;
}
SetFlag( Fcb->FcbState, SCB_STATE_DELETE_ON_CLOSE );
FileObject->DeletePending = TRUE;
Delete = TRUE;
}
else
{
ClearFlag( Fcb->FcbState, SCB_STATE_DELETE_ON_CLOSE );
FileObject->DeletePending = FALSE;
}
}
default:
{
//DbgPrint("收到请求 %d \n",FileInformationClass);
Status = FltAllocateCallbackData(FltObjects->Instance,Ccb->StreamFileInfo.StreamObject,&RetNewCallbackData);
if(NT_SUCCESS(Status))
{
RtlCopyMemory(RetNewCallbackData->Iopb,Data->Iopb,sizeof(FLT_IO_PARAMETER_BLOCK));
ClearFlag(RetNewCallbackData->Iopb->IrpFlags,IRP_PAGING_IO);
RetNewCallbackData->Iopb->TargetFileObject = Ccb->StreamFileInfo.StreamObject;
if(IsMyFakeFcb(RetNewCallbackData->Iopb->Parameters.SetFileInformation.ParentOfTarget )) //如果是一个我们的对象换掉他
{
TargetCcb = RetNewCallbackData->Iopb->Parameters.SetFileInformation.ParentOfTarget->FsContext2;
if(TargetCcb != NULL)
{
RetNewCallbackData->Iopb->Parameters.SetFileInformation.ParentOfTarget = TargetCcb->StreamFileInfo.StreamObject;
}
else
{
try_return(Status = STATUS_ACCESS_DENIED;);
}
}
FltPerformSynchronousIo(RetNewCallbackData);
Data->IoStatus = RetNewCallbackData->IoStatus;
Status = RetNewCallbackData->IoStatus.Status;