This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
yaffs_yaffs2.c
1540 lines (1211 loc) · 38.5 KB
/
yaffs_yaffs2.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-2010 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.
*/
#include "yaffs_guts.h"
#include "yaffs_trace.h"
#include "yaffs_yaffs2.h"
#include "yaffs_checkptrw.h"
#include "yaffs_bitmap.h"
#include "yaffs_qsort.h"
#include "yaffs_nand.h"
#include "yaffs_getblockinfo.h"
#include "yaffs_verify.h"
/*
* Checkpoints are really no benefit on very small partitions.
*
* To save space on small partitions don't bother with checkpoints unless
* the partition is at least this big.
*/
#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
#define YAFFS_SMALL_HOLE_THRESHOLD 4
/*
* Oldest Dirty Sequence Number handling.
*/
/* yaffs2_CalcOldestDirtySequence()
* yaffs2_FindOldestDirtySequence()
* Calculate the oldest dirty sequence number if we don't know it.
*/
void yaffs2_CalcOldestDirtySequence(yaffs_Device *dev)
{
int i;
unsigned seq;
unsigned blockNo = 0;
yaffs_BlockInfo *b;
if(!dev->param.isYaffs2)
return;
/* Find the oldest dirty sequence number. */
seq = dev->sequenceNumber + 1;
b = dev->blockInfo;
for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
(b->pagesInUse - b->softDeletions) < dev->param.nChunksPerBlock &&
b->sequenceNumber < seq) {
seq = b->sequenceNumber;
blockNo = i;
}
b++;
}
if(blockNo){
dev->oldestDirtySequence = seq;
dev->oldestDirtyBlock = blockNo;
}
}
void yaffs2_FindOldestDirtySequence(yaffs_Device *dev)
{
if(!dev->param.isYaffs2)
return;
if(!dev->oldestDirtySequence)
yaffs2_CalcOldestDirtySequence(dev);
}
/*
* yaffs_ClearOldestDirtySequence()
* Called when a block is erased or marked bad. (ie. when its sequenceNumber
* becomes invalid). If the value matches the oldest then we clear
* dev->oldestDirtySequence to force its recomputation.
*/
void yaffs2_ClearOldestDirtySequence(yaffs_Device *dev, yaffs_BlockInfo *bi)
{
if(!dev->param.isYaffs2)
return;
if(!bi || bi->sequenceNumber == dev->oldestDirtySequence){
dev->oldestDirtySequence = 0;
dev->oldestDirtyBlock = 0;
}
}
/*
* yaffs2_UpdateOldestDirtySequence()
* Update the oldest dirty sequence number whenever we dirty a block.
* Only do this if the oldestDirtySequence is actually being tracked.
*/
void yaffs2_UpdateOldestDirtySequence(yaffs_Device *dev, unsigned blockNo, yaffs_BlockInfo *bi)
{
if(!dev->param.isYaffs2)
return;
if(dev->oldestDirtySequence){
if(dev->oldestDirtySequence > bi->sequenceNumber){
dev->oldestDirtySequence = bi->sequenceNumber;
dev->oldestDirtyBlock = blockNo;
}
}
}
int yaffs2_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
yaffs_BlockInfo *bi)
{
if (!dev->param.isYaffs2)
return 1; /* disqualification only applies to yaffs2. */
if (!bi->hasShrinkHeader)
return 1; /* can gc */
yaffs2_FindOldestDirtySequence(dev);
/* Can't do gc of this block if there are any blocks older than this one that have
* discarded pages.
*/
return (bi->sequenceNumber <= dev->oldestDirtySequence);
}
/*
* yaffs2_FindRefreshBlock()
* periodically finds the oldest full block by sequence number for refreshing.
* Only for yaffs2.
*/
__u32 yaffs2_FindRefreshBlock(yaffs_Device *dev)
{
__u32 b ;
__u32 oldest = 0;
__u32 oldestSequence = 0;
yaffs_BlockInfo *bi;
if(!dev->param.isYaffs2)
return oldest;
/*
* If refresh period < 10 then refreshing is disabled.
*/
if(dev->param.refreshPeriod < 10)
return oldest;
/*
* Fix broken values.
*/
if(dev->refreshSkip > dev->param.refreshPeriod)
dev->refreshSkip = dev->param.refreshPeriod;
if(dev->refreshSkip > 0)
return oldest;
/*
* Refresh skip is now zero.
* We'll do a refresh this time around....
* Update the refresh skip and find the oldest block.
*/
dev->refreshSkip = dev->param.refreshPeriod;
dev->refreshCount++;
bi = dev->blockInfo;
for (b = dev->internalStartBlock; b <=dev->internalEndBlock; b++){
if (bi->blockState == YAFFS_BLOCK_STATE_FULL){
if(oldest < 1 ||
bi->sequenceNumber < oldestSequence){
oldest = b;
oldestSequence = bi->sequenceNumber;
}
}
bi++;
}
if (oldest > 0) {
T(YAFFS_TRACE_GC,
(TSTR("GC refresh count %d selected block %d with sequenceNumber %d" TENDSTR),
dev->refreshCount, oldest, oldestSequence));
}
return oldest;
}
int yaffs2_CheckpointRequired(yaffs_Device *dev)
{
int nblocks;
if(!dev->param.isYaffs2)
return 0;
nblocks = dev->internalEndBlock - dev->internalStartBlock + 1 ;
return !dev->param.skipCheckpointWrite &&
!dev->readOnly &&
(nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
}
int yaffs2_CalcCheckpointBlocksRequired(yaffs_Device *dev)
{
int retval;
if(!dev->param.isYaffs2)
return 0;
if (!dev->nCheckpointBlocksRequired &&
yaffs2_CheckpointRequired(dev)){
/* Not a valid value so recalculate */
int nBytes = 0;
int nBlocks;
int devBlocks = (dev->param.endBlock - dev->param.startBlock + 1);
nBytes += sizeof(yaffs_CheckpointValidity);
nBytes += sizeof(yaffs_CheckpointDevice);
nBytes += devBlocks * sizeof(yaffs_BlockInfo);
nBytes += devBlocks * dev->chunkBitmapStride;
nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjects);
nBytes += (dev->tnodeSize + sizeof(__u32)) * (dev->nTnodes);
nBytes += sizeof(yaffs_CheckpointValidity);
nBytes += sizeof(__u32); /* checksum*/
/* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->param.nChunksPerBlock)) + 3;
dev->nCheckpointBlocksRequired = nBlocks;
}
retval = dev->nCheckpointBlocksRequired - dev->blocksInCheckpoint;
if(retval < 0)
retval = 0;
return retval;
}
/*--------------------- Checkpointing --------------------*/
static int yaffs2_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
{
yaffs_CheckpointValidity cp;
memset(&cp, 0, sizeof(cp));
cp.structType = sizeof(cp);
cp.magic = YAFFS_MAGIC;
cp.version = YAFFS_CHECKPOINT_VERSION;
cp.head = (head) ? 1 : 0;
return (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
1 : 0;
}
static int yaffs2_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
{
yaffs_CheckpointValidity cp;
int ok;
ok = (yaffs2_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
if (ok)
ok = (cp.structType == sizeof(cp)) &&
(cp.magic == YAFFS_MAGIC) &&
(cp.version == YAFFS_CHECKPOINT_VERSION) &&
(cp.head == ((head) ? 1 : 0));
return ok ? 1 : 0;
}
static void yaffs2_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
yaffs_Device *dev)
{
cp->nErasedBlocks = dev->nErasedBlocks;
cp->allocationBlock = dev->allocationBlock;
cp->allocationPage = dev->allocationPage;
cp->nFreeChunks = dev->nFreeChunks;
cp->nDeletedFiles = dev->nDeletedFiles;
cp->nUnlinkedFiles = dev->nUnlinkedFiles;
cp->nBackgroundDeletions = dev->nBackgroundDeletions;
cp->sequenceNumber = dev->sequenceNumber;
}
static void yaffs2_CheckpointDeviceToDevice(yaffs_Device *dev,
yaffs_CheckpointDevice *cp)
{
dev->nErasedBlocks = cp->nErasedBlocks;
dev->allocationBlock = cp->allocationBlock;
dev->allocationPage = cp->allocationPage;
dev->nFreeChunks = cp->nFreeChunks;
dev->nDeletedFiles = cp->nDeletedFiles;
dev->nUnlinkedFiles = cp->nUnlinkedFiles;
dev->nBackgroundDeletions = cp->nBackgroundDeletions;
dev->sequenceNumber = cp->sequenceNumber;
}
static int yaffs2_WriteCheckpointDevice(yaffs_Device *dev)
{
yaffs_CheckpointDevice cp;
__u32 nBytes;
__u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
int ok;
/* Write device runtime values*/
yaffs2_DeviceToCheckpointDevice(&cp, dev);
cp.structType = sizeof(cp);
ok = (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
/* Write block info */
if (ok) {
nBytes = nBlocks * sizeof(yaffs_BlockInfo);
ok = (yaffs2_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
}
/* Write chunk bits */
if (ok) {
nBytes = nBlocks * dev->chunkBitmapStride;
ok = (yaffs2_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
}
return ok ? 1 : 0;
}
static int yaffs2_ReadCheckpointDevice(yaffs_Device *dev)
{
yaffs_CheckpointDevice cp;
__u32 nBytes;
__u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
int ok;
ok = (yaffs2_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
if (!ok)
return 0;
if (cp.structType != sizeof(cp))
return 0;
yaffs2_CheckpointDeviceToDevice(dev, &cp);
nBytes = nBlocks * sizeof(yaffs_BlockInfo);
ok = (yaffs2_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
if (!ok)
return 0;
nBytes = nBlocks * dev->chunkBitmapStride;
ok = (yaffs2_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
return ok ? 1 : 0;
}
static void yaffs2_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
yaffs_Object *obj)
{
cp->objectId = obj->objectId;
cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
cp->hdrChunk = obj->hdrChunk;
cp->variantType = obj->variantType;
cp->deleted = obj->deleted;
cp->softDeleted = obj->softDeleted;
cp->unlinked = obj->unlinked;
cp->fake = obj->fake;
cp->renameAllowed = obj->renameAllowed;
cp->unlinkAllowed = obj->unlinkAllowed;
cp->serial = obj->serial;
cp->nDataChunks = obj->nDataChunks;
if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
}
static int yaffs2_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
{
yaffs_Object *parent;
if (obj->variantType != cp->variantType) {
T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
TCONT("chunk %d does not match existing object type %d")
TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
obj->variantType));
return 0;
}
obj->objectId = cp->objectId;
if (cp->parentId)
parent = yaffs_FindOrCreateObjectByNumber(
obj->myDev,
cp->parentId,
YAFFS_OBJECT_TYPE_DIRECTORY);
else
parent = NULL;
if (parent) {
if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
TCONT(" chunk %d Parent type, %d, not directory")
TENDSTR),
cp->objectId, cp->parentId, cp->variantType,
cp->hdrChunk, parent->variantType));
return 0;
}
yaffs_AddObjectToDirectory(parent, obj);
}
obj->hdrChunk = cp->hdrChunk;
obj->variantType = cp->variantType;
obj->deleted = cp->deleted;
obj->softDeleted = cp->softDeleted;
obj->unlinked = cp->unlinked;
obj->fake = cp->fake;
obj->renameAllowed = cp->renameAllowed;
obj->unlinkAllowed = cp->unlinkAllowed;
obj->serial = cp->serial;
obj->nDataChunks = cp->nDataChunks;
if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
if (obj->hdrChunk > 0)
obj->lazyLoaded = 1;
return 1;
}
static int yaffs2_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
__u32 level, int chunkOffset)
{
int i;
yaffs_Device *dev = in->myDev;
int ok = 1;
if (tn) {
if (level > 0) {
for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
if (tn->internal[i]) {
ok = yaffs2_CheckpointTnodeWorker(in,
tn->internal[i],
level - 1,
(chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
}
}
} else if (level == 0) {
__u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
ok = (yaffs2_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
if (ok)
ok = (yaffs2_CheckpointWrite(dev, tn, dev->tnodeSize) == dev->tnodeSize);
}
}
return ok;
}
static int yaffs2_WriteCheckpointTnodes(yaffs_Object *obj)
{
__u32 endMarker = ~0;
int ok = 1;
if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
ok = yaffs2_CheckpointTnodeWorker(obj,
obj->variant.fileVariant.top,
obj->variant.fileVariant.topLevel,
0);
if (ok)
ok = (yaffs2_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
sizeof(endMarker));
}
return ok ? 1 : 0;
}
static int yaffs2_ReadCheckpointTnodes(yaffs_Object *obj)
{
__u32 baseChunk;
int ok = 1;
yaffs_Device *dev = obj->myDev;
yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
yaffs_Tnode *tn;
int nread = 0;
ok = (yaffs2_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
while (ok && (~baseChunk)) {
nread++;
/* Read level 0 tnode */
tn = yaffs_GetTnode(dev);
if (tn){
ok = (yaffs2_CheckpointRead(dev, tn, dev->tnodeSize) == dev->tnodeSize);
} else
ok = 0;
if (tn && ok)
ok = yaffs_AddOrFindLevel0Tnode(dev,
fileStructPtr,
baseChunk,
tn) ? 1 : 0;
if (ok)
ok = (yaffs2_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
}
T(YAFFS_TRACE_CHECKPOINT, (
TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
nread, baseChunk, ok));
return ok ? 1 : 0;
}
static int yaffs2_WriteCheckpointObjects(yaffs_Device *dev)
{
yaffs_Object *obj;
yaffs_CheckpointObject cp;
int i;
int ok = 1;
struct ylist_head *lh;
/* Iterate through the objects in each hash entry,
* dumping them to the checkpointing stream.
*/
for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
ylist_for_each(lh, &dev->objectBucket[i].list) {
if (lh) {
obj = ylist_entry(lh, yaffs_Object, hashLink);
if (!obj->deferedFree) {
yaffs2_ObjectToCheckpointObject(&cp, obj);
cp.structType = sizeof(cp);
T(YAFFS_TRACE_CHECKPOINT, (
TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" TENDSTR),
cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, obj));
ok = (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
ok = yaffs2_WriteCheckpointTnodes(obj);
}
}
}
}
/* Dump end of list */
memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
cp.structType = sizeof(cp);
if (ok)
ok = (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
return ok ? 1 : 0;
}
static int yaffs2_ReadCheckpointObjects(yaffs_Device *dev)
{
yaffs_Object *obj;
yaffs_CheckpointObject cp;
int ok = 1;
int done = 0;
yaffs_Object *hardList = NULL;
while (ok && !done) {
ok = (yaffs2_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
if (cp.structType != sizeof(cp)) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
cp.structType, (int)sizeof(cp), ok));
ok = 0;
}
T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
if (ok && cp.objectId == ~0)
done = 1;
else if (ok) {
obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
if (obj) {
ok = yaffs2_CheckpointObjectToObject(obj, &cp);
if (!ok)
break;
if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
ok = yaffs2_ReadCheckpointTnodes(obj);
} else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
obj->hardLinks.next =
(struct ylist_head *) hardList;
hardList = obj;
}
} else
ok = 0;
}
}
if (ok)
yaffs_HardlinkFixup(dev, hardList);
return ok ? 1 : 0;
}
static int yaffs2_WriteCheckpointSum(yaffs_Device *dev)
{
__u32 checkpointSum;
int ok;
yaffs2_GetCheckpointSum(dev, &checkpointSum);
ok = (yaffs2_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
if (!ok)
return 0;
return 1;
}
static int yaffs2_ReadCheckpointSum(yaffs_Device *dev)
{
__u32 checkpointSum0;
__u32 checkpointSum1;
int ok;
yaffs2_GetCheckpointSum(dev, &checkpointSum0);
ok = (yaffs2_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
if (!ok)
return 0;
if (checkpointSum0 != checkpointSum1)
return 0;
return 1;
}
static int yaffs2_WriteCheckpointData(yaffs_Device *dev)
{
int ok = 1;
if (!yaffs2_CheckpointRequired(dev)) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
ok = 0;
}
if (ok)
ok = yaffs2_CheckpointOpen(dev, 1);
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
ok = yaffs2_WriteCheckpointValidityMarker(dev, 1);
}
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
ok = yaffs2_WriteCheckpointDevice(dev);
}
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
ok = yaffs2_WriteCheckpointObjects(dev);
}
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
ok = yaffs2_WriteCheckpointValidityMarker(dev, 0);
}
if (ok)
ok = yaffs2_WriteCheckpointSum(dev);
if (!yaffs2_CheckpointClose(dev))
ok = 0;
if (ok)
dev->isCheckpointed = 1;
else
dev->isCheckpointed = 0;
return dev->isCheckpointed;
}
static int yaffs2_ReadCheckpointData(yaffs_Device *dev)
{
int ok = 1;
if(!dev->param.isYaffs2)
ok = 0;
if (ok && dev->param.skipCheckpointRead) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
ok = 0;
}
if (ok)
ok = yaffs2_CheckpointOpen(dev, 0); /* open for read */
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
ok = yaffs2_ReadCheckpointValidityMarker(dev, 1);
}
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
ok = yaffs2_ReadCheckpointDevice(dev);
}
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
ok = yaffs2_ReadCheckpointObjects(dev);
}
if (ok) {
T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
ok = yaffs2_ReadCheckpointValidityMarker(dev, 0);
}
if (ok) {
ok = yaffs2_ReadCheckpointSum(dev);
T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
}
if (!yaffs2_CheckpointClose(dev))
ok = 0;
if (ok)
dev->isCheckpointed = 1;
else
dev->isCheckpointed = 0;
return ok ? 1 : 0;
}
void yaffs2_InvalidateCheckpoint(yaffs_Device *dev)
{
if (dev->isCheckpointed ||
dev->blocksInCheckpoint > 0) {
dev->isCheckpointed = 0;
yaffs2_CheckpointInvalidateStream(dev);
}
if (dev->param.markSuperBlockDirty)
dev->param.markSuperBlockDirty(dev);
}
int yaffs_CheckpointSave(yaffs_Device *dev)
{
T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
yaffs_VerifyObjects(dev);
yaffs_VerifyBlocks(dev);
yaffs_VerifyFreeChunks(dev);
if (!dev->isCheckpointed) {
yaffs2_InvalidateCheckpoint(dev);
yaffs2_WriteCheckpointData(dev);
}
T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
return dev->isCheckpointed;
}
int yaffs2_CheckpointRestore(yaffs_Device *dev)
{
int retval;
T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
retval = yaffs2_ReadCheckpointData(dev);
if (dev->isCheckpointed) {
yaffs_VerifyObjects(dev);
yaffs_VerifyBlocks(dev);
yaffs_VerifyFreeChunks(dev);
}
T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
return retval;
}
int yaffs2_HandleHole(yaffs_Object *obj, loff_t newSize)
{
/* if newsSize > oldFileSize.
* We're going to be writing a hole.
* If the hole is small then write zeros otherwise write a start of hole marker.
*/
loff_t oldFileSize;
int increase;
int smallHole ;
int result = YAFFS_OK;
yaffs_Device *dev = NULL;
__u8 *localBuffer = NULL;
int smallIncreaseOk = 0;
if(!obj)
return YAFFS_FAIL;
if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
return YAFFS_FAIL;
dev = obj->myDev;
/* Bail out if not yaffs2 mode */
if(!dev->param.isYaffs2)
return YAFFS_OK;
oldFileSize = obj->variant.fileVariant.fileSize;
if (newSize <= oldFileSize)
return YAFFS_OK;
increase = newSize - oldFileSize;
if(increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->nDataBytesPerChunk &&
yaffs_CheckSpaceForAllocation(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
smallHole = 1;
else
smallHole = 0;
if(smallHole)
localBuffer= yaffs_GetTempBuffer(dev, __LINE__);
if(localBuffer){
/* fill hole with zero bytes */
int pos = oldFileSize;
int thisWrite;
int written;
memset(localBuffer,0,dev->nDataBytesPerChunk);
smallIncreaseOk = 1;
while(increase > 0 && smallIncreaseOk){
thisWrite = increase;
if(thisWrite > dev->nDataBytesPerChunk)
thisWrite = dev->nDataBytesPerChunk;
written = yaffs_DoWriteDataToFile(obj,localBuffer,pos,thisWrite,0);
if(written == thisWrite){
pos += thisWrite;
increase -= thisWrite;
} else
smallIncreaseOk = 0;
}
yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
/* If we were out of space then reverse any chunks we've added */
if(!smallIncreaseOk)
yaffs_ResizeDown(obj, oldFileSize);
}
if (!smallIncreaseOk &&
obj->parent &&
obj->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
obj->parent->objectId != YAFFS_OBJECTID_DELETED){
/* Write a hole start header with the old file size */
yaffs_UpdateObjectHeader(obj, NULL, 0, 1, 0, NULL);
}
return result;
}
typedef struct {
int seq;
int block;
} yaffs_BlockIndex;
static int yaffs2_ybicmp(const void *a, const void *b)
{
register int aseq = ((yaffs_BlockIndex *)a)->seq;
register int bseq = ((yaffs_BlockIndex *)b)->seq;
register int ablock = ((yaffs_BlockIndex *)a)->block;
register int bblock = ((yaffs_BlockIndex *)b)->block;
if (aseq == bseq)
return ablock - bblock;
else
return aseq - bseq;
}
int yaffs2_ScanBackwards(yaffs_Device *dev)
{
yaffs_ExtendedTags tags;
int blk;
int blockIterator;
int startIterator;
int endIterator;
int nBlocksToScan = 0;
int chunk;
int result;
int c;
int deleted;
yaffs_BlockState state;
yaffs_Object *hardList = NULL;
yaffs_BlockInfo *bi;
__u32 sequenceNumber;
yaffs_ObjectHeader *oh;
yaffs_Object *in;
yaffs_Object *parent;
int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
int itsUnlinked;
__u8 *chunkData;
int fileSize;
int isShrink;
int foundChunksInBlock;
int equivalentObjectId;
int alloc_failed = 0;
yaffs_BlockIndex *blockIndex = NULL;
int altBlockIndex = 0;
T(YAFFS_TRACE_SCAN,
(TSTR
("yaffs2_ScanBackwards starts intstartblk %d intendblk %d..."
TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
if (!blockIndex) {
blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
altBlockIndex = 1;
}
if (!blockIndex) {
T(YAFFS_TRACE_SCAN,
(TSTR("yaffs2_ScanBackwards() could not allocate block index!" TENDSTR)));
return YAFFS_FAIL;
}
dev->blocksInCheckpoint = 0;
chunkData = yaffs_GetTempBuffer(dev, __LINE__);
/* Scan all the blocks to determine their state */
bi = dev->blockInfo;
for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
yaffs_ClearChunkBits(dev, blk);
bi->pagesInUse = 0;
bi->softDeletions = 0;
yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
bi->blockState = state;
bi->sequenceNumber = sequenceNumber;
if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
T(YAFFS_TRACE_SCAN_DEBUG,
(TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
state, sequenceNumber));
if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
dev->blocksInCheckpoint++;
} else if (state == YAFFS_BLOCK_STATE_DEAD) {
T(YAFFS_TRACE_BAD_BLOCKS,
(TSTR("block %d is bad" TENDSTR), blk));
} else if (state == YAFFS_BLOCK_STATE_EMPTY) {
T(YAFFS_TRACE_SCAN_DEBUG,