2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_guts.c The main guts of YAFFS
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
16 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.3 2004-11-22 03:22:25 charles Exp $";
20 #include "yaffsinterface.h"
21 #include "yaffs_guts.h"
23 #include "yaffs_tagscompat.h"
25 #ifdef CONFIG_YAFFS_WINCE
26 void yfsd_LockYAFFS(BOOL fsLockOnly);
27 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
30 #define YAFFS_PASSIVE_GC_CHUNKS 2
33 // Use Steven Hill's ECC struff instead
34 // External functions for ECC on data
35 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
36 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
37 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
38 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
40 #include "yaffs_ecc.h"
43 // countBits is a quick way of counting the number of bits in a byte.
44 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
46 static const char yaffs_countBitsTable[256] =
48 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
49 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
50 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
52 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
53 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
54 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
55 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
56 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
58 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
59 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
60 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
61 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
62 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
63 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
66 static int yaffs_CountBits(__u8 x)
69 retVal = yaffs_countBitsTable[x];
75 // Stuff using yea olde tags
76 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
77 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
79 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
80 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
87 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
88 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
89 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
90 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
92 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
93 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
94 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
96 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
97 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
98 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
99 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
100 static int yaffs_CheckStructures(void);
101 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
102 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
104 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
106 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
107 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
110 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags);
112 // Robustification (if it ever comes about...)
113 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
114 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
115 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
116 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
117 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags);
119 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
121 static int yaffs_UnlinkWorker(yaffs_Object *obj);
122 static void yaffs_DestroyObject(yaffs_Object *obj);
125 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
128 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
131 loff_t yaffs_GetFileSize(yaffs_Object *obj);
134 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
136 #ifdef YAFFS_PARANOID
137 static int yaffs_CheckFileSanity(yaffs_Object *in);
139 #define yaffs_CheckFileSanity(in)
142 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
143 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
145 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
147 if(dev->readChunkWithTagsFromNAND)
148 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
150 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
153 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
157 tags->sequenceNumber = dev->sequenceNumber;
159 if(!yaffs_ValidateTags(tags))
161 T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
164 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
168 T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
172 if(dev->writeChunkWithTagsToNAND)
173 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
175 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
178 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
180 if(dev->markNANDBlockBad)
181 return dev->markNANDBlockBad(dev,blockNo);
183 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
185 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
187 if(dev->queryNANDBlock)
188 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
190 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
193 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
197 dev->nBlockErasures++;
198 result = dev->eraseBlockInNAND(dev,blockInNAND);
200 if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
204 static int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
206 return dev->initialiseNAND(dev);
212 // Temporary buffer manipulations
214 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
217 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
219 if(dev->tempBuffer[i].line == 0)
221 dev->tempBuffer[i].line = lineNo;
222 if((i+1) > dev->maxTemp)
224 dev->maxTemp = i + 1;
225 for(j = 0; j <= i; j++)
226 dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
229 return dev->tempBuffer[i].buffer;
233 T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
234 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
236 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
238 T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
240 dev->unmanagedTempAllocations++;
241 // Get an unmanaged one
242 return YMALLOC(dev->nBytesPerChunk);
247 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
250 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
252 if(dev->tempBuffer[i].buffer == buffer)
254 dev->tempBuffer[i].line = 0;
261 // assume it is an unmanaged one.
262 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
264 dev->unmanagedTempDeallocations++;
270 // Chunk bitmap manipulations
272 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
274 if(blk < dev->startBlock || blk > dev->endBlock)
276 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
279 return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock));
282 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
284 __u8 *blkBits = yaffs_BlockBits(dev,blk);
286 memset(blkBits,0,dev->chunkBitmapStride);
289 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
291 __u8 *blkBits = yaffs_BlockBits(dev,blk);
293 blkBits[chunk/8] &= ~ (1<<(chunk & 7));
296 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
298 __u8 *blkBits = yaffs_BlockBits(dev,blk);
300 blkBits[chunk/8] |= (1<<(chunk & 7));
303 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
305 __u8 *blkBits = yaffs_BlockBits(dev,blk);
306 return (blkBits[chunk/8] & (1<<(chunk & 7))) ? 1 :0;
309 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
311 __u8 *blkBits = yaffs_BlockBits(dev,blk);
313 for(i = 0; i < dev->chunkBitmapStride; i++)
315 if(*blkBits) return 1;
322 // Function to manipulate block info
323 static Y_INLINE yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
325 if(blk < dev->startBlock || blk > dev->endBlock)
327 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));
330 return &dev->blockInfo[blk - dev->startBlock];
335 static Y_INLINE int yaffs_HashFunction(int n)
337 return (n % YAFFS_NOBJECT_BUCKETS);
341 yaffs_Object *yaffs_Root(yaffs_Device *dev)
346 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
348 return dev->lostNFoundDir;
352 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
354 if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
356 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
361 return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
366 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
370 int doErrorCorrection)
373 yaffs_Spare localSpare;
382 // If we don't have a real spare, then we use a local one.
383 // Need this for the calculation of the ecc
390 retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
391 if(data && doErrorCorrection)
394 //Todo handle any errors
395 int eccResult1,eccResult2;
398 yaffs_ECCCalculate(data,calcEcc);
399 eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
400 yaffs_ECCCalculate(&data[256],calcEcc);
401 eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
405 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
408 else if(eccResult1<0)
410 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
416 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
419 else if(eccResult2<0)
421 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
425 if(eccResult1 || eccResult2)
427 // Hoosterman, we had a data problem on this page
428 yaffs_HandleReadDataError(dev,chunkInNAND);
434 // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
435 struct yaffs_NANDSpare nspare;
436 retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
437 memcpy (spare, &nspare, sizeof(yaffs_Spare));
438 if(data && doErrorCorrection)
442 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
444 else if(nspare.eccres1<0)
446 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
451 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
453 else if(nspare.eccres2<0)
455 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
458 if(nspare.eccres1 || nspare.eccres2)
460 // Hoosterman, we had a data problem on this page
461 yaffs_HandleReadDataError(dev,chunkInNAND);
473 int yaffs_CheckFF(__u8 *buffer,int nBytes)
475 //Horrible, slow implementation
478 if(*buffer != 0xFF) return 0;
483 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
486 int retval = YAFFS_OK;
487 __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
488 yaffs_ExtendedTags tags;
490 dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
492 if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
494 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
498 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
507 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
517 chunk = yaffs_AllocateChunk(dev,useReserve);
522 // First check this chunk is erased...
523 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
524 writeOk = yaffs_CheckChunkErased(dev,chunk);
528 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
532 writeOk = yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
538 // Copy the data into the robustification buffer.
539 // NB We do this at the end to prevent duplicates in the case of a write error.
541 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
545 yaffs_HandleWriteChunkError(dev,chunk);
549 } while(chunk >= 0 && ! writeOk);
553 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
554 dev->nRetriedWrites+= (attempts - 1);
565 // Functions for robustisizing
569 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
572 yaffs_MarkBlockBad(dev,blockInNAND);
574 yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
576 dev->nRetiredBlocks++;
581 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
583 dev->doingBufferedBlockRewrite = 1;
585 // Remove erased chunks
586 // Rewrite existing chunks to a new block
587 // Set current write block to the new block
589 dev->doingBufferedBlockRewrite = 0;
595 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
597 int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
599 // Mark the block for retirement
600 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
601 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
605 // Just do a garbage collection on the affected block then retire the block
610 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
614 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
618 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
622 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
624 int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
626 // Mark the block for retirement
627 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
629 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
635 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
639 if( memcmp(d0,d1,dataSize) != 0 ||
640 s0->tagByte0 != s1->tagByte0 ||
641 s0->tagByte1 != s1->tagByte1 ||
642 s0->tagByte2 != s1->tagByte2 ||
643 s0->tagByte3 != s1->tagByte3 ||
644 s0->tagByte4 != s1->tagByte4 ||
645 s0->tagByte5 != s1->tagByte5 ||
646 s0->tagByte6 != s1->tagByte6 ||
647 s0->tagByte7 != s1->tagByte7 ||
648 s0->ecc1[0] != s1->ecc1[0] ||
649 s0->ecc1[1] != s1->ecc1[1] ||
650 s0->ecc1[2] != s1->ecc1[2] ||
651 s0->ecc2[0] != s1->ecc2[0] ||
652 s0->ecc2[1] != s1->ecc2[1] ||
653 s0->ecc2[2] != s1->ecc2[2] )
663 ///////////////////////// Object management //////////////////
664 // List of spare objects
665 // The list is hooked together using the first pointer
668 // static yaffs_Object *yaffs_freeObjects = NULL;
670 // static int yaffs_nFreeObjects;
672 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
674 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
677 static __u16 yaffs_CalcNameSum(const YCHAR *name)
682 YUCHAR *bname = (YUCHAR *)name;
685 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
688 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
689 sum += yaffs_toupper(*bname) * i;
700 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
702 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
703 if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
705 yaffs_strcpy(obj->shortName,name);
709 obj->shortName[0]=_Y('\0');
712 obj->sum = yaffs_CalcNameSum(name);
716 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
718 yaffs_ECCCalculate(data , spare->ecc1);
719 yaffs_ECCCalculate(&data[256] , spare->ecc2);
724 ///////////////////////// TNODES ///////////////////////
726 // List of spare tnodes
727 // The list is hooked together using the first pointer
730 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
732 // static int yaffs_nFreeTnodes;
734 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
738 // yaffs_CreateTnodes creates a bunch more tnodes and
739 // adds them to the tnode free list.
740 // Don't use this function directly
742 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
745 yaffs_Tnode *newTnodes;
746 yaffs_TnodeList *tnl;
748 if(nTnodes < 1) return YAFFS_OK;
752 newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
756 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
760 // Hook them into the free list
761 for(i = 0; i < nTnodes - 1; i++)
763 newTnodes[i].internal[0] = &newTnodes[i+1];
764 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
765 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
769 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
770 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
771 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
773 dev->freeTnodes = newTnodes;
774 dev->nFreeTnodes+= nTnodes;
775 dev->nTnodesCreated += nTnodes;
777 // Now add this bunch of tnodes to a list for freeing up.
778 // NB If we can't add this to the management list it isn't fatal
779 // but it just means we can't free this bunch of tnodes later.
780 tnl = YMALLOC(sizeof(yaffs_TnodeList));
783 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
788 tnl->tnodes = newTnodes;
789 tnl->next = dev->allocatedTnodeList;
790 dev->allocatedTnodeList = tnl;
794 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
801 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
802 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
804 yaffs_Tnode *tn = NULL;
806 // If there are none left make more
809 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
814 tn = dev->freeTnodes;
815 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
816 if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
818 // Hoosterman, this thing looks like it isn't in the list
819 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
822 dev->freeTnodes = dev->freeTnodes->internal[0];
825 memset(tn,0,sizeof(yaffs_Tnode));
833 // FreeTnode frees up a tnode and puts it back on the free list
834 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
838 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
839 if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
841 // Hoosterman, this thing looks like it is already in the list
842 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
844 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
846 tn->internal[0] = dev->freeTnodes;
847 dev->freeTnodes = tn;
853 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
855 // Free the list of allocated tnodes
856 yaffs_TnodeList *tmp;
858 while(dev->allocatedTnodeList)
860 tmp = dev->allocatedTnodeList->next;
862 YFREE(dev->allocatedTnodeList->tnodes);
863 YFREE(dev->allocatedTnodeList);
864 dev->allocatedTnodeList = tmp;
868 dev->freeTnodes = NULL;
869 dev->nFreeTnodes = 0;
872 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
874 dev->allocatedTnodeList = NULL;
875 dev->freeTnodes = NULL;
876 dev->nFreeTnodes = 0;
877 dev->nTnodesCreated = 0;
882 void yaffs_TnodeTest(yaffs_Device *dev)
887 yaffs_Tnode *tn[1000];
889 YINFO("Testing TNodes");
891 for(j = 0; j < 50; j++)
893 for(i = 0; i < 1000; i++)
895 tn[i] = yaffs_GetTnode(dev);
898 YALERT("Getting tnode failed");
901 for(i = 0; i < 1000; i+=3)
903 yaffs_FreeTnode(dev,tn[i]);
912 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
914 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
915 // The look up tree is represented by the top tnode and the number of topLevel
916 // in the tree. 0 means only the level 0 tnode is in the tree.
919 // FindLevel0Tnode finds the level 0 tnode, if one exists.
920 // Used when reading.....
921 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
924 yaffs_Tnode *tn = fStruct->top;
926 int requiredTallness;
927 int level = fStruct->topLevel;
929 // Check sane level and chunk Id
930 if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
933 // sprintf(str,"Bad level %d",level);
938 if(chunkId > YAFFS_MAX_CHUNK_ID)
941 // sprintf(str,"Bad chunkId %d",chunkId);
946 // First check we're tall enough (ie enough topLevel)
948 i = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
949 requiredTallness = 0;
952 i >>= YAFFS_TNODES_INTERNAL_BITS;
957 if(requiredTallness > fStruct->topLevel)
959 // Not tall enough, so we can't find it, return NULL.
964 // Traverse down to level 0
965 while (level > 0 && tn)
967 tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) &
968 YAFFS_TNODES_INTERNAL_MASK];
976 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
977 // This happens in two steps:
978 // 1. If the tree isn't tall enough, then make it taller.
979 // 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
981 // Used when modifying the tree.
983 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
988 int requiredTallness;
995 //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
997 // Check sane level and page Id
998 if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
1001 // sprintf(str,"Bad level %d",fStruct->topLevel);
1006 if(chunkId > YAFFS_MAX_CHUNK_ID)
1009 // sprintf(str,"Bad chunkId %d",chunkId);
1014 // First check we're tall enough (ie enough topLevel)
1016 x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
1017 requiredTallness = 0;
1020 x >>= YAFFS_TNODES_INTERNAL_BITS;
1024 //T((TSTR(" required=%d"),requiredTallness));
1027 if(requiredTallness > fStruct->topLevel)
1029 // Not tall enough,gotta make the tree taller
1030 for(i = fStruct->topLevel; i < requiredTallness; i++)
1032 //T((TSTR(" add new top")));
1034 tn = yaffs_GetTnode(dev);
1038 tn->internal[0] = fStruct->top;
1043 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
1047 fStruct->topLevel = requiredTallness;
1051 // Traverse down to level 0, adding anything we need
1053 l = fStruct->topLevel;
1057 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) &
1058 YAFFS_TNODES_INTERNAL_MASK;
1060 //T((TSTR(" [%d:%d]"),l,i));
1062 if(!tn->internal[x])
1064 //T((TSTR(" added")));
1066 tn->internal[x] = yaffs_GetTnode(dev);
1069 tn = tn->internal[x];
1080 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
1085 for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
1087 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
1089 yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
1090 if(yaffs_TagsMatch(tags,objectId,chunkInInode))
1102 // DeleteWorker scans backwards through the tnode tree and deletes all the
1103 // chunks and tnodes in the file
1104 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1106 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1111 yaffs_ExtendedTags tags;
1113 yaffs_Device *dev = in->myDev;
1123 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1127 if(limit && (*limit) < 0)
1133 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1134 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1138 yaffs_FreeTnode(dev,tn->internal[i]);
1139 tn->internal[i] = NULL;
1144 return (allDone) ? 1 : 0;
1150 for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1155 chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1157 theChunk = tn->level0[i] << dev->chunkGroupBits;
1159 foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1163 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1180 return (i < 0) ? 1 : 0;
1192 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1195 yaffs_BlockInfo *theBlock;
1197 T(YAFFS_TRACE_DELETION,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1199 theBlock = yaffs_GetBlockInfo(dev, chunk/dev->nChunksPerBlock);
1202 theBlock->softDeletions++;
1206 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1207 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1209 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1211 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1216 yaffs_Device *dev = in->myDev;
1224 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1228 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1229 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1232 yaffs_FreeTnode(dev,tn->internal[i]);
1233 tn->internal[i] = NULL;
1237 //Hoosterman... how could this happen.
1241 return (allDone) ? 1 : 0;
1246 for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1250 // Note this does not find the real chunk, only the chunk group.
1251 // We make an assumption that a chunk group is niot larger than a block.
1252 theChunk = (tn->level0[i] << dev->chunkGroupBits);
1254 yaffs_SoftDeleteChunk(dev,theChunk);
1271 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1274 obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1277 if(obj->nDataChunks <= 0)
1279 // Empty file with no duplicate object headers, just delete it immediately
1280 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1281 obj->variant.fileVariant.top = NULL;
1282 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1283 yaffs_DoGenericObjectDeletion(obj);
1287 yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1288 obj->softDeleted = 1;
1297 // Pruning removes any part of the file structure tree that is beyond the
1298 // bounds of the file (ie that does not point to chunks).
1300 // A file should only get pruned when its size is reduced.
1302 // Before pruning, the chunks must be pulled from the tree and the
1303 // level 0 tnode entries must be zeroed out.
1304 // Could also use this for file deletion, but that's probably better handled
1305 // by a special case.
1307 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1309 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1318 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1320 if(tn->internal[i] && level > 0)
1322 tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1331 if(hasData == 0 && del0)
1333 // Free and return NULL
1335 yaffs_FreeTnode(dev,tn);
1345 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1352 if(fStruct->topLevel > 0)
1354 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1356 // Now we have a tree with all the non-zero branches NULL but the height
1357 // is the same as it was.
1358 // Let's see if we can trim internal tnodes to shorten the tree.
1359 // We can do this if only the 0th element in the tnode is in use
1360 // (ie all the non-zero are NULL)
1362 while(fStruct->topLevel && !done)
1367 for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1377 fStruct->top = tn->internal[0];
1378 fStruct->topLevel--;
1379 yaffs_FreeTnode(dev,tn);
1395 /////////////////////// End of File Structure functions. /////////////////
1397 // yaffs_CreateFreeObjects creates a bunch more objects and
1398 // adds them to the object free list.
1399 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1402 yaffs_Object *newObjects;
1403 yaffs_ObjectList *list;
1405 if(nObjects < 1) return YAFFS_OK;
1407 // make these things
1409 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1413 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1417 // Hook them into the free list
1418 for(i = 0; i < nObjects - 1; i++)
1420 newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1423 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1424 dev->freeObjects = newObjects;
1425 dev->nFreeObjects+= nObjects;
1426 dev->nObjectsCreated+= nObjects;
1428 // Now add this bunch of Objects to a list for freeing up.
1430 list = YMALLOC(sizeof(yaffs_ObjectList));
1433 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1437 list->objects = newObjects;
1438 list->next = dev->allocatedObjectList;
1439 dev->allocatedObjectList = list;
1448 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1449 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1451 yaffs_Object *tn = NULL;
1453 // If there are none left make more
1454 if(!dev->freeObjects)
1456 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1459 if(dev->freeObjects)
1461 tn = dev->freeObjects;
1462 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1463 dev->nFreeObjects--;
1465 // Now sweeten it up...
1467 memset(tn,0,sizeof(yaffs_Object));
1470 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1471 INIT_LIST_HEAD(&(tn->hardLinks));
1472 INIT_LIST_HEAD(&(tn->hashLink));
1473 INIT_LIST_HEAD(&tn->siblings);
1475 // Add it to the lost and found directory.
1476 // NB Can't put root or lostNFound in lostNFound so
1477 // check if lostNFound exists first
1478 if(dev->lostNFoundDir)
1480 yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);
1488 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1491 yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);
1494 obj->fake = 1; // it is fake so it has no NAND presence...
1495 obj->renameAllowed= 0; // ... and we're not allowed to rename it...
1496 obj->unlinkAllowed= 0; // ... or unlink it
1499 obj->st_mode = mode;
1501 obj->chunkId = 0; // Not a valid chunk.
1509 static void yaffs_UnhashObject(yaffs_Object *tn)
1512 yaffs_Device *dev = tn->myDev;
1515 // If it is still linked into the bucket list, free from the list
1516 if(!list_empty(&tn->hashLink))
1518 list_del_init(&tn->hashLink);
1519 bucket = yaffs_HashFunction(tn->objectId);
1520 dev->objectBucket[bucket].count--;
1526 // FreeObject frees up a Object and puts it back on the free list
1527 static void yaffs_FreeObject(yaffs_Object *tn)
1530 yaffs_Device *dev = tn->myDev;
1535 // We're still hooked up to a cached inode.
1536 // Don't delete now, but mark for later deletion
1537 tn->deferedFree = 1;
1542 yaffs_UnhashObject(tn);
1544 // Link into the free list.
1545 tn->siblings.next = (struct list_head *)(dev->freeObjects);
1546 dev->freeObjects = tn;
1547 dev->nFreeObjects++;
1554 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1556 if(obj->deferedFree)
1558 yaffs_FreeObject(obj);
1566 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1568 // Free the list of allocated Objects
1570 yaffs_ObjectList *tmp;
1572 while( dev->allocatedObjectList)
1574 tmp = dev->allocatedObjectList->next;
1575 YFREE(dev->allocatedObjectList->objects);
1576 YFREE(dev->allocatedObjectList);
1578 dev->allocatedObjectList = tmp;
1581 dev->freeObjects = NULL;
1582 dev->nFreeObjects = 0;
1585 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1589 dev->allocatedObjectList = NULL;
1590 dev->freeObjects = NULL;
1591 dev->nFreeObjects = 0;
1593 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1595 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1596 dev->objectBucket[i].count = 0;
1606 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1611 int lowest = 999999;
1614 // First let's see if we can find one that's empty.
1616 for(i = 0; i < 10 && lowest > 0; i++)
1619 x %= YAFFS_NOBJECT_BUCKETS;
1620 if(dev->objectBucket[x].count < lowest)
1622 lowest = dev->objectBucket[x].count;
1628 // If we didn't find an empty list, then try
1629 // looking a bit further for a short one
1631 for(i = 0; i < 10 && lowest > 3; i++)
1634 x %= YAFFS_NOBJECT_BUCKETS;
1635 if(dev->objectBucket[x].count < lowest)
1637 lowest = dev->objectBucket[x].count;
1646 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1648 int bucket = yaffs_FindNiceObjectBucket(dev);
1650 // Now find an object value that has not already been taken
1651 // by scanning the list.
1654 struct list_head *i;
1656 __u32 n = (__u32)bucket;
1658 //yaffs_CheckObjectHashSanity();
1663 n += YAFFS_NOBJECT_BUCKETS;
1664 if(1 ||dev->objectBucket[bucket].count > 0)
1666 list_for_each(i,&dev->objectBucket[bucket].list)
1668 // If there is already one in the list
1669 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1677 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1682 void yaffs_HashObject(yaffs_Object *in)
1684 int bucket = yaffs_HashFunction(in->objectId);
1685 yaffs_Device *dev = in->myDev;
1687 if(!list_empty(&in->hashLink))
1693 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1694 dev->objectBucket[bucket].count++;
1698 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1700 int bucket = yaffs_HashFunction(number);
1701 struct list_head *i;
1704 list_for_each(i,&dev->objectBucket[bucket].list)
1706 // Look if it is in the list
1709 in = list_entry(i, yaffs_Object,hashLink);
1710 if(in->objectId == number)
1722 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1725 yaffs_Object *theObject;
1729 number = yaffs_CreateNewObjectNumber(dev);
1732 theObject = yaffs_AllocateEmptyObject(dev);
1736 theObject->fake = 0;
1737 theObject->renameAllowed = 1;
1738 theObject->unlinkAllowed = 1;
1739 theObject->objectId = number;
1740 yaffs_HashObject(theObject);
1741 theObject->variantType = type;
1742 #ifdef CONFIG_YAFFS_WINCE
1743 yfsd_WinFileTimeNow(theObject->win_atime);
1744 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1745 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1749 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = Y_CURRENT_TIME;
1753 case YAFFS_OBJECT_TYPE_FILE:
1754 theObject->variant.fileVariant.fileSize = 0;
1755 theObject->variant.fileVariant.scannedFileSize = 0;
1756 theObject->variant.fileVariant.topLevel = 0;
1757 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
1759 case YAFFS_OBJECT_TYPE_DIRECTORY:
1760 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1762 case YAFFS_OBJECT_TYPE_SYMLINK:
1763 // No action required
1765 case YAFFS_OBJECT_TYPE_HARDLINK:
1766 // No action required
1768 case YAFFS_OBJECT_TYPE_SPECIAL:
1769 // No action required
1771 case YAFFS_OBJECT_TYPE_UNKNOWN:
1772 // todo this should not happen
1780 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1782 yaffs_Object *theObject = NULL;
1786 theObject = yaffs_FindObjectByNumber(dev,number);
1791 theObject = yaffs_CreateNewObject(dev,number,type);
1798 YCHAR *yaffs_CloneString(const YCHAR *str)
1800 YCHAR *newStr = NULL;
1804 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1805 yaffs_strcpy(newStr,str);
1813 // Mknod (create) a new object.
1814 // equivalentObject only has meaning for a hard link;
1815 // aliasString only has meaning for a sumlink.
1816 // rdev only has meaning for devices (a subset of special objects)
1817 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1818 yaffs_Object *parent,
1823 yaffs_Object *equivalentObject,
1824 const YCHAR *aliasString,
1829 yaffs_Device *dev = parent->myDev;
1831 // Check if the entry exists. If it does then fail the call since we don't want a dup.
1832 if(yaffs_FindObjectByName(parent,name))
1837 in = yaffs_CreateNewObject(dev,-1,type);
1843 in->variantType = type;
1847 #ifdef CONFIG_YAFFS_WINCE
1848 yfsd_WinFileTimeNow(in->win_atime);
1849 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1850 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1853 in->st_atime = in->st_mtime = in->st_ctime = Y_CURRENT_TIME;
1859 in->nDataChunks = 0;
1861 yaffs_SetObjectName(in,name);
1864 yaffs_AddObjectToDirectory(parent,in);
1866 in->myDev = parent->myDev;
1871 case YAFFS_OBJECT_TYPE_SYMLINK:
1872 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1874 case YAFFS_OBJECT_TYPE_HARDLINK:
1875 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1876 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1877 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1879 case YAFFS_OBJECT_TYPE_FILE: // do nothing
1880 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1881 case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1882 case YAFFS_OBJECT_TYPE_UNKNOWN:
1886 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1887 yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1889 // Could not create the object header, fail the creation
1890 yaffs_DestroyObject(in);
1899 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1901 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1904 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1906 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1909 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1911 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1914 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1916 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1919 // NB yaffs_Link returns the object id of the equivalent object.
1920 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1922 // Get the real object in case we were fed a hard link as an equivalent object
1923 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1925 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1927 return equivalentObject;
1937 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1944 newDir = obj->parent; // use the old directory
1947 // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1948 if(obj->myDev->isYaffs2)
1950 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1954 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1957 deleteOp = (newDir == obj->myDev->deletedDir);
1959 // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1960 // duplicate names are allowed.
1961 // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1965 !yaffs_FindObjectByName(newDir,newName)) &&
1966 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1968 yaffs_SetObjectName(obj,newName);
1971 yaffs_AddObjectToDirectory(newDir,obj);
1973 if(unlinkOp) obj->unlinked = 1;
1975 // If it is a dletion then we mark it as a shrink for gc purposes.
1976 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1987 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1992 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1993 // Special case for case insemsitive systems (eg. WinCE).
1994 // While look-up is case insensitive, the name isn't.
1995 // THerefore we might want to change x.txt to X.txt
1996 if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
2002 obj = yaffs_FindObjectByName(oldDir,oldName);
2003 if(obj && obj->renameAllowed)
2005 return yaffs_ChangeObjectName(obj,newDir,newName,force);
2012 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
2014 // Scan the buckets and check that the lists
2015 // have as many members as the count says there are
2018 struct list_head *j;
2021 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2025 list_for_each(j,&dev->objectBucket[bucket].list)
2030 if(countEm != dev->objectBucket[bucket].count)
2032 T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2041 void yaffs_ObjectTest(yaffs_Device *dev)
2043 yaffs_Object *in[1000];
2045 yaffs_Object *inold[1000];
2049 memset(in,0,1000*sizeof(yaffs_Object *));
2050 memset(inold,0,1000*sizeof(yaffs_Object *));
2052 yaffs_CheckObjectHashSanity(dev);
2054 for(j = 0; j < 10; j++)
2058 for(i = 0; i < 1000; i++)
2060 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2063 YINFO("No more inodes");
2067 inNo[i] = in[i]->objectId;
2071 for(i = 0; i < 1000; i++)
2073 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2075 //T(("Differnce in look up test\n"));
2079 // T(("Look up ok\n"));
2083 yaffs_CheckObjectHashSanity(dev);
2085 for(i = 0; i < 1000; i+=3)
2087 yaffs_FreeObject(in[i]);
2092 yaffs_CheckObjectHashSanity(dev);
2099 /////////////////////////// Block Management and Page Allocation ///////////////////
2102 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2104 dev->allocationBlock = -1; // force it to get a new one
2105 //Todo we're assuming the malloc will pass.
2106 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2107 // Set up dynamic blockinfo stuff.
2108 dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2109 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2110 if(dev->blockInfo && dev->chunkBits)
2112 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2113 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2121 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2123 YFREE(dev->blockInfo);
2124 dev->blockInfo = NULL;
2125 YFREE(dev->chunkBits);
2126 dev->chunkBits = NULL;
2130 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2136 if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2138 if(!bi->hasShrinkHeader) return 1; // can gc
2141 // Find the oldest dirty sequence number if we don't know it and save it
2142 // so we don't have to keep recomputing it.
2143 if(!dev->oldestDirtySequence)
2145 seq = dev->sequenceNumber;
2147 for(i = dev->startBlock; i <= dev->endBlock; i++)
2149 b = yaffs_GetBlockInfo(dev,i);
2150 if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2151 (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2152 b->sequenceNumber < seq)
2154 seq = b->sequenceNumber;
2157 dev->oldestDirtySequence = seq;
2161 // Can't do gc of this block if there are any blocks older than this one that have
2163 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2170 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2171 // for garbage collection.
2176 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2179 int b = dev->currentDirtyChecker;
2185 yaffs_BlockInfo *bi;
2186 static int nonAggressiveSkip = 0;
2188 // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2190 // else (we're doing a leasurely gc), then we only bother to do this if the
2191 // block has only a few pages in use.
2194 nonAggressiveSkip--;
2196 if(!aggressive &&(nonAggressiveSkip > 0))
2201 pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2204 iterations = dev->endBlock - dev->startBlock + 1;
2208 iterations = dev->endBlock - dev->startBlock + 1;
2209 iterations = iterations / 16;
2210 if(iterations > 200)
2216 for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2219 if ( b < dev->startBlock || b > dev->endBlock)
2221 b = dev->startBlock;
2224 if(b < dev->startBlock || b > dev->endBlock)
2226 T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2230 bi = yaffs_GetBlockInfo(dev,b);
2232 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2233 (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2234 yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2237 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2241 dev->currentDirtyChecker = b;
2245 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2248 dev->oldestDirtySequence = 0; // clear this
2252 nonAggressiveSkip = 4;
2259 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2261 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2265 // If the block is still healthy erase it and mark as clean.
2266 // If the block has had a data failure, then retire it.
2267 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2269 if(!bi->needsRetiring)
2271 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2274 dev->nErasureFailures++;
2275 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2279 if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2282 for(i = 0; i < dev->nChunksPerBlock; i++)
2284 if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2286 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2294 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2295 dev->nErasedBlocks++;
2297 bi->softDeletions = 0;
2298 bi->hasShrinkHeader=0;
2299 yaffs_ClearChunkBits(dev,blockNo);
2301 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2305 yaffs_RetireBlock(dev,blockNo);
2306 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2310 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2313 yaffs_BlockInfo *bi;
2315 for(i= dev->startBlock; i <=dev->endBlock; i++)
2317 bi = yaffs_GetBlockInfo(dev,i);
2318 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2319 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));
2321 for(j = 0; j < dev->nChunksPerBlock; j++)
2323 if(yaffs_CheckChunkBit(dev,i,j))
2325 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2329 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2335 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2339 yaffs_BlockInfo *bi;
2344 if(j < 0 || j > 100)
2347 yaffs_DumpBlockStats(dev);
2352 if(dev->nErasedBlocks < 1)
2354 // Hoosterman we've got a problem.
2355 // Can't get space to gc
2356 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2361 // Find an empty block.
2363 for(i = dev->startBlock; i <= dev->endBlock; i++)
2365 dev->allocationBlockFinder++;
2366 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock)
2368 dev->allocationBlockFinder = dev->startBlock;
2371 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2373 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2375 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2376 dev->sequenceNumber++;
2377 bi->sequenceNumber = dev->sequenceNumber;
2378 dev->nErasedBlocks--;
2379 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq %d" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber));
2380 return dev->allocationBlockFinder;
2383 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been one" TENDSTR)));
2391 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2394 yaffs_BlockInfo *bi;
2396 if(dev->allocationBlock < 0)
2398 // Get next block to allocate off
2399 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2400 dev->allocationPage = 0;
2403 if(!useReserve && dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2405 // Not enough space to allocate unless we're allowed to use the reserve.
2409 if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2411 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));
2415 // Next page please....
2416 if(dev->allocationBlock >= 0)
2418 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2420 retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
2421 dev->allocationPage;
2423 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2425 dev->allocationPage++;
2429 // If the block is full set the state to full
2430 if(dev->allocationPage >= dev->nChunksPerBlock)
2432 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2433 dev->allocationBlock = -1;
2440 T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2446 // To determine if we have enough space we just look at the
2447 // number of erased blocks.
2448 // The cache is allowed to use reserved blocks.
2450 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2452 return (dev->nErasedBlocks >= dev->nReservedBlocks);
2456 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2460 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2462 if(dev->allocationBlock> 0)
2464 n += (dev->nChunksPerBlock - dev->allocationPage);
2471 int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2477 int retVal = YAFFS_OK;
2481 int chunksBefore = yaffs_GetErasedChunks(dev);
2484 yaffs_ExtendedTags tags;
2486 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2488 yaffs_Object *object;
2490 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2492 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2493 //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
2495 bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2498 if(!yaffs_StillSomeChunkBits(dev,block))
2500 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2501 yaffs_BlockBecameDirty(dev,block);
2506 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2508 for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock;
2509 chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2510 chunkInBlock++, oldChunk++ )
2512 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2515 // This page is in use and might need to be copied off
2519 //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2521 yaffs_InitialiseTags(&tags);
2523 yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2525 object = yaffs_FindObjectByNumber(dev,tags.objectId);
2527 T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2531 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2534 if(object && object->deleted && tags.chunkId != 0)
2536 // Data chunk in a deleted file, throw it away
2537 // It's a deleted data chunk,
2538 // No need to copy this, just forget about it and fix up the
2541 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0);
2542 object->nDataChunks--;
2544 if(object->nDataChunks <= 0)
2546 // remeber to clean up the object
2547 dev->gcCleanupList[cleanups] = tags.objectId;
2552 else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2554 // Deleted object header with no data chunks.
2555 // Can be discarded and the file deleted.
2556 object->chunkId = 0;
2557 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2558 object->variant.fileVariant.top = NULL;
2559 yaffs_DoGenericObjectDeletion(object);
2564 // It's either a data chunk in a live file or
2565 // an ObjectHeader, so we're interested in it.
2566 // NB Need to keep the ObjectHeaders of deleted files
2567 // until the whole file has been deleted off
2568 tags.serialNumber++;
2572 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2576 retVal = YAFFS_FAIL;
2581 // Ok, now fix up the Tnodes etc.
2583 if(tags.chunkId == 0)
2586 object->chunkId = newChunk;
2587 object->serial = tags.serialNumber;
2591 // It's a data chunk
2592 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2597 yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);
2602 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2605 // Do any required cleanups
2606 for(i = 0; i < cleanups; i++)
2608 // Time to delete the file too
2609 object = yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2612 yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2613 object->variant.fileVariant.top = NULL;
2614 T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2615 yaffs_DoGenericObjectDeletion(object);
2622 if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2624 T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2631 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2633 // find a file to delete
2634 struct list_head *i;
2638 //Scan the unlinked files looking for one to delete
2639 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2643 l = list_entry(i, yaffs_Object,siblings);
2654 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2656 // This does background deletion on unlinked files.. only deleted ones.
2657 // If we don't have a file we're working on then find one
2658 if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2660 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2663 // OK, we're working on a file...
2664 if(dev->unlinkedDeletion)
2666 yaffs_Object *obj = dev->unlinkedDeletion;
2668 int limit; // Number of chunks to delete in a file.
2669 // NB this can be exceeded, but not by much.
2673 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2675 if(obj->nDataChunks == 0)
2677 // Done all the deleting of data chunks.
2678 // Now dump the header and clean up
2679 yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2680 obj->variant.fileVariant.top = NULL;
2681 yaffs_DoGenericObjectDeletion(obj);
2682 dev->nDeletedFiles--;
2683 dev->nUnlinkedFiles--;
2684 dev->nBackgroundDeletions++;
2685 dev->unlinkedDeletion = NULL;
2693 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2694 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2699 //yaffs_DoUnlinkedFileDeletion(dev);
2701 if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2708 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2712 dev->garbageCollections++;
2713 return yaffs_GarbageCollectBlock(dev,block);
2725 // New garbage collector
2726 // If we're very low on erased blocks then we do aggressive garbage collection
2727 // otherwise we do "leasurely" garbage collection.
2728 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2729 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2731 // The idea is to help clear out space in a more spread-out manner.
2732 // Dunno if it really does anything useful.
2734 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2738 int gcOk = YAFFS_OK;
2741 //yaffs_DoUnlinkedFileDeletion(dev);
2743 // This loop should pass the first time.
2744 // We'll only see looping here if the erase of the collected block fails.
2748 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2750 // We need a block soon...
2755 // We're in no hurry
2759 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2763 dev->garbageCollections++;
2766 dev->passiveGarbageCollections++;
2769 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2771 gcOk = yaffs_GarbageCollectBlock(dev,block);
2774 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1))
2776 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2778 } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2780 return aggressive ? gcOk: YAFFS_OK;
2784 //////////////////////////// TAGS ///////////////////////////////////////
2787 void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
2789 memset(tags,0,sizeof(yaffs_ExtendedTags));
2790 tags->validMarker0 = 0xAAAAAAAA;
2791 tags->validMarker1 = 0x55555555;
2794 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
2796 return (tags->validMarker0 == 0xAAAAAAAA && tags->validMarker1 == 0x55555555);
2803 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2807 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2814 for(i = 0; i < 8; i++)
2816 for(j = 1; j &0xff; j<<=1)
2831 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
2833 unsigned ecc = tags->ecc;
2835 yaffs_CalcTagsECC(tags);
2839 if(ecc && ecc <= 64)
2841 // TODO: Handle the failure better. Retire?
2842 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2846 b[ecc / 8] ^= (1 << (ecc & 7));
2848 // Now recvalc the ecc
2849 yaffs_CalcTagsECC(tags);
2851 return 1; // recovered error
2855 // Wierd ecc failure value
2856 // TODO Need to do somethiong here
2857 return -1; //unrecovered error
2863 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2865 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2867 yaffs_CalcTagsECC(tagsPtr);
2869 sparePtr->tagByte0 = tu->asBytes[0];
2870 sparePtr->tagByte1 = tu->asBytes[1];
2871 sparePtr->tagByte2 = tu->asBytes[2];
2872 sparePtr->tagByte3 = tu->asBytes[3];
2873 sparePtr->tagByte4 = tu->asBytes[4];
2874 sparePtr->tagByte5 = tu->asBytes[5];
2875 sparePtr->tagByte6 = tu->asBytes[6];
2876 sparePtr->tagByte7 = tu->asBytes[7];
2879 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2881 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2884 tu->asBytes[0]= sparePtr->tagByte0;
2885 tu->asBytes[1]= sparePtr->tagByte1;
2886 tu->asBytes[2]= sparePtr->tagByte2;
2887 tu->asBytes[3]= sparePtr->tagByte3;
2888 tu->asBytes[4]= sparePtr->tagByte4;
2889 tu->asBytes[5]= sparePtr->tagByte5;
2890 tu->asBytes[6]= sparePtr->tagByte6;
2891 tu->asBytes[7]= sparePtr->tagByte7;
2893 result = yaffs_CheckECCOnTags(tagsPtr);
2896 dev->tagsEccFixed++;
2900 dev->tagsEccUnfixed++;
2904 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2906 memset(spare,0xFF,sizeof(yaffs_Spare));
2912 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2914 // NB There must be tags, data is optional
2915 // If there is data, then an ECC is calculated on it.
2924 //yaffs_SpareInitialise(&spare);
2926 //if(!dev->useNANDECC && buffer)
2928 // yaffs_CalcECC(buffer,&spare);
2931 //yaffs_LoadTagsIntoSpare(&spare,tags);
2933 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2938 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2940 return ( tags->chunkId == chunkInObject &&
2941 tags->objectId == objectId &&
2942 !tags->chunkDeleted) ? 1 : 0;
2948 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2950 //Get the Tnode, then get the level 0 offset chunk offset
2953 yaffs_ExtendedTags localTags;
2956 yaffs_Device *dev = in->myDev;
2961 // Passed a NULL, so use our own tags space
2965 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2969 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2971 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2976 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2978 //Get the Tnode, then get the level 0 offset chunk offset
2981 yaffs_ExtendedTags localTags;
2983 yaffs_Device *dev = in->myDev;
2988 // Passed a NULL, so use our own tags space
2992 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2997 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2999 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
3001 // Delete the entry in the filestructure (if found)
3004 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
3009 //T(("No level 0 found for %d\n", chunkInInode));
3014 //T(("Could not find %d to delete\n",chunkInInode));
3020 #ifdef YAFFS_PARANOID
3022 static int yaffs_CheckFileSanity(yaffs_Object *in)
3030 yaffs_Tags localTags;
3031 yaffs_Tags *tags = &localTags;
3036 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3038 //T(("Object not a file\n"));
3042 objId = in->objectId;
3043 fSize = in->variant.fileVariant.fileSize;
3044 nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3046 for(chunk = 1; chunk <= nChunks; chunk++)
3048 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3053 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3055 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3059 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3060 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3068 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
3069 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
3078 //T(("No level 0 found for %d\n", chunk));
3082 return failed ? YAFFS_FAIL : YAFFS_OK;
3087 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3090 yaffs_Device *dev = in->myDev;
3092 yaffs_ExtendedTags existingTags;
3093 yaffs_ExtendedTags newTags;
3094 unsigned existingSerial, newSerial;
3096 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3102 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
3106 // If we're scanning then we need to test for duplicates
3107 // NB This does not need to be efficient since it should only ever
3108 // happen when the power fails during a write, then only one
3109 // chunk should ever be affected.
3112 if(existingChunk != 0)
3114 // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3115 // thus we have to do a FindChunkInFile to get the real chunk id.
3117 // We have a duplicate now we need to decide which one to use
3118 // To do this we get both sets of tags and compare serial numbers.
3119 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3123 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3125 if(existingChunk <=0)
3127 //Hoosterman - how did this happen?
3129 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3134 // NB The deleted flags should be false, otherwise the chunks will
3135 // not be loaded during a scan
3137 newSerial = newTags.serialNumber;
3138 existingSerial = existingTags.serialNumber;
3140 if( in->myDev->isYaffs2 ||
3141 existingChunk <= 0 ||
3142 ((existingSerial+1) & 3) == newSerial)
3145 // Delete the old one and drop through to update the tnode
3146 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3151 // Delete the new one and return early so that the tnode isn't changed
3152 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3159 if(existingChunk == 0)
3164 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3171 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3173 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3175 if(chunkInNAND >= 0)
3177 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3181 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3183 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3190 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3194 yaffs_ExtendedTags tags;
3195 yaffs_BlockInfo *bi;
3197 if(chunkId <= 0) return;
3200 block = chunkId / dev->nChunksPerBlock;
3201 page = chunkId % dev->nChunksPerBlock;
3203 bi = yaffs_GetBlockInfo(dev,block);
3205 T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3208 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3211 // yaffs_SpareInitialise(&spare);
3213 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3215 //read data before write, to ensure correct ecc
3216 //if we're using MTD verification under Linux
3217 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3220 yaffs_InitialiseTags(&tags);
3222 tags.chunkDeleted = 1;
3225 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3226 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3230 dev->nUnmarkedDeletions++;
3234 // Pull out of the management area.
3235 // If the whole block became dirty, this will kick off an erasure.
3236 if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3237 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3238 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3239 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3243 yaffs_ClearChunkBit(dev,block,page);
3246 if(bi->pagesInUse == 0 &&
3247 !bi->hasShrinkHeader &&
3248 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3249 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3251 yaffs_BlockBecameDirty(dev,block);
3257 // T(("Bad news deleting chunk %d\n",chunkId));
3265 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3267 // Find old chunk Need to do this to get serial number
3268 // Write new one and patch into tree.
3269 // Invalidate old tags.
3272 yaffs_ExtendedTags prevTags;
3275 yaffs_ExtendedTags newTags;
3277 yaffs_Device *dev = in->myDev;
3279 yaffs_CheckGarbageCollection(dev);
3281 // Get the previous chunk at this location in the file if it exists
3282 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3285 yaffs_InitialiseTags(&newTags);
3287 newTags.chunkId = chunkInInode;
3288 newTags.objectId = in->objectId;
3289 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3290 newTags.byteCount = nBytes;
3292 // yaffs_CalcTagsECC(&newTags);
3294 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3298 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3301 if(prevChunkId >= 0)
3303 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3307 yaffs_CheckFileSanity(in);
3318 // UpdateObjectHeader updates the header on NAND for an object.
3319 // If name is not NULL, then that new name is used.
3321 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3324 yaffs_BlockInfo *bi;
3326 yaffs_Device *dev = in->myDev;
3332 yaffs_ExtendedTags newTags;
3334 __u8 *buffer = NULL;
3335 YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3337 // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3339 yaffs_ObjectHeader *oh = NULL;
3340 // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3343 if(!in->fake || force)
3346 yaffs_CheckGarbageCollection(dev);
3348 buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3349 oh = (yaffs_ObjectHeader *)buffer;
3351 prevChunkId = in->chunkId;
3353 if(prevChunkId >= 0)
3355 yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3356 memcpy(oldName,oh->name,sizeof(oh->name));
3359 memset(buffer,0xFF,dev->nBytesPerChunk);
3362 oh->type = in->variantType;
3364 oh->st_mode = in->st_mode;
3366 #ifdef CONFIG_YAFFS_WINCE
3367 oh->win_atime[0] = in->win_atime[0];
3368 oh->win_ctime[0] = in->win_ctime[0];
3369 oh->win_mtime[0] = in->win_mtime[0];
3370 oh->win_atime[1] = in->win_atime[1];
3371 oh->win_ctime[1] = in->win_ctime[1];
3372 oh->win_mtime[1] = in->win_mtime[1];
3374 oh->st_uid = in->st_uid;
3375 oh->st_gid = in->st_gid;
3376 oh->st_atime = in->st_atime;
3377 oh->st_mtime = in->st_mtime;
3378 oh->st_ctime = in->st_ctime;
3379 oh->st_rdev = in->st_rdev;
3383 oh->parentObjectId = in->parent->objectId;
3387 oh->parentObjectId = 0;
3390 //oh->sum = in->sum;
3393 memset(oh->name,0,sizeof(oh->name));
3394 yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3396 else if(prevChunkId)
3398 memcpy(oh->name, oldName,sizeof(oh->name));
3402 memset(oh->name,0,sizeof(oh->name));
3405 oh->isShrink = isShrink;
3407 switch(in->variantType)
3409 case YAFFS_OBJECT_TYPE_UNKNOWN:
3410 // Should not happen
3412 case YAFFS_OBJECT_TYPE_FILE:
3413 oh->fileSize = in->variant.fileVariant.fileSize;
3415 case YAFFS_OBJECT_TYPE_HARDLINK:
3416 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3418 case YAFFS_OBJECT_TYPE_SPECIAL:
3421 case YAFFS_OBJECT_TYPE_DIRECTORY:
3424 case YAFFS_OBJECT_TYPE_SYMLINK:
3425 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3426 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3431 yaffs_InitialiseTags(&newTags);
3433 newTags.chunkId = 0;
3434 newTags.objectId = in->objectId;
3435 newTags.serialNumber = in->serial;
3438 // Create new chunk in NAND
3439 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3444 in->chunkId = newChunkId;
3446 if(prevChunkId >= 0)
3448 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3453 // If this was a shrink, then mark the block that the chunk lives on
3456 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3457 bi->hasShrinkHeader = 1;