- else
- {
- nToCopy = dev->nBytesPerChunk - start;
- nToWriteBack = dev->nBytesPerChunk;
- }
-
- if(nToCopy != dev->nBytesPerChunk)
- {
- // An incomplete start or end chunk (or maybe both start and end chunk)
- if(dev->nShortOpCaches > 0)
- {
- yaffs_ChunkCache *cache;
- // If we can't find the data in the cache, then load it up.
- cache = yaffs_FindChunkCache(in,chunk);
- if(!cache && yaffs_CheckSpaceForAllocation(in->myDev))
- {
- cache = yaffs_GrabChunkCache(in->myDev);
- cache->object = in;
- cache->chunkId = chunk;
- cache->dirty = 0;
- cache->locked = 0;
- yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
- }
-
- if(cache)
- {
- yaffs_UseChunkCache(dev,cache,1);
- cache->locked = 1;
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_UnlockYAFFS(TRUE);
-#endif
-
- memcpy(&cache->data[start],buffer,nToCopy);
-
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_LockYAFFS(TRUE);
-#endif
- cache->locked = 0;
- cache->nBytes = nToWriteBack;
-
- if(writeThrough)
- {
- chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
- cache->chunkId,
- cache->data,
- cache->nBytes,1);
- cache->dirty = 0;
- }
-
- }
- else
- {
- chunkWritten = -1; // fail the write
- }
- }
- else
- {
- // An incomplete start or end chunk (or maybe both start and end chunk)
- // Read into the local buffer then copy, then copy over and write back.
-
- __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
-
- yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
-
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_UnlockYAFFS(TRUE);
-#endif
-
- memcpy(&localBuffer[start],buffer,nToCopy);
-
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_LockYAFFS(TRUE);
-#endif
- chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
-
- yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
-
- //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
- }
-
- }
- else
- {
-
-#ifdef CONFIG_YAFFS_WINCE
- // Under WinCE can't do direct transfer. Need to use a local buffer.
- // This is because we otherwise screw up WinCE's memory mapper
- __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_UnlockYAFFS(TRUE);
-#endif
- memcpy(localBuffer,buffer,dev->nBytesPerChunk);
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_LockYAFFS(TRUE);
-#endif
- chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
- yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
-#else
- // A full chunk. Write directly from the supplied buffer.
- chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
-#endif
- // Since we've overwritten the cached data, we better invalidate it.
- yaffs_InvalidateChunkCache(in,chunk);
- //T(("Write to chunk %d %d\n",chunk,chunkWritten));
- }
-
- if(chunkWritten >= 0)
- {
- n -= nToCopy;
- offset += nToCopy;
- buffer += nToCopy;
- nDone += nToCopy;
- }
-
- }
-
- // Update file object
-
- if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
- {
- in->variant.fileVariant.fileSize = (startOfWrite + nDone);