+Check neccessarities of IRP_MJ_READ.
[captive.git] / src / TraceFS / checktrace.pl
1 #! /usr/bin/perl
2
3 # $Id$
4 # Checks assumptions on Cc* (Cache Manager) behaviour by reading TraceFS log
5 # Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; exactly version 2 of June 1991 is required
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 use strict;
22 use warnings;
23 use Data::Dumper;
24 use Carp qw(cluck confess);
25
26
27 my $filter=0;
28 $Data::Dumper::Sortkeys=1;
29 my $ntfs_blocksize=0x200;
30
31 # $Object->{"by"}="CcSomeFunction";
32 # $Object->{"line_enter"}=123;
33 # $Object->{"line_leave"}=124;
34 # $Object->{"ProcessThread"}="0x12345678/0x12345678";
35 # $Object->{"data"}[dataline]{"FileObject"}="0x12345678";
36 # $Object->{"data"}[dataline]{"FileName"}="\filename" or undef() if NULL;
37 # $Object->{"data"}[dataline]{"Flags"}="0x40100";
38 # $Object->{"data"}[dataline]{"SectionObjectPointer"}="0x12345678";
39 # $Object->{"data"}[dataline]{"SharedCacheMap"}="0x12345678";
40 # $FileObject{$FileObject}{"FileObject"}="0x12345678";
41 # $FileObject{$FileObject}{"SectionObjectPointer"}="0x12345678";
42 # $SectionObjectPointer{$SectionObjectPointer}{"SectionObjectPointer"}="0x12345678";
43 # $SectionObjectPointer{$SectionObjectPointer}{"SharedCacheMap"}="0x12345678";
44 # $SharedCacheMap{$SharedCacheMap}{"SharedCacheMap"}="0x12345678";
45 # $SharedCacheMap{$SharedCacheMap}{"SectionObjectPointer"}="0x12345678";
46 # $SharedCacheMap{$SharedCacheMap}{"AllocationSize"}="0x12345";
47 # $SharedCacheMap{$SharedCacheMap}{"FileSize"}="0x12345";
48 # $SharedCacheMap{$SharedCacheMap}{"ref_count"}=1;
49 # $SharedCacheMap{$SharedCacheMap}{"map"}="0x12345678" (Bcb);
50 # $SharedCacheMap{$SharedCacheMap}{"pin"}{"0x1000"}="0x12345678" (Bcb) if !Bcb->{"OwnerPointer"};
51 # $SharedCacheMap{$SharedCacheMap}{"PinAccess"}=0 or 1;
52 # $SharedCacheMap{$SharedCacheMap}{"LogHandle"}="0x12345678" optional;
53 # $SharedCacheMap{$SharedCacheMap}{"AcquireForLazyWrite"}=0;    # count
54 # $SharedCacheMap{$SharedCacheMap}{"in_memory"}{"0x4000"}=1;    # mapped page?
55 # $LogHandle{$LogHandle}{"LogHandle"}="0x12345678";
56 # $Bcb{$Bcb}{"Bcb"}="0x12345678";
57 # $Bcb{$Bcb}{"SharedCacheMap"}="0x12345678";
58 # $Bcb{$Bcb}{"type"}="pin" or "map";
59 # $Bcb{$Bcb}{"ref_count"}=1;
60 # $Bcb{$Bcb}{"FileOffset"}="0x1000" if {"type"} eq "pin";
61 # $Bcb{$Bcb}{"Buffer"}="0x12345678";    # PAGE_SIZE-aligned for "pin", FileOffset_0-aligned for "map"
62 # $Bcb{$Bcb}{"OwnerPointer"}="0x12345678" optional;
63 # $Bcb{$Bcb}{"Lsn"}="0x12345678" optional;
64 # $Bcb{$Bcb}{"dirty"}=1 optional;
65 # $MdlChain{$MdlChain}{"MdlChain"}="0x12345678";
66 # $MdlChain{$MdlChain}{"FileObject"}="0x12345678";
67 # $MdlChain{$MdlChain}{"FileOffset"}="0x5000";
68 # $MdlChain{$MdlChain}{"Length"}="0x9000";
69
70 my %FileObject;
71 my %LogHandle;
72 my %SectionObjectPointer;
73 my %SharedCacheMap;
74 my %Bcb;
75 my %MdlChain;
76 my %LastLeave;  # $ProcessThread=>[$Object,$Object,...]
77 my $LastLeave;  # ref copy of the last item for the current $ProcessThread
78 my $ProcessThread;
79 my %EnterLeave;
80 my $EnterLeave; # ref copy of the list for the current $ProcessThread
81
82 END {
83         print Data::Dumper->Dump([\%FileObject,\%SectionObjectPointer,\%SharedCacheMap,\%Bcb],
84                                [qw(%FileObject  %SectionObjectPointer  %SharedCacheMap  %Bcb)]) if !$filter;
85         }
86
87 my $Object;
88
89 sub tohex($)
90 {
91 my($num)=@_;
92
93         return sprintf("0x%X",$num);
94 }
95
96 sub FObject($)
97 {
98 my($FileObject)=@_;
99
100         my $FObject=$FileObject{$FileObject};
101         if (!$FObject) {
102                 my($package,$filename,$line,$subroutine)=caller 0;
103                 warn "Non-existent FileObject $FileObject by line $line";
104                 }
105         return $FObject;
106 }
107
108 sub delete_FObject($)
109 {
110 my($FObject)=@_;
111
112         my $FileObject=$FObject->{"FileObject"};
113         delete $FileObject{$FileObject};
114 }
115
116 sub SObject($)
117 {
118 my($SectionObjectPointer)=@_;
119
120         my $SObject=$SectionObjectPointer{$SectionObjectPointer};
121         if (!$SObject) {
122                 my($package,$filename,$line,$subroutine)=caller 0;
123                 warn "Non-existent SectionObjectPointer $SectionObjectPointer by line $line"
124                 }
125         return $SObject;
126 }
127
128 sub SObject_from_FileObject($)
129 {
130 my($FileObject)=@_;
131
132         return if !(my $FObject=FObject $FileObject);
133         my $SObject=SObject $FObject->{"SectionObjectPointer"};
134         if (!$SObject) {
135                 my($package,$filename,$line,$subroutine)=caller 0;
136                 warn "by line $line";
137                 }
138         return $SObject;
139 }
140
141 sub delete_CObject($)
142 {
143 my($CObject)=@_;
144
145         my $SharedCacheMap=$CObject->{"SharedCacheMap"};
146         do { warn "Trailing map $_ of SharedCacheMap $SharedCacheMap during its deletion" if $_; } for ($CObject->{"map"});
147         do { warn "Trailing pin $_ of SharedCacheMap $SharedCacheMap during its deletion" if $_; } for (values(%{$CObject->{"pin"}}));
148         if (my $LogHandle=$CObject->{"LogHandle"}) {
149                 do { warn "INTERNAL: Missing LogHandle $LogHandle for SharedCacheMap $SharedCacheMap"; return; }
150                         if !(my $LObject=$LogHandle{$LogHandle});
151                 # Do not delete $LogHandle as it may be used by many SharedCacheMap-s
152                 }
153         delete $SharedCacheMap{$SharedCacheMap};
154 }
155
156 sub CObject($)
157 {
158 my($SharedCacheMap)=@_;
159
160         my $CObject=$SharedCacheMap{$SharedCacheMap};
161         if (!$CObject) {
162                 my($package,$filename,$line,$subroutine)=caller 0;
163                 warn "Non-existent SharedCacheMap $SharedCacheMap by line $line";
164                 }
165         return $CObject;
166 }
167
168 sub CObject_from_FileObject($)
169 {
170 my($FileObject)=@_;
171
172         return if !(my $SObject=SObject_from_FileObject $FileObject);
173         return if !(my $CObject=CObject $SObject->{"SharedCacheMap"});
174         return $CObject;
175 }
176
177 sub SharedCacheMap_valid($)
178 {
179 my($SharedCacheMap)=@_;
180
181         cluck if !defined $SharedCacheMap;
182         return 0 if "0x".("F"x8) eq $SharedCacheMap;
183         return 0 if !eval $SharedCacheMap;
184         return 1;
185 }
186
187 sub check_data($)
188 {
189 my($data)=@_;
190
191         if (!eval $data->{"SectionObjectPointer"}) {
192                 return if $Object->{"by"} eq "IRP_MJ_CREATE";   # SectionObjectPointer is not yet initialized
193                 warn "Existing FileObject ".$data->{"FileObject"}." but no SectionObjectPointer found"
194                                 if $FileObject{$data->{"FileObject"}} && eval($FileObject{$data->{"FileObject"}}{"SectionObjectPointer"});
195                 return;
196                 }
197         my $SectionObjectPointer=$data->{"SectionObjectPointer"};
198         if (!SharedCacheMap_valid $data->{"SharedCacheMap"} && $SectionObjectPointer{$SectionObjectPointer}) {
199                 return if !(my $SObject=SObject $SectionObjectPointer);
200                 my $SharedCacheMap=$SObject->{"SharedCacheMap"};
201                 return if !eval $SharedCacheMap;
202                 my $CObject=CObject $SharedCacheMap;
203                 warn "Existing SectionObjectPointer ".$data->{"SectionObjectPointer"}." but no SharedCacheMap found,"
204                                                 ." ref_count of SharedCacheMap is ".$CObject->{"ref_count"}
205                                 if $CObject->{"ref_count"};
206 #                               if $SectionObjectPointer{$data->{"SectionObjectPointer"}};
207                 # SharedCacheMap was droppped by async task as it had ref_count==0.
208                 delete_CObject $CObject;
209                 $SObject->{"SharedCacheMap"}=tohex(0);
210                 # FileObject is still valid!
211                 return;
212                 }
213         return if !$FileObject{$data->{"FileObject"}};
214         return if !(my $FObject=FObject $data->{"FileObject"});
215         return if !(my $SObject=SObject $FObject->{"SectionObjectPointer"});
216         my $SharedCacheMap=$SObject->{"SharedCacheMap"};
217         warn "FileObject ".$FObject->{"FileObject"}." SectionObjectPointer ".$SObject->{"SectionObjectPointer"}
218                                         ." expected SharedCacheMap $SharedCacheMap"
219                                         ." but found SharedCacheMap ".$data->{"SharedCacheMap"}
220                         if $SharedCacheMap ne $data->{"SharedCacheMap"};
221         warn "INTERNAL: SharedCacheMap $SharedCacheMap of FileObject ".$FObject->{"FileObject"}." got destroyed"
222                         if !$SharedCacheMap{$SharedCacheMap};
223 }
224
225 sub CcInitializeCacheMap($$$$$)
226 {
227 my($FileObject,$AllocationSize,$FileSize,$ValidDataLength,$PinAccess)=@_;
228
229         $ValidDataLength=$FileSize if $ValidDataLength==eval("0x".("F"x8));
230         $Object->{"ref_count"}=1;
231         $Object->{"AllocationSize"}=tohex($AllocationSize);
232         $Object->{"FileSize"}=tohex($FileSize);
233         $Object->{"ValidDataLength"}=tohex($ValidDataLength);
234         $Object->{"map"}=undef();
235         $Object->{"pin"}={};
236         $Object->{"PinAccess"}=$PinAccess;
237         $Object->{"FileObject"}=$FileObject;
238 }
239
240 sub CcInitializeCacheMap_leave()
241 {
242         my $SharedCacheMap=$Object->{"data"}[1]{"SharedCacheMap"};
243         $Object->{"SharedCacheMap"}=$SharedCacheMap;
244         my $old=$SharedCacheMap{$SharedCacheMap};
245         if (!SharedCacheMap_valid $Object->{"data"}[0]{"SharedCacheMap"} && $old) {
246                 # SharedCacheMap got deleted in the meantime
247                 delete_CObject CObject $SharedCacheMap;
248                 my $SObject=SObject $Object->{"data"}[0]{"SectionObjectPointer"};
249                 $SObject->{"SharedCacheMap"}=tohex(0);
250                 $old=undef();
251                 }
252         if (!$old != !SharedCacheMap_valid $Object->{"data"}[0]{"SharedCacheMap"}) {
253                 warn "Expecting old SharedCacheMap validity ".(!!$old)
254                                 ." but found old SharedCacheMap ".$Object->{"data"}[0]{"SharedCacheMap"};
255                 }
256         warn "New SharedCacheMap ".$Object->{"data"}[1]{"SharedCacheMap"}." is not valid"
257                         if !SharedCacheMap_valid $Object->{"data"}[1]{"SharedCacheMap"};
258         if (SharedCacheMap_valid $Object->{"data"}[0]{"SharedCacheMap"}) {
259                 warn "Existing SharedCacheMap changed"
260                                                 ." from ".$Object->{"data"}[0]{"SharedCacheMap"}." to ".$Object->{"data"}[1]{"SharedCacheMap"}
261                                 if $Object->{"data"}[0]{"SharedCacheMap"} ne $Object->{"data"}[1]{"SharedCacheMap"};
262                 }
263         if ($old) {
264                 for my $field (qw(AllocationSize FileSize PinAccess)) {
265                         warn "SharedCacheMap $SharedCacheMap old instance $field ".$old->{$field}
266                                                         ." != new instance $field ".$Object->{$field}
267                                         if $old->{$field} ne $Object->{$field};
268                         }
269                 do { warn "Existing map Bcb $_ during CcInitializeCacheMap()" if $_; } for ($old->{"map"});
270                 do { warn "Existing pin Bcb $_ during CcInitializeCacheMap()" if $_; } for (values(%{$old->{"pin"}}));
271                 $Object->{"ref_count"}+=$old->{"ref_count"};
272                 }
273         $SharedCacheMap{$SharedCacheMap}=$Object;
274
275         warn "Changed SectionObjectPointer inside CcInitializeCacheMap()"
276                                         ." from ".$Object->{"data"}[0]{"SectionObjectPointer"}." to ".$Object->{"data"}[1]{"SectionObjectPointer"}
277                         if $Object->{"data"}[0]{"SectionObjectPointer"} ne $Object->{"data"}[1]{"SectionObjectPointer"};
278         my $SectionObjectPointer=$Object->{"data"}[1]{"SectionObjectPointer"};
279
280         my $FileObject=$Object->{"FileObject"};
281         if (my $FObject=$FileObject{$FileObject}) {
282                 if (my $SObject=$SectionObjectPointer{$FObject->{"SectionObjectPointer"}}) {
283                         warn "Changed SectionObjectPointer of FileObject $FileObject"
284                                                         ." from ".$FObject->{"SectionObjectPointer"}." to ".$SectionObjectPointer
285                                         if $FObject->{"SectionObjectPointer"} ne $SectionObjectPointer;
286                         }
287                 # Otherwise SectionObjectPointer could be deleted and rebuilt async in the meantime.
288                 }
289         $FileObject{$FileObject}={
290                         "FileObject"=>$FileObject,
291                         "SectionObjectPointer"=>$SectionObjectPointer,
292                         };
293
294         if (my $SObject=$SectionObjectPointer{$SectionObjectPointer}) {
295                 warn "Changed SharedCacheMap of SectionObjectPointer $SectionObjectPointer"
296                                                 ." from ".$SObject->{"SharedCacheMap"}." to ".$SharedCacheMap
297                                 if $SObject->{"SharedCacheMap"} ne $SharedCacheMap && eval($SObject->{"SharedCacheMap"});
298                 }
299         $SectionObjectPointer{$SectionObjectPointer}={
300                         "SectionObjectPointer"=>$SectionObjectPointer,
301                         "SharedCacheMap"=>$SharedCacheMap,
302                         };
303
304         CcSetFileSizes($FileObject,map({ eval($Object->{$_}); } qw(AllocationSize FileSize ValidDataLength)));
305         delete $Object->{$_} for (qw(FileObject ValidDataLength));
306 }
307
308 sub CcUninitializeCacheMap($$)
309 {
310 my($FileObject,$TruncateSize)=@_;
311
312         $Object->{"FileObject"}=$FileObject;
313 }
314
315 sub CcUninitializeCacheMap_leave($)
316 {
317 my($r)=@_;
318
319         my $FileObject=$Object->{"FileObject"};
320         # 'r' means function success.
321         # r=0 either if no CcInitializeCacheMap() was called at all
322         # or if Cc was unable to detach SharedCacheMap and it remains valid
323         # (FIXME: Do we SharedCacheMap->ref_count-- on in such case?).
324         my $SectionObjectPointer=$FileObject{$FileObject}->{"SectionObjectPointer"} if $FileObject{$FileObject};
325         my $SharedCacheMap=$SectionObjectPointer{$SectionObjectPointer}->{"SharedCacheMap"}
326                         if $SectionObjectPointer && $SectionObjectPointer{$SectionObjectPointer};
327         warn "Unexpected 'r' result $r for CcUninitializeCacheMap of FileObject $FileObject"
328                         if !(eval($SharedCacheMap) && !SharedCacheMap_valid($Object->{"data"}[1]{"SharedCacheMap"})) != !$r;
329         if (!eval $SharedCacheMap) {
330                 for my $SharedCacheMap ($Object->{"data"}[0]{"SharedCacheMap"},$Object->{"data"}[1]{"SharedCacheMap"}) {
331                         warn "Not expecting valid SharedCacheMap $SharedCacheMap"
332                                         if SharedCacheMap_valid $SharedCacheMap;
333                         }
334                 return;
335                 }
336         for my $SharedCacheMap ($Object->{"data"}[0]{"SharedCacheMap"}) {
337                 warn "Expecting valid SharedCacheMap $SharedCacheMap"
338                                 if !SharedCacheMap_valid $SharedCacheMap;
339                 }
340         return if !(my $FObject=FObject $FileObject);
341         return if !(my $SObject=SObject $FObject->{"SectionObjectPointer"});
342         return if !(my $CObject=CObject $SObject->{"SharedCacheMap"});
343         if (--$CObject->{"ref_count"}) {
344                 for my $SharedCacheMap ($Object->{"data"}[1]{"SharedCacheMap"}) {
345                         warn "Expecting still valid SharedCacheMap $SharedCacheMap after CcUninitializeCacheMap()"
346                                                         ." with ref_count=".$CObject->{"ref_count"}
347                                         if !SharedCacheMap_valid $SharedCacheMap;
348                         }
349                 return;
350                 }
351         if (!SharedCacheMap_valid $Object->{"data"}[1]{"SharedCacheMap"}) {
352                 delete_CObject $CObject;
353                 $SObject->{"SharedCacheMap"}=tohex(0);
354                 # FileObject is still valid!
355                 }
356         else {
357                 # FIXME: Do we SharedCacheMap->ref_count-- on in such case?
358                 }
359 }
360
361 sub CcSetFileSizes($$$$)
362 {
363 my($FileObject,$AllocationSize,$FileSize,$ValidDataLength)=@_;
364
365         return if !(my $CObject=CObject_from_FileObject $FileObject);
366         my $SharedCacheMap=$CObject->{"SharedCacheMap"};
367         if ($AllocationSize!=eval($CObject->{"AllocationSize"})) {
368                 do { warn "Existing map $_ of FileObject $FileObject SharedCacheMap $SharedCacheMap during CcSetAllocationSizes(),"
369                                                 ." AllocationSize=".$CObject->{"AllocationSize"} if $_; }
370                                 for ($CObject->{"map"});
371                 do { warn "Existing pin $_ of FileObject $FileObject SharedCacheMap $SharedCacheMap during CcSetAllocationSizes(),"
372                                                 ." AllocationSize=".$CObject->{"AllocationSize"} if $_; }
373                                 for (values(%{$CObject->{"pin"}}));
374                 }
375         # $ValidDataLength can be > $CObject->{"FileSize"};
376         warn "ValidDataLength ".tohex($ValidDataLength)." > FileSize ".tohex($FileSize)
377                         if $ValidDataLength>$FileSize;
378         warn "0 != AllocationSize ".tohex($AllocationSize)." % ntfs_blocksize ".tohex($ntfs_blocksize)
379                         if 0!=($AllocationSize%$ntfs_blocksize);
380         # $AllocationSize can be higher
381         warn "FileSize ".tohex($FileSize)." > AllocationSize ".tohex($AllocationSize)
382                         if $FileSize>$AllocationSize;
383         $CObject->{"FileSize"}=tohex($FileSize);
384         $CObject->{"AllocationSize"}=tohex($AllocationSize);
385 }
386
387 sub IRP_MJ_CREATE_leave()
388 {
389         do { warn "Non-NULL SectionObjectPointer $_ not expected" if eval($_); } for ($Object->{"data"}[0]{"SectionObjectPointer"});
390         my $FileObject=$Object->{"data"}[0]{"FileObject"};
391         warn "Existing FileObject $FileObject not expected" if $FileObject{$FileObject};
392         my $SectionObjectPointer=$Object->{"data"}[1]{"SectionObjectPointer"};
393         # We want to track even FileObject without SectionObjectPointer yet.
394 #       if ($SectionObjectPointer && $SectionObjectPointer{$SectionObjectPointer})
395         {
396                 $FileObject{$FileObject}={
397                                 "FileObject"=>$FileObject,
398                                 "SectionObjectPointer"=>$SectionObjectPointer,
399                                 };
400                 }
401         if (eval $SectionObjectPointer) {
402                 my $SharedCacheMap=$Object->{"data"}[1]{"SharedCacheMap"};
403                 if (my $SObject=$SectionObjectPointer{$SectionObjectPointer}) {
404                         warn "Changed SharedCacheMap from stored ".$SObject->{"SharedCacheMap"}." to ".$SharedCacheMap
405                                         if $SObject->{"SharedCacheMap"} ne $SharedCacheMap && $Object->{"by"} ne "IRP_MJ_CREATE";
406                         }
407                 $SectionObjectPointer{$SectionObjectPointer}={
408                                 "SectionObjectPointer"=>$SectionObjectPointer,
409                                 "SharedCacheMap"=>$SharedCacheMap,
410                                 };
411                 }
412 }
413
414 sub BObject($)
415 {
416 my($Bcb)=@_;
417
418         cluck if !defined $Bcb;
419         my $BObject=$Bcb{$Bcb};
420         warn "Non-existent Bcb $Bcb" if !$BObject;
421         return $BObject;
422 }
423
424 sub delete_BObject($)
425 {
426 my($BObject)=@_;
427
428         my $Bcb=$BObject->{"Bcb"};
429         warn "Deleting ref_count=".$BObject->{"ref_count"}." Bcb $Bcb" if $BObject->{"ref_count"};
430         # Do not: warn "Deleting dirty Bcb $Bcb" if $BObject->{"dirty"};
431         # as it is valid to allow sanity check below.
432         warn "Deleting dirty Bcb $Bcb" if $BObject->{"dirty"} && $BObject->{"ref_count"};
433         return if !(my $CObject=CObject $BObject->{"SharedCacheMap"});
434         if ($BObject->{"type"} eq "map") {
435                 for my $pin (values(%{$CObject->{"pin"}})) {
436                         next if !defined $pin;
437                         warn "unpin map but CcPinMappedData pin $pin still exists"
438                                         if $Bcb{$pin}->{"by"} eq "CcPinMappedData";
439                         }
440                 }
441         else {
442                 warn "unpin of pin Bcb $Bcb of SharedCacheMap ".$CObject->{"SharedCacheMap"}
443                                                 ." although FileOffset ".$BObject->{"FileOffset"}." not in_memory"
444                                 if !($CObject->{"in_memory"}{$BObject->{"FileOffset"}});
445                 # Do not: delete $CObject->{"in_memory"}{$BObject->{"FileOffset"}};
446                 # as Cache Manager is not forced to drop it.
447 #               warn "UNMARK: SharedCacheMap ".$CObject->{"SharedCacheMap"}." FileOffset ".$BObject->{"FileOffset"};
448                 }
449         for my $ref ($BObject->{"type"} eq "map" ? \$CObject->{"map"} : \$CObject->{"pin"}{$BObject->{"FileOffset"}}) {
450                 warn "Final unpin but ".$BObject->{"type"}." Bcb $Bcb not registered"
451                                                 ." in SharedCacheMap ".$CObject->{"SharedCacheMap"}." ref ".($$ref || "<undef>")
452                                 if !defined($BObject->{"OwnerPointer"}) && !($$ref && $$ref eq $Bcb)
453                                                 && !($BObject->{"ref_count"}==0 && $BObject->{"dirty"});
454                 if ($$ref && $$ref eq $Bcb) {
455                         $$ref=undef();
456                         # Do not: delete $CObject->{"pin"}{$BObject->{"FileOffset"}} if $BObject->{"type"} eq "pin";
457                         # as it would destroy $$ref slot in &Bcb_checkref '($$ref && $Bcb ne $$ref)' codepath.
458                         }
459                 }
460         delete $Bcb{$Bcb};
461 }
462
463 sub MObject($)
464 {
465 my($MdlChain)=@_;
466
467         cluck if !defined $MdlChain;
468         my $MObject=$MdlChain{$MdlChain};
469         warn "Non-existent MdlChain $MdlChain" if !$MObject;
470         return $MObject;
471 }
472
473 sub Bcb_conflict($;@)
474 {
475 my($CObject,@Bcb_list)=@_;
476
477         my $arg=0;
478         my %check=(
479                 "map"=>$CObject->{"map"},
480                 map(("arg".($arg++)=>$_),@Bcb_list),
481                 %{$CObject->{"pin"}},
482                 );
483         my %reversed;
484         my $BufferBase; # relativized to FileOffset 0
485         my $BufferBase_val;
486         while (my($key,$val)=each(%check)) {
487                 next if !defined $val;
488                 warn "Conflicting Bcb $val of keys $key and ".$reversed{$val}." of SharedCacheMap ".$CObject->{"SharedCacheMap"}
489                                 if $reversed{$val};
490                 # Buffer base should match even between 'map's and 'pin's
491                 # as the data are always mapped only once.
492                 if (my $BObject=BObject $val) {
493                         my $Buffer=eval $BObject->{"Buffer"};
494                         $Buffer-=eval($BObject->{"FileOffset"}) if exists $BObject->{"FileOffset"};
495                         warn "Non-matching Bcb ".$BObject->{"type"}." $val Buffer base ".tohex($Buffer)
496                                                         ." with Bcb ".$Bcb{$BufferBase_val}->{"type"}." $BufferBase_val BufferBase ".tohex($BufferBase)
497                                         if defined($BufferBase) && $Buffer!=$BufferBase;
498                         $BufferBase=$Buffer;
499                         $BufferBase_val=$val;
500                         }
501                 $reversed{$val}=$key;
502                 }
503 }
504
505 # New $BObject will always be forced as the last stored reference.
506 sub Bcb_checkref($$)
507 {
508 my($BObject,$ref)=@_;
509
510         return if !(my $CObject=CObject $BObject->{"SharedCacheMap"});
511         my $type=$BObject->{"type"};
512         my $Bcb=$BObject->{"Bcb"};
513         if ($$ref && $Bcb ne $$ref) {
514                 my $BObject2=$Bcb{$$ref};
515                 warn "new $type Bcb $Bcb != old ".$BObject2->{"type"}." Bcb $$ref";
516                 delete_BObject $BObject2;
517                 warn "INTERNAL: Trailing ref to Bcb $$ref" if $$ref;
518                 }
519         if ($$ref) {
520                 my $BObject2=$Bcb{$$ref};
521                 warn "new $type $Bcb type ".$BObject->{"type"}." != old type $type $$ref type ".$BObject2->{"type"}
522                                 if $BObject->{"type"} ne $BObject2->{"type"};
523                 warn "new $type $Bcb Buffer ".$BObject->{"Buffer"}." != old $type $$ref Buffer ".$BObject2->{"Buffer"}
524                                 if $BObject->{"Buffer"} ne $BObject2->{"Buffer"};
525                 }
526         if ($$ref && $$ref eq $Bcb) {
527                 $BObject->{"ref_count"}+=$Bcb{$$ref}->{"ref_count"};
528                 $$ref=undef();
529                 }
530         $Bcb{$Bcb}=$BObject;    # &Bcb_conflict needs this reference
531         Bcb_conflict $CObject,$Bcb;
532         $$ref=$Bcb;
533 }
534
535 sub map_new($;$$)
536 {
537 my($SharedCacheMap,$FileOffset,$Length)=@_;
538
539         return if !(my $CObject=CObject $SharedCacheMap);
540         if (defined($FileOffset) && defined($Length)) {
541                 warn "Mapping data (end ".tohex($FileOffset+$Length).") out of FileSize ".$CObject->{"FileSize"}
542                                 if $FileOffset+$Length>eval($CObject->{"FileSize"});
543                 }
544         $Object->{"SharedCacheMap"}=$CObject->{"SharedCacheMap"};
545         if (defined $FileOffset) {
546                 $Object->{"FileOffset"}=tohex($FileOffset);
547                 }
548         $Object->{"type"}="map";
549         $Object->{"ref_count"}=1;
550 }
551
552 sub map_new_from_FileObject($;$$)
553 {
554 my($FileObject,$FileOffset,$Length)=@_;
555
556         return if !(my $CObject=CObject_from_FileObject $FileObject);
557         map_new $CObject->{"SharedCacheMap"},$FileOffset,$Length;
558 }
559
560 sub map_new_leave($;$)
561 {
562 my($Bcb,$Buffer)=@_;
563
564         $Object->{"Bcb"}=$Bcb;
565         return if !(my $CObject=CObject $Object->{"SharedCacheMap"});
566
567         if (defined $Buffer) {
568                 $Object->{"Buffer"}=tohex(eval($Buffer)-(eval($Object->{"FileOffset"}) || 0));
569                 }
570         delete $Object->{"FileOffset"};
571
572         my $ref=\$CObject->{"map"};
573         # There may exist some pin bcbs even if we are creating the new map bcb.
574         Bcb_checkref $Object,$ref;
575 }
576
577 sub CcMapData($$$)
578 {
579 my($FileObject,$FileOffset,$Length)=@_;
580
581         map_new_from_FileObject $FileObject,$FileOffset,$Length;
582 }
583
584 sub CcMapData_leave($$)
585 {
586 my($Bcb,$Buffer)=@_;
587
588         map_new_leave $Bcb,$Buffer;
589 }
590
591 sub pin_new($$$)
592 {
593 my($FileObject,$FileOffset,$Length)=@_;
594
595         return if !(my $CObject=CObject_from_FileObject $FileObject);
596         warn "Pinning of non-PinAccess FileObject $FileObject" if !$CObject->{"PinAccess"};
597         warn "Mapping data (end ".tohex($FileOffset+$Length).") out of FileSize ".$CObject->{"FileSize"}
598                         if $FileOffset+$Length>eval($CObject->{"FileSize"});
599         warn "Pinning Length ".tohex($Length)." > 0x1000" if $Length>0x1000;
600         warn "Pinning across file page (start=".tohex($FileOffset).",end-1=".tohex($FileOffset+$Length-1).")"
601                         if ($FileOffset&~0xFFF)!=(($FileOffset+$Length-1)&~0xFFF);
602         $Object->{"SharedCacheMap"}=$CObject->{"SharedCacheMap"};
603         $Object->{"FileOffset"}=tohex($FileOffset);
604         $Object->{"type"}="pin";
605         $Object->{"ref_count"}=1;
606 }
607
608 sub pin_new_leave($$)
609 {
610 my($Bcb,$Buffer)=@_;
611
612         $Object->{"Bcb"}=$Bcb;
613         return if !(my $CObject=CObject $Object->{"SharedCacheMap"});
614         $Object->{"Buffer"}=tohex(eval($Buffer)-(eval($Object->{"FileOffset"})&0xFFF));
615         my $shift=eval($Object->{"FileOffset"})&0xFFF;
616         $Object->{"FileOffset"}=tohex(eval($Object->{"FileOffset"})-$shift);
617         $Object->{"Buffer"}=tohex(eval($Buffer)-$shift);
618
619         warn "pin_new_leave() while FileOffset ".$Object->{"FileOffset"}." not in_memory"
620                                         ." of SharedCacheMap ".$CObject->{"SharedCacheMap"}
621                         if !$CObject->{"in_memory"}{$Object->{"FileOffset"}};
622
623         my $ref=\$CObject->{"pin"}{$Object->{"FileOffset"}};
624         # There may not exist map bcb even if we are creating the new pin bcb.
625         Bcb_checkref $Object,$ref;
626 }
627
628 sub CcPinRead($$$)
629 {
630 my($FileObject,$FileOffset,$Length)=@_;
631
632         pin_new $FileObject,$FileOffset,$Length;
633 }
634
635 sub CcPinRead_leave($$)
636 {
637 my($Bcb,$Buffer)=@_;
638
639         pin_new_leave $Bcb,$Buffer;
640 }
641
642 sub CcPreparePinWrite($$$)
643 {
644 my($FileObject,$FileOffset,$Length)=@_;
645
646         return if !(my $CObject=CObject_from_FileObject $FileObject);
647         # Full pages do not need to be read:
648         if (!($FileOffset&0xFFF)) {
649                 $CObject->{"in_memory"}{tohex $FileOffset}=1;
650                 }
651
652         pin_new $FileObject,$FileOffset,$Length;
653 }
654
655 sub CcPreparePinWrite_leave($$)
656 {
657 my($Bcb,$Buffer)=@_;
658
659         pin_new_leave $Bcb,$Buffer;
660         my $BObject=BObject $Bcb;
661         $BObject->{"dirty"}=1;
662 }
663
664 sub CcPinMappedData($$$)
665 {
666 my($FileObject,$FileOffset,$Length)=@_;
667
668         pin_new $FileObject,$FileOffset,$Length;
669 }
670
671 sub CcPinMappedData_leave($)
672 {
673 my($Bcb)=@_;
674
675         return if !(my $CObject=CObject $Object->{"SharedCacheMap"});
676         do { warn "CcPinMappedData() with Bcb $Bcb on non-CcMapData()ed SharedCacheMap ".$CObject->{"SharedCacheMap"}; return; }
677                         if !(my $mapBcb=$CObject->{"map"});
678         return if !(my $BmapObject=BObject $mapBcb);
679         my $Buffer=tohex(eval($BmapObject->{"Buffer"})+eval($Object->{"FileOffset"}));
680
681         my $Bcb2=$CObject->{"pin"}{tohex(eval($Object->{"FileOffset"})&~0xFFF)};
682         my $BObject2=BObject $Bcb2 if $Bcb2;
683
684         pin_new_leave $Bcb,$Buffer;
685 }
686
687 sub CcSetDirtyPinnedData($$)
688 {
689 my($Bcb,$Lsn)=@_;
690
691         return if !(my $BObject=BObject $Bcb);
692         # Do not: warn "Lsn already set for Bcb $Bcb as ".$BObject->{"Lsn"}." while current Lsn=$Lsn" if $BObject->{"Lsn"};
693         # as it is permitted.
694         warn "Lsn goes backward for Bcb $Bcb old Lsn ".$BObject->{"Lsn"}." to a new Lsn=$Lsn"
695                         if $BObject->{"Lsn"} && eval($BObject->{"Lsn"})>eval($Lsn);
696         $BObject->{"Lsn"}=$Lsn if $Lsn ne "0x".("F"x8);
697         $BObject->{"dirty"}=1;
698         return if !(my $CObject=CObject $BObject->{"SharedCacheMap"});
699 }
700
701 sub FlushToLsnRoutine($$)
702 {
703 my($LogHandle,$Lsn)=@_;
704
705         $Object->{"LogHandle"}=$LogHandle;
706         $Object->{"Lsn"}=$Lsn;
707 }
708
709 my $LogHandle_static;
710 sub CcSetLogHandleForFile($$$)
711 {
712 my($FileObject,$LogHandle,$FlushToLsnRoutine)=@_;
713
714         return if !(my $CObject=CObject_from_FileObject $FileObject);
715         warn "LogHandle ".$CObject->{"LogHandle"}." already exists for SharedCacheMap ".$CObject->{"SharedCacheMap"}
716                         if $CObject->{"LogHandle"};
717         return if !eval $LogHandle;     # $LogHandle may be "0x0"
718         # ntfs.sys uses single LogHandle for its whole session:
719         warn "Non-unique LogHandle $LogHandle while last LogHandle was $LogHandle_static"
720                         if $LogHandle_static && $LogHandle ne $LogHandle_static;
721         $CObject->{"LogHandle"}=$LogHandle;
722         if (!$LogHandle{$LogHandle}) {
723                 $LogHandle{$LogHandle}={
724                                 "LogHandle"=>$LogHandle,
725                                 };
726                 }
727 }
728
729 sub IRP_MJ_WRITE_leave_page($$)
730 {
731 my($ByteOffset,$Lsn_check)=@_;
732
733         my $SharedCacheMap=$Object->{"data"}[0]{"SharedCacheMap"};
734         return if !(my $CObject=CObject $SharedCacheMap);
735         my $FlushToLsnRoutine=$LastLeave if $LastLeave->{"by"} eq "FlushToLsnRoutine";
736         # Do not: my $Bcb=$CObject->{"pin"}{$ByteOffset};
737         # as Bcbs with $BObject->{"OwnerPointer"} are no longer stored in $CObject->{"pin"}.
738         my @Bcbs;
739         for my $Bcb (keys(%Bcb)) {
740                 my $BObject=BObject $Bcb;
741                 if (1
742                                 && $BObject->{"type"} eq "pin"
743                                 && $BObject->{"SharedCacheMap"} eq $SharedCacheMap
744                                 && $BObject->{"FileOffset"} eq $ByteOffset) {
745                         push @Bcbs,$Bcb;
746                         }
747                 }
748         if (!@Bcbs) {
749                 do {
750                                 warn "Non-Bcb IRP_MJ_WRITE ByteOffset=$ByteOffset as non-toplevel function"
751                                                 ." (".join(",",map({ $_->{"line_enter"}.":".$_->{"by"}; } @$EnterLeave)).")";
752 #                               warn Dumper $CObject;
753                                 # Direct IRP_MJ_WRITE can be from callbacked 'FlushToLsnRoutine'.
754                                 # It can occur even from other callbacks ('DirtyPageRoutine' etc.)
755                                 # but it was not needed here yet.
756                                 } if @$EnterLeave && !(${$EnterLeave}[$#$EnterLeave]->{"by"} eq "FlushToLsnRoutine");
757                 warn "Non-Bcb IRP_MJ_WRITE ByteOffset=$ByteOffset but FlushToLsnRoutine was preceding"
758                                 if $FlushToLsnRoutine;
759                 return;
760                 }
761         $CObject->{"in_memory"}{$ByteOffset}=1;
762         warn "Ambiguous matching Bcbs ".join(",",@Bcbs)
763                                         ." to SharedCacheMap $SharedCacheMap WRITE ByteOffset $ByteOffset"
764                         if @Bcbs>=2;
765         my $Bcb=$Bcbs[0];
766         return if !(my $BObject=BObject $Bcb);
767         warn "IRP_MJ_WRITE on non-dirty Bcb $Bcb" if !$BObject->{"dirty"};
768         if ($FlushToLsnRoutine) {
769                 push @$Lsn_check,{
770                                 "Bcb"=>$Bcb,
771                                 "Bcb_Lsn",$BObject->{"Lsn"},
772                                 } if $Lsn_check;
773                 }
774         else {
775                 warn "Missing preceding FlushToLsnRoutine during IRP_MJ_WRITE of Bcb $Bcb with Lsn ".$BObject->{"Lsn"}
776                                 if $BObject->{"Lsn"};
777                 }
778         warn "IRP_MJ_WRITE with FlushToLsnRoutine although not in AcquireForLazyWrite"
779                         if $FlushToLsnRoutine && !($CObject->{"AcquireForLazyWrite"}>=1);
780         warn "IRP_MJ_WRITE not the toplevel function"
781                                                 ." (".join(",",map({ $_->{"line_enter"}.":".$_->{"by"}; } @$EnterLeave)).")"
782                         if !(0==@$EnterLeave
783                          || (1==@$EnterLeave && ${$EnterLeave}[0]->{"by"} eq "CcFlushCache")
784                          || (2==@$EnterLeave && ${$EnterLeave}[0]->{"by"} eq "IRP_MJ_FILE_SYSTEM_CONTROL"
785                                              && ${$EnterLeave}[1]->{"by"} eq "CcFlushCache"));
786         my $CcFlushCache=${$EnterLeave}[$#$EnterLeave];
787         if ($CcFlushCache && $CcFlushCache->{"by"} eq "CcFlushCache") {
788                 $CcFlushCache->{"CcFlushCached"}++;
789                 if ($CcFlushCache->{"FileOffset"} ne "0x".("F"x8) || $CcFlushCache->{"Length"} ne "0x0") {
790                         warn "IRP_MJ_WRITE outside of range of active CcFlushCache()"
791                                         if eval($ByteOffset)< eval($CcFlushCache->{"FileOffset"})
792                                         || eval($ByteOffset)>=eval($CcFlushCache->{"FileOffset"})+eval($CcFlushCache->{"Length"});
793                         }
794                 }
795         # Keep $BObject->{"dirty"} there for &delete_BObject sanity checks.
796         delete_BObject $BObject if $BObject->{"dirty"} && !$BObject->{"ref_count"};
797 }
798
799 sub IRP_MJ_WRITE_leave()
800 {
801         do { warn "Length $_ not divisible by 0x1000" if eval($_)%0x1000; } for ($Object->{"WRITE"}{"Length"});
802         my @Lsn_check;
803         for my $reloffs (0..(eval($Object->{"WRITE"}{"Length"})/0x1000)-1) {
804                 IRP_MJ_WRITE_leave_page tohex(eval($Object->{"WRITE"}{"ByteOffset"})+0x1000*$reloffs),\@Lsn_check;
805                 }
806
807         if ($LastLeave->{"by"} eq "FlushToLsnRoutine" && (my $FlushToLsnRoutine=$LastLeave)) {
808                 my $Lsn_max;
809                 for (@Lsn_check) {
810                         my $Lsn=eval $_->{"Bcb_Lsn"};
811                         $Lsn_max=$Lsn if !defined($Lsn_max) || $Lsn_max<$Lsn;
812                         }
813                 warn "FlushToLsnRoutine of line_enter ".$FlushToLsnRoutine->{"line_enter"}
814                                                 ." got Lsn ".$FlushToLsnRoutine->{"Lsn"}." although Bcbs have "
815                                                 .join(",",map({ "(".$_->{"Bcb"}.":".$_->{"Bcb_Lsn"}.")"; } @Lsn_check))
816                                 if tohex($Lsn_max) ne $FlushToLsnRoutine->{"Lsn"};
817                 }
818 }
819
820 sub IRP_MJ_READ_leave()
821 {
822         # toplevel IRP_MJ_READ has no requirements
823         return if (0==@$EnterLeave);
824         my @stack=map({ $_->{"by"}=~/^IRP_MJ_/ ? () : $_ } @$EnterLeave);
825         my $opObject=$stack[0] if 1==@stack;
826         warn "IRP_MJ_READ not the expected function stack"
827                                                 ." (".join(",",map({ $_->{"line_enter"}.":".$_->{"by"}; } @$EnterLeave)).")"
828                         if !($opObject->{"by"} eq "CcMapData"
829                           || $opObject->{"by"} eq "CcCopyRead"
830                           || $opObject->{"by"} eq "CcMdlRead"
831                           || $opObject->{"by"} eq "CcPinRead");
832         if ($opObject->{"by"} eq "CcMdlRead") {
833                 do { warn "Length $_ not divisible by 0x1000" if eval($_)%0x1000; } for ($Object->{"READ"}{"Length"});
834                 }
835         else {
836                 do { warn "Length $_ not 0x1000" if eval($_)!=0x1000; } for ($Object->{"READ"}{"Length"});
837                 }
838         my $SharedCacheMap=$Object->{"data"}[0]{"SharedCacheMap"};
839         return if !(my $CObject=CObject $SharedCacheMap);
840         for my $reloffs (0..eval($Object->{"READ"}{"Length"})/0x1000-1) {
841                 my $ByteOffset=tohex(eval($Object->{"READ"}{"ByteOffset"})+$reloffs*0x1000);
842                 # Do not: warn "Reading ByteOffset $ByteOffset into SharedCacheMap $SharedCacheMap twice"
843                 #             if $CObject->{"in_memory"}{$ByteOffset};
844                 # as it may be still cached there as Cache Manager is not forced to drop it.
845                 $CObject->{"in_memory"}{$ByteOffset}=1;
846 #               warn "MARK: SharedCacheMap ".$CObject->{"SharedCacheMap"}." FileOffset $ByteOffset";
847                 }
848 }
849
850 sub CcPurgeCacheSection($$$$$)
851 {
852 my($SectionObjectPointer,$SharedCacheMap,$FileOffset,$Length,$UninitializeCacheMaps)=@_;
853
854         return if !(my $CObject=CObject $SharedCacheMap);
855         warn "Unexpected UninitializeCacheMaps $UninitializeCacheMaps" if $UninitializeCacheMaps ne "0";
856         my $all=($FileOffset eq "0x".("F"x8) && !eval $Length);
857         warn "Not yet implemented ranged CcPurgeCacheSection()" if !$all;
858         do { warn "Existing map Bcb $_ during CcPurgeCacheSection()" if $_; } for ($CObject->{"map"});
859         do { warn "Existing pin Bcb $_ during CcPurgeCacheSection()" if $_; } for (values(%{$CObject->{"pin"}}));
860         delete $CObject->{"in_memory"};
861 }
862
863 sub CcFlushCache($$$$)
864 {
865 my($SectionObjectPointer,$SharedCacheMap,$FileOffset,$Length)=@_;
866
867         $Object->{"CcFlushCached"}=0;
868         $Object->{"FileOffset"}=$FileOffset;
869         $Object->{"Length"}=$Length;
870 }
871
872 sub CcFlushCache_leave($$)
873 {
874 my($Status,$Information)=@_;
875
876         warn "CcFlushCache() not the toplevel function"
877                                                 ." (".join(",",map({ $_->{"line_enter"}.":".$_->{"by"}; } @$EnterLeave)).")"
878                         if !(0==@$EnterLeave
879                          || (1==@$EnterLeave && ${$EnterLeave}[0]->{"by"} eq "IRP_MJ_FILE_SYSTEM_CONTROL"));
880         if ($Status ne "0x".("F"x8) || $Information ne "0x".("F"x8)) {
881                 warn "Unexpected Status $Status" if eval $Status;
882                 warn "Unexpected Information $Information while CcFlushCached=".$Object->{"CcFlushCached"}
883                                 if eval($Information)!=eval($Object->{"CcFlushCached"})*0x1000;
884                 }
885 }
886
887 sub CcPrepareMdlWrite($$$)
888 {
889 my($FileObject,$FileOffset,$Length)=@_;
890
891         $Object->{"FileObject"}=$FileObject;
892         warn "FileOffset $FileOffset not divisible by 0x1000" if eval($FileOffset)%0x1000;
893         $Object->{"FileOffset"}=$FileOffset;
894         warn "Length $Length not divisible by 0x1000" if eval($Length)%0x1000;
895         $Object->{"Length"}=$Length;
896 }
897
898 sub CcPrepareMdlWrite_leave($$$)
899 {
900 my($MdlChain,$Status,$Information)=@_;
901
902         do { warn "Unexpected Status $Status"; return; } if eval $Status;
903         warn "Unexpected Information $Information" if $Information ne $Object->{"Length"};
904         warn "MdlChain $MdlChain already exists" if $MdlChain{$MdlChain};
905         $MdlChain{$MdlChain}=$Object;
906 }
907
908 sub CcMdlWriteComplete($$$)
909 {
910 my($FileObject,$FileOffset,$MdlChain)=@_;
911
912         return if !(my $MObject=MObject $MdlChain);
913         warn "CcMdlWriteComplete() parameter FileObject $FileObject"
914                                         ." not matching MdlChain $MdlChain FileObject ".$MObject->{"FileObject"}
915                         if $FileObject ne $MObject->{"FileObject"};
916         warn "CcMdlWriteComplete() parameter FileOffset $FileOffset"
917                                         ." not matching MdlChain $MdlChain FileOffset ".$MObject->{"FileOffset"}
918                         if $FileOffset ne $MObject->{"FileOffset"};
919         # Propose MdlChain to a simulated Bcb.
920         # We must split it by pages as pin can be just 0x1000 sized.
921         return if !(my $CObject=CObject_from_FileObject $MObject->{"FileObject"});
922         for my $reloffs (0..eval($MObject->{"Length"})/0x1000-1) {
923                 my $BObject={ %$MObject };
924                 $BObject->{"Bcb"}="MdlChain $MdlChain reloffs $reloffs";
925                 $BObject->{"FileOffset"}=tohex(eval($MObject->{"FileOffset"})+$reloffs*0x1000);
926                 $BObject->{"SharedCacheMap"}=$CObject->{"SharedCacheMap"};
927                 $BObject->{"type"}="pin";
928                 $BObject->{"ref_count"}=0;
929                 $BObject->{"dirty"}=1;
930                 warn "Bcb ".$BObject->{"Bcb"}." already exists" if $Bcb{$BObject->{"Bcb"}};
931                 $Bcb{$BObject->{"Bcb"}}=$BObject;
932                 }
933         delete $MdlChain{$MdlChain};
934 }
935
936 sub CcMdlWriteAbort($$)
937 {
938 my($FileObject,$MdlChain)=@_;
939
940         warn "CcMdlWriteAbort() not handled";
941 }
942
943 sub AcquireForLazyWrite_leave($)
944 {
945 my($r)=@_;
946
947         warn "Unexpected 'r' $r" if $r ne "1";
948         warn "AcquireForLazyWrite() not the toplevel function" if @$EnterLeave;
949         return if !(my $CObject=CObject $Object->{"data"}[0]{"SharedCacheMap"});
950         $CObject->{"AcquireForLazyWrite"}++;
951 }
952
953 sub ReleaseFromLazyWrite_leave()
954 {
955         warn "ReleaseFromLazyWrite() not the toplevel function" if @$EnterLeave;
956         return if !(my $CObject=CObject $Object->{"data"}[0]{"SharedCacheMap"});
957         warn "Invalid 'AcquireForLazyWrite' value ".$CObject->{"AcquireForLazyWrite"}
958                         if !($CObject->{"AcquireForLazyWrite"}>=1);
959         $CObject->{"AcquireForLazyWrite"}--;
960 }
961
962 sub CcRemapBcb($)
963 {
964 my($Bcb)=@_;
965
966         return if !(my $BObject=BObject $Bcb);
967         map_new $BObject->{"SharedCacheMap"};
968         $Object->{"Buffer"}=tohex(eval($BObject->{"Buffer"})-eval($BObject->{"FileOffset"} || 0));
969 }
970
971 sub CcRemapBcb_leave($)
972 {
973 my($r)=@_;
974
975         map_new_leave $r;
976 }
977
978 sub unpin($)
979 {
980 my($Bcb)=@_;
981
982         return if !(my $BObject=BObject $Bcb);
983         return if --$BObject->{"ref_count"};
984         if ($BObject->{"dirty"}) {
985                 # last unpin of unreferenced dirty Bcb will no longer allow reincarnation
986                 # of the same Bcb to the pin map of its SharedCacheMap.
987                 return if !(my $CObject=CObject $BObject->{"SharedCacheMap"});
988                 warn "unpin() of pin Bcb $Bcb but it is already not registered in SharedCacheMap ".$BObject->{"SharedCacheMap"}." pin map"
989                                 if (!$CObject->{"pin"}{$BObject->{"FileOffset"}} || $CObject->{"pin"}{$BObject->{"FileOffset"}} ne $Bcb)
990                                                 && !$BObject->{"OwnerPointer"};
991                 delete $CObject->{"pin"}{$BObject->{"FileOffset"}}
992                                 if $CObject->{"pin"}{$BObject->{"FileOffset"}} && ($CObject->{"pin"}{$BObject->{"FileOffset"}} eq $Bcb);
993                 return;
994                 }
995         delete_BObject $BObject;
996 }
997
998 sub CcUnpinData($)
999 {
1000 my($Bcb)=@_;
1001
1002         unpin $Bcb;
1003 }
1004
1005 sub CcUnpinDataForThread($)
1006 {
1007 my($Bcb)=@_;
1008
1009         unpin $Bcb;
1010 }
1011
1012 sub CcSetBcbOwnerPointer($$)
1013 {
1014 my($Bcb,$OwnerPointer)=@_;
1015
1016         return if !(my $BObject=BObject $Bcb);
1017         warn "CcSetBcbOwnerPointer() on map Bcb $Bcb" if $BObject->{"type"} ne "pin";
1018         return if !(my $CObject=CObject $BObject->{"SharedCacheMap"});
1019         warn "Double CcSetBcbOwnerPointer() on Bcb $Bcb" if defined $BObject->{"OwnerPointer"};
1020         my $val=$CObject->{"pin"}{$BObject->{"FileOffset"}};
1021         warn "CcSetBcbOwnerPointer() on unregistered pin Bcb $Bcb" if !$val || $val ne $Bcb;
1022         delete $CObject->{"pin"}{$BObject->{"FileOffset"}} if $val && $val eq $Bcb;
1023         $BObject->{"OwnerPointer"}=$OwnerPointer;
1024 }
1025
1026 sub IRP_MJ_CLOSE_leave()
1027 {
1028         my $FileObject=$Object->{"data"}[0]{"FileObject"};
1029 #       # IRP_MJ_CLOSE of FileObject w/o CcInitializeCacheMap()?
1030 #       return if !$FileObject{$FileObject};
1031         return if !(my $FObject=FObject $FileObject);
1032         if (eval(my $SectionObjectPointer=$FObject->{"SectionObjectPointer"})) {
1033                 return if !(my $SObject=SObject $SectionObjectPointer);
1034                 my $SharedCacheMap=$SObject->{"SharedCacheMap"};
1035                 if (eval $SharedCacheMap) {
1036                         return if !(my $CObject=CObject $SObject->{"SharedCacheMap"});
1037                         # SharedCacheMap may still exist for FCB although this FileObject gets destroyed now.
1038 #                       warn "SectionObjectPointer $SectionObjectPointer still exists during IRP_MJ_CLOSE"
1039 #                                                       ." while SharedCacheMap ".$CObject->{"SharedCacheMap"}." ref_count ".$CObject->{"ref_count"}
1040 #                                       if $SectionObjectPointer && $CObject->{"ref_count"};
1041                         }
1042                 }
1043         delete_FObject $FObject;
1044 }
1045
1046
1047 local $_;
1048 my $hex='0x[\dA-F]+';
1049 my %last_irp_mj;
1050 while (<>) {
1051         chomp;
1052         s/\r$//;
1053         # We may get some foreign garbage without '\n' before our debug data line:
1054         # Do not use '\bTraceFS' as there really can be precediny _any_ data.
1055         s#^.*?TraceFS[(]($hex/$hex)[)]: ## or do { print "$_\n" if $filter; next; };
1056         $ProcessThread=$1;
1057
1058         $Object=undef();
1059         if (/^enter: (\w+)/) {
1060                 $Object={};
1061                 $Object->{"by"}=$1;
1062                 $Object->{"line_enter"}=$.;
1063                 $Object->{"ProcessThread"}=$ProcessThread;
1064                 push @{$EnterLeave{$ProcessThread}},$Object;
1065                 }
1066         elsif (/^leave: (\w+)/) {
1067                 warn "Empty pop stack during 'leave' of $1" if !($Object=pop @{$EnterLeave{$ProcessThread}});
1068                 warn "Non-matching popped 'by' ".$Object->{"by"}." ne current 'leave' $1"
1069                                 if $Object->{"by"} ne $1;
1070                 $Object->{"line_leave"}=$.;
1071                 push @{$LastLeave{$ProcessThread}},$Object;
1072                 }
1073         elsif (my($FileObject,$FileName,$Flags,$SectionObjectPointer,$SharedCacheMap)=
1074                         /^FileObject=($hex): FileName=(?:NULL|'(.*)'),Flags=($hex),SectionObjectPointer=($hex),->SharedCacheMap=($hex)/) {
1075                 my $aref=$EnterLeave{$ProcessThread};
1076                 warn "Empty stack during 'data' line" if !($Object=${$aref}[$#$aref]);
1077                 my $data={
1078                                 "FileObject"=>$FileObject,
1079                                 "FileName"=>$FileName,
1080                                 "Flags"=>$Flags,
1081                                 "SectionObjectPointer"=>$SectionObjectPointer,
1082                                 "SharedCacheMap"=>$SharedCacheMap,
1083                                 "line"=>$.,
1084                                 };
1085                 push @{$Object->{"data"}},$data;
1086                 my $isinit={ map(($_=>1),qw(
1087                                 CcInitializeCacheMap
1088                                 CcUninitializeCacheMap
1089                                 IRP_MJ_CREATE
1090                                 )) }->{$Object->{"by"}};
1091                 check_data $data
1092                                 if 1==@{$Object->{"data"}} || !$isinit;
1093                 if ($isinit) {
1094                         # Prevent 'SharedCacheMap' 0->N change by CcInitializeCacheMap() called inside.
1095                         for my $ref (@$aref[0..$#$aref-1]) {
1096                                 $ref->{"data"}[0]->{"SharedCacheMap"}=$SharedCacheMap;
1097                                 }
1098                         }
1099                 if (2<=@{$Object->{"data"}}) {
1100                         my $data_prev=$Object->{"data"}[$#{$Object->{"data"}}-1];
1101                         for my $field (qw(FileObject FileName Flags),($isinit ? () : qw(SharedCacheMap))) {
1102                                 next if !defined(my $prev=$data_prev->{$field});
1103                                 next if !defined(my $now=$data->{$field});
1104                                 my $by=$Object->{"by"};
1105                                 if ($field eq "Flags") {
1106                                         next if $by eq "IRP_MJ_CREATE" && $field eq "Flags";
1107                                         my $FO_CLEANUP_COMPLETE=0x4000;
1108                                         $now=tohex(eval($now)&~$FO_CLEANUP_COMPLETE) if $by eq "IRP_MJ_CLEANUP";
1109                                         my $FO_FILE_FAST_IO_READ=0x80000;
1110                                         $prev=tohex(eval($prev)&~$FO_FILE_FAST_IO_READ) if $by eq "IRP_MJ_CLEANUP";
1111                                         $now=tohex(eval($now)&~$FO_FILE_FAST_IO_READ) if $by eq "IRP_MJ_READ" && !(eval($prev)&$FO_FILE_FAST_IO_READ);
1112                                         my $FO_FILE_MODIFIED=0x1000;
1113                                         $now=tohex(eval($now)&~$FO_FILE_MODIFIED) if $by eq "IRP_MJ_WRITE" && !(eval($prev)&$FO_FILE_MODIFIED);
1114                                         my $FO_FILE_SIZE_CHANGED=0x2000;
1115                                         $prev=tohex(eval($prev)&~$FO_FILE_MODIFIED)
1116                                                         if $by eq "IRP_MJ_SET_INFORMATION" && !(eval($now)&$FO_FILE_MODIFIED);
1117                                         $prev=tohex(eval($prev)&~$FO_FILE_SIZE_CHANGED)
1118                                                         if $by eq "IRP_MJ_SET_INFORMATION" && !(eval($now)&$FO_FILE_SIZE_CHANGED);
1119                                         }
1120                                 next if $by eq "IRP_MJ_CLOSE" && $field eq "FileName";
1121                                 $prev=~s#\\$## if $by eq "IRP_MJ_CREATE";
1122                                 $prev="\\" if $by eq "IRP_MJ_CREATE" && $prev eq "";
1123                                 $prev=~s#:.*## if $by eq "IRP_MJ_CREATE" && $prev ne $now;
1124                                 next if $field eq "SharedCacheMap" && !SharedCacheMap_valid $prev && !SharedCacheMap_valid $now;
1125                                 do { warn "Changed data field $field, prev=".$data_prev->{$field}.", now=".$data->{$field}." by $by";
1126 #                                               print STDERR Dumper $data_prev,$data;
1127                                                 } if $prev ne $now;
1128                                 }
1129                         }
1130                 next;
1131                 }
1132         elsif (my($op,$ByteOffset,$Length)=
1133                         /^(READ|WRITE): ByteOffset=($hex),Length=($hex)/) {
1134                 my $aref=$EnterLeave{$ProcessThread};
1135                 warn "Empty stack during 'data' line" if !($Object=${$aref}[$#$aref]);
1136                 $Object->{$op}={
1137                         "ByteOffset"=>$ByteOffset,
1138                         "Length"=>$Length,
1139                         };
1140                 next;
1141                 }
1142
1143         $LastLeave=${$LastLeave{$ProcessThread}}[$#{$LastLeave{$ProcessThread}}-1];
1144         $EnterLeave=$EnterLeave{$ProcessThread};
1145
1146         if (my($r)=
1147                         /^leave: IRP_MJ_\w+: r=($hex)/) {
1148                 # Failed requests should make no consequences.
1149                 next if eval($r);
1150                 }
1151
1152         if (my($FileObject,$AllocationSize,$FileSize,$ValidDataLength,$PinAccess)=
1153                         /^enter: CcInitializeCacheMap: FileObject=($hex),FileSizes,->AllocationSize=($hex),->FileSize=($hex),->ValidDataLength=($hex),PinAccess=([01]),/) {
1154                 CcInitializeCacheMap $FileObject,eval($AllocationSize),eval($FileSize),eval($ValidDataLength),eval($PinAccess);
1155                 next;
1156                 }
1157         if (/^leave: CcInitializeCacheMap\b/) {
1158                 CcInitializeCacheMap_leave;
1159                 next;
1160                 }
1161
1162         if (my($FileObject,$TruncateSize)=
1163                         /^enter: CcUninitializeCacheMap: FileObject=($hex),TruncateSize=($hex),/) {
1164                 CcUninitializeCacheMap $FileObject,eval($TruncateSize);
1165                 next;
1166                 }
1167         if (my($r)=
1168                         /^leave: CcUninitializeCacheMap: r=([01])/) {
1169                 CcUninitializeCacheMap_leave $r;
1170                 next;
1171                 }
1172
1173         if (my($FileObject,$AllocationSize,$FileSize,$ValidDataLength)=
1174                         /^enter: CcSetFileSizes: FileObject=($hex),FileSizes,->AllocationSize=($hex),->FileSize=($hex),->ValidDataLength=($hex)/) {
1175                 CcSetFileSizes $FileObject,eval($AllocationSize),eval($FileSize),eval($ValidDataLength);
1176                 next;
1177                 }
1178
1179         if (/^leave: IRP_MJ_CREATE\b/) {
1180                 IRP_MJ_CREATE_leave;
1181                 next;
1182                 }
1183
1184         if (/^leave: IRP_MJ_CLOSE\b/) {
1185                 IRP_MJ_CLOSE_leave;
1186                 next;
1187                 }
1188
1189         if (my($FileObject,$FileOffset,$Length)=
1190                         /^enter: CcMapData: FileObject=($hex),FileOffset=($hex),Length=($hex),Flags=0x1/) {
1191                 CcMapData $FileObject,eval($FileOffset),eval($Length);
1192                 next;
1193                 }
1194         if (my($Bcb,$Buffer)=
1195                         /^leave: CcMapData: r=1,Bcb=($hex),Buffer=($hex)/) {
1196                 CcMapData_leave $Bcb,$Buffer;
1197                 next;
1198                 }
1199
1200         if (my($FileObject,$FileOffset,$Length)=
1201                         /^enter: CcPinRead: FileObject=($hex),FileOffset=($hex),Length=($hex),Flags=0x1/) {
1202                 CcPinRead $FileObject,eval($FileOffset),eval($Length);
1203                 next;
1204                 }
1205         if (my($Bcb,$Buffer)=
1206                         /^leave: CcPinRead: r=1,Bcb=($hex),Buffer=($hex)/) {
1207                 CcPinRead_leave $Bcb,$Buffer;
1208                 next;
1209                 }
1210
1211         if (my($FileObject,$FileOffset,$Length)=
1212                         /^enter: CcPreparePinWrite: FileObject=($hex),FileOffset=($hex),Length=($hex),Zero=([01]),Flags=0x1/) {
1213                 CcPreparePinWrite $FileObject,eval($FileOffset),eval($Length);
1214                 next;
1215                 }
1216         if (my($Bcb,$Buffer)=
1217                         /^leave: CcPreparePinWrite: r=1,Bcb=($hex),Buffer=($hex)/) {
1218                 CcPreparePinWrite_leave $Bcb,$Buffer;
1219                 next;
1220                 }
1221
1222         if (my($FileObject,$FileOffset,$Length)=
1223                         /^enter: CcPinMappedData: FileObject=($hex),FileOffset=($hex),Length=($hex),Flags=0x1/) {
1224                 CcPinMappedData $FileObject,eval($FileOffset),eval($Length);
1225                 next;
1226                 }
1227         if (my($Bcb)=
1228                         /^leave: CcPinMappedData: r=1,Bcb=($hex)/) {
1229                 CcPinMappedData_leave $Bcb;
1230                 next;
1231                 }
1232
1233         if (my($BcbVoid,$Lsn)=
1234                         /^enter: CcSetDirtyPinnedData: BcbVoid=($hex),Lsn=($hex)/) {
1235                 CcSetDirtyPinnedData $BcbVoid,$Lsn;
1236                 next;
1237                 }
1238
1239         if (my($LogHandle,$Lsn)=
1240                         /^enter: FlushToLsnRoutine: LogHandle=($hex),Lsn=($hex)/) {
1241                 FlushToLsnRoutine $LogHandle,$Lsn;
1242                 next;
1243                 }
1244
1245         if (/^leave: IRP_MJ_READ\b/) {
1246                 IRP_MJ_READ_leave;
1247                 next;
1248                 }
1249
1250         if (my($SectionObjectPointer,$SharedCacheMap,$FileOffset,$Length,$UninitializeCacheMaps)=
1251                 /^enter: CcPurgeCacheSection: SectionObjectPointer=($hex),->SharedCacheMap=($hex),FileOffset=($hex),Length=($hex),UninitializeCacheMaps=([01])/) {
1252                 CcPurgeCacheSection $SectionObjectPointer,$SharedCacheMap,$FileOffset,$Length,$UninitializeCacheMaps;
1253                 next;
1254                 }
1255
1256         if (/^leave: IRP_MJ_WRITE\b/) {
1257                 IRP_MJ_WRITE_leave;
1258                 next;
1259                 }
1260
1261         if (my($SectionObjectPointer,$SharedCacheMap,$FileOffset,$Length)=
1262                         /^enter: CcFlushCache: SectionObjectPointer=($hex),->SharedCacheMap=($hex),FileOffset=($hex),Length=($hex)/) {
1263                 CcFlushCache $SectionObjectPointer,$SharedCacheMap,$FileOffset,$Length;
1264                 next;
1265                 }
1266
1267         if (my($Status,$Information)=
1268                         /^leave: CcFlushCache: IoStatus->Status=($hex),IoStatus->Information=($hex)/) {
1269                 CcFlushCache_leave $Status,$Information;
1270                 next;
1271                 }
1272
1273         if (my($r)=
1274                         /^leave: AcquireForLazyWrite: r=([01])/) {
1275                 AcquireForLazyWrite_leave $r;
1276                 }
1277
1278         if (/^leave: ReleaseFromLazyWrite\b/) {
1279                 ReleaseFromLazyWrite_leave;
1280                 }
1281
1282         if (my($FileObject,$LogHandle,$FlushToLsnRoutine)=
1283                         /^enter: CcSetLogHandleForFile: FileObject=($hex),LogHandle=($hex),FlushToLsnRoutine=($hex)/) {
1284                 CcSetLogHandleForFile $FileObject,$LogHandle,$FlushToLsnRoutine;
1285                 next;
1286                 }
1287
1288         if (my($FileObject,$FileOffset,$Length)=
1289                         /^enter: CcPrepareMdlWrite: FileObject=($hex),FileOffset=($hex),Length=($hex)/) {
1290                 CcPrepareMdlWrite $FileObject,$FileOffset,$Length;
1291                 next;
1292                 }
1293         if (my($MdlChain,$Status,$Information)=
1294                         /^leave: CcPrepareMdlWrite: MdlChain=($hex),IoStatus->Status=($hex),IoStatus->Information=($hex)/) {
1295                 CcPrepareMdlWrite_leave $MdlChain,$Status,$Information;
1296                 next;
1297                 }
1298
1299         if (my($FileObject,$FileOffset,$MdlChain)=
1300                         /^enter: CcMdlWriteComplete: FileObject=($hex),FileOffset=($hex),MdlChain=($hex)/) {
1301                 CcMdlWriteComplete $FileObject,$FileOffset,$MdlChain;
1302                 next;
1303                 }
1304
1305         if (my($FileObject,$MdlChain)=
1306                         /^enter: CcMdlWriteAbort: FileObject=($hex),MdlChain=($hex)/) {
1307                 CcMdlWriteAbort $FileObject,$MdlChain;
1308                 next;
1309                 }
1310
1311         if (my($Bcb)=
1312                         /^enter: CcRemapBcb: Bcb=($hex)/) {
1313                 CcRemapBcb $Bcb;
1314                 next;
1315                 }
1316         if (my($r)=
1317                         /^leave: CcRemapBcb: r=($hex)/) {
1318                 CcRemapBcb_leave $r;
1319                 next;
1320                 }
1321
1322         if (my($Bcb)=
1323                         /^enter: CcUnpinData: Bcb=($hex)/) {
1324                 CcUnpinData $Bcb;
1325                 next;
1326                 }
1327         if (my($Bcb)=
1328                         /^enter: CcUnpinDataForThread: Bcb=($hex)/) {
1329                 CcUnpinDataForThread $Bcb;
1330                 next;
1331                 }
1332
1333         if (my($Bcb,$OwnerPointer)=
1334                         /^enter: CcSetBcbOwnerPointer: Bcb=($hex),OwnerPointer=($hex)/) {
1335                 CcSetBcbOwnerPointer $Bcb,$OwnerPointer;
1336                 next;
1337                 }
1338
1339         print "$_\n" if $filter;
1340         }
1341 for my $FileObject (keys(%FileObject)) {
1342         warn "EXIT: still CcInitializeCacheMap FileObject $FileObject";
1343         next if !(my $FObject=FObject $FileObject);
1344         }