- yaffs_UseChunkCache(dev, cache, 0);
-
- cache->locked = 1;
-
-
- memcpy(buffer, &cache->data[start], nToCopy);
-
- cache->locked = 0;
- } else {
- /* Read into the local buffer then copy..*/
-
- __u8 *localBuffer =
- yaffs_GetTempBuffer(dev, __LINE__);
- yaffs_ReadChunkDataFromObject(in, chunk,
- localBuffer);
-
- memcpy(buffer, &localBuffer[start], nToCopy);
-
-
- yaffs_ReleaseTempBuffer(dev, localBuffer,
- __LINE__);
- }
-
- } else {
-
- /* A full chunk. Read directly into the supplied buffer. */
- yaffs_ReadChunkDataFromObject(in, chunk, buffer);
-
- }
-
- n -= nToCopy;
- offset += nToCopy;
- buffer += nToCopy;
- nDone += nToCopy;
-
- }
-
- return nDone;
-}
-
-int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
- int nBytes, int writeThrough)
-{
-
- int chunk;
- __u32 start;
- int nToCopy;
- int n = nBytes;
- int nDone = 0;
- int nToWriteBack;
- int startOfWrite = offset;
- int chunkWritten = 0;
- __u32 nBytesRead;
- __u32 chunkStart;
-
- yaffs_Device *dev;
-
- 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 ||
- start >= dev->nDataBytesPerChunk){
- T(YAFFS_TRACE_ERROR,(
- TSTR("AddrToChunk of offset %d gives chunk %d start %d"
- TENDSTR),
- (int)offset, chunk,start));
- }
- chunk++;
-
- /* OK now check for the curveball where the start and end are in
- * the same chunk.
- */
-
- if ((start + n) < dev->nDataBytesPerChunk) {
- nToCopy = n;
-
- /* Now folks, to calculate how many bytes to write back....
- * If we're overwriting and not writing to then end of file then
- * we need to write back as much as was there before.
- */
-
- chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
-
- if(chunkStart > in->variant.fileVariant.fileSize)
- nBytesRead = 0; /* Past end of file */
- else
- nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
-
- if (nBytesRead > dev->nDataBytesPerChunk) {
- nBytesRead = dev->nDataBytesPerChunk;
- }
-
- nToWriteBack =
- (nBytesRead >
- (start + n)) ? nBytesRead : (start + n);
-
- if(nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
- YBUG();
-
- } else {
- nToCopy = dev->nDataBytesPerChunk - start;
- nToWriteBack = dev->nDataBytesPerChunk;
- }
-
- if (nToCopy != dev->nDataBytesPerChunk || dev->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) {
- 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);
- cache->object = in;
- cache->chunkId = chunk;
- cache->dirty = 0;
- cache->locked = 0;
- yaffs_ReadChunkDataFromObject(in, chunk,
- cache->
- data);
- }
- else if(cache &&
- !cache->dirty &&
- !yaffs_CheckSpaceForAllocation(in->myDev)){
- /* Drop the cache if it was a read cache item and
- * no space check has been made for it.
- */
- cache = NULL;
- }
-
- if (cache) {
- yaffs_UseChunkCache(dev, cache, 1);
- cache->locked = 1;
-
-
- memcpy(&cache->data[start], buffer,
- nToCopy);
-
-
- 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);
-
-
-
- memcpy(&localBuffer[start], buffer, nToCopy);
-
- chunkWritten =
- yaffs_WriteChunkDataToObject(in, chunk,
- localBuffer,
- nToWriteBack,
- 0);
-
- yaffs_ReleaseTempBuffer(dev, localBuffer,
- __LINE__);
-
- }
-
- } else {
- /* A full chunk. Write directly from the supplied buffer. */
-
-
-
- chunkWritten =
- yaffs_WriteChunkDataToObject(in, chunk, buffer,
- dev->nDataBytesPerChunk,
- 0);
-
- /* Since we've overwritten the cached data, we better invalidate it. */
- yaffs_InvalidateChunkCache(in, chunk);
- }
-
- 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);
- }
-
- in->dirty = 1;
-
- return nDone;
-}
-
-
-/* ---------------------- File resizing stuff ------------------ */
-
-static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
-{
-
- yaffs_Device *dev = in->myDev;
- int oldFileSize = in->variant.fileVariant.fileSize;
-
- int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
-
- int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
- dev->nDataBytesPerChunk;
- int i;
- int chunkId;
-
- /* Delete backwards so that we don't end up with holes if
- * power is lost part-way through the operation.
- */
- for (i = lastDel; i >= startDel; i--) {
- /* NB this could be optimised somewhat,
- * eg. could retrieve the tags and write them without
- * using yaffs_DeleteChunk
- */
-
- chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
- if (chunkId > 0) {
- if (chunkId <
- (dev->internalStartBlock * dev->nChunksPerBlock)
- || chunkId >=
- ((dev->internalEndBlock +
- 1) * dev->nChunksPerBlock)) {
- T(YAFFS_TRACE_ALWAYS,
- (TSTR("Found daft chunkId %d for %d" TENDSTR),
- chunkId, i));
- } else {
- in->nDataChunks--;
- yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
- }
- }
- }
-
-}
-
-int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
-{
-
- int oldFileSize = in->variant.fileVariant.fileSize;
- __u32 newSizeOfPartialChunk;
- int newFullChunks;
-
- yaffs_Device *dev = in->myDev;
-
- yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
-
- yaffs_FlushFilesChunkCache(in);
- yaffs_InvalidateWholeChunkCache(in);
-
- yaffs_CheckGarbageCollection(dev);
-
- if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
- return YAFFS_FAIL;
- }
-
- if (newSize == oldFileSize) {
- return YAFFS_OK;
- }
-
- if (newSize < oldFileSize) {
-
- yaffs_PruneResizedChunks(in, newSize);
-
- if (newSizeOfPartialChunk != 0) {
- int lastChunk = 1 + newFullChunks;
-
- __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
-
- /* Got to read and rewrite the last chunk with its new size and zero pad */
- yaffs_ReadChunkDataFromObject(in, lastChunk,
- localBuffer);
-
- memset(localBuffer + newSizeOfPartialChunk, 0,
- dev->nDataBytesPerChunk - newSizeOfPartialChunk);
-
- yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
- newSizeOfPartialChunk, 1);
-
- yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
- }
-
- in->variant.fileVariant.fileSize = newSize;
-
- yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
- } else {
- /* newsSize > oldFileSize */
- in->variant.fileVariant.fileSize = newSize;
- }
-
-
-
- /* Write a new object header.
- * show we've shrunk the file, if need be
- * Do this only if the file is not in the deleted directories.
- */
- if (in->parent &&
- in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
- in->parent->objectId != YAFFS_OBJECTID_DELETED) {
- yaffs_UpdateObjectHeader(in, NULL, 0,
- (newSize < oldFileSize) ? 1 : 0, 0);
- }
-
- return YAFFS_OK;
-}
-
-loff_t yaffs_GetFileSize(yaffs_Object * obj)
-{
- 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);
- default:
- return 0;
- }
-}
-
-
-
-int yaffs_FlushFile(yaffs_Object * in, int updateTime)
-{
- int retVal;
- if (in->dirty) {
- yaffs_FlushFilesChunkCache(in);
- if (updateTime) {
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_WinFileTimeNow(in->win_mtime);
-#else
-
- in->yst_mtime = Y_CURRENT_TIME;
-
-#endif
- }
-
- retVal =
- (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
- 0) ? YAFFS_OK : YAFFS_FAIL;
- } else {
- retVal = YAFFS_OK;
- }
-
- return retVal;
-
-}
-
-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)) {
- /* Move to the unlinked directory so we have a record that it was deleted. */
- yaffs_ChangeObjectName(in, in->myDev->deletedDir,_Y("deleted"), 0, 0);
-
- }
-
- yaffs_RemoveObjectFromDirectory(in);
- yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
- in->hdrChunk = 0;
-
- yaffs_FreeObject(in);
- return YAFFS_OK;
-
-}
-
-/* yaffs_DeleteFile deletes the whole file data
- * and the inode associated with the file.
- * It does not delete the links associated with the file.
- */
-static int yaffs_UnlinkFileIfNeeded(yaffs_Object * in)
-{
-
- int retVal;
- int immediateDeletion = 0;
-
-#ifdef __KERNEL__
- if (!in->myInode) {
- immediateDeletion = 1;
- }
-#else
- if (in->inUse <= 0) {
- immediateDeletion = 1;
- }
-#endif
-
- if (immediateDeletion) {
- retVal =
- yaffs_ChangeObjectName(in, in->myDev->deletedDir,
- _Y("deleted"), 0, 0);
- T(YAFFS_TRACE_TRACING,
- (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
- in->objectId));
- in->deleted = 1;
- in->myDev->nDeletedFiles++;
- if (1 || in->myDev->isYaffs2) {
- yaffs_ResizeFile(in, 0);
- }
- yaffs_SoftDeleteFile(in);
- } else {
- retVal =
- yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
- _Y("unlinked"), 0, 0);
- }
-
-
- return retVal;
-}
-
-int yaffs_DeleteFile(yaffs_Object * in)
-{
- int retVal = YAFFS_OK;
- int deleted = in->deleted;
-
- 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) {
- retVal = yaffs_UnlinkFileIfNeeded(in);
- }
- if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
- in->deleted = deleted = 1;
- in->myDev->nDeletedFiles++;
- yaffs_SoftDeleteFile(in);
- }
- return deleted ? YAFFS_OK : YAFFS_FAIL;
- } else {
- /* The file has no data chunks so we toss it immediately */
- yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
- in->variant.fileVariant.top = NULL;
- yaffs_DoGenericObjectDeletion(in);
-
- return YAFFS_OK;
- }
-}
-
-static int yaffs_DeleteDirectory(yaffs_Object * in)
-{
- /* First check that the directory is empty. */
- if (ylist_empty(&in->variant.directoryVariant.children)) {
- return yaffs_DoGenericObjectDeletion(in);
- }
-
- return YAFFS_FAIL;
-
-}
-
-static int yaffs_DeleteSymLink(yaffs_Object * in)
-{
- YFREE(in->variant.symLinkVariant.alias);
-
- return yaffs_DoGenericObjectDeletion(in);
-}
-
-static int yaffs_DeleteHardLink(yaffs_Object * in)
-{
- /* remove this hardlink from the list assocaited with the equivalent
- * object
- */
- ylist_del_init(&in->hardLinks);
- return yaffs_DoGenericObjectDeletion(in);
-}
-
-int yaffs_DeleteObject(yaffs_Object * obj)
-{
-int retVal = -1;
- switch (obj->variantType) {
- case YAFFS_OBJECT_TYPE_FILE:
- retVal = yaffs_DeleteFile(obj);
- break;
- case YAFFS_OBJECT_TYPE_DIRECTORY:
- return yaffs_DeleteDirectory(obj);
- break;
- case YAFFS_OBJECT_TYPE_SYMLINK:
- retVal = yaffs_DeleteSymLink(obj);
- break;
- case YAFFS_OBJECT_TYPE_HARDLINK:
- retVal = yaffs_DeleteHardLink(obj);
- break;
- case YAFFS_OBJECT_TYPE_SPECIAL:
- retVal = yaffs_DoGenericObjectDeletion(obj);
- break;
- case YAFFS_OBJECT_TYPE_UNKNOWN:
- retVal = 0;
- break; /* should not happen. */
- }
-
- return retVal;
-}
-
-static int yaffs_UnlinkWorker(yaffs_Object * obj)
-{
-
- int immediateDeletion = 0;
-
-#ifdef __KERNEL__
- if (!obj->myInode) {
- immediateDeletion = 1;
- }
-#else
- if (obj->inUse <= 0) {
- 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
- * 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)
- * - Rename the object to the hardlink's name.
- * - Delete the hardlink
- */
-
- yaffs_Object *hl;
- int retVal;
- YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
-
- hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
-
- ylist_del_init(&hl->hardLinks);
- ylist_del_init(&hl->siblings);
-
- yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
-
- retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
-
- if (retVal == YAFFS_OK) {
- retVal = yaffs_DoGenericObjectDeletion(hl);
- }
- return retVal;
-
- } else if(immediateDeletion){
- switch (obj->variantType) {
- case YAFFS_OBJECT_TYPE_FILE:
- return yaffs_DeleteFile(obj);
- break;
- case YAFFS_OBJECT_TYPE_DIRECTORY:
- return yaffs_DeleteDirectory(obj);
- break;
- case YAFFS_OBJECT_TYPE_SYMLINK:
- return yaffs_DeleteSymLink(obj);
- break;
- case YAFFS_OBJECT_TYPE_SPECIAL:
- return yaffs_DoGenericObjectDeletion(obj);
- break;
- case YAFFS_OBJECT_TYPE_HARDLINK:
- case YAFFS_OBJECT_TYPE_UNKNOWN:
- default:
- 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) {
- return yaffs_UnlinkWorker(obj);
- }
-
- return YAFFS_FAIL;
-
-}
-int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
-{
- yaffs_Object *obj;
-
- obj = yaffs_FindObjectByName(dir, name);
- return yaffs_UnlinkObject(obj);
-}
-
-/*----------------------- Initialisation Scanning ---------------------- */
-
-static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
- int backwardScanning)
-{
- yaffs_Object *obj;
-
- if (!backwardScanning) {
- /* Handle YAFFS1 forward scanning case
- * For YAFFS1 we always do the deletion
- */
-
- } else {
- /* Handle YAFFS2 case (backward scanning)
- * If the shadowed object exists then ignore.
- */
- if (yaffs_FindObjectByNumber(dev, objId)) {
- return;
- }
- }
-
- /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
- * We put it in unlinked dir to be cleaned up after the scanning
- */
- obj =
- yaffs_FindOrCreateObjectByNumber(dev, objId,
- YAFFS_OBJECT_TYPE_FILE);
- if (!obj)
- return;
- 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 */
-
-}
-
-typedef struct {
- int seq;
- int block;
-} yaffs_BlockIndex;
-
-
-static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
-{
- yaffs_Object *hl;
- yaffs_Object *in;
-
- while (hardList) {
- hl = hardList;
- hardList = (yaffs_Object *) (hardList->hardLinks.next);
-
- in = yaffs_FindObjectByNumber(dev,
- hl->variant.hardLinkVariant.
- equivalentObjectId);
-
- if (in) {
- /* Add the hardlink pointers */
- hl->variant.hardLinkVariant.equivalentObject = in;
- ylist_add(&hl->hardLinks, &in->hardLinks);
- } else {
- /* Todo Need to report/handle this better.
- * Got a problem... hardlink to a non-existant object
- */
- hl->variant.hardLinkVariant.equivalentObject = NULL;
- YINIT_LIST_HEAD(&hl->hardLinks);
-
- }
-
- }
-
-}
-
-
-
-
-
-static int ybicmp(const void *a, const void *b){
- register int aseq = ((yaffs_BlockIndex *)a)->seq;
- register int bseq = ((yaffs_BlockIndex *)b)->seq;
- register int ablock = ((yaffs_BlockIndex *)a)->block;
- register int bblock = ((yaffs_BlockIndex *)b)->block;
- if( aseq == bseq )
- return ablock - bblock;
- else
- return aseq - bseq;
-
-}
-
-
-struct yaffs_ShadowFixerStruct {
- int objectId;
- int shadowedId;
- struct yaffs_ShadowFixerStruct *next;
-};
-
-
-static void yaffs_StripDeletedObjects(yaffs_Device *dev)
-{
- /*
- * Sort out state of unlinked and deleted objects after scanning.
- */
- struct ylist_head *i;
- struct ylist_head *n;
- yaffs_Object *l;
-
- /* Soft delete all the unlinked files */
- ylist_for_each_safe(i, n,
- &dev->unlinkedDir->variant.directoryVariant.children) {
- if (i) {
- l = ylist_entry(i, yaffs_Object, siblings);
- yaffs_DeleteObject(l);
- }
- }
-
- ylist_for_each_safe(i, n,
- &dev->deletedDir->variant.directoryVariant.children) {
- if (i) {
- l = ylist_entry(i, yaffs_Object, siblings);
- yaffs_DeleteObject(l);