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.
14 #include "yaffs_trace.h"
16 #include "yaffsinterface.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_tagsvalidity.h"
19 #include "yaffs_getblockinfo.h"
21 #include "yaffs_tagscompat.h"
22 #ifndef CONFIG_YAFFS_USE_OWN_SORT
23 #include "yaffs_qsort.h"
25 #include "yaffs_nand.h"
27 #include "yaffs_checkptrw.h"
29 #include "yaffs_nand.h"
30 #include "yaffs_packedtags2.h"
33 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
34 #define YAFFS_GC_GOOD_ENOUGH 2
35 #define YAFFS_GC_PASSIVE_THRESHOLD 4
37 #define YAFFS_SMALL_HOLE_THRESHOLD 3
40 * Checkpoints are really no benefit on very small partitions.
42 * To save space on small partitions don't bother with checkpoints unless
43 * the partition is at least this big.
45 #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
47 #include "yaffs_ecc.h"
50 /* Robustification (if it ever comes about...) */
51 static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
52 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
54 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
56 const yaffs_ExtendedTags *tags);
57 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
58 const yaffs_ExtendedTags *tags);
60 /* Other local prototypes */
61 static void yaffs_UpdateParent(yaffs_Object *obj);
62 static int yaffs_UnlinkObject(yaffs_Object *obj);
63 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
65 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
67 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
69 yaffs_ExtendedTags *tags,
71 static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
72 int chunkInNAND, int inScan);
74 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
75 yaffs_ObjectType type);
76 static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
78 static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
79 int force, int isShrink, int shadows);
80 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
81 static int yaffs_CheckStructures(void);
82 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
84 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
87 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
90 static int yaffs_UnlinkWorker(yaffs_Object *obj);
92 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
95 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
96 yaffs_BlockInfo **blockUsedPtr);
98 static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
100 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
102 static void yaffs_VerifyDirectory(yaffs_Object *directory);
103 #ifdef YAFFS_PARANOID
104 static int yaffs_CheckFileSanity(yaffs_Object *in);
106 #define yaffs_CheckFileSanity(in)
109 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
110 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
112 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
114 static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
115 yaffs_ExtendedTags *tags);
117 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
119 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
120 yaffs_FileStructure *fStruct,
123 static int yaffs_HandleHole(yaffs_Object *obj, loff_t newSize);
124 static void yaffs_SkipRestOfBlock(yaffs_Device *dev);
125 static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
128 yaffs_ExtendedTags *tags);
130 /* Function to calculate chunk and offset */
132 static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
138 chunk = (__u32)(addr >> dev->chunkShift);
140 if (dev->chunkDiv == 1) {
141 /* easy power of 2 case */
142 offset = (__u32)(addr & dev->chunkMask);
144 /* Non power-of-2 case */
148 chunk /= dev->chunkDiv;
150 chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
151 offset = (__u32)(addr - chunkBase);
158 /* Function to return the number of shifts for a power of 2 greater than or
159 * equal to the given number
160 * Note we don't try to cater for all possible numbers and this does not have to
161 * be hellishly efficient.
164 static __u32 ShiftsGE(__u32 x)
169 nShifts = extraBits = 0;
184 /* Function to return the number of shifts to get a 1 in bit 0
187 static __u32 Shifts(__u32 x)
207 * Temporary buffer manipulations.
210 static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
213 __u8 *buf = (__u8 *)1;
215 memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
217 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
218 dev->tempBuffer[i].line = 0; /* not in use */
219 dev->tempBuffer[i].buffer = buf =
220 YMALLOC_DMA(dev->param.totalBytesPerChunk);
223 return buf ? YAFFS_OK : YAFFS_FAIL;
226 __u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
231 if (dev->tempInUse > dev->maxTemp)
232 dev->maxTemp = dev->tempInUse;
234 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
235 if (dev->tempBuffer[i].line == 0) {
236 dev->tempBuffer[i].line = lineNo;
237 if ((i + 1) > dev->maxTemp) {
238 dev->maxTemp = i + 1;
239 for (j = 0; j <= i; j++)
240 dev->tempBuffer[j].maxLine =
241 dev->tempBuffer[j].line;
244 return dev->tempBuffer[i].buffer;
248 T(YAFFS_TRACE_BUFFERS,
249 (TSTR("Out of temp buffers at line %d, other held by lines:"),
251 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
252 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
254 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
257 * If we got here then we have to allocate an unmanaged one
261 dev->unmanagedTempAllocations++;
262 return YMALLOC(dev->nDataBytesPerChunk);
266 void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
273 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
274 if (dev->tempBuffer[i].buffer == buffer) {
275 dev->tempBuffer[i].line = 0;
281 /* assume it is an unmanaged one. */
282 T(YAFFS_TRACE_BUFFERS,
283 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
286 dev->unmanagedTempDeallocations++;
292 * Determine if we have a managed buffer.
294 int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
298 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
299 if (dev->tempBuffer[i].buffer == buffer)
303 for (i = 0; i < dev->param.nShortOpCaches; i++) {
304 if (dev->srCache[i].data == buffer)
308 if (buffer == dev->checkpointBuffer)
311 T(YAFFS_TRACE_ALWAYS,
312 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
319 * Chunk bitmap manipulations
322 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
324 if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
326 (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
330 return dev->chunkBits +
331 (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
334 static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
336 if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
337 chunk < 0 || chunk >= dev->param.nChunksPerBlock) {
339 (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
345 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
347 __u8 *blkBits = yaffs_BlockBits(dev, blk);
349 memset(blkBits, 0, dev->chunkBitmapStride);
352 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
354 __u8 *blkBits = yaffs_BlockBits(dev, blk);
356 yaffs_VerifyChunkBitId(dev, blk, chunk);
358 blkBits[chunk / 8] &= ~(1 << (chunk & 7));
361 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
363 __u8 *blkBits = yaffs_BlockBits(dev, blk);
365 yaffs_VerifyChunkBitId(dev, blk, chunk);
367 blkBits[chunk / 8] |= (1 << (chunk & 7));
370 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
372 __u8 *blkBits = yaffs_BlockBits(dev, blk);
373 yaffs_VerifyChunkBitId(dev, blk, chunk);
375 return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
378 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
380 __u8 *blkBits = yaffs_BlockBits(dev, blk);
382 for (i = 0; i < dev->chunkBitmapStride; i++) {
390 static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
392 __u8 *blkBits = yaffs_BlockBits(dev, blk);
395 for (i = 0; i < dev->chunkBitmapStride; i++) {
412 static int yaffs_SkipVerification(yaffs_Device *dev)
415 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
418 static int yaffs_SkipFullVerification(yaffs_Device *dev)
421 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
424 static int yaffs_SkipNANDVerification(yaffs_Device *dev)
427 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
430 static const char *blockStateName[] = {
443 static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
448 if (yaffs_SkipVerification(dev))
451 /* Report illegal runtime states */
452 if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
453 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
455 switch (bi->blockState) {
456 case YAFFS_BLOCK_STATE_UNKNOWN:
457 case YAFFS_BLOCK_STATE_SCANNING:
458 case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
459 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
460 n, blockStateName[bi->blockState]));
463 /* Check pages in use and soft deletions are legal */
465 actuallyUsed = bi->pagesInUse - bi->softDeletions;
467 if (bi->pagesInUse < 0 || bi->pagesInUse > dev->param.nChunksPerBlock ||
468 bi->softDeletions < 0 || bi->softDeletions > dev->param.nChunksPerBlock ||
469 actuallyUsed < 0 || actuallyUsed > dev->param.nChunksPerBlock)
470 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
471 n, bi->pagesInUse, bi->softDeletions));
474 /* Check chunk bitmap legal */
475 inUse = yaffs_CountChunkBits(dev, n);
476 if (inUse != bi->pagesInUse)
477 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
478 n, bi->pagesInUse, inUse));
480 /* Check that the sequence number is valid.
481 * Ten million is legal, but is very unlikely
483 if (dev->param.isYaffs2 &&
484 (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
485 (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
486 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
487 n, bi->sequenceNumber));
490 static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
493 yaffs_VerifyBlock(dev, bi, n);
495 /* After collection the block should be in the erased state */
496 /* This will need to change if we do partial gc */
498 if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
499 bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
500 T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
505 static void yaffs_VerifyBlocks(yaffs_Device *dev)
508 int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
509 int nIllegalBlockStates = 0;
511 if (yaffs_SkipVerification(dev))
514 memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
516 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
517 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
518 yaffs_VerifyBlock(dev, bi, i);
520 if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
521 nBlocksPerState[bi->blockState]++;
523 nIllegalBlockStates++;
526 T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
527 T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
529 T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
530 if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
531 T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
533 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
534 T(YAFFS_TRACE_VERIFY,
535 (TSTR("%s %d blocks"TENDSTR),
536 blockStateName[i], nBlocksPerState[i]));
538 if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
539 T(YAFFS_TRACE_VERIFY,
540 (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
541 dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
543 if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
544 T(YAFFS_TRACE_VERIFY,
545 (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
546 dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
548 if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
549 T(YAFFS_TRACE_VERIFY,
550 (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
551 nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
553 T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
558 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
559 * case those tests will not be performed.
561 static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
563 if (obj && yaffs_SkipVerification(obj->myDev))
566 if (!(tags && obj && oh)) {
567 T(YAFFS_TRACE_VERIFY,
568 (TSTR("Verifying object header tags %p obj %p oh %p"TENDSTR),
573 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
574 oh->type > YAFFS_OBJECT_TYPE_MAX)
575 T(YAFFS_TRACE_VERIFY,
576 (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
577 tags->objectId, oh->type));
579 if (tags->objectId != obj->objectId)
580 T(YAFFS_TRACE_VERIFY,
581 (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
582 tags->objectId, obj->objectId));
586 * Check that the object's parent ids match if parentCheck requested.
588 * Tests do not apply to the root object.
591 if (parentCheck && tags->objectId > 1 && !obj->parent)
592 T(YAFFS_TRACE_VERIFY,
593 (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
594 tags->objectId, oh->parentObjectId));
596 if (parentCheck && obj->parent &&
597 oh->parentObjectId != obj->parent->objectId &&
598 (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
599 obj->parent->objectId != YAFFS_OBJECTID_DELETED))
600 T(YAFFS_TRACE_VERIFY,
601 (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
602 tags->objectId, oh->parentObjectId, obj->parent->objectId));
604 if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
605 T(YAFFS_TRACE_VERIFY,
606 (TSTR("Obj %d header name is NULL"TENDSTR),
609 if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
610 T(YAFFS_TRACE_VERIFY,
611 (TSTR("Obj %d header name is 0xFF"TENDSTR),
617 /* Not being used, but don't want to throw away yet */
618 static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
619 __u32 level, int chunkOffset)
622 yaffs_Device *dev = obj->myDev;
628 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
629 if (tn->internal[i]) {
630 ok = yaffs_VerifyTnodeWorker(obj,
633 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
636 } else if (level == 0) {
637 yaffs_ExtendedTags tags;
638 __u32 objectId = obj->objectId;
640 chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
642 for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
643 __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
646 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
647 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
648 if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
649 T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
650 objectId, chunkOffset, theChunk,
651 tags.objectId, tags.chunkId));
665 static void yaffs_VerifyFile(yaffs_Object *obj)
667 int requiredTallness;
673 yaffs_ExtendedTags tags;
680 if (yaffs_SkipVerification(obj->myDev))
684 objectId = obj->objectId;
686 /* Check file size is consistent with tnode depth */
687 lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
688 x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
689 requiredTallness = 0;
691 x >>= YAFFS_TNODES_INTERNAL_BITS;
695 actualTallness = obj->variant.fileVariant.topLevel;
697 /* Check that the chunks in the tnode tree are all correct.
698 * We do this by scanning through the tnode tree and
699 * checking the tags for every chunk match.
702 if (yaffs_SkipNANDVerification(dev))
705 for (i = 1; i <= lastChunk; i++) {
706 tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
709 __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
711 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
712 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
713 if (tags.objectId != objectId || tags.chunkId != i) {
714 T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
715 objectId, i, theChunk,
716 tags.objectId, tags.chunkId));
724 static void yaffs_VerifyHardLink(yaffs_Object *obj)
726 if (obj && yaffs_SkipVerification(obj->myDev))
729 /* Verify sane equivalent object */
732 static void yaffs_VerifySymlink(yaffs_Object *obj)
734 if (obj && yaffs_SkipVerification(obj->myDev))
737 /* Verify symlink string */
740 static void yaffs_VerifySpecial(yaffs_Object *obj)
742 if (obj && yaffs_SkipVerification(obj->myDev))
746 static void yaffs_VerifyObject(yaffs_Object *obj)
755 __u32 chunkShouldNotBeDeleted;
761 if (obj->beingCreated)
766 if (yaffs_SkipVerification(dev))
769 /* Check sane object header chunk */
771 chunkMin = dev->internalStartBlock * dev->param.nChunksPerBlock;
772 chunkMax = (dev->internalEndBlock+1) * dev->param.nChunksPerBlock - 1;
774 chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
775 chunkIdOk = chunkInRange || (obj->hdrChunk == 0);
776 chunkValid = chunkInRange &&
777 yaffs_CheckChunkBit(dev,
778 obj->hdrChunk / dev->param.nChunksPerBlock,
779 obj->hdrChunk % dev->param.nChunksPerBlock);
780 chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
783 (!chunkIdOk || chunkShouldNotBeDeleted)) {
784 T(YAFFS_TRACE_VERIFY,
785 (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
786 obj->objectId, obj->hdrChunk,
787 chunkIdOk ? "" : ",out of range",
788 chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
791 if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
792 yaffs_ExtendedTags tags;
793 yaffs_ObjectHeader *oh;
794 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
796 oh = (yaffs_ObjectHeader *)buffer;
798 yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
801 yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
803 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
806 /* Verify it has a parent */
807 if (obj && !obj->fake &&
808 (!obj->parent || obj->parent->myDev != dev)) {
809 T(YAFFS_TRACE_VERIFY,
810 (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
811 obj->objectId, obj->parent));
814 /* Verify parent is a directory */
815 if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
816 T(YAFFS_TRACE_VERIFY,
817 (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
818 obj->objectId, obj->parent->variantType));
821 switch (obj->variantType) {
822 case YAFFS_OBJECT_TYPE_FILE:
823 yaffs_VerifyFile(obj);
825 case YAFFS_OBJECT_TYPE_SYMLINK:
826 yaffs_VerifySymlink(obj);
828 case YAFFS_OBJECT_TYPE_DIRECTORY:
829 yaffs_VerifyDirectory(obj);
831 case YAFFS_OBJECT_TYPE_HARDLINK:
832 yaffs_VerifyHardLink(obj);
834 case YAFFS_OBJECT_TYPE_SPECIAL:
835 yaffs_VerifySpecial(obj);
837 case YAFFS_OBJECT_TYPE_UNKNOWN:
839 T(YAFFS_TRACE_VERIFY,
840 (TSTR("Obj %d has illegaltype %d"TENDSTR),
841 obj->objectId, obj->variantType));
846 static void yaffs_VerifyObjects(yaffs_Device *dev)
850 struct ylist_head *lh;
852 if (yaffs_SkipVerification(dev))
855 /* Iterate through the objects in each hash entry */
857 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
858 ylist_for_each(lh, &dev->objectBucket[i].list) {
860 obj = ylist_entry(lh, yaffs_Object, hashLink);
861 yaffs_VerifyObject(obj);
869 * Simple hash function. Needs to have a reasonable spread
872 static Y_INLINE int yaffs_HashFunction(int n)
875 return n % YAFFS_NOBJECT_BUCKETS;
879 * Access functions to useful fake objects.
880 * Note that root might have a presence in NAND if permissions are set.
883 yaffs_Object *yaffs_Root(yaffs_Device *dev)
888 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
890 return dev->lostNFoundDir;
895 * Erased NAND checking functions
898 int yaffs_CheckFF(__u8 *buffer, int nBytes)
900 /* Horrible, slow implementation */
909 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
912 int retval = YAFFS_OK;
913 __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
914 yaffs_ExtendedTags tags;
917 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
919 if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
922 if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
923 T(YAFFS_TRACE_NANDACCESS,
924 (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
928 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
935 static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
938 yaffs_ExtendedTags *tags)
940 int retval = YAFFS_OK;
941 yaffs_ExtendedTags tempTags;
942 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
945 result = yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,&tempTags);
946 if(memcmp(buffer,data,dev->nDataBytesPerChunk) ||
947 tempTags.objectId != tags->objectId ||
948 tempTags.chunkId != tags->chunkId ||
949 tempTags.byteCount != tags->byteCount)
952 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
957 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
959 yaffs_ExtendedTags *tags,
966 yaffs_InvalidateCheckpoint(dev);
969 yaffs_BlockInfo *bi = 0;
972 chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
978 /* First check this chunk is erased, if it needs
979 * checking. The checking policy (unless forced
980 * always on) is as follows:
982 * Check the first page we try to write in a block.
983 * If the check passes then we don't need to check any
984 * more. If the check fails, we check again...
985 * If the block has been erased, we don't need to check.
987 * However, if the block has been prioritised for gc,
988 * then we think there might be something odd about
989 * this block and stop using it.
991 * Rationale: We should only ever see chunks that have
992 * not been erased if there was a partially written
993 * chunk due to power loss. This checking policy should
994 * catch that case with very few checks and thus save a
995 * lot of checks that are most likely not needed.
998 * If an erase check fails or the write fails we skip the
1002 /* let's give it a try */
1005 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
1006 bi->skipErasedCheck = 0;
1008 if (!bi->skipErasedCheck) {
1009 erasedOk = yaffs_CheckChunkErased(dev, chunk);
1010 if (erasedOk != YAFFS_OK) {
1011 T(YAFFS_TRACE_ERROR,
1012 (TSTR("**>> yaffs chunk %d was not erased"
1015 /* If not erased, delete this one,
1016 * skip rest of block and
1017 * try another chunk */
1018 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
1019 yaffs_SkipRestOfBlock(dev);
1024 writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
1027 if(!bi->skipErasedCheck)
1028 writeOk = yaffs_VerifyChunkWritten(dev, chunk, data, tags);
1030 if (writeOk != YAFFS_OK) {
1031 /* Clean up aborted write, skip to next block and
1032 * try another chunk */
1033 yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
1037 bi->skipErasedCheck = 1;
1039 /* Copy the data into the robustification buffer */
1040 yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
1042 } while (writeOk != YAFFS_OK &&
1043 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
1049 T(YAFFS_TRACE_ERROR,
1050 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
1053 dev->nRetriedWrites += (attempts - 1);
1061 * Oldest Dirty Sequence Number handling.
1064 /* yaffs_CalcOldestDirtySequence()
1065 * yaffs_FindOldestDirtySequence()
1066 * Calculate the oldest dirty sequence number if we don't know it.
1068 static void yaffs_CalcOldestDirtySequence(yaffs_Device *dev)
1072 unsigned blockNo = 0;
1075 if(!dev->param.isYaffs2)
1078 /* Find the oldest dirty sequence number. */
1079 seq = dev->sequenceNumber + 1;
1081 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
1082 if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
1083 (b->pagesInUse - b->softDeletions) < dev->param.nChunksPerBlock &&
1084 b->sequenceNumber < seq) {
1085 seq = b->sequenceNumber;
1092 dev->oldestDirtySequence = seq;
1093 dev->oldestDirtyBlock = blockNo;
1099 static void yaffs_FindOldestDirtySequence(yaffs_Device *dev)
1101 if(dev->param.isYaffs2 && !dev->oldestDirtySequence)
1102 yaffs_CalcOldestDirtySequence(dev);
1106 * yaffs_ClearOldestDirtySequence()
1107 * Called when a block is erased or marked bad. (ie. when its sequenceNumber
1108 * becomes invalid). If the value matches the oldest then we clear
1109 * dev->oldestDirtySequence to force its recomputation.
1111 static void yaffs_ClearOldestDirtySequence(yaffs_Device *dev, yaffs_BlockInfo *bi)
1114 if(!dev->param.isYaffs2)
1117 if(!bi || bi->sequenceNumber == dev->oldestDirtySequence){
1118 dev->oldestDirtySequence = 0;
1119 dev->oldestDirtyBlock = 0;
1124 * yaffs_UpdateOldestDirtySequence()
1125 * Update the oldest dirty sequence number whenever we dirty a block.
1126 * Only do this if the oldestDirtySequence is actually being tracked.
1128 static void yaffs_UpdateOldestDirtySequence(yaffs_Device *dev, unsigned blockNo, yaffs_BlockInfo *bi)
1130 if(dev->param.isYaffs2 && dev->oldestDirtySequence){
1131 if(dev->oldestDirtySequence > bi->sequenceNumber){
1132 dev->oldestDirtySequence = bi->sequenceNumber;
1133 dev->oldestDirtyBlock = blockNo;
1139 * Block retiring for handling a broken block.
1142 static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
1144 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1146 yaffs_InvalidateCheckpoint(dev);
1148 yaffs_ClearOldestDirtySequence(dev,bi);
1150 if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
1151 if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
1152 T(YAFFS_TRACE_ALWAYS, (TSTR(
1153 "yaffs: Failed to mark bad and erase block %d"
1154 TENDSTR), blockInNAND));
1156 yaffs_ExtendedTags tags;
1157 int chunkId = blockInNAND * dev->param.nChunksPerBlock;
1159 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
1161 memset(buffer, 0xff, dev->nDataBytesPerChunk);
1162 yaffs_InitialiseTags(&tags);
1163 tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
1164 if (dev->param.writeChunkWithTagsToNAND(dev, chunkId -
1165 dev->chunkOffset, buffer, &tags) != YAFFS_OK)
1166 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
1167 TCONT("write bad block marker to block %d")
1168 TENDSTR), blockInNAND));
1170 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
1174 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
1175 bi->gcPrioritise = 0;
1176 bi->needsRetiring = 0;
1178 dev->nRetiredBlocks++;
1182 * Functions for robustisizing TODO
1186 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
1188 const yaffs_ExtendedTags *tags)
1191 chunkInNAND=chunkInNAND;
1196 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
1197 const yaffs_ExtendedTags *tags)
1200 chunkInNAND=chunkInNAND;
1204 void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
1206 if (!bi->gcPrioritise) {
1207 bi->gcPrioritise = 1;
1208 dev->hasPendingPrioritisedGCs = 1;
1209 bi->chunkErrorStrikes++;
1211 if (bi->chunkErrorStrikes > 3) {
1212 bi->needsRetiring = 1; /* Too many stikes, so retire this */
1213 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
1219 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
1222 int blockInNAND = chunkInNAND / dev->param.nChunksPerBlock;
1223 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1225 yaffs_HandleChunkError(dev, bi);
1228 /* Was an actual write failure, so mark the block for retirement */
1229 bi->needsRetiring = 1;
1230 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1231 (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
1234 /* Delete the chunk */
1235 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
1236 yaffs_SkipRestOfBlock(dev);
1240 /*---------------- Name handling functions ------------*/
1242 static __u16 yaffs_CalcNameSum(const YCHAR *name)
1247 const YUCHAR *bname = (const YUCHAR *) name;
1249 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
1251 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1252 sum += yaffs_toupper(*bname) * i;
1254 sum += (*bname) * i;
1263 static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
1265 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
1266 memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
1267 if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
1268 yaffs_strcpy(obj->shortName, name);
1270 obj->shortName[0] = _Y('\0');
1272 obj->sum = yaffs_CalcNameSum(name);
1275 /*-------------------- TNODES -------------------
1277 * List of spare tnodes
1278 * The list is hooked together using the first pointer
1282 /* yaffs_CreateTnodes creates a bunch more tnodes and
1283 * adds them to the tnode free list.
1284 * Don't use this function directly
1286 static Y_INLINE int yaffs_CalcTnodeSize(yaffs_Device *dev)
1289 /* Calculate the tnode size in bytes for variable width tnode support.
1290 * Must be a multiple of 32-bits */
1291 tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
1293 if (tnodeSize < sizeof(yaffs_Tnode))
1294 tnodeSize = sizeof(yaffs_Tnode);
1298 static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes)
1301 int tnodeSize = yaffs_CalcTnodeSize(dev);
1302 yaffs_Tnode *newTnodes;
1306 yaffs_TnodeList *tnl;
1312 /* make these things */
1314 newTnodes = YMALLOC(nTnodes * tnodeSize);
1315 mem = (__u8 *)newTnodes;
1318 T(YAFFS_TRACE_ERROR,
1319 (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
1323 /* Hook them into the free list */
1325 for (i = 0; i < nTnodes - 1; i++) {
1326 newTnodes[i].internal[0] = &newTnodes[i + 1];
1327 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1328 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1332 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
1333 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1334 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1336 dev->freeTnodes = newTnodes;
1338 /* New hookup for wide tnodes */
1339 for (i = 0; i < nTnodes - 1; i++) {
1340 curr = (yaffs_Tnode *) &mem[i * tnodeSize];
1341 next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
1342 curr->internal[0] = next;
1345 curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
1346 curr->internal[0] = dev->freeTnodes;
1347 dev->freeTnodes = (yaffs_Tnode *)mem;
1352 dev->nFreeTnodes += nTnodes;
1353 dev->nTnodesCreated += nTnodes;
1355 /* Now add this bunch of tnodes to a list for freeing up.
1356 * NB If we can't add this to the management list it isn't fatal
1357 * but it just means we can't free this bunch of tnodes later.
1360 tnl = YMALLOC(sizeof(yaffs_TnodeList));
1362 T(YAFFS_TRACE_ERROR,
1364 ("yaffs: Could not add tnodes to management list" TENDSTR)));
1367 tnl->tnodes = newTnodes;
1368 tnl->next = dev->allocatedTnodeList;
1369 dev->allocatedTnodeList = tnl;
1372 T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
1377 /* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
1379 static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev)
1381 yaffs_Tnode *tn = NULL;
1383 #ifdef CONFIG_YAFFS_VALGRIND_TEST
1384 tn = YMALLOC(yaffs_CalcTnodeSize(dev));
1386 dev->nTnodesCreated++;
1388 /* If there are none left make more */
1389 if (!dev->freeTnodes)
1390 yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
1392 if (dev->freeTnodes) {
1393 tn = dev->freeTnodes;
1394 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1395 if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
1396 /* Hoosterman, this thing looks like it isn't in the list */
1397 T(YAFFS_TRACE_ALWAYS,
1398 (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
1401 dev->freeTnodes = dev->freeTnodes->internal[0];
1405 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1410 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
1412 yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
1413 int tnodeSize = yaffs_CalcTnodeSize(dev);
1416 memset(tn, 0, tnodeSize);
1421 /* FreeTnode frees up a tnode and puts it back on the free list */
1422 static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
1425 #ifdef CONFIG_YAFFS_VALGRIND_TEST
1427 dev->nTnodesCreated--;
1429 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1430 if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
1431 /* Hoosterman, this thing looks like it is already in the list */
1432 T(YAFFS_TRACE_ALWAYS,
1433 (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
1435 tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1437 tn->internal[0] = dev->freeTnodes;
1438 dev->freeTnodes = tn;
1442 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1445 static void yaffs_DeinitialiseTnodes(yaffs_Device *dev)
1447 /* Free the list of allocated tnodes */
1448 yaffs_TnodeList *tmp;
1450 while (dev->allocatedTnodeList) {
1451 tmp = dev->allocatedTnodeList->next;
1453 YFREE(dev->allocatedTnodeList->tnodes);
1454 YFREE(dev->allocatedTnodeList);
1455 dev->allocatedTnodeList = tmp;
1459 dev->freeTnodes = NULL;
1460 dev->nFreeTnodes = 0;
1461 dev->nTnodesCreated = 0;
1464 static void yaffs_InitialiseTnodes(yaffs_Device *dev)
1466 dev->allocatedTnodeList = NULL;
1467 dev->freeTnodes = NULL;
1468 dev->nFreeTnodes = 0;
1469 dev->nTnodesCreated = 0;
1473 void yaffs_LoadLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
1476 __u32 *map = (__u32 *)tn;
1482 pos &= YAFFS_TNODES_LEVEL0_MASK;
1483 val >>= dev->chunkGroupBits;
1485 bitInMap = pos * dev->tnodeWidth;
1486 wordInMap = bitInMap / 32;
1487 bitInWord = bitInMap & (32 - 1);
1489 mask = dev->tnodeMask << bitInWord;
1491 map[wordInMap] &= ~mask;
1492 map[wordInMap] |= (mask & (val << bitInWord));
1494 if (dev->tnodeWidth > (32 - bitInWord)) {
1495 bitInWord = (32 - bitInWord);
1497 mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
1498 map[wordInMap] &= ~mask;
1499 map[wordInMap] |= (mask & (val >> bitInWord));
1503 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
1506 __u32 *map = (__u32 *)tn;
1512 pos &= YAFFS_TNODES_LEVEL0_MASK;
1514 bitInMap = pos * dev->tnodeWidth;
1515 wordInMap = bitInMap / 32;
1516 bitInWord = bitInMap & (32 - 1);
1518 val = map[wordInMap] >> bitInWord;
1520 if (dev->tnodeWidth > (32 - bitInWord)) {
1521 bitInWord = (32 - bitInWord);
1523 val |= (map[wordInMap] << bitInWord);
1526 val &= dev->tnodeMask;
1527 val <<= dev->chunkGroupBits;
1532 /* ------------------- End of individual tnode manipulation -----------------*/
1534 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
1535 * The look up tree is represented by the top tnode and the number of topLevel
1536 * in the tree. 0 means only the level 0 tnode is in the tree.
1539 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
1540 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
1541 yaffs_FileStructure *fStruct,
1544 yaffs_Tnode *tn = fStruct->top;
1546 int requiredTallness;
1547 int level = fStruct->topLevel;
1551 /* Check sane level and chunk Id */
1552 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
1555 if (chunkId > YAFFS_MAX_CHUNK_ID)
1558 /* First check we're tall enough (ie enough topLevel) */
1560 i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1561 requiredTallness = 0;
1563 i >>= YAFFS_TNODES_INTERNAL_BITS;
1567 if (requiredTallness > fStruct->topLevel)
1568 return NULL; /* Not tall enough, so we can't find it */
1570 /* Traverse down to level 0 */
1571 while (level > 0 && tn) {
1572 tn = tn->internal[(chunkId >>
1573 (YAFFS_TNODES_LEVEL0_BITS +
1575 YAFFS_TNODES_INTERNAL_BITS)) &
1576 YAFFS_TNODES_INTERNAL_MASK];
1583 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
1584 * This happens in two steps:
1585 * 1. If the tree isn't tall enough, then make it taller.
1586 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
1588 * Used when modifying the tree.
1590 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
1591 * be plugged into the ttree.
1594 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
1595 yaffs_FileStructure *fStruct,
1597 yaffs_Tnode *passedTn)
1599 int requiredTallness;
1607 /* Check sane level and page Id */
1608 if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
1611 if (chunkId > YAFFS_MAX_CHUNK_ID)
1614 /* First check we're tall enough (ie enough topLevel) */
1616 x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1617 requiredTallness = 0;
1619 x >>= YAFFS_TNODES_INTERNAL_BITS;
1624 if (requiredTallness > fStruct->topLevel) {
1625 /* Not tall enough, gotta make the tree taller */
1626 for (i = fStruct->topLevel; i < requiredTallness; i++) {
1628 tn = yaffs_GetTnode(dev);
1631 tn->internal[0] = fStruct->top;
1633 fStruct->topLevel++;
1635 T(YAFFS_TRACE_ERROR,
1636 (TSTR("yaffs: no more tnodes" TENDSTR)));
1642 /* Traverse down to level 0, adding anything we need */
1644 l = fStruct->topLevel;
1648 while (l > 0 && tn) {
1650 (YAFFS_TNODES_LEVEL0_BITS +
1651 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
1652 YAFFS_TNODES_INTERNAL_MASK;
1655 if ((l > 1) && !tn->internal[x]) {
1656 /* Add missing non-level-zero tnode */
1657 tn->internal[x] = yaffs_GetTnode(dev);
1658 if(!tn->internal[x])
1660 } else if (l == 1) {
1661 /* Looking from level 1 at level 0 */
1663 /* If we already have one, then release it.*/
1664 if (tn->internal[x])
1665 yaffs_FreeTnode(dev, tn->internal[x]);
1666 tn->internal[x] = passedTn;
1668 } else if (!tn->internal[x]) {
1669 /* Don't have one, none passed in */
1670 tn->internal[x] = yaffs_GetTnode(dev);
1671 if(!tn->internal[x])
1676 tn = tn->internal[x];
1680 /* top is level 0 */
1682 memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1683 yaffs_FreeTnode(dev, passedTn);
1690 static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
1691 yaffs_ExtendedTags *tags, int objectId,
1696 for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
1697 if (yaffs_CheckChunkBit(dev, theChunk / dev->param.nChunksPerBlock,
1698 theChunk % dev->param.nChunksPerBlock)) {
1700 if(dev->chunkGroupSize == 1)
1703 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
1705 if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
1717 /* Experimental code not being used yet. Might speed up file deletion */
1718 /* DeleteWorker scans backwards through the tnode tree and deletes all the
1719 * chunks and tnodes in the file.
1720 * Returns 1 if the tree was deleted.
1721 * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1724 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
1725 int chunkOffset, int *limit)
1730 yaffs_ExtendedTags tags;
1732 yaffs_Device *dev = in->myDev;
1738 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1740 if (tn->internal[i]) {
1741 if (limit && (*limit) < 0) {
1745 yaffs_DeleteWorker(in,
1753 YAFFS_TNODES_INTERNAL_BITS)
1758 yaffs_FreeTnode(dev,
1761 tn->internal[i] = NULL;
1765 return (allDone) ? 1 : 0;
1766 } else if (level == 0) {
1769 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
1771 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
1774 chunkInInode = (chunkOffset <<
1775 YAFFS_TNODES_LEVEL0_BITS) + i;
1778 yaffs_FindChunkInGroup(dev,
1784 if (foundChunk > 0) {
1785 yaffs_DeleteChunk(dev,
1790 *limit = *limit - 1;
1797 yaffs_LoadLevel0Tnode(dev, tn, i, 0);
1801 return (i < 0) ? 1 : 0;
1813 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1815 yaffs_BlockInfo *theBlock;
1818 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1820 blockNo = chunk / dev->param.nChunksPerBlock;
1821 theBlock = yaffs_GetBlockInfo(dev, blockNo);
1823 theBlock->softDeletions++;
1825 yaffs_UpdateOldestDirtySequence(dev, blockNo, theBlock);
1829 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1830 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1832 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1835 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
1836 __u32 level, int chunkOffset)
1841 yaffs_Device *dev = in->myDev;
1846 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1848 if (tn->internal[i]) {
1850 yaffs_SoftDeleteWorker(in,
1856 YAFFS_TNODES_INTERNAL_BITS)
1859 yaffs_FreeTnode(dev,
1862 tn->internal[i] = NULL;
1864 /* Hoosterman... how could this happen? */
1868 return (allDone) ? 1 : 0;
1869 } else if (level == 0) {
1871 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1872 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
1874 /* Note this does not find the real chunk, only the chunk group.
1875 * We make an assumption that a chunk group is not larger than
1878 yaffs_SoftDeleteChunk(dev, theChunk);
1879 yaffs_LoadLevel0Tnode(dev, tn, i, 0);
1893 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1896 obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
1897 if (obj->nDataChunks <= 0) {
1898 /* Empty file with no duplicate object headers, just delete it immediately */
1899 yaffs_FreeTnode(obj->myDev,
1900 obj->variant.fileVariant.top);
1901 obj->variant.fileVariant.top = NULL;
1902 T(YAFFS_TRACE_TRACING,
1903 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1905 yaffs_DoGenericObjectDeletion(obj);
1907 yaffs_SoftDeleteWorker(obj,
1908 obj->variant.fileVariant.top,
1909 obj->variant.fileVariant.
1911 obj->softDeleted = 1;
1916 /* Pruning removes any part of the file structure tree that is beyond the
1917 * bounds of the file (ie that does not point to chunks).
1919 * A file should only get pruned when its size is reduced.
1921 * Before pruning, the chunks must be pulled from the tree and the
1922 * level 0 tnode entries must be zeroed out.
1923 * Could also use this for file deletion, but that's probably better handled
1924 * by a special case.
1926 * This function is recursive. For levels > 0 the function is called again on
1927 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1928 * If there is no data in a subtree then it is pruned.
1931 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
1932 __u32 level, int del0)
1941 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1942 if (tn->internal[i]) {
1944 yaffs_PruneWorker(dev, tn->internal[i],
1946 (i == 0) ? del0 : 1);
1949 if (tn->internal[i])
1953 int tnodeSize_u32 = yaffs_CalcTnodeSize(dev)/sizeof(__u32);
1954 __u32 *map = (__u32 *)tn;
1956 for(i = 0; !hasData && i < tnodeSize_u32; i++){
1962 if (hasData == 0 && del0) {
1963 /* Free and return NULL */
1965 yaffs_FreeTnode(dev, tn);
1975 static int yaffs_PruneFileStructure(yaffs_Device *dev,
1976 yaffs_FileStructure *fStruct)
1983 if (fStruct->topLevel > 0) {
1985 yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
1987 /* Now we have a tree with all the non-zero branches NULL but the height
1988 * is the same as it was.
1989 * Let's see if we can trim internal tnodes to shorten the tree.
1990 * We can do this if only the 0th element in the tnode is in use
1991 * (ie all the non-zero are NULL)
1994 while (fStruct->topLevel && !done) {
1998 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1999 if (tn->internal[i])
2004 fStruct->top = tn->internal[0];
2005 fStruct->topLevel--;
2006 yaffs_FreeTnode(dev, tn);
2016 /*-------------------- End of File Structure functions.-------------------*/
2018 /* yaffs_CreateFreeObjects creates a bunch more objects and
2019 * adds them to the object free list.
2021 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
2024 yaffs_Object *newObjects;
2025 yaffs_ObjectList *list;
2030 /* make these things */
2031 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
2032 list = YMALLOC(sizeof(yaffs_ObjectList));
2034 if (!newObjects || !list) {
2043 T(YAFFS_TRACE_ALLOCATE,
2044 (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
2048 /* Hook them into the free list */
2049 for (i = 0; i < nObjects - 1; i++) {
2050 newObjects[i].siblings.next =
2051 (struct ylist_head *)(&newObjects[i + 1]);
2054 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
2055 dev->freeObjects = newObjects;
2056 dev->nFreeObjects += nObjects;
2057 dev->nObjectsCreated += nObjects;
2059 /* Now add this bunch of Objects to a list for freeing up. */
2061 list->objects = newObjects;
2062 list->next = dev->allocatedObjectList;
2063 dev->allocatedObjectList = list;
2069 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
2070 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
2072 yaffs_Object *tn = NULL;
2074 #ifdef CONFIG_YAFFS_VALGRIND_TEST
2075 tn = YMALLOC(sizeof(yaffs_Object));
2077 dev->nObjectsCreated++;
2079 /* If there are none left make more */
2080 if (!dev->freeObjects)
2081 yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
2083 if (dev->freeObjects) {
2084 tn = dev->freeObjects;
2086 (yaffs_Object *) (dev->freeObjects->siblings.next);
2087 dev->nFreeObjects--;
2091 /* Now sweeten it up... */
2093 memset(tn, 0, sizeof(yaffs_Object));
2094 tn->beingCreated = 1;
2098 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
2099 YINIT_LIST_HEAD(&(tn->hardLinks));
2100 YINIT_LIST_HEAD(&(tn->hashLink));
2101 YINIT_LIST_HEAD(&tn->siblings);
2104 /* Now make the directory sane */
2106 tn->parent = dev->rootDir;
2107 ylist_add(&(tn->siblings), &dev->rootDir->variant.directoryVariant.children);
2110 /* Add it to the lost and found directory.
2111 * NB Can't put root or lostNFound in lostNFound so
2112 * check if lostNFound exists first
2114 if (dev->lostNFoundDir)
2115 yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
2117 tn->beingCreated = 0;
2120 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
2125 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
2130 yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2132 obj->fake = 1; /* it is fake so it might have no NAND presence... */
2133 obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
2134 obj->unlinkAllowed = 0; /* ... or unlink it */
2137 obj->yst_mode = mode;
2139 obj->hdrChunk = 0; /* Not a valid chunk. */
2146 static void yaffs_UnhashObject(yaffs_Object *tn)
2149 yaffs_Device *dev = tn->myDev;
2151 /* If it is still linked into the bucket list, free from the list */
2152 if (!ylist_empty(&tn->hashLink)) {
2153 ylist_del_init(&tn->hashLink);
2154 bucket = yaffs_HashFunction(tn->objectId);
2155 dev->objectBucket[bucket].count--;
2159 /* FreeObject frees up a Object and puts it back on the free list */
2160 static void yaffs_FreeObject(yaffs_Object *tn)
2162 yaffs_Device *dev = tn->myDev;
2164 T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode));
2170 if (!ylist_empty(&tn->siblings))
2175 /* We're still hooked up to a cached inode.
2176 * Don't delete now, but mark for later deletion
2178 tn->deferedFree = 1;
2182 yaffs_UnhashObject(tn);
2184 #ifdef CONFIG_YAFFS_VALGRIND_TEST
2186 dev->nObjectsCreated--;
2189 /* Link into the free list. */
2190 tn->siblings.next = (struct ylist_head *)(dev->freeObjects);
2191 dev->freeObjects = tn;
2192 dev->nFreeObjects++;
2194 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
2198 void yaffs_HandleDeferedFree(yaffs_Object *obj)
2200 if (obj->deferedFree)
2201 yaffs_FreeObject(obj);
2205 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
2207 /* Free the list of allocated Objects */
2209 yaffs_ObjectList *tmp;
2211 while (dev->allocatedObjectList) {
2212 tmp = dev->allocatedObjectList->next;
2213 YFREE(dev->allocatedObjectList->objects);
2214 YFREE(dev->allocatedObjectList);
2216 dev->allocatedObjectList = tmp;
2219 dev->freeObjects = NULL;
2220 dev->nFreeObjects = 0;
2221 dev->nObjectsCreated = 0;
2224 static void yaffs_InitialiseObjects(yaffs_Device *dev)
2228 dev->allocatedObjectList = NULL;
2229 dev->freeObjects = NULL;
2230 dev->nFreeObjects = 0;
2232 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2233 YINIT_LIST_HEAD(&dev->objectBucket[i].list);
2234 dev->objectBucket[i].count = 0;
2238 static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
2242 int lowest = 999999;
2245 /* Search for the shortest list or one that
2249 for (i = 0; i < 10 && lowest > 4; i++) {
2250 dev->bucketFinder++;
2251 dev->bucketFinder %= YAFFS_NOBJECT_BUCKETS;
2252 if (dev->objectBucket[dev->bucketFinder].count < lowest) {
2253 lowest = dev->objectBucket[dev->bucketFinder].count;
2254 l = dev->bucketFinder;
2262 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
2264 int bucket = yaffs_FindNiceObjectBucket(dev);
2266 /* Now find an object value that has not already been taken
2267 * by scanning the list.
2271 struct ylist_head *i;
2273 __u32 n = (__u32) bucket;
2275 /* yaffs_CheckObjectHashSanity(); */
2279 n += YAFFS_NOBJECT_BUCKETS;
2280 if (1 || dev->objectBucket[bucket].count > 0) {
2281 ylist_for_each(i, &dev->objectBucket[bucket].list) {
2282 /* If there is already one in the list */
2283 if (i && ylist_entry(i, yaffs_Object,
2284 hashLink)->objectId == n) {
2294 static void yaffs_HashObject(yaffs_Object *in)
2296 int bucket = yaffs_HashFunction(in->objectId);
2297 yaffs_Device *dev = in->myDev;
2299 ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
2300 dev->objectBucket[bucket].count++;
2303 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
2305 int bucket = yaffs_HashFunction(number);
2306 struct ylist_head *i;
2309 ylist_for_each(i, &dev->objectBucket[bucket].list) {
2310 /* Look if it is in the list */
2312 in = ylist_entry(i, yaffs_Object, hashLink);
2313 if (in->objectId == number) {
2315 /* Don't tell the VFS about this one if it is defered free */
2316 if (in->deferedFree)
2327 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
2328 yaffs_ObjectType type)
2330 yaffs_Object *theObject=NULL;
2331 yaffs_Tnode *tn = NULL;
2334 number = yaffs_CreateNewObjectNumber(dev);
2336 if (type == YAFFS_OBJECT_TYPE_FILE) {
2337 tn = yaffs_GetTnode(dev);
2342 theObject = yaffs_AllocateEmptyObject(dev);
2345 yaffs_FreeTnode(dev,tn);
2351 theObject->fake = 0;
2352 theObject->renameAllowed = 1;
2353 theObject->unlinkAllowed = 1;
2354 theObject->objectId = number;
2355 yaffs_HashObject(theObject);
2356 theObject->variantType = type;
2357 #ifdef CONFIG_YAFFS_WINCE
2358 yfsd_WinFileTimeNow(theObject->win_atime);
2359 theObject->win_ctime[0] = theObject->win_mtime[0] =
2360 theObject->win_atime[0];
2361 theObject->win_ctime[1] = theObject->win_mtime[1] =
2362 theObject->win_atime[1];
2366 theObject->yst_atime = theObject->yst_mtime =
2367 theObject->yst_ctime = Y_CURRENT_TIME;
2370 case YAFFS_OBJECT_TYPE_FILE:
2371 theObject->variant.fileVariant.fileSize = 0;
2372 theObject->variant.fileVariant.scannedFileSize = 0;
2373 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
2374 theObject->variant.fileVariant.topLevel = 0;
2375 theObject->variant.fileVariant.top = tn;
2377 case YAFFS_OBJECT_TYPE_DIRECTORY:
2378 YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
2380 YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
2383 case YAFFS_OBJECT_TYPE_SYMLINK:
2384 case YAFFS_OBJECT_TYPE_HARDLINK:
2385 case YAFFS_OBJECT_TYPE_SPECIAL:
2386 /* No action required */
2388 case YAFFS_OBJECT_TYPE_UNKNOWN:
2389 /* todo this should not happen */
2397 static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
2399 yaffs_ObjectType type)
2401 yaffs_Object *theObject = NULL;
2404 theObject = yaffs_FindObjectByNumber(dev, number);
2407 theObject = yaffs_CreateNewObject(dev, number, type);
2414 static YCHAR *yaffs_CloneString(const YCHAR *str)
2416 YCHAR *newStr = NULL;
2422 len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
2423 newStr = YMALLOC((len + 1) * sizeof(YCHAR));
2425 yaffs_strncpy(newStr, str,len);
2433 * Mknod (create) a new object.
2434 * equivalentObject only has meaning for a hard link;
2435 * aliasString only has meaning for a symlink.
2436 * rdev only has meaning for devices (a subset of special objects)
2439 static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
2440 yaffs_Object *parent,
2445 yaffs_Object *equivalentObject,
2446 const YCHAR *aliasString, __u32 rdev)
2451 yaffs_Device *dev = parent->myDev;
2453 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
2454 if (yaffs_FindObjectByName(parent, name))
2457 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2458 str = yaffs_CloneString(aliasString);
2463 in = yaffs_CreateNewObject(dev, -1, type);
2478 in->variantType = type;
2480 in->yst_mode = mode;
2482 #ifdef CONFIG_YAFFS_WINCE
2483 yfsd_WinFileTimeNow(in->win_atime);
2484 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
2485 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
2488 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
2490 in->yst_rdev = rdev;
2494 in->nDataChunks = 0;
2496 yaffs_SetObjectName(in, name);
2499 yaffs_AddObjectToDirectory(parent, in);
2501 in->myDev = parent->myDev;
2504 case YAFFS_OBJECT_TYPE_SYMLINK:
2505 in->variant.symLinkVariant.alias = str;
2507 case YAFFS_OBJECT_TYPE_HARDLINK:
2508 in->variant.hardLinkVariant.equivalentObject =
2510 in->variant.hardLinkVariant.equivalentObjectId =
2511 equivalentObject->objectId;
2512 ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
2514 case YAFFS_OBJECT_TYPE_FILE:
2515 case YAFFS_OBJECT_TYPE_DIRECTORY:
2516 case YAFFS_OBJECT_TYPE_SPECIAL:
2517 case YAFFS_OBJECT_TYPE_UNKNOWN:
2522 if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
2523 /* Could not create the object header, fail the creation */
2524 yaffs_DeleteObject(in);
2528 yaffs_UpdateParent(parent);
2534 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
2535 __u32 mode, __u32 uid, __u32 gid)
2537 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2538 uid, gid, NULL, NULL, 0);
2541 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
2542 __u32 mode, __u32 uid, __u32 gid)
2544 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2545 mode, uid, gid, NULL, NULL, 0);
2548 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
2549 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
2551 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2552 uid, gid, NULL, NULL, rdev);
2555 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
2556 __u32 mode, __u32 uid, __u32 gid,
2559 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2560 uid, gid, NULL, alias, 0);
2563 /* yaffs_Link returns the object id of the equivalent object.*/
2564 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
2565 yaffs_Object *equivalentObject)
2567 /* Get the real object in case we were fed a hard link as an equivalent object */
2568 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
2570 if (yaffs_MknodObject
2571 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2572 equivalentObject, NULL, 0)) {
2573 return equivalentObject;
2580 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
2581 const YCHAR *newName, int force, int shadows)
2586 yaffs_Object *existingTarget;
2589 newDir = obj->parent; /* use the old directory */
2591 if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
2592 T(YAFFS_TRACE_ALWAYS,
2594 ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
2599 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
2600 if (obj->myDev->param.isYaffs2)
2601 unlinkOp = (newDir == obj->myDev->unlinkedDir);
2603 unlinkOp = (newDir == obj->myDev->unlinkedDir
2604 && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
2606 deleteOp = (newDir == obj->myDev->deletedDir);
2608 existingTarget = yaffs_FindObjectByName(newDir, newName);
2610 /* If the object is a file going into the unlinked directory,
2611 * then it is OK to just stuff it in since duplicate names are allowed.
2612 * else only proceed if the new name does not exist and if we're putting
2613 * it into a directory.
2620 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
2621 yaffs_SetObjectName(obj, newName);
2624 yaffs_AddObjectToDirectory(newDir, obj);
2629 /* If it is a deletion then we mark it as a shrink for gc purposes. */
2630 if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows) >= 0)
2637 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
2638 yaffs_Object *newDir, const YCHAR *newName)
2640 yaffs_Object *obj = NULL;
2641 yaffs_Object *existingTarget = NULL;
2647 if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2649 if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2652 dev = oldDir->myDev;
2654 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2655 /* Special case for case insemsitive systems (eg. WinCE).
2656 * While look-up is case insensitive, the name isn't.
2657 * Therefore we might want to change x.txt to X.txt
2659 if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
2663 if(yaffs_strnlen(newName,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
2667 obj = yaffs_FindObjectByName(oldDir, oldName);
2669 if (obj && obj->renameAllowed) {
2671 /* Now do the handling for an existing target, if there is one */
2673 existingTarget = yaffs_FindObjectByName(newDir, newName);
2674 if (existingTarget &&
2675 existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2676 !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
2677 /* There is a target that is a non-empty directory, so we fail */
2678 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
2679 } else if (existingTarget && existingTarget != obj) {
2680 /* Nuke the target first, using shadowing,
2681 * but only if it isn't the same object.
2683 * Note we must disable gc otherwise it can mess up the shadowing.
2687 yaffs_ChangeObjectName(obj, newDir, newName, force,
2688 existingTarget->objectId);
2689 existingTarget->isShadowed = 1;
2690 yaffs_UnlinkObject(existingTarget);
2694 result = yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
2696 yaffs_UpdateParent(oldDir);
2697 if(newDir != oldDir)
2698 yaffs_UpdateParent(newDir);
2705 /*------------------------- Block Management and Page Allocation ----------------*/
2707 static int yaffs_InitialiseBlocks(yaffs_Device *dev)
2709 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
2711 dev->blockInfo = NULL;
2712 dev->chunkBits = NULL;
2714 dev->allocationBlock = -1; /* force it to get a new one */
2716 /* If the first allocation strategy fails, thry the alternate one */
2717 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2718 if (!dev->blockInfo) {
2719 dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
2720 dev->blockInfoAlt = 1;
2722 dev->blockInfoAlt = 0;
2724 if (dev->blockInfo) {
2725 /* Set up dynamic blockinfo stuff. */
2726 dev->chunkBitmapStride = (dev->param.nChunksPerBlock + 7) / 8; /* round up bytes */
2727 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2728 if (!dev->chunkBits) {
2729 dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
2730 dev->chunkBitsAlt = 1;
2732 dev->chunkBitsAlt = 0;
2735 if (dev->blockInfo && dev->chunkBits) {
2736 memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
2737 memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
2744 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2746 if (dev->blockInfoAlt && dev->blockInfo)
2747 YFREE_ALT(dev->blockInfo);
2748 else if (dev->blockInfo)
2749 YFREE(dev->blockInfo);
2751 dev->blockInfoAlt = 0;
2753 dev->blockInfo = NULL;
2755 if (dev->chunkBitsAlt && dev->chunkBits)
2756 YFREE_ALT(dev->chunkBits);
2757 else if (dev->chunkBits)
2758 YFREE(dev->chunkBits);
2759 dev->chunkBitsAlt = 0;
2760 dev->chunkBits = NULL;
2763 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
2764 yaffs_BlockInfo *bi)
2767 if (!dev->param.isYaffs2)
2768 return 1; /* disqualification only applies to yaffs2. */
2770 if (!bi->hasShrinkHeader)
2771 return 1; /* can gc */
2773 yaffs_FindOldestDirtySequence(dev);
2775 /* Can't do gc of this block if there are any blocks older than this one that have
2778 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2782 * yaffs_FindRefreshBlock()
2783 * periodically finds the oldest full block by sequence number for refreshing.
2786 static __u32 yaffs_FindRefreshBlock(yaffs_Device *dev)
2791 __u32 oldestSequence = 0;
2793 yaffs_BlockInfo *bi;
2796 * If refresh period < 10 then refreshing is disabled.
2798 if(dev->param.refreshPeriod < 10 ||
2799 !dev->param.isYaffs2)
2803 * Fix broken values.
2805 if(dev->refreshSkip > dev->param.refreshPeriod)
2806 dev->refreshSkip = dev->param.refreshPeriod;
2808 if(dev->refreshSkip > 0)
2812 * Refresh skip is now zero.
2813 * We'll do a refresh this time around....
2814 * Update the refresh skip and find the oldest block.
2816 dev->refreshSkip = dev->param.refreshPeriod;
2817 dev->refreshCount++;
2818 bi = dev->blockInfo;
2819 for (b = dev->internalStartBlock; b <=dev->internalEndBlock; b++){
2821 if (bi->blockState == YAFFS_BLOCK_STATE_FULL){
2824 bi->sequenceNumber < oldestSequence){
2826 oldestSequence = bi->sequenceNumber;
2834 (TSTR("GC refresh count %d selected block %d with sequenceNumber %d" TENDSTR),
2835 dev->refreshCount, oldest, oldestSequence));
2842 static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
2844 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
2848 /* If the block is still healthy erase it and mark as clean.
2849 * If the block has had a data failure, then retire it.
2852 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2853 (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
2854 blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
2856 yaffs_ClearOldestDirtySequence(dev,bi);
2858 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2860 /* If this is the block being garbage collected then stop gc'ing this block */
2861 if(blockNo == dev->gcBlock)
2864 /* If this block is currently the best candidate for gc then drop as a candidate */
2865 if(blockNo == dev->gcDirtiest){
2866 dev->gcDirtiest = 0;
2867 dev->gcPagesInUse = 0;
2870 if (!bi->needsRetiring) {
2871 yaffs_InvalidateCheckpoint(dev);
2872 erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
2874 dev->nErasureFailures++;
2875 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2876 (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
2881 ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
2883 for (i = 0; i < dev->param.nChunksPerBlock; i++) {
2884 if (!yaffs_CheckChunkErased
2885 (dev, blockNo * dev->param.nChunksPerBlock + i)) {
2886 T(YAFFS_TRACE_ERROR,
2888 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2889 TENDSTR), blockNo, i));
2895 /* Clean it up... */
2896 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2897 bi->sequenceNumber = 0;
2898 dev->nErasedBlocks++;
2900 bi->softDeletions = 0;
2901 bi->hasShrinkHeader = 0;
2902 bi->skipErasedCheck = 1; /* This is clean, so no need to check */
2903 bi->gcPrioritise = 0;
2904 yaffs_ClearChunkBits(dev, blockNo);
2906 T(YAFFS_TRACE_ERASE,
2907 (TSTR("Erased block %d" TENDSTR), blockNo));
2909 dev->nFreeChunks -= dev->param.nChunksPerBlock; /* We lost a block of free space */
2911 yaffs_RetireBlock(dev, blockNo);
2912 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2913 (TSTR("**>> Block %d retired" TENDSTR), blockNo));
2917 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2921 yaffs_BlockInfo *bi;
2923 if (dev->nErasedBlocks < 1) {
2924 /* Hoosterman we've got a problem.
2925 * Can't get space to gc
2927 T(YAFFS_TRACE_ERROR,
2928 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
2933 /* Find an empty block. */
2935 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
2936 dev->allocationBlockFinder++;
2937 if (dev->allocationBlockFinder < dev->internalStartBlock
2938 || dev->allocationBlockFinder > dev->internalEndBlock) {
2939 dev->allocationBlockFinder = dev->internalStartBlock;
2942 bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
2944 if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
2945 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2946 dev->sequenceNumber++;
2947 bi->sequenceNumber = dev->sequenceNumber;
2948 dev->nErasedBlocks--;
2949 T(YAFFS_TRACE_ALLOCATE,
2950 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
2951 dev->allocationBlockFinder, dev->sequenceNumber,
2952 dev->nErasedBlocks));
2953 return dev->allocationBlockFinder;
2957 T(YAFFS_TRACE_ALWAYS,
2959 ("yaffs tragedy: no more erased blocks, but there should have been %d"
2960 TENDSTR), dev->nErasedBlocks));
2967 static int yaffs_CheckpointRequired(yaffs_Device *dev)
2969 int nblocks = dev->internalEndBlock - dev->internalStartBlock + 1 ;
2970 return dev->param.isYaffs2 &&
2971 !dev->param.skipCheckpointWrite &&
2972 (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
2974 static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
2976 if (!dev->nCheckpointBlocksRequired &&
2977 yaffs_CheckpointRequired(dev)){
2978 /* Not a valid value so recalculate */
2981 int devBlocks = (dev->param.endBlock - dev->param.startBlock + 1);
2982 int tnodeSize = yaffs_CalcTnodeSize(dev);
2984 nBytes += sizeof(yaffs_CheckpointValidity);
2985 nBytes += sizeof(yaffs_CheckpointDevice);
2986 nBytes += devBlocks * sizeof(yaffs_BlockInfo);
2987 nBytes += devBlocks * dev->chunkBitmapStride;
2988 nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjectsCreated - dev->nFreeObjects);
2989 nBytes += (tnodeSize + sizeof(__u32)) * (dev->nTnodesCreated - dev->nFreeTnodes);
2990 nBytes += sizeof(yaffs_CheckpointValidity);
2991 nBytes += sizeof(__u32); /* checksum*/
2993 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
2995 nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->param.nChunksPerBlock)) + 3;
2997 dev->nCheckpointBlocksRequired = nBlocks;
3000 return dev->nCheckpointBlocksRequired;
3004 * Check if there's space to allocate...
3005 * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
3007 static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev, int nChunks)
3010 int reservedBlocks = dev->param.nReservedBlocks;
3011 int checkpointBlocks;
3013 if (dev->param.isYaffs2) {
3014 checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
3015 dev->blocksInCheckpoint;
3016 if (checkpointBlocks < 0)
3017 checkpointBlocks = 0;
3019 checkpointBlocks = 0;
3022 reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->param.nChunksPerBlock);
3024 return (dev->nFreeChunks > (reservedChunks + nChunks));
3027 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
3028 yaffs_BlockInfo **blockUsedPtr)
3031 yaffs_BlockInfo *bi;
3033 if (dev->allocationBlock < 0) {
3034 /* Get next block to allocate off */
3035 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
3036 dev->allocationPage = 0;
3039 if (!useReserve && !yaffs_CheckSpaceForAllocation(dev, 1)) {
3040 /* Not enough space to allocate unless we're allowed to use the reserve. */
3044 if (dev->nErasedBlocks < dev->param.nReservedBlocks
3045 && dev->allocationPage == 0) {
3046 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
3049 /* Next page please.... */
3050 if (dev->allocationBlock >= 0) {
3051 bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
3053 retVal = (dev->allocationBlock * dev->param.nChunksPerBlock) +
3054 dev->allocationPage;
3056 yaffs_SetChunkBit(dev, dev->allocationBlock,
3057 dev->allocationPage);
3059 dev->allocationPage++;
3063 /* If the block is full set the state to full */
3064 if (dev->allocationPage >= dev->param.nChunksPerBlock) {
3065 bi->blockState = YAFFS_BLOCK_STATE_FULL;
3066 dev->allocationBlock = -1;
3075 T(YAFFS_TRACE_ERROR,
3076 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
3081 static int yaffs_GetErasedChunks(yaffs_Device *dev)
3085 n = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3087 if (dev->allocationBlock > 0)
3088 n += (dev->param.nChunksPerBlock - dev->allocationPage);
3095 * yaffs_SkipRestOfBlock() skips over the rest of the allocation block
3096 * if we don't want to write to it.
3098 static void yaffs_SkipRestOfBlock(yaffs_Device *dev)
3100 if(dev->allocationBlock > 0){
3101 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
3102 if(bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING){
3103 bi->blockState = YAFFS_BLOCK_STATE_FULL;
3104 dev->allocationBlock = -1;
3110 static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
3116 int retVal = YAFFS_OK;
3119 int isCheckpointBlock;
3123 int chunksBefore = yaffs_GetErasedChunks(dev);
3126 yaffs_ExtendedTags tags;
3128 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
3130 yaffs_Object *object;
3132 isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
3135 T(YAFFS_TRACE_TRACING,
3136 (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
3139 bi->hasShrinkHeader,
3142 /*yaffs_VerifyFreeChunks(dev); */
3144 if(bi->blockState == YAFFS_BLOCK_STATE_FULL)
3145 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
3147 bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
3151 if (isCheckpointBlock ||
3152 !yaffs_StillSomeChunkBits(dev, block)) {
3153 T(YAFFS_TRACE_TRACING,
3155 ("Collecting block %d that has no chunks in use" TENDSTR),
3157 yaffs_BlockBecameDirty(dev, block);
3160 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
3162 yaffs_VerifyBlock(dev, bi, block);
3164 maxCopies = (wholeBlock) ? dev->param.nChunksPerBlock : 5;
3165 oldChunk = block * dev->param.nChunksPerBlock + dev->gcChunk;
3167 for (/* init already done */;
3168 retVal == YAFFS_OK &&
3169 dev->gcChunk < dev->param.nChunksPerBlock &&
3170 (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
3172 dev->gcChunk++, oldChunk++) {
3173 if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
3175 /* This page is in use and might need to be copied off */
3181 yaffs_InitialiseTags(&tags);
3183 yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
3187 yaffs_FindObjectByNumber(dev,
3190 T(YAFFS_TRACE_GC_DETAIL,
3192 ("Collecting chunk in block %d, %d %d %d " TENDSTR),
3193 dev->gcChunk, tags.objectId, tags.chunkId,
3196 if (object && !yaffs_SkipVerification(dev)) {
3197 if (tags.chunkId == 0)
3198 matchingChunk = object->hdrChunk;
3199 else if (object->softDeleted)
3200 matchingChunk = oldChunk; /* Defeat the test */
3202 matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
3204 if (oldChunk != matchingChunk)
3205 T(YAFFS_TRACE_ERROR,
3206 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
3207 oldChunk, matchingChunk, tags.objectId, tags.chunkId));
3212 T(YAFFS_TRACE_ERROR,
3214 ("page %d in gc has no object: %d %d %d "
3216 tags.objectId, tags.chunkId, tags.byteCount));
3221 object->softDeleted &&
3222 tags.chunkId != 0) {
3223 /* Data chunk in a soft deleted file, throw it away
3224 * It's a soft deleted data chunk,
3225 * No need to copy this, just forget about it and
3226 * fix up the object.
3229 /* Free chunks already includes softdeleted chunks.
3230 * How ever this chunk is going to soon be really deleted
3231 * which will increment free chunks.
3232 * We have to decrement free chunks so this works out properly.
3236 object->nDataChunks--;
3238 if (object->nDataChunks <= 0) {
3239 /* remeber to clean up the object */
3240 dev->gcCleanupList[cleanups] =
3246 /* Todo object && object->deleted && object->nDataChunks == 0 */
3247 /* Deleted object header with no data chunks.
3248 * Can be discarded and the file deleted.
3250 object->hdrChunk = 0;
3251 yaffs_FreeTnode(object->myDev,
3254 object->variant.fileVariant.top = NULL;
3255 yaffs_DoGenericObjectDeletion(object);
3257 } else if (object) {
3258 /* It's either a data chunk in a live file or
3259 * an ObjectHeader, so we're interested in it.
3260 * NB Need to keep the ObjectHeaders of deleted files
3261 * until the whole file has been deleted off
3263 tags.serialNumber++;
3267 if (tags.chunkId == 0) {
3268 /* It is an object Id,
3269 * We need to nuke the shrinkheader flags first
3270 * Also need to clean up shadowing.
3271 * We no longer want the shrinkHeader flag since its work is done
3272 * and if it is left in place it will mess up scanning.
3275 yaffs_ObjectHeader *oh;
3276 oh = (yaffs_ObjectHeader *)buffer;
3279 tags.extraIsShrinkHeader = 0;
3281 oh->shadowsObject = 0;
3282 oh->inbandShadowsObject = 0;
3283 tags.extraShadows = 0;
3285 /* Update file size */
3286 if(object->variantType == YAFFS_OBJECT_TYPE_FILE){
3287 oh->fileSize = object->variant.fileVariant.fileSize;
3288 tags.extraFileLength = oh->fileSize;
3291 yaffs_VerifyObjectHeader(object, oh, &tags, 1);
3293 yaffs_WriteNewChunkWithTagsToNAND(dev,(__u8 *) oh, &tags, 1);
3296 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
3299 retVal = YAFFS_FAIL;
3302 /* Ok, now fix up the Tnodes etc. */
3304 if (tags.chunkId == 0) {
3306 object->hdrChunk = newChunk;
3307 object->serial = tags.serialNumber;
3309 /* It's a data chunk */
3311 ok = yaffs_PutChunkIntoFile
3319 if (retVal == YAFFS_OK)
3320 yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
3325 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3328 /* Do any required cleanups */
3329 for (i = 0; i < cleanups; i++) {
3330 /* Time to delete the file too */
3332 yaffs_FindObjectByNumber(dev,
3333 dev->gcCleanupList[i]);
3335 yaffs_FreeTnode(dev,
3336 object->variant.fileVariant.
3338 object->variant.fileVariant.top = NULL;
3341 ("yaffs: About to finally delete object %d"
3342 TENDSTR), object->objectId));
3343 yaffs_DoGenericObjectDeletion(object);
3344 object->myDev->nDeletedFiles--;
3351 yaffs_VerifyCollectedBlock(dev, bi, block);
3355 if (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
3357 * The gc did not complete. Set block state back to FULL
3358 * because checkpointing does not restore gc.
3360 bi->blockState = YAFFS_BLOCK_STATE_FULL;