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.45 2006-02-02 22:22:57 charles Exp $";
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
24 #define YAFFS_PASSIVE_GC_CHUNKS 2
27 // Use Steven Hill's ECC struff instead
28 // External functions for ECC on data
29 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
30 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
31 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
32 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
34 #include "yaffs_ecc.h"
37 // countBits is a quick way of counting the number of bits in a byte.
38 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
40 static const char yaffs_countBitsTable[256] =
42 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
43 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
44 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
45 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
46 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
47 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
48 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
49 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
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 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
53 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
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 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
57 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
60 static int yaffs_CountBits(__u8 x)
63 retVal = yaffs_countBitsTable[x];
70 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
71 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
72 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
73 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
75 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
76 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
77 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force);
78 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND);
79 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
80 static int yaffs_CheckStructures(void);
81 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
82 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
84 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
87 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
88 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
89 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
90 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
91 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
93 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
95 static int yaffs_UnlinkWorker(yaffs_Object *obj);
96 static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj);
98 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1);
102 loff_t yaffs_GetFileSize(yaffs_Object *obj);
104 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
105 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
108 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
110 #ifdef YAFFS_PARANOID
111 static int yaffs_CheckFileSanity(yaffs_Object *in);
113 #define yaffs_CheckFileSanity(in)
116 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
117 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
119 // Chunk bitmap manipulations
121 static __inline __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
123 if(blk < dev->internalStartBlock || blk > dev->internalEndBlock)
125 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
128 return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
131 static __inline__ void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
133 __u8 *blkBits = yaffs_BlockBits(dev,blk);
135 memset(blkBits,0,dev->chunkBitmapStride);
138 static __inline__ void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
140 __u8 *blkBits = yaffs_BlockBits(dev,blk);
142 blkBits[chunk/8] &= ~ (1<<(chunk & 7));
145 static __inline__ void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
147 __u8 *blkBits = yaffs_BlockBits(dev,blk);
149 blkBits[chunk/8] |= (1<<(chunk & 7));
152 static __inline__ int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
154 __u8 *blkBits = yaffs_BlockBits(dev,blk);
155 return (blkBits[chunk/8] & (1<<(chunk & 7))) ? 1 :0;
158 static __inline__ int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
160 __u8 *blkBits = yaffs_BlockBits(dev,blk);
162 for(i = 0; i < dev->chunkBitmapStride; i++)
164 if(*blkBits) return 1;
170 // Function to manipulate block info
171 static __inline__ yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
173 if(blk < dev->internalStartBlock || blk > dev->internalEndBlock)
175 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));
178 return &dev->blockInfo[blk - dev->internalStartBlock];
182 static __inline__ int yaffs_HashFunction(int n)
185 return (n % YAFFS_NOBJECT_BUCKETS);
189 yaffs_Object *yaffs_Root(yaffs_Device *dev)
194 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
196 return dev->lostNFoundDir;
200 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
202 if(chunkInNAND < dev->internalStartBlock * dev->nChunksPerBlock)
204 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
209 return dev->writeChunkToNAND(dev,chunkInNAND - dev->chunkOffset,data,spare);
214 int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
218 int doErrorCorrection)
221 yaffs_Spare localSpare;
230 // If we don't have a real spare, then we use a local one.
231 // Need this for the calculation of the ecc
238 retVal = dev->readChunkFromNAND(dev,chunkInNAND - dev->chunkOffset,data,spare);
239 if(data && doErrorCorrection)
242 //Todo handle any errors
243 int eccResult1,eccResult2;
246 yaffs_ECCCalculate(data,calcEcc);
247 eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
248 yaffs_ECCCalculate(&data[256],calcEcc);
249 eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
253 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND - dev->chunkOffset));
256 else if(eccResult1<0)
258 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND - dev->chunkOffset));
264 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND - dev->chunkOffset));
267 else if(eccResult2<0)
269 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND - dev->chunkOffset));
273 if(eccResult1 || eccResult2)
275 // Hoosterman, we had a data problem on this page
276 yaffs_HandleReadDataError(dev,chunkInNAND);
282 // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
283 struct yaffs_NANDSpare nspare;
284 retVal = dev->readChunkFromNAND(dev,chunkInNAND - dev->chunkOffset,data,(yaffs_Spare*)&nspare);
285 memcpy (spare, &nspare, sizeof(yaffs_Spare));
286 if(data && doErrorCorrection)
290 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND - dev->chunkOffset));
292 else if(nspare.eccres1<0)
294 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND - dev->chunkOffset));
299 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND - dev->chunkOffset));
301 else if(nspare.eccres2<0)
303 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND - dev->chunkOffset));
306 if(nspare.eccres1 || nspare.eccres2)
308 // Hoosterman, we had a data problem on this page
309 yaffs_HandleReadDataError(dev,chunkInNAND);
318 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
322 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
323 static __u8 data[YAFFS_BYTES_PER_CHUNK];
324 // Might as well always allocate the larger size for dev->useNANDECC == true;
325 static __u8 spare[sizeof(struct yaffs_NANDSpare)];
327 dev->readChunkFromNAND(dev,chunkInNAND - dev->chunkOffset,data,(yaffs_Spare *)spare);
331 memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);
335 if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return YAFFS_FAIL;
336 if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;
345 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
347 dev->nBlockErasures++;
348 return dev->eraseBlockInNAND(dev,blockInNAND - dev->blockOffset);
351 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
353 return dev->initialiseNAND(dev);
356 static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve)
363 unsigned char rbData[YAFFS_BYTES_PER_CHUNK];
367 chunk = yaffs_AllocateChunk(dev,useReserve);
372 // First check this chunk is erased...
373 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
374 writeOk = yaffs_CheckChunkErased(dev,chunk);
378 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
382 writeOk = yaffs_WriteChunkToNAND(dev,chunk,data,spare);
388 // If verify fails, then delete this chunk and try again
389 // To verify we compare everything except the block and
390 // page status bytes.
391 // NB We check a raw read without ECC correction applied
392 yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0);
394 #ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
395 if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare))
398 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk));
407 // Copy the data into the write buffer.
408 // NB We do this at the end to prevent duplicates in the case of a write error.
410 yaffs_HandleWriteChunkOk(dev,chunk,data,spare);
414 yaffs_HandleWriteChunkError(dev,chunk);
418 } while(chunk >= 0 && ! writeOk);
422 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
423 dev->nRetriedWrites+= (attempts - 1);
430 // Functions for robustisizing
434 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
436 // Ding the blockStatus in the first two pages of the block.
440 memset(&spare, 0xff,sizeof(yaffs_Spare));
442 spare.blockStatus = 0;
444 // TODO change this retirement marking for other NAND types
445 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);
446 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);
448 yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
449 dev->nRetiredBlocks++;
454 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
456 dev->doingBufferedBlockRewrite = 1;
458 // Remove erased chunks
459 // Rewrite existing chunks to a new block
460 // Set current write block to the new block
462 dev->doingBufferedBlockRewrite = 0;
468 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
470 int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
472 // Mark the block for retirement
473 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
474 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
478 // Just do a garbage collection on the affected block then retire the block
483 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
487 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
491 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
495 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
497 int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
499 // Mark the block for retirement
500 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
502 yaffs_DeleteChunk(dev,chunkInNAND,1);
508 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
512 if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
513 s0->tagByte0 != s1->tagByte0 ||
514 s0->tagByte1 != s1->tagByte1 ||
515 s0->tagByte2 != s1->tagByte2 ||
516 s0->tagByte3 != s1->tagByte3 ||
517 s0->tagByte4 != s1->tagByte4 ||
518 s0->tagByte5 != s1->tagByte5 ||
519 s0->tagByte6 != s1->tagByte6 ||
520 s0->tagByte7 != s1->tagByte7 ||
521 s0->ecc1[0] != s1->ecc1[0] ||
522 s0->ecc1[1] != s1->ecc1[1] ||
523 s0->ecc1[2] != s1->ecc1[2] ||
524 s0->ecc2[0] != s1->ecc2[0] ||
525 s0->ecc2[1] != s1->ecc2[1] ||
526 s0->ecc2[2] != s1->ecc2[2] )
535 ///////////////////////// Object management //////////////////
536 // List of spare objects
537 // The list is hooked together using the first pointer
540 // static yaffs_Object *yaffs_freeObjects = NULL;
542 // static int yaffs_nFreeObjects;
544 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
546 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
549 static __u16 yaffs_CalcNameSum(const char *name)
554 __u8 *bname = (__u8 *)name;
557 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
560 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
561 sum += toupper(*bname) * i;
572 void yaffs_SetObjectName(yaffs_Object *obj, const char *name)
574 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
575 if(name && strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
577 strcpy(obj->shortName,name);
581 obj->shortName[0]='\0';
584 obj->sum = yaffs_CalcNameSum(name);
587 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
589 yaffs_ECCCalculate(data , spare->ecc1);
590 yaffs_ECCCalculate(&data[256] , spare->ecc2);
593 void yaffs_CalcTagsECC(yaffs_Tags *tags)
597 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
604 for(i = 0; i < 8; i++)
606 for(j = 1; j &0xff; j<<=1)
621 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
623 unsigned ecc = tags->ecc;
625 yaffs_CalcTagsECC(tags);
631 // TODO: Handle the failure better. Retire?
632 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
636 b[ecc / 8] ^= (1 << (ecc & 7));
638 // Now recvalc the ecc
639 yaffs_CalcTagsECC(tags);
641 return 1; // recovered error
645 // Wierd ecc failure value
646 // TODO Need to do somethiong here
647 return -1; //unrecovered error
654 ///////////////////////// TNODES ///////////////////////
656 // List of spare tnodes
657 // The list is hooked together using the first pointer
660 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
662 // static int yaffs_nFreeTnodes;
664 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
668 // yaffs_CreateTnodes creates a bunch more tnodes and
669 // adds them to the tnode free list.
670 // Don't use this function directly
672 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
675 yaffs_Tnode *newTnodes;
676 yaffs_TnodeList *tnl;
678 if(nTnodes < 1) return YAFFS_OK;
682 newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
686 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
690 // Hook them into the free list
691 for(i = 0; i < nTnodes - 1; i++)
693 newTnodes[i].internal[0] = &newTnodes[i+1];
694 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
695 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
699 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
700 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
701 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
703 dev->freeTnodes = newTnodes;
704 dev->nFreeTnodes+= nTnodes;
705 dev->nTnodesCreated += nTnodes;
707 // Now add this bunch of tnodes to a list for freeing up.
708 // NB If we can't add this to the management list it isn't fatal
709 // but it just means we can't free this bunch of tnodes later.
710 tnl = YMALLOC(sizeof(yaffs_TnodeList));
713 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
718 tnl->tnodes = newTnodes;
719 tnl->next = dev->allocatedTnodeList;
720 dev->allocatedTnodeList = tnl;
724 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
731 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
732 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
734 yaffs_Tnode *tn = NULL;
736 // If there are none left make more
739 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
744 tn = dev->freeTnodes;
745 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
746 if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
748 // Hoosterman, this thing looks like it isn't in the list
749 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
752 dev->freeTnodes = dev->freeTnodes->internal[0];
755 memset(tn,0,sizeof(yaffs_Tnode));
763 // FreeTnode frees up a tnode and puts it back on the free list
764 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
768 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
769 if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
771 // Hoosterman, this thing looks like it is already in the list
772 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
774 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
776 tn->internal[0] = dev->freeTnodes;
777 dev->freeTnodes = tn;
783 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
785 // Free the list of allocated tnodes
786 yaffs_TnodeList *tmp;
788 while(dev->allocatedTnodeList)
790 tmp = dev->allocatedTnodeList->next;
792 YFREE(dev->allocatedTnodeList->tnodes);
793 YFREE(dev->allocatedTnodeList);
794 dev->allocatedTnodeList = tmp;
798 dev->freeTnodes = NULL;
799 dev->nFreeTnodes = 0;
802 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
804 dev->allocatedTnodeList = NULL;
805 dev->freeTnodes = NULL;
806 dev->nFreeTnodes = 0;
807 dev->nTnodesCreated = 0;
812 void yaffs_TnodeTest(yaffs_Device *dev)
817 yaffs_Tnode *tn[1000];
819 YINFO("Testing TNodes");
821 for(j = 0; j < 50; j++)
823 for(i = 0; i < 1000; i++)
825 tn[i] = yaffs_GetTnode(dev);
828 YALERT("Getting tnode failed");
831 for(i = 0; i < 1000; i+=3)
833 yaffs_FreeTnode(dev,tn[i]);
842 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
844 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
845 // The look up tree is represented by the top tnode and the number of topLevel
846 // in the tree. 0 means only the level 0 tnode is in the tree.
849 // FindLevel0Tnode finds the level 0 tnode, if one exists.
850 // Used when reading.....
851 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
854 yaffs_Tnode *tn = fStruct->top;
856 int requiredTallness;
857 int level = fStruct->topLevel;
859 // Check sane level and chunk Id
860 if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
863 // sprintf(str,"Bad level %d",level);
868 if(chunkId > YAFFS_MAX_CHUNK_ID)
871 // sprintf(str,"Bad chunkId %d",chunkId);
876 // First check we're tall enough (ie enough topLevel)
878 i = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
879 requiredTallness = 0;
882 i >>= YAFFS_TNODES_INTERNAL_BITS;
887 if(requiredTallness > fStruct->topLevel)
889 // Not tall enough, so we can't find it, return NULL.
894 // Traverse down to level 0
895 while (level > 0 && tn)
897 tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) &
898 YAFFS_TNODES_INTERNAL_MASK];
906 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
907 // This happens in two steps:
908 // 1. If the tree isn't tall enough, then make it taller.
909 // 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
911 // Used when modifying the tree.
913 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
918 int requiredTallness;
925 //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
927 // Check sane level and page Id
928 if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
931 // sprintf(str,"Bad level %d",fStruct->topLevel);
936 if(chunkId > YAFFS_MAX_CHUNK_ID)
939 // sprintf(str,"Bad chunkId %d",chunkId);
944 // First check we're tall enough (ie enough topLevel)
946 x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
947 requiredTallness = 0;
950 x >>= YAFFS_TNODES_INTERNAL_BITS;
954 //T((TSTR(" required=%d"),requiredTallness));
957 if(requiredTallness > fStruct->topLevel)
959 // Not tall enough,gotta make the tree taller
960 for(i = fStruct->topLevel; i < requiredTallness; i++)
962 //T((TSTR(" add new top")));
964 tn = yaffs_GetTnode(dev);
968 tn->internal[0] = fStruct->top;
973 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
977 fStruct->topLevel = requiredTallness;
981 // Traverse down to level 0, adding anything we need
983 l = fStruct->topLevel;
987 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) &
988 YAFFS_TNODES_INTERNAL_MASK;
990 //T((TSTR(" [%d:%d]"),l,i));
994 //T((TSTR(" added")));
996 tn->internal[x] = yaffs_GetTnode(dev);
999 tn = tn->internal[x];
1009 // DeleteWorker scans backwards through the tnode tree and deletes all the
1010 // chunks and tnodes in the file
1011 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1013 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1029 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1033 if(limit && (*limit) < 0)
1039 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1040 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1044 yaffs_FreeTnode(in->myDev,tn->internal[i]);
1045 tn->internal[i] = NULL;
1050 return (allDone) ? 1 : 0;
1056 for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1062 chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1064 theChunk = tn->level0[i] << in->myDev->chunkGroupBits;
1066 // Now we need to search for it
1067 for(j = 0,found = 0; theChunk && j < in->myDev->chunkGroupSize && !found; j++)
1069 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,&tags,&chunkDeleted);
1070 if(yaffs_TagsMatch(&tags,in->objectId,chunkInInode,chunkDeleted))
1084 yaffs_DeleteChunk(in->myDev,theChunk,1);
1101 return (i < 0) ? 1 : 0;
1112 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1113 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1115 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1117 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1122 yaffs_BlockInfo *theBlock;
1134 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1138 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1139 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1142 yaffs_FreeTnode(in->myDev,tn->internal[i]);
1143 tn->internal[i] = NULL;
1147 //Hoosterman... how could this happen.
1151 return (allDone) ? 1 : 0;
1156 for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1160 // Note this does not find the real chunk, only the chunk group.
1161 // We make an assumption that a chunk group is niot larger than a block.
1162 theChunk = (tn->level0[i] << in->myDev->chunkGroupBits);
1163 T(YAFFS_TRACE_SCAN,(TSTR("soft delete tch %d cgb %d chunk %d" TENDSTR),
1164 tn->level0[i],in->myDev->chunkGroupBits,theChunk));
1166 theBlock = yaffs_GetBlockInfo(in->myDev, theChunk/in->myDev->nChunksPerBlock);
1169 theBlock->softDeletions++;
1187 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1190 obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1193 if(obj->nDataChunks <= 0)
1195 // Empty file, just delete it immediately
1196 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1197 obj->variant.fileVariant.top = NULL;
1198 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1199 yaffs_DoGenericObjectDeletion(obj);
1203 yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1204 obj->softDeleted = 1;
1213 // Pruning removes any part of the file structure tree that is beyond the
1214 // bounds of the file (ie that does not point to chunks).
1216 // A file should only get pruned when its size is reduced.
1218 // Before pruning, the chunks must be pulled from the tree and the
1219 // level 0 tnode entries must be zeroed out.
1220 // Could also use this for file deletion, but that's probably better handled
1221 // by a special case.
1223 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1225 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1234 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1236 if(tn->internal[i] && level > 0)
1238 tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1247 if(hasData == 0 && del0)
1249 // Free and return NULL
1251 yaffs_FreeTnode(dev,tn);
1261 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1268 if(fStruct->topLevel > 0)
1270 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1272 // Now we have a tree with all the non-zero branches NULL but the height
1273 // is the same as it was.
1274 // Let's see if we can trim internal tnodes to shorten the tree.
1275 // We can do this if only the 0th element in the tnode is in use
1276 // (ie all the non-zero are NULL)
1278 while(fStruct->topLevel && !done)
1283 for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1293 fStruct->top = tn->internal[0];
1294 fStruct->topLevel--;
1295 yaffs_FreeTnode(dev,tn);
1311 /////////////////////// End of File Structure functions. /////////////////
1313 // yaffs_CreateFreeObjects creates a bunch more objects and
1314 // adds them to the object free list.
1315 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1318 yaffs_Object *newObjects;
1319 yaffs_ObjectList *list;
1321 if(nObjects < 1) return YAFFS_OK;
1323 // make these things
1325 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1329 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1333 // Hook them into the free list
1334 for(i = 0; i < nObjects - 1; i++)
1336 newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1339 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1340 dev->freeObjects = newObjects;
1341 dev->nFreeObjects+= nObjects;
1342 dev->nObjectsCreated+= nObjects;
1344 // Now add this bunch of Objects to a list for freeing up.
1346 list = YMALLOC(sizeof(yaffs_ObjectList));
1349 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1353 list->objects = newObjects;
1354 list->next = dev->allocatedObjectList;
1355 dev->allocatedObjectList = list;
1364 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1365 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1367 yaffs_Object *tn = NULL;
1369 // If there are none left make more
1370 if(!dev->freeObjects)
1372 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1375 if(dev->freeObjects)
1377 tn = dev->freeObjects;
1378 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1379 dev->nFreeObjects--;
1381 // Now sweeten it up...
1383 memset(tn,0,sizeof(yaffs_Object));
1386 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1387 INIT_LIST_HEAD(&(tn->hardLinks));
1388 INIT_LIST_HEAD(&(tn->hashLink));
1389 INIT_LIST_HEAD(&tn->siblings);
1391 // Add it to the lost and found directory.
1392 // NB Can't put root or lostNFound in lostNFound so
1393 // check if lostNFound exists first
1394 if(dev->lostNFoundDir)
1396 yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);
1404 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1407 yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);
1410 obj->fake = 1; // it is fake so it has no NAND presence...
1411 obj->renameAllowed= 0; // ... and we're not allowed to rename it...
1412 obj->unlinkAllowed= 0; // ... or unlink it
1415 obj->yst_mode = mode;
1417 obj->chunkId = 0; // Not a valid chunk.
1425 static void yaffs_UnhashObject(yaffs_Object *tn)
1428 yaffs_Device *dev = tn->myDev;
1431 // If it is still linked into the bucket list, free from the list
1432 if(!list_empty(&tn->hashLink))
1434 list_del_init(&tn->hashLink);
1435 bucket = yaffs_HashFunction(tn->objectId);
1436 dev->objectBucket[bucket].count--;
1442 // FreeObject frees up a Object and puts it back on the free list
1443 static void yaffs_FreeObject(yaffs_Object *tn)
1446 yaffs_Device *dev = tn->myDev;
1451 // We're still hooked up to a cached inode.
1452 // Don't delete now, but mark for later deletion
1453 tn->deferedFree = 1;
1458 yaffs_UnhashObject(tn);
1460 // Link into the free list.
1461 tn->siblings.next = (struct list_head *)(dev->freeObjects);
1462 dev->freeObjects = tn;
1463 dev->nFreeObjects++;
1469 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1471 if(obj->deferedFree)
1473 yaffs_FreeObject(obj);
1481 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1483 // Free the list of allocated Objects
1485 yaffs_ObjectList *tmp;
1487 while( dev->allocatedObjectList)
1489 tmp = dev->allocatedObjectList->next;
1490 YFREE(dev->allocatedObjectList->objects);
1491 YFREE(dev->allocatedObjectList);
1493 dev->allocatedObjectList = tmp;
1496 dev->freeObjects = NULL;
1497 dev->nFreeObjects = 0;
1500 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1504 dev->allocatedObjectList = NULL;
1505 dev->freeObjects = NULL;
1506 dev->nFreeObjects = 0;
1508 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1510 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1511 dev->objectBucket[i].count = 0;
1521 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1526 int lowest = 999999;
1529 // First let's see if we can find one that's empty.
1531 for(i = 0; i < 10 && lowest > 0; i++)
1534 x %= YAFFS_NOBJECT_BUCKETS;
1535 if(dev->objectBucket[x].count < lowest)
1537 lowest = dev->objectBucket[x].count;
1543 // If we didn't find an empty list, then try
1544 // looking a bit further for a short one
1546 for(i = 0; i < 10 && lowest > 3; i++)
1549 x %= YAFFS_NOBJECT_BUCKETS;
1550 if(dev->objectBucket[x].count < lowest)
1552 lowest = dev->objectBucket[x].count;
1561 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1563 int bucket = yaffs_FindNiceObjectBucket(dev);
1565 // Now find an object value that has not already been taken
1566 // by scanning the list.
1569 struct list_head *i;
1571 __u32 n = (__u32)bucket;
1573 //yaffs_CheckObjectHashSanity();
1578 n += YAFFS_NOBJECT_BUCKETS;
1579 if(1 ||dev->objectBucket[bucket].count > 0)
1581 list_for_each(i,&dev->objectBucket[bucket].list)
1583 // If there is already one in the list
1584 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1592 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1597 void yaffs_HashObject(yaffs_Object *in)
1599 int bucket = yaffs_HashFunction(in->objectId);
1600 yaffs_Device *dev = in->myDev;
1602 if(!list_empty(&in->hashLink))
1608 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1609 dev->objectBucket[bucket].count++;
1613 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1615 int bucket = yaffs_HashFunction(number);
1616 struct list_head *i;
1619 list_for_each(i,&dev->objectBucket[bucket].list)
1621 // Look if it is in the list
1624 in = list_entry(i, yaffs_Object,hashLink);
1625 if(in->objectId == number)
1628 // Don't tell the VFS about this if it has been marked for freeing
1642 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1645 yaffs_Object *theObject;
1649 number = yaffs_CreateNewObjectNumber(dev);
1652 theObject = yaffs_AllocateEmptyObject(dev);
1656 theObject->fake = 0;
1657 theObject->renameAllowed = 1;
1658 theObject->unlinkAllowed = 1;
1659 theObject->objectId = number;
1660 yaffs_HashObject(theObject);
1661 theObject->variantType = type;
1662 #ifdef CONFIG_YAFFS_WINCE
1663 yfsd_WinFileTimeNow(theObject->win_atime);
1664 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1665 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1669 theObject->yst_atime = theObject->yst_mtime = theObject->yst_ctime = Y_CURRENT_TIME;
1674 case YAFFS_OBJECT_TYPE_FILE:
1675 theObject->variant.fileVariant.fileSize = 0;
1676 theObject->variant.fileVariant.scannedFileSize = 0;
1677 theObject->variant.fileVariant.topLevel = 0;
1678 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
1680 case YAFFS_OBJECT_TYPE_DIRECTORY:
1681 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1683 case YAFFS_OBJECT_TYPE_SYMLINK:
1684 // No action required
1686 case YAFFS_OBJECT_TYPE_HARDLINK:
1687 // No action required
1689 case YAFFS_OBJECT_TYPE_SPECIAL:
1690 // No action required
1692 case YAFFS_OBJECT_TYPE_UNKNOWN:
1693 // todo this should not happen
1701 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1703 yaffs_Object *theObject = NULL;
1707 theObject = yaffs_FindObjectByNumber(dev,number);
1712 theObject = yaffs_CreateNewObject(dev,number,type);
1719 char *yaffs_CloneString(const char *str)
1721 char *newStr = NULL;
1725 newStr = YMALLOC(strlen(str) + 1);
1734 // Mknod (create) a new object.
1735 // equivalentObject only has meaning for a hard link;
1736 // aliasString only has meaning for a sumlink.
1737 // rdev only has meaning for devices (a subset of special objects)
1738 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1739 yaffs_Object *parent,
1744 yaffs_Object *equivalentObject,
1745 const char *aliasString,
1750 yaffs_Device *dev = parent->myDev;
1752 // Check if the entry exists. If it does then fail the call since we don't want a dup.
1753 if(yaffs_FindObjectByName(parent,name))
1758 in = yaffs_CreateNewObject(dev,-1,type);
1764 in->variantType = type;
1766 in->yst_mode = mode;
1768 #ifdef CONFIG_YAFFS_WINCE
1769 yfsd_WinFileTimeNow(in->win_atime);
1770 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1771 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1775 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
1776 in->yst_rdev = rdev;
1780 in->nDataChunks = 0;
1782 yaffs_SetObjectName(in,name);
1785 yaffs_AddObjectToDirectory(parent,in);
1787 in->myDev = parent->myDev;
1792 case YAFFS_OBJECT_TYPE_SYMLINK:
1793 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1795 case YAFFS_OBJECT_TYPE_HARDLINK:
1796 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1797 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1798 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1800 case YAFFS_OBJECT_TYPE_FILE: // do nothing
1801 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1802 case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1803 case YAFFS_OBJECT_TYPE_UNKNOWN:
1807 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1808 yaffs_UpdateObjectHeader(in,name,0) < 0)
1810 // Could not create the object header, fail the creation
1811 yaffs_AbortHalfCreatedObject(in);
1820 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1822 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1825 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1827 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1830 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1832 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL,parent,name,mode,uid,gid,NULL,NULL,rdev);
1835 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,const char *alias)
1837 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1840 // NB yaffs_Link returns the object id of the equivalent object.
1841 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject)
1843 // Get the real object in case we were fed a hard link as an equivalent object
1844 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1846 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1848 return equivalentObject;
1858 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName,int force)
1864 newDir = obj->parent; // use the old directory
1867 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1869 // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1870 // duplicate names are allowed.
1871 // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1874 !yaffs_FindObjectByName(newDir,newName)) &&
1875 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1877 yaffs_SetObjectName(obj,newName);
1880 yaffs_AddObjectToDirectory(newDir,obj);
1882 if(unlinkOp) obj->unlinked = 1;
1885 if(yaffs_UpdateObjectHeader(obj,newName,0) >= 0)
1896 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName)
1901 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1902 // Special case for WinCE.
1903 // While look-up is case insensitive, the name isn't.
1904 // THerefore we might want to change x.txt to X.txt
1905 if(oldDir == newDir && _stricmp(oldName,newName) == 0)
1911 obj = yaffs_FindObjectByName(oldDir,oldName);
1912 if(obj && obj->renameAllowed)
1914 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1921 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1923 // Scan the buckets and check that the lists
1924 // have as many members as the count says there are
1927 struct list_head *j;
1930 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1934 list_for_each(j,&dev->objectBucket[bucket].list)
1939 if(countEm != dev->objectBucket[bucket].count)
1941 T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
1950 void yaffs_ObjectTest(yaffs_Device *dev)
1952 yaffs_Object *in[1000];
1954 yaffs_Object *inold[1000];
1958 memset(in,0,1000*sizeof(yaffs_Object *));
1959 memset(inold,0,1000*sizeof(yaffs_Object *));
1961 yaffs_CheckObjectHashSanity(dev);
1963 for(j = 0; j < 10; j++)
1967 for(i = 0; i < 1000; i++)
1969 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1972 YINFO("No more inodes");
1976 inNo[i] = in[i]->objectId;
1980 for(i = 0; i < 1000; i++)
1982 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1984 //T(("Differnce in look up test\n"));
1988 // T(("Look up ok\n"));
1992 yaffs_CheckObjectHashSanity(dev);
1994 for(i = 0; i < 1000; i+=3)
1996 yaffs_FreeObject(in[i]);
2001 yaffs_CheckObjectHashSanity(dev);
2008 /////////////////////////// Block Management and Page Allocation ///////////////////
2011 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2013 dev->allocationBlock = -1; // force it to get a new one
2014 //Todo we're assuming the malloc will pass.
2015 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2016 // Set up dynamic blockinfo stuff.
2017 dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2018 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2019 if(dev->blockInfo && dev->chunkBits)
2021 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2022 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2030 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2032 YFREE(dev->blockInfo);
2033 dev->blockInfo = NULL;
2034 YFREE(dev->chunkBits);
2035 dev->chunkBits = NULL;
2038 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2039 // for garbage collection.
2041 static int yaffs_FindDirtiestBlock(yaffs_Device *dev,int aggressive)
2044 int b = dev->currentDirtyChecker;
2050 yaffs_BlockInfo *bi;
2052 // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2055 pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2058 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2062 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2063 iterations = iterations / 16;
2064 if(iterations > 200)
2070 for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2073 if ( b < dev->internalStartBlock || b > dev->internalEndBlock)
2075 b = dev->internalStartBlock;
2078 if(b < dev->internalStartBlock || b > dev->internalEndBlock)
2080 T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2084 bi = yaffs_GetBlockInfo(dev,b);
2086 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2087 (bi->pagesInUse - bi->softDeletions )< pagesInUse)
2090 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2094 dev->currentDirtyChecker = b;
2100 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2102 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2106 // If the block is still healthy erase it and mark as clean.
2107 // If the block has had a data failure, then retire it.
2108 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2110 if(!bi->needsRetiring)
2112 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2115 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2122 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2123 dev->nErasedBlocks++;
2125 bi->softDeletions = 0;
2126 yaffs_ClearChunkBits(dev,blockNo);
2128 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2132 yaffs_RetireBlock(dev,blockNo);
2133 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2138 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2142 yaffs_BlockInfo *bi;
2144 if(dev->nErasedBlocks < 1)
2146 // Hoosterman we've got a problem.
2147 // Can't get space to gc
2148 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during gc" TENDSTR)));
2153 // Find an empty block.
2155 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2157 dev->allocationBlockFinder++;
2158 if(dev->allocationBlockFinder <dev->internalStartBlock || dev->allocationBlockFinder> dev->internalEndBlock)
2160 dev->allocationBlockFinder = dev->internalStartBlock;
2163 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2165 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2167 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2168 dev->nErasedBlocks--;
2169 return dev->allocationBlockFinder;
2178 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2181 yaffs_BlockInfo *bi;
2183 if(dev->allocationBlock < 0)
2185 // Get next block to allocate off
2186 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2187 dev->allocationPage = 0;
2190 if(!useReserve && dev->nErasedBlocks <= dev->nReservedBlocks)
2192 // Not enough space to allocate unless we're allowed to use the reserve.
2196 // Next page please....
2197 if(dev->allocationBlock >= 0)
2199 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2201 retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
2202 dev->allocationPage;
2204 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2206 dev->allocationPage++;
2210 // If the block is full set the state to full
2211 if(dev->allocationPage >= dev->nChunksPerBlock)
2213 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2214 dev->allocationBlock = -1;
2221 T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2226 // To determine if we have enough space we just look at the
2227 // number of erased blocks.
2228 // The cache is allowed to use reserved blocks.
2230 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2232 return (dev->nErasedBlocks >= dev->nReservedBlocks);
2236 static int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2246 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
2248 // yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2250 yaffs_Object *object;
2252 //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
2254 for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock;
2255 chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2256 chunkInBlock++, oldChunk++ )
2258 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2261 // This page is in use and might need to be copied off
2265 //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2267 yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare,1);
2269 yaffs_GetTagsFromSpare(dev,&spare,&tags);
2271 object = yaffs_FindObjectByNumber(dev,tags.objectId);
2273 if(object && object->deleted && tags.chunkId != 0)
2275 // Data chunk in a deleted file, throw it away
2276 // It's a deleted data chunk,
2277 // No need to copy this, just forget about it and fix up the
2280 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0);
2281 object->nDataChunks--;
2283 if(object->nDataChunks <= 0)
2285 // Time to delete the file too
2286 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2287 object->variant.fileVariant.top = NULL;
2288 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2289 yaffs_DoGenericObjectDeletion(object);
2293 else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2295 // Deleted object header with no data chunks.
2296 // Can be discarded and the file deleted.
2297 object->chunkId = 0;
2298 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2299 object->variant.fileVariant.top = NULL;
2300 yaffs_DoGenericObjectDeletion(object);
2305 // It's either a data chunk in a live file or
2306 // an ObjectHeader, so we're interested in it.
2307 // NB Need to keep the ObjectHeaders of deleted files
2308 // until the whole file has been deleted off
2309 tags.serialNumber++;
2310 yaffs_LoadTagsIntoSpare(&spare,&tags);
2314 newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1);
2321 // Ok, now fix up the Tnodes etc.
2323 if(tags.chunkId == 0)
2326 object->chunkId = newChunk;
2327 object->serial = tags.serialNumber;
2331 // It's a data chunk
2332 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2336 yaffs_DeleteChunk(dev,oldChunk,markNAND);
2345 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2347 // find a file to delete
2348 struct list_head *i;
2352 //Scan the unlinked files looking for one to delete
2353 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2357 l = list_entry(i, yaffs_Object,siblings);
2368 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2370 // This does background deletion on unlinked files.. only deleted ones.
2371 // If we don't have a file we're working on then find one
2372 if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2374 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2377 // OK, we're working on a file...
2378 if(dev->unlinkedDeletion)
2380 yaffs_Object *obj = dev->unlinkedDeletion;
2382 int limit; // Number of chunks to delete in a file.
2383 // NB this can be exceeded, but not by much.
2387 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2389 if(obj->nDataChunks == 0)
2391 // Done all the deleting of data chunks.
2392 // Now dump the header and clean up
2393 yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2394 obj->variant.fileVariant.top = NULL;
2395 yaffs_DoGenericObjectDeletion(obj);
2396 dev->nDeletedFiles--;
2397 dev->nUnlinkedFiles--;
2398 dev->nBackgroundDeletions++;
2399 dev->unlinkedDeletion = NULL;
2406 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2407 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2412 //yaffs_DoUnlinkedFileDeletion(dev);
2414 if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2421 block = yaffs_FindDirtiestBlock(dev,aggressive);
2425 dev->garbageCollections++;
2426 return yaffs_GarbageCollectBlock(dev,block);
2438 // New garbage collector
2439 // If we're very low on erased blocks then we do aggressive garbage collection
2440 // otherwise we do "passive" garbage collection.
2441 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2442 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2444 // The idea is to help clear out space in a more spread-out manner.
2445 // Dunno if it really does anything useful.
2447 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2452 //yaffs_DoUnlinkedFileDeletion(dev);
2454 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1))
2459 block = yaffs_FindDirtiestBlock(dev,aggressive);
2463 dev->garbageCollections++;
2466 dev->passiveGarbageCollections++;
2469 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2471 return yaffs_GarbageCollectBlock(dev,block);
2474 return aggressive ? YAFFS_FAIL : YAFFS_OK;
2478 //////////////////////////// TAGS ///////////////////////////////////////
2480 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2482 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2484 yaffs_CalcTagsECC(tagsPtr);
2486 sparePtr->tagByte0 = tu->asBytes[0];
2487 sparePtr->tagByte1 = tu->asBytes[1];
2488 sparePtr->tagByte2 = tu->asBytes[2];
2489 sparePtr->tagByte3 = tu->asBytes[3];
2490 sparePtr->tagByte4 = tu->asBytes[4];
2491 sparePtr->tagByte5 = tu->asBytes[5];
2492 sparePtr->tagByte6 = tu->asBytes[6];
2493 sparePtr->tagByte7 = tu->asBytes[7];
2496 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2498 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2501 tu->asBytes[0]= sparePtr->tagByte0;
2502 tu->asBytes[1]= sparePtr->tagByte1;
2503 tu->asBytes[2]= sparePtr->tagByte2;
2504 tu->asBytes[3]= sparePtr->tagByte3;
2505 tu->asBytes[4]= sparePtr->tagByte4;
2506 tu->asBytes[5]= sparePtr->tagByte5;
2507 tu->asBytes[6]= sparePtr->tagByte6;
2508 tu->asBytes[7]= sparePtr->tagByte7;
2510 result = yaffs_CheckECCOnTags(tagsPtr);
2513 dev->tagsEccFixed++;
2517 dev->tagsEccUnfixed++;
2521 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2523 memset(spare,0xFF,sizeof(yaffs_Spare));
2526 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted)
2531 if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare,1) == YAFFS_OK)
2533 *chunkDeleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
2534 yaffs_GetTagsFromSpare(dev,&spare,tags);
2547 static int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_Tags *tags)
2549 // NB There must be tags, data is optional
2550 // If there is data, then an ECC is calculated on it.
2559 yaffs_SpareInitialise(&spare);
2561 if(!dev->useNANDECC && buffer)
2563 yaffs_CalcECC(buffer,&spare);
2566 yaffs_LoadTagsIntoSpare(&spare,tags);
2568 return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare);
2574 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_Tags *tags, int useReserve)
2576 // NB There must be tags, data is optional
2577 // If there is data, then an ECC is calculated on it.
2586 yaffs_SpareInitialise(&spare);
2588 if(!dev->useNANDECC && buffer)
2590 yaffs_CalcECC(buffer,&spare);
2593 yaffs_LoadTagsIntoSpare(&spare,tags);
2595 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2599 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted)
2601 return ( tags->chunkId == chunkInObject &&
2602 tags->objectId == objectId &&
2603 !chunkDeleted) ? 1 : 0;
2609 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
2611 //Get the Tnode, then get the level 0 offset chunk offset
2614 yaffs_Tags localTags;
2619 yaffs_Device *dev = in->myDev;
2624 // Passed a NULL, so use our own tags space
2628 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2632 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2634 // Now we need to do the shifting etc and search for it
2635 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
2637 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
2638 if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
2649 return found ? theChunk : -1;
2652 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
2654 //Get the Tnode, then get the level 0 offset chunk offset
2657 yaffs_Tags localTags;
2660 yaffs_Device *dev = in->myDev;
2665 // Passed a NULL, so use our own tags space
2669 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2674 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2676 // Now we need to do the shifting etc and search for it
2677 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
2679 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
2680 if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
2691 // Delete the entry in the filestructure
2694 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2699 //T(("No level 0 found for %d\n", chunkInInode));
2704 //T(("Could not find %d to delete\n",chunkInInode));
2706 return found ? theChunk : -1;
2710 #ifdef YAFFS_PARANOID
2712 static int yaffs_CheckFileSanity(yaffs_Object *in)
2720 yaffs_Tags localTags;
2721 yaffs_Tags *tags = &localTags;
2726 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2728 //T(("Object not a file\n"));
2732 objId = in->objectId;
2733 fSize = in->variant.fileVariant.fileSize;
2734 nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
2736 for(chunk = 1; chunk <= nChunks; chunk++)
2738 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
2743 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
2746 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
2747 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
2754 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
2755 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
2764 //T(("No level 0 found for %d\n", chunk));
2768 return failed ? YAFFS_FAIL : YAFFS_OK;
2773 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
2776 yaffs_Device *dev = in->myDev;
2778 yaffs_Tags existingTags;
2780 unsigned existingSerial, newSerial;
2782 int newChunkDeleted;
2785 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2791 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
2795 // If we're scanning then we need to test for duplicates
2796 // NB This does not need to be efficient since it should only ever
2797 // happen when the power fails during a write, then only one
2798 // chunk should ever be affected.
2801 if(existingChunk != 0)
2803 // NB Right now existing chunk will not be real chunkId if the device >= 32MB
2804 // thus we have to do a FindChunkInFile to get the real chunk id.
2806 // We have a duplicate now we need to decide which one to use
2807 // To do this we get both sets of tags and compare serial numbers.
2808 yaffs_ReadChunkTagsFromNAND(dev,chunkInNAND, &newTags,&newChunkDeleted);
2812 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
2814 if(existingChunk <=0)
2816 //Hoosterman - how did this happen?
2818 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
2823 // NB The deleted flags should be false, otherwise the chunks will
2824 // not be loaded during a scan
2826 newSerial = newTags.serialNumber;
2827 existingSerial = existingTags.serialNumber;
2829 if( existingChunk <= 0 ||
2830 ((existingSerial+1) & 3) == newSerial)
2833 // Delete the old one and drop through to update the tnode
2834 yaffs_DeleteChunk(dev,existingChunk,1);
2839 // Delete the new one and return early so that the tnode isn't changed
2840 yaffs_DeleteChunk(dev,chunkInNAND,1);
2847 if(existingChunk == 0)
2852 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
2859 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
2861 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
2863 if(chunkInNAND >= 0)
2865 return yaffs_ReadChunkFromNAND(in->myDev,chunkInNAND,buffer,NULL,1);
2869 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
2876 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND)
2881 yaffs_BlockInfo *bi;
2883 if(chunkId <= 0) return;
2886 block = chunkId / dev->nChunksPerBlock;
2887 page = chunkId % dev->nChunksPerBlock;
2891 yaffs_SpareInitialise(&spare);
2893 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2895 //read data before write, to ensure correct ecc
2896 //if we're using MTD verification under Linux
2897 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
2900 spare.pageStatus = 0; // To mark it as deleted.
2903 yaffs_WriteChunkToNAND(dev,chunkId,NULL,&spare);
2904 yaffs_HandleUpdateChunk(dev,chunkId,&spare);
2908 dev->nUnmarkedDeletions++;
2911 bi = yaffs_GetBlockInfo(dev,block);
2914 // Pull out of the management area.
2915 // If the whole block became dirty, this will kick off an erasure.
2916 if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
2917 bi->blockState == YAFFS_BLOCK_STATE_FULL)
2921 yaffs_ClearChunkBit(dev,block,page);
2924 if(bi->pagesInUse == 0 &&
2925 bi->blockState == YAFFS_BLOCK_STATE_FULL)
2927 yaffs_BlockBecameDirty(dev,block);
2933 // T(("Bad news deleting chunk %d\n",chunkId));
2941 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
2943 // Find old chunk Need to do this to get serial number
2944 // Write new one and patch into tree.
2945 // Invalidate old tags.
2948 yaffs_Tags prevTags;
2953 yaffs_Device *dev = in->myDev;
2955 yaffs_CheckGarbageCollection(dev);
2957 // Get the previous chunk at this location in the file if it exists
2958 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
2961 newTags.chunkId = chunkInInode;
2962 newTags.objectId = in->objectId;
2963 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
2964 newTags.byteCount = nBytes;
2965 newTags.unusedStuff = 0xFFFFFFFF;
2967 yaffs_CalcTagsECC(&newTags);
2969 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
2972 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
2975 if(prevChunkId >= 0)
2977 yaffs_DeleteChunk(dev,prevChunkId,1);
2981 yaffs_CheckFileSanity(in);
2992 // UpdateObjectHeader updates the header on NAND for an object.
2993 // If name is not NULL, then that new name is used.
2995 int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force)
2998 yaffs_Device *dev = in->myDev;
3004 __u8 bufferNew[YAFFS_BYTES_PER_CHUNK];
3005 __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3007 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bufferNew;
3008 yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3011 if(!in->fake || force)
3014 yaffs_CheckGarbageCollection(dev);
3016 memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK);
3018 prevChunkId = in->chunkId;
3020 if(prevChunkId >= 0)
3022 yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL,1);
3026 oh->type = in->variantType;
3028 oh->yst_mode = in->yst_mode;
3030 #ifdef CONFIG_YAFFS_WINCE
3031 oh->win_atime[0] = in->win_atime[0];
3032 oh->win_ctime[0] = in->win_ctime[0];
3033 oh->win_mtime[0] = in->win_mtime[0];
3034 oh->win_atime[1] = in->win_atime[1];
3035 oh->win_ctime[1] = in->win_ctime[1];
3036 oh->win_mtime[1] = in->win_mtime[1];
3038 oh->yst_uid = in->yst_uid;
3039 oh->yst_gid = in->yst_gid;
3040 oh->yst_atime = in->yst_atime;
3041 oh->yst_mtime = in->yst_mtime;
3042 oh->yst_ctime = in->yst_ctime;
3043 oh->yst_rdev = in->yst_rdev;
3047 oh->parentObjectId = in->parent->objectId;
3051 oh->parentObjectId = 0;
3054 //oh->sum = in->sum;
3057 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
3058 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3060 else if(prevChunkId)
3062 memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1);
3066 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
3069 switch(in->variantType)
3071 case YAFFS_OBJECT_TYPE_UNKNOWN:
3072 // Should not happen
3074 case YAFFS_OBJECT_TYPE_FILE:
3075 oh->fileSize = in->variant.fileVariant.fileSize;
3077 case YAFFS_OBJECT_TYPE_HARDLINK:
3078 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3080 case YAFFS_OBJECT_TYPE_SPECIAL:
3083 case YAFFS_OBJECT_TYPE_DIRECTORY:
3086 case YAFFS_OBJECT_TYPE_SYMLINK:
3087 strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3088 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3094 newTags.chunkId = 0;
3095 newTags.objectId = in->objectId;
3096 newTags.serialNumber = in->serial;
3097 newTags.byteCount = 0xFFFFFFFF;
3098 newTags.unusedStuff = 0xFFFFFFFF;
3100 yaffs_CalcTagsECC(&newTags);
3103 // Create new chunk in NAND
3104 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3109 in->chunkId = newChunkId;
3111 if(prevChunkId >= 0)
3113 yaffs_DeleteChunk(dev,prevChunkId,1);
3126 /////////////////////// Short Operations Cache ////////////////////////////////
3127 // In many siturations where there is no high level buffering (eg WinCE) a lot of
3128 // reads might be short sequential reads, and a lot of writes may be short
3129 // sequential writes. eg. scanning/writing a jpeg file.
3130 // In these cases, a short read/write cache can provide a huge perfomance benefit
3131 // with dumb-as-a-rock code.
3132 // There are a limited number (~10) of cache chunks per device so that we don't
3133 // need a very intelligent search.
3139 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3141 yaffs_Device *dev = obj->myDev;
3144 yaffs_ChunkCache *cache;
3145 int chunkWritten = 0;
3147 int nCaches = obj->myDev->nShortOpCaches;
3154 // Find the dirty cache for this object with the lowest chunk id.
3155 for(i = 0; i < nCaches; i++)
3157 if(dev->srCache[i].object == obj &&
3158 dev->srCache[i].dirty)
3160 if(!cache || dev->srCache[i].chunkId < lowest)
3162 cache = &dev->srCache[i];
3163 lowest = cache->chunkId;
3173 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3175 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3177 nBytes= YAFFS_BYTES_PER_CHUNK;
3180 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3186 cache->object = NULL;
3189 } while(cache && chunkWritten > 0);
3193 //Hoosterman, disk full while writing cache out.
3194 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3202 // Grab us a chunk for use.
3203 // First look for an empty one.
3204 // Then look for the least recently used non-dirty one.
3205 // Then look for the least recently used dirty one...., flush and look again.
3206 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3212 if(dev->nShortOpCaches > 0)
3214 for(i = 0; i < dev->nShortOpCaches; i++)
3216 if(!dev->srCache[i].object)
3218 //T(("Grabbing empty %d\n",i));
3220 //printf("Grabbing empty %d\n",i);
3222 return &dev->srCache[i];
3229 usage = 0; // just to stop the compiler grizzling
3231 for(i = 0; i < dev->nShortOpCaches; i++)
3233 if(!dev->srCache[i].dirty &&
3234 ((dev->srCache[i].lastUse < usage && theOne >= 0)||
3237 usage = dev->srCache[i].lastUse;
3242 //T(("Grabbing non-empty %d\n",theOne));
3244 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3246 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
3256 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3258 yaffs_ChunkCache *cache;
3259 yaffs_Object *theObj;
3264 if(dev->nShortOpCaches > 0)
3266 // Try find a non-dirty one...
3268 cache = yaffs_GrabChunkCacheWorker(dev);
3272 // They were all dirty, find the last recently used object and flush
3273 // its cache, then find again.
3274 // NB what's here is not very accurate, we actually flush the object
3275 // the last recently used page.
3277 theObj = dev->srCache[0].object;
3278 usage = dev->srCache[0].lastUse;
3279 cache = &dev->srCache[0];
3282 for(i = 1; i < dev->nShortOpCaches; i++)
3284 if( dev->srCache[i].object &&
3285 dev->srCache[i].lastUse < usage)
3287 usage = dev->srCache[i].lastUse;
3288 theObj = dev->srCache[i].object;
3289 cache = &dev->srCache[i];
3294 if(!cache || cache->dirty)
3298 yaffs_FlushFilesChunkCache(theObj);
3301 cache = yaffs_GrabChunkCacheWorker(dev);
3305 //printf(" pushout %d\n",pushout);
3318 // Find a cached chunk
3319 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3321 yaffs_Device *dev = obj->myDev;
3323 if(dev->nShortOpCaches > 0)
3325 for(i = 0; i < dev->nShortOpCaches; i++)
3327 if(dev->srCache[i].object == obj &&
3328 dev->srCache[i].chunkId == chunkId)
3332 return &dev->srCache[i];
3339 // Mark the chunk for the least recently used algorithym
3340 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3343 if(dev->nShortOpCaches > 0)
3345 if( dev->srLastUse < 0 ||
3346 dev->srLastUse > 100000000)
3348 // Reset the cache usages
3350 for(i = 1; i < dev->nShortOpCaches; i++)
3352 dev->srCache[i].lastUse = 0;
3359 cache->lastUse = dev->srLastUse;
3368 // Invalidate a single cache page.
3369 // Do this when a whole page gets written,
3370 // ie the short cache for this page is no longer valid.
3371 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3373 if(object->myDev->nShortOpCaches > 0)
3375 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3379 cache->object = NULL;
3385 // Invalidate all the cache pages associated with this object
3386 // Do this whenever ther file is deleted or resized.
3387 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3390 yaffs_Device *dev = in->myDev;
3392 if(dev->nShortOpCaches > 0)
3394 // Now invalidate it.
3395 for(i = 0; i < dev->nShortOpCaches; i++)
3397 if(dev->srCache[i].object == in)
3399 dev->srCache[i].object = NULL;
3409 ///////////////////////// File read/write ///////////////////////////////
3410 // Read and write have very similar structures.
3411 // In general the read/write has three parts to it
3412 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3413 // * Some complete chunks
3414 // * An incomplete chunk to end off with
3416 // Curve-balls: the first chunk might also be the last chunk.
3418 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3427 yaffs_ChunkCache *cache;
3435 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; // The first chunk is 1
3436 start = offset % YAFFS_BYTES_PER_CHUNK;
3438 // OK now check for the curveball where the start and end are in
3440 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
3446 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
3449 cache = yaffs_FindChunkCache(in,chunk);
3451 // If the chunk is already in the cache or it is less than a whole chunk
3452 // then use the cache (if there is caching)
3453 // else bypass the cache.
3454 if( cache || nToCopy != YAFFS_BYTES_PER_CHUNK)
3456 if(dev->nShortOpCaches > 0)
3459 // If we can't find the data in the cache, then load it up.
3463 cache = yaffs_GrabChunkCache(in->myDev);
3465 cache->chunkId = chunk;
3467 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3471 yaffs_UseChunkCache(dev,cache,0);
3473 memcpy(buffer,&cache->data[start],nToCopy);
3477 // Read into the local buffer then copy...
3478 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3479 memcpy(buffer,&dev->localBuffer[start],nToCopy);
3485 #ifdef CONFIG_YAFFS_WINCE
3487 // Under WinCE can't do direct transfer. Need to use a local buffer.
3488 // This is because we otherwise screw up WinCE's memory mapper
3489 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3490 memcpy(buffer,dev->localBuffer,YAFFS_BYTES_PER_CHUNK);
3492 // A full chunk. Read directly into the supplied buffer.
3493 yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3509 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3518 int startOfWrite = offset;
3519 int chunkWritten = 0;
3527 while(n > 0 && chunkWritten >= 0)
3529 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1;
3530 start = offset % YAFFS_BYTES_PER_CHUNK;
3533 // OK now check for the curveball where the start and end are in
3536 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
3540 // Now folks, to calculate how many bytes to write back....
3541 // If we're overwriting and not writing to then end of file then
3542 // we need to write back as much as was there before.
3544 nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * YAFFS_BYTES_PER_CHUNK);
3546 if(nBytesRead > YAFFS_BYTES_PER_CHUNK)
3548 nBytesRead = YAFFS_BYTES_PER_CHUNK;
3551 nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3556 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
3557 nToWriteBack = YAFFS_BYTES_PER_CHUNK;
3560 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
3562 // An incomplete start or end chunk (or maybe both start and end chunk)
3563 if(dev->nShortOpCaches > 0)
3565 yaffs_ChunkCache *cache;
3566 // If we can't find the data in the cache, then load it up.
3567 cache = yaffs_FindChunkCache(in,chunk);
3568 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3570 cache = yaffs_GrabChunkCache(in->myDev);
3572 cache->chunkId = chunk;
3574 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3579 yaffs_UseChunkCache(dev,cache,1);
3580 memcpy(&cache->data[start],buffer,nToCopy);
3581 cache->nBytes = nToWriteBack;
3585 chunkWritten = -1; // fail the write
3590 // An incomplete start or end chunk (or maybe both start and end chunk)
3591 // Read into the local buffer then copy, then copy over and write back.
3593 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3595 memcpy(&dev->localBuffer[start],buffer,nToCopy);
3597 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,nToWriteBack,0);
3599 //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3606 #ifdef CONFIG_YAFFS_WINCE
3607 // Under WinCE can't do direct transfer. Need to use a local buffer.
3608 // This is because we otherwise screw up WinCE's memory mapper
3609 memcpy(dev->localBuffer,buffer,YAFFS_BYTES_PER_CHUNK);
3610 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,YAFFS_BYTES_PER_CHUNK,0);
3612 // A full chunk. Write directly from the supplied buffer.
3613 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0);
3615 // Since we've overwritten the cached data, we better invalidate it.
3616 yaffs_InvalidateChunkCache(in,chunk);
3617 //T(("Write to chunk %d %d\n",chunk,chunkWritten));
3620 if(chunkWritten >= 0)
3630 // Update file object
3632 if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
3634 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
3643 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
3647 int oldFileSize = in->variant.fileVariant.fileSize;
3648 int sizeOfPartialChunk = newSize % YAFFS_BYTES_PER_CHUNK;
3650 yaffs_Device *dev = in->myDev;
3653 yaffs_FlushFilesChunkCache(in);
3654 yaffs_InvalidateWholeChunkCache(in);
3656 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3658 return yaffs_GetFileSize(in);
3661 if(newSize < oldFileSize)
3664 int lastDel = 1 + (oldFileSize-1)/YAFFS_BYTES_PER_CHUNK;
3666 int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/
3667 YAFFS_BYTES_PER_CHUNK;
3669 // Delete backwards so that we don't end up with holes if
3670 // power is lost part-way through the operation.
3671 for(i = lastDel; i >= startDel; i--)
3673 // NB this could be optimised somewhat,
3674 // eg. could retrieve the tags and write them without
3675 // using yaffs_DeleteChunk
3677 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
3678 if(chunkId < (dev->internalStartBlock * dev->nChunksPerBlock) ||
3679 chunkId >= ((dev->internalEndBlock+1) * dev->nChunksPerBlock))
3681 //T(("Found daft chunkId %d for %d\n",chunkId,i));
3686 yaffs_DeleteChunk(dev,chunkId,1);
3691 if(sizeOfPartialChunk != 0)
3693 int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK;
3695 // Got to read and rewrite the last chunk with its new size.
3696 // NB Got to zero pad to nuke old data
3697 yaffs_ReadChunkDataFromObject(in,lastChunk,dev->localBuffer);
3698 memset(dev->localBuffer + sizeOfPartialChunk,0, YAFFS_BYTES_PER_CHUNK - sizeOfPartialChunk);
3700 yaffs_WriteChunkDataToObject(in,lastChunk,dev->localBuffer,sizeOfPartialChunk,1);
3704 in->variant.fileVariant.fileSize = newSize;
3706 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
3718 loff_t yaffs_GetFileSize(yaffs_Object *obj)
3720 obj = yaffs_GetEquivalentObject(obj);
3722 switch(obj->variantType)
3724 case YAFFS_OBJECT_TYPE_FILE:
3725 return obj->variant.fileVariant.fileSize;
3726 case YAFFS_OBJECT_TYPE_SYMLINK:
3727 return strlen(obj->variant.symLinkVariant.alias);
3735 // yaffs_FlushFile() updates the file's
3738 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
3743 //T(("flushing object header\n"));
3745 yaffs_FlushFilesChunkCache(in);
3748 #ifdef CONFIG_YAFFS_WINCE
3749 yfsd_WinFileTimeNow(in->win_mtime);
3751 in->yst_mtime = Y_CURRENT_TIME;
3755 retVal = (yaffs_UpdateObjectHeader(in,NULL,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
3767 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
3770 // First off, invalidate the file's data in the cache, without flushing.
3771 yaffs_InvalidateWholeChunkCache(in);
3773 yaffs_RemoveObjectFromDirectory(in);
3774 yaffs_DeleteChunk(in->myDev,in->chunkId,1);
3780 in->myInode->u.generic_ip = NULL;
3785 yaffs_FreeObject(in);
3790 // yaffs_DeleteFile deletes the whole file data
3791 // and the inode associated with the file.
3792 // It does not delete the links associated with the file.
3793 static int yaffs_UnlinkFile(yaffs_Object *in)
3796 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
3798 // Delete the file data & tnodes
3800 yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
3803 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
3805 return yaffs_DoGenericObjectDeletion(in);
3808 int immediateDeletion=0;
3809 retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
3810 if(1 || // Ignore the result of the change name. This will only fail
3811 // if the disk was completely full (an error condition)
3812 // We ignore the error so we can delete files to recover the disk
3816 //in->myDev->nUnlinkedFiles++;
3817 //in->renameAllowed = 0;
3821 // Might be open at present,
3822 // Caught by delete_inode in yaffs_fs.c
3823 immediateDeletion = 1;
3829 immediateDeletion = 1;
3834 if(immediateDeletion)
3836 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
3838 in->myDev->nDeletedFiles++;
3839 yaffs_SoftDeleteFile(in);
3849 int yaffs_DeleteFile(yaffs_Object *in)
3851 int retVal = YAFFS_OK;
3853 if(in->nDataChunks > 0)
3855 // Use soft deletion
3858 retVal = yaffs_UnlinkFile(in);
3860 if(retVal == YAFFS_OK &&
3865 in->myDev->nDeletedFiles++;
3866 yaffs_SoftDeleteFile(in);
3868 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
3872 // The file has no data chunks so we toss it immediately
3873 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
3874 in->variant.fileVariant.top = NULL;
3875 yaffs_DoGenericObjectDeletion(in);
3881 static int yaffs_DeleteDirectory(yaffs_Object *in)
3883 //First check that the directory is empty.
3884 if(list_empty(&in->variant.directoryVariant.children))
3886 return yaffs_DoGenericObjectDeletion(in);
3893 static int yaffs_DeleteSymLink(yaffs_Object *in)
3895 YFREE(in->variant.symLinkVariant.alias);
3897 return yaffs_DoGenericObjectDeletion(in);
3900 static int yaffs_DeleteHardLink(yaffs_Object *in)
3902 // remove this hardlink from the list assocaited with the equivalent
3904 list_del(&in->hardLinks);
3905 return yaffs_DoGenericObjectDeletion(in);
3909 static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj)
3911 switch(obj->variantType)
3913 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
3914 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
3915 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
3916 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
3917 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
3918 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
3923 static int yaffs_UnlinkWorker(yaffs_Object *obj)
3927 if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
3929 return yaffs_DeleteHardLink(obj);
3931 else if(!list_empty(&obj->hardLinks))
3933 // Curve ball: We're unlinking an object that has a hardlink.
3935 // This problem arises because we are not strictly following
3936 // The Linux link/inode model.
3938 // We can't really delete the object.
3939 // Instead, we do the following:
3940 // - Select a hardlink.
3941 // - Unhook it from the hard links
3942 // - Unhook it from its parent directory (so that the rename can work)
3943 // - Rename the object to the hardlink's name.
3944 // - Delete the hardlink
3949 char name[YAFFS_MAX_NAME_LENGTH+1];
3951 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
3953 list_del_init(&hl->hardLinks);
3954 list_del_init(&hl->siblings);
3956 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
3958 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
3960 if(retVal == YAFFS_OK)
3962 retVal = yaffs_DoGenericObjectDeletion(hl);
3969 switch(obj->variantType)
3971 case YAFFS_OBJECT_TYPE_FILE:
3972 return yaffs_UnlinkFile(obj);
3974 case YAFFS_OBJECT_TYPE_DIRECTORY:
3975 return yaffs_DeleteDirectory(obj);
3977 case YAFFS_OBJECT_TYPE_SYMLINK:
3978 return yaffs_DeleteSymLink(obj);
3980 case YAFFS_OBJECT_TYPE_SPECIAL:
3981 return yaffs_DoGenericObjectDeletion(obj);
3983 case YAFFS_OBJECT_TYPE_HARDLINK:
3984 case YAFFS_OBJECT_TYPE_UNKNOWN:
3991 int yaffs_Unlink(yaffs_Object *dir, const char *name)
3995 obj = yaffs_FindObjectByName(dir,name);
3997 if(obj && obj->unlinkAllowed)
3999 return yaffs_UnlinkWorker(obj);
4006 //////////////// Initialisation Scanning /////////////////
4010 // For now we use the SmartMedia check.
4011 // We look at the blockStatus byte in the first two chunks
4012 // These must be 0xFF to pass as OK.
4013 // todo: this function needs to be modifyable foir different NAND types
4014 // and different chunk sizes. Suggest make this into a per-device configurable
4016 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4020 yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&spare,1);
4022 if(yaffs_CountBits(spare.blockStatus) < 7)
4027 if(spare.blockStatus != 0xFF)
4032 yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4035 if(yaffs_CountBits(spare.blockStatus) < 7)
4040 if(spare.blockStatus != 0xFF)
4050 static int yaffs_Scan(yaffs_Device *dev)
4058 yaffs_BlockState state;
4059 yaffs_Object *hardList = NULL;
4061 yaffs_BlockInfo *bi;
4064 yaffs_ObjectHeader *oh;
4066 yaffs_Object *parent;
4068 __u8 chunkData[YAFFS_BYTES_PER_CHUNK];
4071 // Scan all the blocks...
4073 for(blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++)
4076 bi = yaffs_GetBlockInfo(dev,blk);
4077 yaffs_ClearChunkBits(dev,blk);
4079 bi->softDeletions = 0;
4080 state = YAFFS_BLOCK_STATE_SCANNING;
4083 if(yaffs_IsBlockBad(dev,blk))
4085 state = YAFFS_BLOCK_STATE_DEAD;
4086 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4089 // Read each chunk in the block.
4091 for(c = 0; c < dev->nChunksPerBlock &&
4092 state == YAFFS_BLOCK_STATE_SCANNING; c++)
4094 // Read the spare area and decide what to do
4095 chunk = blk * dev->nChunksPerBlock + c;
4097 yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare,1);
4100 // This block looks ok, now what's in this chunk?
4101 yaffs_GetTagsFromSpare(dev,&spare,&tags);
4103 if(yaffs_CountBits(spare.pageStatus) < 6)
4107 dev->nFreeChunks ++;
4108 //T((" %d %d deleted\n",blk,c));
4110 else if(tags.objectId == YAFFS_UNUSED_OBJECT_ID)
4112 // An unassigned chunk in the block
4113 // This means that either the block is empty or
4114 // this is the one being allocated from
4118 // the block is unused
4119 state = YAFFS_BLOCK_STATE_EMPTY;
4120 dev->nErasedBlocks++;
4124 // this is the block being allocated from
4125 T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4126 state = YAFFS_BLOCK_STATE_ALLOCATING;
4127 dev->allocationBlock = blk;
4128 dev->allocationPage = c;
4131 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4133 else if(tags.chunkId > 0)
4137 yaffs_SetChunkBit(dev,blk,c);
4140 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4141 // PutChunkIntoFIle checks for a clash (two data chunks with
4142 // the same chunkId).
4143 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4144 endpos = (tags.chunkId - 1)* YAFFS_BYTES_PER_CHUNK + tags.byteCount;
4145 if(in->variant.fileVariant.scannedFileSize <endpos)
4147 in->variant.fileVariant.scannedFileSize = endpos;
4148 #ifndef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
4149 in->variant.fileVariant.fileSize =
4150 in->variant.fileVariant.scannedFileSize;
4154 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
4158 // chunkId == 0, so it is an ObjectHeader.
4159 // Thus, we read in the object header and make the object
4160 yaffs_SetChunkBit(dev,blk,c);
4163 yaffs_ReadChunkFromNAND(dev,chunk,chunkData,NULL,1);
4165 oh = (yaffs_ObjectHeader *)chunkData;
4167 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4171 // We have already filled this one. We have a duplicate and need to resolve it.
4173 unsigned existingSerial = in->serial;
4174 unsigned newSerial = tags.serialNumber;
4176 if(((existingSerial+1) & 3) == newSerial)
4178 // Use new one - destroy the exisiting one
4179 yaffs_DeleteChunk(dev,in->chunkId,1);
4184 // Use existing - destroy this one.
4185 yaffs_DeleteChunk(dev,chunk,1);
4190 (tags.objectId == YAFFS_OBJECTID_ROOT ||
4191 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4193 // We only load some info, don't fiddle with directory structure
4195 in->variantType = oh->type;
4197 in->yst_mode = oh->yst_mode;
4198 #ifdef CONFIG_YAFFS_WINCE
4199 in->win_atime[0] = oh->win_atime[0];
4200 in->win_ctime[0] = oh->win_ctime[0];
4201 in->win_mtime[0] = oh->win_mtime[0];
4202 in->win_atime[1] = oh->win_atime[1];
4203 in->win_ctime[1] = oh->win_ctime[1];
4204 in->win_mtime[1] = oh->win_mtime[1];
4206 in->yst_uid = oh->yst_uid;
4207 in->yst_gid = oh->yst_gid;
4208 in->yst_atime = oh->yst_atime;
4209 in->yst_mtime = oh->yst_mtime;
4210 in->yst_ctime = oh->yst_ctime;
4211 in->yst_rdev = oh->yst_rdev;
4213 in->chunkId = chunk;
4218 // we need to load this info
4221 in->variantType = oh->type;
4223 in->yst_mode = oh->yst_mode;
4224 #ifdef CONFIG_YAFFS_WINCE
4225 in->win_atime[0] = oh->win_atime[0];
4226 in->win_ctime[0] = oh->win_ctime[0];
4227 in->win_mtime[0] = oh->win_mtime[0];
4228 in->win_atime[1] = oh->win_atime[1];
4229 in->win_ctime[1] = oh->win_ctime[1];
4230 in->win_mtime[1] = oh->win_mtime[1];
4232 in->yst_uid = oh->yst_uid;
4233 in->yst_gid = oh->yst_gid;
4234 in->yst_atime = oh->yst_atime;
4235 in->yst_mtime = oh->yst_mtime;
4236 in->yst_ctime = oh->yst_ctime;
4237 in->yst_rdev = oh->yst_rdev;
4239 in->chunkId = chunk;
4241 yaffs_SetObjectName(in,oh->name);
4244 // directory stuff...
4245 // hook up to parent
4247 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4248 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4250 // Set up as a directory
4251 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4252 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4254 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4256 // Hoosterman, another problem....
4257 // We're trying to use a non-directory as a directory
4259 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4260 parent = dev->lostNFoundDir;
4264 yaffs_AddObjectToDirectory(parent,in);
4265 if(parent == dev->unlinkedDir)
4267 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4268 dev->nDeletedFiles++;
4271 // Note re hardlinks.
4272 // Since we might scan a hardlink before its equivalent object is scanned
4273 // we put them all in a list.
4274 // After scanning is complete, we should have all the objects, so we run through this
4275 // list and fix up all the chains.
4277 switch(in->variantType)
4279 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
4281 case YAFFS_OBJECT_TYPE_FILE:
4282 #ifdef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
4283 in->variant.fileVariant.fileSize = oh->fileSize;
4286 case YAFFS_OBJECT_TYPE_HARDLINK:
4287 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4288 in->hardLinks.next = (struct list_head *)hardList;
4291 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
4293 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4295 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
4296 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4299 //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
4304 if(state == YAFFS_BLOCK_STATE_SCANNING)
4306 // If we got this far while scanning, then the block is fully allocated.
4307 state = YAFFS_BLOCK_STATE_FULL;
4310 bi->blockState = state;
4312 // Now let's see if it was dirty
4313 if( bi->pagesInUse == 0 &&
4314 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4316 yaffs_BlockBecameDirty(dev,blk);
4321 // Fix up the hard link chains.
4322 // We should now have scanned all the objects, now it's time to add these
4327 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4329 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4333 // Add the hardlink pointers
4334 hl->variant.hardLinkVariant.equivalentObject=in;
4335 list_add(&hl->hardLinks,&in->hardLinks);
4339 //Todo Need to report/handle this better.
4340 // Got a problem... hardlink to a non-existant object
4341 hl->variant.hardLinkVariant.equivalentObject=NULL;
4342 INIT_LIST_HEAD(&hl->hardLinks);
4349 struct list_head *i;
4350 struct list_head *n;
4353 // Soft delete all the unlinked files
4354 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4358 l = list_entry(i, yaffs_Object,siblings);
4361 yaffs_SoftDeleteFile(l);
4372 ////////////////////////// Directory Functions /////////////////////////
4375 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
4378 if(obj->siblings.prev == NULL)
4381 INIT_LIST_HEAD(&obj->siblings);
4384 else if(!list_empty(&obj->siblings))
4386 // If it is holed up somewhere else, un hook it
4387 list_del_init(&obj->siblings);
4390 list_add(&obj->siblings,&directory->variant.directoryVariant.children);
4391 obj->parent = directory;
4393 if(directory == obj->myDev->unlinkedDir)
4396 obj->myDev->nUnlinkedFiles++;
4397 obj->renameAllowed = 0;
4401 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
4403 list_del_init(&obj->siblings);
4407 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const char *name)
4411 struct list_head *i;
4412 char buffer[YAFFS_MAX_NAME_LENGTH+1];
4416 sum = yaffs_CalcNameSum(name);
4418 list_for_each(i,&directory->variant.directoryVariant.children)
4422 l = list_entry(i, yaffs_Object,siblings);
4424 // Special case for lost-n-found
4425 if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4427 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
4432 else if(yaffs_SumCompare(l->sum, sum))
4435 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
4436 if(yaffs_strcmp(name,buffer) == 0)
4449 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
4451 struct list_head *i;
4455 list_for_each(i,&theDir->variant.directoryVariant.children)
4459 l = list_entry(i, yaffs_Object,siblings);
4472 // GetEquivalentObject dereferences any hard links to get to the
4475 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
4477 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4479 // We want the object id of the equivalent object, not this one
4480 obj = obj->variant.hardLinkVariant.equivalentObject;
4486 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize)
4488 memset(name,0,buffSize);
4490 if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4492 strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
4494 else if(obj->chunkId <= 0)
4498 sprintf(locName,"%s%d",YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
4499 strncpy(name,locName,buffSize - 1);
4502 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4503 else if(obj->shortName[0])
4505 strcpy(name,obj->shortName);
4510 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
4511 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
4513 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
4515 if(obj->chunkId >= 0)
4517 yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL,1);
4519 strncpy(name,oh->name,buffSize - 1);
4522 return strlen(name);
4525 int yaffs_GetObjectFileLength(yaffs_Object *obj)
4528 // Dereference any hard linking
4529 obj = yaffs_GetEquivalentObject(obj);
4531 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4533 return obj->variant.fileVariant.fileSize;
4535 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
4537 return strlen(obj->variant.symLinkVariant.alias);
4541 // Only a directory should drop through to here
4542 return YAFFS_BYTES_PER_CHUNK;
4546 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
4549 struct list_head *i;
4553 count++; // the object itself
4555 list_for_each(i,&obj->hardLinks)
4557 count++; // add the hard links;
4564 int yaffs_GetObjectInode(yaffs_Object *obj)
4566 obj = yaffs_GetEquivalentObject(obj);
4568 return obj->objectId;
4571 unsigned yaffs_GetObjectType(yaffs_Object *obj)
4573 obj = yaffs_GetEquivalentObject(obj);
4575 switch(obj->variantType)
4577 case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break;
4578 case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break;
4579 case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break;
4580 case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break;
4581 case YAFFS_OBJECT_TYPE_SPECIAL:
4582 if(S_ISFIFO(obj->yst_mode)) return DT_FIFO;
4583 if(S_ISCHR(obj->yst_mode)) return DT_CHR;
4584 if(S_ISBLK(obj->yst_mode)) return DT_BLK;
4585 if(S_ISSOCK(obj->yst_mode)) return DT_SOCK;
4586 default: return DT_REG; break;
4590 char *yaffs_GetSymlinkAlias(yaffs_Object *obj)
4592 obj = yaffs_GetEquivalentObject(obj);
4593 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
4595 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
4599 return yaffs_CloneString("");
4603 #ifndef CONFIG_YAFFS_WINCE
4605 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
4607 unsigned int valid = attr->ia_valid;
4609 if(valid & ATTR_MODE) obj->yst_mode = attr->ia_mode;
4610 if(valid & ATTR_UID) obj->yst_uid = attr->ia_uid;
4611 if(valid & ATTR_GID) obj->yst_gid = attr->ia_gid;
4613 if(valid & ATTR_ATIME) obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
4614 if(valid & ATTR_CTIME) obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
4615 if(valid & ATTR_MTIME) obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
4618 if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
4620 yaffs_UpdateObjectHeader(obj,NULL,1);
4626 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
4628 unsigned int valid = 0;
4630 attr->ia_mode = obj->yst_mode; valid |= ATTR_MODE;
4631 attr->ia_uid = obj->yst_uid; valid |= ATTR_UID;
4632 attr->ia_gid = obj->yst_gid; valid |= ATTR_GID;
4635 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime; valid |= ATTR_ATIME;
4636 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime; valid |= ATTR_CTIME;
4637 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime; valid |= ATTR_MTIME;
4639 attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
4641 attr->ia_valid = valid;
4649 int yaffs_DumpObject(yaffs_Object *obj)
4651 // __u8 buffer[YAFFS_BYTES_PER_CHUNK];
4653 // yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
4655 // memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
4657 // if(obj->chunkId >= 0)
4659 // yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
4662 yaffs_GetObjectName(obj,name,256);
4664 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),
4665 obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial,
4666 obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
4669 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
4670 obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial,
4671 obj->sum, obj->chunkId));
4672 switch(obj->variantType)
4674 case YAFFS_OBJECT_TYPE_FILE:
4675 YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
4677 case YAFFS_OBJECT_TYPE_DIRECTORY:
4678 YPRINTF((" DIRECTORY\n"));
4680 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
4681 case YAFFS_OBJECT_TYPE_SYMLINK:
4682 case YAFFS_OBJECT_TYPE_UNKNOWN:
4691 ///////////////////////// Initialisation code ///////////////////////////
4695 int yaffs_GutsInitialise(yaffs_Device *dev)
4702 if( dev->nBytesPerChunk != YAFFS_BYTES_PER_CHUNK ||
4703 dev->nChunksPerBlock < 2 ||
4704 dev->nReservedBlocks < 2 ||
4705 dev->startBlock < 0 ||
4706 dev->endBlock <= 0 ||
4707 dev->endBlock <= (dev->startBlock + dev->nReservedBlocks)
4710 //these parameters must be set before stating yaffs
4711 // Other parameters internalStartBlock,
4717 if(!yaffs_CheckStructures())
4719 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
4725 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
4731 if(dev->startBlock < 0 ||
4732 (dev->endBlock - dev->startBlock) < 10)
4734 T(YAFFS_TRACE_ALWAYS,(TSTR("startBlock %d or endBlock %d invalid\n" TENDSTR),
4735 dev->startBlock, dev->endBlock));
4740 // Do we need to add an offset to use block 0?
4742 dev->internalStartBlock = dev->startBlock;
4743 dev->internalEndBlock = dev->endBlock;
4744 dev->blockOffset = 0;
4745 dev->chunkOffset = 0;
4747 if(dev->startBlock == 0)
4749 dev->internalStartBlock++;
4750 dev->internalEndBlock++;
4752 dev->chunkOffset = dev->nChunksPerBlock;
4756 nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
4760 // OK now calculate a few things for the device
4761 // Calculate chunkGroupBits.
4762 // We need to find the next power of 2 > than internalEndBlock
4764 x = dev->nChunksPerBlock * (dev->internalEndBlock+1);
4766 for(bits = extraBits = 0; x > 1; bits++)
4768 if(x & 1) extraBits++;
4772 if(extraBits > 0) bits++;
4775 // Level0 Tnodes are 16 bits, so if the bitwidth of the
4776 // chunk range we're using is greater than 16 we need
4777 // to figure out chunk shift and chunkGroupSize
4780 dev->chunkGroupBits = 0;
4784 dev->chunkGroupBits = bits - 16;
4787 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
4789 if(dev->nChunksPerBlock < dev->chunkGroupSize)
4791 // We have a problem because the soft delete won't work if
4792 // the chunk group size > chunks per block.
4793 // This can be remedied by using larger "virtual blocks".
4801 // More device initialisation
4802 dev->garbageCollections = 0;
4803 dev->passiveGarbageCollections = 0;
4804 dev->currentDirtyChecker = 0;
4805 dev->bufferedBlock = -1;
4806 dev->doingBufferedBlockRewrite = 0;
4807 dev->nDeletedFiles = 0;
4808 dev->nBackgroundDeletions=0;
4809 dev->nUnlinkedFiles = 0;
4812 dev->tagsEccFixed=0;
4813 dev->tagsEccUnfixed=0;
4814 dev->nErasedBlocks=0;
4816 dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
4821 yaffs_InitialiseBlocks(dev,nBlocks);
4823 yaffs_InitialiseTnodes(dev);
4825 yaffs_InitialiseObjects(dev);
4827 if(dev->nShortOpCaches > 0)
4831 if(dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
4833 dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
4836 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
4838 for(i=0; i < dev->nShortOpCaches; i++)
4840 dev->srCache[i].object = NULL;
4841 dev->srCache[i].lastUse = 0;
4842 dev->srCache[i].dirty = 0;
4850 // Initialise the unlinked, root and lost and found directories
4851 dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = NULL;
4853 dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4855 dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
4856 dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4857 yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
4860 // Now scan the flash.
4864 dev->nPageReads = 0;
4865 dev->nPageWrites = 0;
4866 dev->nBlockErasures = 0;
4868 dev->nRetriedWrites = 0;
4869 dev->nRetiredBlocks = 0;
4876 void yaffs_Deinitialise(yaffs_Device *dev)
4881 yaffs_DeinitialiseBlocks(dev);
4882 yaffs_DeinitialiseTnodes(dev);
4883 yaffs_DeinitialiseObjects(dev);
4884 if(dev->nShortOpCaches > 0)
4885 YFREE(dev->srCache);
4886 YFREE(dev->localBuffer);
4894 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
4896 int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
4898 struct list_head *i;
4902 // To the free chunks add the chunks that are in the deleted unlinked files.
4903 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
4905 l = list_entry(i, yaffs_Object,siblings);
4909 nFree += l->nDataChunks;
4914 // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);
4916 if(nFree < 0) nFree = 0;
4924 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
4929 int nDirtyCacheChunks=0;
4931 yaffs_BlockInfo *blk;
4933 struct list_head *i;
4936 for(nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock; b++)
4938 blk = yaffs_GetBlockInfo(dev,b);
4940 switch(blk->blockState)
4942 case YAFFS_BLOCK_STATE_EMPTY:
4943 case YAFFS_BLOCK_STATE_ALLOCATING:
4944 case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break;
4952 // To the free chunks add the chunks that are in the deleted unlinked files.
4953 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
4957 l = list_entry(i, yaffs_Object,siblings);
4961 pending += l->nDataChunks;
4968 //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending);
4970 if(nFree != dev->nFreeChunks)
4972 // printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks);
4977 // Now count the number of dirty chunks in the cache and subtract those
4981 for(i = 0; i < dev->nShortOpCaches; i++)
4983 if(dev->srCache[i].dirty) nDirtyCacheChunks++;
4987 nFree -= nDirtyCacheChunks;
4989 nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
4991 if(nFree < 0) nFree = 0;
4999 /////////////////// YAFFS test code //////////////////////////////////
5001 #define yaffs_CheckStruct(structure,syze, name) \
5002 if(sizeof(structure) != syze) \
5004 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
5005 return YAFFS_FAIL; \
5009 static int yaffs_CheckStructures(void)
5011 yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
5012 yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
5013 yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
5014 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
5015 yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
5017 yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
5024 void yaffs_GutsTest(yaffs_Device *dev)
5027 if(yaffs_CheckStructures() != YAFFS_OK)
5029 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
5033 yaffs_TnodeTest(dev);
5034 yaffs_ObjectTest(dev);