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