-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaffs_guts.c
7582 lines (6009 loc) · 182 KB
/
yaffs_guts.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
/*
* YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
*
* Copyright (C) 2002-2007 Aleph One Ltd.
* for Toby Churchill Ltd and Brightstar Engineering
*
* Created by Charles Manning <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
const char *yaffs_guts_c_version =
"$Id: yaffs_guts.c,v 1.87 2009-07-29 04:30:24 charles Exp $";
#include "yportenv.h"
#include "yaffsinterface.h"
#include "yaffs_guts.h"
#include "yaffs_tagsvalidity.h"
#include "yaffs_getblockinfo.h"
#include "yaffs_tagscompat.h"
#ifndef CONFIG_YAFFS_USE_OWN_SORT
#include "yaffs_qsort.h"
#endif
#include "yaffs_nand.h"
#include "yaffs_checkptrw.h"
#include "yaffs_nand.h"
#include "yaffs_packedtags2.h"
#define YAFFS_PASSIVE_GC_CHUNKS 2
#include "yaffs_ecc.h"
/* Robustification (if it ever comes about...) */
static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
int erasedOk);
static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
const __u8 *data,
const yaffs_ExtendedTags *tags);
static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
const yaffs_ExtendedTags *tags);
/* Other local prototypes */
static void yaffs_UpdateParent(yaffs_Object *obj);
static int yaffs_UnlinkObject(yaffs_Object *obj);
static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
const __u8 *buffer,
yaffs_ExtendedTags *tags,
int useReserve);
static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
int chunkInNAND, int inScan);
static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
yaffs_ObjectType type);
static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
yaffs_Object *obj);
static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
int force, int isShrink, int shadows);
static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
static int yaffs_CheckStructures(void);
static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
int chunkOffset, int *limit);
static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
int chunkInNAND);
static int yaffs_UnlinkWorker(yaffs_Object *obj);
static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
int chunkInObject);
static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
yaffs_BlockInfo **blockUsedPtr);
static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
static void yaffs_VerifyDirectory(yaffs_Object *directory);
#ifdef YAFFS_PARANOID
static int yaffs_CheckFileSanity(yaffs_Object *in);
#else
#define yaffs_CheckFileSanity(in)
#endif
static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
yaffs_ExtendedTags *tags);
static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
unsigned pos);
static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
yaffs_FileStructure *fStruct,
__u32 chunkId);
/* Function to calculate chunk and offset */
static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
__u32 *offsetOut)
{
int chunk;
__u32 offset;
chunk = (__u32)(addr >> dev->chunkShift);
if (dev->chunkDiv == 1) {
/* easy power of 2 case */
offset = (__u32)(addr & dev->chunkMask);
} else {
/* Non power-of-2 case */
loff_t chunkBase;
chunk /= dev->chunkDiv;
chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
offset = (__u32)(addr - chunkBase);
}
*chunkOut = chunk;
*offsetOut = offset;
}
/* Function to return the number of shifts for a power of 2 greater than or
* equal to the given number
* Note we don't try to cater for all possible numbers and this does not have to
* be hellishly efficient.
*/
static __u32 ShiftsGE(__u32 x)
{
int extraBits;
int nShifts;
nShifts = extraBits = 0;
while (x > 1) {
if (x & 1)
extraBits++;
x >>= 1;
nShifts++;
}
if (extraBits)
nShifts++;
return nShifts;
}
/* Function to return the number of shifts to get a 1 in bit 0
*/
static __u32 Shifts(__u32 x)
{
int nShifts;
nShifts = 0;
if (!x)
return 0;
while (!(x&1)) {
x >>= 1;
nShifts++;
}
return nShifts;
}
/*
* Temporary buffer manipulations.
*/
static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
{
int i;
__u8 *buf = (__u8 *)1;
memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
dev->tempBuffer[i].line = 0; /* not in use */
dev->tempBuffer[i].buffer = buf =
YMALLOC_DMA(dev->totalBytesPerChunk);
}
return buf ? YAFFS_OK : YAFFS_FAIL;
}
__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
{
int i, j;
dev->tempInUse++;
if (dev->tempInUse > dev->maxTemp)
dev->maxTemp = dev->tempInUse;
for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
if (dev->tempBuffer[i].line == 0) {
dev->tempBuffer[i].line = lineNo;
if ((i + 1) > dev->maxTemp) {
dev->maxTemp = i + 1;
for (j = 0; j <= i; j++)
dev->tempBuffer[j].maxLine =
dev->tempBuffer[j].line;
}
return dev->tempBuffer[i].buffer;
}
}
T(YAFFS_TRACE_BUFFERS,
(TSTR("Out of temp buffers at line %d, other held by lines:"),
lineNo));
for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
/*
* If we got here then we have to allocate an unmanaged one
* This is not good.
*/
dev->unmanagedTempAllocations++;
return YMALLOC(dev->nDataBytesPerChunk);
}
void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
int lineNo)
{
int i;
dev->tempInUse--;
for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
if (dev->tempBuffer[i].buffer == buffer) {
dev->tempBuffer[i].line = 0;
return;
}
}
if (buffer) {
/* assume it is an unmanaged one. */
T(YAFFS_TRACE_BUFFERS,
(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
lineNo));
YFREE(buffer);
dev->unmanagedTempDeallocations++;
}
}
/*
* Determine if we have a managed buffer.
*/
int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
{
int i;
for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
if (dev->tempBuffer[i].buffer == buffer)
return 1;
}
for (i = 0; i < dev->nShortOpCaches; i++) {
if (dev->srCache[i].data == buffer)
return 1;
}
if (buffer == dev->checkpointBuffer)
return 1;
T(YAFFS_TRACE_ALWAYS,
(TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
return 0;
}
/*
* Chunk bitmap manipulations
*/
static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
{
if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
T(YAFFS_TRACE_ERROR,
(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
blk));
YBUG();
}
return dev->chunkBits +
(dev->chunkBitmapStride * (blk - dev->internalStartBlock));
}
static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
{
if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
chunk < 0 || chunk >= dev->nChunksPerBlock) {
T(YAFFS_TRACE_ERROR,
(TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
blk, chunk));
YBUG();
}
}
static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
{
__u8 *blkBits = yaffs_BlockBits(dev, blk);
memset(blkBits, 0, dev->chunkBitmapStride);
}
static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
{
__u8 *blkBits = yaffs_BlockBits(dev, blk);
yaffs_VerifyChunkBitId(dev, blk, chunk);
blkBits[chunk / 8] &= ~(1 << (chunk & 7));
}
static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
{
__u8 *blkBits = yaffs_BlockBits(dev, blk);
yaffs_VerifyChunkBitId(dev, blk, chunk);
blkBits[chunk / 8] |= (1 << (chunk & 7));
}
static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
{
__u8 *blkBits = yaffs_BlockBits(dev, blk);
yaffs_VerifyChunkBitId(dev, blk, chunk);
return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
}
static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
{
__u8 *blkBits = yaffs_BlockBits(dev, blk);
int i;
for (i = 0; i < dev->chunkBitmapStride; i++) {
if (*blkBits)
return 1;
blkBits++;
}
return 0;
}
static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
{
__u8 *blkBits = yaffs_BlockBits(dev, blk);
int i;
int n = 0;
for (i = 0; i < dev->chunkBitmapStride; i++) {
__u8 x = *blkBits;
while (x) {
if (x & 1)
n++;
x >>= 1;
}
blkBits++;
}
return n;
}
/*
* Verification code
*/
static int yaffs_SkipVerification(yaffs_Device *dev)
{
return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
}
static int yaffs_SkipFullVerification(yaffs_Device *dev)
{
return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
}
static int yaffs_SkipNANDVerification(yaffs_Device *dev)
{
return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
}
static const char *blockStateName[] = {
"Unknown",
"Needs scanning",
"Scanning",
"Empty",
"Allocating",
"Full",
"Dirty",
"Checkpoint",
"Collecting",
"Dead"
};
static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
{
int actuallyUsed;
int inUse;
if (yaffs_SkipVerification(dev))
return;
/* Report illegal runtime states */
if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
switch (bi->blockState) {
case YAFFS_BLOCK_STATE_UNKNOWN:
case YAFFS_BLOCK_STATE_SCANNING:
case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
n, blockStateName[bi->blockState]));
}
/* Check pages in use and soft deletions are legal */
actuallyUsed = bi->pagesInUse - bi->softDeletions;
if (bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
n, bi->pagesInUse, bi->softDeletions));
/* Check chunk bitmap legal */
inUse = yaffs_CountChunkBits(dev, n);
if (inUse != bi->pagesInUse)
T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
n, bi->pagesInUse, inUse));
/* Check that the sequence number is valid.
* Ten million is legal, but is very unlikely
*/
if (dev->isYaffs2 &&
(bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
(bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
n, bi->sequenceNumber));
}
static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
int n)
{
yaffs_VerifyBlock(dev, bi, n);
/* After collection the block should be in the erased state */
/* This will need to change if we do partial gc */
if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
n, bi->blockState));
}
}
static void yaffs_VerifyBlocks(yaffs_Device *dev)
{
int i;
int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
int nIllegalBlockStates = 0;
if (yaffs_SkipVerification(dev))
return;
memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
yaffs_VerifyBlock(dev, bi, i);
if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
nBlocksPerState[bi->blockState]++;
else
nIllegalBlockStates++;
}
T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
T(YAFFS_TRACE_VERIFY,
(TSTR("%s %d blocks"TENDSTR),
blockStateName[i], nBlocksPerState[i]));
if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
T(YAFFS_TRACE_VERIFY,
(TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
T(YAFFS_TRACE_VERIFY,
(TSTR("Erased block count wrong dev %d count %d"TENDSTR),
dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
T(YAFFS_TRACE_VERIFY,
(TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
}
/*
* Verify the object header. oh must be valid, but obj and tags may be NULL in which
* case those tests will not be performed.
*/
static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
{
if (obj && yaffs_SkipVerification(obj->myDev))
return;
if (!(tags && obj && oh)) {
T(YAFFS_TRACE_VERIFY,
(TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
(__u32)tags, (__u32)obj, (__u32)oh));
return;
}
if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
oh->type > YAFFS_OBJECT_TYPE_MAX)
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
tags->objectId, oh->type));
if (tags->objectId != obj->objectId)
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d header mismatch objectId %d"TENDSTR),
tags->objectId, obj->objectId));
/*
* Check that the object's parent ids match if parentCheck requested.
*
* Tests do not apply to the root object.
*/
if (parentCheck && tags->objectId > 1 && !obj->parent)
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
tags->objectId, oh->parentObjectId));
if (parentCheck && obj->parent &&
oh->parentObjectId != obj->parent->objectId &&
(oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
obj->parent->objectId != YAFFS_OBJECTID_DELETED))
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
tags->objectId, oh->parentObjectId, obj->parent->objectId));
if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d header name is NULL"TENDSTR),
obj->objectId));
if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d header name is 0xFF"TENDSTR),
obj->objectId));
}
static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
__u32 level, int chunkOffset)
{
int i;
yaffs_Device *dev = obj->myDev;
int ok = 1;
if (tn) {
if (level > 0) {
for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
if (tn->internal[i]) {
ok = yaffs_VerifyTnodeWorker(obj,
tn->internal[i],
level - 1,
(chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
}
}
} else if (level == 0) {
yaffs_ExtendedTags tags;
__u32 objectId = obj->objectId;
chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
__u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
if (theChunk > 0) {
/* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
objectId, chunkOffset, theChunk,
tags.objectId, tags.chunkId));
}
}
chunkOffset++;
}
}
}
return ok;
}
static void yaffs_VerifyFile(yaffs_Object *obj)
{
int requiredTallness;
int actualTallness;
__u32 lastChunk;
__u32 x;
__u32 i;
yaffs_Device *dev;
yaffs_ExtendedTags tags;
yaffs_Tnode *tn;
__u32 objectId;
if (!obj)
return;
if (yaffs_SkipVerification(obj->myDev))
return;
dev = obj->myDev;
objectId = obj->objectId;
/* Check file size is consistent with tnode depth */
lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
requiredTallness = 0;
while (x > 0) {
x >>= YAFFS_TNODES_INTERNAL_BITS;
requiredTallness++;
}
actualTallness = obj->variant.fileVariant.topLevel;
if (requiredTallness > actualTallness)
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
obj->objectId, actualTallness, requiredTallness));
/* Check that the chunks in the tnode tree are all correct.
* We do this by scanning through the tnode tree and
* checking the tags for every chunk match.
*/
if (yaffs_SkipNANDVerification(dev))
return;
for (i = 1; i <= lastChunk; i++) {
tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
if (tn) {
__u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
if (theChunk > 0) {
/* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
if (tags.objectId != objectId || tags.chunkId != i) {
T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
objectId, i, theChunk,
tags.objectId, tags.chunkId));
}
}
}
}
}
static void yaffs_VerifyHardLink(yaffs_Object *obj)
{
if (obj && yaffs_SkipVerification(obj->myDev))
return;
/* Verify sane equivalent object */
}
static void yaffs_VerifySymlink(yaffs_Object *obj)
{
if (obj && yaffs_SkipVerification(obj->myDev))
return;
/* Verify symlink string */
}
static void yaffs_VerifySpecial(yaffs_Object *obj)
{
if (obj && yaffs_SkipVerification(obj->myDev))
return;
}
static void yaffs_VerifyObject(yaffs_Object *obj)
{
yaffs_Device *dev;
__u32 chunkMin;
__u32 chunkMax;
__u32 chunkIdOk;
__u32 chunkInRange;
__u32 chunkShouldNotBeDeleted;
__u32 chunkValid;
if (!obj)
return;
if (obj->beingCreated)
return;
dev = obj->myDev;
if (yaffs_SkipVerification(dev))
return;
/* Check sane object header chunk */
chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
chunkIdOk = chunkInRange || (obj->hdrChunk == 0);
chunkValid = chunkInRange &&
yaffs_CheckChunkBit(dev,
obj->hdrChunk / dev->nChunksPerBlock,
obj->hdrChunk % dev->nChunksPerBlock);
chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
if (!obj->fake &&
(!chunkIdOk || chunkShouldNotBeDeleted)) {
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
obj->objectId, obj->hdrChunk,
chunkIdOk ? "" : ",out of range",
chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
}
if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
yaffs_ExtendedTags tags;
yaffs_ObjectHeader *oh;
__u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
oh = (yaffs_ObjectHeader *)buffer;
yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
&tags);
yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
}
/* Verify it has a parent */
if (obj && !obj->fake &&
(!obj->parent || obj->parent->myDev != dev)) {
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
obj->objectId, obj->parent));
}
/* Verify parent is a directory */
if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
obj->objectId, obj->parent->variantType));
}
switch (obj->variantType) {
case YAFFS_OBJECT_TYPE_FILE:
yaffs_VerifyFile(obj);
break;
case YAFFS_OBJECT_TYPE_SYMLINK:
yaffs_VerifySymlink(obj);
break;
case YAFFS_OBJECT_TYPE_DIRECTORY:
yaffs_VerifyDirectory(obj);
break;
case YAFFS_OBJECT_TYPE_HARDLINK:
yaffs_VerifyHardLink(obj);
break;
case YAFFS_OBJECT_TYPE_SPECIAL:
yaffs_VerifySpecial(obj);
break;
case YAFFS_OBJECT_TYPE_UNKNOWN:
default:
T(YAFFS_TRACE_VERIFY,
(TSTR("Obj %d has illegaltype %d"TENDSTR),
obj->objectId, obj->variantType));
break;
}
}
static void yaffs_VerifyObjects(yaffs_Device *dev)
{
yaffs_Object *obj;
int i;
struct ylist_head *lh;
if (yaffs_SkipVerification(dev))
return;
/* Iterate through the objects in each hash entry */
for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
ylist_for_each(lh, &dev->objectBucket[i].list) {
if (lh) {
obj = ylist_entry(lh, yaffs_Object, hashLink);
yaffs_VerifyObject(obj);
}
}
}
}
/*
* Simple hash function. Needs to have a reasonable spread
*/
static Y_INLINE int yaffs_HashFunction(int n)
{
n = abs(n);
return n % YAFFS_NOBJECT_BUCKETS;
}
/*
* Access functions to useful fake objects.
* Note that root might have a presence in NAND if permissions are set.
*/
yaffs_Object *yaffs_Root(yaffs_Device *dev)
{
return dev->rootDir;
}
yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
{
return dev->lostNFoundDir;
}
/*
* Erased NAND checking functions
*/
int yaffs_CheckFF(__u8 *buffer, int nBytes)
{
/* Horrible, slow implementation */
while (nBytes--) {
if (*buffer != 0xFF)
return 0;
buffer++;
}
return 1;
}
static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
int chunkInNAND)
{
int retval = YAFFS_OK;
__u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
yaffs_ExtendedTags tags;
int result;
result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
retval = YAFFS_FAIL;
if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
T(YAFFS_TRACE_NANDACCESS,
(TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
retval = YAFFS_FAIL;
}
yaffs_ReleaseTempBuffer(dev, data, __LINE__);
return retval;
}
static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
const __u8 *data,
yaffs_ExtendedTags *tags,
int useReserve)
{
int attempts = 0;
int writeOk = 0;
int chunk;
yaffs_InvalidateCheckpoint(dev);
do {
yaffs_BlockInfo *bi = 0;
int erasedOk = 0;
chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
if (chunk < 0) {
/* no space */
break;
}
/* First check this chunk is erased, if it needs
* checking. The checking policy (unless forced
* always on) is as follows:
*
* Check the first page we try to write in a block.
* If the check passes then we don't need to check any
* more. If the check fails, we check again...
* If the block has been erased, we don't need to check.
*
* However, if the block has been prioritised for gc,
* then we think there might be something odd about
* this block and stop using it.
*
* Rationale: We should only ever see chunks that have
* not been erased if there was a partially written
* chunk due to power loss. This checking policy should
* catch that case with very few checks and thus save a
* lot of checks that are most likely not needed.
*/
if (bi->gcPrioritise) {
yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
/* try another chunk */
continue;
}
/* let's give it a try */
attempts++;
#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
bi->skipErasedCheck = 0;
#endif
if (!bi->skipErasedCheck) {
erasedOk = yaffs_CheckChunkErased(dev, chunk);
if (erasedOk != YAFFS_OK) {
T(YAFFS_TRACE_ERROR,
(TSTR("**>> yaffs chunk %d was not erased"
TENDSTR), chunk));
/* try another chunk */
continue;
}
bi->skipErasedCheck = 1;
}
writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
data, tags);
if (writeOk != YAFFS_OK) {
yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
/* try another chunk */
continue;
}
/* Copy the data into the robustification buffer */
yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
} while (writeOk != YAFFS_OK &&
(yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));