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.1 2004-11-03 08:14:07 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 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_DELETE,(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;
1532 yaffs_UnhashObject(tn);
1534 // Link into the free list.
1535 tn->siblings.next = (struct list_head *)(dev->freeObjects);
1536 dev->freeObjects = tn;
1537 dev->nFreeObjects++;
1543 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1545 // Free the list of allocated Objects
1547 yaffs_ObjectList *tmp;
1549 while( dev->allocatedObjectList)
1551 tmp = dev->allocatedObjectList->next;
1552 YFREE(dev->allocatedObjectList->objects);
1553 YFREE(dev->allocatedObjectList);
1555 dev->allocatedObjectList = tmp;
1558 dev->freeObjects = NULL;
1559 dev->nFreeObjects = 0;
1562 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1566 dev->allocatedObjectList = NULL;
1567 dev->freeObjects = NULL;
1568 dev->nFreeObjects = 0;
1570 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1572 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1573 dev->objectBucket[i].count = 0;
1583 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1588 int lowest = 999999;
1591 // First let's see if we can find one that's empty.
1593 for(i = 0; i < 10 && lowest > 0; i++)
1596 x %= YAFFS_NOBJECT_BUCKETS;
1597 if(dev->objectBucket[x].count < lowest)
1599 lowest = dev->objectBucket[x].count;
1605 // If we didn't find an empty list, then try
1606 // looking a bit further for a short one
1608 for(i = 0; i < 10 && lowest > 3; i++)
1611 x %= YAFFS_NOBJECT_BUCKETS;
1612 if(dev->objectBucket[x].count < lowest)
1614 lowest = dev->objectBucket[x].count;
1623 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1625 int bucket = yaffs_FindNiceObjectBucket(dev);
1627 // Now find an object value that has not already been taken
1628 // by scanning the list.
1631 struct list_head *i;
1633 __u32 n = (__u32)bucket;
1635 //yaffs_CheckObjectHashSanity();
1640 n += YAFFS_NOBJECT_BUCKETS;
1641 if(1 ||dev->objectBucket[bucket].count > 0)
1643 list_for_each(i,&dev->objectBucket[bucket].list)
1645 // If there is already one in the list
1646 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1654 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1659 void yaffs_HashObject(yaffs_Object *in)
1661 int bucket = yaffs_HashFunction(in->objectId);
1662 yaffs_Device *dev = in->myDev;
1664 if(!list_empty(&in->hashLink))
1670 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1671 dev->objectBucket[bucket].count++;
1675 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1677 int bucket = yaffs_HashFunction(number);
1678 struct list_head *i;
1681 list_for_each(i,&dev->objectBucket[bucket].list)
1683 // Look if it is in the list
1686 in = list_entry(i, yaffs_Object,hashLink);
1687 if(in->objectId == number)
1699 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1702 yaffs_Object *theObject;
1706 number = yaffs_CreateNewObjectNumber(dev);
1709 theObject = yaffs_AllocateEmptyObject(dev);
1713 theObject->fake = 0;
1714 theObject->renameAllowed = 1;
1715 theObject->unlinkAllowed = 1;
1716 theObject->objectId = number;
1717 yaffs_HashObject(theObject);
1718 theObject->variantType = type;
1719 #ifdef CONFIG_YAFFS_WINCE
1720 yfsd_WinFileTimeNow(theObject->win_atime);
1721 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1722 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1726 #if defined(CONFIG_KERNEL_2_5)
1727 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = CURRENT_TIME.tv_sec;
1729 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = CURRENT_TIME;
1734 case YAFFS_OBJECT_TYPE_FILE:
1735 theObject->variant.fileVariant.fileSize = 0;
1736 theObject->variant.fileVariant.scannedFileSize = 0;
1737 theObject->variant.fileVariant.topLevel = 0;
1738 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
1740 case YAFFS_OBJECT_TYPE_DIRECTORY:
1741 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1743 case YAFFS_OBJECT_TYPE_SYMLINK:
1744 // No action required
1746 case YAFFS_OBJECT_TYPE_HARDLINK:
1747 // No action required
1749 case YAFFS_OBJECT_TYPE_SPECIAL:
1750 // No action required
1752 case YAFFS_OBJECT_TYPE_UNKNOWN:
1753 // todo this should not happen
1761 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1763 yaffs_Object *theObject = NULL;
1767 theObject = yaffs_FindObjectByNumber(dev,number);
1772 theObject = yaffs_CreateNewObject(dev,number,type);
1779 YCHAR *yaffs_CloneString(const YCHAR *str)
1781 YCHAR *newStr = NULL;
1785 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1786 yaffs_strcpy(newStr,str);
1794 // Mknod (create) a new object.
1795 // equivalentObject only has meaning for a hard link;
1796 // aliasString only has meaning for a sumlink.
1797 // rdev only has meaning for devices (a subset of special objects)
1798 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1799 yaffs_Object *parent,
1804 yaffs_Object *equivalentObject,
1805 const YCHAR *aliasString,
1810 yaffs_Device *dev = parent->myDev;
1812 // Check if the entry exists. If it does then fail the call since we don't want a dup.
1813 if(yaffs_FindObjectByName(parent,name))
1818 in = yaffs_CreateNewObject(dev,-1,type);
1824 in->variantType = type;
1828 #ifdef CONFIG_YAFFS_WINCE
1829 yfsd_WinFileTimeNow(in->win_atime);
1830 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1831 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1834 #if defined(CONFIG_KERNEL_2_5)
1835 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME.tv_sec;
1837 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
1843 in->nDataChunks = 0;
1845 yaffs_SetObjectName(in,name);
1848 yaffs_AddObjectToDirectory(parent,in);
1850 in->myDev = parent->myDev;
1855 case YAFFS_OBJECT_TYPE_SYMLINK:
1856 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1858 case YAFFS_OBJECT_TYPE_HARDLINK:
1859 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1860 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1861 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1863 case YAFFS_OBJECT_TYPE_FILE: // do nothing
1864 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1865 case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1866 case YAFFS_OBJECT_TYPE_UNKNOWN:
1870 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1871 yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1873 // Could not create the object header, fail the creation
1874 yaffs_DestroyObject(in);
1883 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1885 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1888 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1890 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1893 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1895 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1898 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1900 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1903 // NB yaffs_Link returns the object id of the equivalent object.
1904 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1906 // Get the real object in case we were fed a hard link as an equivalent object
1907 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1909 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1911 return equivalentObject;
1921 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1928 newDir = obj->parent; // use the old directory
1931 // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1932 if(obj->myDev->isYaffs2)
1934 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1938 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1941 deleteOp = (newDir == obj->myDev->deletedDir);
1943 // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1944 // duplicate names are allowed.
1945 // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1949 !yaffs_FindObjectByName(newDir,newName)) &&
1950 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1952 yaffs_SetObjectName(obj,newName);
1955 yaffs_AddObjectToDirectory(newDir,obj);
1957 if(unlinkOp) obj->unlinked = 1;
1959 // If it is a dletion then we mark it as a shrink for gc purposes.
1960 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1971 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1976 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1977 // Special case for WinCE.
1978 // While look-up is case insensitive, the name isn't.
1979 // THerefore we might want to change x.txt to X.txt
1980 if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1986 obj = yaffs_FindObjectByName(oldDir,oldName);
1987 if(obj && obj->renameAllowed)
1989 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1996 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1998 // Scan the buckets and check that the lists
1999 // have as many members as the count says there are
2002 struct list_head *j;
2005 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2009 list_for_each(j,&dev->objectBucket[bucket].list)
2014 if(countEm != dev->objectBucket[bucket].count)
2016 T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2025 void yaffs_ObjectTest(yaffs_Device *dev)
2027 yaffs_Object *in[1000];
2029 yaffs_Object *inold[1000];
2033 memset(in,0,1000*sizeof(yaffs_Object *));
2034 memset(inold,0,1000*sizeof(yaffs_Object *));
2036 yaffs_CheckObjectHashSanity(dev);
2038 for(j = 0; j < 10; j++)
2042 for(i = 0; i < 1000; i++)
2044 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2047 YINFO("No more inodes");
2051 inNo[i] = in[i]->objectId;
2055 for(i = 0; i < 1000; i++)
2057 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2059 //T(("Differnce in look up test\n"));
2063 // T(("Look up ok\n"));
2067 yaffs_CheckObjectHashSanity(dev);
2069 for(i = 0; i < 1000; i+=3)
2071 yaffs_FreeObject(in[i]);
2076 yaffs_CheckObjectHashSanity(dev);
2083 /////////////////////////// Block Management and Page Allocation ///////////////////
2086 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2088 dev->allocationBlock = -1; // force it to get a new one
2089 //Todo we're assuming the malloc will pass.
2090 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2091 // Set up dynamic blockinfo stuff.
2092 dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2093 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2094 if(dev->blockInfo && dev->chunkBits)
2096 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2097 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2105 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2107 YFREE(dev->blockInfo);
2108 dev->blockInfo = NULL;
2109 YFREE(dev->chunkBits);
2110 dev->chunkBits = NULL;
2114 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2120 if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2122 if(!bi->hasShrinkHeader) return 1; // can gc
2125 // Find the oldest dirty sequence number if we don't know it and save it
2126 // so we don't have to keep recomputing it.
2127 if(!dev->oldestDirtySequence)
2129 seq = dev->sequenceNumber;
2131 for(i = dev->startBlock; i <= dev->endBlock; i++)
2133 b = yaffs_GetBlockInfo(dev,i);
2134 if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2135 (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2136 b->sequenceNumber < seq)
2138 seq = b->sequenceNumber;
2141 dev->oldestDirtySequence = seq;
2145 // Can't do gc of this block if there are any blocks older than this one that have
2147 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2154 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2155 // for garbage collection.
2160 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2163 int b = dev->currentDirtyChecker;
2169 yaffs_BlockInfo *bi;
2170 static int nonAggressiveSkip = 0;
2172 // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2174 // else (we're doing a leasurely gc), then we only bother to do this if the
2175 // block has only a few pages in use.
2178 nonAggressiveSkip--;
2180 if(!aggressive &&(nonAggressiveSkip > 0))
2185 pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2188 iterations = dev->endBlock - dev->startBlock + 1;
2192 iterations = dev->endBlock - dev->startBlock + 1;
2193 iterations = iterations / 16;
2194 if(iterations > 200)
2200 for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2203 if ( b < dev->startBlock || b > dev->endBlock)
2205 b = dev->startBlock;
2208 if(b < dev->startBlock || b > dev->endBlock)
2210 T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2214 bi = yaffs_GetBlockInfo(dev,b);
2216 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2217 (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2218 yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2221 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2225 dev->currentDirtyChecker = b;
2229 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2232 dev->oldestDirtySequence = 0; // clear this
2236 nonAggressiveSkip = 4;
2243 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2245 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2249 // If the block is still healthy erase it and mark as clean.
2250 // If the block has had a data failure, then retire it.
2251 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2253 if(!bi->needsRetiring)
2255 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2258 dev->nErasureFailures++;
2259 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2263 if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2266 for(i = 0; i < dev->nChunksPerBlock; i++)
2268 if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2270 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2278 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2279 dev->nErasedBlocks++;
2281 bi->softDeletions = 0;
2282 bi->hasShrinkHeader=0;
2283 yaffs_ClearChunkBits(dev,blockNo);
2285 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2289 yaffs_RetireBlock(dev,blockNo);
2290 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2294 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2297 yaffs_BlockInfo *bi;
2299 for(i= dev->startBlock; i <=dev->endBlock; i++)
2301 bi = yaffs_GetBlockInfo(dev,i);
2302 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2303 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));
2305 for(j = 0; j < dev->nChunksPerBlock; j++)
2307 if(yaffs_CheckChunkBit(dev,i,j))
2309 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2313 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2319 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2323 yaffs_BlockInfo *bi;
2328 if(j < 0 || j > 100)
2331 yaffs_DumpBlockStats(dev);
2336 if(dev->nErasedBlocks < 1)
2338 // Hoosterman we've got a problem.
2339 // Can't get space to gc
2340 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2345 // Find an empty block.
2347 for(i = dev->startBlock; i <= dev->endBlock; i++)
2349 dev->allocationBlockFinder++;
2350 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock)
2352 dev->allocationBlockFinder = dev->startBlock;
2355 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2357 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2359 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2360 dev->sequenceNumber++;
2361 bi->sequenceNumber = dev->sequenceNumber;
2362 dev->nErasedBlocks--;
2363 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq %d" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber));
2364 return dev->allocationBlockFinder;
2367 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been one" TENDSTR)));
2375 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2378 yaffs_BlockInfo *bi;
2380 if(dev->allocationBlock < 0)
2382 // Get next block to allocate off
2383 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2384 dev->allocationPage = 0;
2387 if(!useReserve && dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2389 // Not enough space to allocate unless we're allowed to use the reserve.
2393 if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2395 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));
2399 // Next page please....
2400 if(dev->allocationBlock >= 0)
2402 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2404 retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
2405 dev->allocationPage;
2407 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2409 dev->allocationPage++;
2413 // If the block is full set the state to full
2414 if(dev->allocationPage >= dev->nChunksPerBlock)
2416 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2417 dev->allocationBlock = -1;
2424 T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2430 // To determine if we have enough space we just look at the
2431 // number of erased blocks.
2432 // The cache is allowed to use reserved blocks.
2434 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2436 return (dev->nErasedBlocks >= dev->nReservedBlocks);
2440 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2444 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2446 if(dev->allocationBlock> 0)
2448 n += (dev->nChunksPerBlock - dev->allocationPage);
2455 int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2461 int retVal = YAFFS_OK;
2465 int chunksBefore = yaffs_GetErasedChunks(dev);
2468 yaffs_ExtendedTags tags;
2470 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2472 yaffs_Object *object;
2474 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2476 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2477 //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
2479 bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2482 if(!yaffs_StillSomeChunkBits(dev,block))
2484 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2485 yaffs_BlockBecameDirty(dev,block);
2490 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2492 for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock;
2493 chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2494 chunkInBlock++, oldChunk++ )
2496 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2499 // This page is in use and might need to be copied off
2503 //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2505 yaffs_InitialiseTags(&tags);
2507 yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2509 object = yaffs_FindObjectByNumber(dev,tags.objectId);
2511 T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2515 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2518 if(object && object->deleted && tags.chunkId != 0)
2520 // Data chunk in a deleted file, throw it away
2521 // It's a deleted data chunk,
2522 // No need to copy this, just forget about it and fix up the
2525 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0);
2526 object->nDataChunks--;
2528 if(object->nDataChunks <= 0)
2530 // remeber to clean up the object
2531 dev->gcCleanupList[cleanups] = tags.objectId;
2536 else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2538 // Deleted object header with no data chunks.
2539 // Can be discarded and the file deleted.
2540 object->chunkId = 0;
2541 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2542 object->variant.fileVariant.top = NULL;
2543 yaffs_DoGenericObjectDeletion(object);
2548 // It's either a data chunk in a live file or
2549 // an ObjectHeader, so we're interested in it.
2550 // NB Need to keep the ObjectHeaders of deleted files
2551 // until the whole file has been deleted off
2552 tags.serialNumber++;
2556 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2560 retVal = YAFFS_FAIL;
2565 // Ok, now fix up the Tnodes etc.
2567 if(tags.chunkId == 0)
2570 object->chunkId = newChunk;
2571 object->serial = tags.serialNumber;
2575 // It's a data chunk
2576 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2581 yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);
2586 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2589 // Do any required cleanups
2590 for(i = 0; i < cleanups; i++)
2592 // Time to delete the file too
2593 object = yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2596 yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2597 object->variant.fileVariant.top = NULL;
2598 T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2599 yaffs_DoGenericObjectDeletion(object);
2606 if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2608 T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2615 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2617 // find a file to delete
2618 struct list_head *i;
2622 //Scan the unlinked files looking for one to delete
2623 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2627 l = list_entry(i, yaffs_Object,siblings);
2638 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2640 // This does background deletion on unlinked files.. only deleted ones.
2641 // If we don't have a file we're working on then find one
2642 if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2644 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2647 // OK, we're working on a file...
2648 if(dev->unlinkedDeletion)
2650 yaffs_Object *obj = dev->unlinkedDeletion;
2652 int limit; // Number of chunks to delete in a file.
2653 // NB this can be exceeded, but not by much.
2657 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2659 if(obj->nDataChunks == 0)
2661 // Done all the deleting of data chunks.
2662 // Now dump the header and clean up
2663 yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2664 obj->variant.fileVariant.top = NULL;
2665 yaffs_DoGenericObjectDeletion(obj);
2666 dev->nDeletedFiles--;
2667 dev->nUnlinkedFiles--;
2668 dev->nBackgroundDeletions++;
2669 dev->unlinkedDeletion = NULL;
2677 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2678 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2683 //yaffs_DoUnlinkedFileDeletion(dev);
2685 if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2692 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2696 dev->garbageCollections++;
2697 return yaffs_GarbageCollectBlock(dev,block);
2709 // New garbage collector
2710 // If we're very low on erased blocks then we do aggressive garbage collection
2711 // otherwise we do "leasurely" garbage collection.
2712 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2713 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2715 // The idea is to help clear out space in a more spread-out manner.
2716 // Dunno if it really does anything useful.
2718 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2725 //yaffs_DoUnlinkedFileDeletion(dev);
2727 // This loop should pass the first time.
2728 // We'll only see looping here if the erase of the collected block fails.
2732 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2734 // We need a block soon...
2739 // We're in no hurry
2743 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2747 dev->garbageCollections++;
2750 dev->passiveGarbageCollections++;
2753 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2755 gcOk = yaffs_GarbageCollectBlock(dev,block);
2758 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1))
2760 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2762 } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2764 return aggressive ? gcOk: YAFFS_OK;
2768 //////////////////////////// TAGS ///////////////////////////////////////
2771 void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
2773 memset(tags,0,sizeof(yaffs_ExtendedTags));
2774 tags->validMarker0 = 0xAAAAAAAA;
2775 tags->validMarker1 = 0x55555555;
2778 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
2780 return (tags->validMarker0 == 0xAAAAAAAA && tags->validMarker1 == 0x55555555);
2787 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2791 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2798 for(i = 0; i < 8; i++)
2800 for(j = 1; j &0xff; j<<=1)
2815 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
2817 unsigned ecc = tags->ecc;
2819 yaffs_CalcTagsECC(tags);
2823 if(ecc && ecc <= 64)
2825 // TODO: Handle the failure better. Retire?
2826 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2830 b[ecc / 8] ^= (1 << (ecc & 7));
2832 // Now recvalc the ecc
2833 yaffs_CalcTagsECC(tags);
2835 return 1; // recovered error
2839 // Wierd ecc failure value
2840 // TODO Need to do somethiong here
2841 return -1; //unrecovered error
2847 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2849 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2851 yaffs_CalcTagsECC(tagsPtr);
2853 sparePtr->tagByte0 = tu->asBytes[0];
2854 sparePtr->tagByte1 = tu->asBytes[1];
2855 sparePtr->tagByte2 = tu->asBytes[2];
2856 sparePtr->tagByte3 = tu->asBytes[3];
2857 sparePtr->tagByte4 = tu->asBytes[4];
2858 sparePtr->tagByte5 = tu->asBytes[5];
2859 sparePtr->tagByte6 = tu->asBytes[6];
2860 sparePtr->tagByte7 = tu->asBytes[7];
2863 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2865 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2868 tu->asBytes[0]= sparePtr->tagByte0;
2869 tu->asBytes[1]= sparePtr->tagByte1;
2870 tu->asBytes[2]= sparePtr->tagByte2;
2871 tu->asBytes[3]= sparePtr->tagByte3;
2872 tu->asBytes[4]= sparePtr->tagByte4;
2873 tu->asBytes[5]= sparePtr->tagByte5;
2874 tu->asBytes[6]= sparePtr->tagByte6;
2875 tu->asBytes[7]= sparePtr->tagByte7;
2877 result = yaffs_CheckECCOnTags(tagsPtr);
2880 dev->tagsEccFixed++;
2884 dev->tagsEccUnfixed++;
2888 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2890 memset(spare,0xFF,sizeof(yaffs_Spare));
2896 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2898 // NB There must be tags, data is optional
2899 // If there is data, then an ECC is calculated on it.
2908 //yaffs_SpareInitialise(&spare);
2910 //if(!dev->useNANDECC && buffer)
2912 // yaffs_CalcECC(buffer,&spare);
2915 //yaffs_LoadTagsIntoSpare(&spare,tags);
2917 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2922 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2924 return ( tags->chunkId == chunkInObject &&
2925 tags->objectId == objectId &&
2926 !tags->chunkDeleted) ? 1 : 0;
2932 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2934 //Get the Tnode, then get the level 0 offset chunk offset
2937 yaffs_ExtendedTags localTags;
2940 yaffs_Device *dev = in->myDev;
2945 // Passed a NULL, so use our own tags space
2949 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2953 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2955 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2960 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2962 //Get the Tnode, then get the level 0 offset chunk offset
2965 yaffs_ExtendedTags localTags;
2967 yaffs_Device *dev = in->myDev;
2972 // Passed a NULL, so use our own tags space
2976 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2981 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2983 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2985 // Delete the entry in the filestructure (if found)
2988 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2993 //T(("No level 0 found for %d\n", chunkInInode));
2998 //T(("Could not find %d to delete\n",chunkInInode));
3004 #ifdef YAFFS_PARANOID
3006 static int yaffs_CheckFileSanity(yaffs_Object *in)
3014 yaffs_Tags localTags;
3015 yaffs_Tags *tags = &localTags;
3020 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3022 //T(("Object not a file\n"));
3026 objId = in->objectId;
3027 fSize = in->variant.fileVariant.fileSize;
3028 nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3030 for(chunk = 1; chunk <= nChunks; chunk++)
3032 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3037 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3039 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3043 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3044 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3052 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
3053 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
3062 //T(("No level 0 found for %d\n", chunk));
3066 return failed ? YAFFS_FAIL : YAFFS_OK;
3071 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3074 yaffs_Device *dev = in->myDev;
3076 yaffs_ExtendedTags existingTags;
3077 yaffs_ExtendedTags newTags;
3078 unsigned existingSerial, newSerial;
3080 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3086 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
3090 // If we're scanning then we need to test for duplicates
3091 // NB This does not need to be efficient since it should only ever
3092 // happen when the power fails during a write, then only one
3093 // chunk should ever be affected.
3096 if(existingChunk != 0)
3098 // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3099 // thus we have to do a FindChunkInFile to get the real chunk id.
3101 // We have a duplicate now we need to decide which one to use
3102 // To do this we get both sets of tags and compare serial numbers.
3103 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3107 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3109 if(existingChunk <=0)
3111 //Hoosterman - how did this happen?
3113 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3118 // NB The deleted flags should be false, otherwise the chunks will
3119 // not be loaded during a scan
3121 newSerial = newTags.serialNumber;
3122 existingSerial = existingTags.serialNumber;
3124 if( in->myDev->isYaffs2 ||
3125 existingChunk <= 0 ||
3126 ((existingSerial+1) & 3) == newSerial)
3129 // Delete the old one and drop through to update the tnode
3130 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3135 // Delete the new one and return early so that the tnode isn't changed
3136 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3143 if(existingChunk == 0)
3148 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3155 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3157 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3159 if(chunkInNAND >= 0)
3161 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3165 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3167 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3174 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3178 yaffs_ExtendedTags tags;
3179 yaffs_BlockInfo *bi;
3181 if(chunkId <= 0) return;
3184 block = chunkId / dev->nChunksPerBlock;
3185 page = chunkId % dev->nChunksPerBlock;
3187 bi = yaffs_GetBlockInfo(dev,block);
3189 T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3192 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3195 // yaffs_SpareInitialise(&spare);
3197 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3199 //read data before write, to ensure correct ecc
3200 //if we're using MTD verification under Linux
3201 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3204 yaffs_InitialiseTags(&tags);
3206 tags.chunkDeleted = 1;
3209 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3210 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3214 dev->nUnmarkedDeletions++;
3218 // Pull out of the management area.
3219 // If the whole block became dirty, this will kick off an erasure.
3220 if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3221 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3222 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3223 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3227 yaffs_ClearChunkBit(dev,block,page);
3230 if(bi->pagesInUse == 0 &&
3231 !bi->hasShrinkHeader &&
3232 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3233 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3235 yaffs_BlockBecameDirty(dev,block);
3241 // T(("Bad news deleting chunk %d\n",chunkId));
3249 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3251 // Find old chunk Need to do this to get serial number
3252 // Write new one and patch into tree.
3253 // Invalidate old tags.
3256 yaffs_ExtendedTags prevTags;
3259 yaffs_ExtendedTags newTags;
3261 yaffs_Device *dev = in->myDev;
3263 yaffs_CheckGarbageCollection(dev);
3265 // Get the previous chunk at this location in the file if it exists
3266 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3269 yaffs_InitialiseTags(&newTags);
3271 newTags.chunkId = chunkInInode;
3272 newTags.objectId = in->objectId;
3273 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3274 newTags.byteCount = nBytes;
3276 // yaffs_CalcTagsECC(&newTags);
3278 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3282 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3285 if(prevChunkId >= 0)
3287 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3291 yaffs_CheckFileSanity(in);
3302 // UpdateObjectHeader updates the header on NAND for an object.
3303 // If name is not NULL, then that new name is used.
3305 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3308 yaffs_BlockInfo *bi;
3310 yaffs_Device *dev = in->myDev;
3316 yaffs_ExtendedTags newTags;
3318 __u8 *buffer = NULL;
3319 YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3321 // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3323 yaffs_ObjectHeader *oh = NULL;
3324 // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3327 if(!in->fake || force)
3330 yaffs_CheckGarbageCollection(dev);
3332 buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3333 oh = (yaffs_ObjectHeader *)buffer;
3335 prevChunkId = in->chunkId;
3337 if(prevChunkId >= 0)
3339 yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3340 memcpy(oldName,oh->name,sizeof(oh->name));
3343 memset(buffer,0xFF,dev->nBytesPerChunk);
3346 oh->type = in->variantType;
3348 oh->st_mode = in->st_mode;
3350 #ifdef CONFIG_YAFFS_WINCE
3351 oh->win_atime[0] = in->win_atime[0];
3352 oh->win_ctime[0] = in->win_ctime[0];
3353 oh->win_mtime[0] = in->win_mtime[0];
3354 oh->win_atime[1] = in->win_atime[1];
3355 oh->win_ctime[1] = in->win_ctime[1];
3356 oh->win_mtime[1] = in->win_mtime[1];
3358 oh->st_uid = in->st_uid;
3359 oh->st_gid = in->st_gid;
3360 oh->st_atime = in->st_atime;
3361 oh->st_mtime = in->st_mtime;
3362 oh->st_ctime = in->st_ctime;
3363 oh->st_rdev = in->st_rdev;
3367 oh->parentObjectId = in->parent->objectId;
3371 oh->parentObjectId = 0;
3374 //oh->sum = in->sum;
3377 memset(oh->name,0,sizeof(oh->name));
3378 yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3380 else if(prevChunkId)
3382 memcpy(oh->name, oldName,sizeof(oh->name));
3386 memset(oh->name,0,sizeof(oh->name));
3389 oh->isShrink = isShrink;
3391 switch(in->variantType)
3393 case YAFFS_OBJECT_TYPE_UNKNOWN:
3394 // Should not happen
3396 case YAFFS_OBJECT_TYPE_FILE:
3397 oh->fileSize = in->variant.fileVariant.fileSize;
3399 case YAFFS_OBJECT_TYPE_HARDLINK:
3400 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3402 case YAFFS_OBJECT_TYPE_SPECIAL:
3405 case YAFFS_OBJECT_TYPE_DIRECTORY:
3408 case YAFFS_OBJECT_TYPE_SYMLINK:
3409 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3410 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3415 yaffs_InitialiseTags(&newTags);
3417 newTags.chunkId = 0;
3418 newTags.objectId = in->objectId;
3419 newTags.serialNumber = in->serial;
3422 // Create new chunk in NAND
3423 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3428 in->chunkId = newChunkId;
3430 if(prevChunkId >= 0)
3432 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3437 // If this was a shrink, then mark the block that the chunk lives on
3440 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3441 bi->hasShrinkHeader = 1;
3446 retVal = newChunkId;
3451 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3457 /////////////////////// Short Operations Cache ////////////////////////////////
3458 // In many siturations where there is no high level buffering (eg WinCE) a lot of
3459 // reads might be short sequential reads, and a lot of writes may be short
3460 // sequential writes. eg. scanning/writing a jpeg file.
3461 // In these cases, a short read/write cache can provide a huge perfomance benefit
3462 // with dumb-as-a-rock code.
3463 // There are a limited number (~10) of cache chunks per device so that we don't
3464 // need a very intelligent search.
3470 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3472 yaffs_Device *dev = obj->myDev;
3475 yaffs_ChunkCache *cache;
3478 int nCaches = obj->myDev->nShortOpCaches;
3485 // Find the dirty cache for this object with the lowest chunk id.
3486 for(i = 0; i < nCaches; i++)
3488 if(dev->srCache[i].object == obj &&
3489 dev->srCache[i].dirty)
3491 if(!cache || dev->srCache[i].chunkId < lowest)
3493 cache = &dev->srCache[i];
3494 lowest = cache->chunkId;
3499 if(cache && !cache->locked)
3501 //Write it out and free it up
3504 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3506 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3508 nBytes= YAFFS_BYTES_PER_CHUNK;
3511 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3517 cache->object = NULL;
3520 } while(cache && chunkWritten > 0);
3524 //Hoosterman, disk full while writing cache out.
3525 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3533 // Grab us a chunk for use.
3534 // First look for an empty one.
3535 // Then look for the least recently used non-dirty one.
3536 // Then look for the least recently used dirty one...., flush and look again.
3537 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3543 if(dev->nShortOpCaches > 0)
3545 for(i = 0; i < dev->nShortOpCaches; i++)
3547 if(!dev->srCache[i].object)
3549 //T(("Grabbing empty %d\n",i));
3551 //printf("Grabbing empty %d\n",i);
3553 return &dev->srCache[i];
3560 usage = 0; // just to stop the compiler grizzling
3562 for(i = 0; i < dev->nShortOpCaches; i++)
3564 if(!dev->srCache[i].dirty &&
3565 ((dev->srCache[i].lastUse < usage && theOne >= 0)||
3568 usage = dev->srCache[i].lastUse;
3573 //T(("Grabbing non-empty %d\n",theOne));
3575 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3577 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
3587 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3589 yaffs_ChunkCache *cache;
3590 yaffs_Object *theObj;
3595 if(dev->nShortOpCaches > 0)
3597 // Try find a non-dirty one...
3599 cache = yaffs_GrabChunkCacheWorker(dev);
3603 // They were all dirty, find the last recently used object and flush
3604 // its cache, then find again.
3605 // NB what's here is not very accurate, we actually flush the object
3606 // the last recently used page.
3613 for(i = 0; i < dev->nShortOpCaches; i++)
3615 if( dev->srCache[i].object &&
3616 !dev->srCache[i].locked &&
3617 (dev->srCache[i].lastUse < usage || !cache))
3619 usage = dev->srCache[i].lastUse;
3620 theObj = dev->srCache[i].object;
3621 cache = &dev->srCache[i];
3626 if(!cache || cache->dirty)
3630 yaffs_FlushFilesChunkCache(theObj);
3633 cache = yaffs_GrabChunkCacheWorker(dev);
3637 //printf(" pushout %d\n",pushout);
3650 // Find a cached chunk
3651 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3653 yaffs_Device *dev = obj->myDev;
3655 if(dev->nShortOpCaches > 0)
3657 for(i = 0; i < dev->nShortOpCaches; i++)
3659 if(dev->srCache[i].object == obj &&
3660 dev->srCache[i].chunkId == chunkId)
3664 return &dev->srCache[i];
3671 // Mark the chunk for the least recently used algorithym
3672 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3675 if(dev->nShortOpCaches > 0)
3677 if( dev->srLastUse < 0 ||
3678 dev->srLastUse > 100000000)
3680 // Reset the cache usages
3682 for(i = 1; i < dev->nShortOpCaches; i++)
3684 dev->srCache[i].lastUse = 0;
3691 cache->lastUse = dev->srLastUse;
3700 // Invalidate a single cache page.
3701 // Do this when a whole page gets written,
3702 // ie the short cache for this page is no longer valid.
3703 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3705 if(object->myDev->nShortOpCaches > 0)
3707 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3711 cache->object = NULL;
3717 // Invalidate all the cache pages associated with this object
3718 // Do this whenever ther file is deleted or resized.
3719 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3722 yaffs_Device *dev = in->myDev;
3724 if(dev->nShortOpCaches > 0)
3726 // Now invalidate it.
3727 for(i = 0; i < dev->nShortOpCaches; i++)
3729 if(dev->srCache[i].object == in)
3731 dev->srCache[i].object = NULL;
3741 ///////////////////////// File read/write ///////////////////////////////
3742 // Read and write have very similar structures.
3743 // In general the read/write has three parts to it
3744 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3745 // * Some complete chunks
3746 // * An incomplete chunk to end off with
3748 // Curve-balls: the first chunk might also be the last chunk.
3750 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3759 yaffs_ChunkCache *cache;
3767 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3768 start = offset % dev->nBytesPerChunk;
3770 // OK now check for the curveball where the start and end are in
3772 if( (start + n) < dev->nBytesPerChunk)
3778 nToCopy = dev->nBytesPerChunk - start;
3781 cache = yaffs_FindChunkCache(in,chunk);
3783 // If the chunk is already in the cache or it is less than a whole chunk
3784 // then use the cache (if there is caching)
3785 // else bypass the cache.
3786 if( cache || nToCopy != dev->nBytesPerChunk)
3788 if(dev->nShortOpCaches > 0)
3791 // If we can't find the data in the cache, then load it up.
3795 cache = yaffs_GrabChunkCache(in->myDev);
3797 cache->chunkId = chunk;
3800 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3804 yaffs_UseChunkCache(dev,cache,0);
3808 #ifdef CONFIG_YAFFS_WINCE
3809 yfsd_UnlockYAFFS(TRUE);
3811 memcpy(buffer,&cache->data[start],nToCopy);
3813 #ifdef CONFIG_YAFFS_WINCE
3814 yfsd_LockYAFFS(TRUE);
3820 // Read into the local buffer then copy...
3822 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3823 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3824 #ifdef CONFIG_YAFFS_WINCE
3825 yfsd_UnlockYAFFS(TRUE);
3827 memcpy(buffer,&localBuffer[start],nToCopy);
3829 #ifdef CONFIG_YAFFS_WINCE
3830 yfsd_LockYAFFS(TRUE);
3832 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3838 #ifdef CONFIG_YAFFS_WINCE
3839 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3841 // Under WinCE can't do direct transfer. Need to use a local buffer.
3842 // This is because we otherwise screw up WinCE's memory mapper
3843 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3845 #ifdef CONFIG_YAFFS_WINCE
3846 yfsd_UnlockYAFFS(TRUE);
3848 memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3850 #ifdef CONFIG_YAFFS_WINCE
3851 yfsd_LockYAFFS(TRUE);
3852 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3856 // A full chunk. Read directly into the supplied buffer.
3857 yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3873 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3882 int startOfWrite = offset;
3883 int chunkWritten = 0;
3891 while(n > 0 && chunkWritten >= 0)
3893 chunk = offset / dev->nBytesPerChunk + 1;
3894 start = offset % dev->nBytesPerChunk;
3897 // OK now check for the curveball where the start and end are in
3900 if( (start + n) < dev->nBytesPerChunk)
3904 // Now folks, to calculate how many bytes to write back....
3905 // If we're overwriting and not writing to then end of file then
3906 // we need to write back as much as was there before.
3908 nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3910 if(nBytesRead > dev->nBytesPerChunk)
3912 nBytesRead = dev->nBytesPerChunk;
3915 nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3920 nToCopy = dev->nBytesPerChunk - start;
3921 nToWriteBack = dev->nBytesPerChunk;
3924 if(nToCopy != dev->nBytesPerChunk)
3926 // An incomplete start or end chunk (or maybe both start and end chunk)
3927 if(dev->nShortOpCaches > 0)
3929 yaffs_ChunkCache *cache;
3930 // If we can't find the data in the cache, then load it up.
3931 cache = yaffs_FindChunkCache(in,chunk);
3932 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3934 cache = yaffs_GrabChunkCache(in->myDev);
3936 cache->chunkId = chunk;
3939 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3944 yaffs_UseChunkCache(dev,cache,1);
3946 #ifdef CONFIG_YAFFS_WINCE
3947 yfsd_UnlockYAFFS(TRUE);
3950 memcpy(&cache->data[start],buffer,nToCopy);
3952 #ifdef CONFIG_YAFFS_WINCE
3953 yfsd_LockYAFFS(TRUE);
3956 cache->nBytes = nToWriteBack;
3960 chunkWritten = -1; // fail the write
3965 // An incomplete start or end chunk (or maybe both start and end chunk)
3966 // Read into the local buffer then copy, then copy over and write back.
3968 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3970 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3972 #ifdef CONFIG_YAFFS_WINCE
3973 yfsd_UnlockYAFFS(TRUE);
3976 memcpy(&localBuffer[start],buffer,nToCopy);
3978 #ifdef CONFIG_YAFFS_WINCE
3979 yfsd_LockYAFFS(TRUE);
3981 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
3983 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3985 //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3992 #ifdef CONFIG_YAFFS_WINCE
3993 // Under WinCE can't do direct transfer. Need to use a local buffer.
3994 // This is because we otherwise screw up WinCE's memory mapper
3995 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3996 #ifdef CONFIG_YAFFS_WINCE
3997 yfsd_UnlockYAFFS(TRUE);
3999 memcpy(localBuffer,buffer,dev->nBytesPerChunk);
4000 #ifdef CONFIG_YAFFS_WINCE
4001 yfsd_LockYAFFS(TRUE);
4003 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
4004 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4006 // A full chunk. Write directly from the supplied buffer.
4007 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
4009 // Since we've overwritten the cached data, we better invalidate it.
4010 yaffs_InvalidateChunkCache(in,chunk);
4011 //T(("Write to chunk %d %d\n",chunk,chunkWritten));
4014 if(chunkWritten >= 0)
4024 // Update file object
4026 if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
4028 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4036 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
4039 yaffs_Device *dev = in->myDev;
4040 int oldFileSize = in->variant.fileVariant.fileSize;
4043 int lastDel = 1 + (oldFileSize-1)/dev->nBytesPerChunk;
4045 int startDel = 1 + (newSize + dev->nBytesPerChunk - 1)/
4046 dev->nBytesPerChunk;
4050 // Delete backwards so that we don't end up with holes if
4051 // power is lost part-way through the operation.
4052 for(i = lastDel; i >= startDel; i--)
4054 // NB this could be optimised somewhat,
4055 // eg. could retrieve the tags and write them without
4056 // using yaffs_DeleteChunk
4058 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
4061 if(chunkId < (dev->startBlock * dev->nChunksPerBlock) ||
4062 chunkId >= ((dev->endBlock+1) * dev->nChunksPerBlock))
4064 T(YAFFS_TRACE_ALWAYS,("Found daft chunkId %d for %d\n",chunkId,i));
4069 yaffs_DeleteChunk(dev,chunkId,1,__LINE__);
4076 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
4079 int oldFileSize = in->variant.fileVariant.fileSize;
4080 int sizeOfPartialChunk;
4081 yaffs_Device *dev = in->myDev;
4083 sizeOfPartialChunk = newSize % dev->nBytesPerChunk;
4086 yaffs_FlushFilesChunkCache(in);
4087 yaffs_InvalidateWholeChunkCache(in);
4089 yaffs_CheckGarbageCollection(dev);
4091 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
4093 return yaffs_GetFileSize(in);
4096 if(newSize < oldFileSize)
4099 yaffs_PruneResizedChunks(in,newSize);
4101 if(sizeOfPartialChunk != 0)
4103 int lastChunk = 1+ newSize/dev->nBytesPerChunk;
4104 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4106 // Got to read and rewrite the last chunk with its new size and zero pad
4107 yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
4109 memset(localBuffer + sizeOfPartialChunk,0, dev->nBytesPerChunk - sizeOfPartialChunk);
4111 yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
4113 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4116 in->variant.fileVariant.fileSize = newSize;
4118 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
4120 // TODO write a new object header to show we've shrunk the file
4121 // Do this only if the file is not in the deleted directory.
4122 if(in->parent->objectId != YAFFS_OBJECTID_UNLINKED)
4124 yaffs_UpdateObjectHeader(in,NULL, 0, 1);
4138 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4140 obj = yaffs_GetEquivalentObject(obj);
4142 switch(obj->variantType)
4144 case YAFFS_OBJECT_TYPE_FILE:
4145 return obj->variant.fileVariant.fileSize;
4146 case YAFFS_OBJECT_TYPE_SYMLINK:
4147 return yaffs_strlen(obj->variant.symLinkVariant.alias);
4155 // yaffs_FlushFile() updates the file's
4158 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
4163 //T(("flushing object header\n"));
4165 yaffs_FlushFilesChunkCache(in);
4168 #ifdef CONFIG_YAFFS_WINCE
4169 yfsd_WinFileTimeNow(in->win_mtime);
4171 #if defined(CONFIG_KERNEL_2_5)
4172 in->st_mtime = CURRENT_TIME.tv_sec;
4174 in->st_mtime = CURRENT_TIME;
4179 retVal = (yaffs_UpdateObjectHeader(in,NULL,0,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
4191 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4194 // First off, invalidate the file's data in the cache, without flushing.
4195 yaffs_InvalidateWholeChunkCache(in);
4197 if(in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir))
4199 // Move to the unlinked directory so we have a record that it was deleted.
4200 yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4205 yaffs_RemoveObjectFromDirectory(in);
4206 yaffs_DeleteChunk(in->myDev,in->chunkId,1,__LINE__);
4212 in->myInode->u.generic_ip = NULL;
4216 yaffs_FreeObject(in);
4221 // yaffs_DeleteFile deletes the whole file data
4222 // and the inode associated with the file.
4223 // It does not delete the links associated with the file.
4224 static int yaffs_UnlinkFile(yaffs_Object *in)
4227 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
4229 // Delete the file data & tnodes
4231 yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
4234 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4236 return yaffs_DoGenericObjectDeletion(in);
4239 int immediateDeletion=0;
4244 //in->myDev->nUnlinkedFiles++;
4245 //in->renameAllowed = 0;
4249 immediateDeletion = 1;
4255 immediateDeletion = 1;
4259 if(immediateDeletion)
4261 retVal = yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4262 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
4264 in->myDev->nDeletedFiles++;
4265 yaffs_SoftDeleteFile(in);
4269 retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
4279 int yaffs_DeleteFile(yaffs_Object *in)
4281 int retVal = YAFFS_OK;
4283 if(in->nDataChunks > 0)
4285 // Use soft deletion
4288 retVal = yaffs_UnlinkFile(in);
4290 if(retVal == YAFFS_OK &&
4295 in->myDev->nDeletedFiles++;
4296 yaffs_SoftDeleteFile(in);
4298 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
4302 // The file has no data chunks so we toss it immediately
4303 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4304 in->variant.fileVariant.top = NULL;
4305 yaffs_DoGenericObjectDeletion(in);
4311 static int yaffs_DeleteDirectory(yaffs_Object *in)
4313 //First check that the directory is empty.
4314 if(list_empty(&in->variant.directoryVariant.children))
4316 return yaffs_DoGenericObjectDeletion(in);
4323 static int yaffs_DeleteSymLink(yaffs_Object *in)
4325 YFREE(in->variant.symLinkVariant.alias);
4327 return yaffs_DoGenericObjectDeletion(in);
4330 static int yaffs_DeleteHardLink(yaffs_Object *in)
4332 // remove this hardlink from the list assocaited with the equivalent
4334 list_del(&in->hardLinks);
4335 return yaffs_DoGenericObjectDeletion(in);
4339 static void yaffs_DestroyObject(yaffs_Object *obj)
4341 switch(obj->variantType)
4343 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
4344 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
4345 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
4346 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
4347 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
4348 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
4353 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4357 if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4359 return yaffs_DeleteHardLink(obj);
4361 else if(!list_empty(&obj->hardLinks))
4363 // Curve ball: We're unlinking an object that has a hardlink.
4365 // This problem arises because we are not strictly following
4366 // The Linux link/inode model.
4368 // We can't really delete the object.
4369 // Instead, we do the following:
4370 // - Select a hardlink.
4371 // - Unhook it from the hard links
4372 // - Unhook it from its parent directory (so that the rename can work)
4373 // - Rename the object to the hardlink's name.
4374 // - Delete the hardlink
4379 YCHAR name[YAFFS_MAX_NAME_LENGTH+1];
4381 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
4383 list_del_init(&hl->hardLinks);
4384 list_del_init(&hl->siblings);
4386 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
4388 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
4390 if(retVal == YAFFS_OK)
4392 retVal = yaffs_DoGenericObjectDeletion(hl);
4399 switch(obj->variantType)
4401 case YAFFS_OBJECT_TYPE_FILE:
4402 return yaffs_UnlinkFile(obj);
4404 case YAFFS_OBJECT_TYPE_DIRECTORY:
4405 return yaffs_DeleteDirectory(obj);
4407 case YAFFS_OBJECT_TYPE_SYMLINK:
4408 return yaffs_DeleteSymLink(obj);
4410 case YAFFS_OBJECT_TYPE_HARDLINK:
4411 case YAFFS_OBJECT_TYPE_UNKNOWN:
4418 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4422 obj = yaffs_FindObjectByName(dir,name);
4424 if(obj && obj->unlinkAllowed)
4426 return yaffs_UnlinkWorker(obj);
4433 //////////////// Initialisation Scanning /////////////////
4437 // For now we use the SmartMedia check.
4438 // We look at the blockStatus byte in the first two chunks
4439 // These must be 0xFF to pass as OK.
4440 // todo: this function needs to be modifyable foir different NAND types
4441 // and different chunk sizes. Suggest make this into a per-device configurable
4443 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4445 yaffsExtendedTags *tags;
4447 yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&tags,1);
4449 if(yaffs_CountBits(spare.blockStatus) < 7)
4454 if(spare.blockStatus != 0xFF)
4459 yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4462 if(yaffs_CountBits(spare.blockStatus) < 7)
4467 if(spare.blockStatus != 0xFF)
4488 static int yaffs_Scan(yaffs_Device *dev)
4490 yaffs_ExtendedTags tags;
4495 int nBlocksToScan = 0;
4500 yaffs_BlockState state;
4501 yaffs_Object *hardList = NULL;
4503 yaffs_BlockInfo *bi;
4505 yaffs_ObjectHeader *oh;
4507 yaffs_Object *parent;
4508 int nBlocks = dev->endBlock - dev->startBlock + 1;
4512 yaffs_BlockIndex *blockIndex = NULL;
4514 chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4517 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4521 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
4525 // Scan all the blocks to determine their state
4526 for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
4528 bi = yaffs_GetBlockInfo(dev,blk);
4529 yaffs_ClearChunkBits(dev,blk);
4531 bi->softDeletions = 0;
4533 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4535 bi->blockState = state;
4536 bi->sequenceNumber = sequenceNumber;
4538 if(state == YAFFS_BLOCK_STATE_DEAD)
4540 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4542 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4544 dev->nErasedBlocks++;
4545 dev->nFreeChunks += dev->nChunksPerBlock;
4547 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4550 // Determine the highest sequence number
4551 if( dev->isYaffs2 &&
4552 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
4553 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
4556 blockIndex[nBlocksToScan].seq = sequenceNumber;
4557 blockIndex[nBlocksToScan].block = blk;
4561 if(sequenceNumber >= dev->sequenceNumber)
4563 dev->sequenceNumber = sequenceNumber;
4566 else if(dev->isYaffs2)
4568 // TODO: Nasty sequence number!
4569 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
4576 // Dungy old bubble sort for now...
4579 yaffs_BlockIndex temp;
4583 for(i = 0; i < nBlocksToScan; i++)
4584 for(j = i+1; j < nBlocksToScan; j++)
4585 if(blockIndex[i].seq > blockIndex[j].seq)
4587 temp = blockIndex[j];
4588 blockIndex[j] = blockIndex[i];
4589 blockIndex[i] = temp;
4594 // Now scan the blocks looking at the data.
4598 endIterator = nBlocksToScan-1;
4602 startIterator = dev->startBlock;
4603 endIterator = dev->endBlock;
4606 for(blockIterator = startIterator; blockIterator <= endIterator; blockIterator++)
4611 // get the block to scan in the correct order
4612 blk = blockIndex[blockIterator].block;
4616 blk = blockIterator;
4620 bi = yaffs_GetBlockInfo(dev,blk);
4621 state = bi->blockState;
4625 for(c = 0; c < dev->nChunksPerBlock &&
4626 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++)
4628 // Read the tags and decide what to do
4629 chunk = blk * dev->nChunksPerBlock + c;
4631 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
4633 // Let's have a good look at this chunk...
4636 if(!dev->isYaffs2 && tags.chunkDeleted)
4640 dev->nFreeChunks ++;
4641 //T((" %d %d deleted\n",blk,c));
4643 else if(!tags.chunkUsed)
4645 // An unassigned chunk in the block
4646 // This means that either the block is empty or
4647 // this is the one being allocated from
4651 // We're looking at the first chunk in the block so the block is unused
4652 state = YAFFS_BLOCK_STATE_EMPTY;
4653 dev->nErasedBlocks++;
4657 // this is the block being allocated from
4658 T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4659 state = YAFFS_BLOCK_STATE_ALLOCATING;
4660 dev->allocationBlock = blk;
4661 dev->allocationPage = c;
4662 dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
4663 // go forth from here.
4664 //Yaffs2 sanity check:
4665 // This should be the one with the highest sequence number
4666 if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
4668 T(YAFFS_TRACE_ALWAYS,
4669 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
4670 blk,bi->sequenceNumber,dev->sequenceNumber));
4674 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4676 else if(tags.chunkId > 0)
4680 yaffs_SetChunkBit(dev,blk,c);
4683 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4684 // PutChunkIntoFIle checks for a clash (two data chunks with
4685 // the same chunkId).
4686 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4687 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
4688 if(in->variant.fileVariant.scannedFileSize <endpos)
4690 in->variant.fileVariant.scannedFileSize = endpos;
4691 if(!dev->useHeaderFileSize)
4693 in->variant.fileVariant.fileSize =
4694 in->variant.fileVariant.scannedFileSize;
4698 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
4702 // chunkId == 0, so it is an ObjectHeader.
4703 // Thus, we read in the object header and make the object
4704 yaffs_SetChunkBit(dev,blk,c);
4707 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
4709 oh = (yaffs_ObjectHeader *)chunkData;
4711 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4715 // We have already filled this one. We have a duplicate and need to resolve it.
4717 unsigned existingSerial = in->serial;
4718 unsigned newSerial = tags.serialNumber;
4720 if( dev->isYaffs2 ||
4721 ((existingSerial+1) & 3) == newSerial)
4723 // Use new one - destroy the exisiting one
4724 yaffs_DeleteChunk(dev,in->chunkId,1,__LINE__);
4729 // Use existing - destroy this one.
4730 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
4735 (tags.objectId == YAFFS_OBJECTID_ROOT ||
4736 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4738 // We only load some info, don't fiddle with directory structure
4740 in->variantType = oh->type;
4742 in->st_mode = oh->st_mode;
4743 #ifdef CONFIG_YAFFS_WINCE
4744 in->win_atime[0] = oh->win_atime[0];
4745 in->win_ctime[0] = oh->win_ctime[0];
4746 in->win_mtime[0] = oh->win_mtime[0];
4747 in->win_atime[1] = oh->win_atime[1];
4748 in->win_ctime[1] = oh->win_ctime[1];
4749 in->win_mtime[1] = oh->win_mtime[1];
4751 in->st_uid = oh->st_uid;
4752 in->st_gid = oh->st_gid;
4753 in->st_atime = oh->st_atime;
4754 in->st_mtime = oh->st_mtime;
4755 in->st_ctime = oh->st_ctime;
4756 in->st_rdev = oh->st_rdev;
4758 in->chunkId = chunk;
4763 // we need to load this info
4766 in->variantType = oh->type;
4768 in->st_mode = oh->st_mode;
4769 #ifdef CONFIG_YAFFS_WINCE
4770 in->win_atime[0] = oh->win_atime[0];
4771 in->win_ctime[0] = oh->win_ctime[0];
4772 in->win_mtime[0] = oh->win_mtime[0];
4773 in->win_atime[1] = oh->win_atime[1];
4774 in->win_ctime[1] = oh->win_ctime[1];
4775 in->win_mtime[1] = oh->win_mtime[1];
4777 in->st_uid = oh->st_uid;
4778 in->st_gid = oh->st_gid;
4779 in->st_atime = oh->st_atime;
4780 in->st_mtime = oh->st_mtime;
4781 in->st_ctime = oh->st_ctime;
4782 in->st_rdev = oh->st_rdev;
4784 in->chunkId = chunk;
4786 yaffs_SetObjectName(in,oh->name);
4789 // directory stuff...
4790 // hook up to parent
4792 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4793 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4795 // Set up as a directory
4796 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4797 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4799 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4801 // Hoosterman, another problem....
4802 // We're trying to use a non-directory as a directory
4804 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4805 parent = dev->lostNFoundDir;
4808 yaffs_AddObjectToDirectory(parent,in);
4810 if(0 && (parent == dev->deletedDir ||
4811 parent == dev->unlinkedDir))
4813 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4814 dev->nDeletedFiles++;
4817 // Note re hardlinks.
4818 // Since we might scan a hardlink before its equivalent object is scanned
4819 // we put them all in a list.
4820 // After scanning is complete, we should have all the objects, so we run through this
4821 // list and fix up all the chains.
4823 switch(in->variantType)
4825 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
4827 case YAFFS_OBJECT_TYPE_FILE:
4828 if(dev->isYaffs2 && oh->isShrink)
4830 // Prune back the shrunken chunks
4831 yaffs_PruneResizedChunks(in,oh->fileSize);
4832 // Mark the block as having a shrinkHeader
4833 bi->hasShrinkHeader = 1;
4836 if(dev->useHeaderFileSize)
4837 in->variant.fileVariant.fileSize = oh->fileSize;
4840 case YAFFS_OBJECT_TYPE_HARDLINK:
4841 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4842 in->hardLinks.next = (struct list_head *)hardList;
4845 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
4847 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4849 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
4850 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4854 if(parent == dev->deletedDir)
4856 yaffs_DestroyObject(in);
4857 bi->hasShrinkHeader = 1;
4859 //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
4864 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4866 // If we got this far while scanning, then the block is fully allocated.
4867 state = YAFFS_BLOCK_STATE_FULL;
4870 bi->blockState = state;
4872 // Now let's see if it was dirty
4873 if( bi->pagesInUse == 0 &&
4874 !bi->hasShrinkHeader &&
4875 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4877 yaffs_BlockBecameDirty(dev,blk);
4887 // Ok, we've done all the scanning.
4889 // Fix up the hard link chains.
4890 // We should now have scanned all the objects, now it's time to add these
4895 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4897 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4901 // Add the hardlink pointers
4902 hl->variant.hardLinkVariant.equivalentObject=in;
4903 list_add(&hl->hardLinks,&in->hardLinks);
4907 //Todo Need to report/handle this better.
4908 // Got a problem... hardlink to a non-existant object
4909 hl->variant.hardLinkVariant.equivalentObject=NULL;
4910 INIT_LIST_HEAD(&hl->hardLinks);
4917 struct list_head *i;
4918 struct list_head *n;
4921 // Soft delete all the unlinked files
4922 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4926 l = list_entry(i, yaffs_Object,siblings);
4929 yaffs_DestroyObject(l);
4935 yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
4941 ////////////////////////// Directory Functions /////////////////////////
4944 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
4947 if(obj->siblings.prev == NULL)
4950 INIT_LIST_HEAD(&obj->siblings);
4953 else if(!list_empty(&obj->siblings))
4955 // If it is holed up somewhere else, un hook it
4956 list_del_init(&obj->siblings);
4959 list_add(&obj->siblings,&directory->variant.directoryVariant.children);
4960 obj->parent = directory;
4962 if(directory == obj->myDev->unlinkedDir || directory == obj->myDev->deletedDir)
4965 obj->myDev->nUnlinkedFiles++;
4966 obj->renameAllowed = 0;
4970 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
4972 list_del_init(&obj->siblings);
4976 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const YCHAR *name)
4980 struct list_head *i;
4981 YCHAR buffer[YAFFS_MAX_NAME_LENGTH+1];
4985 sum = yaffs_CalcNameSum(name);
4987 list_for_each(i,&directory->variant.directoryVariant.children)
4991 l = list_entry(i, yaffs_Object,siblings);
4993 // Special case for lost-n-found
4994 if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4996 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
5001 else if(yaffs_SumCompare(l->sum, sum)||
5002 l->chunkId <= 0) //LostnFound cunk called Objxxx
5005 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
5006 if(yaffs_strcmp(name,buffer) == 0)
5019 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
5021 struct list_head *i;
5025 list_for_each(i,&theDir->variant.directoryVariant.children)
5029 l = list_entry(i, yaffs_Object,siblings);
5042 // GetEquivalentObject dereferences any hard links to get to the
5045 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5047 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
5049 // We want the object id of the equivalent object, not this one
5050 obj = obj->variant.hardLinkVariant.equivalentObject;
5056 int yaffs_GetObjectName(yaffs_Object *obj,YCHAR *name,int buffSize)
5058 memset(name,0,buffSize * sizeof(YCHAR));
5060 if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5062 yaffs_strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
5064 else if(obj->chunkId <= 0)
5068 yaffs_sprintf(locName,_Y("%s%d"),YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
5069 yaffs_strncpy(name,locName,buffSize - 1);
5072 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5073 else if(obj->shortName[0])
5075 yaffs_strcpy(name,obj->shortName);
5080 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev,__LINE__);
5082 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5084 memset(buffer,0,obj->myDev->nBytesPerChunk);
5086 if(obj->chunkId >= 0)
5088 yaffs_ReadChunkWithTagsFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5090 yaffs_strncpy(name,oh->name,buffSize - 1);
5092 yaffs_ReleaseTempBuffer(obj->myDev,buffer,__LINE__);
5095 return yaffs_strlen(name);
5098 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5101 // Dereference any hard linking
5102 obj = yaffs_GetEquivalentObject(obj);
5104 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5106 return obj->variant.fileVariant.fileSize;
5108 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5110 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5114 // Only a directory should drop through to here
5115 return obj->myDev->nBytesPerChunk;
5119 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5122 struct list_head *i;
5126 count++; // the object itself
5128 list_for_each(i,&obj->hardLinks)
5130 count++; // add the hard links;
5137 int yaffs_GetObjectInode(yaffs_Object *obj)
5139 obj = yaffs_GetEquivalentObject(obj);
5141 return obj->objectId;
5144 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5146 obj = yaffs_GetEquivalentObject(obj);
5148 switch(obj->variantType)
5150 case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break;
5151 case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break;
5152 case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break;
5153 case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break;
5154 case YAFFS_OBJECT_TYPE_SPECIAL:
5155 if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
5156 if(S_ISCHR(obj->st_mode)) return DT_CHR;
5157 if(S_ISBLK(obj->st_mode)) return DT_BLK;
5158 if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
5159 default: return DT_REG; break;
5163 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5165 obj = yaffs_GetEquivalentObject(obj);
5166 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5168 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5172 return yaffs_CloneString(_Y(""));
5176 #ifndef CONFIG_YAFFS_WINCE
5178 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5180 unsigned int valid = attr->ia_valid;
5182 if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
5183 if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
5184 if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
5186 #if defined(CONFIG_KERNEL_2_5)
5187 if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime.tv_sec;
5188 if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime.tv_sec;
5189 if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime.tv_sec;
5191 if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime;
5192 if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime;
5193 if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime;
5196 if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
5198 yaffs_UpdateObjectHeader(obj,NULL,1,0);
5203 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5205 unsigned int valid = 0;
5207 attr->ia_mode = obj->st_mode; valid |= ATTR_MODE;
5208 attr->ia_uid = obj->st_uid; valid |= ATTR_UID;
5209 attr->ia_gid = obj->st_gid; valid |= ATTR_GID;
5211 #if defined(CONFIG_KERNEL_2_5)
5212 attr->ia_atime.tv_sec = obj->st_atime; valid |= ATTR_ATIME;
5213 attr->ia_ctime.tv_sec = obj->st_ctime; valid |= ATTR_CTIME;
5214 attr->ia_mtime.tv_sec = obj->st_mtime; valid |= ATTR_MTIME;
5216 attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME;
5217 attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME;
5218 attr->ia_mtime = obj->st_mtime; valid |= ATTR_MTIME;
5220 attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
5222 attr->ia_valid = valid;
5230 int yaffs_DumpObject(yaffs_Object *obj)
5232 // __u8 buffer[YAFFS_BYTES_PER_CHUNK];
5234 // yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5236 // memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
5238 // if(obj->chunkId >= 0)
5240 // yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5243 yaffs_GetObjectName(obj,name,256);
5245 T(YAFFS_TRACE_ALWAYS,(TSTR("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d type %d size %d\n" TENDSTR),
5246 obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial,
5247 obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5250 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
5251 obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial,
5252 obj->sum, obj->chunkId));
5253 switch(obj->variantType)
5255 case YAFFS_OBJECT_TYPE_FILE:
5256 YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
5258 case YAFFS_OBJECT_TYPE_DIRECTORY:
5259 YPRINTF((" DIRECTORY\n"));
5261 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
5262 case YAFFS_OBJECT_TYPE_SYMLINK:
5263 case YAFFS_OBJECT_TYPE_UNKNOWN:
5272 ///////////////////////// Initialisation code ///////////////////////////
5275 int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5278 // Common functions, gotta have
5279 if(!dev->eraseBlockInNAND ||
5280 !dev->initialiseNAND) return 0;
5282 // Can use the "with tags" style interface for yaffs1 or yaffs2
5283 if(dev->writeChunkWithTagsToNAND &&
5284 dev->readChunkWithTagsFromNAND &&
5285 !dev->writeChunkToNAND &&
5286 !dev->readChunkFromNAND &&
5287 dev->markNANDBlockBad &&
5288 dev->queryNANDBlock) return 1;
5290 // Can use the "spare" style interface for yaffs1
5291 if(!dev->isYaffs2 &&
5292 !dev->writeChunkWithTagsToNAND &&
5293 !dev->readChunkWithTagsFromNAND &&
5294 dev->writeChunkToNAND &&
5295 dev->readChunkFromNAND &&
5296 !dev->markNANDBlockBad &&
5297 !dev->queryNANDBlock) return 1;
5303 int yaffs_GutsInitialise(yaffs_Device *dev)
5310 // Check stuff that must be set
5314 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Need a device\n" TENDSTR)));
5318 // Check geometry parameters.
5320 if( (dev->isYaffs2 && dev->nBytesPerChunk <1024) ||
5321 (!dev->isYaffs2 && dev->nBytesPerChunk !=512) ||
5322 dev->nChunksPerBlock < 2 ||
5323 dev->nReservedBlocks < 2 ||
5324 dev->startBlock <= 0 ||
5325 dev->endBlock <= 0 ||
5326 dev->endBlock <= (dev->startBlock + dev->nReservedBlocks + 2) // otherwise it is too small
5329 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: nand geometry problems\n" TENDSTR)));
5334 // Got the right mix of functions?
5336 if(!yaffs_CheckDevFunctions(dev))
5339 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5344 // This is really a compilation check.
5345 if(!yaffs_CheckStructures())
5347 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
5353 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
5359 // Finished with most checks. One or two more checks happen later on too.
5365 nBlocks = dev->endBlock - dev->startBlock + 1;
5369 // OK now calculate a few things for the device
5370 // Calculate chunkGroupBits.
5371 // We need to find the next power of 2 > than endBlock
5373 x = dev->nChunksPerBlock * (dev->endBlock+1);
5375 for(bits = extraBits = 0; x > 1; bits++)
5377 if(x & 1) extraBits++;
5381 if(extraBits > 0) bits++;
5384 // Level0 Tnodes are 16 bits, so if the bitwidth of the
5385 // chunk range we're using is greater than 16 we need
5386 // to figure out chunk shift and chunkGroupSize
5389 dev->chunkGroupBits = 0;
5393 dev->chunkGroupBits = bits - 16;
5396 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
5398 if(dev->nChunksPerBlock < dev->chunkGroupSize)
5400 // We have a problem because the soft delete won't work if
5401 // the chunk group size > chunks per block.
5402 // This can be remedied by using larger "virtual blocks".
5403 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: chunk group too large\n" TENDSTR)));
5409 // OK, we've finished verifying the device, lets continue with initialisation
5411 // More device initialisation
5412 dev->garbageCollections = 0;
5413 dev->passiveGarbageCollections = 0;
5414 dev->currentDirtyChecker = 0;
5415 dev->bufferedBlock = -1;
5416 dev->doingBufferedBlockRewrite = 0;
5417 dev->nDeletedFiles = 0;
5418 dev->nBackgroundDeletions=0;
5419 dev->nUnlinkedFiles = 0;
5422 dev->tagsEccFixed=0;
5423 dev->tagsEccUnfixed=0;
5424 dev->nErasureFailures = 0;
5426 //dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
5427 // Initialise temporary buffers
5430 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5432 dev->tempBuffer[i].line = 0; // not in use
5433 dev->tempBuffer[i].buffer = YMALLOC(dev->nBytesPerChunk);
5440 yaffs_InitialiseBlocks(dev,nBlocks);
5442 yaffs_InitialiseTnodes(dev);
5444 yaffs_InitialiseObjects(dev);
5446 dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
5448 if(dev->nShortOpCaches > 0)
5452 if(dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
5454 dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
5457 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
5459 for(i=0; i < dev->nShortOpCaches; i++)
5461 dev->srCache[i].object = NULL;
5462 dev->srCache[i].lastUse = 0;
5463 dev->srCache[i].dirty = 0;
5464 dev->srCache[i].data = YMALLOC(dev->nBytesPerChunk);
5472 // Initialise the unlinked, root and lost and found directories
5473 dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = dev->deletedDir = NULL;
5475 dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5476 dev->deletedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5478 dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
5479 dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
5480 yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
5484 dev->useHeaderFileSize = 1;
5487 // Now scan the flash.
5491 dev->nPageReads = 0;
5492 dev->nPageWrites = 0;
5493 dev->nBlockErasures = 0;
5495 dev->nRetriedWrites = 0;
5497 dev->nRetiredBlocks = 0;
5504 void yaffs_Deinitialise(yaffs_Device *dev)
5510 yaffs_DeinitialiseBlocks(dev);
5511 yaffs_DeinitialiseTnodes(dev);
5512 yaffs_DeinitialiseObjects(dev);
5513 if(dev->nShortOpCaches > 0)
5516 for(i=0; i < dev->nShortOpCaches; i++)
5518 YFREE(dev->srCache[i].data);
5521 YFREE(dev->srCache);
5524 YFREE(dev->gcCleanupList);
5526 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5528 YFREE(dev->tempBuffer[i].buffer);
5538 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5540 int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
5542 struct list_head *i;
5546 // To the free chunks add the chunks that are in the deleted unlinked files.
5547 list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
5549 l = list_entry(i, yaffs_Object,siblings);
5553 nFree += l->nDataChunks;
5558 // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);
5560 if(nFree < 0) nFree = 0;
5568 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5573 int nDirtyCacheChunks=0;
5575 yaffs_BlockInfo *blk;
5577 struct list_head *i;
5580 for(nFree = 0, b = dev->startBlock; b <= dev->endBlock; b++)
5582 blk = yaffs_GetBlockInfo(dev,b);
5584 switch(blk->blockState)
5586 case YAFFS_BLOCK_STATE_EMPTY:
5587 case YAFFS_BLOCK_STATE_ALLOCATING:
5588 case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break;
5596 // To the free chunks add the chunks that are in the deleted unlinked files.
5597 list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
5601 l = list_entry(i, yaffs_Object,siblings);
5605 pending += l->nDataChunks;
5612 //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending);
5614 if(nFree != dev->nFreeChunks)
5616 // printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks);
5621 // Now count the number of dirty chunks in the cache and subtract those
5625 for(i = 0; i < dev->nShortOpCaches; i++)
5627 if(dev->srCache[i].dirty) nDirtyCacheChunks++;
5631 nFree -= nDirtyCacheChunks;
5633 nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
5635 if(nFree < 0) nFree = 0;
5643 /////////////////// YAFFS test code //////////////////////////////////
5645 #define yaffs_CheckStruct(structure,syze, name) \
5646 if(sizeof(structure) != syze) \
5648 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
5649 return YAFFS_FAIL; \
5653 static int yaffs_CheckStructures(void)
5655 // yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
5656 // yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
5657 // yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
5658 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
5659 yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
5661 yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
5668 void yaffs_GutsTest(yaffs_Device *dev)
5671 if(yaffs_CheckStructures() != YAFFS_OK)
5673 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
5677 yaffs_TnodeTest(dev);
5678 yaffs_ObjectTest(dev);