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