*** empty log message ***
[yaffs2.git] / yaffs_guts.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  * yaffs_guts.c  The main guts of YAFFS
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  */
14  //yaffs_guts.c
15
16 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.1 2004-11-03 08:14:07 charles Exp $";
17
18 #include "yportenv.h"
19
20 #include "yaffsinterface.h"
21 #include "yaffs_guts.h"
22
23 #include "yaffs_tagscompat.h"
24
25 #ifdef CONFIG_YAFFS_WINCE
26 void yfsd_LockYAFFS(BOOL fsLockOnly);
27 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
28 #endif
29
30 #define YAFFS_PASSIVE_GC_CHUNKS 2
31
32 #if 0
33 // Use Steven Hill's ECC struff instead
34 // External functions for ECC on data
35 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
36 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
37 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
38 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
39 #else
40 #include "yaffs_ecc.h"
41 #endif
42
43 // countBits is a quick way of counting the number of bits in a byte.
44 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
45
46 static const char yaffs_countBitsTable[256] =
47 {
48 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
49 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
50 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
52 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
53 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
54 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
55 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
56 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
58 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
59 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
60 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
61 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
62 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
63 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
64 };
65
66 static int yaffs_CountBits(__u8 x)
67 {
68         int retVal;
69         retVal = yaffs_countBitsTable[x];
70         return retVal;
71 }
72
73
74 #if 0
75 // Stuff using yea olde tags
76 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
77 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
78
79 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
80 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
81 #else
82 #endif
83
84 // NAND access
85
86
87 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
88 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
89 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
90 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
91 // Local prototypes
92 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
93 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
94 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
95
96 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
97 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
98 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
99 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
100 static int yaffs_CheckStructures(void);
101 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
102 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
103
104 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
105
106 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
107 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
108
109
110 static int  yaffs_ValidateTags(yaffs_ExtendedTags *tags);
111
112 // Robustification (if it ever comes about...)
113 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
114 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
115 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
116 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
117 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND,  const yaffs_ExtendedTags *tags);
118
119 static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
120
121 static int yaffs_UnlinkWorker(yaffs_Object *obj);
122 static void yaffs_DestroyObject(yaffs_Object *obj);
123
124 #if 0
125 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
126 #endif
127
128 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
129
130
131 loff_t yaffs_GetFileSize(yaffs_Object *obj);
132
133
134 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
135
136 #ifdef YAFFS_PARANOID
137 static int yaffs_CheckFileSanity(yaffs_Object *in);
138 #else
139 #define yaffs_CheckFileSanity(in)
140 #endif
141
142 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
143 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
144
145 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
146 {
147         if(dev->readChunkWithTagsFromNAND)
148                 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
149         else
150                 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
151 }
152
153 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
154 {
155         if(tags)
156         {
157                 tags->sequenceNumber = dev->sequenceNumber;
158                 tags->chunkUsed = 1;
159                 if(!yaffs_ValidateTags(tags))
160                 {
161                         T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
162                         YBUG();
163                 }
164                 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
165         }
166         else
167         {
168                         T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
169                         YBUG();
170         }
171
172         if(dev->writeChunkWithTagsToNAND)
173                 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
174         else
175                 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
176 }
177
178 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
179 {
180         if(dev->markNANDBlockBad)
181                 return dev->markNANDBlockBad(dev,blockNo);
182         else
183                 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
184 }
185 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
186 {
187         if(dev->queryNANDBlock)
188                 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
189         else
190                 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
191 }
192
193 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
194 {
195         int result;
196
197         dev->nBlockErasures++;
198         result = dev->eraseBlockInNAND(dev,blockInNAND);
199
200         if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
201         return result;
202 }
203
204 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
205 {
206         return dev->initialiseNAND(dev);
207 }
208
209
210
211
212 // Temporary buffer manipulations
213
214 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
215 {
216         int i,j;
217         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
218         {
219                 if(dev->tempBuffer[i].line == 0)
220                 {
221                         dev->tempBuffer[i].line = lineNo;
222                         if((i+1) > dev->maxTemp)
223                         {
224                                 dev->maxTemp = i + 1;
225                                 for(j = 0; j <= i; j++)
226                                         dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
227                         }       
228                         
229                         return dev->tempBuffer[i].buffer;
230                 }
231         }
232
233         T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
234         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
235         {
236                 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
237         }
238         T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
239         
240         dev->unmanagedTempAllocations++;
241         // Get an unmanaged one
242         return YMALLOC(dev->nBytesPerChunk);    
243         
244         
245 }
246
247 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
248 {
249         int i;
250         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
251         {
252                 if(dev->tempBuffer[i].buffer == buffer)
253                 {
254                         dev->tempBuffer[i].line = 0;
255                         return;
256                 }
257         }
258         
259         if(buffer)
260         {
261                 // assume it is an unmanaged one.
262                 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
263                 YFREE(buffer);
264                 dev->unmanagedTempDeallocations++;
265         }
266
267 }
268
269
270 // Chunk bitmap manipulations
271
272 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
273 {
274         if(blk < dev->startBlock || blk > dev->endBlock)
275         {
276                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
277                 YBUG();
278         }
279         return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock));
280 }
281
282 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
283 {
284         __u8 *blkBits = yaffs_BlockBits(dev,blk);
285
286          memset(blkBits,0,dev->chunkBitmapStride);
287 }
288
289 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
290 {
291         __u8 *blkBits = yaffs_BlockBits(dev,blk);
292         
293         blkBits[chunk/8] &=  ~ (1<<(chunk & 7));
294 }
295
296 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
297 {
298         __u8 *blkBits = yaffs_BlockBits(dev,blk);
299
300         blkBits[chunk/8] |=   (1<<(chunk & 7));
301 }
302
303 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
304 {
305         __u8 *blkBits = yaffs_BlockBits(dev,blk);
306         return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;
307 }
308
309 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
310 {
311         __u8 *blkBits = yaffs_BlockBits(dev,blk);
312         int i;
313         for(i = 0; i < dev->chunkBitmapStride; i++)
314         {
315                 if(*blkBits) return 1;
316                 blkBits++;
317         }
318         return 0;
319 }
320
321 #if 0
322 // Function to manipulate block info
323 static  Y_INLINE yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
324 {
325         if(blk < dev->startBlock || blk > dev->endBlock)
326         {
327                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));
328                 YBUG();
329         }
330         return &dev->blockInfo[blk - dev->startBlock];
331 }
332 #endif
333
334
335 static  Y_INLINE int yaffs_HashFunction(int n)
336 {
337         return (n % YAFFS_NOBJECT_BUCKETS);
338 }
339
340
341 yaffs_Object *yaffs_Root(yaffs_Device *dev)
342 {
343         return dev->rootDir;
344 }
345
346 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
347 {
348         return dev->lostNFoundDir;
349 }
350
351 #if 0
352 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
353 {
354         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
355         {
356                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
357                 return YAFFS_FAIL;
358         }
359
360         dev->nPageWrites++;
361         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
362 }
363
364
365
366 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
367                                                         int chunkInNAND, 
368                                                         __u8 *data, 
369                                                         yaffs_Spare *spare, 
370                                                         int doErrorCorrection)
371 {
372         int retVal;
373         yaffs_Spare localSpare;
374
375         dev->nPageReads++;
376         
377         
378
379         
380         if(!spare && data)
381         {
382                 // If we don't have a real spare, then we use a local one.
383                 // Need this for the calculation of the ecc
384                 spare = &localSpare;
385         }
386         
387
388         if(!dev->useNANDECC)
389         {
390                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
391                 if(data && doErrorCorrection)
392                 {
393                         // Do ECC correction
394                         //Todo handle any errors
395                 int eccResult1,eccResult2;
396                 __u8 calcEcc[3];
397                 
398                         yaffs_ECCCalculate(data,calcEcc);
399                         eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
400                         yaffs_ECCCalculate(&data[256],calcEcc);
401                         eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
402
403                         if(eccResult1>0)
404                         {
405                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
406                                 dev->eccFixed++;
407                         }
408                         else if(eccResult1<0)
409                         {
410                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
411                                 dev->eccUnfixed++;
412                         }
413
414                         if(eccResult2>0)
415                         {
416                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
417                                 dev->eccFixed++;
418                         }
419                         else if(eccResult2<0)
420                         {
421                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
422                                 dev->eccUnfixed++;
423                         }
424
425                         if(eccResult1 || eccResult2)
426                         {
427                                 // Hoosterman, we had a data problem on this page
428                                 yaffs_HandleReadDataError(dev,chunkInNAND);
429                         }
430                 }
431         }
432         else
433         {
434         // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
435         struct yaffs_NANDSpare nspare;
436                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
437                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
438                 if(data && doErrorCorrection)
439                 {
440                         if(nspare.eccres1>0)
441                         {
442                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
443                         }
444                         else if(nspare.eccres1<0)
445                         {
446                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
447                         }
448
449                         if(nspare.eccres2>0)
450                         {
451                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
452                         }
453                         else if(nspare.eccres2<0)
454                         {
455                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
456                         }
457
458                         if(nspare.eccres1 || nspare.eccres2)
459                         {
460                                 // Hoosterman, we had a data problem on this page
461                                 yaffs_HandleReadDataError(dev,chunkInNAND);
462                         }
463
464                 }
465         }
466         return retVal;
467 }
468
469 #endif
470
471
472
473 int yaffs_CheckFF(__u8 *buffer,int nBytes)
474 {
475         //Horrible, slow implementation
476         while(nBytes--)
477         {
478                 if(*buffer != 0xFF) return 0; 
479         }
480         return 1;
481 }
482
483 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
484 {
485
486         int retval = YAFFS_OK;
487         __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
488         yaffs_ExtendedTags tags;
489
490         dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
491         
492         if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
493         {
494                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
495                 retval = YAFFS_FAIL;
496         }
497
498         yaffs_ReleaseTempBuffer(dev,data,__LINE__);
499         
500         return retval;
501         
502 }
503
504
505
506 #if 1
507 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
508 {
509         int chunk;
510         
511         int writeOk = 1;
512         int attempts = 0;
513         
514
515         
516         do{
517                 chunk = yaffs_AllocateChunk(dev,useReserve);
518         
519                 if(chunk >= 0)
520                 {
521
522                         // First check this chunk is erased...
523 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
524                         writeOk = yaffs_CheckChunkErased(dev,chunk);
525 #endif          
526                         if(!writeOk)
527                         {
528                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
529                         }
530                         else
531                         {
532                                 writeOk =  yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
533                         }
534                         attempts++;
535
536                         if(writeOk)
537                         {
538                                 // Copy the data into the robustification buffer.
539                                 // NB We do this at the end to prevent duplicates in the case of a write error.
540                                 //Todo
541                                 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
542                         }
543                         else
544                         {
545                                 yaffs_HandleWriteChunkError(dev,chunk);
546                         }
547                 }
548                 
549         } while(chunk >= 0 && ! writeOk);
550         
551         if(attempts > 1)
552         {
553                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
554                 dev->nRetriedWrites+= (attempts - 1);   
555         }
556         
557
558         
559         return chunk;
560 }
561 #endif
562
563
564 ///
565 // Functions for robustisizing
566 //
567 //
568
569 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
570 {
571         
572         yaffs_MarkBlockBad(dev,blockInNAND);
573         
574         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
575
576         dev->nRetiredBlocks++;
577 }
578
579
580
581 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
582 {
583         dev->doingBufferedBlockRewrite = 1;
584         //
585         //      Remove erased chunks
586         //  Rewrite existing chunks to a new block
587         //      Set current write block to the new block
588         
589         dev->doingBufferedBlockRewrite = 0;
590         
591         return 1;
592 }
593
594
595 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
596 {
597         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
598
599         // Mark the block for retirement
600         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
601         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
602
603
604         //TODO  
605         // Just do a garbage collection on the affected block then retire the block
606         // NB recursion
607 }
608
609
610 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
611 {
612 }
613
614 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
615 {
616 }
617
618 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
619 {
620 }
621
622 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
623 {
624         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
625
626         // Mark the block for retirement
627         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
628         // Delete the chunk
629         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
630 }
631
632
633
634 #if 0
635 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
636 {
637
638
639         if( memcmp(d0,d1,dataSize) != 0 ||
640                 s0->tagByte0 != s1->tagByte0 ||
641                 s0->tagByte1 != s1->tagByte1 ||
642                 s0->tagByte2 != s1->tagByte2 ||
643                 s0->tagByte3 != s1->tagByte3 ||
644                 s0->tagByte4 != s1->tagByte4 ||
645                 s0->tagByte5 != s1->tagByte5 ||
646                 s0->tagByte6 != s1->tagByte6 ||
647                 s0->tagByte7 != s1->tagByte7 ||
648                 s0->ecc1[0]  != s1->ecc1[0]  ||
649                 s0->ecc1[1]  != s1->ecc1[1]  ||
650                 s0->ecc1[2]  != s1->ecc1[2]  ||
651                 s0->ecc2[0]  != s1->ecc2[0]  ||
652                 s0->ecc2[1]  != s1->ecc2[1]  ||
653                 s0->ecc2[2]  != s1->ecc2[2] )
654                 {
655                         return 0;
656                 }
657         
658         return 1;
659 }
660 #endif
661
662
663 ///////////////////////// Object management //////////////////
664 // List of spare objects
665 // The list is hooked together using the first pointer
666 // in the object
667
668 // static yaffs_Object *yaffs_freeObjects = NULL;
669
670 // static int yaffs_nFreeObjects;
671
672 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
673
674 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
675
676
677 static __u16 yaffs_CalcNameSum(const YCHAR *name)
678 {
679         __u16 sum = 0;
680         __u16 i = 1;
681         
682         YUCHAR *bname = (YUCHAR *)name;
683         if(bname)
684         {
685                 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
686                 {
687
688 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
689                         sum += yaffs_toupper(*bname) * i;
690 #else
691                         sum += (*bname) * i;
692 #endif
693                         i++;
694                         bname++;
695                 }
696         }
697         return sum;
698 }
699
700 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
701 {
702 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
703                                         if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
704                                         {
705                                                 yaffs_strcpy(obj->shortName,name);
706                                         }
707                                         else
708                                         {
709                                                 obj->shortName[0]=_Y('\0');
710                                         }
711 #endif
712                                         obj->sum = yaffs_CalcNameSum(name);
713 }
714
715 #if 0
716 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
717 {
718         yaffs_ECCCalculate(data , spare->ecc1);
719         yaffs_ECCCalculate(&data[256] , spare->ecc2);
720 }
721 #endif
722
723
724 ///////////////////////// TNODES ///////////////////////
725
726 // List of spare tnodes
727 // The list is hooked together using the first pointer
728 // in the tnode.
729
730 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
731
732 // static int yaffs_nFreeTnodes;
733
734 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
735
736
737
738 // yaffs_CreateTnodes creates a bunch more tnodes and
739 // adds them to the tnode free list.
740 // Don't use this function directly
741
742 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
743 {
744     int i;
745     yaffs_Tnode *newTnodes;
746     yaffs_TnodeList *tnl;
747     
748     if(nTnodes < 1) return YAFFS_OK;
749    
750         // make these things
751         
752     newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
753    
754     if (!newTnodes)
755     {
756                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
757                 return YAFFS_FAIL;
758     }
759     
760     // Hook them into the free list
761     for(i = 0; i < nTnodes - 1; i++)
762     {
763         newTnodes[i].internal[0] = &newTnodes[i+1];
764 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
765         newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
766 #endif
767     }
768         
769         newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
770 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
771         newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
772 #endif
773         dev->freeTnodes = newTnodes;
774         dev->nFreeTnodes+= nTnodes;
775         dev->nTnodesCreated += nTnodes;
776
777         // Now add this bunch of tnodes to a list for freeing up.
778         // NB If we can't add this to the management list it isn't fatal
779         // but it just means we can't free this bunch of tnodes later.
780         tnl = YMALLOC(sizeof(yaffs_TnodeList));
781         if(!tnl)
782         {
783                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
784                 
785         }
786         else
787         {
788                 tnl->tnodes = newTnodes;
789                 tnl->next = dev->allocatedTnodeList;
790                 dev->allocatedTnodeList = tnl;
791         }
792
793
794         T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
795
796
797         return YAFFS_OK;
798 }
799
800
801 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
802 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
803 {
804         yaffs_Tnode *tn = NULL;
805         
806         // If there are none left make more
807         if(!dev->freeTnodes)
808         {
809                 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
810         }
811         
812         if(dev->freeTnodes)
813         {
814                 tn = dev->freeTnodes;
815 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
816         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
817                 {
818                         // Hoosterman, this thing looks like it isn't in the list
819                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
820                 }
821 #endif
822                 dev->freeTnodes = dev->freeTnodes->internal[0];
823                 dev->nFreeTnodes--;
824                 // zero out
825                 memset(tn,0,sizeof(yaffs_Tnode));
826         }
827         
828
829         return tn;
830 }
831
832
833 // FreeTnode frees up a tnode and puts it back on the free list
834 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
835 {
836         if(tn)
837         {
838 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
839         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
840                 {
841                         // Hoosterman, this thing looks like it is already in the list
842                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
843                 }
844                 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
845 #endif
846                 tn->internal[0] = dev->freeTnodes;
847                 dev->freeTnodes = tn;
848                 dev->nFreeTnodes++;
849         }
850 }
851
852
853 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
854 {
855         // Free the list of allocated tnodes
856         yaffs_TnodeList *tmp;
857                 
858         while(dev->allocatedTnodeList)
859         {
860                 tmp =  dev->allocatedTnodeList->next;
861
862                 YFREE(dev->allocatedTnodeList->tnodes);
863                 YFREE(dev->allocatedTnodeList);
864                 dev->allocatedTnodeList = tmp;
865                 
866         }
867         
868         dev->freeTnodes = NULL;
869         dev->nFreeTnodes = 0;
870 }
871
872 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
873 {
874         dev->allocatedTnodeList = NULL;
875         dev->freeTnodes = NULL;
876         dev->nFreeTnodes = 0;
877         dev->nTnodesCreated = 0;
878
879 }
880
881 #if 0
882 void yaffs_TnodeTest(yaffs_Device *dev)
883 {
884
885         int i;
886         int j;
887         yaffs_Tnode *tn[1000];
888         
889         YINFO("Testing TNodes");
890         
891         for(j = 0; j < 50; j++)
892         {
893                 for(i = 0; i < 1000; i++)
894                 {
895                         tn[i] = yaffs_GetTnode(dev);
896                         if(!tn[i])
897                         {
898                                 YALERT("Getting tnode failed");
899                         }
900                 }
901                 for(i = 0; i < 1000; i+=3)
902                 {
903                         yaffs_FreeTnode(dev,tn[i]);
904                         tn[i] = NULL;
905                 }
906                 
907         }
908 }
909 #endif
910
911
912 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
913
914 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
915 // The look up tree is represented by the top tnode and the number of topLevel
916 // in the tree. 0 means only the level 0 tnode is in the tree.
917
918
919 // FindLevel0Tnode finds the level 0 tnode, if one exists.
920 // Used when reading.....
921 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
922 {
923         
924         yaffs_Tnode *tn = fStruct->top;
925         __u32 i;
926         int requiredTallness;   
927         int level = fStruct->topLevel;
928         
929         // Check sane level and chunk Id
930         if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
931         {
932 //              char str[50];
933 //              sprintf(str,"Bad level %d",level);
934 //              YALERT(str);
935                 return NULL;
936         }
937         
938         if(chunkId > YAFFS_MAX_CHUNK_ID)
939         {
940 //              char str[50];
941 //              sprintf(str,"Bad chunkId %d",chunkId);
942 //              YALERT(str);
943                 return NULL;
944         }
945
946         // First check we're tall enough (ie enough topLevel)
947         
948         i = chunkId >> (/*dev->chunkGroupBits  + */YAFFS_TNODES_LEVEL0_BITS);
949         requiredTallness = 0;
950         while(i)
951         {
952                 i >>= YAFFS_TNODES_INTERNAL_BITS;
953                 requiredTallness++;
954         }
955         
956         
957         if(requiredTallness > fStruct->topLevel)
958         {
959                 // Not tall enough, so we can't find it, return NULL.
960                 return NULL;
961         }
962                 
963         
964         // Traverse down to level 0
965         while (level > 0 && tn)
966         {
967             tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
968                                YAFFS_TNODES_INTERNAL_MASK]; 
969                 level--;
970         
971         }
972         
973         return tn;              
974 }
975
976 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
977 // This happens in two steps:
978 //  1. If the tree isn't tall enough, then make it taller.
979 //  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
980 //
981 // Used when modifying the tree.
982 //
983 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
984 {
985         
986         yaffs_Tnode *tn; 
987         
988         int requiredTallness;
989         int i;
990         int l;
991         
992         __u32 x;
993                 
994         
995         //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
996         
997         // Check sane level and page Id
998         if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
999         {
1000 //              char str[50];
1001 //              sprintf(str,"Bad level %d",fStruct->topLevel);
1002 //              YALERT(str);
1003                 return NULL;
1004         }
1005         
1006         if(chunkId > YAFFS_MAX_CHUNK_ID)
1007         {
1008 //              char str[50];
1009 //              sprintf(str,"Bad chunkId %d",chunkId);
1010 //              YALERT(str);
1011                 return NULL;
1012         }
1013         
1014         // First check we're tall enough (ie enough topLevel)
1015         
1016         x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
1017         requiredTallness = 0;
1018         while(x)
1019         {
1020                 x >>= YAFFS_TNODES_INTERNAL_BITS;
1021                 requiredTallness++;
1022         }
1023         
1024         //T((TSTR(" required=%d"),requiredTallness));
1025         
1026         
1027         if(requiredTallness > fStruct->topLevel)
1028         {
1029                 // Not tall enough,gotta make the tree taller
1030                 for(i = fStruct->topLevel; i < requiredTallness; i++)
1031                 {
1032                         //T((TSTR(" add new top")));
1033                         
1034                         tn = yaffs_GetTnode(dev);
1035                         
1036                         if(tn)
1037                         {
1038                                 tn->internal[0] = fStruct->top;
1039                                 fStruct->top = tn;
1040                         }
1041                         else
1042                         {
1043                                         T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
1044                         }
1045                 }
1046                 
1047                 fStruct->topLevel = requiredTallness;
1048         }
1049         
1050         
1051         // Traverse down to level 0, adding anything we need
1052         
1053         l = fStruct->topLevel;
1054         tn = fStruct->top;
1055         while (l > 0 && tn)
1056         {
1057                 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
1058                                YAFFS_TNODES_INTERNAL_MASK;
1059                                
1060                 //T((TSTR(" [%d:%d]"),l,i));
1061                 
1062             if(!tn->internal[x])
1063             {
1064                 //T((TSTR(" added")));
1065                 
1066                 tn->internal[x] = yaffs_GetTnode(dev);
1067             }
1068             
1069             tn =        tn->internal[x];
1070                 l--;
1071         
1072         }
1073         
1074         //TSTR(TENDSTR)));
1075         
1076         return tn;              
1077 }
1078
1079
1080 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
1081 {
1082         int j;
1083         
1084                                         
1085         for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
1086         {
1087                 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
1088                 {
1089                         yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
1090                         if(yaffs_TagsMatch(tags,objectId,chunkInInode))
1091                         {
1092                                 // found it;
1093                                 return theChunk;
1094                                         
1095                         }
1096                 }
1097                 theChunk++;
1098         }
1099         return -1;
1100 }
1101
1102 // DeleteWorker scans backwards through the tnode tree and deletes all the
1103 // chunks and tnodes in the file
1104 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1105
1106 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1107 {
1108         int i;
1109         int chunkInInode;
1110         int theChunk;
1111         yaffs_ExtendedTags tags;
1112         int foundChunk;
1113         yaffs_Device *dev = in->myDev;
1114
1115         int allDone = 1;
1116         
1117         
1118         if(tn)
1119         {
1120                 if(level > 0)
1121                 {
1122                 
1123                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1124                         {
1125                             if(tn->internal[i])
1126                         {
1127                                         if(limit && (*limit) < 0)
1128                                         {
1129                                                 allDone = 0;
1130                                         }
1131                                         else
1132                                         {
1133                                                 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1134                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1135                                         }
1136                                         if(allDone)
1137                                         {
1138                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1139                                         tn->internal[i] = NULL;
1140                                         }
1141                             }
1142                     
1143                         }
1144                         return (allDone) ? 1 : 0;
1145                 }
1146                 else if(level == 0)
1147                 {
1148                         int hitLimit = 0;
1149                         
1150                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1151                         {
1152                             if(tn->level0[i])
1153                         {
1154                                         
1155                                         chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1156                                         
1157                                         theChunk =  tn->level0[i] << dev->chunkGroupBits;
1158                                         
1159                                         foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1160                                         
1161                                         if(foundChunk > 0)
1162                                         {
1163                                                 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1164                                                 in->nDataChunks--;
1165                                                 if(limit)
1166                                                 { 
1167                                                         *limit = *limit-1;
1168                                                         if(*limit <= 0) 
1169                                                         { 
1170                                                                 hitLimit = 1;
1171                                                         }
1172                                                 }
1173                                         
1174                                         }
1175                                         
1176                                 tn->level0[i] = 0;
1177                             }
1178                     
1179                         }
1180                         return (i < 0) ? 1 : 0;
1181
1182                         
1183                 }
1184                 
1185         }
1186         
1187         return 1;
1188         
1189 }
1190
1191
1192 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1193 {
1194
1195         yaffs_BlockInfo *theBlock;                                      
1196         
1197         T(YAFFS_TRACE_DELETE,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1198                                                 
1199         theBlock =      yaffs_GetBlockInfo(dev,  chunk/dev->nChunksPerBlock);
1200         if(theBlock)
1201         {
1202                 theBlock->softDeletions++;
1203         }
1204 }
1205
1206 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1207 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1208 // of the tnode.
1209 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1210 //
1211 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1212 {
1213         int i;
1214         int theChunk;
1215         int allDone = 1;
1216         yaffs_Device *dev = in->myDev;
1217         
1218         
1219         if(tn)
1220         {
1221                 if(level > 0)
1222                 {
1223                 
1224                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1225                         {
1226                             if(tn->internal[i])
1227                         {
1228                                                 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1229                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1230                                         if(allDone)
1231                                         {
1232                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1233                                         tn->internal[i] = NULL;
1234                                         }
1235                                         else
1236                                         {
1237                                                 //Hoosterman... how could this happen.
1238                                         }                           
1239                                 }                   
1240                         }
1241                         return (allDone) ? 1 : 0;
1242                 }
1243                 else if(level == 0)
1244                 {
1245                         
1246                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1247                         {
1248                             if(tn->level0[i])
1249                         {
1250                                         // Note this does not find the real chunk, only the chunk group.
1251                                         // We make an assumption that a chunk group is niot larger than a block.
1252                                         theChunk =  (tn->level0[i] << dev->chunkGroupBits);
1253                                         
1254                                         yaffs_SoftDeleteChunk(dev,theChunk);
1255                                 tn->level0[i] = 0;
1256                             }
1257                     
1258                         }
1259                         return 1;
1260                         
1261                 }
1262                 
1263         }
1264         
1265         return 1;
1266                 
1267 }
1268
1269
1270
1271 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1272 {
1273         if(obj->deleted &&
1274            obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1275            !obj->softDeleted)
1276         {
1277                 if(obj->nDataChunks <= 0)
1278                 {
1279                                 // Empty file with no duplicate object headers, just delete it immediately
1280                                 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1281                                 obj->variant.fileVariant.top = NULL;
1282                                 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1283                                 yaffs_DoGenericObjectDeletion(obj);     
1284                 }
1285                 else
1286                 {
1287                         yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1288                         obj->softDeleted = 1;
1289                 }
1290         }
1291 }
1292
1293
1294
1295
1296
1297 // Pruning removes any part of the file structure tree that is beyond the
1298 // bounds of the file (ie that does not point to chunks).
1299 //
1300 // A file should only get pruned when its size is reduced.
1301 //
1302 // Before pruning, the chunks must be pulled from the tree and the
1303 // level 0 tnode entries must be zeroed out.
1304 // Could also use this for file deletion, but that's probably better handled
1305 // by a special case.
1306
1307 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1308
1309 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1310 {
1311         int i;
1312         int hasData;
1313         
1314         if(tn)
1315         {
1316                 hasData = 0;
1317                 
1318                 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1319                 {
1320                     if(tn->internal[i] && level > 0)
1321                     {
1322                         tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1323                     }
1324                     
1325                     if(tn->internal[i])
1326                     {
1327                         hasData++;
1328                         }
1329                 }
1330                 
1331                 if(hasData == 0 && del0)
1332                 {
1333                         // Free and return NULL
1334                         
1335                         yaffs_FreeTnode(dev,tn);
1336                         tn = NULL;
1337                 }
1338                 
1339         }
1340
1341         return tn;
1342         
1343 }
1344
1345 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1346 {
1347         int i;
1348         int hasData;
1349         int done = 0;
1350         yaffs_Tnode *tn;
1351         
1352         if(fStruct->topLevel > 0)
1353         {
1354                 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1355                 
1356                 // Now we have a tree with all the non-zero branches NULL but the height
1357                 // is the same as it was.
1358                 // Let's see if we can trim internal tnodes to shorten the tree.
1359                 // We can do this if only the 0th element in the tnode is in use 
1360                 // (ie all the non-zero are NULL)
1361                 
1362                 while(fStruct->topLevel && !done)
1363                 {
1364                         tn = fStruct->top;
1365                         
1366                         hasData = 0;
1367                         for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1368                         {
1369                                 if(tn->internal[i])
1370                         {
1371                                 hasData++;
1372                                 }
1373                         }
1374                         
1375                         if(!hasData)
1376                         {
1377                                 fStruct->top = tn->internal[0];
1378                                 fStruct->topLevel--;
1379                                 yaffs_FreeTnode(dev,tn);
1380                         }
1381                         else
1382                         {
1383                                 done = 1;
1384                         }
1385                 }
1386         }
1387         
1388         return YAFFS_OK;
1389 }
1390
1391
1392
1393
1394
1395 /////////////////////// End of File Structure functions. /////////////////
1396
1397 // yaffs_CreateFreeObjects creates a bunch more objects and
1398 // adds them to the object free list.
1399 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1400 {
1401     int i;
1402     yaffs_Object *newObjects;
1403     yaffs_ObjectList *list;
1404     
1405     if(nObjects < 1) return YAFFS_OK;
1406    
1407         // make these things
1408         
1409     newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1410    
1411     if (!newObjects)
1412     {
1413                 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1414                 return YAFFS_FAIL;
1415     }
1416     
1417     // Hook them into the free list
1418     for(i = 0; i < nObjects - 1; i++)
1419     {
1420         newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1421     }
1422         
1423         newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1424         dev->freeObjects = newObjects;
1425         dev->nFreeObjects+= nObjects;
1426         dev->nObjectsCreated+= nObjects;
1427         
1428         // Now add this bunch of Objects to a list for freeing up.
1429         
1430         list = YMALLOC(sizeof(yaffs_ObjectList));
1431         if(!list)
1432         {
1433                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1434         }
1435         else
1436         {
1437                 list->objects = newObjects;
1438                 list->next = dev->allocatedObjectList;
1439                 dev->allocatedObjectList = list;
1440         }
1441         
1442         
1443         
1444         return YAFFS_OK;
1445 }
1446
1447
1448 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1449 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1450 {
1451         yaffs_Object *tn = NULL;
1452         
1453         // If there are none left make more
1454         if(!dev->freeObjects)
1455         {
1456                 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1457         }
1458         
1459         if(dev->freeObjects)
1460         {
1461                 tn = dev->freeObjects;
1462                 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1463                 dev->nFreeObjects--;
1464                 
1465                 // Now sweeten it up...
1466         
1467                 memset(tn,0,sizeof(yaffs_Object));
1468                 tn->myDev = dev;
1469                 tn->chunkId = -1;
1470                 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1471                 INIT_LIST_HEAD(&(tn->hardLinks));
1472                 INIT_LIST_HEAD(&(tn->hashLink));
1473                 INIT_LIST_HEAD(&tn->siblings);
1474                 
1475                 // Add it to the lost and found directory.
1476                 // NB Can't put root or lostNFound in lostNFound so
1477                 // check if lostNFound exists first
1478                 if(dev->lostNFoundDir)
1479                 {
1480                         yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);      
1481                 }
1482         }
1483         
1484
1485         return tn;
1486 }
1487
1488 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1489 {
1490
1491         yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);              
1492         if(obj)
1493         {
1494                 obj->fake = 1;                  // it is fake so it has no NAND presence...
1495                 obj->renameAllowed= 0;  // ... and we're not allowed to rename it...
1496                 obj->unlinkAllowed= 0;  // ... or unlink it
1497                 obj->deleted = 0;
1498                 obj->unlinked = 0;
1499                 obj->st_mode = mode;
1500                 obj->myDev = dev;
1501                 obj->chunkId = 0; // Not a valid chunk.
1502         }
1503         
1504         return obj;
1505         
1506 }
1507
1508
1509 static void yaffs_UnhashObject(yaffs_Object *tn)
1510 {
1511         int bucket;
1512         yaffs_Device *dev = tn->myDev;
1513         
1514         
1515         // If it is still linked into the bucket list, free from the list
1516         if(!list_empty(&tn->hashLink))
1517         {
1518                 list_del_init(&tn->hashLink);
1519                 bucket =  yaffs_HashFunction(tn->objectId);
1520                 dev->objectBucket[bucket].count--;
1521         }
1522         
1523 }
1524
1525
1526 // FreeObject frees up a Object and puts it back on the free list
1527 static void yaffs_FreeObject(yaffs_Object *tn)
1528 {
1529
1530         yaffs_Device *dev = tn->myDev;
1531         
1532         yaffs_UnhashObject(tn);
1533         
1534         // Link into the free list.
1535         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1536         dev->freeObjects = tn;
1537         dev->nFreeObjects++;
1538 }
1539
1540
1541
1542
1543 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1544 {
1545         // Free the list of allocated Objects
1546         
1547         yaffs_ObjectList *tmp;
1548         
1549         while( dev->allocatedObjectList)
1550         {
1551                 tmp =  dev->allocatedObjectList->next;
1552                 YFREE(dev->allocatedObjectList->objects);
1553                 YFREE(dev->allocatedObjectList);
1554                 
1555                 dev->allocatedObjectList =  tmp;
1556         }
1557         
1558         dev->freeObjects = NULL;
1559         dev->nFreeObjects = 0;
1560 }
1561
1562 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1563 {
1564         int i;
1565         
1566         dev->allocatedObjectList = NULL;
1567         dev->freeObjects = NULL;
1568         dev->nFreeObjects = 0;
1569         
1570         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1571         {
1572                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1573                 dev->objectBucket[i].count = 0; 
1574         }
1575
1576 }
1577
1578
1579
1580
1581
1582
1583 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1584 {
1585         static int x = 0;
1586         int i;
1587         int l = 999;
1588         int lowest = 999999;
1589
1590                 
1591         // First let's see if we can find one that's empty.
1592         
1593         for(i = 0; i < 10 && lowest > 0; i++)
1594          {
1595                 x++;
1596                 x %=  YAFFS_NOBJECT_BUCKETS;
1597                 if(dev->objectBucket[x].count < lowest)
1598                 {
1599                         lowest = dev->objectBucket[x].count;
1600                         l = x;
1601                 }
1602                 
1603         }
1604         
1605         // If we didn't find an empty list, then try
1606         // looking a bit further for a short one
1607         
1608         for(i = 0; i < 10 && lowest > 3; i++)
1609          {
1610                 x++;
1611                 x %=  YAFFS_NOBJECT_BUCKETS;
1612                 if(dev->objectBucket[x].count < lowest)
1613                 {
1614                         lowest = dev->objectBucket[x].count;
1615                         l = x;
1616                 }
1617                 
1618         }
1619         
1620         return l;
1621 }
1622
1623 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1624 {
1625         int bucket = yaffs_FindNiceObjectBucket(dev);
1626         
1627         // Now find an object value that has not already been taken
1628         // by scanning the list.
1629         
1630         int found = 0;
1631         struct list_head *i;
1632         
1633         __u32 n = (__u32)bucket;
1634
1635         //yaffs_CheckObjectHashSanity();        
1636         
1637         while(!found)
1638         {
1639                 found = 1;
1640                 n +=  YAFFS_NOBJECT_BUCKETS;
1641                 if(1 ||dev->objectBucket[bucket].count > 0)
1642                 {
1643                         list_for_each(i,&dev->objectBucket[bucket].list)
1644                         {
1645                                 // If there is already one in the list
1646                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1647                                 {
1648                                         found = 0;
1649                                 }
1650                         }
1651                 }
1652         }
1653         
1654         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1655         
1656         return n;       
1657 }
1658
1659 void yaffs_HashObject(yaffs_Object *in)
1660 {
1661         int bucket = yaffs_HashFunction(in->objectId);
1662         yaffs_Device *dev = in->myDev;
1663         
1664         if(!list_empty(&in->hashLink))
1665         {
1666                 //YINFO("!!!");
1667         }
1668
1669         
1670         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1671         dev->objectBucket[bucket].count++;
1672
1673 }
1674
1675 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1676 {
1677         int bucket = yaffs_HashFunction(number);
1678         struct list_head *i;
1679         yaffs_Object *in;
1680         
1681         list_for_each(i,&dev->objectBucket[bucket].list)
1682         {
1683                 // Look if it is in the list
1684                 if(i)
1685                 {
1686                         in = list_entry(i, yaffs_Object,hashLink);
1687                         if(in->objectId == number)
1688                         {
1689                                 return in;
1690                         }
1691                 }
1692         }
1693         
1694         return NULL;
1695 }
1696
1697
1698
1699 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1700 {
1701                 
1702         yaffs_Object *theObject;
1703
1704         if(number < 0)
1705         {
1706                 number = yaffs_CreateNewObjectNumber(dev);
1707         }
1708         
1709         theObject = yaffs_AllocateEmptyObject(dev);
1710         
1711         if(theObject)
1712         {
1713                 theObject->fake = 0;
1714                 theObject->renameAllowed = 1;
1715                 theObject->unlinkAllowed = 1;
1716                 theObject->objectId = number;
1717                 yaffs_HashObject(theObject);
1718                 theObject->variantType = type;
1719 #ifdef CONFIG_YAFFS_WINCE
1720                 yfsd_WinFileTimeNow(theObject->win_atime);
1721                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1722                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1723
1724 #else
1725
1726 #if defined(CONFIG_KERNEL_2_5)
1727                 theObject->st_atime = theObject->st_mtime =     theObject->st_ctime = CURRENT_TIME.tv_sec;              
1728 #else
1729                 theObject->st_atime = theObject->st_mtime =     theObject->st_ctime = CURRENT_TIME;             
1730 #endif
1731 #endif
1732                 switch(type)
1733                 {
1734                         case YAFFS_OBJECT_TYPE_FILE: 
1735                                 theObject->variant.fileVariant.fileSize = 0;
1736                                 theObject->variant.fileVariant.scannedFileSize = 0;
1737                                 theObject->variant.fileVariant.topLevel = 0;
1738                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1739                                 break;
1740                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1741                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1742                                 break;
1743                         case YAFFS_OBJECT_TYPE_SYMLINK:
1744                                 // No action required
1745                                 break;
1746                         case YAFFS_OBJECT_TYPE_HARDLINK:
1747                                 // No action required
1748                                 break;
1749                         case YAFFS_OBJECT_TYPE_SPECIAL:
1750                                 // No action required
1751                                 break;
1752                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1753                                 // todo this should not happen
1754                                 break;
1755                 }
1756         }
1757         
1758         return theObject;
1759 }
1760
1761 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1762 {
1763         yaffs_Object *theObject = NULL;
1764         
1765         if(number > 0)
1766         {
1767                 theObject = yaffs_FindObjectByNumber(dev,number);
1768         }
1769         
1770         if(!theObject)
1771         {
1772                 theObject = yaffs_CreateNewObject(dev,number,type);
1773         }
1774         
1775         return theObject;
1776
1777 }
1778
1779 YCHAR *yaffs_CloneString(const YCHAR *str)
1780 {
1781         YCHAR *newStr = NULL;
1782         
1783         if(str && *str)
1784         {
1785                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1786                 yaffs_strcpy(newStr,str);
1787         }
1788
1789         return newStr;
1790         
1791 }
1792
1793 //
1794 // Mknod (create) a new object.
1795 // equivalentObject only has meaning for a hard link;
1796 // aliasString only has meaning for a sumlink.
1797 // rdev only has meaning for devices (a subset of special objects)
1798 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1799                                                                  yaffs_Object *parent,
1800                                                                  const YCHAR *name, 
1801                                                                  __u32 mode,
1802                                                                  __u32 uid,
1803                                                                  __u32 gid,
1804                                                                  yaffs_Object *equivalentObject,
1805                                                                  const YCHAR *aliasString,
1806                                                                  __u32 rdev)
1807 {
1808         yaffs_Object *in;
1809
1810         yaffs_Device *dev = parent->myDev;
1811         
1812         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1813         if(yaffs_FindObjectByName(parent,name))
1814         {
1815                 return NULL;
1816         }
1817         
1818         in = yaffs_CreateNewObject(dev,-1,type);
1819         
1820         if(in)
1821         {
1822                 in->chunkId = -1;
1823                 in->valid = 1;
1824                 in->variantType = type;
1825
1826                 in->st_mode  = mode;
1827                 
1828 #ifdef CONFIG_YAFFS_WINCE
1829                 yfsd_WinFileTimeNow(in->win_atime);
1830                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1831                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1832                 
1833 #else
1834 #if defined(CONFIG_KERNEL_2_5)
1835                 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME.tv_sec;
1836 #else
1837                 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
1838 #endif
1839                 in->st_rdev  = rdev;
1840                 in->st_uid   = uid;
1841                 in->st_gid   = gid;
1842 #endif          
1843                 in->nDataChunks = 0;
1844
1845                 yaffs_SetObjectName(in,name);
1846                 in->dirty = 1;
1847                 
1848                 yaffs_AddObjectToDirectory(parent,in);
1849                 
1850                 in->myDev = parent->myDev;
1851                 
1852                                 
1853                 switch(type)
1854                 {
1855                         case YAFFS_OBJECT_TYPE_SYMLINK:
1856                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1857                                 break;
1858                         case YAFFS_OBJECT_TYPE_HARDLINK:
1859                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1860                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1861                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1862                                 break;
1863                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1864                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1865                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1866                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1867                                 break;
1868                 }
1869
1870                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1871                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1872                 {
1873                         // Could not create the object header, fail the creation
1874                         yaffs_DestroyObject(in);
1875                         in = NULL;
1876                 }
1877
1878         }
1879         
1880         return in;
1881 }
1882
1883 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1884 {
1885         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1886 }
1887
1888 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1889 {
1890         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1891 }
1892
1893 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1894 {
1895         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1896 }
1897
1898 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1899 {
1900         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1901 }
1902
1903 // NB yaffs_Link returns the object id of the equivalent object.
1904 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1905 {
1906         // Get the real object in case we were fed a hard link as an equivalent object
1907         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1908         
1909         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1910         {
1911                 return equivalentObject;
1912         }
1913         else
1914         {
1915                 return NULL;
1916         }
1917         
1918 }
1919
1920
1921 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1922 {
1923         int unlinkOp;
1924         int deleteOp;
1925
1926         if(newDir == NULL)
1927         {
1928                 newDir = obj->parent; // use the old directory
1929         }
1930
1931         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1932         if(obj->myDev->isYaffs2)
1933         {
1934                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1935         }
1936         else
1937         {
1938                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1939         }
1940
1941         deleteOp = (newDir == obj->myDev->deletedDir);
1942         
1943         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1944         // duplicate names are allowed.
1945         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1946         if( (unlinkOp|| 
1947                  deleteOp ||
1948                  force || 
1949                  !yaffs_FindObjectByName(newDir,newName))  &&
1950              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1951         {
1952                 yaffs_SetObjectName(obj,newName);
1953                 obj->dirty = 1;
1954                 
1955                 yaffs_AddObjectToDirectory(newDir,obj);
1956                 
1957                 if(unlinkOp) obj->unlinked = 1;
1958                 
1959                 // If it is a dletion then we mark it as a shrink for gc purposes.
1960                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1961                 {
1962                         return YAFFS_OK;
1963                 }
1964         }
1965         
1966         return YAFFS_FAIL;
1967 }
1968
1969
1970
1971 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1972 {
1973         yaffs_Object *obj;
1974         int force = 0;
1975         
1976 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1977         // Special case for WinCE.
1978         // While look-up is case insensitive, the name isn't.
1979         // THerefore we might want to change x.txt to X.txt
1980         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1981         {
1982                 force = 1;
1983         }       
1984 #endif
1985         
1986         obj = yaffs_FindObjectByName(oldDir,oldName);
1987         if(obj && obj->renameAllowed)
1988         {
1989                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1990         }
1991         return YAFFS_FAIL;
1992 }
1993
1994
1995
1996 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1997 {
1998         // Scan the buckets and check that the lists 
1999         // have as many members as the count says there are
2000         int bucket;
2001         int countEm;
2002         struct list_head *j;
2003         int ok = YAFFS_OK;
2004         
2005         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2006         {
2007                 countEm = 0;
2008                 
2009                 list_for_each(j,&dev->objectBucket[bucket].list)
2010                 {
2011                         countEm++;
2012                 }
2013                 
2014                 if(countEm != dev->objectBucket[bucket].count)
2015                 {
2016                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2017                         ok = YAFFS_FAIL;
2018                 }
2019         }
2020
2021         return ok;
2022 }
2023
2024 #if 0
2025 void yaffs_ObjectTest(yaffs_Device *dev)
2026 {
2027         yaffs_Object *in[1000];
2028         int inNo[1000];
2029         yaffs_Object *inold[1000];
2030         int i;
2031         int j;
2032         
2033         memset(in,0,1000*sizeof(yaffs_Object *));
2034         memset(inold,0,1000*sizeof(yaffs_Object *));
2035         
2036         yaffs_CheckObjectHashSanity(dev);
2037         
2038         for(j = 0; j < 10; j++)
2039         {
2040                 //T(("%d\n",j));
2041                 
2042                 for(i = 0; i < 1000; i++)
2043                 {
2044                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2045                         if(!in[i])
2046                         {
2047                                 YINFO("No more inodes");
2048                         }
2049                         else
2050                         {
2051                                 inNo[i] = in[i]->objectId;
2052                         }
2053                 }
2054                 
2055                 for(i = 0; i < 1000; i++)
2056                 {
2057                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2058                         {
2059                                 //T(("Differnce in look up test\n"));
2060                         }
2061                         else
2062                         {
2063                                 // T(("Look up ok\n"));
2064                         }
2065                 }
2066                 
2067                 yaffs_CheckObjectHashSanity(dev);
2068         
2069                 for(i = 0; i < 1000; i+=3)
2070                 {
2071                         yaffs_FreeObject(in[i]);        
2072                         in[i] = NULL;
2073                 }
2074                 
2075         
2076                 yaffs_CheckObjectHashSanity(dev);
2077         }
2078                 
2079 }
2080
2081 #endif
2082
2083 /////////////////////////// Block Management and Page Allocation ///////////////////
2084
2085
2086 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2087 {
2088         dev->allocationBlock = -1; // force it to get a new one
2089         //Todo we're assuming the malloc will pass.
2090         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2091         // Set up dynamic blockinfo stuff.
2092         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2093         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2094         if(dev->blockInfo && dev->chunkBits)
2095         {
2096                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2097                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2098                 return YAFFS_OK;
2099         }
2100         
2101         return YAFFS_FAIL;
2102         
2103 }
2104
2105 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2106 {
2107         YFREE(dev->blockInfo);
2108         dev->blockInfo = NULL;
2109         YFREE(dev->chunkBits);
2110         dev->chunkBits = NULL;
2111 }
2112
2113
2114 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2115 {
2116         int i;
2117         __u32 seq;
2118         yaffs_BlockInfo *b;
2119         
2120         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2121         
2122         if(!bi->hasShrinkHeader) return 1; // can gc
2123
2124
2125         // Find the oldest dirty sequence number if we don't know it and save it
2126         // so we don't have to keep recomputing it.
2127         if(!dev->oldestDirtySequence)
2128         {
2129                 seq = dev->sequenceNumber;
2130
2131                 for(i = dev->startBlock; i <= dev->endBlock; i++)
2132                 {
2133                         b = yaffs_GetBlockInfo(dev,i);
2134                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2135                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2136                         b->sequenceNumber < seq)
2137                         {
2138                                 seq = b->sequenceNumber;
2139                         }
2140                 }
2141                 dev->oldestDirtySequence = seq;
2142         }
2143
2144
2145         // Can't do gc of this block if there are any blocks older than this one that have
2146         // discarded pages.
2147         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2148         
2149         
2150         return 1;
2151
2152 }
2153
2154 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2155 // for garbage collection.
2156 //
2157
2158
2159
2160 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2161 {
2162
2163         int b = dev->currentDirtyChecker;
2164         
2165         int i;
2166         int iterations;
2167         int dirtiest = -1;
2168         int pagesInUse; 
2169         yaffs_BlockInfo *bi;
2170         static int  nonAggressiveSkip = 0;
2171
2172         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2173         // search harder.
2174         // else (we're doing a leasurely gc), then we only bother to do this if the
2175         // block has only a few pages in use.
2176         
2177
2178         nonAggressiveSkip--;
2179
2180         if(!aggressive &&(nonAggressiveSkip > 0))
2181         {
2182                 return -1;
2183         }
2184
2185         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2186         if(aggressive)
2187         {
2188                 iterations = dev->endBlock - dev->startBlock + 1;
2189         }
2190         else
2191         {
2192                 iterations = dev->endBlock - dev->startBlock + 1;
2193                 iterations = iterations / 16; 
2194                 if(iterations > 200)
2195                 {
2196                         iterations = 200;
2197                 }
2198         }
2199         
2200         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2201         {
2202                 b++;
2203                 if ( b < dev->startBlock || b > dev->endBlock)
2204                 {
2205                         b =  dev->startBlock;
2206                 }
2207
2208                 if(b < dev->startBlock || b > dev->endBlock)
2209                 {
2210                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2211                         YBUG();
2212                 }
2213                 
2214                 bi = yaffs_GetBlockInfo(dev,b);
2215                 
2216                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2217                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2218                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2219                 {
2220                         dirtiest = b;
2221                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2222                 }
2223         }
2224         
2225         dev->currentDirtyChecker = b;
2226         
2227         if(dirtiest > 0)
2228         {
2229                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2230         }
2231         
2232         dev->oldestDirtySequence = 0; // clear this
2233         
2234         if(dirtiest > 0)
2235         {
2236                 nonAggressiveSkip = 4;
2237         }
2238
2239         return dirtiest;
2240 }
2241
2242
2243 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2244 {
2245         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2246         
2247         int erasedOk = 0;
2248         
2249         // If the block is still healthy erase it and mark as clean.
2250         // If the block has had a data failure, then retire it.
2251         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2252
2253         if(!bi->needsRetiring)
2254         {
2255                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2256                 if(!erasedOk)
2257                 {
2258                         dev->nErasureFailures++;
2259                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2260                 }
2261         }
2262
2263         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2264         {
2265                         int i;
2266                         for(i = 0; i < dev->nChunksPerBlock; i++)
2267                         {
2268                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2269                                         {
2270                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2271                                         }
2272                         }
2273         }
2274         
2275         if( erasedOk )
2276         {
2277                 // Clean it up...
2278                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2279                 dev->nErasedBlocks++;
2280                 bi->pagesInUse = 0;
2281                 bi->softDeletions = 0;
2282                 bi->hasShrinkHeader=0;
2283                 yaffs_ClearChunkBits(dev,blockNo);
2284         
2285                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2286         }
2287         else
2288         {
2289                 yaffs_RetireBlock(dev,blockNo);
2290                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2291         }
2292 }
2293
2294 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2295 {
2296         int i,j;
2297         yaffs_BlockInfo *bi;
2298         
2299         for(i= dev->startBlock; i <=dev->endBlock; i++)
2300         {
2301                 bi = yaffs_GetBlockInfo(dev,i);
2302                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2303                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2304                 
2305                 for(j = 0; j < dev->nChunksPerBlock; j++)
2306                 {
2307                         if(yaffs_CheckChunkBit(dev,i,j))
2308                         {
2309                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2310
2311                         }
2312                 }
2313                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2314
2315         }
2316 }
2317
2318
2319 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2320 {
2321         int i;
2322         
2323         yaffs_BlockInfo *bi;
2324         
2325 #if 0
2326         static int j = 0;
2327         j++;
2328         if(j < 0 || j > 100)
2329         {
2330                 j = 0;
2331                 yaffs_DumpBlockStats(dev);
2332         }
2333         
2334 #endif
2335         
2336         if(dev->nErasedBlocks < 1)
2337         {
2338                 // Hoosterman we've got a problem.
2339                 // Can't get space to gc
2340                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2341
2342                 return -1;
2343         }
2344         
2345         // Find an empty block.
2346         
2347         for(i = dev->startBlock; i <= dev->endBlock; i++)
2348         {
2349                 dev->allocationBlockFinder++;
2350                 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock) 
2351                 {
2352                         dev->allocationBlockFinder = dev->startBlock;
2353                 }
2354                 
2355                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2356
2357                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2358                 {
2359                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2360                         dev->sequenceNumber++;
2361                         bi->sequenceNumber = dev->sequenceNumber;
2362                         dev->nErasedBlocks--;           
2363                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber));   
2364                         return dev->allocationBlockFinder;
2365                 }
2366         }
2367                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been one" TENDSTR)));
2368
2369         
2370         return -1;      
2371 }
2372
2373
2374
2375 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2376 {
2377         int retVal;
2378         yaffs_BlockInfo *bi;
2379         
2380         if(dev->allocationBlock < 0)
2381         {
2382                 // Get next block to allocate off
2383                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2384                 dev->allocationPage = 0;
2385         }
2386         
2387         if(!useReserve &&  dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2388         {
2389                 // Not enough space to allocate unless we're allowed to use the reserve.
2390                 return -1;
2391         }
2392
2393         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2394         {
2395                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2396         }
2397
2398         
2399         // Next page please....
2400         if(dev->allocationBlock >= 0)
2401         {
2402                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2403                 
2404                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2405                                   dev->allocationPage;
2406                 bi->pagesInUse++;
2407                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2408
2409                 dev->allocationPage++;
2410                 
2411                 dev->nFreeChunks--;
2412                 
2413                 // If the block is full set the state to full
2414                 if(dev->allocationPage >= dev->nChunksPerBlock)
2415                 {
2416                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2417                         dev->allocationBlock = -1;
2418                 }
2419
2420
2421                 return retVal;
2422                 
2423         }
2424         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2425
2426         return -1;      
2427 }
2428
2429
2430 // To determine if we have enough space we just look at the 
2431 // number of erased blocks.
2432 // The cache is allowed to use reserved blocks.
2433
2434 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2435 {
2436         return (dev->nErasedBlocks >= dev->nReservedBlocks);
2437 }
2438
2439
2440 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2441 {
2442                 int n;
2443
2444                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2445
2446                 if(dev->allocationBlock> 0)
2447                 {
2448                         n += (dev->nChunksPerBlock - dev->allocationPage);
2449                 }
2450
2451                 return n;
2452
2453 }
2454
2455 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2456 {
2457         int oldChunk;
2458         int newChunk;
2459         int chunkInBlock;
2460         int markNAND;
2461         int retVal = YAFFS_OK;
2462         int cleanups = 0;
2463         int i;
2464
2465         int chunksBefore = yaffs_GetErasedChunks(dev);
2466         int chunksAfter;
2467
2468         yaffs_ExtendedTags  tags;
2469         
2470         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2471         
2472         yaffs_Object *object;
2473         
2474         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2475
2476         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2477         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2478
2479         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2480
2481
2482         if(!yaffs_StillSomeChunkBits(dev,block))
2483         {
2484                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2485                 yaffs_BlockBecameDirty(dev,block);
2486         }
2487         else
2488         {
2489
2490                         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2491
2492         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2493             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2494             chunkInBlock++, oldChunk++ )
2495         {
2496                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2497                 {
2498                         
2499                         // This page is in use and might need to be copied off
2500                         
2501                         markNAND = 1;
2502                         
2503                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2504                         
2505                         yaffs_InitialiseTags(&tags);
2506                         
2507                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2508
2509                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2510                         
2511                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2512                         
2513                         if(!object)
2514                         {
2515                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2516                         }
2517                         
2518                         if(object && object->deleted && tags.chunkId != 0)
2519                         {
2520                                 // Data chunk in a deleted file, throw it away
2521                                 // It's a deleted data chunk,
2522                                 // No need to copy this, just forget about it and fix up the
2523                                 // object.
2524                                 
2525                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2526                                 object->nDataChunks--;
2527                                 
2528                                 if(object->nDataChunks <= 0)
2529                                 {
2530                                         // remeber to clean up the object
2531                                         dev->gcCleanupList[cleanups] = tags.objectId;
2532                                         cleanups++;
2533                                 }
2534                                 markNAND = 0;
2535                         }
2536                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2537                         {
2538                                 // Deleted object header with no data chunks.
2539                                 // Can be discarded and the file deleted.
2540                                 object->chunkId = 0;
2541                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2542                                 object->variant.fileVariant.top = NULL;
2543                                 yaffs_DoGenericObjectDeletion(object);
2544                                 
2545                         }
2546                         else if(object)
2547                         {
2548                                 // It's either a data chunk in a live file or
2549                                 // an ObjectHeader, so we're interested in it.
2550                                 // NB Need to keep the ObjectHeaders of deleted files
2551                                 // until the whole file has been deleted off
2552                                 tags.serialNumber++;
2553
2554                                 dev->nGCCopies++;
2555
2556                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2557                         
2558                                 if(newChunk < 0)
2559                                 {
2560                                         retVal =  YAFFS_FAIL;
2561                                 }
2562                                 else
2563                                 {
2564                         
2565                                         // Ok, now fix up the Tnodes etc.
2566                         
2567                                         if(tags.chunkId == 0)
2568                                         {
2569                                                 // It's a header
2570                                                 object->chunkId = newChunk;
2571                                                 object->serial = tags.serialNumber;
2572                                         }
2573                                         else
2574                                         {
2575                                                 // It's a data chunk
2576                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2577                                         }
2578                                 }
2579                         }
2580                         
2581                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2582                         
2583                 }
2584         }
2585
2586         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2587
2588
2589         // Do any required cleanups
2590         for(i = 0; i < cleanups; i++)
2591         {                                               
2592                 // Time to delete the file too
2593                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2594                 if(object)
2595                 {
2596                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2597                         object->variant.fileVariant.top = NULL;
2598                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2599                         yaffs_DoGenericObjectDeletion(object);
2600                 }
2601
2602         }
2603
2604         }
2605
2606         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2607         {
2608                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2609         }
2610                         
2611         return YAFFS_OK;
2612 }
2613
2614 #if 0
2615 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2616 {
2617         // find a file to delete
2618         struct list_head *i;    
2619         yaffs_Object *l;
2620
2621
2622         //Scan the unlinked files looking for one to delete
2623         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2624         {
2625                 if(i)
2626                 {
2627                         l = list_entry(i, yaffs_Object,siblings);
2628                         if(l->deleted)
2629                         {
2630                                 return l;                       
2631                         }
2632                 }
2633         }       
2634         return NULL;
2635 }
2636
2637
2638 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2639 {
2640         // This does background deletion on unlinked files.. only deleted ones.
2641         // If we don't have a file we're working on then find one
2642         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2643         {
2644                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2645         }
2646         
2647         // OK, we're working on a file...
2648         if(dev->unlinkedDeletion)
2649         {
2650                 yaffs_Object *obj = dev->unlinkedDeletion;
2651                 int delresult;
2652                 int limit; // Number of chunks to delete in a file.
2653                                    // NB this can be exceeded, but not by much.
2654                                    
2655                 limit = -1;
2656
2657                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2658                 
2659                 if(obj->nDataChunks == 0)
2660                 {
2661                         // Done all the deleting of data chunks.
2662                         // Now dump the header and clean up
2663                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2664                         obj->variant.fileVariant.top = NULL;
2665                         yaffs_DoGenericObjectDeletion(obj);
2666                         dev->nDeletedFiles--;
2667                         dev->nUnlinkedFiles--;
2668                         dev->nBackgroundDeletions++;
2669                         dev->unlinkedDeletion = NULL;   
2670                 }
2671         }
2672 }
2673
2674 #endif
2675
2676 #if 0
2677 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2678 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2679 {
2680         int block;
2681         int aggressive=0;
2682         
2683         //yaffs_DoUnlinkedFileDeletion(dev);
2684         
2685         if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2686         {
2687                 aggressive = 1;
2688         }               
2689         
2690         if(aggressive)
2691         {
2692                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2693                 
2694                 if(block >= 0)
2695                 {
2696                         dev->garbageCollections++;
2697                         return yaffs_GarbageCollectBlock(dev,block);
2698                 }       
2699                 else
2700                 {
2701                         return YAFFS_FAIL;
2702                 }
2703         }
2704
2705         return YAFFS_OK;
2706 }
2707 #endif
2708
2709 // New garbage collector
2710 // If we're very low on erased blocks then we do aggressive garbage collection
2711 // otherwise we do "leasurely" garbage collection.
2712 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2713 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2714 //
2715 // The idea is to help clear out space in a more spread-out manner.
2716 // Dunno if it really does anything useful.
2717 //
2718 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2719 {
2720         int block;
2721         int aggressive; 
2722         int gcOk;
2723         int maxTries = 0;
2724         
2725         //yaffs_DoUnlinkedFileDeletion(dev);
2726
2727         // This loop should pass the first time.
2728         // We'll only see looping here if the erase of the collected block fails.
2729         
2730         do{
2731                 maxTries++;
2732                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2733                 {
2734                         // We need a block soon...
2735                         aggressive = 1;
2736                 }
2737                 else 
2738                 {
2739                         // We're in no hurry
2740                         aggressive = 0;
2741                 }
2742         
2743                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2744         
2745                 if(block > 0)
2746                 {
2747                         dev->garbageCollections++;
2748                         if(!aggressive)
2749                         {
2750                                 dev->passiveGarbageCollections++;
2751                         }
2752
2753                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2754
2755                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2756                 }
2757
2758                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) 
2759                 {
2760                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2761                 }
2762         } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2763
2764         return aggressive ? gcOk: YAFFS_OK;
2765 }
2766
2767
2768 //////////////////////////// TAGS ///////////////////////////////////////
2769
2770
2771 void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
2772 {
2773         memset(tags,0,sizeof(yaffs_ExtendedTags));
2774         tags->validMarker0 = 0xAAAAAAAA;
2775         tags->validMarker1 = 0x55555555;
2776 }
2777
2778 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
2779 {
2780         return (tags->validMarker0 == 0xAAAAAAAA &&     tags->validMarker1 == 0x55555555);
2781
2782 }
2783
2784
2785 #if 0
2786
2787 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2788 {
2789         // Calculate an ecc
2790         
2791         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2792         unsigned  i,j;
2793         unsigned  ecc = 0;
2794         unsigned bit = 0;
2795
2796         tags->ecc = 0;
2797         
2798         for(i = 0; i < 8; i++)
2799         {
2800                 for(j = 1; j &0xff; j<<=1)
2801                 {
2802                         bit++;
2803                         if(b[i] & j)
2804                         {
2805                                 ecc ^= bit;
2806                         }
2807                 }
2808         }
2809         
2810         tags->ecc = ecc;
2811         
2812         
2813 }
2814
2815 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2816 {
2817         unsigned ecc = tags->ecc;
2818         
2819         yaffs_CalcTagsECC(tags);
2820         
2821         ecc ^= tags->ecc;
2822         
2823         if(ecc && ecc <= 64)
2824         {
2825                 // TODO: Handle the failure better. Retire?
2826                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2827
2828                 ecc--;
2829                                 
2830                 b[ecc / 8] ^= (1 << (ecc & 7));
2831                 
2832                 // Now recvalc the ecc
2833                 yaffs_CalcTagsECC(tags);
2834                 
2835                 return 1; // recovered error
2836         }
2837         else if(ecc)
2838         {
2839                 // Wierd ecc failure value
2840                 // TODO Need to do somethiong here
2841                 return -1; //unrecovered error
2842         }
2843         
2844         return 0;
2845 }
2846
2847 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2848 {
2849         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2850         
2851         yaffs_CalcTagsECC(tagsPtr);
2852         
2853         sparePtr->tagByte0 = tu->asBytes[0];
2854         sparePtr->tagByte1 = tu->asBytes[1];
2855         sparePtr->tagByte2 = tu->asBytes[2];
2856         sparePtr->tagByte3 = tu->asBytes[3];
2857         sparePtr->tagByte4 = tu->asBytes[4];
2858         sparePtr->tagByte5 = tu->asBytes[5];
2859         sparePtr->tagByte6 = tu->asBytes[6];
2860         sparePtr->tagByte7 = tu->asBytes[7];
2861 }
2862
2863 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2864 {
2865         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2866         int result;
2867
2868         tu->asBytes[0]= sparePtr->tagByte0;
2869         tu->asBytes[1]= sparePtr->tagByte1;
2870         tu->asBytes[2]= sparePtr->tagByte2;
2871         tu->asBytes[3]= sparePtr->tagByte3;
2872         tu->asBytes[4]= sparePtr->tagByte4;
2873         tu->asBytes[5]= sparePtr->tagByte5;
2874         tu->asBytes[6]= sparePtr->tagByte6;
2875         tu->asBytes[7]= sparePtr->tagByte7;
2876         
2877         result =  yaffs_CheckECCOnTags(tagsPtr);
2878         if(result> 0)
2879         {
2880                 dev->tagsEccFixed++;
2881         }
2882         else if(result <0)
2883         {
2884                 dev->tagsEccUnfixed++;
2885         }
2886 }
2887
2888 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2889 {
2890         memset(spare,0xFF,sizeof(yaffs_Spare));
2891 }
2892
2893 #endif
2894
2895 #if 0
2896 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2897 {
2898         // NB There must be tags, data is optional
2899         // If there is data, then an ECC is calculated on it.
2900         
2901         yaffs_Spare spare;
2902         
2903         if(!tags)
2904         {
2905                 return YAFFS_FAIL;
2906         }
2907         
2908         //yaffs_SpareInitialise(&spare);
2909         
2910         //if(!dev->useNANDECC && buffer)
2911         //{
2912         //      yaffs_CalcECC(buffer,&spare);
2913         //}
2914         
2915         //yaffs_LoadTagsIntoSpare(&spare,tags);
2916         
2917         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2918         
2919 }
2920 #endif
2921
2922 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2923 {
2924         return  (  tags->chunkId == chunkInObject &&
2925                            tags->objectId == objectId &&
2926                            !tags->chunkDeleted) ? 1 : 0;
2927         
2928 }
2929
2930
2931
2932 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2933 {
2934         //Get the Tnode, then get the level 0 offset chunk offset
2935     yaffs_Tnode *tn;     
2936     int theChunk = -1;
2937     yaffs_ExtendedTags localTags;
2938     int retVal = -1;
2939     
2940     yaffs_Device *dev = in->myDev;
2941     
2942     
2943     if(!tags)
2944     {
2945         // Passed a NULL, so use our own tags space
2946         tags = &localTags;
2947     }
2948     
2949     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2950     
2951     if(tn)
2952     {
2953                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2954                 
2955                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2956     }
2957     return retVal;
2958 }
2959
2960 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2961 {
2962         //Get the Tnode, then get the level 0 offset chunk offset
2963     yaffs_Tnode *tn;     
2964     int theChunk = -1;
2965     yaffs_ExtendedTags localTags;
2966
2967     yaffs_Device *dev = in->myDev;
2968     int retVal = -1;
2969     
2970     if(!tags)
2971     {
2972         // Passed a NULL, so use our own tags space
2973         tags = &localTags;
2974     }
2975     
2976     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2977     
2978     if(tn)
2979     {
2980     
2981                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2982                 
2983                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2984     
2985                 // Delete the entry in the filestructure (if found)
2986                 if(retVal != -1)
2987                 {
2988                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2989                 }
2990     }
2991     else
2992     {
2993         //T(("No level 0 found for %d\n", chunkInInode));
2994     }
2995     
2996     if(retVal == -1)
2997     {
2998         //T(("Could not find %d to delete\n",chunkInInode));
2999     }
3000     return retVal;
3001 }
3002
3003
3004 #ifdef YAFFS_PARANOID
3005
3006 static int yaffs_CheckFileSanity(yaffs_Object *in)
3007 {
3008         int chunk;
3009         int nChunks;
3010         int fSize;
3011         int failed = 0;
3012         int objId;
3013         yaffs_Tnode *tn;
3014     yaffs_Tags localTags;
3015     yaffs_Tags *tags = &localTags;
3016     int theChunk;
3017     int chunkDeleted;
3018     
3019         
3020         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3021         {
3022                 //T(("Object not a file\n"));
3023                 return YAFFS_FAIL;
3024         }
3025         
3026         objId = in->objectId;
3027         fSize  = in->variant.fileVariant.fileSize;
3028         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3029         
3030         for(chunk = 1; chunk <= nChunks; chunk++)
3031         {
3032                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3033     
3034                 if(tn)
3035                 {
3036     
3037                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3038                         
3039                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3040                         {
3041
3042
3043                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3044                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3045                                 {
3046                                         // found it;
3047                                 
3048                                 }
3049                         }
3050                         else
3051                         {
3052                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
3053                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
3054                                                 
3055                                 failed = 1;
3056                                                         
3057                         }
3058     
3059                 }
3060                 else
3061                 {
3062                         //T(("No level 0 found for %d\n", chunk));
3063                 }
3064         }
3065         
3066         return failed ? YAFFS_FAIL : YAFFS_OK;
3067 }
3068
3069 #endif
3070
3071 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3072 {
3073         yaffs_Tnode *tn;
3074         yaffs_Device *dev = in->myDev;
3075         int existingChunk;
3076         yaffs_ExtendedTags existingTags;
3077         yaffs_ExtendedTags newTags;
3078         unsigned existingSerial, newSerial;
3079                 
3080         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3081         if(!tn)
3082         {
3083                 return YAFFS_FAIL;
3084         }
3085
3086         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
3087         
3088         if(inScan)
3089         {
3090                 // If we're scanning then we need to test for duplicates
3091                 // NB This does not need to be efficient since it should only ever 
3092                 // happen when the power fails during a write, then only one
3093                 // chunk should ever be affected.
3094         
3095                 
3096                 if(existingChunk != 0)
3097                 {
3098                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3099                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3100                         //
3101                         // We have a duplicate now we need to decide which one to use
3102                         // To do this we get both sets of tags and compare serial numbers.
3103                         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3104                         
3105                         
3106                         // Do a proper find
3107                         existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3108
3109                         if(existingChunk <=0)
3110                         {
3111                                 //Hoosterman - how did this happen?
3112                                 
3113                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3114
3115                         }
3116
3117                         
3118                         // NB The deleted flags should be false, otherwise the chunks will 
3119                         // not be loaded during a scan
3120                         
3121                         newSerial = newTags.serialNumber;
3122                         existingSerial = existingTags.serialNumber;
3123                         
3124                         if( in->myDev->isYaffs2 ||
3125                             existingChunk <= 0 ||
3126                             ((existingSerial+1) & 3) == newSerial)
3127                         {
3128                                 // Use new
3129                                 // Delete the old one and drop through to update the tnode
3130                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3131                         }
3132                         else
3133                         {
3134                                 // Use existing.
3135                                 // Delete the new one and return early so that the tnode isn't changed
3136                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3137                                 return YAFFS_OK;
3138                         }
3139                 }
3140
3141         }
3142                 
3143         if(existingChunk == 0)
3144         {
3145                 in->nDataChunks++;
3146         }
3147         
3148         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3149         
3150         return YAFFS_OK;
3151 }
3152
3153
3154
3155 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3156 {
3157     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3158     
3159     if(chunkInNAND >= 0)
3160     {
3161                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3162         }
3163         else
3164         {
3165                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3166
3167                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3168                 return 0;
3169         }
3170
3171 }
3172
3173
3174 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3175 {
3176         int block;
3177         int page;
3178         yaffs_ExtendedTags tags;
3179         yaffs_BlockInfo *bi;
3180         
3181         if(chunkId <= 0) return;        
3182         
3183         dev->nDeletions++;
3184         block = chunkId / dev->nChunksPerBlock;
3185         page = chunkId % dev->nChunksPerBlock;
3186         
3187         bi = yaffs_GetBlockInfo(dev,block);
3188         
3189         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3190         
3191         if(markNAND && 
3192           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3193           !dev->isYaffs2)
3194         {
3195 //              yaffs_SpareInitialise(&spare);
3196
3197 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3198
3199                 //read data before write, to ensure correct ecc 
3200                 //if we're using MTD verification under Linux
3201                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3202 #endif
3203
3204                 yaffs_InitialiseTags(&tags);
3205
3206                 tags.chunkDeleted = 1;
3207
3208         
3209                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3210                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3211         }
3212         else
3213         {
3214                         dev->nUnmarkedDeletions++;
3215         }       
3216         
3217         
3218         // Pull out of the management area.
3219         // If the whole block became dirty, this will kick off an erasure.
3220         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3221             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3222             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3223             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3224         {
3225                 dev->nFreeChunks++;
3226
3227                 yaffs_ClearChunkBit(dev,block,page);
3228                 bi->pagesInUse--;
3229                 
3230                 if(bi->pagesInUse == 0 &&
3231                    !bi->hasShrinkHeader &&
3232                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3233                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3234             {
3235                 yaffs_BlockBecameDirty(dev,block);
3236             }
3237
3238         }
3239         else
3240         {
3241                 // T(("Bad news deleting chunk %d\n",chunkId));
3242         }
3243         
3244 }
3245
3246
3247
3248
3249 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3250 {
3251         // Find old chunk Need to do this to get serial number
3252         // Write new one and patch into tree.
3253         // Invalidate old tags.
3254
3255     int prevChunkId;
3256     yaffs_ExtendedTags prevTags;
3257     
3258     int newChunkId;
3259     yaffs_ExtendedTags newTags;
3260
3261     yaffs_Device *dev = in->myDev;    
3262
3263         yaffs_CheckGarbageCollection(dev);
3264
3265         // Get the previous chunk at this location in the file if it exists
3266     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3267     
3268     // Set up new tags
3269     yaffs_InitialiseTags(&newTags);
3270     
3271         newTags.chunkId = chunkInInode;
3272         newTags.objectId = in->objectId;
3273         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3274         newTags.byteCount = nBytes;
3275                 
3276 //      yaffs_CalcTagsECC(&newTags);
3277
3278         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3279         
3280         if(newChunkId >= 0)
3281         {
3282                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3283                 
3284                 
3285                 if(prevChunkId >= 0)
3286                 {
3287                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3288         
3289                 }
3290                 
3291                 yaffs_CheckFileSanity(in);
3292         }
3293         return newChunkId;
3294
3295
3296
3297
3298
3299 }
3300
3301
3302 // UpdateObjectHeader updates the header on NAND for an object.
3303 // If name is not NULL, then that new name is used.
3304 //
3305 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3306 {
3307
3308         yaffs_BlockInfo *bi;
3309         
3310         yaffs_Device *dev = in->myDev;
3311         
3312     int prevChunkId;
3313     int retVal = 0;
3314     
3315     int newChunkId;
3316     yaffs_ExtendedTags newTags;
3317     
3318     __u8 *buffer = NULL;
3319     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3320     
3321    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3322     
3323     yaffs_ObjectHeader *oh = NULL;
3324     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3325
3326     
3327     if(!in->fake || force)
3328     {
3329   
3330                 yaffs_CheckGarbageCollection(dev);              
3331     
3332             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3333             oh  = (yaffs_ObjectHeader *)buffer;
3334     
3335                 prevChunkId = in->chunkId;
3336     
3337                 if(prevChunkId >= 0)
3338                 {
3339                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3340                         memcpy(oldName,oh->name,sizeof(oh->name));      
3341                 }
3342
3343                 memset(buffer,0xFF,dev->nBytesPerChunk);
3344
3345                 // Header data
3346                 oh->type = in->variantType;
3347                 
3348                 oh->st_mode = in->st_mode;
3349
3350 #ifdef CONFIG_YAFFS_WINCE
3351                 oh->win_atime[0] = in->win_atime[0];
3352                 oh->win_ctime[0] = in->win_ctime[0];
3353                 oh->win_mtime[0] = in->win_mtime[0];
3354                 oh->win_atime[1] = in->win_atime[1];
3355                 oh->win_ctime[1] = in->win_ctime[1];
3356                 oh->win_mtime[1] = in->win_mtime[1];
3357 #else
3358                 oh->st_uid = in->st_uid;
3359                 oh->st_gid = in->st_gid;
3360                 oh->st_atime = in->st_atime;
3361                 oh->st_mtime = in->st_mtime;
3362                 oh->st_ctime = in->st_ctime;
3363                 oh->st_rdev = in->st_rdev;
3364 #endif  
3365                 if(in->parent)
3366                 {
3367                         oh->parentObjectId = in->parent->objectId;
3368                 }
3369                 else
3370                 {
3371                         oh->parentObjectId = 0;
3372                 }
3373                 
3374                 //oh->sum = in->sum;
3375                 if(name && *name)
3376                 {
3377                         memset(oh->name,0,sizeof(oh->name));
3378                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3379                 }
3380                 else if(prevChunkId)
3381                 {       
3382                         memcpy(oh->name, oldName,sizeof(oh->name));
3383                 }
3384                 else
3385                 {
3386                         memset(oh->name,0,sizeof(oh->name));    
3387                 }
3388                 
3389                 oh->isShrink = isShrink;
3390         
3391                 switch(in->variantType)
3392                 {
3393                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3394                                 // Should not happen
3395                                 break;
3396                         case YAFFS_OBJECT_TYPE_FILE:
3397                                 oh->fileSize = in->variant.fileVariant.fileSize;
3398                                 break;
3399                         case YAFFS_OBJECT_TYPE_HARDLINK:
3400                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3401                                 break;
3402                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3403                                 // Do nothing
3404                                 break;
3405                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3406                                 // Do nothing
3407                                 break;
3408                         case YAFFS_OBJECT_TYPE_SYMLINK:
3409                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3410                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3411                                 break;
3412                 }
3413
3414                 // Tags
3415                 yaffs_InitialiseTags(&newTags);
3416                 in->serial++;
3417                 newTags.chunkId = 0;
3418                 newTags.objectId = in->objectId;
3419                 newTags.serialNumber = in->serial;
3420         
3421
3422                 // Create new chunk in NAND
3423                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3424     
3425                 if(newChunkId >= 0)
3426                 {
3427                 
3428                         in->chunkId = newChunkId;       
3429                 
3430                         if(prevChunkId >= 0)
3431                         {
3432                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3433                         }
3434                 
3435                         in->dirty = 0;
3436                         
3437                         // If this was a shrink, then mark the block that the chunk lives on
3438                         if(isShrink)
3439                         {
3440                                 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3441                                 bi->hasShrinkHeader = 1;
3442                         }
3443                         
3444                 }
3445                 
3446                 retVal =  newChunkId;
3447
3448     }
3449     
3450         if(buffer) 
3451                 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3452         
3453     return retVal;
3454 }
3455
3456
3457 /////////////////////// Short Operations Cache ////////////////////////////////
3458 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3459 //      reads might be short sequential reads, and a lot of writes may be short 
3460 //  sequential writes. eg. scanning/writing a jpeg file.
3461 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3462 //  with dumb-as-a-rock code.
3463 //  There are a limited number (~10) of cache chunks per device so that we don't
3464 //  need a very intelligent search.
3465
3466
3467
3468
3469
3470 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3471 {
3472         yaffs_Device *dev = obj->myDev;
3473         int lowest;
3474         int i;
3475         yaffs_ChunkCache *cache;
3476         int chunkWritten;
3477         //int nBytes;
3478         int nCaches = obj->myDev->nShortOpCaches;
3479         
3480         if  (nCaches > 0)
3481         {
3482                 do{
3483                         cache = NULL;
3484                 
3485                         // Find the dirty cache for this object with the lowest chunk id.
3486                         for(i = 0; i < nCaches; i++)
3487                         {
3488                                 if(dev->srCache[i].object == obj &&
3489                                    dev->srCache[i].dirty)
3490                                 {
3491                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3492                                         {
3493                                                 cache = &dev->srCache[i];
3494                                                 lowest = cache->chunkId;
3495                                         }
3496                                 }
3497                         }
3498                 
3499                         if(cache && !cache->locked)
3500                         {
3501                                 //Write it out and free it up
3502
3503 #if 0
3504                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3505                         
3506                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3507                                 {
3508                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3509                                 }
3510 #endif                  
3511                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3512                                                                                                                         cache->chunkId,
3513                                                                                                                         cache->data,
3514                                                                                                                         cache->nBytes,1);
3515
3516                                 cache->dirty = 0;
3517                                 cache->object = NULL;
3518                         }
3519                 
3520                 } while(cache && chunkWritten > 0);
3521         
3522                 if(cache)
3523                 {
3524                         //Hoosterman, disk full while writing cache out.
3525                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3526
3527                 }
3528         }       
3529                 
3530 }
3531
3532
3533 // Grab us a chunk for use.
3534 // First look for an empty one. 
3535 // Then look for the least recently used non-dirty one.
3536 // Then look for the least recently used dirty one...., flush and look again.
3537 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3538 {
3539         int i;
3540         int usage;
3541         int theOne;
3542         
3543         if(dev->nShortOpCaches > 0)
3544         {
3545                 for(i = 0; i < dev->nShortOpCaches; i++)
3546                 {
3547                         if(!dev->srCache[i].object)
3548                         {
3549                                 //T(("Grabbing empty %d\n",i));
3550                                 
3551                                 //printf("Grabbing empty %d\n",i);
3552                         
3553                                 return &dev->srCache[i];
3554                         }
3555                 }
3556                 
3557                 return NULL;
3558         
3559                 theOne = -1; 
3560                 usage = 0; // just to stop the compiler grizzling
3561         
3562                 for(i = 0; i < dev->nShortOpCaches; i++)
3563                 {
3564                         if(!dev->srCache[i].dirty &&
3565                         ((dev->srCache[i].lastUse < usage  && theOne >= 0)|| 
3566                                 theOne < 0))
3567                         {
3568                                 usage = dev->srCache[i].lastUse;
3569                                 theOne = i;
3570                         }
3571                 }
3572         
3573                 //T(("Grabbing non-empty %d\n",theOne));
3574                 
3575                 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3576                 
3577                 return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
3578         }
3579         else
3580         {
3581                 return NULL;
3582         }
3583         
3584 }
3585
3586
3587 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3588 {
3589         yaffs_ChunkCache *cache;
3590         yaffs_Object *theObj;
3591         int usage;
3592         int i;
3593         int pushout;
3594         
3595         if(dev->nShortOpCaches > 0)
3596         {
3597                 // Try find a non-dirty one...
3598         
3599                 cache = yaffs_GrabChunkCacheWorker(dev);
3600         
3601                 if(!cache)
3602                 {
3603                         // They were all dirty, find the last recently used object and flush
3604                         // its cache, then  find again.
3605                         // NB what's here is not very accurate, we actually flush the object
3606                         // the last recently used page.
3607                 
3608                         theObj = NULL;
3609                         usage = 0;
3610                         cache = NULL;
3611                         pushout = 0;
3612         
3613                         for(i = 0; i < dev->nShortOpCaches; i++)
3614                         {
3615                                 if( dev->srCache[i].object && 
3616                                         !dev->srCache[i].locked &&
3617                                         (dev->srCache[i].lastUse < usage || !cache))
3618                                 {
3619                                         usage  = dev->srCache[i].lastUse;
3620                                         theObj = dev->srCache[i].object;
3621                                         cache = &dev->srCache[i];
3622                                         pushout = i;
3623                                 }
3624                         }
3625                 
3626                         if(!cache || cache->dirty)
3627                         {
3628                         
3629                                 //printf("Dirty ");
3630                                 yaffs_FlushFilesChunkCache(theObj);
3631                 
3632                                 // Try again
3633                                 cache = yaffs_GrabChunkCacheWorker(dev);
3634                         }
3635                         else
3636                         {
3637                                 //printf(" pushout %d\n",pushout);
3638                         }
3639                         
3640                 }
3641
3642                 return cache;
3643         }
3644         else
3645                 return NULL;
3646
3647 }
3648
3649
3650 // Find a cached chunk
3651 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3652 {
3653         yaffs_Device *dev = obj->myDev;
3654         int i;
3655         if(dev->nShortOpCaches > 0)
3656         {
3657                 for(i = 0; i < dev->nShortOpCaches; i++)
3658                 {
3659                         if(dev->srCache[i].object == obj && 
3660                         dev->srCache[i].chunkId == chunkId)
3661                         {
3662                                 dev->cacheHits++;
3663                         
3664                                 return &dev->srCache[i];
3665                         }
3666                 }
3667         }
3668         return NULL;
3669 }
3670
3671 // Mark the chunk for the least recently used algorithym
3672 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3673 {
3674
3675         if(dev->nShortOpCaches > 0)
3676         {
3677                 if( dev->srLastUse < 0 || 
3678                         dev->srLastUse > 100000000)
3679                 {
3680                         // Reset the cache usages
3681                         int i;
3682                         for(i = 1; i < dev->nShortOpCaches; i++)
3683                         {
3684                                 dev->srCache[i].lastUse = 0;
3685                         }
3686                         dev->srLastUse = 0;
3687                 }
3688
3689                 dev->srLastUse++;
3690         
3691                 cache->lastUse = dev->srLastUse;
3692
3693                 if(isAWrite)
3694                 {
3695                         cache->dirty = 1;
3696                 }
3697         }
3698 }
3699
3700 // Invalidate a single cache page.
3701 // Do this when a whole page gets written,
3702 // ie the short cache for this page is no longer valid.
3703 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3704 {
3705         if(object->myDev->nShortOpCaches > 0)
3706         {
3707                 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3708
3709                 if(cache)
3710                 {
3711                         cache->object = NULL;
3712                 }
3713         }
3714 }
3715
3716
3717 // Invalidate all the cache pages associated with this object
3718 // Do this whenever ther file is deleted or resized.
3719 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3720 {
3721         int i;
3722         yaffs_Device *dev = in->myDev;
3723         
3724         if(dev->nShortOpCaches > 0)
3725         { 
3726                 // Now invalidate it.
3727                 for(i = 0; i < dev->nShortOpCaches; i++)
3728                 {
3729                         if(dev->srCache[i].object == in)
3730                         {
3731                                 dev->srCache[i].object = NULL;
3732                         }
3733                 }
3734         }
3735 }
3736
3737
3738
3739
3740
3741 ///////////////////////// File read/write ///////////////////////////////
3742 // Read and write have very similar structures.
3743 // In general the read/write has three parts to it
3744 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3745 // * Some complete chunks
3746 // * An incomplete chunk to end off with
3747 //
3748 // Curve-balls: the first chunk might also be the last chunk.
3749
3750 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3751 {
3752         
3753         
3754         int chunk;
3755         int start;
3756         int nToCopy;
3757         int n = nBytes;
3758         int nDone = 0;
3759         yaffs_ChunkCache *cache;
3760         
3761         yaffs_Device *dev;
3762         
3763         dev = in->myDev;
3764         
3765         while(n > 0)
3766         {
3767                 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3768                 start = offset % dev->nBytesPerChunk;
3769
3770                 // OK now check for the curveball where the start and end are in
3771                 // the same chunk.      
3772                 if(     (start + n) < dev->nBytesPerChunk)
3773                 {
3774                         nToCopy = n;
3775                 }
3776                 else
3777                 {
3778                         nToCopy = dev->nBytesPerChunk - start;
3779                 }
3780         
3781                 cache = yaffs_FindChunkCache(in,chunk);
3782                 
3783                 // If the chunk is already in the cache or it is less than a whole chunk
3784                 // then use the cache (if there is caching)
3785                 // else bypass the cache.
3786                 if( cache || nToCopy != dev->nBytesPerChunk)
3787                 {
3788                         if(dev->nShortOpCaches > 0)
3789                         {
3790                                 
3791                                 // If we can't find the data in the cache, then load it up.
3792                                 
3793                                 if(!cache)
3794                                 {
3795                                         cache = yaffs_GrabChunkCache(in->myDev);
3796                                         cache->object = in;
3797                                         cache->chunkId = chunk;
3798                                         cache->dirty = 0;
3799                                         cache->locked = 0;
3800                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3801                                         cache->nBytes = 0;      
3802                                 }
3803                         
3804                                 yaffs_UseChunkCache(dev,cache,0);
3805
3806                                 cache->locked = 1;
3807
3808 #ifdef CONFIG_YAFFS_WINCE
3809                                 yfsd_UnlockYAFFS(TRUE);
3810 #endif
3811                                 memcpy(buffer,&cache->data[start],nToCopy);
3812
3813 #ifdef CONFIG_YAFFS_WINCE
3814                                 yfsd_LockYAFFS(TRUE);
3815 #endif
3816                                 cache->locked = 0;
3817                         }
3818                         else
3819                         {
3820                                 // Read into the local buffer then copy...
3821                                 
3822                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3823                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);            
3824 #ifdef CONFIG_YAFFS_WINCE
3825                                 yfsd_UnlockYAFFS(TRUE);
3826 #endif
3827                                 memcpy(buffer,&localBuffer[start],nToCopy);
3828
3829 #ifdef CONFIG_YAFFS_WINCE
3830                                 yfsd_LockYAFFS(TRUE);
3831 #endif
3832                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3833                         }
3834
3835                 }
3836                 else
3837                 {
3838 #ifdef CONFIG_YAFFS_WINCE
3839                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3840                         
3841                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3842                         // This is because we otherwise screw up WinCE's memory mapper
3843                         yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3844
3845 #ifdef CONFIG_YAFFS_WINCE
3846                                 yfsd_UnlockYAFFS(TRUE);
3847 #endif
3848                         memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3849
3850 #ifdef CONFIG_YAFFS_WINCE
3851                                 yfsd_LockYAFFS(TRUE);
3852                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3853 #endif
3854
3855 #else
3856                         // A full chunk. Read directly into the supplied buffer.
3857                         yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3858 #endif
3859                 }
3860                 
3861                 n -= nToCopy;
3862                 offset += nToCopy;
3863                 buffer += nToCopy;
3864                 nDone += nToCopy;
3865                 
3866         }
3867         
3868         return nDone;
3869 }
3870
3871
3872
3873 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3874 {       
3875         
3876         int chunk;
3877         int start;
3878         int nToCopy;
3879         int n = nBytes;
3880         int nDone = 0;
3881         int nToWriteBack;
3882         int startOfWrite = offset;
3883         int chunkWritten = 0;
3884         int nBytesRead;
3885         
3886         yaffs_Device *dev;
3887         
3888         dev = in->myDev;
3889         
3890         
3891         while(n > 0 && chunkWritten >= 0)
3892         {
3893                 chunk = offset / dev->nBytesPerChunk + 1;
3894                 start = offset % dev->nBytesPerChunk;
3895                 
3896
3897                 // OK now check for the curveball where the start and end are in
3898                 // the same chunk.
3899                 
3900                 if(     (start + n) < dev->nBytesPerChunk)
3901                 {
3902                         nToCopy = n;
3903                         
3904                         // Now folks, to calculate how many bytes to write back....
3905                         // If we're overwriting and not writing to then end of file then
3906                         // we need to write back as much as was there before.
3907                         
3908                         nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3909                         
3910                         if(nBytesRead > dev->nBytesPerChunk)
3911                         {
3912                                 nBytesRead = dev->nBytesPerChunk;
3913                         }
3914                         
3915                         nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3916                         
3917                 }
3918                 else
3919                 {
3920                         nToCopy = dev->nBytesPerChunk - start;
3921                         nToWriteBack = dev->nBytesPerChunk;
3922                 }
3923         
3924                 if(nToCopy != dev->nBytesPerChunk)
3925                 {
3926                         // An incomplete start or end chunk (or maybe both start and end chunk)
3927                         if(dev->nShortOpCaches > 0)
3928                         {
3929                                 yaffs_ChunkCache *cache;
3930                                 // If we can't find the data in the cache, then load it up.
3931                                 cache = yaffs_FindChunkCache(in,chunk);
3932                                 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3933                                 {
3934                                         cache = yaffs_GrabChunkCache(in->myDev);
3935                                         cache->object = in;
3936                                         cache->chunkId = chunk;
3937                                         cache->dirty = 0;
3938                                         cache->locked = 0;
3939                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);            
3940                                 }
3941                         
3942                                 if(cache)
3943                                 {       
3944                                         yaffs_UseChunkCache(dev,cache,1);
3945                                         cache->locked = 1;
3946 #ifdef CONFIG_YAFFS_WINCE
3947                                 yfsd_UnlockYAFFS(TRUE);
3948 #endif
3949         
3950                                         memcpy(&cache->data[start],buffer,nToCopy);
3951
3952 #ifdef CONFIG_YAFFS_WINCE
3953                                 yfsd_LockYAFFS(TRUE);
3954 #endif
3955                                         cache->locked = 0;
3956                                         cache->nBytes = nToWriteBack;
3957                                 }
3958                                 else
3959                                 {
3960                                         chunkWritten = -1; // fail the write
3961                                 }
3962                         }
3963                         else
3964                         {
3965                                 // An incomplete start or end chunk (or maybe both start and end chunk)
3966                                 // Read into the local buffer then copy, then copy over and write back.
3967                                 
3968                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3969                 
3970                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3971                                 
3972 #ifdef CONFIG_YAFFS_WINCE
3973                                 yfsd_UnlockYAFFS(TRUE);
3974 #endif
3975                                         
3976                                 memcpy(&localBuffer[start],buffer,nToCopy);
3977                         
3978 #ifdef CONFIG_YAFFS_WINCE
3979                                 yfsd_LockYAFFS(TRUE);
3980 #endif
3981                                 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
3982                                 
3983                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3984                         
3985                                 //T(("Write with readback to chunk %d %d  start %d  copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3986                         }
3987                         
3988                 }
3989                 else
3990                 {
3991                         
3992 #ifdef CONFIG_YAFFS_WINCE
3993                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3994                         // This is because we otherwise screw up WinCE's memory mapper
3995                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3996 #ifdef CONFIG_YAFFS_WINCE
3997                                 yfsd_UnlockYAFFS(TRUE);
3998 #endif
3999                         memcpy(localBuffer,buffer,dev->nBytesPerChunk);
4000 #ifdef CONFIG_YAFFS_WINCE
4001                                 yfsd_LockYAFFS(TRUE);
4002 #endif
4003                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
4004                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4005 #else
4006                         // A full chunk. Write directly from the supplied buffer.
4007                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
4008 #endif
4009                         // Since we've overwritten the cached data, we better invalidate it.
4010                         yaffs_InvalidateChunkCache(in,chunk);
4011                         //T(("Write to chunk %d %d\n",chunk,chunkWritten));
4012                 }
4013                 
4014                 if(chunkWritten >= 0)
4015                 {
4016                         n -= nToCopy;
4017                         offset += nToCopy;
4018                         buffer += nToCopy;
4019                         nDone += nToCopy;
4020                 }
4021                 
4022         }
4023         
4024         // Update file object
4025         
4026         if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
4027         {
4028                 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4029         }
4030         
4031         in->dirty = 1;
4032         
4033         return nDone;
4034 }
4035
4036 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
4037 {
4038
4039         yaffs_Device *dev = in->myDev;
4040         int oldFileSize = in->variant.fileVariant.fileSize;
4041
4042         
4043         int lastDel = 1 + (oldFileSize-1)/dev->nBytesPerChunk;
4044                 
4045         int startDel = 1 + (newSize + dev->nBytesPerChunk - 1)/
4046                                                 dev->nBytesPerChunk;
4047         int i;
4048         int chunkId;
4049
4050         // Delete backwards so that we don't end up with holes if
4051         // power is lost part-way through the operation.
4052         for(i = lastDel; i >= startDel; i--)
4053         {
4054                 // NB this could be optimised somewhat,
4055                 // eg. could retrieve the tags and write them without
4056                 // using yaffs_DeleteChunk
4057
4058                 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
4059                 if(chunkId > 0)
4060                 {
4061                         if(chunkId < (dev->startBlock * dev->nChunksPerBlock) || 
4062                        chunkId >= ((dev->endBlock+1) * dev->nChunksPerBlock))
4063                         {
4064                                 T(YAFFS_TRACE_ALWAYS,("Found daft chunkId %d for %d\n",chunkId,i));
4065                         }
4066                         else
4067                         {
4068                                 in->nDataChunks--;
4069                                 yaffs_DeleteChunk(dev,chunkId,1,__LINE__);
4070                         }
4071                 }
4072         }
4073                 
4074 }
4075
4076 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
4077 {
4078
4079         int oldFileSize = in->variant.fileVariant.fileSize;
4080         int sizeOfPartialChunk;
4081         yaffs_Device *dev = in->myDev;
4082         
4083          sizeOfPartialChunk  = newSize % dev->nBytesPerChunk;
4084                 
4085
4086         yaffs_FlushFilesChunkCache(in); 
4087         yaffs_InvalidateWholeChunkCache(in);
4088
4089         yaffs_CheckGarbageCollection(dev);
4090         
4091         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
4092         {
4093                 return yaffs_GetFileSize(in);
4094         }
4095         
4096         if(newSize < oldFileSize)
4097         {
4098
4099                 yaffs_PruneResizedChunks(in,newSize);
4100                 
4101                 if(sizeOfPartialChunk != 0)
4102                 {
4103                         int lastChunk = 1+ newSize/dev->nBytesPerChunk;
4104                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4105                         
4106                         // Got to read and rewrite the last chunk with its new size and zero pad
4107                         yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
4108                         
4109                         memset(localBuffer + sizeOfPartialChunk,0, dev->nBytesPerChunk - sizeOfPartialChunk);
4110                         
4111                         yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
4112                                 
4113                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4114                 }
4115                 
4116                 in->variant.fileVariant.fileSize = newSize;
4117                 
4118                 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
4119                 
4120                 // TODO write a new object header to show we've shrunk the file
4121                 // Do this only if the file is not in the deleted directory.
4122                 if(in->parent->objectId != YAFFS_OBJECTID_UNLINKED)
4123                 {
4124                         yaffs_UpdateObjectHeader(in,NULL, 0, 1);
4125                 }
4126
4127                 
4128                 return newSize;
4129                 
4130         }
4131         else
4132         {
4133                 return oldFileSize;
4134         }
4135 }
4136
4137
4138 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4139 {
4140         obj = yaffs_GetEquivalentObject(obj);
4141         
4142         switch(obj->variantType)
4143         {
4144                 case YAFFS_OBJECT_TYPE_FILE: 
4145                         return obj->variant.fileVariant.fileSize;
4146                 case YAFFS_OBJECT_TYPE_SYMLINK:
4147                         return yaffs_strlen(obj->variant.symLinkVariant.alias);
4148                 default:
4149                         return 0;
4150         }
4151 }
4152
4153
4154
4155 // yaffs_FlushFile() updates the file's
4156 // objectId in NAND
4157
4158 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
4159 {
4160         int retVal;
4161         if(in->dirty)
4162         {
4163                 //T(("flushing object header\n"));
4164                 
4165                 yaffs_FlushFilesChunkCache(in);
4166                 if(updateTime)
4167                 {
4168 #ifdef CONFIG_YAFFS_WINCE
4169                         yfsd_WinFileTimeNow(in->win_mtime);
4170 #else
4171 #if defined(CONFIG_KERNEL_2_5)
4172                         in->st_mtime = CURRENT_TIME.tv_sec;
4173 #else
4174                         in->st_mtime = CURRENT_TIME;
4175 #endif
4176 #endif
4177                 }
4178
4179                 retVal = (yaffs_UpdateObjectHeader(in,NULL,0,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
4180         }
4181         else
4182         {
4183                 retVal = YAFFS_OK;
4184         }
4185         
4186         return retVal;
4187         
4188 }
4189
4190
4191 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4192 {
4193
4194         // First off, invalidate the file's data in the cache, without flushing.
4195         yaffs_InvalidateWholeChunkCache(in);
4196
4197         if(in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir))
4198         {
4199                 // Move to the unlinked directory so we have a record that it was deleted.
4200                 yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4201
4202         }
4203
4204         
4205         yaffs_RemoveObjectFromDirectory(in);
4206         yaffs_DeleteChunk(in->myDev,in->chunkId,1,__LINE__);
4207         in->chunkId = -1;
4208
4209 #ifdef __KERNEL__
4210         if(in->myInode)
4211         {
4212                 in->myInode->u.generic_ip = NULL;
4213                 in->myInode = 0;
4214         }
4215 #endif
4216         yaffs_FreeObject(in);
4217         return YAFFS_OK;
4218
4219 }
4220
4221 // yaffs_DeleteFile deletes the whole file data
4222 // and the inode associated with the file.
4223 // It does not delete the links associated with the file.
4224 static int yaffs_UnlinkFile(yaffs_Object *in)
4225 {
4226
4227 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
4228
4229         // Delete the file data & tnodes
4230
4231          yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
4232          
4233
4234         yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4235         
4236         return  yaffs_DoGenericObjectDeletion(in);
4237 #else
4238         int retVal;
4239         int immediateDeletion=0;
4240
4241         if(1)
4242         {
4243                 //in->unlinked = 1;
4244                 //in->myDev->nUnlinkedFiles++;
4245                 //in->renameAllowed = 0;
4246 #ifdef __KERNEL__
4247                 if(!in->myInode)
4248                 {
4249                         immediateDeletion = 1;
4250
4251                 }
4252 #else
4253                 if(in->inUse <= 0)
4254                 {
4255                         immediateDeletion = 1;
4256
4257                 }
4258 #endif
4259                 if(immediateDeletion)
4260                 {
4261                         retVal = yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4262                         T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
4263                         in->deleted=1;
4264                         in->myDev->nDeletedFiles++;
4265                         yaffs_SoftDeleteFile(in);
4266                 }
4267                 else
4268                 {
4269                         retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
4270                 }
4271         
4272         }
4273         return retVal;
4274
4275         
4276 #endif
4277 }
4278
4279 int yaffs_DeleteFile(yaffs_Object *in)
4280 {
4281         int retVal = YAFFS_OK;
4282         
4283         if(in->nDataChunks > 0)
4284         {
4285                 // Use soft deletion
4286                 if(!in->unlinked)
4287                 {
4288                         retVal = yaffs_UnlinkFile(in);
4289                 }
4290                 if(retVal == YAFFS_OK && 
4291                 in->unlinked &&
4292                 !in->deleted)
4293                 {
4294                         in->deleted = 1;
4295                         in->myDev->nDeletedFiles++;
4296                         yaffs_SoftDeleteFile(in);
4297                 }
4298                 return in->deleted ? YAFFS_OK : YAFFS_FAIL;     
4299         }
4300         else
4301         {
4302                 // The file has no data chunks so we toss it immediately
4303                 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4304                 in->variant.fileVariant.top = NULL;
4305                 yaffs_DoGenericObjectDeletion(in);      
4306                 
4307                 return YAFFS_OK;        
4308         }
4309 }
4310
4311 static int yaffs_DeleteDirectory(yaffs_Object *in)
4312 {
4313         //First check that the directory is empty.
4314         if(list_empty(&in->variant.directoryVariant.children))
4315         {
4316                 return  yaffs_DoGenericObjectDeletion(in);
4317         }
4318         
4319         return YAFFS_FAIL;
4320         
4321 }
4322
4323 static int yaffs_DeleteSymLink(yaffs_Object *in)
4324 {
4325         YFREE(in->variant.symLinkVariant.alias);
4326
4327         return  yaffs_DoGenericObjectDeletion(in);
4328 }
4329
4330 static int yaffs_DeleteHardLink(yaffs_Object *in)
4331 {
4332         // remove this hardlink from the list assocaited with the equivalent
4333         // object
4334         list_del(&in->hardLinks);
4335         return  yaffs_DoGenericObjectDeletion(in);      
4336 }
4337
4338
4339 static void yaffs_DestroyObject(yaffs_Object *obj)
4340 {
4341         switch(obj->variantType)
4342         {
4343                 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
4344                 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
4345                 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
4346                 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
4347                 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
4348                 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
4349         }
4350 }
4351
4352
4353 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4354 {
4355
4356         
4357         if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4358         {
4359                 return  yaffs_DeleteHardLink(obj);
4360         }
4361         else if(!list_empty(&obj->hardLinks))
4362         {       
4363                 // Curve ball: We're unlinking an object that has a hardlink.
4364                 //
4365                 //      This problem arises because we are not strictly following
4366                 //  The Linux link/inode model.
4367                 //
4368                 // We can't really delete the object.
4369                 // Instead, we do the following:
4370                 // - Select a hardlink.
4371                 // - Unhook it from the hard links
4372                 // - Unhook it from its parent directory (so that the rename can work)
4373                 // - Rename the object to the hardlink's name.
4374                 // - Delete the hardlink
4375                 
4376                 
4377                 yaffs_Object *hl;
4378                 int retVal;
4379                 YCHAR name[YAFFS_MAX_NAME_LENGTH+1];
4380                 
4381                 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
4382                 
4383                 list_del_init(&hl->hardLinks);
4384                 list_del_init(&hl->siblings);
4385                 
4386                 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
4387                 
4388                 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
4389                 
4390                 if(retVal == YAFFS_OK)
4391                 {
4392                         retVal = yaffs_DoGenericObjectDeletion(hl);
4393                 }
4394                 return retVal;
4395                                 
4396         }
4397         else
4398         {
4399                 switch(obj->variantType)
4400                 {
4401                         case YAFFS_OBJECT_TYPE_FILE:
4402                                 return yaffs_UnlinkFile(obj);
4403                                 break;
4404                         case YAFFS_OBJECT_TYPE_DIRECTORY:
4405                                 return yaffs_DeleteDirectory(obj);
4406                                 break;
4407                         case YAFFS_OBJECT_TYPE_SYMLINK:
4408                                 return yaffs_DeleteSymLink(obj);
4409                                 break;
4410                         case YAFFS_OBJECT_TYPE_HARDLINK:
4411                         case YAFFS_OBJECT_TYPE_UNKNOWN:
4412                         default:
4413                                 return YAFFS_FAIL;
4414                 }
4415         }
4416 }
4417
4418 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4419 {
4420         yaffs_Object *obj;
4421         
4422          obj = yaffs_FindObjectByName(dir,name);
4423          
4424          if(obj && obj->unlinkAllowed)
4425          {
4426                 return yaffs_UnlinkWorker(obj);
4427          }
4428          
4429          return YAFFS_FAIL;
4430         
4431 }
4432
4433 //////////////// Initialisation Scanning /////////////////
4434
4435
4436 #if 0
4437 // For now we use the SmartMedia check.
4438 // We look at the blockStatus byte in the first two chunks
4439 // These must be 0xFF to pass as OK.
4440 // todo: this function needs to be modifyable foir different NAND types
4441 // and different chunk sizes.  Suggest make this into a per-device configurable
4442 // function.
4443 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4444 {
4445         yaffsExtendedTags *tags;
4446         
4447         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&tags,1);
4448 #if 1
4449         if(yaffs_CountBits(spare.blockStatus) < 7)
4450         {
4451                 return 1;
4452         }
4453 #else
4454         if(spare.blockStatus != 0xFF)
4455         {
4456                 return 1;
4457         }
4458 #endif
4459         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4460
4461 #if 1
4462         if(yaffs_CountBits(spare.blockStatus) < 7)
4463         {
4464                 return 1;
4465         }
4466 #else
4467         if(spare.blockStatus != 0xFF)
4468         {
4469                 return 1;
4470         }
4471 #endif
4472         
4473         return 0;
4474         
4475 }
4476
4477 #endif
4478
4479
4480 typedef struct 
4481 {
4482         int seq;
4483         int block;
4484 } yaffs_BlockIndex;
4485
4486
4487
4488 static int yaffs_Scan(yaffs_Device *dev)
4489 {
4490         yaffs_ExtendedTags tags;
4491         int blk;
4492         int blockIterator;
4493         int startIterator;
4494         int endIterator;
4495         int nBlocksToScan = 0;
4496         
4497         int chunk;
4498         int c;
4499         int deleted;
4500         yaffs_BlockState state;
4501         yaffs_Object *hardList = NULL;
4502         yaffs_Object *hl;
4503         yaffs_BlockInfo *bi;
4504         int sequenceNumber;     
4505         yaffs_ObjectHeader *oh;
4506         yaffs_Object *in;
4507         yaffs_Object *parent;
4508         int nBlocks = dev->endBlock - dev->startBlock + 1;
4509         
4510         __u8 *chunkData;
4511
4512         yaffs_BlockIndex *blockIndex = NULL;
4513         
4514         chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4515         
4516         
4517         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4518         
4519         if(dev->isYaffs2)
4520         {
4521                 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));               
4522         }
4523         
4524         
4525         // Scan all the blocks to determine their state
4526         for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
4527         {
4528                 bi = yaffs_GetBlockInfo(dev,blk);
4529                 yaffs_ClearChunkBits(dev,blk);
4530                 bi->pagesInUse = 0;
4531                 bi->softDeletions = 0;
4532                                 
4533                 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4534                 
4535                 bi->blockState = state;
4536                 bi->sequenceNumber = sequenceNumber;
4537                 
4538                 if(state == YAFFS_BLOCK_STATE_DEAD)
4539                 {
4540                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4541                 }
4542                 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4543                 {
4544                         dev->nErasedBlocks++;
4545                         dev->nFreeChunks += dev->nChunksPerBlock;
4546                 }
4547                 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4548                 {
4549                                         
4550                         // Determine the highest sequence number
4551                         if( dev->isYaffs2 &&
4552                             sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
4553                             sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
4554                          {
4555                                 
4556                                 blockIndex[nBlocksToScan].seq = sequenceNumber;
4557                                 blockIndex[nBlocksToScan].block = blk;
4558                                 
4559                                 nBlocksToScan++;
4560
4561                                 if(sequenceNumber >= dev->sequenceNumber)
4562                                 {
4563                                         dev->sequenceNumber = sequenceNumber;
4564                                 }
4565                         }
4566                         else if(dev->isYaffs2)
4567                         {
4568                                 // TODO: Nasty sequence number!
4569                                 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
4570
4571                         }
4572                 }
4573         }
4574         
4575         // Sort the blocks
4576         // Dungy old bubble sort for now...
4577         if(dev->isYaffs2)
4578         {
4579                 yaffs_BlockIndex temp;
4580                 int i;
4581                 int j;
4582                 
4583                 for(i = 0; i < nBlocksToScan; i++)
4584                         for(j = i+1; j < nBlocksToScan; j++)
4585                          if(blockIndex[i].seq > blockIndex[j].seq)
4586                          {
4587                                 temp = blockIndex[j];
4588                                 blockIndex[j] = blockIndex[i];
4589                                 blockIndex[i] = temp;
4590                          }
4591         }
4592         
4593         
4594         // Now scan the blocks looking at the data.
4595         if(dev->isYaffs2)
4596         {
4597                 startIterator = 0;
4598                 endIterator = nBlocksToScan-1;
4599         }
4600         else
4601         {
4602                 startIterator = dev->startBlock;
4603                 endIterator = dev->endBlock;
4604         }
4605         
4606         for(blockIterator = startIterator; blockIterator <= endIterator; blockIterator++)
4607         {
4608         
4609                 if(dev->isYaffs2)
4610                 {
4611                         // get the block to scan in the correct order
4612                         blk = blockIndex[blockIterator].block;
4613                 }
4614                 else
4615                 {
4616                         blk = blockIterator;
4617                 }
4618
4619
4620                 bi = yaffs_GetBlockInfo(dev,blk);
4621                 state = bi->blockState;
4622                 
4623                 deleted = 0;
4624                 
4625                 for(c = 0; c < dev->nChunksPerBlock && 
4626                                    state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++)
4627                 {
4628                         // Read the tags and decide what to do
4629                         chunk = blk * dev->nChunksPerBlock + c;
4630                         
4631                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
4632
4633                         // Let's have a good look at this chunk...
4634         
4635                         
4636                         if(!dev->isYaffs2 && tags.chunkDeleted)
4637                         {
4638                                 // A deleted chunk
4639                                 deleted++;
4640                                 dev->nFreeChunks ++;
4641                                 //T((" %d %d deleted\n",blk,c));
4642                         }
4643                         else if(!tags.chunkUsed)
4644                         {
4645                                 // An unassigned chunk in the block
4646                                 // This means that either the block is empty or 
4647                                 // this is the one being allocated from
4648                                 
4649                                 if(c == 0)
4650                                 {
4651                                         // We're looking at the first chunk in the block so the block is unused
4652                                         state = YAFFS_BLOCK_STATE_EMPTY;
4653                                         dev->nErasedBlocks++;
4654                                 }
4655                                 else
4656                                 {
4657                                         // this is the block being allocated from
4658                                         T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4659                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
4660                                         dev->allocationBlock = blk;
4661                                         dev->allocationPage = c;
4662                                         dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
4663                                                                                                           // go forth from here.
4664                                         //Yaffs2 sanity check:
4665                                         // This should be the one with the highest sequence number
4666                                         if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
4667                                         {
4668                                                 T(YAFFS_TRACE_ALWAYS,
4669                                                                 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
4670                                                                 blk,bi->sequenceNumber,dev->sequenceNumber));
4671                                         }
4672                                 }
4673
4674                                 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4675                         }
4676                         else if(tags.chunkId > 0)
4677                         {
4678                                 int endpos;
4679                                 // A data chunk.
4680                                 yaffs_SetChunkBit(dev,blk,c);
4681                                 bi->pagesInUse++;
4682                                                                 
4683                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4684                                 // PutChunkIntoFIle checks for a clash (two data chunks with
4685                                 // the same chunkId).
4686                                 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4687                                 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
4688                                 if(in->variant.fileVariant.scannedFileSize <endpos)
4689                                 {
4690                                         in->variant.fileVariant.scannedFileSize = endpos;
4691                                         if(!dev->useHeaderFileSize)
4692                                         {
4693                                                 in->variant.fileVariant.fileSize =      
4694                                                         in->variant.fileVariant.scannedFileSize;
4695                                         }
4696
4697                                 }
4698                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
4699                         }
4700                         else
4701                         {
4702                                 // chunkId == 0, so it is an ObjectHeader.
4703                                 // Thus, we read in the object header and make the object
4704                                 yaffs_SetChunkBit(dev,blk,c);
4705                                 bi->pagesInUse++;
4706                                                         
4707                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
4708                                 
4709                                 oh = (yaffs_ObjectHeader *)chunkData;
4710                                 
4711                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4712                                 
4713                                 if(in->valid)
4714                                 {
4715                                         // We have already filled this one. We have a duplicate and need to resolve it.
4716                                         
4717                                         unsigned existingSerial = in->serial;
4718                                         unsigned newSerial = tags.serialNumber;
4719                                         
4720                                         if( dev->isYaffs2 ||
4721                                             ((existingSerial+1) & 3) == newSerial)
4722                                         {
4723                                                 // Use new one - destroy the exisiting one
4724                                                 yaffs_DeleteChunk(dev,in->chunkId,1,__LINE__);
4725                                                 in->valid = 0;
4726                                         }
4727                                         else
4728                                         {
4729                                                 // Use existing - destroy this one.
4730                                                 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
4731                                         }
4732                                 }
4733                                 
4734                                 if(!in->valid &&
4735                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
4736                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4737                                 {
4738                                         // We only load some info, don't fiddle with directory structure
4739                                         in->valid = 1;
4740                                         in->variantType = oh->type;
4741         
4742                                         in->st_mode  = oh->st_mode;
4743 #ifdef CONFIG_YAFFS_WINCE
4744                                         in->win_atime[0] = oh->win_atime[0];
4745                                         in->win_ctime[0] = oh->win_ctime[0];
4746                                         in->win_mtime[0] = oh->win_mtime[0];
4747                                         in->win_atime[1] = oh->win_atime[1];
4748                                         in->win_ctime[1] = oh->win_ctime[1];
4749                                         in->win_mtime[1] = oh->win_mtime[1];
4750 #else
4751                                         in->st_uid   = oh->st_uid;
4752                                         in->st_gid   = oh->st_gid;
4753                                         in->st_atime = oh->st_atime;
4754                                         in->st_mtime = oh->st_mtime;
4755                                         in->st_ctime = oh->st_ctime;
4756                                         in->st_rdev = oh->st_rdev;
4757 #endif
4758                                         in->chunkId  = chunk;
4759
4760                                 }
4761                                 else if(!in->valid)
4762                                 {
4763                                         // we need to load this info
4764                                 
4765                                         in->valid = 1;
4766                                         in->variantType = oh->type;
4767         
4768                                         in->st_mode  = oh->st_mode;
4769 #ifdef CONFIG_YAFFS_WINCE
4770                                         in->win_atime[0] = oh->win_atime[0];
4771                                         in->win_ctime[0] = oh->win_ctime[0];
4772                                         in->win_mtime[0] = oh->win_mtime[0];
4773                                         in->win_atime[1] = oh->win_atime[1];
4774                                         in->win_ctime[1] = oh->win_ctime[1];
4775                                         in->win_mtime[1] = oh->win_mtime[1];
4776 #else
4777                                         in->st_uid   = oh->st_uid;
4778                                         in->st_gid   = oh->st_gid;
4779                                         in->st_atime = oh->st_atime;
4780                                         in->st_mtime = oh->st_mtime;
4781                                         in->st_ctime = oh->st_ctime;
4782                                         in->st_rdev = oh->st_rdev;
4783 #endif
4784                                         in->chunkId  = chunk;
4785
4786                                         yaffs_SetObjectName(in,oh->name);
4787                                         in->dirty = 0;
4788                                                         
4789                                         // directory stuff...
4790                                         // hook up to parent
4791         
4792                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4793                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4794                                         {
4795                                                 // Set up as a directory
4796                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4797                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4798                                         }
4799                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4800                                         {
4801                                                 // Hoosterman, another problem....
4802                                                 // We're trying to use a non-directory as a directory
4803
4804                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4805                                                 parent = dev->lostNFoundDir;
4806                                         }
4807                                 
4808                                         yaffs_AddObjectToDirectory(parent,in);
4809
4810                                         if(0 && (parent == dev->deletedDir ||
4811                                            parent == dev->unlinkedDir))
4812                                         {
4813                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4814                                                 dev->nDeletedFiles++;
4815                                         }
4816                                 
4817                                         // Note re hardlinks.
4818                                         // Since we might scan a hardlink before its equivalent object is scanned
4819                                         // we put them all in a list.
4820                                         // After scanning is complete, we should have all the objects, so we run through this
4821                                         // list and fix up all the chains.              
4822         
4823                                         switch(in->variantType)
4824                                         {
4825                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
4826                                                         break;
4827                                                 case YAFFS_OBJECT_TYPE_FILE:
4828                                                         if(dev->isYaffs2 && oh->isShrink)
4829                                                         {
4830                                                                 // Prune back the shrunken chunks
4831                                                                 yaffs_PruneResizedChunks(in,oh->fileSize);
4832                                                                 // Mark the block as having a shrinkHeader
4833                                                                 bi->hasShrinkHeader = 1;
4834                                                         }
4835                                                         
4836                                                         if(dev->useHeaderFileSize)
4837                                                                 in->variant.fileVariant.fileSize = oh->fileSize;
4838                                                                 
4839                                                         break;
4840                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
4841                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4842                                                         in->hardLinks.next = (struct list_head *)hardList;
4843                                                         hardList = in;
4844                                                         break;
4845                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
4846                                                         break;
4847                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4848                                                         break;
4849                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
4850                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4851                                                         break;
4852                                         }
4853
4854                                         if(parent == dev->deletedDir)
4855                                         {
4856                                                 yaffs_DestroyObject(in);
4857                                                 bi->hasShrinkHeader = 1;
4858                                         }
4859                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
4860                                 }
4861                         }
4862                 }
4863                 
4864                 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4865                 {
4866                         // If we got this far while scanning, then the block is fully allocated.
4867                         state = YAFFS_BLOCK_STATE_FULL; 
4868                 }
4869                 
4870                 bi->blockState = state;
4871                 
4872                 // Now let's see if it was dirty
4873                 if(     bi->pagesInUse == 0 &&
4874                         !bi->hasShrinkHeader &&
4875                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4876             {
4877                 yaffs_BlockBecameDirty(dev,blk);
4878             }
4879
4880         }
4881         
4882         if(blockIndex)
4883         {
4884                 YFREE(blockIndex);
4885         }
4886         
4887         // Ok, we've done all the scanning.
4888         
4889         // Fix up the hard link chains.
4890         // We should now have scanned all the objects, now it's time to add these 
4891         // hardlinks.
4892         while(hardList)
4893         {
4894                 hl = hardList;
4895                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4896                 
4897                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4898                 
4899                 if(in)
4900                 {
4901                         // Add the hardlink pointers
4902                         hl->variant.hardLinkVariant.equivalentObject=in;
4903                         list_add(&hl->hardLinks,&in->hardLinks);
4904                 }
4905                 else
4906                 {
4907                         //Todo Need to report/handle this better.
4908                         // Got a problem... hardlink to a non-existant object
4909                         hl->variant.hardLinkVariant.equivalentObject=NULL;
4910                         INIT_LIST_HEAD(&hl->hardLinks);
4911                         
4912                 }
4913                 
4914         }
4915         
4916         {
4917                 struct list_head *i;    
4918                 struct list_head *n;
4919                         
4920                 yaffs_Object *l;
4921                 // Soft delete all the unlinked files
4922                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4923                 {
4924                         if(i)
4925                         {
4926                                 l = list_entry(i, yaffs_Object,siblings);
4927                                 if(l->deleted)
4928                                 {
4929                                         yaffs_DestroyObject(l);         
4930                                 }
4931                         }
4932                 }       
4933         }
4934         
4935         yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
4936         return YAFFS_OK;
4937 }
4938
4939
4940
4941 ////////////////////////// Directory Functions /////////////////////////
4942
4943
4944 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
4945 {
4946
4947         if(obj->siblings.prev == NULL)
4948         {
4949                 // Not initialised
4950                 INIT_LIST_HEAD(&obj->siblings);
4951                 
4952         }
4953         else if(!list_empty(&obj->siblings))
4954         {
4955                 // If it is holed up somewhere else, un hook it
4956                 list_del_init(&obj->siblings);
4957         }
4958         // Now add it
4959         list_add(&obj->siblings,&directory->variant.directoryVariant.children);
4960         obj->parent = directory;
4961         
4962         if(directory == obj->myDev->unlinkedDir || directory == obj->myDev->deletedDir)
4963         {
4964                 obj->unlinked = 1;
4965                 obj->myDev->nUnlinkedFiles++;
4966                 obj->renameAllowed = 0;
4967         }
4968 }
4969
4970 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
4971 {
4972         list_del_init(&obj->siblings);
4973         obj->parent = NULL;
4974 }
4975
4976 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const YCHAR *name)
4977 {
4978         int sum;
4979         
4980         struct list_head *i;
4981         YCHAR buffer[YAFFS_MAX_NAME_LENGTH+1];
4982         
4983         yaffs_Object *l;
4984         
4985         sum = yaffs_CalcNameSum(name);
4986         
4987         list_for_each(i,&directory->variant.directoryVariant.children)
4988         {
4989                 if(i)
4990                 {
4991                         l = list_entry(i, yaffs_Object,siblings);
4992                 
4993                         // Special case for lost-n-found
4994                         if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4995                         {
4996                                 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
4997                                 {
4998                                         return l;
4999                                 }
5000                         }
5001                         else if(yaffs_SumCompare(l->sum, sum)||
5002                                     l->chunkId <= 0) //LostnFound cunk called Objxxx
5003                         {
5004                                 // Do a real check
5005                                 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
5006                                 if(yaffs_strcmp(name,buffer) == 0)
5007                                 {
5008                                         return l;
5009                                 }
5010                         
5011                         }                       
5012                 }
5013         }
5014         
5015         return NULL;
5016 }
5017
5018
5019 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
5020 {
5021         struct list_head *i;    
5022         yaffs_Object *l;
5023         
5024         
5025         list_for_each(i,&theDir->variant.directoryVariant.children)
5026         {
5027                 if(i)
5028                 {
5029                         l = list_entry(i, yaffs_Object,siblings);
5030                         if(l && !fn(l))
5031                         {
5032                                 return YAFFS_FAIL;
5033                         }
5034                 }
5035         }
5036         
5037         return YAFFS_OK;
5038
5039 }
5040
5041
5042 // GetEquivalentObject dereferences any hard links to get to the
5043 // actual object.
5044
5045 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5046 {
5047         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
5048         {
5049                 // We want the object id of the equivalent object, not this one
5050                 obj = obj->variant.hardLinkVariant.equivalentObject;
5051         }
5052         return obj;
5053
5054 }
5055
5056 int yaffs_GetObjectName(yaffs_Object *obj,YCHAR *name,int buffSize)
5057 {
5058         memset(name,0,buffSize * sizeof(YCHAR));
5059         
5060         if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5061         {
5062                 yaffs_strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
5063         }
5064         else if(obj->chunkId <= 0)
5065         {
5066                 YCHAR locName[20];
5067                 // make up a name
5068                 yaffs_sprintf(locName,_Y("%s%d"),YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
5069                 yaffs_strncpy(name,locName,buffSize - 1);
5070
5071         }
5072 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5073         else if(obj->shortName[0])
5074         {
5075                 yaffs_strcpy(name,obj->shortName);
5076         }
5077 #endif
5078         else
5079         {
5080                 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev,__LINE__);
5081                 
5082                 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5083
5084                 memset(buffer,0,obj->myDev->nBytesPerChunk);
5085         
5086                 if(obj->chunkId >= 0)
5087                 {
5088                         yaffs_ReadChunkWithTagsFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5089                 }
5090                 yaffs_strncpy(name,oh->name,buffSize - 1);
5091
5092                 yaffs_ReleaseTempBuffer(obj->myDev,buffer,__LINE__);
5093         }
5094         
5095         return yaffs_strlen(name);
5096 }
5097
5098 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5099 {
5100         
5101         // Dereference any hard linking
5102         obj = yaffs_GetEquivalentObject(obj);
5103         
5104         if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5105         {
5106                 return obj->variant.fileVariant.fileSize;
5107         }
5108         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5109         {
5110                 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5111         }
5112         else
5113         {
5114                 // Only a directory should drop through to here
5115                 return obj->myDev->nBytesPerChunk;
5116         }       
5117 }
5118
5119 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5120 {
5121         int count = 0; 
5122         struct list_head *i;
5123         
5124         if(!obj->unlinked)
5125         {
5126                 count++;        // the object itself
5127         }
5128         list_for_each(i,&obj->hardLinks)
5129         {
5130                 count++;        // add the hard links;
5131         }
5132         return count;
5133         
5134 }
5135
5136
5137 int yaffs_GetObjectInode(yaffs_Object *obj)
5138 {
5139         obj = yaffs_GetEquivalentObject(obj);
5140         
5141         return obj->objectId;
5142 }
5143
5144 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5145 {
5146         obj = yaffs_GetEquivalentObject(obj);
5147         
5148         switch(obj->variantType)
5149         {
5150                 case YAFFS_OBJECT_TYPE_FILE:            return DT_REG; break;
5151                 case YAFFS_OBJECT_TYPE_DIRECTORY:       return DT_DIR; break;
5152                 case YAFFS_OBJECT_TYPE_SYMLINK:         return DT_LNK; break;
5153                 case YAFFS_OBJECT_TYPE_HARDLINK:        return DT_REG; break;
5154                 case YAFFS_OBJECT_TYPE_SPECIAL:         
5155                         if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
5156                         if(S_ISCHR(obj->st_mode)) return DT_CHR;
5157                         if(S_ISBLK(obj->st_mode)) return DT_BLK;
5158                         if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
5159                 default: return DT_REG; break;
5160         }
5161 }
5162
5163 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5164 {
5165         obj = yaffs_GetEquivalentObject(obj);
5166         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5167         {
5168                 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5169         }
5170         else
5171         {
5172                 return yaffs_CloneString(_Y(""));
5173         }
5174 }
5175
5176 #ifndef CONFIG_YAFFS_WINCE
5177
5178 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5179 {
5180         unsigned int valid = attr->ia_valid;
5181         
5182         if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
5183         if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
5184         if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
5185         
5186 #if defined(CONFIG_KERNEL_2_5)
5187         if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime.tv_sec;
5188         if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime.tv_sec;
5189         if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime.tv_sec;
5190 #else
5191         if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime;
5192         if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime;
5193         if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime;
5194 #endif
5195         
5196         if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
5197         
5198         yaffs_UpdateObjectHeader(obj,NULL,1,0);
5199         
5200         return YAFFS_OK;
5201         
5202 }
5203 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5204 {
5205         unsigned int valid = 0;
5206         
5207         attr->ia_mode = obj->st_mode;   valid |= ATTR_MODE;
5208         attr->ia_uid = obj->st_uid;             valid |= ATTR_UID;
5209         attr->ia_gid = obj->st_gid;             valid |= ATTR_GID;
5210         
5211 #if defined(CONFIG_KERNEL_2_5)
5212         attr->ia_atime.tv_sec = obj->st_atime;  valid |= ATTR_ATIME;
5213         attr->ia_ctime.tv_sec = obj->st_ctime;  valid |= ATTR_CTIME;
5214         attr->ia_mtime.tv_sec = obj->st_mtime;  valid |= ATTR_MTIME;
5215 #else   
5216         attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME;
5217         attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME;
5218         attr->ia_mtime = obj->st_mtime; valid |= ATTR_MTIME;
5219 #endif  
5220         attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
5221         
5222         attr->ia_valid = valid;
5223         
5224         return YAFFS_OK;
5225         
5226 }
5227
5228 #endif
5229
5230 int yaffs_DumpObject(yaffs_Object *obj)
5231 {
5232 //      __u8 buffer[YAFFS_BYTES_PER_CHUNK];
5233         YCHAR name[257];
5234 //      yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5235
5236 //      memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
5237         
5238 //      if(obj->chunkId >= 0)
5239 //      {
5240 //              yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5241 //      }
5242         
5243         yaffs_GetObjectName(obj,name,256);
5244         
5245         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),
5246                         obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial, 
5247                         obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5248
5249 #if 0
5250         YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
5251                         obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial, 
5252                         obj->sum, obj->chunkId));
5253                 switch(obj->variantType)
5254         {
5255                 case YAFFS_OBJECT_TYPE_FILE: 
5256                         YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
5257                         break;
5258                 case YAFFS_OBJECT_TYPE_DIRECTORY:
5259                         YPRINTF((" DIRECTORY\n"));
5260                         break;
5261                 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
5262                 case YAFFS_OBJECT_TYPE_SYMLINK:
5263                 case YAFFS_OBJECT_TYPE_UNKNOWN:
5264                 default:
5265         }
5266 #endif
5267         
5268         return YAFFS_OK;
5269 }
5270
5271
5272 ///////////////////////// Initialisation code ///////////////////////////
5273
5274
5275 int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5276 {
5277
5278         // Common functions, gotta have
5279         if(!dev->eraseBlockInNAND ||
5280            !dev->initialiseNAND) return 0;
5281
5282         // Can use the "with tags" style interface for yaffs1 or yaffs2
5283         if(dev->writeChunkWithTagsToNAND &&
5284            dev->readChunkWithTagsFromNAND &&
5285            !dev->writeChunkToNAND &&
5286            !dev->readChunkFromNAND &&
5287            dev->markNANDBlockBad &&
5288            dev->queryNANDBlock) return 1;
5289
5290         // Can use the "spare" style interface for yaffs1
5291         if(!dev->isYaffs2 &&
5292            !dev->writeChunkWithTagsToNAND &&
5293            !dev->readChunkWithTagsFromNAND &&
5294            dev->writeChunkToNAND &&
5295            dev->readChunkFromNAND &&
5296            !dev->markNANDBlockBad &&
5297            !dev->queryNANDBlock) return 1;
5298
5299         return 0; // bad
5300 }
5301
5302
5303 int yaffs_GutsInitialise(yaffs_Device *dev)
5304 {
5305         unsigned x;
5306         int bits;
5307         int extraBits;
5308         int nBlocks;
5309
5310         // Check stuff that must be set
5311
5312         if(!dev)
5313         {
5314                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Need a device\n" TENDSTR)));
5315                 return YAFFS_FAIL;
5316         }
5317
5318         // Check geometry parameters.
5319
5320         if(     (dev->isYaffs2 && dev->nBytesPerChunk <1024)  ||
5321                 (!dev->isYaffs2 && dev->nBytesPerChunk !=512)  ||
5322                 dev->nChunksPerBlock < 2 ||
5323                 dev->nReservedBlocks < 2 ||
5324                 dev->startBlock <= 0 ||
5325                 dev->endBlock <= 0 ||
5326                 dev->endBlock <= (dev->startBlock + dev->nReservedBlocks + 2) // otherwise it is too small
5327           )
5328         {
5329                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: nand geometry problems\n" TENDSTR)));
5330                 return YAFFS_FAIL;
5331         }
5332
5333
5334         // Got the right mix of functions?
5335         //
5336         if(!yaffs_CheckDevFunctions(dev))
5337         {
5338                 //Function missing
5339                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5340
5341                 return YAFFS_FAIL;
5342         }
5343
5344         // This is really a compilation check.
5345         if(!yaffs_CheckStructures())
5346         {
5347                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
5348                 return YAFFS_FAIL;
5349         }
5350
5351         if(dev->isMounted)
5352         {
5353                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
5354                 return YAFFS_FAIL;
5355         }
5356
5357         //
5358         //
5359         // Finished with most checks. One or two more checks happen later on too.
5360         //
5361
5362         dev->isMounted = 1;
5363
5364
5365         nBlocks = dev->endBlock - dev->startBlock + 1;
5366
5367
5368
5369         // OK now calculate a few things for the device
5370         // Calculate chunkGroupBits.
5371         // We need to find the next power of 2 > than endBlock
5372         
5373         x = dev->nChunksPerBlock * (dev->endBlock+1);
5374         
5375         for(bits = extraBits = 0; x > 1; bits++)
5376         {
5377                 if(x & 1) extraBits++;
5378                 x >>= 1;
5379         }
5380
5381         if(extraBits > 0) bits++;
5382         
5383         
5384         // Level0 Tnodes are 16 bits, so if the bitwidth of the
5385         // chunk range we're using is greater than 16 we need
5386         // to figure out chunk shift and chunkGroupSize
5387         if(bits <= 16) 
5388         {
5389                 dev->chunkGroupBits = 0;
5390         }
5391         else
5392         {
5393                 dev->chunkGroupBits = bits - 16;
5394         }
5395         
5396         dev->chunkGroupSize = 1 << dev->chunkGroupBits;
5397
5398         if(dev->nChunksPerBlock < dev->chunkGroupSize)
5399         {
5400                 // We have a problem because the soft delete won't work if
5401                 // the chunk group size > chunks per block.
5402                 // This can be remedied by using larger "virtual blocks".
5403                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: chunk group too large\n" TENDSTR)));
5404                 
5405                 return YAFFS_FAIL;
5406         }
5407
5408         
5409         // OK, we've finished verifying the device, lets continue with initialisation
5410         
5411         // More device initialisation
5412         dev->garbageCollections = 0;
5413         dev->passiveGarbageCollections = 0;
5414         dev->currentDirtyChecker = 0;
5415         dev->bufferedBlock = -1;
5416         dev->doingBufferedBlockRewrite = 0;
5417         dev->nDeletedFiles = 0;
5418         dev->nBackgroundDeletions=0;
5419         dev->nUnlinkedFiles = 0;
5420         dev->eccFixed=0;
5421         dev->eccUnfixed=0;
5422         dev->tagsEccFixed=0;
5423         dev->tagsEccUnfixed=0;
5424         dev->nErasureFailures = 0;
5425         
5426         //dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
5427         // Initialise temporary buffers
5428         {
5429                 int i;
5430                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5431                 {
5432                         dev->tempBuffer[i].line = 0; // not in use
5433                         dev->tempBuffer[i].buffer = YMALLOC(dev->nBytesPerChunk);
5434                 }
5435         }
5436         
5437
5438         
5439         
5440         yaffs_InitialiseBlocks(dev,nBlocks);
5441         
5442         yaffs_InitialiseTnodes(dev);
5443
5444         yaffs_InitialiseObjects(dev);
5445         
5446         dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
5447         
5448         if(dev->nShortOpCaches > 0)
5449         { 
5450                 int i;
5451                 
5452                 if(dev->nShortOpCaches >  YAFFS_MAX_SHORT_OP_CACHES)
5453                 {
5454                         dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
5455                 }
5456                 
5457                 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
5458                 
5459                 for(i=0; i < dev->nShortOpCaches; i++)
5460                 {
5461                         dev->srCache[i].object = NULL;
5462                         dev->srCache[i].lastUse = 0;
5463                         dev->srCache[i].dirty = 0;
5464                         dev->srCache[i].data = YMALLOC(dev->nBytesPerChunk);
5465                 }
5466                 dev->srLastUse = 0;
5467         }
5468
5469         dev->cacheHits = 0;
5470         
5471         
5472         // Initialise the unlinked, root and lost and found directories
5473         dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = dev->deletedDir = NULL;
5474         
5475         dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5476         dev->deletedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5477
5478         dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
5479         dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
5480         yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
5481         
5482         if(dev->isYaffs2) 
5483         {
5484                 dev->useHeaderFileSize = 1;
5485         }
5486                 
5487         // Now scan the flash.  
5488         yaffs_Scan(dev);
5489         
5490         // Zero out stats
5491         dev->nPageReads = 0;
5492     dev->nPageWrites =  0;
5493         dev->nBlockErasures = 0;
5494         dev->nGCCopies = 0;
5495         dev->nRetriedWrites = 0;
5496
5497         dev->nRetiredBlocks = 0;
5498
5499         
5500         return YAFFS_OK;
5501                 
5502 }
5503
5504 void yaffs_Deinitialise(yaffs_Device *dev)
5505 {
5506         if(dev->isMounted)
5507         {
5508                 int i;
5509         
5510                 yaffs_DeinitialiseBlocks(dev);
5511                 yaffs_DeinitialiseTnodes(dev);
5512                 yaffs_DeinitialiseObjects(dev);
5513                 if(dev->nShortOpCaches > 0)
5514                 {
5515                                 
5516                         for(i=0; i < dev->nShortOpCaches; i++)
5517                         {
5518                                 YFREE(dev->srCache[i].data);
5519                         }
5520
5521                         YFREE(dev->srCache);
5522                 }
5523
5524                 YFREE(dev->gcCleanupList);
5525
5526                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5527                 {
5528                         YFREE(dev->tempBuffer[i].buffer);
5529                 }
5530                 
5531                 dev->isMounted = 0;
5532         }
5533         
5534 }
5535
5536 #if 0
5537
5538 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5539 {
5540         int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
5541         
5542         struct list_head *i;    
5543         yaffs_Object *l;
5544         
5545         
5546         // To the free chunks add the chunks that are in the deleted unlinked files.
5547         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
5548         {
5549                 l = list_entry(i, yaffs_Object,siblings);
5550                 if(l->deleted)
5551                 {
5552                         nFree++;
5553                         nFree += l->nDataChunks;
5554                 }
5555         }
5556         
5557         
5558         // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);        
5559
5560         if(nFree < 0) nFree = 0;
5561
5562         return nFree;   
5563         
5564 }
5565
5566 #endif
5567
5568 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5569 {
5570         int nFree;
5571         int pending;
5572         int b;
5573         int nDirtyCacheChunks=0;
5574         
5575         yaffs_BlockInfo *blk;
5576         
5577         struct list_head *i;    
5578         yaffs_Object *l;
5579         
5580         for(nFree = 0, b = dev->startBlock; b <= dev->endBlock; b++)
5581         {
5582                 blk = yaffs_GetBlockInfo(dev,b);
5583                 
5584                 switch(blk->blockState)
5585                 {
5586                         case YAFFS_BLOCK_STATE_EMPTY:
5587                         case YAFFS_BLOCK_STATE_ALLOCATING: 
5588                         case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break;
5589                         default: break;
5590                 }
5591         }
5592         
5593         
5594         pending = 0;
5595         
5596         // To the free chunks add the chunks that are in the deleted unlinked files.
5597         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
5598         {
5599                 if(i)
5600                 {
5601                         l = list_entry(i, yaffs_Object,siblings);
5602                         if(l->deleted)
5603                         {
5604                                 pending++;
5605                                 pending += l->nDataChunks;
5606                         }
5607                 }
5608         }
5609         
5610         
5611         
5612         //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending);
5613         
5614         if(nFree != dev->nFreeChunks) 
5615         {
5616         //      printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks);
5617         }
5618
5619         nFree += pending;
5620         
5621         // Now count the number of dirty chunks in the cache and subtract those
5622         
5623         {
5624                 int i;
5625                 for(i = 0; i < dev->nShortOpCaches; i++)
5626                 {
5627                         if(dev->srCache[i].dirty) nDirtyCacheChunks++;
5628                 }
5629         }
5630         
5631         nFree -= nDirtyCacheChunks;
5632         
5633         nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
5634         
5635         if(nFree < 0) nFree = 0;
5636
5637         return nFree;   
5638         
5639 }
5640
5641
5642
5643 /////////////////// YAFFS test code //////////////////////////////////
5644
5645 #define yaffs_CheckStruct(structure,syze, name) \
5646            if(sizeof(structure) != syze) \
5647                { \
5648                  T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
5649                  return YAFFS_FAIL; \
5650                    }
5651                  
5652                  
5653 static int yaffs_CheckStructures(void)
5654 {
5655 //      yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
5656 //      yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
5657 //      yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
5658 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
5659         yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
5660 #endif
5661         yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
5662         
5663         
5664         return YAFFS_OK;
5665 }
5666
5667 #if 0
5668 void yaffs_GutsTest(yaffs_Device *dev)
5669 {
5670         
5671         if(yaffs_CheckStructures() != YAFFS_OK)
5672         {
5673                 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
5674                 return;
5675         }
5676         
5677         yaffs_TnodeTest(dev);
5678         yaffs_ObjectTest(dev);  
5679 }
5680 #endif
5681
5682
5683