340c7366cee299d2a97d062a404d782a2266c263
[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.3 2004-11-22 03:22:25 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 static 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_DELETION,(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 #ifdef  __KERNEL__
1533         if(tn->myInode)
1534         {
1535                 // We're still hooked up to a cached inode.
1536                 // Don't delete now, but mark for later deletion
1537                 tn->deferedFree = 1;
1538                 return;
1539         }
1540 #endif
1541         
1542         yaffs_UnhashObject(tn);
1543         
1544         // Link into the free list.
1545         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1546         dev->freeObjects = tn;
1547         dev->nFreeObjects++;
1548 }
1549
1550
1551
1552 #ifdef __KERNEL__
1553
1554 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1555 {
1556         if(obj->deferedFree)
1557         {
1558            yaffs_FreeObject(obj);
1559         }
1560 }
1561
1562 #endif
1563
1564
1565
1566 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1567 {
1568         // Free the list of allocated Objects
1569         
1570         yaffs_ObjectList *tmp;
1571         
1572         while( dev->allocatedObjectList)
1573         {
1574                 tmp =  dev->allocatedObjectList->next;
1575                 YFREE(dev->allocatedObjectList->objects);
1576                 YFREE(dev->allocatedObjectList);
1577                 
1578                 dev->allocatedObjectList =  tmp;
1579         }
1580         
1581         dev->freeObjects = NULL;
1582         dev->nFreeObjects = 0;
1583 }
1584
1585 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1586 {
1587         int i;
1588         
1589         dev->allocatedObjectList = NULL;
1590         dev->freeObjects = NULL;
1591         dev->nFreeObjects = 0;
1592         
1593         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1594         {
1595                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1596                 dev->objectBucket[i].count = 0; 
1597         }
1598
1599 }
1600
1601
1602
1603
1604
1605
1606 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1607 {
1608         static int x = 0;
1609         int i;
1610         int l = 999;
1611         int lowest = 999999;
1612
1613                 
1614         // First let's see if we can find one that's empty.
1615         
1616         for(i = 0; i < 10 && lowest > 0; i++)
1617          {
1618                 x++;
1619                 x %=  YAFFS_NOBJECT_BUCKETS;
1620                 if(dev->objectBucket[x].count < lowest)
1621                 {
1622                         lowest = dev->objectBucket[x].count;
1623                         l = x;
1624                 }
1625                 
1626         }
1627         
1628         // If we didn't find an empty list, then try
1629         // looking a bit further for a short one
1630         
1631         for(i = 0; i < 10 && lowest > 3; i++)
1632          {
1633                 x++;
1634                 x %=  YAFFS_NOBJECT_BUCKETS;
1635                 if(dev->objectBucket[x].count < lowest)
1636                 {
1637                         lowest = dev->objectBucket[x].count;
1638                         l = x;
1639                 }
1640                 
1641         }
1642         
1643         return l;
1644 }
1645
1646 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1647 {
1648         int bucket = yaffs_FindNiceObjectBucket(dev);
1649         
1650         // Now find an object value that has not already been taken
1651         // by scanning the list.
1652         
1653         int found = 0;
1654         struct list_head *i;
1655         
1656         __u32 n = (__u32)bucket;
1657
1658         //yaffs_CheckObjectHashSanity();        
1659         
1660         while(!found)
1661         {
1662                 found = 1;
1663                 n +=  YAFFS_NOBJECT_BUCKETS;
1664                 if(1 ||dev->objectBucket[bucket].count > 0)
1665                 {
1666                         list_for_each(i,&dev->objectBucket[bucket].list)
1667                         {
1668                                 // If there is already one in the list
1669                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1670                                 {
1671                                         found = 0;
1672                                 }
1673                         }
1674                 }
1675         }
1676         
1677         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1678         
1679         return n;       
1680 }
1681
1682 void yaffs_HashObject(yaffs_Object *in)
1683 {
1684         int bucket = yaffs_HashFunction(in->objectId);
1685         yaffs_Device *dev = in->myDev;
1686         
1687         if(!list_empty(&in->hashLink))
1688         {
1689                 //YINFO("!!!");
1690         }
1691
1692         
1693         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1694         dev->objectBucket[bucket].count++;
1695
1696 }
1697
1698 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1699 {
1700         int bucket = yaffs_HashFunction(number);
1701         struct list_head *i;
1702         yaffs_Object *in;
1703         
1704         list_for_each(i,&dev->objectBucket[bucket].list)
1705         {
1706                 // Look if it is in the list
1707                 if(i)
1708                 {
1709                         in = list_entry(i, yaffs_Object,hashLink);
1710                         if(in->objectId == number)
1711                         {
1712                                 return in;
1713                         }
1714                 }
1715         }
1716         
1717         return NULL;
1718 }
1719
1720
1721
1722 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1723 {
1724                 
1725         yaffs_Object *theObject;
1726
1727         if(number < 0)
1728         {
1729                 number = yaffs_CreateNewObjectNumber(dev);
1730         }
1731         
1732         theObject = yaffs_AllocateEmptyObject(dev);
1733         
1734         if(theObject)
1735         {
1736                 theObject->fake = 0;
1737                 theObject->renameAllowed = 1;
1738                 theObject->unlinkAllowed = 1;
1739                 theObject->objectId = number;
1740                 yaffs_HashObject(theObject);
1741                 theObject->variantType = type;
1742 #ifdef CONFIG_YAFFS_WINCE
1743                 yfsd_WinFileTimeNow(theObject->win_atime);
1744                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1745                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1746
1747 #else
1748
1749                 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = Y_CURRENT_TIME;               
1750 #endif
1751                 switch(type)
1752                 {
1753                         case YAFFS_OBJECT_TYPE_FILE: 
1754                                 theObject->variant.fileVariant.fileSize = 0;
1755                                 theObject->variant.fileVariant.scannedFileSize = 0;
1756                                 theObject->variant.fileVariant.topLevel = 0;
1757                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1758                                 break;
1759                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1760                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1761                                 break;
1762                         case YAFFS_OBJECT_TYPE_SYMLINK:
1763                                 // No action required
1764                                 break;
1765                         case YAFFS_OBJECT_TYPE_HARDLINK:
1766                                 // No action required
1767                                 break;
1768                         case YAFFS_OBJECT_TYPE_SPECIAL:
1769                                 // No action required
1770                                 break;
1771                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1772                                 // todo this should not happen
1773                                 break;
1774                 }
1775         }
1776         
1777         return theObject;
1778 }
1779
1780 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1781 {
1782         yaffs_Object *theObject = NULL;
1783         
1784         if(number > 0)
1785         {
1786                 theObject = yaffs_FindObjectByNumber(dev,number);
1787         }
1788         
1789         if(!theObject)
1790         {
1791                 theObject = yaffs_CreateNewObject(dev,number,type);
1792         }
1793         
1794         return theObject;
1795
1796 }
1797
1798 YCHAR *yaffs_CloneString(const YCHAR *str)
1799 {
1800         YCHAR *newStr = NULL;
1801         
1802         if(str && *str)
1803         {
1804                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1805                 yaffs_strcpy(newStr,str);
1806         }
1807
1808         return newStr;
1809         
1810 }
1811
1812 //
1813 // Mknod (create) a new object.
1814 // equivalentObject only has meaning for a hard link;
1815 // aliasString only has meaning for a sumlink.
1816 // rdev only has meaning for devices (a subset of special objects)
1817 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1818                                                                  yaffs_Object *parent,
1819                                                                  const YCHAR *name, 
1820                                                                  __u32 mode,
1821                                                                  __u32 uid,
1822                                                                  __u32 gid,
1823                                                                  yaffs_Object *equivalentObject,
1824                                                                  const YCHAR *aliasString,
1825                                                                  __u32 rdev)
1826 {
1827         yaffs_Object *in;
1828
1829         yaffs_Device *dev = parent->myDev;
1830         
1831         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1832         if(yaffs_FindObjectByName(parent,name))
1833         {
1834                 return NULL;
1835         }
1836         
1837         in = yaffs_CreateNewObject(dev,-1,type);
1838         
1839         if(in)
1840         {
1841                 in->chunkId = -1;
1842                 in->valid = 1;
1843                 in->variantType = type;
1844
1845                 in->st_mode  = mode;
1846                 
1847 #ifdef CONFIG_YAFFS_WINCE
1848                 yfsd_WinFileTimeNow(in->win_atime);
1849                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1850                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1851                 
1852 #else
1853                 in->st_atime = in->st_mtime = in->st_ctime = Y_CURRENT_TIME;
1854
1855                 in->st_rdev  = rdev;
1856                 in->st_uid   = uid;
1857                 in->st_gid   = gid;
1858 #endif          
1859                 in->nDataChunks = 0;
1860
1861                 yaffs_SetObjectName(in,name);
1862                 in->dirty = 1;
1863                 
1864                 yaffs_AddObjectToDirectory(parent,in);
1865                 
1866                 in->myDev = parent->myDev;
1867                 
1868                                 
1869                 switch(type)
1870                 {
1871                         case YAFFS_OBJECT_TYPE_SYMLINK:
1872                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1873                                 break;
1874                         case YAFFS_OBJECT_TYPE_HARDLINK:
1875                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1876                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1877                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1878                                 break;
1879                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1880                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1881                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1882                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1883                                 break;
1884                 }
1885
1886                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1887                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1888                 {
1889                         // Could not create the object header, fail the creation
1890                         yaffs_DestroyObject(in);
1891                         in = NULL;
1892                 }
1893
1894         }
1895         
1896         return in;
1897 }
1898
1899 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1900 {
1901         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1902 }
1903
1904 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1905 {
1906         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1907 }
1908
1909 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1910 {
1911         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1912 }
1913
1914 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1915 {
1916         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1917 }
1918
1919 // NB yaffs_Link returns the object id of the equivalent object.
1920 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1921 {
1922         // Get the real object in case we were fed a hard link as an equivalent object
1923         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1924         
1925         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1926         {
1927                 return equivalentObject;
1928         }
1929         else
1930         {
1931                 return NULL;
1932         }
1933         
1934 }
1935
1936
1937 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1938 {
1939         int unlinkOp;
1940         int deleteOp;
1941
1942         if(newDir == NULL)
1943         {
1944                 newDir = obj->parent; // use the old directory
1945         }
1946
1947         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1948         if(obj->myDev->isYaffs2)
1949         {
1950                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1951         }
1952         else
1953         {
1954                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1955         }
1956
1957         deleteOp = (newDir == obj->myDev->deletedDir);
1958         
1959         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1960         // duplicate names are allowed.
1961         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1962         if( (unlinkOp|| 
1963                  deleteOp ||
1964                  force || 
1965                  !yaffs_FindObjectByName(newDir,newName))  &&
1966              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1967         {
1968                 yaffs_SetObjectName(obj,newName);
1969                 obj->dirty = 1;
1970                 
1971                 yaffs_AddObjectToDirectory(newDir,obj);
1972                 
1973                 if(unlinkOp) obj->unlinked = 1;
1974                 
1975                 // If it is a dletion then we mark it as a shrink for gc purposes.
1976                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1977                 {
1978                         return YAFFS_OK;
1979                 }
1980         }
1981         
1982         return YAFFS_FAIL;
1983 }
1984
1985
1986
1987 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1988 {
1989         yaffs_Object *obj;
1990         int force = 0;
1991         
1992 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1993         // Special case for case insemsitive systems (eg. WinCE).
1994         // While look-up is case insensitive, the name isn't.
1995         // THerefore we might want to change x.txt to X.txt
1996         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1997         {
1998                 force = 1;
1999         }       
2000 #endif
2001         
2002         obj = yaffs_FindObjectByName(oldDir,oldName);
2003         if(obj && obj->renameAllowed)
2004         {
2005                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
2006         }
2007         return YAFFS_FAIL;
2008 }
2009
2010
2011
2012 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
2013 {
2014         // Scan the buckets and check that the lists 
2015         // have as many members as the count says there are
2016         int bucket;
2017         int countEm;
2018         struct list_head *j;
2019         int ok = YAFFS_OK;
2020         
2021         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2022         {
2023                 countEm = 0;
2024                 
2025                 list_for_each(j,&dev->objectBucket[bucket].list)
2026                 {
2027                         countEm++;
2028                 }
2029                 
2030                 if(countEm != dev->objectBucket[bucket].count)
2031                 {
2032                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2033                         ok = YAFFS_FAIL;
2034                 }
2035         }
2036
2037         return ok;
2038 }
2039
2040 #if 0
2041 void yaffs_ObjectTest(yaffs_Device *dev)
2042 {
2043         yaffs_Object *in[1000];
2044         int inNo[1000];
2045         yaffs_Object *inold[1000];
2046         int i;
2047         int j;
2048         
2049         memset(in,0,1000*sizeof(yaffs_Object *));
2050         memset(inold,0,1000*sizeof(yaffs_Object *));
2051         
2052         yaffs_CheckObjectHashSanity(dev);
2053         
2054         for(j = 0; j < 10; j++)
2055         {
2056                 //T(("%d\n",j));
2057                 
2058                 for(i = 0; i < 1000; i++)
2059                 {
2060                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2061                         if(!in[i])
2062                         {
2063                                 YINFO("No more inodes");
2064                         }
2065                         else
2066                         {
2067                                 inNo[i] = in[i]->objectId;
2068                         }
2069                 }
2070                 
2071                 for(i = 0; i < 1000; i++)
2072                 {
2073                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2074                         {
2075                                 //T(("Differnce in look up test\n"));
2076                         }
2077                         else
2078                         {
2079                                 // T(("Look up ok\n"));
2080                         }
2081                 }
2082                 
2083                 yaffs_CheckObjectHashSanity(dev);
2084         
2085                 for(i = 0; i < 1000; i+=3)
2086                 {
2087                         yaffs_FreeObject(in[i]);        
2088                         in[i] = NULL;
2089                 }
2090                 
2091         
2092                 yaffs_CheckObjectHashSanity(dev);
2093         }
2094                 
2095 }
2096
2097 #endif
2098
2099 /////////////////////////// Block Management and Page Allocation ///////////////////
2100
2101
2102 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2103 {
2104         dev->allocationBlock = -1; // force it to get a new one
2105         //Todo we're assuming the malloc will pass.
2106         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2107         // Set up dynamic blockinfo stuff.
2108         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2109         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2110         if(dev->blockInfo && dev->chunkBits)
2111         {
2112                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2113                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2114                 return YAFFS_OK;
2115         }
2116         
2117         return YAFFS_FAIL;
2118         
2119 }
2120
2121 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2122 {
2123         YFREE(dev->blockInfo);
2124         dev->blockInfo = NULL;
2125         YFREE(dev->chunkBits);
2126         dev->chunkBits = NULL;
2127 }
2128
2129
2130 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2131 {
2132         int i;
2133         __u32 seq;
2134         yaffs_BlockInfo *b;
2135         
2136         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2137         
2138         if(!bi->hasShrinkHeader) return 1; // can gc
2139
2140
2141         // Find the oldest dirty sequence number if we don't know it and save it
2142         // so we don't have to keep recomputing it.
2143         if(!dev->oldestDirtySequence)
2144         {
2145                 seq = dev->sequenceNumber;
2146
2147                 for(i = dev->startBlock; i <= dev->endBlock; i++)
2148                 {
2149                         b = yaffs_GetBlockInfo(dev,i);
2150                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2151                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2152                         b->sequenceNumber < seq)
2153                         {
2154                                 seq = b->sequenceNumber;
2155                         }
2156                 }
2157                 dev->oldestDirtySequence = seq;
2158         }
2159
2160
2161         // Can't do gc of this block if there are any blocks older than this one that have
2162         // discarded pages.
2163         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2164         
2165         
2166         return 1;
2167
2168 }
2169
2170 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2171 // for garbage collection.
2172 //
2173
2174
2175
2176 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2177 {
2178
2179         int b = dev->currentDirtyChecker;
2180         
2181         int i;
2182         int iterations;
2183         int dirtiest = -1;
2184         int pagesInUse; 
2185         yaffs_BlockInfo *bi;
2186         static int  nonAggressiveSkip = 0;
2187
2188         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2189         // search harder.
2190         // else (we're doing a leasurely gc), then we only bother to do this if the
2191         // block has only a few pages in use.
2192         
2193
2194         nonAggressiveSkip--;
2195
2196         if(!aggressive &&(nonAggressiveSkip > 0))
2197         {
2198                 return -1;
2199         }
2200
2201         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2202         if(aggressive)
2203         {
2204                 iterations = dev->endBlock - dev->startBlock + 1;
2205         }
2206         else
2207         {
2208                 iterations = dev->endBlock - dev->startBlock + 1;
2209                 iterations = iterations / 16; 
2210                 if(iterations > 200)
2211                 {
2212                         iterations = 200;
2213                 }
2214         }
2215         
2216         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2217         {
2218                 b++;
2219                 if ( b < dev->startBlock || b > dev->endBlock)
2220                 {
2221                         b =  dev->startBlock;
2222                 }
2223
2224                 if(b < dev->startBlock || b > dev->endBlock)
2225                 {
2226                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2227                         YBUG();
2228                 }
2229                 
2230                 bi = yaffs_GetBlockInfo(dev,b);
2231                 
2232                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2233                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2234                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2235                 {
2236                         dirtiest = b;
2237                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2238                 }
2239         }
2240         
2241         dev->currentDirtyChecker = b;
2242         
2243         if(dirtiest > 0)
2244         {
2245                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2246         }
2247         
2248         dev->oldestDirtySequence = 0; // clear this
2249         
2250         if(dirtiest > 0)
2251         {
2252                 nonAggressiveSkip = 4;
2253         }
2254
2255         return dirtiest;
2256 }
2257
2258
2259 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2260 {
2261         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2262         
2263         int erasedOk = 0;
2264         
2265         // If the block is still healthy erase it and mark as clean.
2266         // If the block has had a data failure, then retire it.
2267         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2268
2269         if(!bi->needsRetiring)
2270         {
2271                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2272                 if(!erasedOk)
2273                 {
2274                         dev->nErasureFailures++;
2275                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2276                 }
2277         }
2278
2279         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2280         {
2281                         int i;
2282                         for(i = 0; i < dev->nChunksPerBlock; i++)
2283                         {
2284                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2285                                         {
2286                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2287                                         }
2288                         }
2289         }
2290         
2291         if( erasedOk )
2292         {
2293                 // Clean it up...
2294                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2295                 dev->nErasedBlocks++;
2296                 bi->pagesInUse = 0;
2297                 bi->softDeletions = 0;
2298                 bi->hasShrinkHeader=0;
2299                 yaffs_ClearChunkBits(dev,blockNo);
2300         
2301                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2302         }
2303         else
2304         {
2305                 yaffs_RetireBlock(dev,blockNo);
2306                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2307         }
2308 }
2309
2310 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2311 {
2312         int i,j;
2313         yaffs_BlockInfo *bi;
2314         
2315         for(i= dev->startBlock; i <=dev->endBlock; i++)
2316         {
2317                 bi = yaffs_GetBlockInfo(dev,i);
2318                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2319                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2320                 
2321                 for(j = 0; j < dev->nChunksPerBlock; j++)
2322                 {
2323                         if(yaffs_CheckChunkBit(dev,i,j))
2324                         {
2325                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2326
2327                         }
2328                 }
2329                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2330
2331         }
2332 }
2333
2334
2335 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2336 {
2337         int i;
2338         
2339         yaffs_BlockInfo *bi;
2340         
2341 #if 0
2342         static int j = 0;
2343         j++;
2344         if(j < 0 || j > 100)
2345         {
2346                 j = 0;
2347                 yaffs_DumpBlockStats(dev);
2348         }
2349         
2350 #endif
2351         
2352         if(dev->nErasedBlocks < 1)
2353         {
2354                 // Hoosterman we've got a problem.
2355                 // Can't get space to gc
2356                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2357
2358                 return -1;
2359         }
2360         
2361         // Find an empty block.
2362         
2363         for(i = dev->startBlock; i <= dev->endBlock; i++)
2364         {
2365                 dev->allocationBlockFinder++;
2366                 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock) 
2367                 {
2368                         dev->allocationBlockFinder = dev->startBlock;
2369                 }
2370                 
2371                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2372
2373                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2374                 {
2375                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2376                         dev->sequenceNumber++;
2377                         bi->sequenceNumber = dev->sequenceNumber;
2378                         dev->nErasedBlocks--;           
2379                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber));   
2380                         return dev->allocationBlockFinder;
2381                 }
2382         }
2383                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been one" TENDSTR)));
2384
2385         
2386         return -1;      
2387 }
2388
2389
2390
2391 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2392 {
2393         int retVal;
2394         yaffs_BlockInfo *bi;
2395         
2396         if(dev->allocationBlock < 0)
2397         {
2398                 // Get next block to allocate off
2399                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2400                 dev->allocationPage = 0;
2401         }
2402         
2403         if(!useReserve &&  dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2404         {
2405                 // Not enough space to allocate unless we're allowed to use the reserve.
2406                 return -1;
2407         }
2408
2409         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2410         {
2411                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2412         }
2413
2414         
2415         // Next page please....
2416         if(dev->allocationBlock >= 0)
2417         {
2418                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2419                 
2420                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2421                                   dev->allocationPage;
2422                 bi->pagesInUse++;
2423                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2424
2425                 dev->allocationPage++;
2426                 
2427                 dev->nFreeChunks--;
2428                 
2429                 // If the block is full set the state to full
2430                 if(dev->allocationPage >= dev->nChunksPerBlock)
2431                 {
2432                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2433                         dev->allocationBlock = -1;
2434                 }
2435
2436
2437                 return retVal;
2438                 
2439         }
2440         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2441
2442         return -1;      
2443 }
2444
2445
2446 // To determine if we have enough space we just look at the 
2447 // number of erased blocks.
2448 // The cache is allowed to use reserved blocks.
2449
2450 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2451 {
2452         return (dev->nErasedBlocks >= dev->nReservedBlocks);
2453 }
2454
2455
2456 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2457 {
2458                 int n;
2459
2460                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2461
2462                 if(dev->allocationBlock> 0)
2463                 {
2464                         n += (dev->nChunksPerBlock - dev->allocationPage);
2465                 }
2466
2467                 return n;
2468
2469 }
2470
2471 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2472 {
2473         int oldChunk;
2474         int newChunk;
2475         int chunkInBlock;
2476         int markNAND;
2477         int retVal = YAFFS_OK;
2478         int cleanups = 0;
2479         int i;
2480
2481         int chunksBefore = yaffs_GetErasedChunks(dev);
2482         int chunksAfter;
2483
2484         yaffs_ExtendedTags  tags;
2485         
2486         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2487         
2488         yaffs_Object *object;
2489         
2490         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2491
2492         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2493         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2494
2495         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2496
2497
2498         if(!yaffs_StillSomeChunkBits(dev,block))
2499         {
2500                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2501                 yaffs_BlockBecameDirty(dev,block);
2502         }
2503         else
2504         {
2505
2506                         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2507
2508         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2509             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2510             chunkInBlock++, oldChunk++ )
2511         {
2512                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2513                 {
2514                         
2515                         // This page is in use and might need to be copied off
2516                         
2517                         markNAND = 1;
2518                         
2519                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2520                         
2521                         yaffs_InitialiseTags(&tags);
2522                         
2523                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2524
2525                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2526                         
2527                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2528                         
2529                         if(!object)
2530                         {
2531                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2532                         }
2533                         
2534                         if(object && object->deleted && tags.chunkId != 0)
2535                         {
2536                                 // Data chunk in a deleted file, throw it away
2537                                 // It's a deleted data chunk,
2538                                 // No need to copy this, just forget about it and fix up the
2539                                 // object.
2540                                 
2541                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2542                                 object->nDataChunks--;
2543                                 
2544                                 if(object->nDataChunks <= 0)
2545                                 {
2546                                         // remeber to clean up the object
2547                                         dev->gcCleanupList[cleanups] = tags.objectId;
2548                                         cleanups++;
2549                                 }
2550                                 markNAND = 0;
2551                         }
2552                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2553                         {
2554                                 // Deleted object header with no data chunks.
2555                                 // Can be discarded and the file deleted.
2556                                 object->chunkId = 0;
2557                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2558                                 object->variant.fileVariant.top = NULL;
2559                                 yaffs_DoGenericObjectDeletion(object);
2560                                 
2561                         }
2562                         else if(object)
2563                         {
2564                                 // It's either a data chunk in a live file or
2565                                 // an ObjectHeader, so we're interested in it.
2566                                 // NB Need to keep the ObjectHeaders of deleted files
2567                                 // until the whole file has been deleted off
2568                                 tags.serialNumber++;
2569
2570                                 dev->nGCCopies++;
2571
2572                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2573                         
2574                                 if(newChunk < 0)
2575                                 {
2576                                         retVal =  YAFFS_FAIL;
2577                                 }
2578                                 else
2579                                 {
2580                         
2581                                         // Ok, now fix up the Tnodes etc.
2582                         
2583                                         if(tags.chunkId == 0)
2584                                         {
2585                                                 // It's a header
2586                                                 object->chunkId = newChunk;
2587                                                 object->serial = tags.serialNumber;
2588                                         }
2589                                         else
2590                                         {
2591                                                 // It's a data chunk
2592                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2593                                         }
2594                                 }
2595                         }
2596                         
2597                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2598                         
2599                 }
2600         }
2601
2602         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2603
2604
2605         // Do any required cleanups
2606         for(i = 0; i < cleanups; i++)
2607         {                                               
2608                 // Time to delete the file too
2609                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2610                 if(object)
2611                 {
2612                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2613                         object->variant.fileVariant.top = NULL;
2614                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2615                         yaffs_DoGenericObjectDeletion(object);
2616                 }
2617
2618         }
2619
2620         }
2621
2622         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2623         {
2624                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2625         }
2626                         
2627         return YAFFS_OK;
2628 }
2629
2630 #if 0
2631 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2632 {
2633         // find a file to delete
2634         struct list_head *i;    
2635         yaffs_Object *l;
2636
2637
2638         //Scan the unlinked files looking for one to delete
2639         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2640         {
2641                 if(i)
2642                 {
2643                         l = list_entry(i, yaffs_Object,siblings);
2644                         if(l->deleted)
2645                         {
2646                                 return l;                       
2647                         }
2648                 }
2649         }       
2650         return NULL;
2651 }
2652
2653
2654 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2655 {
2656         // This does background deletion on unlinked files.. only deleted ones.
2657         // If we don't have a file we're working on then find one
2658         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2659         {
2660                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2661         }
2662         
2663         // OK, we're working on a file...
2664         if(dev->unlinkedDeletion)
2665         {
2666                 yaffs_Object *obj = dev->unlinkedDeletion;
2667                 int delresult;
2668                 int limit; // Number of chunks to delete in a file.
2669                                    // NB this can be exceeded, but not by much.
2670                                    
2671                 limit = -1;
2672
2673                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2674                 
2675                 if(obj->nDataChunks == 0)
2676                 {
2677                         // Done all the deleting of data chunks.
2678                         // Now dump the header and clean up
2679                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2680                         obj->variant.fileVariant.top = NULL;
2681                         yaffs_DoGenericObjectDeletion(obj);
2682                         dev->nDeletedFiles--;
2683                         dev->nUnlinkedFiles--;
2684                         dev->nBackgroundDeletions++;
2685                         dev->unlinkedDeletion = NULL;   
2686                 }
2687         }
2688 }
2689
2690 #endif
2691
2692 #if 0
2693 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2694 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2695 {
2696         int block;
2697         int aggressive=0;
2698         
2699         //yaffs_DoUnlinkedFileDeletion(dev);
2700         
2701         if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2702         {
2703                 aggressive = 1;
2704         }               
2705         
2706         if(aggressive)
2707         {
2708                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2709                 
2710                 if(block >= 0)
2711                 {
2712                         dev->garbageCollections++;
2713                         return yaffs_GarbageCollectBlock(dev,block);
2714                 }       
2715                 else
2716                 {
2717                         return YAFFS_FAIL;
2718                 }
2719         }
2720
2721         return YAFFS_OK;
2722 }
2723 #endif
2724
2725 // New garbage collector
2726 // If we're very low on erased blocks then we do aggressive garbage collection
2727 // otherwise we do "leasurely" garbage collection.
2728 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2729 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2730 //
2731 // The idea is to help clear out space in a more spread-out manner.
2732 // Dunno if it really does anything useful.
2733 //
2734 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2735 {
2736         int block;
2737         int aggressive; 
2738         int gcOk = YAFFS_OK;
2739         int maxTries = 0;
2740         
2741         //yaffs_DoUnlinkedFileDeletion(dev);
2742
2743         // This loop should pass the first time.
2744         // We'll only see looping here if the erase of the collected block fails.
2745         
2746         do{
2747                 maxTries++;
2748                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2749                 {
2750                         // We need a block soon...
2751                         aggressive = 1;
2752                 }
2753                 else 
2754                 {
2755                         // We're in no hurry
2756                         aggressive = 0;
2757                 }
2758         
2759                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2760         
2761                 if(block > 0)
2762                 {
2763                         dev->garbageCollections++;
2764                         if(!aggressive)
2765                         {
2766                                 dev->passiveGarbageCollections++;
2767                         }
2768
2769                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2770
2771                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2772                 }
2773
2774                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) 
2775                 {
2776                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2777                 }
2778         } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2779
2780         return aggressive ? gcOk: YAFFS_OK;
2781 }
2782
2783
2784 //////////////////////////// TAGS ///////////////////////////////////////
2785
2786
2787 void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
2788 {
2789         memset(tags,0,sizeof(yaffs_ExtendedTags));
2790         tags->validMarker0 = 0xAAAAAAAA;
2791         tags->validMarker1 = 0x55555555;
2792 }
2793
2794 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
2795 {
2796         return (tags->validMarker0 == 0xAAAAAAAA &&     tags->validMarker1 == 0x55555555);
2797
2798 }
2799
2800
2801 #if 0
2802
2803 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2804 {
2805         // Calculate an ecc
2806         
2807         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2808         unsigned  i,j;
2809         unsigned  ecc = 0;
2810         unsigned bit = 0;
2811
2812         tags->ecc = 0;
2813         
2814         for(i = 0; i < 8; i++)
2815         {
2816                 for(j = 1; j &0xff; j<<=1)
2817                 {
2818                         bit++;
2819                         if(b[i] & j)
2820                         {
2821                                 ecc ^= bit;
2822                         }
2823                 }
2824         }
2825         
2826         tags->ecc = ecc;
2827         
2828         
2829 }
2830
2831 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2832 {
2833         unsigned ecc = tags->ecc;
2834         
2835         yaffs_CalcTagsECC(tags);
2836         
2837         ecc ^= tags->ecc;
2838         
2839         if(ecc && ecc <= 64)
2840         {
2841                 // TODO: Handle the failure better. Retire?
2842                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2843
2844                 ecc--;
2845                                 
2846                 b[ecc / 8] ^= (1 << (ecc & 7));
2847                 
2848                 // Now recvalc the ecc
2849                 yaffs_CalcTagsECC(tags);
2850                 
2851                 return 1; // recovered error
2852         }
2853         else if(ecc)
2854         {
2855                 // Wierd ecc failure value
2856                 // TODO Need to do somethiong here
2857                 return -1; //unrecovered error
2858         }
2859         
2860         return 0;
2861 }
2862
2863 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2864 {
2865         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2866         
2867         yaffs_CalcTagsECC(tagsPtr);
2868         
2869         sparePtr->tagByte0 = tu->asBytes[0];
2870         sparePtr->tagByte1 = tu->asBytes[1];
2871         sparePtr->tagByte2 = tu->asBytes[2];
2872         sparePtr->tagByte3 = tu->asBytes[3];
2873         sparePtr->tagByte4 = tu->asBytes[4];
2874         sparePtr->tagByte5 = tu->asBytes[5];
2875         sparePtr->tagByte6 = tu->asBytes[6];
2876         sparePtr->tagByte7 = tu->asBytes[7];
2877 }
2878
2879 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2880 {
2881         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2882         int result;
2883
2884         tu->asBytes[0]= sparePtr->tagByte0;
2885         tu->asBytes[1]= sparePtr->tagByte1;
2886         tu->asBytes[2]= sparePtr->tagByte2;
2887         tu->asBytes[3]= sparePtr->tagByte3;
2888         tu->asBytes[4]= sparePtr->tagByte4;
2889         tu->asBytes[5]= sparePtr->tagByte5;
2890         tu->asBytes[6]= sparePtr->tagByte6;
2891         tu->asBytes[7]= sparePtr->tagByte7;
2892         
2893         result =  yaffs_CheckECCOnTags(tagsPtr);
2894         if(result> 0)
2895         {
2896                 dev->tagsEccFixed++;
2897         }
2898         else if(result <0)
2899         {
2900                 dev->tagsEccUnfixed++;
2901         }
2902 }
2903
2904 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2905 {
2906         memset(spare,0xFF,sizeof(yaffs_Spare));
2907 }
2908
2909 #endif
2910
2911 #if 0
2912 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2913 {
2914         // NB There must be tags, data is optional
2915         // If there is data, then an ECC is calculated on it.
2916         
2917         yaffs_Spare spare;
2918         
2919         if(!tags)
2920         {
2921                 return YAFFS_FAIL;
2922         }
2923         
2924         //yaffs_SpareInitialise(&spare);
2925         
2926         //if(!dev->useNANDECC && buffer)
2927         //{
2928         //      yaffs_CalcECC(buffer,&spare);
2929         //}
2930         
2931         //yaffs_LoadTagsIntoSpare(&spare,tags);
2932         
2933         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2934         
2935 }
2936 #endif
2937
2938 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2939 {
2940         return  (  tags->chunkId == chunkInObject &&
2941                            tags->objectId == objectId &&
2942                            !tags->chunkDeleted) ? 1 : 0;
2943         
2944 }
2945
2946
2947
2948 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2949 {
2950         //Get the Tnode, then get the level 0 offset chunk offset
2951     yaffs_Tnode *tn;     
2952     int theChunk = -1;
2953     yaffs_ExtendedTags localTags;
2954     int retVal = -1;
2955     
2956     yaffs_Device *dev = in->myDev;
2957     
2958     
2959     if(!tags)
2960     {
2961         // Passed a NULL, so use our own tags space
2962         tags = &localTags;
2963     }
2964     
2965     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2966     
2967     if(tn)
2968     {
2969                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2970                 
2971                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2972     }
2973     return retVal;
2974 }
2975
2976 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2977 {
2978         //Get the Tnode, then get the level 0 offset chunk offset
2979     yaffs_Tnode *tn;     
2980     int theChunk = -1;
2981     yaffs_ExtendedTags localTags;
2982
2983     yaffs_Device *dev = in->myDev;
2984     int retVal = -1;
2985     
2986     if(!tags)
2987     {
2988         // Passed a NULL, so use our own tags space
2989         tags = &localTags;
2990     }
2991     
2992     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2993     
2994     if(tn)
2995     {
2996     
2997                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2998                 
2999                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
3000     
3001                 // Delete the entry in the filestructure (if found)
3002                 if(retVal != -1)
3003                 {
3004                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
3005                 }
3006     }
3007     else
3008     {
3009         //T(("No level 0 found for %d\n", chunkInInode));
3010     }
3011     
3012     if(retVal == -1)
3013     {
3014         //T(("Could not find %d to delete\n",chunkInInode));
3015     }
3016     return retVal;
3017 }
3018
3019
3020 #ifdef YAFFS_PARANOID
3021
3022 static int yaffs_CheckFileSanity(yaffs_Object *in)
3023 {
3024         int chunk;
3025         int nChunks;
3026         int fSize;
3027         int failed = 0;
3028         int objId;
3029         yaffs_Tnode *tn;
3030     yaffs_Tags localTags;
3031     yaffs_Tags *tags = &localTags;
3032     int theChunk;
3033     int chunkDeleted;
3034     
3035         
3036         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3037         {
3038                 //T(("Object not a file\n"));
3039                 return YAFFS_FAIL;
3040         }
3041         
3042         objId = in->objectId;
3043         fSize  = in->variant.fileVariant.fileSize;
3044         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3045         
3046         for(chunk = 1; chunk <= nChunks; chunk++)
3047         {
3048                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3049     
3050                 if(tn)
3051                 {
3052     
3053                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3054                         
3055                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3056                         {
3057
3058
3059                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3060                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3061                                 {
3062                                         // found it;
3063                                 
3064                                 }
3065                         }
3066                         else
3067                         {
3068                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
3069                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
3070                                                 
3071                                 failed = 1;
3072                                                         
3073                         }
3074     
3075                 }
3076                 else
3077                 {
3078                         //T(("No level 0 found for %d\n", chunk));
3079                 }
3080         }
3081         
3082         return failed ? YAFFS_FAIL : YAFFS_OK;
3083 }
3084
3085 #endif
3086
3087 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3088 {
3089         yaffs_Tnode *tn;
3090         yaffs_Device *dev = in->myDev;
3091         int existingChunk;
3092         yaffs_ExtendedTags existingTags;
3093         yaffs_ExtendedTags newTags;
3094         unsigned existingSerial, newSerial;
3095                 
3096         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3097         if(!tn)
3098         {
3099                 return YAFFS_FAIL;
3100         }
3101
3102         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
3103         
3104         if(inScan)
3105         {
3106                 // If we're scanning then we need to test for duplicates
3107                 // NB This does not need to be efficient since it should only ever 
3108                 // happen when the power fails during a write, then only one
3109                 // chunk should ever be affected.
3110         
3111                 
3112                 if(existingChunk != 0)
3113                 {
3114                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3115                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3116                         //
3117                         // We have a duplicate now we need to decide which one to use
3118                         // To do this we get both sets of tags and compare serial numbers.
3119                         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3120                         
3121                         
3122                         // Do a proper find
3123                         existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3124
3125                         if(existingChunk <=0)
3126                         {
3127                                 //Hoosterman - how did this happen?
3128                                 
3129                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3130
3131                         }
3132
3133                         
3134                         // NB The deleted flags should be false, otherwise the chunks will 
3135                         // not be loaded during a scan
3136                         
3137                         newSerial = newTags.serialNumber;
3138                         existingSerial = existingTags.serialNumber;
3139                         
3140                         if( in->myDev->isYaffs2 ||
3141                             existingChunk <= 0 ||
3142                             ((existingSerial+1) & 3) == newSerial)
3143                         {
3144                                 // Use new
3145                                 // Delete the old one and drop through to update the tnode
3146                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3147                         }
3148                         else
3149                         {
3150                                 // Use existing.
3151                                 // Delete the new one and return early so that the tnode isn't changed
3152                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3153                                 return YAFFS_OK;
3154                         }
3155                 }
3156
3157         }
3158                 
3159         if(existingChunk == 0)
3160         {
3161                 in->nDataChunks++;
3162         }
3163         
3164         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3165         
3166         return YAFFS_OK;
3167 }
3168
3169
3170
3171 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3172 {
3173     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3174     
3175     if(chunkInNAND >= 0)
3176     {
3177                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3178         }
3179         else
3180         {
3181                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3182
3183                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3184                 return 0;
3185         }
3186
3187 }
3188
3189
3190 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3191 {
3192         int block;
3193         int page;
3194         yaffs_ExtendedTags tags;
3195         yaffs_BlockInfo *bi;
3196         
3197         if(chunkId <= 0) return;        
3198         
3199         dev->nDeletions++;
3200         block = chunkId / dev->nChunksPerBlock;
3201         page = chunkId % dev->nChunksPerBlock;
3202         
3203         bi = yaffs_GetBlockInfo(dev,block);
3204         
3205         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3206         
3207         if(markNAND && 
3208           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3209           !dev->isYaffs2)
3210         {
3211 //              yaffs_SpareInitialise(&spare);
3212
3213 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3214
3215                 //read data before write, to ensure correct ecc 
3216                 //if we're using MTD verification under Linux
3217                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3218 #endif
3219
3220                 yaffs_InitialiseTags(&tags);
3221
3222                 tags.chunkDeleted = 1;
3223
3224         
3225                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3226                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3227         }
3228         else
3229         {
3230                         dev->nUnmarkedDeletions++;
3231         }       
3232         
3233         
3234         // Pull out of the management area.
3235         // If the whole block became dirty, this will kick off an erasure.
3236         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3237             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3238             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3239             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3240         {
3241                 dev->nFreeChunks++;
3242
3243                 yaffs_ClearChunkBit(dev,block,page);
3244                 bi->pagesInUse--;
3245                 
3246                 if(bi->pagesInUse == 0 &&
3247                    !bi->hasShrinkHeader &&
3248                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3249                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3250             {
3251                 yaffs_BlockBecameDirty(dev,block);
3252             }
3253
3254         }
3255         else
3256         {
3257                 // T(("Bad news deleting chunk %d\n",chunkId));
3258         }
3259         
3260 }
3261
3262
3263
3264
3265 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3266 {
3267         // Find old chunk Need to do this to get serial number
3268         // Write new one and patch into tree.
3269         // Invalidate old tags.
3270
3271     int prevChunkId;
3272     yaffs_ExtendedTags prevTags;
3273     
3274     int newChunkId;
3275     yaffs_ExtendedTags newTags;
3276
3277     yaffs_Device *dev = in->myDev;    
3278
3279         yaffs_CheckGarbageCollection(dev);
3280
3281         // Get the previous chunk at this location in the file if it exists
3282     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3283     
3284     // Set up new tags
3285     yaffs_InitialiseTags(&newTags);
3286     
3287         newTags.chunkId = chunkInInode;
3288         newTags.objectId = in->objectId;
3289         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3290         newTags.byteCount = nBytes;
3291                 
3292 //      yaffs_CalcTagsECC(&newTags);
3293
3294         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3295         
3296         if(newChunkId >= 0)
3297         {
3298                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3299                 
3300                 
3301                 if(prevChunkId >= 0)
3302                 {
3303                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3304         
3305                 }
3306                 
3307                 yaffs_CheckFileSanity(in);
3308         }
3309         return newChunkId;
3310
3311
3312
3313
3314
3315 }
3316
3317
3318 // UpdateObjectHeader updates the header on NAND for an object.
3319 // If name is not NULL, then that new name is used.
3320 //
3321 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3322 {
3323
3324         yaffs_BlockInfo *bi;
3325         
3326         yaffs_Device *dev = in->myDev;
3327         
3328     int prevChunkId;
3329     int retVal = 0;
3330     
3331     int newChunkId;
3332     yaffs_ExtendedTags newTags;
3333     
3334     __u8 *buffer = NULL;
3335     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3336     
3337    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3338     
3339     yaffs_ObjectHeader *oh = NULL;
3340     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3341
3342     
3343     if(!in->fake || force)
3344     {
3345   
3346                 yaffs_CheckGarbageCollection(dev);              
3347     
3348             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3349             oh  = (yaffs_ObjectHeader *)buffer;
3350     
3351                 prevChunkId = in->chunkId;
3352     
3353                 if(prevChunkId >= 0)
3354                 {
3355                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3356                         memcpy(oldName,oh->name,sizeof(oh->name));      
3357                 }
3358
3359                 memset(buffer,0xFF,dev->nBytesPerChunk);
3360
3361                 // Header data
3362                 oh->type = in->variantType;
3363                 
3364                 oh->st_mode = in->st_mode;
3365
3366 #ifdef CONFIG_YAFFS_WINCE
3367                 oh->win_atime[0] = in->win_atime[0];
3368                 oh->win_ctime[0] = in->win_ctime[0];
3369                 oh->win_mtime[0] = in->win_mtime[0];
3370                 oh->win_atime[1] = in->win_atime[1];
3371                 oh->win_ctime[1] = in->win_ctime[1];
3372                 oh->win_mtime[1] = in->win_mtime[1];
3373 #else
3374                 oh->st_uid = in->st_uid;
3375                 oh->st_gid = in->st_gid;
3376                 oh->st_atime = in->st_atime;
3377                 oh->st_mtime = in->st_mtime;
3378                 oh->st_ctime = in->st_ctime;
3379                 oh->st_rdev = in->st_rdev;
3380 #endif  
3381                 if(in->parent)
3382                 {
3383                         oh->parentObjectId = in->parent->objectId;
3384                 }
3385                 else
3386                 {
3387                         oh->parentObjectId = 0;
3388                 }
3389                 
3390                 //oh->sum = in->sum;
3391                 if(name && *name)
3392                 {
3393                         memset(oh->name,0,sizeof(oh->name));
3394                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3395                 }
3396                 else if(prevChunkId)
3397                 {       
3398                         memcpy(oh->name, oldName,sizeof(oh->name));
3399                 }
3400                 else
3401                 {
3402                         memset(oh->name,0,sizeof(oh->name));    
3403                 }
3404                 
3405                 oh->isShrink = isShrink;
3406         
3407                 switch(in->variantType)
3408                 {
3409                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3410                                 // Should not happen
3411                                 break;
3412                         case YAFFS_OBJECT_TYPE_FILE:
3413                                 oh->fileSize = in->variant.fileVariant.fileSize;
3414                                 break;
3415                         case YAFFS_OBJECT_TYPE_HARDLINK:
3416                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3417                                 break;
3418                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3419                                 // Do nothing
3420                                 break;
3421                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3422                                 // Do nothing
3423                                 break;
3424                         case YAFFS_OBJECT_TYPE_SYMLINK:
3425                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3426                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3427                                 break;
3428                 }
3429
3430                 // Tags
3431                 yaffs_InitialiseTags(&newTags);
3432                 in->serial++;
3433                 newTags.chunkId = 0;
3434                 newTags.objectId = in->objectId;
3435                 newTags.serialNumber = in->serial;
3436         
3437
3438                 // Create new chunk in NAND
3439                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3440     
3441                 if(newChunkId >= 0)
3442                 {
3443                 
3444                         in->chunkId = newChunkId;       
3445                 
3446                         if(prevChunkId >= 0)
3447                         {
3448                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3449                         }
3450                 
3451                         in->dirty = 0;
3452                         
3453                         // If this was a shrink, then mark the block that the chunk lives on
3454                         if(isShrink)
3455                         {
3456                                 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3457                                 bi->hasShrinkHeader = 1;
3458                         }
3459                         
3460                 }
3461                 
3462                 retVal =  newChunkId;
3463
3464     }
3465     
3466         if(buffer) 
3467                 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3468         
3469     return retVal;
3470 }
3471
3472
3473 /////////////////////// Short Operations Cache ////////////////////////////////
3474 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3475 //      reads might be short sequential reads, and a lot of writes may be short 
3476 //  sequential writes. eg. scanning/writing a jpeg file.
3477 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3478 //  with dumb-as-a-rock code.
3479 //  There are a limited number (~10) of cache chunks per device so that we don't
3480 //  need a very intelligent search.
3481
3482
3483
3484
3485
3486 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3487 {
3488         yaffs_Device *dev = obj->myDev;
3489         int lowest;
3490         int i;
3491         yaffs_ChunkCache *cache;
3492         int chunkWritten;
3493         //int nBytes;
3494         int nCaches = obj->myDev->nShortOpCaches;
3495         
3496         if  (nCaches > 0)
3497         {
3498                 do{
3499                         cache = NULL;
3500                 
3501                         // Find the dirty cache for this object with the lowest chunk id.
3502                         for(i = 0; i < nCaches; i++)
3503                         {
3504                                 if(dev->srCache[i].object == obj &&
3505                                    dev->srCache[i].dirty)
3506                                 {
3507                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3508                                         {
3509                                                 cache = &dev->srCache[i];
3510                                                 lowest = cache->chunkId;
3511                                         }
3512                                 }
3513                         }
3514                 
3515                         if(cache && !cache->locked)
3516                         {
3517                                 //Write it out and free it up
3518
3519 #if 0
3520                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3521                         
3522                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3523                                 {
3524                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3525                                 }
3526 #endif                  
3527                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3528                                                                                                                         cache->chunkId,
3529                                                                                                                         cache->data,
3530                                                                                                                         cache->nBytes,1);
3531
3532                                 cache->dirty = 0;
3533                                 cache->object = NULL;
3534                         }
3535                 
3536                 } while(cache && chunkWritten > 0);
3537         
3538                 if(cache)
3539                 {
3540                         //Hoosterman, disk full while writing cache out.
3541                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3542
3543                 }
3544         }       
3545                 
3546 }
3547
3548
3549 // Grab us a chunk for use.
3550 // First look for an empty one. 
3551 // Then look for the least recently used non-dirty one.
3552 // Then look for the least recently used dirty one...., flush and look again.
3553 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3554 {
3555         int i;
3556         int usage;
3557         int theOne;
3558         
3559         if(dev->nShortOpCaches > 0)
3560         {
3561                 for(i = 0; i < dev->nShortOpCaches; i++)
3562                 {
3563                         if(!dev->srCache[i].object)
3564                         {
3565                                 //T(("Grabbing empty %d\n",i));
3566                                 
3567                                 //printf("Grabbing empty %d\n",i);
3568                         
3569                                 return &dev->srCache[i];
3570                         }
3571                 }
3572                 
3573                 return NULL;
3574         
3575                 theOne = -1; 
3576                 usage = 0; // just to stop the compiler grizzling
3577         
3578                 for(i = 0; i < dev->nShortOpCaches; i++)
3579                 {
3580                         if(!dev->srCache[i].dirty &&
3581                         ((dev->srCache[i].lastUse < usage  && theOne >= 0)|| 
3582                                 theOne < 0))
3583                         {
3584                                 usage = dev->srCache[i].lastUse;
3585                                 theOne = i;
3586                         }
3587                 }
3588         
3589                 //T(("Grabbing non-empty %d\n",theOne));
3590                 
3591                 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3592                 
3593                 return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
3594         }
3595         else
3596         {
3597                 return NULL;
3598         }
3599         
3600 }
3601
3602
3603 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3604 {
3605         yaffs_ChunkCache *cache;
3606         yaffs_Object *theObj;
3607         int usage;
3608         int i;
3609         int pushout;
3610         
3611         if(dev->nShortOpCaches > 0)
3612         {
3613                 // Try find a non-dirty one...
3614         
3615                 cache = yaffs_GrabChunkCacheWorker(dev);
3616         
3617                 if(!cache)
3618                 {
3619                         // They were all dirty, find the last recently used object and flush
3620                         // its cache, then  find again.
3621                         // NB what's here is not very accurate, we actually flush the object
3622                         // the last recently used page.
3623                 
3624                         theObj = NULL;
3625                         usage = 0;
3626                         cache = NULL;
3627                         pushout = 0;
3628         
3629                         for(i = 0; i < dev->nShortOpCaches; i++)
3630                         {
3631                                 if( dev->srCache[i].object && 
3632                                         !dev->srCache[i].locked &&
3633                                         (dev->srCache[i].lastUse < usage || !cache))
3634                                 {
3635                                         usage  = dev->srCache[i].lastUse;
3636                                         theObj = dev->srCache[i].object;
3637                                         cache = &dev->srCache[i];
3638                                         pushout = i;
3639                                 }
3640                         }
3641                 
3642                         if(!cache || cache->dirty)
3643                         {
3644                         
3645                                 //printf("Dirty ");
3646                                 yaffs_FlushFilesChunkCache(theObj);
3647                 
3648                                 // Try again
3649                                 cache = yaffs_GrabChunkCacheWorker(dev);
3650                         }
3651                         else
3652                         {
3653                                 //printf(" pushout %d\n",pushout);
3654                         }
3655                         
3656                 }
3657
3658                 return cache;
3659         }
3660         else
3661                 return NULL;
3662
3663 }
3664
3665
3666 // Find a cached chunk
3667 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3668 {
3669         yaffs_Device *dev = obj->myDev;
3670         int i;
3671         if(dev->nShortOpCaches > 0)
3672         {
3673                 for(i = 0; i < dev->nShortOpCaches; i++)
3674                 {
3675                         if(dev->srCache[i].object == obj && 
3676                         dev->srCache[i].chunkId == chunkId)
3677                         {
3678                                 dev->cacheHits++;
3679                         
3680                                 return &dev->srCache[i];
3681                         }
3682                 }
3683         }
3684         return NULL;
3685 }
3686
3687 // Mark the chunk for the least recently used algorithym
3688 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3689 {
3690
3691         if(dev->nShortOpCaches > 0)
3692         {
3693                 if( dev->srLastUse < 0 || 
3694                         dev->srLastUse > 100000000)
3695                 {
3696                         // Reset the cache usages
3697                         int i;
3698                         for(i = 1; i < dev->nShortOpCaches; i++)
3699                         {
3700                                 dev->srCache[i].lastUse = 0;
3701                         }
3702                         dev->srLastUse = 0;
3703                 }
3704
3705                 dev->srLastUse++;
3706         
3707                 cache->lastUse = dev->srLastUse;
3708
3709                 if(isAWrite)
3710                 {
3711                         cache->dirty = 1;
3712                 }
3713         }
3714 }
3715
3716 // Invalidate a single cache page.
3717 // Do this when a whole page gets written,
3718 // ie the short cache for this page is no longer valid.
3719 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3720 {
3721         if(object->myDev->nShortOpCaches > 0)
3722         {
3723                 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3724
3725                 if(cache)
3726                 {
3727                         cache->object = NULL;
3728                 }
3729         }
3730 }
3731
3732
3733 // Invalidate all the cache pages associated with this object
3734 // Do this whenever ther file is deleted or resized.
3735 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3736 {
3737         int i;
3738         yaffs_Device *dev = in->myDev;
3739         
3740         if(dev->nShortOpCaches > 0)
3741         { 
3742                 // Now invalidate it.
3743                 for(i = 0; i < dev->nShortOpCaches; i++)
3744                 {
3745                         if(dev->srCache[i].object == in)
3746                         {
3747                                 dev->srCache[i].object = NULL;
3748                         }
3749                 }
3750         }
3751 }
3752
3753
3754
3755
3756
3757 ///////////////////////// File read/write ///////////////////////////////
3758 // Read and write have very similar structures.
3759 // In general the read/write has three parts to it
3760 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3761 // * Some complete chunks
3762 // * An incomplete chunk to end off with
3763 //
3764 // Curve-balls: the first chunk might also be the last chunk.
3765
3766 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3767 {
3768         
3769         
3770         int chunk;
3771         int start;
3772         int nToCopy;
3773         int n = nBytes;
3774         int nDone = 0;
3775         yaffs_ChunkCache *cache;
3776         
3777         yaffs_Device *dev;
3778         
3779         dev = in->myDev;
3780         
3781         while(n > 0)
3782         {
3783                 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3784                 start = offset % dev->nBytesPerChunk;
3785
3786                 // OK now check for the curveball where the start and end are in
3787                 // the same chunk.      
3788                 if(     (start + n) < dev->nBytesPerChunk)
3789                 {
3790                         nToCopy = n;
3791                 }
3792                 else
3793                 {
3794                         nToCopy = dev->nBytesPerChunk - start;
3795                 }
3796         
3797                 cache = yaffs_FindChunkCache(in,chunk);
3798                 
3799                 // If the chunk is already in the cache or it is less than a whole chunk
3800                 // then use the cache (if there is caching)
3801                 // else bypass the cache.
3802                 if( cache || nToCopy != dev->nBytesPerChunk)
3803                 {
3804                         if(dev->nShortOpCaches > 0)
3805                         {
3806                                 
3807                                 // If we can't find the data in the cache, then load it up.
3808                                 
3809                                 if(!cache)
3810                                 {
3811                                         cache = yaffs_GrabChunkCache(in->myDev);
3812                                         cache->object = in;
3813                                         cache->chunkId = chunk;
3814                                         cache->dirty = 0;
3815                                         cache->locked = 0;
3816                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3817                                         cache->nBytes = 0;      
3818                                 }
3819                         
3820                                 yaffs_UseChunkCache(dev,cache,0);
3821
3822                                 cache->locked = 1;
3823
3824 #ifdef CONFIG_YAFFS_WINCE
3825                                 yfsd_UnlockYAFFS(TRUE);
3826 #endif
3827                                 memcpy(buffer,&cache->data[start],nToCopy);
3828
3829 #ifdef CONFIG_YAFFS_WINCE
3830                                 yfsd_LockYAFFS(TRUE);
3831 #endif
3832                                 cache->locked = 0;
3833                         }
3834                         else
3835                         {
3836                                 // Read into the local buffer then copy...
3837                                 
3838                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3839                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);            
3840 #ifdef CONFIG_YAFFS_WINCE
3841                                 yfsd_UnlockYAFFS(TRUE);
3842 #endif
3843                                 memcpy(buffer,&localBuffer[start],nToCopy);
3844
3845 #ifdef CONFIG_YAFFS_WINCE
3846                                 yfsd_LockYAFFS(TRUE);
3847 #endif
3848                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3849                         }
3850
3851                 }
3852                 else
3853                 {
3854 #ifdef CONFIG_YAFFS_WINCE
3855                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3856                         
3857                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3858                         // This is because we otherwise screw up WinCE's memory mapper
3859                         yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3860
3861 #ifdef CONFIG_YAFFS_WINCE
3862                                 yfsd_UnlockYAFFS(TRUE);
3863 #endif
3864                         memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3865
3866 #ifdef CONFIG_YAFFS_WINCE
3867                                 yfsd_LockYAFFS(TRUE);
3868                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3869 #endif
3870
3871 #else
3872                         // A full chunk. Read directly into the supplied buffer.
3873                         yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3874 #endif
3875                 }
3876                 
3877                 n -= nToCopy;
3878                 offset += nToCopy;
3879                 buffer += nToCopy;
3880                 nDone += nToCopy;
3881                 
3882         }
3883         
3884         return nDone;
3885 }
3886
3887
3888
3889 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3890 {       
3891         
3892         int chunk;
3893         int start;
3894         int nToCopy;
3895         int n = nBytes;
3896         int nDone = 0;
3897         int nToWriteBack;
3898         int startOfWrite = offset;
3899         int chunkWritten = 0;
3900         int nBytesRead;
3901         
3902         yaffs_Device *dev;
3903         
3904         dev = in->myDev;
3905         
3906         
3907         while(n > 0 && chunkWritten >= 0)
3908         {
3909                 chunk = offset / dev->nBytesPerChunk + 1;
3910                 start = offset % dev->nBytesPerChunk;
3911                 
3912
3913                 // OK now check for the curveball where the start and end are in
3914                 // the same chunk.
3915                 
3916                 if(     (start + n) < dev->nBytesPerChunk)
3917                 {
3918                         nToCopy = n;
3919                         
3920                         // Now folks, to calculate how many bytes to write back....
3921                         // If we're overwriting and not writing to then end of file then
3922                         // we need to write back as much as was there before.
3923                         
3924                         nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3925                         
3926                         if(nBytesRead > dev->nBytesPerChunk)
3927                         {
3928                                 nBytesRead = dev->nBytesPerChunk;
3929                         }
3930                         
3931                         nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3932                         
3933                 }
3934                 else
3935                 {
3936                         nToCopy = dev->nBytesPerChunk - start;
3937                         nToWriteBack = dev->nBytesPerChunk;
3938                 }
3939         
3940                 if(nToCopy != dev->nBytesPerChunk)
3941                 {
3942                         // An incomplete start or end chunk (or maybe both start and end chunk)
3943                         if(dev->nShortOpCaches > 0)
3944                         {
3945                                 yaffs_ChunkCache *cache;
3946                                 // If we can't find the data in the cache, then load it up.
3947                                 cache = yaffs_FindChunkCache(in,chunk);
3948                                 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3949                                 {
3950                                         cache = yaffs_GrabChunkCache(in->myDev);
3951                                         cache->object = in;
3952                                         cache->chunkId = chunk;
3953                                         cache->dirty = 0;
3954                                         cache->locked = 0;
3955                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);            
3956                                 }
3957                         
3958                                 if(cache)
3959                                 {       
3960                                         yaffs_UseChunkCache(dev,cache,1);
3961                                         cache->locked = 1;
3962 #ifdef CONFIG_YAFFS_WINCE
3963                                 yfsd_UnlockYAFFS(TRUE);
3964 #endif
3965         
3966                                         memcpy(&cache->data[start],buffer,nToCopy);
3967
3968 #ifdef CONFIG_YAFFS_WINCE
3969                                 yfsd_LockYAFFS(TRUE);
3970 #endif
3971                                         cache->locked = 0;
3972                                         cache->nBytes = nToWriteBack;
3973                                 }
3974                                 else
3975                                 {
3976                                         chunkWritten = -1; // fail the write
3977                                 }
3978                         }
3979                         else
3980                         {
3981                                 // An incomplete start or end chunk (or maybe both start and end chunk)
3982                                 // Read into the local buffer then copy, then copy over and write back.
3983                                 
3984                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3985                 
3986                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3987                                 
3988 #ifdef CONFIG_YAFFS_WINCE
3989                                 yfsd_UnlockYAFFS(TRUE);
3990 #endif
3991                                         
3992                                 memcpy(&localBuffer[start],buffer,nToCopy);
3993                         
3994 #ifdef CONFIG_YAFFS_WINCE
3995                                 yfsd_LockYAFFS(TRUE);
3996 #endif
3997                                 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
3998                                 
3999                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4000                         
4001                                 //T(("Write with readback to chunk %d %d  start %d  copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
4002                         }
4003                         
4004                 }
4005                 else
4006                 {
4007                         
4008 #ifdef CONFIG_YAFFS_WINCE
4009                         // Under WinCE can't do direct transfer. Need to use a local buffer.
4010                         // This is because we otherwise screw up WinCE's memory mapper
4011                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4012 #ifdef CONFIG_YAFFS_WINCE
4013                                 yfsd_UnlockYAFFS(TRUE);
4014 #endif
4015                         memcpy(localBuffer,buffer,dev->nBytesPerChunk);
4016 #ifdef CONFIG_YAFFS_WINCE
4017                                 yfsd_LockYAFFS(TRUE);
4018 #endif
4019                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
4020                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4021 #else
4022                         // A full chunk. Write directly from the supplied buffer.
4023                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
4024 #endif
4025                         // Since we've overwritten the cached data, we better invalidate it.
4026                         yaffs_InvalidateChunkCache(in,chunk);
4027                         //T(("Write to chunk %d %d\n",chunk,chunkWritten));
4028                 }
4029                 
4030                 if(chunkWritten >= 0)
4031                 {
4032                         n -= nToCopy;
4033                         offset += nToCopy;
4034                         buffer += nToCopy;
4035                         nDone += nToCopy;
4036                 }
4037                 
4038         }
4039         
4040         // Update file object
4041         
4042         if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
4043         {
4044                 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4045         }
4046         
4047         in->dirty = 1;
4048         
4049         return nDone;
4050 }
4051
4052 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
4053 {
4054
4055         yaffs_Device *dev = in->myDev;
4056         int oldFileSize = in->variant.fileVariant.fileSize;
4057
4058         
4059         int lastDel = 1 + (oldFileSize-1)/dev->nBytesPerChunk;
4060                 
4061         int startDel = 1 + (newSize + dev->nBytesPerChunk - 1)/
4062                                                 dev->nBytesPerChunk;
4063         int i;
4064         int chunkId;
4065
4066         // Delete backwards so that we don't end up with holes if
4067         // power is lost part-way through the operation.
4068         for(i = lastDel; i >= startDel; i--)
4069         {
4070                 // NB this could be optimised somewhat,
4071                 // eg. could retrieve the tags and write them without
4072                 // using yaffs_DeleteChunk
4073
4074                 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
4075                 if(chunkId > 0)
4076                 {
4077                         if(chunkId < (dev->startBlock * dev->nChunksPerBlock) || 
4078                        chunkId >= ((dev->endBlock+1) * dev->nChunksPerBlock))
4079                         {
4080                                 T(YAFFS_TRACE_ALWAYS,("Found daft chunkId %d for %d\n",chunkId,i));
4081                         }
4082                         else
4083                         {
4084                                 in->nDataChunks--;
4085                                 yaffs_DeleteChunk(dev,chunkId,1,__LINE__);
4086                         }
4087                 }
4088         }
4089                 
4090 }
4091
4092 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
4093 {
4094
4095         int oldFileSize = in->variant.fileVariant.fileSize;
4096         int sizeOfPartialChunk;
4097         yaffs_Device *dev = in->myDev;
4098         
4099          sizeOfPartialChunk  = newSize % dev->nBytesPerChunk;
4100                 
4101
4102         yaffs_FlushFilesChunkCache(in); 
4103         yaffs_InvalidateWholeChunkCache(in);
4104
4105         yaffs_CheckGarbageCollection(dev);
4106         
4107         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
4108         {
4109                 return yaffs_GetFileSize(in);
4110         }
4111         
4112         if(newSize < oldFileSize)
4113         {
4114
4115                 yaffs_PruneResizedChunks(in,newSize);
4116                 
4117                 if(sizeOfPartialChunk != 0)
4118                 {
4119                         int lastChunk = 1+ newSize/dev->nBytesPerChunk;
4120                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4121                         
4122                         // Got to read and rewrite the last chunk with its new size and zero pad
4123                         yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
4124                         
4125                         memset(localBuffer + sizeOfPartialChunk,0, dev->nBytesPerChunk - sizeOfPartialChunk);
4126                         
4127                         yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
4128                                 
4129                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4130                 }
4131                 
4132                 in->variant.fileVariant.fileSize = newSize;
4133                 
4134                 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
4135                 
4136                 // TODO write a new object header to show we've shrunk the file
4137                 // Do this only if the file is not in the deleted directory.
4138                 if(in->parent->objectId != YAFFS_OBJECTID_UNLINKED)
4139                 {
4140                         yaffs_UpdateObjectHeader(in,NULL, 0, 1);
4141                 }
4142
4143                 
4144                 return newSize;
4145                 
4146         }
4147         else
4148         {
4149                 return oldFileSize;
4150         }
4151 }
4152
4153
4154 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4155 {
4156         obj = yaffs_GetEquivalentObject(obj);
4157         
4158         switch(obj->variantType)
4159         {
4160                 case YAFFS_OBJECT_TYPE_FILE: 
4161                         return obj->variant.fileVariant.fileSize;
4162                 case YAFFS_OBJECT_TYPE_SYMLINK:
4163                         return yaffs_strlen(obj->variant.symLinkVariant.alias);
4164                 default:
4165                         return 0;
4166         }
4167 }
4168
4169
4170
4171 // yaffs_FlushFile() updates the file's
4172 // objectId in NAND
4173
4174 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
4175 {
4176         int retVal;
4177         if(in->dirty)
4178         {
4179                 //T(("flushing object header\n"));
4180                 
4181                 yaffs_FlushFilesChunkCache(in);
4182                 if(updateTime)
4183                 {
4184 #ifdef CONFIG_YAFFS_WINCE
4185                         yfsd_WinFileTimeNow(in->win_mtime);
4186 #else
4187
4188                         in->st_mtime = Y_CURRENT_TIME;
4189
4190 #endif
4191                 }
4192
4193                 retVal = (yaffs_UpdateObjectHeader(in,NULL,0,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
4194         }
4195         else
4196         {
4197                 retVal = YAFFS_OK;
4198         }
4199         
4200         return retVal;
4201         
4202 }
4203
4204
4205 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4206 {
4207
4208         // First off, invalidate the file's data in the cache, without flushing.
4209         yaffs_InvalidateWholeChunkCache(in);
4210
4211         if(in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir))
4212         {
4213                 // Move to the unlinked directory so we have a record that it was deleted.
4214                 yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4215
4216         }
4217
4218         
4219         yaffs_RemoveObjectFromDirectory(in);
4220         yaffs_DeleteChunk(in->myDev,in->chunkId,1,__LINE__);
4221         in->chunkId = -1;
4222
4223 #ifdef __KERNEL__
4224         if(in->myInode)
4225         {
4226                 in->myInode->u.generic_ip = NULL;
4227                 in->myInode = 0;
4228         }
4229 #endif
4230         yaffs_FreeObject(in);
4231         return YAFFS_OK;
4232
4233 }
4234
4235 // yaffs_DeleteFile deletes the whole file data
4236 // and the inode associated with the file.
4237 // It does not delete the links associated with the file.
4238 static int yaffs_UnlinkFile(yaffs_Object *in)
4239 {
4240
4241 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
4242
4243         // Delete the file data & tnodes
4244
4245          yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
4246          
4247
4248         yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4249         
4250         return  yaffs_DoGenericObjectDeletion(in);
4251 #else
4252         int retVal;
4253         int immediateDeletion=0;
4254
4255         if(1)
4256         {
4257                 //in->unlinked = 1;
4258                 //in->myDev->nUnlinkedFiles++;
4259                 //in->renameAllowed = 0;
4260 #ifdef __KERNEL__
4261                 if(!in->myInode)
4262                 {
4263                         immediateDeletion = 1;
4264
4265                 }
4266 #else
4267                 if(in->inUse <= 0)
4268                 {
4269                         immediateDeletion = 1;
4270
4271                 }
4272 #endif
4273                 if(immediateDeletion)
4274                 {
4275                         retVal = yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4276                         T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
4277                         in->deleted=1;
4278                         in->myDev->nDeletedFiles++;
4279                         yaffs_SoftDeleteFile(in);
4280                 }
4281                 else
4282                 {
4283                         retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
4284                 }
4285         
4286         }
4287         return retVal;
4288
4289         
4290 #endif
4291 }
4292
4293 int yaffs_DeleteFile(yaffs_Object *in)
4294 {
4295         int retVal = YAFFS_OK;
4296         
4297         if(in->nDataChunks > 0)
4298         {
4299                 // Use soft deletion
4300                 if(!in->unlinked)
4301                 {
4302                         retVal = yaffs_UnlinkFile(in);
4303                 }
4304                 if(retVal == YAFFS_OK && 
4305                 in->unlinked &&
4306                 !in->deleted)
4307                 {
4308                         in->deleted = 1;
4309                         in->myDev->nDeletedFiles++;
4310                         yaffs_SoftDeleteFile(in);
4311                 }
4312                 return in->deleted ? YAFFS_OK : YAFFS_FAIL;     
4313         }
4314         else
4315         {
4316                 // The file has no data chunks so we toss it immediately
4317                 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4318                 in->variant.fileVariant.top = NULL;
4319                 yaffs_DoGenericObjectDeletion(in);      
4320                 
4321                 return YAFFS_OK;        
4322         }
4323 }
4324
4325 static int yaffs_DeleteDirectory(yaffs_Object *in)
4326 {
4327         //First check that the directory is empty.
4328         if(list_empty(&in->variant.directoryVariant.children))
4329         {
4330                 return  yaffs_DoGenericObjectDeletion(in);
4331         }
4332         
4333         return YAFFS_FAIL;
4334         
4335 }
4336
4337 static int yaffs_DeleteSymLink(yaffs_Object *in)
4338 {
4339         YFREE(in->variant.symLinkVariant.alias);
4340
4341         return  yaffs_DoGenericObjectDeletion(in);
4342 }
4343
4344 static int yaffs_DeleteHardLink(yaffs_Object *in)
4345 {
4346         // remove this hardlink from the list assocaited with the equivalent
4347         // object
4348         list_del(&in->hardLinks);
4349         return  yaffs_DoGenericObjectDeletion(in);      
4350 }
4351
4352
4353 static void yaffs_DestroyObject(yaffs_Object *obj)
4354 {
4355         switch(obj->variantType)
4356         {
4357                 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
4358                 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
4359                 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
4360                 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
4361                 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
4362                 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
4363         }
4364 }
4365
4366
4367 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4368 {
4369
4370         
4371         if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4372         {
4373                 return  yaffs_DeleteHardLink(obj);
4374         }
4375         else if(!list_empty(&obj->hardLinks))
4376         {       
4377                 // Curve ball: We're unlinking an object that has a hardlink.
4378                 //
4379                 //      This problem arises because we are not strictly following
4380                 //  The Linux link/inode model.
4381                 //
4382                 // We can't really delete the object.
4383                 // Instead, we do the following:
4384                 // - Select a hardlink.
4385                 // - Unhook it from the hard links
4386                 // - Unhook it from its parent directory (so that the rename can work)
4387                 // - Rename the object to the hardlink's name.
4388                 // - Delete the hardlink
4389                 
4390                 
4391                 yaffs_Object *hl;
4392                 int retVal;
4393                 YCHAR name[YAFFS_MAX_NAME_LENGTH+1];
4394                 
4395                 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
4396                 
4397                 list_del_init(&hl->hardLinks);
4398                 list_del_init(&hl->siblings);
4399                 
4400                 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
4401                 
4402                 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
4403                 
4404                 if(retVal == YAFFS_OK)
4405                 {
4406                         retVal = yaffs_DoGenericObjectDeletion(hl);
4407                 }
4408                 return retVal;
4409                                 
4410         }
4411         else
4412         {
4413                 switch(obj->variantType)
4414                 {
4415                         case YAFFS_OBJECT_TYPE_FILE:
4416                                 return yaffs_UnlinkFile(obj);
4417                                 break;
4418                         case YAFFS_OBJECT_TYPE_DIRECTORY:
4419                                 return yaffs_DeleteDirectory(obj);
4420                                 break;
4421                         case YAFFS_OBJECT_TYPE_SYMLINK:
4422                                 return yaffs_DeleteSymLink(obj);
4423                                 break;
4424                         case YAFFS_OBJECT_TYPE_HARDLINK:
4425                         case YAFFS_OBJECT_TYPE_UNKNOWN:
4426                         default:
4427                                 return YAFFS_FAIL;
4428                 }
4429         }
4430 }
4431
4432 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4433 {
4434         yaffs_Object *obj;
4435         
4436          obj = yaffs_FindObjectByName(dir,name);
4437          
4438          if(obj && obj->unlinkAllowed)
4439          {
4440                 return yaffs_UnlinkWorker(obj);
4441          }
4442          
4443          return YAFFS_FAIL;
4444         
4445 }
4446
4447 //////////////// Initialisation Scanning /////////////////
4448
4449
4450 #if 0
4451 // For now we use the SmartMedia check.
4452 // We look at the blockStatus byte in the first two chunks
4453 // These must be 0xFF to pass as OK.
4454 // todo: this function needs to be modifyable foir different NAND types
4455 // and different chunk sizes.  Suggest make this into a per-device configurable
4456 // function.
4457 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4458 {
4459         yaffsExtendedTags *tags;
4460         
4461         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&tags,1);
4462 #if 1
4463         if(yaffs_CountBits(spare.blockStatus) < 7)
4464         {
4465                 return 1;
4466         }
4467 #else
4468         if(spare.blockStatus != 0xFF)
4469         {
4470                 return 1;
4471         }
4472 #endif
4473         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4474
4475 #if 1
4476         if(yaffs_CountBits(spare.blockStatus) < 7)
4477         {
4478                 return 1;
4479         }
4480 #else
4481         if(spare.blockStatus != 0xFF)
4482         {
4483                 return 1;
4484         }
4485 #endif
4486         
4487         return 0;
4488         
4489 }
4490
4491 #endif
4492
4493
4494 typedef struct 
4495 {
4496         int seq;
4497         int block;
4498 } yaffs_BlockIndex;
4499
4500
4501
4502 static int yaffs_Scan(yaffs_Device *dev)
4503 {
4504         yaffs_ExtendedTags tags;
4505         int blk;
4506         int blockIterator;
4507         int startIterator;
4508         int endIterator;
4509         int nBlocksToScan = 0;
4510         
4511         int chunk;
4512         int c;
4513         int deleted;
4514         yaffs_BlockState state;
4515         yaffs_Object *hardList = NULL;
4516         yaffs_Object *hl;
4517         yaffs_BlockInfo *bi;
4518         int sequenceNumber;     
4519         yaffs_ObjectHeader *oh;
4520         yaffs_Object *in;
4521         yaffs_Object *parent;
4522         int nBlocks = dev->endBlock - dev->startBlock + 1;
4523         
4524         __u8 *chunkData;
4525
4526         yaffs_BlockIndex *blockIndex = NULL;
4527
4528         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan starts..." TENDSTR)));
4529         
4530         chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4531         
4532         
4533         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4534         
4535         if(dev->isYaffs2)
4536         {
4537                 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));               
4538         }
4539         
4540         
4541         // Scan all the blocks to determine their state
4542         for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
4543         {
4544                 bi = yaffs_GetBlockInfo(dev,blk);
4545                 yaffs_ClearChunkBits(dev,blk);
4546                 bi->pagesInUse = 0;
4547                 bi->softDeletions = 0;
4548                                 
4549                 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4550                 
4551                 bi->blockState = state;
4552                 bi->sequenceNumber = sequenceNumber;
4553                 
4554                 if(state == YAFFS_BLOCK_STATE_DEAD)
4555                 {
4556                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4557                 }
4558                 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4559                 {
4560                         dev->nErasedBlocks++;
4561                         dev->nFreeChunks += dev->nChunksPerBlock;
4562                 }
4563                 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4564                 {
4565                                         
4566                         // Determine the highest sequence number
4567                         if( dev->isYaffs2 &&
4568                             sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
4569                             sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
4570                          {
4571                                 
4572                                 blockIndex[nBlocksToScan].seq = sequenceNumber;
4573                                 blockIndex[nBlocksToScan].block = blk;
4574                                 
4575                                 nBlocksToScan++;
4576
4577                                 if(sequenceNumber >= dev->sequenceNumber)
4578                                 {
4579                                         dev->sequenceNumber = sequenceNumber;
4580                                 }
4581                         }
4582                         else if(dev->isYaffs2)
4583                         {
4584                                 // TODO: Nasty sequence number!
4585                                 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
4586
4587                         }
4588                 }
4589         }
4590         
4591         // Sort the blocks
4592         // Dungy old bubble sort for now...
4593         if(dev->isYaffs2)
4594         {
4595                 yaffs_BlockIndex temp;
4596                 int i;
4597                 int j;
4598                 
4599                 for(i = 0; i < nBlocksToScan; i++)
4600                         for(j = i+1; j < nBlocksToScan; j++)
4601                          if(blockIndex[i].seq > blockIndex[j].seq)
4602                          {
4603                                 temp = blockIndex[j];
4604                                 blockIndex[j] = blockIndex[i];
4605                                 blockIndex[i] = temp;
4606                          }
4607         }
4608         
4609         
4610         // Now scan the blocks looking at the data.
4611         if(dev->isYaffs2)
4612         {
4613                 startIterator = 0;
4614                 endIterator = nBlocksToScan-1;
4615         }
4616         else
4617         {
4618                 startIterator = dev->startBlock;
4619                 endIterator = dev->endBlock;
4620         }
4621         
4622         for(blockIterator = startIterator; blockIterator <= endIterator; blockIterator++)
4623         {
4624         
4625                 if(dev->isYaffs2)
4626                 {
4627                         // get the block to scan in the correct order
4628                         blk = blockIndex[blockIterator].block;
4629                 }
4630                 else
4631                 {
4632                         blk = blockIterator;
4633                 }
4634
4635
4636                 bi = yaffs_GetBlockInfo(dev,blk);
4637                 state = bi->blockState;
4638                 
4639                 deleted = 0;
4640                 
4641                 for(c = 0; c < dev->nChunksPerBlock && 
4642                                    state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++)
4643                 {
4644                         // Read the tags and decide what to do
4645                         chunk = blk * dev->nChunksPerBlock + c;
4646                         
4647                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
4648
4649                         // Let's have a good look at this chunk...
4650         
4651                         
4652                         if(!dev->isYaffs2 && tags.chunkDeleted)
4653                         {
4654                                 // A deleted chunk
4655                                 deleted++;
4656                                 dev->nFreeChunks ++;
4657                                 //T((" %d %d deleted\n",blk,c));
4658                         }
4659                         else if(!tags.chunkUsed)
4660                         {
4661                                 // An unassigned chunk in the block
4662                                 // This means that either the block is empty or 
4663                                 // this is the one being allocated from
4664                                 
4665                                 if(c == 0)
4666                                 {
4667                                         // We're looking at the first chunk in the block so the block is unused
4668                                         state = YAFFS_BLOCK_STATE_EMPTY;
4669                                         dev->nErasedBlocks++;
4670                                 }
4671                                 else
4672                                 {
4673                                         // this is the block being allocated from
4674                                         T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4675                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
4676                                         dev->allocationBlock = blk;
4677                                         dev->allocationPage = c;
4678                                         dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
4679                                                                                                           // go forth from here.
4680                                         //Yaffs2 sanity check:
4681                                         // This should be the one with the highest sequence number
4682                                         if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
4683                                         {
4684                                                 T(YAFFS_TRACE_ALWAYS,
4685                                                                 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
4686                                                                 blk,bi->sequenceNumber,dev->sequenceNumber));
4687                                         }
4688                                 }
4689
4690                                 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4691                         }
4692                         else if(tags.chunkId > 0)
4693                         {
4694                                 int endpos;
4695                                 // A data chunk.
4696                                 yaffs_SetChunkBit(dev,blk,c);
4697                                 bi->pagesInUse++;
4698                                                                 
4699                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4700                                 // PutChunkIntoFIle checks for a clash (two data chunks with
4701                                 // the same chunkId).
4702                                 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4703                                 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
4704                                 if(in->variant.fileVariant.scannedFileSize <endpos)
4705                                 {
4706                                         in->variant.fileVariant.scannedFileSize = endpos;
4707                                         if(!dev->useHeaderFileSize)
4708                                         {
4709                                                 in->variant.fileVariant.fileSize =      
4710                                                         in->variant.fileVariant.scannedFileSize;
4711                                         }
4712
4713                                 }
4714                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
4715                         }
4716                         else
4717                         {
4718                                 // chunkId == 0, so it is an ObjectHeader.
4719                                 // Thus, we read in the object header and make the object
4720                                 yaffs_SetChunkBit(dev,blk,c);
4721                                 bi->pagesInUse++;
4722                                                         
4723                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
4724                                 
4725                                 oh = (yaffs_ObjectHeader *)chunkData;
4726                                 
4727                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4728                                 
4729                                 if(in->valid)
4730                                 {
4731                                         // We have already filled this one. We have a duplicate and need to resolve it.
4732                                         
4733                                         unsigned existingSerial = in->serial;
4734                                         unsigned newSerial = tags.serialNumber;
4735                                         
4736                                         if( dev->isYaffs2 ||
4737                                             ((existingSerial+1) & 3) == newSerial)
4738                                         {
4739                                                 // Use new one - destroy the exisiting one
4740                                                 yaffs_DeleteChunk(dev,in->chunkId,1,__LINE__);
4741                                                 in->valid = 0;
4742                                         }
4743                                         else
4744                                         {
4745                                                 // Use existing - destroy this one.
4746                                                 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
4747                                         }
4748                                 }
4749                                 
4750                                 if(!in->valid &&
4751                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
4752                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4753                                 {
4754                                         // We only load some info, don't fiddle with directory structure
4755                                         in->valid = 1;
4756                                         in->variantType = oh->type;
4757         
4758                                         in->st_mode  = oh->st_mode;
4759 #ifdef CONFIG_YAFFS_WINCE
4760                                         in->win_atime[0] = oh->win_atime[0];
4761                                         in->win_ctime[0] = oh->win_ctime[0];
4762                                         in->win_mtime[0] = oh->win_mtime[0];
4763                                         in->win_atime[1] = oh->win_atime[1];
4764                                         in->win_ctime[1] = oh->win_ctime[1];
4765                                         in->win_mtime[1] = oh->win_mtime[1];
4766 #else
4767                                         in->st_uid   = oh->st_uid;
4768                                         in->st_gid   = oh->st_gid;
4769                                         in->st_atime = oh->st_atime;
4770                                         in->st_mtime = oh->st_mtime;
4771                                         in->st_ctime = oh->st_ctime;
4772                                         in->st_rdev = oh->st_rdev;
4773 #endif
4774                                         in->chunkId  = chunk;
4775
4776                                 }
4777                                 else if(!in->valid)
4778                                 {
4779                                         // we need to load this info
4780                                 
4781                                         in->valid = 1;
4782                                         in->variantType = oh->type;
4783         
4784                                         in->st_mode  = oh->st_mode;
4785 #ifdef CONFIG_YAFFS_WINCE
4786                                         in->win_atime[0] = oh->win_atime[0];
4787                                         in->win_ctime[0] = oh->win_ctime[0];
4788                                         in->win_mtime[0] = oh->win_mtime[0];
4789                                         in->win_atime[1] = oh->win_atime[1];
4790                                         in->win_ctime[1] = oh->win_ctime[1];
4791                                         in->win_mtime[1] = oh->win_mtime[1];
4792 #else
4793                                         in->st_uid   = oh->st_uid;
4794                                         in->st_gid   = oh->st_gid;
4795                                         in->st_atime = oh->st_atime;
4796                                         in->st_mtime = oh->st_mtime;
4797                                         in->st_ctime = oh->st_ctime;
4798                                         in->st_rdev = oh->st_rdev;
4799 #endif
4800                                         in->chunkId  = chunk;
4801
4802                                         yaffs_SetObjectName(in,oh->name);
4803                                         in->dirty = 0;
4804                                                         
4805                                         // directory stuff...
4806                                         // hook up to parent
4807         
4808                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4809                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4810                                         {
4811                                                 // Set up as a directory
4812                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4813                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4814                                         }
4815                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4816                                         {
4817                                                 // Hoosterman, another problem....
4818                                                 // We're trying to use a non-directory as a directory
4819
4820                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4821                                                 parent = dev->lostNFoundDir;
4822                                         }
4823                                 
4824                                         yaffs_AddObjectToDirectory(parent,in);
4825
4826                                         if(0 && (parent == dev->deletedDir ||
4827                                            parent == dev->unlinkedDir))
4828                                         {
4829                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4830                                                 dev->nDeletedFiles++;
4831                                         }
4832                                 
4833                                         // Note re hardlinks.
4834                                         // Since we might scan a hardlink before its equivalent object is scanned
4835                                         // we put them all in a list.
4836                                         // After scanning is complete, we should have all the objects, so we run through this
4837                                         // list and fix up all the chains.              
4838         
4839                                         switch(in->variantType)
4840                                         {
4841                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
4842                                                         break;
4843                                                 case YAFFS_OBJECT_TYPE_FILE:
4844                                                         if(dev->isYaffs2 && oh->isShrink)
4845                                                         {
4846                                                                 // Prune back the shrunken chunks
4847                                                                 yaffs_PruneResizedChunks(in,oh->fileSize);
4848                                                                 // Mark the block as having a shrinkHeader
4849                                                                 bi->hasShrinkHeader = 1;
4850                                                         }
4851                                                         
4852                                                         if(dev->useHeaderFileSize)
4853                                                                 in->variant.fileVariant.fileSize = oh->fileSize;
4854                                                                 
4855                                                         break;
4856                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
4857                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4858                                                         in->hardLinks.next = (struct list_head *)hardList;
4859                                                         hardList = in;
4860                                                         break;
4861                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
4862                                                         break;
4863                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4864                                                         break;
4865                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
4866                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4867                                                         break;
4868                                         }
4869
4870                                         if(parent == dev->deletedDir)
4871                                         {
4872                                                 yaffs_DestroyObject(in);
4873                                                 bi->hasShrinkHeader = 1;
4874                                         }
4875                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
4876                                 }
4877                         }
4878                 }
4879                 
4880                 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4881                 {
4882                         // If we got this far while scanning, then the block is fully allocated.
4883                         state = YAFFS_BLOCK_STATE_FULL; 
4884                 }
4885                 
4886                 bi->blockState = state;
4887                 
4888                 // Now let's see if it was dirty
4889                 if(     bi->pagesInUse == 0 &&
4890                         !bi->hasShrinkHeader &&
4891                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4892             {
4893                 yaffs_BlockBecameDirty(dev,blk);
4894             }
4895
4896         }
4897         
4898         if(blockIndex)
4899         {
4900                 YFREE(blockIndex);
4901         }
4902         
4903         // Ok, we've done all the scanning.
4904         
4905         // Fix up the hard link chains.
4906         // We should now have scanned all the objects, now it's time to add these 
4907         // hardlinks.
4908         while(hardList)
4909         {
4910                 hl = hardList;
4911                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4912                 
4913                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4914                 
4915                 if(in)
4916                 {
4917                         // Add the hardlink pointers
4918                         hl->variant.hardLinkVariant.equivalentObject=in;
4919                         list_add(&hl->hardLinks,&in->hardLinks);
4920                 }
4921                 else
4922                 {
4923                         //Todo Need to report/handle this better.
4924                         // Got a problem... hardlink to a non-existant object
4925                         hl->variant.hardLinkVariant.equivalentObject=NULL;
4926                         INIT_LIST_HEAD(&hl->hardLinks);
4927                         
4928                 }
4929                 
4930         }
4931         
4932         {
4933                 struct list_head *i;    
4934                 struct list_head *n;
4935                         
4936                 yaffs_Object *l;
4937                 // Soft delete all the unlinked files
4938                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4939                 {
4940                         if(i)
4941                         {
4942                                 l = list_entry(i, yaffs_Object,siblings);
4943                                 if(l->deleted)
4944                                 {
4945                                         yaffs_DestroyObject(l);         
4946                                 }
4947                         }
4948                 }       
4949         }
4950         
4951         yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
4952
4953         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan ends" TENDSTR)));
4954
4955         return YAFFS_OK;
4956 }
4957
4958
4959
4960 ////////////////////////// Directory Functions /////////////////////////
4961
4962
4963 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
4964 {
4965
4966         if(obj->siblings.prev == NULL)
4967         {
4968                 // Not initialised
4969                 INIT_LIST_HEAD(&obj->siblings);
4970                 
4971         }
4972         else if(!list_empty(&obj->siblings))
4973         {
4974                 // If it is holed up somewhere else, un hook it
4975                 list_del_init(&obj->siblings);
4976         }
4977         // Now add it
4978         list_add(&obj->siblings,&directory->variant.directoryVariant.children);
4979         obj->parent = directory;
4980         
4981         if(directory == obj->myDev->unlinkedDir || directory == obj->myDev->deletedDir)
4982         {
4983                 obj->unlinked = 1;
4984                 obj->myDev->nUnlinkedFiles++;
4985                 obj->renameAllowed = 0;
4986         }
4987 }
4988
4989 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
4990 {
4991         list_del_init(&obj->siblings);
4992         obj->parent = NULL;
4993 }
4994
4995 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const YCHAR *name)
4996 {
4997         int sum;
4998         
4999         struct list_head *i;
5000         YCHAR buffer[YAFFS_MAX_NAME_LENGTH+1];
5001         
5002         yaffs_Object *l;
5003         
5004         sum = yaffs_CalcNameSum(name);
5005         
5006         list_for_each(i,&directory->variant.directoryVariant.children)
5007         {
5008                 if(i)
5009                 {
5010                         l = list_entry(i, yaffs_Object,siblings);
5011                 
5012                         // Special case for lost-n-found
5013                         if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5014                         {
5015                                 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
5016                                 {
5017                                         return l;
5018                                 }
5019                         }
5020                         else if(yaffs_SumCompare(l->sum, sum)||
5021                                     l->chunkId <= 0) //LostnFound cunk called Objxxx
5022                         {
5023                                 // Do a real check
5024                                 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
5025                                 if(yaffs_strcmp(name,buffer) == 0)
5026                                 {
5027                                         return l;
5028                                 }
5029                         
5030                         }                       
5031                 }
5032         }
5033         
5034         return NULL;
5035 }
5036
5037
5038 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
5039 {
5040         struct list_head *i;    
5041         yaffs_Object *l;
5042         
5043         
5044         list_for_each(i,&theDir->variant.directoryVariant.children)
5045         {
5046                 if(i)
5047                 {
5048                         l = list_entry(i, yaffs_Object,siblings);
5049                         if(l && !fn(l))
5050                         {
5051                                 return YAFFS_FAIL;
5052                         }
5053                 }
5054         }
5055         
5056         return YAFFS_OK;
5057
5058 }
5059
5060
5061 // GetEquivalentObject dereferences any hard links to get to the
5062 // actual object.
5063
5064 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5065 {
5066         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
5067         {
5068                 // We want the object id of the equivalent object, not this one
5069                 obj = obj->variant.hardLinkVariant.equivalentObject;
5070         }
5071         return obj;
5072
5073 }
5074
5075 int yaffs_GetObjectName(yaffs_Object *obj,YCHAR *name,int buffSize)
5076 {
5077         memset(name,0,buffSize * sizeof(YCHAR));
5078         
5079         if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5080         {
5081                 yaffs_strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
5082         }
5083         else if(obj->chunkId <= 0)
5084         {
5085                 YCHAR locName[20];
5086                 // make up a name
5087                 yaffs_sprintf(locName,_Y("%s%d"),YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
5088                 yaffs_strncpy(name,locName,buffSize - 1);
5089
5090         }
5091 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5092         else if(obj->shortName[0])
5093         {
5094                 yaffs_strcpy(name,obj->shortName);
5095         }
5096 #endif
5097         else
5098         {
5099                 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev,__LINE__);
5100                 
5101                 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5102
5103                 memset(buffer,0,obj->myDev->nBytesPerChunk);
5104         
5105                 if(obj->chunkId >= 0)
5106                 {
5107                         yaffs_ReadChunkWithTagsFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5108                 }
5109                 yaffs_strncpy(name,oh->name,buffSize - 1);
5110
5111                 yaffs_ReleaseTempBuffer(obj->myDev,buffer,__LINE__);
5112         }
5113         
5114         return yaffs_strlen(name);
5115 }
5116
5117 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5118 {
5119         
5120         // Dereference any hard linking
5121         obj = yaffs_GetEquivalentObject(obj);
5122         
5123         if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5124         {
5125                 return obj->variant.fileVariant.fileSize;
5126         }
5127         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5128         {
5129                 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5130         }
5131         else
5132         {
5133                 // Only a directory should drop through to here
5134                 return obj->myDev->nBytesPerChunk;
5135         }       
5136 }
5137
5138 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5139 {
5140         int count = 0; 
5141         struct list_head *i;
5142         
5143         if(!obj->unlinked)
5144         {
5145                 count++;        // the object itself
5146         }
5147         list_for_each(i,&obj->hardLinks)
5148         {
5149                 count++;        // add the hard links;
5150         }
5151         return count;
5152         
5153 }
5154
5155
5156 int yaffs_GetObjectInode(yaffs_Object *obj)
5157 {
5158         obj = yaffs_GetEquivalentObject(obj);
5159         
5160         return obj->objectId;
5161 }
5162
5163 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5164 {
5165         obj = yaffs_GetEquivalentObject(obj);
5166         
5167         switch(obj->variantType)
5168         {
5169                 case YAFFS_OBJECT_TYPE_FILE:            return DT_REG; break;
5170                 case YAFFS_OBJECT_TYPE_DIRECTORY:       return DT_DIR; break;
5171                 case YAFFS_OBJECT_TYPE_SYMLINK:         return DT_LNK; break;
5172                 case YAFFS_OBJECT_TYPE_HARDLINK:        return DT_REG; break;
5173                 case YAFFS_OBJECT_TYPE_SPECIAL:         
5174                         if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
5175                         if(S_ISCHR(obj->st_mode)) return DT_CHR;
5176                         if(S_ISBLK(obj->st_mode)) return DT_BLK;
5177                         if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
5178                 default: return DT_REG; break;
5179         }
5180 }
5181
5182 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5183 {
5184         obj = yaffs_GetEquivalentObject(obj);
5185         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5186         {
5187                 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5188         }
5189         else
5190         {
5191                 return yaffs_CloneString(_Y(""));
5192         }
5193 }
5194
5195 #ifndef CONFIG_YAFFS_WINCE
5196
5197 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5198 {
5199         unsigned int valid = attr->ia_valid;
5200         
5201         if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
5202         if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
5203         if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
5204         
5205         if(valid & ATTR_ATIME) obj->st_atime = Y_TIME_CONVERT(attr->ia_atime);
5206         if(valid & ATTR_CTIME) obj->st_ctime = Y_TIME_CONVERT(attr->ia_ctime);
5207         if(valid & ATTR_MTIME) obj->st_mtime = Y_TIME_CONVERT(attr->ia_mtime);
5208         
5209         if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
5210         
5211         yaffs_UpdateObjectHeader(obj,NULL,1,0);
5212         
5213         return YAFFS_OK;
5214         
5215 }
5216 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5217 {
5218         unsigned int valid = 0;
5219         
5220         attr->ia_mode = obj->st_mode;   valid |= ATTR_MODE;
5221         attr->ia_uid = obj->st_uid;             valid |= ATTR_UID;
5222         attr->ia_gid = obj->st_gid;             valid |= ATTR_GID;
5223         
5224         Y_TIME_CONVERT(attr->ia_atime)= obj->st_atime;  valid |= ATTR_ATIME;
5225         Y_TIME_CONVERT(attr->ia_ctime) = obj->st_ctime; valid |= ATTR_CTIME;
5226         Y_TIME_CONVERT(attr->ia_mtime) = obj->st_mtime; valid |= ATTR_MTIME;
5227
5228         attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
5229         
5230         attr->ia_valid = valid;
5231         
5232         return YAFFS_OK;
5233         
5234 }
5235
5236 #endif
5237
5238 int yaffs_DumpObject(yaffs_Object *obj)
5239 {
5240 //      __u8 buffer[YAFFS_BYTES_PER_CHUNK];
5241         YCHAR name[257];
5242 //      yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5243
5244 //      memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
5245         
5246 //      if(obj->chunkId >= 0)
5247 //      {
5248 //              yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5249 //      }
5250         
5251         yaffs_GetObjectName(obj,name,256);
5252         
5253         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),
5254                         obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial, 
5255                         obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5256
5257 #if 0
5258         YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
5259                         obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial, 
5260                         obj->sum, obj->chunkId));
5261                 switch(obj->variantType)
5262         {
5263                 case YAFFS_OBJECT_TYPE_FILE: 
5264                         YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
5265                         break;
5266                 case YAFFS_OBJECT_TYPE_DIRECTORY:
5267                         YPRINTF((" DIRECTORY\n"));
5268                         break;
5269                 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
5270                 case YAFFS_OBJECT_TYPE_SYMLINK:
5271                 case YAFFS_OBJECT_TYPE_UNKNOWN:
5272                 default:
5273         }
5274 #endif
5275         
5276         return YAFFS_OK;
5277 }
5278
5279
5280 ///////////////////////// Initialisation code ///////////////////////////
5281
5282
5283 int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5284 {
5285
5286         // Common functions, gotta have
5287         if(!dev->eraseBlockInNAND ||
5288            !dev->initialiseNAND) return 0;
5289
5290 #ifdef CONFIG_YAFFS_YAFFS2
5291
5292         // Can use the "with tags" style interface for yaffs1 or yaffs2
5293         if(dev->writeChunkWithTagsToNAND &&
5294            dev->readChunkWithTagsFromNAND &&
5295            !dev->writeChunkToNAND &&
5296            !dev->readChunkFromNAND &&
5297            dev->markNANDBlockBad &&
5298            dev->queryNANDBlock) return 1;
5299 #endif
5300
5301         // Can use the "spare" style interface for yaffs1
5302         if(!dev->isYaffs2 &&
5303            !dev->writeChunkWithTagsToNAND &&
5304            !dev->readChunkWithTagsFromNAND &&
5305            dev->writeChunkToNAND &&
5306            dev->readChunkFromNAND &&
5307            !dev->markNANDBlockBad &&
5308            !dev->queryNANDBlock) return 1;
5309
5310         return 0; // bad
5311 }
5312
5313
5314 int yaffs_GutsInitialise(yaffs_Device *dev)
5315 {
5316         unsigned x;
5317         int bits;
5318         int extraBits;
5319         int nBlocks;
5320
5321         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
5322         // Check stuff that must be set
5323
5324         if(!dev)
5325         {
5326                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Need a device" TENDSTR)));
5327                 return YAFFS_FAIL;
5328         }
5329
5330         // Check geometry parameters.
5331
5332         if(     (dev->isYaffs2 && dev->nBytesPerChunk <1024)  ||
5333                 (!dev->isYaffs2 && dev->nBytesPerChunk !=512)  ||
5334                 dev->nChunksPerBlock < 2 ||
5335                 dev->nReservedBlocks < 2 ||
5336                 dev->startBlock <= 0 ||
5337                 dev->endBlock <= 0 ||
5338                 dev->endBlock <= (dev->startBlock + dev->nReservedBlocks + 2) // otherwise it is too small
5339           )
5340         {
5341                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s " TENDSTR),
5342                    dev->nBytesPerChunk, dev->isYaffs2 ? "2" : ""));
5343                 return YAFFS_FAIL;
5344         }
5345
5346         if(yaffs_InitialiseNAND(dev) != YAFFS_OK)
5347         {
5348                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
5349                 return YAFFS_FAIL;
5350         }
5351
5352         // Got the right mix of functions?
5353         //
5354         if(!yaffs_CheckDevFunctions(dev))
5355         {
5356                 //Function missing
5357                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5358
5359                 return YAFFS_FAIL;
5360         }
5361
5362         // This is really a compilation check.
5363         if(!yaffs_CheckStructures())
5364         {
5365                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
5366                 return YAFFS_FAIL;
5367         }
5368
5369         if(dev->isMounted)
5370         {
5371                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
5372                 return YAFFS_FAIL;
5373         }
5374
5375         //
5376         //
5377         // Finished with most checks. One or two more checks happen later on too.
5378         //
5379
5380         dev->isMounted = 1;
5381
5382
5383         nBlocks = dev->endBlock - dev->startBlock + 1;
5384
5385
5386
5387         // OK now calculate a few things for the device
5388         // Calculate chunkGroupBits.
5389         // We need to find the next power of 2 > than endBlock
5390         
5391         x = dev->nChunksPerBlock * (dev->endBlock+1);
5392         
5393         for(bits = extraBits = 0; x > 1; bits++)
5394         {
5395                 if(x & 1) extraBits++;
5396                 x >>= 1;
5397         }
5398
5399         if(extraBits > 0) bits++;
5400         
5401         
5402         // Level0 Tnodes are 16 bits, so if the bitwidth of the
5403         // chunk range we're using is greater than 16 we need
5404         // to figure out chunk shift and chunkGroupSize
5405         if(bits <= 16) 
5406         {
5407                 dev->chunkGroupBits = 0;
5408         }
5409         else
5410         {
5411                 dev->chunkGroupBits = bits - 16;
5412         }
5413         
5414         dev->chunkGroupSize = 1 << dev->chunkGroupBits;
5415
5416         if(dev->nChunksPerBlock < dev->chunkGroupSize)
5417         {
5418                 // We have a problem because the soft delete won't work if
5419                 // the chunk group size > chunks per block.
5420                 // This can be remedied by using larger "virtual blocks".
5421                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: chunk group too large\n" TENDSTR)));
5422                 
5423                 return YAFFS_FAIL;
5424         }
5425
5426         
5427         // OK, we've finished verifying the device, lets continue with initialisation
5428         
5429         // More device initialisation
5430         dev->garbageCollections = 0;
5431         dev->passiveGarbageCollections = 0;
5432         dev->currentDirtyChecker = 0;
5433         dev->bufferedBlock = -1;
5434         dev->doingBufferedBlockRewrite = 0;
5435         dev->nDeletedFiles = 0;
5436         dev->nBackgroundDeletions=0;
5437         dev->nUnlinkedFiles = 0;
5438         dev->eccFixed=0;
5439         dev->eccUnfixed=0;
5440         dev->tagsEccFixed=0;
5441         dev->tagsEccUnfixed=0;
5442         dev->nErasureFailures = 0;
5443         
5444         //dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
5445         // Initialise temporary buffers
5446         {
5447                 int i;
5448                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5449                 {
5450                         dev->tempBuffer[i].line = 0; // not in use
5451                         dev->tempBuffer[i].buffer = YMALLOC(dev->nBytesPerChunk);
5452                 }
5453         }
5454         
5455
5456         
5457         yaffs_InitialiseBlocks(dev,nBlocks);
5458         
5459         yaffs_InitialiseTnodes(dev);
5460
5461         yaffs_InitialiseObjects(dev);
5462         
5463         dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
5464         
5465         if(dev->nShortOpCaches > 0)
5466         { 
5467                 int i;
5468                 
5469                 if(dev->nShortOpCaches >  YAFFS_MAX_SHORT_OP_CACHES)
5470                 {
5471                         dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
5472                 }
5473                 
5474                 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
5475                 
5476                 for(i=0; i < dev->nShortOpCaches; i++)
5477                 {
5478                         dev->srCache[i].object = NULL;
5479                         dev->srCache[i].lastUse = 0;
5480                         dev->srCache[i].dirty = 0;
5481                         dev->srCache[i].data = YMALLOC(dev->nBytesPerChunk);
5482                 }
5483                 dev->srLastUse = 0;
5484         }
5485
5486         dev->cacheHits = 0;
5487         
5488         
5489         // Initialise the unlinked, root and lost and found directories
5490         dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = dev->deletedDir = NULL;
5491         
5492         dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5493         dev->deletedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5494
5495         dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
5496         dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
5497         yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
5498         
5499         if(dev->isYaffs2) 
5500         {
5501                 dev->useHeaderFileSize = 1;
5502         }
5503                 
5504         // Now scan the flash.  
5505         yaffs_Scan(dev);
5506         
5507         // Zero out stats
5508         dev->nPageReads = 0;
5509     dev->nPageWrites =  0;
5510         dev->nBlockErasures = 0;
5511         dev->nGCCopies = 0;
5512         dev->nRetriedWrites = 0;
5513
5514         dev->nRetiredBlocks = 0;
5515
5516         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
5517         return YAFFS_OK;
5518                 
5519 }
5520
5521 void yaffs_Deinitialise(yaffs_Device *dev)
5522 {
5523         if(dev->isMounted)
5524         {
5525                 int i;
5526         
5527                 yaffs_DeinitialiseBlocks(dev);
5528                 yaffs_DeinitialiseTnodes(dev);
5529                 yaffs_DeinitialiseObjects(dev);
5530                 if(dev->nShortOpCaches > 0)
5531                 {
5532                                 
5533                         for(i=0; i < dev->nShortOpCaches; i++)
5534                         {
5535                                 YFREE(dev->srCache[i].data);
5536                         }
5537
5538                         YFREE(dev->srCache);
5539                 }
5540
5541                 YFREE(dev->gcCleanupList);
5542
5543                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5544                 {
5545                         YFREE(dev->tempBuffer[i].buffer);
5546                 }
5547                 
5548                 dev->isMounted = 0;
5549         }
5550         
5551 }
5552
5553 #if 0
5554
5555 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5556 {
5557         int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
5558         
5559         struct list_head *i;    
5560         yaffs_Object *l;
5561         
5562         
5563         // To the free chunks add the chunks that are in the deleted unlinked files.
5564         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
5565         {
5566                 l = list_entry(i, yaffs_Object,siblings);
5567                 if(l->deleted)
5568                 {
5569                         nFree++;
5570                         nFree += l->nDataChunks;
5571                 }
5572         }
5573         
5574         
5575         // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);        
5576
5577         if(nFree < 0) nFree = 0;
5578
5579         return nFree;   
5580         
5581 }
5582
5583 #endif
5584
5585 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5586 {
5587         int nFree;
5588         int pending;
5589         int b;
5590         int nDirtyCacheChunks=0;
5591         
5592         yaffs_BlockInfo *blk;
5593         
5594         struct list_head *i;    
5595         yaffs_Object *l;
5596         
5597         for(nFree = 0, b = dev->startBlock; b <= dev->endBlock; b++)
5598         {
5599                 blk = yaffs_GetBlockInfo(dev,b);
5600                 
5601                 switch(blk->blockState)
5602                 {
5603                         case YAFFS_BLOCK_STATE_EMPTY:
5604                         case YAFFS_BLOCK_STATE_ALLOCATING: 
5605                         case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break;
5606                         default: break;
5607                 }
5608         }
5609         
5610         
5611         pending = 0;
5612         
5613         // To the free chunks add the chunks that are in the deleted unlinked files.
5614         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
5615         {
5616                 if(i)
5617                 {
5618                         l = list_entry(i, yaffs_Object,siblings);
5619                         if(l->deleted)
5620                         {
5621                                 pending++;
5622                                 pending += l->nDataChunks;
5623                         }
5624                 }
5625         }
5626         
5627         
5628         
5629         //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending);
5630         
5631         if(nFree != dev->nFreeChunks) 
5632         {
5633         //      printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks);
5634         }
5635
5636         nFree += pending;
5637         
5638         // Now count the number of dirty chunks in the cache and subtract those
5639         
5640         {
5641                 int i;
5642                 for(i = 0; i < dev->nShortOpCaches; i++)
5643                 {
5644                         if(dev->srCache[i].dirty) nDirtyCacheChunks++;
5645                 }
5646         }
5647         
5648         nFree -= nDirtyCacheChunks;
5649         
5650         nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
5651         
5652         if(nFree < 0) nFree = 0;
5653
5654         return nFree;   
5655         
5656 }
5657
5658
5659
5660 /////////////////// YAFFS test code //////////////////////////////////
5661
5662 #define yaffs_CheckStruct(structure,syze, name) \
5663            if(sizeof(structure) != syze) \
5664                { \
5665                  T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
5666                  return YAFFS_FAIL; \
5667                    }
5668                  
5669                  
5670 static int yaffs_CheckStructures(void)
5671 {
5672 //      yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
5673 //      yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
5674 //      yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
5675 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
5676         yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
5677 #endif
5678         yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
5679         
5680         
5681         return YAFFS_OK;
5682 }
5683
5684 #if 0
5685 void yaffs_GutsTest(yaffs_Device *dev)
5686 {
5687         
5688         if(yaffs_CheckStructures() != YAFFS_OK)
5689         {
5690                 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
5691                 return;
5692         }
5693         
5694         yaffs_TnodeTest(dev);
5695         yaffs_ObjectTest(dev);  
5696 }
5697 #endif
5698
5699
5700