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