- 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);