2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
\r
3 * yaffs_guts.c The main guts of YAFFS
\r
5 * Copyright (C) 2002 Aleph One Ltd.
\r
6 * for Toby Churchill Ltd and Brightstar Engineering
\r
8 * Created by Charles Manning <charles@aleph1.co.uk>
\r
10 * This program is free software; you can redistribute it and/or modify
\r
11 * it under the terms of the GNU General Public License version 2 as
\r
12 * published by the Free Software Foundation.
\r
17 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.12 2002-11-08 07:19:41 charles Exp $";
\r
19 #include "yportenv.h"
\r
21 #include "yaffsinterface.h"
\r
22 #include "yaffs_guts.h"
\r
25 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
\r
29 // External functions for ECC on data
\r
30 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
\r
31 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
\r
34 // countBits is a quick way of counting the number of bits in a byte.
\r
35 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
\r
37 static const char yaffs_countBitsTable[256] =
\r
39 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
\r
40 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
\r
41 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
\r
42 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
\r
43 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
\r
44 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
\r
45 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
\r
46 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
\r
47 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
\r
48 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
\r
49 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
\r
50 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
\r
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
\r
52 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
\r
53 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
\r
54 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
\r
57 static int yaffs_CountBits(__u8 x)
\r
60 retVal = yaffs_countBitsTable[x];
\r
67 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
\r
68 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
\r
69 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
\r
70 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
\r
72 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
\r
73 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
\r
74 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force);
\r
75 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId);
\r
76 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
\r
77 static int yaffs_CheckStructures(void);
\r
78 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
\r
79 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
\r
81 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
\r
84 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
\r
85 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
\r
86 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
\r
87 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
\r
88 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
\r
90 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
\r
92 static int yaffs_UnlinkWorker(yaffs_Object *obj);
\r
95 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1);
\r
99 loff_t yaffs_GetFileSize(yaffs_Object *obj);
\r
101 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
\r
102 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
\r
105 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
\r
107 #ifdef YAFFS_PARANOID
\r
108 static int yaffs_CheckFileSanity(yaffs_Object *in);
\r
110 #define yaffs_CheckFileSanity(in)
\r
113 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
\r
114 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
\r
117 static __inline__ yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
\r
119 if(blk < dev->startBlock || blk > dev->endBlock)
\r
121 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: block %d is not valid" TENDSTR),blk));
\r
124 return &dev->blockInfo[blk - dev->startBlock];
\r
128 static __inline__ int yaffs_HashFunction(int n)
\r
130 return (n % YAFFS_NOBJECT_BUCKETS);
\r
134 yaffs_Object *yaffs_Root(yaffs_Device *dev)
\r
136 return dev->rootDir;
\r
139 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
\r
141 return dev->lostNFoundDir;
\r
145 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
\r
147 if(chunkInNAND < dev->startBlock * YAFFS_CHUNKS_PER_BLOCK)
\r
149 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
\r
153 dev->nPageWrites++;
\r
154 return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
\r
158 int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
\r
161 yaffs_Spare *spare,
\r
162 int doErrorCorrection)
\r
166 yaffs_Spare localSpare;
\r
167 int eccResult1,eccResult2;
\r
176 // If we don't have a real spare, then we use a local one.
\r
177 // Need this for the calculation of the ecc
\r
178 spare = &localSpare;
\r
181 retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
\r
182 if(data && doErrorCorrection)
\r
184 // Do ECC correction
\r
185 //Todo handle any errors
\r
186 nand_calculate_ecc(data,calcEcc);
\r
187 eccResult1 = nand_correct_data (data,spare->ecc1, calcEcc);
\r
188 nand_calculate_ecc(&data[256],calcEcc);
\r
189 eccResult2 = nand_correct_data (&data[256],spare->ecc2, calcEcc);
\r
193 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
\r
196 else if(eccResult1<0)
\r
198 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
\r
204 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
\r
207 else if(eccResult2<0)
\r
209 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
\r
213 if(eccResult1 || eccResult2)
\r
215 // Hoosterman, we had a data problem on this page
\r
216 yaffs_HandleReadDataError(dev,chunkInNAND);
\r
224 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
\r
227 static int init = 0;
\r
228 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
\r
229 static __u8 data[YAFFS_BYTES_PER_CHUNK];
\r
230 static __u8 spare[16];
\r
233 dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare);
\r
239 memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);
\r
243 if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return YAFFS_FAIL;
\r
244 if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;
\r
253 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
\r
255 dev->nBlockErasures++;
\r
256 return dev->eraseBlockInNAND(dev,blockInNAND);
\r
259 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
\r
261 return dev->initialiseNAND(dev);
\r
264 static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve)
\r
271 unsigned char rbData[YAFFS_BYTES_PER_CHUNK];
\r
272 yaffs_Spare rbSpare;
\r
275 chunk = yaffs_AllocateChunk(dev,useReserve);
\r
280 // First check this chunk is erased...
\r
281 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
\r
282 writeOk = yaffs_CheckChunkErased(dev,chunk);
\r
286 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
\r
290 writeOk = yaffs_WriteChunkToNAND(dev,chunk,data,spare);
\r
295 // Readback & verify
\r
296 // If verify fails, then delete this chunk and try again
\r
297 // To verify we compare everything except the block and
\r
298 // page status bytes.
\r
299 // NB We check a raw read without ECC correction applied
\r
300 yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0);
\r
302 #ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
\r
303 if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare))
\r
306 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk));
\r
315 // Copy the data into the write buffer.
\r
316 // NB We do this at the end to prevent duplicates in the case of a write error.
\r
318 yaffs_HandleWriteChunkOk(dev,chunk,data,spare);
\r
322 yaffs_HandleWriteChunkError(dev,chunk);
\r
326 } while(chunk >= 0 && ! writeOk);
\r
330 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
\r
331 dev->nRetriedWrites+= (attempts - 1);
\r
338 // Functions for robustisizing
\r
342 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
\r
344 // Ding the blockStatus in the first two pages of the block.
\r
348 memset(&spare, 0xff,sizeof(yaffs_Spare));
\r
350 spare.blockStatus = 0;
\r
352 yaffs_WriteChunkToNAND(dev, blockInNAND * YAFFS_CHUNKS_PER_BLOCK, NULL , &spare);
\r
353 yaffs_WriteChunkToNAND(dev, blockInNAND * YAFFS_CHUNKS_PER_BLOCK + 1, NULL , &spare);
\r
355 yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
\r
356 dev->nRetiredBlocks++;
\r
361 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
\r
363 dev->doingBufferedBlockRewrite = 1;
\r
365 // Remove erased chunks
\r
366 // Rewrite existing chunks to a new block
\r
367 // Set current write block to the new block
\r
369 dev->doingBufferedBlockRewrite = 0;
\r
375 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
\r
377 int blockInNAND = chunkInNAND/YAFFS_CHUNKS_PER_BLOCK;
\r
379 // Mark the block for retirement
\r
380 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
\r
381 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
\r
385 // Just do a garbage collection on the affected block then retire the block
\r
390 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
\r
394 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
\r
398 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
\r
402 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
\r
404 int blockInNAND = chunkInNAND/YAFFS_CHUNKS_PER_BLOCK;
\r
406 // Mark the block for retirement
\r
407 yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
\r
408 // Delete the chunk
\r
409 yaffs_DeleteChunk(dev,chunkInNAND);
\r
415 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
\r
419 if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
\r
420 s0->tagByte0 != s1->tagByte0 ||
\r
421 s0->tagByte1 != s1->tagByte1 ||
\r
422 s0->tagByte2 != s1->tagByte2 ||
\r
423 s0->tagByte3 != s1->tagByte3 ||
\r
424 s0->tagByte4 != s1->tagByte4 ||
\r
425 s0->tagByte5 != s1->tagByte5 ||
\r
426 s0->tagByte6 != s1->tagByte6 ||
\r
427 s0->tagByte7 != s1->tagByte7 ||
\r
428 s0->ecc1[0] != s1->ecc1[0] ||
\r
429 s0->ecc1[1] != s1->ecc1[1] ||
\r
430 s0->ecc1[2] != s1->ecc1[2] ||
\r
431 s0->ecc2[0] != s1->ecc2[0] ||
\r
432 s0->ecc2[1] != s1->ecc2[1] ||
\r
433 s0->ecc2[2] != s1->ecc2[2] )
\r
442 ///////////////////////// Object management //////////////////
\r
443 // List of spare objects
\r
444 // The list is hooked together using the first pointer
\r
447 // static yaffs_Object *yaffs_freeObjects = NULL;
\r
449 // static int yaffs_nFreeObjects;
\r
451 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
\r
453 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
\r
456 static __u16 yaffs_CalcNameSum(const char *name)
\r
461 __u8 *bname = (__u8 *)name;
\r
464 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
\r
466 #ifdef CONFIG_YAFFS_WINCE
\r
467 sum += toupper(*bname) * i;
\r
469 sum += (*bname) * i;
\r
478 void yaffs_SetObjectName(yaffs_Object *obj, const char *name)
\r
480 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
\r
481 if(name && strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
\r
483 strcpy(obj->shortName,name);
\r
487 obj->shortName[0]='\0';
\r
490 obj->sum = yaffs_CalcNameSum(name);
\r
493 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
\r
495 nand_calculate_ecc (data , spare->ecc1);
\r
496 nand_calculate_ecc (&data[256] , spare->ecc2);
\r
499 void yaffs_CalcTagsECC(yaffs_Tags *tags)
\r
501 // Calculate an ecc
\r
503 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
\r
510 for(i = 0; i < 8; i++)
\r
512 for(j = 1; j &0xff; j<<=1)
\r
527 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
\r
529 unsigned ecc = tags->ecc;
\r
531 yaffs_CalcTagsECC(tags);
\r
535 if(ecc && ecc <= 64)
\r
537 // TODO: Handle the failure better. Retire?
\r
538 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
\r
542 b[ecc / 8] ^= (1 << (ecc & 7));
\r
544 // Now recvalc the ecc
\r
545 yaffs_CalcTagsECC(tags);
\r
547 return 1; // recovered error
\r
551 // Wierd ecc failure value
\r
552 // TODO Need to do somethiong here
\r
553 return -1; //unrecovered error
\r
560 ///////////////////////// TNODES ///////////////////////
\r
562 // List of spare tnodes
\r
563 // The list is hooked together using the first pointer
\r
566 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
\r
568 // static int yaffs_nFreeTnodes;
\r
570 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
\r
574 // yaffs_CreateTnodes creates a bunch more tnodes and
\r
575 // adds them to the tnode free list.
\r
576 // Don't use this function directly
\r
578 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
\r
581 yaffs_Tnode *newTnodes;
\r
582 yaffs_TnodeList *tnl;
\r
584 if(nTnodes < 1) return YAFFS_OK;
\r
586 // make these things
\r
588 newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
\r
592 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
\r
596 // Hook them into the free list
\r
597 for(i = 0; i < nTnodes - 1; i++)
\r
599 newTnodes[i].internal[0] = &newTnodes[i+1];
\r
602 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
\r
603 dev->freeTnodes = newTnodes;
\r
604 dev->nFreeTnodes+= nTnodes;
\r
605 dev->nTnodesCreated += nTnodes;
\r
607 // Now add this bunch of tnodes to a list for freeing up.
\r
608 // NB If we can't add this to the management list it isn't fatal
\r
609 // but it just means we can't free this bunch of tnodes later.
\r
610 tnl = YMALLOC(sizeof(yaffs_TnodeList));
\r
613 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
\r
618 tnl->tnodes = newTnodes;
\r
619 tnl->next = dev->allocatedTnodeList;
\r
620 dev->allocatedTnodeList = tnl;
\r
624 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
\r
631 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
\r
632 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
\r
634 yaffs_Tnode *tn = NULL;
\r
636 // If there are none left make more
\r
637 if(!dev->freeTnodes)
\r
639 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
\r
642 if(dev->freeTnodes)
\r
644 tn = dev->freeTnodes;
\r
645 dev->freeTnodes = dev->freeTnodes->internal[0];
\r
646 dev->nFreeTnodes--;
\r
648 memset(tn,0,sizeof(yaffs_Tnode));
\r
656 // FreeTnode frees up a tnode and puts it back on the free list
\r
657 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
\r
659 tn->internal[0] = dev->freeTnodes;
\r
660 dev->freeTnodes = tn;
\r
661 dev->nFreeTnodes++;
\r
665 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
\r
667 // Free the list of allocated tnodes
\r
669 while(dev->allocatedTnodeList)
\r
671 YFREE(dev->allocatedTnodeList->tnodes);
\r
672 dev->allocatedTnodeList = dev->allocatedTnodeList->next;
\r
675 dev->freeTnodes = NULL;
\r
676 dev->nFreeTnodes = 0;
\r
679 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
\r
681 dev->allocatedTnodeList = NULL;
\r
682 dev->freeTnodes = NULL;
\r
683 dev->nFreeTnodes = 0;
\r
684 dev->nTnodesCreated = 0;
\r
689 void yaffs_TnodeTest(yaffs_Device *dev)
\r
694 yaffs_Tnode *tn[1000];
\r
696 YINFO("Testing TNodes");
\r
698 for(j = 0; j < 50; j++)
\r
700 for(i = 0; i < 1000; i++)
\r
702 tn[i] = yaffs_GetTnode(dev);
\r
705 YALERT("Getting tnode failed");
\r
708 for(i = 0; i < 1000; i+=3)
\r
710 yaffs_FreeTnode(dev,tn[i]);
\r
719 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
\r
721 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
\r
722 // The look up tree is represented by the top tnode and the number of topLevel
\r
723 // in the tree. 0 means only the level 0 tnode is in the tree.
\r
726 // FindLevel0Tnode finds the level 0 tnode, if one exists.
\r
727 // Used when reading.....
\r
728 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
\r
731 yaffs_Tnode *tn = fStruct->top;
\r
733 int requiredTallness;
\r
734 int level = fStruct->topLevel;
\r
736 // Check sane level and chunk Id
\r
737 if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
\r
740 // sprintf(str,"Bad level %d",level);
\r
745 if(chunkId > YAFFS_MAX_CHUNK_ID)
\r
748 // sprintf(str,"Bad chunkId %d",chunkId);
\r
753 // First check we're tall enough (ie enough topLevel)
\r
755 i = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
\r
756 requiredTallness = 0;
\r
759 i >>= YAFFS_TNODES_INTERNAL_BITS;
\r
760 requiredTallness++;
\r
764 if(requiredTallness > fStruct->topLevel)
\r
766 // Not tall enough, so we can't find it, return NULL.
\r
771 // Traverse down to level 0
\r
772 while (level > 0 && tn)
\r
774 tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) &
\r
775 YAFFS_TNODES_INTERNAL_MASK];
\r
783 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
\r
784 // This happens in two steps:
\r
785 // 1. If the tree isn't tall enough, then make it taller.
\r
786 // 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
\r
788 // Used when modifying the tree.
\r
790 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
\r
795 int requiredTallness;
\r
802 //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
\r
804 // Check sane level and page Id
\r
805 if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
\r
808 // sprintf(str,"Bad level %d",fStruct->topLevel);
\r
813 if(chunkId > YAFFS_MAX_CHUNK_ID)
\r
816 // sprintf(str,"Bad chunkId %d",chunkId);
\r
821 // First check we're tall enough (ie enough topLevel)
\r
823 x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
\r
824 requiredTallness = 0;
\r
827 x >>= YAFFS_TNODES_INTERNAL_BITS;
\r
828 requiredTallness++;
\r
831 //T((TSTR(" required=%d"),requiredTallness));
\r
834 if(requiredTallness > fStruct->topLevel)
\r
836 // Not tall enough,gotta make the tree taller
\r
837 for(i = fStruct->topLevel; i < requiredTallness; i++)
\r
839 //T((TSTR(" add new top")));
\r
841 tn = yaffs_GetTnode(dev);
\r
845 tn->internal[0] = fStruct->top;
\r
850 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
\r
854 fStruct->topLevel = requiredTallness;
\r
858 // Traverse down to level 0, adding anything we need
\r
860 l = fStruct->topLevel;
\r
862 while (l > 0 && tn)
\r
864 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) &
\r
865 YAFFS_TNODES_INTERNAL_MASK;
\r
867 //T((TSTR(" [%d:%d]"),l,i));
\r
869 if(!tn->internal[x])
\r
871 //T((TSTR(" added")));
\r
873 tn->internal[x] = yaffs_GetTnode(dev);
\r
876 tn = tn->internal[x];
\r
886 // DeleteWorker scans backwards through the tnode tree and deletes all the
\r
887 // chunks and tnodes in the file
\r
888 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
\r
890 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
\r
906 for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
\r
908 if(tn->internal[i])
\r
910 if(limit && (*limit) < 0)
\r
916 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
\r
917 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
\r
921 yaffs_FreeTnode(in->myDev,tn->internal[i]);
\r
922 tn->internal[i] = NULL;
\r
927 return (allDone) ? 1 : 0;
\r
929 else if(level == 0)
\r
931 for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0; i--) //NB Don't apply the limit here, always delete a whole level0
\r
937 chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
\r
939 theChunk = tn->level0[i] << in->myDev->chunkGroupBits;
\r
941 // Now we need to search for it
\r
942 for(j = 0,found = 0; theChunk && j < in->myDev->chunkGroupSize && !found; j++)
\r
944 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,&tags,&chunkDeleted);
\r
945 if(yaffs_TagsMatch(&tags,in->objectId,chunkInInode,chunkDeleted))
\r
959 yaffs_DeleteChunk(in->myDev,theChunk);
\r
986 // Pruning removes any part of the file structure tree that is beyond the
\r
987 // bounds of the file (ie that does not point to chunks).
\r
989 // A file should only get pruned when its size is reduced.
\r
991 // Before pruning, the chunks must be pulled from the tree and the
\r
992 // level 0 tnode entries must be zeroed out.
\r
993 // Could also use this for file deletion, but that's probably better handled
\r
994 // by a special case.
\r
996 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
\r
998 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
\r
1007 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
\r
1009 if(tn->internal[i] && level > 0)
\r
1011 tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
\r
1014 if(tn->internal[i])
\r
1020 if(hasData == 0 && del0)
\r
1022 // Free and return NULL
\r
1024 yaffs_FreeTnode(dev,tn);
\r
1034 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
\r
1041 if(fStruct->topLevel > 0)
\r
1043 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
\r
1045 // Now we have a tree with all the non-zero branches NULL but the height
\r
1046 // is the same as it was.
\r
1047 // Let's see if we can trim internal tnodes to shorten the tree.
\r
1048 // We can do this if only the 0th element in the tnode is in use
\r
1049 // (ie all the non-zero are NULL)
\r
1051 while(fStruct->topLevel && !done)
\r
1053 tn = fStruct->top;
\r
1056 for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
\r
1058 if(tn->internal[i])
\r
1066 fStruct->top = tn->internal[0];
\r
1067 fStruct->topLevel--;
\r
1068 yaffs_FreeTnode(dev,tn);
\r
1084 /////////////////////// End of File Structure functions. /////////////////
\r
1086 // yaffs_CreateFreeObjects creates a bunch more objects and
\r
1087 // adds them to the object free list.
\r
1088 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
\r
1091 yaffs_Object *newObjects;
\r
1092 yaffs_ObjectList *list;
\r
1094 if(nObjects < 1) return YAFFS_OK;
\r
1096 // make these things
\r
1098 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
\r
1102 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
\r
1103 return YAFFS_FAIL;
\r
1106 // Hook them into the free list
\r
1107 for(i = 0; i < nObjects - 1; i++)
\r
1109 (yaffs_Object *)newObjects[i].siblings.next = &newObjects[i+1];
\r
1112 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
\r
1113 dev->freeObjects = newObjects;
\r
1114 dev->nFreeObjects+= nObjects;
\r
1115 dev->nObjectsCreated+= nObjects;
\r
1117 // Now add this bunch of Objects to a list for freeing up.
\r
1119 list = YMALLOC(sizeof(yaffs_ObjectList));
\r
1122 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
\r
1126 list->objects = newObjects;
\r
1127 list->next = dev->allocatedObjectList;
\r
1128 dev->allocatedObjectList = list;
\r
1137 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
\r
1138 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
\r
1140 yaffs_Object *tn = NULL;
\r
1142 // If there are none left make more
\r
1143 if(!dev->freeObjects)
\r
1145 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
\r
1148 if(dev->freeObjects)
\r
1150 tn = dev->freeObjects;
\r
1151 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
\r
1152 dev->nFreeObjects--;
\r
1154 // Now sweeten it up...
\r
1156 memset(tn,0,sizeof(yaffs_Object));
\r
1159 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
\r
1160 INIT_LIST_HEAD(&(tn->hardLinks));
\r
1161 INIT_LIST_HEAD(&(tn->hashLink));
\r
1162 INIT_LIST_HEAD(&tn->siblings);
\r
1164 // Add it to the lost and found directory.
\r
1165 // NB Can't put root or lostNFound in lostNFound so
\r
1166 // check if lostNFound exists first
\r
1167 if(dev->lostNFoundDir)
\r
1169 yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);
\r
1177 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
\r
1180 yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);
\r
1183 obj->fake = 1; // it is fake so it has no NAND presence...
\r
1184 obj->renameAllowed= 0; // ... and we're not allowed to rename it...
\r
1185 obj->unlinkAllowed= 0; // ... or unlink it
\r
1187 obj->unlinked = 0;
\r
1188 obj->st_mode = mode;
\r
1190 obj->chunkId = 0; // Not a valid chunk.
\r
1198 static void yaffs_UnhashObject(yaffs_Object *tn)
\r
1201 yaffs_Device *dev = tn->myDev;
\r
1204 // If it is still linked into the bucket list, free from the list
\r
1205 if(!list_empty(&tn->hashLink))
\r
1207 list_del_init(&tn->hashLink);
\r
1208 bucket = yaffs_HashFunction(tn->objectId);
\r
1209 dev->objectBucket[bucket].count--;
\r
1215 // FreeObject frees up a Object and puts it back on the free list
\r
1216 static void yaffs_FreeObject(yaffs_Object *tn)
\r
1219 yaffs_Device *dev = tn->myDev;
\r
1221 yaffs_UnhashObject(tn);
\r
1223 // Link into the free list.
\r
1224 (yaffs_Object *)(tn->siblings.next) = dev->freeObjects;
\r
1225 dev->freeObjects = tn;
\r
1226 dev->nFreeObjects++;
\r
1232 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
\r
1234 // Free the list of allocated Objects
\r
1236 while( dev->allocatedObjectList)
\r
1238 YFREE(dev->allocatedObjectList->objects);
\r
1239 dev->allocatedObjectList = dev->allocatedObjectList->next;
\r
1242 dev->freeObjects = NULL;
\r
1243 dev->nFreeObjects = 0;
\r
1246 static void yaffs_InitialiseObjects(yaffs_Device *dev)
\r
1250 dev->allocatedObjectList = NULL;
\r
1251 dev->freeObjects = NULL;
\r
1252 dev->nFreeObjects = 0;
\r
1254 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
\r
1256 INIT_LIST_HEAD(&dev->objectBucket[i].list);
\r
1257 dev->objectBucket[i].count = 0;
\r
1267 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
\r
1272 int lowest = 999999;
\r
1275 // First let's see if we can find one that's empty.
\r
1277 for(i = 0; i < 10 && lowest > 0; i++)
\r
1280 x %= YAFFS_NOBJECT_BUCKETS;
\r
1281 if(dev->objectBucket[x].count < lowest)
\r
1283 lowest = dev->objectBucket[x].count;
\r
1289 // If we didn't find an empty list, then try
\r
1290 // looking a bit further for a short one
\r
1292 for(i = 0; i < 10 && lowest > 3; i++)
\r
1295 x %= YAFFS_NOBJECT_BUCKETS;
\r
1296 if(dev->objectBucket[x].count < lowest)
\r
1298 lowest = dev->objectBucket[x].count;
\r
1307 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
\r
1309 int bucket = yaffs_FindNiceObjectBucket(dev);
\r
1311 // Now find an object value that has not already been taken
\r
1312 // by scanning the list.
\r
1315 struct list_head *i;
\r
1317 __u32 n = (__u32)bucket;
\r
1319 //yaffs_CheckObjectHashSanity();
\r
1324 n += YAFFS_NOBJECT_BUCKETS;
\r
1325 if(1 ||dev->objectBucket[bucket].count > 0)
\r
1327 list_for_each(i,&dev->objectBucket[bucket].list)
\r
1329 // If there is already one in the list
\r
1330 if(list_entry(i, yaffs_Object,hashLink)->objectId == n)
\r
1338 //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
\r
1343 void yaffs_HashObject(yaffs_Object *in)
\r
1345 int bucket = yaffs_HashFunction(in->objectId);
\r
1346 yaffs_Device *dev = in->myDev;
\r
1348 if(!list_empty(&in->hashLink))
\r
1354 list_add(&in->hashLink,&dev->objectBucket[bucket].list);
\r
1355 dev->objectBucket[bucket].count++;
\r
1359 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
\r
1361 int bucket = yaffs_HashFunction(number);
\r
1362 struct list_head *i;
\r
1365 list_for_each(i,&dev->objectBucket[bucket].list)
\r
1367 // Look if it is in the list
\r
1368 in = list_entry(i, yaffs_Object,hashLink);
\r
1369 if(in->objectId == number)
\r
1380 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
\r
1383 yaffs_Object *theObject;
\r
1387 number = yaffs_CreateNewObjectNumber(dev);
\r
1390 theObject = yaffs_AllocateEmptyObject(dev);
\r
1394 theObject->fake = 0;
\r
1395 theObject->renameAllowed = 1;
\r
1396 theObject->unlinkAllowed = 1;
\r
1397 theObject->objectId = number;
\r
1398 yaffs_HashObject(theObject);
\r
1399 theObject->variantType = type;
\r
1400 #ifdef CONFIG_YAFFS_WINCE
\r
1401 yfsd_WinFileTimeNow(theObject->win_atime);
\r
1402 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
\r
1403 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
\r
1406 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = CURRENT_TIME;
\r
1410 case YAFFS_OBJECT_TYPE_FILE:
\r
1411 theObject->variant.fileVariant.fileSize = 0;
\r
1412 theObject->variant.fileVariant.scannedFileSize = 0;
\r
1413 theObject->variant.fileVariant.topLevel = 0;
\r
1414 theObject->variant.fileVariant.top = yaffs_GetTnode(dev);
\r
1416 case YAFFS_OBJECT_TYPE_DIRECTORY:
\r
1417 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
\r
1419 case YAFFS_OBJECT_TYPE_SYMLINK:
\r
1420 // No action required
\r
1422 case YAFFS_OBJECT_TYPE_HARDLINK:
\r
1423 // No action required
\r
1425 case YAFFS_OBJECT_TYPE_SPECIAL:
\r
1426 // No action required
\r
1428 case YAFFS_OBJECT_TYPE_UNKNOWN:
\r
1429 // todo this should not happen
\r
1437 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
\r
1439 yaffs_Object *theObject = NULL;
\r
1443 theObject = yaffs_FindObjectByNumber(dev,number);
\r
1448 theObject = yaffs_CreateNewObject(dev,number,type);
\r
1455 char *yaffs_CloneString(const char *str)
\r
1457 char *newStr = NULL;
\r
1461 newStr = YMALLOC(strlen(str) + 1);
\r
1462 strcpy(newStr,str);
\r
1470 // Mknod (create) a new object.
\r
1471 // equivalentObject only has meaning for a hard link;
\r
1472 // aliasString only has meaning for a sumlink.
\r
1473 // rdev only has meaning for devices (a subset of special objects)
\r
1474 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
\r
1475 yaffs_Object *parent,
\r
1476 const char *name,
\r
1480 yaffs_Object *equivalentObject,
\r
1481 const char *aliasString,
\r
1486 yaffs_Device *dev = parent->myDev;
\r
1488 // Check if the entry exists. If it does then fail the call since we don't want a dup.
\r
1489 if(yaffs_FindObjectByName(parent,name))
\r
1494 in = yaffs_CreateNewObject(dev,-1,type);
\r
1500 in->variantType = type;
\r
1502 in->st_mode = mode;
\r
1504 #ifdef CONFIG_YAFFS_WINCE
\r
1505 yfsd_WinFileTimeNow(in->win_atime);
\r
1506 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
\r
1507 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[0];
\r
1510 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
\r
1511 in->st_rdev = rdev;
\r
1515 in->nDataChunks = 0;
\r
1517 yaffs_SetObjectName(in,name);
\r
1520 yaffs_AddObjectToDirectory(parent,in);
\r
1522 in->myDev = parent->myDev;
\r
1527 case YAFFS_OBJECT_TYPE_SYMLINK:
\r
1528 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
\r
1530 case YAFFS_OBJECT_TYPE_HARDLINK:
\r
1531 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
\r
1532 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
\r
1533 list_add(&in->hardLinks,&equivalentObject->hardLinks);
\r
1535 case YAFFS_OBJECT_TYPE_FILE: // do nothing
\r
1536 case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
\r
1537 case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
\r
1538 case YAFFS_OBJECT_TYPE_UNKNOWN:
\r
1542 if(yaffs_GetNumberOfFreeChunks(dev) <= 0 ||
\r
1543 yaffs_UpdateObjectHeader(in,name,0) < 0)
\r
1545 // Could not create the object header, fail the creation
\r
1546 yaffs_UnlinkWorker(in);
\r
1555 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
\r
1557 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
\r
1560 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
\r
1562 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
\r
1565 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
\r
1567 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
\r
1570 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,const char *alias)
\r
1572 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
\r
1575 // NB yaffs_Link returns the object id of the equivalent object.
\r
1576 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject)
\r
1578 // Get the real object in case we were fed a hard link as an equivalent object
\r
1579 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
\r
1581 if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
\r
1583 return equivalentObject;
\r
1593 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName,int force)
\r
1597 if(newDir == NULL)
\r
1599 newDir = obj->parent; // use the old directory
\r
1602 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
\r
1604 // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
\r
1605 // duplicate names are allowed.
\r
1606 // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
\r
1609 !yaffs_FindObjectByName(newDir,newName)) &&
\r
1610 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
\r
1612 yaffs_SetObjectName(obj,newName);
\r
1615 yaffs_AddObjectToDirectory(newDir,obj);
\r
1617 if(unlinkOp) obj->unlinked = 1;
\r
1620 if(yaffs_UpdateObjectHeader(obj,newName,0) >= 0)
\r
1626 return YAFFS_FAIL;
\r
1631 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName)
\r
1633 yaffs_Object *obj;
\r
1636 #ifdef CONFIG_YAFFS_WINCE
\r
1637 // Special case for WinCE.
\r
1638 // While look-up is case insensitive, the name isn't.
\r
1639 // THerefore we might want to change x.txt to X.txt
\r
1640 if(oldDir == newDir && _stricmp(oldName,newName) == 0)
\r
1646 obj = yaffs_FindObjectByName(oldDir,oldName);
\r
1647 if(obj && obj->renameAllowed)
\r
1649 return yaffs_ChangeObjectName(obj,newDir,newName,force);
\r
1651 return YAFFS_FAIL;
\r
1656 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
\r
1658 // Scan the buckets and check that the lists
\r
1659 // have as many members as the count says there are
\r
1662 struct list_head *j;
\r
1663 int ok = YAFFS_OK;
\r
1665 for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
\r
1669 list_for_each(j,&dev->objectBucket[bucket].list)
\r
1674 if(countEm != dev->objectBucket[bucket].count)
\r
1676 T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
\r
1685 void yaffs_ObjectTest(yaffs_Device *dev)
\r
1687 yaffs_Object *in[1000];
\r
1689 yaffs_Object *inold[1000];
\r
1693 memset(in,0,1000*sizeof(yaffs_Object *));
\r
1694 memset(inold,0,1000*sizeof(yaffs_Object *));
\r
1696 yaffs_CheckObjectHashSanity(dev);
\r
1698 for(j = 0; j < 10; j++)
\r
1702 for(i = 0; i < 1000; i++)
\r
1704 in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
\r
1707 YINFO("No more inodes");
\r
1711 inNo[i] = in[i]->objectId;
\r
1715 for(i = 0; i < 1000; i++)
\r
1717 if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
\r
1719 //T(("Differnce in look up test\n"));
\r
1723 // T(("Look up ok\n"));
\r
1727 yaffs_CheckObjectHashSanity(dev);
\r
1729 for(i = 0; i < 1000; i+=3)
\r
1731 yaffs_FreeObject(in[i]);
\r
1736 yaffs_CheckObjectHashSanity(dev);
\r
1743 /////////////////////////// Block Management and Page Allocation ///////////////////
\r
1746 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
\r
1748 dev->allocationBlock = -1; // force it to get a new one
\r
1749 //Todo we're assuming the malloc will pass.
\r
1750 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
\r
1751 if(dev->blockInfo)
\r
1753 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
\r
1756 return YAFFS_FAIL;
\r
1760 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
\r
1762 YFREE(dev->blockInfo);
\r
1765 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
\r
1766 // for garbage collection.
\r
1768 static int yaffs_FindDirtiestBlock(yaffs_Device *dev)
\r
1771 int b = dev->currentDirtyChecker;
\r
1774 int dirtiest = -1;
\r
1775 int pagesInUse = 100; // silly big number
\r
1776 yaffs_BlockInfo *bi;
\r
1778 for(i = dev->startBlock; i <= dev->endBlock && pagesInUse > 2 ; i++)
\r
1781 if ( b < dev->startBlock || b > dev->endBlock)
\r
1783 b = dev->startBlock;
\r
1786 if(b < dev->startBlock || b > dev->endBlock)
\r
1788 T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
\r
1792 bi = yaffs_GetBlockInfo(dev,b);
\r
1794 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
\r
1795 bi->pagesInUse < pagesInUse)
\r
1798 pagesInUse = bi->pagesInUse;
\r
1802 dev->currentDirtyChecker = b;
\r
1808 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
\r
1810 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
\r
1814 // If the block is still healthy erase it and mark as clean.
\r
1815 // If the block has had a data failure, then retire it.
\r
1816 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
\r
1818 if(!bi->needsRetiring)
\r
1820 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
\r
1823 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
\r
1829 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
\r
1830 dev->nErasedBlocks++;
\r
1831 bi->pagesInUse = 0;
\r
1834 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
\r
1838 yaffs_RetireBlock(dev,blockNo);
\r
1839 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
\r
1844 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
\r
1847 yaffs_BlockInfo *bi;
\r
1849 if(dev->nErasedBlocks < 1)
\r
1851 // Hoosterman we've got a problem.
\r
1852 // Can't get space to gc
\r
1853 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: no space during gc" TENDSTR)));
\r
1858 // Find an empty block.
\r
1860 for(i = dev->startBlock; i <= dev->endBlock; i++)
\r
1862 bi = yaffs_GetBlockInfo(dev,i);
\r
1863 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
\r
1865 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
\r
1866 dev->nErasedBlocks--;
\r
1876 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
\r
1879 yaffs_BlockInfo *bi;
\r
1881 if(dev->allocationBlock < 0)
\r
1883 // Get next block to allocate off
\r
1884 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
\r
1885 dev->allocationPage = 0;
\r
1888 if(!useReserve && dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS)
\r
1890 // Not enough space to allocate unless we're allowed to use the reserve.
\r
1894 // Next page please....
\r
1895 if(dev->allocationBlock >= 0)
\r
1897 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
\r
1899 retVal = (dev->allocationBlock * YAFFS_CHUNKS_PER_BLOCK) +
\r
1900 dev->allocationPage;
\r
1902 bi->pageBits |= (1 << (dev->allocationPage));
\r
1904 dev->allocationPage++;
\r
1906 dev->nFreeChunks--;
\r
1908 // If the block is full set the state to full
\r
1909 if(dev->allocationPage >= YAFFS_CHUNKS_PER_BLOCK)
\r
1911 bi->blockState = YAFFS_BLOCK_STATE_FULL;
\r
1912 dev->allocationBlock = -1;
\r
1919 T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
\r
1924 // To determine if we have enough space we just look at the
\r
1925 // number of erased blocks.
\r
1926 // The cache is allowed to use reserved blocks.
\r
1928 int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
\r
1930 return (dev->nErasedBlocks >= YAFFS_RESERVED_BLOCKS);
\r
1934 int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
\r
1941 yaffs_Spare spare;
\r
1943 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
\r
1945 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
\r
1947 yaffs_Object *object;
\r
1949 //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));
\r
1951 for(mask = 1,oldChunk = block * YAFFS_CHUNKS_PER_BLOCK;
\r
1952 mask && bi->pageBits;
\r
1953 mask <<= 1, oldChunk++ )
\r
1955 if(bi->pageBits & mask)
\r
1958 // This page is in use and needs to be copied off
\r
1962 //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
\r
1964 yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare,1);
\r
1966 yaffs_GetTagsFromSpare(dev,&spare,&tags);
\r
1968 object = yaffs_FindObjectByNumber(dev,tags.objectId);
\r
1970 if(object && object->deleted && tags.chunkId != 0)
\r
1972 // Data chunk in a deleted file, throw it away
\r
1973 // It's a deleted data chunk,
\r
1974 // No need to copy this, just forget about it and fix up the
\r
1977 yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0);
\r
1978 object->nDataChunks--;
\r
1980 else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
\r
1982 // Deleted object header with no data chunks.
\r
1983 // Can be discarded
\r
1984 object->chunkId = 0;
\r
1985 //Todo some clean up
\r
1990 // It's either a data chunk in a live file or
\r
1991 // an ObjectHeader, so we're interested in it.
\r
1992 // NB Need to keep the ObjectHeaders of deleted files
\r
1993 // until the whole file has been deleted off
\r
1994 tags.serialNumber++;
\r
1995 yaffs_LoadTagsIntoSpare(&spare,&tags);
\r
1998 newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1);
\r
2002 return YAFFS_FAIL;
\r
2005 // Ok, now fix up the Tnodes etc.
\r
2007 if(tags.chunkId == 0)
\r
2010 object->chunkId = newChunk;
\r
2014 // It's a data chunk
\r
2015 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
\r
2019 yaffs_DeleteChunk(dev,oldChunk);
\r
2028 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
\r
2030 // todo find a file to delete
\r
2031 struct list_head *i;
\r
2035 // To the free chunks add the chunks that are in the deleted unlinked files.
\r
2036 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
\r
2038 l = list_entry(i, yaffs_Object,siblings);
\r
2047 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
\r
2049 // This does background deletion on unlinked files.. only deleted ones.
\r
2050 // If we don't have a file we're working on then find one
\r
2051 if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
\r
2053 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
\r
2056 // OK, we're working on a file...
\r
2057 if(dev->unlinkedDeletion)
\r
2059 yaffs_Object *obj = dev->unlinkedDeletion;
\r
2061 int limit; // Number of chunks to delete in a file.
\r
2062 // NB this can be exceeded, but not by much.
\r
2066 if(dev->nErasedBlocks <= (YAFFS_RESERVED_BLOCKS + YAFFS_GARBAGE_COLLECT_LOW_WATER))
\r
2068 limit = 50; // Doing GC soon, so dig deeper
\r
2076 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
\r
2078 if(obj->nDataChunks == 0)
\r
2080 // Done all the deleting of data chunks.
\r
2081 // Now dump the header and clean up
\r
2082 yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
\r
2083 yaffs_DoGenericObjectDeletion(obj);
\r
2084 dev->nDeletedFiles--;
\r
2085 dev->nUnlinkedFiles--;
\r
2086 dev->nBackgroundDeletions++;
\r
2087 dev->unlinkedDeletion = NULL;
\r
2094 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
\r
2098 yaffs_DoUnlinkedFileDeletion(dev);
\r
2100 if(dev->nErasedBlocks <= (YAFFS_RESERVED_BLOCKS + YAFFS_GARBAGE_COLLECT_LOW_WATER))
\r
2102 dev->garbageCollectionRequired = 1;
\r
2105 if(dev->garbageCollectionRequired)
\r
2107 dev->garbageCollections++;
\r
2108 dev->garbageCollectionRequired = 0;
\r
2109 if(dev->blockSelectedForGC >= 0)
\r
2111 block = dev->blockSelectedForGC;
\r
2115 block = yaffs_FindDirtiestBlock(dev);
\r
2120 return yaffs_GarbageCollectBlock(dev,block);
\r
2124 return YAFFS_FAIL;
\r
2132 //////////////////////////// TAGS ///////////////////////////////////////
\r
2134 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
\r
2136 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
\r
2138 yaffs_CalcTagsECC(tagsPtr);
\r
2140 sparePtr->tagByte0 = tu->asBytes[0];
\r
2141 sparePtr->tagByte1 = tu->asBytes[1];
\r
2142 sparePtr->tagByte2 = tu->asBytes[2];
\r
2143 sparePtr->tagByte3 = tu->asBytes[3];
\r
2144 sparePtr->tagByte4 = tu->asBytes[4];
\r
2145 sparePtr->tagByte5 = tu->asBytes[5];
\r
2146 sparePtr->tagByte6 = tu->asBytes[6];
\r
2147 sparePtr->tagByte7 = tu->asBytes[7];
\r
2150 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
\r
2152 yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
\r
2155 tu->asBytes[0]= sparePtr->tagByte0;
\r
2156 tu->asBytes[1]= sparePtr->tagByte1;
\r
2157 tu->asBytes[2]= sparePtr->tagByte2;
\r
2158 tu->asBytes[3]= sparePtr->tagByte3;
\r
2159 tu->asBytes[4]= sparePtr->tagByte4;
\r
2160 tu->asBytes[5]= sparePtr->tagByte5;
\r
2161 tu->asBytes[6]= sparePtr->tagByte6;
\r
2162 tu->asBytes[7]= sparePtr->tagByte7;
\r
2164 result = yaffs_CheckECCOnTags(tagsPtr);
\r
2167 dev->tagsEccFixed++;
\r
2169 else if(result <0)
\r
2171 dev->tagsEccUnfixed++;
\r
2175 static void yaffs_SpareInitialise(yaffs_Spare *spare)
\r
2177 memset(spare,0xFF,sizeof(yaffs_Spare));
\r
2180 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted)
\r
2184 yaffs_Spare spare;
\r
2185 if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare,1) == YAFFS_OK)
\r
2187 *chunkDeleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
\r
2188 yaffs_GetTagsFromSpare(dev,&spare,tags);
\r
2193 return YAFFS_FAIL;
\r
2201 static int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_Tags *tags)
\r
2203 // NB There must be tags, data is optional
\r
2204 // If there is data, then an ECC is calculated on it.
\r
2206 yaffs_Spare spare;
\r
2210 return YAFFS_FAIL;
\r
2213 yaffs_SpareInitialise(&spare);
\r
2218 yaffs_CalcECC(buffer,&spare);
\r
2221 yaffs_LoadTagsIntoSpare(&spare,tags);
\r
2223 return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare);
\r
2229 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_Tags *tags, int useReserve)
\r
2231 // NB There must be tags, data is optional
\r
2232 // If there is data, then an ECC is calculated on it.
\r
2234 yaffs_Spare spare;
\r
2238 return YAFFS_FAIL;
\r
2241 yaffs_SpareInitialise(&spare);
\r
2246 yaffs_CalcECC(buffer,&spare);
\r
2249 yaffs_LoadTagsIntoSpare(&spare,tags);
\r
2251 return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
\r
2255 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted)
\r
2257 return ( tags->chunkId == chunkInObject &&
\r
2258 tags->objectId == objectId &&
\r
2259 !chunkDeleted) ? 1 : 0;
\r
2265 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
\r
2267 //Get the Tnode, then get the level 0 offset chunk offset
\r
2269 int theChunk = -1;
\r
2270 yaffs_Tags localTags;
\r
2275 yaffs_Device *dev = in->myDev;
\r
2280 // Passed a NULL, so use our own tags space
\r
2281 tags = &localTags;
\r
2284 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
\r
2288 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
\r
2290 // Now we need to do the shifting etc and search for it
\r
2291 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
\r
2293 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
\r
2294 if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
\r
2305 return found ? theChunk : -1;
\r
2308 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
\r
2310 //Get the Tnode, then get the level 0 offset chunk offset
\r
2312 int theChunk = -1;
\r
2313 yaffs_Tags localTags;
\r
2316 yaffs_Device *dev = in->myDev;
\r
2321 // Passed a NULL, so use our own tags space
\r
2322 tags = &localTags;
\r
2325 tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
\r
2330 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
\r
2332 // Now we need to do the shifting etc and search for it
\r
2333 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
\r
2335 yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
\r
2336 if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
\r
2347 // Delete the entry in the filestructure
\r
2350 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
\r
2355 //T(("No level 0 found for %d\n", chunkInInode));
\r
2360 //T(("Could not find %d to delete\n",chunkInInode));
\r
2362 return found ? theChunk : -1;
\r
2366 #ifdef YAFFS_PARANOID
\r
2368 static int yaffs_CheckFileSanity(yaffs_Object *in)
\r
2376 yaffs_Tags localTags;
\r
2377 yaffs_Tags *tags = &localTags;
\r
2382 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
\r
2384 //T(("Object not a file\n"));
\r
2385 return YAFFS_FAIL;
\r
2388 objId = in->objectId;
\r
2389 fSize = in->variant.fileVariant.fileSize;
\r
2390 nChunks = (fSize + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
\r
2392 for(chunk = 1; chunk <= nChunks; chunk++)
\r
2394 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
\r
2399 theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
\r
2402 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
\r
2403 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
\r
2410 //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n",
\r
2411 // objId,chunk,theChunk,tags->chunkId,tags->objectId);
\r
2420 //T(("No level 0 found for %d\n", chunk));
\r
2424 return failed ? YAFFS_FAIL : YAFFS_OK;
\r
2429 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
\r
2432 yaffs_Device *dev = in->myDev;
\r
2433 int existingChunk;
\r
2434 yaffs_Tags existingTags;
\r
2435 yaffs_Tags newTags;
\r
2436 unsigned existingSerial, newSerial;
\r
2438 int newChunkDeleted;
\r
2441 tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
\r
2444 return YAFFS_FAIL;
\r
2447 existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];
\r
2451 // If we're scanning then we need to test for duplicates
\r
2452 // NB This does not need to be efficient since it should only ever
\r
2453 // happen when the power fails during a write, then only one
\r
2454 // chunk should ever be affected.
\r
2457 if(existingChunk != 0)
\r
2459 // NB Right now existing chunk will not be real chunkId if the device >= 32MB
\r
2460 // thus we have to do a FindChunkInFile to get the real chunk id.
\r
2462 // We have a duplicate now we need to decide which one to use
\r
2463 // To do this we get both sets of tags and compare serial numbers.
\r
2464 yaffs_ReadChunkTagsFromNAND(dev,chunkInNAND, &newTags,&newChunkDeleted);
\r
2467 // Do a proper find
\r
2468 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
\r
2470 if(existingChunk <=0)
\r
2472 //Hoosterman - how did this happen?
\r
2474 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: existing chunk < 0 in scan" TENDSTR)));
\r
2479 // NB The deleted flags should be false, otherwise the chunks will
\r
2480 // not be loaded during a scan
\r
2482 newSerial = newTags.serialNumber;
\r
2483 existingSerial = existingTags.serialNumber;
\r
2485 if( existingChunk <= 0 ||
\r
2486 ((existingSerial+1) & 3) == newSerial)
\r
2489 // Delete the old one and drop through to update the tnode
\r
2490 yaffs_DeleteChunk(dev,existingChunk);
\r
2495 // Delete the new one and return early so that the tnode isn't changed
\r
2496 yaffs_DeleteChunk(dev,chunkInNAND);
\r
2503 if(existingChunk == 0)
\r
2505 in->nDataChunks++;
\r
2508 tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
\r
2515 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
\r
2517 int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
\r
2519 if(chunkInNAND >= 0)
\r
2521 return yaffs_ReadChunkFromNAND(in->myDev,chunkInNAND,buffer,NULL,1);
\r
2531 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId)
\r
2535 yaffs_Spare spare;
\r
2536 yaffs_BlockInfo *bi;
\r
2538 if(chunkId <= 0) return;
\r
2540 block = chunkId / YAFFS_CHUNKS_PER_BLOCK;
\r
2541 page = chunkId % YAFFS_CHUNKS_PER_BLOCK;
\r
2542 yaffs_SpareInitialise(&spare);
\r
2544 spare.pageStatus = 0; // To mark it as deleted.
\r
2547 yaffs_WriteChunkToNAND(dev,chunkId,NULL,&spare);
\r
2548 yaffs_HandleUpdateChunk(dev,chunkId,&spare);
\r
2549 bi = yaffs_GetBlockInfo(dev,block);
\r
2552 // Pull out of the management area.
\r
2553 // If the whole block became dirty, this will kick off an erasure.
\r
2554 if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
\r
2555 bi->blockState == YAFFS_BLOCK_STATE_FULL)
\r
2557 dev->nFreeChunks++;
\r
2559 bi->pageBits &= ~(1 << page);
\r
2562 if(bi->pagesInUse == 0 &&
\r
2563 bi->blockState == YAFFS_BLOCK_STATE_FULL)
\r
2565 yaffs_BlockBecameDirty(dev,block);
\r
2571 // T(("Bad news deleting chunk %d\n",chunkId));
\r
2579 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
\r
2581 // Find old chunk Need to do this to get serial number
\r
2582 // Write new one and patch into tree.
\r
2583 // Invalidate old tags.
\r
2586 yaffs_Tags prevTags;
\r
2589 yaffs_Tags newTags;
\r
2591 yaffs_Device *dev = in->myDev;
\r
2593 yaffs_CheckGarbageCollection(dev);
\r
2595 // Get the previous chunk at this location in the file if it exists
\r
2596 prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
\r
2598 // Set up new tags
\r
2599 newTags.chunkId = chunkInInode;
\r
2600 newTags.objectId = in->objectId;
\r
2601 newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
\r
2602 newTags.byteCount = nBytes;
\r
2603 newTags.unusedStuff = 0xFFFFFFFF;
\r
2605 yaffs_CalcTagsECC(&newTags);
\r
2607 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
\r
2608 if(newChunkId >= 0)
\r
2610 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
\r
2613 if(prevChunkId >= 0)
\r
2615 yaffs_DeleteChunk(dev,prevChunkId);
\r
2619 yaffs_CheckFileSanity(in);
\r
2621 return newChunkId;
\r
2630 // UpdateObjectHeader updates the header on NAND for an object.
\r
2631 // If name is not NULL, then that new name is used.
\r
2633 int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force)
\r
2636 yaffs_Device *dev = in->myDev;
\r
2641 yaffs_Tags newTags;
\r
2642 __u8 bufferNew[YAFFS_BYTES_PER_CHUNK];
\r
2643 __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
\r
2645 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bufferNew;
\r
2646 yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
\r
2649 if(!in->fake || force)
\r
2652 yaffs_CheckGarbageCollection(dev);
\r
2654 memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK);
\r
2656 prevChunkId = in->chunkId;
\r
2658 if(prevChunkId >= 0)
\r
2660 yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL,1);
\r
2664 oh->type = in->variantType;
\r
2666 oh->st_mode = in->st_mode;
\r
2668 #ifdef CONFIG_YAFFS_WINCE
\r
2669 oh->win_atime[0] = in->win_atime[0];
\r
2670 oh->win_ctime[0] = in->win_ctime[0];
\r
2671 oh->win_mtime[0] = in->win_mtime[0];
\r
2672 oh->win_atime[1] = in->win_atime[1];
\r
2673 oh->win_ctime[1] = in->win_ctime[1];
\r
2674 oh->win_mtime[1] = in->win_mtime[1];
\r
2676 oh->st_uid = in->st_uid;
\r
2677 oh->st_gid = in->st_gid;
\r
2678 oh->st_atime = in->st_atime;
\r
2679 oh->st_mtime = in->st_mtime;
\r
2680 oh->st_ctime = in->st_ctime;
\r
2681 oh->st_rdev = in->st_rdev;
\r
2685 oh->parentObjectId = in->parent->objectId;
\r
2689 oh->parentObjectId = 0;
\r
2692 //oh->sum = in->sum;
\r
2695 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
\r
2696 strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
\r
2698 else if(prevChunkId)
\r
2700 memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1);
\r
2704 memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
\r
2707 switch(in->variantType)
\r
2709 case YAFFS_OBJECT_TYPE_UNKNOWN:
\r
2710 // Should not happen
\r
2712 case YAFFS_OBJECT_TYPE_FILE:
\r
2713 oh->fileSize = in->variant.fileVariant.fileSize;
\r
2715 case YAFFS_OBJECT_TYPE_HARDLINK:
\r
2716 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
\r
2718 case YAFFS_OBJECT_TYPE_SPECIAL:
\r
2721 case YAFFS_OBJECT_TYPE_DIRECTORY:
\r
2724 case YAFFS_OBJECT_TYPE_SYMLINK:
\r
2725 strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
\r
2726 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
\r
2732 newTags.chunkId = 0;
\r
2733 newTags.objectId = in->objectId;
\r
2734 newTags.serialNumber = in->serial;
\r
2735 newTags.byteCount = 0xFFFFFFFF;
\r
2736 newTags.unusedStuff = 0xFFFFFFFF;
\r
2738 yaffs_CalcTagsECC(&newTags);
\r
2741 // Create new chunk in NAND
\r
2742 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags,1);
\r
2744 if(newChunkId >= 0)
\r
2747 in->chunkId = newChunkId;
\r
2749 if(prevChunkId >= 0)
\r
2751 yaffs_DeleteChunk(dev,prevChunkId);
\r
2757 return newChunkId;
\r
2764 /////////////////////// Short Operations Cache ////////////////////////////////
\r
2765 // In many siturations where there is no high level buffering (eg WinCE) a lot of
\r
2766 // reads might be short sequential reads, and a lot of writes may be short
\r
2767 // sequential writes. eg. scanning/writing a jpeg file.
\r
2768 // In these cases, a short read/write cache can provide a huge perfomance benefit
\r
2769 // with dumb-as-a-rock code.
\r
2770 // There are a limited number (~10) of cache chunks per device so that we don't
\r
2771 // need a very intelligent search.
\r
2774 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
\r
2777 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
\r
2779 yaffs_Device *dev = obj->myDev;
\r
2782 yaffs_ChunkCache *cache;
\r
2789 // Find the dirty cache for this object with the lowest chunk id.
\r
2790 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2792 if(dev->srCache[i].object == obj &&
\r
2793 dev->srCache[i].dirty)
\r
2795 if(!cache || dev->srCache[i].chunkId < lowest)
\r
2797 cache = &dev->srCache[i];
\r
2798 lowest = cache->chunkId;
\r
2807 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
\r
2809 if(nBytes > YAFFS_BYTES_PER_CHUNK)
\r
2811 nBytes= YAFFS_BYTES_PER_CHUNK;
\r
2814 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
\r
2822 } while(cache && chunkWritten > 0);
\r
2826 //Hoosterman, disk full while writing cache out.
\r
2827 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: no space during caceh write" TENDSTR)));
\r
2834 // Grab us a chunk for use.
\r
2835 // First look for an empty one.
\r
2836 // Then look for the least recently used non-dirty one.
\r
2837 // Then look for the least recently used dirty one...., flush and look again.
\r
2838 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
\r
2844 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2846 if(!dev->srCache[i].object)
\r
2848 //T(("Grabbing empty %d\n",i));
\r
2850 return &dev->srCache[i];
\r
2855 usage = 0; // just to stop the compiler grizzling
\r
2857 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2859 if(!dev->srCache[i].dirty &&
\r
2860 ((dev->srCache[i].lastUse < usage && theOne >= 0)||
\r
2863 usage = dev->srCache[i].lastUse;
\r
2868 //T(("Grabbing non-empty %d\n",theOne));
\r
2869 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
\r
2874 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
\r
2876 yaffs_ChunkCache *cache;
\r
2877 yaffs_Object *theObj;
\r
2881 // Try find a non-dirty one...
\r
2883 cache = yaffs_GrabChunkCacheWorker(dev);
\r
2887 // They were all dirty, find the last recently used object and flush
\r
2888 // its cache, then find again.
\r
2889 // NB what's here is not very accurate, we actually flush the object
\r
2890 // the last recently used page.
\r
2892 theObj = dev->srCache[0].object;
\r
2893 usage = dev->srCache[0].lastUse;
\r
2895 for(i = 1; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2897 if( dev->srCache[i].object &&
\r
2898 dev->srCache[i].lastUse < usage)
\r
2900 usage = dev->srCache[i].lastUse;
\r
2901 theObj = dev->srCache[i].object;
\r
2905 yaffs_FlushFilesChunkCache(theObj);
\r
2908 cache = yaffs_GrabChunkCacheWorker(dev);
\r
2916 // Find a cached chunk
\r
2917 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
\r
2919 yaffs_Device *dev = obj->myDev;
\r
2922 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2924 if(dev->srCache[i].object == obj &&
\r
2925 dev->srCache[i].chunkId == chunkId)
\r
2929 return &dev->srCache[i];
\r
2936 // Mark the chunk for the least recently used algorithym
\r
2937 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
\r
2939 if( dev->srLastUse < 0 ||
\r
2940 dev->srLastUse > 100000000)
\r
2942 // Reset the cache usages
\r
2944 for(i = 1; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2946 dev->srCache[i].lastUse = 0;
\r
2948 dev->srLastUse = 0;
\r
2953 cache->lastUse = dev->srLastUse;
\r
2961 // Invalidate a single cache page.
\r
2962 // Do this when a whole page gets written,
\r
2963 // ie the short cache for this page is no longer valid.
\r
2964 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
\r
2966 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
\r
2970 cache->object = NULL;
\r
2975 // Invalidate all the cache pages associated with this object
\r
2976 // Do this whenever ther file is deleted or resized.
\r
2977 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
\r
2980 yaffs_Device *dev = in->myDev;
\r
2982 // Now invalidate it.
\r
2983 for(i = 0; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
2985 if(dev->srCache[i].object == in)
\r
2987 dev->srCache[i].object = NULL;
\r
2995 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
\r
3001 static yaffs_ChunkCache *yaffs_FindChunkCache(yaffs_Device *dev, int objectId, int chunkId)
\r
3006 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache)
\r
3010 static void yaffs_InvalidateChunkCache(yaffs_Object *obj, int chunkId)
\r
3014 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
\r
3024 ///////////////////////// File read/write ///////////////////////////////
\r
3025 // Read and write have very similar structures.
\r
3026 // In general the read/write has three parts to it
\r
3027 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
\r
3028 // * Some complete chunks
\r
3029 // * An incomplete chunk to end off with
\r
3031 // Curve-balls: the first chunk might also be the last chunk.
\r
3033 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
\r
3045 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; // The first chunk is 1
\r
3046 start = offset % YAFFS_BYTES_PER_CHUNK;
\r
3048 // OK now check for the curveball where the start and end are in
\r
3049 // the same chunk.
\r
3050 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
\r
3056 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
\r
3059 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
\r
3061 // An incomplete start or end chunk (or maybe both start and end chunk)
\r
3062 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
\r
3063 yaffs_ChunkCache *cache;
\r
3064 // If we can't find the data in the cache, then load it up.
\r
3065 cache = yaffs_FindChunkCache(in,chunk);
\r
3068 cache = yaffs_GrabChunkCache(in->myDev);
\r
3069 cache->object = in;
\r
3070 cache->chunkId = chunk;
\r
3072 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
\r
3075 yaffs_UseChunkCache(in->myDev,cache,0);
\r
3077 memcpy(buffer,&cache->data[start],nToCopy);
\r
3079 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
\r
3080 // Read into the local buffer then copy...
\r
3081 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
\r
3082 memcpy(buffer,&localBuffer[start],nToCopy);
\r
3087 #ifdef CONFIG_YAFFS_WINCE
\r
3088 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
\r
3090 // Under WinCE can't do direct transfer. Need to use a local buffer.
\r
3091 // This is because we otherwise screw up WinCE's memory mapper
\r
3092 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
\r
3093 memcpy(buffer,localBuffer,YAFFS_BYTES_PER_CHUNK);
\r
3095 // A full chunk. Read directly into the supplied buffer.
\r
3096 yaffs_ReadChunkDataFromObject(in,chunk,buffer);
\r
3101 offset += nToCopy;
\r
3102 buffer += nToCopy;
\r
3112 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
\r
3121 int startOfWrite = offset;
\r
3122 int chunkWritten = 0;
\r
3127 while(n > 0 && chunkWritten >= 0)
\r
3129 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1;
\r
3130 start = offset % YAFFS_BYTES_PER_CHUNK;
\r
3133 // OK now check for the curveball where the start and end are in
\r
3134 // the same chunk.
\r
3136 if( (start + n) < YAFFS_BYTES_PER_CHUNK)
\r
3140 // Now folks, to calculate how many bytes to write back....
\r
3141 // If we're overwriting and not writing to then end of file then
\r
3142 // we need to write back as much as was there before.
\r
3144 nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * YAFFS_BYTES_PER_CHUNK);
\r
3146 if(nBytesRead > YAFFS_BYTES_PER_CHUNK)
\r
3148 nBytesRead = YAFFS_BYTES_PER_CHUNK;
\r
3151 nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
\r
3156 nToCopy = YAFFS_BYTES_PER_CHUNK - start;
\r
3157 nToWriteBack = YAFFS_BYTES_PER_CHUNK;
\r
3160 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
\r
3162 // An incomplete start or end chunk (or maybe both start and end chunk)
\r
3163 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
\r
3164 yaffs_ChunkCache *cache;
\r
3165 // If we can't find the data in the cache, then load it up.
\r
3166 cache = yaffs_FindChunkCache(in,chunk);
\r
3167 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
\r
3169 cache = yaffs_GrabChunkCache(in->myDev);
\r
3170 cache->object = in;
\r
3171 cache->chunkId = chunk;
\r
3173 yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
\r
3178 yaffs_UseChunkCache(in->myDev,cache,1);
\r
3179 memcpy(&cache->data[start],buffer,nToCopy);
\r
3183 chunkWritten = -1; // fail the write
\r
3186 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
\r
3188 // An incomplete start or end chunk (or maybe both start and end chunk)
\r
3189 // Read into the local buffer then copy, then copy over and write back.
\r
3191 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
\r
3193 memcpy(&localBuffer[start],buffer,nToCopy);
\r
3195 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
\r
3197 //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
\r
3204 #ifdef CONFIG_YAFFS_WINCE
\r
3205 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
\r
3206 // Under WinCE can't do direct transfer. Need to use a local buffer.
\r
3207 // This is because we otherwise screw up WinCE's memory mapper
\r
3208 memcpy(localBuffer,buffer,YAFFS_BYTES_PER_CHUNK);
\r
3209 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,YAFFS_BYTES_PER_CHUNK,0);
\r
3211 // A full chunk. Write directly from the supplied buffer.
\r
3212 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0);
\r
3214 // Since we've overwritten the cached data, we better invalidate it.
\r
3215 yaffs_InvalidateChunkCache(in,chunk);
\r
3216 //T(("Write to chunk %d %d\n",chunk,chunkWritten));
\r
3219 if(chunkWritten >= 0)
\r
3222 offset += nToCopy;
\r
3223 buffer += nToCopy;
\r
3229 // Update file object
\r
3231 if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
\r
3233 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
\r
3242 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
\r
3246 int oldFileSize = in->variant.fileVariant.fileSize;
\r
3247 int sizeOfPartialChunk = newSize % YAFFS_BYTES_PER_CHUNK;
\r
3249 yaffs_Device *dev = in->myDev;
\r
3251 __u8 localBuffer[YAFFS_BYTES_PER_CHUNK];
\r
3253 yaffs_FlushFilesChunkCache(in);
\r
3254 yaffs_InvalidateWholeChunkCache(in);
\r
3256 if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
\r
3258 return yaffs_GetFileSize(in);
\r
3261 if(newSize < oldFileSize)
\r
3264 int lastDel = 1 + (oldFileSize-1)/YAFFS_BYTES_PER_CHUNK;
\r
3266 int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/
\r
3267 YAFFS_BYTES_PER_CHUNK;
\r
3269 // Delete backwards so that we don't end up with holes if
\r
3270 // power is lost part-way through the operation.
\r
3271 for(i = lastDel; i >= startDel; i--)
\r
3273 // NB this could be optimised somewhat,
\r
3274 // eg. could retrieve the tags and write them without
\r
3275 // using yaffs_DeleteChunk
\r
3277 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
\r
3278 if(chunkId <= 0 || chunkId >= (dev->endBlock * 32))
\r
3280 //T(("Found daft chunkId %d for %d\n",chunkId,i));
\r
3284 in->nDataChunks--;
\r
3285 yaffs_DeleteChunk(dev,chunkId);
\r
3290 if(sizeOfPartialChunk != 0)
\r
3292 int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK;
\r
3294 // Got to read and rewrite the last chunk with its new size.
\r
3295 yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
\r
3297 yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
\r
3301 in->variant.fileVariant.fileSize = newSize;
\r
3303 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
\r
3310 return oldFileSize;
\r
3315 loff_t yaffs_GetFileSize(yaffs_Object *obj)
\r
3317 obj = yaffs_GetEquivalentObject(obj);
\r
3319 switch(obj->variantType)
\r
3321 case YAFFS_OBJECT_TYPE_FILE:
\r
3322 return obj->variant.fileVariant.fileSize;
\r
3323 case YAFFS_OBJECT_TYPE_SYMLINK:
\r
3324 return strlen(obj->variant.symLinkVariant.alias);
\r
3332 // yaffs_FlushFile() updates the file's
\r
3333 // objectId in NAND
\r
3335 int yaffs_FlushFile(yaffs_Object *in)
\r
3340 //T(("flushing object header\n"));
\r
3342 yaffs_FlushFilesChunkCache(in);
\r
3344 #ifdef CONFIG_YAFFS_WINCE
\r
3345 yfsd_WinFileTimeNow(in->win_mtime);
\r
3347 in->st_mtime = CURRENT_TIME;
\r
3350 retVal = yaffs_UpdateObjectHeader(in,NULL,0);
\r
3354 retVal = YAFFS_OK;
\r
3362 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
\r
3365 // First off, invalidate the file's data in the cache, without flushing.
\r
3366 yaffs_InvalidateWholeChunkCache(in);
\r
3368 yaffs_RemoveObjectFromDirectory(in);
\r
3369 yaffs_DeleteChunk(in->myDev,in->chunkId);
\r
3373 in->myInode->u.generic_ip = NULL;
\r
3377 yaffs_FreeObject(in);
\r
3382 // yaffs_DeleteFile deletes the whole file data
\r
3383 // and the inode associated with the file.
\r
3384 // It does not delete the links associated with the file.
\r
3385 static int yaffs_UnlinkFile(yaffs_Object *in)
\r
3388 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
\r
3389 // Delete the file data & tnodes
\r
3391 yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
\r
3394 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
\r
3396 return yaffs_DoGenericObjectDeletion(in);
\r
3399 int immediateDeletion=0;
\r
3400 retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
\r
3401 if(retVal == YAFFS_OK)
\r
3403 //in->unlinked = 1;
\r
3404 //in->myDev->nUnlinkedFiles++;
\r
3405 //in->renameAllowed = 0;
\r
3409 immediateDeletion = 1;
\r
3413 #ifdef CONFIG_YAFFS_WINCE
\r
3414 if(in->inUse <= 0)
\r
3416 immediateDeletion = 1;
\r
3421 if(immediateDeletion)
\r
3423 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
\r
3425 in->myDev->nDeletedFiles++;
\r
3435 int yaffs_DeleteFile(yaffs_Object *in)
\r
3437 int retVal = YAFFS_OK;
\r
3440 retVal = yaffs_UnlinkFile(in);
\r
3442 if(retVal == YAFFS_OK &&
\r
3447 in->myDev->nDeletedFiles++;
\r
3449 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
\r
3452 static int yaffs_DeleteDirectory(yaffs_Object *in)
\r
3454 //First check that the directory is empty.
\r
3455 if(list_empty(&in->variant.directoryVariant.children))
\r
3457 return yaffs_DoGenericObjectDeletion(in);
\r
3460 return YAFFS_FAIL;
\r
3464 static int yaffs_DeleteSymLink(yaffs_Object *in)
\r
3466 YFREE(in->variant.symLinkVariant.alias);
\r
3468 return yaffs_DoGenericObjectDeletion(in);
\r
3471 static int yaffs_DeleteHardLink(yaffs_Object *in)
\r
3473 // remove this hardlink from the list assocaited with the equivalent
\r
3475 list_del(&in->hardLinks);
\r
3476 return yaffs_DoGenericObjectDeletion(in);
\r
3480 static int yaffs_UnlinkWorker(yaffs_Object *obj)
\r
3484 if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
\r
3486 return yaffs_DeleteHardLink(obj);
\r
3488 else if(!list_empty(&obj->hardLinks))
\r
3490 // Curve ball: We're unlinking an object that has a hardlink.
\r
3492 // This problem arises because we are not strictly following
\r
3493 // The Linux link/inode model.
\r
3495 // We can't really delete the object.
\r
3496 // Instead, we do the following:
\r
3497 // - Select a hardlink.
\r
3498 // - Unhook it from the hard links
\r
3499 // - Unhook it from its parent directory (so that the rename can work)
\r
3500 // - Rename the object to the hardlink's name.
\r
3501 // - Delete the hardlink
\r
3506 char name[YAFFS_MAX_NAME_LENGTH+1];
\r
3508 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
\r
3510 list_del_init(&hl->hardLinks);
\r
3511 list_del_init(&hl->siblings);
\r
3513 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
\r
3515 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
\r
3517 if(retVal == YAFFS_OK)
\r
3519 retVal = yaffs_DoGenericObjectDeletion(hl);
\r
3526 switch(obj->variantType)
\r
3528 case YAFFS_OBJECT_TYPE_FILE:
\r
3529 return yaffs_UnlinkFile(obj);
\r
3531 case YAFFS_OBJECT_TYPE_DIRECTORY:
\r
3532 return yaffs_DeleteDirectory(obj);
\r
3534 case YAFFS_OBJECT_TYPE_SYMLINK:
\r
3535 return yaffs_DeleteSymLink(obj);
\r
3537 case YAFFS_OBJECT_TYPE_HARDLINK:
\r
3538 case YAFFS_OBJECT_TYPE_UNKNOWN:
\r
3540 return YAFFS_FAIL;
\r
3545 int yaffs_Unlink(yaffs_Object *dir, const char *name)
\r
3547 yaffs_Object *obj;
\r
3549 obj = yaffs_FindObjectByName(dir,name);
\r
3551 if(obj && obj->unlinkAllowed)
\r
3553 return yaffs_UnlinkWorker(obj);
\r
3556 return YAFFS_FAIL;
\r
3560 //////////////// Initialisation Scanning /////////////////
\r
3564 // For now we use the SmartMedia check.
\r
3565 // We look at the blockStatus byte in the first two chunks
\r
3566 // These must be 0xFF to pass as OK.
\r
3567 // todo: this function needs to be modifyable foir different NAND types
\r
3568 // and different chunk sizes. Suggest make this into a per-device configurable
\r
3570 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
\r
3572 yaffs_Spare spare;
\r
3574 yaffs_ReadChunkFromNAND(dev,blk * YAFFS_CHUNKS_PER_BLOCK,NULL,&spare,1);
\r
3576 if(yaffs_CountBits(spare.blockStatus) < 7)
\r
3581 if(spare.blockStatus != 0xFF)
\r
3586 yaffs_ReadChunkFromNAND(dev,blk * YAFFS_CHUNKS_PER_BLOCK + 1,NULL,&spare,1);
\r
3589 if(yaffs_CountBits(spare.blockStatus) < 7)
\r
3594 if(spare.blockStatus != 0xFF)
\r
3604 static int yaffs_Scan(yaffs_Device *dev)
\r
3606 yaffs_Spare spare;
\r
3612 yaffs_BlockState state;
\r
3613 yaffs_Object *hardList = NULL;
\r
3615 yaffs_BlockInfo *bi;
\r
3618 yaffs_ObjectHeader *oh;
\r
3620 yaffs_Object *parent;
\r
3622 __u8 chunkData[YAFFS_BYTES_PER_CHUNK];
\r
3625 // Scan all the blocks...
\r
3627 for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
\r
3630 bi = yaffs_GetBlockInfo(dev,blk);
\r
3632 bi->pagesInUse = 0;
\r
3633 state = YAFFS_BLOCK_STATE_SCANNING;
\r
3636 if(yaffs_IsBlockBad(dev,blk))
\r
3638 state = YAFFS_BLOCK_STATE_DEAD;
\r
3639 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
\r
3642 // Read each chunk in the block.
\r
3644 for(c = 0; c < YAFFS_CHUNKS_PER_BLOCK &&
\r
3645 state == YAFFS_BLOCK_STATE_SCANNING; c++)
\r
3647 // Read the spare area and decide what to do
\r
3648 chunk = blk * YAFFS_CHUNKS_PER_BLOCK + c;
\r
3650 yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare,1);
\r
3653 // This block looks ok, now what's in this chunk?
\r
3654 yaffs_GetTagsFromSpare(dev,&spare,&tags);
\r
3656 if(yaffs_CountBits(spare.pageStatus) < 6)
\r
3658 // A deleted chunk
\r
3660 dev->nFreeChunks ++;
\r
3661 //T((" %d %d deleted\n",blk,c));
\r
3663 else if(tags.objectId == YAFFS_UNUSED_OBJECT_ID)
\r
3665 // An unassigned chunk in the block
\r
3666 // This means that either the block is empty or
\r
3667 // this is the one being allocated from
\r
3671 // the block is unused
\r
3672 state = YAFFS_BLOCK_STATE_EMPTY;
\r
3673 dev->nErasedBlocks++;
\r
3677 // this is the block being allocated from
\r
3678 T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
\r
3679 state = YAFFS_BLOCK_STATE_ALLOCATING;
\r
3680 dev->allocationBlock = blk;
\r
3681 dev->allocationPage = c;
\r
3684 dev->nFreeChunks += (YAFFS_CHUNKS_PER_BLOCK - c);
\r
3686 else if(tags.chunkId > 0)
\r
3690 bi->pageBits |= (1 << c);
\r
3693 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
\r
3694 // PutChunkIntoFIle checks for a clash (two data chunks with
\r
3695 // the same chunkId).
\r
3696 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
\r
3697 endpos = (tags.chunkId - 1)* YAFFS_BYTES_PER_CHUNK + tags.byteCount;
\r
3698 if(in->variant.fileVariant.scannedFileSize <endpos)
\r
3700 in->variant.fileVariant.scannedFileSize = endpos;
\r
3701 #ifndef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
\r
3702 in->variant.fileVariant.fileSize =
\r
3703 in->variant.fileVariant.scannedFileSize;
\r
3707 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));
\r
3711 // chunkId == 0, so it is an ObjectHeader.
\r
3712 // Thus, we read in the object header and make the object
\r
3713 bi->pageBits |= (1 << c);
\r
3716 yaffs_ReadChunkFromNAND(dev,chunk,chunkData,NULL,1);
\r
3718 oh = (yaffs_ObjectHeader *)chunkData;
\r
3720 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
\r
3724 // We have already filled this one. We have a duplicate and need to resolve it.
\r
3726 unsigned existingSerial = in->serial;
\r
3727 unsigned newSerial = tags.serialNumber;
\r
3729 if(((existingSerial+1) & 3) == newSerial)
\r
3731 // Use new one - destroy the exisiting one
\r
3732 yaffs_DeleteChunk(dev,in->chunkId);
\r
3737 // Use existing - destroy this one.
\r
3738 yaffs_DeleteChunk(dev,chunk);
\r
3743 (tags.objectId == YAFFS_OBJECTID_ROOT ||
\r
3744 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
\r
3746 // We only load some info, don't fiddle with directory structure
\r
3748 in->variantType = oh->type;
\r
3750 in->st_mode = oh->st_mode;
\r
3751 #ifdef CONFIG_YAFFS_WINCE
\r
3752 in->win_atime[0] = oh->win_atime[0];
\r
3753 in->win_ctime[0] = oh->win_ctime[0];
\r
3754 in->win_mtime[0] = oh->win_mtime[0];
\r
3755 in->win_atime[1] = oh->win_atime[1];
\r
3756 in->win_ctime[1] = oh->win_ctime[1];
\r
3757 in->win_mtime[1] = oh->win_mtime[1];
\r
3759 in->st_uid = oh->st_uid;
\r
3760 in->st_gid = oh->st_gid;
\r
3761 in->st_atime = oh->st_atime;
\r
3762 in->st_mtime = oh->st_mtime;
\r
3763 in->st_ctime = oh->st_ctime;
\r
3764 in->st_rdev = oh->st_rdev;
\r
3766 in->chunkId = chunk;
\r
3769 else if(!in->valid)
\r
3771 // we need to load this info
\r
3774 in->variantType = oh->type;
\r
3776 in->st_mode = oh->st_mode;
\r
3777 #ifdef CONFIG_YAFFS_WINCE
\r
3778 in->win_atime[0] = oh->win_atime[0];
\r
3779 in->win_ctime[0] = oh->win_ctime[0];
\r
3780 in->win_mtime[0] = oh->win_mtime[0];
\r
3781 in->win_atime[1] = oh->win_atime[1];
\r
3782 in->win_ctime[1] = oh->win_ctime[1];
\r
3783 in->win_mtime[1] = oh->win_mtime[1];
\r
3785 in->st_uid = oh->st_uid;
\r
3786 in->st_gid = oh->st_gid;
\r
3787 in->st_atime = oh->st_atime;
\r
3788 in->st_mtime = oh->st_mtime;
\r
3789 in->st_ctime = oh->st_ctime;
\r
3790 in->st_rdev = oh->st_rdev;
\r
3792 in->chunkId = chunk;
\r
3794 yaffs_SetObjectName(in,oh->name);
\r
3797 // directory stuff...
\r
3798 // hook up to parent
\r
3800 parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
\r
3801 if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
\r
3803 // Set up as a directory
\r
3804 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
\r
3805 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
\r
3807 else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
\r
3809 // Hoosterman, another problem....
\r
3810 // We're trying to use a non-directory as a directory
\r
3811 // Todo ... handle
\r
3812 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tradgedy: attempting to use non-directory as a directory in scan" TENDSTR)));
\r
3816 yaffs_AddObjectToDirectory(parent,in);
\r
3817 if(parent == dev->unlinkedDir)
\r
3819 in->deleted = 1; // If it is unlinked at start up then it wants deleting
\r
3820 dev->nDeletedFiles++;
\r
3823 // Note re hardlinks.
\r
3824 // Since we might scan a hardlink before its equivalent object is scanned
\r
3825 // we put them all in a list.
\r
3826 // After scanning is complete, we should have all the objects, so we run through this
\r
3827 // list and fix up all the chains.
\r
3829 switch(in->variantType)
\r
3831 case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem
\r
3833 case YAFFS_OBJECT_TYPE_FILE:
\r
3834 #ifdef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
\r
3835 in->variant.fileVariant.fileSize = oh->fileSize;
\r
3838 case YAFFS_OBJECT_TYPE_HARDLINK:
\r
3839 in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
\r
3840 (yaffs_Object *)(in->hardLinks.next) = hardList;
\r
3843 case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing
\r
3845 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
\r
3847 case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing
\r
3848 in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
\r
3851 //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));
\r
3856 if(state == YAFFS_BLOCK_STATE_SCANNING)
\r
3858 // If we got this far while scanning, then the block is fully allocated.
\r
3859 state = YAFFS_BLOCK_STATE_FULL;
\r
3862 bi->blockState = state;
\r
3864 // Now let's see if it was dirty
\r
3865 if( bi->pagesInUse == 0 &&
\r
3866 bi->blockState == YAFFS_BLOCK_STATE_FULL)
\r
3868 yaffs_BlockBecameDirty(dev,blk);
\r
3873 // Fix up the hard link chains.
\r
3874 // We should now have scanned all the objects, now it's time to add these
\r
3879 hardList = (yaffs_Object *)(hardList->hardLinks.next);
\r
3881 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
\r
3885 // Add the hardlink pointers
\r
3886 hl->variant.hardLinkVariant.equivalentObject=in;
\r
3887 list_add(&hl->hardLinks,&in->hardLinks);
\r
3891 //Todo Need to report/handle this better.
\r
3892 // Got a problem... hardlink to a non-existant object
\r
3893 hl->variant.hardLinkVariant.equivalentObject=NULL;
\r
3894 INIT_LIST_HEAD(&hl->hardLinks);
\r
3906 ////////////////////////// Directory Functions /////////////////////////
\r
3909 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
\r
3912 if(obj->siblings.prev == NULL)
\r
3914 // Not initialised
\r
3915 INIT_LIST_HEAD(&obj->siblings);
\r
3918 else if(!list_empty(&obj->siblings))
\r
3920 // If it is holed up somewhere else, un hook it
\r
3921 list_del_init(&obj->siblings);
\r
3924 list_add(&obj->siblings,&directory->variant.directoryVariant.children);
\r
3925 obj->parent = directory;
\r
3927 if(directory == obj->myDev->unlinkedDir)
\r
3929 obj->unlinked = 1;
\r
3930 obj->myDev->nUnlinkedFiles++;
\r
3931 obj->renameAllowed = 0;
\r
3935 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
\r
3937 list_del_init(&obj->siblings);
\r
3938 obj->parent = NULL;
\r
3941 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const char *name)
\r
3945 struct list_head *i;
\r
3946 char buffer[YAFFS_MAX_NAME_LENGTH+1];
\r
3950 sum = yaffs_CalcNameSum(name);
\r
3952 list_for_each(i,&directory->variant.directoryVariant.children)
\r
3954 l = list_entry(i, yaffs_Object,siblings);
\r
3956 // Special case for lost-n-found
\r
3957 if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
\r
3959 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
\r
3964 else if(yaffs_SumCompare(l->sum, sum))
\r
3966 // Do a real check
\r
3967 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
\r
3968 if(yaffs_strcmp(name,buffer) == 0)
\r
3981 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
\r
3983 struct list_head *i;
\r
3987 list_for_each(i,&theDir->variant.directoryVariant.children)
\r
3989 l = list_entry(i, yaffs_Object,siblings);
\r
3992 return YAFFS_FAIL;
\r
4001 // GetEquivalentObject dereferences any hard links to get to the
\r
4004 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
\r
4006 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
\r
4008 // We want the object id of the equivalent object, not this one
\r
4009 obj = obj->variant.hardLinkVariant.equivalentObject;
\r
4015 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize)
\r
4017 memset(name,0,buffSize);
\r
4019 if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
\r
4021 strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
\r
4023 else if(obj->chunkId <= 0)
\r
4027 sprintf(locName,"%s%d",YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
\r
4028 strncpy(name,locName,buffSize - 1);
\r
4031 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
\r
4032 else if(obj->shortName[0])
\r
4034 strcpy(name,obj->shortName);
\r
4039 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
\r
4040 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
\r
4042 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
\r
4044 if(obj->chunkId >= 0)
\r
4046 yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL,1);
\r
4048 strncpy(name,oh->name,buffSize - 1);
\r
4051 return strlen(name);
\r
4054 int yaffs_GetObjectFileLength(yaffs_Object *obj)
\r
4057 // Dereference any hard linking
\r
4058 obj = yaffs_GetEquivalentObject(obj);
\r
4060 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
\r
4062 return obj->variant.fileVariant.fileSize;
\r
4064 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
\r
4066 return strlen(obj->variant.symLinkVariant.alias);
\r
4070 // Only a directory should drop through to here
\r
4071 return YAFFS_BYTES_PER_CHUNK;
\r
4075 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
\r
4078 struct list_head *i;
\r
4080 if(!obj->unlinked)
\r
4082 count++; // the object itself
\r
4084 list_for_each(i,&obj->hardLinks)
\r
4086 count++; // add the hard links;
\r
4093 int yaffs_GetObjectInode(yaffs_Object *obj)
\r
4095 obj = yaffs_GetEquivalentObject(obj);
\r
4097 return obj->objectId;
\r
4100 unsigned yaffs_GetObjectType(yaffs_Object *obj)
\r
4102 obj = yaffs_GetEquivalentObject(obj);
\r
4104 switch(obj->variantType)
\r
4106 case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break;
\r
4107 case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break;
\r
4108 case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break;
\r
4109 case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break;
\r
4110 case YAFFS_OBJECT_TYPE_SPECIAL:
\r
4111 if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
\r
4112 if(S_ISCHR(obj->st_mode)) return DT_CHR;
\r
4113 if(S_ISBLK(obj->st_mode)) return DT_BLK;
\r
4114 if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
\r
4115 default: return DT_REG; break;
\r
4119 char *yaffs_GetSymlinkAlias(yaffs_Object *obj)
\r
4121 obj = yaffs_GetEquivalentObject(obj);
\r
4122 if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
\r
4124 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
\r
4128 return yaffs_CloneString("");
\r
4132 #ifndef CONFIG_YAFFS_WINCE
\r
4134 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
\r
4136 unsigned int valid = attr->ia_valid;
\r
4138 if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
\r
4139 if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
\r
4140 if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
\r
4142 if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime;
\r
4143 if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime;
\r
4144 if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime;
\r
4146 if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
\r
4148 yaffs_UpdateObjectHeader(obj,NULL,1);
\r
4153 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
\r
4155 unsigned int valid = 0;
\r
4157 attr->ia_mode = obj->st_mode; valid |= ATTR_MODE;
\r
4158 attr->ia_uid = obj->st_uid; valid |= ATTR_UID;
\r
4159 attr->ia_gid = obj->st_gid; valid |= ATTR_GID;
\r
4161 attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME;
\r
4162 attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME;
\r
4163 attr->ia_mtime = obj->st_mtime; valid |= ATTR_MTIME;
\r
4165 attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
\r
4167 attr->ia_valid = valid;
\r
4175 int yaffs_DumpObject(yaffs_Object *obj)
\r
4177 // __u8 buffer[YAFFS_BYTES_PER_CHUNK];
\r
4179 // yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
\r
4181 // memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
\r
4183 // if(obj->chunkId >= 0)
\r
4185 // yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
\r
4188 yaffs_GetObjectName(obj,name,256);
\r
4190 T(YAFFS_TRACE_ALWAYS,(TSTR("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d type %d size %d\n" TENDSTR),
\r
4191 obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial,
\r
4192 obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
\r
4195 YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
\r
4196 obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial,
\r
4197 obj->sum, obj->chunkId));
\r
4198 switch(obj->variantType)
\r
4200 case YAFFS_OBJECT_TYPE_FILE:
\r
4201 YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
\r
4203 case YAFFS_OBJECT_TYPE_DIRECTORY:
\r
4204 YPRINTF((" DIRECTORY\n"));
\r
4206 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
\r
4207 case YAFFS_OBJECT_TYPE_SYMLINK:
\r
4208 case YAFFS_OBJECT_TYPE_UNKNOWN:
\r
4217 ///////////////////////// Initialisation code ///////////////////////////
\r
4221 int yaffs_GutsInitialise(yaffs_Device *dev)
\r
4230 if(!yaffs_CheckStructures())
\r
4232 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
\r
4233 return YAFFS_FAIL;
\r
4236 if(dev->startBlock <= 0 ||
\r
4237 (dev->endBlock - dev->startBlock) < 10)
\r
4239 T(YAFFS_TRACE_ALWAYS,(TSTR("startBlock %d or endBlock %d invalid\n" TENDSTR),
\r
4240 dev->startBlock, dev->endBlock));
\r
4241 return YAFFS_FAIL;
\r
4244 nBlocks = dev->endBlock - dev->startBlock + 1;
\r
4248 // OK now calculate a few things for the device
\r
4249 // Calculate chunkGroupBits.
\r
4250 // We need to find the next power of 2 > than endBlock
\r
4252 x = YAFFS_CHUNKS_PER_BLOCK * (dev->endBlock+1);
\r
4254 for(bits = extraBits = 0; x > 1; bits++)
\r
4256 if(x & 1) extraBits++;
\r
4260 if(extraBits > 0) bits++;
\r
4263 // Level0 Tnodes are 16 bits, so if the bitwidth of the
\r
4264 // chunk range we're using is greater than 16 we need
\r
4265 // to figure out chunk shift and chunkGroupSize
\r
4268 dev->chunkGroupBits = 0;
\r
4272 dev->chunkGroupBits = bits - 16;
\r
4274 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
\r
4277 // More device initialisation
\r
4278 dev->garbageCollectionRequired = 0;
\r
4279 dev->garbageCollections = 0;
\r
4280 dev->currentDirtyChecker = 0;
\r
4281 dev->bufferedBlock = -1;
\r
4282 dev->doingBufferedBlockRewrite = 0;
\r
4283 dev->blockSelectedForGC = -1;
\r
4284 dev->nDeletedFiles = 0;
\r
4285 dev->nBackgroundDeletions=0;
\r
4286 dev->nUnlinkedFiles = 0;
\r
4288 dev->eccUnfixed=0;
\r
4289 dev->tagsEccFixed=0;
\r
4290 dev->tagsEccUnfixed=0;
\r
4292 yaffs_InitialiseBlocks(dev,nBlocks);
\r
4294 yaffs_InitialiseTnodes(dev);
\r
4296 yaffs_InitialiseObjects(dev);
\r
4298 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
\r
4301 for(i=0; i < YAFFS_N_CACHE_CHUNKS; i++)
\r
4303 dev->srCache[i].object = NULL;
\r
4304 dev->srCache[i].lastUse = 0;
\r
4305 dev->srCache[i].dirty = 0;
\r
4307 dev->srLastUse = 0;
\r
4311 dev->cacheHits = 0;
\r
4314 // Initialise the unlinked, root and lost and found directories
\r
4315 dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = NULL;
\r
4317 dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
\r
4319 dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
\r
4320 dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
\r
4321 yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
\r
4324 // Now scan the flash.
\r
4328 dev->nPageReads = 0;
\r
4329 dev->nPageWrites = 0;
\r
4330 dev->nBlockErasures = 0;
\r
4331 dev->nGCCopies = 0;
\r
4332 dev->nRetriedWrites = 0;
\r
4333 dev->nRetiredBlocks = 0;
\r
4340 void yaffs_Deinitialise(yaffs_Device *dev)
\r
4342 yaffs_DeinitialiseBlocks(dev);
\r
4343 yaffs_DeinitialiseTnodes(dev);
\r
4344 yaffs_DeinitialiseObjects(dev);
\r
4348 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
\r
4350 int nFree = dev->nFreeChunks - (YAFFS_CHUNKS_PER_BLOCK * YAFFS_RESERVED_BLOCKS);
\r
4352 struct list_head *i;
\r
4356 // To the free chunks add the chunks that are in the deleted unlinked files.
\r
4357 list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
\r
4359 l = list_entry(i, yaffs_Object,siblings);
\r
4363 nFree += l->nDataChunks;
\r
4368 return (nFree < 0) ? 0 : nFree;
\r
4373 /////////////////// YAFFS test code //////////////////////////////////
\r
4375 #define yaffs_CheckStruct(structure,syze, name) \
\r
4376 if(sizeof(structure) != syze) \
\r
4378 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
\r
4379 return YAFFS_FAIL; \
\r
4383 static int yaffs_CheckStructures(void)
\r
4385 yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
\r
4386 yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
\r
4387 yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
\r
4388 yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
\r
4389 yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
\r
4396 void yaffs_GutsTest(yaffs_Device *dev)
\r
4399 if(yaffs_CheckStructures() != YAFFS_OK)
\r
4401 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
\r
4405 yaffs_TnodeTest(dev);
\r
4406 yaffs_ObjectTest(dev);
\r