-}
-
-/* Pruning removes any part of the file structure tree that is beyond the
- * bounds of the file (ie that does not point to chunks).
- *
- * A file should only get pruned when its size is reduced.
- *
- * Before pruning, the chunks must be pulled from the tree and the
- * 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,
- __u32 level, int del0)
-{
- int i;
- int hasData;
-
- if (tn) {
- hasData = 0;
-
- 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++;
- }
- } 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 */
-
- yaffs_FreeTnode(dev, tn);
- tn = NULL;
- }
-
- }
-
- return tn;
-
-}
-
-static int yaffs_PruneFileStructure(yaffs_Device *dev,
- yaffs_FileStructure *fStruct)
-{
- int i;
- int hasData;
- int done = 0;
- yaffs_Tnode *tn;
-
- if (fStruct->topLevel > 0) {
- fStruct->top =
- yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
-
- /* Now we have a tree with all the non-zero branches NULL but the height
- * is the same as it was.
- * Let's see if we can trim internal tnodes to shorten the tree.
- * We can do this if only the 0th element in the tnode is in use
- * (ie all the non-zero are NULL)
- */
-
- while (fStruct->topLevel && !done) {
- tn = fStruct->top;
-
- hasData = 0;
- for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
- if (tn->internal[i])
- hasData++;
- }
-
- if (!hasData) {
- fStruct->top = tn->internal[0];
- fStruct->topLevel--;
- yaffs_FreeTnode(dev, tn);
- } else {
- done = 1;
- }
- }
- }
-
- return YAFFS_OK;
-}
-
-/*-------------------- End of File Structure functions.-------------------*/
-
-/* yaffs_CreateFreeObjects creates a bunch more objects and
- * adds them to the object free list.
- */
-static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
-{
- int i;
- yaffs_Object *newObjects;
- yaffs_ObjectList *list;
-
- if (nObjects < 1)
- return YAFFS_OK;
-
- /* make these things */
- newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
- list = YMALLOC(sizeof(yaffs_ObjectList));
-
- if (!newObjects || !list) {
- if (newObjects){
- YFREE(newObjects);
- newObjects = NULL;
- }
- if (list){
- YFREE(list);
- list = NULL;
- }
- T(YAFFS_TRACE_ALLOCATE,
- (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
- return YAFFS_FAIL;
- }
-
- /* Hook them into the free list */
- for (i = 0; i < nObjects - 1; i++) {
- newObjects[i].siblings.next =
- (struct ylist_head *)(&newObjects[i + 1]);
- }
-
- newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
- dev->freeObjects = newObjects;
- dev->nFreeObjects += nObjects;
- dev->nObjectsCreated += nObjects;
-
- /* Now add this bunch of Objects to a list for freeing up. */
-
- list->objects = newObjects;
- list->next = dev->allocatedObjectList;
- dev->allocatedObjectList = list;
-
- return YAFFS_OK;
-}
-
-
-/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
-static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
-{
- yaffs_Object *tn = NULL;
-
-#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)
- yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
-
- if (dev->freeObjects) {
- tn = dev->freeObjects;
- dev->freeObjects =
- (yaffs_Object *) (dev->freeObjects->siblings.next);
- dev->nFreeObjects--;
- }
-#endif
- if (tn) {
- /* Now sweeten it up... */
-
- memset(tn, 0, sizeof(yaffs_Object));
- tn->beingCreated = 1;
-
- tn->myDev = dev;
- tn->hdrChunk = 0;
- tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
- YINIT_LIST_HEAD(&(tn->hardLinks));
- YINIT_LIST_HEAD(&(tn->hashLink));
- YINIT_LIST_HEAD(&tn->siblings);
-
-
- /* Now make the directory sane */
- if (dev->rootDir) {
- tn->parent = dev->rootDir;
- ylist_add(&(tn->siblings), &dev->rootDir->variant.directoryVariant.children);
- }
-
- /* Add it to the lost and found directory.
- * NB Can't put root or lostNFound in lostNFound so
- * check if lostNFound exists first
- */
- if (dev->lostNFoundDir)
- yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
-
- tn->beingCreated = 0;
- }
-
- dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
-
- return tn;
-}
-
-static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
- __u32 mode)
-{
-
- yaffs_Object *obj =
- yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
- if (obj) {
- obj->fake = 1; /* it is fake so it might have no NAND presence... */
- obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
- obj->unlinkAllowed = 0; /* ... or unlink it */
- obj->deleted = 0;
- obj->unlinked = 0;
- obj->yst_mode = mode;
- obj->myDev = dev;
- obj->hdrChunk = 0; /* Not a valid chunk. */
- }
-
- return obj;
-
-}
-
-static void yaffs_UnhashObject(yaffs_Object *tn)
-{
- int bucket;
- yaffs_Device *dev = tn->myDev;
-
- /* If it is still linked into the bucket list, free from the list */
- if (!ylist_empty(&tn->hashLink)) {
- ylist_del_init(&tn->hashLink);
- bucket = yaffs_HashFunction(tn->objectId);
- dev->objectBucket[bucket].count--;
- }
-}
-
-/* FreeObject frees up a Object and puts it back on the free list */
-static void yaffs_FreeObject(yaffs_Object *tn)
-{
- yaffs_Device *dev = tn->myDev;
-
- 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();
-
-
- if (tn->myInode) {
- /* We're still hooked up to a cached inode.
- * Don't delete now, but mark for later deletion
- */
- tn->deferedFree = 1;
- return;
- }
-
- yaffs_UnhashObject(tn);
-
-#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);
- dev->freeObjects = tn;
- dev->nFreeObjects++;
-#endif
- dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
-}
-
-
-void yaffs_HandleDeferedFree(yaffs_Object *obj)
-{
- if (obj->deferedFree)
- yaffs_FreeObject(obj);
-}
-
-
-static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
-{
- /* Free the list of allocated Objects */
-
- yaffs_ObjectList *tmp;
-
- while (dev->allocatedObjectList) {
- tmp = dev->allocatedObjectList->next;
- YFREE(dev->allocatedObjectList->objects);
- YFREE(dev->allocatedObjectList);
-
- dev->allocatedObjectList = tmp;
- }
-
- dev->freeObjects = NULL;
- dev->nFreeObjects = 0;
- dev->nObjectsCreated = 0;
-}
-
-static void yaffs_InitialiseObjects(yaffs_Device *dev)
-{
- int i;
-
- dev->allocatedObjectList = NULL;
- dev->freeObjects = NULL;
- dev->nFreeObjects = 0;
-
- for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
- YINIT_LIST_HEAD(&dev->objectBucket[i].list);
- dev->objectBucket[i].count = 0;
- }
-}
-
-static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
-{
- int i;
- int l = 999;
- int lowest = 999999;
-
-
- /* Search for the shortest list or one that
- * isn't too long.
- */
-
- 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;
- }
-
- }
-
- return l;
-}
-
-static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
-{
- int bucket = yaffs_FindNiceObjectBucket(dev);
-
- /* Now find an object value that has not already been taken
- * by scanning the list.
- */
-
- int found = 0;
- struct ylist_head *i;
-
- __u32 n = (__u32) bucket;
-
- /* yaffs_CheckObjectHashSanity(); */
-
- while (!found) {
- found = 1;
- n += YAFFS_NOBJECT_BUCKETS;
- if (1 || dev->objectBucket[bucket].count > 0) {
- ylist_for_each(i, &dev->objectBucket[bucket].list) {
- /* If there is already one in the list */
- if (i && ylist_entry(i, yaffs_Object,
- hashLink)->objectId == n) {
- found = 0;
- }
- }
- }
- }
-
- return n;
-}
-
-static void yaffs_HashObject(yaffs_Object *in)
-{
- int bucket = yaffs_HashFunction(in->objectId);
- yaffs_Device *dev = in->myDev;
-
- ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
- dev->objectBucket[bucket].count++;
-}
-
-yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
-{
- int bucket = yaffs_HashFunction(number);
- struct ylist_head *i;
- yaffs_Object *in;
-
- ylist_for_each(i, &dev->objectBucket[bucket].list) {
- /* Look if it is in the list */
- if (i) {
- in = ylist_entry(i, yaffs_Object, hashLink);
- if (in->objectId == number) {
-
- /* Don't tell the VFS about this one if it is defered free */
- if (in->deferedFree)
- return NULL;
-
- return in;
- }
- }
- }
-
- return NULL;
-}
-
-yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
- yaffs_ObjectType type)
-{
- yaffs_Object *theObject=NULL;
- yaffs_Tnode *tn = NULL;
-
- if (number < 0)
- number = yaffs_CreateNewObjectNumber(dev);
-
- if (type == YAFFS_OBJECT_TYPE_FILE) {
- tn = yaffs_GetTnode(dev);
- 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;
- theObject->unlinkAllowed = 1;
- theObject->objectId = number;
- yaffs_HashObject(theObject);
- theObject->variantType = type;
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_WinFileTimeNow(theObject->win_atime);
- theObject->win_ctime[0] = theObject->win_mtime[0] =
- theObject->win_atime[0];
- theObject->win_ctime[1] = theObject->win_mtime[1] =
- theObject->win_atime[1];
-
-#else
-
- theObject->yst_atime = theObject->yst_mtime =
- theObject->yst_ctime = Y_CURRENT_TIME;
-#endif
- switch (type) {
- case YAFFS_OBJECT_TYPE_FILE:
- theObject->variant.fileVariant.fileSize = 0;
- theObject->variant.fileVariant.scannedFileSize = 0;
- theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
- theObject->variant.fileVariant.topLevel = 0;
- theObject->variant.fileVariant.top = tn;
- break;
- 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:
- case YAFFS_OBJECT_TYPE_SPECIAL:
- /* No action required */
- break;
- case YAFFS_OBJECT_TYPE_UNKNOWN:
- /* todo this should not happen */
- break;
- }
- }
-
- return theObject;
-}
-
-static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
- int number,
- yaffs_ObjectType type)
-{
- yaffs_Object *theObject = NULL;
-
- if (number > 0)
- theObject = yaffs_FindObjectByNumber(dev, number);
-
- if (!theObject)
- theObject = yaffs_CreateNewObject(dev, number, type);
-
- return theObject;
-
-}
-
-
-static YCHAR *yaffs_CloneString(const YCHAR *str)
-{
- YCHAR *newStr = NULL;
- int len;
-
- 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;
-
-}
-
-/*
- * Mknod (create) a new object.
- * equivalentObject only has meaning for a hard link;
- * aliasString only has meaning for a symlink.
- * rdev only has meaning for devices (a subset of special objects)
- */
-
-static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
- yaffs_Object *parent,
- const YCHAR *name,
- __u32 mode,
- __u32 uid,
- __u32 gid,
- yaffs_Object *equivalentObject,
- const YCHAR *aliasString, __u32 rdev)
-{
- yaffs_Object *in;
- YCHAR *str = NULL;
-
- 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))
- return NULL;
-
- if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
- str = yaffs_CloneString(aliasString);
- if (!str)
- return NULL;
- }
-
- in = yaffs_CreateNewObject(dev, -1, type);
-
- if (!in){
- if(str)
- YFREE(str);
- return NULL;
- }
-
-
-
-
-
- if (in) {
- in->hdrChunk = 0;
- in->valid = 1;
- in->variantType = type;
-
- in->yst_mode = mode;
-
-#ifdef CONFIG_YAFFS_WINCE
- yfsd_WinFileTimeNow(in->win_atime);
- in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
- in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
-
-#else
- in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
-
- in->yst_rdev = rdev;
- in->yst_uid = uid;
- in->yst_gid = gid;
-#endif
- in->nDataChunks = 0;
-
- yaffs_SetObjectName(in, name);
- in->dirty = 1;
-
- yaffs_AddObjectToDirectory(parent, in);
-
- in->myDev = parent->myDev;
-
- switch (type) {
- case YAFFS_OBJECT_TYPE_SYMLINK:
- in->variant.symLinkVariant.alias = str;
- break;
- case YAFFS_OBJECT_TYPE_HARDLINK:
- in->variant.hardLinkVariant.equivalentObject =
- equivalentObject;
- in->variant.hardLinkVariant.equivalentObjectId =
- equivalentObject->objectId;
- ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
- break;
- case YAFFS_OBJECT_TYPE_FILE:
- case YAFFS_OBJECT_TYPE_DIRECTORY:
- case YAFFS_OBJECT_TYPE_SPECIAL:
- case YAFFS_OBJECT_TYPE_UNKNOWN:
- /* do nothing */
- break;
- }
-
- if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
- /* Could not create the object header, fail the creation */
- yaffs_DeleteObject(in);
- in = NULL;
- }
-
- yaffs_UpdateParent(parent);
- }
-
- return in;
-}
-
-yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
- __u32 mode, __u32 uid, __u32 gid)
-{
- return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
- uid, gid, NULL, NULL, 0);
-}
-
-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++;
-
- for (b = dev->internalStartBlock; b <=dev->internalEndBlock; b++){
-
- bi = yaffs_GetBlockInfo(dev, b);
-
-
- if (bi->blockState == YAFFS_BLOCK_STATE_FULL){
-
- if(oldest < 1 ||
- bi->sequenceNumber < oldestSequence){
- oldest = b;
- oldestSequence = bi->sequenceNumber;
- }
- }
- }
-
- 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) {
- for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) {
-
- bi = yaffs_GetBlockInfo(dev, i);
- /* yaffs_VerifyBlock(dev,bi,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 */
- }
- }
- }
-
- 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 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);
-}
-
-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)) {
- /* 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->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;