3 * YAFFS: Yet another FFS. A NAND-flash specific file system.
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.9 2005-07-18 23:16:04 charles Exp $";
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
23 #include "yaffs_tagsvalidity.h"
26 #include "yaffs_tagscompat.h"
28 #ifdef CONFIG_YAFFS_WINCE
29 void yfsd_LockYAFFS(BOOL fsLockOnly);
30 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
33 #define YAFFS_PASSIVE_GC_CHUNKS 2
36 // Use Steven Hill's ECC struff instead
37 // External functions for ECC on data
38 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
39 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
40 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
41 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
43 #include "yaffs_ecc.h"
47 // countBits is a quick way of counting the number of bits in a byte.
48 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
50 static const char yaffs_countBitsTable[256] =
52 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
53 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
54 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
55 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
56 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
58 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
59 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
60 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
61 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
62 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
63 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
64 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
65 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
66 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
67 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
70 static int yaffs_CountBits(__u8 x)
73 retVal = yaffs_countBitsTable[x];
81 // Stuff using yea olde tags
82 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
83 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
85 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
86 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
93 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
94 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
95 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
96 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
98 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
100 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
102 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
104 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
105 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
106 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
107 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
108 static int yaffs_CheckStructures(void);
109 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
110 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
112 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
114 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
115 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
118 // Robustification (if it ever comes about...)
119 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
121 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
123 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
124 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
125 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags);
127 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
129 static int yaffs_UnlinkWorker(yaffs_Object *obj);
130 static void yaffs_DestroyObject(yaffs_Object *obj);
133 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
136 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
139 loff_t yaffs_GetFileSize(yaffs_Object *obj);
142 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
144 static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
146 #ifdef YAFFS_PARANOID
147 static int yaffs_CheckFileSanity(yaffs_Object *in);
149 #define yaffs_CheckFileSanity(in)
152 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
153 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
155 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
157 chunkInNAND -= dev->chunkOffset;
159 if(dev->readChunkWithTagsFromNAND)
160 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
162 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
165 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
167 chunkInNAND -= dev->chunkOffset;
171 tags->sequenceNumber = dev->sequenceNumber;
173 if(!yaffs_ValidateTags(tags))
175 T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
178 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
182 T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
186 if(dev->writeChunkWithTagsToNAND)
187 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
189 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
192 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
194 blockNo -= dev->blockOffset;
196 if(dev->markNANDBlockBad)
197 return dev->markNANDBlockBad(dev,blockNo);
199 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
201 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
203 blockNo -= dev->blockOffset;
205 if(dev->queryNANDBlock)
206 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
208 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
211 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
215 blockInNAND -= dev->blockOffset;
217 dev->nBlockErasures++;
218 result = dev->eraseBlockInNAND(dev,blockInNAND);
220 if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
224 static int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
226 return dev->initialiseNAND(dev);
232 // Temporary buffer manipulations
234 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
237 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
239 if(dev->tempBuffer[i].line == 0)
241 dev->tempBuffer[i].line = lineNo;
242 if((i+1) > dev->maxTemp)
244 dev->maxTemp = i + 1;
245 for(j = 0; j <= i; j++)
246 dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
249 return dev->tempBuffer[i].buffer;
253 T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
254 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
256 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
258 T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
260 dev->unmanagedTempAllocations++;
261 // Get an unmanaged one
262 return YMALLOC(dev->nBytesPerChunk);
267 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
270 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
272 if(dev->tempBuffer[i].buffer == buffer)
274 dev->tempBuffer[i].line = 0;
281 // assume it is an unmanaged one.
282 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
284 dev->unmanagedTempDeallocations++;
290 // Chunk bitmap manipulations
292 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
294 if(blk < dev->internalStartBlock || blk > dev->internalEndBlock)
296 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
299 return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
302 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
304 __u8 *blkBits = yaffs_BlockBits(dev,blk);
306 memset(blkBits,0,dev->chunkBitmapStride);
309 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
311 __u8 *blkBits = yaffs_BlockBits(dev,blk);
313 blkBits[chunk/8] &= ~ (1<<(chunk & 7));
316 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
318 __u8 *blkBits = yaffs_BlockBits(dev,blk);
320 blkBits[chunk/8] |= (1<<(chunk & 7));
323 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
325 __u8 *blkBits = yaffs_BlockBits(dev,blk);
326 return (blkBits[chunk/8] & (1<<(chunk & 7))) ? 1 :0;
329 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
331 __u8 *blkBits = yaffs_BlockBits(dev,blk);
333 for(i = 0; i < dev->chunkBitmapStride; i++)
335 if(*blkBits) return 1;
342 static Y_INLINE int yaffs_HashFunction(int n)
344 return (n % YAFFS_NOBJECT_BUCKETS);
348 yaffs_Object *yaffs_Root(yaffs_Device *dev)
353 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
355 return dev->lostNFoundDir;
361 int yaffs_CheckFF(__u8 *buffer,int nBytes)
363 //Horrible, slow implementation
366 if(*buffer != 0xFF) return 0;
372 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
375 int retval = YAFFS_OK;
376 __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
377 yaffs_ExtendedTags tags;
379 // NCB dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
380 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
382 if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
384 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
388 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
397 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
407 chunk = yaffs_AllocateChunk(dev,useReserve);
412 // First check this chunk is erased...
413 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
414 writeOk = yaffs_CheckChunkErased(dev,chunk);
418 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
422 writeOk = yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
428 // Copy the data into the robustification buffer.
429 // NB We do this at the end to prevent duplicates in the case of a write error.
431 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
435 yaffs_HandleWriteChunkError(dev,chunk);
439 } while(chunk >= 0 && ! writeOk);
443 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
444 dev->nRetriedWrites+= (attempts - 1);
455 // Functions for robustisizing
459 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
462 yaffs_MarkBlockBad(dev,blockInNAND);
464 yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
466 dev->nRetiredBlocks++;
471 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
473 dev->doingBufferedBlockRewrite = 1;
475 // Remove erased chunks
476 // Rewrite existing chunks to a new block
477 // Set current write block to the new block
479 dev->doingBufferedBlockRewrite = 0;
485 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
487 int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
489 // Mark the block for retirement
490 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
491 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
495 // Just do a garbage collection on the affected block then retire the block
500 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
506 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
510 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
514 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
516 int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
518 // Mark the block for retirement
519 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
521 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
527 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
531 if( memcmp(d0,d1,dataSize) != 0 ||
532 s0->tagByte0 != s1->tagByte0 ||
533 s0->tagByte1 != s1->tagByte1 ||
534 s0->tagByte2 != s1->tagByte2 ||
535 s0->tagByte3 != s1->tagByte3 ||
536 s0->tagByte4 != s1->tagByte4 ||
537 s0->tagByte5 != s1->tagByte5 ||
538 s0->tagByte6 != s1->tagByte6 ||
539 s0->tagByte7 != s1->tagByte7 ||
540 s0->ecc1[0] != s1->ecc1[0] ||
541 s0->ecc1[1] != s1->ecc1[1] ||
542 s0->ecc1[2] != s1->ecc1[2] ||
543 s0->ecc2[0] != s1->ecc2[0] ||
544 s0->ecc2[1] != s1->ecc2[1] ||
545 s0->ecc2[2] != s1->ecc2[2] )
555 ///////////////////////// Object management //////////////////
556 // List of spare objects
557 // The list is hooked together using the first pointer
560 // static yaffs_Object *yaffs_freeObjects = NULL;
562 // static int yaffs_nFreeObjects;
564 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
566 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
569 static __u16 yaffs_CalcNameSum(const YCHAR *name)
574 YUCHAR *bname = (YUCHAR *)name;
577 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
580 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
581 sum += yaffs_toupper(*bname) * i;
592 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
594 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
595 if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
597 yaffs_strcpy(obj->shortName,name);
601 obj->shortName[0]=_Y('\0');
604 obj->sum = yaffs_CalcNameSum(name);
608 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
610 yaffs_ECCCalculate(data , spare->ecc1);
611 yaffs_ECCCalculate(&data[256] , spare->ecc2);
616 ///////////////////////// TNODES ///////////////////////
618 // List of spare tnodes
619 // The list is hooked together using the first pointer
622 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
624 // static int yaffs_nFreeTnodes;
626 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
630 // yaffs_CreateTnodes creates a bunch more tnodes and
631 // adds them to the tnode free list.
632 // Don't use this function directly
634 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
637 yaffs_Tnode *newTnodes;
638 yaffs_TnodeList *tnl;
640 if(nTnodes < 1) return YAFFS_OK;
644 newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
648 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
652 // Hook them into the free list
653 for(i = 0; i < nTnodes - 1; i++)
655 newTnodes[i].internal[0] = &newTnodes[i+1];
656 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
657 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
661 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
662 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
663 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
665 dev->freeTnodes = newTnodes;
666 dev->nFreeTnodes+= nTnodes;
667 dev->nTnodesCreated += nTnodes;
669 // Now add this bunch of tnodes to a list for freeing up.
670 // NB If we can't add this to the management list it isn't fatal
671 // but it just means we can't free this bunch of tnodes later.
672 tnl = YMALLOC(sizeof(yaffs_TnodeList));
675 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
680 tnl->tnodes = newTnodes;
681 tnl->next = dev->allocatedTnodeList;
682 dev->allocatedTnodeList = tnl;
686 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
693 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
694 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
696 yaffs_Tnode *tn = NULL;
698 // If there are none left make more
701 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
706 tn = dev->freeTnodes;
707 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
708 if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
710 // Hoosterman, this thing looks like it isn't in the list
711 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
714 dev->freeTnodes = dev->freeTnodes->internal[0];
717 memset(tn,0,sizeof(yaffs_Tnode));
725 // FreeTnode frees up a tnode and puts it back on the free list
726 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
730 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
731 if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
733 // Hoosterman, this thing looks like it is already in the list
734 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
736 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
738 tn->internal[0] = dev->freeTnodes;
739 dev->freeTnodes = tn;
745 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
747 // Free the list of allocated tnodes
748 yaffs_TnodeList *tmp;
750 while(dev->allocatedTnodeList)
752 tmp = dev->allocatedTnodeList->next;
754 YFREE(dev->allocatedTnodeList->tnodes);
755 YFREE(dev->allocatedTnodeList);
756 dev->allocatedTnodeList = tmp;
760 dev->freeTnodes = NULL;
761 dev->nFreeTnodes = 0;
764 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
766 dev->allocatedTnodeList = NULL;
767 dev->freeTnodes = NULL;
768 dev->nFreeTnodes = 0;
769 dev->nTnodesCreated = 0;
774 void yaffs_TnodeTest(yaffs_Device *dev)
779 yaffs_Tnode *tn[1000];
781 YINFO("Testing TNodes");
783 for(j = 0; j < 50; j++)
785 for(i = 0; i < 1000; i++)
787 tn[i] = yaffs_GetTnode(dev);
790 YALERT("Getting tnode failed");
793 for(i = 0; i < 1000; i+=3)
795 yaffs_FreeTnode(dev,tn[i]);
804 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
806 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
807 // The look up tree is represented by the top tnode and the number of topLevel
808 // in the tree. 0 means only the level 0 tnode is in the tree.
811 // FindLevel0Tnode finds the level 0 tnode, if one exists.
812 // Used when reading.....
813 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
816 yaffs_Tnode *tn = fStruct->top;
818 int requiredTallness;
819 int level = fStruct->topLevel;
821 // Check sane level and chunk Id
822 if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
825 // sprintf(str,"Bad level %d",level);
830 if(chunkId > YAFFS_MAX_CHUNK_ID)
833 // sprintf(str,"Bad chunkId %d",chunkId);
838 // First check we're tall enough (ie enough topLevel)
840 i = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
841 requiredTallness = 0;
844 i >>= YAFFS_TNODES_INTERNAL_BITS;
849 if(requiredTallness > fStruct->topLevel)
851 // Not tall enough, so we can't find it, return NULL.
856 // Traverse down to level 0
857 while (level > 0 && tn)
859 tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) &
860 YAFFS_TNODES_INTERNAL_MASK];
868 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
869 // This happens in two steps:
870 // 1. If the tree isn't tall enough, then make it taller.
871 // 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
873 // Used when modifying the tree.
875 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
880 int requiredTallness;
887 //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
889 // Check sane level and page Id
890 if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
893 // sprintf(str,"Bad level %d",fStruct->topLevel);
898 if(chunkId > YAFFS_MAX_CHUNK_ID)
901 // sprintf(str,"Bad chunkId %d",chunkId);
906 // First check we're tall enough (ie enough topLevel)
908 x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
909 requiredTallness = 0;
912 x >>= YAFFS_TNODES_INTERNAL_BITS;
916 //T((TSTR(" required=%d"),requiredTallness));
919 if(requiredTallness > fStruct->topLevel)
921 // Not tall enough,gotta make the tree taller
922 for(i = fStruct->topLevel; i < requiredTallness; i++)
924 //T((TSTR(" add new top")));
926 tn = yaffs_GetTnode(dev);
930 tn->internal[0] = fStruct->top;
935 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
939 fStruct->topLevel = requiredTallness;
943 // Traverse down to level 0, adding anything we need
945 l = fStruct->topLevel;
949 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) &
950 YAFFS_TNODES_INTERNAL_MASK;
952 //T((TSTR(" [%d:%d]"),l,i));
956 //T((TSTR(" added")));
958 tn->internal[x] = yaffs_GetTnode(dev);
961 tn = tn->internal[x];
972 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
977 for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
979 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
981 yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
982 if(yaffs_TagsMatch(tags,objectId,chunkInInode))
994 // DeleteWorker scans backwards through the tnode tree and deletes all the
995 // chunks and tnodes in the file
996 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
998 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1003 yaffs_ExtendedTags tags;
1005 yaffs_Device *dev = in->myDev;
1015 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1019 if(limit && (*limit) < 0)
1025 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1026 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1030 yaffs_FreeTnode(dev,tn->internal[i]);
1031 tn->internal[i] = NULL;
1036 return (allDone) ? 1 : 0;
1042 for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1047 chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1049 theChunk = tn->level0[i] << dev->chunkGroupBits;
1051 foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1055 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1072 return (i < 0) ? 1 : 0;
1084 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1087 yaffs_BlockInfo *theBlock;
1089 T(YAFFS_TRACE_DELETION,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1091 theBlock = yaffs_GetBlockInfo(dev, chunk/dev->nChunksPerBlock);
1094 theBlock->softDeletions++;
1099 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1100 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1102 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1104 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1109 yaffs_Device *dev = in->myDev;
1117 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1121 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1122 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1125 yaffs_FreeTnode(dev,tn->internal[i]);
1126 tn->internal[i] = NULL;
1130 //Hoosterman... how could this happen.
1134 return (allDone) ? 1 : 0;
1139 for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1143 // Note this does not find the real chunk, only the chunk group.
1144 // We make an assumption that a chunk group is niot larger than a block.
1145 theChunk = (tn->level0[i] << dev->chunkGroupBits);
1147 yaffs_SoftDeleteChunk(dev,theChunk);
1164 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1167 obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1170 if(obj->nDataChunks <= 0)
1172 // Empty file with no duplicate object headers, just delete it immediately
1173 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1174 obj->variant.fileVariant.top = NULL;
1175 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1176 yaffs_DoGenericObjectDeletion(obj);
1180 yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1181 obj->softDeleted = 1;
1190 // Pruning removes any part of the file structure tree that is beyond the
1191 // bounds of the file (ie that does not point to chunks).
1193 // A file should only get pruned when its size is reduced.
1195 // Before pruning, the chunks must be pulled from the tree and the
1196 // level 0 tnode entries must be zeroed out.
1197 // Could also use this for file deletion, but that's probably better handled
1198 // by a special case.
1200 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1202 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1211 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1213 if(tn->internal[i] && level > 0)
1215 tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1224 if(hasData == 0 && del0)
1226 // Free and return NULL
1228 yaffs_FreeTnode(dev,tn);
1238 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1245 if(fStruct->topLevel > 0)
1247 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1249 // Now we have a tree with all the non-zero branches NULL but the height
1250 // is the same as it was.
1251 // Let's see if we can trim internal tnodes to shorten the tree.
1252 // We can do this if only the 0th element in the tnode is in use
1253 // (ie all the non-zero are NULL)
1255 while(fStruct->topLevel && !done)
1260 for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1270 fStruct->top = tn->internal[0];
1271 fStruct->topLevel--;
1272 yaffs_FreeTnode(dev,tn);
1288 /////////////////////// End of File Structure functions. /////////////////
1290 // yaffs_CreateFreeObjects creates a bunch more objects and
1291 // adds them to the object free list.
1292 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1295 yaffs_Object *newObjects;
1296 yaffs_ObjectList *list;
1298 if(nObjects < 1) return YAFFS_OK;
1300 // make these things
1302 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1306 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1310 // Hook them into the free list
1311 for(i = 0; i < nObjects - 1; i++)
1313 newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1316 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1317 dev->freeObjects = newObjects;
1318 dev->nFreeObjects+= nObjects;
1319 dev->nObjectsCreated+= nObjects;
1321 // Now add this bunch of Objects to a list for freeing up.
1323 list = YMALLOC(sizeof(yaffs_ObjectList));
1326 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1330 list->objects = newObjects;
1331 list->next = dev->allocatedObjectList;
1332 dev->allocatedObjectList = list;
1341 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1342 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1344 yaffs_Object *tn = NULL;
1346 // If there are none left make more
1347 if(!dev->freeObjects)
1349 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1352 if(dev->freeObjects)
1354 tn = dev->freeObjects;
1355 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1356 dev->nFreeObjects--;
1358 // Now sweeten it up...
1360 memset(tn,0,sizeof(yaffs_Object));
1363 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1364 INIT_LIST_HEAD(&(tn->hardLinks));
1365 INIT_LIST_HEAD(&(tn->hashLink));
1366 INIT_LIST_HEAD(&tn->siblings);
1368 // Add it to the lost and found directory.
1369 // NB Can't put root or lostNFound in lostNFound so
1370 // check if lostNFound exists first
1371 if(dev->lostNFoundDir)
1373 yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);
1381 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1384 yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);
1387 obj->fake = 1; // it is fake so it has no NAND presence...
1388 obj->renameAllowed= 0; // ... and we're not allowed to rename it...
1389 obj->unlinkAllowed= 0; // ... or unlink it
1392 obj->yst_mode = mode;
1394 obj->chunkId = 0; // Not a valid chunk.
1402 static void yaffs_UnhashObject(yaffs_Object *tn)
1405 yaffs_Device *dev = tn->myDev;
1408 // If it is still linked into the bucket list, free from the list
1409 if(!list_empty(&tn->hashLink))
1411 list_del_init(&tn->hashLink);
1412 bucket = yaffs_HashFunction(tn->objectId);
1413 dev->objectBucket[bucket].count--;
1419 // FreeObject frees up a Object and puts it back on the free list
1420 static void yaffs_FreeObject(yaffs_Object *tn)
1423 yaffs_Device *dev = tn->myDev;
1428 // We're still hooked up to a cached inode.
1429 // Don't delete now, but mark for later deletion
1430 tn->deferedFree = 1;
1435 yaffs_UnhashObject(tn);
1437 // Link into the free list.
1438 tn->siblings.next = (struct list_head *)(dev->freeObjects);
1439 dev->freeObjects = tn;
1440 dev->nFreeObjects++;
1447 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1449 if(obj->deferedFree)
1451 yaffs_FreeObject(obj);
1459 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1461 // Free the list of allocated Objects
1463 yaffs_ObjectList *tmp;
1465 while( dev->allocatedObjectList)
1467 tmp = dev->allocatedObjectList->next;
1468 YFREE(dev->allocatedObjectList->objects);
1469 YFREE(dev->allocatedObjectList);
1471 dev->allocatedObjectList = tmp;
1474 dev->freeObjects = NULL;
1475 dev->nFreeObjects = 0;
1478 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1482 dev->allocatedObjectList = NULL;
1483 dev->freeObjects = NULL;
1484 dev->nFreeObjects = 0;
1486 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1488 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1489 dev->objectBucket[i].count = 0;
1499 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1504 int lowest = 999999;
1507 // First let's see if we can find one that's empty.
1509 for(i = 0; i < 10 && lowest > 0; i++)
1512 x %= YAFFS_NOBJECT_BUCKETS;
1513 if(dev->objectBucket[x].count < lowest)
1515 lowest = dev->objectBucket[x].count;
1521 // If we didn't find an empty list, then try
1522 // looking a bit further for a short one
1524 for(i = 0; i < 10 && lowest > 3; i++)
1527 x %= YAFFS_NOBJECT_BUCKETS;
1528 if(dev->objectBucket[x].count < lowest)
1530 lowest = dev->objectBucket[x].count;
1539 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1541 int bucket = yaffs_FindNiceObjectBucket(dev);
1543 // Now find an object value that has not already been taken
1544 // by scanning the list.
1547 struct list_head *i;
1549 __u32 n = (__u32)bucket;
1551 //yaffs_CheckObjectHashSanity();
1556 n += YAFFS_NOBJECT_BUCKETS;
1557 if(1 ||dev->objectBucket[bucket].count > 0)
1559 list_for_each(i,&dev->objectBucket[bucket].list)
1561 // If there is already one in the list
1562 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1570 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1575 void yaffs_HashObject(yaffs_Object *in)
1577 int bucket = yaffs_HashFunction(in->objectId);
1578 yaffs_Device *dev = in->myDev;
1580 if(!list_empty(&in->hashLink))
1586 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1587 dev->objectBucket[bucket].count++;
1591 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1593 int bucket = yaffs_HashFunction(number);
1594 struct list_head *i;
1597 list_for_each(i,&dev->objectBucket[bucket].list)
1599 // Look if it is in the list
1602 in = list_entry(i, yaffs_Object,hashLink);
1603 if(in->objectId == number)
1606 // Don't tell the VFS about this one if it is defered free
1621 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1624 yaffs_Object *theObject;
1628 number = yaffs_CreateNewObjectNumber(dev);
1631 theObject = yaffs_AllocateEmptyObject(dev);
1635 theObject->fake = 0;
1636 theObject->renameAllowed = 1;
1637 theObject->unlinkAllowed = 1;
1638 theObject->objectId = number;
1639 yaffs_HashObject(theObject);
1640 theObject->variantType = type;
1641 #ifdef CONFIG_YAFFS_WINCE
1642 yfsd_WinFileTimeNow(theObject->win_atime);
1643 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1644 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1648 theObject->yst_atime = theObject->yst_mtime = theObject->yst_ctime = Y_CURRENT_TIME;
1652 case YAFFS_OBJECT_TYPE_FILE:
1653 theObject->variant.fileVariant.fileSize = 0;
1654 theObject->variant.fileVariant.scannedFileSize = 0;
1655 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; // max __u32
1656 theObject->variant.fileVariant.topLevel = 0;
1657 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
1659 case YAFFS_OBJECT_TYPE_DIRECTORY:
1660 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1662 case YAFFS_OBJECT_TYPE_SYMLINK:
1663 // No action required
1665 case YAFFS_OBJECT_TYPE_HARDLINK:
1666 // No action required
1668 case YAFFS_OBJECT_TYPE_SPECIAL:
1669 // No action required
1671 case YAFFS_OBJECT_TYPE_UNKNOWN:
1672 // todo this should not happen
1680 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1682 yaffs_Object *theObject = NULL;
1686 theObject = yaffs_FindObjectByNumber(dev,number);
1691 theObject = yaffs_CreateNewObject(dev,number,type);
1698 YCHAR *yaffs_CloneString(const YCHAR *str)
1700 YCHAR *newStr = NULL;
1704 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1705 yaffs_strcpy(newStr,str);
1713 // Mknod (create) a new object.
1714 // equivalentObject only has meaning for a hard link;
1715 // aliasString only has meaning for a sumlink.
1716 // rdev only has meaning for devices (a subset of special objects)
1717 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1718 yaffs_Object *parent,
1723 yaffs_Object *equivalentObject,
1724 const YCHAR *aliasString,
1729 yaffs_Device *dev = parent->myDev;
1731 // Check if the entry exists. If it does then fail the call since we don't want a dup.
1732 if(yaffs_FindObjectByName(parent,name))
1737 in = yaffs_CreateNewObject(dev,-1,type);
1743 in->variantType = type;
1745 in->yst_mode = mode;
1747 #ifdef CONFIG_YAFFS_WINCE
1748 yfsd_WinFileTimeNow(in->win_atime);
1749 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1750 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1753 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
1755 in->yst_rdev = rdev;
1759 in->nDataChunks = 0;
1761 yaffs_SetObjectName(in,name);
1764 yaffs_AddObjectToDirectory(parent,in);
1766 in->myDev = parent->myDev;
1771 case YAFFS_OBJECT_TYPE_SYMLINK:
1772 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1774 case YAFFS_OBJECT_TYPE_HARDLINK:
1775 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1776 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1777 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1779 case YAFFS_OBJECT_TYPE_FILE: // do nothing
1780 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1781 case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1782 case YAFFS_OBJECT_TYPE_UNKNOWN:
1786 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1787 yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1789 // Could not create the object header, fail the creation
1790 yaffs_DestroyObject(in);
1799 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1801 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1804 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1806 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1809 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1811 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL,parent,name,mode,uid,gid,NULL,NULL,rdev);
1814 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1816 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1819 // NB yaffs_Link returns the object id of the equivalent object.
1820 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1822 // Get the real object in case we were fed a hard link as an equivalent object
1823 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1825 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1827 return equivalentObject;
1837 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1844 newDir = obj->parent; // use the old directory
1847 if(newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1849 T(YAFFS_TRACE_ALWAYS,(TSTR("tragendy: yaffs_ChangeObjectName: newDir is not a directory"TENDSTR)));
1853 // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1854 if(obj->myDev->isYaffs2)
1856 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1860 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1863 deleteOp = (newDir == obj->myDev->deletedDir);
1865 // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1866 // duplicate names are allowed.
1867 // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1871 !yaffs_FindObjectByName(newDir,newName)) &&
1872 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1874 yaffs_SetObjectName(obj,newName);
1877 yaffs_AddObjectToDirectory(newDir,obj);
1879 if(unlinkOp) obj->unlinked = 1;
1881 // If it is a deletion then we mark it as a shrink for gc purposes.
1882 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1893 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1898 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1899 // Special case for case insemsitive systems (eg. WinCE).
1900 // While look-up is case insensitive, the name isn't.
1901 // THerefore we might want to change x.txt to X.txt
1902 if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1908 obj = yaffs_FindObjectByName(oldDir,oldName);
1909 if(obj && obj->renameAllowed)
1911 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1919 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1921 // Scan the buckets and check that the lists
1922 // have as many members as the count says there are
1925 struct list_head *j;
1928 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1932 list_for_each(j,&dev->objectBucket[bucket].list)
1937 if(countEm != dev->objectBucket[bucket].count)
1939 T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
1948 void yaffs_ObjectTest(yaffs_Device *dev)
1950 yaffs_Object *in[1000];
1952 yaffs_Object *inold[1000];
1956 memset(in,0,1000*sizeof(yaffs_Object *));
1957 memset(inold,0,1000*sizeof(yaffs_Object *));
1959 yaffs_CheckObjectHashSanity(dev);
1961 for(j = 0; j < 10; j++)
1965 for(i = 0; i < 1000; i++)
1967 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1970 YINFO("No more inodes");
1974 inNo[i] = in[i]->objectId;
1978 for(i = 0; i < 1000; i++)
1980 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1982 //T(("Differnce in look up test\n"));
1986 // T(("Look up ok\n"));
1990 yaffs_CheckObjectHashSanity(dev);
1992 for(i = 0; i < 1000; i+=3)
1994 yaffs_FreeObject(in[i]);
1999 yaffs_CheckObjectHashSanity(dev);
2006 /////////////////////////// Block Management and Page Allocation ///////////////////
2009 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2011 dev->allocationBlock = -1; // force it to get a new one
2012 //Todo we're assuming the malloc will pass.
2013 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2014 // Set up dynamic blockinfo stuff.
2015 dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2016 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2017 if(dev->blockInfo && dev->chunkBits)
2019 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2020 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2028 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2030 YFREE(dev->blockInfo);
2031 dev->blockInfo = NULL;
2032 YFREE(dev->chunkBits);
2033 dev->chunkBits = NULL;
2037 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2043 if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2045 if(!bi->hasShrinkHeader) return 1; // can gc
2048 // Find the oldest dirty sequence number if we don't know it and save it
2049 // so we don't have to keep recomputing it.
2050 if(!dev->oldestDirtySequence)
2052 seq = dev->sequenceNumber;
2054 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2056 b = yaffs_GetBlockInfo(dev,i);
2057 if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2058 (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2059 b->sequenceNumber < seq)
2061 seq = b->sequenceNumber;
2064 dev->oldestDirtySequence = seq;
2068 // Can't do gc of this block if there are any blocks older than this one that have
2070 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2077 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2078 // for garbage collection.
2083 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2086 int b = dev->currentDirtyChecker;
2092 yaffs_BlockInfo *bi;
2093 static int nonAggressiveSkip = 0;
2095 // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2097 // else (we're doing a leasurely gc), then we only bother to do this if the
2098 // block has only a few pages in use.
2101 nonAggressiveSkip--;
2103 if(!aggressive &&(nonAggressiveSkip > 0))
2108 pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2112 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2116 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2117 iterations = iterations / 16;
2118 if(iterations > 200)
2124 for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2127 if ( b < dev->internalStartBlock || b > dev->internalEndBlock)
2129 b = dev->internalStartBlock;
2132 if(b < dev->internalStartBlock || b > dev->internalEndBlock)
2134 T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2138 bi = yaffs_GetBlockInfo(dev,b);
2140 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2141 (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2142 yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2145 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2149 dev->currentDirtyChecker = b;
2153 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2156 dev->oldestDirtySequence = 0; // clear this
2160 nonAggressiveSkip = 4;
2167 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2169 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2173 // If the block is still healthy erase it and mark as clean.
2174 // If the block has had a data failure, then retire it.
2175 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2177 if(!bi->needsRetiring)
2179 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2182 dev->nErasureFailures++;
2183 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2187 if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2190 for(i = 0; i < dev->nChunksPerBlock; i++)
2192 if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2194 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2202 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2203 dev->nErasedBlocks++;
2205 bi->softDeletions = 0;
2206 bi->hasShrinkHeader=0;
2207 yaffs_ClearChunkBits(dev,blockNo);
2209 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2213 dev->nFreeChunks -= dev->nChunksPerBlock; // We lost a block of free space
2215 yaffs_RetireBlock(dev,blockNo);
2216 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2221 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2224 yaffs_BlockInfo *bi;
2226 for(i= dev->internalStartBlock; i <=dev->internalEndBlock; i++)
2228 bi = yaffs_GetBlockInfo(dev,i);
2229 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2230 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));
2232 for(j = 0; j < dev->nChunksPerBlock; j++)
2234 if(yaffs_CheckChunkBit(dev,i,j))
2236 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2240 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2247 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2251 yaffs_BlockInfo *bi;
2256 if(j < 0 || j > 100)
2259 yaffs_DumpBlockStats(dev);
2264 if(dev->nErasedBlocks < 1)
2266 // Hoosterman we've got a problem.
2267 // Can't get space to gc
2268 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2273 // Find an empty block.
2275 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2277 dev->allocationBlockFinder++;
2278 if(dev->allocationBlockFinder < dev->internalStartBlock || dev->allocationBlockFinder> dev->internalEndBlock)
2280 dev->allocationBlockFinder = dev->internalStartBlock;
2283 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2285 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2287 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2288 dev->sequenceNumber++;
2289 bi->sequenceNumber = dev->sequenceNumber;
2290 dev->nErasedBlocks--;
2291 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq %d, %d left" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber, dev->nErasedBlocks));
2292 return dev->allocationBlockFinder;
2297 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been %d" TENDSTR),dev->nErasedBlocks));
2307 // To determine if we have enough space we just look at the
2308 // number of erased blocks.
2310 static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev)
2312 int reservedChunks = (dev->nReservedBlocks * dev->nChunksPerBlock);
2313 return (dev->nFreeChunks > reservedChunks);
2317 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2320 yaffs_BlockInfo *bi;
2322 if(dev->allocationBlock < 0)
2324 // Get next block to allocate off
2325 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2326 dev->allocationPage = 0;
2329 if(!useReserve && !yaffs_CheckSpaceForAllocation(dev))
2331 // Not enough space to allocate unless we're allowed to use the reserve.
2335 if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2337 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));
2341 // Next page please....
2342 if(dev->allocationBlock >= 0)
2344 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2346 retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
2347 dev->allocationPage;
2349 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2351 dev->allocationPage++;
2355 // If the block is full set the state to full
2356 if(dev->allocationPage >= dev->nChunksPerBlock)
2358 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2359 dev->allocationBlock = -1;
2366 T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2374 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2378 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2380 if(dev->allocationBlock> 0)
2382 n += (dev->nChunksPerBlock - dev->allocationPage);
2389 int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2395 int retVal = YAFFS_OK;
2399 int chunksBefore = yaffs_GetErasedChunks(dev);
2402 yaffs_ExtendedTags tags;
2404 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2406 yaffs_Object *object;
2408 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2410 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2411 //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
2413 //yaffs_VerifyFreeChunks(dev);
2415 bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2417 dev->nFreeChunks -= bi->softDeletions; // Take off the number of soft deleted entries because
2418 // they're going to get really deleted during GC.
2421 if(!yaffs_StillSomeChunkBits(dev,block))
2423 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2424 yaffs_BlockBecameDirty(dev,block);
2429 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2431 for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock;
2432 chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2433 chunkInBlock++, oldChunk++ )
2435 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2438 // This page is in use and might need to be copied off
2442 //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2444 yaffs_InitialiseTags(&tags);
2446 yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2448 object = yaffs_FindObjectByNumber(dev,tags.objectId);
2450 T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2454 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2457 if(object && object->deleted && tags.chunkId != 0)
2459 // Data chunk in a deleted file, throw it away
2460 // It's a soft deleted data chunk,
2461 // No need to copy this, just forget about it and fix up the
2464 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0);
2465 object->nDataChunks--;
2467 if(object->nDataChunks <= 0)
2469 // remeber to clean up the object
2470 dev->gcCleanupList[cleanups] = tags.objectId;
2475 else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2477 // Deleted object header with no data chunks.
2478 // Can be discarded and the file deleted.
2479 object->chunkId = 0;
2480 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2481 object->variant.fileVariant.top = NULL;
2482 yaffs_DoGenericObjectDeletion(object);
2487 // It's either a data chunk in a live file or
2488 // an ObjectHeader, so we're interested in it.
2489 // NB Need to keep the ObjectHeaders of deleted files
2490 // until the whole file has been deleted off
2491 tags.serialNumber++;
2495 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2499 retVal = YAFFS_FAIL;
2504 // Ok, now fix up the Tnodes etc.
2506 if(tags.chunkId == 0)
2509 object->chunkId = newChunk;
2510 object->serial = tags.serialNumber;
2514 // It's a data chunk
2515 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2520 yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);
2525 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2527 //yaffs_VerifyFreeChunks(dev);
2529 // Do any required cleanups
2530 for(i = 0; i < cleanups; i++)
2532 // Time to delete the file too
2533 object = yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2536 yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2537 object->variant.fileVariant.top = NULL;
2538 T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2539 yaffs_DoGenericObjectDeletion(object);
2546 if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2548 T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2552 //yaffs_VerifyFreeChunks(dev);
2558 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2560 // find a file to delete
2561 struct list_head *i;
2565 //Scan the unlinked files looking for one to delete
2566 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2570 l = list_entry(i, yaffs_Object,siblings);
2581 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2583 // This does background deletion on unlinked files.. only deleted ones.
2584 // If we don't have a file we're working on then find one
2585 if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2587 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2590 // OK, we're working on a file...
2591 if(dev->unlinkedDeletion)
2593 yaffs_Object *obj = dev->unlinkedDeletion;
2595 int limit; // Number of chunks to delete in a file.
2596 // NB this can be exceeded, but not by much.
2600 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2602 if(obj->nDataChunks == 0)
2604 // Done all the deleting of data chunks.
2605 // Now dump the header and clean up
2606 yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2607 obj->variant.fileVariant.top = NULL;
2608 yaffs_DoGenericObjectDeletion(obj);
2609 dev->nDeletedFiles--;
2610 dev->nUnlinkedFiles--;
2611 dev->nBackgroundDeletions++;
2612 dev->unlinkedDeletion = NULL;
2621 // New garbage collector
2622 // If we're very low on erased blocks then we do aggressive garbage collection
2623 // otherwise we do "leasurely" garbage collection.
2624 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2625 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2627 // The idea is to help clear out space in a more spread-out manner.
2628 // Dunno if it really does anything useful.
2630 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2634 int gcOk = YAFFS_OK;
2637 //yaffs_DoUnlinkedFileDeletion(dev);
2639 //yaffs_VerifyFreeChunks(dev);
2641 // This loop should pass the first time.
2642 // We'll only see looping here if the erase of the collected block fails.
2646 if(dev->nErasedBlocks < dev->nReservedBlocks)
2648 // We need a block soon...
2653 // We're in no hurry
2657 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2661 dev->garbageCollections++;
2664 dev->passiveGarbageCollections++;
2667 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2669 gcOk = yaffs_GarbageCollectBlock(dev,block);
2672 if(dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0)
2674 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2676 } while((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0) && (maxTries < 2));
2678 return aggressive ? gcOk: YAFFS_OK;
2682 //////////////////////////// TAGS ///////////////////////////////////////
2688 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2692 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2699 for(i = 0; i < 8; i++)
2701 for(j = 1; j &0xff; j<<=1)
2716 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
2718 unsigned ecc = tags->ecc;
2720 yaffs_CalcTagsECC(tags);
2724 if(ecc && ecc <= 64)
2726 // TODO: Handle the failure better. Retire?
2727 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2731 b[ecc / 8] ^= (1 << (ecc & 7));
2733 // Now recvalc the ecc
2734 yaffs_CalcTagsECC(tags);
2736 return 1; // recovered error
2740 // Wierd ecc failure value
2741 // TODO Need to do somethiong here
2742 return -1; //unrecovered error
2748 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2750 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2752 yaffs_CalcTagsECC(tagsPtr);
2754 sparePtr->tagByte0 = tu->asBytes[0];
2755 sparePtr->tagByte1 = tu->asBytes[1];
2756 sparePtr->tagByte2 = tu->asBytes[2];
2757 sparePtr->tagByte3 = tu->asBytes[3];
2758 sparePtr->tagByte4 = tu->asBytes[4];
2759 sparePtr->tagByte5 = tu->asBytes[5];
2760 sparePtr->tagByte6 = tu->asBytes[6];
2761 sparePtr->tagByte7 = tu->asBytes[7];
2764 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2766 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2769 tu->asBytes[0]= sparePtr->tagByte0;
2770 tu->asBytes[1]= sparePtr->tagByte1;
2771 tu->asBytes[2]= sparePtr->tagByte2;
2772 tu->asBytes[3]= sparePtr->tagByte3;
2773 tu->asBytes[4]= sparePtr->tagByte4;
2774 tu->asBytes[5]= sparePtr->tagByte5;
2775 tu->asBytes[6]= sparePtr->tagByte6;
2776 tu->asBytes[7]= sparePtr->tagByte7;
2778 result = yaffs_CheckECCOnTags(tagsPtr);
2781 dev->tagsEccFixed++;
2785 dev->tagsEccUnfixed++;
2789 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2791 memset(spare,0xFF,sizeof(yaffs_Spare));
2797 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2799 // NB There must be tags, data is optional
2800 // If there is data, then an ECC is calculated on it.
2809 //yaffs_SpareInitialise(&spare);
2811 //if(!dev->useNANDECC && buffer)
2813 // yaffs_CalcECC(buffer,&spare);
2816 //yaffs_LoadTagsIntoSpare(&spare,tags);
2818 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2823 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2825 return ( tags->chunkId == chunkInObject &&
2826 tags->objectId == objectId &&
2827 !tags->chunkDeleted) ? 1 : 0;
2831 /////////////////////////////////////////////////////////////////////////////////////////////////////////
2834 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2836 //Get the Tnode, then get the level 0 offset chunk offset
2839 yaffs_ExtendedTags localTags;
2842 yaffs_Device *dev = in->myDev;
2847 // Passed a NULL, so use our own tags space
2851 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2855 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2857 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2862 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2864 //Get the Tnode, then get the level 0 offset chunk offset
2867 yaffs_ExtendedTags localTags;
2869 yaffs_Device *dev = in->myDev;
2874 // Passed a NULL, so use our own tags space
2878 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2883 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2885 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2887 // Delete the entry in the filestructure (if found)
2890 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2895 //T(("No level 0 found for %d\n", chunkInInode));
2900 //T(("Could not find %d to delete\n",chunkInInode));
2906 #ifdef YAFFS_PARANOID
2908 static int yaffs_CheckFileSanity(yaffs_Object *in)
2916 yaffs_Tags localTags;
2917 yaffs_Tags *tags = &localTags;
2922 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2924 //T(("Object not a file\n"));
2928 objId = in->objectId;
2929 fSize = in->variant.fileVariant.fileSize;
2930 nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
2932 for(chunk = 1; chunk <= nChunks; chunk++)
2934 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
2939 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
2941 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
2945 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
2946 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
2954 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
2955 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
2964 //T(("No level 0 found for %d\n", chunk));
2968 return failed ? YAFFS_FAIL : YAFFS_OK;
2973 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
2975 // NB inScan is zero unless scanning. For forward scanning, inScan is > 0; for backward scanning inScan is < 0
2977 yaffs_Device *dev = in->myDev;
2979 yaffs_ExtendedTags existingTags;
2980 yaffs_ExtendedTags newTags;
2981 unsigned existingSerial, newSerial;
2983 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2985 // Just ignore an attempt at putting a chunk into a non-file during scanning
2986 // If it is not during Scanning then something went wrong!
2989 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy:attempt to put data chunk into a non-file" TENDSTR)));
2993 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
2997 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3003 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
3007 // If we're scanning then we need to test for duplicates
3008 // NB This does not need to be efficient since it should only ever
3009 // happen when the power fails during a write, then only one
3010 // chunk should ever be affected.
3012 // Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3013 // Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3017 if(existingChunk != 0)
3019 // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3020 // thus we have to do a FindChunkInFile to get the real chunk id.
3022 // We have a duplicate now we need to decide which one to use:
3024 // Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3025 // Forward scanning YAFFS2: The new one is what we use, dump the old one.
3026 // YAFFS1: Get both sets of tags and compare serial numbers.
3032 // Only do this for forward scanning
3033 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3037 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3040 if(existingChunk <=0)
3042 //Hoosterman - how did this happen?
3044 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3049 // NB The deleted flags should be false, otherwise the chunks will
3050 // not be loaded during a scan
3052 newSerial = newTags.serialNumber;
3053 existingSerial = existingTags.serialNumber;
3056 ( in->myDev->isYaffs2 ||
3057 existingChunk <= 0 ||
3058 ((existingSerial+1) & 3) == newSerial))
3060 // Forward scanning.
3062 // Delete the old one and drop through to update the tnode
3063 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3067 // Backward scanning or we want to use the existing one
3069 // Delete the new one and return early so that the tnode isn't changed
3070 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3077 if(existingChunk == 0)
3082 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3089 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3091 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3093 if(chunkInNAND >= 0)
3095 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3099 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3101 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3108 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3112 yaffs_ExtendedTags tags;
3113 yaffs_BlockInfo *bi;
3115 if(chunkId <= 0) return;
3118 block = chunkId / dev->nChunksPerBlock;
3119 page = chunkId % dev->nChunksPerBlock;
3121 bi = yaffs_GetBlockInfo(dev,block);
3123 T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3126 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3129 // yaffs_SpareInitialise(&spare);
3131 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3133 //read data before write, to ensure correct ecc
3134 //if we're using MTD verification under Linux
3135 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3138 yaffs_InitialiseTags(&tags);
3140 tags.chunkDeleted = 1;
3143 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3144 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3148 dev->nUnmarkedDeletions++;
3152 // Pull out of the management area.
3153 // If the whole block became dirty, this will kick off an erasure.
3154 if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3155 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3156 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3157 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3161 yaffs_ClearChunkBit(dev,block,page);
3165 if(bi->pagesInUse == 0 &&
3166 !bi->hasShrinkHeader &&
3167 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3168 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3170 yaffs_BlockBecameDirty(dev,block);
3176 // T(("Bad news deleting chunk %d\n",chunkId));
3184 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3186 // Find old chunk Need to do this to get serial number
3187 // Write new one and patch into tree.
3188 // Invalidate old tags.
3191 yaffs_ExtendedTags prevTags;
3194 yaffs_ExtendedTags newTags;
3196 yaffs_Device *dev = in->myDev;
3198 yaffs_CheckGarbageCollection(dev);
3200 // Get the previous chunk at this location in the file if it exists
3201 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3204 yaffs_InitialiseTags(&newTags);
3206 newTags.chunkId = chunkInInode;
3207 newTags.objectId = in->objectId;
3208 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3209 newTags.byteCount = nBytes;
3211 // yaffs_CalcTagsECC(&newTags);
3213 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3217 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3220 if(prevChunkId >= 0)
3222 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3226 yaffs_CheckFileSanity(in);
3237 // UpdateObjectHeader updates the header on NAND for an object.
3238 // If name is not NULL, then that new name is used.
3240 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3243 yaffs_BlockInfo *bi;
3245 yaffs_Device *dev = in->myDev;
3251 yaffs_ExtendedTags newTags;
3253 __u8 *buffer = NULL;
3254 YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3256 // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3258 yaffs_ObjectHeader *oh = NULL;
3259 // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3262 if(!in->fake || force)
3265 yaffs_CheckGarbageCollection(dev);
3267 buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3268 oh = (yaffs_ObjectHeader *)buffer;
3270 prevChunkId = in->chunkId;
3272 if(prevChunkId >= 0)
3274 yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3275 memcpy(oldName,oh->name,sizeof(oh->name));
3278 memset(buffer,0xFF,dev->nBytesPerChunk);
3281 oh->type = in->variantType;
3283 oh->yst_mode = in->yst_mode;
3285 #ifdef CONFIG_YAFFS_WINCE
3286 oh->win_atime[0] = in->win_atime[0];
3287 oh->win_ctime[0] = in->win_ctime[0];
3288 oh->win_mtime[0] = in->win_mtime[0];
3289 oh->win_atime[1] = in->win_atime[1];
3290 oh->win_ctime[1] = in->win_ctime[1];
3291 oh->win_mtime[1] = in->win_mtime[1];
3293 oh->yst_uid = in->yst_uid;
3294 oh->yst_gid = in->yst_gid;
3295 oh->yst_atime = in->yst_atime;
3296 oh->yst_mtime = in->yst_mtime;
3297 oh->yst_ctime = in->yst_ctime;
3298 oh->yst_rdev = in->yst_rdev;
3302 oh->parentObjectId = in->parent->objectId;
3306 oh->parentObjectId = 0;
3309 //oh->sum = in->sum;
3312 memset(oh->name,0,sizeof(oh->name));
3313 yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3315 else if(prevChunkId)
3317 memcpy(oh->name, oldName,sizeof(oh->name));
3321 memset(oh->name,0,sizeof(oh->name));
3324 oh->isShrink = isShrink;
3326 switch(in->variantType)
3328 case YAFFS_OBJECT_TYPE_UNKNOWN:
3329 // Should not happen
3331 case YAFFS_OBJECT_TYPE_FILE:
3332 oh->fileSize = (oh->parentObjectId == YAFFS_OBJECTID_DELETED ||
3333 oh->parentObjectId == YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.fileVariant.fileSize;
3335 case YAFFS_OBJECT_TYPE_HARDLINK:
3336 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3338 case YAFFS_OBJECT_TYPE_SPECIAL:
3341 case YAFFS_OBJECT_TYPE_DIRECTORY:
3344 case YAFFS_OBJECT_TYPE_SYMLINK:
3345 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3346 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3351 yaffs_InitialiseTags(&newTags);
3353 newTags.chunkId = 0;
3354 newTags.objectId = in->objectId;
3355 newTags.serialNumber = in->serial;
3357 // Add extra info for file header
3359 newTags.extraHeaderInfoAvailable = 1;
3360 newTags.extraParentObjectId = oh->parentObjectId;
3361 newTags.extraFileLength = oh->fileSize;
3362 newTags.extraIsShrinkHeader = oh->isShrink;
3363 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3364 newTags.extraObjectType = in->variantType;
3366 // Create new chunk in NAND
3367 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3372 in->chunkId = newChunkId;
3374 if(prevChunkId >= 0)
3376 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3381 // If this was a shrink, then mark the block that the chunk lives on
3384 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3385 bi->hasShrinkHeader = 1;
3390 retVal = newChunkId;
3395 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3401 /////////////////////// Short Operations Cache ////////////////////////////////
3402 // In many siturations where there is no high level buffering (eg WinCE) a lot of
3403 // reads might be short sequential reads, and a lot of writes may be short
3404 // sequential writes. eg. scanning/writing a jpeg file.
3405 // In these cases, a short read/write cache can provide a huge perfomance benefit
3406 // with dumb-as-a-rock code.
3407 // There are a limited number (~10) of cache chunks per device so that we don't
3408 // need a very intelligent search.
3414 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3416 yaffs_Device *dev = obj->myDev;
3419 yaffs_ChunkCache *cache;
3420 int chunkWritten = 0;
3422 int nCaches = obj->myDev->nShortOpCaches;
3429 // Find the dirty cache for this object with the lowest chunk id.
3430 for(i = 0; i < nCaches; i++)
3432 if(dev->srCache[i].object == obj &&
3433 dev->srCache[i].dirty)
3435 if(!cache || dev->srCache[i].chunkId < lowest)
3437 cache = &dev->srCache[i];
3438 lowest = cache->chunkId;
3443 if(cache && !cache->locked)
3445 //Write it out and free it up
3448 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3450 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3452 nBytes= YAFFS_BYTES_PER_CHUNK;
3455 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3461 cache->object = NULL;
3464 } while(cache && chunkWritten > 0);
3468 //Hoosterman, disk full while writing cache out.
3469 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3477 // Grab us a chunk for use.
3478 // First look for an empty one.
3479 // Then look for the least recently used non-dirty one.
3480 // Then look for the least recently used dirty one...., flush and look again.
3481 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3487 if(dev->nShortOpCaches > 0)
3489 for(i = 0; i < dev->nShortOpCaches; i++)
3491 if(!dev->srCache[i].object)
3493 //T(("Grabbing empty %d\n",i));
3495 //printf("Grabbing empty %d\n",i);
3497 return &dev->srCache[i];
3504 usage = 0; // just to stop the compiler grizzling
3506 for(i = 0; i < dev->nShortOpCaches; i++)
3508 if(!dev->srCache[i].dirty &&
3509 ((dev->srCache[i].lastUse < usage && theOne >= 0)||
3512 usage = dev->srCache[i].lastUse;
3517 //T(("Grabbing non-empty %d\n",theOne));
3519 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3521 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
3531 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3533 yaffs_ChunkCache *cache;
3534 yaffs_Object *theObj;
3539 if(dev->nShortOpCaches > 0)
3541 // Try find a non-dirty one...
3543 cache = yaffs_GrabChunkCacheWorker(dev);
3547 // They were all dirty, find the last recently used object and flush
3548 // its cache, then find again.
3549 // NB what's here is not very accurate, we actually flush the object
3550 // the last recently used page.
3552 // With locking we can't assume we can use entry zero
3560 for(i = 0; i < dev->nShortOpCaches; i++)
3562 if( dev->srCache[i].object &&
3563 !dev->srCache[i].locked &&
3564 (dev->srCache[i].lastUse < usage || !cache))
3566 usage = dev->srCache[i].lastUse;
3567 theObj = dev->srCache[i].object;
3568 cache = &dev->srCache[i];
3573 if(!cache || cache->dirty)
3577 yaffs_FlushFilesChunkCache(theObj);
3580 cache = yaffs_GrabChunkCacheWorker(dev);
3584 //printf(" pushout %d\n",pushout);
3596 // Find a cached chunk
3597 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3599 yaffs_Device *dev = obj->myDev;
3601 if(dev->nShortOpCaches > 0)
3603 for(i = 0; i < dev->nShortOpCaches; i++)
3605 if(dev->srCache[i].object == obj &&
3606 dev->srCache[i].chunkId == chunkId)
3610 return &dev->srCache[i];
3617 // Mark the chunk for the least recently used algorithym
3618 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3621 if(dev->nShortOpCaches > 0)
3623 if( dev->srLastUse < 0 ||
3624 dev->srLastUse > 100000000)
3626 // Reset the cache usages
3628 for(i = 1; i < dev->nShortOpCaches; i++)
3630 dev->srCache[i].lastUse = 0;
3637 cache->lastUse = dev->srLastUse;
3646 // Invalidate a single cache page.
3647 // Do this when a whole page gets written,
3648 // ie the short cache for this page is no longer valid.
3649 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3651 if(object->myDev->nShortOpCaches > 0)
3653 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3657 cache->object = NULL;
3663 // Invalidate all the cache pages associated with this object
3664 // Do this whenever ther file is deleted or resized.
3665 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3668 yaffs_Device *dev = in->myDev;
3670 if(dev->nShortOpCaches > 0)
3672 // Now invalidate it.
3673 for(i = 0; i < dev->nShortOpCaches; i++)
3675 if(dev->srCache[i].object == in)
3677 dev->srCache[i].object = NULL;
3687 ///////////////////////// File read/write ///////////////////////////////
3688 // Read and write have very similar structures.
3689 // In general the read/write has three parts to it
3690 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3691 // * Some complete chunks
3692 // * An incomplete chunk to end off with
3694 // Curve-balls: the first chunk might also be the last chunk.
3696 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3705 yaffs_ChunkCache *cache;
3713 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3714 start = offset % dev->nBytesPerChunk;
3716 // OK now check for the curveball where the start and end are in
3718 if( (start + n) < dev->nBytesPerChunk)
3724 nToCopy = dev->nBytesPerChunk - start;
3727 cache = yaffs_FindChunkCache(in,chunk);
3729 // If the chunk is already in the cache or it is less than a whole chunk
3730 // then use the cache (if there is caching)
3731 // else bypass the cache.
3732 if( cache || nToCopy != dev->nBytesPerChunk)
3734 if(dev->nShortOpCaches > 0)
3737 // If we can't find the data in the cache, then load it up.
3741 cache = yaffs_GrabChunkCache(in->myDev);
3743 cache->chunkId = chunk;
3746 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3750 yaffs_UseChunkCache(dev,cache,0);
3754 #ifdef CONFIG_YAFFS_WINCE
3755 yfsd_UnlockYAFFS(TRUE);
3757 memcpy(buffer,&cache->data[start],nToCopy);
3759 #ifdef CONFIG_YAFFS_WINCE
3760 yfsd_LockYAFFS(TRUE);
3766 // Read into the local buffer then copy...
3768 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3769 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3770 #ifdef CONFIG_YAFFS_WINCE
3771 yfsd_UnlockYAFFS(TRUE);
3773 memcpy(buffer,&localBuffer[start],nToCopy);
3775 #ifdef CONFIG_YAFFS_WINCE
3776 yfsd_LockYAFFS(TRUE);
3778 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3784 #ifdef CONFIG_YAFFS_WINCE
3785 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3787 // Under WinCE can't do direct transfer. Need to use a local buffer.
3788 // This is because we otherwise screw up WinCE's memory mapper
3789 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3791 #ifdef CONFIG_YAFFS_WINCE
3792 yfsd_UnlockYAFFS(TRUE);
3794 memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3796 #ifdef CONFIG_YAFFS_WINCE
3797 yfsd_LockYAFFS(TRUE);
3798 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3802 // A full chunk. Read directly into the supplied buffer.
3803 yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3819 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes, int writeThrough)
3828 int startOfWrite = offset;
3829 int chunkWritten = 0;
3837 while(n > 0 && chunkWritten >= 0)
3839 chunk = offset / dev->nBytesPerChunk + 1;
3840 start = offset % dev->nBytesPerChunk;
3843 // OK now check for the curveball where the start and end are in
3846 if((start + n) < dev->nBytesPerChunk)
3850 // Now folks, to calculate how many bytes to write back....
3851 // If we're overwriting and not writing to then end of file then
3852 // we need to write back as much as was there before.
3854 nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3856 if(nBytesRead > dev->nBytesPerChunk)
3858 nBytesRead = dev->nBytesPerChunk;
3861 nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3866 nToCopy = dev->nBytesPerChunk - start;
3867 nToWriteBack = dev->nBytesPerChunk;
3870 if(nToCopy != dev->nBytesPerChunk)
3872 // An incomplete start or end chunk (or maybe both start and end chunk)
3873 if(dev->nShortOpCaches > 0)
3875 yaffs_ChunkCache *cache;
3876 // If we can't find the data in the cache, then load it up.
3877 cache = yaffs_FindChunkCache(in,chunk);
3878 if(!cache && yaffs_CheckSpaceForAllocation(in->myDev))
3880 cache = yaffs_GrabChunkCache(in->myDev);
3882 cache->chunkId = chunk;
3885 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3890 yaffs_UseChunkCache(dev,cache,1);
3892 #ifdef CONFIG_YAFFS_WINCE
3893 yfsd_UnlockYAFFS(TRUE);
3896 memcpy(&cache->data[start],buffer,nToCopy);
3898 #ifdef CONFIG_YAFFS_WINCE
3899 yfsd_LockYAFFS(TRUE);
3902 cache->nBytes = nToWriteBack;
3906 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3916 chunkWritten = -1; // fail the write
3921 // An incomplete start or end chunk (or maybe both start and end chunk)
3922 // Read into the local buffer then copy, then copy over and write back.
3924 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3926 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3928 #ifdef CONFIG_YAFFS_WINCE
3929 yfsd_UnlockYAFFS(TRUE);
3932 memcpy(&localBuffer[start],buffer,nToCopy);
3934 #ifdef CONFIG_YAFFS_WINCE
3935 yfsd_LockYAFFS(TRUE);
3937 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
3939 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3941 //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3948 #ifdef CONFIG_YAFFS_WINCE
3949 // Under WinCE can't do direct transfer. Need to use a local buffer.
3950 // This is because we otherwise screw up WinCE's memory mapper
3951 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3952 #ifdef CONFIG_YAFFS_WINCE
3953 yfsd_UnlockYAFFS(TRUE);
3955 memcpy(localBuffer,buffer,dev->nBytesPerChunk);
3956 #ifdef CONFIG_YAFFS_WINCE
3957 yfsd_LockYAFFS(TRUE);
3959 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
3960 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3962 // A full chunk. Write directly from the supplied buffer.
3963 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
3965 // Since we've overwritten the cached data, we better invalidate it.
3966 yaffs_InvalidateChunkCache(in,chunk);
3967 //T(("Write to chunk %d %d\n",chunk,chunkWritten));
3970 if(chunkWritten >= 0)
3980 // Update file object
3982 if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
3984 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
3992 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
3995 yaffs_Device *dev = in->myDev;
3996 int oldFileSize = in->variant.fileVariant.fileSize;
3999 int lastDel = 1 + (oldFileSize-1)/dev->nBytesPerChunk;
4001 int startDel = 1 + (newSize + dev->nBytesPerChunk - 1)/
4002 dev->nBytesPerChunk;
4006 // Delete backwards so that we don't end up with holes if
4007 // power is lost part-way through the operation.
4008 for(i = lastDel; i >= startDel; i--)
4010 // NB this could be optimised somewhat,
4011 // eg. could retrieve the tags and write them without
4012 // using yaffs_DeleteChunk
4014 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
4017 if(chunkId < (dev->internalStartBlock * dev->nChunksPerBlock) ||
4018 chunkId >= ((dev->internalEndBlock+1) * dev->nChunksPerBlock))
4020 T(YAFFS_TRACE_ALWAYS,(TSTR("Found daft chunkId %d for %d"TENDSTR),chunkId,i));
4025 yaffs_DeleteChunk(dev,chunkId,1,__LINE__);
4032 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
4035 int oldFileSize = in->variant.fileVariant.fileSize;
4036 int sizeOfPartialChunk;
4037 yaffs_Device *dev = in->myDev;
4039 sizeOfPartialChunk = newSize % dev->nBytesPerChunk;
4042 yaffs_FlushFilesChunkCache(in);
4043 yaffs_InvalidateWholeChunkCache(in);
4045 yaffs_CheckGarbageCollection(dev);
4047 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
4049 return yaffs_GetFileSize(in);
4052 if(newSize < oldFileSize)
4055 yaffs_PruneResizedChunks(in,newSize);
4057 if(sizeOfPartialChunk != 0)
4059 int lastChunk = 1+ newSize/dev->nBytesPerChunk;
4060 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4062 // Got to read and rewrite the last chunk with its new size and zero pad
4063 yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
4065 memset(localBuffer + sizeOfPartialChunk,0, dev->nBytesPerChunk - sizeOfPartialChunk);
4067 yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
4069 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4072 in->variant.fileVariant.fileSize = newSize;
4074 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
4076 // Write a new object header to show we've shrunk the file
4077 // Do this only if the file is not in the deleted directories.
4078 if(in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
4079 in->parent->objectId != YAFFS_OBJECTID_DELETED
4082 yaffs_UpdateObjectHeader(in,NULL, 0, 1);
4096 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4098 obj = yaffs_GetEquivalentObject(obj);
4100 switch(obj->variantType)
4102 case YAFFS_OBJECT_TYPE_FILE:
4103 return obj->variant.fileVariant.fileSize;
4104 case YAFFS_OBJECT_TYPE_SYMLINK:
4105 return yaffs_strlen(obj->variant.symLinkVariant.alias);
4113 // yaffs_FlushFile() updates the file's
4116 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
4121 //T(("flushing object header\n"));
4123 yaffs_FlushFilesChunkCache(in);
4126 #ifdef CONFIG_YAFFS_WINCE
4127 yfsd_WinFileTimeNow(in->win_mtime);
4130 in->yst_mtime = Y_CURRENT_TIME;
4135 retVal = (yaffs_UpdateObjectHeader(in,NULL,0,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
4147 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4150 // First off, invalidate the file's data in the cache, without flushing.
4151 yaffs_InvalidateWholeChunkCache(in);
4153 if(in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir))
4155 // Move to the unlinked directory so we have a record that it was deleted.
4156 yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4161 yaffs_RemoveObjectFromDirectory(in);
4162 yaffs_DeleteChunk(in->myDev,in->chunkId,1,__LINE__);
4168 in->myInode->u.generic_ip = NULL;
4174 yaffs_FreeObject(in);
4179 // yaffs_DeleteFile deletes the whole file data
4180 // and the inode associated with the file.
4181 // It does not delete the links associated with the file.
4182 static int yaffs_UnlinkFile(yaffs_Object *in)
4185 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
4187 // Delete the file data & tnodes
4189 yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
4192 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4194 return yaffs_DoGenericObjectDeletion(in);
4197 int immediateDeletion=0;
4202 //in->myDev->nUnlinkedFiles++;
4203 //in->renameAllowed = 0;
4207 immediateDeletion = 1;
4213 immediateDeletion = 1;
4217 if(immediateDeletion)
4219 retVal = yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4220 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
4222 in->myDev->nDeletedFiles++;
4223 if( 0 && in->myDev->isYaffs2)
4225 yaffs_ResizeFile(in,0);
4227 yaffs_SoftDeleteFile(in);
4231 retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
4241 int yaffs_DeleteFile(yaffs_Object *in)
4243 int retVal = YAFFS_OK;
4245 if(in->nDataChunks > 0)
4247 // Use soft deletion
4250 retVal = yaffs_UnlinkFile(in);
4252 if(retVal == YAFFS_OK &&
4257 in->myDev->nDeletedFiles++;
4258 yaffs_SoftDeleteFile(in);
4260 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
4264 // The file has no data chunks so we toss it immediately
4265 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4266 in->variant.fileVariant.top = NULL;
4267 yaffs_DoGenericObjectDeletion(in);
4273 static int yaffs_DeleteDirectory(yaffs_Object *in)
4275 //First check that the directory is empty.
4276 if(list_empty(&in->variant.directoryVariant.children))
4278 return yaffs_DoGenericObjectDeletion(in);
4285 static int yaffs_DeleteSymLink(yaffs_Object *in)
4287 YFREE(in->variant.symLinkVariant.alias);
4289 return yaffs_DoGenericObjectDeletion(in);
4292 static int yaffs_DeleteHardLink(yaffs_Object *in)
4294 // remove this hardlink from the list assocaited with the equivalent
4296 list_del(&in->hardLinks);
4297 return yaffs_DoGenericObjectDeletion(in);
4301 static void yaffs_DestroyObject(yaffs_Object *obj)
4303 switch(obj->variantType)
4305 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
4306 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
4307 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
4308 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
4309 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
4310 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
4315 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4319 if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4321 return yaffs_DeleteHardLink(obj);
4323 else if(!list_empty(&obj->hardLinks))
4325 // Curve ball: We're unlinking an object that has a hardlink.
4327 // This problem arises because we are not strictly following
4328 // The Linux link/inode model.
4330 // We can't really delete the object.
4331 // Instead, we do the following:
4332 // - Select a hardlink.
4333 // - Unhook it from the hard links
4334 // - Unhook it from its parent directory (so that the rename can work)
4335 // - Rename the object to the hardlink's name.
4336 // - Delete the hardlink
4341 YCHAR name[YAFFS_MAX_NAME_LENGTH+1];
4343 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
4345 list_del_init(&hl->hardLinks);
4346 list_del_init(&hl->siblings);
4348 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
4350 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
4352 if(retVal == YAFFS_OK)
4354 retVal = yaffs_DoGenericObjectDeletion(hl);
4361 switch(obj->variantType)
4363 case YAFFS_OBJECT_TYPE_FILE:
4364 return yaffs_UnlinkFile(obj);
4366 case YAFFS_OBJECT_TYPE_DIRECTORY:
4367 return yaffs_DeleteDirectory(obj);
4369 case YAFFS_OBJECT_TYPE_SYMLINK:
4370 return yaffs_DeleteSymLink(obj);
4372 case YAFFS_OBJECT_TYPE_SPECIAL:
4373 return yaffs_DoGenericObjectDeletion(obj);
4375 case YAFFS_OBJECT_TYPE_HARDLINK:
4376 case YAFFS_OBJECT_TYPE_UNKNOWN:
4383 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4387 obj = yaffs_FindObjectByName(dir,name);
4389 if(obj && obj->unlinkAllowed)
4391 return yaffs_UnlinkWorker(obj);
4398 //////////////// Initialisation Scanning /////////////////
4402 // For now we use the SmartMedia check.
4403 // We look at the blockStatus byte in the first two chunks
4404 // These must be 0xFF to pass as OK.
4405 // todo: this function needs to be modifyable foir different NAND types
4406 // and different chunk sizes. Suggest make this into a per-device configurable
4408 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4410 yaffsExtendedTags *tags;
4412 yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&tags,1);
4414 if(yaffs_CountBits(spare.blockStatus) < 7)
4419 if(spare.blockStatus != 0xFF)
4424 yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4427 if(yaffs_CountBits(spare.blockStatus) < 7)
4432 if(spare.blockStatus != 0xFF)
4453 static int yaffs_Scan(yaffs_Device *dev)
4455 yaffs_ExtendedTags tags;
4460 int nBlocksToScan = 0;
4465 yaffs_BlockState state;
4466 yaffs_Object *hardList = NULL;
4468 yaffs_BlockInfo *bi;
4470 yaffs_ObjectHeader *oh;
4472 yaffs_Object *parent;
4473 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
4477 yaffs_BlockIndex *blockIndex = NULL;
4479 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),dev->internalStartBlock,dev->internalEndBlock));
4481 chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4484 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4488 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
4492 // Scan all the blocks to determine their state
4493 for(blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++)
4495 bi = yaffs_GetBlockInfo(dev,blk);
4496 yaffs_ClearChunkBits(dev,blk);
4498 bi->softDeletions = 0;
4500 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4502 bi->blockState = state;
4503 bi->sequenceNumber = sequenceNumber;
4505 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block scanning block %d state %d seq %d" TENDSTR),blk,state,sequenceNumber));
4507 if(state == YAFFS_BLOCK_STATE_DEAD)
4509 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4511 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4513 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block empty " TENDSTR)));
4514 dev->nErasedBlocks++;
4515 dev->nFreeChunks += dev->nChunksPerBlock;
4517 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4520 // Determine the highest sequence number
4521 if( dev->isYaffs2 &&
4522 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
4523 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
4526 blockIndex[nBlocksToScan].seq = sequenceNumber;
4527 blockIndex[nBlocksToScan].block = blk;
4531 if(sequenceNumber >= dev->sequenceNumber)
4533 dev->sequenceNumber = sequenceNumber;
4536 else if(dev->isYaffs2)
4538 // TODO: Nasty sequence number!
4539 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
4546 // Dungy old bubble sort for now...
4549 yaffs_BlockIndex temp;
4553 for(i = 0; i < nBlocksToScan; i++)
4554 for(j = i+1; j < nBlocksToScan; j++)
4555 if(blockIndex[i].seq > blockIndex[j].seq)
4557 temp = blockIndex[j];
4558 blockIndex[j] = blockIndex[i];
4559 blockIndex[i] = temp;
4564 // Now scan the blocks looking at the data.
4568 endIterator = nBlocksToScan-1;
4569 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("%d blocks to be scanned" TENDSTR),nBlocksToScan));
4573 startIterator = dev->internalStartBlock;
4574 endIterator = dev->internalEndBlock;
4577 // For each block....
4578 for(blockIterator = startIterator; blockIterator <= endIterator; blockIterator++)
4583 // get the block to scan in the correct order
4584 blk = blockIndex[blockIterator].block;
4588 blk = blockIterator;
4592 bi = yaffs_GetBlockInfo(dev,blk);
4593 state = bi->blockState;
4597 // For each chunk in each block that needs scanning....
4598 for(c = 0; c < dev->nChunksPerBlock &&
4599 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++)
4601 // Read the tags and decide what to do
4602 chunk = blk * dev->nChunksPerBlock + c;
4604 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
4606 // Let's have a good look at this chunk...
4609 if(!dev->isYaffs2 && tags.chunkDeleted)
4614 dev->nFreeChunks ++;
4615 //T((" %d %d deleted\n",blk,c));
4617 else if(!tags.chunkUsed)
4619 // An unassigned chunk in the block
4620 // This means that either the block is empty or
4621 // this is the one being allocated from
4625 // We're looking at the first chunk in the block so the block is unused
4626 state = YAFFS_BLOCK_STATE_EMPTY;
4627 dev->nErasedBlocks++;
4631 // this is the block being allocated from
4632 T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4633 state = YAFFS_BLOCK_STATE_ALLOCATING;
4634 dev->allocationBlock = blk;
4635 dev->allocationPage = c;
4636 dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
4637 // go forth from here.
4638 //Yaffs2 sanity check:
4639 // This should be the one with the highest sequence number
4640 if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
4642 T(YAFFS_TRACE_ALWAYS,
4643 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
4644 blk,bi->sequenceNumber,dev->sequenceNumber));
4648 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4650 else if(tags.chunkId > 0)
4652 // chunkId > 0 so it is a data chunk...
4653 unsigned int endpos;
4655 yaffs_SetChunkBit(dev,blk,c);
4658 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4659 // PutChunkIntoFile checks for a clash (two data chunks with
4660 // the same chunkId).
4661 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4662 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
4663 if(in->variantType == YAFFS_OBJECT_TYPE_FILE && in->variant.fileVariant.scannedFileSize <endpos)
4665 in->variant.fileVariant.scannedFileSize = endpos;
4666 if(!dev->useHeaderFileSize)
4668 in->variant.fileVariant.fileSize = in->variant.fileVariant.scannedFileSize;
4672 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
4676 // chunkId == 0, so it is an ObjectHeader.
4677 // Thus, we read in the object header and make the object
4678 yaffs_SetChunkBit(dev,blk,c);
4681 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
4683 oh = (yaffs_ObjectHeader *)chunkData;
4685 in = yaffs_FindObjectByNumber(dev,tags.objectId);
4686 if(in && in->variantType != oh->type)
4688 // This should not happen, but somehow
4689 // Wev'e ended up with an objectId that has been reused but not yet
4690 // deleted, and worse still it has changed type. Delete the old object.
4692 yaffs_DestroyObject(in);
4697 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4701 // We have already filled this one. We have a duplicate and need to resolve it.
4703 unsigned existingSerial = in->serial;
4704 unsigned newSerial = tags.serialNumber;
4706 if( dev->isYaffs2 ||
4707 ((existingSerial+1) & 3) == newSerial)
4709 // Use new one - destroy the exisiting one
4710 yaffs_DeleteChunk(dev,in->chunkId,1,__LINE__);
4715 // Use existing - destroy this one.
4716 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
4721 (tags.objectId == YAFFS_OBJECTID_ROOT ||
4722 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4724 // We only load some info, don't fiddle with directory structure
4726 in->variantType = oh->type;
4728 in->yst_mode = oh->yst_mode;
4729 #ifdef CONFIG_YAFFS_WINCE
4730 in->win_atime[0] = oh->win_atime[0];
4731 in->win_ctime[0] = oh->win_ctime[0];
4732 in->win_mtime[0] = oh->win_mtime[0];
4733 in->win_atime[1] = oh->win_atime[1];
4734 in->win_ctime[1] = oh->win_ctime[1];
4735 in->win_mtime[1] = oh->win_mtime[1];
4737 in->yst_uid = oh->yst_uid;
4738 in->yst_gid = oh->yst_gid;
4739 in->yst_atime = oh->yst_atime;
4740 in->yst_mtime = oh->yst_mtime;
4741 in->yst_ctime = oh->yst_ctime;
4742 in->yst_rdev = oh->yst_rdev;
4744 in->chunkId = chunk;
4749 // we need to load this info
4752 in->variantType = oh->type;
4754 in->yst_mode = oh->yst_mode;
4755 #ifdef CONFIG_YAFFS_WINCE
4756 in->win_atime[0] = oh->win_atime[0];
4757 in->win_ctime[0] = oh->win_ctime[0];
4758 in->win_mtime[0] = oh->win_mtime[0];
4759 in->win_atime[1] = oh->win_atime[1];
4760 in->win_ctime[1] = oh->win_ctime[1];
4761 in->win_mtime[1] = oh->win_mtime[1];
4763 in->yst_uid = oh->yst_uid;
4764 in->yst_gid = oh->yst_gid;
4765 in->yst_atime = oh->yst_atime;
4766 in->yst_mtime = oh->yst_mtime;
4767 in->yst_ctime = oh->yst_ctime;
4768 in->yst_rdev = oh->yst_rdev;
4770 in->chunkId = chunk;
4772 yaffs_SetObjectName(in,oh->name);
4775 // directory stuff...
4776 // hook up to parent
4778 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4779 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4781 // Set up as a directory
4782 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4783 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4785 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4787 // Hoosterman, another problem....
4788 // We're trying to use a non-directory as a directory
4790 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4791 parent = dev->lostNFoundDir;
4794 yaffs_AddObjectToDirectory(parent,in);
4796 if(0 && (parent == dev->deletedDir ||
4797 parent == dev->unlinkedDir))
4799 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4800 dev->nDeletedFiles++;
4803 // Note re hardlinks.
4804 // Since we might scan a hardlink before its equivalent object is scanned
4805 // we put them all in a list.
4806 // After scanning is complete, we should have all the objects, so we run through this
4807 // list and fix up all the chains.
4809 switch(in->variantType)
4811 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
4813 case YAFFS_OBJECT_TYPE_FILE:
4814 if(dev->isYaffs2 && oh->isShrink)
4816 // Prune back the shrunken chunks
4817 yaffs_PruneResizedChunks(in,oh->fileSize);
4818 // Mark the block as having a shrinkHeader
4819 bi->hasShrinkHeader = 1;
4822 if(dev->useHeaderFileSize)
4824 in->variant.fileVariant.fileSize = oh->fileSize;
4827 case YAFFS_OBJECT_TYPE_HARDLINK:
4828 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4829 in->hardLinks.next = (struct list_head *)hardList;
4832 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
4834 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4836 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
4837 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4841 if(parent == dev->deletedDir)
4843 yaffs_DestroyObject(in);
4844 bi->hasShrinkHeader = 1;
4846 //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
4851 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4853 // If we got this far while scanning, then the block is fully allocated.
4854 state = YAFFS_BLOCK_STATE_FULL;
4857 bi->blockState = state;
4859 // Now let's see if it was dirty
4860 if( bi->pagesInUse == 0 &&
4861 !bi->hasShrinkHeader &&
4862 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4864 yaffs_BlockBecameDirty(dev,blk);
4874 // Ok, we've done all the scanning.
4876 // Fix up the hard link chains.
4877 // We should now have scanned all the objects, now it's time to add these
4882 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4884 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4888 // Add the hardlink pointers
4889 hl->variant.hardLinkVariant.equivalentObject=in;
4890 list_add(&hl->hardLinks,&in->hardLinks);
4894 //Todo Need to report/handle this better.
4895 // Got a problem... hardlink to a non-existant object
4896 hl->variant.hardLinkVariant.equivalentObject=NULL;
4897 INIT_LIST_HEAD(&hl->hardLinks);
4904 struct list_head *i;
4905 struct list_head *n;
4908 // Soft delete all the unlinked files
4909 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4913 l = list_entry(i, yaffs_Object,siblings);
4914 yaffs_DestroyObject(l);
4919 yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
4921 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan ends" TENDSTR)));
4927 static int yaffs_ScanBackwards(yaffs_Device *dev)
4929 yaffs_ExtendedTags tags;
4934 int nBlocksToScan = 0;
4939 yaffs_BlockState state;
4940 yaffs_Object *hardList = NULL;
4942 yaffs_BlockInfo *bi;
4944 yaffs_ObjectHeader *oh;
4946 yaffs_Object *parent;
4947 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
4951 yaffs_BlockIndex *blockIndex = NULL;
4956 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
4960 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards starts intstartblk %d intendblk %d..." TENDSTR),dev->internalStartBlock,dev->internalEndBlock));
4962 chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4965 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4969 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
4973 // Scan all the blocks to determine their state
4974 for(blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++)
4976 bi = yaffs_GetBlockInfo(dev,blk);
4977 yaffs_ClearChunkBits(dev,blk);
4979 bi->softDeletions = 0;
4981 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4983 bi->blockState = state;
4984 bi->sequenceNumber = sequenceNumber;
4986 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block scanning block %d state %d seq %d" TENDSTR),blk,state,sequenceNumber));
4988 if(state == YAFFS_BLOCK_STATE_DEAD)
4990 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4992 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4994 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block empty " TENDSTR)));
4995 dev->nErasedBlocks++;
4996 dev->nFreeChunks += dev->nChunksPerBlock;
4998 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5001 // Determine the highest sequence number
5002 if( dev->isYaffs2 &&
5003 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
5004 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
5007 blockIndex[nBlocksToScan].seq = sequenceNumber;
5008 blockIndex[nBlocksToScan].block = blk;
5012 if(sequenceNumber >= dev->sequenceNumber)
5014 dev->sequenceNumber = sequenceNumber;
5017 else if(dev->isYaffs2)
5019 // TODO: Nasty sequence number!
5020 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
5027 // Dungy old bubble sort for now...
5030 yaffs_BlockIndex temp;
5034 for(i = 0; i < nBlocksToScan; i++)
5035 for(j = i+1; j < nBlocksToScan; j++)
5036 if(blockIndex[i].seq > blockIndex[j].seq)
5038 temp = blockIndex[j];
5039 blockIndex[j] = blockIndex[i];
5040 blockIndex[i] = temp;
5045 // Now scan the blocks looking at the data.
5049 endIterator = nBlocksToScan-1;
5050 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("%d blocks to be scanned" TENDSTR),nBlocksToScan));
5054 // For each block.... backwards
5055 for(blockIterator = endIterator; blockIterator >= startIterator; blockIterator--)
5058 // get the block to scan in the correct order
5059 blk = blockIndex[blockIterator].block;
5062 bi = yaffs_GetBlockInfo(dev,blk);
5063 state = bi->blockState;
5067 if( 0 && // Disable since this is redundant.
5068 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5070 // Let's look at the first chunk in the block
5071 chunk = blk * dev->nChunksPerBlock;
5073 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
5075 // Let's have a good look at this chunk...
5079 // An unassigned chunk in the block
5080 // This means that either the block is empty or
5081 // this is the one being allocated from
5083 // We're looking at the first chunk in the block so the block is unused
5084 state = YAFFS_BLOCK_STATE_EMPTY;
5085 dev->nErasedBlocks++;
5086 dev->nFreeChunks += dev->nChunksPerBlock;
5091 // For each chunk in each block that needs scanning....
5092 for(c = dev->nChunksPerBlock-1; c >= 0 &&
5093 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
5094 state == YAFFS_BLOCK_STATE_ALLOCATING); c--)
5096 // Scan backwards...
5097 // Read the tags and decide what to do
5098 chunk = blk * dev->nChunksPerBlock + c;
5100 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
5102 // Let's have a good look at this chunk...
5106 // An unassigned chunk in the block
5107 // This means that either the block is empty or
5108 // this is the one being allocated from
5112 // We're looking at the first chunk in the block so the block is unused
5113 state = YAFFS_BLOCK_STATE_EMPTY;
5114 dev->nErasedBlocks++;
5118 // this is the block being allocated from
5119 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5121 T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
5123 state = YAFFS_BLOCK_STATE_ALLOCATING;
5124 dev->allocationBlock = blk;
5125 dev->allocationPage = c;
5126 dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
5127 // go forth from here.
5128 //Yaffs2 sanity check:
5129 // This should be the one with the highest sequence number
5130 if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
5132 T(YAFFS_TRACE_ALWAYS,
5133 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
5134 blk,bi->sequenceNumber,dev->sequenceNumber));
5138 dev->nFreeChunks ++;
5140 else if(tags.chunkId > 0)
5142 // chunkId > 0 so it is a data chunk...
5143 unsigned int endpos;
5145 __u32 chunkBase = (tags.chunkId - 1)* dev->nBytesPerChunk;
5147 yaffs_SetChunkBit(dev,blk,c);
5150 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
5151 if(in->variantType == YAFFS_OBJECT_TYPE_FILE &&
5152 chunkBase < in->variant.fileVariant.shrinkSize)
5154 // This has not been invalidated by a resize
5155 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,-1);
5158 // File size is calculated by looking at the data chunks if we have not
5159 // seen an object header yet. Stop this practice once we find an object header.
5160 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
5161 if(!in->valid && // have not got an object header yet
5162 in->variant.fileVariant.scannedFileSize <endpos)
5164 in->variant.fileVariant.scannedFileSize = endpos;
5165 in->variant.fileVariant.fileSize = in->variant.fileVariant.scannedFileSize;
5171 // This chunk has been invalidated by a resize, so delete
5172 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
5176 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
5180 // chunkId == 0, so it is an ObjectHeader.
5181 // Thus, we read in the object header and make the object
5182 yaffs_SetChunkBit(dev,blk,c);
5188 if(tags.extraHeaderInfoAvailable)
5190 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,tags.extraObjectType);
5194 if(!in || !in->valid)
5197 // If we don't have valid info then we need to read the chunk
5198 // TODO In future we can probably defer reading the chunk and
5199 // living with invalid data until needed.
5201 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
5203 oh = (yaffs_ObjectHeader *)chunkData;
5206 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
5212 // TODO Hoosterman we have a problem!
5213 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: Could not make object for object %d at chunk %d during scan" TENDSTR),tags.objectId,chunk));
5219 // We have already filled this one. We have a duplicate that will be discarded, but
5220 // we first have to suck out resize info if it is a file.
5222 if( (in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
5223 ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
5224 (tags.extraHeaderInfoAvailable && tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
5227 __u32 thisSize = (oh) ? oh->fileSize : tags.extraFileLength;
5228 __u32 parentObjectId = (oh) ? oh->parentObjectId : tags.extraParentObjectId;
5229 unsigned isShrink = (oh) ? oh->isShrink : tags.extraIsShrinkHeader;
5231 // If it is deleted (unlinked at start also means deleted)
5232 // we treat the file size as being zeroed at this point.
5233 if(parentObjectId == YAFFS_OBJECTID_DELETED ||
5234 parentObjectId == YAFFS_OBJECTID_UNLINKED)
5240 if(in->variant.fileVariant.shrinkSize > thisSize)
5242 in->variant.fileVariant.shrinkSize = thisSize;
5247 bi->hasShrinkHeader = 1;
5251 // Use existing - destroy this one.
5252 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
5257 (tags.objectId == YAFFS_OBJECTID_ROOT ||
5258 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
5260 // We only load some info, don't fiddle with directory structure
5262 in->variantType = oh->type;
5264 in->yst_mode = oh->yst_mode;
5265 #ifdef CONFIG_YAFFS_WINCE
5266 in->win_atime[0] = oh->win_atime[0];
5267 in->win_ctime[0] = oh->win_ctime[0];
5268 in->win_mtime[0] = oh->win_mtime[0];
5269 in->win_atime[1] = oh->win_atime[1];
5270 in->win_ctime[1] = oh->win_ctime[1];
5271 in->win_mtime[1] = oh->win_mtime[1];
5273 in->yst_uid = oh->yst_uid;
5274 in->yst_gid = oh->yst_gid;
5275 in->yst_atime = oh->yst_atime;
5276 in->yst_mtime = oh->yst_mtime;
5277 in->yst_ctime = oh->yst_ctime;
5278 in->yst_rdev = oh->yst_rdev;
5280 in->chunkId = chunk;
5285 // we need to load this info
5288 in->variantType = oh->type;
5290 in->yst_mode = oh->yst_mode;
5291 #ifdef CONFIG_YAFFS_WINCE
5292 in->win_atime[0] = oh->win_atime[0];
5293 in->win_ctime[0] = oh->win_ctime[0];
5294 in->win_mtime[0] = oh->win_mtime[0];
5295 in->win_atime[1] = oh->win_atime[1];
5296 in->win_ctime[1] = oh->win_ctime[1];
5297 in->win_mtime[1] = oh->win_mtime[1];
5299 in->yst_uid = oh->yst_uid;
5300 in->yst_gid = oh->yst_gid;
5301 in->yst_atime = oh->yst_atime;
5302 in->yst_mtime = oh->yst_mtime;
5303 in->yst_ctime = oh->yst_ctime;
5304 in->yst_rdev = oh->yst_rdev;
5306 in->chunkId = chunk;
5308 yaffs_SetObjectName(in,oh->name);
5311 // directory stuff...
5312 // hook up to parent
5314 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
5315 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
5317 // Set up as a directory
5318 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
5319 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
5321 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5323 // Hoosterman, another problem....
5324 // We're trying to use a non-directory as a directory
5326 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
5327 parent = dev->lostNFoundDir;
5330 yaffs_AddObjectToDirectory(parent,in);
5332 if((parent == dev->deletedDir ||
5333 parent == dev->unlinkedDir))
5335 in->deleted = 1; // If it is unlinked at start up then it wants deleting
5340 // Mark the block as having a shrinkHeader
5341 bi->hasShrinkHeader = 1;
5345 // Note re hardlinks.
5346 // Since we might scan a hardlink before its equivalent object is scanned
5347 // we put them all in a list.
5348 // After scanning is complete, we should have all the objects, so we run through this
5349 // list and fix up all the chains.
5351 switch(in->variantType)
5353 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
5355 case YAFFS_OBJECT_TYPE_FILE:
5358 if(in->variant.fileVariant.scannedFileSize < oh->fileSize)
5360 in->variant.fileVariant.fileSize = oh->fileSize;
5361 in->variant.fileVariant.scannedFileSize = in->variant.fileVariant.fileSize;
5366 if(in->variant.fileVariant.shrinkSize > oh->fileSize)
5368 in->variant.fileVariant.shrinkSize = oh->fileSize;
5373 case YAFFS_OBJECT_TYPE_HARDLINK:
5374 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
5375 in->hardLinks.next = (struct list_head *)hardList;
5378 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
5380 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
5382 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
5383 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
5388 if(parent == dev->deletedDir)
5390 yaffs_DestroyObject(in);
5391 bi->hasShrinkHeader = 1;
5394 //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
5399 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5401 // If we got this far while scanning, then the block is fully allocated.
5402 state = YAFFS_BLOCK_STATE_FULL;
5405 bi->blockState = state;
5407 // Now let's see if it was dirty
5408 if( bi->pagesInUse == 0 &&
5409 !bi->hasShrinkHeader &&
5410 bi->blockState == YAFFS_BLOCK_STATE_FULL)
5412 yaffs_BlockBecameDirty(dev,blk);
5422 // Ok, we've done all the scanning.
5424 // Fix up the hard link chains.
5425 // We should now have scanned all the objects, now it's time to add these
5430 hardList = (yaffs_Object *)(hardList->hardLinks.next);
5432 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
5436 // Add the hardlink pointers
5437 hl->variant.hardLinkVariant.equivalentObject=in;
5438 list_add(&hl->hardLinks,&in->hardLinks);
5442 //Todo Need to report/handle this better.
5443 // Got a problem... hardlink to a non-existant object
5444 hl->variant.hardLinkVariant.equivalentObject=NULL;
5445 INIT_LIST_HEAD(&hl->hardLinks);
5452 struct list_head *i;
5453 struct list_head *n;
5457 // Soft delete all the unlinked files
5458 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
5462 l = list_entry(i, yaffs_Object,siblings);
5463 yaffs_DestroyObject(l);
5467 // Soft delete all the deletedDir files
5468 list_for_each_safe(i,n,&dev->deletedDir->variant.directoryVariant.children)
5472 l = list_entry(i, yaffs_Object,siblings);
5473 yaffs_DestroyObject(l);
5479 yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
5481 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards ends" TENDSTR)));
5488 ////////////////////////// Directory Functions /////////////////////////
5491 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
5496 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: Trying to add an object to a null pointer directory" TENDSTR)));
5499 if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5501 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: Trying to add an object to a non-directory" TENDSTR)));
5505 if(obj->siblings.prev == NULL)
5508 INIT_LIST_HEAD(&obj->siblings);
5511 else if(!list_empty(&obj->siblings))
5513 // If it is holed up somewhere else, un hook it
5514 list_del_init(&obj->siblings);
5517 list_add(&obj->siblings,&directory->variant.directoryVariant.children);
5518 obj->parent = directory;
5520 if(directory == obj->myDev->unlinkedDir || directory == obj->myDev->deletedDir)
5523 obj->myDev->nUnlinkedFiles++;
5524 obj->renameAllowed = 0;
5528 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
5530 list_del_init(&obj->siblings);
5534 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const YCHAR *name)
5538 struct list_head *i;
5539 YCHAR buffer[YAFFS_MAX_NAME_LENGTH+1];
5546 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: null pointer directory"TENDSTR)));
5549 if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5551 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: non-directory"TENDSTR)));
5555 sum = yaffs_CalcNameSum(name);
5557 list_for_each(i,&directory->variant.directoryVariant.children)
5561 l = list_entry(i, yaffs_Object,siblings);
5563 // Special case for lost-n-found
5564 if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5566 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
5571 else if(yaffs_SumCompare(l->sum, sum)||
5572 l->chunkId <= 0) //LostnFound cunk called Objxxx
5575 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
5576 if(yaffs_strcmp(name,buffer) == 0)
5589 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
5591 struct list_head *i;
5597 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: null pointer directory"TENDSTR)));
5600 if(theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5602 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: non-directory"TENDSTR)));
5606 list_for_each(i,&theDir->variant.directoryVariant.children)
5610 l = list_entry(i, yaffs_Object,siblings);
5623 // GetEquivalentObject dereferences any hard links to get to the
5626 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5628 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
5630 // We want the object id of the equivalent object, not this one
5631 obj = obj->variant.hardLinkVariant.equivalentObject;
5637 int yaffs_GetObjectName(yaffs_Object *obj,YCHAR *name,int buffSize)
5639 memset(name,0,buffSize * sizeof(YCHAR));
5641 if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5643 yaffs_strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
5645 else if(obj->chunkId <= 0)
5649 yaffs_sprintf(locName,_Y("%s%d"),YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
5650 yaffs_strncpy(name,locName,buffSize - 1);
5653 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5654 else if(obj->shortName[0])
5656 yaffs_strcpy(name,obj->shortName);
5661 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev,__LINE__);
5663 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5665 memset(buffer,0,obj->myDev->nBytesPerChunk);
5667 if(obj->chunkId >= 0)
5669 yaffs_ReadChunkWithTagsFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5671 yaffs_strncpy(name,oh->name,buffSize - 1);
5673 yaffs_ReleaseTempBuffer(obj->myDev,buffer,__LINE__);
5676 return yaffs_strlen(name);
5679 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5682 // Dereference any hard linking
5683 obj = yaffs_GetEquivalentObject(obj);
5685 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5687 return obj->variant.fileVariant.fileSize;
5689 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5691 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5695 // Only a directory should drop through to here
5696 return obj->myDev->nBytesPerChunk;
5700 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5703 struct list_head *i;
5707 count++; // the object itself
5709 list_for_each(i,&obj->hardLinks)
5711 count++; // add the hard links;
5718 int yaffs_GetObjectInode(yaffs_Object *obj)
5720 obj = yaffs_GetEquivalentObject(obj);
5722 return obj->objectId;
5725 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5727 obj = yaffs_GetEquivalentObject(obj);
5729 switch(obj->variantType)
5731 case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break;
5732 case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break;
5733 case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break;
5734 case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break;
5735 case YAFFS_OBJECT_TYPE_SPECIAL:
5736 if(S_ISFIFO(obj->yst_mode)) return DT_FIFO;
5737 if(S_ISCHR(obj->yst_mode)) return DT_CHR;
5738 if(S_ISBLK(obj->yst_mode)) return DT_BLK;
5739 if(S_ISSOCK(obj->yst_mode)) return DT_SOCK;
5740 default: return DT_REG; break;
5744 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5746 obj = yaffs_GetEquivalentObject(obj);
5747 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5749 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5753 return yaffs_CloneString(_Y(""));
5757 #ifndef CONFIG_YAFFS_WINCE
5759 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5761 unsigned int valid = attr->ia_valid;
5763 if(valid & ATTR_MODE) obj->yst_mode = attr->ia_mode;
5764 if(valid & ATTR_UID) obj->yst_uid = attr->ia_uid;
5765 if(valid & ATTR_GID) obj->yst_gid = attr->ia_gid;
5767 if(valid & ATTR_ATIME) obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
5768 if(valid & ATTR_CTIME) obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
5769 if(valid & ATTR_MTIME) obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
5771 if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
5773 yaffs_UpdateObjectHeader(obj,NULL,1,0);
5778 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5780 unsigned int valid = 0;
5782 attr->ia_mode = obj->yst_mode; valid |= ATTR_MODE;
5783 attr->ia_uid = obj->yst_uid; valid |= ATTR_UID;
5784 attr->ia_gid = obj->yst_gid; valid |= ATTR_GID;
5786 Y_TIME_CONVERT(attr->ia_atime)= obj->yst_atime; valid |= ATTR_ATIME;
5787 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime; valid |= ATTR_CTIME;
5788 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime; valid |= ATTR_MTIME;
5790 attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
5792 attr->ia_valid = valid;
5800 int yaffs_DumpObject(yaffs_Object *obj)
5802 // __u8 buffer[YAFFS_BYTES_PER_CHUNK];
5804 // yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5806 // memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
5808 // if(obj->chunkId >= 0)
5810 // yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5813 yaffs_GetObjectName(obj,name,256);
5815 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),
5816 obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial,
5817 obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5820 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
5821 obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial,
5822 obj->sum, obj->chunkId));
5823 switch(obj->variantType)
5825 case YAFFS_OBJECT_TYPE_FILE:
5826 YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
5828 case YAFFS_OBJECT_TYPE_DIRECTORY:
5829 YPRINTF((" DIRECTORY\n"));
5831 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
5832 case YAFFS_OBJECT_TYPE_SYMLINK:
5833 case YAFFS_OBJECT_TYPE_UNKNOWN:
5842 ///////////////////////// Initialisation code ///////////////////////////
5845 int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5848 // Common functions, gotta have
5849 if(!dev->eraseBlockInNAND ||
5850 !dev->initialiseNAND) return 0;
5852 #ifdef CONFIG_YAFFS_YAFFS2
5854 // Can use the "with tags" style interface for yaffs1 or yaffs2
5855 if(dev->writeChunkWithTagsToNAND &&
5856 dev->readChunkWithTagsFromNAND &&
5857 !dev->writeChunkToNAND &&
5858 !dev->readChunkFromNAND &&
5859 dev->markNANDBlockBad &&
5860 dev->queryNANDBlock) return 1;
5863 // Can use the "spare" style interface for yaffs1
5864 if(!dev->isYaffs2 &&
5865 !dev->writeChunkWithTagsToNAND &&
5866 !dev->readChunkWithTagsFromNAND &&
5867 dev->writeChunkToNAND &&
5868 dev->readChunkFromNAND &&
5869 !dev->markNANDBlockBad &&
5870 !dev->queryNANDBlock) return 1;
5876 int yaffs_GutsInitialise(yaffs_Device *dev)
5883 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
5884 // Check stuff that must be set
5888 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Need a device" TENDSTR)));
5892 dev->internalStartBlock = dev->startBlock;
5893 dev->internalEndBlock = dev->endBlock;
5894 dev->blockOffset = 0;
5895 dev->chunkOffset = 0;
5896 dev->nFreeChunks = 0;
5898 if(dev->startBlock == 0)
5900 dev->internalStartBlock = dev->startBlock + 1;
5901 dev->internalEndBlock = dev->endBlock + 1;
5902 dev->blockOffset = 1;
5903 dev->chunkOffset = dev->nChunksPerBlock;
5906 // Check geometry parameters.
5908 if( (dev->isYaffs2 && dev->nBytesPerChunk <1024) ||
5909 (!dev->isYaffs2 && dev->nBytesPerChunk !=512) ||
5910 dev->nChunksPerBlock < 2 ||
5911 dev->nReservedBlocks < 2 ||
5912 dev->internalStartBlock <= 0 ||
5913 dev->internalEndBlock <= 0 ||
5914 dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
5917 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s " TENDSTR),
5918 dev->nBytesPerChunk, dev->isYaffs2 ? "2" : ""));
5922 if(yaffs_InitialiseNAND(dev) != YAFFS_OK)
5924 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
5928 // Got the right mix of functions?
5930 if(!yaffs_CheckDevFunctions(dev))
5933 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5938 // This is really a compilation check.
5939 if(!yaffs_CheckStructures())
5941 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
5947 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
5953 // Finished with most checks. One or two more checks happen later on too.
5959 nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5963 // OK now calculate a few things for the device
5964 // Calculate chunkGroupBits.
5965 // We need to find the next power of 2 > than internalEndBlock
5967 x = dev->nChunksPerBlock * (dev->internalEndBlock+1);
5969 for(bits = extraBits = 0; x > 1; bits++)
5971 if(x & 1) extraBits++;
5975 if(extraBits > 0) bits++;
5978 // Level0 Tnodes are 16 bits, so if the bitwidth of the
5979 // chunk range we're using is greater than 16 we need
5980 // to figure out chunk shift and chunkGroupSize
5983 dev->chunkGroupBits = 0;
5987 dev->chunkGroupBits = bits - 16;
5990 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
5992 if(dev->nChunksPerBlock < dev->chunkGroupSize)
5994 // We have a problem because the soft delete won't work if
5995 // the chunk group size > chunks per block.
5996 // This can be remedied by using larger "virtual blocks".
5997 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: chunk group too large\n" TENDSTR)));
6003 // OK, we've finished verifying the device, lets continue with initialisation
6005 // More device initialisation
6006 dev->garbageCollections = 0;
6007 dev->passiveGarbageCollections = 0;
6008 dev->currentDirtyChecker = 0;
6009 dev->bufferedBlock = -1;
6010 dev->doingBufferedBlockRewrite = 0;
6011 dev->nDeletedFiles = 0;
6012 dev->nBackgroundDeletions=0;
6013 dev->nUnlinkedFiles = 0;
6016 dev->tagsEccFixed=0;
6017 dev->tagsEccUnfixed=0;
6018 dev->nErasureFailures = 0;
6019 dev->nErasedBlocks = 0;
6021 //dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
6022 // Initialise temporary buffers
6025 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6027 dev->tempBuffer[i].line = 0; // not in use
6028 dev->tempBuffer[i].buffer = YMALLOC(dev->nBytesPerChunk);
6034 yaffs_InitialiseBlocks(dev,nBlocks);
6036 yaffs_InitialiseTnodes(dev);
6038 yaffs_InitialiseObjects(dev);
6040 dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
6042 if(dev->nShortOpCaches > 0)
6046 if(dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
6048 dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
6051 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
6053 for(i=0; i < dev->nShortOpCaches; i++)
6055 dev->srCache[i].object = NULL;
6056 dev->srCache[i].lastUse = 0;
6057 dev->srCache[i].dirty = 0;
6058 dev->srCache[i].data = YMALLOC(dev->nBytesPerChunk);
6066 // Initialise the unlinked, root and lost and found directories
6067 dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = dev->deletedDir = NULL;
6069 dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
6070 dev->deletedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_DELETED, S_IFDIR);
6072 dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
6073 dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
6074 yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
6078 dev->useHeaderFileSize = 1;
6081 // Now scan the flash.
6084 yaffs_ScanBackwards(dev);
6089 dev->nPageReads = 0;
6090 dev->nPageWrites = 0;
6091 dev->nBlockErasures = 0;
6093 dev->nRetriedWrites = 0;
6095 dev->nRetiredBlocks = 0;
6097 yaffs_VerifyFreeChunks(dev);
6099 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
6104 void yaffs_Deinitialise(yaffs_Device *dev)
6110 yaffs_DeinitialiseBlocks(dev);
6111 yaffs_DeinitialiseTnodes(dev);
6112 yaffs_DeinitialiseObjects(dev);
6113 if(dev->nShortOpCaches > 0)
6116 for(i=0; i < dev->nShortOpCaches; i++)
6118 YFREE(dev->srCache[i].data);
6121 YFREE(dev->srCache);
6124 YFREE(dev->gcCleanupList);
6126 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6128 YFREE(dev->tempBuffer[i].buffer);
6138 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
6140 int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
6142 struct list_head *i;
6146 // To the free chunks add the chunks that are in the deleted unlinked files.
6147 list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
6149 l = list_entry(i, yaffs_Object,siblings);
6153 nFree += l->nDataChunks;
6158 // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);
6160 if(nFree < 0) nFree = 0;
6168 static int yaffs_CountFreeChunks(yaffs_Device *dev)
6173 yaffs_BlockInfo *blk;
6176 for(nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock; b++)
6178 blk = yaffs_GetBlockInfo(dev,b);
6180 switch(blk->blockState)
6182 case YAFFS_BLOCK_STATE_EMPTY:
6183 case YAFFS_BLOCK_STATE_ALLOCATING:
6184 case YAFFS_BLOCK_STATE_COLLECTING:
6185 case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse + blk->softDeletions); break;
6196 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
6198 // This is what we report to the outside world
6201 int nDirtyCacheChunks;
6204 nFree = dev->nFreeChunks;
6206 nFree = yaffs_CountFreeChunks(dev);
6209 // Now count the number of dirty chunks in the cache and subtract those
6213 for( nDirtyCacheChunks = 0,i = 0; i < dev->nShortOpCaches; i++)
6215 if(dev->srCache[i].dirty) nDirtyCacheChunks++;
6219 nFree -= nDirtyCacheChunks;
6221 nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
6223 if(nFree < 0) nFree = 0;
6229 static int yaffs_freeVerificationFailures;
6231 static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
6233 int counted = yaffs_CountFreeChunks(dev);
6235 int difference = dev->nFreeChunks - counted;
6239 T(YAFFS_TRACE_ALWAYS,(TSTR("Freechunks verification failure %d %d %d" TENDSTR),dev->nFreeChunks,counted,difference));
6240 yaffs_freeVerificationFailures++;
6244 /////////////////// YAFFS test code //////////////////////////////////
6246 #define yaffs_CheckStruct(structure,syze, name) \
6247 if(sizeof(structure) != syze) \
6249 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
6250 return YAFFS_FAIL; \
6254 static int yaffs_CheckStructures(void)
6256 // yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
6257 // yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
6258 // yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
6259 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
6260 yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
6262 yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
6269 void yaffs_GutsTest(yaffs_Device *dev)
6272 if(yaffs_CheckStructures() != YAFFS_OK)
6274 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
6278 yaffs_TnodeTest(dev);
6279 yaffs_ObjectTest(dev);