Some tinkering on test harness and st_xxx to yst_xxx changes
[yaffs2.git] / yaffs_guts.c
1
2 /*
3  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
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  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15  //yaffs_guts.c
16
17 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.8 2005-07-05 23:54:59 charles Exp $";
18
19 #include "yportenv.h"
20
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
23 #include "yaffs_tagsvalidity.h"
24
25
26 #include "yaffs_tagscompat.h"
27
28 #ifdef CONFIG_YAFFS_WINCE
29 void yfsd_LockYAFFS(BOOL fsLockOnly);
30 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
31 #endif
32
33 #define YAFFS_PASSIVE_GC_CHUNKS 2
34
35 #if 0
36 // Use Steven Hill's ECC struff instead
37 // External functions for ECC on data
38 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
39 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
40 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
41 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
42 #else
43 #include "yaffs_ecc.h"
44 #endif
45
46 #if 0
47 // countBits is a quick way of counting the number of bits in a byte.
48 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
49
50 static const char yaffs_countBitsTable[256] =
51 {
52 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
53 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
54 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
55 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
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 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
61 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
62 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
63 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
64 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
65 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
66 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
67 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
68 };
69
70 static int yaffs_CountBits(__u8 x)
71 {
72         int retVal;
73         retVal = yaffs_countBitsTable[x];
74         return retVal;
75 }
76
77 #endif
78
79
80 #if 0
81 // Stuff using yea olde tags
82 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
83 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
84
85 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
86 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
87 #else
88 #endif
89
90 // NAND access
91
92
93 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
94 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
95 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
96 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
97 // Local prototypes
98 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
99 #if 0
100 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
101 #endif
102 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
103
104 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
105 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
106 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
107 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
108 static int yaffs_CheckStructures(void);
109 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
110 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
111
112 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
113
114 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
115 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
116
117
118 // Robustification (if it ever comes about...)
119 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
120 #if 0
121 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
122 #endif
123 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
124 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
125 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND,  const yaffs_ExtendedTags *tags);
126
127 static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
128
129 static int yaffs_UnlinkWorker(yaffs_Object *obj);
130 static void yaffs_DestroyObject(yaffs_Object *obj);
131
132 #if 0
133 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
134 #endif
135
136 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
137
138
139 loff_t yaffs_GetFileSize(yaffs_Object *obj);
140
141
142 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
143
144 #ifdef YAFFS_PARANOID
145 static int yaffs_CheckFileSanity(yaffs_Object *in);
146 #else
147 #define yaffs_CheckFileSanity(in)
148 #endif
149
150 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
151 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
152
153 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
154 {
155         chunkInNAND -= dev->chunkOffset;
156         
157         if(dev->readChunkWithTagsFromNAND)
158                 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
159         else
160                 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
161 }
162
163 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
164 {
165         chunkInNAND -= dev->chunkOffset;
166         
167         if(tags)
168         {
169                 tags->sequenceNumber = dev->sequenceNumber;
170                 tags->chunkUsed = 1;
171                 if(!yaffs_ValidateTags(tags))
172                 {
173                         T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
174                         YBUG();
175                 }
176                 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
177         }
178         else
179         {
180                         T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
181                         YBUG();
182         }
183
184         if(dev->writeChunkWithTagsToNAND)
185                 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
186         else
187                 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
188 }
189
190 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
191 {
192         blockNo -= dev->blockOffset;
193         
194         if(dev->markNANDBlockBad)
195                 return dev->markNANDBlockBad(dev,blockNo);
196         else
197                 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
198 }
199 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
200 {
201         blockNo -= dev->blockOffset;
202         
203         if(dev->queryNANDBlock)
204                 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
205         else
206                 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
207 }
208
209 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
210 {
211         int result;
212         
213         blockInNAND -= dev->blockOffset;
214
215         dev->nBlockErasures++;
216         result = dev->eraseBlockInNAND(dev,blockInNAND);
217
218         if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
219         return result;
220 }
221
222 static int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
223 {
224         return dev->initialiseNAND(dev);
225 }
226
227
228
229
230 // Temporary buffer manipulations
231
232 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
233 {
234         int i,j;
235         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
236         {
237                 if(dev->tempBuffer[i].line == 0)
238                 {
239                         dev->tempBuffer[i].line = lineNo;
240                         if((i+1) > dev->maxTemp)
241                         {
242                                 dev->maxTemp = i + 1;
243                                 for(j = 0; j <= i; j++)
244                                         dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
245                         }       
246                         
247                         return dev->tempBuffer[i].buffer;
248                 }
249         }
250
251         T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
252         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
253         {
254                 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
255         }
256         T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
257         
258         dev->unmanagedTempAllocations++;
259         // Get an unmanaged one
260         return YMALLOC(dev->nBytesPerChunk);    
261         
262         
263 }
264
265 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
266 {
267         int i;
268         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
269         {
270                 if(dev->tempBuffer[i].buffer == buffer)
271                 {
272                         dev->tempBuffer[i].line = 0;
273                         return;
274                 }
275         }
276         
277         if(buffer)
278         {
279                 // assume it is an unmanaged one.
280                 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
281                 YFREE(buffer);
282                 dev->unmanagedTempDeallocations++;
283         }
284
285 }
286
287
288 // Chunk bitmap manipulations
289
290 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
291 {
292         if(blk < dev->internalStartBlock || blk > dev->internalEndBlock)
293         {
294                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
295                 YBUG();
296         }
297         return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
298 }
299
300 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
301 {
302         __u8 *blkBits = yaffs_BlockBits(dev,blk);
303
304          memset(blkBits,0,dev->chunkBitmapStride);
305 }
306
307 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
308 {
309         __u8 *blkBits = yaffs_BlockBits(dev,blk);
310         
311         blkBits[chunk/8] &=  ~ (1<<(chunk & 7));
312 }
313
314 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
315 {
316         __u8 *blkBits = yaffs_BlockBits(dev,blk);
317
318         blkBits[chunk/8] |=   (1<<(chunk & 7));
319 }
320
321 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
322 {
323         __u8 *blkBits = yaffs_BlockBits(dev,blk);
324         return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;
325 }
326
327 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
328 {
329         __u8 *blkBits = yaffs_BlockBits(dev,blk);
330         int i;
331         for(i = 0; i < dev->chunkBitmapStride; i++)
332         {
333                 if(*blkBits) return 1;
334                 blkBits++;
335         }
336         return 0;
337 }
338
339
340 static  Y_INLINE int yaffs_HashFunction(int n)
341 {
342         return (n % YAFFS_NOBJECT_BUCKETS);
343 }
344
345
346 yaffs_Object *yaffs_Root(yaffs_Device *dev)
347 {
348         return dev->rootDir;
349 }
350
351 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
352 {
353         return dev->lostNFoundDir;
354 }
355
356
357
358
359 int yaffs_CheckFF(__u8 *buffer,int nBytes)
360 {
361         //Horrible, slow implementation
362         while(nBytes--)
363         {
364                 if(*buffer != 0xFF) return 0;
365                 buffer++;
366         }
367         return 1;
368 }
369
370 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
371 {
372
373         int retval = YAFFS_OK;
374         __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
375         yaffs_ExtendedTags tags;
376
377 // NCB          dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
378         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
379         
380         if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
381         {
382                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
383                 retval = YAFFS_FAIL;
384         }
385
386         yaffs_ReleaseTempBuffer(dev,data,__LINE__);
387         
388         return retval;
389         
390 }
391
392
393
394 #if 1
395 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
396 {
397         int chunk;
398         
399         int writeOk = 1;
400         int attempts = 0;
401         
402
403         
404         do{
405                 chunk = yaffs_AllocateChunk(dev,useReserve);
406         
407                 if(chunk >= 0)
408                 {
409
410                         // First check this chunk is erased...
411 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
412                         writeOk = yaffs_CheckChunkErased(dev,chunk);
413 #endif          
414                         if(!writeOk)
415                         {
416                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
417                         }
418                         else
419                         {
420                                 writeOk =  yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
421                         }
422                         attempts++;
423
424                         if(writeOk)
425                         {
426                                 // Copy the data into the robustification buffer.
427                                 // NB We do this at the end to prevent duplicates in the case of a write error.
428                                 //Todo
429                                 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
430                         }
431                         else
432                         {
433                                 yaffs_HandleWriteChunkError(dev,chunk);
434                         }
435                 }
436                 
437         } while(chunk >= 0 && ! writeOk);
438         
439         if(attempts > 1)
440         {
441                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
442                 dev->nRetriedWrites+= (attempts - 1);   
443         }
444         
445
446         
447         return chunk;
448 }
449 #endif
450
451
452 ///
453 // Functions for robustisizing
454 //
455 //
456
457 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
458 {
459         
460         yaffs_MarkBlockBad(dev,blockInNAND);
461         
462         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
463
464         dev->nRetiredBlocks++;
465 }
466
467
468 #if 0
469 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
470 {
471         dev->doingBufferedBlockRewrite = 1;
472         //
473         //      Remove erased chunks
474         //  Rewrite existing chunks to a new block
475         //      Set current write block to the new block
476         
477         dev->doingBufferedBlockRewrite = 0;
478         
479         return 1;
480 }
481
482
483 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
484 {
485         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
486
487         // Mark the block for retirement
488         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
489         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
490
491
492         //TODO  
493         // Just do a garbage collection on the affected block then retire the block
494         // NB recursion
495 }
496
497
498 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
499 {
500 }
501
502 #endif
503
504 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
505 {
506 }
507
508 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
509 {
510 }
511
512 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
513 {
514         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
515
516         // Mark the block for retirement
517         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
518         // Delete the chunk
519         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
520 }
521
522
523
524 #if 0
525 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
526 {
527
528
529         if( memcmp(d0,d1,dataSize) != 0 ||
530                 s0->tagByte0 != s1->tagByte0 ||
531                 s0->tagByte1 != s1->tagByte1 ||
532                 s0->tagByte2 != s1->tagByte2 ||
533                 s0->tagByte3 != s1->tagByte3 ||
534                 s0->tagByte4 != s1->tagByte4 ||
535                 s0->tagByte5 != s1->tagByte5 ||
536                 s0->tagByte6 != s1->tagByte6 ||
537                 s0->tagByte7 != s1->tagByte7 ||
538                 s0->ecc1[0]  != s1->ecc1[0]  ||
539                 s0->ecc1[1]  != s1->ecc1[1]  ||
540                 s0->ecc1[2]  != s1->ecc1[2]  ||
541                 s0->ecc2[0]  != s1->ecc2[0]  ||
542                 s0->ecc2[1]  != s1->ecc2[1]  ||
543                 s0->ecc2[2]  != s1->ecc2[2] )
544                 {
545                         return 0;
546                 }
547         
548         return 1;
549 }
550 #endif
551
552
553 ///////////////////////// Object management //////////////////
554 // List of spare objects
555 // The list is hooked together using the first pointer
556 // in the object
557
558 // static yaffs_Object *yaffs_freeObjects = NULL;
559
560 // static int yaffs_nFreeObjects;
561
562 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
563
564 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
565
566
567 static __u16 yaffs_CalcNameSum(const YCHAR *name)
568 {
569         __u16 sum = 0;
570         __u16 i = 1;
571         
572         YUCHAR *bname = (YUCHAR *)name;
573         if(bname)
574         {
575                 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
576                 {
577
578 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
579                         sum += yaffs_toupper(*bname) * i;
580 #else
581                         sum += (*bname) * i;
582 #endif
583                         i++;
584                         bname++;
585                 }
586         }
587         return sum;
588 }
589
590 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
591 {
592 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
593                                         if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
594                                         {
595                                                 yaffs_strcpy(obj->shortName,name);
596                                         }
597                                         else
598                                         {
599                                                 obj->shortName[0]=_Y('\0');
600                                         }
601 #endif
602                                         obj->sum = yaffs_CalcNameSum(name);
603 }
604
605 #if 0
606 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
607 {
608         yaffs_ECCCalculate(data , spare->ecc1);
609         yaffs_ECCCalculate(&data[256] , spare->ecc2);
610 }
611 #endif
612
613
614 ///////////////////////// TNODES ///////////////////////
615
616 // List of spare tnodes
617 // The list is hooked together using the first pointer
618 // in the tnode.
619
620 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
621
622 // static int yaffs_nFreeTnodes;
623
624 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
625
626
627
628 // yaffs_CreateTnodes creates a bunch more tnodes and
629 // adds them to the tnode free list.
630 // Don't use this function directly
631
632 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
633 {
634     int i;
635     yaffs_Tnode *newTnodes;
636     yaffs_TnodeList *tnl;
637     
638     if(nTnodes < 1) return YAFFS_OK;
639    
640         // make these things
641         
642     newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
643    
644     if (!newTnodes)
645     {
646                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
647                 return YAFFS_FAIL;
648     }
649     
650     // Hook them into the free list
651     for(i = 0; i < nTnodes - 1; i++)
652     {
653         newTnodes[i].internal[0] = &newTnodes[i+1];
654 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
655         newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
656 #endif
657     }
658         
659         newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
660 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
661         newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
662 #endif
663         dev->freeTnodes = newTnodes;
664         dev->nFreeTnodes+= nTnodes;
665         dev->nTnodesCreated += nTnodes;
666
667         // Now add this bunch of tnodes to a list for freeing up.
668         // NB If we can't add this to the management list it isn't fatal
669         // but it just means we can't free this bunch of tnodes later.
670         tnl = YMALLOC(sizeof(yaffs_TnodeList));
671         if(!tnl)
672         {
673                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
674                 
675         }
676         else
677         {
678                 tnl->tnodes = newTnodes;
679                 tnl->next = dev->allocatedTnodeList;
680                 dev->allocatedTnodeList = tnl;
681         }
682
683
684         T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
685
686
687         return YAFFS_OK;
688 }
689
690
691 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
692 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
693 {
694         yaffs_Tnode *tn = NULL;
695         
696         // If there are none left make more
697         if(!dev->freeTnodes)
698         {
699                 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
700         }
701         
702         if(dev->freeTnodes)
703         {
704                 tn = dev->freeTnodes;
705 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
706         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
707                 {
708                         // Hoosterman, this thing looks like it isn't in the list
709                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
710                 }
711 #endif
712                 dev->freeTnodes = dev->freeTnodes->internal[0];
713                 dev->nFreeTnodes--;
714                 // zero out
715                 memset(tn,0,sizeof(yaffs_Tnode));
716         }
717         
718
719         return tn;
720 }
721
722
723 // FreeTnode frees up a tnode and puts it back on the free list
724 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
725 {
726         if(tn)
727         {
728 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
729         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
730                 {
731                         // Hoosterman, this thing looks like it is already in the list
732                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
733                 }
734                 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
735 #endif
736                 tn->internal[0] = dev->freeTnodes;
737                 dev->freeTnodes = tn;
738                 dev->nFreeTnodes++;
739         }
740 }
741
742
743 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
744 {
745         // Free the list of allocated tnodes
746         yaffs_TnodeList *tmp;
747                 
748         while(dev->allocatedTnodeList)
749         {
750                 tmp =  dev->allocatedTnodeList->next;
751
752                 YFREE(dev->allocatedTnodeList->tnodes);
753                 YFREE(dev->allocatedTnodeList);
754                 dev->allocatedTnodeList = tmp;
755                 
756         }
757         
758         dev->freeTnodes = NULL;
759         dev->nFreeTnodes = 0;
760 }
761
762 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
763 {
764         dev->allocatedTnodeList = NULL;
765         dev->freeTnodes = NULL;
766         dev->nFreeTnodes = 0;
767         dev->nTnodesCreated = 0;
768
769 }
770
771 #if 0
772 void yaffs_TnodeTest(yaffs_Device *dev)
773 {
774
775         int i;
776         int j;
777         yaffs_Tnode *tn[1000];
778         
779         YINFO("Testing TNodes");
780         
781         for(j = 0; j < 50; j++)
782         {
783                 for(i = 0; i < 1000; i++)
784                 {
785                         tn[i] = yaffs_GetTnode(dev);
786                         if(!tn[i])
787                         {
788                                 YALERT("Getting tnode failed");
789                         }
790                 }
791                 for(i = 0; i < 1000; i+=3)
792                 {
793                         yaffs_FreeTnode(dev,tn[i]);
794                         tn[i] = NULL;
795                 }
796                 
797         }
798 }
799 #endif
800
801
802 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
803
804 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
805 // The look up tree is represented by the top tnode and the number of topLevel
806 // in the tree. 0 means only the level 0 tnode is in the tree.
807
808
809 // FindLevel0Tnode finds the level 0 tnode, if one exists.
810 // Used when reading.....
811 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
812 {
813         
814         yaffs_Tnode *tn = fStruct->top;
815         __u32 i;
816         int requiredTallness;   
817         int level = fStruct->topLevel;
818         
819         // Check sane level and chunk Id
820         if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
821         {
822 //              char str[50];
823 //              sprintf(str,"Bad level %d",level);
824 //              YALERT(str);
825                 return NULL;
826         }
827         
828         if(chunkId > YAFFS_MAX_CHUNK_ID)
829         {
830 //              char str[50];
831 //              sprintf(str,"Bad chunkId %d",chunkId);
832 //              YALERT(str);
833                 return NULL;
834         }
835
836         // First check we're tall enough (ie enough topLevel)
837         
838         i = chunkId >> (/*dev->chunkGroupBits  + */YAFFS_TNODES_LEVEL0_BITS);
839         requiredTallness = 0;
840         while(i)
841         {
842                 i >>= YAFFS_TNODES_INTERNAL_BITS;
843                 requiredTallness++;
844         }
845         
846         
847         if(requiredTallness > fStruct->topLevel)
848         {
849                 // Not tall enough, so we can't find it, return NULL.
850                 return NULL;
851         }
852                 
853         
854         // Traverse down to level 0
855         while (level > 0 && tn)
856         {
857             tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
858                                YAFFS_TNODES_INTERNAL_MASK]; 
859                 level--;
860         
861         }
862         
863         return tn;              
864 }
865
866 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
867 // This happens in two steps:
868 //  1. If the tree isn't tall enough, then make it taller.
869 //  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
870 //
871 // Used when modifying the tree.
872 //
873 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
874 {
875         
876         yaffs_Tnode *tn; 
877         
878         int requiredTallness;
879         int i;
880         int l;
881         
882         __u32 x;
883                 
884         
885         //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
886         
887         // Check sane level and page Id
888         if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
889         {
890 //              char str[50];
891 //              sprintf(str,"Bad level %d",fStruct->topLevel);
892 //              YALERT(str);
893                 return NULL;
894         }
895         
896         if(chunkId > YAFFS_MAX_CHUNK_ID)
897         {
898 //              char str[50];
899 //              sprintf(str,"Bad chunkId %d",chunkId);
900 //              YALERT(str);
901                 return NULL;
902         }
903         
904         // First check we're tall enough (ie enough topLevel)
905         
906         x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
907         requiredTallness = 0;
908         while(x)
909         {
910                 x >>= YAFFS_TNODES_INTERNAL_BITS;
911                 requiredTallness++;
912         }
913         
914         //T((TSTR(" required=%d"),requiredTallness));
915         
916         
917         if(requiredTallness > fStruct->topLevel)
918         {
919                 // Not tall enough,gotta make the tree taller
920                 for(i = fStruct->topLevel; i < requiredTallness; i++)
921                 {
922                         //T((TSTR(" add new top")));
923                         
924                         tn = yaffs_GetTnode(dev);
925                         
926                         if(tn)
927                         {
928                                 tn->internal[0] = fStruct->top;
929                                 fStruct->top = tn;
930                         }
931                         else
932                         {
933                                         T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
934                         }
935                 }
936                 
937                 fStruct->topLevel = requiredTallness;
938         }
939         
940         
941         // Traverse down to level 0, adding anything we need
942         
943         l = fStruct->topLevel;
944         tn = fStruct->top;
945         while (l > 0 && tn)
946         {
947                 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
948                                YAFFS_TNODES_INTERNAL_MASK;
949                                
950                 //T((TSTR(" [%d:%d]"),l,i));
951                 
952             if(!tn->internal[x])
953             {
954                 //T((TSTR(" added")));
955                 
956                 tn->internal[x] = yaffs_GetTnode(dev);
957             }
958             
959             tn =        tn->internal[x];
960                 l--;
961         
962         }
963         
964         //TSTR(TENDSTR)));
965         
966         return tn;              
967 }
968
969
970 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
971 {
972         int j;
973         
974                                         
975         for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
976         {
977                 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
978                 {
979                         yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
980                         if(yaffs_TagsMatch(tags,objectId,chunkInInode))
981                         {
982                                 // found it;
983                                 return theChunk;
984                                         
985                         }
986                 }
987                 theChunk++;
988         }
989         return -1;
990 }
991
992 // DeleteWorker scans backwards through the tnode tree and deletes all the
993 // chunks and tnodes in the file
994 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
995
996 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
997 {
998         int i;
999         int chunkInInode;
1000         int theChunk;
1001         yaffs_ExtendedTags tags;
1002         int foundChunk;
1003         yaffs_Device *dev = in->myDev;
1004
1005         int allDone = 1;
1006         
1007         
1008         if(tn)
1009         {
1010                 if(level > 0)
1011                 {
1012                 
1013                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1014                         {
1015                             if(tn->internal[i])
1016                         {
1017                                         if(limit && (*limit) < 0)
1018                                         {
1019                                                 allDone = 0;
1020                                         }
1021                                         else
1022                                         {
1023                                                 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1024                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1025                                         }
1026                                         if(allDone)
1027                                         {
1028                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1029                                         tn->internal[i] = NULL;
1030                                         }
1031                             }
1032                     
1033                         }
1034                         return (allDone) ? 1 : 0;
1035                 }
1036                 else if(level == 0)
1037                 {
1038                         int hitLimit = 0;
1039                         
1040                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1041                         {
1042                             if(tn->level0[i])
1043                         {
1044                                         
1045                                         chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1046                                         
1047                                         theChunk =  tn->level0[i] << dev->chunkGroupBits;
1048                                         
1049                                         foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1050                                         
1051                                         if(foundChunk > 0)
1052                                         {
1053                                                 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1054                                                 in->nDataChunks--;
1055                                                 if(limit)
1056                                                 { 
1057                                                         *limit = *limit-1;
1058                                                         if(*limit <= 0) 
1059                                                         { 
1060                                                                 hitLimit = 1;
1061                                                         }
1062                                                 }
1063                                         
1064                                         }
1065                                         
1066                                 tn->level0[i] = 0;
1067                             }
1068                     
1069                         }
1070                         return (i < 0) ? 1 : 0;
1071
1072                         
1073                 }
1074                 
1075         }
1076         
1077         return 1;
1078         
1079 }
1080
1081
1082 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1083 {
1084
1085         yaffs_BlockInfo *theBlock;                                      
1086         
1087         T(YAFFS_TRACE_DELETION,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1088                                                 
1089         theBlock =      yaffs_GetBlockInfo(dev,  chunk/dev->nChunksPerBlock);
1090         if(theBlock)
1091         {
1092                 theBlock->softDeletions++;
1093         }
1094 }
1095
1096 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1097 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1098 // of the tnode.
1099 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1100 //
1101 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1102 {
1103         int i;
1104         int theChunk;
1105         int allDone = 1;
1106         yaffs_Device *dev = in->myDev;
1107         
1108         
1109         if(tn)
1110         {
1111                 if(level > 0)
1112                 {
1113                 
1114                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1115                         {
1116                             if(tn->internal[i])
1117                         {
1118                                                 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1119                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1120                                         if(allDone)
1121                                         {
1122                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1123                                         tn->internal[i] = NULL;
1124                                         }
1125                                         else
1126                                         {
1127                                                 //Hoosterman... how could this happen.
1128                                         }                           
1129                                 }                   
1130                         }
1131                         return (allDone) ? 1 : 0;
1132                 }
1133                 else if(level == 0)
1134                 {
1135                         
1136                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1137                         {
1138                             if(tn->level0[i])
1139                         {
1140                                         // Note this does not find the real chunk, only the chunk group.
1141                                         // We make an assumption that a chunk group is niot larger than a block.
1142                                         theChunk =  (tn->level0[i] << dev->chunkGroupBits);
1143                                         
1144                                         yaffs_SoftDeleteChunk(dev,theChunk);
1145                                 tn->level0[i] = 0;
1146                             }
1147                     
1148                         }
1149                         return 1;
1150                         
1151                 }
1152                 
1153         }
1154         
1155         return 1;
1156                 
1157 }
1158
1159
1160
1161 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1162 {
1163         if(obj->deleted &&
1164            obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1165            !obj->softDeleted)
1166         {
1167                 if(obj->nDataChunks <= 0)
1168                 {
1169                                 // Empty file with no duplicate object headers, just delete it immediately
1170                                 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1171                                 obj->variant.fileVariant.top = NULL;
1172                                 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1173                                 yaffs_DoGenericObjectDeletion(obj);     
1174                 }
1175                 else
1176                 {
1177                         yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1178                         obj->softDeleted = 1;
1179                 }
1180         }
1181 }
1182
1183
1184
1185
1186
1187 // Pruning removes any part of the file structure tree that is beyond the
1188 // bounds of the file (ie that does not point to chunks).
1189 //
1190 // A file should only get pruned when its size is reduced.
1191 //
1192 // Before pruning, the chunks must be pulled from the tree and the
1193 // level 0 tnode entries must be zeroed out.
1194 // Could also use this for file deletion, but that's probably better handled
1195 // by a special case.
1196
1197 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1198
1199 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1200 {
1201         int i;
1202         int hasData;
1203         
1204         if(tn)
1205         {
1206                 hasData = 0;
1207                 
1208                 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1209                 {
1210                     if(tn->internal[i] && level > 0)
1211                     {
1212                         tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1213                     }
1214                     
1215                     if(tn->internal[i])
1216                     {
1217                         hasData++;
1218                         }
1219                 }
1220                 
1221                 if(hasData == 0 && del0)
1222                 {
1223                         // Free and return NULL
1224                         
1225                         yaffs_FreeTnode(dev,tn);
1226                         tn = NULL;
1227                 }
1228                 
1229         }
1230
1231         return tn;
1232         
1233 }
1234
1235 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1236 {
1237         int i;
1238         int hasData;
1239         int done = 0;
1240         yaffs_Tnode *tn;
1241         
1242         if(fStruct->topLevel > 0)
1243         {
1244                 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1245                 
1246                 // Now we have a tree with all the non-zero branches NULL but the height
1247                 // is the same as it was.
1248                 // Let's see if we can trim internal tnodes to shorten the tree.
1249                 // We can do this if only the 0th element in the tnode is in use 
1250                 // (ie all the non-zero are NULL)
1251                 
1252                 while(fStruct->topLevel && !done)
1253                 {
1254                         tn = fStruct->top;
1255                         
1256                         hasData = 0;
1257                         for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1258                         {
1259                                 if(tn->internal[i])
1260                         {
1261                                 hasData++;
1262                                 }
1263                         }
1264                         
1265                         if(!hasData)
1266                         {
1267                                 fStruct->top = tn->internal[0];
1268                                 fStruct->topLevel--;
1269                                 yaffs_FreeTnode(dev,tn);
1270                         }
1271                         else
1272                         {
1273                                 done = 1;
1274                         }
1275                 }
1276         }
1277         
1278         return YAFFS_OK;
1279 }
1280
1281
1282
1283
1284
1285 /////////////////////// End of File Structure functions. /////////////////
1286
1287 // yaffs_CreateFreeObjects creates a bunch more objects and
1288 // adds them to the object free list.
1289 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1290 {
1291     int i;
1292     yaffs_Object *newObjects;
1293     yaffs_ObjectList *list;
1294     
1295     if(nObjects < 1) return YAFFS_OK;
1296    
1297         // make these things
1298         
1299     newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1300    
1301     if (!newObjects)
1302     {
1303                 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1304                 return YAFFS_FAIL;
1305     }
1306     
1307     // Hook them into the free list
1308     for(i = 0; i < nObjects - 1; i++)
1309     {
1310         newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1311     }
1312         
1313         newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1314         dev->freeObjects = newObjects;
1315         dev->nFreeObjects+= nObjects;
1316         dev->nObjectsCreated+= nObjects;
1317         
1318         // Now add this bunch of Objects to a list for freeing up.
1319         
1320         list = YMALLOC(sizeof(yaffs_ObjectList));
1321         if(!list)
1322         {
1323                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1324         }
1325         else
1326         {
1327                 list->objects = newObjects;
1328                 list->next = dev->allocatedObjectList;
1329                 dev->allocatedObjectList = list;
1330         }
1331         
1332         
1333         
1334         return YAFFS_OK;
1335 }
1336
1337
1338 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1339 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1340 {
1341         yaffs_Object *tn = NULL;
1342         
1343         // If there are none left make more
1344         if(!dev->freeObjects)
1345         {
1346                 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1347         }
1348         
1349         if(dev->freeObjects)
1350         {
1351                 tn = dev->freeObjects;
1352                 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1353                 dev->nFreeObjects--;
1354                 
1355                 // Now sweeten it up...
1356         
1357                 memset(tn,0,sizeof(yaffs_Object));
1358                 tn->myDev = dev;
1359                 tn->chunkId = -1;
1360                 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1361                 INIT_LIST_HEAD(&(tn->hardLinks));
1362                 INIT_LIST_HEAD(&(tn->hashLink));
1363                 INIT_LIST_HEAD(&tn->siblings);
1364                 
1365                 // Add it to the lost and found directory.
1366                 // NB Can't put root or lostNFound in lostNFound so
1367                 // check if lostNFound exists first
1368                 if(dev->lostNFoundDir)
1369                 {
1370                         yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);      
1371                 }
1372         }
1373         
1374
1375         return tn;
1376 }
1377
1378 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1379 {
1380
1381         yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);              
1382         if(obj)
1383         {
1384                 obj->fake = 1;                  // it is fake so it has no NAND presence...
1385                 obj->renameAllowed= 0;  // ... and we're not allowed to rename it...
1386                 obj->unlinkAllowed= 0;  // ... or unlink it
1387                 obj->deleted = 0;
1388                 obj->unlinked = 0;
1389                 obj->st_mode = mode;
1390                 obj->myDev = dev;
1391                 obj->chunkId = 0; // Not a valid chunk.
1392         }
1393         
1394         return obj;
1395         
1396 }
1397
1398
1399 static void yaffs_UnhashObject(yaffs_Object *tn)
1400 {
1401         int bucket;
1402         yaffs_Device *dev = tn->myDev;
1403         
1404         
1405         // If it is still linked into the bucket list, free from the list
1406         if(!list_empty(&tn->hashLink))
1407         {
1408                 list_del_init(&tn->hashLink);
1409                 bucket =  yaffs_HashFunction(tn->objectId);
1410                 dev->objectBucket[bucket].count--;
1411         }
1412         
1413 }
1414
1415
1416 // FreeObject frees up a Object and puts it back on the free list
1417 static void yaffs_FreeObject(yaffs_Object *tn)
1418 {
1419
1420         yaffs_Device *dev = tn->myDev;
1421         
1422 #ifdef  __KERNEL__
1423         if(tn->myInode)
1424         {
1425                 // We're still hooked up to a cached inode.
1426                 // Don't delete now, but mark for later deletion
1427                 tn->deferedFree = 1;
1428                 return;
1429         }
1430 #endif
1431         
1432         yaffs_UnhashObject(tn);
1433         
1434         // Link into the free list.
1435         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1436         dev->freeObjects = tn;
1437         dev->nFreeObjects++;
1438 }
1439
1440
1441
1442 #ifdef __KERNEL__
1443
1444 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1445 {
1446         if(obj->deferedFree)
1447         {
1448            yaffs_FreeObject(obj);
1449         }
1450 }
1451
1452 #endif
1453
1454
1455
1456 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1457 {
1458         // Free the list of allocated Objects
1459         
1460         yaffs_ObjectList *tmp;
1461         
1462         while( dev->allocatedObjectList)
1463         {
1464                 tmp =  dev->allocatedObjectList->next;
1465                 YFREE(dev->allocatedObjectList->objects);
1466                 YFREE(dev->allocatedObjectList);
1467                 
1468                 dev->allocatedObjectList =  tmp;
1469         }
1470         
1471         dev->freeObjects = NULL;
1472         dev->nFreeObjects = 0;
1473 }
1474
1475 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1476 {
1477         int i;
1478         
1479         dev->allocatedObjectList = NULL;
1480         dev->freeObjects = NULL;
1481         dev->nFreeObjects = 0;
1482         
1483         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1484         {
1485                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1486                 dev->objectBucket[i].count = 0; 
1487         }
1488
1489 }
1490
1491
1492
1493
1494
1495
1496 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1497 {
1498         static int x = 0;
1499         int i;
1500         int l = 999;
1501         int lowest = 999999;
1502
1503                 
1504         // First let's see if we can find one that's empty.
1505         
1506         for(i = 0; i < 10 && lowest > 0; i++)
1507          {
1508                 x++;
1509                 x %=  YAFFS_NOBJECT_BUCKETS;
1510                 if(dev->objectBucket[x].count < lowest)
1511                 {
1512                         lowest = dev->objectBucket[x].count;
1513                         l = x;
1514                 }
1515                 
1516         }
1517         
1518         // If we didn't find an empty list, then try
1519         // looking a bit further for a short one
1520         
1521         for(i = 0; i < 10 && lowest > 3; i++)
1522          {
1523                 x++;
1524                 x %=  YAFFS_NOBJECT_BUCKETS;
1525                 if(dev->objectBucket[x].count < lowest)
1526                 {
1527                         lowest = dev->objectBucket[x].count;
1528                         l = x;
1529                 }
1530                 
1531         }
1532         
1533         return l;
1534 }
1535
1536 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1537 {
1538         int bucket = yaffs_FindNiceObjectBucket(dev);
1539         
1540         // Now find an object value that has not already been taken
1541         // by scanning the list.
1542         
1543         int found = 0;
1544         struct list_head *i;
1545         
1546         __u32 n = (__u32)bucket;
1547
1548         //yaffs_CheckObjectHashSanity();        
1549         
1550         while(!found)
1551         {
1552                 found = 1;
1553                 n +=  YAFFS_NOBJECT_BUCKETS;
1554                 if(1 ||dev->objectBucket[bucket].count > 0)
1555                 {
1556                         list_for_each(i,&dev->objectBucket[bucket].list)
1557                         {
1558                                 // If there is already one in the list
1559                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1560                                 {
1561                                         found = 0;
1562                                 }
1563                         }
1564                 }
1565         }
1566         
1567         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1568         
1569         return n;       
1570 }
1571
1572 void yaffs_HashObject(yaffs_Object *in)
1573 {
1574         int bucket = yaffs_HashFunction(in->objectId);
1575         yaffs_Device *dev = in->myDev;
1576         
1577         if(!list_empty(&in->hashLink))
1578         {
1579                 //YINFO("!!!");
1580         }
1581
1582         
1583         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1584         dev->objectBucket[bucket].count++;
1585
1586 }
1587
1588 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1589 {
1590         int bucket = yaffs_HashFunction(number);
1591         struct list_head *i;
1592         yaffs_Object *in;
1593         
1594         list_for_each(i,&dev->objectBucket[bucket].list)
1595         {
1596                 // Look if it is in the list
1597                 if(i)
1598                 {
1599                         in = list_entry(i, yaffs_Object,hashLink);
1600                         if(in->objectId == number)
1601                         {
1602 #ifdef __KERNEL__
1603                                 // Don't tell the VFS about this one if it is defered free
1604                                 if(in->deferedFree)
1605                                   return NULL;
1606 #endif
1607                                   
1608                                 return in;
1609                         }
1610                 }
1611         }
1612         
1613         return NULL;
1614 }
1615
1616
1617
1618 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1619 {
1620                 
1621         yaffs_Object *theObject;
1622
1623         if(number < 0)
1624         {
1625                 number = yaffs_CreateNewObjectNumber(dev);
1626         }
1627         
1628         theObject = yaffs_AllocateEmptyObject(dev);
1629         
1630         if(theObject)
1631         {
1632                 theObject->fake = 0;
1633                 theObject->renameAllowed = 1;
1634                 theObject->unlinkAllowed = 1;
1635                 theObject->objectId = number;
1636                 yaffs_HashObject(theObject);
1637                 theObject->variantType = type;
1638 #ifdef CONFIG_YAFFS_WINCE
1639                 yfsd_WinFileTimeNow(theObject->win_atime);
1640                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1641                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1642
1643 #else
1644
1645                 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = Y_CURRENT_TIME;               
1646 #endif
1647                 switch(type)
1648                 {
1649                         case YAFFS_OBJECT_TYPE_FILE: 
1650                                 theObject->variant.fileVariant.fileSize = 0;
1651                                 theObject->variant.fileVariant.scannedFileSize = 0;
1652                                 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; // max __u32
1653                                 theObject->variant.fileVariant.topLevel = 0;
1654                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1655                                 break;
1656                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1657                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1658                                 break;
1659                         case YAFFS_OBJECT_TYPE_SYMLINK:
1660                                 // No action required
1661                                 break;
1662                         case YAFFS_OBJECT_TYPE_HARDLINK:
1663                                 // No action required
1664                                 break;
1665                         case YAFFS_OBJECT_TYPE_SPECIAL:
1666                                 // No action required
1667                                 break;
1668                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1669                                 // todo this should not happen
1670                                 break;
1671                 }
1672         }
1673         
1674         return theObject;
1675 }
1676
1677 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1678 {
1679         yaffs_Object *theObject = NULL;
1680         
1681         if(number > 0)
1682         {
1683                 theObject = yaffs_FindObjectByNumber(dev,number);
1684         }
1685         
1686         if(!theObject)
1687         {
1688                 theObject = yaffs_CreateNewObject(dev,number,type);
1689         }
1690         
1691         return theObject;
1692
1693 }
1694
1695 YCHAR *yaffs_CloneString(const YCHAR *str)
1696 {
1697         YCHAR *newStr = NULL;
1698         
1699         if(str && *str)
1700         {
1701                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1702                 yaffs_strcpy(newStr,str);
1703         }
1704
1705         return newStr;
1706         
1707 }
1708
1709 //
1710 // Mknod (create) a new object.
1711 // equivalentObject only has meaning for a hard link;
1712 // aliasString only has meaning for a sumlink.
1713 // rdev only has meaning for devices (a subset of special objects)
1714 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1715                                                                  yaffs_Object *parent,
1716                                                                  const YCHAR *name, 
1717                                                                  __u32 mode,
1718                                                                  __u32 uid,
1719                                                                  __u32 gid,
1720                                                                  yaffs_Object *equivalentObject,
1721                                                                  const YCHAR *aliasString,
1722                                                                  __u32 rdev)
1723 {
1724         yaffs_Object *in;
1725
1726         yaffs_Device *dev = parent->myDev;
1727         
1728         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1729         if(yaffs_FindObjectByName(parent,name))
1730         {
1731                 return NULL;
1732         }
1733         
1734         in = yaffs_CreateNewObject(dev,-1,type);
1735         
1736         if(in)
1737         {
1738                 in->chunkId = -1;
1739                 in->valid = 1;
1740                 in->variantType = type;
1741
1742                 in->st_mode  = mode;
1743                 
1744 #ifdef CONFIG_YAFFS_WINCE
1745                 yfsd_WinFileTimeNow(in->win_atime);
1746                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1747                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1748                 
1749 #else
1750                 in->st_atime = in->st_mtime = in->st_ctime = Y_CURRENT_TIME;
1751
1752                 in->st_rdev  = rdev;
1753                 in->st_uid   = uid;
1754                 in->st_gid   = gid;
1755 #endif          
1756                 in->nDataChunks = 0;
1757
1758                 yaffs_SetObjectName(in,name);
1759                 in->dirty = 1;
1760                 
1761                 yaffs_AddObjectToDirectory(parent,in);
1762                 
1763                 in->myDev = parent->myDev;
1764                 
1765                                 
1766                 switch(type)
1767                 {
1768                         case YAFFS_OBJECT_TYPE_SYMLINK:
1769                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1770                                 break;
1771                         case YAFFS_OBJECT_TYPE_HARDLINK:
1772                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1773                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1774                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1775                                 break;
1776                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1777                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1778                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1779                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1780                                 break;
1781                 }
1782
1783                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1784                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1785                 {
1786                         // Could not create the object header, fail the creation
1787                         yaffs_DestroyObject(in);
1788                         in = NULL;
1789                 }
1790
1791         }
1792         
1793         return in;
1794 }
1795
1796 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1797 {
1798         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1799 }
1800
1801 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1802 {
1803         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1804 }
1805
1806 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1807 {
1808         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL,parent,name,mode,uid,gid,NULL,NULL,rdev);
1809 }
1810
1811 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1812 {
1813         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1814 }
1815
1816 // NB yaffs_Link returns the object id of the equivalent object.
1817 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1818 {
1819         // Get the real object in case we were fed a hard link as an equivalent object
1820         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1821         
1822         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1823         {
1824                 return equivalentObject;
1825         }
1826         else
1827         {
1828                 return NULL;
1829         }
1830         
1831 }
1832
1833
1834 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1835 {
1836         int unlinkOp;
1837         int deleteOp;
1838
1839         if(newDir == NULL)
1840         {
1841                 newDir = obj->parent; // use the old directory
1842         }
1843         
1844         if(newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1845         {
1846                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragendy: yaffs_ChangeObjectName: newDir is not a directory"TENDSTR)));
1847                 YBUG();
1848         }
1849
1850         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1851         if(obj->myDev->isYaffs2)
1852         {
1853                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1854         }
1855         else
1856         {
1857                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1858         }
1859
1860         deleteOp = (newDir == obj->myDev->deletedDir);
1861         
1862         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1863         // duplicate names are allowed.
1864         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1865         if( (unlinkOp|| 
1866                  deleteOp ||
1867                  force || 
1868                  !yaffs_FindObjectByName(newDir,newName))  &&
1869              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1870         {
1871                 yaffs_SetObjectName(obj,newName);
1872                 obj->dirty = 1;
1873                 
1874                 yaffs_AddObjectToDirectory(newDir,obj);
1875                 
1876                 if(unlinkOp) obj->unlinked = 1;
1877                 
1878                 // If it is a deletion then we mark it as a shrink for gc purposes.
1879                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1880                 {
1881                         return YAFFS_OK;
1882                 }
1883         }
1884         
1885         return YAFFS_FAIL;
1886 }
1887
1888
1889
1890 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1891 {
1892         yaffs_Object *obj;
1893         int force = 0;
1894         
1895 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1896         // Special case for case insemsitive systems (eg. WinCE).
1897         // While look-up is case insensitive, the name isn't.
1898         // THerefore we might want to change x.txt to X.txt
1899         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1900         {
1901                 force = 1;
1902         }       
1903 #endif
1904         
1905         obj = yaffs_FindObjectByName(oldDir,oldName);
1906         if(obj && obj->renameAllowed)
1907         {
1908                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1909         }
1910         return YAFFS_FAIL;
1911 }
1912
1913
1914 #if 0
1915
1916 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1917 {
1918         // Scan the buckets and check that the lists 
1919         // have as many members as the count says there are
1920         int bucket;
1921         int countEm;
1922         struct list_head *j;
1923         int ok = YAFFS_OK;
1924         
1925         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1926         {
1927                 countEm = 0;
1928                 
1929                 list_for_each(j,&dev->objectBucket[bucket].list)
1930                 {
1931                         countEm++;
1932                 }
1933                 
1934                 if(countEm != dev->objectBucket[bucket].count)
1935                 {
1936                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
1937                         ok = YAFFS_FAIL;
1938                 }
1939         }
1940
1941         return ok;
1942 }
1943
1944
1945 void yaffs_ObjectTest(yaffs_Device *dev)
1946 {
1947         yaffs_Object *in[1000];
1948         int inNo[1000];
1949         yaffs_Object *inold[1000];
1950         int i;
1951         int j;
1952         
1953         memset(in,0,1000*sizeof(yaffs_Object *));
1954         memset(inold,0,1000*sizeof(yaffs_Object *));
1955         
1956         yaffs_CheckObjectHashSanity(dev);
1957         
1958         for(j = 0; j < 10; j++)
1959         {
1960                 //T(("%d\n",j));
1961                 
1962                 for(i = 0; i < 1000; i++)
1963                 {
1964                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1965                         if(!in[i])
1966                         {
1967                                 YINFO("No more inodes");
1968                         }
1969                         else
1970                         {
1971                                 inNo[i] = in[i]->objectId;
1972                         }
1973                 }
1974                 
1975                 for(i = 0; i < 1000; i++)
1976                 {
1977                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1978                         {
1979                                 //T(("Differnce in look up test\n"));
1980                         }
1981                         else
1982                         {
1983                                 // T(("Look up ok\n"));
1984                         }
1985                 }
1986                 
1987                 yaffs_CheckObjectHashSanity(dev);
1988         
1989                 for(i = 0; i < 1000; i+=3)
1990                 {
1991                         yaffs_FreeObject(in[i]);        
1992                         in[i] = NULL;
1993                 }
1994                 
1995         
1996                 yaffs_CheckObjectHashSanity(dev);
1997         }
1998                 
1999 }
2000
2001 #endif
2002
2003 /////////////////////////// Block Management and Page Allocation ///////////////////
2004
2005
2006 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2007 {
2008         dev->allocationBlock = -1; // force it to get a new one
2009         //Todo we're assuming the malloc will pass.
2010         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2011         // Set up dynamic blockinfo stuff.
2012         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2013         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2014         if(dev->blockInfo && dev->chunkBits)
2015         {
2016                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2017                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2018                 return YAFFS_OK;
2019         }
2020         
2021         return YAFFS_FAIL;
2022         
2023 }
2024
2025 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2026 {
2027         YFREE(dev->blockInfo);
2028         dev->blockInfo = NULL;
2029         YFREE(dev->chunkBits);
2030         dev->chunkBits = NULL;
2031 }
2032
2033
2034 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2035 {
2036         int i;
2037         __u32 seq;
2038         yaffs_BlockInfo *b;
2039         
2040         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2041         
2042         if(!bi->hasShrinkHeader) return 1; // can gc
2043
2044
2045         // Find the oldest dirty sequence number if we don't know it and save it
2046         // so we don't have to keep recomputing it.
2047         if(!dev->oldestDirtySequence)
2048         {
2049                 seq = dev->sequenceNumber;
2050
2051                 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2052                 {
2053                         b = yaffs_GetBlockInfo(dev,i);
2054                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2055                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2056                         b->sequenceNumber < seq)
2057                         {
2058                                 seq = b->sequenceNumber;
2059                         }
2060                 }
2061                 dev->oldestDirtySequence = seq;
2062         }
2063
2064
2065         // Can't do gc of this block if there are any blocks older than this one that have
2066         // discarded pages.
2067         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2068         
2069         
2070         return 1;
2071
2072 }
2073
2074 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2075 // for garbage collection.
2076 //
2077
2078
2079
2080 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2081 {
2082
2083         int b = dev->currentDirtyChecker;
2084         
2085         int i;
2086         int iterations;
2087         int dirtiest = -1;
2088         int pagesInUse; 
2089         yaffs_BlockInfo *bi;
2090         static int  nonAggressiveSkip = 0;
2091
2092         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2093         // search harder.
2094         // else (we're doing a leasurely gc), then we only bother to do this if the
2095         // block has only a few pages in use.
2096         
2097
2098         nonAggressiveSkip--;
2099
2100         if(!aggressive &&(nonAggressiveSkip > 0))
2101         {
2102                 return -1;
2103         }
2104
2105         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2106         if(aggressive)
2107         {
2108                 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2109         }
2110         else
2111         {
2112                 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2113                 iterations = iterations / 16; 
2114                 if(iterations > 200)
2115                 {
2116                         iterations = 200;
2117                 }
2118         }
2119         
2120         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2121         {
2122                 b++;
2123                 if ( b < dev->internalStartBlock || b > dev->internalEndBlock)
2124                 {
2125                         b =  dev->internalStartBlock;
2126                 }
2127
2128                 if(b < dev->internalStartBlock || b > dev->internalEndBlock)
2129                 {
2130                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2131                         YBUG();
2132                 }
2133                 
2134                 bi = yaffs_GetBlockInfo(dev,b);
2135                 
2136                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2137                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2138                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2139                 {
2140                         dirtiest = b;
2141                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2142                 }
2143         }
2144         
2145         dev->currentDirtyChecker = b;
2146         
2147         if(dirtiest > 0)
2148         {
2149                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2150         }
2151         
2152         dev->oldestDirtySequence = 0; // clear this
2153         
2154         if(dirtiest > 0)
2155         {
2156                 nonAggressiveSkip = 4;
2157         }
2158
2159         return dirtiest;
2160 }
2161
2162
2163 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2164 {
2165         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2166         
2167         int erasedOk = 0;
2168         
2169         // If the block is still healthy erase it and mark as clean.
2170         // If the block has had a data failure, then retire it.
2171         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2172
2173         if(!bi->needsRetiring)
2174         {
2175                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2176                 if(!erasedOk)
2177                 {
2178                         dev->nErasureFailures++;
2179                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2180                 }
2181         }
2182
2183         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2184         {
2185                         int i;
2186                         for(i = 0; i < dev->nChunksPerBlock; i++)
2187                         {
2188                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2189                                         {
2190                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2191                                         }
2192                         }
2193         }
2194         
2195         if( erasedOk )
2196         {
2197                 // Clean it up...
2198                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2199                 dev->nErasedBlocks++;
2200                 bi->pagesInUse = 0;
2201                 bi->softDeletions = 0;
2202                 bi->hasShrinkHeader=0;
2203                 yaffs_ClearChunkBits(dev,blockNo);
2204         
2205                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2206         }
2207         else
2208         {
2209                 yaffs_RetireBlock(dev,blockNo);
2210                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2211         }
2212 }
2213
2214 #if 0
2215 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2216 {
2217         int i,j;
2218         yaffs_BlockInfo *bi;
2219         
2220         for(i= dev->internalStartBlock; i <=dev->internalEndBlock; i++)
2221         {
2222                 bi = yaffs_GetBlockInfo(dev,i);
2223                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2224                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2225                 
2226                 for(j = 0; j < dev->nChunksPerBlock; j++)
2227                 {
2228                         if(yaffs_CheckChunkBit(dev,i,j))
2229                         {
2230                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2231
2232                         }
2233                 }
2234                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2235
2236         }
2237 }
2238 #endif
2239
2240
2241 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2242 {
2243         int i;
2244         
2245         yaffs_BlockInfo *bi;
2246         
2247 #if 0
2248         static int j = 0;
2249         j++;
2250         if(j < 0 || j > 100)
2251         {
2252                 j = 0;
2253                 yaffs_DumpBlockStats(dev);
2254         }
2255         
2256 #endif
2257         
2258         if(dev->nErasedBlocks < 1)
2259         {
2260                 // Hoosterman we've got a problem.
2261                 // Can't get space to gc
2262                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2263
2264                 return -1;
2265         }
2266         
2267         // Find an empty block.
2268         
2269         for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2270         {
2271                 dev->allocationBlockFinder++;
2272                 if(dev->allocationBlockFinder < dev->internalStartBlock || dev->allocationBlockFinder> dev->internalEndBlock) 
2273                 {
2274                         dev->allocationBlockFinder = dev->internalStartBlock;
2275                 }
2276                 
2277                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2278
2279                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2280                 {
2281                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2282                         dev->sequenceNumber++;
2283                         bi->sequenceNumber = dev->sequenceNumber;
2284                         dev->nErasedBlocks--;           
2285                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d, %d left" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber, dev->nErasedBlocks));      
2286                         return dev->allocationBlockFinder;
2287                 }
2288         }
2289         
2290         
2291         T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been %d" TENDSTR),dev->nErasedBlocks));
2292         
2293         
2294         
2295
2296         
2297         return -1;      
2298 }
2299
2300
2301
2302 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2303 {
2304         int retVal;
2305         yaffs_BlockInfo *bi;
2306         
2307         if(dev->allocationBlock < 0)
2308         {
2309                 // Get next block to allocate off
2310                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2311                 dev->allocationPage = 0;
2312         }
2313         
2314         if(!useReserve &&  dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2315         {
2316                 // Not enough space to allocate unless we're allowed to use the reserve.
2317                 return -1;
2318         }
2319
2320         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2321         {
2322                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2323         }
2324
2325         
2326         // Next page please....
2327         if(dev->allocationBlock >= 0)
2328         {
2329                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2330                 
2331                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2332                                   dev->allocationPage;
2333                 bi->pagesInUse++;
2334                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2335
2336                 dev->allocationPage++;
2337                 
2338                 dev->nFreeChunks--;
2339                 
2340                 // If the block is full set the state to full
2341                 if(dev->allocationPage >= dev->nChunksPerBlock)
2342                 {
2343                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2344                         dev->allocationBlock = -1;
2345                 }
2346
2347
2348                 return retVal;
2349                 
2350         }
2351         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2352
2353         return -1;      
2354 }
2355
2356
2357 // To determine if we have enough space we just look at the 
2358 // number of erased blocks.
2359 // The cache is allowed to use reserved blocks.
2360
2361 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2362 {
2363         return (dev->nErasedBlocks >= dev->nReservedBlocks);
2364 }
2365
2366
2367 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2368 {
2369                 int n;
2370
2371                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2372
2373                 if(dev->allocationBlock> 0)
2374                 {
2375                         n += (dev->nChunksPerBlock - dev->allocationPage);
2376                 }
2377
2378                 return n;
2379
2380 }
2381
2382 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2383 {
2384         int oldChunk;
2385         int newChunk;
2386         int chunkInBlock;
2387         int markNAND;
2388         int retVal = YAFFS_OK;
2389         int cleanups = 0;
2390         int i;
2391
2392         int chunksBefore = yaffs_GetErasedChunks(dev);
2393         int chunksAfter;
2394
2395         yaffs_ExtendedTags  tags;
2396         
2397         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2398         
2399         yaffs_Object *object;
2400         
2401         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2402
2403         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2404         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2405
2406         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2407
2408
2409         if(!yaffs_StillSomeChunkBits(dev,block))
2410         {
2411                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2412                 yaffs_BlockBecameDirty(dev,block);
2413         }
2414         else
2415         {
2416
2417                         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2418
2419         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2420             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2421             chunkInBlock++, oldChunk++ )
2422         {
2423                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2424                 {
2425                         
2426                         // This page is in use and might need to be copied off
2427                         
2428                         markNAND = 1;
2429                         
2430                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2431                         
2432                         yaffs_InitialiseTags(&tags);
2433                         
2434                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2435
2436                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2437                         
2438                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2439                         
2440                         if(!object)
2441                         {
2442                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2443                         }
2444                         
2445                         if(object && object->deleted && tags.chunkId != 0)
2446                         {
2447                                 // Data chunk in a deleted file, throw it away
2448                                 // It's a deleted data chunk,
2449                                 // No need to copy this, just forget about it and fix up the
2450                                 // object.
2451                                 
2452                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2453                                 object->nDataChunks--;
2454                                 
2455                                 if(object->nDataChunks <= 0)
2456                                 {
2457                                         // remeber to clean up the object
2458                                         dev->gcCleanupList[cleanups] = tags.objectId;
2459                                         cleanups++;
2460                                 }
2461                                 markNAND = 0;
2462                         }
2463                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2464                         {
2465                                 // Deleted object header with no data chunks.
2466                                 // Can be discarded and the file deleted.
2467                                 object->chunkId = 0;
2468                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2469                                 object->variant.fileVariant.top = NULL;
2470                                 yaffs_DoGenericObjectDeletion(object);
2471                                 
2472                         }
2473                         else if(object)
2474                         {
2475                                 // It's either a data chunk in a live file or
2476                                 // an ObjectHeader, so we're interested in it.
2477                                 // NB Need to keep the ObjectHeaders of deleted files
2478                                 // until the whole file has been deleted off
2479                                 tags.serialNumber++;
2480
2481                                 dev->nGCCopies++;
2482
2483                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2484                         
2485                                 if(newChunk < 0)
2486                                 {
2487                                         retVal =  YAFFS_FAIL;
2488                                 }
2489                                 else
2490                                 {
2491                         
2492                                         // Ok, now fix up the Tnodes etc.
2493                         
2494                                         if(tags.chunkId == 0)
2495                                         {
2496                                                 // It's a header
2497                                                 object->chunkId = newChunk;
2498                                                 object->serial = tags.serialNumber;
2499                                         }
2500                                         else
2501                                         {
2502                                                 // It's a data chunk
2503                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2504                                         }
2505                                 }
2506                         }
2507                         
2508                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2509                         
2510                 }
2511         }
2512
2513         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2514
2515
2516         // Do any required cleanups
2517         for(i = 0; i < cleanups; i++)
2518         {                                               
2519                 // Time to delete the file too
2520                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2521                 if(object)
2522                 {
2523                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2524                         object->variant.fileVariant.top = NULL;
2525                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2526                         yaffs_DoGenericObjectDeletion(object);
2527                 }
2528
2529         }
2530
2531         }
2532
2533         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2534         {
2535                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2536         }
2537                         
2538         return YAFFS_OK;
2539 }
2540
2541 #if 0
2542 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2543 {
2544         // find a file to delete
2545         struct list_head *i;    
2546         yaffs_Object *l;
2547
2548
2549         //Scan the unlinked files looking for one to delete
2550         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2551         {
2552                 if(i)
2553                 {
2554                         l = list_entry(i, yaffs_Object,siblings);
2555                         if(l->deleted)
2556                         {
2557                                 return l;                       
2558                         }
2559                 }
2560         }       
2561         return NULL;
2562 }
2563
2564
2565 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2566 {
2567         // This does background deletion on unlinked files.. only deleted ones.
2568         // If we don't have a file we're working on then find one
2569         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2570         {
2571                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2572         }
2573         
2574         // OK, we're working on a file...
2575         if(dev->unlinkedDeletion)
2576         {
2577                 yaffs_Object *obj = dev->unlinkedDeletion;
2578                 int delresult;
2579                 int limit; // Number of chunks to delete in a file.
2580                                    // NB this can be exceeded, but not by much.
2581                                    
2582                 limit = -1;
2583
2584                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2585                 
2586                 if(obj->nDataChunks == 0)
2587                 {
2588                         // Done all the deleting of data chunks.
2589                         // Now dump the header and clean up
2590                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2591                         obj->variant.fileVariant.top = NULL;
2592                         yaffs_DoGenericObjectDeletion(obj);
2593                         dev->nDeletedFiles--;
2594                         dev->nUnlinkedFiles--;
2595                         dev->nBackgroundDeletions++;
2596                         dev->unlinkedDeletion = NULL;   
2597                 }
2598         }
2599 }
2600
2601 #endif
2602
2603
2604
2605 // New garbage collector
2606 // If we're very low on erased blocks then we do aggressive garbage collection
2607 // otherwise we do "leasurely" garbage collection.
2608 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2609 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2610 //
2611 // The idea is to help clear out space in a more spread-out manner.
2612 // Dunno if it really does anything useful.
2613 //
2614 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2615 {
2616         int block;
2617         int aggressive; 
2618         int gcOk = YAFFS_OK;
2619         int maxTries = 0;
2620         
2621         //yaffs_DoUnlinkedFileDeletion(dev);
2622
2623         // This loop should pass the first time.
2624         // We'll only see looping here if the erase of the collected block fails.
2625         
2626         do{
2627                 maxTries++;
2628                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2629                 {
2630                         // We need a block soon...
2631                         aggressive = 1;
2632                 }
2633                 else 
2634                 {
2635                         // We're in no hurry
2636                         aggressive = 0;
2637                 }
2638         
2639                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2640         
2641                 if(block > 0)
2642                 {
2643                         dev->garbageCollections++;
2644                         if(!aggressive)
2645                         {
2646                                 dev->passiveGarbageCollections++;
2647                         }
2648
2649                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2650
2651                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2652                 }
2653
2654                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) 
2655                 {
2656                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2657                 }
2658         } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2659
2660         return aggressive ? gcOk: YAFFS_OK;
2661 }
2662
2663
2664 //////////////////////////// TAGS ///////////////////////////////////////
2665
2666
2667
2668 #if 0
2669
2670 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2671 {
2672         // Calculate an ecc
2673         
2674         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2675         unsigned  i,j;
2676         unsigned  ecc = 0;
2677         unsigned bit = 0;
2678
2679         tags->ecc = 0;
2680         
2681         for(i = 0; i < 8; i++)
2682         {
2683                 for(j = 1; j &0xff; j<<=1)
2684                 {
2685                         bit++;
2686                         if(b[i] & j)
2687                         {
2688                                 ecc ^= bit;
2689                         }
2690                 }
2691         }
2692         
2693         tags->ecc = ecc;
2694         
2695         
2696 }
2697
2698 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2699 {
2700         unsigned ecc = tags->ecc;
2701         
2702         yaffs_CalcTagsECC(tags);
2703         
2704         ecc ^= tags->ecc;
2705         
2706         if(ecc && ecc <= 64)
2707         {
2708                 // TODO: Handle the failure better. Retire?
2709                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2710
2711                 ecc--;
2712                                 
2713                 b[ecc / 8] ^= (1 << (ecc & 7));
2714                 
2715                 // Now recvalc the ecc
2716                 yaffs_CalcTagsECC(tags);
2717                 
2718                 return 1; // recovered error
2719         }
2720         else if(ecc)
2721         {
2722                 // Wierd ecc failure value
2723                 // TODO Need to do somethiong here
2724                 return -1; //unrecovered error
2725         }
2726         
2727         return 0;
2728 }
2729
2730 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2731 {
2732         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2733         
2734         yaffs_CalcTagsECC(tagsPtr);
2735         
2736         sparePtr->tagByte0 = tu->asBytes[0];
2737         sparePtr->tagByte1 = tu->asBytes[1];
2738         sparePtr->tagByte2 = tu->asBytes[2];
2739         sparePtr->tagByte3 = tu->asBytes[3];
2740         sparePtr->tagByte4 = tu->asBytes[4];
2741         sparePtr->tagByte5 = tu->asBytes[5];
2742         sparePtr->tagByte6 = tu->asBytes[6];
2743         sparePtr->tagByte7 = tu->asBytes[7];
2744 }
2745
2746 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2747 {
2748         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2749         int result;
2750
2751         tu->asBytes[0]= sparePtr->tagByte0;
2752         tu->asBytes[1]= sparePtr->tagByte1;
2753         tu->asBytes[2]= sparePtr->tagByte2;
2754         tu->asBytes[3]= sparePtr->tagByte3;
2755         tu->asBytes[4]= sparePtr->tagByte4;
2756         tu->asBytes[5]= sparePtr->tagByte5;
2757         tu->asBytes[6]= sparePtr->tagByte6;
2758         tu->asBytes[7]= sparePtr->tagByte7;
2759         
2760         result =  yaffs_CheckECCOnTags(tagsPtr);
2761         if(result> 0)
2762         {
2763                 dev->tagsEccFixed++;
2764         }
2765         else if(result <0)
2766         {
2767                 dev->tagsEccUnfixed++;
2768         }
2769 }
2770
2771 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2772 {
2773         memset(spare,0xFF,sizeof(yaffs_Spare));
2774 }
2775
2776 #endif
2777
2778 #if 0
2779 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2780 {
2781         // NB There must be tags, data is optional
2782         // If there is data, then an ECC is calculated on it.
2783         
2784         yaffs_Spare spare;
2785         
2786         if(!tags)
2787         {
2788                 return YAFFS_FAIL;
2789         }
2790         
2791         //yaffs_SpareInitialise(&spare);
2792         
2793         //if(!dev->useNANDECC && buffer)
2794         //{
2795         //      yaffs_CalcECC(buffer,&spare);
2796         //}
2797         
2798         //yaffs_LoadTagsIntoSpare(&spare,tags);
2799         
2800         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2801         
2802 }
2803 #endif
2804
2805 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2806 {
2807         return  (  tags->chunkId == chunkInObject &&
2808                            tags->objectId == objectId &&
2809                            !tags->chunkDeleted) ? 1 : 0;
2810         
2811 }
2812
2813 /////////////////////////////////////////////////////////////////////////////////////////////////////////
2814
2815
2816 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2817 {
2818         //Get the Tnode, then get the level 0 offset chunk offset
2819     yaffs_Tnode *tn;     
2820     int theChunk = -1;
2821     yaffs_ExtendedTags localTags;
2822     int retVal = -1;
2823     
2824     yaffs_Device *dev = in->myDev;
2825     
2826     
2827     if(!tags)
2828     {
2829         // Passed a NULL, so use our own tags space
2830         tags = &localTags;
2831     }
2832     
2833     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2834     
2835     if(tn)
2836     {
2837                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2838                 
2839                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2840     }
2841     return retVal;
2842 }
2843
2844 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2845 {
2846         //Get the Tnode, then get the level 0 offset chunk offset
2847     yaffs_Tnode *tn;     
2848     int theChunk = -1;
2849     yaffs_ExtendedTags localTags;
2850
2851     yaffs_Device *dev = in->myDev;
2852     int retVal = -1;
2853     
2854     if(!tags)
2855     {
2856         // Passed a NULL, so use our own tags space
2857         tags = &localTags;
2858     }
2859     
2860     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2861     
2862     if(tn)
2863     {
2864     
2865                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2866                 
2867                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2868     
2869                 // Delete the entry in the filestructure (if found)
2870                 if(retVal != -1)
2871                 {
2872                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2873                 }
2874     }
2875     else
2876     {
2877         //T(("No level 0 found for %d\n", chunkInInode));
2878     }
2879     
2880     if(retVal == -1)
2881     {
2882         //T(("Could not find %d to delete\n",chunkInInode));
2883     }
2884     return retVal;
2885 }
2886
2887
2888 #ifdef YAFFS_PARANOID
2889
2890 static int yaffs_CheckFileSanity(yaffs_Object *in)
2891 {
2892         int chunk;
2893         int nChunks;
2894         int fSize;
2895         int failed = 0;
2896         int objId;
2897         yaffs_Tnode *tn;
2898     yaffs_Tags localTags;
2899     yaffs_Tags *tags = &localTags;
2900     int theChunk;
2901     int chunkDeleted;
2902     
2903         
2904         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2905         {
2906                 //T(("Object not a file\n"));
2907                 return YAFFS_FAIL;
2908         }
2909         
2910         objId = in->objectId;
2911         fSize  = in->variant.fileVariant.fileSize;
2912         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
2913         
2914         for(chunk = 1; chunk <= nChunks; chunk++)
2915         {
2916                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
2917     
2918                 if(tn)
2919                 {
2920     
2921                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
2922                         
2923                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
2924                         {
2925
2926
2927                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
2928                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
2929                                 {
2930                                         // found it;
2931                                 
2932                                 }
2933                         }
2934                         else
2935                         {
2936                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
2937                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
2938                                                 
2939                                 failed = 1;
2940                                                         
2941                         }
2942     
2943                 }
2944                 else
2945                 {
2946                         //T(("No level 0 found for %d\n", chunk));
2947                 }
2948         }
2949         
2950         return failed ? YAFFS_FAIL : YAFFS_OK;
2951 }
2952
2953 #endif
2954
2955 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
2956 {
2957         // NB inScan is zero unless scanning. For forward scanning, inScan is > 0; for backward scanning inScan is < 0
2958         yaffs_Tnode *tn;
2959         yaffs_Device *dev = in->myDev;
2960         int existingChunk;
2961         yaffs_ExtendedTags existingTags;
2962         yaffs_ExtendedTags newTags;
2963         unsigned existingSerial, newSerial;
2964         
2965         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2966         {
2967                 // Just ignore an attempt at putting a chunk into a non-file during scanning
2968                 // If it is not during Scanning then something went wrong!
2969                 if(!inScan)
2970                 {
2971                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy:attempt to put data chunk into a non-file" TENDSTR)));
2972                         YBUG();
2973                 }
2974                 
2975                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
2976                 return YAFFS_OK;
2977         }
2978                 
2979         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2980         if(!tn)
2981         {
2982                 return YAFFS_FAIL;
2983         }
2984
2985         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
2986         
2987         if(inScan != 0)
2988         {
2989                 // If we're scanning then we need to test for duplicates
2990                 // NB This does not need to be efficient since it should only ever 
2991                 // happen when the power fails during a write, then only one
2992                 // chunk should ever be affected.
2993                 //
2994                 // Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
2995                 // Update: For backward scanning we don't need to re-read tags so this is quite cheap.
2996                 
2997         
2998                 
2999                 if(existingChunk != 0)
3000                 {
3001                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3002                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3003                         //
3004                         // We have a duplicate now we need to decide which one to use:
3005                         //
3006                         // Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3007                         // Forward scanning YAFFS2: The new one is what we use, dump the old one.
3008                         // YAFFS1: Get both sets of tags and compare serial numbers.
3009                         //
3010                         //
3011                         
3012                         if(inScan > 0)
3013                         {
3014                                 // Only do this for forward scanning
3015                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3016                         
3017                         
3018                                 // Do a proper find
3019                                 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3020                         }
3021
3022                         if(existingChunk <=0)
3023                         {
3024                                 //Hoosterman - how did this happen?
3025                                 
3026                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3027
3028                         }
3029
3030                         
3031                         // NB The deleted flags should be false, otherwise the chunks will 
3032                         // not be loaded during a scan
3033                         
3034                         newSerial = newTags.serialNumber;
3035                         existingSerial = existingTags.serialNumber;
3036                         
3037                         if( (inScan > 0) &&
3038                             ( in->myDev->isYaffs2  ||
3039                               existingChunk <= 0 ||
3040                              ((existingSerial+1) & 3) == newSerial))
3041                         {
3042                                 // Forward scanning.                            
3043                                 // Use new
3044                                 // Delete the old one and drop through to update the tnode
3045                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3046                         }
3047                         else
3048                         {
3049                                 // Backward scanning or we want to use the existing one
3050                                 // Use existing.
3051                                 // Delete the new one and return early so that the tnode isn't changed
3052                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3053                                 return YAFFS_OK;
3054                         }
3055                 }
3056
3057         }
3058                 
3059         if(existingChunk == 0)
3060         {
3061                 in->nDataChunks++;
3062         }
3063         
3064         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3065         
3066         return YAFFS_OK;
3067 }
3068
3069
3070
3071 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3072 {
3073     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3074     
3075     if(chunkInNAND >= 0)
3076     {
3077                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3078         }
3079         else
3080         {
3081                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3082
3083                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3084                 return 0;
3085         }
3086
3087 }
3088
3089
3090 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3091 {
3092         int block;
3093         int page;
3094         yaffs_ExtendedTags tags;
3095         yaffs_BlockInfo *bi;
3096         
3097         if(chunkId <= 0) return;        
3098         
3099         dev->nDeletions++;
3100         block = chunkId / dev->nChunksPerBlock;
3101         page = chunkId % dev->nChunksPerBlock;
3102         
3103         bi = yaffs_GetBlockInfo(dev,block);
3104         
3105         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3106         
3107         if(markNAND && 
3108           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3109           !dev->isYaffs2)
3110         {
3111 //              yaffs_SpareInitialise(&spare);
3112
3113 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3114
3115                 //read data before write, to ensure correct ecc 
3116                 //if we're using MTD verification under Linux
3117                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3118 #endif
3119
3120                 yaffs_InitialiseTags(&tags);
3121
3122                 tags.chunkDeleted = 1;
3123
3124         
3125                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3126                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3127         }
3128         else
3129         {
3130                         dev->nUnmarkedDeletions++;
3131         }       
3132         
3133         
3134         // Pull out of the management area.
3135         // If the whole block became dirty, this will kick off an erasure.
3136         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3137             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3138             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3139             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3140         {
3141                 dev->nFreeChunks++;
3142
3143                 yaffs_ClearChunkBit(dev,block,page);
3144                 bi->pagesInUse--;
3145                 
3146                 if(bi->pagesInUse == 0 &&
3147                    !bi->hasShrinkHeader &&
3148                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3149                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3150             {
3151                 yaffs_BlockBecameDirty(dev,block);
3152             }
3153
3154         }
3155         else
3156         {
3157                 // T(("Bad news deleting chunk %d\n",chunkId));
3158         }
3159         
3160 }
3161
3162
3163
3164
3165 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3166 {
3167         // Find old chunk Need to do this to get serial number
3168         // Write new one and patch into tree.
3169         // Invalidate old tags.
3170
3171     int prevChunkId;
3172     yaffs_ExtendedTags prevTags;
3173     
3174     int newChunkId;
3175     yaffs_ExtendedTags newTags;
3176
3177     yaffs_Device *dev = in->myDev;    
3178
3179         yaffs_CheckGarbageCollection(dev);
3180
3181         // Get the previous chunk at this location in the file if it exists
3182     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3183     
3184     // Set up new tags
3185     yaffs_InitialiseTags(&newTags);
3186     
3187         newTags.chunkId = chunkInInode;
3188         newTags.objectId = in->objectId;
3189         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3190         newTags.byteCount = nBytes;
3191                 
3192 //      yaffs_CalcTagsECC(&newTags);
3193
3194         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3195         
3196         if(newChunkId >= 0)
3197         {
3198                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3199                 
3200                 
3201                 if(prevChunkId >= 0)
3202                 {
3203                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3204         
3205                 }
3206                 
3207                 yaffs_CheckFileSanity(in);
3208         }
3209         return newChunkId;
3210
3211
3212
3213
3214
3215 }
3216
3217
3218 // UpdateObjectHeader updates the header on NAND for an object.
3219 // If name is not NULL, then that new name is used.
3220 //
3221 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3222 {
3223
3224         yaffs_BlockInfo *bi;
3225         
3226         yaffs_Device *dev = in->myDev;
3227         
3228     int prevChunkId;
3229     int retVal = 0;
3230     
3231     int newChunkId;
3232     yaffs_ExtendedTags newTags;
3233     
3234     __u8 *buffer = NULL;
3235     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3236     
3237    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3238     
3239     yaffs_ObjectHeader *oh = NULL;
3240     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3241
3242     
3243     if(!in->fake || force)
3244     {
3245   
3246                 yaffs_CheckGarbageCollection(dev);              
3247     
3248             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3249             oh  = (yaffs_ObjectHeader *)buffer;
3250     
3251                 prevChunkId = in->chunkId;
3252     
3253                 if(prevChunkId >= 0)
3254                 {
3255                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3256                         memcpy(oldName,oh->name,sizeof(oh->name));      
3257                 }
3258
3259                 memset(buffer,0xFF,dev->nBytesPerChunk);
3260
3261                 // Header data
3262                 oh->type = in->variantType;
3263                 
3264                 oh->st_mode = in->st_mode;
3265
3266 #ifdef CONFIG_YAFFS_WINCE
3267                 oh->win_atime[0] = in->win_atime[0];
3268                 oh->win_ctime[0] = in->win_ctime[0];
3269                 oh->win_mtime[0] = in->win_mtime[0];
3270                 oh->win_atime[1] = in->win_atime[1];
3271                 oh->win_ctime[1] = in->win_ctime[1];
3272                 oh->win_mtime[1] = in->win_mtime[1];
3273 #else
3274                 oh->st_uid = in->st_uid;
3275                 oh->st_gid = in->st_gid;
3276                 oh->st_atime = in->st_atime;
3277                 oh->st_mtime = in->st_mtime;
3278                 oh->st_ctime = in->st_ctime;
3279                 oh->st_rdev = in->st_rdev;
3280 #endif  
3281                 if(in->parent)
3282                 {
3283                         oh->parentObjectId = in->parent->objectId;
3284                 }
3285                 else
3286                 {
3287                         oh->parentObjectId = 0;
3288                 }
3289                 
3290                 //oh->sum = in->sum;
3291                 if(name && *name)
3292                 {
3293                         memset(oh->name,0,sizeof(oh->name));
3294                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3295                 }
3296                 else if(prevChunkId)
3297                 {       
3298                         memcpy(oh->name, oldName,sizeof(oh->name));
3299                 }
3300                 else
3301                 {
3302                         memset(oh->name,0,sizeof(oh->name));    
3303                 }
3304                 
3305                 oh->isShrink = isShrink;
3306         
3307                 switch(in->variantType)
3308                 {
3309                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3310                                 // Should not happen
3311                                 break;
3312                         case YAFFS_OBJECT_TYPE_FILE:
3313                                 oh->fileSize = (oh->parentObjectId == YAFFS_OBJECTID_DELETED ||
3314                                                 oh->parentObjectId == YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.fileVariant.fileSize;
3315                                 break;
3316                         case YAFFS_OBJECT_TYPE_HARDLINK:
3317                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3318                                 break;
3319                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3320                                 // Do nothing
3321                                 break;
3322                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3323                                 // Do nothing
3324                                 break;
3325                         case YAFFS_OBJECT_TYPE_SYMLINK:
3326                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3327                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3328                                 break;
3329                 }
3330
3331                 // Tags
3332                 yaffs_InitialiseTags(&newTags);
3333                 in->serial++;
3334                 newTags.chunkId = 0;
3335                 newTags.objectId = in->objectId;
3336                 newTags.serialNumber = in->serial;
3337                 
3338                 // Add extra info for file header
3339                 
3340                 newTags.extraHeaderInfoAvailable = 1;
3341                 newTags.extraParentObjectId = oh->parentObjectId;
3342                 newTags.extraFileLength = oh->fileSize;
3343                 newTags.extraIsShrinkHeader = oh->isShrink;
3344                 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3345                 newTags.extraObjectType  = in->variantType;
3346                 
3347                 // Create new chunk in NAND
3348                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3349     
3350                 if(newChunkId >= 0)
3351                 {
3352                 
3353                         in->chunkId = newChunkId;       
3354                 
3355                         if(prevChunkId >= 0)
3356                         {
3357                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3358                         }
3359                 
3360                         in->dirty = 0;
3361                         
3362                         // If this was a shrink, then mark the block that the chunk lives on
3363                         if(isShrink)
3364                         {
3365                                 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3366                                 bi->hasShrinkHeader = 1;
3367                         }
3368                         
3369                 }
3370                 
3371                 retVal =  newChunkId;
3372
3373     }
3374     
3375         if(buffer) 
3376                 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3377         
3378     return retVal;
3379 }
3380
3381
3382 /////////////////////// Short Operations Cache ////////////////////////////////
3383 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3384 //      reads might be short sequential reads, and a lot of writes may be short 
3385 //  sequential writes. eg. scanning/writing a jpeg file.
3386 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3387 //  with dumb-as-a-rock code.
3388 //  There are a limited number (~10) of cache chunks per device so that we don't
3389 //  need a very intelligent search.
3390
3391
3392
3393
3394
3395 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3396 {
3397         yaffs_Device *dev = obj->myDev;
3398         int lowest;
3399         int i;
3400         yaffs_ChunkCache *cache;
3401         int chunkWritten = 0;
3402         //int nBytes;
3403         int nCaches = obj->myDev->nShortOpCaches;
3404         
3405         if  (nCaches > 0)
3406         {
3407                 do{
3408                         cache = NULL;
3409                 
3410                         // Find the dirty cache for this object with the lowest chunk id.
3411                         for(i = 0; i < nCaches; i++)
3412                         {
3413                                 if(dev->srCache[i].object == obj &&
3414                                    dev->srCache[i].dirty)
3415                                 {
3416                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3417                                         {
3418                                                 cache = &dev->srCache[i];
3419                                                 lowest = cache->chunkId;
3420                                         }
3421                                 }
3422                         }
3423                 
3424                         if(cache && !cache->locked)
3425                         {
3426                                 //Write it out and free it up
3427
3428 #if 0
3429                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3430                         
3431                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3432                                 {
3433                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3434                                 }
3435 #endif                  
3436                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3437                                                                                                                         cache->chunkId,
3438                                                                                                                         cache->data,
3439                                                                                                                         cache->nBytes,1);
3440
3441                                 cache->dirty = 0;
3442                                 cache->object = NULL;
3443       &