X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=blobdiff_plain;f=yaffs_guts.c;h=d55aa95b91e450758bf54474e047f7766100515f;hp=4359db29f02081d189bed3c7d46d629763459461;hb=1d2a2cb9408c2f3ed5f585b2d1669e5f6f7b7d6c;hpb=3e5718ec7f0df7b76837d10583419b745cb27474 diff --git a/yaffs_guts.c b/yaffs_guts.c index 4359db2..d55aa95 100644 --- a/yaffs_guts.c +++ b/yaffs_guts.c @@ -1,7 +1,7 @@ /* * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. * - * Copyright (C) 2002-2007 Aleph One Ltd. + * Copyright (C) 2002-2010 Aleph One Ltd. * for Toby Churchill Ltd and Brightstar Engineering * * Created by Charles Manning @@ -10,11 +10,8 @@ * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ - -const char *yaffs_guts_c_version = - "$Id: yaffs_guts.c,v 1.81 2009-03-06 17:20:51 wookey Exp $"; - #include "yportenv.h" +#include "yaffs_trace.h" #include "yaffsinterface.h" #include "yaffs_guts.h" @@ -33,7 +30,19 @@ const char *yaffs_guts_c_version = #include "yaffs_packedtags2.h" -#define YAFFS_PASSIVE_GC_CHUNKS 2 +/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */ +#define YAFFS_GC_GOOD_ENOUGH 2 +#define YAFFS_GC_PASSIVE_THRESHOLD 4 + +#define YAFFS_SMALL_HOLE_THRESHOLD 3 + +/* + * Checkpoints are really no benefit on very small partitions. + * + * To save space on small partitions don't bother with checkpoints unless + * the partition is at least this big. + */ +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60 #include "yaffs_ecc.h" @@ -49,6 +58,7 @@ static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND, const yaffs_ExtendedTags *tags); /* Other local prototypes */ +static void yaffs_UpdateParent(yaffs_Object *obj); static int yaffs_UnlinkObject(yaffs_Object *obj); static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj); @@ -69,8 +79,6 @@ static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, int isShrink, int shadows); static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj); static int yaffs_CheckStructures(void); -static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, - int chunkOffset, int *limit); static int yaffs_DoGenericObjectDeletion(yaffs_Object *in); static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo); @@ -84,8 +92,6 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj); static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject); -loff_t yaffs_GetFileSize(yaffs_Object *obj); - static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve, yaffs_BlockInfo **blockUsedPtr); @@ -114,6 +120,12 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId); +static int yaffs_HandleHole(yaffs_Object *obj, loff_t newSize); +static void yaffs_SkipRestOfBlock(yaffs_Device *dev); +static int yaffs_VerifyChunkWritten(yaffs_Device *dev, + int chunkInNAND, + const __u8 *data, + yaffs_ExtendedTags *tags); /* Function to calculate chunk and offset */ @@ -174,7 +186,7 @@ static __u32 ShiftsGE(__u32 x) static __u32 Shifts(__u32 x) { - int nShifts; + __u32 nShifts; nShifts = 0; @@ -205,7 +217,7 @@ static int yaffs_InitialiseTempBuffers(yaffs_Device *dev) for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) { dev->tempBuffer[i].line = 0; /* not in use */ dev->tempBuffer[i].buffer = buf = - YMALLOC_DMA(dev->totalBytesPerChunk); + YMALLOC_DMA(dev->param.totalBytesPerChunk); } return buf ? YAFFS_OK : YAFFS_FAIL; @@ -236,9 +248,9 @@ __u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo) T(YAFFS_TRACE_BUFFERS, (TSTR("Out of temp buffers at line %d, other held by lines:"), lineNo)); - for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line)); - } + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR))); /* @@ -288,7 +300,7 @@ int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer) return 1; } - for (i = 0; i < dev->nShortOpCaches; i++) { + for (i = 0; i < dev->param.nShortOpCaches; i++) { if (dev->srCache[i].data == buffer) return 1; } @@ -322,7 +334,7 @@ static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk) static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk) { if (blk < dev->internalStartBlock || blk > dev->internalEndBlock || - chunk < 0 || chunk >= dev->nChunksPerBlock) { + chunk < 0 || chunk >= dev->param.nChunksPerBlock) { T(YAFFS_TRACE_ERROR, (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR), blk, chunk)); @@ -399,16 +411,19 @@ static int yaffs_CountChunkBits(yaffs_Device *dev, int blk) static int yaffs_SkipVerification(yaffs_Device *dev) { + dev=dev; return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL)); } static int yaffs_SkipFullVerification(yaffs_Device *dev) { + dev=dev; return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL)); } static int yaffs_SkipNANDVerification(yaffs_Device *dev) { + dev=dev; return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND)); } @@ -449,9 +464,9 @@ static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n) actuallyUsed = bi->pagesInUse - bi->softDeletions; - if (bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock || - bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock || - actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock) + if (bi->pagesInUse < 0 || bi->pagesInUse > dev->param.nChunksPerBlock || + bi->softDeletions < 0 || bi->softDeletions > dev->param.nChunksPerBlock || + actuallyUsed < 0 || actuallyUsed > dev->param.nChunksPerBlock) T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR), n, bi->pagesInUse, bi->softDeletions)); @@ -465,7 +480,7 @@ static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n) /* Check that the sequence number is valid. * Ten million is legal, but is very unlikely */ - if (dev->isYaffs2 && + if (dev->param.isYaffs2 && (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) && (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000)) T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR), @@ -550,8 +565,8 @@ static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, if (!(tags && obj && oh)) { T(YAFFS_TRACE_VERIFY, - (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR), - (__u32)tags, (__u32)obj, (__u32)oh)); + (TSTR("Verifying object header tags %p obj %p oh %p"TENDSTR), + tags, obj, oh)); return; } @@ -598,7 +613,8 @@ static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, } - +#if 0 +/* Not being used, but don't want to throw away yet */ static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn, __u32 level, int chunkOffset) { @@ -644,6 +660,7 @@ static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn, } +#endif static void yaffs_VerifyFile(yaffs_Object *obj) { @@ -677,12 +694,6 @@ static void yaffs_VerifyFile(yaffs_Object *obj) actualTallness = obj->variant.fileVariant.topLevel; - if (requiredTallness > actualTallness) - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR), - obj->objectId, actualTallness, requiredTallness)); - - /* Check that the chunks in the tnode tree are all correct. * We do this by scanning through the tnode tree and * checking the tags for every chunk match. @@ -757,15 +768,15 @@ static void yaffs_VerifyObject(yaffs_Object *obj) /* Check sane object header chunk */ - chunkMin = dev->internalStartBlock * dev->nChunksPerBlock; - chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1; + chunkMin = dev->internalStartBlock * dev->param.nChunksPerBlock; + chunkMax = (dev->internalEndBlock+1) * dev->param.nChunksPerBlock - 1; chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax); - chunkIdOk = chunkInRange || obj->hdrChunk == 0; + chunkIdOk = chunkInRange || (obj->hdrChunk == 0); chunkValid = chunkInRange && yaffs_CheckChunkBit(dev, - obj->hdrChunk / dev->nChunksPerBlock, - obj->hdrChunk % dev->nChunksPerBlock); + obj->hdrChunk / dev->param.nChunksPerBlock, + obj->hdrChunk % dev->param.nChunksPerBlock); chunkShouldNotBeDeleted = chunkInRange && !chunkValid; if (!obj->fake && @@ -920,6 +931,29 @@ static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev, } + +static int yaffs_VerifyChunkWritten(yaffs_Device *dev, + int chunkInNAND, + const __u8 *data, + yaffs_ExtendedTags *tags) +{ + int retval = YAFFS_OK; + yaffs_ExtendedTags tempTags; + __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__); + int result; + + result = yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,&tempTags); + if(memcmp(buffer,data,dev->nDataBytesPerChunk) || + tempTags.objectId != tags->objectId || + tempTags.chunkId != tags->chunkId || + tempTags.byteCount != tags->byteCount) + retval = YAFFS_FAIL; + + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__); + + return retval; +} + static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags, @@ -959,12 +993,11 @@ static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, * chunk due to power loss. This checking policy should * catch that case with very few checks and thus save a * lot of checks that are most likely not needed. + * + * Mods to the above + * If an erase check fails or the write fails we skip the + * rest of the block. */ - if (bi->gcPrioritise) { - yaffs_DeleteChunk(dev, chunk, 1, __LINE__); - /* try another chunk */ - continue; - } /* let's give it a try */ attempts++; @@ -976,23 +1009,33 @@ static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, erasedOk = yaffs_CheckChunkErased(dev, chunk); if (erasedOk != YAFFS_OK) { T(YAFFS_TRACE_ERROR, - (TSTR ("**>> yaffs chunk %d was not erased" + (TSTR("**>> yaffs chunk %d was not erased" TENDSTR), chunk)); - /* try another chunk */ + /* If not erased, delete this one, + * skip rest of block and + * try another chunk */ + yaffs_DeleteChunk(dev,chunk,1,__LINE__); + yaffs_SkipRestOfBlock(dev); continue; } - bi->skipErasedCheck = 1; } writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk, data, tags); + + if(!bi->skipErasedCheck) + writeOk = yaffs_VerifyChunkWritten(dev, chunk, data, tags); + if (writeOk != YAFFS_OK) { + /* Clean up aborted write, skip to next block and + * try another chunk */ yaffs_HandleWriteChunkError(dev, chunk, erasedOk); - /* try another chunk */ continue; } + bi->skipErasedCheck = 1; + /* Copy the data into the robustification buffer */ yaffs_HandleWriteChunkOk(dev, chunk, data, tags); @@ -1013,6 +1056,85 @@ static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, return chunk; } + +/* + * Oldest Dirty Sequence Number handling. + */ + +/* yaffs_CalcOldestDirtySequence() + * yaffs_FindOldestDirtySequence() + * Calculate the oldest dirty sequence number if we don't know it. + */ +static void yaffs_CalcOldestDirtySequence(yaffs_Device *dev) +{ + int i; + unsigned seq; + unsigned blockNo = 0; + yaffs_BlockInfo *b; + + if(!dev->param.isYaffs2) + return; + + /* Find the oldest dirty sequence number. */ + seq = dev->sequenceNumber + 1; + b = dev->blockInfo; + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) { + if (b->blockState == YAFFS_BLOCK_STATE_FULL && + (b->pagesInUse - b->softDeletions) < dev->param.nChunksPerBlock && + b->sequenceNumber < seq) { + seq = b->sequenceNumber; + blockNo = i; + } + b++; + } + + if(blockNo){ + dev->oldestDirtySequence = seq; + dev->oldestDirtyBlock = blockNo; + } + +} + + +static void yaffs_FindOldestDirtySequence(yaffs_Device *dev) +{ + if(dev->param.isYaffs2 && !dev->oldestDirtySequence) + yaffs_CalcOldestDirtySequence(dev); +} + +/* + * yaffs_ClearOldestDirtySequence() + * Called when a block is erased or marked bad. (ie. when its sequenceNumber + * becomes invalid). If the value matches the oldest then we clear + * dev->oldestDirtySequence to force its recomputation. + */ +static void yaffs_ClearOldestDirtySequence(yaffs_Device *dev, yaffs_BlockInfo *bi) +{ + + if(!dev->param.isYaffs2) + return; + + if(!bi || bi->sequenceNumber == dev->oldestDirtySequence){ + dev->oldestDirtySequence = 0; + dev->oldestDirtyBlock = 0; + } +} + +/* + * yaffs_UpdateOldestDirtySequence() + * Update the oldest dirty sequence number whenever we dirty a block. + * Only do this if the oldestDirtySequence is actually being tracked. + */ +static void yaffs_UpdateOldestDirtySequence(yaffs_Device *dev, unsigned blockNo, yaffs_BlockInfo *bi) +{ + if(dev->param.isYaffs2 && dev->oldestDirtySequence){ + if(dev->oldestDirtySequence > bi->sequenceNumber){ + dev->oldestDirtySequence = bi->sequenceNumber; + dev->oldestDirtyBlock = blockNo; + } + } +} + /* * Block retiring for handling a broken block. */ @@ -1022,6 +1144,8 @@ static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND) yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND); yaffs_InvalidateCheckpoint(dev); + + yaffs_ClearOldestDirtySequence(dev,bi); if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) { if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) { @@ -1030,14 +1154,14 @@ static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND) TENDSTR), blockInNAND)); } else { yaffs_ExtendedTags tags; - int chunkId = blockInNAND * dev->nChunksPerBlock; + int chunkId = blockInNAND * dev->param.nChunksPerBlock; __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__); memset(buffer, 0xff, dev->nDataBytesPerChunk); yaffs_InitialiseTags(&tags); tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK; - if (dev->writeChunkWithTagsToNAND(dev, chunkId - + if (dev->param.writeChunkWithTagsToNAND(dev, chunkId - dev->chunkOffset, buffer, &tags) != YAFFS_OK) T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to " TCONT("write bad block marker to block %d") @@ -1063,11 +1187,18 @@ static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *tags) { + dev=dev; + chunkInNAND=chunkInNAND; + data=data; + tags=tags; } static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND, const yaffs_ExtendedTags *tags) { + dev=dev; + chunkInNAND=chunkInNAND; + tags=tags; } void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi) @@ -1088,7 +1219,7 @@ void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi) static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND, int erasedOk) { - int blockInNAND = chunkInNAND / dev->nChunksPerBlock; + int blockInNAND = chunkInNAND / dev->param.nChunksPerBlock; yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND); yaffs_HandleChunkError(dev, bi); @@ -1102,6 +1233,7 @@ static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND, /* Delete the chunk */ yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__); + yaffs_SkipRestOfBlock(dev); } @@ -1131,12 +1263,11 @@ static __u16 yaffs_CalcNameSum(const YCHAR *name) static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name) { #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM - memset(obj->shortName, 0, sizeof (YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1)); - if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) { + memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1)); + if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH) yaffs_strcpy(obj->shortName, name); - } else { + else obj->shortName[0] = _Y('\0'); - } #endif obj->sum = yaffs_CalcNameSum(name); } @@ -1152,11 +1283,22 @@ static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name) * adds them to the tnode free list. * Don't use this function directly */ +static Y_INLINE int yaffs_CalcTnodeSize(yaffs_Device *dev) +{ + int tnodeSize; + /* Calculate the tnode size in bytes for variable width tnode support. + * Must be a multiple of 32-bits */ + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; + + if (tnodeSize < sizeof(yaffs_Tnode)) + tnodeSize = sizeof(yaffs_Tnode); + return tnodeSize; +} static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes) { int i; - int tnodeSize; + int tnodeSize = yaffs_CalcTnodeSize(dev); yaffs_Tnode *newTnodes; __u8 *mem; yaffs_Tnode *curr; @@ -1166,12 +1308,6 @@ static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes) if (nTnodes < 1) return YAFFS_OK; - /* Calculate the tnode size in bytes for variable width tnode support. - * Must be a multiple of 32-bits */ - tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; - - if (tnodeSize < sizeof(yaffs_Tnode)) - tnodeSize = sizeof(yaffs_Tnode); /* make these things */ @@ -1244,10 +1380,14 @@ static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev) { yaffs_Tnode *tn = NULL; +#ifdef CONFIG_YAFFS_VALGRIND_TEST + tn = YMALLOC(yaffs_CalcTnodeSize(dev)); + if(tn) + dev->nTnodesCreated++; +#else /* If there are none left make more */ - if (!dev->freeTnodes) { + if (!dev->freeTnodes) yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES); - } if (dev->freeTnodes) { tn = dev->freeTnodes; @@ -1261,7 +1401,7 @@ static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev) dev->freeTnodes = dev->freeTnodes->internal[0]; dev->nFreeTnodes--; } - +#endif dev->nCheckpointBlocksRequired = 0; /* force recalculation*/ return tn; @@ -1270,10 +1410,7 @@ static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev) static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev) { yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev); - int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; - - if (tnodeSize < sizeof(yaffs_Tnode)) - tnodeSize = sizeof(yaffs_Tnode); + int tnodeSize = yaffs_CalcTnodeSize(dev); if (tn) memset(tn, 0, tnodeSize); @@ -1285,6 +1422,10 @@ static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev) static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn) { if (tn) { +#ifdef CONFIG_YAFFS_VALGRIND_TEST + YFREE(tn); + dev->nTnodesCreated--; +#else #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) { /* Hoosterman, this thing looks like it is already in the list */ @@ -1296,6 +1437,7 @@ static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn) tn->internal[0] = dev->freeTnodes; dev->freeTnodes = tn; dev->nFreeTnodes++; +#endif } dev->nCheckpointBlocksRequired = 0; /* force recalculation*/ } @@ -1316,6 +1458,7 @@ static void yaffs_DeinitialiseTnodes(yaffs_Device *dev) dev->freeTnodes = NULL; dev->nFreeTnodes = 0; + dev->nTnodesCreated = 0; } static void yaffs_InitialiseTnodes(yaffs_Device *dev) @@ -1327,7 +1470,7 @@ static void yaffs_InitialiseTnodes(yaffs_Device *dev) } -void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, +void yaffs_LoadLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val) { __u32 *map = (__u32 *)tn; @@ -1403,14 +1546,14 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev, int requiredTallness; int level = fStruct->topLevel; + dev=dev; + /* Check sane level and chunk Id */ - if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) { + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) return NULL; - } - if (chunkId > YAFFS_MAX_CHUNK_ID) { + if (chunkId > YAFFS_MAX_CHUNK_ID) return NULL; - } /* First check we're tall enough (ie enough topLevel) */ @@ -1421,10 +1564,8 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev, requiredTallness++; } - if (requiredTallness > fStruct->topLevel) { - /* Not tall enough, so we can't find it, return NULL. */ - return NULL; - } + if (requiredTallness > fStruct->topLevel) + return NULL; /* Not tall enough, so we can't find it */ /* Traverse down to level 0 */ while (level > 0 && tn) { @@ -1464,13 +1605,11 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, /* Check sane level and page Id */ - if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) { + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) return NULL; - } - if (chunkId > YAFFS_MAX_CHUNK_ID) { + if (chunkId > YAFFS_MAX_CHUNK_ID) return NULL; - } /* First check we're tall enough (ie enough topLevel) */ @@ -1491,13 +1630,13 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, if (tn) { tn->internal[0] = fStruct->top; fStruct->top = tn; + fStruct->topLevel++; } else { T(YAFFS_TRACE_ERROR, - (TSTR("yaffs: no more tnodes" TENDSTR))); + (TSTR("yaffs: no more tnodes" TENDSTR))); + return NULL; } } - - fStruct->topLevel = requiredTallness; } /* Traverse down to level 0, adding anything we need */ @@ -1516,7 +1655,8 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, if ((l > 1) && !tn->internal[x]) { /* Add missing non-level-zero tnode */ tn->internal[x] = yaffs_GetTnode(dev); - + if(!tn->internal[x]) + return NULL; } else if (l == 1) { /* Looking from level 1 at level 0 */ if (passedTn) { @@ -1528,6 +1668,8 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, } else if (!tn->internal[x]) { /* Don't have one, none passed in */ tn->internal[x] = yaffs_GetTnode(dev); + if(!tn->internal[x]) + return NULL; } } @@ -1552,13 +1694,18 @@ static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, int j; for (j = 0; theChunk && j < dev->chunkGroupSize; j++) { - if (yaffs_CheckChunkBit(dev, theChunk / dev->nChunksPerBlock, - theChunk % dev->nChunksPerBlock)) { - yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, - tags); - if (yaffs_TagsMatch(tags, objectId, chunkInInode)) { - /* found it; */ + if (yaffs_CheckChunkBit(dev, theChunk / dev->param.nChunksPerBlock, + theChunk % dev->param.nChunksPerBlock)) { + + if(dev->chunkGroupSize == 1) return theChunk; + else { + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, + tags); + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) { + /* found it; */ + return theChunk; + } } } theChunk++; @@ -1566,9 +1713,10 @@ static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, return -1; } - +#if 0 +/* Experimental code not being used yet. Might speed up file deletion */ /* DeleteWorker scans backwards through the tnode tree and deletes all the - * chunks and tnodes in the file + * chunks and tnodes in the file. * Returns 1 if the tree was deleted. * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete. */ @@ -1640,14 +1788,13 @@ static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, in->nDataChunks--; if (limit) { *limit = *limit - 1; - if (*limit <= 0) { + if (*limit <= 0) hitLimit = 1; - } } } - yaffs_PutLevel0Tnode(dev, tn, i, 0); + yaffs_LoadLevel0Tnode(dev, tn, i, 0); } } @@ -1661,16 +1808,21 @@ static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, } +#endif + static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk) { yaffs_BlockInfo *theBlock; + unsigned blockNo; T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk)); - theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock); + blockNo = chunk / dev->param.nChunksPerBlock; + theBlock = yaffs_GetBlockInfo(dev, blockNo); if (theBlock) { theBlock->softDeletions++; dev->nFreeChunks++; + yaffs_UpdateOldestDirtySequence(dev, blockNo, theBlock); } } @@ -1724,7 +1876,7 @@ static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, * a block. */ yaffs_SoftDeleteChunk(dev, theChunk); - yaffs_PutLevel0Tnode(dev, tn, i, 0); + yaffs_LoadLevel0Tnode(dev, tn, i, 0); } } @@ -1770,6 +1922,10 @@ static void yaffs_SoftDeleteFile(yaffs_Object *obj) * level 0 tnode entries must be zeroed out. * Could also use this for file deletion, but that's probably better handled * by a special case. + * + * This function is recursive. For levels > 0 the function is called again on + * any sub-tree. For level == 0 we just check if the sub-tree has data. + * If there is no data in a subtree then it is pruned. */ static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, @@ -1781,18 +1937,27 @@ static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, if (tn) { hasData = 0; - for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) { - if (tn->internal[i] && level > 0) { - tn->internal[i] = - yaffs_PruneWorker(dev, tn->internal[i], - level - 1, - (i == 0) ? del0 : 1); - } + if(level > 0){ + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) { + if (tn->internal[i]) { + tn->internal[i] = + yaffs_PruneWorker(dev, tn->internal[i], + level - 1, + (i == 0) ? del0 : 1); + } - if (tn->internal[i]) { - hasData++; + if (tn->internal[i]) + hasData++; } - } + } else { + int tnodeSize_u32 = yaffs_CalcTnodeSize(dev)/sizeof(__u32); + __u32 *map = (__u32 *)tn; + + for(i = 0; !hasData && i < tnodeSize_u32; i++){ + if(map[i]) + hasData++; + } + } if (hasData == 0 && del0) { /* Free and return NULL */ @@ -1831,9 +1996,8 @@ static int yaffs_PruneFileStructure(yaffs_Device *dev, hasData = 0; for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) { - if (tn->internal[i]) { + if (tn->internal[i]) hasData++; - } } if (!hasData) { @@ -1868,10 +2032,14 @@ static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects) list = YMALLOC(sizeof(yaffs_ObjectList)); if (!newObjects || !list) { - if (newObjects) + if (newObjects){ YFREE(newObjects); - if (list) + newObjects = NULL; + } + if (list){ YFREE(list); + list = NULL; + } T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Could not allocate more objects" TENDSTR))); return YAFFS_FAIL; @@ -1903,13 +2071,14 @@ static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev) { yaffs_Object *tn = NULL; -#ifdef VALGRIND_TEST +#ifdef CONFIG_YAFFS_VALGRIND_TEST tn = YMALLOC(sizeof(yaffs_Object)); + if(tn) + dev->nObjectsCreated++; #else /* If there are none left make more */ - if (!dev->freeObjects) { + if (!dev->freeObjects) yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS); - } if (dev->freeObjects) { tn = dev->freeObjects; @@ -1942,9 +2111,8 @@ static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev) * NB Can't put root or lostNFound in lostNFound so * check if lostNFound exists first */ - if (dev->lostNFoundDir) { + if (dev->lostNFoundDir) yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn); - } tn->beingCreated = 0; } @@ -1993,17 +2161,16 @@ static void yaffs_FreeObject(yaffs_Object *tn) { yaffs_Device *dev = tn->myDev; -#ifdef __KERNEL__ - T(YAFFS_TRACE_OS,(TSTR("FreeObject %p inode %p"TENDSTR),tn,tn->myInode)); -#endif - - if(tn->parent) + T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode)); + + if (!tn) + YBUG(); + if (tn->parent) YBUG(); if (!ylist_empty(&tn->siblings)) YBUG(); -#ifdef __KERNEL__ if (tn->myInode) { /* We're still hooked up to a cached inode. * Don't delete now, but mark for later deletion @@ -2011,12 +2178,13 @@ static void yaffs_FreeObject(yaffs_Object *tn) tn->deferedFree = 1; return; } -#endif yaffs_UnhashObject(tn); -#ifdef VALGRIND_TEST +#ifdef CONFIG_YAFFS_VALGRIND_TEST YFREE(tn); + dev->nObjectsCreated--; + tn = NULL; #else /* Link into the free list. */ tn->siblings.next = (struct ylist_head *)(dev->freeObjects); @@ -2026,16 +2194,13 @@ static void yaffs_FreeObject(yaffs_Object *tn) dev->nCheckpointBlocksRequired = 0; /* force recalculation*/ } -#ifdef __KERNEL__ void yaffs_HandleDeferedFree(yaffs_Object *obj) { - if (obj->deferedFree) { + if (obj->deferedFree) yaffs_FreeObject(obj); - } } -#endif static void yaffs_DeinitialiseObjects(yaffs_Device *dev) { @@ -2053,6 +2218,7 @@ static void yaffs_DeinitialiseObjects(yaffs_Device *dev) dev->freeObjects = NULL; dev->nFreeObjects = 0; + dev->nObjectsCreated = 0; } static void yaffs_InitialiseObjects(yaffs_Device *dev) @@ -2071,33 +2237,21 @@ static void yaffs_InitialiseObjects(yaffs_Device *dev) static int yaffs_FindNiceObjectBucket(yaffs_Device *dev) { - static int x; int i; int l = 999; int lowest = 999999; - /* First let's see if we can find one that's empty. */ - for (i = 0; i < 10 && lowest > 0; i++) { - x++; - x %= YAFFS_NOBJECT_BUCKETS; - if (dev->objectBucket[x].count < lowest) { - lowest = dev->objectBucket[x].count; - l = x; - } - - } - - /* If we didn't find an empty list, then try - * looking a bit further for a short one + /* Search for the shortest list or one that + * isn't too long. */ - for (i = 0; i < 10 && lowest > 3; i++) { - x++; - x %= YAFFS_NOBJECT_BUCKETS; - if (dev->objectBucket[x].count < lowest) { - lowest = dev->objectBucket[x].count; - l = x; + for (i = 0; i < 10 && lowest > 4; i++) { + dev->bucketFinder++; + dev->bucketFinder %= YAFFS_NOBJECT_BUCKETS; + if (dev->objectBucket[dev->bucketFinder].count < lowest) { + lowest = dev->objectBucket[dev->bucketFinder].count; + l = dev->bucketFinder; } } @@ -2157,11 +2311,10 @@ yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number) if (i) { in = ylist_entry(i, yaffs_Object, hashLink); if (in->objectId == number) { -#ifdef __KERNEL__ + /* Don't tell the VFS about this one if it is defered free */ if (in->deferedFree) return NULL; -#endif return in; } @@ -2174,25 +2327,26 @@ yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number) yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number, yaffs_ObjectType type) { - yaffs_Object *theObject; + yaffs_Object *theObject=NULL; yaffs_Tnode *tn = NULL; - if (number < 0) { + if (number < 0) number = yaffs_CreateNewObjectNumber(dev); - } - - theObject = yaffs_AllocateEmptyObject(dev); - if (!theObject) - return NULL; if (type == YAFFS_OBJECT_TYPE_FILE) { tn = yaffs_GetTnode(dev); - if (!tn) { - yaffs_FreeObject(theObject); + if (!tn) return NULL; - } } + theObject = yaffs_AllocateEmptyObject(dev); + if (!theObject){ + if(tn) + yaffs_FreeTnode(dev,tn); + return NULL; + } + + if (theObject) { theObject->fake = 0; theObject->renameAllowed = 1; @@ -2223,6 +2377,8 @@ yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number, case YAFFS_OBJECT_TYPE_DIRECTORY: YINIT_LIST_HEAD(&theObject->variant.directoryVariant. children); + YINIT_LIST_HEAD(&theObject->variant.directoryVariant. + dirty); break; case YAFFS_OBJECT_TYPE_SYMLINK: case YAFFS_OBJECT_TYPE_HARDLINK: @@ -2244,13 +2400,11 @@ static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, { yaffs_Object *theObject = NULL; - if (number > 0) { + if (number > 0) theObject = yaffs_FindObjectByNumber(dev, number); - } - if (!theObject) { + if (!theObject) theObject = yaffs_CreateNewObject(dev, number, type); - } return theObject; @@ -2260,13 +2414,17 @@ static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, static YCHAR *yaffs_CloneString(const YCHAR *str) { YCHAR *newStr = NULL; + int len; - if (str && *str) { - newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR)); - if (newStr) - yaffs_strcpy(newStr, str); - } + if (!str) + str = _Y(""); + len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH); + newStr = YMALLOC((len + 1) * sizeof(YCHAR)); + if (newStr){ + yaffs_strncpy(newStr, str,len); + newStr[len] = 0; + } return newStr; } @@ -2274,7 +2432,7 @@ static YCHAR *yaffs_CloneString(const YCHAR *str) /* * Mknod (create) a new object. * equivalentObject only has meaning for a hard link; - * aliasString only has meaning for a sumlink. + * aliasString only has meaning for a symlink. * rdev only has meaning for devices (a subset of special objects) */ @@ -2293,23 +2451,25 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type, yaffs_Device *dev = parent->myDev; /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/ - if (yaffs_FindObjectByName(parent, name)) { + if (yaffs_FindObjectByName(parent, name)) return NULL; - } - - in = yaffs_CreateNewObject(dev, -1, type); - - if (!in) - return YAFFS_FAIL; if (type == YAFFS_OBJECT_TYPE_SYMLINK) { str = yaffs_CloneString(aliasString); - if (!str) { - yaffs_FreeObject(in); + if (!str) return NULL; - } } + in = yaffs_CreateNewObject(dev, -1, type); + + if (!in){ + if(str) + YFREE(str); + return NULL; + } + + + if (in) { @@ -2365,6 +2525,7 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type, in = NULL; } + yaffs_UpdateParent(parent); } return in; @@ -2424,9 +2585,8 @@ static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, yaffs_Object *existingTarget; - if (newDir == NULL) { + if (newDir == NULL) newDir = obj->parent; /* use the old directory */ - } if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) { T(YAFFS_TRACE_ALWAYS, @@ -2437,12 +2597,11 @@ static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, } /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */ - if (obj->myDev->isYaffs2) { + if (obj->myDev->param.isYaffs2) unlinkOp = (newDir == obj->myDev->unlinkedDir); - } else { + else unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE); - } deleteOp = (newDir == obj->myDev->deletedDir); @@ -2481,6 +2640,8 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *obj = NULL; yaffs_Object *existingTarget = NULL; int force = 0; + int result; + yaffs_Device *dev; if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) @@ -2488,17 +2649,18 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) YBUG(); + dev = oldDir->myDev; + #ifdef CONFIG_YAFFS_CASE_INSENSITIVE /* Special case for case insemsitive systems (eg. WinCE). * While look-up is case insensitive, the name isn't. * Therefore we might want to change x.txt to X.txt */ - if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) { + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) force = 1; - } #endif - else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH) + if(yaffs_strnlen(newName,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH) /* ENAMETOOLONG */ return YAFFS_FAIL; @@ -2516,14 +2678,26 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */ } else if (existingTarget && existingTarget != obj) { /* Nuke the target first, using shadowing, - * but only if it isn't the same object + * but only if it isn't the same object. + * + * Note we must disable gc otherwise it can mess up the shadowing. + * */ + dev->gcDisable=1; yaffs_ChangeObjectName(obj, newDir, newName, force, existingTarget->objectId); + existingTarget->isShadowed = 1; yaffs_UnlinkObject(existingTarget); + dev->gcDisable=0; } - return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0); + result = yaffs_ChangeObjectName(obj, newDir, newName, 1, 0); + + yaffs_UpdateParent(oldDir); + if(newDir != oldDir) + yaffs_UpdateParent(newDir); + + return result; } return YAFFS_FAIL; } @@ -2549,7 +2723,7 @@ static int yaffs_InitialiseBlocks(yaffs_Device *dev) if (dev->blockInfo) { /* Set up dynamic blockinfo stuff. */ - dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */ + dev->chunkBitmapStride = (dev->param.nChunksPerBlock + 7) / 8; /* round up bytes */ dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks); if (!dev->chunkBits) { dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks); @@ -2589,33 +2763,14 @@ static void yaffs_DeinitialiseBlocks(yaffs_Device *dev) static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi) { - int i; - __u32 seq; - yaffs_BlockInfo *b; - if (!dev->isYaffs2) + if (!dev->param.isYaffs2) return 1; /* disqualification only applies to yaffs2. */ if (!bi->hasShrinkHeader) return 1; /* can gc */ - /* Find the oldest dirty sequence number if we don't know it and save it - * so we don't have to keep recomputing it. - */ - if (!dev->oldestDirtySequence) { - seq = dev->sequenceNumber; - - for (i = dev->internalStartBlock; i <= dev->internalEndBlock; - i++) { - b = yaffs_GetBlockInfo(dev, i); - if (b->blockState == YAFFS_BLOCK_STATE_FULL && - (b->pagesInUse - b->softDeletions) < - dev->nChunksPerBlock && b->sequenceNumber < seq) { - seq = b->sequenceNumber; - } - } - dev->oldestDirtySequence = seq; - } + yaffs_FindOldestDirtySequence(dev); /* Can't do gc of this block if there are any blocks older than this one that have * discarded pages. @@ -2623,119 +2778,67 @@ static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, return (bi->sequenceNumber <= dev->oldestDirtySequence); } -/* FindDiretiestBlock is used to select the dirtiest block (or close enough) - * for garbage collection. +/* + * yaffs_FindRefreshBlock() + * periodically finds the oldest full block by sequence number for refreshing. + * Only for yaffs2. */ - -static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev, - int aggressive) +static __u32 yaffs_FindRefreshBlock(yaffs_Device *dev) { - int b = dev->currentDirtyChecker; - - int i; - int iterations; - int dirtiest = -1; - int pagesInUse = 0; - int prioritised = 0; - yaffs_BlockInfo *bi; - int pendingPrioritisedExist = 0; - - /* First let's see if we need to grab a prioritised block */ - if (dev->hasPendingPrioritisedGCs) { - for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) { + __u32 b ; - bi = yaffs_GetBlockInfo(dev, i); - /* yaffs_VerifyBlock(dev,bi,i); */ + __u32 oldest = 0; + __u32 oldestSequence = 0; - if (bi->gcPrioritise) { - pendingPrioritisedExist = 1; - if (bi->blockState == YAFFS_BLOCK_STATE_FULL && - yaffs_BlockNotDisqualifiedFromGC(dev, bi)) { - pagesInUse = (bi->pagesInUse - bi->softDeletions); - dirtiest = i; - prioritised = 1; - aggressive = 1; /* Fool the non-aggressive skip logiv below */ - } - } - } - - if (!pendingPrioritisedExist) /* None found, so we can clear this */ - dev->hasPendingPrioritisedGCs = 0; - } + yaffs_BlockInfo *bi; - /* If we're doing aggressive GC then we are happy to take a less-dirty block, and - * search harder. - * else (we're doing a leasurely gc), then we only bother to do this if the - * block has only a few pages in use. + /* + * If refresh period < 10 then refreshing is disabled. */ + if(dev->param.refreshPeriod < 10 || + !dev->param.isYaffs2) + return oldest; - dev->nonAggressiveSkip--; + /* + * Fix broken values. + */ + if(dev->refreshSkip > dev->param.refreshPeriod) + dev->refreshSkip = dev->param.refreshPeriod; - if (!aggressive && (dev->nonAggressiveSkip > 0)) { - return -1; - } + if(dev->refreshSkip > 0) + return oldest; - if (!prioritised) - pagesInUse = - (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1; - - if (aggressive) { - iterations = - dev->internalEndBlock - dev->internalStartBlock + 1; - } else { - iterations = - dev->internalEndBlock - dev->internalStartBlock + 1; - iterations = iterations / 16; - if (iterations > 200) { - iterations = 200; - } - } - - for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) { - b++; - if (b < dev->internalStartBlock || b > dev->internalEndBlock) { - b = dev->internalStartBlock; - } - - if (b < dev->internalStartBlock || b > dev->internalEndBlock) { - T(YAFFS_TRACE_ERROR, - (TSTR("**>> Block %d is not valid" TENDSTR), b)); - YBUG(); - } - - bi = yaffs_GetBlockInfo(dev, b); - -#if 0 - if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) { - dirtiest = b; - pagesInUse = 0; - } else -#endif - - if (bi->blockState == YAFFS_BLOCK_STATE_FULL && - (bi->pagesInUse - bi->softDeletions) < pagesInUse && - yaffs_BlockNotDisqualifiedFromGC(dev, bi)) { - dirtiest = b; - pagesInUse = (bi->pagesInUse - bi->softDeletions); + /* + * Refresh skip is now zero. + * We'll do a refresh this time around.... + * Update the refresh skip and find the oldest block. + */ + dev->refreshSkip = dev->param.refreshPeriod; + dev->refreshCount++; + bi = dev->blockInfo; + for (b = dev->internalStartBlock; b <=dev->internalEndBlock; b++){ + + if (bi->blockState == YAFFS_BLOCK_STATE_FULL){ + + if(oldest < 1 || + bi->sequenceNumber < oldestSequence){ + oldest = b; + oldestSequence = bi->sequenceNumber; + } } + bi++; } - dev->currentDirtyChecker = b; - - if (dirtiest > 0) { + if (oldest > 0) { T(YAFFS_TRACE_GC, - (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest, - dev->nChunksPerBlock - pagesInUse, prioritised)); + (TSTR("GC refresh count %d selected block %d with sequenceNumber %d" TENDSTR), + dev->refreshCount, oldest, oldestSequence)); } - dev->oldestDirtySequence = 0; - - if (dirtiest > 0) - dev->nonAggressiveSkip = 4; - - return dirtiest; + return oldest; } + static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo) { yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo); @@ -2750,8 +2853,20 @@ static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo) (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR), blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : "")); + yaffs_ClearOldestDirtySequence(dev,bi); + bi->blockState = YAFFS_BLOCK_STATE_DIRTY; + /* If this is the block being garbage collected then stop gc'ing this block */ + if(blockNo == dev->gcBlock) + dev->gcBlock = 0; + + /* If this block is currently the best candidate for gc then drop as a candidate */ + if(blockNo == dev->gcDirtiest){ + dev->gcDirtiest = 0; + dev->gcPagesInUse = 0; + } + if (!bi->needsRetiring) { yaffs_InvalidateCheckpoint(dev); erasedOk = yaffs_EraseBlockInNAND(dev, blockNo); @@ -2765,9 +2880,9 @@ static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo) if (erasedOk && ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) { int i; - for (i = 0; i < dev->nChunksPerBlock; i++) { + for (i = 0; i < dev->param.nChunksPerBlock; i++) { if (!yaffs_CheckChunkErased - (dev, blockNo * dev->nChunksPerBlock + i)) { + (dev, blockNo * dev->param.nChunksPerBlock + i)) { T(YAFFS_TRACE_ERROR, (TSTR (">>Block %d erasure supposedly OK, but chunk %d not erased" @@ -2779,6 +2894,7 @@ static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo) if (erasedOk) { /* Clean it up... */ bi->blockState = YAFFS_BLOCK_STATE_EMPTY; + bi->sequenceNumber = 0; dev->nErasedBlocks++; bi->pagesInUse = 0; bi->softDeletions = 0; @@ -2790,7 +2906,7 @@ static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo) T(YAFFS_TRACE_ERASE, (TSTR("Erased block %d" TENDSTR), blockNo)); } else { - dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */ + dev->nFreeChunks -= dev->param.nChunksPerBlock; /* We lost a block of free space */ yaffs_RetireBlock(dev, blockNo); T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, @@ -2848,20 +2964,22 @@ static int yaffs_FindBlockForAllocation(yaffs_Device *dev) +static int yaffs_CheckpointRequired(yaffs_Device *dev) +{ + int nblocks = dev->internalEndBlock - dev->internalStartBlock + 1 ; + return dev->param.isYaffs2 && + !dev->param.skipCheckpointWrite && + (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS); +} static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev) { if (!dev->nCheckpointBlocksRequired && - dev->isYaffs2) { + yaffs_CheckpointRequired(dev)){ /* Not a valid value so recalculate */ int nBytes = 0; int nBlocks; - int devBlocks = (dev->endBlock - dev->startBlock + 1); - int tnodeSize; - - tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; - - if (tnodeSize < sizeof(yaffs_Tnode)) - tnodeSize = sizeof(yaffs_Tnode); + int devBlocks = (dev->param.endBlock - dev->param.startBlock + 1); + int tnodeSize = yaffs_CalcTnodeSize(dev); nBytes += sizeof(yaffs_CheckpointValidity); nBytes += sizeof(yaffs_CheckpointDevice); @@ -2874,7 +2992,7 @@ static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev) /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */ - nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->nChunksPerBlock)) + 3; + nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->param.nChunksPerBlock)) + 3; dev->nCheckpointBlocksRequired = nBlocks; } @@ -2886,13 +3004,13 @@ static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev) * Check if there's space to allocate... * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()? */ -static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev) +static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev, int nChunks) { int reservedChunks; - int reservedBlocks = dev->nReservedBlocks; + int reservedBlocks = dev->param.nReservedBlocks; int checkpointBlocks; - if (dev->isYaffs2) { + if (dev->param.isYaffs2) { checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint; if (checkpointBlocks < 0) @@ -2901,9 +3019,9 @@ static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev) checkpointBlocks = 0; } - reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock); + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->param.nChunksPerBlock); - return (dev->nFreeChunks > reservedChunks); + return (dev->nFreeChunks > (reservedChunks + nChunks)); } static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve, @@ -2918,12 +3036,12 @@ static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve, dev->allocationPage = 0; } - if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) { + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev, 1)) { /* Not enough space to allocate unless we're allowed to use the reserve. */ return -1; } - if (dev->nErasedBlocks < dev->nReservedBlocks + if (dev->nErasedBlocks < dev->param.nReservedBlocks && dev->allocationPage == 0) { T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR))); } @@ -2932,7 +3050,7 @@ static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve, if (dev->allocationBlock >= 0) { bi = yaffs_GetBlockInfo(dev, dev->allocationBlock); - retVal = (dev->allocationBlock * dev->nChunksPerBlock) + + retVal = (dev->allocationBlock * dev->param.nChunksPerBlock) + dev->allocationPage; bi->pagesInUse++; yaffs_SetChunkBit(dev, dev->allocationBlock, @@ -2943,7 +3061,7 @@ static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve, dev->nFreeChunks--; /* If the block is full set the state to full */ - if (dev->allocationPage >= dev->nChunksPerBlock) { + if (dev->allocationPage >= dev->param.nChunksPerBlock) { bi->blockState = YAFFS_BLOCK_STATE_FULL; dev->allocationBlock = -1; } @@ -2964,16 +3082,31 @@ static int yaffs_GetErasedChunks(yaffs_Device *dev) { int n; - n = dev->nErasedBlocks * dev->nChunksPerBlock; + n = dev->nErasedBlocks * dev->param.nChunksPerBlock; - if (dev->allocationBlock > 0) { - n += (dev->nChunksPerBlock - dev->allocationPage); - } + if (dev->allocationBlock > 0) + n += (dev->param.nChunksPerBlock - dev->allocationPage); return n; } +/* + * yaffs_SkipRestOfBlock() skips over the rest of the allocation block + * if we don't want to write to it. + */ +static void yaffs_SkipRestOfBlock(yaffs_Device *dev) +{ + if(dev->allocationBlock > 0){ + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->allocationBlock); + if(bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING){ + bi->blockState = YAFFS_BLOCK_STATE_FULL; + dev->allocationBlock = -1; + } + } +} + + static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, int wholeBlock) { @@ -2998,7 +3131,6 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT); - bi->blockState = YAFFS_BLOCK_STATE_COLLECTING; T(YAFFS_TRACE_TRACING, (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR), @@ -3009,14 +3141,12 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, /*yaffs_VerifyFreeChunks(dev); */ + if(bi->blockState == YAFFS_BLOCK_STATE_FULL) + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING; + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */ - /* Take off the number of soft deleted entries because - * they're going to get really deleted during GC. - */ - dev->nFreeChunks -= bi->softDeletions; - - dev->isDoingGC = 1; + dev->gcDisable = 1; if (isCheckpointBlock || !yaffs_StillSomeChunkBits(dev, block)) { @@ -3031,12 +3161,12 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, yaffs_VerifyBlock(dev, bi, block); - maxCopies = (wholeBlock) ? dev->nChunksPerBlock : 10; - oldChunk = block * dev->nChunksPerBlock + dev->gcChunk; + maxCopies = (wholeBlock) ? dev->param.nChunksPerBlock : 5; + oldChunk = block * dev->param.nChunksPerBlock + dev->gcChunk; for (/* init already done */; retVal == YAFFS_OK && - dev->gcChunk < dev->nChunksPerBlock && + dev->gcChunk < dev->param.nChunksPerBlock && (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) && maxCopies > 0; dev->gcChunk++, oldChunk++) { @@ -3095,6 +3225,13 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, * No need to copy this, just forget about it and * fix up the object. */ + + /* Free chunks already includes softdeleted chunks. + * How ever this chunk is going to soon be really deleted + * which will increment free chunks. + * We have to decrement free chunks so this works out properly. + */ + dev->nFreeChunks--; object->nDataChunks--; @@ -3130,20 +3267,33 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, if (tags.chunkId == 0) { /* It is an object Id, * We need to nuke the shrinkheader flags first + * Also need to clean up shadowing. * We no longer want the shrinkHeader flag since its work is done * and if it is left in place it will mess up scanning. */ yaffs_ObjectHeader *oh; oh = (yaffs_ObjectHeader *)buffer; + oh->isShrink = 0; tags.extraIsShrinkHeader = 0; - yaffs_VerifyObjectHeader(object, oh, &tags, 1); - } + oh->shadowsObject = 0; + oh->inbandShadowsObject = 0; + tags.extraShadows = 0; - newChunk = - yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1); + /* Update file size */ + if(object->variantType == YAFFS_OBJECT_TYPE_FILE){ + oh->fileSize = object->variant.fileVariant.fileSize; + tags.extraFileLength = oh->fileSize; + } + + yaffs_VerifyObjectHeader(object, oh, &tags, 1); + newChunk = + yaffs_WriteNewChunkWithTagsToNAND(dev,(__u8 *) oh, &tags, 1); + } else + newChunk = + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1); if (newChunk < 0) { retVal = YAFFS_FAIL; @@ -3157,7 +3307,8 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, object->serial = tags.serialNumber; } else { /* It's a data chunk */ - yaffs_PutChunkIntoFile + int ok; + ok = yaffs_PutChunkIntoFile (object, tags.chunkId, newChunk, 0); @@ -3199,25 +3350,182 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, yaffs_VerifyCollectedBlock(dev, bi, block); - chunksAfter = yaffs_GetErasedChunks(dev); - if (chunksBefore >= chunksAfter) { - T(YAFFS_TRACE_GC, - (TSTR - ("gc did not increase free chunks before %d after %d" - TENDSTR), chunksBefore, chunksAfter)); - } - /* If the gc completed then clear the current gcBlock so that we find another. */ - if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) { - dev->gcBlock = -1; + + if (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) { + /* + * The gc did not complete. Set block state back to FULL + * because checkpointing does not restore gc. + */ + bi->blockState = YAFFS_BLOCK_STATE_FULL; + } else { + /* The gc completed. */ + chunksAfter = yaffs_GetErasedChunks(dev); + if (chunksBefore >= chunksAfter) { + T(YAFFS_TRACE_GC, + (TSTR + ("gc did not increase free chunks before %d after %d" + TENDSTR), chunksBefore, chunksAfter)); + } + dev->gcBlock = 0; dev->gcChunk = 0; } - dev->isDoingGC = 0; + dev->gcDisable = 0; return retVal; } +/* + * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough) + * for garbage collection. + */ + +static unsigned yaffs_FindBlockForGarbageCollection(yaffs_Device *dev, + int aggressive, + int background) +{ + int i; + int iterations; + unsigned selected = 0; + int prioritised = 0; + int prioritisedExists = 0; + yaffs_BlockInfo *bi; + int threshold; + + /* First let's see if we need to grab a prioritised block */ + if (dev->hasPendingPrioritisedGCs && !aggressive) { + dev->gcDirtiest = 0; + bi = dev->blockInfo; + for (i = dev->internalStartBlock; + i <= dev->internalEndBlock && !selected; + i++) { + + if (bi->gcPrioritise) { + prioritisedExists = 1; + if (bi->blockState == YAFFS_BLOCK_STATE_FULL && + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) { + selected = i; + prioritised = 1; + } + } + bi++; + } + + /* + * If there is a prioritised block and none was selected then + * this happened because there is at least one old dirty block gumming + * up the works. Let's gc the oldest dirty block. + */ + + if(prioritisedExists && + !selected && + dev->oldestDirtyBlock > 0) + selected = dev->oldestDirtyBlock; + + if (!prioritisedExists) /* None found, so we can clear this */ + dev->hasPendingPrioritisedGCs = 0; + } + + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and + * search harder. + * else (we're doing a leasurely gc), then we only bother to do this if the + * block has only a few pages in use. + */ + + if (!selected){ + int pagesUsed; + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1; + if (aggressive){ + threshold = dev->param.nChunksPerBlock; + iterations = nBlocks; + } else { + int maxThreshold = dev->param.nChunksPerBlock/2; + threshold = background ? + (dev->gcNotDone + 2) * 2 : 0; + if(threshold maxThreshold) + threshold = maxThreshold; + + iterations = nBlocks / 16 + 1; + if (iterations > 100) + iterations = 100; + } + + for (i = 0; + i < iterations && + (dev->gcDirtiest < 1 || + dev->gcPagesInUse > YAFFS_GC_GOOD_ENOUGH); + i++) { + dev->gcBlockFinder++; + if (dev->gcBlockFinder < dev->internalStartBlock || + dev->gcBlockFinder > dev->internalEndBlock) + dev->gcBlockFinder = dev->internalStartBlock; + + bi = yaffs_GetBlockInfo(dev, dev->gcBlockFinder); + + pagesUsed = bi->pagesInUse - bi->softDeletions; + + if (bi->blockState == YAFFS_BLOCK_STATE_FULL && + pagesUsed < dev->param.nChunksPerBlock && + (dev->gcDirtiest < 1 || pagesUsed < dev->gcPagesInUse) && + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) { + dev->gcDirtiest = dev->gcBlockFinder; + dev->gcPagesInUse = pagesUsed; + } + } + + if(dev->gcDirtiest > 0 && dev->gcPagesInUse <= threshold) + selected = dev->gcDirtiest; + } + + /* + * If nothing has been selected for a while, try selecting the oldest dirty + * because that's gumming up the works. + */ + + if(!selected && dev->param.isYaffs2 && + dev->gcNotDone >= ( background ? 10 : 20)){ + yaffs_FindOldestDirtySequence(dev); + if(dev->oldestDirtyBlock > 0) { + selected = dev->oldestDirtyBlock; + dev->gcDirtiest = selected; + dev->oldestDirtyGCs++; + bi = yaffs_GetBlockInfo(dev, selected); + dev->gcPagesInUse = bi->pagesInUse - bi->softDeletions; + } else + dev->gcNotDone = 0; + } + + if(selected){ + T(YAFFS_TRACE_GC, + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), + selected, + dev->param.nChunksPerBlock - dev->gcPagesInUse, + prioritised)); + + if(background) + dev->backgroundGCs++; + dev->gcDirtiest = 0; + dev->gcPagesInUse = 0; + dev->gcNotDone = 0; + if(dev->refreshSkip > 0) + dev->refreshSkip--; + } else{ + dev->gcNotDone++; + T(YAFFS_TRACE_GC, + (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR), + dev->gcBlockFinder, dev->gcNotDone, + threshold, + dev->gcDirtiest, dev->gcPagesInUse, + dev->oldestDirtyBlock, + background ? " bg" : "")); + } + + return selected; +} + /* New garbage collector * If we're very low on erased blocks then we do aggressive garbage collection * otherwise we do "leasurely" garbage collection. @@ -3227,22 +3535,28 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block, * The idea is to help clear out space in a more spread-out manner. * Dunno if it really does anything useful. */ -static int yaffs_CheckGarbageCollection(yaffs_Device *dev) +static int yaffs_CheckGarbageCollection(yaffs_Device *dev, int background) { - int block; - int aggressive; + int aggressive = 0; int gcOk = YAFFS_OK; int maxTries = 0; + int minErased; + int erasedChunks; + int checkpointBlockAdjust; - if (dev->isDoingGC) { + if(dev->param.gcControl && + (dev->param.gcControl(dev) & 1) == 0) + return YAFFS_OK; + + if (dev->gcDisable) { /* Bail out so we don't get recursive gc */ return YAFFS_OK; } /* This loop should pass the first time. - * We'll only see looping here if the erase of the collected block fails. + * We'll only see looping here if the collection does not increase space. */ do { @@ -3252,48 +3566,79 @@ static int yaffs_CheckGarbageCollection(yaffs_Device *dev) if (checkpointBlockAdjust < 0) checkpointBlockAdjust = 0; - if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) { - /* We need a block soon...*/ + minErased = dev->param.nReservedBlocks + checkpointBlockAdjust + 1; + erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock; + + /* If we need a block soon then do aggressive gc.*/ + if (dev->nErasedBlocks < minErased) aggressive = 1; - } else { - /* We're in no hurry */ - aggressive = 0; + else { + if(dev->gcSkip > 20) + dev->gcSkip = 20; + if(erasedChunks < dev->nFreeChunks/2 || + dev->gcSkip < 1 || + background) + aggressive = 0; + else { + dev->gcSkip--; + break; + } } - if (dev->gcBlock <= 0) { - dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive); + dev->gcSkip = 5; + + /* If we don't already have a block being gc'd then see if we should start another */ + + if (dev->gcBlock < 1 && !aggressive) { + dev->gcBlock = yaffs_FindRefreshBlock(dev); + dev->gcChunk = 0; + } + if (dev->gcBlock < 1) { + dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive, background); dev->gcChunk = 0; } - block = dev->gcBlock; - - if (block > 0) { - dev->garbageCollections++; - if (!aggressive) { - dev->passiveGarbageCollections++; - } + if (dev->gcBlock > 0) { + dev->allGCs++; + if (!aggressive) + dev->passiveGCs++; T(YAFFS_TRACE_GC, (TSTR ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR), dev->nErasedBlocks, aggressive)); - gcOk = yaffs_GarbageCollectBlock(dev, block, aggressive); + gcOk = yaffs_GarbageCollectBlock(dev, dev->gcBlock, aggressive); } - if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) { + if (dev->nErasedBlocks < (dev->param.nReservedBlocks) && dev->gcBlock > 0) { T(YAFFS_TRACE_GC, (TSTR ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" - TENDSTR), dev->nErasedBlocks, maxTries, block)); + TENDSTR), dev->nErasedBlocks, maxTries, dev->gcBlock)); } - } while ((dev->nErasedBlocks < dev->nReservedBlocks) && - (block > 0) && + } while ((dev->nErasedBlocks < dev->param.nReservedBlocks) && + (dev->gcBlock > 0) && (maxTries < 2)); return aggressive ? gcOk : YAFFS_OK; } +/* + * yaffs_BackgroundGarbageCollect() + * Garbage collects. Intended to be called from a background thread. + * Returns non-zero if at least half the free chunks are erased. + */ +int yaffs_BackgroundGarbageCollect(yaffs_Device *dev, unsigned urgency) +{ + int erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock; + + T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency)); + + yaffs_CheckGarbageCollection(dev, 1); + return erasedChunks > dev->nFreeChunks/2; +} + /*------------------------- TAGS --------------------------------*/ static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, @@ -3362,16 +3707,10 @@ static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode, chunkInInode); /* Delete the entry in the filestructure (if found) */ - if (retVal != -1) { - yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0); - } - } else { - /*T(("No level 0 found for %d\n", chunkInInode)); */ + if (retVal != -1) + yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, 0); } - if (retVal == -1) { - /* T(("Could not find %d to delete\n",chunkInInode)); */ - } return retVal; } @@ -3390,10 +3729,8 @@ static int yaffs_CheckFileSanity(yaffs_Object *in) int theChunk; int chunkDeleted; - if (in->variantType != YAFFS_OBJECT_TYPE_FILE) { - /* T(("Object not a file\n")); */ + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) return YAFFS_FAIL; - } objId = in->objectId; fSize = in->variant.fileVariant.fileSize; @@ -3409,8 +3746,8 @@ static int yaffs_CheckFileSanity(yaffs_Object *in) theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk); if (yaffs_CheckChunkBits - (dev, theChunk / dev->nChunksPerBlock, - theChunk % dev->nChunksPerBlock)) { + (dev, theChunk / dev->param.nChunksPerBlock, + theChunk % dev->param.nChunksPerBlock)) { yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk, tags, @@ -3441,6 +3778,8 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode, /* NB inScan is zero unless scanning. * For forward scanning, inScan is > 0; * for backward scanning inScan is < 0 + * + * chunkInNAND = 0 is a dummy insert to make sure the tnodes are there. */ yaffs_Tnode *tn; @@ -3470,9 +3809,12 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode, &in->variant.fileVariant, chunkInInode, NULL); - if (!tn) { + if (!tn) return YAFFS_FAIL; - } + + if(!chunkInNAND) + /* Dummy insert, bail now */ + return YAFFS_OK; existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode); @@ -3529,7 +3871,7 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode, } if ((inScan > 0) && - (in->myDev->isYaffs2 || + (in->myDev->param.isYaffs2 || existingChunk <= 0 || ((existingSerial + 1) & 3) == newSerial)) { /* Forward scanning. @@ -3551,11 +3893,10 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode, } - if (existingChunk == 0) { + if (existingChunk == 0) in->nDataChunks++; - } - yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND); + yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, chunkInNAND); return YAFFS_OK; } @@ -3565,10 +3906,10 @@ static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode, { int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL); - if (chunkInNAND >= 0) { + if (chunkInNAND >= 0) return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND, buffer, NULL); - } else { + else { T(YAFFS_TRACE_NANDACCESS, (TSTR("Chunk %d not found zero instead" TENDSTR), chunkInNAND)); @@ -3589,10 +3930,9 @@ void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn) if (chunkId <= 0) return; - dev->nDeletions++; - block = chunkId / dev->nChunksPerBlock; - page = chunkId % dev->nChunksPerBlock; + block = chunkId / dev->param.nChunksPerBlock; + page = chunkId % dev->param.nChunksPerBlock; if (!yaffs_CheckChunkBit(dev, block, page)) @@ -3601,12 +3941,14 @@ void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn) chunkId)); bi = yaffs_GetBlockInfo(dev, block); + + yaffs_UpdateOldestDirtySequence(dev, block, bi); T(YAFFS_TRACE_DELETION, (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId)); if (markNAND && - bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) { + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->param.isYaffs2) { yaffs_InitialiseTags(&tags); @@ -3638,8 +3980,6 @@ void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn) yaffs_BlockBecameDirty(dev, block); } - } else { - /* T(("Bad news deleting chunk %d\n",chunkId)); */ } } @@ -3661,10 +4001,16 @@ static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode, yaffs_Device *dev = in->myDev; - yaffs_CheckGarbageCollection(dev); + yaffs_CheckGarbageCollection(dev,0); - /* Get the previous chunk at this location in the file if it exists */ + /* Get the previous chunk at this location in the file if it exists. + * If it does not exist then put a zero into the tree. This creates + * the tnode now, rather than later when it is harder to clean up. + */ prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags); + if(prevChunkId < 1 && + !yaffs_PutChunkIntoFile(in, chunkInInode, 0, 0)) + return 0; /* Set up new tags */ yaffs_InitialiseTags(&newTags); @@ -3672,27 +4018,26 @@ static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode, newTags.chunkId = chunkInInode; newTags.objectId = in->objectId; newTags.serialNumber = - (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1; + (prevChunkId > 0) ? prevTags.serialNumber + 1 : 1; newTags.byteCount = nBytes; - if (nBytes < 1 || nBytes > dev->totalBytesPerChunk) { - T(YAFFS_TRACE_ERROR, - (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes)); - YBUG(); + if (nBytes < 1 || nBytes > dev->param.totalBytesPerChunk) { + T(YAFFS_TRACE_ERROR, + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes)); + YBUG(); } - + + newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags, useReserve); - if (newChunkId >= 0) { + if (newChunkId > 0) { yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0); - if (prevChunkId >= 0) { + if (prevChunkId > 0) yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__); - } - yaffs_CheckFileSanity(in); } return newChunkId; @@ -3717,6 +4062,7 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, int newChunkId; yaffs_ExtendedTags newTags; yaffs_ExtendedTags oldTags; + YCHAR *alias = NULL; __u8 *buffer = NULL; YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1]; @@ -3730,7 +4076,7 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, in == dev->rootDir || /* The rootDir should also be saved */ force) { - yaffs_CheckGarbageCollection(dev); + yaffs_CheckGarbageCollection(dev,0); yaffs_CheckObjectDetailsLoaded(in); buffer = yaffs_GetTempBuffer(in->myDev, __LINE__); @@ -3768,20 +4114,18 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, oh->yst_ctime = in->yst_ctime; oh->yst_rdev = in->yst_rdev; #endif - if (in->parent) { + if (in->parent) oh->parentObjectId = in->parent->objectId; - } else { + else oh->parentObjectId = 0; - } if (name && *name) { memset(oh->name, 0, sizeof(oh->name)); yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH); - } else if (prevChunkId >= 0) { + } else if (prevChunkId > 0) memcpy(oh->name, oldName, sizeof(oh->name)); - } else { + else memset(oh->name, 0, sizeof(oh->name)); - } oh->isShrink = isShrink; @@ -3807,8 +4151,11 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, /* Do nothing */ break; case YAFFS_OBJECT_TYPE_SYMLINK: + alias = in->variant.symLinkVariant.alias; + if(!alias) + alias = _Y("no alias"); yaffs_strncpy(oh->alias, - in->variant.symLinkVariant.alias, + alias, YAFFS_MAX_ALIAS_LENGTH); oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0; break; @@ -3836,13 +4183,13 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, /* Create new chunk in NAND */ newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags, - (prevChunkId >= 0) ? 1 : 0); + (prevChunkId > 0) ? 1 : 0); if (newChunkId >= 0) { in->hdrChunk = newChunkId; - if (prevChunkId >= 0) { + if (prevChunkId > 0) { yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__); } @@ -3853,7 +4200,7 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force, /* If this was a shrink, then mark the block that the chunk lives on */ if (isShrink) { bi = yaffs_GetBlockInfo(in->myDev, - newChunkId / in->myDev->nChunksPerBlock); + newChunkId / in->myDev->param.nChunksPerBlock); bi->hasShrinkHeader = 1; } @@ -3887,7 +4234,7 @@ static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj) yaffs_Device *dev = obj->myDev; int i; yaffs_ChunkCache *cache; - int nCaches = obj->myDev->nShortOpCaches; + int nCaches = obj->myDev->param.nShortOpCaches; for (i = 0; i < nCaches; i++) { cache = &dev->srCache[i]; @@ -3907,7 +4254,7 @@ static void yaffs_FlushFilesChunkCache(yaffs_Object *obj) int i; yaffs_ChunkCache *cache; int chunkWritten = 0; - int nCaches = obj->myDev->nShortOpCaches; + int nCaches = obj->myDev->param.nShortOpCaches; if (nCaches > 0) { do { @@ -3959,7 +4306,7 @@ static void yaffs_FlushFilesChunkCache(yaffs_Object *obj) void yaffs_FlushEntireDeviceCache(yaffs_Device *dev) { yaffs_Object *obj; - int nCaches = dev->nShortOpCaches; + int nCaches = dev->param.nShortOpCaches; int i; /* Find a dirty object in the cache and flush it... @@ -3990,8 +4337,8 @@ static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev) { int i; - if (dev->nShortOpCaches > 0) { - for (i = 0; i < dev->nShortOpCaches; i++) { + if (dev->param.nShortOpCaches > 0) { + for (i = 0; i < dev->param.nShortOpCaches; i++) { if (!dev->srCache[i].object) return &dev->srCache[i]; } @@ -4008,7 +4355,7 @@ static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev) int i; int pushout; - if (dev->nShortOpCaches > 0) { + if (dev->param.nShortOpCaches > 0) { /* Try find a non-dirty one... */ cache = yaffs_GrabChunkCacheWorker(dev); @@ -4027,7 +4374,7 @@ static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev) cache = NULL; pushout = -1; - for (i = 0; i < dev->nShortOpCaches; i++) { + for (i = 0; i < dev->param.nShortOpCaches; i++) { if (dev->srCache[i].object && !dev->srCache[i].locked && (dev->srCache[i].lastUse < usage || !cache)) { @@ -4057,8 +4404,8 @@ static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, { yaffs_Device *dev = obj->myDev; int i; - if (dev->nShortOpCaches > 0) { - for (i = 0; i < dev->nShortOpCaches; i++) { + if (dev->param.nShortOpCaches > 0) { + for (i = 0; i < dev->param.nShortOpCaches; i++) { if (dev->srCache[i].object == obj && dev->srCache[i].chunkId == chunkId) { dev->cacheHits++; @@ -4075,13 +4422,13 @@ static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite) { - if (dev->nShortOpCaches > 0) { + if (dev->param.nShortOpCaches > 0) { if (dev->srLastUse < 0 || dev->srLastUse > 100000000) { /* Reset the cache usages */ int i; - for (i = 1; i < dev->nShortOpCaches; i++) { + for (i = 1; i < dev->param.nShortOpCaches; i++) dev->srCache[i].lastUse = 0; - } + dev->srLastUse = 0; } @@ -4089,9 +4436,8 @@ static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, cache->lastUse = dev->srLastUse; - if (isAWrite) { + if (isAWrite) cache->dirty = 1; - } } } @@ -4101,12 +4447,11 @@ static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, */ static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId) { - if (object->myDev->nShortOpCaches > 0) { + if (object->myDev->param.nShortOpCaches > 0) { yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId); - if (cache) { + if (cache) cache->object = NULL; - } } } @@ -4118,12 +4463,11 @@ static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in) int i; yaffs_Device *dev = in->myDev; - if (dev->nShortOpCaches > 0) { + if (dev->param.nShortOpCaches > 0) { /* Invalidate it. */ - for (i = 0; i < dev->nShortOpCaches; i++) { - if (dev->srCache[i].object == in) { + for (i = 0; i < dev->param.nShortOpCaches; i++) { + if (dev->srCache[i].object == in) dev->srCache[i].object = NULL; - } } } } @@ -4173,7 +4517,6 @@ static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp, cp->nUnlinkedFiles = dev->nUnlinkedFiles; cp->nBackgroundDeletions = dev->nBackgroundDeletions; cp->sequenceNumber = dev->sequenceNumber; - cp->oldestDirtySequence = dev->oldestDirtySequence; } @@ -4189,7 +4532,6 @@ static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev, dev->nUnlinkedFiles = cp->nUnlinkedFiles; dev->nBackgroundDeletions = cp->nBackgroundDeletions; dev->sequenceNumber = cp->sequenceNumber; - dev->oldestDirtySequence = cp->oldestDirtySequence; } @@ -4340,11 +4682,7 @@ static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn, int i; yaffs_Device *dev = in->myDev; int ok = 1; - int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; - - if (tnodeSize < sizeof(yaffs_Tnode)) - tnodeSize = sizeof(yaffs_Tnode); - + int tnodeSize = yaffs_CalcTnodeSize(dev); if (tn) { if (level > 0) { @@ -4395,10 +4733,7 @@ static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj) yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant; yaffs_Tnode *tn; int nread = 0; - int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8; - - if (tnodeSize < sizeof(yaffs_Tnode)) - tnodeSize = sizeof(yaffs_Tnode); + int tnodeSize = yaffs_CalcTnodeSize(dev); ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk)); @@ -4454,14 +4789,13 @@ static int yaffs_WriteCheckpointObjects(yaffs_Device *dev) cp.structType = sizeof(cp); T(YAFFS_TRACE_CHECKPOINT, ( - TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR), - cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, (unsigned) obj)); + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" TENDSTR), + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, obj)); ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)); - if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE) { + if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE) ok = yaffs_WriteCheckpointTnodes(obj); - } } } } @@ -4489,7 +4823,7 @@ static int yaffs_ReadCheckpointObjects(yaffs_Device *dev) ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp)); if (cp.structType != sizeof(cp)) { T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR), - cp.structType, sizeof(cp), ok)); + cp.structType, (int)sizeof(cp), ok)); ok = 0; } @@ -4561,7 +4895,7 @@ static int yaffs_WriteCheckpointData(yaffs_Device *dev) { int ok = 1; - if (dev->skipCheckpointWrite || !dev->isYaffs2) { + if (!yaffs_CheckpointRequired(dev)) { T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR))); ok = 0; } @@ -4586,10 +4920,8 @@ static int yaffs_WriteCheckpointData(yaffs_Device *dev) ok = yaffs_WriteCheckpointValidityMarker(dev, 0); } - if (ok) { + if (ok) ok = yaffs_WriteCheckpointSum(dev); - } - if (!yaffs_CheckpointClose(dev)) ok = 0; @@ -4606,7 +4938,7 @@ static int yaffs_ReadCheckpointData(yaffs_Device *dev) { int ok = 1; - if (dev->skipCheckpointRead || !dev->isYaffs2) { + if (dev->param.skipCheckpointRead || !dev->param.isYaffs2) { T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR))); ok = 0; } @@ -4654,9 +4986,9 @@ static void yaffs_InvalidateCheckpoint(yaffs_Device *dev) dev->blocksInCheckpoint > 0) { dev->isCheckpointed = 0; yaffs_CheckpointInvalidateStream(dev); - if (dev->superBlock && dev->markSuperBlockDirty) - dev->markSuperBlockDirty(dev->superBlock); } + if (dev->param.markSuperBlockDirty) + dev->param.markSuperBlockDirty(dev); } @@ -4731,11 +5063,10 @@ int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset, /* OK now check for the curveball where the start and end are in * the same chunk. */ - if ((start + n) < dev->nDataBytesPerChunk) { + if ((start + n) < dev->nDataBytesPerChunk) nToCopy = n; - } else { + else nToCopy = dev->nDataBytesPerChunk - start; - } cache = yaffs_FindChunkCache(in, chunk); @@ -4743,8 +5074,8 @@ int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset, * or we're using inband tags then use the cache (if there is caching) * else bypass the cache. */ - if (cache || nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) { - if (dev->nShortOpCaches > 0) { + if (cache || nToCopy != dev->nDataBytesPerChunk || dev->param.inbandTags) { + if (dev->param.nShortOpCaches > 0) { /* If we can't find the data in the cache, then load it up. */ @@ -4800,7 +5131,7 @@ int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset, return nDone; } -int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, +int yaffs_DoWriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, int nBytes, int writeThrough) { @@ -4820,8 +5151,6 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, dev = in->myDev; while (n > 0 && chunkWritten >= 0) { - /* chunk = offset / dev->nDataBytesPerChunk + 1; */ - /* start = offset % dev->nDataBytesPerChunk; */ yaffs_AddrToChunk(dev, offset, &chunk, &start); if (chunk * dev->nDataBytesPerChunk + start != offset || @@ -4831,7 +5160,7 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, TENDSTR), (int)offset, chunk, start)); } - chunk++; + chunk++; /* File pos to chunk in file offset */ /* OK now check for the curveball where the start and end are in * the same chunk. @@ -4852,9 +5181,8 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, else nBytesRead = in->variant.fileVariant.fileSize - chunkStart; - if (nBytesRead > dev->nDataBytesPerChunk) { + if (nBytesRead > dev->nDataBytesPerChunk) nBytesRead = dev->nDataBytesPerChunk; - } nToWriteBack = (nBytesRead > @@ -4868,29 +5196,27 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, nToWriteBack = dev->nDataBytesPerChunk; } - if (nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) { + if (nToCopy != dev->nDataBytesPerChunk || dev->param.inbandTags) { /* An incomplete start or end chunk (or maybe both start and end chunk), * or we're using inband tags, so we want to use the cache buffers. */ - if (dev->nShortOpCaches > 0) { + if (dev->param.nShortOpCaches > 0) { yaffs_ChunkCache *cache; /* If we can't find the data in the cache, then load the cache */ cache = yaffs_FindChunkCache(in, chunk); if (!cache - && yaffs_CheckSpaceForAllocation(in-> - myDev)) { - cache = yaffs_GrabChunkCache(in->myDev); + && yaffs_CheckSpaceForAllocation(dev, 1)) { + cache = yaffs_GrabChunkCache(dev); cache->object = in; cache->chunkId = chunk; cache->dirty = 0; cache->locked = 0; yaffs_ReadChunkDataFromObject(in, chunk, - cache-> - data); + cache->data); } else if (cache && !cache->dirty && - !yaffs_CheckSpaceForAllocation(in->myDev)) { + !yaffs_CheckSpaceForAllocation(dev, 1)) { /* Drop the cache if it was a read cache item and * no space check has been made for it. */ @@ -4973,15 +5299,22 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, /* Update file object */ - if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) { + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) in->variant.fileVariant.fileSize = (startOfWrite + nDone); - } in->dirty = 1; return nDone; } +int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset, + int nBytes, int writeThrough) +{ + yaffs_HandleHole(in,offset); + return yaffs_DoWriteDataToFile(in,buffer,offset,nBytes,writeThrough); +} + + /* ---------------------- File resizing stuff ------------------ */ @@ -5010,10 +5343,10 @@ static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize) chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL); if (chunkId > 0) { if (chunkId < - (dev->internalStartBlock * dev->nChunksPerBlock) + (dev->internalStartBlock * dev->param.nChunksPerBlock) || chunkId >= ((dev->internalEndBlock + - 1) * dev->nChunksPerBlock)) { + 1) * dev->param.nChunksPerBlock)) { T(YAFFS_TRACE_ALWAYS, (TSTR("Found daft chunkId %d for %d" TENDSTR), chunkId, i)); @@ -5026,84 +5359,175 @@ static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize) } -int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize) -{ - int oldFileSize = in->variant.fileVariant.fileSize; - __u32 newSizeOfPartialChunk; +static void yaffs_ResizeDown( yaffs_Object *obj, loff_t newSize) +{ int newFullChunks; - - yaffs_Device *dev = in->myDev; + __u32 newSizeOfPartialChunk; + yaffs_Device *dev = obj->myDev; yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk); - yaffs_FlushFilesChunkCache(in); - yaffs_InvalidateWholeChunkCache(in); + yaffs_PruneResizedChunks(obj, newSize); - yaffs_CheckGarbageCollection(dev); + if (newSizeOfPartialChunk != 0) { + int lastChunk = 1 + newFullChunks; + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__); - if (in->variantType != YAFFS_OBJECT_TYPE_FILE) { - return YAFFS_FAIL; - } + /* Got to read and rewrite the last chunk with its new size and zero pad */ + yaffs_ReadChunkDataFromObject(obj, lastChunk, localBuffer); + memset(localBuffer + newSizeOfPartialChunk, 0, + dev->nDataBytesPerChunk - newSizeOfPartialChunk); - if (newSize == oldFileSize) { - return YAFFS_OK; + yaffs_WriteChunkDataToObject(obj, lastChunk, localBuffer, + newSizeOfPartialChunk, 1); + + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__); } - if (newSize < oldFileSize) { + obj->variant.fileVariant.fileSize = newSize; + + yaffs_PruneFileStructure(dev, &obj->variant.fileVariant); +} + + +static int yaffs_HandleHole(yaffs_Object *obj, loff_t newSize) +{ + /* if newsSize > oldFileSize. + * We're going to be writing a hole. + * If the hole is small then write zeros otherwise write a start of hole marker. + */ + - yaffs_PruneResizedChunks(in, newSize); + loff_t oldFileSize; + int increase; + int smallHole ; + int result = YAFFS_OK; + yaffs_Device *dev = NULL; + + __u8 *localBuffer = NULL; + + int smallIncreaseOk = 0; + + if(!obj) + return YAFFS_FAIL; - if (newSizeOfPartialChunk != 0) { - int lastChunk = 1 + newFullChunks; + if(obj->variantType != YAFFS_OBJECT_TYPE_FILE) + return YAFFS_FAIL; + + dev = obj->myDev; + + /* Bail out if not yaffs2 mode */ + if(!dev->param.isYaffs2) + return YAFFS_OK; - __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__); + oldFileSize = obj->variant.fileVariant.fileSize; - /* Got to read and rewrite the last chunk with its new size and zero pad */ - yaffs_ReadChunkDataFromObject(in, lastChunk, - localBuffer); + if (newSize <= oldFileSize) + return YAFFS_OK; - memset(localBuffer + newSizeOfPartialChunk, 0, - dev->nDataBytesPerChunk - newSizeOfPartialChunk); + increase = newSize - oldFileSize; - yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer, - newSizeOfPartialChunk, 1); + if(increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->nDataBytesPerChunk && + yaffs_CheckSpaceForAllocation(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1)) + smallHole = 1; + else + smallHole = 0; - yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__); + if(smallHole) + localBuffer= yaffs_GetTempBuffer(dev, __LINE__); + + if(localBuffer){ + /* fill hole with zero bytes */ + int pos = oldFileSize; + int thisWrite; + int written; + memset(localBuffer,0,dev->nDataBytesPerChunk); + smallIncreaseOk = 1; + + while(increase > 0 && smallIncreaseOk){ + thisWrite = increase; + if(thisWrite > dev->nDataBytesPerChunk) + thisWrite = dev->nDataBytesPerChunk; + written = yaffs_DoWriteDataToFile(obj,localBuffer,pos,thisWrite,0); + if(written == thisWrite){ + pos += thisWrite; + increase -= thisWrite; + } else + smallIncreaseOk = 0; } - in->variant.fileVariant.fileSize = newSize; + yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__); - yaffs_PruneFileStructure(dev, &in->variant.fileVariant); - } else { - /* newsSize > oldFileSize */ - in->variant.fileVariant.fileSize = newSize; + /* If we were out of space then reverse any chunks we've added */ + if(!smallIncreaseOk) + yaffs_ResizeDown(obj, oldFileSize); + } + + if (!smallIncreaseOk && + obj->parent && + obj->parent->objectId != YAFFS_OBJECTID_UNLINKED && + obj->parent->objectId != YAFFS_OBJECTID_DELETED){ + /* Write a hole start header with the old file size */ + yaffs_UpdateObjectHeader(obj, NULL, 0,1,0); } + return result; + +} + +int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize) +{ + yaffs_Device *dev = in->myDev; + int oldFileSize = in->variant.fileVariant.fileSize; + + yaffs_FlushFilesChunkCache(in); + yaffs_InvalidateWholeChunkCache(in); + + yaffs_CheckGarbageCollection(dev,0); + + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) + return YAFFS_FAIL; + + if (newSize == oldFileSize) + return YAFFS_OK; + + if(newSize > oldFileSize){ + yaffs_HandleHole(in,newSize); + in->variant.fileVariant.fileSize = newSize; + } else { + /* newSize < oldFileSize */ + yaffs_ResizeDown(in, newSize); + } - /* Write a new object header. + /* Write a new object header to reflect the resize. * show we've shrunk the file, if need be - * Do this only if the file is not in the deleted directories. + * Do this only if the file is not in the deleted directories + * and is not shadowed. */ if (in->parent && + !in->isShadowed && in->parent->objectId != YAFFS_OBJECTID_UNLINKED && - in->parent->objectId != YAFFS_OBJECTID_DELETED) { - yaffs_UpdateObjectHeader(in, NULL, 0, - (newSize < oldFileSize) ? 1 : 0, 0); - } + in->parent->objectId != YAFFS_OBJECTID_DELETED) + yaffs_UpdateObjectHeader(in, NULL, 0,0,0); + return YAFFS_OK; } loff_t yaffs_GetFileSize(yaffs_Object *obj) { + YCHAR *alias = NULL; obj = yaffs_GetEquivalentObject(obj); switch (obj->variantType) { case YAFFS_OBJECT_TYPE_FILE: return obj->variant.fileVariant.fileSize; case YAFFS_OBJECT_TYPE_SYMLINK: - return yaffs_strlen(obj->variant.symLinkVariant.alias); + alias = obj->variant.symLinkVariant.alias; + if(!alias) + return 0; + return yaffs_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH); default: return 0; } @@ -5111,23 +5535,27 @@ loff_t yaffs_GetFileSize(yaffs_Object *obj) -int yaffs_FlushFile(yaffs_Object *in, int updateTime) +int yaffs_FlushFile(yaffs_Object *in, int updateTime, int dataSync) { int retVal; if (in->dirty) { yaffs_FlushFilesChunkCache(in); - if (updateTime) { + if(dataSync) /* Only sync data */ + retVal=YAFFS_OK; + else { + if (updateTime) { #ifdef CONFIG_YAFFS_WINCE - yfsd_WinFileTimeNow(in->win_mtime); + yfsd_WinFileTimeNow(in->win_mtime); #else - in->yst_mtime = Y_CURRENT_TIME; + in->yst_mtime = Y_CURRENT_TIME; #endif - } + } - retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >= - 0) ? YAFFS_OK : YAFFS_FAIL; + retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >= + 0) ? YAFFS_OK : YAFFS_FAIL; + } } else { retVal = YAFFS_OK; } @@ -5142,7 +5570,7 @@ static int yaffs_DoGenericObjectDeletion(yaffs_Object *in) /* First off, invalidate the file's data in the cache, without flushing. */ yaffs_InvalidateWholeChunkCache(in); - if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) { + if (in->myDev->param.isYaffs2 && (in->parent != in->myDev->deletedDir)) { /* Move to the unlinked directory so we have a record that it was deleted. */ yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0); @@ -5166,16 +5594,10 @@ static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in) int retVal; int immediateDeletion = 0; + yaffs_Device *dev = in->myDev; -#ifdef __KERNEL__ - if (!in->myInode) { + if (!in->myInode) immediateDeletion = 1; - } -#else - if (in->inUse <= 0) { - immediateDeletion = 1; - } -#endif if (immediateDeletion) { retVal = @@ -5186,9 +5608,8 @@ static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in) in->objectId)); in->deleted = 1; in->myDev->nDeletedFiles++; - if (1 || in->myDev->isYaffs2) { + if (dev->param.disableSoftDelete || dev->param.isYaffs2) yaffs_ResizeFile(in, 0); - } yaffs_SoftDeleteFile(in); } else { retVal = @@ -5204,18 +5625,21 @@ int yaffs_DeleteFile(yaffs_Object *in) { int retVal = YAFFS_OK; int deleted = in->deleted; + yaffs_Device *dev = in->myDev; - yaffs_ResizeFile(in, 0); + if (dev->param.disableSoftDelete || dev->param.isYaffs2) + yaffs_ResizeFile(in, 0); if (in->nDataChunks > 0) { /* Use soft deletion if there is data in the file. * That won't be the case if it has been resized to zero. */ - if (!in->unlinked) { + if (!in->unlinked) retVal = yaffs_UnlinkFileIfNeeded(in); - } + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) { - in->deleted = deleted = 1; + in->deleted = 1; + deleted = 1; in->myDev->nDeletedFiles++; yaffs_SoftDeleteFile(in); } @@ -5230,20 +5654,26 @@ int yaffs_DeleteFile(yaffs_Object *in) } } -static int yaffs_DeleteDirectory(yaffs_Object *in) +static int yaffs_IsNonEmptyDirectory(yaffs_Object *obj) { - /* First check that the directory is empty. */ - if (ylist_empty(&in->variant.directoryVariant.children)) { - return yaffs_DoGenericObjectDeletion(in); - } + return (obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) && + !(ylist_empty(&obj->variant.directoryVariant.children)); +} - return YAFFS_FAIL; +static int yaffs_DeleteDirectory(yaffs_Object *obj) +{ + /* First check that the directory is empty. */ + if (yaffs_IsNonEmptyDirectory(obj)) + return YAFFS_FAIL; + return yaffs_DoGenericObjectDeletion(obj); } static int yaffs_DeleteSymLink(yaffs_Object *in) { - YFREE(in->variant.symLinkVariant.alias); + if(in->variant.symLinkVariant.alias) + YFREE(in->variant.symLinkVariant.alias); + in->variant.symLinkVariant.alias=NULL; return yaffs_DoGenericObjectDeletion(in); } @@ -5265,6 +5695,10 @@ int retVal = -1; retVal = yaffs_DeleteFile(obj); break; case YAFFS_OBJECT_TYPE_DIRECTORY: + if(!ylist_empty(&obj->variant.directoryVariant.dirty)){ + T(YAFFS_TRACE_BACKGROUND, (TSTR("Remove object %d from dirty directories" TENDSTR),obj->objectId)); + ylist_del_init(&obj->variant.directoryVariant.dirty); + } return yaffs_DeleteDirectory(obj); break; case YAFFS_OBJECT_TYPE_SYMLINK: @@ -5277,10 +5711,10 @@ int retVal = -1; retVal = yaffs_DoGenericObjectDeletion(obj); break; case YAFFS_OBJECT_TYPE_UNKNOWN: - retVal = 0; + retVal = 0; break; /* should not happen. */ } - + return retVal; } @@ -5289,57 +5723,57 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj) int immediateDeletion = 0; -#ifdef __KERNEL__ - if (!obj->myInode) { - immediateDeletion = 1; - } -#else - if (obj->inUse <= 0) { + if (!obj->myInode) immediateDeletion = 1; - } -#endif - if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) { - return yaffs_DeleteHardLink(obj); - } else if (!ylist_empty(&obj->hardLinks)) { - /* Curve ball: We're unlinking an object that has a hardlink. - * - * This problem arises because we are not strictly following + if(obj) + yaffs_UpdateParent(obj->parent); + + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) { + return yaffs_DeleteHardLink(obj); + } else if (!ylist_empty(&obj->hardLinks)) { + /* Curve ball: We're unlinking an object that has a hardlink. + * + * This problem arises because we are not strictly following * The Linux link/inode model. * * We can't really delete the object. * Instead, we do the following: * - Select a hardlink. * - Unhook it from the hard links - * - Unhook it from its parent directory (so that the rename can work) + * - Move it from its parent directory (so that the rename can work) * - Rename the object to the hardlink's name. * - Delete the hardlink */ yaffs_Object *hl; + yaffs_Object *parent; int retVal; YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks); + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1); + parent = hl->parent; + ylist_del_init(&hl->hardLinks); - ylist_del_init(&hl->siblings); - yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1); + yaffs_AddObjectToDirectory(obj->myDev->unlinkedDir, hl); - retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0); + retVal = yaffs_ChangeObjectName(obj,parent, name, 0, 0); - if (retVal == YAFFS_OK) { + if (retVal == YAFFS_OK) retVal = yaffs_DoGenericObjectDeletion(hl); - } + return retVal; - } else if(immediateDeletion){ + } else if (immediateDeletion) { switch (obj->variantType) { case YAFFS_OBJECT_TYPE_FILE: return yaffs_DeleteFile(obj); break; case YAFFS_OBJECT_TYPE_DIRECTORY: + ylist_del_init(&obj->variant.directoryVariant.dirty); return yaffs_DeleteDirectory(obj); break; case YAFFS_OBJECT_TYPE_SYMLINK: @@ -5353,19 +5787,19 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj) default: return YAFFS_FAIL; } - } else { + } else if(yaffs_IsNonEmptyDirectory(obj)) + return YAFFS_FAIL; + else return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir, _Y("unlinked"), 0, 0); - } } static int yaffs_UnlinkObject(yaffs_Object *obj) { - if (obj && obj->unlinkAllowed) { + if (obj && obj->unlinkAllowed) return yaffs_UnlinkWorker(obj); - } return YAFFS_FAIL; @@ -5394,9 +5828,9 @@ static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId, /* Handle YAFFS2 case (backward scanning) * If the shadowed object exists then ignore. */ - if (yaffs_FindObjectByNumber(dev, objId)) { + obj = yaffs_FindObjectByNumber(dev, objId); + if(obj) return; - } } /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc. @@ -5407,6 +5841,7 @@ static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId, YAFFS_OBJECT_TYPE_FILE); if (!obj) return; + obj->isShadowed = 1; yaffs_AddObjectToDirectory(dev->unlinkedDir, obj); obj->variant.fileVariant.shrinkSize = 0; obj->valid = 1; /* So that we don't read any other info for this file */ @@ -5499,6 +5934,125 @@ static void yaffs_StripDeletedObjects(yaffs_Device *dev) } +/* + * This code iterates through all the objects making sure that they are rooted. + * Any unrooted objects are re-rooted in lost+found. + * An object needs to be in one of: + * - Directly under deleted, unlinked + * - Directly or indirectly under root. + * + * Note: + * This code assumes that we don't ever change the current relationships between + * directories: + * rootDir->parent == unlinkedDir->parent == deletedDir->parent == NULL + * lostNfound->parent == rootDir + * + * This fixes the problem where directories might have inadvertently been deleted + * leaving the object "hanging" without being rooted in the directory tree. + */ + +static int yaffs_HasNULLParent(yaffs_Device *dev, yaffs_Object *obj) +{ + return (obj == dev->deletedDir || + obj == dev->unlinkedDir|| + obj == dev->rootDir); +} + +static void yaffs_FixHangingObjects(yaffs_Device *dev) +{ + yaffs_Object *obj; + yaffs_Object *parent; + int i; + struct ylist_head *lh; + struct ylist_head *n; + int depthLimit; + int hanging; + + + /* Iterate through the objects in each hash entry, + * looking at each object. + * Make sure it is rooted. + */ + + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { + ylist_for_each_safe(lh, n, &dev->objectBucket[i].list) { + if (lh) { + obj = ylist_entry(lh, yaffs_Object, hashLink); + parent= obj->parent; + + if(yaffs_HasNULLParent(dev,obj)){ + /* These directories are not hanging */ + hanging = 0; + } + else if(!parent || parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) + hanging = 1; + else if(yaffs_HasNULLParent(dev,parent)) + hanging = 0; + else { + /* + * Need to follow the parent chain to see if it is hanging. + */ + hanging = 0; + depthLimit=100; + + while(parent != dev->rootDir && + parent->parent && + parent->parent->variantType == YAFFS_OBJECT_TYPE_DIRECTORY && + depthLimit > 0){ + parent = parent->parent; + depthLimit--; + } + if(parent != dev->rootDir) + hanging = 1; + } + if(hanging){ + T(YAFFS_TRACE_SCAN, + (TSTR("Hanging object %d moved to lost and found" TENDSTR), + obj->objectId)); + yaffs_AddObjectToDirectory(dev->lostNFoundDir,obj); + } + } + } + } +} + + +/* + * Delete directory contents for cleaning up lost and found. + */ +static void yaffs_DeleteDirectoryContents(yaffs_Object *dir) +{ + yaffs_Object *obj; + struct ylist_head *lh; + struct ylist_head *n; + + if(dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) + YBUG(); + + ylist_for_each_safe(lh, n, &dir->variant.directoryVariant.children) { + if (lh) { + obj = ylist_entry(lh, yaffs_Object, siblings); + if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) + yaffs_DeleteDirectoryContents(obj); + + T(YAFFS_TRACE_SCAN, + (TSTR("Deleting lost_found object %d" TENDSTR), + obj->objectId)); + + /* Need to use UnlinkObject since Delete would not handle + * hardlinked objects correctly. + */ + yaffs_UnlinkObject(obj); + } + } + +} + +static void yaffs_EmptyLostAndFound(yaffs_Device *dev) +{ + yaffs_DeleteDirectoryContents(dev->lostNFoundDir); +} + static int yaffs_Scan(yaffs_Device *dev) { yaffs_ExtendedTags tags; @@ -5537,8 +6091,8 @@ static int yaffs_Scan(yaffs_Device *dev) dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER; /* Scan all the blocks to determine their state */ + bi = dev->blockInfo; for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) { - bi = yaffs_GetBlockInfo(dev, blk); yaffs_ClearChunkBits(dev, blk); bi->pagesInUse = 0; bi->softDeletions = 0; @@ -5562,8 +6116,9 @@ static int yaffs_Scan(yaffs_Device *dev) T(YAFFS_TRACE_SCAN_DEBUG, (TSTR("Block empty " TENDSTR))); dev->nErasedBlocks++; - dev->nFreeChunks += dev->nChunksPerBlock; + dev->nFreeChunks += dev->param.nChunksPerBlock; } + bi++; } startIterator = dev->internalStartBlock; @@ -5585,10 +6140,10 @@ static int yaffs_Scan(yaffs_Device *dev) deleted = 0; /* For each chunk in each block that needs scanning....*/ - for (c = 0; !alloc_failed && c < dev->nChunksPerBlock && + for (c = 0; !alloc_failed && c < dev->param.nChunksPerBlock && state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) { /* Read the tags and decide what to do */ - chunk = blk * dev->nChunksPerBlock + c; + chunk = blk * dev->param.nChunksPerBlock + c; result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL, &tags); @@ -5622,11 +6177,11 @@ static int yaffs_Scan(yaffs_Device *dev) dev->allocationBlock = blk; dev->allocationPage = c; dev->allocationBlockFinder = blk; - /* Set it to here to encourage the allocator to go forth from here. */ + /* Set block finder here to encourage the allocator to go forth from here. */ } - dev->nFreeChunks += (dev->nChunksPerBlock - c); + dev->nFreeChunks += (dev->param.nChunksPerBlock - c); } else if (tags.chunkId > 0) { /* chunkId > 0 so it is a data chunk... */ unsigned int endpos; @@ -5659,7 +6214,7 @@ static int yaffs_Scan(yaffs_Device *dev) endpos) { in->variant.fileVariant. scannedFileSize = endpos; - if (!dev->useHeaderFileSize) { + if (!dev->param.useHeaderFileSize) { in->variant.fileVariant. fileSize = in->variant.fileVariant. @@ -5838,7 +6393,7 @@ static int yaffs_Scan(yaffs_Device *dev) /* Todo got a problem */ break; case YAFFS_OBJECT_TYPE_FILE: - if (dev->useHeaderFileSize) + if (dev->param.useHeaderFileSize) in->variant.fileVariant. fileSize = @@ -5868,12 +6423,6 @@ static int yaffs_Scan(yaffs_Device *dev) break; } -/* - if (parent == dev->deletedDir) { - yaffs_DestroyObject(in); - bi->hasShrinkHeader = 1; - } -*/ } } } @@ -5883,6 +6432,12 @@ static int yaffs_Scan(yaffs_Device *dev) state = YAFFS_BLOCK_STATE_FULL; } + if (state == YAFFS_BLOCK_STATE_ALLOCATING) { + /* If the block was partially allocated then treat it as fully allocated.*/ + state = YAFFS_BLOCK_STATE_FULL; + dev->allocationBlock = -1; + } + bi->blockState = state; /* Now let's see if it was dirty */ @@ -5915,13 +6470,13 @@ static int yaffs_Scan(yaffs_Device *dev) * then setting the object header to unshadowed. */ obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId); - if(obj) + if (obj) yaffs_DeleteObject(obj); - + obj = yaffs_FindObjectByNumber(dev, fixer->objectId); - if(obj){ + + if (obj) yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0); - } YFREE(fixer); } @@ -5929,9 +6484,8 @@ static int yaffs_Scan(yaffs_Device *dev) yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__); - if (alloc_failed) { + if (alloc_failed) return YAFFS_FAIL; - } T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR))); @@ -5986,7 +6540,7 @@ static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in) yaffs_SetObjectName(in, oh->name); if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) { - in->variant.symLinkVariant.alias = + in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias); if (!in->variant.symLinkVariant.alias) alloc_failed = 1; /* Not returned to caller */ @@ -6030,7 +6584,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) yaffs_BlockIndex *blockIndex = NULL; int altBlockIndex = 0; - if (!dev->isYaffs2) { + if (!dev->param.isYaffs2) { T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR))); return YAFFS_FAIL; @@ -6062,8 +6616,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) chunkData = yaffs_GetTempBuffer(dev, __LINE__); /* Scan all the blocks to determine their state */ + bi = dev->blockInfo; for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) { - bi = yaffs_GetBlockInfo(dev, blk); yaffs_ClearChunkBits(dev, blk); bi->pagesInUse = 0; bi->softDeletions = 0; @@ -6093,7 +6647,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) T(YAFFS_TRACE_SCAN_DEBUG, (TSTR("Block empty " TENDSTR))); dev->nErasedBlocks++; - dev->nFreeChunks += dev->nChunksPerBlock; + dev->nFreeChunks += dev->param.nChunksPerBlock; } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { /* Determine the highest sequence number */ @@ -6105,9 +6659,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) nBlocksToScan++; - if (sequenceNumber >= dev->sequenceNumber) { + if (sequenceNumber >= dev->sequenceNumber) dev->sequenceNumber = sequenceNumber; - } } else { /* TODO: Nasty sequence number! */ T(YAFFS_TRACE_SCAN, @@ -6117,6 +6670,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) } } + bi++; } T(YAFFS_TRACE_SCAN, @@ -6179,7 +6733,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) /* For each chunk in each block that needs scanning.... */ foundChunksInBlock = 0; - for (c = dev->nChunksPerBlock - 1; + for (c = dev->param.nChunksPerBlock - 1; !alloc_failed && c >= 0 && (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || state == YAFFS_BLOCK_STATE_ALLOCATING); c--) { @@ -6187,7 +6741,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) * Read the tags and decide what to do */ - chunk = blk * dev->nChunksPerBlock + c; + chunk = blk * dev->param.nChunksPerBlock + c; result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL, &tags); @@ -6227,14 +6781,10 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) dev->allocationBlockFinder = blk; } else { /* This is a partially written block that is not - * the current allocation block. This block must have - * had a write failure, so set up for retirement. + * the current allocation block. */ - /* bi->needsRetiring = 1; ??? TODO */ - bi->gcPrioritise = 1; - - T(YAFFS_TRACE_ALWAYS, + T(YAFFS_TRACE_SCAN, (TSTR("Partially written block %d detected" TENDSTR), blk)); } @@ -6273,35 +6823,26 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) if (in && in->variantType == YAFFS_OBJECT_TYPE_FILE - && chunkBase < - in->variant.fileVariant.shrinkSize) { + && chunkBase < in->variant.fileVariant.shrinkSize) { /* This has not been invalidated by a resize */ - if (!yaffs_PutChunkIntoFile(in, tags.chunkId, - chunk, -1)) { + if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, -1)) { alloc_failed = 1; } /* File size is calculated by looking at the data chunks if we have not * seen an object header yet. Stop this practice once we find an object header. */ - endpos = - (tags.chunkId - - 1) * dev->nDataBytesPerChunk + - tags.byteCount; + endpos = chunkBase + tags.byteCount; if (!in->valid && /* have not got an object header yet */ - in->variant.fileVariant. - scannedFileSize < endpos) { - in->variant.fileVariant. - scannedFileSize = endpos; - in->variant.fileVariant. - fileSize = - in->variant.fileVariant. - scannedFileSize; + in->variant.fileVariant.scannedFileSize < endpos) { + in->variant.fileVariant.scannedFileSize = endpos; + in->variant.fileVariant.fileSize = endpos; } } else if (in) { - /* This chunk has been invalidated by a resize, so delete */ + /* This chunk has been invalidated by a resize, or a past file deletion + * so delete the chunk*/ yaffs_DeleteChunk(dev, chunk, 1, __LINE__); } @@ -6318,17 +6859,15 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) in = NULL; if (tags.extraHeaderInfoAvailable) { - in = yaffs_FindOrCreateObjectByNumber - (dev, tags.objectId, - tags.extraObjectType); + in = yaffs_FindOrCreateObjectByNumber(dev, + tags.objectId, + tags.extraObjectType); if (!in) alloc_failed = 1; } if (!in || -#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD - !in->valid || -#endif + (!in->valid && dev->param.disableLazyLoad) || tags.extraShadows || (!in->valid && (tags.objectId == YAFFS_OBJECTID_ROOT || @@ -6346,7 +6885,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) oh = (yaffs_ObjectHeader *) chunkData; - if (dev->inbandTags) { + if (dev->param.inbandTags) { /* Fix up the header if they got corrupted by inband tags */ oh->shadowsObject = oh->inbandShadowsObject; oh->isShrink = oh->inbandIsShrink; @@ -6404,17 +6943,11 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) isShrink = 1; } - if (isShrink && - in->variant.fileVariant. - shrinkSize > thisSize) { - in->variant.fileVariant. - shrinkSize = - thisSize; - } + if (isShrink && in->variant.fileVariant.shrinkSize > thisSize) + in->variant.fileVariant.shrinkSize = thisSize; - if (isShrink) { + if (isShrink) bi->hasShrinkHeader = 1; - } } /* Use existing - destroy this one. */ @@ -6498,6 +7031,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) oh-> shadowsObject, 1); + yaffs_SetObjectName(in, oh->name); @@ -6583,14 +7117,12 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) * than its current data extents. */ in->variant.fileVariant.fileSize = fileSize; - in->variant.fileVariant.scannedFileSize = - in->variant.fileVariant.fileSize; + in->variant.fileVariant.scannedFileSize = fileSize; } - if (isShrink && - in->variant.fileVariant.shrinkSize > fileSize) { + if (in->variant.fileVariant.shrinkSize > fileSize) in->variant.fileVariant.shrinkSize = fileSize; - } + break; case YAFFS_OBJECT_TYPE_HARDLINK: @@ -6629,6 +7161,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) state = YAFFS_BLOCK_STATE_FULL; } + bi->blockState = state; /* Now let's see if it was dirty */ @@ -6639,6 +7172,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) } } + + yaffs_SkipRestOfBlock(dev); if (altBlockIndex) YFREE_ALT(blockIndex); @@ -6655,9 +7190,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev) yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__); - if (alloc_failed) { + if (alloc_failed) return YAFFS_FAIL; - } T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR))); @@ -6714,7 +7248,7 @@ static void yaffs_VerifyDirectory(yaffs_Object *directory) { struct ylist_head *lh; yaffs_Object *listObj; - + if (!directory) { YBUG(); return; @@ -6742,6 +7276,66 @@ static void yaffs_VerifyDirectory(yaffs_Object *directory) } } +/* + *yaffs_UpdateParent() handles fixing a directories mtime and ctime when a new + * link (ie. name) is created or deleted in the directory. + * + * ie. + * create dir/a : update dir's mtime/ctime + * rm dir/a: update dir's mtime/ctime + * modify dir/a: don't update dir's mtimme/ctime + * + * This can be handled immediately or defered. Defering helps reduce the number + * of updates when many files in a directory are changed within a brief period. + * + * If the directory updating is defered then yaffs_UpdateDirtyDirecories must be + * called periodically. + */ + +static void yaffs_UpdateParent(yaffs_Object *obj) +{ + yaffs_Device *dev; + if(!obj) + return; + + dev = obj->myDev; + obj->dirty = 1; + obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME; + if(dev->param.deferDirectoryUpdate){ + struct ylist_head *link = &obj->variant.directoryVariant.dirty; + + if(ylist_empty(link)){ + ylist_add(link,&dev->dirtyDirectories); + T(YAFFS_TRACE_BACKGROUND, (TSTR("Added object %d to dirty directories" TENDSTR),obj->objectId)); + } + + } else + yaffs_UpdateObjectHeader(obj,NULL,0,0,0); +} + +void yaffs_UpdateDirtyDirectories(yaffs_Device *dev) +{ + struct ylist_head *link; + yaffs_Object *obj; + yaffs_DirectoryStructure *dS; + yaffs_ObjectVariant *oV; + + T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR))); + + while(!ylist_empty(&dev->dirtyDirectories)){ + link = dev->dirtyDirectories.next; + ylist_del_init(link); + + dS=ylist_entry(link,yaffs_DirectoryStructure,dirty); + oV = ylist_entry(dS,yaffs_ObjectVariant,directoryVariant); + obj = ylist_entry(oV,yaffs_Object,variant); + + T(YAFFS_TRACE_BACKGROUND, (TSTR("Update directory %d" TENDSTR), obj->objectId)); + + if(obj->dirty) + yaffs_UpdateObjectHeader(obj,NULL,0,0,0); + } +} static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj) { @@ -6753,17 +7347,16 @@ static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj) yaffs_VerifyDirectory(parent); - if (dev && dev->removeObjectCallback) - dev->removeObjectCallback(obj); + if (dev && dev->param.removeObjectCallback) + dev->param.removeObjectCallback(obj); ylist_del_init(&obj->siblings); obj->parent = NULL; - + yaffs_VerifyDirectory(parent); } - static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj) { @@ -6819,9 +7412,8 @@ yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory, yaffs_Object *l; - if (!name) { + if (!name) return NULL; - } if (!directory) { T(YAFFS_TRACE_ALWAYS, @@ -6851,18 +7443,16 @@ yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory, /* Special case for lost-n-found */ if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) { - if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) { + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) return l; - } } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) { /* LostnFound chunk called Objxxx * Do a real check */ yaffs_GetObjectName(l, buffer, - YAFFS_MAX_NAME_LENGTH); - if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0) { + YAFFS_MAX_NAME_LENGTH + 1); + if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0) return l; - } } } } @@ -6890,16 +7480,15 @@ int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir, T(YAFFS_TRACE_ALWAYS, (TSTR ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR))); - YBUG(); - return YAFFS_FAIL; - } + YBUG(); + return YAFFS_FAIL; + } ylist_for_each(i, &theDir->variant.directoryVariant.children) { if (i) { l = ylist_entry(i, yaffs_Object, siblings); - if (l && !fn(l)) { + if (l && !fn(l)) return YAFFS_FAIL; - } } } @@ -6948,9 +7537,8 @@ int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize) } #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM - else if (obj->shortName[0]) { - yaffs_strcpy(name, obj->shortName); - } + else if (obj->shortName[0]) + yaffs_strncpy(name, obj->shortName,YAFFS_SHORT_NAME_LENGTH+1); #endif else { int result; @@ -6966,11 +7554,12 @@ int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize) NULL); } yaffs_strncpy(name, oh->name, buffSize - 1); + name[buffSize-1]=0; yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__); } - return yaffs_strlen(name); + return yaffs_strnlen(name,buffSize-1); } int yaffs_GetObjectFileLength(yaffs_Object *obj) @@ -6978,11 +7567,12 @@ int yaffs_GetObjectFileLength(yaffs_Object *obj) /* Dereference any hard linking */ obj = yaffs_GetEquivalentObject(obj); - if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) { + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) return obj->variant.fileVariant.fileSize; - } - if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) { - return yaffs_strlen(obj->variant.symLinkVariant.alias); + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){ + if(!obj->variant.symLinkVariant.alias) + return 0; + return yaffs_strnlen(obj->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH); } else { /* Only a directory should drop through to here */ return obj->myDev->nDataBytesPerChunk; @@ -6994,12 +7584,12 @@ int yaffs_GetObjectLinkCount(yaffs_Object *obj) int count = 0; struct ylist_head *i; - if (!obj->unlinked) { + if (!obj->unlinked) count++; /* the object itself */ - } - ylist_for_each(i, &obj->hardLinks) { + + ylist_for_each(i, &obj->hardLinks) count++; /* add the hard links; */ - } + return count; } @@ -7045,11 +7635,10 @@ unsigned yaffs_GetObjectType(yaffs_Object *obj) YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj) { obj = yaffs_GetEquivalentObject(obj); - if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) { + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) return yaffs_CloneString(obj->variant.symLinkVariant.alias); - } else { + else return yaffs_CloneString(_Y("")); - } } #ifndef CONFIG_YAFFS_WINCE @@ -7113,7 +7702,7 @@ int yaffs_DumpObject(yaffs_Object *obj) { YCHAR name[257]; - yaffs_GetObjectName(obj, name, 256); + yaffs_GetObjectName(obj, name, YAFFS_MAX_NAME_LENGTH + 1); T(YAFFS_TRACE_ALWAYS, (TSTR @@ -7133,30 +7722,32 @@ static int yaffs_CheckDevFunctions(const yaffs_Device *dev) { /* Common functions, gotta have */ - if (!dev->eraseBlockInNAND || !dev->initialiseNAND) + if (!dev->param.eraseBlockInNAND || !dev->param.initialiseNAND) return 0; #ifdef CONFIG_YAFFS_YAFFS2 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */ - if (dev->writeChunkWithTagsToNAND && - dev->readChunkWithTagsFromNAND && - !dev->writeChunkToNAND && - !dev->readChunkFromNAND && - dev->markNANDBlockBad && dev->queryNANDBlock) + if (dev->param.writeChunkWithTagsToNAND && + dev->param.readChunkWithTagsFromNAND && + !dev->param.writeChunkToNAND && + !dev->param.readChunkFromNAND && + dev->param.markNANDBlockBad && + dev->param.queryNANDBlock) return 1; #endif /* Can use the "spare" style interface for yaffs1 */ - if (!dev->isYaffs2 && - !dev->writeChunkWithTagsToNAND && - !dev->readChunkWithTagsFromNAND && - dev->writeChunkToNAND && - dev->readChunkFromNAND && - !dev->markNANDBlockBad && !dev->queryNANDBlock) + if (!dev->param.isYaffs2 && + !dev->param.writeChunkWithTagsToNAND && + !dev->param.readChunkWithTagsFromNAND && + dev->param.writeChunkToNAND && + dev->param.readChunkFromNAND && + !dev->param.markNANDBlockBad && + !dev->param.queryNANDBlock) return 1; - return 0; /* bad */ + return 0; /* bad */ } @@ -7203,35 +7794,35 @@ int yaffs_GutsInitialise(yaffs_Device *dev) return YAFFS_FAIL; } - dev->internalStartBlock = dev->startBlock; - dev->internalEndBlock = dev->endBlock; + dev->internalStartBlock = dev->param.startBlock; + dev->internalEndBlock = dev->param.endBlock; dev->blockOffset = 0; dev->chunkOffset = 0; dev->nFreeChunks = 0; - dev->gcBlock = -1; + dev->gcBlock = 0; - if (dev->startBlock == 0) { - dev->internalStartBlock = dev->startBlock + 1; - dev->internalEndBlock = dev->endBlock + 1; + if (dev->param.startBlock == 0) { + dev->internalStartBlock = dev->param.startBlock + 1; + dev->internalEndBlock = dev->param.endBlock + 1; dev->blockOffset = 1; - dev->chunkOffset = dev->nChunksPerBlock; + dev->chunkOffset = dev->param.nChunksPerBlock; } /* Check geometry parameters. */ - if ((!dev->inbandTags && dev->isYaffs2 && dev->totalBytesPerChunk < 1024) || - (!dev->isYaffs2 && dev->totalBytesPerChunk < 512) || - (dev->inbandTags && !dev->isYaffs2) || - dev->nChunksPerBlock < 2 || - dev->nReservedBlocks < 2 || + if ((!dev->param.inbandTags && dev->param.isYaffs2 && dev->param.totalBytesPerChunk < 1024) || + (!dev->param.isYaffs2 && dev->param.totalBytesPerChunk < 512) || + (dev->param.inbandTags && !dev->param.isYaffs2) || + dev->param.nChunksPerBlock < 2 || + dev->param.nReservedBlocks < 2 || dev->internalStartBlock <= 0 || dev->internalEndBlock <= 0 || - dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2)) { /* otherwise it is too small */ + dev->internalEndBlock <= (dev->internalStartBlock + dev->param.nReservedBlocks + 2)) { /* otherwise it is too small */ T(YAFFS_TRACE_ALWAYS, (TSTR ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d " - TENDSTR), dev->totalBytesPerChunk, dev->isYaffs2 ? "2" : "", dev->inbandTags)); + TENDSTR), dev->param.totalBytesPerChunk, dev->param.isYaffs2 ? "2" : "", dev->param.inbandTags)); return YAFFS_FAIL; } @@ -7242,10 +7833,10 @@ int yaffs_GutsInitialise(yaffs_Device *dev) } /* Sort out space for inband tags, if required */ - if (dev->inbandTags) - dev->nDataBytesPerChunk = dev->totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart); + if (dev->param.inbandTags) + dev->nDataBytesPerChunk = dev->param.totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart); else - dev->nDataBytesPerChunk = dev->totalBytesPerChunk; + dev->nDataBytesPerChunk = dev->param.totalBytesPerChunk; /* Got the right mix of functions? */ if (!yaffs_CheckDevFunctions(dev)) { @@ -7292,12 +7883,12 @@ int yaffs_GutsInitialise(yaffs_Device *dev) * We need to find the next power of 2 > than internalEndBlock */ - x = dev->nChunksPerBlock * (dev->internalEndBlock + 1); + x = dev->param.nChunksPerBlock * (dev->internalEndBlock + 1); bits = ShiftsGE(x); /* Set up tnode width if wide tnodes are enabled. */ - if (!dev->wideTnodesDisabled) { + if (!dev->param.wideTnodesDisabled) { /* bits must be even so that we end up with 32-bit words */ if (bits & 1) bits++; @@ -7324,7 +7915,7 @@ int yaffs_GutsInitialise(yaffs_Device *dev) dev->chunkGroupSize = 1 << dev->chunkGroupBits; - if (dev->nChunksPerBlock < dev->chunkGroupSize) { + if (dev->param.nChunksPerBlock < dev->chunkGroupSize) { /* We have a problem because the soft delete won't work if * the chunk group size > chunks per block. * This can be remedied by using larger "virtual blocks". @@ -7338,9 +7929,11 @@ int yaffs_GutsInitialise(yaffs_Device *dev) /* OK, we've finished verifying the device, lets continue with initialisation */ /* More device initialisation */ - dev->garbageCollections = 0; - dev->passiveGarbageCollections = 0; - dev->currentDirtyChecker = 0; + dev->allGCs = 0; + dev->passiveGCs = 0; + dev->oldestDirtyGCs = 0; + dev->backgroundGCs = 0; + dev->gcBlockFinder = 0; dev->bufferedBlock = -1; dev->doingBufferedBlockRewrite = 0; dev->nDeletedFiles = 0; @@ -7352,8 +7945,11 @@ int yaffs_GutsInitialise(yaffs_Device *dev) dev->tagsEccUnfixed = 0; dev->nErasureFailures = 0; dev->nErasedBlocks = 0; - dev->isDoingGC = 0; + dev->gcDisable= 0; dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */ + YINIT_LIST_HEAD(&dev->dirtyDirectories); + dev->oldestDirtySequence = 0; + dev->oldestDirtyBlock = 0; /* Initialise temporary buffers and caches. */ if (!yaffs_InitialiseTempBuffers(dev)) @@ -7364,14 +7960,13 @@ int yaffs_GutsInitialise(yaffs_Device *dev) if (!init_failed && - dev->nShortOpCaches > 0) { + dev->param.nShortOpCaches > 0) { int i; void *buf; - int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache); + int srCacheBytes = dev->param.nShortOpCaches * sizeof(yaffs_ChunkCache); - if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) { - dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES; - } + if (dev->param.nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) + dev->param.nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES; dev->srCache = YMALLOC(srCacheBytes); @@ -7380,11 +7975,11 @@ int yaffs_GutsInitialise(yaffs_Device *dev) if (dev->srCache) memset(dev->srCache, 0, srCacheBytes); - for (i = 0; i < dev->nShortOpCaches && buf; i++) { + for (i = 0; i < dev->param.nShortOpCaches && buf; i++) { dev->srCache[i].object = NULL; dev->srCache[i].lastUse = 0; dev->srCache[i].dirty = 0; - dev->srCache[i].data = buf = YMALLOC_DMA(dev->totalBytesPerChunk); + dev->srCache[i].data = buf = YMALLOC_DMA(dev->param.totalBytesPerChunk); } if (!buf) init_failed = 1; @@ -7395,14 +7990,14 @@ int yaffs_GutsInitialise(yaffs_Device *dev) dev->cacheHits = 0; if (!init_failed) { - dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32)); + dev->gcCleanupList = YMALLOC(dev->param.nChunksPerBlock * sizeof(__u32)); if (!dev->gcCleanupList) init_failed = 1; } - if (dev->isYaffs2) { - dev->useHeaderFileSize = 1; - } + if (dev->param.isYaffs2) + dev->param.useHeaderFileSize = 1; + if (!init_failed && !yaffs_InitialiseBlocks(dev)) init_failed = 1; @@ -7415,7 +8010,7 @@ int yaffs_GutsInitialise(yaffs_Device *dev) if (!init_failed) { /* Now scan the flash. */ - if (dev->isYaffs2) { + if (dev->param.isYaffs2) { if (yaffs_CheckpointRestore(dev)) { yaffs_CheckObjectDetailsLoaded(dev->rootDir); T(YAFFS_TRACE_ALWAYS, @@ -7437,7 +8032,6 @@ int yaffs_GutsInitialise(yaffs_Device *dev) dev->nDeletedFiles = 0; dev->nUnlinkedFiles = 0; dev->nBackgroundDeletions = 0; - dev->oldestDirtySequence = 0; if (!init_failed && !yaffs_InitialiseBlocks(dev)) init_failed = 1; @@ -7455,6 +8049,9 @@ int yaffs_GutsInitialise(yaffs_Device *dev) init_failed = 1; yaffs_StripDeletedObjects(dev); + yaffs_FixHangingObjects(dev); + if(dev->param.emptyLostAndFound) + yaffs_EmptyLostAndFound(dev); } if (init_failed) { @@ -7478,6 +8075,9 @@ int yaffs_GutsInitialise(yaffs_Device *dev) yaffs_VerifyFreeChunks(dev); yaffs_VerifyBlocks(dev); + /* Clean up any aborted checkpoint data */ + if(!dev->isCheckpointed && dev->blocksInCheckpoint > 0) + yaffs_InvalidateCheckpoint(dev); T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR))); @@ -7493,10 +8093,10 @@ void yaffs_Deinitialise(yaffs_Device *dev) yaffs_DeinitialiseBlocks(dev); yaffs_DeinitialiseTnodes(dev); yaffs_DeinitialiseObjects(dev); - if (dev->nShortOpCaches > 0 && + if (dev->param.nShortOpCaches > 0 && dev->srCache) { - for (i = 0; i < dev->nShortOpCaches; i++) { + for (i = 0; i < dev->param.nShortOpCaches; i++) { if (dev->srCache[i].data) YFREE(dev->srCache[i].data); dev->srCache[i].data = NULL; @@ -7508,41 +8108,38 @@ void yaffs_Deinitialise(yaffs_Device *dev) YFREE(dev->gcCleanupList); - for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) YFREE(dev->tempBuffer[i].buffer); - } - dev->isMounted = 0; - if (dev->deinitialiseNAND) - dev->deinitialiseNAND(dev); + if (dev->param.deinitialiseNAND) + dev->param.deinitialiseNAND(dev); } } static int yaffs_CountFreeChunks(yaffs_Device *dev) { - int nFree; + int nFree=0; int b; yaffs_BlockInfo *blk; - for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock; - b++) { - blk = yaffs_GetBlockInfo(dev, b); - + blk = dev->blockInfo; + for (b = dev->internalStartBlock; b <= dev->internalEndBlock; b++) { switch (blk->blockState) { case YAFFS_BLOCK_STATE_EMPTY: case YAFFS_BLOCK_STATE_ALLOCATING: case YAFFS_BLOCK_STATE_COLLECTING: case YAFFS_BLOCK_STATE_FULL: nFree += - (dev->nChunksPerBlock - blk->pagesInUse + + (dev->param.nChunksPerBlock - blk->pagesInUse + blk->softDeletions); break; default: break; } + blk++; } return nFree; @@ -7555,6 +8152,7 @@ int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev) int nFree; int nDirtyCacheChunks; int blocksForCheckpoint; + int i; #if 1 nFree = dev->nFreeChunks; @@ -7566,24 +8164,21 @@ int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev) /* Now count the number of dirty chunks in the cache and subtract those */ - { - int i; - for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) { - if (dev->srCache[i].dirty) - nDirtyCacheChunks++; - } + for (nDirtyCacheChunks = 0, i = 0; i < dev->param.nShortOpCaches; i++) { + if (dev->srCache[i].dirty) + nDirtyCacheChunks++; } nFree -= nDirtyCacheChunks; - nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock); + nFree -= ((dev->param.nReservedBlocks + 1) * dev->param.nChunksPerBlock); /* Now we figure out how much to reserve for the checkpoint and report that... */ blocksForCheckpoint = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint; if (blocksForCheckpoint < 0) blocksForCheckpoint = 0; - nFree -= (blocksForCheckpoint * dev->nChunksPerBlock); + nFree -= (blocksForCheckpoint * dev->param.nChunksPerBlock); if (nFree < 0) nFree = 0; @@ -7619,8 +8214,8 @@ static void yaffs_VerifyFreeChunks(yaffs_Device *dev) #define yaffs_CheckStruct(structure, syze, name) \ do { \ if (sizeof(structure) != syze) { \ - T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\ - name, syze, sizeof(structure))); \ + T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\ + name, syze, (int) sizeof(structure))); \ return YAFFS_FAIL; \ } \ } while (0) @@ -7631,7 +8226,7 @@ static int yaffs_CheckStructures(void) /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */ /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */ #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG - yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode"); +/* yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode"); */ #endif #ifndef CONFIG_YAFFS_WINCE yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");