2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_guts.c The main guts of YAFFS
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
19 #include "yaffsinterface.h"
20 #include "yaffs_guts.h"
29 // External functions for ECC on data
30 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
31 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
34 // countBits is a quick way of counting the number of bits in a byte.
35 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
37 static const char yaffs_countBits[256] =
39 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
40 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
41 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
42 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
43 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
44 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
45 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
46 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
47 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
48 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
49 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
50 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
52 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
53 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
54 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
60 //static yaffs_Device *yaffs_device;
61 //yaffs_Object *yaffs_rootDir;
62 //yaffs_Object *yaffs_lostNFound;
67 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
68 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
69 static void yaffs_GetTagsFromSpare(yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
70 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
72 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
73 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
74 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name);
75 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId);
76 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
77 static int yaffs_CheckStructures(void);
78 static yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
80 loff_t yaffs_GetFileSize(yaffs_Object *obj);
83 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
86 static int yaffs_CheckFileSanity(yaffs_Object *in)
88 #define yaffs_CheckFileSanity(in)
91 static int __inline__ yaffs_HashFunction(int n)
93 return (n % YAFFS_NOBJECT_BUCKETS);
97 yaffs_Object *yaffs_Root(yaffs_Device *dev)
102 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
104 return dev->lostNFoundDir;
108 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
110 return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
113 int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
117 yaffs_Spare localSpare;
121 // If we don't have a real spare, then we use a local one.
122 // Need this for the calculation of the ecc
126 retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
130 //Todo handle any errors
131 nand_calculate_ecc(data,calcEcc);
132 nand_correct_data (data,spare->ecc1, calcEcc);
133 nand_calculate_ecc(&data[256],calcEcc);
134 nand_correct_data (&data[256],spare->ecc2, calcEcc);
139 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
141 return dev->eraseBlockInNAND(dev,blockInNAND);
144 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
146 return dev->initialiseNAND(dev);
149 static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve)
155 unsigned char rbData[YAFFS_BYTES_PER_CHUNK];
159 chunk = yaffs_AllocateChunk(dev,useReserve);
163 writeOk = yaffs_WriteChunkToNAND(dev,chunk,data,spare);
167 // If verify fails, then delete this chunk and try again
168 // To verify we compare everything except the block and
169 // page status bytes.
170 yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare);
172 if(memcmp(data,rbData,YAFFS_BYTES_PER_CHUNK) != 0 ||
173 spare->tagByte0 != rbSpare.tagByte0 ||
174 spare->tagByte1 != rbSpare.tagByte1 ||
175 spare->tagByte2 != rbSpare.tagByte2 ||
176 spare->tagByte3 != rbSpare.tagByte3 ||
177 spare->tagByte4 != rbSpare.tagByte4 ||
178 spare->tagByte5 != rbSpare.tagByte5 ||
179 spare->tagByte6 != rbSpare.tagByte6 ||
180 spare->tagByte7 != rbSpare.tagByte7 ||
181 spare->ecc1[0] != rbSpare.ecc1[0] ||
182 spare->ecc1[1] != rbSpare.ecc1[1] ||
183 spare->ecc1[2] != rbSpare.ecc1[2] ||
184 spare->ecc2[0] != rbSpare.ecc2[0] ||
185 spare->ecc2[1] != rbSpare.ecc2[1] ||
186 spare->ecc2[2] != rbSpare.ecc2[2] )
189 yaffs_DeleteChunk(dev,chunk);
195 } while(chunk >= 0 && ! writeOk);
203 ///////////////////////// Object management //////////////////
204 // List of spare objects
205 // The list is hooked together using the first pointer
208 // static yaffs_Object *yaffs_freeObjects = NULL;
210 // static int yaffs_nFreeObjects;
212 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
214 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
217 static __u16 yaffs_CalcNameSum(const char *name)
222 __u8 *bname = (__u8 *)name;
234 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
236 nand_calculate_ecc (data , spare->ecc1);
237 nand_calculate_ecc (&data[256] , spare->ecc2);
240 void yaffs_CalcTagsECC(yaffs_Tags *tags)
242 // Todo don't do anything yet. Need to calculate ecc
243 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
250 for(i = 0; i < 8; i++)
252 for(j = 1; j &0x7f; j<<=1)
267 void yaffs_CheckECCOnTags(yaffs_Tags *tags)
269 unsigned ecc = tags->ecc;
271 yaffs_CalcTagsECC(tags);
278 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
282 b[ecc / 8] ^= (1 << (ecc & 7));
284 // Now recvalc the ecc
285 yaffs_CalcTagsECC(tags);
290 ///////////////////////// TNODES ///////////////////////
292 // List of spare tnodes
293 // The list is hooked together using the first pointer
296 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
298 // static int yaffs_nFreeTnodes;
300 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
304 // yaffs_CreateTnodes creates a bunch more tnodes and
305 // adds them to the tnode free list.
306 // Don't use this function directly
308 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
311 yaffs_Tnode *newTnodes;
312 yaffs_TnodeList *tnl;
314 if(nTnodes < 1) return YAFFS_OK;
318 newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
322 YALERT("Could not malloc tnodes");
326 // Hook them into the free list
327 for(i = 0; i < nTnodes - 1; i++)
329 newTnodes[i].internal[0] = &newTnodes[i+1];
332 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
333 dev->freeTnodes = newTnodes;
334 dev->nFreeTnodes+= nTnodes;
335 dev->nTnodesCreated += nTnodes;
337 // Now add this bunch of tnodes to a list for freeing up.
339 tnl = YMALLOC(sizeof(yaffs_TnodeList));
342 YALERT("Could not add tnodes to management list");
346 tnl->tnodes = newTnodes;
347 tnl->next = dev->allocatedTnodeList;
348 dev->allocatedTnodeList = tnl;
352 YINFO("Tnodes created");
359 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
360 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
362 yaffs_Tnode *tn = NULL;
364 // If there are none left make more
367 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
372 tn = dev->freeTnodes;
373 dev->freeTnodes = dev->freeTnodes->internal[0];
376 memset(tn,0,sizeof(yaffs_Tnode));
384 // FreeTnode frees up a tnode and puts it back on the free list
385 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
387 tn->internal[0] = dev->freeTnodes;
388 dev->freeTnodes = tn;
393 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
395 // Free the list of allocated tnodes
397 while(dev->allocatedTnodeList)
399 YFREE(dev->allocatedTnodeList->tnodes);
400 dev->allocatedTnodeList = dev->allocatedTnodeList->next;
403 dev->freeTnodes = NULL;
404 dev->nFreeTnodes = 0;
407 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
409 dev->allocatedTnodeList = NULL;
410 dev->freeTnodes = NULL;
411 dev->nFreeTnodes = 0;
412 dev->nTnodesCreated = 0;
416 void yaffs_TnodeTest(yaffs_Device *dev)
421 yaffs_Tnode *tn[1000];
423 YINFO("Testing TNodes");
425 for(j = 0; j < 50; j++)
427 for(i = 0; i < 1000; i++)
429 tn[i] = yaffs_GetTnode(dev);
432 YALERT("Getting tnode failed");
435 for(i = 0; i < 1000; i+=3)
437 yaffs_FreeTnode(dev,tn[i]);
444 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
446 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
447 // The look up tree is represented by the top tnode and the number of topLevel
448 // in the tree. 0 means only the level 0 tnode is in the tree.
451 // FindLevel0Tnode finds the level 0 tnode, if one exists.
452 // Used when reading.....
453 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
456 yaffs_Tnode *tn = fStruct->top;
458 int requiredTallness;
459 int level = fStruct->topLevel;
461 // Check sane level and chunk Id
462 if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
465 sprintf(str,"Bad level %d",level);
469 if(chunkId > YAFFS_MAX_CHUNK_ID)
472 sprintf(str,"Bad chunkId %d",chunkId);
477 // First check we're tall enough (ie enough topLevel)
479 i = chunkId >> (dev->chunkGroupBits + YAFFS_TNODES_LEVEL0_BITS);
480 requiredTallness = 0;
483 i >>= YAFFS_TNODES_INTERNAL_BITS;
488 if(requiredTallness > fStruct->topLevel)
490 // Not tall enough, so we can't find it, return NULL.
495 // Traverse down to level 0
496 while (level > 0 && tn)
498 tn = tn->internal[(chunkId >>(dev->chunkGroupBits + YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) &
499 YAFFS_TNODES_INTERNAL_MASK];
507 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
508 // This happens in two steps:
509 // 1. If the tree isn't tall enough, then make it taller.
510 // 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
512 // Used when modifying the tree.
514 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
519 int requiredTallness;
525 T(("AddOrFind topLevel=%d, chunk=%d",fStruct->topLevel,chunkId));
527 // Check sane level and page Id
528 if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
531 sprintf(str,"Bad level %d",fStruct->topLevel);
536 if(chunkId > YAFFS_MAX_CHUNK_ID)
539 sprintf(str,"Bad chunkId %d",chunkId);
544 // First check we're tall enough (ie enough topLevel)
546 i = chunkId >> (dev->chunkGroupBits + YAFFS_TNODES_LEVEL0_BITS);
547 requiredTallness = 0;
550 i >>= YAFFS_TNODES_INTERNAL_BITS;
554 T((" required=%d",requiredTallness));
557 if(requiredTallness > fStruct->topLevel)
559 // Not tall enough,gotta make the tree taller
560 for(i = fStruct->topLevel; i < requiredTallness; i++)
564 tn = yaffs_GetTnode(dev);
568 tn->internal[0] = fStruct->top;
573 YALERT("No more tnodes");
577 fStruct->topLevel = requiredTallness;
581 // Traverse down to level 0, adding anything we need
583 l = fStruct->topLevel;
587 i = (chunkId >> (dev->chunkGroupBits +YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) &
588 YAFFS_TNODES_INTERNAL_MASK;
596 tn->internal[i] = yaffs_GetTnode(dev);
599 tn = tn->internal[i];
610 // Pruning removes any part of the file structure tree that is beyond the
611 // bounds of the file.
612 // A file should only get pruned when its size is reduced.
614 // Before pruning, the chunks must be pulled from the tree and the
615 // level 0 tnode entries must be zeroed out.
616 // Could also use this for file deletion, but that's probably better handled
617 // by a special case.
619 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
621 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
630 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
632 if(tn->internal[i] && level > 0)
634 tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
643 if(hasData == 0 && del0)
645 // Free and return NULL
647 yaffs_FreeTnode(dev,tn);
657 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
664 if(fStruct->topLevel > 0)
666 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
668 // Now we have a tree with all the non-zero branches NULL but the height
669 // is the same as it was.
670 // Let's see if we can trim internal tnodes to shorten the tree.
671 // We can do this if only the 0th element in the tnode is in use
672 // (ie all the non-zero are NULL)
674 while(fStruct->topLevel && !done)
679 for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
689 fStruct->top = tn->internal[0];
691 yaffs_FreeTnode(dev,tn);
706 /////////////////////// End of File Structure functions. /////////////////
708 // yaffs_CreateFreeObjects creates a bunch more objects and
709 // adds them to the object free list.
710 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
713 yaffs_Object *newObjects;
714 yaffs_ObjectList *list;
716 if(nObjects < 1) return YAFFS_OK;
720 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
724 YALERT("Could not allocate more objects");
728 // Hook them into the free list
729 for(i = 0; i < nObjects - 1; i++)
731 (yaffs_Object *)newObjects[i].siblings.next = &newObjects[i+1];
734 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
735 dev->freeObjects = newObjects;
736 dev->nFreeObjects+= nObjects;
737 dev->nObjectsCreated+= nObjects;
739 // Now add this bunch of Objects to a list for freeing up.
741 list = YMALLOC(sizeof(yaffs_ObjectList));
744 YALERT("Could not add Objects to management list");
748 list->objects = newObjects;
749 list->next = dev->allocatedObjectList;
750 dev->allocatedObjectList = list;
754 YINFO("Objects created");
761 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
762 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
764 yaffs_Object *tn = NULL;
766 // If there are none left make more
767 if(!dev->freeObjects)
769 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
774 tn = dev->freeObjects;
775 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
778 // Now sweeten it up...
780 memset(tn,0,sizeof(yaffs_Object));
782 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
783 INIT_LIST_HEAD(&(tn->hardLinks));
784 INIT_LIST_HEAD(&(tn->hashLink));
785 INIT_LIST_HEAD(&tn->siblings);
787 // Add it to the lost and found directory.
788 // NB Can't put root or lostNFound in lostNFound so
789 // check if lostNFound exists first
790 if(dev->lostNFoundDir)
792 yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);
800 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
803 yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);
806 obj->fake = 1; // it is fake so it has no NAND presence...
807 obj->renameAllowed= 0; // ... and we're not allowed to rename it...
808 obj->unlinkAllowed= 0; // ... or unlink it
811 obj->chunkId = 0; // Not a valid chunk.
819 static void yaffs_UnhashObject(yaffs_Object *tn)
822 yaffs_Device *dev = tn->myDev;
825 // If it is still linked into the bucket list, free from the list
826 if(!list_empty(&tn->hashLink))
828 list_del_init(&tn->hashLink);
829 bucket = yaffs_HashFunction(tn->objectId);
830 dev->objectBucket[bucket].count--;
836 // FreeObject frees up a Object and puts it back on the free list
837 static void yaffs_FreeObject(yaffs_Object *tn)
840 yaffs_Device *dev = tn->myDev;
842 yaffs_UnhashObject(tn);
844 // Link into the free list.
845 (yaffs_Object *)(tn->siblings.next) = dev->freeObjects;
846 dev->freeObjects = tn;
853 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
855 // Free the list of allocated Objects
857 while( dev->allocatedObjectList)
859 YFREE(dev->allocatedObjectList->objects);
860 dev->allocatedObjectList = dev->allocatedObjectList->next;
863 dev->freeObjects = NULL;
864 dev->nFreeObjects = 0;
867 static void yaffs_InitialiseObjects(yaffs_Device *dev)
871 dev->allocatedObjectList = NULL;
872 dev->freeObjects = NULL;
873 dev->nFreeObjects = 0;
875 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
877 INIT_LIST_HEAD(&dev->objectBucket[i].list);
878 dev->objectBucket[i].count = 0;
888 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
896 // First let's see if we can find one that's empty.
898 for(i = 0; i < 10 && lowest > 0; i++)
901 x %= YAFFS_NOBJECT_BUCKETS;
902 if(dev->objectBucket[x].count < lowest)
904 lowest = dev->objectBucket[x].count;
910 // If we didn't find an empty list, then try
911 // looking a bit further for a short one
913 for(i = 0; i < 10 && lowest > 3; i++)
916 x %= YAFFS_NOBJECT_BUCKETS;
917 if(dev->objectBucket[x].count < lowest)
919 lowest = dev->objectBucket[x].count;
928 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
930 int bucket = yaffs_FindNiceObjectBucket(dev);
932 // Now find an object value that has not already been taken
933 // by scanning the list.
940 //yaffs_CheckObjectHashSanity();
945 n += YAFFS_NOBJECT_BUCKETS;
946 if(1 ||dev->objectBucket[bucket].count > 0)
948 list_for_each(i,&dev->objectBucket[bucket].list)
950 // If there is already one in the list
951 if(list_entry(i, yaffs_Object,hashLink)->objectId == n)
959 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
964 void yaffs_HashObject(yaffs_Object *in)
966 int bucket = yaffs_HashFunction(in->objectId);
967 yaffs_Device *dev = in->myDev;
969 if(!list_empty(&in->hashLink))
975 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
976 dev->objectBucket[bucket].count++;
980 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,int number)
982 int bucket = yaffs_HashFunction(number);
986 list_for_each(i,&dev->objectBucket[bucket].list)
988 // Lookm if it is in the list
989 in = list_entry(i, yaffs_Object,hashLink);
990 if(in->objectId == number)
1001 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1004 yaffs_Object *theObject;
1008 number = yaffs_CreateNewObjectNumber(dev);
1011 theObject = yaffs_AllocateEmptyObject(dev);
1015 theObject->fake = 0;
1016 theObject->renameAllowed = 1;
1017 theObject->unlinkAllowed = 1;
1018 theObject->objectId = number;
1019 theObject->myDev = dev;
1020 yaffs_HashObject(theObject);
1021 theObject->variantType = type;
1022 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = CURRENT_TIME;
1026 case YAFFS_OBJECT_TYPE_FILE:
1027 theObject->variant.fileVariant.fileSize = 0;
1028 theObject->variant.fileVariant.topLevel = 0;
1029 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
1031 case YAFFS_OBJECT_TYPE_DIRECTORY:
1032 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1034 case YAFFS_OBJECT_TYPE_SYMLINK:
1035 // No action required
1037 case YAFFS_OBJECT_TYPE_HARDLINK:
1038 // No action required
1040 case YAFFS_OBJECT_TYPE_UNKNOWN:
1041 // todo this should not happen
1048 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1050 yaffs_Object *theObject = NULL;
1054 theObject = yaffs_FindObjectByNumber(dev,number);
1059 theObject = yaffs_CreateNewObject(dev,number,type);
1066 char *yaffs_CloneString(const char *str)
1068 char *newStr = NULL;
1072 newStr = YMALLOC(strlen(str) + 1);
1081 // Mknod (create) a new object.
1082 // equivalentObject only has meaning for a hard link;
1083 // aliasString only has meaning for a sumlink.
1084 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1085 yaffs_Object *parent,
1090 yaffs_Object *equivalentObject,
1091 const char *aliasString)
1095 yaffs_Device *dev = parent->myDev;
1097 in = yaffs_CreateNewObject(dev,-1,type);
1103 in->variantType = type;
1108 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
1110 in->sum = yaffs_CalcNameSum(name);
1113 yaffs_AddObjectToDirectory(parent,in);
1115 in->myDev = parent->myDev;
1120 case YAFFS_OBJECT_TYPE_SYMLINK:
1121 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1123 case YAFFS_OBJECT_TYPE_HARDLINK:
1124 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1125 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1126 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1128 case YAFFS_OBJECT_TYPE_FILE: // do nothing
1129 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1130 case YAFFS_OBJECT_TYPE_UNKNOWN:
1133 yaffs_UpdateObjectHeader(in,name);
1140 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1142 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL);
1145 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1147 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL);
1150 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,const char *alias)
1152 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias);
1155 // NB yaffs_Link returns the object id of the equivalent object.
1156 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject)
1158 // Get the real object in case we were fed a hard link as an equivalent object
1159 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1161 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL))
1163 return equivalentObject;
1173 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName)
1175 //yaffs_Device *dev = obj->myDev;
1179 newDir = obj->parent; // use the old directory
1182 // Only proceed if the new name does not exist and
1183 // if we're putting it into a directory.
1184 if(!yaffs_FindObjectByName(newDir,newName) &&
1185 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1187 obj->sum = yaffs_CalcNameSum(newName);
1189 yaffs_AddObjectToDirectory(newDir,obj);
1191 return yaffs_UpdateObjectHeader(obj,newName);
1197 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName)
1201 obj = yaffs_FindObjectByName(oldDir,oldName);
1202 if(obj && obj->renameAllowed)
1204 return yaffs_ChangeObjectName(obj,newDir,newName);
1211 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1213 // Scan the buckets and check that the lists
1214 // have as many members as the count says there are
1217 struct list_head *j;
1220 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1224 list_for_each(j,&dev->objectBucket[bucket].list)
1229 if(countEm != dev->objectBucket[bucket].count)
1231 YALERT("Inode hash inconsistency");
1239 void yaffs_ObjectTest(yaffs_Device *dev)
1241 yaffs_Object *in[1000];
1243 yaffs_Object *inold[1000];
1247 memset(in,0,1000*sizeof(yaffs_Object *));
1248 memset(inold,0,1000*sizeof(yaffs_Object *));
1250 yaffs_CheckObjectHashSanity(dev);
1252 for(j = 0; j < 10; j++)
1256 for(i = 0; i < 1000; i++)
1258 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1261 YINFO("No more inodes");
1265 inNo[i] = in[i]->objectId;
1269 for(i = 0; i < 1000; i++)
1271 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1273 T(("Differnce in look up test\n"));
1277 // T(("Look up ok\n"));
1281 yaffs_CheckObjectHashSanity(dev);
1283 for(i = 0; i < 1000; i+=3)
1285 yaffs_FreeObject(in[i]);
1290 yaffs_CheckObjectHashSanity(dev);
1297 /////////////////////////// Block Management and Page Allocation ///////////////////
1300 static void yaffs_InitialiseBlocks(yaffs_Device *dev)
1302 dev->blockInfo = YMALLOC(dev->nBlocks * sizeof(yaffs_BlockInfo));
1303 memset(dev->blockInfo,0,dev->nBlocks * sizeof(yaffs_BlockInfo));
1304 dev->allocationBlock = -1; // force it to get a new one
1307 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
1309 YFREE(dev->blockInfo);
1312 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
1313 // for garbage collection.
1315 static int yaffs_FindDirtiestBlock(yaffs_Device *dev)
1318 int b = dev->currentDirtyChecker;
1322 int pagesInUse = 100; // silly big number
1324 for(i = dev->startBlock; i <= dev->endBlock && pagesInUse > 2 ; i++)
1327 if (b > dev->endBlock)
1329 b = dev->startBlock;
1332 if(dev->blockInfo[b].blockState == YAFFS_BLOCK_STATE_FULL &&
1333 (dev->blockInfo)[b].pagesInUse < pagesInUse)
1336 pagesInUse = (dev->blockInfo)[b].pagesInUse;
1340 dev->currentDirtyChecker = b;
1346 static int yaffs_FindBlockForAllocation(yaffs_Device *dev,int useReserve)
1350 if(useReserve && dev->nErasedBlocks < 1)
1352 // Hoosterman we've got a problem.
1353 // Can't get space to gc
1356 else if(!useReserve && dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS)
1358 // We are not in GC, so we hold some in reserve so we can get
1362 // Find an empty block.
1364 for(i = dev->startBlock; i <= dev->endBlock; i++)
1367 if(dev->blockInfo[i].blockState == YAFFS_BLOCK_STATE_EMPTY)
1369 dev->blockInfo[i].blockState = YAFFS_BLOCK_STATE_ALLOCATING;
1370 dev->nErasedBlocks--;
1371 if(dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS)
1373 dev->garbageCollectionRequired = 1;
1384 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
1386 yaffs_BlockInfo *bi = &dev->blockInfo[blockNo];
1388 // Mark as dirty, erase it and mark as clean.
1389 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
1390 yaffs_EraseBlockInNAND(dev,blockNo);
1391 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
1392 dev->nErasedBlocks++;
1396 T(("Erased block %d\n",blockNo));
1400 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
1404 if(dev->allocationBlock < 0)
1406 // Get next block to allocate off
1407 dev->allocationBlock = yaffs_FindBlockForAllocation(dev,useReserve);
1408 dev->allocationPage = 0;
1411 // Next page please....
1412 if(dev->allocationBlock >= 0)
1414 retVal = (dev->allocationBlock * YAFFS_CHUNKS_PER_BLOCK) +
1415 dev->allocationPage;
1416 dev->blockInfo[dev->allocationBlock].pagesInUse++;
1417 dev->blockInfo[dev->allocationBlock].pageBits |=
1418 (1 << (dev->allocationPage));
1420 dev->allocationPage++;
1424 // If the block is full set the state to full
1425 if(dev->allocationPage >= YAFFS_CHUNKS_PER_BLOCK)
1427 dev->blockInfo[dev->allocationBlock].blockState = YAFFS_BLOCK_STATE_FULL;
1428 dev->allocationBlock = -1;
1431 #ifdef YAFFS_PARANOID
1432 if(yaffs_CheckChunkErased(retVal) == YAFFS_FAIL)
1434 T(("..................Trying to allocate non-erased page %d\n",retVal));
1440 T(("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!\n"));
1446 int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
1455 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
1457 yaffs_BlockInfo *bi = &dev->blockInfo[block];
1459 yaffs_Object *object;
1461 T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
1463 for(mask = 1,oldChunk = block * YAFFS_CHUNKS_PER_BLOCK;
1464 mask && bi->pageBits;
1465 mask <<= 1, oldChunk++ )
1467 if(bi->pageBits & mask)
1470 // This page is in use and needs to be copied off
1472 T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
1474 yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare);
1476 yaffs_GetTagsFromSpare(&spare,&tags);
1477 tags.serialNumber++;
1478 yaffs_LoadTagsIntoSpare(&spare,&tags);
1481 newChunk = yaffs_AllocatePage(dev,1);
1487 yaffs_WriteChunkToNAND(dev,newChunk, buffer, &spare);
1490 newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1);
1497 object = yaffs_FindObjectByNumber(dev,tags.objectId);
1499 // Ok, now fix up the Tnodes etc.
1501 if(tags.chunkId == 0)
1504 object->chunkId = newChunk;
1508 // It's a data chunk
1509 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
1513 yaffs_DeleteChunk(dev,oldChunk);
1521 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
1525 if(dev->garbageCollectionRequired)
1527 dev->garbageCollectionRequired = 0;
1528 block = yaffs_FindDirtiestBlock(dev);
1531 return yaffs_GarbageCollectBlock(dev,block);
1543 //////////////////////////// TAGS ///////////////////////////////////////
1545 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
1547 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
1549 yaffs_CalcTagsECC(tagsPtr);
1551 sparePtr->tagByte0 = tu->asBytes[0];
1552 sparePtr->tagByte1 = tu->asBytes[1];
1553 sparePtr->tagByte2 = tu->asBytes[2];
1554 sparePtr->tagByte3 = tu->asBytes[3];
1555 sparePtr->tagByte4 = tu->asBytes[4];
1556 sparePtr->tagByte5 = tu->asBytes[5];
1557 sparePtr->tagByte6 = tu->asBytes[6];
1558 sparePtr->tagByte7 = tu->asBytes[7];
1561 static void yaffs_GetTagsFromSpare(yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
1563 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
1565 tu->asBytes[0]= sparePtr->tagByte0;
1566 tu->asBytes[1]= sparePtr->tagByte1;
1567 tu->asBytes[2]= sparePtr->tagByte2;
1568 tu->asBytes[3]= sparePtr->tagByte3;
1569 tu->asBytes[4]= sparePtr->tagByte4;
1570 tu->asBytes[5]= sparePtr->tagByte5;
1571 tu->asBytes[6]= sparePtr->tagByte6;
1572 tu->asBytes[7]= sparePtr->tagByte7;
1574 yaffs_CheckECCOnTags(tagsPtr);
1577 static void yaffs_SpareInitialise(yaffs_Spare *spare)
1579 memset(spare,0xFF,sizeof(yaffs_Spare));
1582 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags)
1587 if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare) == YAFFS_OK)
1589 yaffs_GetTagsFromSpare(&spare,tags);
1601 static int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_Tags *tags)
1603 // NB There must be tags, data is optional
1604 // If there is data, then an ECC is calculated on it.
1613 yaffs_SpareInitialise(&spare);
1618 yaffs_CalcECC(buffer,&spare);
1621 yaffs_LoadTagsIntoSpare(&spare,tags);
1623 return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare);
1627 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_Tags *tags, int useReserve)
1629 // NB There must be tags, data is optional
1630 // If there is data, then an ECC is calculated on it.
1639 yaffs_SpareInitialise(&spare);
1644 yaffs_CalcECC(buffer,&spare);
1647 yaffs_LoadTagsIntoSpare(&spare,tags);
1649 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
1656 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
1658 //Get the Tnode, then get the level 0 offset chunk offset
1661 yaffs_Tags localTags;
1664 yaffs_Device *dev = in->myDev;
1669 // Passed a NULL, so use our own tags space
1673 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
1677 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
1679 // Now we need to do the shifting etc and search for it
1680 for(i = 0,found = 0; i < dev->chunkGroupSize && !found; i++)
1682 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags);
1683 if(tags->chunkId == chunkInInode &&
1684 tags->objectId == in->objectId)
1695 return found ? theChunk : -1;
1698 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
1700 //Get the Tnode, then get the level 0 offset chunk offset
1703 yaffs_Tags localTags;
1706 yaffs_Device *dev = in->myDev;
1710 // Passed a NULL, so use our own tags space
1714 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
1719 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
1721 // Now we need to do the shifting etc and search for it
1722 for(i = 0,found = 0; i < dev->chunkGroupSize && !found; i++)
1724 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags);
1725 if(tags->chunkId == chunkInInode &&
1726 tags->objectId == in->objectId)
1737 // Delete the entry in the filestructure
1740 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
1745 T(("No level 0 found for %d\n", chunkInInode));
1750 T(("Could not find %d to delete\n",chunkInInode));
1752 return found ? theChunk : -1;
1758 static int yaffs_CheckFileSanity(yaffs_Object *in)
1766 yaffs_Tags localTags;
1767 yaffs_Tags *tags = &localTags;
1771 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
1773 T(("Object not a file\n"));
1777 objId = in->objectId;
1778 fSize = in->variant.fileVariant.fileSize;
1779 nChunks = (fSize + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
1781 for(chunk = 1; chunk <= nChunks; chunk++)
1783 tn = yaffs_FindLevel0Tnode(&in->variant.fileVariant, chunk);
1788 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
1791 yaffs_ReadChunkTagsFromNAND(theChunk,tags);
1792 if(tags->chunkId == chunk &&
1793 tags->objectId == in->objectId)
1800 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
1801 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
1810 T(("No level 0 found for %d\n", chunk));
1814 return failed ? YAFFS_FAIL : YAFFS_OK;
1819 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
1822 yaffs_Device *dev = in->myDev;
1824 yaffs_Tags existingTags;
1826 unsigned existingSerial, newSerial;
1829 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
1833 // If we're scanning then we need to test for duplicates
1834 // NB This does not need to be efficient since it should only ever
1835 // happen when the power fails during a write, then only one
1836 // chunk should ever be affected.
1838 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
1840 if(existingChunk != 0)
1842 // We have a duplicate now we need to decide which one to use
1843 // To do this we get both sets of tags and compare serial numbers.
1844 yaffs_ReadChunkTagsFromNAND(dev,chunkInInode, &newTags);
1845 yaffs_ReadChunkTagsFromNAND(dev,existingChunk, &existingTags);
1846 newSerial = newTags.serialNumber;
1847 existingSerial = existingTags.serialNumber;
1848 if(((existingSerial+1) & 3) == newSerial)
1851 // Delete the old one and drop through to update the tnode
1852 yaffs_DeleteChunk(dev,existingChunk);
1857 // Delete the new one and return early so that the tnode isn't changed
1858 yaffs_DeleteChunk(dev,chunkInInode);
1864 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = chunkInNAND;
1871 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
1873 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
1875 if(chunkInNAND >= 0)
1877 return yaffs_ReadChunkFromNAND(in->myDev,chunkInNAND,buffer,NULL);
1887 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId)
1889 int block = chunkId / YAFFS_CHUNKS_PER_BLOCK;
1890 int page = chunkId % YAFFS_CHUNKS_PER_BLOCK;
1893 // Mark the deleted NAND page as deleted
1899 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
1902 // Pull out of the management area.
1903 if( dev->blockInfo[block].blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
1904 dev->blockInfo[block].blockState == YAFFS_BLOCK_STATE_FULL)
1908 dev->blockInfo[block].pageBits &= ~(1 << page);
1909 dev->blockInfo[block].pagesInUse--;
1911 if( dev->blockInfo[block].pagesInUse == 0 &&
1912 dev->blockInfo[block].blockState == YAFFS_BLOCK_STATE_FULL)
1914 yaffs_BlockBecameDirty(dev,block);
1920 T(("Bad news deleting chunk %d\n",chunkId));
1928 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
1930 // Find old chunk Need to do this to get serial number
1931 // Write new one and patch into tree.
1932 // Invalidate old tags.
1935 yaffs_Tags prevTags;
1940 yaffs_Device *dev = in->myDev;
1942 yaffs_CheckGarbageCollection(dev);
1944 // Get the previous chunk at this location in the file if it exists
1945 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
1948 newTags.chunkId = chunkInInode;
1949 newTags.objectId = in->objectId;
1950 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
1951 newTags.byteCount = nBytes;
1952 newTags.unusedStuff = 0xFFFFFFFF;
1954 yaffs_CalcTagsECC(&newTags);
1958 // Create new chunk in NAND
1959 newChunkId = yaffs_AllocatePage(dev,useReserve);
1966 yaffs_WriteChunkWithTagsToNAND(dev,newChunkId,buffer,&newTags);
1968 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId);
1971 if(prevChunkId >= 0)
1973 yaffs_DeleteChunk(dev,prevChunkId);
1977 yaffs_CheckFileSanity(in);
1986 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
1989 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
1992 if(prevChunkId >= 0)
1994 yaffs_DeleteChunk(dev,prevChunkId);
1998 yaffs_CheckFileSanity(in);
2009 // UpdateObjectHeader updates the header on NAND for an object.
2010 // If name is not NULL, then that new name is used.
2012 int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name)
2015 yaffs_Device *dev = in->myDev;
2021 __u8 bufferNew[YAFFS_BYTES_PER_CHUNK];
2022 __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
2024 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bufferNew;
2025 yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
2030 yaffs_CheckGarbageCollection(dev);
2032 memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK);
2034 prevChunkId = in->chunkId;
2036 if(prevChunkId >= 0)
2038 yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL);
2042 oh->type = in->variantType;
2044 oh->st_mode = in->st_mode;
2045 oh->st_uid = in->st_uid;
2046 oh->st_gid = in->st_gid;
2047 oh->st_atime = in->st_atime;
2048 oh->st_mtime = in->st_mtime;
2049 oh->st_ctime = in->st_ctime;
2051 oh->parentObjectId = in->parent->objectId;
2055 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
2056 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
2060 memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1);
2063 switch(in->variantType)
2065 case YAFFS_OBJECT_TYPE_UNKNOWN:
2066 // Should not happen
2068 case YAFFS_OBJECT_TYPE_FILE:
2069 oh->fileSize = in->variant.fileVariant.fileSize;
2071 case YAFFS_OBJECT_TYPE_HARDLINK:
2072 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
2074 case YAFFS_OBJECT_TYPE_DIRECTORY:
2077 case YAFFS_OBJECT_TYPE_SYMLINK:
2078 strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
2079 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
2085 newTags.chunkId = 0;
2086 newTags.objectId = in->objectId;
2087 newTags.serialNumber = in->serial;
2088 newTags.byteCount = 0xFFFFFFFF;
2089 newTags.unusedStuff = 0xFFFFFFFF;
2091 yaffs_CalcTagsECC(&newTags);
2096 // Create new chunk in NAND
2097 newChunkId = yaffs_AllocatePage(dev,1);
2102 yaffs_WriteChunkWithTagsToNAND(dev,newChunkId,bufferNew,&newTags);
2104 in->chunkId = newChunkId;
2106 if(prevChunkId >= 0)
2108 yaffs_DeleteChunk(dev,prevChunkId);
2117 // Create new chunk in NAND
2118 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags,1);
2123 in->chunkId = newChunkId;
2125 if(prevChunkId >= 0)
2127 yaffs_DeleteChunk(dev,prevChunkId);
2142 ///////////////////////// File read/write ///////////////////////////////
2143 // Read and write have very similar structures.
2144 // In general the read/write has three parts to it
2145 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
2146 // * Some complete chunks
2147 // * An incomplete chunk to end off with
2149 // Curve-balls: the first chunk might also be the last chunk.
2151 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
2154 // yaffs_Device *dev = in->myDev;
2156 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
2166 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; // The first chunk is 1
2167 start = offset % YAFFS_BYTES_PER_CHUNK;
2169 // OK now check for the curveball where the start and end are in
2171 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
2177 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
2180 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
2182 // An incomplete start or end chunk (or maybe both start and end chunk)
2183 // Read into the local buffer then copy...
2185 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
2186 memcpy(buffer,&localBuffer[start],nToCopy);
2190 // A full chunk. Read directly into the supplied buffer.
2191 yaffs_ReadChunkDataFromObject(in,chunk,buffer);
2205 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
2207 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
2215 int endOfWrite = offset+nBytes;
2216 int chunkWritten = 0;
2218 while(n > 0 && chunkWritten >= 0)
2220 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1;
2221 start = offset % YAFFS_BYTES_PER_CHUNK;
2224 // OK now check for the curveball where the start and end are in
2226 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
2229 nToWriteBack = (start + n);
2233 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
2234 nToWriteBack = YAFFS_BYTES_PER_CHUNK;
2237 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
2239 // An incomplete start or end chunk (or maybe both start and end chunk)
2240 // Read into the local buffer then copy, then copy over and write back.
2242 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
2244 memcpy(&localBuffer[start],buffer,nToCopy);
2246 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
2248 T(("Write with readback to chunk %d %d\n",chunk,chunkWritten));
2253 // A full chunk. Write directly from the supplied buffer.
2254 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0);
2255 T(("Write to chunk %d %d\n",chunk,chunkWritten));
2258 if(chunkWritten >= 0)
2268 // Update file object
2270 if(endOfWrite > in->variant.fileVariant.fileSize)
2272 in->variant.fileVariant.fileSize = endOfWrite;
2276 in->st_mtime = CURRENT_TIME;
2282 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
2286 int oldFileSize = in->variant.fileVariant.fileSize;
2287 int sizeOfLastChunk = newSize % YAFFS_BYTES_PER_CHUNK;
2289 yaffs_Device *dev = in->myDev;
2291 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
2293 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2295 return yaffs_GetFileSize(in);
2298 if(newSize < oldFileSize)
2301 int lastDel = 1 + oldFileSize/YAFFS_BYTES_PER_CHUNK;
2303 int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/
2304 YAFFS_BYTES_PER_CHUNK;
2306 for(i = startDel; i <= lastDel; i++)
2308 // NB this could be optimised somewhat,
2309 // eg. could retrieve the tags and write them without
2310 // using yaffs_DeleteChunk
2311 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
2312 if(chunkId < 0 || chunkId >= (dev->endBlock * 32))
2314 T(("Found daft chunkId %d for %d\n",chunkId,i));
2318 yaffs_DeleteChunk(dev,chunkId);
2323 if(sizeOfLastChunk != 0)
2325 int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK;
2327 // Got to read and rewrite the last chunk with its new size.
2328 yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
2330 yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfLastChunk,1);
2334 in->variant.fileVariant.fileSize = newSize;
2336 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
2348 loff_t yaffs_GetFileSize(yaffs_Object *obj)
2350 obj = yaffs_GetEquivalentObject(obj);
2352 switch(obj->variantType)
2354 case YAFFS_OBJECT_TYPE_FILE:
2355 return obj->variant.fileVariant.fileSize;
2356 case YAFFS_OBJECT_TYPE_SYMLINK:
2357 return strlen(obj->variant.symLinkVariant.alias);
2365 // yaffs_FlushFile() updates the file's
2368 int yaffs_FlushFile(yaffs_Object *in)
2373 retVal = yaffs_UpdateObjectHeader(in,NULL);
2385 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
2387 yaffs_RemoveObjectFromDirectory(in);
2388 yaffs_DeleteChunk(in->myDev,in->chunkId);
2389 yaffs_FreeObject(in);
2394 // yaffs_DeleteFile deletes the whole file data
2395 // and the inode associated with the file.
2396 // It does not delete the links associated with the file.
2397 static int yaffs_DeleteFile(yaffs_Object *in)
2399 // Delete the file data & tnodes
2400 yaffs_ResizeFile(in,0);
2401 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
2403 return yaffs_DoGenericObjectDeletion(in);
2406 static int yaffs_DeleteDirectory(yaffs_Object *in)
2408 //First check that the directory is empty.
2409 if(list_empty(&in->variant.directoryVariant.children))
2411 return yaffs_DoGenericObjectDeletion(in);
2418 static int yaffs_DeleteSymLink(yaffs_Object *in)
2420 YFREE(in->variant.symLinkVariant.alias);
2422 return yaffs_DoGenericObjectDeletion(in);
2425 static int yaffs_DeleteHardLink(yaffs_Object *in)
2427 // remove this hardlink from the list assocaited with the equivalent
2429 list_del(&in->hardLinks);
2430 return yaffs_DoGenericObjectDeletion(in);
2434 static int yaffs_UnlinkWorker(yaffs_Object *obj)
2438 if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
2440 return yaffs_DeleteHardLink(obj);
2442 else if(!list_empty(&obj->hardLinks))
2445 // Curve ball: We're unlinking an object that has a hardlink.
2446 // Therefore we can't really delete the object.
2447 // Instead, we do the following:
2448 // - Select a hardlink.
2449 // - Re-type a hardlink as the equivalent object and populate the fields, including the
2450 // objectId. Updating the object id is important so that all the hardlinks do not need
2452 // - Update the equivalet object pointers.
2453 // - Delete all object.
2456 struct list_head *i;
2459 yaffs_RemoveObjectFromDirectory(obj);
2463 hl = list_entry(obj->hardLinks.next, yaffs_Object,hardLinks);
2466 hl->st_mode = obj->st_mode;
2467 hl->st_uid = obj->st_uid;
2468 hl->st_gid = obj->st_gid;
2469 hl->st_atime = obj->st_atime;
2470 hl->st_mtime = obj->st_mtime;
2471 hl->st_ctime = obj->st_ctime;
2473 hl->variantType = obj->variantType;
2475 switch(hl->variantType)
2477 case YAFFS_OBJECT_TYPE_FILE:
2478 case YAFFS_OBJECT_TYPE_SYMLINK:
2479 // These types are OK to just copy across.
2480 hl->variant = obj->variant;
2482 case YAFFS_OBJECT_TYPE_DIRECTORY:
2484 list_add(&hl->variant.directoryVariant.children,
2485 &obj->variant.directoryVariant.children);
2486 list_del(&obj->variant.directoryVariant.children);
2488 // Now change all the directory children to point to the new parent.
2489 list_for_each(i,&hl->variant.directoryVariant.children)
2491 list_entry(i,yaffs_Object,siblings)->parent = hl;
2495 case YAFFS_OBJECT_TYPE_HARDLINK:
2496 case YAFFS_OBJECT_TYPE_UNKNOWN:
2497 // Should not be either of these types.
2500 // Now fix up the hardlink chain
2501 list_del(&obj->hardLinks);
2503 list_for_each(i,&hl->hardLinks)
2505 list_entry(i,yaffs_Object,hardLinks)->variant.hardLinkVariant.equivalentObject = hl;
2506 list_entry(i,yaffs_Object,hardLinks)->variant.hardLinkVariant.equivalentObjectId = hl->objectId;
2509 // Now fix up the hash links.
2510 yaffs_UnhashObject(hl);
2511 hl->objectId = obj->objectId;
2512 yaffs_HashObject(hl);
2514 // Update the hardlink which has become an object
2515 yaffs_UpdateObjectHeader(hl,NULL);
2517 // Finally throw away the deleted object
2518 yaffs_DeleteChunk(obj->myDev,obj->chunkId);
2519 yaffs_FreeObject(obj);
2523 // Curve ball: We're unlinking an object that has a hardlink.
2525 // This problem arises because we are not strictly following
2526 // The Linux link/inode model.
2528 // We can't really delete the object.
2529 // Instead, we do the following:
2530 // - Select a hardlink.
2531 // - Unhook it from the hard links
2532 // - Unhook it from its parent directory (so that the rename can work)
2533 // - Rename the object to the hardlink's name.
2534 // - Delete the hardlink
2539 char name[YAFFS_MAX_NAME_LENGTH+1];
2541 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
2542 list_del_init(&hl->hardLinks);
2543 list_del_init(&hl->siblings);
2545 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
2547 retVal = yaffs_ChangeObjectName(obj, hl->parent, name);
2548 if(retVal == YAFFS_OK)
2550 retVal = yaffs_DoGenericObjectDeletion(hl);
2560 switch(obj->variantType)
2562 case YAFFS_OBJECT_TYPE_FILE:
2563 return yaffs_DeleteFile(obj);
2565 case YAFFS_OBJECT_TYPE_DIRECTORY:
2566 return yaffs_DeleteDirectory(obj);
2568 case YAFFS_OBJECT_TYPE_SYMLINK:
2569 return yaffs_DeleteSymLink(obj);
2571 case YAFFS_OBJECT_TYPE_HARDLINK:
2572 case YAFFS_OBJECT_TYPE_UNKNOWN:
2579 int yaffs_Unlink(yaffs_Object *dir, const char *name)
2583 obj = yaffs_FindObjectByName(dir,name);
2585 if(obj && obj->unlinkAllowed)
2587 return yaffs_UnlinkWorker(obj);
2594 //////////////// Initialisation Scanning /////////////////
2597 static int yaffs_Scan(yaffs_Device *dev)
2606 yaffs_BlockState state;
2607 yaffs_Object *hardList = NULL;
2612 yaffs_ObjectHeader *oh;
2614 yaffs_Object *parent;
2616 __u8 chunkData[YAFFS_BYTES_PER_CHUNK];
2618 for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
2623 state = YAFFS_BLOCK_STATE_UNKNOWN;
2625 for(c = 0; c < YAFFS_CHUNKS_PER_BLOCK &&
2626 state == YAFFS_BLOCK_STATE_UNKNOWN; c++)
2628 // Read the spare area and decide what to do
2629 chunk = blk * YAFFS_CHUNKS_PER_BLOCK + c;
2630 yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare);
2633 // Is this a valid block?
2634 if(yaffs_countBits[spare.blockStatus] >= 7)
2636 // This block looks ok, now what's in this chunk?
2637 yaffs_GetTagsFromSpare(&spare,&tags);
2639 if(tags.objectId == YAFFS_UNUSED_OBJECT_ID)
2641 // An unassigned chunk in the block
2642 // This means that either the block is empty or
2643 // this is the one being allocated from
2647 // the block is unused
2648 state = YAFFS_BLOCK_STATE_EMPTY;
2649 dev->nErasedBlocks++;
2653 // this is the block being allocated from
2654 T((" allocating %d %d\n",blk,c));
2655 state = YAFFS_BLOCK_STATE_ALLOCATING;
2656 dev->allocationBlock = blk;
2657 dev->allocationPage = c;
2660 dev->nFreeChunks += (YAFFS_CHUNKS_PER_BLOCK - c);
2662 else if(tags.objectId == 0)
2666 dev->nFreeChunks ++;
2667 T((" %d %d deleted\n",blk,c));
2669 else if(tags.chunkId > 0)
2673 pageBits |= ( 1 <<c);
2674 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
2675 // PutChuunkIntoFIle checks for a clash (two data chunks with
2676 // the same chunkId).
2677 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
2678 T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
2682 // chunkId == 0, so it is an ObjectHeader.
2684 pageBits |= ( 1 <<c);
2685 yaffs_ReadChunkFromNAND(dev,chunk,chunkData,NULL);
2686 oh = (yaffs_ObjectHeader *)chunkData;
2688 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
2691 // todo we have already filled this one. We have
2692 // a duplicate. Need to fix
2695 // we don't have a duplicate...
2698 in->variantType = oh->type;
2700 in->st_mode = oh->st_mode;
2701 in->st_uid = oh->st_uid;
2702 in->st_gid = oh->st_gid;
2703 in->st_atime = oh->st_atime;
2704 in->st_mtime = oh->st_mtime;
2705 in->st_ctime = oh->st_ctime;
2706 in->chunkId = chunk;
2711 // directory stuff...
2712 // hook up to parent
2714 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
2715 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
2717 // Set up as a directory
2718 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
2719 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
2721 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2723 // Hoosterman, another problem....
2724 // We're trying to use a non-directory as a directory
2728 yaffs_AddObjectToDirectory(parent,in);
2730 // Note re hardlinks.
2731 // Since we might scan a hardlink before its equivalent object is scanned
2732 // we put them all in a list.
2733 // After scanning is complete, we should have all the objects, so we run through this
2734 // list and fix up all the chains.
2736 switch(in->variantType)
2738 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
2740 case YAFFS_OBJECT_TYPE_FILE:
2741 in->variant.fileVariant.fileSize = oh->fileSize;
2743 case YAFFS_OBJECT_TYPE_HARDLINK:
2744 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
2745 (yaffs_Object *)(in->hardLinks.next) = hardList;
2748 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
2750 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
2751 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
2754 T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
2760 state = YAFFS_BLOCK_STATE_DEAD;
2764 if(state == YAFFS_BLOCK_STATE_UNKNOWN)
2766 // If we got this far, then the block is fully allocated.
2767 // ie. Full or Dirty
2768 state = (inuse) ? YAFFS_BLOCK_STATE_FULL : YAFFS_BLOCK_STATE_DIRTY;
2772 dev->blockInfo[blk].pageBits = pageBits;
2773 dev->blockInfo[blk].pagesInUse = inuse;
2774 dev->blockInfo[blk].blockState = state;
2778 // Todo fix up the hard link chains
2782 hardList = (yaffs_Object *)(hardList->hardLinks.next);
2784 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
2788 hl->variant.hardLinkVariant.equivalentObject=in;
2789 list_add(&hl->hardLinks,&in->hardLinks);
2793 //Todo Need to report this better.
2794 hl->variant.hardLinkVariant.equivalentObject=NULL;
2795 INIT_LIST_HEAD(&hl->hardLinks);
2807 ////////////////////////// Directory Functions /////////////////////////
2810 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
2813 if(obj->siblings.prev == NULL)
2816 INIT_LIST_HEAD(&obj->siblings);
2819 else if(!list_empty(&obj->siblings))
2821 // If it is holed up somewhere else, un hook it
2822 list_del_init(&obj->siblings);
2825 list_add(&obj->siblings,&directory->variant.directoryVariant.children);
2826 obj->parent = directory;
2829 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
2831 list_del_init(&obj->siblings);
2835 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const char *name)
2839 struct list_head *i;
2840 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
2841 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
2845 sum = yaffs_CalcNameSum(name);
2847 list_for_each(i,&directory->variant.directoryVariant.children)
2849 l = list_entry(i, yaffs_Object,siblings);
2851 // Special case for lost-n-found
2852 if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
2854 if(strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
2859 else if(l->sum == sum)
2862 yaffs_ReadChunkFromNAND(l->myDev,l->chunkId,buffer,NULL);
2863 if(strcmp(name,oh->name) == 0)
2876 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
2878 struct list_head *i;
2882 list_for_each(i,&theDir->variant.directoryVariant.children)
2884 l = list_entry(i, yaffs_Object,siblings);
2896 // GetEquivalentObject dereferences any hard links to get to the
2899 static yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
2901 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
2903 // We want the object id of the equivalent object, not this one
2904 obj = obj->variant.hardLinkVariant.equivalentObject;
2910 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize)
2912 memset(name,0,buffSize);
2914 if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
2916 strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
2920 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
2921 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
2923 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
2925 if(obj->chunkId >= 0)
2927 yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
2929 strncpy(name,oh->name,buffSize - 1);
2932 return strlen(name);
2935 int yaffs_GetObjectFileLength(yaffs_Object *obj)
2938 // Dereference any hard linking
2939 obj = yaffs_GetEquivalentObject(obj);
2941 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
2943 return obj->variant.fileVariant.fileSize;
2945 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
2947 return strlen(obj->variant.symLinkVariant.alias);
2951 // Only a directory should drop through to here
2952 return YAFFS_BYTES_PER_CHUNK;
2956 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
2958 int count = 1; // the object itself
2959 struct list_head *i;
2961 list_for_each(i,&obj->hardLinks)
2970 int yaffs_GetObjectInode(yaffs_Object *obj)
2972 obj = yaffs_GetEquivalentObject(obj);
2974 return obj->objectId;
2977 unsigned yaffs_GetObjectType(yaffs_Object *obj)
2979 obj = yaffs_GetEquivalentObject(obj);
2981 switch(obj->variantType)
2983 case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break;
2984 case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break;
2985 case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break;
2986 case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break;
2987 default: return DT_REG; break;
2991 char *yaffs_GetSymlinkAlias(yaffs_Object *obj)
2993 obj = yaffs_GetEquivalentObject(obj);
2994 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
2996 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
3000 return yaffs_CloneString("");
3005 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
3007 unsigned int valid = attr->ia_valid;
3009 if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
3010 if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
3011 if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
3013 if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime;
3014 if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime;
3015 if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime;
3017 if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
3019 yaffs_UpdateObjectHeader(obj,NULL);
3024 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
3026 unsigned int valid = 0;
3028 attr->ia_mode = obj->st_mode; valid |= ATTR_MODE;
3029 attr->ia_uid = obj->st_uid; valid |= ATTR_UID;
3030 attr->ia_gid = obj->st_gid; valid |= ATTR_GID;
3032 attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME;
3033 attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME;
3034 attr->ia_mtime = obj->st_mtime; valid |= ATTR_MTIME;
3036 attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
3038 attr->ia_valid = valid;
3046 int yaffs_DumpObject(yaffs_Object *obj)
3048 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
3050 // yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
3052 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
3054 if(obj->chunkId >= 0)
3056 yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
3059 yaffs_GetObjectName(obj,name,256);
3061 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d type %d size %d\n",
3062 yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial,
3063 obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
3066 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
3067 obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial,
3068 obj->sum, obj->chunkId));
3069 switch(obj->variantType)
3071 case YAFFS_OBJECT_TYPE_FILE:
3072 YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
3074 case YAFFS_OBJECT_TYPE_DIRECTORY:
3075 YPRINTF((" DIRECTORY\n"));
3077 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
3078 case YAFFS_OBJECT_TYPE_SYMLINK:
3079 case YAFFS_OBJECT_TYPE_UNKNOWN:
3088 ///////////////////////// Initialisation code ///////////////////////////
3092 int yaffs_GutsInitialise(yaffs_Device *dev)
3100 if(!yaffs_CheckStructures())
3106 // OK now calculate a few things for the device
3107 // Calculate chunkGroupBits.
3108 // If there are 64k or less chunks then this is 1
3109 // Else it is log2(nChunks) - 16
3111 x = nChunks = YAFFS_CHUNKS_PER_BLOCK * dev->nBlocks;
3113 for(bits = 0, x = nChunks; (x & 1) == 0; bits++)
3121 YPRINTF(("nBlocks should be a power of 2 but is %u\n",
3128 dev->chunkGroupBits = 0;
3129 dev->chunkGroupSize = 1;
3133 dev->chunkGroupBits = bits - 16;
3134 dev->chunkGroupSize = nChunks/0x10000;
3137 // More device initialisation
3138 dev->garbageCollectionRequired = 0;
3139 dev->currentDirtyChecker = 0;
3141 yaffs_InitialiseBlocks(dev);
3143 yaffs_InitialiseTnodes(dev);
3145 yaffs_InitialiseObjects(dev);
3148 // Initialise the root and lost and found directories
3149 dev->lostNFoundDir = dev->rootDir = NULL;
3150 dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
3151 dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_ROOT_MODE | S_IFDIR);
3152 yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
3154 // Now scan the flash.
3162 void yaffs_Deinitialise(yaffs_Device *dev)
3164 yaffs_DeinitialiseBlocks(dev);
3165 yaffs_DeinitialiseTnodes(dev);
3166 yaffs_DeinitialiseObjects(dev);
3170 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
3172 int nFree = dev->nFreeChunks - (YAFFS_CHUNKS_PER_BLOCK * YAFFS_RESERVED_BLOCKS);
3174 return (nFree < 0) ? 0 : nFree;
3179 /////////////////// YAFFS test code //////////////////////////////////
3181 #define yaffs_CheckStruct(structure,syze, name) \
3182 if(sizeof(structure) != syze) \
3183 { YPRINTF(("%s should be %d but is %d\n",name,syze,sizeof(structure))); \
3184 return YAFFS_FAIL; \
3188 static int yaffs_CheckStructures(void)
3190 yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
3191 yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
3192 yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
3193 yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
3194 yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
3200 void yaffs_GutsTest(yaffs_Device *dev)
3203 if(yaffs_CheckStructures() != YAFFS_OK)
3205 YPRINTF(("One or more structures malformed-- aborting\n"));
3209 YPRINTF(("Structures OK\n"));
3212 yaffs_TnodeTest(dev);
3213 yaffs_ObjectTest(dev);