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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
17 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.15 2002-12-13 00:13:06 charles Exp $";
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
25 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
29 // External functions for ECC on data
30 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
31 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
34 // countBits is a quick way of counting the number of bits in a byte.
35 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
37 static const char yaffs_countBitsTable[256] =
39 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
40 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
41 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
42 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
43 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
44 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
45 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
46 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
47 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
48 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
49 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
50 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
52 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
53 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
54 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
57 static int yaffs_CountBits(__u8 x)
60 retVal = yaffs_countBitsTable[x];
67 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
68 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
69 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
70 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
72 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
73 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
74 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force);
75 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId);
76 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
77 static int yaffs_CheckStructures(void);
78 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
79 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
81 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
84 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
85 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
86 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
87 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
88 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
90 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
92 static int yaffs_UnlinkWorker(yaffs_Object *obj);
95 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1);
99 loff_t yaffs_GetFileSize(yaffs_Object *obj);
101 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
102 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
105 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
107 #ifdef YAFFS_PARANOID
108 static int yaffs_CheckFileSanity(yaffs_Object *in);
110 #define yaffs_CheckFileSanity(in)
113 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
114 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
117 static __inline__ yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
119 if(blk < dev->startBlock || blk > dev->endBlock)
121 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: block %d is not valid" TENDSTR),blk));
124 return &dev->blockInfo[blk - dev->startBlock];
128 static __inline__ int yaffs_HashFunction(int n)
130 return (n % YAFFS_NOBJECT_BUCKETS);
134 yaffs_Object *yaffs_Root(yaffs_Device *dev)
139 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
141 return dev->lostNFoundDir;
145 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
147 if(chunkInNAND < dev->startBlock * YAFFS_CHUNKS_PER_BLOCK)
149 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
154 return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
163 int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
167 int doErrorCorrection)
170 yaffs_Spare localSpare;
172 #ifndef CONFIG_YAFFS_USE_NANDECC
174 int eccResult1,eccResult2;
176 struct nandspare nspare;
186 // If we don't have a real spare, then we use a local one.
187 // Need this for the calculation of the ecc
192 #ifndef CONFIG_YAFFS_USE_NANDECC
193 retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
194 if(data && doErrorCorrection)
197 //Todo handle any errors
198 nand_calculate_ecc(data,calcEcc);
199 eccResult1 = nand_correct_data (data,spare->ecc1, calcEcc);
200 nand_calculate_ecc(&data[256],calcEcc);
201 eccResult2 = nand_correct_data (&data[256],spare->ecc2, calcEcc);
205 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
208 else if(eccResult1<0)
210 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
216 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
219 else if(eccResult2<0)
221 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
225 if(eccResult1 || eccResult2)
227 // Hoosterman, we had a data problem on this page
228 yaffs_HandleReadDataError(dev,chunkInNAND);
232 retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
233 memcpy (spare, &nspare, sizeof(yaffs_Spare));
234 if(data && doErrorCorrection)
238 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
240 else if(nspare.eccres1<0)
242 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
247 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
249 else if(nspare.eccres2<0)
251 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
254 if(nspare.eccres2 || nspare.eccres2)
256 // Hoosterman, we had a data problem on this page
257 yaffs_HandleReadDataError(dev,chunkInNAND);
266 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
270 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
271 static __u8 data[YAFFS_BYTES_PER_CHUNK];
272 static __u8 spare[16];
275 dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare);
281 memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);
285 if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return YAFFS_FAIL;
286 if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;
295 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
297 dev->nBlockErasures++;
298 return dev->eraseBlockInNAND(dev,blockInNAND);
301 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
303 return dev->initialiseNAND(dev);
306 static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve)
313 unsigned char rbData[YAFFS_BYTES_PER_CHUNK];
317 chunk = yaffs_AllocateChunk(dev,useReserve);
322 // First check this chunk is erased...
323 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
324 writeOk = yaffs_CheckChunkErased(dev,chunk);
328 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
332 writeOk = yaffs_WriteChunkToNAND(dev,chunk,data,spare);
338 // If verify fails, then delete this chunk and try again
339 // To verify we compare everything except the block and
340 // page status bytes.
341 // NB We check a raw read without ECC correction applied
342 yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0);
344 #ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
345 if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare))
348 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk));
357 // Copy the data into the write buffer.
358 // NB We do this at the end to prevent duplicates in the case of a write error.
360 yaffs_HandleWriteChunkOk(dev,chunk,data,spare);
364 yaffs_HandleWriteChunkError(dev,chunk);
368 } while(chunk >= 0 && ! writeOk);
372 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
373 dev->nRetriedWrites+= (attempts - 1);
380 // Functions for robustisizing
384 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
386 // Ding the blockStatus in the first two pages of the block.
390 memset(&spare, 0xff,sizeof(yaffs_Spare));
392 spare.blockStatus = 0;
394 yaffs_WriteChunkToNAND(dev, blockInNAND * YAFFS_CHUNKS_PER_BLOCK, NULL , &spare);
395 yaffs_WriteChunkToNAND(dev, blockInNAND * YAFFS_CHUNKS_PER_BLOCK + 1, NULL , &spare);
397 yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
398 dev->nRetiredBlocks++;
403 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
405 dev->doingBufferedBlockRewrite = 1;
407 // Remove erased chunks
408 // Rewrite existing chunks to a new block
409 // Set current write block to the new block
411 dev->doingBufferedBlockRewrite = 0;
417 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
419 int blockInNAND = chunkInNAND/YAFFS_CHUNKS_PER_BLOCK;
421 // Mark the block for retirement
422 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
423 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
427 // Just do a garbage collection on the affected block then retire the block
432 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
436 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
440 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
444 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
446 int blockInNAND = chunkInNAND/YAFFS_CHUNKS_PER_BLOCK;
448 // Mark the block for retirement
449 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
451 yaffs_DeleteChunk(dev,chunkInNAND);
457 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
461 if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
462 s0->tagByte0 != s1->tagByte0 ||
463 s0->tagByte1 != s1->tagByte1 ||
464 s0->tagByte2 != s1->tagByte2 ||
465 s0->tagByte3 != s1->tagByte3 ||
466 s0->tagByte4 != s1->tagByte4 ||
467 s0->tagByte5 != s1->tagByte5 ||
468 s0->tagByte6 != s1->tagByte6 ||
469 s0->tagByte7 != s1->tagByte7 ||
470 s0->ecc1[0] != s1->ecc1[0] ||
471 s0->ecc1[1] != s1->ecc1[1] ||
472 s0->ecc1[2] != s1->ecc1[2] ||
473 s0->ecc2[0] != s1->ecc2[0] ||
474 s0->ecc2[1] != s1->ecc2[1] ||
475 s0->ecc2[2] != s1->ecc2[2] )
484 ///////////////////////// Object management //////////////////
485 // List of spare objects
486 // The list is hooked together using the first pointer
489 // static yaffs_Object *yaffs_freeObjects = NULL;
491 // static int yaffs_nFreeObjects;
493 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
495 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
498 static __u16 yaffs_CalcNameSum(const char *name)
503 __u8 *bname = (__u8 *)name;
506 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
508 #ifdef CONFIG_YAFFS_WINCE
509 sum += toupper(*bname) * i;
520 void yaffs_SetObjectName(yaffs_Object *obj, const char *name)
522 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
523 if(name && strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
525 strcpy(obj->shortName,name);
529 obj->shortName[0]='\0';
532 obj->sum = yaffs_CalcNameSum(name);
535 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
537 nand_calculate_ecc (data , spare->ecc1);
538 nand_calculate_ecc (&data[256] , spare->ecc2);
541 void yaffs_CalcTagsECC(yaffs_Tags *tags)
545 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
552 for(i = 0; i < 8; i++)
554 for(j = 1; j &0xff; j<<=1)
569 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
571 unsigned ecc = tags->ecc;
573 yaffs_CalcTagsECC(tags);
579 // TODO: Handle the failure better. Retire?
580 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
584 b[ecc / 8] ^= (1 << (ecc & 7));
586 // Now recvalc the ecc
587 yaffs_CalcTagsECC(tags);
589 return 1; // recovered error
593 // Wierd ecc failure value
594 // TODO Need to do somethiong here
595 return -1; //unrecovered error
602 ///////////////////////// TNODES ///////////////////////
604 // List of spare tnodes
605 // The list is hooked together using the first pointer
608 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
610 // static int yaffs_nFreeTnodes;
612 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
616 // yaffs_CreateTnodes creates a bunch more tnodes and
617 // adds them to the tnode free list.
618 // Don't use this function directly
620 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
623 yaffs_Tnode *newTnodes;
624 yaffs_TnodeList *tnl;
626 if(nTnodes < 1) return YAFFS_OK;
630 newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
634 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
638 // Hook them into the free list
639 for(i = 0; i < nTnodes - 1; i++)
641 newTnodes[i].internal[0] = &newTnodes[i+1];
644 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
645 dev->freeTnodes = newTnodes;
646 dev->nFreeTnodes+= nTnodes;
647 dev->nTnodesCreated += nTnodes;
649 // Now add this bunch of tnodes to a list for freeing up.
650 // NB If we can't add this to the management list it isn't fatal
651 // but it just means we can't free this bunch of tnodes later.
652 tnl = YMALLOC(sizeof(yaffs_TnodeList));
655 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
660 tnl->tnodes = newTnodes;
661 tnl->next = dev->allocatedTnodeList;
662 dev->allocatedTnodeList = tnl;
666 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
673 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
674 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
676 yaffs_Tnode *tn = NULL;
678 // If there are none left make more
681 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
686 tn = dev->freeTnodes;
687 dev->freeTnodes = dev->freeTnodes->internal[0];
690 memset(tn,0,sizeof(yaffs_Tnode));
698 // FreeTnode frees up a tnode and puts it back on the free list
699 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
701 tn->internal[0] = dev->freeTnodes;
702 dev->freeTnodes = tn;
707 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
709 // Free the list of allocated tnodes
711 while(dev->allocatedTnodeList)
713 YFREE(dev->allocatedTnodeList->tnodes);
714 dev->allocatedTnodeList = dev->allocatedTnodeList->next;
717 dev->freeTnodes = NULL;
718 dev->nFreeTnodes = 0;
721 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
723 dev->allocatedTnodeList = NULL;
724 dev->freeTnodes = NULL;
725 dev->nFreeTnodes = 0;
726 dev->nTnodesCreated = 0;
731 void yaffs_TnodeTest(yaffs_Device *dev)
736 yaffs_Tnode *tn[1000];
738 YINFO("Testing TNodes");
740 for(j = 0; j < 50; j++)
742 for(i = 0; i < 1000; i++)
744 tn[i] = yaffs_GetTnode(dev);
747 YALERT("Getting tnode failed");
750 for(i = 0; i < 1000; i+=3)
752 yaffs_FreeTnode(dev,tn[i]);
761 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
763 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
764 // The look up tree is represented by the top tnode and the number of topLevel
765 // in the tree. 0 means only the level 0 tnode is in the tree.
768 // FindLevel0Tnode finds the level 0 tnode, if one exists.
769 // Used when reading.....
770 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
773 yaffs_Tnode *tn = fStruct->top;
775 int requiredTallness;
776 int level = fStruct->topLevel;
778 // Check sane level and chunk Id
779 if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
782 // sprintf(str,"Bad level %d",level);
787 if(chunkId > YAFFS_MAX_CHUNK_ID)
790 // sprintf(str,"Bad chunkId %d",chunkId);
795 // First check we're tall enough (ie enough topLevel)
797 i = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
798 requiredTallness = 0;
801 i >>= YAFFS_TNODES_INTERNAL_BITS;
806 if(requiredTallness > fStruct->topLevel)
808 // Not tall enough, so we can't find it, return NULL.
813 // Traverse down to level 0
814 while (level > 0 && tn)
816 tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) &
817 YAFFS_TNODES_INTERNAL_MASK];
825 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
826 // This happens in two steps:
827 // 1. If the tree isn't tall enough, then make it taller.
828 // 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
830 // Used when modifying the tree.
832 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
837 int requiredTallness;
844 //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
846 // Check sane level and page Id
847 if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
850 // sprintf(str,"Bad level %d",fStruct->topLevel);
855 if(chunkId > YAFFS_MAX_CHUNK_ID)
858 // sprintf(str,"Bad chunkId %d",chunkId);
863 // First check we're tall enough (ie enough topLevel)
865 x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
866 requiredTallness = 0;
869 x >>= YAFFS_TNODES_INTERNAL_BITS;
873 //T((TSTR(" required=%d"),requiredTallness));
876 if(requiredTallness > fStruct->topLevel)
878 // Not tall enough,gotta make the tree taller
879 for(i = fStruct->topLevel; i < requiredTallness; i++)
881 //T((TSTR(" add new top")));
883 tn = yaffs_GetTnode(dev);
887 tn->internal[0] = fStruct->top;
892 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
896 fStruct->topLevel = requiredTallness;
900 // Traverse down to level 0, adding anything we need
902 l = fStruct->topLevel;
906 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) &
907 YAFFS_TNODES_INTERNAL_MASK;
909 //T((TSTR(" [%d:%d]"),l,i));
913 //T((TSTR(" added")));
915 tn->internal[x] = yaffs_GetTnode(dev);
918 tn = tn->internal[x];
928 // DeleteWorker scans backwards through the tnode tree and deletes all the
929 // chunks and tnodes in the file
930 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
932 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
948 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
952 if(limit && (*limit) < 0)
958 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
959 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
963 yaffs_FreeTnode(in->myDev,tn->internal[i]);
964 tn->internal[i] = NULL;
969 return (allDone) ? 1 : 0;
973 for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0; i--) //NB Don't apply the limit here, always delete a whole level0
979 chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
981 theChunk = tn->level0[i] << in->myDev->chunkGroupBits;
983 // Now we need to search for it
984 for(j = 0,found = 0; theChunk && j < in->myDev->chunkGroupSize && !found; j++)
986 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,&tags,&chunkDeleted);
987 if(yaffs_TagsMatch(&tags,in->objectId,chunkInInode,chunkDeleted))
1001 yaffs_DeleteChunk(in->myDev,theChunk);
1028 // Pruning removes any part of the file structure tree that is beyond the
1029 // bounds of the file (ie that does not point to chunks).
1031 // A file should only get pruned when its size is reduced.
1033 // Before pruning, the chunks must be pulled from the tree and the
1034 // level 0 tnode entries must be zeroed out.
1035 // Could also use this for file deletion, but that's probably better handled
1036 // by a special case.
1038 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1040 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1049 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1051 if(tn->internal[i] && level > 0)
1053 tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1062 if(hasData == 0 && del0)
1064 // Free and return NULL
1066 yaffs_FreeTnode(dev,tn);
1076 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1083 if(fStruct->topLevel > 0)
1085 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1087 // Now we have a tree with all the non-zero branches NULL but the height
1088 // is the same as it was.
1089 // Let's see if we can trim internal tnodes to shorten the tree.
1090 // We can do this if only the 0th element in the tnode is in use
1091 // (ie all the non-zero are NULL)
1093 while(fStruct->topLevel && !done)
1098 for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1108 fStruct->top = tn->internal[0];
1109 fStruct->topLevel--;
1110 yaffs_FreeTnode(dev,tn);
1126 /////////////////////// End of File Structure functions. /////////////////
1128 // yaffs_CreateFreeObjects creates a bunch more objects and
1129 // adds them to the object free list.
1130 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1133 yaffs_Object *newObjects;
1134 yaffs_ObjectList *list;
1136 if(nObjects < 1) return YAFFS_OK;
1138 // make these things
1140 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1144 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1148 // Hook them into the free list
1149 for(i = 0; i < nObjects - 1; i++)
1151 (yaffs_Object *)newObjects[i].siblings.next = &newObjects[i+1];
1154 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1155 dev->freeObjects = newObjects;
1156 dev->nFreeObjects+= nObjects;
1157 dev->nObjectsCreated+= nObjects;
1159 // Now add this bunch of Objects to a list for freeing up.
1161 list = YMALLOC(sizeof(yaffs_ObjectList));
1164 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1168 list->objects = newObjects;
1169 list->next = dev->allocatedObjectList;
1170 dev->allocatedObjectList = list;
1179 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1180 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1182 yaffs_Object *tn = NULL;
1184 // If there are none left make more
1185 if(!dev->freeObjects)
1187 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1190 if(dev->freeObjects)
1192 tn = dev->freeObjects;
1193 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1194 dev->nFreeObjects--;
1196 // Now sweeten it up...
1198 memset(tn,0,sizeof(yaffs_Object));
1201 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1202 INIT_LIST_HEAD(&(tn->hardLinks));
1203 INIT_LIST_HEAD(&(tn->hashLink));
1204 INIT_LIST_HEAD(&tn->siblings);
1206 // Add it to the lost and found directory.
1207 // NB Can't put root or lostNFound in lostNFound so
1208 // check if lostNFound exists first
1209 if(dev->lostNFoundDir)
1211 yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);
1219 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1222 yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);
1225 obj->fake = 1; // it is fake so it has no NAND presence...
1226 obj->renameAllowed= 0; // ... and we're not allowed to rename it...
1227 obj->unlinkAllowed= 0; // ... or unlink it
1230 obj->st_mode = mode;
1232 obj->chunkId = 0; // Not a valid chunk.
1240 static void yaffs_UnhashObject(yaffs_Object *tn)
1243 yaffs_Device *dev = tn->myDev;
1246 // If it is still linked into the bucket list, free from the list
1247 if(!list_empty(&tn->hashLink))
1249 list_del_init(&tn->hashLink);
1250 bucket = yaffs_HashFunction(tn->objectId);
1251 dev->objectBucket[bucket].count--;
1257 // FreeObject frees up a Object and puts it back on the free list
1258 static void yaffs_FreeObject(yaffs_Object *tn)
1261 yaffs_Device *dev = tn->myDev;
1263 yaffs_UnhashObject(tn);
1265 // Link into the free list.
1266 (yaffs_Object *)(tn->siblings.next) = dev->freeObjects;
1267 dev->freeObjects = tn;
1268 dev->nFreeObjects++;
1274 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1276 // Free the list of allocated Objects
1278 while( dev->allocatedObjectList)
1280 YFREE(dev->allocatedObjectList->objects);
1281 dev->allocatedObjectList = dev->allocatedObjectList->next;
1284 dev->freeObjects = NULL;
1285 dev->nFreeObjects = 0;
1288 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1292 dev->allocatedObjectList = NULL;
1293 dev->freeObjects = NULL;
1294 dev->nFreeObjects = 0;
1296 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1298 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1299 dev->objectBucket[i].count = 0;
1309 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1314 int lowest = 999999;
1317 // First let's see if we can find one that's empty.
1319 for(i = 0; i < 10 && lowest > 0; i++)
1322 x %= YAFFS_NOBJECT_BUCKETS;
1323 if(dev->objectBucket[x].count < lowest)
1325 lowest = dev->objectBucket[x].count;
1331 // If we didn't find an empty list, then try
1332 // looking a bit further for a short one
1334 for(i = 0; i < 10 && lowest > 3; i++)
1337 x %= YAFFS_NOBJECT_BUCKETS;
1338 if(dev->objectBucket[x].count < lowest)
1340 lowest = dev->objectBucket[x].count;
1349 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1351 int bucket = yaffs_FindNiceObjectBucket(dev);
1353 // Now find an object value that has not already been taken
1354 // by scanning the list.
1357 struct list_head *i;
1359 __u32 n = (__u32)bucket;
1361 //yaffs_CheckObjectHashSanity();
1366 n += YAFFS_NOBJECT_BUCKETS;
1367 if(1 ||dev->objectBucket[bucket].count > 0)
1369 list_for_each(i,&dev->objectBucket[bucket].list)
1371 // If there is already one in the list
1372 if(list_entry(i, yaffs_Object,hashLink)->objectId == n)
1380 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1385 void yaffs_HashObject(yaffs_Object *in)
1387 int bucket = yaffs_HashFunction(in->objectId);
1388 yaffs_Device *dev = in->myDev;
1390 if(!list_empty(&in->hashLink))
1396 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1397 dev->objectBucket[bucket].count++;
1401 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1403 int bucket = yaffs_HashFunction(number);
1404 struct list_head *i;
1407 list_for_each(i,&dev->objectBucket[bucket].list)
1409 // Look if it is in the list
1410 in = list_entry(i, yaffs_Object,hashLink);
1411 if(in->objectId == number)
1422 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1425 yaffs_Object *theObject;
1429 number = yaffs_CreateNewObjectNumber(dev);
1432 theObject = yaffs_AllocateEmptyObject(dev);
1436 theObject->fake = 0;
1437 theObject->renameAllowed = 1;
1438 theObject->unlinkAllowed = 1;
1439 theObject->objectId = number;
1440 yaffs_HashObject(theObject);
1441 theObject->variantType = type;
1442 #ifdef CONFIG_YAFFS_WINCE
1443 yfsd_WinFileTimeNow(theObject->win_atime);
1444 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1445 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1448 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = CURRENT_TIME;
1452 case YAFFS_OBJECT_TYPE_FILE:
1453 theObject->variant.fileVariant.fileSize = 0;
1454 theObject->variant.fileVariant.scannedFileSize = 0;
1455 theObject->variant.fileVariant.topLevel = 0;
1456 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
1458 case YAFFS_OBJECT_TYPE_DIRECTORY:
1459 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1461 case YAFFS_OBJECT_TYPE_SYMLINK:
1462 // No action required
1464 case YAFFS_OBJECT_TYPE_HARDLINK:
1465 // No action required
1467 case YAFFS_OBJECT_TYPE_SPECIAL:
1468 // No action required
1470 case YAFFS_OBJECT_TYPE_UNKNOWN:
1471 // todo this should not happen
1479 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1481 yaffs_Object *theObject = NULL;
1485 theObject = yaffs_FindObjectByNumber(dev,number);
1490 theObject = yaffs_CreateNewObject(dev,number,type);
1497 char *yaffs_CloneString(const char *str)
1499 char *newStr = NULL;
1503 newStr = YMALLOC(strlen(str) + 1);
1512 // Mknod (create) a new object.
1513 // equivalentObject only has meaning for a hard link;
1514 // aliasString only has meaning for a sumlink.
1515 // rdev only has meaning for devices (a subset of special objects)
1516 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1517 yaffs_Object *parent,
1522 yaffs_Object *equivalentObject,
1523 const char *aliasString,
1528 yaffs_Device *dev = parent->myDev;
1530 // Check if the entry exists. If it does then fail the call since we don't want a dup.
1531 if(yaffs_FindObjectByName(parent,name))
1536 in = yaffs_CreateNewObject(dev,-1,type);
1542 in->variantType = type;
1546 #ifdef CONFIG_YAFFS_WINCE
1547 yfsd_WinFileTimeNow(in->win_atime);
1548 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1549 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[0];
1552 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
1557 in->nDataChunks = 0;
1559 yaffs_SetObjectName(in,name);
1562 yaffs_AddObjectToDirectory(parent,in);
1564 in->myDev = parent->myDev;
1569 case YAFFS_OBJECT_TYPE_SYMLINK:
1570 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1572 case YAFFS_OBJECT_TYPE_HARDLINK:
1573 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1574 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1575 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1577 case YAFFS_OBJECT_TYPE_FILE: // do nothing
1578 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1579 case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1580 case YAFFS_OBJECT_TYPE_UNKNOWN:
1584 if(yaffs_GetNumberOfFreeChunks(dev) <= 0 ||
1585 yaffs_UpdateObjectHeader(in,name,0) < 0)
1587 // Could not create the object header, fail the creation
1588 yaffs_UnlinkWorker(in);
1597 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1599 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1602 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1604 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1607 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1609 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1612 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,const char *alias)
1614 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1617 // NB yaffs_Link returns the object id of the equivalent object.
1618 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject)
1620 // Get the real object in case we were fed a hard link as an equivalent object
1621 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1623 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1625 return equivalentObject;
1635 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName,int force)
1641 newDir = obj->parent; // use the old directory
1644 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1646 // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1647 // duplicate names are allowed.
1648 // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1651 !yaffs_FindObjectByName(newDir,newName)) &&
1652 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1654 yaffs_SetObjectName(obj,newName);
1657 yaffs_AddObjectToDirectory(newDir,obj);
1659 if(unlinkOp) obj->unlinked = 1;
1662 if(yaffs_UpdateObjectHeader(obj,newName,0) >= 0)
1673 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName)
1678 #ifdef CONFIG_YAFFS_WINCE
1679 // Special case for WinCE.
1680 // While look-up is case insensitive, the name isn't.
1681 // THerefore we might want to change x.txt to X.txt
1682 if(oldDir == newDir && _stricmp(oldName,newName) == 0)
1688 obj = yaffs_FindObjectByName(oldDir,oldName);
1689 if(obj && obj->renameAllowed)
1691 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1698 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1700 // Scan the buckets and check that the lists
1701 // have as many members as the count says there are
1704 struct list_head *j;
1707 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1711 list_for_each(j,&dev->objectBucket[bucket].list)
1716 if(countEm != dev->objectBucket[bucket].count)
1718 T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
1727 void yaffs_ObjectTest(yaffs_Device *dev)
1729 yaffs_Object *in[1000];
1731 yaffs_Object *inold[1000];
1735 memset(in,0,1000*sizeof(yaffs_Object *));
1736 memset(inold,0,1000*sizeof(yaffs_Object *));
1738 yaffs_CheckObjectHashSanity(dev);
1740 for(j = 0; j < 10; j++)
1744 for(i = 0; i < 1000; i++)
1746 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1749 YINFO("No more inodes");
1753 inNo[i] = in[i]->objectId;
1757 for(i = 0; i < 1000; i++)
1759 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1761 //T(("Differnce in look up test\n"));
1765 // T(("Look up ok\n"));
1769 yaffs_CheckObjectHashSanity(dev);
1771 for(i = 0; i < 1000; i+=3)
1773 yaffs_FreeObject(in[i]);
1778 yaffs_CheckObjectHashSanity(dev);
1785 /////////////////////////// Block Management and Page Allocation ///////////////////
1788 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
1790 dev->allocationBlock = -1; // force it to get a new one
1791 //Todo we're assuming the malloc will pass.
1792 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
1795 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
1802 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
1804 YFREE(dev->blockInfo);
1807 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
1808 // for garbage collection.
1810 static int yaffs_FindDirtiestBlock(yaffs_Device *dev)
1813 int b = dev->currentDirtyChecker;
1817 int pagesInUse = 100; // silly big number
1818 yaffs_BlockInfo *bi;
1820 for(i = dev->startBlock; i <= dev->endBlock && pagesInUse > 2 ; i++)
1823 if ( b < dev->startBlock || b > dev->endBlock)
1825 b = dev->startBlock;
1828 if(b < dev->startBlock || b > dev->endBlock)
1830 T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
1834 bi = yaffs_GetBlockInfo(dev,b);
1836 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
1837 bi->pagesInUse < pagesInUse)
1840 pagesInUse = bi->pagesInUse;
1844 dev->currentDirtyChecker = b;
1850 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
1852 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
1856 // If the block is still healthy erase it and mark as clean.
1857 // If the block has had a data failure, then retire it.
1858 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
1860 if(!bi->needsRetiring)
1862 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
1865 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
1871 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
1872 dev->nErasedBlocks++;
1876 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
1880 yaffs_RetireBlock(dev,blockNo);
1881 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
1886 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
1889 yaffs_BlockInfo *bi;
1891 if(dev->nErasedBlocks < 1)
1893 // Hoosterman we've got a problem.
1894 // Can't get space to gc
1895 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: no space during gc" TENDSTR)));
1900 // Find an empty block.
1902 for(i = dev->startBlock; i <= dev->endBlock; i++)
1904 dev->allocationBlockFinder++;
1905 if(dev->allocationBlockFinder < dev->startBlock ||
1906 dev->allocationBlockFinder > dev->endBlock)
1908 dev->allocationBlockFinder = dev->startBlock;
1911 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
1912 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
1914 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
1915 dev->nErasedBlocks--;
1916 return dev->allocationBlockFinder;
1925 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
1928 yaffs_BlockInfo *bi;
1930 if(dev->allocationBlock < 0)
1932 // Get next block to allocate off
1933 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
1934 dev->allocationPage = 0;
1937 if(!useReserve && dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS)
1939 // Not enough space to allocate unless we're allowed to use the reserve.
1943 // Next page please....
1944 if(dev->allocationBlock >= 0)
1946 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
1948 retVal = (dev->allocationBlock * YAFFS_CHUNKS_PER_BLOCK) +
1949 dev->allocationPage;
1951 bi->pageBits |= (1 << (dev->allocationPage));
1953 dev->allocationPage++;
1957 // If the block is full set the state to full
1958 if(dev->allocationPage >= YAFFS_CHUNKS_PER_BLOCK)
1960 bi->blockState = YAFFS_BLOCK_STATE_FULL;
1961 dev->allocationBlock = -1;
1968 T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
1973 // To determine if we have enough space we just look at the
1974 // number of erased blocks.
1975 // The cache is allowed to use reserved blocks.
1977 int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
1979 return (dev->nErasedBlocks >= YAFFS_RESERVED_BLOCKS);
1983 int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
1992 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
1994 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
1996 yaffs_Object *object;
1998 //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
2000 for(mask = 1,oldChunk = block * YAFFS_CHUNKS_PER_BLOCK;
2001 mask && bi->pageBits;
2002 mask <<= 1, oldChunk++ )
2004 if(bi->pageBits & mask)
2007 // This page is in use and needs to be copied off
2011 //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2013 yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare,1);
2015 yaffs_GetTagsFromSpare(dev,&spare,&tags);
2017 object = yaffs_FindObjectByNumber(dev,tags.objectId);
2019 if(object && object->deleted && tags.chunkId != 0)
2021 // Data chunk in a deleted file, throw it away
2022 // It's a deleted data chunk,
2023 // No need to copy this, just forget about it and fix up the
2026 yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0);
2027 object->nDataChunks--;
2029 else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2031 // Deleted object header with no data chunks.
2033 object->chunkId = 0;
2034 //Todo some clean up
2039 // It's either a data chunk in a live file or
2040 // an ObjectHeader, so we're interested in it.
2041 // NB Need to keep the ObjectHeaders of deleted files
2042 // until the whole file has been deleted off
2043 tags.serialNumber++;
2044 yaffs_LoadTagsIntoSpare(&spare,&tags);
2047 newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1);
2054 // Ok, now fix up the Tnodes etc.
2056 if(tags.chunkId == 0)
2059 object->chunkId = newChunk;
2063 // It's a data chunk
2064 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2068 yaffs_DeleteChunk(dev,oldChunk);
2077 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2079 // todo find a file to delete
2080 struct list_head *i;
2084 // To the free chunks add the chunks that are in the deleted unlinked files.
2085 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2087 l = list_entry(i, yaffs_Object,siblings);
2096 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2098 // This does background deletion on unlinked files.. only deleted ones.
2099 // If we don't have a file we're working on then find one
2100 if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2102 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2105 // OK, we're working on a file...
2106 if(dev->unlinkedDeletion)
2108 yaffs_Object *obj = dev->unlinkedDeletion;
2110 int limit; // Number of chunks to delete in a file.
2111 // NB this can be exceeded, but not by much.
2115 if(dev->nErasedBlocks <= (YAFFS_RESERVED_BLOCKS + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2117 limit = 50; // Doing GC soon, so dig deeper
2125 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2127 if(obj->nDataChunks == 0)
2129 // Done all the deleting of data chunks.
2130 // Now dump the header and clean up
2131 yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2132 yaffs_DoGenericObjectDeletion(obj);
2133 dev->nDeletedFiles--;
2134 dev->nUnlinkedFiles--;
2135 dev->nBackgroundDeletions++;
2136 dev->unlinkedDeletion = NULL;
2143 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2147 yaffs_DoUnlinkedFileDeletion(dev);
2149 if(dev->nErasedBlocks <= (YAFFS_RESERVED_BLOCKS + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2151 dev->garbageCollectionRequired = 1;
2154 if(dev->garbageCollectionRequired)
2156 dev->garbageCollections++;
2157 dev->garbageCollectionRequired = 0;
2158 if(dev->blockSelectedForGC >= 0)
2160 block = dev->blockSelectedForGC;
2164 block = yaffs_FindDirtiestBlock(dev);
2169 return yaffs_GarbageCollectBlock(dev,block);
2181 //////////////////////////// TAGS ///////////////////////////////////////
2183 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2185 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2187 yaffs_CalcTagsECC(tagsPtr);
2189 sparePtr->tagByte0 = tu->asBytes[0];
2190 sparePtr->tagByte1 = tu->asBytes[1];
2191 sparePtr->tagByte2 = tu->asBytes[2];
2192 sparePtr->tagByte3 = tu->asBytes[3];
2193 sparePtr->tagByte4 = tu->asBytes[4];
2194 sparePtr->tagByte5 = tu->asBytes[5];
2195 sparePtr->tagByte6 = tu->asBytes[6];
2196 sparePtr->tagByte7 = tu->asBytes[7];
2199 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2201 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2204 tu->asBytes[0]= sparePtr->tagByte0;
2205 tu->asBytes[1]= sparePtr->tagByte1;
2206 tu->asBytes[2]= sparePtr->tagByte2;
2207 tu->asBytes[3]= sparePtr->tagByte3;
2208 tu->asBytes[4]= sparePtr->tagByte4;
2209 tu->asBytes[5]= sparePtr->tagByte5;
2210 tu->asBytes[6]= sparePtr->tagByte6;
2211 tu->asBytes[7]= sparePtr->tagByte7;
2213 result = yaffs_CheckECCOnTags(tagsPtr);
2216 dev->tagsEccFixed++;
2220 dev->tagsEccUnfixed++;
2224 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2226 memset(spare,0xFF,sizeof(yaffs_Spare));
2229 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted)
2234 if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare,1) == YAFFS_OK)
2236 *chunkDeleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
2237 yaffs_GetTagsFromSpare(dev,&spare,tags);
2250 static int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_Tags *tags)
2252 // NB There must be tags, data is optional
2253 // If there is data, then an ECC is calculated on it.
2262 yaffs_SpareInitialise(&spare);
2264 #ifndef CONFIG_YAFFS_USE_NANDECC
2267 yaffs_CalcECC(buffer,&spare);
2271 yaffs_LoadTagsIntoSpare(&spare,tags);
2273 return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare);
2279 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_Tags *tags, int useReserve)
2281 // NB There must be tags, data is optional
2282 // If there is data, then an ECC is calculated on it.
2291 yaffs_SpareInitialise(&spare);
2293 #ifndef CONFIG_YAFFS_USE_NANDECC
2297 yaffs_CalcECC(buffer,&spare);
2301 yaffs_LoadTagsIntoSpare(&spare,tags);
2303 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2307 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted)
2309 return ( tags->chunkId == chunkInObject &&
2310 tags->objectId == objectId &&
2311 !chunkDeleted) ? 1 : 0;
2317 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
2319 //Get the Tnode, then get the level 0 offset chunk offset
2322 yaffs_Tags localTags;
2327 yaffs_Device *dev = in->myDev;
2332 // Passed a NULL, so use our own tags space
2336 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2340 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2342 // Now we need to do the shifting etc and search for it
2343 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
2345 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
2346 if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
2357 return found ? theChunk : -1;
2360 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
2362 //Get the Tnode, then get the level 0 offset chunk offset
2365 yaffs_Tags localTags;
2368 yaffs_Device *dev = in->myDev;
2373 // Passed a NULL, so use our own tags space
2377 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2382 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2384 // Now we need to do the shifting etc and search for it
2385 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
2387 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
2388 if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
2399 // Delete the entry in the filestructure
2402 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2407 //T(("No level 0 found for %d\n", chunkInInode));
2412 //T(("Could not find %d to delete\n",chunkInInode));
2414 return found ? theChunk : -1;
2418 #ifdef YAFFS_PARANOID
2420 static int yaffs_CheckFileSanity(yaffs_Object *in)
2428 yaffs_Tags localTags;
2429 yaffs_Tags *tags = &localTags;
2434 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2436 //T(("Object not a file\n"));
2440 objId = in->objectId;
2441 fSize = in->variant.fileVariant.fileSize;
2442 nChunks = (fSize + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
2444 for(chunk = 1; chunk <= nChunks; chunk++)
2446 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
2451 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
2454 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
2455 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
2462 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
2463 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
2472 //T(("No level 0 found for %d\n", chunk));
2476 return failed ? YAFFS_FAIL : YAFFS_OK;
2481 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
2484 yaffs_Device *dev = in->myDev;
2486 yaffs_Tags existingTags;
2488 unsigned existingSerial, newSerial;
2490 int newChunkDeleted;
2493 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2499 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
2503 // If we're scanning then we need to test for duplicates
2504 // NB This does not need to be efficient since it should only ever
2505 // happen when the power fails during a write, then only one
2506 // chunk should ever be affected.
2509 if(existingChunk != 0)
2511 // NB Right now existing chunk will not be real chunkId if the device >= 32MB
2512 // thus we have to do a FindChunkInFile to get the real chunk id.
2514 // We have a duplicate now we need to decide which one to use
2515 // To do this we get both sets of tags and compare serial numbers.
2516 yaffs_ReadChunkTagsFromNAND(dev,chunkInNAND, &newTags,&newChunkDeleted);
2520 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
2522 if(existingChunk <=0)
2524 //Hoosterman - how did this happen?
2526 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: existing chunk < 0 in scan" TENDSTR)));
2531 // NB The deleted flags should be false, otherwise the chunks will
2532 // not be loaded during a scan
2534 newSerial = newTags.serialNumber;
2535 existingSerial = existingTags.serialNumber;
2537 if( existingChunk <= 0 ||
2538 ((existingSerial+1) & 3) == newSerial)
2541 // Delete the old one and drop through to update the tnode
2542 yaffs_DeleteChunk(dev,existingChunk);
2547 // Delete the new one and return early so that the tnode isn't changed
2548 yaffs_DeleteChunk(dev,chunkInNAND);
2555 if(existingChunk == 0)
2560 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
2567 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
2569 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
2571 if(chunkInNAND >= 0)
2573 return yaffs_ReadChunkFromNAND(in->myDev,chunkInNAND,buffer,NULL,1);
2583 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId)
2588 yaffs_BlockInfo *bi;
2590 if(chunkId <= 0) return;
2592 block = chunkId / YAFFS_CHUNKS_PER_BLOCK;
2593 page = chunkId % YAFFS_CHUNKS_PER_BLOCK;
2594 yaffs_SpareInitialise(&spare);
2596 spare.pageStatus = 0; // To mark it as deleted.
2599 yaffs_WriteChunkToNAND(dev,chunkId,NULL,&spare);
2600 yaffs_HandleUpdateChunk(dev,chunkId,&spare);
2601 bi = yaffs_GetBlockInfo(dev,block);
2604 // Pull out of the management area.
2605 // If the whole block became dirty, this will kick off an erasure.
2606 if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
2607 bi->blockState == YAFFS_BLOCK_STATE_FULL)
2611 bi->pageBits &= ~(1 << page);
2614 if(bi->pagesInUse == 0 &&
2615 bi->blockState == YAFFS_BLOCK_STATE_FULL)
2617 yaffs_BlockBecameDirty(dev,block);
2623 // T(("Bad news deleting chunk %d\n",chunkId));
2631 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
2633 // Find old chunk Need to do this to get serial number
2634 // Write new one and patch into tree.
2635 // Invalidate old tags.
2638 yaffs_Tags prevTags;
2643 yaffs_Device *dev = in->myDev;
2645 yaffs_CheckGarbageCollection(dev);
2647 // Get the previous chunk at this location in the file if it exists
2648 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
2651 newTags.chunkId = chunkInInode;
2652 newTags.objectId = in->objectId;
2653 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
2654 newTags.byteCount = nBytes;
2655 newTags.unusedStuff = 0xFFFFFFFF;
2657 yaffs_CalcTagsECC(&newTags);
2659 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
2662 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
2665 if(prevChunkId >= 0)
2667 yaffs_DeleteChunk(dev,prevChunkId);
2671 yaffs_CheckFileSanity(in);
2682 // UpdateObjectHeader updates the header on NAND for an object.
2683 // If name is not NULL, then that new name is used.
2685 int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force)
2688 yaffs_Device *dev = in->myDev;
2694 __u8 bufferNew[YAFFS_BYTES_PER_CHUNK];
2695 __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
2697 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bufferNew;
2698 yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
2701 if(!in->fake || force)
2704 yaffs_CheckGarbageCollection(dev);
2706 memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK);
2708 prevChunkId = in->chunkId;
2710 if(prevChunkId >= 0)
2712 yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL,1);
2716 oh->type = in->variantType;
2718 oh->st_mode = in->st_mode;
2720 #ifdef CONFIG_YAFFS_WINCE
2721 oh->win_atime[0] = in->win_atime[0];
2722 oh->win_ctime[0] = in->win_ctime[0];
2723 oh->win_mtime[0] = in->win_mtime[0];
2724 oh->win_atime[1] = in->win_atime[1];
2725 oh->win_ctime[1] = in->win_ctime[1];
2726 oh->win_mtime[1] = in->win_mtime[1];
2728 oh->st_uid = in->st_uid;
2729 oh->st_gid = in->st_gid;
2730 oh->st_atime = in->st_atime;
2731 oh->st_mtime = in->st_mtime;
2732 oh->st_ctime = in->st_ctime;
2733 oh->st_rdev = in->st_rdev;
2737 oh->parentObjectId = in->parent->objectId;
2741 oh->parentObjectId = 0;
2744 //oh->sum = in->sum;
2747 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
2748 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
2750 else if(prevChunkId)
2752 memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1);
2756 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
2759 switch(in->variantType)
2761 case YAFFS_OBJECT_TYPE_UNKNOWN:
2762 // Should not happen
2764 case YAFFS_OBJECT_TYPE_FILE:
2765 oh->fileSize = in->variant.fileVariant.fileSize;
2767 case YAFFS_OBJECT_TYPE_HARDLINK:
2768 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
2770 case YAFFS_OBJECT_TYPE_SPECIAL:
2773 case YAFFS_OBJECT_TYPE_DIRECTORY:
2776 case YAFFS_OBJECT_TYPE_SYMLINK:
2777 strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
2778 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
2784 newTags.chunkId = 0;
2785 newTags.objectId = in->objectId;
2786 newTags.serialNumber = in->serial;
2787 newTags.byteCount = 0xFFFFFFFF;
2788 newTags.unusedStuff = 0xFFFFFFFF;
2790 yaffs_CalcTagsECC(&newTags);
2793 // Create new chunk in NAND
2794 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags,1);
2799 in->chunkId = newChunkId;
2801 if(prevChunkId >= 0)
2803 yaffs_DeleteChunk(dev,prevChunkId);
2816 /////////////////////// Short Operations Cache ////////////////////////////////
2817 // In many siturations where there is no high level buffering (eg WinCE) a lot of
2818 // reads might be short sequential reads, and a lot of writes may be short
2819 // sequential writes. eg. scanning/writing a jpeg file.
2820 // In these cases, a short read/write cache can provide a huge perfomance benefit
2821 // with dumb-as-a-rock code.
2822 // There are a limited number (~10) of cache chunks per device so that we don't
2823 // need a very intelligent search.
2826 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
2829 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
2831 yaffs_Device *dev = obj->myDev;
2834 yaffs_ChunkCache *cache;
2841 // Find the dirty cache for this object with the lowest chunk id.
2842 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
2844 if(dev->srCache[i].object == obj &&
2845 dev->srCache[i].dirty)
2847 if(!cache || dev->srCache[i].chunkId < lowest)
2849 cache = &dev->srCache[i];
2850 lowest = cache->chunkId;
2859 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
2861 if(nBytes > YAFFS_BYTES_PER_CHUNK)
2863 nBytes= YAFFS_BYTES_PER_CHUNK;
2866 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
2874 } while(cache && chunkWritten > 0);
2878 //Hoosterman, disk full while writing cache out.
2879 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: no space during caceh write" TENDSTR)));
2886 // Grab us a chunk for use.
2887 // First look for an empty one.
2888 // Then look for the least recently used non-dirty one.
2889 // Then look for the least recently used dirty one...., flush and look again.
2890 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
2896 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
2898 if(!dev->srCache[i].object)
2900 //T(("Grabbing empty %d\n",i));
2902 return &dev->srCache[i];
2907 usage = 0; // just to stop the compiler grizzling
2909 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
2911 if(!dev->srCache[i].dirty &&
2912 ((dev->srCache[i].lastUse < usage && theOne >= 0)||
2915 usage = dev->srCache[i].lastUse;
2920 //T(("Grabbing non-empty %d\n",theOne));
2921 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
2926 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
2928 yaffs_ChunkCache *cache;
2929 yaffs_Object *theObj;
2933 // Try find a non-dirty one...
2935 cache = yaffs_GrabChunkCacheWorker(dev);
2939 // They were all dirty, find the last recently used object and flush
2940 // its cache, then find again.
2941 // NB what's here is not very accurate, we actually flush the object
2942 // the last recently used page.
2944 theObj = dev->srCache[0].object;
2945 usage = dev->srCache[0].lastUse;
2947 for(i = 1; i < YAFFS_N_CACHE_CHUNKS; i++)
2949 if( dev->srCache[i].object &&
2950 dev->srCache[i].lastUse < usage)
2952 usage = dev->srCache[i].lastUse;
2953 theObj = dev->srCache[i].object;
2957 yaffs_FlushFilesChunkCache(theObj);
2960 cache = yaffs_GrabChunkCacheWorker(dev);
2968 // Find a cached chunk
2969 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
2971 yaffs_Device *dev = obj->myDev;
2974 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
2976 if(dev->srCache[i].object == obj &&
2977 dev->srCache[i].chunkId == chunkId)
2981 return &dev->srCache[i];
2988 // Mark the chunk for the least recently used algorithym
2989 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
2991 if( dev->srLastUse < 0 ||
2992 dev->srLastUse > 100000000)
2994 // Reset the cache usages
2996 for(i = 1; i < YAFFS_N_CACHE_CHUNKS; i++)
2998 dev->srCache[i].lastUse = 0;
3005 cache->lastUse = dev->srLastUse;
3013 // Invalidate a single cache page.
3014 // Do this when a whole page gets written,
3015 // ie the short cache for this page is no longer valid.
3016 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3018 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3022 cache->object = NULL;
3027 // Invalidate all the cache pages associated with this object
3028 // Do this whenever ther file is deleted or resized.
3029 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3032 yaffs_Device *dev = in->myDev;
3034 // Now invalidate it.
3035 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
3037 if(dev->srCache[i].object == in)
3039 dev->srCache[i].object = NULL;
3046 // Stubs for disabling short op caching
3048 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3051 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3057 static yaffs_ChunkCache *yaffs_FindChunkCache(yaffs_Device *dev, int objectId, int chunkId)
3062 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache)
3066 static void yaffs_InvalidateChunkCache(yaffs_Object *obj, int chunkId)
3070 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3080 ///////////////////////// File read/write ///////////////////////////////
3081 // Read and write have very similar structures.
3082 // In general the read/write has three parts to it
3083 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3084 // * Some complete chunks
3085 // * An incomplete chunk to end off with
3087 // Curve-balls: the first chunk might also be the last chunk.
3089 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3105 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; // The first chunk is 1
3106 start = offset % YAFFS_BYTES_PER_CHUNK;
3108 // OK now check for the curveball where the start and end are in
3110 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
3116 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
3119 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
3121 // An incomplete start or end chunk (or maybe both start and end chunk)
3122 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
3123 yaffs_ChunkCache *cache;
3124 // If we can't find the data in the cache, then load it up.
3125 cache = yaffs_FindChunkCache(in,chunk);
3128 cache = yaffs_GrabChunkCache(in->myDev);
3130 cache->chunkId = chunk;
3132 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3135 yaffs_UseChunkCache(dev,cache,0);
3137 memcpy(buffer,&cache->data[start],nToCopy);
3139 // Read into the local buffer then copy...
3140 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3141 memcpy(buffer,&dev->localBuffer[start],nToCopy);
3146 #ifdef CONFIG_YAFFS_WINCE
3148 // Under WinCE can't do direct transfer. Need to use a local buffer.
3149 // This is because we otherwise screw up WinCE's memory mapper
3150 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3151 memcpy(buffer,dev->localBuffer,YAFFS_BYTES_PER_CHUNK);
3153 // A full chunk. Read directly into the supplied buffer.
3154 yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3170 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3179 int startOfWrite = offset;
3180 int chunkWritten = 0;
3188 while(n > 0 && chunkWritten >= 0)
3190 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1;
3191 start = offset % YAFFS_BYTES_PER_CHUNK;
3194 // OK now check for the curveball where the start and end are in
3197 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
3201 // Now folks, to calculate how many bytes to write back....
3202 // If we're overwriting and not writing to then end of file then
3203 // we need to write back as much as was there before.
3205 nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * YAFFS_BYTES_PER_CHUNK);
3207 if(nBytesRead > YAFFS_BYTES_PER_CHUNK)
3209 nBytesRead = YAFFS_BYTES_PER_CHUNK;
3212 nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3217 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
3218 nToWriteBack = YAFFS_BYTES_PER_CHUNK;
3221 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
3223 // An incomplete start or end chunk (or maybe both start and end chunk)
3224 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
3225 yaffs_ChunkCache *cache;
3226 // If we can't find the data in the cache, then load it up.
3227 cache = yaffs_FindChunkCache(in,chunk);
3228 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3230 cache = yaffs_GrabChunkCache(in->myDev);
3232 cache->chunkId = chunk;
3234 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3239 yaffs_UseChunkCache(dev,cache,1);
3240 memcpy(&cache->data[start],buffer,nToCopy);
3244 chunkWritten = -1; // fail the write
3249 // An incomplete start or end chunk (or maybe both start and end chunk)
3250 // Read into the local buffer then copy, then copy over and write back.
3252 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3254 memcpy(&dev->localBuffer[start],buffer,nToCopy);
3256 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,nToWriteBack,0);
3258 //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3265 #ifdef CONFIG_YAFFS_WINCE
3266 // Under WinCE can't do direct transfer. Need to use a local buffer.
3267 // This is because we otherwise screw up WinCE's memory mapper
3268 memcpy(dev->localBuffer,buffer,YAFFS_BYTES_PER_CHUNK);
3269 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,YAFFS_BYTES_PER_CHUNK,0);
3271 // A full chunk. Write directly from the supplied buffer.
3272 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0);
3274 // Since we've overwritten the cached data, we better invalidate it.
3275 yaffs_InvalidateChunkCache(in,chunk);
3276 //T(("Write to chunk %d %d\n",chunk,chunkWritten));
3279 if(chunkWritten >= 0)
3289 // Update file object
3291 if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
3293 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
3302 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
3306 int oldFileSize = in->variant.fileVariant.fileSize;
3307 int sizeOfPartialChunk = newSize % YAFFS_BYTES_PER_CHUNK;
3309 yaffs_Device *dev = in->myDev;
3312 yaffs_FlushFilesChunkCache(in);
3313 yaffs_InvalidateWholeChunkCache(in);
3315 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3317 return yaffs_GetFileSize(in);
3320 if(newSize < oldFileSize)
3323 int lastDel = 1 + (oldFileSize-1)/YAFFS_BYTES_PER_CHUNK;
3325 int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/
3326 YAFFS_BYTES_PER_CHUNK;
3328 // Delete backwards so that we don't end up with holes if
3329 // power is lost part-way through the operation.
3330 for(i = lastDel; i >= startDel; i--)
3332 // NB this could be optimised somewhat,
3333 // eg. could retrieve the tags and write them without
3334 // using yaffs_DeleteChunk
3336 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
3337 if(chunkId <= 0 || chunkId >= (dev->endBlock * 32))
3339 //T(("Found daft chunkId %d for %d\n",chunkId,i));
3344 yaffs_DeleteChunk(dev,chunkId);
3349 if(sizeOfPartialChunk != 0)
3351 int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK;
3353 // Got to read and rewrite the last chunk with its new size.
3354 yaffs_ReadChunkDataFromObject(in,lastChunk,dev->localBuffer);
3356 yaffs_WriteChunkDataToObject(in,lastChunk,dev->localBuffer,sizeOfPartialChunk,1);
3360 in->variant.fileVariant.fileSize = newSize;
3362 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
3374 loff_t yaffs_GetFileSize(yaffs_Object *obj)
3376 obj = yaffs_GetEquivalentObject(obj);
3378 switch(obj->variantType)
3380 case YAFFS_OBJECT_TYPE_FILE:
3381 return obj->variant.fileVariant.fileSize;
3382 case YAFFS_OBJECT_TYPE_SYMLINK:
3383 return strlen(obj->variant.symLinkVariant.alias);
3391 // yaffs_FlushFile() updates the file's
3394 int yaffs_FlushFile(yaffs_Object *in)
3399 //T(("flushing object header\n"));
3401 yaffs_FlushFilesChunkCache(in);
3403 #ifdef CONFIG_YAFFS_WINCE
3404 yfsd_WinFileTimeNow(in->win_mtime);
3406 in->st_mtime = CURRENT_TIME;
3409 retVal = yaffs_UpdateObjectHeader(in,NULL,0);
3421 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
3424 // First off, invalidate the file's data in the cache, without flushing.
3425 yaffs_InvalidateWholeChunkCache(in);
3427 yaffs_RemoveObjectFromDirectory(in);
3428 yaffs_DeleteChunk(in->myDev,in->chunkId);
3432 in->myInode->u.generic_ip = NULL;
3436 yaffs_FreeObject(in);
3441 // yaffs_DeleteFile deletes the whole file data
3442 // and the inode associated with the file.
3443 // It does not delete the links associated with the file.
3444 static int yaffs_UnlinkFile(yaffs_Object *in)
3447 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
3448 // Delete the file data & tnodes
3450 yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
3453 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
3455 return yaffs_DoGenericObjectDeletion(in);
3458 int immediateDeletion=0;
3459 retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
3460 if(retVal == YAFFS_OK)
3463 //in->myDev->nUnlinkedFiles++;
3464 //in->renameAllowed = 0;
3468 immediateDeletion = 1;
3472 #ifdef CONFIG_YAFFS_WINCE
3475 immediateDeletion = 1;
3480 if(immediateDeletion)
3482 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
3484 in->myDev->nDeletedFiles++;
3494 int yaffs_DeleteFile(yaffs_Object *in)
3496 int retVal = YAFFS_OK;
3499 retVal = yaffs_UnlinkFile(in);
3501 if(retVal == YAFFS_OK &&
3506 in->myDev->nDeletedFiles++;
3508 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
3511 static int yaffs_DeleteDirectory(yaffs_Object *in)
3513 //First check that the directory is empty.
3514 if(list_empty(&in->variant.directoryVariant.children))
3516 return yaffs_DoGenericObjectDeletion(in);
3523 static int yaffs_DeleteSymLink(yaffs_Object *in)
3525 YFREE(in->variant.symLinkVariant.alias);
3527 return yaffs_DoGenericObjectDeletion(in);
3530 static int yaffs_DeleteHardLink(yaffs_Object *in)
3532 // remove this hardlink from the list assocaited with the equivalent
3534 list_del(&in->hardLinks);
3535 return yaffs_DoGenericObjectDeletion(in);
3539 static int yaffs_UnlinkWorker(yaffs_Object *obj)
3543 if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
3545 return yaffs_DeleteHardLink(obj);
3547 else if(!list_empty(&obj->hardLinks))
3549 // Curve ball: We're unlinking an object that has a hardlink.
3551 // This problem arises because we are not strictly following
3552 // The Linux link/inode model.
3554 // We can't really delete the object.
3555 // Instead, we do the following:
3556 // - Select a hardlink.
3557 // - Unhook it from the hard links
3558 // - Unhook it from its parent directory (so that the rename can work)
3559 // - Rename the object to the hardlink's name.
3560 // - Delete the hardlink
3565 char name[YAFFS_MAX_NAME_LENGTH+1];
3567 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
3569 list_del_init(&hl->hardLinks);
3570 list_del_init(&hl->siblings);
3572 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
3574 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
3576 if(retVal == YAFFS_OK)
3578 retVal = yaffs_DoGenericObjectDeletion(hl);
3585 switch(obj->variantType)
3587 case YAFFS_OBJECT_TYPE_FILE:
3588 return yaffs_UnlinkFile(obj);
3590 case YAFFS_OBJECT_TYPE_DIRECTORY:
3591 return yaffs_DeleteDirectory(obj);
3593 case YAFFS_OBJECT_TYPE_SYMLINK:
3594 return yaffs_DeleteSymLink(obj);
3596 case YAFFS_OBJECT_TYPE_HARDLINK:
3597 case YAFFS_OBJECT_TYPE_UNKNOWN:
3604 int yaffs_Unlink(yaffs_Object *dir, const char *name)
3608 obj = yaffs_FindObjectByName(dir,name);
3610 if(obj && obj->unlinkAllowed)
3612 return yaffs_UnlinkWorker(obj);
3619 //////////////// Initialisation Scanning /////////////////
3623 // For now we use the SmartMedia check.
3624 // We look at the blockStatus byte in the first two chunks
3625 // These must be 0xFF to pass as OK.
3626 // todo: this function needs to be modifyable foir different NAND types
3627 // and different chunk sizes. Suggest make this into a per-device configurable
3629 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
3633 yaffs_ReadChunkFromNAND(dev,blk * YAFFS_CHUNKS_PER_BLOCK,NULL,&spare,1);
3635 if(yaffs_CountBits(spare.blockStatus) < 7)
3640 if(spare.blockStatus != 0xFF)
3645 yaffs_ReadChunkFromNAND(dev,blk * YAFFS_CHUNKS_PER_BLOCK + 1,NULL,&spare,1);
3648 if(yaffs_CountBits(spare.blockStatus) < 7)
3653 if(spare.blockStatus != 0xFF)
3663 static int yaffs_Scan(yaffs_Device *dev)
3671 yaffs_BlockState state;
3672 yaffs_Object *hardList = NULL;
3674 yaffs_BlockInfo *bi;
3677 yaffs_ObjectHeader *oh;
3679 yaffs_Object *parent;
3681 __u8 chunkData[YAFFS_BYTES_PER_CHUNK];
3684 // Scan all the blocks...
3686 for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
3689 bi = yaffs_GetBlockInfo(dev,blk);
3692 state = YAFFS_BLOCK_STATE_SCANNING;
3695 if(yaffs_IsBlockBad(dev,blk))
3697 state = YAFFS_BLOCK_STATE_DEAD;
3698 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
3701 // Read each chunk in the block.
3703 for(c = 0; c < YAFFS_CHUNKS_PER_BLOCK &&
3704 state == YAFFS_BLOCK_STATE_SCANNING; c++)
3706 // Read the spare area and decide what to do
3707 chunk = blk * YAFFS_CHUNKS_PER_BLOCK + c;
3709 yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare,1);
3712 // This block looks ok, now what's in this chunk?
3713 yaffs_GetTagsFromSpare(dev,&spare,&tags);
3715 if(yaffs_CountBits(spare.pageStatus) < 6)
3719 dev->nFreeChunks ++;
3720 //T((" %d %d deleted\n",blk,c));
3722 else if(tags.objectId == YAFFS_UNUSED_OBJECT_ID)
3724 // An unassigned chunk in the block
3725 // This means that either the block is empty or
3726 // this is the one being allocated from
3730 // the block is unused
3731 state = YAFFS_BLOCK_STATE_EMPTY;
3732 dev->nErasedBlocks++;
3736 // this is the block being allocated from
3737 T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
3738 state = YAFFS_BLOCK_STATE_ALLOCATING;
3739 dev->allocationBlock = blk;
3740 dev->allocationPage = c;
3743 dev->nFreeChunks += (YAFFS_CHUNKS_PER_BLOCK - c);
3745 else if(tags.chunkId > 0)
3749 bi->pageBits |= (1 << c);
3752 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
3753 // PutChunkIntoFIle checks for a clash (two data chunks with
3754 // the same chunkId).
3755 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
3756 endpos = (tags.chunkId - 1)* YAFFS_BYTES_PER_CHUNK + tags.byteCount;
3757 if(in->variant.fileVariant.scannedFileSize <endpos)
3759 in->variant.fileVariant.scannedFileSize = endpos;
3760 #ifndef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
3761 in->variant.fileVariant.fileSize =
3762 in->variant.fileVariant.scannedFileSize;
3766 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
3770 // chunkId == 0, so it is an ObjectHeader.
3771 // Thus, we read in the object header and make the object
3772 bi->pageBits |= (1 << c);
3775 yaffs_ReadChunkFromNAND(dev,chunk,chunkData,NULL,1);
3777 oh = (yaffs_ObjectHeader *)chunkData;
3779 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
3783 // We have already filled this one. We have a duplicate and need to resolve it.
3785 unsigned existingSerial = in->serial;
3786 unsigned newSerial = tags.serialNumber;
3788 if(((existingSerial+1) & 3) == newSerial)
3790 // Use new one - destroy the exisiting one
3791 yaffs_DeleteChunk(dev,in->chunkId);
3796 // Use existing - destroy this one.
3797 yaffs_DeleteChunk(dev,chunk);
3802 (tags.objectId == YAFFS_OBJECTID_ROOT ||
3803 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
3805 // We only load some info, don't fiddle with directory structure
3807 in->variantType = oh->type;
3809 in->st_mode = oh->st_mode;
3810 #ifdef CONFIG_YAFFS_WINCE
3811 in->win_atime[0] = oh->win_atime[0];
3812 in->win_ctime[0] = oh->win_ctime[0];
3813 in->win_mtime[0] = oh->win_mtime[0];
3814 in->win_atime[1] = oh->win_atime[1];
3815 in->win_ctime[1] = oh->win_ctime[1];
3816 in->win_mtime[1] = oh->win_mtime[1];
3818 in->st_uid = oh->st_uid;
3819 in->st_gid = oh->st_gid;
3820 in->st_atime = oh->st_atime;
3821 in->st_mtime = oh->st_mtime;
3822 in->st_ctime = oh->st_ctime;
3823 in->st_rdev = oh->st_rdev;
3825 in->chunkId = chunk;
3830 // we need to load this info
3833 in->variantType = oh->type;
3835 in->st_mode = oh->st_mode;
3836 #ifdef CONFIG_YAFFS_WINCE
3837 in->win_atime[0] = oh->win_atime[0];
3838 in->win_ctime[0] = oh->win_ctime[0];
3839 in->win_mtime[0] = oh->win_mtime[0];
3840 in->win_atime[1] = oh->win_atime[1];
3841 in->win_ctime[1] = oh->win_ctime[1];
3842 in->win_mtime[1] = oh->win_mtime[1];
3844 in->st_uid = oh->st_uid;
3845 in->st_gid = oh->st_gid;
3846 in->st_atime = oh->st_atime;
3847 in->st_mtime = oh->st_mtime;
3848 in->st_ctime = oh->st_ctime;
3849 in->st_rdev = oh->st_rdev;
3851 in->chunkId = chunk;
3853 yaffs_SetObjectName(in,oh->name);
3856 // directory stuff...
3857 // hook up to parent
3859 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
3860 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
3862 // Set up as a directory
3863 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
3864 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
3866 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
3868 // Hoosterman, another problem....
3869 // We're trying to use a non-directory as a directory
3871 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: attempting to use non-directory as a directory in scan" TENDSTR)));
3875 yaffs_AddObjectToDirectory(parent,in);
3876 if(parent == dev->unlinkedDir)
3878 in->deleted = 1; // If it is unlinked at start up then it wants deleting
3879 dev->nDeletedFiles++;
3882 // Note re hardlinks.
3883 // Since we might scan a hardlink before its equivalent object is scanned
3884 // we put them all in a list.
3885 // After scanning is complete, we should have all the objects, so we run through this
3886 // list and fix up all the chains.
3888 switch(in->variantType)
3890 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
3892 case YAFFS_OBJECT_TYPE_FILE:
3893 #ifdef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
3894 in->variant.fileVariant.fileSize = oh->fileSize;
3897 case YAFFS_OBJECT_TYPE_HARDLINK:
3898 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
3899 (yaffs_Object *)(in->hardLinks.next) = hardList;
3902 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
3904 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
3906 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
3907 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
3910 //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
3915 if(state == YAFFS_BLOCK_STATE_SCANNING)
3917 // If we got this far while scanning, then the block is fully allocated.
3918 state = YAFFS_BLOCK_STATE_FULL;
3921 bi->blockState = state;
3923 // Now let's see if it was dirty
3924 if( bi->pagesInUse == 0 &&
3925 bi->blockState == YAFFS_BLOCK_STATE_FULL)
3927 yaffs_BlockBecameDirty(dev,blk);
3932 // Fix up the hard link chains.
3933 // We should now have scanned all the objects, now it's time to add these
3938 hardList = (yaffs_Object *)(hardList->hardLinks.next);
3940 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
3944 // Add the hardlink pointers
3945 hl->variant.hardLinkVariant.equivalentObject=in;
3946 list_add(&hl->hardLinks,&in->hardLinks);
3950 //Todo Need to report/handle this better.
3951 // Got a problem... hardlink to a non-existant object
3952 hl->variant.hardLinkVariant.equivalentObject=NULL;
3953 INIT_LIST_HEAD(&hl->hardLinks);
3965 ////////////////////////// Directory Functions /////////////////////////
3968 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
3971 if(obj->siblings.prev == NULL)
3974 INIT_LIST_HEAD(&obj->siblings);
3977 else if(!list_empty(&obj->siblings))
3979 // If it is holed up somewhere else, un hook it
3980 list_del_init(&obj->siblings);
3983 list_add(&obj->siblings,&directory->variant.directoryVariant.children);
3984 obj->parent = directory;
3986 if(directory == obj->myDev->unlinkedDir)
3989 obj->myDev->nUnlinkedFiles++;
3990 obj->renameAllowed = 0;
3994 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
3996 list_del_init(&obj->siblings);
4000 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const char *name)
4004 struct list_head *i;
4005 char buffer[YAFFS_MAX_NAME_LENGTH+1];
4009 sum = yaffs_CalcNameSum(name);
4011 list_for_each(i,&directory->variant.directoryVariant.children)
4013 l = list_entry(i, yaffs_Object,siblings);
4015 // Special case for lost-n-found
4016 if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4018 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
4023 else if(yaffs_SumCompare(l->sum, sum))
4026 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
4027 if(yaffs_strcmp(name,buffer) == 0)
4040 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
4042 struct list_head *i;
4046 list_for_each(i,&theDir->variant.directoryVariant.children)
4048 l = list_entry(i, yaffs_Object,siblings);
4060 // GetEquivalentObject dereferences any hard links to get to the
4063 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
4065 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4067 // We want the object id of the equivalent object, not this one
4068 obj = obj->variant.hardLinkVariant.equivalentObject;
4074 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize)
4076 memset(name,0,buffSize);
4078 if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4080 strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
4082 else if(obj->chunkId <= 0)
4086 sprintf(locName,"%s%d",YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
4087 strncpy(name,locName,buffSize - 1);
4090 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4091 else if(obj->shortName[0])
4093 strcpy(name,obj->shortName);
4098 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
4099 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
4101 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
4103 if(obj->chunkId >= 0)
4105 yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL,1);
4107 strncpy(name,oh->name,buffSize - 1);
4110 return strlen(name);
4113 int yaffs_GetObjectFileLength(yaffs_Object *obj)
4116 // Dereference any hard linking
4117 obj = yaffs_GetEquivalentObject(obj);
4119 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4121 return obj->variant.fileVariant.fileSize;
4123 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
4125 return strlen(obj->variant.symLinkVariant.alias);
4129 // Only a directory should drop through to here
4130 return YAFFS_BYTES_PER_CHUNK;
4134 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
4137 struct list_head *i;
4141 count++; // the object itself
4143 list_for_each(i,&obj->hardLinks)
4145 count++; // add the hard links;
4152 int yaffs_GetObjectInode(yaffs_Object *obj)
4154 obj = yaffs_GetEquivalentObject(obj);
4156 return obj->objectId;
4159 unsigned yaffs_GetObjectType(yaffs_Object *obj)
4161 obj = yaffs_GetEquivalentObject(obj);
4163 switch(obj->variantType)
4165 case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break;
4166 case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break;
4167 case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break;
4168 case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break;
4169 case YAFFS_OBJECT_TYPE_SPECIAL:
4170 if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
4171 if(S_ISCHR(obj->st_mode)) return DT_CHR;
4172 if(S_ISBLK(obj->st_mode)) return DT_BLK;
4173 if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
4174 default: return DT_REG; break;
4178 char *yaffs_GetSymlinkAlias(yaffs_Object *obj)
4180 obj = yaffs_GetEquivalentObject(obj);
4181 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
4183 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
4187 return yaffs_CloneString("");
4191 #ifndef CONFIG_YAFFS_WINCE
4193 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
4195 unsigned int valid = attr->ia_valid;
4197 if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
4198 if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
4199 if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
4201 if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime;
4202 if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime;
4203 if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime;
4205 if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
4207 yaffs_UpdateObjectHeader(obj,NULL,1);
4212 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
4214 unsigned int valid = 0;
4216 attr->ia_mode = obj->st_mode; valid |= ATTR_MODE;
4217 attr->ia_uid = obj->st_uid; valid |= ATTR_UID;
4218 attr->ia_gid = obj->st_gid; valid |= ATTR_GID;
4220 attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME;
4221 attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME;
4222 attr->ia_mtime = obj->st_mtime; valid |= ATTR_MTIME;
4224 attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
4226 attr->ia_valid = valid;
4234 int yaffs_DumpObject(yaffs_Object *obj)
4236 // __u8 buffer[YAFFS_BYTES_PER_CHUNK];
4238 // yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
4240 // memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
4242 // if(obj->chunkId >= 0)
4244 // yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
4247 yaffs_GetObjectName(obj,name,256);
4249 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),
4250 obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial,
4251 obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
4254 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
4255 obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial,
4256 obj->sum, obj->chunkId));
4257 switch(obj->variantType)
4259 case YAFFS_OBJECT_TYPE_FILE:
4260 YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
4262 case YAFFS_OBJECT_TYPE_DIRECTORY:
4263 YPRINTF((" DIRECTORY\n"));
4265 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
4266 case YAFFS_OBJECT_TYPE_SYMLINK:
4267 case YAFFS_OBJECT_TYPE_UNKNOWN:
4276 ///////////////////////// Initialisation code ///////////////////////////
4280 int yaffs_GutsInitialise(yaffs_Device *dev)
4289 if(!yaffs_CheckStructures())
4291 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
4295 if(dev->startBlock <= 0 ||
4296 (dev->endBlock - dev->startBlock) < 10)
4298 T(YAFFS_TRACE_ALWAYS,(TSTR("startBlock %d or endBlock %d invalid\n" TENDSTR),
4299 dev->startBlock, dev->endBlock));
4303 nBlocks = dev->endBlock - dev->startBlock + 1;
4307 // OK now calculate a few things for the device
4308 // Calculate chunkGroupBits.
4309 // We need to find the next power of 2 > than endBlock
4311 x = YAFFS_CHUNKS_PER_BLOCK * (dev->endBlock+1);
4313 for(bits = extraBits = 0; x > 1; bits++)
4315 if(x & 1) extraBits++;
4319 if(extraBits > 0) bits++;
4322 // Level0 Tnodes are 16 bits, so if the bitwidth of the
4323 // chunk range we're using is greater than 16 we need
4324 // to figure out chunk shift and chunkGroupSize
4327 dev->chunkGroupBits = 0;
4331 dev->chunkGroupBits = bits - 16;
4333 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
4336 // More device initialisation
4337 dev->garbageCollectionRequired = 0;
4338 dev->garbageCollections = 0;
4339 dev->currentDirtyChecker = 0;
4340 dev->bufferedBlock = -1;
4341 dev->doingBufferedBlockRewrite = 0;
4342 dev->blockSelectedForGC = -1;
4343 dev->nDeletedFiles = 0;
4344 dev->nBackgroundDeletions=0;
4345 dev->nUnlinkedFiles = 0;
4348 dev->tagsEccFixed=0;
4349 dev->tagsEccUnfixed=0;
4351 dev->localBuffer = YMALLOC(YAFFS_BYTES_PER_CHUNK);
4353 yaffs_InitialiseBlocks(dev,nBlocks);
4355 yaffs_InitialiseTnodes(dev);
4357 yaffs_InitialiseObjects(dev);
4359 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
4362 for(i=0; i < YAFFS_N_CACHE_CHUNKS; i++)
4364 dev->srCache[i].object = NULL;
4365 dev->srCache[i].lastUse = 0;
4366 dev->srCache[i].dirty = 0;
4375 // Initialise the unlinked, root and lost and found directories
4376 dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = NULL;
4378 dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4380 dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
4381 dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4382 yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
4385 // Now scan the flash.
4389 dev->nPageReads = 0;
4390 dev->nPageWrites = 0;
4391 dev->nBlockErasures = 0;
4393 dev->nRetriedWrites = 0;
4394 dev->nRetiredBlocks = 0;
4401 void yaffs_Deinitialise(yaffs_Device *dev)
4403 yaffs_DeinitialiseBlocks(dev);
4404 yaffs_DeinitialiseTnodes(dev);
4405 yaffs_DeinitialiseObjects(dev);
4409 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
4411 int nFree = dev->nFreeChunks - (YAFFS_CHUNKS_PER_BLOCK * YAFFS_RESERVED_BLOCKS);
4413 struct list_head *i;
4417 // To the free chunks add the chunks that are in the deleted unlinked files.
4418 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
4420 l = list_entry(i, yaffs_Object,siblings);
4424 nFree += l->nDataChunks;
4429 return (nFree < 0) ? 0 : nFree;
4434 /////////////////// YAFFS test code //////////////////////////////////
4436 #define yaffs_CheckStruct(structure,syze, name) \
4437 if(sizeof(structure) != syze) \
4439 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
4440 return YAFFS_FAIL; \
4444 static int yaffs_CheckStructures(void)
4446 yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
4447 yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
4448 yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
4449 yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
4450 yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
4457 void yaffs_GutsTest(yaffs_Device *dev)
4460 if(yaffs_CheckStructures() != YAFFS_OK)
4462 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
4466 yaffs_TnodeTest(dev);
4467 yaffs_ObjectTest(dev);