-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcluster.c
2090 lines (1754 loc) · 54.9 KB
/
cluster.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
/***************************************************************************
*
* Copyright (c) 1997-2022 Jeff V. Merkey
* 7260 SE Tenino St.
* Portland, Oregon 97206
* jeffmerkey@gmail.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the Lesser GNU Public License as published by the
* Free Software Foundation, version 2.1, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Original Authorship :
* source code written by Jeff V. Merkey
*
* Original Contributors :
* Jeff V. Merkey
*
*
*
****************************************************************************
*
*
* AUTHOR : Jeff V. Merkey (jeffmerkey@gmail.com)
* FILE : CLUSTER.C
* DESCRIP : Volume Cluster Routines
* DATE : December 1, 1998
*
*
***************************************************************************/
#include "globals.h"
ULONG TruncateClusterChain(VOLUME *volume, ULONG *Chain, ULONG Index,
ULONG PrevChain, ULONG size, ULONG SAFlag,
ULONG Attributes)
{
register ULONG cluster, fcluster, prev_cluster, cbytes, NewCluster;
register ULONG index, FileIndex, FileOffset, prev_index;
MIRROR_LRU *lru = 0;
ULONG retCode = 0;
register FAT_ENTRY *FAT;
FAT_ENTRY FAT_S;
register VOLUME_WORKSPACE *WorkSpace;
register ULONG SuballocSize;
if (!Chain)
return NwInvalidParameter;
if (!(*Chain))
{
NWFSPrint("nwfs: fat chain was NULL (truncate)\n");
return NwFileCorrupt;
}
if (*Chain == (ULONG) -1)
return 0;
index = 0;
prev_cluster = (ULONG) -1;
prev_index = (ULONG) -1;
cluster = *Chain;
if (PrevChain)
prev_cluster = PrevChain;
if (Index)
index = Index;
FileIndex = size / volume->ClusterSize;
FileOffset = size % volume->ClusterSize;
#if (VERBOSE)
NWFSPrint("nwfs_truncate: size-%d chain-%08X index-%X offset-%X\n",
(int) size,
(unsigned int)*Chain,
(unsigned int)FileIndex,
(unsigned int)FileOffset);
#endif
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
// check if suballocation is legal for this file.
if ((!SAFlag) || (Attributes & NO_SUBALLOC) || (Attributes & TRANSACTION))
return NwNotPermitted;
// if we are at our target index
if (index == FileIndex)
{
SuballocSize = GetSuballocSize(volume, cluster);
// this case assumes we will free the current suballoc
// fragment.
if (!FileOffset)
{
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, (ULONG) -1))
return NwVolumeCorrupt;
}
else
*Chain = (ULONG) -1;
FreeSuballocRecord(volume, cluster);
return 0;
}
if ((FileOffset + (SUBALLOC_BLOCK_SIZE - 1)) < SuballocSize)
{
// for this case, we are going to free the current suballoc
// element, and replace it with a suballoc element of a
// smaller size.
WorkSpace = AllocateWorkspace(volume);
if (WorkSpace)
{
NewCluster = AllocateSuballocRecord(volume, FileOffset,
&retCode);
if (NewCluster)
{
// here we read the previous data from the suballoc element
cbytes = ReadSuballocRecord(volume, 0, cluster,
&WorkSpace->Buffer[0],
FileOffset,
KERNEL_ADDRESS_SPACE,
&retCode);
if (cbytes != FileOffset)
{
FreeSuballocRecord(volume, NewCluster);
FreeWorkspace(volume, WorkSpace);
goto SkipFirstSuballocReplace;
}
// write the previous data to the new suballoc element
cbytes = WriteSuballocRecord(volume, 0, NewCluster,
&WorkSpace->Buffer[0],
FileOffset,
KERNEL_ADDRESS_SPACE,
&retCode);
if (cbytes != FileOffset)
{
FreeSuballocRecord(volume, NewCluster);
FreeWorkspace(volume, WorkSpace);
goto SkipFirstSuballocReplace;
}
FreeWorkspace(volume, WorkSpace);
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, NewCluster))
{
FreeSuballocRecord(volume, NewCluster);
return NwVolumeCorrupt;
}
}
else
*Chain = NewCluster;
FreeSuballocRecord(volume, cluster);
return 0;
}
FreeWorkspace(volume, WorkSpace);
}
}
}
SkipFirstSuballocReplace:;
return 0;
}
FAT = GetFatEntryAndLRU(volume, cluster, &lru, &FAT_S);
if (FAT)
{
prev_index = index;
index = FAT->FATIndex;
}
while (FAT && FAT->FATCluster)
{
// if we are past our target index in the fat chain, then
// truncate the remaining elements in the chain.
if (index > FileIndex)
{
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, (ULONG) -1))
return NwVolumeCorrupt;
}
else
*Chain = (ULONG) -1;
goto FreeChain;
}
// check if we have found the cluster that matches our target
// index.
if (index == FileIndex)
{
// if size is on a cluster boundry, then free the current
// cluster and all entries following it unless the file has
// holes.
if (!FileOffset)
{
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, (ULONG) -1))
return NwVolumeCorrupt;
}
else
*Chain = (ULONG) -1;
goto FreeChain;
}
// This case assumes that we have detected a file element
// that is the first element in the fat chain and that
// is not at the expected index of zero. This means that
// the first portion of the file is sparse and has a hole in
// it. It is invalid to suballocate the first non-zero
// indexed block within a fat chain because the index value
// cannot be stored by a suballocation element. As such,
// this is the one of two cases where we do not suballocate a
// partial cluster. Since the file is sparse, we are already
// getting significantly more efficient storage than the
// count of clusters reflected in the index count.
// If index is non-zero and we are at the head of the fat
// chain, then do not suballocate this cluster.
if (index && (prev_cluster == (ULONG) -1))
{
#if (VERBOSE)
NWFSPrint("detected sparse element %08X-[%08X] (head)\n",
(unsigned int) index,
(unsigned int) cluster);
#endif
goto DontSuballocate;
}
// if we are not the first cluster in the chain, but if we are
// the next cluster immediately following an allocation hole
// in the file, then do not suballocate this cluster. we
// keep track of the previous index value, and if the index
// numbers in the fat table are not sequential, then we know
// that this cluster immediately follows a hole in the
// file.
if ((prev_cluster != (ULONG) -1) && ((prev_index + 1) != index))
{
#if (VERBOSE)
NWFSPrint("detected sparse element %08X-[%08X] (chain)\n",
(unsigned int) index,
(unsigned int) cluster);
#endif
goto DontSuballocate;
}
// check if suballocation is legal for this file.
if ((!SAFlag) || (Attributes & NO_SUBALLOC) ||
(Attributes & TRANSACTION))
goto DontSuballocate;
// If this cluster has a sequential index count, and is not
// a sparse cluster at the chain head, and its current
// allocation size is not within volume->ClusterSize - 511,
// and the file is not flagged transactional, and suballocation
// has not been disabled at the file level, then convert
// this cluster into a suballocation element, copy the
// data from the partially filled cluster, and free
// everything in the chain, including this cluster.
if ((FileOffset + (SUBALLOC_BLOCK_SIZE - 1)) < volume->ClusterSize)
{
// for this case, we are going to free the current cluster
// and replace it with a suballoc element of a
// smaller size.
WorkSpace = AllocateWorkspace(volume);
if (WorkSpace)
{
NewCluster = AllocateSuballocRecord(volume, FileOffset, &retCode);
if (NewCluster)
{
// here we read the previous data from the cluster
cbytes = ReadClusterWithOffset(volume,
cluster,
0,
&WorkSpace->Buffer[0],
FileOffset,
KERNEL_ADDRESS_SPACE,
&retCode,
DATA_PRIORITY);
if (cbytes != FileOffset)
{
FreeSuballocRecord(volume, NewCluster);
FreeWorkspace(volume, WorkSpace);
goto DontSuballocate;
}
// write the previous data to the new suballoc element
cbytes = WriteSuballocRecord(volume,
0,
NewCluster,
&WorkSpace->Buffer[0],
FileOffset,
KERNEL_ADDRESS_SPACE,
&retCode);
if (cbytes != FileOffset)
{
FreeSuballocRecord(volume, NewCluster);
FreeWorkspace(volume, WorkSpace);
goto DontSuballocate;
}
FreeWorkspace(volume, WorkSpace);
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, NewCluster))
{
FreeSuballocRecord(volume, NewCluster);
return NwVolumeCorrupt;
}
}
else
*Chain = NewCluster;
goto FreeChain;
}
FreeWorkspace(volume, WorkSpace);
}
}
DontSuballocate:;
// set this cluster as end of file
if (SetClusterValue(volume, cluster, (ULONG) -1))
return NwVolumeCorrupt;
// bump to next cluster
cluster = FAT->FATCluster;
FAT = GetFatEntryAndLRU(volume, cluster, &lru, &FAT_S);
if (FAT)
{
prev_index = index;
index = FAT->FATIndex;
}
goto FreeChain;
}
// save previous cluster
prev_cluster = cluster;
// bump to next cluster
cluster = FAT->FATCluster;
// check if the next cluster is a suballoc element or EOF
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
// check if suballocation is legal for this file.
if ((!SAFlag) || (Attributes & NO_SUBALLOC) || (Attributes & TRANSACTION))
return NwNotPermitted;
// if we are at our target index
if ((index + 1) == FileIndex)
{
SuballocSize = GetSuballocSize(volume, cluster);
// this case assumes we will free the current suballoc
// fragment.
if (!FileOffset)
{
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, (ULONG) -1))
return NwVolumeCorrupt;
}
else
*Chain = (ULONG) -1;
FreeSuballocRecord(volume, cluster);
return 0;
}
if ((FileOffset + (SUBALLOC_BLOCK_SIZE - 1)) < SuballocSize)
{
// for this case, we are going to free the current suballoc
// element, and replace it with a suballoc element of a
// smaller size.
WorkSpace = AllocateWorkspace(volume);
if (WorkSpace)
{
NewCluster = AllocateSuballocRecord(volume, FileOffset,
&retCode);
if (NewCluster)
{
// here we read the previous data from the suballoc element
cbytes = ReadSuballocRecord(volume, 0, cluster,
&WorkSpace->Buffer[0],
FileOffset,
KERNEL_ADDRESS_SPACE,
&retCode);
if (cbytes != FileOffset)
{
FreeSuballocRecord(volume, NewCluster);
FreeWorkspace(volume, WorkSpace);
goto SkipSuballocReplace;
}
// write the previous data to the new suballoc element
cbytes = WriteSuballocRecord(volume, 0, NewCluster,
&WorkSpace->Buffer[0],
FileOffset,
KERNEL_ADDRESS_SPACE,
&retCode);
if (cbytes != FileOffset)
{
FreeSuballocRecord(volume, NewCluster);
FreeWorkspace(volume, WorkSpace);
goto SkipSuballocReplace;
}
FreeWorkspace(volume, WorkSpace);
// if there was a previous cluster, then set as EOF
if (prev_cluster != (ULONG) -1)
{
if (SetClusterValue(volume, prev_cluster, NewCluster))
{
FreeSuballocRecord(volume, NewCluster);
return NwVolumeCorrupt;
}
}
else
*Chain = NewCluster;
FreeSuballocRecord(volume, cluster);
return 0;
}
FreeWorkspace(volume, WorkSpace);
}
}
}
SkipSuballocReplace:;
return 0;
}
FAT = GetFatEntryAndLRU(volume, cluster, &lru, &FAT_S);
if (FAT)
{
prev_index = index;
index = FAT->FATIndex;
}
}
return 0;
FreeChain:;
while (FAT && FAT->FATCluster)
{
// save current cluster
fcluster = cluster;
// bump to next cluster
cluster = FAT->FATCluster;
// clear this cluster in the FAT table
retCode = NWUpdateFat(volume, FAT, lru, 0, 0, fcluster);
if (retCode)
{
NWFSPrint("*** error updating FAT [%X/%X] ***\n",
(unsigned int)lru->Cluster1,
(unsigned int)lru->Cluster2);
return NwVolumeCorrupt;
}
// set current cluster free in bit block list
SetFreeClusterValue(volume, fcluster, 0);
volume->VolumeFreeClusters++;
if (volume->VolumeAllocatedClusters)
volume->VolumeAllocatedClusters--;
// check if the next cluster is a suballoc element or EOF
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
if ((!SAFlag) || (Attributes & NO_SUBALLOC) || (Attributes & TRANSACTION))
return NwNotPermitted;
// set suballoc element free in list
FreeSuballocRecord(volume, cluster);
return 0;
}
FAT = GetFatEntryAndLRU(volume, cluster, &lru, &FAT_S);
if (FAT)
index = FAT->FATIndex;
}
return 0;
}
//
// Cluster Allocation bitmap routines
//
ULONG InitializeClusterFreeList(VOLUME *volume)
{
return (CreateBitBlockList(&volume->FreeBlockList,
volume->VolumeClusters,
BIT_BLOCK_SIZE,
"cluster free"));
}
ULONG ExtendClusterFreeList(VOLUME *volume, ULONG Amount)
{
#if (VERBOSE)
NWFSPrint("extend_cluster_free_list\n");
#endif
return (AdjustBitBlockList(&volume->FreeBlockList, Amount));
}
ULONG FreeClusterFreeList(VOLUME *volume)
{
#if (VERBOSE)
NWFSPrint("free_cluster_free_list\n");
#endif
return (FreeBitBlockList(&volume->FreeBlockList));
}
ULONG GetFreeClusterValue(VOLUME *volume, ULONG Cluster)
{
#if (VERBOSE)
NWFSPrint("get_free_cluster_value\n");
#endif
return (GetBitBlockValue(&volume->FreeBlockList, Cluster));
}
ULONG SetFreeClusterValue(VOLUME *volume, ULONG Cluster, ULONG flag)
{
register int i;
#if (VERBOSE)
NWFSPrint("set_free_cluster_value\n");
#endif
for (i=0; i < volume->MountedNumberOfSegments; i++)
{
if (Cluster < (volume->SegmentClusterStart[i] +
volume->SegmentClusterSize[i]))
{
if (Cluster >= volume->SegmentClusterStart[i])
{
if (Cluster < volume->LastAllocatedIndex[i])
volume->LastAllocatedIndex[i] = Cluster;
break;
}
}
}
return (SetBitBlockValue(&volume->FreeBlockList, Cluster, flag));
}
ULONG FindFreeCluster(VOLUME *volume, ULONG Cluster)
{
#if (VERBOSE)
NWFSPrint("find_free_cluster\n");
#endif
return (ScanAndSetBitBlockValueWithIndex(&volume->FreeBlockList, 0, Cluster,
1));
}
//
//
//
ULONG InitializeClusterAssignedList(VOLUME *volume)
{
return (CreateBitBlockList(&volume->AssignedBlockList,
volume->VolumeClusters,
BIT_BLOCK_SIZE,
"cluster assign"));
}
ULONG ExtendClusterAssignedList(VOLUME *volume, ULONG Amount)
{
return (AdjustBitBlockList(&volume->AssignedBlockList, Amount));
}
ULONG FreeClusterAssignedList(VOLUME *volume)
{
return (FreeBitBlockList(&volume->AssignedBlockList));
}
ULONG GetAssignedClusterValue(VOLUME *volume, ULONG Cluster)
{
return (GetBitBlockValue(&volume->AssignedBlockList, Cluster));
}
ULONG SetAssignedClusterValue(VOLUME *volume, ULONG Cluster, ULONG flag)
{
return (SetBitBlockValue(&volume->AssignedBlockList, Cluster, flag));
}
ULONG FindAssignedCluster(VOLUME *volume, ULONG Cluster)
{
return (ScanBitBlockValueWithIndex(&volume->AssignedBlockList, 0, Cluster,
1));
}
//
// This routine compares the free list built from the fat against
// a free list built from reported allocations in the directory,
// suballoc file chains, and DOS/MAC namespace data chains to
// determine if any orphaned or broken fat chains are wasting
// space on the device.
//
ULONG AdjustAllocatedClusters(VOLUME *volume)
{
register ULONG total = 0;
#if (!WINDOWS_NT_RO)
register ULONG i, val1, val2, ccode, retries;
#if (CACHE_FAT_TABLES)
extern ULONG FlushFAT(VOLUME *volume);
#endif
#endif
#if (MOUNT_VERBOSE)
NWFSPrint("*** Verifying Volume Allocation ***\n");
#endif
// if the count of empty subdirectories on this volume is greater
// than the current free directory blocks, then preallocate
// free space in the directory file until the count of empty
// directories is greater than or equal to the number of
// free directory blocks in the directory file. Netware treats
// this as a fatal error if any empty subdirectories exist for
// which there is no corresponding free directory block. We
// don't treat this as a fatal error, and will attempt to
// pre-allocate free records until a reasonable limit
// is reached. What is odd about this case is that Netware 4.2
// will actually break this rule, and create empty subdirectories
// without populating them properly, then vrepair the volume
// when it mounts.
#if (!WINDOWS_NT_RO)
#if (VERBOSE)
NWFSPrint("nwfs: free dir block count-%d free dir count-%d\n",
(int)volume->FreeDirectoryBlockCount,
(int)volume->FreeDirectoryCount);
#endif
if (volume->FreeDirectoryBlockCount < volume->FreeDirectoryCount)
{
retries = 0;
while (volume->FreeDirectoryBlockCount < volume->FreeDirectoryCount)
{
ccode = PreAllocateEmptyDirectorySpace(volume);
if (ccode)
break;
if (retries++ > 10) // if we get stuck in a loop here, then exit
{
NWFSPrint("nwfs: exceeded pre-allocated record limit (verify) [%u-%u]\n",
(unsigned int)volume->FreeDirectoryCount,
(unsigned int)volume->FreeDirectoryBlockCount);
break;
}
}
}
for (i=0; i < volume->MountedVolumeClusters; i++)
{
val1 = GetFreeClusterValue(volume, i);
val2 = GetAssignedClusterValue(volume, i);
if (val1 != val2)
{
// if the record is unallocated in the free list but we found
// an allocation for this cluster in either the directory,
// extdirectory, suballoc, or macintosh chain, then mark this
// cluster as free in the cluster free list.
if (!val1 && val2)
{
// this means the volume is corrupted.
NWFSPrint("nwfs: directory reports allocation not in fat table\n");
return 0;
}
// set this cluster as free and record that we have freed an
// unassigned cluster.
SetFreeClusterValue(volume, i, 0);
// clear the cluster fields in the fat table
FreeClusterNoFlush(volume, i);
// track total clusters freed
total++;
}
}
#if (CACHE_FAT_TABLES)
// flush out dirty fat buffers
FlushFAT(volume);
#endif
#endif
return total;
}
ULONG NWUpdateFat(VOLUME *volume, FAT_ENTRY *FAT, MIRROR_LRU *lru,
ULONG index, ULONG value, ULONG cluster)
{
register ULONG retCode;
if (!FAT || !lru)
return -1;
FAT->FATIndex = index;
FAT->FATCluster = value;
retCode = WriteFATEntry(volume, lru, FAT, cluster);
if (retCode)
NWFSPrint("WriteFATEntry cluster-%X error\n", (unsigned)cluster);
#if (CACHE_FAT_TABLES)
FlushFATBuffer(volume, lru, cluster);
#endif
return retCode;
}
ULONG NWUpdateFatNoFlush(VOLUME *volume, FAT_ENTRY *FAT, MIRROR_LRU *lru,
ULONG index, ULONG value, ULONG cluster)
{
register ULONG retCode;
if (!FAT || !lru)
return -1;
FAT->FATIndex = index;
FAT->FATCluster = value;
retCode = WriteFATEntry(volume, lru, FAT, cluster);
if (retCode)
NWFSPrint("WriteFATEntry cluster-%X error\n", (unsigned)cluster);
if (lru)
lru->BlockState = L_DIRTY;
return 0;
}
ULONG GetChainSize(VOLUME *volume, long ClusterChain)
{
register ULONG cluster;
ULONG total = 0;
register ULONG index = 0;
register FAT_ENTRY *FAT;
FAT_ENTRY FAT_S;
if ((!ClusterChain) || (ClusterChain == (ULONG) -1))
return total;
cluster = ClusterChain;
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return total;
if (cluster & 0x80000000)
total += GetSuballocSize(volume, cluster);
return total;
}
FAT = GetFatEntry(volume, cluster, &FAT_S);
if (FAT)
index = FAT->FATIndex;
while (FAT && FAT->FATCluster)
{
// If we detect a non-ascending index sequence, then we assume
// that the fat chain is corrupt.
if (total > ((index + 1) * volume->ClusterSize))
return total;
// set total to indexed offset in fat chain plus the current
// cluster allocation.
total = ((index + 1) * volume->ClusterSize);
// bump to next cluster
cluster = FAT->FATCluster;
// check if the next cluster is a suballoc element or EOF
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return total;
if (cluster & 0x80000000)
total += GetSuballocSize(volume, cluster);
return total;
}
FAT = GetFatEntry(volume, cluster, &FAT_S);
if (FAT)
index = FAT->FATIndex;
}
return total;
}
ULONG GetChainSizeValidate(VOLUME *volume,
long ClusterChain,
ULONG SAFlag,
ULONG *total)
{
register ULONG cluster, index = 0;
register FAT_ENTRY *FAT;
FAT_ENTRY FAT_S;
if (!total)
return NwInvalidParameter;
*total = 0;
if (!ClusterChain)
return NwChainBad;
if (ClusterChain == (ULONG) -1)
return 0;
cluster = ClusterChain;
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
if (cluster & 0x80000000)
{
if (!SAFlag)
return NwInvalidParameter;
(*total) += GetSuballocSize(volume, cluster);
}
return 0;
}
FAT = GetFatEntry(volume, cluster, &FAT_S);
if (FAT)
index = FAT->FATIndex;
if (!FAT->FATCluster)
return NwChainBad;
while (FAT && FAT->FATCluster)
{
// If we detect a non-ascending index sequence, then we assume
// that the fat chain is corrupt.
if ((*total) > ((index + 1) * volume->ClusterSize))
return NwChainBad;
// set total to indexed offset in fat chain plus the current
// cluster allocation.
(*total) = ((index + 1) * volume->ClusterSize);
// bump to next cluster
cluster = FAT->FATCluster;
// check if the next cluster is a suballoc element or EOF
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
if (cluster & 0x80000000)
{
if (!SAFlag)
return NwInvalidParameter;
(*total) += GetSuballocSize(volume, cluster);
}
return 0;
}
FAT = GetFatEntry(volume, cluster, &FAT_S);
if (FAT)
index = FAT->FATIndex;
if (!FAT->FATCluster)
return NwChainBad;
}
return 0;
}
// this function builds a detailed allocation map of all clusters
// and suballoc elements that exist within the specified fat
// chain.
ULONG BuildChainAssignment(VOLUME *volume, ULONG Chain, ULONG SAFlag)
{
register ULONG cluster, retCode;
register FAT_ENTRY *FAT;
FAT_ENTRY FAT_S;
if ((!Chain) || (Chain == (ULONG) -1))
return 0;
cluster = Chain;
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
if (cluster & 0x80000000)
{
if (!SAFlag)
return NwInvalidParameter;
retCode = SetSuballocListValue(volume, cluster, 1);
if (retCode)
NWFSPrint("nwfs: multiple files point to suballoc record\n");
return retCode;
}
return 0;
}
FAT = GetFatEntry(volume, cluster, &FAT_S);
while (FAT && FAT->FATCluster)
{
retCode = GetAssignedClusterValue(volume, cluster);
if (retCode)
{
NWFSPrint("nwfs: detected invalid assigned cluster-%X\n",
(unsigned int)cluster);
return -1;
}
retCode = SetAssignedClusterValue(volume, cluster, 1);
if (retCode)
{
NWFSPrint("nwfs: assigned bit block value not set cluster-%X\n",
(unsigned int)cluster);
}
// bump to next cluster
cluster = FAT->FATCluster;
// check if the next cluster is a suballoc element or EOF
if (cluster & 0x80000000)
{
if (cluster == (ULONG) -1)
return 0;
if (cluster & 0x80000000)
{
if (!SAFlag)
return NwInvalidParameter;
retCode = SetSuballocListValue(volume, cluster, 1);
if (retCode)
NWFSPrint("nwfs: multiple files point to suballoc record\n");
return retCode;
}
return 0;
}