-yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
- __u32 mode, __u32 uid, __u32 gid)
-{
- return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
- mode, uid, gid, NULL, NULL, 0);
-}
-
-yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
- __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
-{
- return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
- uid, gid, NULL, NULL, rdev);
-}
-
-yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
- __u32 mode, __u32 uid, __u32 gid,
- const YCHAR *alias)
-{
- return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
- uid, gid, NULL, alias, 0);
-}
-
-/* yaffs_Link returns the object id of the equivalent object.*/
-yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
- yaffs_Object *equivalentObject)
-{
- /* Get the real object in case we were fed a hard link as an equivalent object */
- equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
-
- if (yaffs_MknodObject
- (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
- equivalentObject, NULL, 0)) {
- return equivalentObject;
- } else {
- return NULL;
- }
-
-}
-
-static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
- const YCHAR *newName, int force, int shadows)
-{
- int unlinkOp;
- int deleteOp;
-
- yaffs_Object *existingTarget;
-
- if (newDir == NULL)
- newDir = obj->parent; /* use the old directory */
-
- if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
- T(YAFFS_TRACE_ALWAYS,
- (TSTR
- ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
- TENDSTR)));
- YBUG();
- }
-
- /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
- if (obj->myDev->param.isYaffs2)
- unlinkOp = (newDir == obj->myDev->unlinkedDir);
- else
- unlinkOp = (newDir == obj->myDev->unlinkedDir
- && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
-
- deleteOp = (newDir == obj->myDev->deletedDir);
-
- existingTarget = yaffs_FindObjectByName(newDir, newName);
-
- /* If the object is a file going into the unlinked directory,
- * then it is OK to just stuff it in since duplicate names are allowed.
- * else only proceed if the new name does not exist and if we're putting
- * it into a directory.
- */
- if ((unlinkOp ||
- deleteOp ||
- force ||
- (shadows > 0) ||
- !existingTarget) &&
- newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
- yaffs_SetObjectName(obj, newName);
- obj->dirty = 1;
-
- yaffs_AddObjectToDirectory(newDir, obj);
-
- if (unlinkOp)
- obj->unlinked = 1;
-
- /* If it is a deletion then we mark it as a shrink for gc purposes. */
- if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows) >= 0)
- return YAFFS_OK;
- }
-
- return YAFFS_FAIL;
-}
-
-int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
- yaffs_Object *newDir, const YCHAR *newName)
-{
- yaffs_Object *obj = NULL;
- yaffs_Object *existingTarget = NULL;
- int force = 0;
- int result;
- yaffs_Device *dev;
-
-
- if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
- YBUG();
- 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)
- force = 1;
-#endif
-
- if(yaffs_strnlen(newName,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
- /* ENAMETOOLONG */
- return YAFFS_FAIL;
-
- obj = yaffs_FindObjectByName(oldDir, oldName);
-
- if (obj && obj->renameAllowed) {
-
- /* Now do the handling for an existing target, if there is one */
-
- existingTarget = yaffs_FindObjectByName(newDir, newName);
- if (existingTarget &&
- existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
- !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
- /* There is a target that is a non-empty directory, so we fail */
- 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.
- *
- * Note we must disable gc otherwise it can mess up the shadowing.
- *
- */
- dev->isDoingGC=1;
- yaffs_ChangeObjectName(obj, newDir, newName, force,
- existingTarget->objectId);
- existingTarget->isShadowed = 1;
- yaffs_UnlinkObject(existingTarget);
- dev->isDoingGC=0;
- }
-
- result = yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
-
- yaffs_UpdateParent(oldDir);
- if(newDir != oldDir)
- yaffs_UpdateParent(newDir);
-
- return result;
- }
- return YAFFS_FAIL;
-}
-
-/*------------------------- Block Management and Page Allocation ----------------*/
-
-static int yaffs_InitialiseBlocks(yaffs_Device *dev)
-{
- int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
-
- dev->blockInfo = NULL;
- dev->chunkBits = NULL;
-
- dev->allocationBlock = -1; /* force it to get a new one */
-
- /* If the first allocation strategy fails, thry the alternate one */
- dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
- if (!dev->blockInfo) {
- dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
- dev->blockInfoAlt = 1;
- } else
- dev->blockInfoAlt = 0;
-
- if (dev->blockInfo) {
- /* Set up dynamic blockinfo stuff. */
- 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);
- dev->chunkBitsAlt = 1;
- } else
- dev->chunkBitsAlt = 0;
- }
-
- if (dev->blockInfo && dev->chunkBits) {
- memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
- memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
- return YAFFS_OK;
- }
-
- return YAFFS_FAIL;
-}
-
-static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
-{
- if (dev->blockInfoAlt && dev->blockInfo)
- YFREE_ALT(dev->blockInfo);
- else if (dev->blockInfo)
- YFREE(dev->blockInfo);
-
- dev->blockInfoAlt = 0;
-
- dev->blockInfo = NULL;
-
- if (dev->chunkBitsAlt && dev->chunkBits)
- YFREE_ALT(dev->chunkBits);
- else if (dev->chunkBits)
- YFREE(dev->chunkBits);
- dev->chunkBitsAlt = 0;
- dev->chunkBits = NULL;
-}
-
-static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
- yaffs_BlockInfo *bi)
-{
-
- if (!dev->param.isYaffs2)
- return 1; /* disqualification only applies to yaffs2. */
-
- if (!bi->hasShrinkHeader)
- return 1; /* can gc */
-
- yaffs_FindOldestDirtySequence(dev);
-
- /* Can't do gc of this block if there are any blocks older than this one that have
- * discarded pages.
- */
- return (bi->sequenceNumber <= dev->oldestDirtySequence);
-}
-
-/*
- * yaffs_FindRefreshBlock()
- * periodically finds the oldest full block by sequence number for refreshing.
- * Only for yaffs2.
- */
-static __u32 yaffs_FindRefreshBlock(yaffs_Device *dev)
-{
- __u32 b ;
-
- __u32 oldest = 0;
- __u32 oldestSequence = 0;
-
- yaffs_BlockInfo *bi;
-
- /*
- * If refresh period < 10 then refreshing is disabled.
- */
- if(dev->param.refreshPeriod < 10 ||
- !dev->param.isYaffs2)
- return oldest;
-
- /*
- * Fix broken values.
- */
- if(dev->refreshSkip > dev->param.refreshPeriod)
- dev->refreshSkip = dev->param.refreshPeriod;
-
- if(dev->refreshSkip > 0){
- dev->refreshSkip--;
- return oldest;
- }
-
- /*
- * 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++;
- }
-
- if (oldest > 0) {
- T(YAFFS_TRACE_GC,
- (TSTR("GC refresh count %d selected block %d with sequenceNumber %d" TENDSTR),
- dev->refreshCount, oldest, oldestSequence));
- }
-
- return oldest;
-}
-
-/*
- * FindDiretiestBlock is used to select the dirtiest block (or close enough)
- * for garbage collection.
- */
-
-static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
- int aggressive)
-{
- 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) {
- bi = dev->blockInfo;
- for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) {
-
- 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 */
- }
- }
- bi++;
- }
-
- if (!pendingPrioritisedExist) /* 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.
- */
-
- dev->nonAggressiveSkip--;
-
- if (!aggressive && (dev->nonAggressiveSkip > 0))
- return -1;
-
- if (!prioritised)
- pagesInUse =
- (aggressive) ? dev->param.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 (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
- (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
- yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
- dirtiest = b;
- pagesInUse = (bi->pagesInUse - bi->softDeletions);
- }
- }
-
- dev->currentDirtyChecker = b;
-
- if (dirtiest > 0) {
- T(YAFFS_TRACE_GC,
- (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
- dev->param.nChunksPerBlock - pagesInUse, prioritised));
- }
-
- if (dirtiest > 0)
- dev->nonAggressiveSkip = 4;
-
- return dirtiest;
-}
-
-static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
-{
- yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
-
- int erasedOk = 0;
-
- /* If the block is still healthy erase it and mark as clean.
- * If the block has had a data failure, then retire it.
- */
-
- T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
- (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 (!bi->needsRetiring) {
- yaffs_InvalidateCheckpoint(dev);
- erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
- if (!erasedOk) {
- dev->nErasureFailures++;
- T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
- (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
- }
- }
-
- if (erasedOk &&
- ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
- int i;
- for (i = 0; i < dev->param.nChunksPerBlock; i++) {
- if (!yaffs_CheckChunkErased
- (dev, blockNo * dev->param.nChunksPerBlock + i)) {
- T(YAFFS_TRACE_ERROR,
- (TSTR
- (">>Block %d erasure supposedly OK, but chunk %d not erased"
- TENDSTR), blockNo, i));
- }
- }
- }
-
- if (erasedOk) {
- /* Clean it up... */
- bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
- bi->sequenceNumber = 0;
- dev->nErasedBlocks++;
- bi->pagesInUse = 0;
- bi->softDeletions = 0;
- bi->hasShrinkHeader = 0;
- bi->skipErasedCheck = 1; /* This is clean, so no need to check */
- bi->gcPrioritise = 0;
- yaffs_ClearChunkBits(dev, blockNo);
-
- T(YAFFS_TRACE_ERASE,
- (TSTR("Erased block %d" TENDSTR), blockNo));
- } else {
- dev->nFreeChunks -= dev->param.nChunksPerBlock; /* We lost a block of free space */
-
- yaffs_RetireBlock(dev, blockNo);
- T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
- (TSTR("**>> Block %d retired" TENDSTR), blockNo));
- }
-}
-
-static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
-{
- int i;
-
- yaffs_BlockInfo *bi;
-
- if (dev->nErasedBlocks < 1) {
- /* Hoosterman we've got a problem.
- * Can't get space to gc
- */
- T(YAFFS_TRACE_ERROR,
- (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
-
- return -1;
- }
-
- /* Find an empty block. */
-
- for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
- dev->allocationBlockFinder++;
- if (dev->allocationBlockFinder < dev->internalStartBlock
- || dev->allocationBlockFinder > dev->internalEndBlock) {
- dev->allocationBlockFinder = dev->internalStartBlock;
- }
-
- bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
-
- if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
- bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
- dev->sequenceNumber++;
- bi->sequenceNumber = dev->sequenceNumber;
- dev->nErasedBlocks--;
- T(YAFFS_TRACE_ALLOCATE,
- (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
- dev->allocationBlockFinder, dev->sequenceNumber,
- dev->nErasedBlocks));
- return dev->allocationBlockFinder;
- }
- }
-
- T(YAFFS_TRACE_ALWAYS,
- (TSTR
- ("yaffs tragedy: no more erased blocks, but there should have been %d"
- TENDSTR), dev->nErasedBlocks));
-
- return -1;
-}
-
-
-
-static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
-{
- if (!dev->nCheckpointBlocksRequired &&
- dev->param.isYaffs2) {
- /* Not a valid value so recalculate */
- int nBytes = 0;
- int nBlocks;
- int devBlocks = (dev->param.endBlock - dev->param.startBlock + 1);
- int tnodeSize = yaffs_CalcTnodeSize(dev);
-
- nBytes += sizeof(yaffs_CheckpointValidity);
- nBytes += sizeof(yaffs_CheckpointDevice);
- nBytes += devBlocks * sizeof(yaffs_BlockInfo);
- nBytes += devBlocks * dev->chunkBitmapStride;
- nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjectsCreated - dev->nFreeObjects);
- nBytes += (tnodeSize + sizeof(__u32)) * (dev->nTnodesCreated - dev->nFreeTnodes);
- nBytes += sizeof(yaffs_CheckpointValidity);
- nBytes += sizeof(__u32); /* checksum*/
-
- /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
-
- nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->param.nChunksPerBlock)) + 3;
-
- dev->nCheckpointBlocksRequired = nBlocks;
- }
-
- return dev->nCheckpointBlocksRequired;
-}
-
-/*
- * 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, int nChunks)
-{
- int reservedChunks;
- int reservedBlocks = dev->param.nReservedBlocks;
- int checkpointBlocks;
-
- if (dev->param.isYaffs2) {
- checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
- dev->blocksInCheckpoint;
- if (checkpointBlocks < 0)
- checkpointBlocks = 0;
- } else {
- checkpointBlocks = 0;
- }
-
- reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->param.nChunksPerBlock);
-
- return (dev->nFreeChunks > (reservedChunks + nChunks));
-}
-
-static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
- yaffs_BlockInfo **blockUsedPtr)
-{
- int retVal;
- yaffs_BlockInfo *bi;
-
- if (dev->allocationBlock < 0) {
- /* Get next block to allocate off */
- dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
- dev->allocationPage = 0;
- }
-
- 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->param.nReservedBlocks
- && dev->allocationPage == 0) {
- T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
- }
-
- /* Next page please.... */
- if (dev->allocationBlock >= 0) {
- bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
-
- retVal = (dev->allocationBlock * dev->param.nChunksPerBlock) +
- dev->allocationPage;
- bi->pagesInUse++;
- yaffs_SetChunkBit(dev, dev->allocationBlock,
- dev->allocationPage);
-
- dev->allocationPage++;
-
- dev->nFreeChunks--;
-
- /* If the block is full set the state to full */
- if (dev->allocationPage >= dev->param.nChunksPerBlock) {
- bi->blockState = YAFFS_BLOCK_STATE_FULL;
- dev->allocationBlock = -1;
- }
-
- if (blockUsedPtr)
- *blockUsedPtr = bi;
-
- return retVal;
- }
-
- T(YAFFS_TRACE_ERROR,
- (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
-
- return -1;
-}
-
-static int yaffs_GetErasedChunks(yaffs_Device *dev)
-{
- int n;
-
- n = dev->nErasedBlocks * dev->param.nChunksPerBlock;
-
- 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)
-{
- int oldChunk;
- int newChunk;
- int markNAND;
- int retVal = YAFFS_OK;
- int cleanups = 0;
- int i;
- int isCheckpointBlock;
- int matchingChunk;
- int maxCopies;
-
- int chunksBefore = yaffs_GetErasedChunks(dev);
- int chunksAfter;
-
- yaffs_ExtendedTags tags;
-
- yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
-
- yaffs_Object *object;
-
- isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
-
-
- T(YAFFS_TRACE_TRACING,
- (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
- block,
- bi->pagesInUse,
- bi->hasShrinkHeader,
- wholeBlock));
-
- /*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.
- */
- if(dev->gcChunk == 0) /* first time through for this block */
- dev->nFreeChunks -= bi->softDeletions;
-
- dev->isDoingGC = 1;
-
- if (isCheckpointBlock ||
- !yaffs_StillSomeChunkBits(dev, block)) {
- T(YAFFS_TRACE_TRACING,
- (TSTR
- ("Collecting block %d that has no chunks in use" TENDSTR),
- block));
- yaffs_BlockBecameDirty(dev, block);
- } else {
-
- __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
-
- yaffs_VerifyBlock(dev, bi, block);
-
- maxCopies = (wholeBlock) ? dev->param.nChunksPerBlock : 10;
- oldChunk = block * dev->param.nChunksPerBlock + dev->gcChunk;
-
- for (/* init already done */;
- retVal == YAFFS_OK &&
- dev->gcChunk < dev->param.nChunksPerBlock &&
- (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
- maxCopies > 0;
- dev->gcChunk++, oldChunk++) {
- if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
-
- /* This page is in use and might need to be copied off */
-
- maxCopies--;
-
- markNAND = 1;
-
- yaffs_InitialiseTags(&tags);
-
- yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
- buffer, &tags);
-
- object =
- yaffs_FindObjectByNumber(dev,
- tags.objectId);
-
- T(YAFFS_TRACE_GC_DETAIL,
- (TSTR
- ("Collecting chunk in block %d, %d %d %d " TENDSTR),
- dev->gcChunk, tags.objectId, tags.chunkId,
- tags.byteCount));
-
- if (object && !yaffs_SkipVerification(dev)) {
- if (tags.chunkId == 0)
- matchingChunk = object->hdrChunk;
- else if (object->softDeleted)
- matchingChunk = oldChunk; /* Defeat the test */
- else
- matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
-
- if (oldChunk != matchingChunk)
- T(YAFFS_TRACE_ERROR,
- (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
- oldChunk, matchingChunk, tags.objectId, tags.chunkId));
-
- }
-
- if (!object) {
- T(YAFFS_TRACE_ERROR,
- (TSTR
- ("page %d in gc has no object: %d %d %d "
- TENDSTR), oldChunk,
- tags.objectId, tags.chunkId, tags.byteCount));
- }
-
- if (object &&
- object->deleted &&
- object->softDeleted &&
- tags.chunkId != 0) {
- /* Data chunk in a soft deleted file, throw it away
- * It's a soft deleted data chunk,
- * No need to copy this, just forget about it and
- * fix up the object.
- */
-
- object->nDataChunks--;
-
- if (object->nDataChunks <= 0) {
- /* remeber to clean up the object */
- dev->gcCleanupList[cleanups] =
- tags.objectId;
- cleanups++;
- }
- markNAND = 0;
- } else if (0) {
- /* Todo object && object->deleted && object->nDataChunks == 0 */
- /* Deleted object header with no data chunks.
- * Can be discarded and the file deleted.
- */
- object->hdrChunk = 0;
- yaffs_FreeTnode(object->myDev,
- object->variant.
- fileVariant.top);
- object->variant.fileVariant.top = NULL;
- yaffs_DoGenericObjectDeletion(object);
-
- } else if (object) {
- /* It's either a data chunk in a live file or
- * an ObjectHeader, so we're interested in it.
- * NB Need to keep the ObjectHeaders of deleted files
- * until the whole file has been deleted off
- */
- tags.serialNumber++;
-
- dev->nGCCopies++;
-
- 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;
-
- oh->shadowsObject = 0;
- oh->inbandShadowsObject = 0;
- tags.extraShadows = 0;
-
- /* 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;
- } else {
-
- /* Ok, now fix up the Tnodes etc. */
-
- if (tags.chunkId == 0) {
- /* It's a header */
- object->hdrChunk = newChunk;
- object->serial = tags.serialNumber;
- } else {
- /* It's a data chunk */
- int ok;
- ok = yaffs_PutChunkIntoFile
- (object,
- tags.chunkId,
- newChunk, 0);
- }
- }
- }
-
- if (retVal == YAFFS_OK)
- yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
-
- }
- }
-
- yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
-
-
- /* Do any required cleanups */
- for (i = 0; i < cleanups; i++) {
- /* Time to delete the file too */
- object =
- yaffs_FindObjectByNumber(dev,
- dev->gcCleanupList[i]);
- if (object) {
- yaffs_FreeTnode(dev,
- object->variant.fileVariant.
- top);
- object->variant.fileVariant.top = NULL;
- T(YAFFS_TRACE_GC,
- (TSTR
- ("yaffs: About to finally delete object %d"
- TENDSTR), object->objectId));
- yaffs_DoGenericObjectDeletion(object);
- object->myDev->nDeletedFiles--;
- }
-
- }
-
- }
-
- yaffs_VerifyCollectedBlock(dev, bi, block);
-
-
-
- /* If the gc completed then clear the current gcBlock so that we find another. */
- if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) {
- 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 = -1;
- dev->gcChunk = 0;
- }
-
- dev->isDoingGC = 0;
-
- return retVal;
-}
-
-/* New garbage collector
- * If we're very low on erased blocks then we do aggressive garbage collection
- * otherwise we do "leasurely" garbage collection.
- * Aggressive gc looks further (whole array) and will accept less dirty blocks.
- * Passive gc only inspects smaller areas and will only accept more dirty blocks.
- *
- * 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)
-{
- int block;
- int aggressive;
- int gcOk = YAFFS_OK;
- int maxTries = 0;
-
- int checkpointBlockAdjust;
-
- if(dev->param.gcControl &&
- (dev->param.gcControl(dev) & 1) == 0)
- return YAFFS_OK;
-
- if (dev->isDoingGC) {
- /* 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.
- */
-
- do {
- maxTries++;
-
- checkpointBlockAdjust = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
- if (checkpointBlockAdjust < 0)
- checkpointBlockAdjust = 0;
-
- /* If we need a block soon then do aggressive gc.*/
- if (dev->nErasedBlocks < (dev->param.nReservedBlocks + checkpointBlockAdjust + 2))
- aggressive = 1;
- else
- aggressive = 0;
-
- /* 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);
- dev->gcChunk = 0;
- }
-
- block = dev->gcBlock;
-
- if (block > 0) {
- dev->garbageCollections++;
- if (!aggressive)
- dev->passiveGarbageCollections++;
-
- T(YAFFS_TRACE_GC,
- (TSTR
- ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
- dev->nErasedBlocks, aggressive));
-
- gcOk = yaffs_GarbageCollectBlock(dev, block, aggressive);
- }
-
- if (dev->nErasedBlocks < (dev->param.nReservedBlocks) && block > 0) {
- T(YAFFS_TRACE_GC,
- (TSTR
- ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
- TENDSTR), dev->nErasedBlocks, maxTries, block));
- }
- } while ((dev->nErasedBlocks < dev->param.nReservedBlocks) &&
- (block > 0) &&
- (maxTries < 2));
-
- return aggressive ? gcOk : YAFFS_OK;
-}
-
-/*------------------------- TAGS --------------------------------*/
-
-static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
- int chunkInObject)
-{
- return (tags->chunkId == chunkInObject &&
- tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
-
-}
-
-
-/*-------------------- Data file manipulation -----------------*/
-
-static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
- yaffs_ExtendedTags *tags)
-{
- /*Get the Tnode, then get the level 0 offset chunk offset */
- yaffs_Tnode *tn;
- int theChunk = -1;
- yaffs_ExtendedTags localTags;
- int retVal = -1;
-
- yaffs_Device *dev = in->myDev;
-
- if (!tags) {
- /* Passed a NULL, so use our own tags space */
- tags = &localTags;
- }
-
- tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
-
- if (tn) {
- theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
-
- retVal =
- yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
- chunkInInode);
- }
- return retVal;
-}
-
-static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
- yaffs_ExtendedTags *tags)
-{
- /* Get the Tnode, then get the level 0 offset chunk offset */
- yaffs_Tnode *tn;
- int theChunk = -1;
- yaffs_ExtendedTags localTags;
-
- yaffs_Device *dev = in->myDev;
- int retVal = -1;
-
- if (!tags) {
- /* Passed a NULL, so use our own tags space */
- tags = &localTags;
- }
-
- tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
-
- if (tn) {
-
- theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
-
- retVal =
- yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
- chunkInInode);
-
- /* Delete the entry in the filestructure (if found) */
- if (retVal != -1)
- yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, 0);
- }
-
- return retVal;
-}
-
-#ifdef YAFFS_PARANOID
-
-static int yaffs_CheckFileSanity(yaffs_Object *in)
-{
- int chunk;
- int nChunks;
- int fSize;
- int failed = 0;
- int objId;
- yaffs_Tnode *tn;
- yaffs_Tags localTags;
- yaffs_Tags *tags = &localTags;
- int theChunk;
- int chunkDeleted;
-
- if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
- return YAFFS_FAIL;
-
- objId = in->objectId;
- fSize = in->variant.fileVariant.fileSize;
- nChunks =
- (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
-
- for (chunk = 1; chunk <= nChunks; chunk++) {
- tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
- chunk);
-
- if (tn) {
-
- theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
-
- if (yaffs_CheckChunkBits
- (dev, theChunk / dev->param.nChunksPerBlock,
- theChunk % dev->param.nChunksPerBlock)) {
-
- yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
- tags,
- &chunkDeleted);
- if (yaffs_TagsMatch
- (tags, in->objectId, chunk, chunkDeleted)) {
- /* found it; */
-
- }
- } else {
-
- failed = 1;
- }
-
- } else {
- /* T(("No level 0 found for %d\n", chunk)); */
- }
- }
-
- return failed ? YAFFS_FAIL : YAFFS_OK;
-}
-
-#endif
-
-static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
- int chunkInNAND, int inScan)
-{
- /* 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;
- yaffs_Device *dev = in->myDev;
- int existingChunk;
- yaffs_ExtendedTags existingTags;
- yaffs_ExtendedTags newTags;
- unsigned existingSerial, newSerial;
-
- if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
- /* Just ignore an attempt at putting a chunk into a non-file during scanning
- * If it is not during Scanning then something went wrong!
- */
- if (!inScan) {
- T(YAFFS_TRACE_ERROR,
- (TSTR
- ("yaffs tragedy:attempt to put data chunk into a non-file"
- TENDSTR)));
- YBUG();
- }
-
- yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
- return YAFFS_OK;
- }
-
- tn = yaffs_AddOrFindLevel0Tnode(dev,
- &in->variant.fileVariant,
- chunkInInode,
- NULL);
- if (!tn)
- return YAFFS_FAIL;
-
- if(!chunkInNAND)
- /* Dummy insert, bail now */
- return YAFFS_OK;
-
- existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
-
- if (inScan != 0) {
- /* If we're scanning then we need to test for duplicates
- * NB This does not need to be efficient since it should only ever
- * happen when the power fails during a write, then only one
- * chunk should ever be affected.
- *
- * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
- * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
- */
-
- if (existingChunk > 0) {
- /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
- * thus we have to do a FindChunkInFile to get the real chunk id.
- *
- * We have a duplicate now we need to decide which one to use:
- *
- * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
- * Forward scanning YAFFS2: The new one is what we use, dump the old one.
- * YAFFS1: Get both sets of tags and compare serial numbers.
- */
-
- if (inScan > 0) {
- /* Only do this for forward scanning */
- yaffs_ReadChunkWithTagsFromNAND(dev,
- chunkInNAND,
- NULL, &newTags);
-
- /* Do a proper find */
- existingChunk =
- yaffs_FindChunkInFile(in, chunkInInode,
- &existingTags);
- }
-
- if (existingChunk <= 0) {
- /*Hoosterman - how did this happen? */
-
- T(YAFFS_TRACE_ERROR,
- (TSTR
- ("yaffs tragedy: existing chunk < 0 in scan"
- TENDSTR)));
-
- }
-
- /* NB The deleted flags should be false, otherwise the chunks will
- * not be loaded during a scan
- */
-
- if (inScan > 0) {
- newSerial = newTags.serialNumber;
- existingSerial = existingTags.serialNumber;
- }
-
- if ((inScan > 0) &&
- (in->myDev->param.isYaffs2 ||
- existingChunk <= 0 ||
- ((existingSerial + 1) & 3) == newSerial)) {
- /* Forward scanning.
- * Use new
- * Delete the old one and drop through to update the tnode
- */
- yaffs_DeleteChunk(dev, existingChunk, 1,
- __LINE__);
- } else {
- /* Backward scanning or we want to use the existing one
- * Use existing.
- * Delete the new one and return early so that the tnode isn't changed
- */
- yaffs_DeleteChunk(dev, chunkInNAND, 1,
- __LINE__);
- return YAFFS_OK;
- }
- }
-
- }
-
- if (existingChunk == 0)
- in->nDataChunks++;
-
- yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
-
- return YAFFS_OK;
-}
-
-static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
- __u8 *buffer)
-{
- int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
-
- if (chunkInNAND >= 0)
- return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
- buffer, NULL);
- else {
- T(YAFFS_TRACE_NANDACCESS,
- (TSTR("Chunk %d not found zero instead" TENDSTR),
- chunkInNAND));
- /* get sane (zero) data if you read a hole */
- memset(buffer, 0, in->myDev->nDataBytesPerChunk);
- return 0;
- }
-
-}
-
-void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
-{
- int block;
- int page;
- yaffs_ExtendedTags tags;
- yaffs_BlockInfo *bi;
-
- if (chunkId <= 0)
- return;
-
- dev->nDeletions++;
- block = chunkId / dev->param.nChunksPerBlock;
- page = chunkId % dev->param.nChunksPerBlock;
-
-
- if (!yaffs_CheckChunkBit(dev, block, page))
- T(YAFFS_TRACE_VERIFY,
- (TSTR("Deleting invalid chunk %d"TENDSTR),
- chunkId));
-
- bi = yaffs_GetBlockInfo(dev, block);
-
- yaffs_UpdateOldestDirtySequence(dev,bi);
-
- T(YAFFS_TRACE_DELETION,
- (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
-
- if (markNAND &&
- bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->param.isYaffs2) {
-
- yaffs_InitialiseTags(&tags);
-
- tags.chunkDeleted = 1;
-
- yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
- yaffs_HandleUpdateChunk(dev, chunkId, &tags);
- } else {
- dev->nUnmarkedDeletions++;
- }
-
- /* Pull out of the management area.
- * If the whole block became dirty, this will kick off an erasure.
- */
- if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
- bi->blockState == YAFFS_BLOCK_STATE_FULL ||
- bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
- bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
- dev->nFreeChunks++;
-
- yaffs_ClearChunkBit(dev, block, page);
-
- bi->pagesInUse--;
-
- if (bi->pagesInUse == 0 &&
- !bi->hasShrinkHeader &&
- bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
- bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
- yaffs_BlockBecameDirty(dev, block);
- }
-
- }
-
-}
-
-static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
- const __u8 *buffer, int nBytes,
- int useReserve)
-{
- /* Find old chunk Need to do this to get serial number
- * Write new one and patch into tree.
- * Invalidate old tags.
- */
-
- int prevChunkId;
- yaffs_ExtendedTags prevTags;
-
- int newChunkId;
- yaffs_ExtendedTags newTags;
-
- yaffs_Device *dev = in->myDev;
-
- yaffs_CheckGarbageCollection(dev);
-
- /* 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);
-
- newTags.chunkId = chunkInInode;
- newTags.objectId = in->objectId;
- newTags.serialNumber =
- (prevChunkId > 0) ? prevTags.serialNumber + 1 : 1;
- newTags.byteCount = nBytes;
-
- 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) {
- yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
-
- if (prevChunkId > 0)
- yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
-
- yaffs_CheckFileSanity(in);
- }
- return newChunkId;
-
-}
-
-/* UpdateObjectHeader updates the header on NAND for an object.
- * If name is not NULL, then that new name is used.
- */
-int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
- int isShrink, int shadows)
-{
-
- yaffs_BlockInfo *bi;
-
- yaffs_Device *dev = in->myDev;
-
- int prevChunkId;
- int retVal = 0;
- int result = 0;
-
- int newChunkId;
- yaffs_ExtendedTags newTags;
- yaffs_ExtendedTags oldTags;
- YCHAR *alias = NULL;
-
- __u8 *buffer = NULL;
- YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
-
- yaffs_ObjectHeader *oh = NULL;
-
- yaffs_strcpy(oldName, _Y("silly old name"));
-
-
- if (!in->fake ||
- in == dev->rootDir || /* The rootDir should also be saved */
- force) {
-
- yaffs_CheckGarbageCollection(dev);
- yaffs_CheckObjectDetailsLoaded(in);
-
- buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
- oh = (yaffs_ObjectHeader *) buffer;
-
- prevChunkId = in->hdrChunk;
-
- if (prevChunkId > 0) {
- result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
- buffer, &oldTags);
-
- yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
-
- memcpy(oldName, oh->name, sizeof(oh->name));
- }
-
- memset(buffer, 0xFF, dev->nDataBytesPerChunk);
-
- oh->type = in->variantType;
- oh->yst_mode = in->yst_mode;
- oh->shadowsObject = oh->inbandShadowsObject = shadows;
-
-#ifdef CONFIG_YAFFS_WINCE
- oh->win_atime[0] = in->win_atime[0];
- oh->win_ctime[0] = in->win_ctime[0];
- oh->win_mtime[0] = in->win_mtime[0];
- oh->win_atime[1] = in->win_atime[1];
- oh->win_ctime[1] = in->win_ctime[1];
- oh->win_mtime[1] = in->win_mtime[1];
-#else
- oh->yst_uid = in->yst_uid;
- oh->yst_gid = in->yst_gid;
- oh->yst_atime = in->yst_atime;
- oh->yst_mtime = in->yst_mtime;
- oh->yst_ctime = in->yst_ctime;
- oh->yst_rdev = in->yst_rdev;
-#endif
- if (in->parent)
- oh->parentObjectId = in->parent->objectId;
- 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)
- memcpy(oh->name, oldName, sizeof(oh->name));
- else
- memset(oh->name, 0, sizeof(oh->name));
-
- oh->isShrink = isShrink;
-
- switch (in->variantType) {
- case YAFFS_OBJECT_TYPE_UNKNOWN:
- /* Should not happen */
- break;
- case YAFFS_OBJECT_TYPE_FILE:
- oh->fileSize =
- (oh->parentObjectId == YAFFS_OBJECTID_DELETED
- || oh->parentObjectId ==
- YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
- fileVariant.fileSize;
- break;
- case YAFFS_OBJECT_TYPE_HARDLINK:
- oh->equivalentObjectId =
- in->variant.hardLinkVariant.equivalentObjectId;
- break;
- case YAFFS_OBJECT_TYPE_SPECIAL:
- /* Do nothing */
- break;
- case YAFFS_OBJECT_TYPE_DIRECTORY:
- /* Do nothing */
- break;
- case YAFFS_OBJECT_TYPE_SYMLINK:
- alias = in->variant.symLinkVariant.alias;
- if(!alias)
- alias = _Y("no alias");
- yaffs_strncpy(oh->alias,
- alias,
- YAFFS_MAX_ALIAS_LENGTH);
- oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
- break;
- }
-
- /* Tags */
- yaffs_InitialiseTags(&newTags);
- in->serial++;
- newTags.chunkId = 0;
- newTags.objectId = in->objectId;
- newTags.serialNumber = in->serial;
-
- /* Add extra info for file header */
-
- newTags.extraHeaderInfoAvailable = 1;
- newTags.extraParentObjectId = oh->parentObjectId;
- newTags.extraFileLength = oh->fileSize;
- newTags.extraIsShrinkHeader = oh->isShrink;
- newTags.extraEquivalentObjectId = oh->equivalentObjectId;
- newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
- newTags.extraObjectType = in->variantType;
-
- yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
-
- /* Create new chunk in NAND */
- newChunkId =
- yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
- (prevChunkId > 0) ? 1 : 0);
-
- if (newChunkId >= 0) {
-
- in->hdrChunk = newChunkId;
-
- if (prevChunkId > 0) {
- yaffs_DeleteChunk(dev, prevChunkId, 1,
- __LINE__);
- }
-
- if (!yaffs_ObjectHasCachedWriteData(in))
- in->dirty = 0;
-
- /* If this was a shrink, then mark the block that the chunk lives on */
- if (isShrink) {
- bi = yaffs_GetBlockInfo(in->myDev,
- newChunkId / in->myDev->param.nChunksPerBlock);
- bi->hasShrinkHeader = 1;
- }
-
- }
-
- retVal = newChunkId;
-
- }
-
- if (buffer)
- yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
-
- return retVal;
-}
-
-/*------------------------ Short Operations Cache ----------------------------------------
- * In many situations where there is no high level buffering (eg WinCE) a lot of
- * reads might be short sequential reads, and a lot of writes may be short
- * sequential writes. eg. scanning/writing a jpeg file.
- * In these cases, a short read/write cache can provide a huge perfomance benefit
- * with dumb-as-a-rock code.
- * In Linux, the page cache provides read buffering aand the short op cache provides write
- * buffering.
- *
- * There are a limited number (~10) of cache chunks per device so that we don't
- * need a very intelligent search.
- */
-
-static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
-{
- yaffs_Device *dev = obj->myDev;
- int i;
- yaffs_ChunkCache *cache;
- int nCaches = obj->myDev->param.nShortOpCaches;
-
- for (i = 0; i < nCaches; i++) {
- cache = &dev->srCache[i];
- if (cache->object == obj &&
- cache->dirty)
- return 1;
- }
-
- return 0;
-}
-
-
-static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
-{
- yaffs_Device *dev = obj->myDev;
- int lowest = -99; /* Stop compiler whining. */
- int i;
- yaffs_ChunkCache *cache;
- int chunkWritten = 0;
- int nCaches = obj->myDev->param.nShortOpCaches;
-
- if (nCaches > 0) {
- do {
- cache = NULL;
-
- /* Find the dirty cache for this object with the lowest chunk id. */
- for (i = 0; i < nCaches; i++) {
- if (dev->srCache[i].object == obj &&
- dev->srCache[i].dirty) {
- if (!cache
- || dev->srCache[i].chunkId <
- lowest) {
- cache = &dev->srCache[i];
- lowest = cache->chunkId;
- }
- }
- }
-
- if (cache && !cache->locked) {
- /* Write it out and free it up */
-
- chunkWritten =
- yaffs_WriteChunkDataToObject(cache->object,
- cache->chunkId,
- cache->data,
- cache->nBytes,
- 1);
- cache->dirty = 0;
- cache->object = NULL;
- }
-
- } while (cache && chunkWritten > 0);
-
- if (cache) {
- /* Hoosterman, disk full while writing cache out. */
- T(YAFFS_TRACE_ERROR,
- (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
-
- }
- }
-
-}
-
-/*yaffs_FlushEntireDeviceCache(dev)
- *
- *
- */
-
-void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
-{
- yaffs_Object *obj;
- int nCaches = dev->param.nShortOpCaches;
- int i;
-
- /* Find a dirty object in the cache and flush it...
- * until there are no further dirty objects.
- */
- do {
- obj = NULL;
- for (i = 0; i < nCaches && !obj; i++) {
- if (dev->srCache[i].object &&
- dev->srCache[i].dirty)
- obj = dev->srCache[i].object;
-
- }
- if (obj)
- yaffs_FlushFilesChunkCache(obj);
-
- } while (obj);
-
-}
-
-
-/* Grab us a cache chunk for use.
- * First look for an empty one.
- * Then look for the least recently used non-dirty one.
- * Then look for the least recently used dirty one...., flush and look again.
- */
-static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
-{
- int i;
-
- if (dev->param.nShortOpCaches > 0) {
- for (i = 0; i < dev->param.nShortOpCaches; i++) {
- if (!dev->srCache[i].object)
- return &dev->srCache[i];
- }
- }
-
- return NULL;
-}
-
-static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
-{
- yaffs_ChunkCache *cache;
- yaffs_Object *theObj;
- int usage;
- int i;
- int pushout;
-
- if (dev->param.nShortOpCaches > 0) {
- /* Try find a non-dirty one... */
-
- cache = yaffs_GrabChunkCacheWorker(dev);
-
- if (!cache) {
- /* They were all dirty, find the last recently used object and flush
- * its cache, then find again.
- * NB what's here is not very accurate, we actually flush the object
- * the last recently used page.
- */
-
- /* With locking we can't assume we can use entry zero */
-
- theObj = NULL;
- usage = -1;
- cache = NULL;
- pushout = -1;
-
- for (i = 0; i < dev->param.nShortOpCaches; i++) {
- if (dev->srCache[i].object &&
- !dev->srCache[i].locked &&
- (dev->srCache[i].lastUse < usage || !cache)) {
- usage = dev->srCache[i].lastUse;
- theObj = dev->srCache[i].object;
- cache = &dev->srCache[i];
- pushout = i;
- }
- }
-
- if (!cache || cache->dirty) {
- /* Flush and try again */
- yaffs_FlushFilesChunkCache(theObj);
- cache = yaffs_GrabChunkCacheWorker(dev);
- }
-
- }
- return cache;
- } else
- return NULL;
-
-}
-
-/* Find a cached chunk */
-static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
- int chunkId)
-{
- yaffs_Device *dev = obj->myDev;
- int 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++;
-
- return &dev->srCache[i];
- }
- }
- }
- return NULL;
-}
-
-/* Mark the chunk for the least recently used algorithym */
-static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
- int isAWrite)
-{
-
- if (dev->param.nShortOpCaches > 0) {
- if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
- /* Reset the cache usages */
- int i;
- for (i = 1; i < dev->param.nShortOpCaches; i++)
- dev->srCache[i].lastUse = 0;
-
- dev->srLastUse = 0;
- }
-
- dev->srLastUse++;
-
- cache->lastUse = dev->srLastUse;
-
- if (isAWrite)
- cache->dirty = 1;
- }
-}
-
-/* Invalidate a single cache page.
- * Do this when a whole page gets written,
- * ie the short cache for this page is no longer valid.
- */
-static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
-{
- if (object->myDev->param.nShortOpCaches > 0) {
- yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
-
- if (cache)
- cache->object = NULL;
- }
-}
-
-/* Invalidate all the cache pages associated with this object
- * Do this whenever ther file is deleted or resized.
- */
-static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
-{
- int i;
- yaffs_Device *dev = in->myDev;
-
- if (dev->param.nShortOpCaches > 0) {
- /* Invalidate it. */
- for (i = 0; i < dev->param.nShortOpCaches; i++) {
- if (dev->srCache[i].object == in)
- dev->srCache[i].object = NULL;
- }
- }
-}
-
-/*--------------------- Checkpointing --------------------*/
-
-
-static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
-{
- yaffs_CheckpointValidity cp;
-
- memset(&cp, 0, sizeof(cp));
-
- cp.structType = sizeof(cp);
- cp.magic = YAFFS_MAGIC;
- cp.version = YAFFS_CHECKPOINT_VERSION;
- cp.head = (head) ? 1 : 0;
-
- return (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
- 1 : 0;
-}
-
-static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
-{
- yaffs_CheckpointValidity cp;
- int ok;
-
- ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
-
- if (ok)
- ok = (cp.structType == sizeof(cp)) &&
- (cp.magic == YAFFS_MAGIC) &&
- (cp.version == YAFFS_CHECKPOINT_VERSION) &&
- (cp.head == ((head) ? 1 : 0));
- return ok ? 1 : 0;
-}
-
-static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
- yaffs_Device *dev)
-{
- cp->nErasedBlocks = dev->nErasedBlocks;
- cp->allocationBlock = dev->allocationBlock;
- cp->allocationPage = dev->allocationPage;
- cp->nFreeChunks = dev->nFreeChunks;
-
- cp->nDeletedFiles = dev->nDeletedFiles;
- cp->nUnlinkedFiles = dev->nUnlinkedFiles;
- cp->nBackgroundDeletions = dev->nBackgroundDeletions;
- cp->sequenceNumber = dev->sequenceNumber;
-
-}
-
-static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
- yaffs_CheckpointDevice *cp)
-{
- dev->nErasedBlocks = cp->nErasedBlocks;
- dev->allocationBlock = cp->allocationBlock;
- dev->allocationPage = cp->allocationPage;
- dev->nFreeChunks = cp->nFreeChunks;
-
- dev->nDeletedFiles = cp->nDeletedFiles;
- dev->nUnlinkedFiles = cp->nUnlinkedFiles;
- dev->nBackgroundDeletions = cp->nBackgroundDeletions;
- dev->sequenceNumber = cp->sequenceNumber;
-}
-
-
-static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
-{
- yaffs_CheckpointDevice cp;
- __u32 nBytes;
- __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
-
- int ok;
-
- /* Write device runtime values*/
- yaffs_DeviceToCheckpointDevice(&cp, dev);
- cp.structType = sizeof(cp);
-
- ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
-
- /* Write block info */
- if (ok) {
- nBytes = nBlocks * sizeof(yaffs_BlockInfo);
- ok = (yaffs_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
- }
-
- /* Write chunk bits */
- if (ok) {
- nBytes = nBlocks * dev->chunkBitmapStride;
- ok = (yaffs_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
- }
- return ok ? 1 : 0;
-
-}
-
-static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
-{
- yaffs_CheckpointDevice cp;
- __u32 nBytes;
- __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
-
- int ok;
-
- ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
- if (!ok)
- return 0;
-
- if (cp.structType != sizeof(cp))
- return 0;
-
-
- yaffs_CheckpointDeviceToDevice(dev, &cp);
-
- nBytes = nBlocks * sizeof(yaffs_BlockInfo);
-
- ok = (yaffs_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
-
- if (!ok)
- return 0;
- nBytes = nBlocks * dev->chunkBitmapStride;
-
- ok = (yaffs_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
-
- return ok ? 1 : 0;
-}
-
-static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
- yaffs_Object *obj)
-{
-
- cp->objectId = obj->objectId;
- cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
- cp->hdrChunk = obj->hdrChunk;
- cp->variantType = obj->variantType;
- cp->deleted = obj->deleted;
- cp->softDeleted = obj->softDeleted;
- cp->unlinked = obj->unlinked;
- cp->fake = obj->fake;
- cp->renameAllowed = obj->renameAllowed;
- cp->unlinkAllowed = obj->unlinkAllowed;
- cp->serial = obj->serial;
- cp->nDataChunks = obj->nDataChunks;
-
- if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
- cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
- else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
- cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
-}
-
-static int yaffs_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
-{
-
- yaffs_Object *parent;
-
- if (obj->variantType != cp->variantType) {
- T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
- TCONT("chunk %d does not match existing object type %d")
- TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
- obj->variantType));
- return 0;
- }
-
- obj->objectId = cp->objectId;
-
- if (cp->parentId)
- parent = yaffs_FindOrCreateObjectByNumber(
- obj->myDev,
- cp->parentId,
- YAFFS_OBJECT_TYPE_DIRECTORY);
- else
- parent = NULL;
-
- if (parent) {
- if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
- T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
- TCONT(" chunk %d Parent type, %d, not directory")
- TENDSTR),
- cp->objectId, cp->parentId, cp->variantType,
- cp->hdrChunk, parent->variantType));
- return 0;
- }
- yaffs_AddObjectToDirectory(parent, obj);
- }
-
- obj->hdrChunk = cp->hdrChunk;
- obj->variantType = cp->variantType;
- obj->deleted = cp->deleted;
- obj->softDeleted = cp->softDeleted;
- obj->unlinked = cp->unlinked;
- obj->fake = cp->fake;
- obj->renameAllowed = cp->renameAllowed;
- obj->unlinkAllowed = cp->unlinkAllowed;
- obj->serial = cp->serial;
- obj->nDataChunks = cp->nDataChunks;
-
- if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
- obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
- else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
- obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
-
- if (obj->hdrChunk > 0)
- obj->lazyLoaded = 1;
- return 1;
-}
-
-
-
-static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
- __u32 level, int chunkOffset)
-{
- int i;
- yaffs_Device *dev = in->myDev;
- int ok = 1;
- int tnodeSize = yaffs_CalcTnodeSize(dev);
-
- if (tn) {
- if (level > 0) {
-
- for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
- if (tn->internal[i]) {
- ok = yaffs_CheckpointTnodeWorker(in,
- tn->internal[i],
- level - 1,
- (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
- }
- }
- } else if (level == 0) {
- __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
- ok = (yaffs_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
- if (ok)
- ok = (yaffs_CheckpointWrite(dev, tn, tnodeSize) == tnodeSize);
- }
- }
-
- return ok;
-
-}
-
-static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
-{
- __u32 endMarker = ~0;
- int ok = 1;
-
- if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
- ok = yaffs_CheckpointTnodeWorker(obj,
- obj->variant.fileVariant.top,
- obj->variant.fileVariant.topLevel,
- 0);
- if (ok)
- ok = (yaffs_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
- sizeof(endMarker));
- }
-
- return ok ? 1 : 0;
-}
-
-static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
-{
- __u32 baseChunk;
- int ok = 1;
- yaffs_Device *dev = obj->myDev;
- yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
- yaffs_Tnode *tn;
- int nread = 0;
- int tnodeSize = yaffs_CalcTnodeSize(dev);
-
- ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
-
- while (ok && (~baseChunk)) {
- nread++;
- /* Read level 0 tnode */
-
-
- tn = yaffs_GetTnodeRaw(dev);
- if (tn)
- ok = (yaffs_CheckpointRead(dev, tn, tnodeSize) == tnodeSize);
- else
- ok = 0;
-
- if (tn && ok)
- ok = yaffs_AddOrFindLevel0Tnode(dev,
- fileStructPtr,
- baseChunk,
- tn) ? 1 : 0;
-
- if (ok)
- ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
-
- }
-
- T(YAFFS_TRACE_CHECKPOINT, (
- TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
- nread, baseChunk, ok));
-
- return ok ? 1 : 0;
-}
-
-
-static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
-{
- yaffs_Object *obj;
- yaffs_CheckpointObject cp;
- int i;
- int ok = 1;
- struct ylist_head *lh;
-
-
- /* Iterate through the objects in each hash entry,
- * dumping them to the checkpointing stream.
- */
-
- for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
- ylist_for_each(lh, &dev->objectBucket[i].list) {
- if (lh) {
- obj = ylist_entry(lh, yaffs_Object, hashLink);
- if (!obj->deferedFree) {
- yaffs_ObjectToCheckpointObject(&cp, obj);
- cp.structType = sizeof(cp);