2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 const char *yaffs_guts_c_version =
15 "$Id: yaffs_guts.c,v 1.52 2007/10/16 00:45:05 charles Exp $";
19 #include "yaffsinterface.h"
20 #include "yaffs_guts.h"
21 #include "yaffs_tagsvalidity.h"
23 #include "yaffs_tagscompat.h"
24 #ifndef CONFIG_YAFFS_USE_OWN_SORT
25 #include "yaffs_qsort.h"
27 #include "yaffs_nand.h"
29 #include "yaffs_checkptrw.h"
31 #include "yaffs_nand.h"
32 #include "yaffs_packedtags2.h"
35 #ifdef CONFIG_YAFFS_WINCE
36 void yfsd_LockYAFFS(BOOL fsLockOnly);
37 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
40 #define YAFFS_PASSIVE_GC_CHUNKS 2
42 #include "yaffs_ecc.h"
45 /* Robustification (if it ever comes about...) */
46 static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
47 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
48 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
50 const yaffs_ExtendedTags * tags);
51 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
52 const yaffs_ExtendedTags * tags);
54 /* Other local prototypes */
55 static int yaffs_UnlinkObject( yaffs_Object *obj);
56 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
58 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
60 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
62 yaffs_ExtendedTags * tags,
64 static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
65 int chunkInNAND, int inScan);
67 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
68 yaffs_ObjectType type);
69 static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
71 static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
72 int force, int isShrink, int shadows);
73 static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
74 static int yaffs_CheckStructures(void);
75 static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
76 int chunkOffset, int *limit);
77 static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
79 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
81 static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
82 static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
85 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
88 static int yaffs_UnlinkWorker(yaffs_Object * obj);
89 static void yaffs_DestroyObject(yaffs_Object * obj);
91 static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
94 loff_t yaffs_GetFileSize(yaffs_Object * obj);
96 static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
98 static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
100 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
102 #ifdef YAFFS_PARANOID
103 static int yaffs_CheckFileSanity(yaffs_Object * in);
105 #define yaffs_CheckFileSanity(in)
108 static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
109 static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
111 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
113 static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
114 yaffs_ExtendedTags * tags);
116 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
117 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
118 yaffs_FileStructure * fStruct,
122 /* Function to calculate chunk and offset */
124 static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
127 /* Easy-peasy power of 2 case */
128 *chunk = (__u32)(addr >> dev->chunkShift);
129 *offset = (__u32)(addr & dev->chunkMask);
131 else if(dev->crumbsPerChunk)
133 /* Case where we're using "crumbs" */
134 *offset = (__u32)(addr & dev->crumbMask);
135 addr >>= dev->crumbShift;
136 *chunk = ((__u32)addr)/dev->crumbsPerChunk;
137 *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
143 /* Function to return the number of shifts for a power of 2 greater than or equal
144 * to the given number
145 * Note we don't try to cater for all possible numbers and this does not have to
146 * be hellishly efficient.
149 static __u32 ShiftsGE(__u32 x)
154 nShifts = extraBits = 0;
157 if(x & 1) extraBits++;
168 /* Function to return the number of shifts to get a 1 in bit 0
171 static __u32 ShiftDiv(__u32 x)
190 * Temporary buffer manipulations.
193 static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
196 __u8 *buf = (__u8 *)1;
198 memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
200 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
201 dev->tempBuffer[i].line = 0; /* not in use */
202 dev->tempBuffer[i].buffer = buf =
203 YMALLOC_DMA(dev->nDataBytesPerChunk);
206 return buf ? YAFFS_OK : YAFFS_FAIL;
210 static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
213 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
214 if (dev->tempBuffer[i].line == 0) {
215 dev->tempBuffer[i].line = lineNo;
216 if ((i + 1) > dev->maxTemp) {
217 dev->maxTemp = i + 1;
218 for (j = 0; j <= i; j++)
219 dev->tempBuffer[j].maxLine =
220 dev->tempBuffer[j].line;
223 return dev->tempBuffer[i].buffer;
227 T(YAFFS_TRACE_BUFFERS,
228 (TSTR("Out of temp buffers at line %d, other held by lines:"),
230 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
231 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
233 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
236 * If we got here then we have to allocate an unmanaged one
240 dev->unmanagedTempAllocations++;
241 return YMALLOC(dev->nDataBytesPerChunk);
245 static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
249 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
250 if (dev->tempBuffer[i].buffer == buffer) {
251 dev->tempBuffer[i].line = 0;
257 /* assume it is an unmanaged one. */
258 T(YAFFS_TRACE_BUFFERS,
259 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
262 dev->unmanagedTempDeallocations++;
268 * Determine if we have a managed buffer.
270 int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
273 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
274 if (dev->tempBuffer[i].buffer == buffer)
279 for (i = 0; i < dev->nShortOpCaches; i++) {
280 if( dev->srCache[i].data == buffer )
285 if (buffer == dev->checkpointBuffer)
288 T(YAFFS_TRACE_ALWAYS,
289 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
296 * Chunk bitmap manipulations
299 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
301 if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
303 (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
307 return dev->chunkBits +
308 (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
311 static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
313 if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
314 chunk < 0 || chunk >= dev->nChunksPerBlock) {
316 (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
321 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
323 __u8 *blkBits = yaffs_BlockBits(dev, blk);
325 memset(blkBits, 0, dev->chunkBitmapStride);
328 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
330 __u8 *blkBits = yaffs_BlockBits(dev, blk);
332 yaffs_VerifyChunkBitId(dev,blk,chunk);
334 blkBits[chunk / 8] &= ~(1 << (chunk & 7));
337 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
339 __u8 *blkBits = yaffs_BlockBits(dev, blk);
341 yaffs_VerifyChunkBitId(dev,blk,chunk);
343 blkBits[chunk / 8] |= (1 << (chunk & 7));
346 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
348 __u8 *blkBits = yaffs_BlockBits(dev, blk);
349 yaffs_VerifyChunkBitId(dev,blk,chunk);
351 return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
354 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
356 __u8 *blkBits = yaffs_BlockBits(dev, blk);
358 for (i = 0; i < dev->chunkBitmapStride; i++) {
366 static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
368 __u8 *blkBits = yaffs_BlockBits(dev, blk);
371 for (i = 0; i < dev->chunkBitmapStride; i++) {
388 static int yaffs_SkipVerification(yaffs_Device *dev)
390 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
393 static int yaffs_SkipFullVerification(yaffs_Device *dev)
395 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
398 static int yaffs_SkipNANDVerification(yaffs_Device *dev)
400 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
403 static const char * blockStateName[] = {
416 static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
421 if(yaffs_SkipVerification(dev))
424 /* Report illegal runtime states */
425 if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
426 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
428 switch(bi->blockState){
429 case YAFFS_BLOCK_STATE_UNKNOWN:
430 case YAFFS_BLOCK_STATE_SCANNING:
431 case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
432 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
433 n,blockStateName[bi->blockState]));
436 /* Check pages in use and soft deletions are legal */
438 actuallyUsed = bi->pagesInUse - bi->softDeletions;
440 if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
441 bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
442 actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
443 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
444 n,bi->pagesInUse,bi->softDeletions));
447 /* Check chunk bitmap legal */
448 inUse = yaffs_CountChunkBits(dev,n);
449 if(inUse != bi->pagesInUse)
450 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
451 n,bi->pagesInUse,inUse));
453 /* Check that the sequence number is valid.
454 * Ten million is legal, but is very unlikely
457 (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
458 (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
459 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
460 n,bi->sequenceNumber));
464 static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
466 yaffs_VerifyBlock(dev,bi,n);
468 /* After collection the block should be in the erased state */
469 /* TODO: This will need to change if we do partial gc */
471 if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
472 T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
477 static void yaffs_VerifyBlocks(yaffs_Device *dev)
480 int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
481 int nIllegalBlockStates = 0;
484 if(yaffs_SkipVerification(dev))
487 memset(nBlocksPerState,0,sizeof(nBlocksPerState));
490 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
491 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
492 yaffs_VerifyBlock(dev,bi,i);
494 if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
495 nBlocksPerState[bi->blockState]++;
497 nIllegalBlockStates++;
501 T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
502 T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
504 T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
505 if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
506 T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
508 for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
509 T(YAFFS_TRACE_VERIFY,
510 (TSTR("%s %d blocks"TENDSTR),
511 blockStateName[i],nBlocksPerState[i]));
513 if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
514 T(YAFFS_TRACE_VERIFY,
515 (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
516 dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
518 if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
519 T(YAFFS_TRACE_VERIFY,
520 (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
521 dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
523 if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
524 T(YAFFS_TRACE_VERIFY,
525 (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
526 nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
528 T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
533 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
534 * case those tests will not be performed.
536 static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
538 if(yaffs_SkipVerification(obj->myDev))
541 if(!(tags && obj && oh)){
542 T(YAFFS_TRACE_VERIFY,
543 (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
544 (__u32)tags,(__u32)obj,(__u32)oh));
548 if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
549 oh->type > YAFFS_OBJECT_TYPE_MAX)
550 T(YAFFS_TRACE_VERIFY,
551 (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
552 tags->objectId, oh->type));
554 if(tags->objectId != obj->objectId)
555 T(YAFFS_TRACE_VERIFY,
556 (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
557 tags->objectId, obj->objectId));
561 * Check that the object's parent ids match if parentCheck requested.
563 * Tests do not apply to the root object.
566 if(parentCheck && tags->objectId > 1 && !obj->parent)
567 T(YAFFS_TRACE_VERIFY,
568 (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
569 tags->objectId, oh->parentObjectId));
572 if(parentCheck && obj->parent &&
573 oh->parentObjectId != obj->parent->objectId &&
574 (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
575 obj->parent->objectId != YAFFS_OBJECTID_DELETED))
576 T(YAFFS_TRACE_VERIFY,
577 (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
578 tags->objectId, oh->parentObjectId, obj->parent->objectId));
581 if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
582 T(YAFFS_TRACE_VERIFY,
583 (TSTR("Obj %d header name is NULL"TENDSTR),
586 if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
587 T(YAFFS_TRACE_VERIFY,
588 (TSTR("Obj %d header name is 0xFF"TENDSTR),
594 static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
595 __u32 level, int chunkOffset)
598 yaffs_Device *dev = obj->myDev;
600 int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
605 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
606 if (tn->internal[i]) {
607 ok = yaffs_VerifyTnodeWorker(obj,
610 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
613 } else if (level == 0) {
615 yaffs_ExtendedTags tags;
616 __u32 objectId = obj->objectId;
618 chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
620 for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
621 __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
624 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
625 yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
626 if(tags.objectId != objectId || tags.chunkId != chunkOffset){
627 T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
628 objectId, chunkOffset, theChunk,
629 tags.objectId, tags.chunkId));
642 static void yaffs_VerifyFile(yaffs_Object *obj)
644 int requiredTallness;
651 yaffs_ExtendedTags tags;
655 if(obj && yaffs_SkipVerification(obj->myDev))
659 objectId = obj->objectId;
661 /* Check file size is consistent with tnode depth */
662 lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
663 x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
664 requiredTallness = 0;
666 x >>= YAFFS_TNODES_INTERNAL_BITS;
670 actualTallness = obj->variant.fileVariant.topLevel;
672 if(requiredTallness > actualTallness )
673 T(YAFFS_TRACE_VERIFY,
674 (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
675 obj->objectId,actualTallness, requiredTallness));
678 /* Check that the chunks in the tnode tree are all correct.
679 * We do this by scanning through the tnode tree and
680 * checking the tags for every chunk match.
683 if(yaffs_SkipNANDVerification(dev))
686 for(i = 1; i <= lastChunk; i++){
687 tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
690 __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
692 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
693 yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
694 if(tags.objectId != objectId || tags.chunkId != i){
695 T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
696 objectId, i, theChunk,
697 tags.objectId, tags.chunkId));
706 static void yaffs_VerifyDirectory(yaffs_Object *obj)
708 if(obj && yaffs_SkipVerification(obj->myDev))
713 static void yaffs_VerifyHardLink(yaffs_Object *obj)
715 if(obj && yaffs_SkipVerification(obj->myDev))
718 /* Verify sane equivalent object */
721 static void yaffs_VerifySymlink(yaffs_Object *obj)
723 if(obj && yaffs_SkipVerification(obj->myDev))
726 /* Verify symlink string */
729 static void yaffs_VerifySpecial(yaffs_Object *obj)
731 if(obj && yaffs_SkipVerification(obj->myDev))
735 static void yaffs_VerifyObject(yaffs_Object *obj)
750 if(yaffs_SkipVerification(dev))
753 /* Check sane object header chunk */
755 chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
756 chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
758 chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
759 chunkIsLive = chunkIdOk &&
760 yaffs_CheckChunkBit(dev,
761 obj->chunkId / dev->nChunksPerBlock,
762 obj->chunkId % dev->nChunksPerBlock);
764 (!chunkIdOk || !chunkIsLive)) {
765 T(YAFFS_TRACE_VERIFY,
766 (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
767 obj->objectId,obj->chunkId,
768 chunkIdOk ? "" : ",out of range",
769 chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
772 if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
773 yaffs_ExtendedTags tags;
774 yaffs_ObjectHeader *oh;
775 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
777 oh = (yaffs_ObjectHeader *)buffer;
779 yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
781 yaffs_VerifyObjectHeader(obj,oh,&tags,1);
783 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
786 /* Verify it has a parent */
787 if(obj && !obj->fake &&
788 (!obj->parent || obj->parent->myDev != dev)){
789 T(YAFFS_TRACE_VERIFY,
790 (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
791 obj->objectId,obj->parent));
794 /* Verify parent is a directory */
795 if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
796 T(YAFFS_TRACE_VERIFY,
797 (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
798 obj->objectId,obj->parent->variantType));
801 switch(obj->variantType){
802 case YAFFS_OBJECT_TYPE_FILE:
803 yaffs_VerifyFile(obj);
805 case YAFFS_OBJECT_TYPE_SYMLINK:
806 yaffs_VerifySymlink(obj);
808 case YAFFS_OBJECT_TYPE_DIRECTORY:
809 yaffs_VerifyDirectory(obj);
811 case YAFFS_OBJECT_TYPE_HARDLINK:
812 yaffs_VerifyHardLink(obj);
814 case YAFFS_OBJECT_TYPE_SPECIAL:
815 yaffs_VerifySpecial(obj);
817 case YAFFS_OBJECT_TYPE_UNKNOWN:
819 T(YAFFS_TRACE_VERIFY,
820 (TSTR("Obj %d has illegaltype %d"TENDSTR),
821 obj->objectId,obj->variantType));
828 static void yaffs_VerifyObjects(yaffs_Device *dev)
832 struct list_head *lh;
834 if(yaffs_SkipVerification(dev))
837 /* Iterate through the objects in each hash entry */
839 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
840 list_for_each(lh, &dev->objectBucket[i].list) {
842 obj = list_entry(lh, yaffs_Object, hashLink);
843 yaffs_VerifyObject(obj);
852 * Simple hash function. Needs to have a reasonable spread
855 static Y_INLINE int yaffs_HashFunction(int n)
858 return (n % YAFFS_NOBJECT_BUCKETS);
862 * Access functions to useful fake objects
865 yaffs_Object *yaffs_Root(yaffs_Device * dev)
870 yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
872 return dev->lostNFoundDir;
877 * Erased NAND checking functions
880 int yaffs_CheckFF(__u8 * buffer, int nBytes)
882 /* Horrible, slow implementation */
891 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
895 int retval = YAFFS_OK;
896 __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
897 yaffs_ExtendedTags tags;
900 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
902 if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
906 if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
907 T(YAFFS_TRACE_NANDACCESS,
908 (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
912 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
918 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
920 yaffs_ExtendedTags * tags,
927 yaffs_InvalidateCheckpoint(dev);
930 yaffs_BlockInfo *bi = 0;
933 chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
939 /* First check this chunk is erased, if it needs
940 * checking. The checking policy (unless forced
941 * always on) is as follows:
943 * Check the first page we try to write in a block.
944 * If the check passes then we don't need to check any
945 * more. If the check fails, we check again...
946 * If the block has been erased, we don't need to check.
948 * However, if the block has been prioritised for gc,
949 * then we think there might be something odd about
950 * this block and stop using it.
952 * Rationale: We should only ever see chunks that have
953 * not been erased if there was a partially written
954 * chunk due to power loss. This checking policy should
955 * catch that case with very few checks and thus save a
956 * lot of checks that are most likely not needed.
958 if (bi->gcPrioritise) {
959 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
960 /* try another chunk */
964 /* let's give it a try */
967 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
968 bi->skipErasedCheck = 0;
970 if (!bi->skipErasedCheck) {
971 erasedOk = yaffs_CheckChunkErased(dev, chunk);
972 if (erasedOk != YAFFS_OK) {
974 (TSTR ("**>> yaffs chunk %d was not erased"
977 /* try another chunk */
980 bi->skipErasedCheck = 1;
983 writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
985 if (writeOk != YAFFS_OK) {
986 yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
987 /* try another chunk */
991 /* Copy the data into the robustification buffer */
992 yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
994 } while (writeOk != YAFFS_OK &&
995 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
1001 T(YAFFS_TRACE_ERROR,
1002 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
1005 dev->nRetriedWrites += (attempts - 1);
1012 * Block retiring for handling a broken block.
1015 static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
1017 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1019 yaffs_InvalidateCheckpoint(dev);
1021 yaffs_MarkBlockBad(dev, blockInNAND);
1023 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
1024 bi->gcPrioritise = 0;
1025 bi->needsRetiring = 0;
1027 dev->nRetiredBlocks++;
1031 * Functions for robustisizing TODO
1035 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
1037 const yaffs_ExtendedTags * tags)
1041 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
1042 const yaffs_ExtendedTags * tags)
1046 void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
1048 if(!bi->gcPrioritise){
1049 bi->gcPrioritise = 1;
1050 dev->hasPendingPrioritisedGCs = 1;
1051 bi->chunkErrorStrikes ++;
1053 if(bi->chunkErrorStrikes > 3){
1054 bi->needsRetiring = 1; /* Too many stikes, so retire this */
1055 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
1062 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
1065 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
1066 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1068 yaffs_HandleChunkError(dev,bi);
1072 /* Was an actual write failure, so mark the block for retirement */
1073 bi->needsRetiring = 1;
1074 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1075 (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
1080 /* Delete the chunk */
1081 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
1085 /*---------------- Name handling functions ------------*/
1087 static __u16 yaffs_CalcNameSum(const YCHAR * name)
1092 YUCHAR *bname = (YUCHAR *) name;
1094 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
1096 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1097 sum += yaffs_toupper(*bname) * i;
1099 sum += (*bname) * i;
1108 static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
1110 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
1111 if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
1112 yaffs_strcpy(obj->shortName, name);
1114 obj->shortName[0] = _Y('\0');
1117 obj->sum = yaffs_CalcNameSum(name);
1120 /*-------------------- TNODES -------------------
1122 * List of spare tnodes
1123 * The list is hooked together using the first pointer
1127 /* yaffs_CreateTnodes creates a bunch more tnodes and
1128 * adds them to the tnode free list.
1129 * Don't use this function directly
1132 static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
1136 yaffs_Tnode *newTnodes;
1140 yaffs_TnodeList *tnl;
1145 /* Calculate the tnode size in bytes for variable width tnode support.
1146 * Must be a multiple of 32-bits */
1147 tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
1149 /* make these things */
1151 newTnodes = YMALLOC(nTnodes * tnodeSize);
1152 mem = (__u8 *)newTnodes;
1155 T(YAFFS_TRACE_ERROR,
1156 (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
1160 /* Hook them into the free list */
1162 for (i = 0; i < nTnodes - 1; i++) {
1163 newTnodes[i].internal[0] = &newTnodes[i + 1];
1164 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1165 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1169 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
1170 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1171 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1173 dev->freeTnodes = newTnodes;
1175 /* New hookup for wide tnodes */
1176 for(i = 0; i < nTnodes -1; i++) {
1177 curr = (yaffs_Tnode *) &mem[i * tnodeSize];
1178 next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
1179 curr->internal[0] = next;
1182 curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
1183 curr->internal[0] = dev->freeTnodes;
1184 dev->freeTnodes = (yaffs_Tnode *)mem;
1189 dev->nFreeTnodes += nTnodes;
1190 dev->nTnodesCreated += nTnodes;
1192 /* Now add this bunch of tnodes to a list for freeing up.
1193 * NB If we can't add this to the management list it isn't fatal
1194 * but it just means we can't free this bunch of tnodes later.
1197 tnl = YMALLOC(sizeof(yaffs_TnodeList));
1199 T(YAFFS_TRACE_ERROR,
1201 ("yaffs: Could not add tnodes to management list" TENDSTR)));
1205 tnl->tnodes = newTnodes;
1206 tnl->next = dev->allocatedTnodeList;
1207 dev->allocatedTnodeList = tnl;
1210 T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
1215 /* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
1217 static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
1219 yaffs_Tnode *tn = NULL;
1221 /* If there are none left make more */
1222 if (!dev->freeTnodes) {
1223 yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
1226 if (dev->freeTnodes) {
1227 tn = dev->freeTnodes;
1228 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1229 if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
1230 /* Hoosterman, this thing looks like it isn't in the list */
1231 T(YAFFS_TRACE_ALWAYS,
1232 (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
1235 dev->freeTnodes = dev->freeTnodes->internal[0];
1242 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
1244 yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
1247 memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1252 /* FreeTnode frees up a tnode and puts it back on the free list */
1253 static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
1256 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1257 if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
1258 /* Hoosterman, this thing looks like it is already in the list */
1259 T(YAFFS_TRACE_ALWAYS,
1260 (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
1262 tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1264 tn->internal[0] = dev->freeTnodes;
1265 dev->freeTnodes = tn;
1270 static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
1272 /* Free the list of allocated tnodes */
1273 yaffs_TnodeList *tmp;
1275 while (dev->allocatedTnodeList) {
1276 tmp = dev->allocatedTnodeList->next;
1278 YFREE(dev->allocatedTnodeList->tnodes);
1279 YFREE(dev->allocatedTnodeList);
1280 dev->allocatedTnodeList = tmp;
1284 dev->freeTnodes = NULL;
1285 dev->nFreeTnodes = 0;
1288 static void yaffs_InitialiseTnodes(yaffs_Device * dev)
1290 dev->allocatedTnodeList = NULL;
1291 dev->freeTnodes = NULL;
1292 dev->nFreeTnodes = 0;
1293 dev->nTnodesCreated = 0;
1298 void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
1300 __u32 *map = (__u32 *)tn;
1306 pos &= YAFFS_TNODES_LEVEL0_MASK;
1307 val >>= dev->chunkGroupBits;
1309 bitInMap = pos * dev->tnodeWidth;
1310 wordInMap = bitInMap /32;
1311 bitInWord = bitInMap & (32 -1);
1313 mask = dev->tnodeMask << bitInWord;
1315 map[wordInMap] &= ~mask;
1316 map[wordInMap] |= (mask & (val << bitInWord));
1318 if(dev->tnodeWidth > (32-bitInWord)) {
1319 bitInWord = (32 - bitInWord);
1321 mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
1322 map[wordInMap] &= ~mask;
1323 map[wordInMap] |= (mask & (val >> bitInWord));
1327 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
1329 __u32 *map = (__u32 *)tn;
1335 pos &= YAFFS_TNODES_LEVEL0_MASK;
1337 bitInMap = pos * dev->tnodeWidth;
1338 wordInMap = bitInMap /32;
1339 bitInWord = bitInMap & (32 -1);
1341 val = map[wordInMap] >> bitInWord;
1343 if(dev->tnodeWidth > (32-bitInWord)) {
1344 bitInWord = (32 - bitInWord);
1346 val |= (map[wordInMap] << bitInWord);
1349 val &= dev->tnodeMask;
1350 val <<= dev->chunkGroupBits;
1355 /* ------------------- End of individual tnode manipulation -----------------*/
1357 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
1358 * The look up tree is represented by the top tnode and the number of topLevel
1359 * in the tree. 0 means only the level 0 tnode is in the tree.
1362 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
1363 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
1364 yaffs_FileStructure * fStruct,
1368 yaffs_Tnode *tn = fStruct->top;
1370 int requiredTallness;
1371 int level = fStruct->topLevel;
1373 /* Check sane level and chunk Id */
1374 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
1378 if (chunkId > YAFFS_MAX_CHUNK_ID) {
1382 /* First check we're tall enough (ie enough topLevel) */
1384 i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1385 requiredTallness = 0;
1387 i >>= YAFFS_TNODES_INTERNAL_BITS;
1391 if (requiredTallness > fStruct->topLevel) {
1392 /* Not tall enough, so we can't find it, return NULL. */
1396 /* Traverse down to level 0 */
1397 while (level > 0 && tn) {
1399 internal[(chunkId >>
1400 ( YAFFS_TNODES_LEVEL0_BITS +
1402 YAFFS_TNODES_INTERNAL_BITS)
1404 YAFFS_TNODES_INTERNAL_MASK];
1412 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
1413 * This happens in two steps:
1414 * 1. If the tree isn't tall enough, then make it taller.
1415 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
1417 * Used when modifying the tree.
1419 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
1420 * be plugged into the ttree.
1423 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
1424 yaffs_FileStructure * fStruct,
1426 yaffs_Tnode *passedTn)
1429 int requiredTallness;
1437 /* Check sane level and page Id */
1438 if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
1442 if (chunkId > YAFFS_MAX_CHUNK_ID) {
1446 /* First check we're tall enough (ie enough topLevel) */
1448 x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1449 requiredTallness = 0;
1451 x >>= YAFFS_TNODES_INTERNAL_BITS;
1456 if (requiredTallness > fStruct->topLevel) {
1457 /* Not tall enough,gotta make the tree taller */
1458 for (i = fStruct->topLevel; i < requiredTallness; i++) {
1460 tn = yaffs_GetTnode(dev);
1463 tn->internal[0] = fStruct->top;
1466 T(YAFFS_TRACE_ERROR,
1467 (TSTR("yaffs: no more tnodes" TENDSTR)));
1471 fStruct->topLevel = requiredTallness;
1474 /* Traverse down to level 0, adding anything we need */
1476 l = fStruct->topLevel;
1480 while (l > 0 && tn) {
1482 ( YAFFS_TNODES_LEVEL0_BITS +
1483 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
1484 YAFFS_TNODES_INTERNAL_MASK;
1487 if((l>1) && !tn->internal[x]){
1488 /* Add missing non-level-zero tnode */
1489 tn->internal[x] = yaffs_GetTnode(dev);
1492 /* Looking from level 1 at level 0 */
1494 /* If we already have one, then release it.*/
1496 yaffs_FreeTnode(dev,tn->internal[x]);
1497 tn->internal[x] = passedTn;
1499 } else if(!tn->internal[x]) {
1500 /* Don't have one, none passed in */
1501 tn->internal[x] = yaffs_GetTnode(dev);
1505 tn = tn->internal[x];
1509 /* top is level 0 */
1511 memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1512 yaffs_FreeTnode(dev,passedTn);
1519 static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
1520 yaffs_ExtendedTags * tags, int objectId,
1525 for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
1526 if (yaffs_CheckChunkBit
1527 (dev, theChunk / dev->nChunksPerBlock,
1528 theChunk % dev->nChunksPerBlock)) {
1529 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
1531 if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
1543 /* DeleteWorker scans backwards through the tnode tree and deletes all the
1544 * chunks and tnodes in the file
1545 * Returns 1 if the tree was deleted.
1546 * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1549 static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
1550 int chunkOffset, int *limit)
1555 yaffs_ExtendedTags tags;
1557 yaffs_Device *dev = in->myDev;
1564 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1566 if (tn->internal[i]) {
1567 if (limit && (*limit) < 0) {
1571 yaffs_DeleteWorker(in,
1579 YAFFS_TNODES_INTERNAL_BITS)
1584 yaffs_FreeTnode(dev,
1587 tn->internal[i] = NULL;
1592 return (allDone) ? 1 : 0;
1593 } else if (level == 0) {
1596 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
1598 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
1603 YAFFS_TNODES_LEVEL0_BITS) + i;
1606 yaffs_FindChunkInGroup(dev,
1612 if (foundChunk > 0) {
1613 yaffs_DeleteChunk(dev,
1618 *limit = *limit - 1;
1626 yaffs_PutLevel0Tnode(dev,tn,i,0);
1630 return (i < 0) ? 1 : 0;
1640 static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
1643 yaffs_BlockInfo *theBlock;
1645 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1647 theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
1649 theBlock->softDeletions++;
1654 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1655 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1657 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1660 static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
1661 __u32 level, int chunkOffset)
1666 yaffs_Device *dev = in->myDev;
1671 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1673 if (tn->internal[i]) {
1675 yaffs_SoftDeleteWorker(in,
1681 YAFFS_TNODES_INTERNAL_BITS)
1684 yaffs_FreeTnode(dev,
1687 tn->internal[i] = NULL;
1689 /* Hoosterman... how could this happen? */
1693 return (allDone) ? 1 : 0;
1694 } else if (level == 0) {
1696 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1697 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
1699 /* Note this does not find the real chunk, only the chunk group.
1700 * We make an assumption that a chunk group is not larger than
1703 yaffs_SoftDeleteChunk(dev, theChunk);
1704 yaffs_PutLevel0Tnode(dev,tn,i,0);
1718 static void yaffs_SoftDeleteFile(yaffs_Object * obj)
1721 obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
1722 if (obj->nDataChunks <= 0) {
1723 /* Empty file with no duplicate object headers, just delete it immediately */
1724 yaffs_FreeTnode(obj->myDev,
1725 obj->variant.fileVariant.top);
1726 obj->variant.fileVariant.top = NULL;
1727 T(YAFFS_TRACE_TRACING,
1728 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1730 yaffs_DoGenericObjectDeletion(obj);
1732 yaffs_SoftDeleteWorker(obj,
1733 obj->variant.fileVariant.top,
1734 obj->variant.fileVariant.
1736 obj->softDeleted = 1;
1741 /* Pruning removes any part of the file structure tree that is beyond the
1742 * bounds of the file (ie that does not point to chunks).
1744 * A file should only get pruned when its size is reduced.
1746 * Before pruning, the chunks must be pulled from the tree and the
1747 * level 0 tnode entries must be zeroed out.
1748 * Could also use this for file deletion, but that's probably better handled
1749 * by a special case.
1752 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
1753 __u32 level, int del0)
1761 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1762 if (tn->internal[i] && level > 0) {
1764 yaffs_PruneWorker(dev, tn->internal[i],
1766 (i == 0) ? del0 : 1);
1769 if (tn->internal[i]) {
1774 if (hasData == 0 && del0) {
1775 /* Free and return NULL */
1777 yaffs_FreeTnode(dev, tn);
1787 static int yaffs_PruneFileStructure(yaffs_Device * dev,
1788 yaffs_FileStructure * fStruct)
1795 if (fStruct->topLevel > 0) {
1797 yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
1799 /* Now we have a tree with all the non-zero branches NULL but the height
1800 * is the same as it was.
1801 * Let's see if we can trim internal tnodes to shorten the tree.
1802 * We can do this if only the 0th element in the tnode is in use
1803 * (ie all the non-zero are NULL)
1806 while (fStruct->topLevel && !done) {
1810 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1811 if (tn->internal[i]) {
1817 fStruct->top = tn->internal[0];
1818 fStruct->topLevel--;
1819 yaffs_FreeTnode(dev, tn);
1829 /*-------------------- End of File Structure functions.-------------------*/
1831 /* yaffs_CreateFreeObjects creates a bunch more objects and
1832 * adds them to the object free list.
1834 static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
1837 yaffs_Object *newObjects;
1838 yaffs_ObjectList *list;
1843 /* make these things */
1844 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1845 list = YMALLOC(sizeof(yaffs_ObjectList));
1847 if (!newObjects || !list) {
1852 T(YAFFS_TRACE_ALLOCATE,
1853 (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1857 /* Hook them into the free list */
1858 for (i = 0; i < nObjects - 1; i++) {
1859 newObjects[i].siblings.next =
1860 (struct list_head *)(&newObjects[i + 1]);
1863 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1864 dev->freeObjects = newObjects;
1865 dev->nFreeObjects += nObjects;
1866 dev->nObjectsCreated += nObjects;
1868 /* Now add this bunch of Objects to a list for freeing up. */
1870 list->objects = newObjects;
1871 list->next = dev->allocatedObjectList;
1872 dev->allocatedObjectList = list;
1878 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1879 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
1881 yaffs_Object *tn = NULL;
1883 /* If there are none left make more */
1884 if (!dev->freeObjects) {
1885 yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
1888 if (dev->freeObjects) {
1889 tn = dev->freeObjects;
1891 (yaffs_Object *) (dev->freeObjects->siblings.next);
1892 dev->nFreeObjects--;
1894 /* Now sweeten it up... */
1896 memset(tn, 0, sizeof(yaffs_Object));
1899 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1900 INIT_LIST_HEAD(&(tn->hardLinks));
1901 INIT_LIST_HEAD(&(tn->hashLink));
1902 INIT_LIST_HEAD(&tn->siblings);
1904 /* Add it to the lost and found directory.
1905 * NB Can't put root or lostNFound in lostNFound so
1906 * check if lostNFound exists first
1908 if (dev->lostNFoundDir) {
1909 yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
1916 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
1921 yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1923 obj->fake = 1; /* it is fake so it has no NAND presence... */
1924 obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
1925 obj->unlinkAllowed = 0; /* ... or unlink it */
1928 obj->yst_mode = mode;
1930 obj->chunkId = 0; /* Not a valid chunk. */
1937 static void yaffs_UnhashObject(yaffs_Object * tn)
1940 yaffs_Device *dev = tn->myDev;
1942 /* If it is still linked into the bucket list, free from the list */
1943 if (!list_empty(&tn->hashLink)) {
1944 list_del_init(&tn->hashLink);
1945 bucket = yaffs_HashFunction(tn->objectId);
1946 dev->objectBucket[bucket].count--;
1951 /* FreeObject frees up a Object and puts it back on the free list */
1952 static void yaffs_FreeObject(yaffs_Object * tn)
1955 yaffs_Device *dev = tn->myDev;
1959 /* We're still hooked up to a cached inode.
1960 * Don't delete now, but mark for later deletion
1962 tn->deferedFree = 1;
1967 yaffs_UnhashObject(tn);
1969 /* Link into the free list. */
1970 tn->siblings.next = (struct list_head *)(dev->freeObjects);
1971 dev->freeObjects = tn;
1972 dev->nFreeObjects++;
1977 void yaffs_HandleDeferedFree(yaffs_Object * obj)
1979 if (obj->deferedFree) {
1980 yaffs_FreeObject(obj);
1986 static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
1988 /* Free the list of allocated Objects */
1990 yaffs_ObjectList *tmp;
1992 while (dev->allocatedObjectList) {
1993 tmp = dev->allocatedObjectList->next;
1994 YFREE(dev->allocatedObjectList->objects);
1995 YFREE(dev->allocatedObjectList);
1997 dev->allocatedObjectList = tmp;
2000 dev->freeObjects = NULL;
2001 dev->nFreeObjects = 0;
2004 static void yaffs_InitialiseObjects(yaffs_Device * dev)
2008 dev->allocatedObjectList = NULL;
2009 dev->freeObjects = NULL;
2010 dev->nFreeObjects = 0;
2012 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2013 INIT_LIST_HEAD(&dev->objectBucket[i].list);
2014 dev->objectBucket[i].count = 0;
2019 static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
2024 int lowest = 999999;
2026 /* First let's see if we can find one that's empty. */
2028 for (i = 0; i < 10 && lowest > 0; i++) {
2030 x %= YAFFS_NOBJECT_BUCKETS;
2031 if (dev->objectBucket[x].count < lowest) {
2032 lowest = dev->objectBucket[x].count;
2038 /* If we didn't find an empty list, then try
2039 * looking a bit further for a short one
2042 for (i = 0; i < 10 && lowest > 3; i++) {
2044 x %= YAFFS_NOBJECT_BUCKETS;
2045 if (dev->objectBucket[x].count < lowest) {
2046 lowest = dev->objectBucket[x].count;
2055 static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
2057 int bucket = yaffs_FindNiceObjectBucket(dev);
2059 /* Now find an object value that has not already been taken
2060 * by scanning the list.
2064 struct list_head *i;
2066 __u32 n = (__u32) bucket;
2068 /* yaffs_CheckObjectHashSanity(); */
2072 n += YAFFS_NOBJECT_BUCKETS;
2073 if (1 || dev->objectBucket[bucket].count > 0) {
2074 list_for_each(i, &dev->objectBucket[bucket].list) {
2075 /* If there is already one in the list */
2077 && list_entry(i, yaffs_Object,
2078 hashLink)->objectId == n) {
2089 static void yaffs_HashObject(yaffs_Object * in)
2091 int bucket = yaffs_HashFunction(in->objectId);
2092 yaffs_Device *dev = in->myDev;
2094 list_add(&in->hashLink, &dev->objectBucket[bucket].list);
2095 dev->objectBucket[bucket].count++;
2099 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
2101 int bucket = yaffs_HashFunction(number);
2102 struct list_head *i;
2105 list_for_each(i, &dev->objectBucket[bucket].list) {
2106 /* Look if it is in the list */
2108 in = list_entry(i, yaffs_Object, hashLink);
2109 if (in->objectId == number) {
2111 /* Don't tell the VFS about this one if it is defered free */
2112 if (in->deferedFree)
2124 yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
2125 yaffs_ObjectType type)
2128 yaffs_Object *theObject;
2132 number = yaffs_CreateNewObjectNumber(dev);
2135 theObject = yaffs_AllocateEmptyObject(dev);
2139 if(type == YAFFS_OBJECT_TYPE_FILE){
2140 tn = yaffs_GetTnode(dev);
2142 yaffs_FreeObject(theObject);
2150 theObject->fake = 0;
2151 theObject->renameAllowed = 1;
2152 theObject->unlinkAllowed = 1;
2153 theObject->objectId = number;
2154 yaffs_HashObject(theObject);
2155 theObject->variantType = type;
2156 #ifdef CONFIG_YAFFS_WINCE
2157 yfsd_WinFileTimeNow(theObject->win_atime);
2158 theObject->win_ctime[0] = theObject->win_mtime[0] =
2159 theObject->win_atime[0];
2160 theObject->win_ctime[1] = theObject->win_mtime[1] =
2161 theObject->win_atime[1];
2165 theObject->yst_atime = theObject->yst_mtime =
2166 theObject->yst_ctime = Y_CURRENT_TIME;
2169 case YAFFS_OBJECT_TYPE_FILE:
2170 theObject->variant.fileVariant.fileSize = 0;
2171 theObject->variant.fileVariant.scannedFileSize = 0;
2172 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
2173 theObject->variant.fileVariant.topLevel = 0;
2174 theObject->variant.fileVariant.top = tn;
2176 case YAFFS_OBJECT_TYPE_DIRECTORY:
2177 INIT_LIST_HEAD(&theObject->variant.directoryVariant.
2180 case YAFFS_OBJECT_TYPE_SYMLINK:
2181 case YAFFS_OBJECT_TYPE_HARDLINK:
2182 case YAFFS_OBJECT_TYPE_SPECIAL:
2183 /* No action required */
2185 case YAFFS_OBJECT_TYPE_UNKNOWN:
2186 /* todo this should not happen */
2194 static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
2196 yaffs_ObjectType type)
2198 yaffs_Object *theObject = NULL;
2201 theObject = yaffs_FindObjectByNumber(dev, number);
2205 theObject = yaffs_CreateNewObject(dev, number, type);
2213 static YCHAR *yaffs_CloneString(const YCHAR * str)
2215 YCHAR *newStr = NULL;
2218 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
2220 yaffs_strcpy(newStr, str);
2228 * Mknod (create) a new object.
2229 * equivalentObject only has meaning for a hard link;
2230 * aliasString only has meaning for a sumlink.
2231 * rdev only has meaning for devices (a subset of special objects)
2234 static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
2235 yaffs_Object * parent,
2240 yaffs_Object * equivalentObject,
2241 const YCHAR * aliasString, __u32 rdev)
2246 yaffs_Device *dev = parent->myDev;
2248 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
2249 if (yaffs_FindObjectByName(parent, name)) {
2253 in = yaffs_CreateNewObject(dev, -1, type);
2255 if(type == YAFFS_OBJECT_TYPE_SYMLINK){
2256 str = yaffs_CloneString(aliasString);
2258 yaffs_FreeObject(in);
2268 in->variantType = type;
2270 in->yst_mode = mode;
2272 #ifdef CONFIG_YAFFS_WINCE
2273 yfsd_WinFileTimeNow(in->win_atime);
2274 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
2275 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
2278 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
2280 in->yst_rdev = rdev;
2284 in->nDataChunks = 0;
2286 yaffs_SetObjectName(in, name);
2289 yaffs_AddObjectToDirectory(parent, in);
2291 in->myDev = parent->myDev;
2294 case YAFFS_OBJECT_TYPE_SYMLINK:
2295 in->variant.symLinkVariant.alias = str;
2297 case YAFFS_OBJECT_TYPE_HARDLINK:
2298 in->variant.hardLinkVariant.equivalentObject =
2300 in->variant.hardLinkVariant.equivalentObjectId =
2301 equivalentObject->objectId;
2302 list_add(&in->hardLinks, &equivalentObject->hardLinks);
2304 case YAFFS_OBJECT_TYPE_FILE:
2305 case YAFFS_OBJECT_TYPE_DIRECTORY:
2306 case YAFFS_OBJECT_TYPE_SPECIAL:
2307 case YAFFS_OBJECT_TYPE_UNKNOWN:
2312 if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
2313 /* Could not create the object header, fail the creation */
2314 yaffs_DestroyObject(in);
2323 yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
2324 __u32 mode, __u32 uid, __u32 gid)
2326 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2327 uid, gid, NULL, NULL, 0);
2330 yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
2331 __u32 mode, __u32 uid, __u32 gid)
2333 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2334 mode, uid, gid, NULL, NULL, 0);
2337 yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
2338 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
2340 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2341 uid, gid, NULL, NULL, rdev);
2344 yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
2345 __u32 mode, __u32 uid, __u32 gid,
2346 const YCHAR * alias)
2348 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2349 uid, gid, NULL, alias, 0);
2352 /* yaffs_Link returns the object id of the equivalent object.*/
2353 yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
2354 yaffs_Object * equivalentObject)
2356 /* Get the real object in case we were fed a hard link as an equivalent object */
2357 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
2359 if (yaffs_MknodObject
2360 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2361 equivalentObject, NULL, 0)) {
2362 return equivalentObject;
2369 static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
2370 const YCHAR * newName, int force, int shadows)
2375 yaffs_Object *existingTarget;
2377 if (newDir == NULL) {
2378 newDir = obj->parent; /* use the old directory */
2381 if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
2382 T(YAFFS_TRACE_ALWAYS,
2384 ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
2389 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
2390 if (obj->myDev->isYaffs2) {
2391 unlinkOp = (newDir == obj->myDev->unlinkedDir);
2393 unlinkOp = (newDir == obj->myDev->unlinkedDir
2394 && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
2397 deleteOp = (newDir == obj->myDev->deletedDir);
2399 existingTarget = yaffs_FindObjectByName(newDir, newName);
2401 /* If the object is a file going into the unlinked directory,
2402 * then it is OK to just stuff it in since duplicate names are allowed.
2403 * else only proceed if the new name does not exist and if we're putting
2404 * it into a directory.
2411 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
2412 yaffs_SetObjectName(obj, newName);
2415 yaffs_AddObjectToDirectory(newDir, obj);
2420 /* If it is a deletion then we mark it as a shrink for gc purposes. */
2421 if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
2428 int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
2429 yaffs_Object * newDir, const YCHAR * newName)
2432 yaffs_Object *existingTarget;
2435 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2436 /* Special case for case insemsitive systems (eg. WinCE).
2437 * While look-up is case insensitive, the name isn't.
2438 * Therefore we might want to change x.txt to X.txt
2440 if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
2445 obj = yaffs_FindObjectByName(oldDir, oldName);
2446 /* Check new name to long. */
2447 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
2448 yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
2451 else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
2452 yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
2456 if (obj && obj->renameAllowed) {
2458 /* Now do the handling for an existing target, if there is one */
2460 existingTarget = yaffs_FindObjectByName(newDir, newName);
2461 if (existingTarget &&
2462 existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2463 !list_empty(&existingTarget->variant.directoryVariant.children)) {
2464 /* There is a target that is a non-empty directory, so we fail */
2465 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
2466 } else if (existingTarget && existingTarget != obj) {
2467 /* Nuke the target first, using shadowing,
2468 * but only if it isn't the same object
2470 yaffs_ChangeObjectName(obj, newDir, newName, force,
2471 existingTarget->objectId);
2472 yaffs_UnlinkObject(existingTarget);
2475 return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
2480 /*------------------------- Block Management and Page Allocation ----------------*/
2482 static int yaffs_InitialiseBlocks(yaffs_Device * dev)
2484 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
2486 dev->blockInfo = NULL;
2487 dev->chunkBits = NULL;
2489 dev->allocationBlock = -1; /* force it to get a new one */
2491 /* If the first allocation strategy fails, thry the alternate one */
2492 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2493 if(!dev->blockInfo){
2494 dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
2495 dev->blockInfoAlt = 1;
2498 dev->blockInfoAlt = 0;
2502 /* Set up dynamic blockinfo stuff. */
2503 dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
2504 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2505 if(!dev->chunkBits){
2506 dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
2507 dev->chunkBitsAlt = 1;
2510 dev->chunkBitsAlt = 0;
2513 if (dev->blockInfo && dev->chunkBits) {
2514 memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
2515 memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
2523 static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
2525 if(dev->blockInfoAlt && dev->blockInfo)
2526 YFREE_ALT(dev->blockInfo);
2527 else if(dev->blockInfo)
2528 YFREE(dev->blockInfo);
2530 dev->blockInfoAlt = 0;
2532 dev->blockInfo = NULL;
2534 if(dev->chunkBitsAlt && dev->chunkBits)
2535 YFREE_ALT(dev->chunkBits);
2536 else if(dev->chunkBits)
2537 YFREE(dev->chunkBits);
2538 dev->chunkBitsAlt = 0;
2539 dev->chunkBits = NULL;
2542 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
2543 yaffs_BlockInfo * bi)
2550 return 1; /* disqualification only applies to yaffs2. */
2552 if (!bi->hasShrinkHeader)
2553 return 1; /* can gc */
2555 /* Find the oldest dirty sequence number if we don't know it and save it
2556 * so we don't have to keep recomputing it.
2558 if (!dev->oldestDirtySequence) {
2559 seq = dev->sequenceNumber;
2561 for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
2563 b = yaffs_GetBlockInfo(dev, i);
2564 if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
2565 (b->pagesInUse - b->softDeletions) <
2566 dev->nChunksPerBlock && b->sequenceNumber < seq) {
2567 seq = b->sequenceNumber;
2570 dev->oldestDirtySequence = seq;
2573 /* Can't do gc of this block if there are any blocks older than this one that have
2576 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2580 /* FindDiretiestBlock is used to select the dirtiest block (or close enough)
2581 * for garbage collection.
2584 static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
2588 int b = dev->currentDirtyChecker;
2595 yaffs_BlockInfo *bi;
2596 int pendingPrioritisedExist = 0;
2598 /* First let's see if we need to grab a prioritised block */
2599 if(dev->hasPendingPrioritisedGCs){
2600 for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
2602 bi = yaffs_GetBlockInfo(dev, i);
2603 //yaffs_VerifyBlock(dev,bi,i);
2605 if(bi->gcPrioritise) {
2606 pendingPrioritisedExist = 1;
2607 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2608 yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
2609 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2612 aggressive = 1; /* Fool the non-aggressive skip logiv below */
2617 if(!pendingPrioritisedExist) /* None found, so we can clear this */
2618 dev->hasPendingPrioritisedGCs = 0;
2621 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2623 * else (we're doing a leasurely gc), then we only bother to do this if the
2624 * block has only a few pages in use.
2627 dev->nonAggressiveSkip--;
2629 if (!aggressive && (dev->nonAggressiveSkip > 0)) {
2635 (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2639 dev->internalEndBlock - dev->internalStartBlock + 1;
2642 dev->internalEndBlock - dev->internalStartBlock + 1;
2643 iterations = iterations / 16;
2644 if (iterations > 200) {
2649 for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
2651 if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
2652 b = dev->internalStartBlock;
2655 if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
2656 T(YAFFS_TRACE_ERROR,
2657 (TSTR("**>> Block %d is not valid" TENDSTR), b));
2661 bi = yaffs_GetBlockInfo(dev, b);
2664 if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
2671 if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2672 (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
2673 yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
2675 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2679 dev->currentDirtyChecker = b;
2683 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
2684 dev->nChunksPerBlock - pagesInUse,prioritised));
2687 dev->oldestDirtySequence = 0;
2690 dev->nonAggressiveSkip = 4;
2696 static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
2698 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
2702 /* If the block is still healthy erase it and mark as clean.
2703 * If the block has had a data failure, then retire it.
2706 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2707 (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
2708 blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
2710 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2712 if (!bi->needsRetiring) {
2713 yaffs_InvalidateCheckpoint(dev);
2714 erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
2716 dev->nErasureFailures++;
2717 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2718 (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
2723 ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
2725 for (i = 0; i < dev->nChunksPerBlock; i++) {
2726 if (!yaffs_CheckChunkErased
2727 (dev, blockNo * dev->nChunksPerBlock + i)) {
2728 T(YAFFS_TRACE_ERROR,
2730 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2731 TENDSTR), blockNo, i));
2737 /* Clean it up... */
2738 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2739 dev->nErasedBlocks++;
2741 bi->softDeletions = 0;
2742 bi->hasShrinkHeader = 0;
2743 bi->skipErasedCheck = 1; /* This is clean, so no need to check */
2744 bi->gcPrioritise = 0;
2745 yaffs_ClearChunkBits(dev, blockNo);
2747 T(YAFFS_TRACE_ERASE,
2748 (TSTR("Erased block %d" TENDSTR), blockNo));
2750 dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
2752 yaffs_RetireBlock(dev, blockNo);
2753 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2754 (TSTR("**>> Block %d retired" TENDSTR), blockNo));
2758 static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
2762 yaffs_BlockInfo *bi;
2764 if (dev->nErasedBlocks < 1) {
2765 /* Hoosterman we've got a problem.
2766 * Can't get space to gc
2768 T(YAFFS_TRACE_ERROR,
2769 (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2774 /* Find an empty block. */
2776 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
2777 dev->allocationBlockFinder++;
2778 if (dev->allocationBlockFinder < dev->internalStartBlock
2779 || dev->allocationBlockFinder > dev->internalEndBlock) {
2780 dev->allocationBlockFinder = dev->internalStartBlock;
2783 bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
2785 if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
2786 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2787 dev->sequenceNumber++;
2788 bi->sequenceNumber = dev->sequenceNumber;
2789 dev->nErasedBlocks--;
2790 T(YAFFS_TRACE_ALLOCATE,
2791 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
2792 dev->allocationBlockFinder, dev->sequenceNumber,
2793 dev->nErasedBlocks));
2794 return dev->allocationBlockFinder;
2798 T(YAFFS_TRACE_ALWAYS,
2800 ("yaffs tragedy: no more eraased blocks, but there should have been %d"
2801 TENDSTR), dev->nErasedBlocks));
2807 // Check if there's space to allocate...
2808 // Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
2809 static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
2812 int reservedBlocks = dev->nReservedBlocks;
2813 int checkpointBlocks;
2815 checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
2816 if(checkpointBlocks < 0)
2817 checkpointBlocks = 0;
2819 reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
2821 return (dev->nFreeChunks > reservedChunks);
2824 static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
2827 yaffs_BlockInfo *bi;
2829 if (dev->allocationBlock < 0) {
2830 /* Get next block to allocate off */
2831 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2832 dev->allocationPage = 0;
2835 if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
2836 /* Not enough space to allocate unless we're allowed to use the reserve. */
2840 if (dev->nErasedBlocks < dev->nReservedBlocks
2841 && dev->allocationPage == 0) {
2842 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
2845 /* Next page please.... */
2846 if (dev->allocationBlock >= 0) {
2847 bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
2849 retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
2850 dev->allocationPage;
2852 yaffs_SetChunkBit(dev, dev->allocationBlock,
2853 dev->allocationPage);
2855 dev->allocationPage++;
2859 /* If the block is full set the state to full */
2860 if (dev->allocationPage >= dev->nChunksPerBlock) {
2861 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2862 dev->allocationBlock = -1;
2871 T(YAFFS_TRACE_ERROR,
2872 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2877 static int yaffs_GetErasedChunks(yaffs_Device * dev)
2881 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2883 if (dev->allocationBlock > 0) {
2884 n += (dev->nChunksPerBlock - dev->allocationPage);
2891 static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
2897 int retVal = YAFFS_OK;
2900 int isCheckpointBlock;
2903 int chunksBefore = yaffs_GetErasedChunks(dev);
2906 yaffs_ExtendedTags tags;
2908 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
2910 yaffs_Object *object;
2912 isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
2914 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2916 T(YAFFS_TRACE_TRACING,
2917 (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
2918 bi->pagesInUse, bi->hasShrinkHeader));
2920 /*yaffs_VerifyFreeChunks(dev); */
2922 bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
2924 /* Take off the number of soft deleted entries because
2925 * they're going to get really deleted during GC.
2927 dev->nFreeChunks -= bi->softDeletions;
2931 if (isCheckpointBlock ||
2932 !yaffs_StillSomeChunkBits(dev, block)) {
2933 T(YAFFS_TRACE_TRACING,
2935 ("Collecting block %d that has no chunks in use" TENDSTR),
2937 yaffs_BlockBecameDirty(dev, block);
2940 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
2942 yaffs_VerifyBlock(dev,bi,block);
2944 for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
2945 chunkInBlock < dev->nChunksPerBlock
2946 && yaffs_StillSomeChunkBits(dev, block);
2947 chunkInBlock++, oldChunk++) {
2948 if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
2950 /* This page is in use and might need to be copied off */
2954 yaffs_InitialiseTags(&tags);
2956 yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
2960 yaffs_FindObjectByNumber(dev,
2963 T(YAFFS_TRACE_GC_DETAIL,
2965 ("Collecting page %d, %d %d %d " TENDSTR),
2966 chunkInBlock, tags.objectId, tags.chunkId,
2969 if(object && !yaffs_SkipVerification(dev)){
2970 if(tags.chunkId == 0)
2971 matchingChunk = object->chunkId;
2972 else if(object->softDeleted)
2973 matchingChunk = oldChunk; /* Defeat the test */
2975 matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
2977 if(oldChunk != matchingChunk)
2978 T(YAFFS_TRACE_ERROR,
2979 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2980 oldChunk,matchingChunk,tags.objectId, tags.chunkId));
2985 T(YAFFS_TRACE_ERROR,
2987 ("page %d in gc has no object: %d %d %d "
2989 tags.objectId, tags.chunkId, tags.byteCount));
2992 if (object && object->deleted
2993 && tags.chunkId != 0) {
2994 /* Data chunk in a deleted file, throw it away
2995 * It's a soft deleted data chunk,
2996 * No need to copy this, just forget about it and
2997 * fix up the object.
3000 object->nDataChunks--;
3002 if (object->nDataChunks <= 0) {
3003 /* remeber to clean up the object */
3004 dev->gcCleanupList[cleanups] =
3010 /* Todo object && object->deleted && object->nDataChunks == 0 */
3012 /* Deleted object header with no data chunks.
3013 * Can be discarded and the file deleted.
3015 object->chunkId = 0;
3016 yaffs_FreeTnode(object->myDev,
3019 object->variant.fileVariant.top = NULL;
3020 yaffs_DoGenericObjectDeletion(object);
3022 } else if (object) {
3023 /* It's either a data chunk in a live file or
3024 * an ObjectHeader, so we're interested in it.
3025 * NB Need to keep the ObjectHeaders of deleted files
3026 * until the whole file has been deleted off
3028 tags.serialNumber++;
3032 if (tags.chunkId == 0) {
3033 /* It is an object Id,
3034 * We need to nuke the shrinkheader flags first
3035 * We no longer want the shrinkHeader flag since its work is done
3036 * and if it is left in place it will mess up scanning.
3037 * Also, clear out any shadowing stuff
3040 yaffs_ObjectHeader *oh;
3041 oh = (yaffs_ObjectHeader *)buffer;
3043 oh->shadowsObject = -1;
3044 tags.extraShadows = 0;
3045 tags.extraIsShrinkHeader = 0;
3047 yaffs_VerifyObjectHeader(object,oh,&tags,1);
3051 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
3054 retVal = YAFFS_FAIL;
3057 /* Ok, now fix up the Tnodes etc. */
3059 if (tags.chunkId == 0) {
3061 object->chunkId = newChunk;
3062 object->serial = tags.serialNumber;
3064 /* It's a data chunk */
3065 yaffs_PutChunkIntoFile
3073 yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
3078 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3081 /* Do any required cleanups */
3082 for (i = 0; i < cleanups; i++) {
3083 /* Time to delete the file too */
3085 yaffs_FindObjectByNumber(dev,
3086 dev->gcCleanupList[i]);
3088 yaffs_FreeTnode(dev,
3089 object->variant.fileVariant.
3091 object->variant.fileVariant.top = NULL;
3094 ("yaffs: About to finally delete object %d"
3095 TENDSTR), object->objectId));
3096 yaffs_DoGenericObjectDeletion(object);
3097 object->myDev->nDeletedFiles--;
3104 yaffs_VerifyCollectedBlock(dev,bi,block);
3106 if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
3109 ("gc did not increase free chunks before %d after %d"
3110 TENDSTR), chunksBefore, chunksAfter));
3118 /* New garbage collector
3119 * If we're very low on erased blocks then we do aggressive garbage collection
3120 * otherwise we do "leasurely" garbage collection.
3121 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
3122 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
3124 * The idea is to help clear out space in a more spread-out manner.
3125 * Dunno if it really does anything useful.
3127 static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
3131 int gcOk = YAFFS_OK;
3134 int checkpointBlockAdjust;
3136 if (dev->isDoingGC) {
3137 /* Bail out so we don't get recursive gc */
3141 /* This loop should pass the first time.
3142 * We'll only see looping here if the erase of the collected block fails.
3148 checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
3149 if(checkpointBlockAdjust < 0)
3150 checkpointBlockAdjust = 0;
3152 if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
3153 /* We need a block soon...*/
3156 /* We're in no hurry */
3160 block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
3163 dev->garbageCollections++;
3165 dev->passiveGarbageCollections++;
3170 ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
3171 dev->nErasedBlocks, aggressive));
3173 gcOk = yaffs_GarbageCollectBlock(dev, block);
3176 if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
3179 ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
3180 TENDSTR), dev->nErasedBlocks, maxTries, block));
3182 } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
3185 return aggressive ? gcOk : YAFFS_OK;
3188 /*------------------------- TAGS --------------------------------*/
3190 static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3193 return (tags->chunkId == chunkInObject &&
3194 tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
3199 /*-------------------- Data file manipulation -----------------*/
3201 static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3202 yaffs_ExtendedTags * tags)
3204 /*Get the Tnode, then get the level 0 offset chunk offset */
3207 yaffs_ExtendedTags localTags;
3210 yaffs_Device *dev = in->myDev;
3213 /* Passed a NULL, so use our own tags space */
3217 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3220 theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
3223 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3229 static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
3230 yaffs_ExtendedTags * tags)
3232 /* Get the Tnode, then get the level 0 offset chunk offset */
3235 yaffs_ExtendedTags localTags;
3237 yaffs_Device *dev = in->myDev;
3241 /* Passed a NULL, so use our own tags space */
3245 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3249 theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
3252 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3255 /* Delete the entry in the filestructure (if found) */
3257 yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
3260 /*T(("No level 0 found for %d\n", chunkInInode)); */
3264 /* T(("Could not find %d to delete\n",chunkInInode)); */
3269 #ifdef YAFFS_PARANOID
3271 static int yaffs_CheckFileSanity(yaffs_Object * in)
3279 yaffs_Tags localTags;
3280 yaffs_Tags *tags = &localTags;
3284 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
3285 /* T(("Object not a file\n")); */
3289 objId = in->objectId;
3290 fSize = in->variant.fileVariant.fileSize;
3292 (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
3294 for (chunk = 1; chunk <= nChunks; chunk++) {
3295 tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
3300 theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
3302 if (yaffs_CheckChunkBits
3303 (dev, theChunk / dev->nChunksPerBlock,
3304 theChunk % dev->nChunksPerBlock)) {
3306 yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
3310 (tags, in->objectId, chunk, chunkDeleted)) {
3320 /* T(("No level 0 found for %d\n", chunk)); */
3324 return failed ? YAFFS_FAIL : YAFFS_OK;
3329 static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3330 int chunkInNAND, int inScan)
3332 /* NB inScan is zero unless scanning.
3333 * For forward scanning, inScan is > 0;
3334 * for backward scanning inScan is < 0
3338 yaffs_Device *dev = in->myDev;
3340 yaffs_ExtendedTags existingTags;
3341 yaffs_ExtendedTags newTags;
3342 unsigned existingSerial, newSerial;
3344 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
3345 /* Just ignore an attempt at putting a chunk into a non-file during scanning
3346 * If it is not during Scanning then something went wrong!
3349 T(YAFFS_TRACE_ERROR,
3351 ("yaffs tragedy:attempt to put data chunk into a non-file"
3356 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
3360 tn = yaffs_AddOrFindLevel0Tnode(dev,
3361 &in->variant.fileVariant,
3368 existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
3371 /* If we're scanning then we need to test for duplicates
3372 * NB This does not need to be efficient since it should only ever
3373 * happen when the power fails during a write, then only one
3374 * chunk should ever be affected.
3376 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3377 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3380 if (existingChunk != 0) {
3381 /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
3382 * thus we have to do a FindChunkInFile to get the real chunk id.
3384 * We have a duplicate now we need to decide which one to use:
3386 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3387 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
3388 * YAFFS1: Get both sets of tags and compare serial numbers.
3392 /* Only do this for forward scanning */
3393 yaffs_ReadChunkWithTagsFromNAND(dev,
3397 /* Do a proper find */
3399 yaffs_FindChunkInFile(in, chunkInInode,
3403 if (existingChunk <= 0) {
3404 /*Hoosterman - how did this happen? */
3406 T(YAFFS_TRACE_ERROR,
3408 ("yaffs tragedy: existing chunk < 0 in scan"
3413 /* NB The deleted flags should be false, otherwise the chunks will
3414 * not be loaded during a scan
3417 newSerial = newTags.serialNumber;
3418 existingSerial = existingTags.serialNumber;
3421 (in->myDev->isYaffs2 ||
3422 existingChunk <= 0 ||
3423 ((existingSerial + 1) & 3) == newSerial)) {
3424 /* Forward scanning.
3426 * Delete the old one and drop through to update the tnode
3428 yaffs_DeleteChunk(dev, existingChunk, 1,
3431 /* Backward scanning or we want to use the existing one
3433 * Delete the new one and return early so that the tnode isn't changed
3435 yaffs_DeleteChunk(dev, chunkInNAND, 1,
3443 if (existingChunk == 0) {
3447 yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
3452 static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
3455 int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
3457 if (chunkInNAND >= 0) {
3458 return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
3461 T(YAFFS_TRACE_NANDACCESS,
3462 (TSTR("Chunk %d not found zero instead" TENDSTR),
3464 /* get sane (zero) data if you read a hole */
3465 memset(buffer, 0, in->myDev->nDataBytesPerChunk);
3471 void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
3475 yaffs_ExtendedTags tags;
3476 yaffs_BlockInfo *bi;
3483 block = chunkId / dev->nChunksPerBlock;
3484 page = chunkId % dev->nChunksPerBlock;
3487 if(!yaffs_CheckChunkBit(dev,block,page))
3488 T(YAFFS_TRACE_VERIFY,
3489 (TSTR("Deleting invalid chunk %d"TENDSTR),
3492 bi = yaffs_GetBlockInfo(dev, block);
3494 T(YAFFS_TRACE_DELETION,
3495 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
3498 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
3500 yaffs_InitialiseTags(&tags);
3502 tags.chunkDeleted = 1;
3504 yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
3505 yaffs_HandleUpdateChunk(dev, chunkId, &tags);
3507 dev->nUnmarkedDeletions++;
3510 /* Pull out of the management area.
3511 * If the whole block became dirty, this will kick off an erasure.
3513 if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3514 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3515 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3516 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
3519 yaffs_ClearChunkBit(dev, block, page);
3523 if (bi->pagesInUse == 0 &&
3524 !bi->hasShrinkHeader &&
3525 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3526 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3527 yaffs_BlockBecameDirty(dev, block);
3531 /* T(("Bad news deleting chunk %d\n",chunkId)); */
3536 static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
3537 const __u8 * buffer, int nBytes,
3540 /* Find old chunk Need to do this to get serial number
3541 * Write new one and patch into tree.
3542 * Invalidate old tags.
3546 yaffs_ExtendedTags prevTags;
3549 yaffs_ExtendedTags newTags;
3551 yaffs_Device *dev = in->myDev;
3553 yaffs_CheckGarbageCollection(dev);
3555 /* Get the previous chunk at this location in the file if it exists */
3556 prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
3558 /* Set up new tags */
3559 yaffs_InitialiseTags(&newTags);
3561 newTags.chunkId = chunkInInode;
3562 newTags.objectId = in->objectId;
3563 newTags.serialNumber =
3564 (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3565 newTags.byteCount = nBytes;
3568 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3571 if (newChunkId >= 0) {
3572 yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
3574 if (prevChunkId >= 0) {
3575 yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
3579 yaffs_CheckFileSanity(in);
3585 /* UpdateObjectHeader updates the header on NAND for an object.
3586 * If name is not NULL, then that new name is used.
3588 int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
3589 int isShrink, int shadows)
3592 yaffs_BlockInfo *bi;
3594 yaffs_Device *dev = in->myDev;
3601 yaffs_ExtendedTags newTags;
3602 yaffs_ExtendedTags oldTags;
3604 __u8 *buffer = NULL;
3605 YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
3607 yaffs_ObjectHeader *oh = NULL;
3609 yaffs_strcpy(oldName,"silly old name");
3611 if (!in->fake || force) {
3613 yaffs_CheckGarbageCollection(dev);
3614 yaffs_CheckObjectDetailsLoaded(in);
3616 buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
3617 oh = (yaffs_ObjectHeader *) buffer;
3619 prevChunkId = in->chunkId;
3621 if (prevChunkId >= 0) {
3622 result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
3625 yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
3627 memcpy(oldName, oh->name, sizeof(oh->name));
3630 memset(buffer, 0xFF, dev->nDataBytesPerChunk);
3632 oh->type = in->variantType;
3633 oh->yst_mode = in->yst_mode;
3634 oh->shadowsObject = shadows;
3636 #ifdef CONFIG_YAFFS_WINCE
3637 oh->win_atime[0] = in->win_atime[0];
3638 oh->win_ctime[0] = in->win_ctime[0];
3639 oh->win_mtime[0] = in->win_mtime[0];
3640 oh->win_atime[1] = in->win_atime[1];
3641 oh->win_ctime[1] = in->win_ctime[1];
3642 oh->win_mtime[1] = in->win_mtime[1];
3644 oh->yst_uid = in->yst_uid;
3645 oh->yst_gid = in->yst_gid;
3646 oh->yst_atime = in->yst_atime;
3647 oh->yst_mtime = in->yst_mtime;
3648 oh->yst_ctime = in->yst_ctime;
3649 oh->yst_rdev = in->yst_rdev;
3652 oh->parentObjectId = in->parent->objectId;
3654 oh->parentObjectId = 0;
3657 if (name && *name) {
3658 memset(oh->name, 0, sizeof(oh->name));
3659 yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
3660 } else if (prevChunkId>=0) {
3661 memcpy(oh->name, oldName, sizeof(oh->name));
3663 memset(oh->name, 0, sizeof(oh->name));
3666 oh->isShrink = isShrink;
3668 switch (in->variantType) {
3669 case YAFFS_OBJECT_TYPE_UNKNOWN:
3670 /* Should not happen */
3672 case YAFFS_OBJECT_TYPE_FILE:
3674 (oh->parentObjectId == YAFFS_OBJECTID_DELETED
3675 || oh->parentObjectId ==
3676 YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
3677 fileVariant.fileSize;
3679 case YAFFS_OBJECT_TYPE_HARDLINK:
3680 oh->equivalentObjectId =
3681 in->variant.hardLinkVariant.equivalentObjectId;
3683 case YAFFS_OBJECT_TYPE_SPECIAL:
3686 case YAFFS_OBJECT_TYPE_DIRECTORY:
3689 case YAFFS_OBJECT_TYPE_SYMLINK:
3690 yaffs_strncpy(oh->alias,
3691 in->variant.symLinkVariant.alias,
3692 YAFFS_MAX_ALIAS_LENGTH);
3693 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3698 yaffs_InitialiseTags(&newTags);
3700 newTags.chunkId = 0;
3701 newTags.objectId = in->objectId;
3702 newTags.serialNumber = in->serial;
3704 /* Add extra info for file header */
3706 newTags.extraHeaderInfoAvailable = 1;
3707 newTags.extraParentObjectId = oh->parentObjectId;
3708 newTags.extraFileLength = oh->fileSize;
3709 newTags.extraIsShrinkHeader = oh->isShrink;
3710 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3711 newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
3712 newTags.extraObjectType = in->variantType;
3714 yaffs_VerifyObjectHeader(in,oh,&newTags,1);
3716 /* Create new chunk in NAND */
3718 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3719 (prevChunkId >= 0) ? 1 : 0);
3721 if (newChunkId >= 0) {
3723 in->chunkId = newChunkId;
3725 if (prevChunkId >= 0) {
3726 yaffs_DeleteChunk(dev, prevChunkId, 1,
3730 if(!yaffs_ObjectHasCachedWriteData(in))
3733 /* If this was a shrink, then mark the block that the chunk lives on */
3735 bi = yaffs_GetBlockInfo(in->myDev,
3736 newChunkId /in->myDev-> nChunksPerBlock);
3737 bi->hasShrinkHeader = 1;
3742 retVal = newChunkId;
3747 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3752 /*------------------------ Short Operations Cache ----------------------------------------
3753 * In many situations where there is no high level buffering (eg WinCE) a lot of
3754 * reads might be short sequential reads, and a lot of writes may be short
3755 * sequential writes. eg. scanning/writing a jpeg file.
3756 * In these cases, a short read/write cache can provide a huge perfomance benefit
3757 * with dumb-as-a-rock code.
3758 * In Linux, the page cache provides read buffering aand the short op cache provides write
3761 * There are a limited number (~10) of cache chunks per device so that we don't
3762 * need a very intelligent search.
3765 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
3767 yaffs_Device *dev = obj->myDev;
3769 yaffs_ChunkCache *cache;
3770 int nCaches = obj->myDev->nShortOpCaches;
3772 for(i = 0; i < nCaches; i++){
3773 cache = &dev->srCache[i];
3774 if (cache->object == obj &&
3783 static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
3785 yaffs_Device *dev = obj->myDev;
3786 int lowest = -99; /* Stop compiler whining. */
3788 yaffs_ChunkCache *cache;
3789 int chunkWritten = 0;
3790 int nCaches = obj->myDev->nShortOpCaches;
3796 /* Find the dirty cache for this object with the lowest chunk id. */
3797 for (i = 0; i < nCaches; i++) {
3798 if (dev->srCache[i].object == obj &&
3799 dev->srCache[i].dirty) {
3801 || dev->srCache[i].chunkId <
3803 cache = &dev->srCache[i];
3804 lowest = cache->chunkId;
3809 if (cache && !cache->locked) {
3810 /* Write it out and free it up */
3813 yaffs_WriteChunkDataToObject(cache->object,
3819 cache->object = NULL;
3822 } while (cache && chunkWritten > 0);
3825 /* Hoosterman, disk full while writing cache out. */
3826 T(YAFFS_TRACE_ERROR,
3827 (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3834 /*yaffs_FlushEntireDeviceCache(dev)
3839 void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
3842 int nCaches = dev->nShortOpCaches;
3845 /* Find a dirty object in the cache and flush it...
3846 * until there are no further dirty objects.
3850 for( i = 0; i < nCaches && !obj; i++) {
3851 if (dev->srCache[i].object &&
3852 dev->srCache[i].dirty)
3853 obj = dev->srCache[i].object;
3857 yaffs_FlushFilesChunkCache(obj);
3864 /* Grab us a cache chunk for use.
3865 * First look for an empty one.
3866 * Then look for the least recently used non-dirty one.
3867 * Then look for the least recently used dirty one...., flush and look again.
3869 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
3875 if (dev->nShortOpCaches > 0) {
3876 for (i = 0; i < dev->nShortOpCaches; i++) {
3877 if (!dev->srCache[i].object)
3878 return &dev->srCache[i];
3884 usage = 0; /* just to stop the compiler grizzling */
3886 for (i = 0; i < dev->nShortOpCaches; i++) {
3887 if (!dev->srCache[i].dirty &&
3888 ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
3890 usage = dev->srCache[i].lastUse;
3896 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
3903 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
3905 yaffs_ChunkCache *cache;
3906 yaffs_Object *theObj;
3911 if (dev->nShortOpCaches > 0) {
3912 /* Try find a non-dirty one... */
3914 cache = yaffs_GrabChunkCacheWorker(dev);
3917 /* They were all dirty, find the last recently used object and flush
3918 * its cache, then find again.
3919 * NB what's here is not very accurate, we actually flush the object
3920 * the last recently used page.
3923 /* With locking we can't assume we can use entry zero */
3930 for (i = 0; i < dev->nShortOpCaches; i++) {
3931 if (dev->srCache[i].object &&
3932 !dev->srCache[i].locked &&
3933 (dev->srCache[i].lastUse < usage || !cache))
3935 usage = dev->srCache[i].lastUse;
3936 theObj = dev->srCache[i].object;
3937 cache = &dev->srCache[i];
3942 if (!cache || cache->dirty) {
3943 /* Flush and try again */
3944 yaffs_FlushFilesChunkCache(theObj);
3945 cache = yaffs_GrabChunkCacheWorker(dev);
3955 /* Find a cached chunk */
3956 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
3959 yaffs_Device *dev = obj->myDev;
3961 if (dev->nShortOpCaches > 0) {
3962 for (i = 0; i < dev->nShortOpCaches; i++) {
3963 if (dev->srCache[i].object == obj &&
3964 dev->srCache[i].chunkId == chunkId) {
3967 return &dev->srCache[i];
3974 /* Mark the chunk for the least recently used algorithym */
3975 static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
3979 if (dev->nShortOpCaches > 0) {
3980 if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
3981 /* Reset the cache usages */
3983 for (i = 1; i < dev->nShortOpCaches; i++) {
3984 dev->srCache[i].lastUse = 0;
3991 cache->lastUse = dev->srLastUse;
3999 /* Invalidate a single cache page.
4000 * Do this when a whole page gets written,
4001 * ie the short cache for this page is no longer valid.
4003 static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
4005 if (object->myDev->nShortOpCaches > 0) {
4006 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
4009 cache->object = NULL;
4014 /* Invalidate all the cache pages associated with this object
4015 * Do this whenever ther file is deleted or resized.
4017 static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
4020 yaffs_Device *dev = in->myDev;
4022 if (dev->nShortOpCaches > 0) {
4023 /* Invalidate it. */
4024 for (i = 0; i < dev->nShortOpCaches; i++) {
4025 if (dev->srCache[i].object == in) {
4026 dev->srCache[i].object = NULL;
4032 /*--------------------- Checkpointing --------------------*/
4035 static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
4037 yaffs_CheckpointValidity cp;
4039 memset(&cp,0,sizeof(cp));
4041 cp.structType = sizeof(cp);
4042 cp.magic = YAFFS_MAGIC;
4043 cp.version = YAFFS_CHECKPOINT_VERSION;
4044 cp.head = (head) ? 1 : 0;
4046 return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
4050 static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
4052 yaffs_CheckpointValidity cp;
4055 ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
4058 ok = (cp.structType == sizeof(cp)) &&
4059 (cp.magic == YAFFS_MAGIC) &&
4060 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
4061 (cp.head == ((head) ? 1 : 0));
4065 static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
4068 cp->nErasedBlocks = dev->nErasedBlocks;
4069 cp->allocationBlock = dev->allocationBlock;
4070 cp->allocationPage = dev->allocationPage;
4071 cp->nFreeChunks = dev->nFreeChunks;
4073 cp->nDeletedFiles = dev->nDeletedFiles;
4074 cp->nUnlinkedFiles = dev->nUnlinkedFiles;
4075 cp->nBackgroundDeletions = dev->nBackgroundDeletions;
4076 cp->sequenceNumber = dev->sequenceNumber;
4077 cp->oldestDirtySequence = dev->oldestDirtySequence;
4081 static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
4082 yaffs_CheckpointDevice *cp)
4084 dev->nErasedBlocks = cp->nErasedBlocks;
4085 dev->allocationBlock = cp->allocationBlock;
4086 dev->allocationPage = cp->allocationPage;
4087 dev->nFreeChunks = cp->nFreeChunks;
4089 dev->nDeletedFiles = cp->nDeletedFiles;
4090 dev->nUnlinkedFiles = cp->nUnlinkedFiles;
4091 dev->nBackgroundDeletions = cp->nBackgroundDeletions;
4092 dev->sequenceNumber = cp->sequenceNumber;
4093 dev->oldestDirtySequence = cp->oldestDirtySequence;
4097 static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
4099 yaffs_CheckpointDevice cp;
4101 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
4105 /* Write device runtime values*/
4106 yaffs_DeviceToCheckpointDevice(&cp,dev);
4107 cp.structType = sizeof(cp);
4109 ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
4111 /* Write block info */
4113 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
4114 ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
4117 /* Write chunk bits */
4119 nBytes = nBlocks * dev->chunkBitmapStride;
4120 ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
4126 static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
4128 yaffs_CheckpointDevice cp;
4130 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
4134 ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
4138 if(cp.structType != sizeof(cp))
4142 yaffs_CheckpointDeviceToDevice(dev,&cp);
4144 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
4146 ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
4150 nBytes = nBlocks * dev->chunkBitmapStride;
4152 ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
4157 static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
4161 cp->objectId = obj->objectId;
4162 cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
4163 cp->chunkId = obj->chunkId;
4164 cp->variantType = obj->variantType;
4165 cp->deleted = obj->deleted;
4166 cp->softDeleted = obj->softDeleted;
4167 cp->unlinked = obj->unlinked;
4168 cp->fake = obj->fake;
4169 cp->renameAllowed = obj->renameAllowed;
4170 cp->unlinkAllowed = obj->unlinkAllowed;
4171 cp->serial = obj->serial;
4172 cp->nDataChunks = obj->nDataChunks;
4174 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4175 cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
4176 else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4177 cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
4180 static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
4183 yaffs_Object *parent;
4185 obj->objectId = cp->objectId;
4188 parent = yaffs_FindOrCreateObjectByNumber(
4191 YAFFS_OBJECT_TYPE_DIRECTORY);
4196 yaffs_AddObjectToDirectory(parent, obj);
4198 obj->chunkId = cp->chunkId;
4199 obj->variantType = cp->variantType;
4200 obj->deleted = cp->deleted;
4201 obj->softDeleted = cp->softDeleted;
4202 obj->unlinked = cp->unlinked;
4203 obj->fake = cp->fake;
4204 obj->renameAllowed = cp->renameAllowed;
4205 obj->unlinkAllowed = cp->unlinkAllowed;
4206 obj->serial = cp->serial;
4207 obj->nDataChunks = cp->nDataChunks;
4209 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4210 obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
4211 else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4212 obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
4214 if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
4215 obj->lazyLoaded = 1;
4220 static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
4221 __u32 level, int chunkOffset)
4224 yaffs_Device *dev = in->myDev;
4226 int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4231 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4232 if (tn->internal[i]) {
4233 ok = yaffs_CheckpointTnodeWorker(in,
4236 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4239 } else if (level == 0) {
4240 __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
4241 /* printf("write tnode at %d\n",baseOffset); */
4242 ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
4244 ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
4252 static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
4254 __u32 endMarker = ~0;
4257 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
4258 ok = yaffs_CheckpointTnodeWorker(obj,
4259 obj->variant.fileVariant.top,
4260 obj->variant.fileVariant.topLevel,
4263 ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
4270 static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
4274 yaffs_Device *dev = obj->myDev;
4275 yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
4279 ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
4281 while(ok && (~baseChunk)){
4283 /* Read level 0 tnode */
4286 /* printf("read tnode at %d\n",baseChunk); */
4287 tn = yaffs_GetTnodeRaw(dev);
4289 ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
4290 (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4295 ok = yaffs_AddOrFindLevel0Tnode(dev,
4303 ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
4307 T(YAFFS_TRACE_CHECKPOINT,(
4308 TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
4309 nread,baseChunk,ok));
4315 static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
4318 yaffs_CheckpointObject cp;
4321 struct list_head *lh;
4324 /* Iterate through the objects in each hash entry,
4325 * dumping them to the checkpointing stream.
4328 for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
4329 list_for_each(lh, &dev->objectBucket[i].list) {
4331 obj = list_entry(lh, yaffs_Object, hashLink);
4332 if (!obj->deferedFree) {
4333 yaffs_ObjectToCheckpointObject(&cp,obj);
4334 cp.structType = sizeof(cp);
4336 T(YAFFS_TRACE_CHECKPOINT,(
4337 TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
4338 cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
4340 ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
4342 if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
4343 ok = yaffs_WriteCheckpointTnodes(obj);
4350 /* Dump end of list */
4351 memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
4352 cp.structType = sizeof(cp);
4355 ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
4360 static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
4363 yaffs_CheckpointObject cp;
4366 yaffs_Object *hardList = NULL;
4368 while(ok && !done) {
4369 ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
4370 if(cp.structType != sizeof(cp)) {
4371 T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
4372 cp.structType,sizeof(cp),ok));
4376 T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
4377 cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
4379 if(ok && cp.objectId == ~0)
4382 obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
4384 yaffs_CheckpointObjectToObject(obj,&cp);
4385 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
4386 ok = yaffs_ReadCheckpointTnodes(obj);
4387 } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
4388 obj->hardLinks.next =
4389 (struct list_head *)
4399 yaffs_HardlinkFixup(dev,hardList);
4404 static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
4406 __u32 checkpointSum;
4409 yaffs_GetCheckpointSum(dev,&checkpointSum);
4411 ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
4419 static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
4421 __u32 checkpointSum0;
4422 __u32 checkpointSum1;
4425 yaffs_GetCheckpointSum(dev,&checkpointSum0);
4427 ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
4432 if(checkpointSum0 != checkpointSum1)
4439 static int yaffs_WriteCheckpointData(yaffs_Device *dev)
4444 if(dev->skipCheckpointWrite || !dev->isYaffs2){
4445 T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
4450 ok = yaffs_CheckpointOpen(dev,1);
4453 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
4454 ok = yaffs_WriteCheckpointValidityMarker(dev,1);
4457 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
4458 ok = yaffs_WriteCheckpointDevice(dev);
4461 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
4462 ok = yaffs_WriteCheckpointObjects(dev);
4465 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
4466 ok = yaffs_WriteCheckpointValidityMarker(dev,0);
4470 ok = yaffs_WriteCheckpointSum(dev);
4474 if(!yaffs_CheckpointClose(dev))
4478 dev->isCheckpointed = 1;
4480 dev->isCheckpointed = 0;
4482 return dev->isCheckpointed;
4485 static int yaffs_ReadCheckpointData(yaffs_Device *dev)
4489 if(dev->skipCheckpointRead || !dev->isYaffs2){
4490 T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
4495 ok = yaffs_CheckpointOpen(dev,0); /* open for read */
4498 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
4499 ok = yaffs_ReadCheckpointValidityMarker(dev,1);
4502 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
4503 ok = yaffs_ReadCheckpointDevice(dev);
4506 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
4507 ok = yaffs_ReadCheckpointObjects(dev);
4510 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
4511 ok = yaffs_ReadCheckpointValidityMarker(dev,0);
4515 ok = yaffs_ReadCheckpointSum(dev);
4516 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
4519 if(!yaffs_CheckpointClose(dev))
4523 dev->isCheckpointed = 1;
4525 dev->isCheckpointed = 0;
4531 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
4533 if(dev->isCheckpointed ||
4534 dev->blocksInCheckpoint > 0){
4535 dev->isCheckpointed = 0;
4536 yaffs_CheckpointInvalidateStream(dev);
4537 if(dev->superBlock && dev->markSuperBlockDirty)
4538 dev->markSuperBlockDirty(dev->superBlock);
4543 int yaffs_CheckpointSave(yaffs_Device *dev)
4546 T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4548 yaffs_VerifyObjects(dev);
4549 yaffs_VerifyBlocks(dev);
4550 yaffs_VerifyFreeChunks(dev);
4552 if(!dev->isCheckpointed) {
4553 yaffs_InvalidateCheckpoint(dev);
4554 yaffs_WriteCheckpointData(dev);
4557 T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4559 return dev->isCheckpointed;
4562 int yaffs_CheckpointRestore(yaffs_Device *dev)
4565 T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4567 retval = yaffs_ReadCheckpointData(dev);
4569 if(dev->isCheckpointed){
4570 yaffs_VerifyObjects(dev);
4571 yaffs_VerifyBlocks(dev);
4572 yaffs_VerifyFreeChunks(dev);
4575 T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4580 /*--------------------- File read/write ------------------------
4581 * Read and write have very similar structures.
4582 * In general the read/write has three parts to it
4583 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
4584 * Some complete chunks
4585 * An incomplete chunk to end off with
4587 * Curve-balls: the first chunk might also be the last chunk.
4590 int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
4599 yaffs_ChunkCache *cache;
4606 //chunk = offset / dev->nDataBytesPerChunk + 1;
4607 //start = offset % dev->nDataBytesPerChunk;
4608 yaffs_AddrToChunk(dev,offset,&chunk,&start);
4611 /* OK now check for the curveball where the start and end are in
4614 if ((start + n) < dev->nDataBytesPerChunk) {
4617 nToCopy = dev->nDataBytesPerChunk - start;
4620 cache = yaffs_FindChunkCache(in, chunk);
4622 /* If the chunk is already in the cache or it is less than a whole chunk
4623 * then use the cache (if there is caching)
4624 * else bypass the cache.
4626 if (cache || nToCopy != dev->nDataBytesPerChunk) {
4627 if (dev->nShortOpCaches > 0) {
4629 /* If we can't find the data in the cache, then load it up. */
4632 cache = yaffs_GrabChunkCache(in->myDev);
4634 cache->chunkId = chunk;
4637 yaffs_ReadChunkDataFromObject(in, chunk,
4643 yaffs_UseChunkCache(dev, cache, 0);
4647 #ifdef CONFIG_YAFFS_WINCE
4648 yfsd_UnlockYAFFS(TRUE);
4650 memcpy(buffer, &cache->data[start], nToCopy);
4652 #ifdef CONFIG_YAFFS_WINCE
4653 yfsd_LockYAFFS(TRUE);
4657 /* Read into the local buffer then copy..*/
4660 yaffs_GetTempBuffer(dev, __LINE__);
4661 yaffs_ReadChunkDataFromObject(in, chunk,
4663 #ifdef CONFIG_YAFFS_WINCE
4664 yfsd_UnlockYAFFS(TRUE);
4666 memcpy(buffer, &localBuffer[start], nToCopy);
4668 #ifdef CONFIG_YAFFS_WINCE
4669 yfsd_LockYAFFS(TRUE);
4671 yaffs_ReleaseTempBuffer(dev, localBuffer,
4676 #ifdef CONFIG_YAFFS_WINCE
4677 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4679 /* Under WinCE can't do direct transfer. Need to use a local buffer.
4680 * This is because we otherwise screw up WinCE's memory mapper
4682 yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
4684 #ifdef CONFIG_YAFFS_WINCE
4685 yfsd_UnlockYAFFS(TRUE);
4687 memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
4689 #ifdef CONFIG_YAFFS_WINCE
4690 yfsd_LockYAFFS(TRUE);
4691 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
4695 /* A full chunk. Read directly into the supplied buffer. */
4696 yaffs_ReadChunkDataFromObject(in, chunk, buffer);
4710 int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
4711 int nBytes, int writeThrough)
4720 int startOfWrite = offset;
4721 int chunkWritten = 0;
4728 while (n > 0 && chunkWritten >= 0) {
4729 //chunk = offset / dev->nDataBytesPerChunk + 1;
4730 //start = offset % dev->nDataBytesPerChunk;
4731 yaffs_AddrToChunk(dev,offset,&chunk,&start);
4734 /* OK now check for the curveball where the start and end are in
4738 if ((start + n) < dev->nDataBytesPerChunk) {
4741 /* Now folks, to calculate how many bytes to write back....
4742 * If we're overwriting and not writing to then end of file then
4743 * we need to write back as much as was there before.
4747 in->variant.fileVariant.fileSize -
4748 ((chunk - 1) * dev->nDataBytesPerChunk);
4750 if (nBytesRead > dev->nDataBytesPerChunk) {
4751 nBytesRead = dev->nDataBytesPerChunk;
4756 (start + n)) ? nBytesRead : (start + n);
4759 nToCopy = dev->nDataBytesPerChunk - start;
4760 nToWriteBack = dev->nDataBytesPerChunk;
4763 if (nToCopy != dev->nDataBytesPerChunk) {
4764 /* An incomplete start or end chunk (or maybe both start and end chunk) */
4765 if (dev->nShortOpCaches > 0) {
4766 yaffs_ChunkCache *cache;
4767 /* If we can't find the data in the cache, then load the cache */
4768 cache = yaffs_FindChunkCache(in, chunk);
4771 && yaffs_CheckSpaceForAllocation(in->
4773 cache = yaffs_GrabChunkCache(in->myDev);
4775 cache->chunkId = chunk;
4778 yaffs_ReadChunkDataFromObject(in, chunk,
4784 !yaffs_CheckSpaceForAllocation(in->myDev)){
4785 /* Drop the cache if it was a read cache item and
4786 * no space check has been made for it.
4792 yaffs_UseChunkCache(dev, cache, 1);
4794 #ifdef CONFIG_YAFFS_WINCE
4795 yfsd_UnlockYAFFS(TRUE);
4798 memcpy(&cache->data[start], buffer,
4801 #ifdef CONFIG_YAFFS_WINCE
4802 yfsd_LockYAFFS(TRUE);
4805 cache->nBytes = nToWriteBack;
4809 yaffs_WriteChunkDataToObject
4812 cache->data, cache->nBytes,
4818 chunkWritten = -1; /* fail the write */
4821 /* An incomplete start or end chunk (or maybe both start and end chunk)
4822 * Read into the local buffer then copy, then copy over and write back.
4826 yaffs_GetTempBuffer(dev, __LINE__);
4828 yaffs_ReadChunkDataFromObject(in, chunk,
4831 #ifdef CONFIG_YAFFS_WINCE
4832 yfsd_UnlockYAFFS(TRUE);
4835 memcpy(&localBuffer[start], buffer, nToCopy);
4837 #ifdef CONFIG_YAFFS_WINCE
4838 yfsd_LockYAFFS(TRUE);
4841 yaffs_WriteChunkDataToObject(in, chunk,
4846 yaffs_ReleaseTempBuffer(dev, localBuffer,
4853 #ifdef CONFIG_YAFFS_WINCE
4854 /* Under WinCE can't do direct transfer. Need to use a local buffer.
4855 * This is because we otherwise screw up WinCE's memory mapper
4857 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4858 #ifdef CONFIG_YAFFS_WINCE
4859 yfsd_UnlockYAFFS(TRUE);
4861 memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
4862 #ifdef CONFIG_YAFFS_WINCE
4863 yfsd_LockYAFFS(TRUE);
4866 yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
4867 dev->nDataBytesPerChunk,
4869 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
4871 /* A full chunk. Write directly from the supplied buffer. */
4873 yaffs_WriteChunkDataToObject(in, chunk, buffer,
4874 dev->nDataBytesPerChunk,
4877 /* Since we've overwritten the cached data, we better invalidate it. */
4878 yaffs_InvalidateChunkCache(in, chunk);
4881 if (chunkWritten >= 0) {
4890 /* Update file object */
4892 if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
4893 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4902 /* ---------------------- File resizing stuff ------------------ */
4904 static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
4907 yaffs_Device *dev = in->myDev;
4908 int oldFileSize = in->variant.fileVariant.fileSize;
4910 int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
4912 int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
4913 dev->nDataBytesPerChunk;
4917 /* Delete backwards so that we don't end up with holes if
4918 * power is lost part-way through the operation.
4920 for (i = lastDel; i >= startDel; i--) {
4921 /* NB this could be optimised somewhat,
4922 * eg. could retrieve the tags and write them without
4923 * using yaffs_DeleteChunk
4926 chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
4929 (dev->internalStartBlock * dev->nChunksPerBlock)
4931 ((dev->internalEndBlock +
4932 1) * dev->nChunksPerBlock)) {
4933 T(YAFFS_TRACE_ALWAYS,
4934 (TSTR("Found daft chunkId %d for %d" TENDSTR),
4938 yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
4945 int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
4948 int oldFileSize = in->variant.fileVariant.fileSize;
4949 int newSizeOfPartialChunk;
4952 yaffs_Device *dev = in->myDev;
4954 yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
4956 yaffs_FlushFilesChunkCache(in);
4957 yaffs_InvalidateWholeChunkCache(in);
4959 yaffs_CheckGarbageCollection(dev);
4961 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
4962 return yaffs_GetFileSize(in);
4965 if (newSize == oldFileSize) {
4969 if (newSize < oldFileSize) {
4971 yaffs_PruneResizedChunks(in, newSize);
4973 if (newSizeOfPartialChunk != 0) {
4974 int lastChunk = 1 + newFullChunks;
4976 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4978 /* Got to read and rewrite the last chunk with its new size and zero pad */
4979 yaffs_ReadChunkDataFromObject(in, lastChunk,
4982 memset(localBuffer + newSizeOfPartialChunk, 0,
4983 dev->nDataBytesPerChunk - newSizeOfPartialChunk);
4985 yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
4986 newSizeOfPartialChunk, 1);
4988 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
4991 in->variant.fileVariant.fileSize = newSize;
4993 yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
4995 /* newsSize > oldFileSize */
4996 in->variant.fileVariant.fileSize = newSize;
5001 /* Write a new object header.
5002 * show we've shrunk the file, if need be
5003 * Do this only if the file is not in the deleted directories.
5005 if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
5006 in->parent->objectId != YAFFS_OBJECTID_DELETED) {
5007 yaffs_UpdateObjectHeader(in, NULL, 0,
5008 (newSize < oldFileSize) ? 1 : 0, 0);
5014 loff_t yaffs_GetFileSize(yaffs_Object * obj)
5016 obj = yaffs_GetEquivalentObject(obj);
5018 switch (obj->variantType) {
5019 case YAFFS_OBJECT_TYPE_FILE:
5020 return obj->variant.fileVariant.fileSize;
5021 case YAFFS_OBJECT_TYPE_SYMLINK:
5022 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5030 int yaffs_FlushFile(yaffs_Object * in, int updateTime)
5034 yaffs_FlushFilesChunkCache(in);
5036 #ifdef CONFIG_YAFFS_WINCE
5037 yfsd_WinFileTimeNow(in->win_mtime);
5040 in->yst_mtime = Y_CURRENT_TIME;
5046 (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
5047 0) ? YAFFS_OK : YAFFS_FAIL;
5056 static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
5059 /* First off, invalidate the file's data in the cache, without flushing. */
5060 yaffs_InvalidateWholeChunkCache(in);
5062 if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
5063 /* Move to the unlinked directory so we have a record that it was deleted. */
5064 yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
5068 yaffs_RemoveObjectFromDirectory(in);
5069 yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
5072 yaffs_FreeObject(in);
5077 /* yaffs_DeleteFile deletes the whole file data
5078 * and the inode associated with the file.
5079 * It does not delete the links associated with the file.
5081 static int yaffs_UnlinkFile(yaffs_Object * in)
5085 int immediateDeletion = 0;
5090 immediateDeletion = 1;
5094 if (in->inUse <= 0) {
5095 immediateDeletion = 1;
5099 if (immediateDeletion) {
5101 yaffs_ChangeObjectName(in, in->myDev->deletedDir,
5103 T(YAFFS_TRACE_TRACING,
5104 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
5107 in->myDev->nDeletedFiles++;
5108 if (0 && in->myDev->isYaffs2) {
5109 yaffs_ResizeFile(in, 0);
5111 yaffs_SoftDeleteFile(in);
5114 yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
5122 int yaffs_DeleteFile(yaffs_Object * in)
5124 int retVal = YAFFS_OK;
5126 if (in->nDataChunks > 0) {
5127 /* Use soft deletion if there is data in the file */
5128 if (!in->unlinked) {
5129 retVal = yaffs_UnlinkFile(in);
5131 if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
5133 in->myDev->nDeletedFiles++;
5134 yaffs_SoftDeleteFile(in);
5136 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
5138 /* The file has no data chunks so we toss it immediately */
5139 yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
5140 in->variant.fileVariant.top = NULL;
5141 yaffs_DoGenericObjectDeletion(in);
5147 static int yaffs_DeleteDirectory(yaffs_Object * in)
5149 /* First check that the directory is empty. */
5150 if (list_empty(&in->variant.directoryVariant.children)) {
5151 return yaffs_DoGenericObjectDeletion(in);
5158 static int yaffs_DeleteSymLink(yaffs_Object * in)
5160 YFREE(in->variant.symLinkVariant.alias);
5162 return yaffs_DoGenericObjectDeletion(in);
5165 static int yaffs_DeleteHardLink(yaffs_Object * in)
5167 /* remove this hardlink from the list assocaited with the equivalent
5170 list_del(&in->hardLinks);
5171 return yaffs_DoGenericObjectDeletion(in);
5174 static void yaffs_DestroyObject(yaffs_Object * obj)
5176 switch (obj->variantType) {
5177 case YAFFS_OBJECT_TYPE_FILE:
5178 yaffs_DeleteFile(obj);
5180 case YAFFS_OBJECT_TYPE_DIRECTORY:
5181 yaffs_DeleteDirectory(obj);
5183 case YAFFS_OBJECT_TYPE_SYMLINK:
5184 yaffs_DeleteSymLink(obj);
5186 case YAFFS_OBJECT_TYPE_HARDLINK:
5187 yaffs_DeleteHardLink(obj);
5189 case YAFFS_OBJECT_TYPE_SPECIAL:
5190 yaffs_DoGenericObjectDeletion(obj);
5192 case YAFFS_OBJECT_TYPE_UNKNOWN:
5193 break; /* should not happen. */
5197 static int yaffs_UnlinkWorker(yaffs_Object * obj)
5200 if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
5201 return yaffs_DeleteHardLink(obj);
5202 } else if (!list_empty(&obj->hardLinks)) {
5203 /* Curve ball: We're unlinking an object that has a hardlink.
5205 * This problem arises because we are not strictly following
5206 * The Linux link/inode model.
5208 * We can't really delete the object.
5209 * Instead, we do the following:
5210 * - Select a hardlink.
5211 * - Unhook it from the hard links
5212 * - Unhook it from its parent directory (so that the rename can work)
5213 * - Rename the object to the hardlink's name.
5214 * - Delete the hardlink
5219 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
5221 hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
5223 list_del_init(&hl->hardLinks);
5224 list_del_init(&hl->siblings);
5226 yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
5228 retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
5230 if (retVal == YAFFS_OK) {
5231 retVal = yaffs_DoGenericObjectDeletion(hl);
5236 switch (obj->variantType) {
5237 case YAFFS_OBJECT_TYPE_FILE:
5238 return yaffs_UnlinkFile(obj);
5240 case YAFFS_OBJECT_TYPE_DIRECTORY:
5241 return yaffs_DeleteDirectory(obj);
5243 case YAFFS_OBJECT_TYPE_SYMLINK:
5244 return yaffs_DeleteSymLink(obj);
5246 case YAFFS_OBJECT_TYPE_SPECIAL:
5247 return yaffs_DoGenericObjectDeletion(obj);
5249 case YAFFS_OBJECT_TYPE_HARDLINK:
5250 case YAFFS_OBJECT_TYPE_UNKNOWN:
5258 static int yaffs_UnlinkObject( yaffs_Object *obj)
5261 if (obj && obj->unlinkAllowed) {
5262 return yaffs_UnlinkWorker(obj);
5268 int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
5272 obj = yaffs_FindObjectByName(dir, name);
5273 return yaffs_UnlinkObject(obj);
5276 /*----------------------- Initialisation Scanning ---------------------- */
5278 static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
5279 int backwardScanning)
5283 if (!backwardScanning) {
5284 /* Handle YAFFS1 forward scanning case
5285 * For YAFFS1 we always do the deletion
5289 /* Handle YAFFS2 case (backward scanning)
5290 * If the shadowed object exists then ignore.
5292 if (yaffs_FindObjectByNumber(dev, objId)) {
5297 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
5298 * We put it in unlinked dir to be cleaned up after the scanning
5301 yaffs_FindOrCreateObjectByNumber(dev, objId,
5302 YAFFS_OBJECT_TYPE_FILE);
5303 yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
5304 obj->variant.fileVariant.shrinkSize = 0;
5305 obj->valid = 1; /* So that we don't read any other info for this file */
5315 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
5322 hardList = (yaffs_Object *) (hardList->hardLinks.next);
5324 in = yaffs_FindObjectByNumber(dev,
5325 hl->variant.hardLinkVariant.
5326 equivalentObjectId);
5329 /* Add the hardlink pointers */
5330 hl->variant.hardLinkVariant.equivalentObject = in;
5331 list_add(&hl->hardLinks, &in->hardLinks);
5333 /* Todo Need to report/handle this better.
5334 * Got a problem... hardlink to a non-existant object
5336 hl->variant.hardLinkVariant.equivalentObject = NULL;
5337 INIT_LIST_HEAD(&hl->hardLinks);
5349 static int ybicmp(const void *a, const void *b){
5350 register int aseq = ((yaffs_BlockIndex *)a)->seq;
5351 register int bseq = ((yaffs_BlockIndex *)b)->seq;
5352 register int ablock = ((yaffs_BlockIndex *)a)->block;
5353 register int bblock = ((yaffs_BlockIndex *)b)->block;
5355 return ablock - bblock;
5361 static int yaffs_Scan(yaffs_Device * dev)
5363 yaffs_ExtendedTags tags;
5368 int nBlocksToScan = 0;
5374 yaffs_BlockState state;
5375 yaffs_Object *hardList = NULL;
5376 yaffs_BlockInfo *bi;
5378 yaffs_ObjectHeader *oh;
5380 yaffs_Object *parent;
5381 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5383 int alloc_failed = 0;
5388 yaffs_BlockIndex *blockIndex = NULL;
5390 if (dev->isYaffs2) {
5392 (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
5396 //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
5399 (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
5400 dev->internalStartBlock, dev->internalEndBlock));
5402 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
5404 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
5406 if (dev->isYaffs2) {
5407 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
5412 /* Scan all the blocks to determine their state */
5413 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
5414 bi = yaffs_GetBlockInfo(dev, blk);
5415 yaffs_ClearChunkBits(dev, blk);
5417 bi->softDeletions = 0;
5419 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
5421 bi->blockState = state;
5422 bi->sequenceNumber = sequenceNumber;
5424 T(YAFFS_TRACE_SCAN_DEBUG,
5425 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
5426 state, sequenceNumber));
5428 if (state == YAFFS_BLOCK_STATE_DEAD) {
5429 T(YAFFS_TRACE_BAD_BLOCKS,
5430 (TSTR("block %d is bad" TENDSTR), blk));
5431 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
5432 T(YAFFS_TRACE_SCAN_DEBUG,
5433 (TSTR("Block empty " TENDSTR)));
5434 dev->nErasedBlocks++;
5435 dev->nFreeChunks += dev->nChunksPerBlock;
5436 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
5438 /* Determine the highest sequence number */
5439 if (dev->isYaffs2 &&
5440 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
5441 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
5443 blockIndex[nBlocksToScan].seq = sequenceNumber;
5444 blockIndex[nBlocksToScan].block = blk;
5448 if (sequenceNumber >= dev->sequenceNumber) {
5449 dev->sequenceNumber = sequenceNumber;
5451 } else if (dev->isYaffs2) {
5452 /* TODO: Nasty sequence number! */
5455 ("Block scanning block %d has bad sequence number %d"
5456 TENDSTR), blk, sequenceNumber));
5463 * Dungy old bubble sort for now...
5465 if (dev->isYaffs2) {
5466 yaffs_BlockIndex temp;
5470 for (i = 0; i < nBlocksToScan; i++)
5471 for (j = i + 1; j < nBlocksToScan; j++)
5472 if (blockIndex[i].seq > blockIndex[j].seq) {
5473 temp = blockIndex[j];
5474 blockIndex[j] = blockIndex[i];
5475 blockIndex[i] = temp;
5479 /* Now scan the blocks looking at the data. */
5480 if (dev->isYaffs2) {
5482 endIterator = nBlocksToScan - 1;
5483 T(YAFFS_TRACE_SCAN_DEBUG,
5484 (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
5486 startIterator = dev->internalStartBlock;
5487 endIterator = dev->internalEndBlock;
5490 /* For each block.... */
5491 for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
5494 if (dev->isYaffs2) {
5495 /* get the block to scan in the correct order */
5496 blk = blockIndex[blockIterator].block;
5498 blk = blockIterator;
5501 bi = yaffs_GetBlockInfo(dev, blk);
5502 state = bi->blockState;
5506 /* For each chunk in each block that needs scanning....*/
5507 for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
5508 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
5509 /* Read the tags and decide what to do */
5510 chunk = blk * dev->nChunksPerBlock + c;
5512 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
5515 /* Let's have a good look at this chunk... */
5517 if (!dev->isYaffs2 && tags.chunkDeleted) {
5523 /*T((" %d %d deleted\n",blk,c)); */
5524 } else if (!tags.chunkUsed) {
5525 /* An unassigned chunk in the block
5526 * This means that either the block is empty or
5527 * this is the one being allocated from
5531 /* We're looking at the first chunk in the block so the block is unused */
5532 state = YAFFS_BLOCK_STATE_EMPTY;
5533 dev->nErasedBlocks++;
5535 /* this is the block being allocated from */
5538 (" Allocating from %d %d" TENDSTR),
5540 state = YAFFS_BLOCK_STATE_ALLOCATING;
5541 dev->allocationBlock = blk;
5542 dev->allocationPage = c;
5543 dev->allocationBlockFinder = blk;
5544 /* Set it to here to encourage the allocator to go forth from here. */
5546 /* Yaffs2 sanity check:
5547 * This should be the one with the highest sequence number
5550 && (dev->sequenceNumber !=
5551 bi->sequenceNumber)) {
5552 T(YAFFS_TRACE_ALWAYS,
5554 ("yaffs: Allocation block %d was not highest sequence id:"
5555 " block seq = %d, dev seq = %d"
5556 TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
5560 dev->nFreeChunks += (dev->nChunksPerBlock - c);
5561 } else if (tags.chunkId > 0) {
5562 /* chunkId > 0 so it is a data chunk... */
5563 unsigned int endpos;
5565 yaffs_SetChunkBit(dev, blk, c);
5568 in = yaffs_FindOrCreateObjectByNumber(dev,
5571 YAFFS_OBJECT_TYPE_FILE);
5572 /* PutChunkIntoFile checks for a clash (two data chunks with
5573 * the same chunkId).
5580 if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
5585 (tags.chunkId - 1) * dev->nDataBytesPerChunk +
5588 in->variantType == YAFFS_OBJECT_TYPE_FILE
5589 && in->variant.fileVariant.scannedFileSize <
5591 in->variant.fileVariant.
5592 scannedFileSize = endpos;
5593 if (!dev->useHeaderFileSize) {
5594 in->variant.fileVariant.
5596 in->variant.fileVariant.
5601 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
5603 /* chunkId == 0, so it is an ObjectHeader.
5604 * Thus, we read in the object header and make the object
5606 yaffs_SetChunkBit(dev, blk, c);
5609 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
5613 oh = (yaffs_ObjectHeader *) chunkData;
5615 in = yaffs_FindObjectByNumber(dev,
5617 if (in && in->variantType != oh->type) {
5618 /* This should not happen, but somehow
5619 * Wev'e ended up with an objectId that has been reused but not yet
5620 * deleted, and worse still it has changed type. Delete the old object.
5623 yaffs_DestroyObject(in);
5628 in = yaffs_FindOrCreateObjectByNumber(dev,
5636 if (in && oh->shadowsObject > 0) {
5637 yaffs_HandleShadowedObject(dev,
5643 if (in && in->valid) {
5644 /* We have already filled this one. We have a duplicate and need to resolve it. */
5646 unsigned existingSerial = in->serial;
5647 unsigned newSerial = tags.serialNumber;
5649 if (dev->isYaffs2 ||
5650 ((existingSerial + 1) & 3) ==
5652 /* Use new one - destroy the exisiting one */
5653 yaffs_DeleteChunk(dev,
5658 /* Use existing - destroy this one. */
5659 yaffs_DeleteChunk(dev, chunk, 1,
5664 if (in && !in->valid &&
5665 (tags.objectId == YAFFS_OBJECTID_ROOT ||
5666 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
5667 /* We only load some info, don't fiddle with directory structure */
5669 in->variantType = oh->type;
5671 in->yst_mode = oh->yst_mode;
5672 #ifdef CONFIG_YAFFS_WINCE
5673 in->win_atime[0] = oh->win_atime[0];
5674 in->win_ctime[0] = oh->win_ctime[0];
5675 in->win_mtime[0] = oh->win_mtime[0];
5676 in->win_atime[1] = oh->win_atime[1];
5677 in->win_ctime[1] = oh->win_ctime[1];
5678 in->win_mtime[1] = oh->win_mtime[1];
5680 in->yst_uid = oh->yst_uid;
5681 in->yst_gid = oh->yst_gid;
5682 in->yst_atime = oh->yst_atime;
5683 in->yst_mtime = oh->yst_mtime;
5684 in->yst_ctime = oh->yst_ctime;
5685 in->yst_rdev = oh->yst_rdev;
5687 in->chunkId = chunk;
5689 } else if (in && !in->valid) {
5690 /* we need to load this info */
5693 in->variantType = oh->type;
5695 in->yst_mode = oh->yst_mode;
5696 #ifdef CONFIG_YAFFS_WINCE
5697 in->win_atime[0] = oh->win_atime[0];
5698 in->win_ctime[0] = oh->win_ctime[0];
5699 in->win_mtime[0] = oh->win_mtime[0];
5700 in->win_atime[1] = oh->win_atime[1];
5701 in->win_ctime[1] = oh->win_ctime[1];
5702 in->win_mtime[1] = oh->win_mtime[1];
5704 in->yst_uid = oh->yst_uid;
5705 in->yst_gid = oh->yst_gid;
5706 in->yst_atime = oh->yst_atime;
5707 in->yst_mtime = oh->yst_mtime;
5708 in->yst_ctime = oh->yst_ctime;
5709 in->yst_rdev = oh->yst_rdev;
5711 in->chunkId = chunk;
5713 yaffs_SetObjectName(in, oh->name);
5716 /* directory stuff...
5721 yaffs_FindOrCreateObjectByNumber
5722 (dev, oh->parentObjectId,
5723 YAFFS_OBJECT_TYPE_DIRECTORY);
5724 if (parent->variantType ==
5725 YAFFS_OBJECT_TYPE_UNKNOWN) {
5726 /* Set up as a directory */
5727 parent->variantType =
5728 YAFFS_OBJECT_TYPE_DIRECTORY;
5729 INIT_LIST_HEAD(&parent->variant.
5732 } else if (parent->variantType !=
5733 YAFFS_OBJECT_TYPE_DIRECTORY)
5735 /* Hoosterman, another problem....
5736 * We're trying to use a non-directory as a directory
5739 T(YAFFS_TRACE_ERROR,
5741 ("yaffs tragedy: attempting to use non-directory as"
5742 " a directory in scan. Put in lost+found."
5744 parent = dev->lostNFoundDir;
5747 yaffs_AddObjectToDirectory(parent, in);
5749 if (0 && (parent == dev->deletedDir ||
5750 parent == dev->unlinkedDir)) {
5751 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
5752 dev->nDeletedFiles++;
5754 /* Note re hardlinks.
5755 * Since we might scan a hardlink before its equivalent object is scanned
5756 * we put them all in a list.
5757 * After scanning is complete, we should have all the objects, so we run through this
5758 * list and fix up all the chains.
5761 switch (in->variantType) {
5762 case YAFFS_OBJECT_TYPE_UNKNOWN:
5763 /* Todo got a problem */
5765 case YAFFS_OBJECT_TYPE_FILE:
5768 /* Prune back the shrunken chunks */
5769 yaffs_PruneResizedChunks
5771 /* Mark the block as having a shrinkHeader */
5772 bi->hasShrinkHeader = 1;
5775 if (dev->useHeaderFileSize)
5777 in->variant.fileVariant.
5782 case YAFFS_OBJECT_TYPE_HARDLINK:
5783 in->variant.hardLinkVariant.
5784 equivalentObjectId =
5785 oh->equivalentObjectId;
5786 in->hardLinks.next =
5787 (struct list_head *)
5791 case YAFFS_OBJECT_TYPE_DIRECTORY:
5794 case YAFFS_OBJECT_TYPE_SPECIAL:
5797 case YAFFS_OBJECT_TYPE_SYMLINK:
5798 in->variant.symLinkVariant.alias =
5799 yaffs_CloneString(oh->alias);
5800 if(!in->variant.symLinkVariant.alias)
5805 if (parent == dev->deletedDir) {
5806 yaffs_DestroyObject(in);
5807 bi->hasShrinkHeader = 1;
5813 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
5814 /* If we got this far while scanning, then the block is fully allocated.*/
5815 state = YAFFS_BLOCK_STATE_FULL;
5818 bi->blockState = state;
5820 /* Now let's see if it was dirty */
5821 if (bi->pagesInUse == 0 &&
5822 !bi->hasShrinkHeader &&
5823 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
5824 yaffs_BlockBecameDirty(dev, blk);
5834 /* Ok, we've done all the scanning.
5835 * Fix up the hard link chains.
5836 * We should now have scanned all the objects, now it's time to add these
5840 yaffs_HardlinkFixup(dev,hardList);
5842 /* Handle the unlinked files. Since they were left in an unlinked state we should
5846 struct list_head *i;
5847 struct list_head *n;
5850 /* Soft delete all the unlinked files */
5851 list_for_each_safe(i, n,
5852 &dev->unlinkedDir->variant.directoryVariant.
5855 l = list_entry(i, yaffs_Object, siblings);
5856 yaffs_DestroyObject(l);
5861 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
5867 T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
5873 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
5876 yaffs_ObjectHeader *oh;
5877 yaffs_Device *dev = in->myDev;
5878 yaffs_ExtendedTags tags;
5880 int alloc_failed = 0;
5886 T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
5888 in->lazyLoaded ? "not yet" : "already"));
5893 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
5895 result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
5896 oh = (yaffs_ObjectHeader *) chunkData;
5898 in->yst_mode = oh->yst_mode;
5899 #ifdef CONFIG_YAFFS_WINCE
5900 in->win_atime[0] = oh->win_atime[0];
5901 in->win_ctime[0] = oh->win_ctime[0];
5902 in->win_mtime[0] = oh->win_mtime[0];
5903 in->win_atime[1] = oh->win_atime[1];
5904 in->win_ctime[1] = oh->win_ctime[1];
5905 in->win_mtime[1] = oh->win_mtime[1];
5907 in->yst_uid = oh->yst_uid;
5908 in->yst_gid = oh->yst_gid;
5909 in->yst_atime = oh->yst_atime;
5910 in->yst_mtime = oh->yst_mtime;
5911 in->yst_ctime = oh->yst_ctime;
5912 in->yst_rdev = oh->yst_rdev;
5915 yaffs_SetObjectName(in, oh->name);
5917 if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
5918 in->variant.symLinkVariant.alias =
5919 yaffs_CloneString(oh->alias);
5920 if(!in->variant.symLinkVariant.alias)
5921 alloc_failed = 1; /* Not returned to caller */
5924 yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
5928 static int yaffs_ScanBackwards(yaffs_Device * dev)
5930 yaffs_ExtendedTags tags;
5935 int nBlocksToScan = 0;
5941 yaffs_BlockState state;
5942 yaffs_Object *hardList = NULL;
5943 yaffs_BlockInfo *bi;
5945 yaffs_ObjectHeader *oh;
5947 yaffs_Object *parent;
5948 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5954 int foundChunksInBlock;
5955 int equivalentObjectId;
5956 int alloc_failed = 0;
5959 yaffs_BlockIndex *blockIndex = NULL;
5960 int altBlockIndex = 0;
5962 if (!dev->isYaffs2) {
5964 (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
5970 ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
5971 TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
5974 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
5976 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
5979 blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
5985 (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
5989 dev->blocksInCheckpoint = 0;
5991 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
5993 /* Scan all the blocks to determine their state */
5994 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
5995 bi = yaffs_GetBlockInfo(dev, blk);
5996 yaffs_ClearChunkBits(dev, blk);
5998 bi->softDeletions = 0;
6000 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
6002 bi->blockState = state;
6003 bi->sequenceNumber = sequenceNumber;
6005 if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
6006 bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
6008 T(YAFFS_TRACE_SCAN_DEBUG,
6009 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
6010 state, sequenceNumber));
6013 if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
6014 dev->blocksInCheckpoint++;
6016 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
6017 T(YAFFS_TRACE_BAD_BLOCKS,
6018 (TSTR("block %d is bad" TENDSTR), blk));
6019 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
6020 T(YAFFS_TRACE_SCAN_DEBUG,
6021 (TSTR("Block empty " TENDSTR)));
6022 dev->nErasedBlocks++;
6023 dev->nFreeChunks += dev->nChunksPerBlock;
6024 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6026 /* Determine the highest sequence number */
6027 if (dev->isYaffs2 &&
6028 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
6029 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
6031 blockIndex[nBlocksToScan].seq = sequenceNumber;
6032 blockIndex[nBlocksToScan].block = blk;
6036 if (sequenceNumber >= dev->sequenceNumber) {
6037 dev->sequenceNumber = sequenceNumber;
6039 } else if (dev->isYaffs2) {
6040 /* TODO: Nasty sequence number! */
6043 ("Block scanning block %d has bad sequence number %d"
6044 TENDSTR), blk, sequenceNumber));
6051 (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
6057 /* Sort the blocks */
6058 #ifndef CONFIG_YAFFS_USE_OWN_SORT
6060 /* Use qsort now. */
6061 yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
6065 /* Dungy old bubble sort... */
6067 yaffs_BlockIndex temp;
6071 for (i = 0; i < nBlocksToScan; i++)
6072 for (j = i + 1; j < nBlocksToScan; j++)
6073 if (blockIndex[i].seq > blockIndex[j].seq) {
6074 temp = blockIndex[j];
6075 blockIndex[j] = blockIndex[i];
6076 blockIndex[i] = temp;
6083 T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
6085 /* Now scan the blocks looking at the data. */
6087 endIterator = nBlocksToScan - 1;
6088 T(YAFFS_TRACE_SCAN_DEBUG,
6089 (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
6091 /* For each block.... backwards */
6092 for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
6094 /* Cooperative multitasking! This loop can run for so
6095 long that watchdog timers expire. */
6098 /* get the block to scan in the correct order */
6099 blk = blockIndex[blockIterator].block;
6101 bi = yaffs_GetBlockInfo(dev, blk);
6104 state = bi->blockState;
6108 /* For each chunk in each block that needs scanning.... */
6109 foundChunksInBlock = 0;
6110 for (c = dev->nChunksPerBlock - 1;
6111 !alloc_failed && c >= 0 &&
6112 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6113 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
6114 /* Scan backwards...
6115 * Read the tags and decide what to do
6118 chunk = blk * dev->nChunksPerBlock + c;
6120 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
6123 /* Let's have a good look at this chunk... */
6125 if (!tags.chunkUsed) {
6126 /* An unassigned chunk in the block.
6127 * If there are used chunks after this one, then
6128 * it is a chunk that was skipped due to failing the erased
6129 * check. Just skip it so that it can be deleted.
6130 * But, more typically, We get here when this is an unallocated
6131 * chunk and his means that either the block is empty or
6132 * this is the one being allocated from
6135 if(foundChunksInBlock)
6137 /* This is a chunk that was skipped due to failing the erased check */
6139 } else if (c == 0) {
6140 /* We're looking at the first chunk in the block so the block is unused */
6141 state = YAFFS_BLOCK_STATE_EMPTY;
6142 dev->nErasedBlocks++;
6144 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6145 state == YAFFS_BLOCK_STATE_ALLOCATING) {
6146 if(dev->sequenceNumber == bi->sequenceNumber) {
6147 /* this is the block being allocated from */
6151 (" Allocating from %d %d"
6154 state = YAFFS_BLOCK_STATE_ALLOCATING;
6155 dev->allocationBlock = blk;
6156 dev->allocationPage = c;
6157 dev->allocationBlockFinder = blk;
6160 /* This is a partially written block that is not
6161 * the current allocation block. This block must have
6162 * had a write failure, so set up for retirement.
6165 bi->needsRetiring = 1;
6166 bi->gcPrioritise = 1;
6168 T(YAFFS_TRACE_ALWAYS,
6169 (TSTR("Partially written block %d being set for retirement" TENDSTR),
6179 } else if (tags.chunkId > 0) {
6180 /* chunkId > 0 so it is a data chunk... */
6181 unsigned int endpos;
6183 (tags.chunkId - 1) * dev->nDataBytesPerChunk;
6185 foundChunksInBlock = 1;
6188 yaffs_SetChunkBit(dev, blk, c);
6191 in = yaffs_FindOrCreateObjectByNumber(dev,
6194 YAFFS_OBJECT_TYPE_FILE);
6201 in->variantType == YAFFS_OBJECT_TYPE_FILE
6203 in->variant.fileVariant.shrinkSize) {
6204 /* This has not been invalidated by a resize */
6205 if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
6210 /* File size is calculated by looking at the data chunks if we have not
6211 * seen an object header yet. Stop this practice once we find an object header.
6215 1) * dev->nDataBytesPerChunk +
6218 if (!in->valid && /* have not got an object header yet */
6219 in->variant.fileVariant.
6220 scannedFileSize < endpos) {
6221 in->variant.fileVariant.
6222 scannedFileSize = endpos;
6223 in->variant.fileVariant.
6225 in->variant.fileVariant.
6230 /* This chunk has been invalidated by a resize, so delete */
6231 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
6235 /* chunkId == 0, so it is an ObjectHeader.
6236 * Thus, we read in the object header and make the object
6238 foundChunksInBlock = 1;
6240 yaffs_SetChunkBit(dev, blk, c);
6246 if (tags.extraHeaderInfoAvailable) {
6247 in = yaffs_FindOrCreateObjectByNumber
6248 (dev, tags.objectId,
6249 tags.extraObjectType);
6253 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
6256 tags.extraShadows ||
6258 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6259 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
6262 /* If we don't have valid info then we need to read the chunk
6263 * TODO In future we can probably defer reading the chunk and
6264 * living with invalid data until needed.
6267 result = yaffs_ReadChunkWithTagsFromNAND(dev,
6272 oh = (yaffs_ObjectHeader *) chunkData;
6275 in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
6280 /* TODO Hoosterman we have a problem! */
6281 T(YAFFS_TRACE_ERROR,
6283 ("yaffs tragedy: Could not make object for object %d "
6284 "at chunk %d during scan"
6285 TENDSTR), tags.objectId, chunk));
6290 /* We have already filled this one.
6291 * We have a duplicate that will be discarded, but
6292 * we first have to suck out resize info if it is a file.
6295 if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
6297 oh-> type == YAFFS_OBJECT_TYPE_FILE)||
6298 (tags.extraHeaderInfoAvailable &&
6299 tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
6302 (oh) ? oh->fileSize : tags.
6304 __u32 parentObjectId =
6306 parentObjectId : tags.
6307 extraParentObjectId;
6309 (oh) ? oh->isShrink : tags.
6310 extraIsShrinkHeader;
6312 /* If it is deleted (unlinked at start also means deleted)
6313 * we treat the file size as being zeroed at this point.
6315 if (parentObjectId ==
6316 YAFFS_OBJECTID_DELETED
6317 || parentObjectId ==
6318 YAFFS_OBJECTID_UNLINKED) {
6324 in->variant.fileVariant.
6325 shrinkSize > thisSize) {
6326 in->variant.fileVariant.
6332 bi->hasShrinkHeader = 1;
6336 /* Use existing - destroy this one. */
6337 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
6342 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6344 YAFFS_OBJECTID_LOSTNFOUND)) {
6345 /* We only load some info, don't fiddle with directory structure */
6349 in->variantType = oh->type;
6351 in->yst_mode = oh->yst_mode;
6352 #ifdef CONFIG_YAFFS_WINCE
6353 in->win_atime[0] = oh->win_atime[0];
6354 in->win_ctime[0] = oh->win_ctime[0];
6355 in->win_mtime[0] = oh->win_mtime[0];
6356 in->win_atime[1] = oh->win_atime[1];
6357 in->win_ctime[1] = oh->win_ctime[1];
6358 in->win_mtime[1] = oh->win_mtime[1];
6360 in->yst_uid = oh->yst_uid;
6361 in->yst_gid = oh->yst_gid;
6362 in->yst_atime = oh->yst_atime;
6363 in->yst_mtime = oh->yst_mtime;
6364 in->yst_ctime = oh->yst_ctime;
6365 in->yst_rdev = oh->yst_rdev;
6369 in->variantType = tags.extraObjectType;
6373 in->chunkId = chunk;
6375 } else if (!in->valid) {
6376 /* we need to load this info */
6379 in->chunkId = chunk;
6382 in->variantType = oh->type;
6384 in->yst_mode = oh->yst_mode;
6385 #ifdef CONFIG_YAFFS_WINCE
6386 in->win_atime[0] = oh->win_atime[0];
6387 in->win_ctime[0] = oh->win_ctime[0];
6388 in->win_mtime[0] = oh->win_mtime[0];
6389 in->win_atime[1] = oh->win_atime[1];
6390 in->win_ctime[1] = oh->win_ctime[1];
6391 in->win_mtime[1] = oh->win_mtime[1];
6393 in->yst_uid = oh->yst_uid;
6394 in->yst_gid = oh->yst_gid;
6395 in->yst_atime = oh->yst_atime;
6396 in->yst_mtime = oh->yst_mtime;
6397 in->yst_ctime = oh->yst_ctime;
6398 in->yst_rdev = oh->yst_rdev;
6401 if (oh->shadowsObject > 0)
6402 yaffs_HandleShadowedObject(dev,
6408 yaffs_SetObjectName(in, oh->name);
6410 yaffs_FindOrCreateObjectByNumber
6411 (dev, oh->parentObjectId,
6412 YAFFS_OBJECT_TYPE_DIRECTORY);
6414 fileSize = oh->fileSize;
6415 isShrink = oh->isShrink;
6416 equivalentObjectId = oh->equivalentObjectId;
6420 in->variantType = tags.extraObjectType;
6422 yaffs_FindOrCreateObjectByNumber
6423 (dev, tags.extraParentObjectId,
6424 YAFFS_OBJECT_TYPE_DIRECTORY);
6425 fileSize = tags.extraFileLength;
6426 isShrink = tags.extraIsShrinkHeader;
6427 equivalentObjectId = tags.extraEquivalentObjectId;
6433 /* directory stuff...
6437 if (parent->variantType ==
6438 YAFFS_OBJECT_TYPE_UNKNOWN) {
6439 /* Set up as a directory */
6440 parent->variantType =
6441 YAFFS_OBJECT_TYPE_DIRECTORY;
6442 INIT_LIST_HEAD(&parent->variant.
6445 } else if (parent->variantType !=
6446 YAFFS_OBJECT_TYPE_DIRECTORY)
6448 /* Hoosterman, another problem....
6449 * We're trying to use a non-directory as a directory
6452 T(YAFFS_TRACE_ERROR,
6454 ("yaffs tragedy: attempting to use non-directory as"
6455 " a directory in scan. Put in lost+found."
6457 parent = dev->lostNFoundDir;
6460 yaffs_AddObjectToDirectory(parent, in);
6462 itsUnlinked = (parent == dev->deletedDir) ||
6463 (parent == dev->unlinkedDir);
6466 /* Mark the block as having a shrinkHeader */
6467 bi->hasShrinkHeader = 1;
6470 /* Note re hardlinks.
6471 * Since we might scan a hardlink before its equivalent object is scanned
6472 * we put them all in a list.
6473 * After scanning is complete, we should have all the objects, so we run
6474 * through this list and fix up all the chains.
6477 switch (in->variantType) {
6478 case YAFFS_OBJECT_TYPE_UNKNOWN:
6479 /* Todo got a problem */
6481 case YAFFS_OBJECT_TYPE_FILE:
6483 if (in->variant.fileVariant.
6484 scannedFileSize < fileSize) {
6485 /* This covers the case where the file size is greater
6486 * than where the data is
6487 * This will happen if the file is resized to be larger
6488 * than its current data extents.
6490 in->variant.fileVariant.fileSize = fileSize;
6491 in->variant.fileVariant.scannedFileSize =
6492 in->variant.fileVariant.fileSize;
6496 in->variant.fileVariant.shrinkSize > fileSize) {
6497 in->variant.fileVariant.shrinkSize = fileSize;
6501 case YAFFS_OBJECT_TYPE_HARDLINK:
6503 in->variant.hardLinkVariant.equivalentObjectId =
6505 in->hardLinks.next =
6506 (struct list_head *) hardList;
6510 case YAFFS_OBJECT_TYPE_DIRECTORY:
6513 case YAFFS_OBJECT_TYPE_SPECIAL:
6516 case YAFFS_OBJECT_TYPE_SYMLINK:
6518 in->variant.symLinkVariant.alias =
6519 yaffs_CloneString(oh->
6521 if(!in->variant.symLinkVariant.alias)
6531 } /* End of scanning for each chunk */
6533 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6534 /* If we got this far while scanning, then the block is fully allocated. */
6535 state = YAFFS_BLOCK_STATE_FULL;
6538 bi->blockState = state;
6540 /* Now let's see if it was dirty */
6541 if (bi->pagesInUse == 0 &&
6542 !bi->hasShrinkHeader &&
6543 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
6544 yaffs_BlockBecameDirty(dev, blk);
6550 YFREE_ALT(blockIndex);
6554 /* Ok, we've done all the scanning.
6555 * Fix up the hard link chains.
6556 * We should now have scanned all the objects, now it's time to add these
6559 yaffs_HardlinkFixup(dev,hardList);
6563 * Sort out state of unlinked and deleted objects.
6566 struct list_head *i;
6567 struct list_head *n;
6571 /* Soft delete all the unlinked files */
6572 list_for_each_safe(i, n,
6573 &dev->unlinkedDir->variant.directoryVariant.
6576 l = list_entry(i, yaffs_Object, siblings);
6577 yaffs_DestroyObject(l);
6581 /* Soft delete all the deletedDir files */
6582 list_for_each_safe(i, n,
6583 &dev->deletedDir->variant.directoryVariant.
6586 l = list_entry(i, yaffs_Object, siblings);
6587 yaffs_DestroyObject(l);
6593 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
6599 T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
6604 /*------------------------------ Directory Functions ----------------------------- */
6606 static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
6608 yaffs_Device *dev = obj->myDev;
6610 if(dev && dev->removeObjectCallback)
6611 dev->removeObjectCallback(obj);
6613 list_del_init(&obj->siblings);
6618 static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
6623 T(YAFFS_TRACE_ALWAYS,
6625 ("tragedy: Trying to add an object to a null pointer directory"
6629 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6630 T(YAFFS_TRACE_ALWAYS,
6632 ("tragedy: Trying to add an object to a non-directory"
6637 if (obj->siblings.prev == NULL) {
6638 /* Not initialised */
6639 INIT_LIST_HEAD(&obj->siblings);
6641 } else if (!list_empty(&obj->siblings)) {
6642 /* If it is holed up somewhere else, un hook it */
6643 yaffs_RemoveObjectFromDirectory(obj);
6646 list_add(&obj->siblings, &directory->variant.directoryVariant.children);
6647 obj->parent = directory;
6649 if (directory == obj->myDev->unlinkedDir
6650 || directory == obj->myDev->deletedDir) {
6652 obj->myDev->nUnlinkedFiles++;
6653 obj->renameAllowed = 0;
6657 yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
6662 struct list_head *i;
6663 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
6672 T(YAFFS_TRACE_ALWAYS,
6674 ("tragedy: yaffs_FindObjectByName: null pointer directory"
6678 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6679 T(YAFFS_TRACE_ALWAYS,
6681 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
6685 sum = yaffs_CalcNameSum(name);
6687 list_for_each(i, &directory->variant.directoryVariant.children) {
6689 l = list_entry(i, yaffs_Object, siblings);
6691 yaffs_CheckObjectDetailsLoaded(l);
6693 /* Special case for lost-n-found */
6694 if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
6695 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
6698 } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
6700 /* LostnFound cunk called Objxxx
6703 yaffs_GetObjectName(l, buffer,
6704 YAFFS_MAX_NAME_LENGTH);
6705 if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
6718 int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
6719 int (*fn) (yaffs_Object *))
6721 struct list_head *i;
6725 T(YAFFS_TRACE_ALWAYS,
6727 ("tragedy: yaffs_FindObjectByName: null pointer directory"
6731 if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6732 T(YAFFS_TRACE_ALWAYS,
6734 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
6738 list_for_each(i, &theDir->variant.directoryVariant.children) {
6740 l = list_entry(i, yaffs_Object, siblings);
6752 /* GetEquivalentObject dereferences any hard links to get to the
6756 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
6758 if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
6759 /* We want the object id of the equivalent object, not this one */
6760 obj = obj->variant.hardLinkVariant.equivalentObject;
6761 yaffs_CheckObjectDetailsLoaded(obj);
6767 int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
6769 memset(name, 0, buffSize * sizeof(YCHAR));
6771 yaffs_CheckObjectDetailsLoaded(obj);
6773 if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
6774 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
6775 } else if (obj->chunkId <= 0) {
6777 /* make up a name */
6778 yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
6780 yaffs_strncpy(name, locName, buffSize - 1);
6783 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
6784 else if (obj->shortName[0]) {
6785 yaffs_strcpy(name, obj->shortName);
6790 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
6792 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
6794 memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
6796 if (obj->chunkId >= 0) {
6797 result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
6798 obj->chunkId, buffer,
6801 yaffs_strncpy(name, oh->name, buffSize - 1);
6803 yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
6806 return yaffs_strlen(name);
6809 int yaffs_GetObjectFileLength(yaffs_Object * obj)
6812 /* Dereference any hard linking */
6813 obj = yaffs_GetEquivalentObject(obj);
6815 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
6816 return obj->variant.fileVariant.fileSize;
6818 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
6819 return yaffs_strlen(obj->variant.symLinkVariant.alias);
6821 /* Only a directory should drop through to here */
6822 return obj->myDev->nDataBytesPerChunk;
6826 int yaffs_GetObjectLinkCount(yaffs_Object * obj)
6829 struct list_head *i;
6831 if (!obj->unlinked) {
6832 count++; /* the object itself */
6834 list_for_each(i, &obj->hardLinks) {
6835 count++; /* add the hard links; */
6841 int yaffs_GetObjectInode(yaffs_Object * obj)
6843 obj = yaffs_GetEquivalentObject(obj);
6845 return obj->objectId;
6848 unsigned yaffs_GetObjectType(yaffs_Object * obj)
6850 obj = yaffs_GetEquivalentObject(obj);
6852 switch (obj->variantType) {
6853 case YAFFS_OBJECT_TYPE_FILE:
6856 case YAFFS_OBJECT_TYPE_DIRECTORY:
6859 case YAFFS_OBJECT_TYPE_SYMLINK:
6862 case YAFFS_OBJECT_TYPE_HARDLINK:
6865 case YAFFS_OBJECT_TYPE_SPECIAL:
6866 if (S_ISFIFO(obj->yst_mode))
6868 if (S_ISCHR(obj->yst_mode))
6870 if (S_ISBLK(obj->yst_mode))
6872 if (S_ISSOCK(obj->yst_mode))
6880 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
6882 obj = yaffs_GetEquivalentObject(obj);
6883 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
6884 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
6886 return yaffs_CloneString(_Y(""));
6890 #ifndef CONFIG_YAFFS_WINCE
6892 int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
6894 unsigned int valid = attr->ia_valid;
6896 if (valid & ATTR_MODE)
6897 obj->yst_mode = attr->ia_mode;
6898 if (valid & ATTR_UID)
6899 obj->yst_uid = attr->ia_uid;
6900 if (valid & ATTR_GID)
6901 obj->yst_gid = attr->ia_gid;
6903 if (valid & ATTR_ATIME)
6904 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
6905 if (valid & ATTR_CTIME)
6906 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
6907 if (valid & ATTR_MTIME)
6908 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
6910 if (valid & ATTR_SIZE)
6911 yaffs_ResizeFile(obj, attr->ia_size);
6913 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
6918 int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
6920 unsigned int valid = 0;
6922 attr->ia_mode = obj->yst_mode;
6924 attr->ia_uid = obj->yst_uid;
6926 attr->ia_gid = obj->yst_gid;
6929 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
6930 valid |= ATTR_ATIME;
6931 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
6932 valid |= ATTR_CTIME;
6933 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
6934 valid |= ATTR_MTIME;
6936 attr->ia_size = yaffs_GetFileSize(obj);
6939 attr->ia_valid = valid;
6948 int yaffs_DumpObject(yaffs_Object * obj)
6952 yaffs_GetObjectName(obj, name, 256);
6954 T(YAFFS_TRACE_ALWAYS,
6956 ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
6957 " chunk %d type %d size %d\n"
6958 TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
6959 obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
6960 yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
6966 /*---------------------------- Initialisation code -------------------------------------- */
6968 static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
6971 /* Common functions, gotta have */
6972 if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
6975 #ifdef CONFIG_YAFFS_YAFFS2
6977 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
6978 if (dev->writeChunkWithTagsToNAND &&
6979 dev->readChunkWithTagsFromNAND &&
6980 !dev->writeChunkToNAND &&
6981 !dev->readChunkFromNAND &&
6982 dev->markNANDBlockBad && dev->queryNANDBlock)
6986 /* Can use the "spare" style interface for yaffs1 */
6987 if (!dev->isYaffs2 &&
6988 !dev->writeChunkWithTagsToNAND &&
6989 !dev->readChunkWithTagsFromNAND &&
6990 dev->writeChunkToNAND &&
6991 dev->readChunkFromNAND &&
6992 !dev->markNANDBlockBad && !dev->queryNANDBlock)
6999 static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
7001 /* Initialise the unlinked, deleted, root and lost and found directories */
7003 dev->lostNFoundDir = dev->rootDir = NULL;
7004 dev->unlinkedDir = dev->deletedDir = NULL;
7007 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
7010 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
7013 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
7014 YAFFS_ROOT_MODE | S_IFDIR);
7015 dev->lostNFoundDir =
7016 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
7017 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
7019 if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
7020 yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
7027 int yaffs_GutsInitialise(yaffs_Device * dev)
7029 int init_failed = 0;
7033 T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
7035 /* Check stuff that must be set */
7038 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
7042 dev->internalStartBlock = dev->startBlock;
7043 dev->internalEndBlock = dev->endBlock;
7044 dev->blockOffset = 0;
7045 dev->chunkOffset = 0;
7046 dev->nFreeChunks = 0;
7048 if (dev->startBlock == 0) {
7049 dev->internalStartBlock = dev->startBlock + 1;
7050 dev->internalEndBlock = dev->endBlock + 1;
7051 dev->blockOffset = 1;
7052 dev->chunkOffset = dev->nChunksPerBlock;
7055 /* Check geometry parameters. */
7057 if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
7058 (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
7059 dev->nChunksPerBlock < 2 ||
7060 dev->nReservedBlocks < 2 ||
7061 dev->internalStartBlock <= 0 ||
7062 dev->internalEndBlock <= 0 ||
7063 dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
7065 T(YAFFS_TRACE_ALWAYS,
7067 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
7068 TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
7072 if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
7073 T(YAFFS_TRACE_ALWAYS,
7074 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
7078 /* Got the right mix of functions? */
7079 if (!yaffs_CheckDevFunctions(dev)) {
7080 /* Function missing */
7081 T(YAFFS_TRACE_ALWAYS,
7083 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
7088 /* This is really a compilation check. */
7089 if (!yaffs_CheckStructures()) {
7090 T(YAFFS_TRACE_ALWAYS,
7091 (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
7095 if (dev->isMounted) {
7096 T(YAFFS_TRACE_ALWAYS,
7097 (TSTR("yaffs: device already mounted\n" TENDSTR)));
7101 /* Finished with most checks. One or two more checks happen later on too. */
7107 /* OK now calculate a few things for the device */
7110 * Calculate all the chunk size manipulation numbers:
7112 /* Start off assuming it is a power of 2 */
7113 dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
7114 dev->chunkMask = (1<<dev->chunkShift) - 1;
7116 if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
7117 /* Yes it is a power of 2, disable crumbs */
7119 dev->crumbShift = 0;
7120 dev->crumbsPerChunk = 0;
7122 /* Not a power of 2, use crumbs instead */
7123 dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
7124 dev->crumbMask = (1<<dev->crumbShift)-1;
7125 dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
7126 dev->chunkShift = 0;
7132 * Calculate chunkGroupBits.
7133 * We need to find the next power of 2 > than internalEndBlock
7136 x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
7140 /* Set up tnode width if wide tnodes are enabled. */
7141 if(!dev->wideTnodesDisabled){
7142 /* bits must be even so that we end up with 32-bit words */
7146 dev->tnodeWidth = 16;
7148 dev->tnodeWidth = bits;
7151 dev->tnodeWidth = 16;
7153 dev->tnodeMask = (1<<dev->tnodeWidth)-1;
7155 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
7156 * so if the bitwidth of the
7157 * chunk range we're using is greater than 16 we need
7158 * to figure out chunk shift and chunkGroupSize
7161 if (bits <= dev->tnodeWidth)
7162 dev->chunkGroupBits = 0;
7164 dev->chunkGroupBits = bits - dev->tnodeWidth;
7167 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
7169 if (dev->nChunksPerBlock < dev->chunkGroupSize) {
7170 /* We have a problem because the soft delete won't work if
7171 * the chunk group size > chunks per block.
7172 * This can be remedied by using larger "virtual blocks".
7174 T(YAFFS_TRACE_ALWAYS,
7175 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
7180 /* OK, we've finished verifying the device, lets continue with initialisation */
7182 /* More device initialisation */
7183 dev->garbageCollections = 0;
7184 dev->passiveGarbageCollections = 0;
7185 dev->currentDirtyChecker = 0;
7186 dev->bufferedBlock = -1;
7187 dev->doingBufferedBlockRewrite = 0;
7188 dev->nDeletedFiles = 0;
7189 dev->nBackgroundDeletions = 0;
7190 dev->nUnlinkedFiles = 0;
7192 dev->eccUnfixed = 0;
7193 dev->tagsEccFixed = 0;
7194 dev->tagsEccUnfixed = 0;
7195 dev->nErasureFailures = 0;
7196 dev->nErasedBlocks = 0;
7198 dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
7200 /* Initialise temporary buffers and caches. */
7201 if(!yaffs_InitialiseTempBuffers(dev))
7204 dev->srCache = NULL;
7205 dev->gcCleanupList = NULL;
7209 dev->nShortOpCaches > 0) {
7212 int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
7214 if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
7215 dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
7218 buf = dev->srCache = YMALLOC(srCacheBytes);
7221 memset(dev->srCache,0,srCacheBytes);
7223 for (i = 0; i < dev->nShortOpCaches && buf; i++) {
7224 dev->srCache[i].object = NULL;
7225 dev->srCache[i].lastUse = 0;
7226 dev->srCache[i].dirty = 0;
7227 dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
7238 dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
7239 if(!dev->gcCleanupList)
7243 if (dev->isYaffs2) {
7244 dev->useHeaderFileSize = 1;
7246 if(!init_failed && !yaffs_InitialiseBlocks(dev))
7249 yaffs_InitialiseTnodes(dev);
7250 yaffs_InitialiseObjects(dev);
7252 if(!init_failed && !yaffs_CreateInitialDirectories(dev))
7257 /* Now scan the flash. */
7258 if (dev->isYaffs2) {
7259 if(yaffs_CheckpointRestore(dev)) {
7260 T(YAFFS_TRACE_ALWAYS,
7261 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
7264 /* Clean up the mess caused by an aborted checkpoint load
7265 * and scan backwards.
7267 yaffs_DeinitialiseBlocks(dev);
7268 yaffs_DeinitialiseTnodes(dev);
7269 yaffs_DeinitialiseObjects(dev);
7272 dev->nErasedBlocks = 0;
7273 dev->nFreeChunks = 0;
7274 dev->allocationBlock = -1;
7275 dev->allocationPage = -1;
7276 dev->nDeletedFiles = 0;
7277 dev->nUnlinkedFiles = 0;
7278 dev->nBackgroundDeletions = 0;
7279 dev->oldestDirtySequence = 0;
7281 if(!init_failed && !yaffs_InitialiseBlocks(dev))
7284 yaffs_InitialiseTnodes(dev);
7285 yaffs_InitialiseObjects(dev);
7287 if(!init_failed && !yaffs_CreateInitialDirectories(dev))
7290 if(!init_failed && !yaffs_ScanBackwards(dev))
7294 if(!yaffs_Scan(dev))
7299 /* Clean up the mess */
7300 T(YAFFS_TRACE_TRACING,
7301 (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
7303 yaffs_Deinitialise(dev);
7307 /* Zero out stats */
7308 dev->nPageReads = 0;
7309 dev->nPageWrites = 0;
7310 dev->nBlockErasures = 0;
7312 dev->nRetriedWrites = 0;
7314 dev->nRetiredBlocks = 0;
7316 yaffs_VerifyFreeChunks(dev);
7317 yaffs_VerifyBlocks(dev);
7320 T(YAFFS_TRACE_TRACING,
7321 (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
7326 void yaffs_Deinitialise(yaffs_Device * dev)
7328 if (dev->isMounted) {
7331 yaffs_DeinitialiseBlocks(dev);
7332 yaffs_DeinitialiseTnodes(dev);
7333 yaffs_DeinitialiseObjects(dev);
7334 if (dev->nShortOpCaches > 0 &&
7337 for (i = 0; i < dev->nShortOpCaches; i++) {
7338 if(dev->srCache[i].data)
7339 YFREE(dev->srCache[i].data);
7340 dev->srCache[i].data = NULL;
7343 YFREE(dev->srCache);
7344 dev->srCache = NULL;
7347 YFREE(dev->gcCleanupList);
7349 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
7350 YFREE(dev->tempBuffer[i].buffer);
7358 static int yaffs_CountFreeChunks(yaffs_Device * dev)
7363 yaffs_BlockInfo *blk;
7365 for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
7367 blk = yaffs_GetBlockInfo(dev, b);
7369 switch (blk->blockState) {
7370 case YAFFS_BLOCK_STATE_EMPTY:
7371 case YAFFS_BLOCK_STATE_ALLOCATING:
7372 case YAFFS_BLOCK_STATE_COLLECTING:
7373 case YAFFS_BLOCK_STATE_FULL:
7375 (dev->nChunksPerBlock - blk->pagesInUse +
7376 blk->softDeletions);
7387 int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
7389 /* This is what we report to the outside world */
7392 int nDirtyCacheChunks;
7393 int blocksForCheckpoint;
7396 nFree = dev->nFreeChunks;
7398 nFree = yaffs_CountFreeChunks(dev);
7401 nFree += dev->nDeletedFiles;
7403 /* Now count the number of dirty chunks in the cache and subtract those */
7407 for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
7408 if (dev->srCache[i].dirty)
7409 nDirtyCacheChunks++;
7413 nFree -= nDirtyCacheChunks;
7415 nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
7417 /* Now we figure out how much to reserve for the checkpoint and report that... */
7418 blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
7419 if(blocksForCheckpoint < 0)
7420 blocksForCheckpoint = 0;
7422 nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
7431 static int yaffs_freeVerificationFailures;
7433 static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
7438 if(yaffs_SkipVerification(dev))
7441 counted = yaffs_CountFreeChunks(dev);
7443 difference = dev->nFreeChunks - counted;
7446 T(YAFFS_TRACE_ALWAYS,
7447 (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
7448 dev->nFreeChunks, counted, difference));
7449 yaffs_freeVerificationFailures++;
7453 /*---------------------------------------- YAFFS test code ----------------------*/
7455 #define yaffs_CheckStruct(structure,syze, name) \
7456 if(sizeof(structure) != syze) \
7458 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
7459 name,syze,sizeof(structure))); \
7460 return YAFFS_FAIL; \
7463 static int yaffs_CheckStructures(void)
7465 /* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
7466 /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
7467 /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
7468 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
7469 yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
7471 yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")