2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_guts.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_yaffs2.h"
18 #include "yaffs_checkptrw.h"
19 #include "yaffs_bitmap.h"
20 #include "yaffs_qsort.h"
21 #include "yaffs_nand.h"
22 #include "yaffs_getblockinfo.h"
25 * Checkpoints are really no benefit on very small partitions.
27 * To save space on small partitions don't bother with checkpoints unless
28 * the partition is at least this big.
30 #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
32 #define YAFFS_SMALL_HOLE_THRESHOLD 4
36 * Oldest Dirty Sequence Number handling.
39 /* yaffs2_CalcOldestDirtySequence()
40 * yaffs2_FindOldestDirtySequence()
41 * Calculate the oldest dirty sequence number if we don't know it.
43 void yaffs2_CalcOldestDirtySequence(yaffs_Device *dev)
50 if(!dev->param.isYaffs2)
53 /* Find the oldest dirty sequence number. */
54 seq = dev->sequenceNumber + 1;
56 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
57 if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
58 (b->pagesInUse - b->softDeletions) < dev->param.nChunksPerBlock &&
59 b->sequenceNumber < seq) {
60 seq = b->sequenceNumber;
67 dev->oldestDirtySequence = seq;
68 dev->oldestDirtyBlock = blockNo;
74 void yaffs2_FindOldestDirtySequence(yaffs_Device *dev)
76 if(!dev->param.isYaffs2)
79 if(!dev->oldestDirtySequence)
80 yaffs2_CalcOldestDirtySequence(dev);
84 * yaffs_ClearOldestDirtySequence()
85 * Called when a block is erased or marked bad. (ie. when its sequenceNumber
86 * becomes invalid). If the value matches the oldest then we clear
87 * dev->oldestDirtySequence to force its recomputation.
89 void yaffs2_ClearOldestDirtySequence(yaffs_Device *dev, yaffs_BlockInfo *bi)
92 if(!dev->param.isYaffs2)
95 if(!bi || bi->sequenceNumber == dev->oldestDirtySequence){
96 dev->oldestDirtySequence = 0;
97 dev->oldestDirtyBlock = 0;
102 * yaffs2_UpdateOldestDirtySequence()
103 * Update the oldest dirty sequence number whenever we dirty a block.
104 * Only do this if the oldestDirtySequence is actually being tracked.
106 void yaffs2_UpdateOldestDirtySequence(yaffs_Device *dev, unsigned blockNo, yaffs_BlockInfo *bi)
108 if(!dev->param.isYaffs2)
111 if(dev->oldestDirtySequence){
112 if(dev->oldestDirtySequence > bi->sequenceNumber){
113 dev->oldestDirtySequence = bi->sequenceNumber;
114 dev->oldestDirtyBlock = blockNo;
119 int yaffs2_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
123 if (!dev->param.isYaffs2)
124 return 1; /* disqualification only applies to yaffs2. */
126 if (!bi->hasShrinkHeader)
127 return 1; /* can gc */
129 yaffs2_FindOldestDirtySequence(dev);
131 /* Can't do gc of this block if there are any blocks older than this one that have
134 return (bi->sequenceNumber <= dev->oldestDirtySequence);
138 * yaffs2_FindRefreshBlock()
139 * periodically finds the oldest full block by sequence number for refreshing.
142 __u32 yaffs2_FindRefreshBlock(yaffs_Device *dev)
147 __u32 oldestSequence = 0;
151 if(!dev->param.isYaffs2)
155 * If refresh period < 10 then refreshing is disabled.
157 if(dev->param.refreshPeriod < 10)
163 if(dev->refreshSkip > dev->param.refreshPeriod)
164 dev->refreshSkip = dev->param.refreshPeriod;
166 if(dev->refreshSkip > 0)
170 * Refresh skip is now zero.
171 * We'll do a refresh this time around....
172 * Update the refresh skip and find the oldest block.
174 dev->refreshSkip = dev->param.refreshPeriod;
177 for (b = dev->internalStartBlock; b <=dev->internalEndBlock; b++){
179 if (bi->blockState == YAFFS_BLOCK_STATE_FULL){
182 bi->sequenceNumber < oldestSequence){
184 oldestSequence = bi->sequenceNumber;
192 (TSTR("GC refresh count %d selected block %d with sequenceNumber %d" TENDSTR),
193 dev->refreshCount, oldest, oldestSequence));
199 int yaffs2_CheckpointRequired(yaffs_Device *dev)
203 if(!dev->param.isYaffs2)
206 nblocks = dev->internalEndBlock - dev->internalStartBlock + 1 ;
208 return !dev->param.skipCheckpointWrite &&
209 (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
212 int yaffs2_CalcCheckpointBlocksRequired(yaffs_Device *dev)
216 if(!dev->param.isYaffs2)
219 if (!dev->nCheckpointBlocksRequired &&
220 yaffs2_CheckpointRequired(dev)){
221 /* Not a valid value so recalculate */
224 int devBlocks = (dev->param.endBlock - dev->param.startBlock + 1);
226 nBytes += sizeof(yaffs_CheckpointValidity);
227 nBytes += sizeof(yaffs_CheckpointDevice);
228 nBytes += devBlocks * sizeof(yaffs_BlockInfo);
229 nBytes += devBlocks * dev->chunkBitmapStride;
230 nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjects);
231 nBytes += (dev->tnodeSize + sizeof(__u32)) * (dev->nTnodes);
232 nBytes += sizeof(yaffs_CheckpointValidity);
233 nBytes += sizeof(__u32); /* checksum*/
235 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
237 nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->param.nChunksPerBlock)) + 3;
239 dev->nCheckpointBlocksRequired = nBlocks;
242 retval = dev->nCheckpointBlocksRequired - dev->blocksInCheckpoint;
248 /*--------------------- Checkpointing --------------------*/
251 static int yaffs2_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
253 yaffs_CheckpointValidity cp;
255 memset(&cp, 0, sizeof(cp));
257 cp.structType = sizeof(cp);
258 cp.magic = YAFFS_MAGIC;
259 cp.version = YAFFS_CHECKPOINT_VERSION;
260 cp.head = (head) ? 1 : 0;
262 return (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
266 static int yaffs2_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
268 yaffs_CheckpointValidity cp;
271 ok = (yaffs2_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
274 ok = (cp.structType == sizeof(cp)) &&
275 (cp.magic == YAFFS_MAGIC) &&
276 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
277 (cp.head == ((head) ? 1 : 0));
281 static void yaffs2_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
284 cp->nErasedBlocks = dev->nErasedBlocks;
285 cp->allocationBlock = dev->allocationBlock;
286 cp->allocationPage = dev->allocationPage;
287 cp->nFreeChunks = dev->nFreeChunks;
289 cp->nDeletedFiles = dev->nDeletedFiles;
290 cp->nUnlinkedFiles = dev->nUnlinkedFiles;
291 cp->nBackgroundDeletions = dev->nBackgroundDeletions;
292 cp->sequenceNumber = dev->sequenceNumber;
296 static void yaffs2_CheckpointDeviceToDevice(yaffs_Device *dev,
297 yaffs_CheckpointDevice *cp)
299 dev->nErasedBlocks = cp->nErasedBlocks;
300 dev->allocationBlock = cp->allocationBlock;
301 dev->allocationPage = cp->allocationPage;
302 dev->nFreeChunks = cp->nFreeChunks;
304 dev->nDeletedFiles = cp->nDeletedFiles;
305 dev->nUnlinkedFiles = cp->nUnlinkedFiles;
306 dev->nBackgroundDeletions = cp->nBackgroundDeletions;
307 dev->sequenceNumber = cp->sequenceNumber;
311 static int yaffs2_WriteCheckpointDevice(yaffs_Device *dev)
313 yaffs_CheckpointDevice cp;
315 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
319 /* Write device runtime values*/
320 yaffs2_DeviceToCheckpointDevice(&cp, dev);
321 cp.structType = sizeof(cp);
323 ok = (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
325 /* Write block info */
327 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
328 ok = (yaffs2_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
331 /* Write chunk bits */
333 nBytes = nBlocks * dev->chunkBitmapStride;
334 ok = (yaffs2_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
340 static int yaffs2_ReadCheckpointDevice(yaffs_Device *dev)
342 yaffs_CheckpointDevice cp;
344 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
348 ok = (yaffs2_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
352 if (cp.structType != sizeof(cp))
356 yaffs2_CheckpointDeviceToDevice(dev, &cp);
358 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
360 ok = (yaffs2_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
364 nBytes = nBlocks * dev->chunkBitmapStride;
366 ok = (yaffs2_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
371 static void yaffs2_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
375 cp->objectId = obj->objectId;
376 cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
377 cp->hdrChunk = obj->hdrChunk;
378 cp->variantType = obj->variantType;
379 cp->deleted = obj->deleted;
380 cp->softDeleted = obj->softDeleted;
381 cp->unlinked = obj->unlinked;
382 cp->fake = obj->fake;
383 cp->renameAllowed = obj->renameAllowed;
384 cp->unlinkAllowed = obj->unlinkAllowed;
385 cp->serial = obj->serial;
386 cp->nDataChunks = obj->nDataChunks;
388 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
389 cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
390 else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
391 cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
394 static int yaffs2_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
397 yaffs_Object *parent;
399 if (obj->variantType != cp->variantType) {
400 T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
401 TCONT("chunk %d does not match existing object type %d")
402 TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
407 obj->objectId = cp->objectId;
410 parent = yaffs_FindOrCreateObjectByNumber(
413 YAFFS_OBJECT_TYPE_DIRECTORY);
418 if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
419 T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
420 TCONT(" chunk %d Parent type, %d, not directory")
422 cp->objectId, cp->parentId, cp->variantType,
423 cp->hdrChunk, parent->variantType));
426 yaffs_AddObjectToDirectory(parent, obj);
429 obj->hdrChunk = cp->hdrChunk;
430 obj->variantType = cp->variantType;
431 obj->deleted = cp->deleted;
432 obj->softDeleted = cp->softDeleted;
433 obj->unlinked = cp->unlinked;
434 obj->fake = cp->fake;
435 obj->renameAllowed = cp->renameAllowed;
436 obj->unlinkAllowed = cp->unlinkAllowed;
437 obj->serial = cp->serial;
438 obj->nDataChunks = cp->nDataChunks;
440 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
441 obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
442 else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
443 obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
445 if (obj->hdrChunk > 0)
452 static int yaffs2_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
453 __u32 level, int chunkOffset)
456 yaffs_Device *dev = in->myDev;
462 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
463 if (tn->internal[i]) {
464 ok = yaffs2_CheckpointTnodeWorker(in,
467 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
470 } else if (level == 0) {
471 __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
472 ok = (yaffs2_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
474 ok = (yaffs2_CheckpointWrite(dev, tn, dev->tnodeSize) == dev->tnodeSize);
482 static int yaffs2_WriteCheckpointTnodes(yaffs_Object *obj)
484 __u32 endMarker = ~0;
487 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
488 ok = yaffs2_CheckpointTnodeWorker(obj,
489 obj->variant.fileVariant.top,
490 obj->variant.fileVariant.topLevel,
493 ok = (yaffs2_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
500 static int yaffs2_ReadCheckpointTnodes(yaffs_Object *obj)
504 yaffs_Device *dev = obj->myDev;
505 yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
509 ok = (yaffs2_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
511 while (ok && (~baseChunk)) {
513 /* Read level 0 tnode */
516 tn = yaffs_GetTnode(dev);
518 ok = (yaffs2_CheckpointRead(dev, tn, dev->tnodeSize) == dev->tnodeSize);
523 ok = yaffs_AddOrFindLevel0Tnode(dev,
529 ok = (yaffs2_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
533 T(YAFFS_TRACE_CHECKPOINT, (
534 TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
535 nread, baseChunk, ok));
541 static int yaffs2_WriteCheckpointObjects(yaffs_Device *dev)
544 yaffs_CheckpointObject cp;
547 struct ylist_head *lh;
550 /* Iterate through the objects in each hash entry,
551 * dumping them to the checkpointing stream.
554 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
555 ylist_for_each(lh, &dev->objectBucket[i].list) {
557 obj = ylist_entry(lh, yaffs_Object, hashLink);
558 if (!obj->deferedFree) {
559 yaffs2_ObjectToCheckpointObject(&cp, obj);
560 cp.structType = sizeof(cp);
562 T(YAFFS_TRACE_CHECKPOINT, (
563 TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" TENDSTR),
564 cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, obj));
566 ok = (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
568 if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
569 ok = yaffs2_WriteCheckpointTnodes(obj);
575 /* Dump end of list */
576 memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
577 cp.structType = sizeof(cp);
580 ok = (yaffs2_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
585 static int yaffs2_ReadCheckpointObjects(yaffs_Device *dev)
588 yaffs_CheckpointObject cp;
591 yaffs_Object *hardList = NULL;
593 while (ok && !done) {
594 ok = (yaffs2_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
595 if (cp.structType != sizeof(cp)) {
596 T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
597 cp.structType, (int)sizeof(cp), ok));
601 T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
602 cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
604 if (ok && cp.objectId == ~0)
607 obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
609 ok = yaffs2_CheckpointObjectToObject(obj, &cp);
612 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
613 ok = yaffs2_ReadCheckpointTnodes(obj);
614 } else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
615 obj->hardLinks.next =
616 (struct ylist_head *) hardList;
625 yaffs_HardlinkFixup(dev, hardList);
630 static int yaffs2_WriteCheckpointSum(yaffs_Device *dev)
635 yaffs2_GetCheckpointSum(dev, &checkpointSum);
637 ok = (yaffs2_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
645 static int yaffs2_ReadCheckpointSum(yaffs_Device *dev)
647 __u32 checkpointSum0;
648 __u32 checkpointSum1;
651 yaffs2_GetCheckpointSum(dev, &checkpointSum0);
653 ok = (yaffs2_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
658 if (checkpointSum0 != checkpointSum1)
665 static int yaffs2_WriteCheckpointData(yaffs_Device *dev)
669 if (!yaffs2_CheckpointRequired(dev)) {
670 T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
675 ok = yaffs2_CheckpointOpen(dev, 1);
678 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
679 ok = yaffs2_WriteCheckpointValidityMarker(dev, 1);
682 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
683 ok = yaffs2_WriteCheckpointDevice(dev);
686 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
687 ok = yaffs2_WriteCheckpointObjects(dev);
690 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
691 ok = yaffs2_WriteCheckpointValidityMarker(dev, 0);
695 ok = yaffs2_WriteCheckpointSum(dev);
697 if (!yaffs2_CheckpointClose(dev))
701 dev->isCheckpointed = 1;
703 dev->isCheckpointed = 0;
705 return dev->isCheckpointed;
708 static int yaffs2_ReadCheckpointData(yaffs_Device *dev)
712 if(!dev->param.isYaffs2)
715 if (ok && dev->param.skipCheckpointRead) {
716 T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
721 ok = yaffs2_CheckpointOpen(dev, 0); /* open for read */
724 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
725 ok = yaffs2_ReadCheckpointValidityMarker(dev, 1);
728 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
729 ok = yaffs2_ReadCheckpointDevice(dev);
732 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
733 ok = yaffs2_ReadCheckpointObjects(dev);
736 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
737 ok = yaffs2_ReadCheckpointValidityMarker(dev, 0);
741 ok = yaffs2_ReadCheckpointSum(dev);
742 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
745 if (!yaffs2_CheckpointClose(dev))
749 dev->isCheckpointed = 1;
751 dev->isCheckpointed = 0;
757 void yaffs2_InvalidateCheckpoint(yaffs_Device *dev)
759 if (dev->isCheckpointed ||
760 dev->blocksInCheckpoint > 0) {
761 dev->isCheckpointed = 0;
762 yaffs2_CheckpointInvalidateStream(dev);
764 if (dev->param.markSuperBlockDirty)
765 dev->param.markSuperBlockDirty(dev);
769 int yaffs_CheckpointSave(yaffs_Device *dev)
772 T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
774 yaffs_VerifyObjects(dev);
775 yaffs_VerifyBlocks(dev);
776 yaffs_VerifyFreeChunks(dev);
778 if (!dev->isCheckpointed) {
779 yaffs2_InvalidateCheckpoint(dev);
780 yaffs2_WriteCheckpointData(dev);
783 T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
785 return dev->isCheckpointed;
788 int yaffs2_CheckpointRestore(yaffs_Device *dev)
791 T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
793 retval = yaffs2_ReadCheckpointData(dev);
795 if (dev->isCheckpointed) {
796 yaffs_VerifyObjects(dev);
797 yaffs_VerifyBlocks(dev);
798 yaffs_VerifyFreeChunks(dev);
801 T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
806 int yaffs2_HandleHole(yaffs_Object *obj, loff_t newSize)
808 /* if newsSize > oldFileSize.
809 * We're going to be writing a hole.
810 * If the hole is small then write zeros otherwise write a start of hole marker.
817 int result = YAFFS_OK;
818 yaffs_Device *dev = NULL;
820 __u8 *localBuffer = NULL;
822 int smallIncreaseOk = 0;
827 if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
832 /* Bail out if not yaffs2 mode */
833 if(!dev->param.isYaffs2)
836 oldFileSize = obj->variant.fileVariant.fileSize;
838 if (newSize <= oldFileSize)
841 increase = newSize - oldFileSize;
843 if(increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->nDataBytesPerChunk &&
844 yaffs_CheckSpaceForAllocation(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
850 localBuffer= yaffs_GetTempBuffer(dev, __LINE__);
853 /* fill hole with zero bytes */
854 int pos = oldFileSize;
857 memset(localBuffer,0,dev->nDataBytesPerChunk);
860 while(increase > 0 && smallIncreaseOk){
861 thisWrite = increase;
862 if(thisWrite > dev->nDataBytesPerChunk)
863 thisWrite = dev->nDataBytesPerChunk;
864 written = yaffs_DoWriteDataToFile(obj,localBuffer,pos,thisWrite,0);
865 if(written == thisWrite){
867 increase -= thisWrite;
872 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
874 /* If we were out of space then reverse any chunks we've added */
876 yaffs_ResizeDown(obj, oldFileSize);
879 if (!smallIncreaseOk &&
881 obj->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
882 obj->parent->objectId != YAFFS_OBJECTID_DELETED){
883 /* Write a hole start header with the old file size */
884 yaffs_UpdateObjectHeader(obj, NULL, 0, 1, 0, NULL);
898 static int yaffs2_ybicmp(const void *a, const void *b)
900 register int aseq = ((yaffs_BlockIndex *)a)->seq;
901 register int bseq = ((yaffs_BlockIndex *)b)->seq;
902 register int ablock = ((yaffs_BlockIndex *)a)->block;
903 register int bblock = ((yaffs_BlockIndex *)b)->block;
905 return ablock - bblock;
910 int yaffs2_ScanBackwards(yaffs_Device *dev)
912 yaffs_ExtendedTags tags;
917 int nBlocksToScan = 0;
923 yaffs_BlockState state;
924 yaffs_Object *hardList = NULL;
926 __u32 sequenceNumber;
927 yaffs_ObjectHeader *oh;
929 yaffs_Object *parent;
930 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
936 int foundChunksInBlock;
937 int equivalentObjectId;
938 int alloc_failed = 0;
941 yaffs_BlockIndex *blockIndex = NULL;
942 int altBlockIndex = 0;
946 ("yaffs2_ScanBackwards starts intstartblk %d intendblk %d..."
947 TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
950 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
952 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
955 blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
961 (TSTR("yaffs2_ScanBackwards() could not allocate block index!" TENDSTR)));
965 dev->blocksInCheckpoint = 0;
967 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
969 /* Scan all the blocks to determine their state */
971 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
972 yaffs_ClearChunkBits(dev, blk);
974 bi->softDeletions = 0;
976 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
978 bi->blockState = state;
979 bi->sequenceNumber = sequenceNumber;
981 if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
982 bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
983 if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
984 bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
986 T(YAFFS_TRACE_SCAN_DEBUG,
987 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
988 state, sequenceNumber));
991 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
992 dev->blocksInCheckpoint++;
994 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
995 T(YAFFS_TRACE_BAD_BLOCKS,
996 (TSTR("block %d is bad" TENDSTR), blk));
997 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
998 T(YAFFS_TRACE_SCAN_DEBUG,
999 (TSTR("Block empty " TENDSTR)));
1000 dev->nErasedBlocks++;
1001 dev->nFreeChunks += dev->param.nChunksPerBlock;
1002 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1004 /* Determine the highest sequence number */
1005 if (sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1006 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1008 blockIndex[nBlocksToScan].seq = sequenceNumber;
1009 blockIndex[nBlocksToScan].block = blk;
1013 if (sequenceNumber >= dev->sequenceNumber)
1014 dev->sequenceNumber = sequenceNumber;
1016 /* TODO: Nasty sequence number! */
1019 ("Block scanning block %d has bad sequence number %d"
1020 TENDSTR), blk, sequenceNumber));
1028 (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
1034 /* Sort the blocks by sequence number*/
1035 yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), yaffs2_ybicmp);
1039 T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
1041 /* Now scan the blocks looking at the data. */
1043 endIterator = nBlocksToScan - 1;
1044 T(YAFFS_TRACE_SCAN_DEBUG,
1045 (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
1047 /* For each block.... backwards */
1048 for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
1050 /* Cooperative multitasking! This loop can run for so
1051 long that watchdog timers expire. */
1054 /* get the block to scan in the correct order */
1055 blk = blockIndex[blockIterator].block;
1057 bi = yaffs_GetBlockInfo(dev, blk);
1060 state = bi->blockState;
1064 /* For each chunk in each block that needs scanning.... */
1065 foundChunksInBlock = 0;
1066 for (c = dev->param.nChunksPerBlock - 1;
1067 !alloc_failed && c >= 0 &&
1068 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1069 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
1070 /* Scan backwards...
1071 * Read the tags and decide what to do
1074 chunk = blk * dev->param.nChunksPerBlock + c;
1076 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
1079 /* Let's have a good look at this chunk... */
1081 if (!tags.chunkUsed) {
1082 /* An unassigned chunk in the block.
1083 * If there are used chunks after this one, then
1084 * it is a chunk that was skipped due to failing the erased
1085 * check. Just skip it so that it can be deleted.
1086 * But, more typically, We get here when this is an unallocated
1087 * chunk and his means that either the block is empty or
1088 * this is the one being allocated from
1091 if (foundChunksInBlock) {
1092 /* This is a chunk that was skipped due to failing the erased check */
1093 } else if (c == 0) {
1094 /* We're looking at the first chunk in the block so the block is unused */
1095 state = YAFFS_BLOCK_STATE_EMPTY;
1096 dev->nErasedBlocks++;
1098 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1099 state == YAFFS_BLOCK_STATE_ALLOCATING) {
1100 if (dev->sequenceNumber == bi->sequenceNumber) {
1101 /* this is the block being allocated from */
1105 (" Allocating from %d %d"
1108 state = YAFFS_BLOCK_STATE_ALLOCATING;
1109 dev->allocationBlock = blk;
1110 dev->allocationPage = c;
1111 dev->allocationBlockFinder = blk;
1113 /* This is a partially written block that is not
1114 * the current allocation block.
1118 (TSTR("Partially written block %d detected" TENDSTR),
1126 } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED) {
1128 (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
1133 } else if (tags.objectId > YAFFS_MAX_OBJECT_ID ||
1134 tags.chunkId > YAFFS_MAX_CHUNK_ID ||
1135 (tags.chunkId > 0 && tags.byteCount > dev->nDataBytesPerChunk) ||
1136 tags.sequenceNumber != bi->sequenceNumber ) {
1138 (TSTR("Chunk (%d:%d) with bad tags:obj = %d, chunkId = %d, byteCount = %d, ignored"TENDSTR),
1139 blk, c,tags.objectId, tags.chunkId, tags.byteCount));
1143 } else if (tags.chunkId > 0) {
1144 /* chunkId > 0 so it is a data chunk... */
1145 unsigned int endpos;
1147 (tags.chunkId - 1) * dev->nDataBytesPerChunk;
1149 foundChunksInBlock = 1;
1152 yaffs_SetChunkBit(dev, blk, c);
1155 in = yaffs_FindOrCreateObjectByNumber(dev,
1158 YAFFS_OBJECT_TYPE_FILE);
1165 in->variantType == YAFFS_OBJECT_TYPE_FILE
1166 && chunkBase < in->variant.fileVariant.shrinkSize) {
1167 /* This has not been invalidated by a resize */
1168 if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, -1)) {
1172 /* File size is calculated by looking at the data chunks if we have not
1173 * seen an object header yet. Stop this practice once we find an object header.
1175 endpos = chunkBase + tags.byteCount;
1177 if (!in->valid && /* have not got an object header yet */
1178 in->variant.fileVariant.scannedFileSize < endpos) {
1179 in->variant.fileVariant.scannedFileSize = endpos;
1180 in->variant.fileVariant.fileSize = endpos;
1184 /* This chunk has been invalidated by a resize, or a past file deletion
1185 * so delete the chunk*/
1186 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
1190 /* chunkId == 0, so it is an ObjectHeader.
1191 * Thus, we read in the object header and make the object
1193 foundChunksInBlock = 1;
1195 yaffs_SetChunkBit(dev, blk, c);
1201 if (tags.extraHeaderInfoAvailable) {
1202 in = yaffs_FindOrCreateObjectByNumber(dev,
1204 tags.extraObjectType);
1210 (!in->valid && dev->param.disableLazyLoad) ||
1211 tags.extraShadows ||
1213 (tags.objectId == YAFFS_OBJECTID_ROOT ||
1214 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))) {
1216 /* If we don't have valid info then we need to read the chunk
1217 * TODO In future we can probably defer reading the chunk and
1218 * living with invalid data until needed.
1221 result = yaffs_ReadChunkWithTagsFromNAND(dev,
1226 oh = (yaffs_ObjectHeader *) chunkData;
1228 if (dev->param.inbandTags) {
1229 /* Fix up the header if they got corrupted by inband tags */
1230 oh->shadowsObject = oh->inbandShadowsObject;
1231 oh->isShrink = oh->inbandIsShrink;
1235 in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
1243 /* TODO Hoosterman we have a problem! */
1244 T(YAFFS_TRACE_ERROR,
1246 ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
1247 TENDSTR), tags.objectId, chunk));
1252 /* We have already filled this one.
1253 * We have a duplicate that will be discarded, but
1254 * we first have to suck out resize info if it is a file.
1257 if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
1259 oh->type == YAFFS_OBJECT_TYPE_FILE) ||
1260 (tags.extraHeaderInfoAvailable &&
1261 tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))) {
1263 (oh) ? oh->fileSize : tags.
1265 __u32 parentObjectId =
1267 parentObjectId : tags.
1268 extraParentObjectId;
1272 (oh) ? oh->isShrink : tags.
1273 extraIsShrinkHeader;
1275 /* If it is deleted (unlinked at start also means deleted)
1276 * we treat the file size as being zeroed at this point.
1278 if (parentObjectId ==
1279 YAFFS_OBJECTID_DELETED
1280 || parentObjectId ==
1281 YAFFS_OBJECTID_UNLINKED) {
1286 if (isShrink && in->variant.fileVariant.shrinkSize > thisSize)
1287 in->variant.fileVariant.shrinkSize = thisSize;
1290 bi->hasShrinkHeader = 1;
1293 /* Use existing - destroy this one. */
1294 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
1298 if (!in->valid && in->variantType !=
1299 (oh ? oh->type : tags.extraObjectType))
1300 T(YAFFS_TRACE_ERROR, (
1301 TSTR("yaffs tragedy: Bad object type, "
1302 TCONT("%d != %d, for object %d at chunk ")
1303 TCONT("%d during scan")
1305 oh->type : tags.extraObjectType,
1306 in->variantType, tags.objectId,
1310 (tags.objectId == YAFFS_OBJECTID_ROOT ||
1312 YAFFS_OBJECTID_LOSTNFOUND)) {
1313 /* We only load some info, don't fiddle with directory structure */
1318 in->yst_mode = oh->yst_mode;
1319 #ifdef CONFIG_YAFFS_WINCE
1320 in->win_atime[0] = oh->win_atime[0];
1321 in->win_ctime[0] = oh->win_ctime[0];
1322 in->win_mtime[0] = oh->win_mtime[0];
1323 in->win_atime[1] = oh->win_atime[1];
1324 in->win_ctime[1] = oh->win_ctime[1];
1325 in->win_mtime[1] = oh->win_mtime[1];
1327 in->yst_uid = oh->yst_uid;
1328 in->yst_gid = oh->yst_gid;
1329 in->yst_atime = oh->yst_atime;
1330 in->yst_mtime = oh->yst_mtime;
1331 in->yst_ctime = oh->yst_ctime;
1332 in->yst_rdev = oh->yst_rdev;
1340 in->hdrChunk = chunk;
1342 } else if (!in->valid) {
1343 /* we need to load this info */
1346 in->hdrChunk = chunk;
1349 in->variantType = oh->type;
1351 in->yst_mode = oh->yst_mode;
1352 #ifdef CONFIG_YAFFS_WINCE
1353 in->win_atime[0] = oh->win_atime[0];
1354 in->win_ctime[0] = oh->win_ctime[0];
1355 in->win_mtime[0] = oh->win_mtime[0];
1356 in->win_atime[1] = oh->win_atime[1];
1357 in->win_ctime[1] = oh->win_ctime[1];
1358 in->win_mtime[1] = oh->win_mtime[1];
1360 in->yst_uid = oh->yst_uid;
1361 in->yst_gid = oh->yst_gid;
1362 in->yst_atime = oh->yst_atime;
1363 in->yst_mtime = oh->yst_mtime;
1364 in->yst_ctime = oh->yst_ctime;
1365 in->yst_rdev = oh->yst_rdev;
1368 if (oh->shadowsObject > 0)
1369 yaffs_HandleShadowedObject(dev,
1376 yaffs_SetObjectNameFromOH(in, oh);
1378 yaffs_FindOrCreateObjectByNumber
1379 (dev, oh->parentObjectId,
1380 YAFFS_OBJECT_TYPE_DIRECTORY);
1382 fileSize = oh->fileSize;
1383 isShrink = oh->isShrink;
1384 equivalentObjectId = oh->equivalentObjectId;
1387 in->variantType = tags.extraObjectType;
1389 yaffs_FindOrCreateObjectByNumber
1390 (dev, tags.extraParentObjectId,
1391 YAFFS_OBJECT_TYPE_DIRECTORY);
1392 fileSize = tags.extraFileLength;
1393 isShrink = tags.extraIsShrinkHeader;
1394 equivalentObjectId = tags.extraEquivalentObjectId;
1403 /* directory stuff...
1407 if (parent && parent->variantType ==
1408 YAFFS_OBJECT_TYPE_UNKNOWN) {
1409 /* Set up as a directory */
1410 parent->variantType =
1411 YAFFS_OBJECT_TYPE_DIRECTORY;
1412 YINIT_LIST_HEAD(&parent->variant.
1415 } else if (!parent || parent->variantType !=
1416 YAFFS_OBJECT_TYPE_DIRECTORY) {
1417 /* Hoosterman, another problem....
1418 * We're trying to use a non-directory as a directory
1421 T(YAFFS_TRACE_ERROR,
1423 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1425 parent = dev->lostNFoundDir;
1428 yaffs_AddObjectToDirectory(parent, in);
1430 itsUnlinked = (parent == dev->deletedDir) ||
1431 (parent == dev->unlinkedDir);
1434 /* Mark the block as having a shrinkHeader */
1435 bi->hasShrinkHeader = 1;
1438 /* Note re hardlinks.
1439 * Since we might scan a hardlink before its equivalent object is scanned
1440 * we put them all in a list.
1441 * After scanning is complete, we should have all the objects, so we run
1442 * through this list and fix up all the chains.
1445 switch (in->variantType) {
1446 case YAFFS_OBJECT_TYPE_UNKNOWN:
1447 /* Todo got a problem */
1449 case YAFFS_OBJECT_TYPE_FILE:
1451 if (in->variant.fileVariant.
1452 scannedFileSize < fileSize) {
1453 /* This covers the case where the file size is greater
1454 * than where the data is
1455 * This will happen if the file is resized to be larger
1456 * than its current data extents.
1458 in->variant.fileVariant.fileSize = fileSize;
1459 in->variant.fileVariant.scannedFileSize = fileSize;
1462 if (in->variant.fileVariant.shrinkSize > fileSize)
1463 in->variant.fileVariant.shrinkSize = fileSize;
1467 case YAFFS_OBJECT_TYPE_HARDLINK:
1469 in->variant.hardLinkVariant.equivalentObjectId =
1471 in->hardLinks.next =
1472 (struct ylist_head *) hardList;
1476 case YAFFS_OBJECT_TYPE_DIRECTORY:
1479 case YAFFS_OBJECT_TYPE_SPECIAL:
1482 case YAFFS_OBJECT_TYPE_SYMLINK:
1484 in->variant.symLinkVariant.alias =
1485 yaffs_CloneString(oh->alias);
1486 if (!in->variant.symLinkVariant.alias)
1496 } /* End of scanning for each chunk */
1498 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1499 /* If we got this far while scanning, then the block is fully allocated. */
1500 state = YAFFS_BLOCK_STATE_FULL;
1504 bi->blockState = state;
1506 /* Now let's see if it was dirty */
1507 if (bi->pagesInUse == 0 &&
1508 !bi->hasShrinkHeader &&
1509 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
1510 yaffs_BlockBecameDirty(dev, blk);
1515 yaffs_SkipRestOfBlock(dev);
1518 YFREE_ALT(blockIndex);
1522 /* Ok, we've done all the scanning.
1523 * Fix up the hard link chains.
1524 * We should now have scanned all the objects, now it's time to add these
1527 yaffs_HardlinkFixup(dev, hardList);
1530 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
1535 T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_ScanBackwards ends" TENDSTR)));