c24c3fe28310423e5ab205848c32a047673c6f71
[yaffs2.git] / yaffs_guts.c
1
2 /*
3  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15  //yaffs_guts.c
16
17 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.7 2005-07-03 10:32:40 charles Exp $";
18
19 #include "yportenv.h"
20
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
23 #include "yaffs_tagsvalidity.h"
24
25
26 #include "yaffs_tagscompat.h"
27
28 #ifdef CONFIG_YAFFS_WINCE
29 void yfsd_LockYAFFS(BOOL fsLockOnly);
30 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
31 #endif
32
33 #define YAFFS_PASSIVE_GC_CHUNKS 2
34
35 #if 0
36 // Use Steven Hill's ECC struff instead
37 // External functions for ECC on data
38 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
39 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
40 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
41 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
42 #else
43 #include "yaffs_ecc.h"
44 #endif
45
46 #if 0
47 // countBits is a quick way of counting the number of bits in a byte.
48 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
49
50 static const char yaffs_countBitsTable[256] =
51 {
52 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
53 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
54 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
55 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
56 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
58 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
59 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
60 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
61 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
62 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
63 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
64 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
65 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
66 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
67 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
68 };
69
70 static int yaffs_CountBits(__u8 x)
71 {
72         int retVal;
73         retVal = yaffs_countBitsTable[x];
74         return retVal;
75 }
76
77 #endif
78
79
80 #if 0
81 // Stuff using yea olde tags
82 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
83 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
84
85 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
86 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
87 #else
88 #endif
89
90 // NAND access
91
92
93 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
94 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
95 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
96 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
97 // Local prototypes
98 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
99 #if 0
100 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
101 #endif
102 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
103
104 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
105 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
106 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
107 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
108 static int yaffs_CheckStructures(void);
109 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
110 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
111
112 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
113
114 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
115 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
116
117
118 // Robustification (if it ever comes about...)
119 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
120 #if 0
121 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
122 #endif
123 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
124 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
125 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND,  const yaffs_ExtendedTags *tags);
126
127 static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
128
129 static int yaffs_UnlinkWorker(yaffs_Object *obj);
130 static void yaffs_DestroyObject(yaffs_Object *obj);
131
132 #if 0
133 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
134 #endif
135
136 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
137
138
139 loff_t yaffs_GetFileSize(yaffs_Object *obj);
140
141
142 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
143
144 #ifdef YAFFS_PARANOID
145 static int yaffs_CheckFileSanity(yaffs_Object *in);
146 #else
147 #define yaffs_CheckFileSanity(in)
148 #endif
149
150 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
151 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
152
153 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
154 {
155         if(dev->readChunkWithTagsFromNAND)
156                 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
157         else
158                 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
159 }
160
161 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
162 {
163         if(tags)
164         {
165                 tags->sequenceNumber = dev->sequenceNumber;
166                 tags->chunkUsed = 1;
167                 if(!yaffs_ValidateTags(tags))
168                 {
169                         T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
170                         YBUG();
171                 }
172                 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
173         }
174         else
175         {
176                         T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
177                         YBUG();
178         }
179
180         if(dev->writeChunkWithTagsToNAND)
181                 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
182         else
183                 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
184 }
185
186 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
187 {
188         if(dev->markNANDBlockBad)
189                 return dev->markNANDBlockBad(dev,blockNo);
190         else
191                 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
192 }
193 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
194 {
195         if(dev->queryNANDBlock)
196                 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
197         else
198                 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
199 }
200
201 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
202 {
203         int result;
204
205         dev->nBlockErasures++;
206         result = dev->eraseBlockInNAND(dev,blockInNAND);
207
208         if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
209         return result;
210 }
211
212 static int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
213 {
214         return dev->initialiseNAND(dev);
215 }
216
217
218
219
220 // Temporary buffer manipulations
221
222 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
223 {
224         int i,j;
225         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
226         {
227                 if(dev->tempBuffer[i].line == 0)
228                 {
229                         dev->tempBuffer[i].line = lineNo;
230                         if((i+1) > dev->maxTemp)
231                         {
232                                 dev->maxTemp = i + 1;
233                                 for(j = 0; j <= i; j++)
234                                         dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
235                         }       
236                         
237                         return dev->tempBuffer[i].buffer;
238                 }
239         }
240
241         T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
242         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
243         {
244                 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
245         }
246         T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
247         
248         dev->unmanagedTempAllocations++;
249         // Get an unmanaged one
250         return YMALLOC(dev->nBytesPerChunk);    
251         
252         
253 }
254
255 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
256 {
257         int i;
258         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
259         {
260                 if(dev->tempBuffer[i].buffer == buffer)
261                 {
262                         dev->tempBuffer[i].line = 0;
263                         return;
264                 }
265         }
266         
267         if(buffer)
268         {
269                 // assume it is an unmanaged one.
270                 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
271                 YFREE(buffer);
272                 dev->unmanagedTempDeallocations++;
273         }
274
275 }
276
277
278 // Chunk bitmap manipulations
279
280 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
281 {
282         if(blk < dev->startBlock || blk > dev->endBlock)
283         {
284                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
285                 YBUG();
286         }
287         return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock));
288 }
289
290 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
291 {
292         __u8 *blkBits = yaffs_BlockBits(dev,blk);
293
294          memset(blkBits,0,dev->chunkBitmapStride);
295 }
296
297 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
298 {
299         __u8 *blkBits = yaffs_BlockBits(dev,blk);
300         
301         blkBits[chunk/8] &=  ~ (1<<(chunk & 7));
302 }
303
304 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
305 {
306         __u8 *blkBits = yaffs_BlockBits(dev,blk);
307
308         blkBits[chunk/8] |=   (1<<(chunk & 7));
309 }
310
311 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
312 {
313         __u8 *blkBits = yaffs_BlockBits(dev,blk);
314         return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;
315 }
316
317 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
318 {
319         __u8 *blkBits = yaffs_BlockBits(dev,blk);
320         int i;
321         for(i = 0; i < dev->chunkBitmapStride; i++)
322         {
323                 if(*blkBits) return 1;
324                 blkBits++;
325         }
326         return 0;
327 }
328
329 #if 0
330 // Function to manipulate block info
331 static  Y_INLINE yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
332 {
333         if(blk < dev->startBlock || blk > dev->endBlock)
334         {
335                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));
336                 YBUG();
337         }
338         return &dev->blockInfo[blk - dev->startBlock];
339 }
340 #endif
341
342
343 static  Y_INLINE int yaffs_HashFunction(int n)
344 {
345         return (n % YAFFS_NOBJECT_BUCKETS);
346 }
347
348
349 yaffs_Object *yaffs_Root(yaffs_Device *dev)
350 {
351         return dev->rootDir;
352 }
353
354 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
355 {
356         return dev->lostNFoundDir;
357 }
358
359 #if 0
360 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
361 {
362         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
363         {
364                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
365                 return YAFFS_FAIL;
366         }
367
368         dev->nPageWrites++;
369         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
370 }
371
372
373
374 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
375                                                         int chunkInNAND, 
376                                                         __u8 *data, 
377                                                         yaffs_Spare *spare, 
378                                                         int doErrorCorrection)
379 {
380         int retVal;
381         yaffs_Spare localSpare;
382
383         dev->nPageReads++;
384         
385         
386
387         
388         if(!spare && data)
389         {
390                 // If we don't have a real spare, then we use a local one.
391                 // Need this for the calculation of the ecc
392                 spare = &localSpare;
393         }
394         
395
396         if(!dev->useNANDECC)
397         {
398                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
399                 if(data && doErrorCorrection)
400                 {
401                         // Do ECC correction
402                         //Todo handle any errors
403                 int eccResult1,eccResult2;
404                 __u8 calcEcc[3];
405                 
406                         yaffs_ECCCalculate(data,calcEcc);
407                         eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
408                         yaffs_ECCCalculate(&data[256],calcEcc);
409                         eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
410
411                         if(eccResult1>0)
412                         {
413                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
414                                 dev->eccFixed++;
415                         }
416                         else if(eccResult1<0)
417                         {
418                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
419                                 dev->eccUnfixed++;
420                         }
421
422                         if(eccResult2>0)
423                         {
424                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
425                                 dev->eccFixed++;
426                         }
427                         else if(eccResult2<0)
428                         {
429                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
430                                 dev->eccUnfixed++;
431                         }
432
433                         if(eccResult1 || eccResult2)
434                         {
435                                 // Hoosterman, we had a data problem on this page
436                                 yaffs_HandleReadDataError(dev,chunkInNAND);
437                         }
438                 }
439         }
440         else
441         {
442         // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
443         struct yaffs_NANDSpare nspare;
444                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
445                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
446                 if(data && doErrorCorrection)
447                 {
448                         if(nspare.eccres1>0)
449                         {
450                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
451                         }
452                         else if(nspare.eccres1<0)
453                         {
454                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
455                         }
456
457                         if(nspare.eccres2>0)
458                         {
459                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
460                         }
461                         else if(nspare.eccres2<0)
462                         {
463                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
464                         }
465
466                         if(nspare.eccres1 || nspare.eccres2)
467                         {
468                                 // Hoosterman, we had a data problem on this page
469                                 yaffs_HandleReadDataError(dev,chunkInNAND);
470                         }
471
472                 }
473         }
474         return retVal;
475 }
476
477 #endif
478
479
480
481 int yaffs_CheckFF(__u8 *buffer,int nBytes)
482 {
483         //Horrible, slow implementation
484         while(nBytes--)
485         {
486                 if(*buffer != 0xFF) return 0;
487                 buffer++;
488         }
489         return 1;
490 }
491
492 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
493 {
494
495         int retval = YAFFS_OK;
496         __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
497         yaffs_ExtendedTags tags;
498
499 // NCB          dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
500         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
501         
502         if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
503         {
504                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
505                 retval = YAFFS_FAIL;
506         }
507
508         yaffs_ReleaseTempBuffer(dev,data,__LINE__);
509         
510         return retval;
511         
512 }
513
514
515
516 #if 1
517 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
518 {
519         int chunk;
520         
521         int writeOk = 1;
522         int attempts = 0;
523         
524
525         
526         do{
527                 chunk = yaffs_AllocateChunk(dev,useReserve);
528         
529                 if(chunk >= 0)
530                 {
531
532                         // First check this chunk is erased...
533 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
534                         writeOk = yaffs_CheckChunkErased(dev,chunk);
535 #endif          
536                         if(!writeOk)
537                         {
538                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
539                         }
540                         else
541                         {
542                                 writeOk =  yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
543                         }
544                         attempts++;
545
546                         if(writeOk)
547                         {
548                                 // Copy the data into the robustification buffer.
549                                 // NB We do this at the end to prevent duplicates in the case of a write error.
550                                 //Todo
551                                 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
552                         }
553                         else
554                         {
555                                 yaffs_HandleWriteChunkError(dev,chunk);
556                         }
557                 }
558                 
559         } while(chunk >= 0 && ! writeOk);
560         
561         if(attempts > 1)
562         {
563                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
564                 dev->nRetriedWrites+= (attempts - 1);   
565         }
566         
567
568         
569         return chunk;
570 }
571 #endif
572
573
574 ///
575 // Functions for robustisizing
576 //
577 //
578
579 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
580 {
581         
582         yaffs_MarkBlockBad(dev,blockInNAND);
583         
584         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
585
586         dev->nRetiredBlocks++;
587 }
588
589
590 #if 0
591 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
592 {
593         dev->doingBufferedBlockRewrite = 1;
594         //
595         //      Remove erased chunks
596         //  Rewrite existing chunks to a new block
597         //      Set current write block to the new block
598         
599         dev->doingBufferedBlockRewrite = 0;
600         
601         return 1;
602 }
603
604
605 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
606 {
607         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
608
609         // Mark the block for retirement
610         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
611         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
612
613
614         //TODO  
615         // Just do a garbage collection on the affected block then retire the block
616         // NB recursion
617 }
618
619
620 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
621 {
622 }
623
624 #endif
625
626 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
627 {
628 }
629
630 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
631 {
632 }
633
634 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
635 {
636         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
637
638         // Mark the block for retirement
639         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
640         // Delete the chunk
641         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
642 }
643
644
645
646 #if 0
647 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
648 {
649
650
651         if( memcmp(d0,d1,dataSize) != 0 ||
652                 s0->tagByte0 != s1->tagByte0 ||
653                 s0->tagByte1 != s1->tagByte1 ||
654                 s0->tagByte2 != s1->tagByte2 ||
655                 s0->tagByte3 != s1->tagByte3 ||
656                 s0->tagByte4 != s1->tagByte4 ||
657                 s0->tagByte5 != s1->tagByte5 ||
658                 s0->tagByte6 != s1->tagByte6 ||
659                 s0->tagByte7 != s1->tagByte7 ||
660                 s0->ecc1[0]  != s1->ecc1[0]  ||
661                 s0->ecc1[1]  != s1->ecc1[1]  ||
662                 s0->ecc1[2]  != s1->ecc1[2]  ||
663                 s0->ecc2[0]  != s1->ecc2[0]  ||
664                 s0->ecc2[1]  != s1->ecc2[1]  ||
665                 s0->ecc2[2]  != s1->ecc2[2] )
666                 {
667                         return 0;
668                 }
669         
670         return 1;
671 }
672 #endif
673
674
675 ///////////////////////// Object management //////////////////
676 // List of spare objects
677 // The list is hooked together using the first pointer
678 // in the object
679
680 // static yaffs_Object *yaffs_freeObjects = NULL;
681
682 // static int yaffs_nFreeObjects;
683
684 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
685
686 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
687
688
689 static __u16 yaffs_CalcNameSum(const YCHAR *name)
690 {
691         __u16 sum = 0;
692         __u16 i = 1;
693         
694         YUCHAR *bname = (YUCHAR *)name;
695         if(bname)
696         {
697                 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
698                 {
699
700 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
701                         sum += yaffs_toupper(*bname) * i;
702 #else
703                         sum += (*bname) * i;
704 #endif
705                         i++;
706                         bname++;
707                 }
708         }
709         return sum;
710 }
711
712 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
713 {
714 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
715                                         if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
716                                         {
717                                                 yaffs_strcpy(obj->shortName,name);
718                                         }
719                                         else
720                                         {
721                                                 obj->shortName[0]=_Y('\0');
722                                         }
723 #endif
724                                         obj->sum = yaffs_CalcNameSum(name);
725 }
726
727 #if 0
728 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
729 {
730         yaffs_ECCCalculate(data , spare->ecc1);
731         yaffs_ECCCalculate(&data[256] , spare->ecc2);
732 }
733 #endif
734
735
736 ///////////////////////// TNODES ///////////////////////
737
738 // List of spare tnodes
739 // The list is hooked together using the first pointer
740 // in the tnode.
741
742 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
743
744 // static int yaffs_nFreeTnodes;
745
746 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
747
748
749
750 // yaffs_CreateTnodes creates a bunch more tnodes and
751 // adds them to the tnode free list.
752 // Don't use this function directly
753
754 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
755 {
756     int i;
757     yaffs_Tnode *newTnodes;
758     yaffs_TnodeList *tnl;
759     
760     if(nTnodes < 1) return YAFFS_OK;
761    
762         // make these things
763         
764     newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
765    
766     if (!newTnodes)
767     {
768                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
769                 return YAFFS_FAIL;
770     }
771     
772     // Hook them into the free list
773     for(i = 0; i < nTnodes - 1; i++)
774     {
775         newTnodes[i].internal[0] = &newTnodes[i+1];
776 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
777         newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
778 #endif
779     }
780         
781         newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
782 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
783         newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
784 #endif
785         dev->freeTnodes = newTnodes;
786         dev->nFreeTnodes+= nTnodes;
787         dev->nTnodesCreated += nTnodes;
788
789         // Now add this bunch of tnodes to a list for freeing up.
790         // NB If we can't add this to the management list it isn't fatal
791         // but it just means we can't free this bunch of tnodes later.
792         tnl = YMALLOC(sizeof(yaffs_TnodeList));
793         if(!tnl)
794         {
795                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
796                 
797         }
798         else
799         {
800                 tnl->tnodes = newTnodes;
801                 tnl->next = dev->allocatedTnodeList;
802                 dev->allocatedTnodeList = tnl;
803         }
804
805
806         T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
807
808
809         return YAFFS_OK;
810 }
811
812
813 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
814 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
815 {
816         yaffs_Tnode *tn = NULL;
817         
818         // If there are none left make more
819         if(!dev->freeTnodes)
820         {
821                 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
822         }
823         
824         if(dev->freeTnodes)
825         {
826                 tn = dev->freeTnodes;
827 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
828         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
829                 {
830                         // Hoosterman, this thing looks like it isn't in the list
831                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
832                 }
833 #endif
834                 dev->freeTnodes = dev->freeTnodes->internal[0];
835                 dev->nFreeTnodes--;
836                 // zero out
837                 memset(tn,0,sizeof(yaffs_Tnode));
838         }
839         
840
841         return tn;
842 }
843
844
845 // FreeTnode frees up a tnode and puts it back on the free list
846 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
847 {
848         if(tn)
849         {
850 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
851         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
852                 {
853                         // Hoosterman, this thing looks like it is already in the list
854                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
855                 }
856                 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
857 #endif
858                 tn->internal[0] = dev->freeTnodes;
859                 dev->freeTnodes = tn;
860                 dev->nFreeTnodes++;
861         }
862 }
863
864
865 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
866 {
867         // Free the list of allocated tnodes
868         yaffs_TnodeList *tmp;
869                 
870         while(dev->allocatedTnodeList)
871         {
872                 tmp =  dev->allocatedTnodeList->next;
873
874                 YFREE(dev->allocatedTnodeList->tnodes);
875                 YFREE(dev->allocatedTnodeList);
876                 dev->allocatedTnodeList = tmp;
877                 
878         }
879         
880         dev->freeTnodes = NULL;
881         dev->nFreeTnodes = 0;
882 }
883
884 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
885 {
886         dev->allocatedTnodeList = NULL;
887         dev->freeTnodes = NULL;
888         dev->nFreeTnodes = 0;
889         dev->nTnodesCreated = 0;
890
891 }
892
893 #if 0
894 void yaffs_TnodeTest(yaffs_Device *dev)
895 {
896
897         int i;
898         int j;
899         yaffs_Tnode *tn[1000];
900         
901         YINFO("Testing TNodes");
902         
903         for(j = 0; j < 50; j++)
904         {
905                 for(i = 0; i < 1000; i++)
906                 {
907                         tn[i] = yaffs_GetTnode(dev);
908                         if(!tn[i])
909                         {
910                                 YALERT("Getting tnode failed");
911                         }
912                 }
913                 for(i = 0; i < 1000; i+=3)
914                 {
915                         yaffs_FreeTnode(dev,tn[i]);
916                         tn[i] = NULL;
917                 }
918                 
919         }
920 }
921 #endif
922
923
924 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
925
926 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
927 // The look up tree is represented by the top tnode and the number of topLevel
928 // in the tree. 0 means only the level 0 tnode is in the tree.
929
930
931 // FindLevel0Tnode finds the level 0 tnode, if one exists.
932 // Used when reading.....
933 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
934 {
935         
936         yaffs_Tnode *tn = fStruct->top;
937         __u32 i;
938         int requiredTallness;   
939         int level = fStruct->topLevel;
940         
941         // Check sane level and chunk Id
942         if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
943         {
944 //              char str[50];
945 //              sprintf(str,"Bad level %d",level);
946 //              YALERT(str);
947                 return NULL;
948         }
949         
950         if(chunkId > YAFFS_MAX_CHUNK_ID)
951         {
952 //              char str[50];
953 //              sprintf(str,"Bad chunkId %d",chunkId);
954 //              YALERT(str);
955                 return NULL;
956         }
957
958         // First check we're tall enough (ie enough topLevel)
959         
960         i = chunkId >> (/*dev->chunkGroupBits  + */YAFFS_TNODES_LEVEL0_BITS);
961         requiredTallness = 0;
962         while(i)
963         {
964                 i >>= YAFFS_TNODES_INTERNAL_BITS;
965                 requiredTallness++;
966         }
967         
968         
969         if(requiredTallness > fStruct->topLevel)
970         {
971                 // Not tall enough, so we can't find it, return NULL.
972                 return NULL;
973         }
974                 
975         
976         // Traverse down to level 0
977         while (level > 0 && tn)
978         {
979             tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
980                                YAFFS_TNODES_INTERNAL_MASK]; 
981                 level--;
982         
983         }
984         
985         return tn;              
986 }
987
988 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
989 // This happens in two steps:
990 //  1. If the tree isn't tall enough, then make it taller.
991 //  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
992 //
993 // Used when modifying the tree.
994 //
995 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
996 {
997         
998         yaffs_Tnode *tn; 
999         
1000         int requiredTallness;
1001         int i;
1002         int l;
1003         
1004         __u32 x;
1005                 
1006         
1007         //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
1008         
1009         // Check sane level and page Id
1010         if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
1011         {
1012 //              char str[50];
1013 //              sprintf(str,"Bad level %d",fStruct->topLevel);
1014 //              YALERT(str);
1015                 return NULL;
1016         }
1017         
1018         if(chunkId > YAFFS_MAX_CHUNK_ID)
1019         {
1020 //              char str[50];
1021 //              sprintf(str,"Bad chunkId %d",chunkId);
1022 //              YALERT(str);
1023                 return NULL;
1024         }
1025         
1026         // First check we're tall enough (ie enough topLevel)
1027         
1028         x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
1029         requiredTallness = 0;
1030         while(x)
1031         {
1032                 x >>= YAFFS_TNODES_INTERNAL_BITS;
1033                 requiredTallness++;
1034         }
1035         
1036         //T((TSTR(" required=%d"),requiredTallness));
1037         
1038         
1039         if(requiredTallness > fStruct->topLevel)
1040         {
1041                 // Not tall enough,gotta make the tree taller
1042                 for(i = fStruct->topLevel; i < requiredTallness; i++)
1043                 {
1044                         //T((TSTR(" add new top")));
1045                         
1046                         tn = yaffs_GetTnode(dev);
1047                         
1048                         if(tn)
1049                         {
1050                                 tn->internal[0] = fStruct->top;
1051                                 fStruct->top = tn;
1052                         }
1053                         else
1054                         {
1055                                         T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
1056                         }
1057                 }
1058                 
1059                 fStruct->topLevel = requiredTallness;
1060         }
1061         
1062         
1063         // Traverse down to level 0, adding anything we need
1064         
1065         l = fStruct->topLevel;
1066         tn = fStruct->top;
1067         while (l > 0 && tn)
1068         {
1069                 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
1070                                YAFFS_TNODES_INTERNAL_MASK;
1071                                
1072                 //T((TSTR(" [%d:%d]"),l,i));
1073                 
1074             if(!tn->internal[x])
1075             {
1076                 //T((TSTR(" added")));
1077                 
1078                 tn->internal[x] = yaffs_GetTnode(dev);
1079             }
1080             
1081             tn =        tn->internal[x];
1082                 l--;
1083         
1084         }
1085         
1086         //TSTR(TENDSTR)));
1087         
1088         return tn;              
1089 }
1090
1091
1092 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
1093 {
1094         int j;
1095         
1096                                         
1097         for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
1098         {
1099                 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
1100                 {
1101                         yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
1102                         if(yaffs_TagsMatch(tags,objectId,chunkInInode))
1103                         {
1104                                 // found it;
1105                                 return theChunk;
1106                                         
1107                         }
1108                 }
1109                 theChunk++;
1110         }
1111         return -1;
1112 }
1113
1114 // DeleteWorker scans backwards through the tnode tree and deletes all the
1115 // chunks and tnodes in the file
1116 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1117
1118 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1119 {
1120         int i;
1121         int chunkInInode;
1122         int theChunk;
1123         yaffs_ExtendedTags tags;
1124         int foundChunk;
1125         yaffs_Device *dev = in->myDev;
1126
1127         int allDone = 1;
1128         
1129         
1130         if(tn)
1131         {
1132                 if(level > 0)
1133                 {
1134                 
1135                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1136                         {
1137                             if(tn->internal[i])
1138                         {
1139                                         if(limit && (*limit) < 0)
1140                                         {
1141                                                 allDone = 0;
1142                                         }
1143                                         else
1144                                         {
1145                                                 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1146                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1147                                         }
1148                                         if(allDone)
1149                                         {
1150                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1151                                         tn->internal[i] = NULL;
1152                                         }
1153                             }
1154                     
1155                         }
1156                         return (allDone) ? 1 : 0;
1157                 }
1158                 else if(level == 0)
1159                 {
1160                         int hitLimit = 0;
1161                         
1162                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1163                         {
1164                             if(tn->level0[i])
1165                         {
1166                                         
1167                                         chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1168                                         
1169                                         theChunk =  tn->level0[i] << dev->chunkGroupBits;
1170                                         
1171                                         foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1172                                         
1173                                         if(foundChunk > 0)
1174                                         {
1175                                                 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1176                                                 in->nDataChunks--;
1177                                                 if(limit)
1178                                                 { 
1179                                                         *limit = *limit-1;
1180                                                         if(*limit <= 0) 
1181                                                         { 
1182                                                                 hitLimit = 1;
1183                                                         }
1184                                                 }
1185                                         
1186                                         }
1187                                         
1188                                 tn->level0[i] = 0;
1189                             }
1190                     
1191                         }
1192                         return (i < 0) ? 1 : 0;
1193
1194                         
1195                 }
1196                 
1197         }
1198         
1199         return 1;
1200         
1201 }
1202
1203
1204 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1205 {
1206
1207         yaffs_BlockInfo *theBlock;                                      
1208         
1209         T(YAFFS_TRACE_DELETION,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1210                                                 
1211         theBlock =      yaffs_GetBlockInfo(dev,  chunk/dev->nChunksPerBlock);
1212         if(theBlock)
1213         {
1214                 theBlock->softDeletions++;
1215         }
1216 }
1217
1218 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1219 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1220 // of the tnode.
1221 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1222 //
1223 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1224 {
1225         int i;
1226         int theChunk;
1227         int allDone = 1;
1228         yaffs_Device *dev = in->myDev;
1229         
1230         
1231         if(tn)
1232         {
1233                 if(level > 0)
1234                 {
1235                 
1236                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1237                         {
1238                             if(tn->internal[i])
1239                         {
1240                                                 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1241                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1242                                         if(allDone)
1243                                         {
1244                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1245                                         tn->internal[i] = NULL;
1246                                         }
1247                                         else
1248                                         {
1249                                                 //Hoosterman... how could this happen.
1250                                         }                           
1251                                 }                   
1252                         }
1253                         return (allDone) ? 1 : 0;
1254                 }
1255                 else if(level == 0)
1256                 {
1257                         
1258                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1259                         {
1260                             if(tn->level0[i])
1261                         {
1262                                         // Note this does not find the real chunk, only the chunk group.
1263                                         // We make an assumption that a chunk group is niot larger than a block.
1264                                         theChunk =  (tn->level0[i] << dev->chunkGroupBits);
1265                                         
1266                                         yaffs_SoftDeleteChunk(dev,theChunk);
1267                                 tn->level0[i] = 0;
1268                             }
1269                     
1270                         }
1271                         return 1;
1272                         
1273                 }
1274                 
1275         }
1276         
1277         return 1;
1278                 
1279 }
1280
1281
1282
1283 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1284 {
1285         if(obj->deleted &&
1286            obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1287            !obj->softDeleted)
1288         {
1289                 if(obj->nDataChunks <= 0)
1290                 {
1291                                 // Empty file with no duplicate object headers, just delete it immediately
1292                                 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1293                                 obj->variant.fileVariant.top = NULL;
1294                                 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1295                                 yaffs_DoGenericObjectDeletion(obj);     
1296                 }
1297                 else
1298                 {
1299                         yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1300                         obj->softDeleted = 1;
1301                 }
1302         }
1303 }
1304
1305
1306
1307
1308
1309 // Pruning removes any part of the file structure tree that is beyond the
1310 // bounds of the file (ie that does not point to chunks).
1311 //
1312 // A file should only get pruned when its size is reduced.
1313 //
1314 // Before pruning, the chunks must be pulled from the tree and the
1315 // level 0 tnode entries must be zeroed out.
1316 // Could also use this for file deletion, but that's probably better handled
1317 // by a special case.
1318
1319 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1320
1321 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1322 {
1323         int i;
1324         int hasData;
1325         
1326         if(tn)
1327         {
1328                 hasData = 0;
1329                 
1330                 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1331                 {
1332                     if(tn->internal[i] && level > 0)
1333                     {
1334                         tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1335                     }
1336                     
1337                     if(tn->internal[i])
1338                     {
1339                         hasData++;
1340                         }
1341                 }
1342                 
1343                 if(hasData == 0 && del0)
1344                 {
1345                         // Free and return NULL
1346                         
1347                         yaffs_FreeTnode(dev,tn);
1348                         tn = NULL;
1349                 }
1350                 
1351         }
1352
1353         return tn;
1354         
1355 }
1356
1357 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1358 {
1359         int i;
1360         int hasData;
1361         int done = 0;
1362         yaffs_Tnode *tn;
1363         
1364         if(fStruct->topLevel > 0)
1365         {
1366                 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1367                 
1368                 // Now we have a tree with all the non-zero branches NULL but the height
1369                 // is the same as it was.
1370                 // Let's see if we can trim internal tnodes to shorten the tree.
1371                 // We can do this if only the 0th element in the tnode is in use 
1372                 // (ie all the non-zero are NULL)
1373                 
1374                 while(fStruct->topLevel && !done)
1375                 {
1376                         tn = fStruct->top;
1377                         
1378                         hasData = 0;
1379                         for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1380                         {
1381                                 if(tn->internal[i])
1382                         {
1383                                 hasData++;
1384                                 }
1385                         }
1386                         
1387                         if(!hasData)
1388                         {
1389                                 fStruct->top = tn->internal[0];
1390                                 fStruct->topLevel--;
1391                                 yaffs_FreeTnode(dev,tn);
1392                         }
1393                         else
1394                         {
1395                                 done = 1;
1396                         }
1397                 }
1398         }
1399         
1400         return YAFFS_OK;
1401 }
1402
1403
1404
1405
1406
1407 /////////////////////// End of File Structure functions. /////////////////
1408
1409 // yaffs_CreateFreeObjects creates a bunch more objects and
1410 // adds them to the object free list.
1411 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1412 {
1413     int i;
1414     yaffs_Object *newObjects;
1415     yaffs_ObjectList *list;
1416     
1417     if(nObjects < 1) return YAFFS_OK;
1418    
1419         // make these things
1420         
1421     newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1422    
1423     if (!newObjects)
1424     {
1425                 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1426                 return YAFFS_FAIL;
1427     }
1428     
1429     // Hook them into the free list
1430     for(i = 0; i < nObjects - 1; i++)
1431     {
1432         newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1433     }
1434         
1435         newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1436         dev->freeObjects = newObjects;
1437         dev->nFreeObjects+= nObjects;
1438         dev->nObjectsCreated+= nObjects;
1439         
1440         // Now add this bunch of Objects to a list for freeing up.
1441         
1442         list = YMALLOC(sizeof(yaffs_ObjectList));
1443         if(!list)
1444         {
1445                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1446         }
1447         else
1448         {
1449                 list->objects = newObjects;
1450                 list->next = dev->allocatedObjectList;
1451                 dev->allocatedObjectList = list;
1452         }
1453         
1454         
1455         
1456         return YAFFS_OK;
1457 }
1458
1459
1460 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1461 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1462 {
1463         yaffs_Object *tn = NULL;
1464         
1465         // If there are none left make more
1466         if(!dev->freeObjects)
1467         {
1468                 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1469         }
1470         
1471         if(dev->freeObjects)
1472         {
1473                 tn = dev->freeObjects;
1474                 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1475                 dev->nFreeObjects--;
1476                 
1477                 // Now sweeten it up...
1478         
1479                 memset(tn,0,sizeof(yaffs_Object));
1480                 tn->myDev = dev;
1481                 tn->chunkId = -1;
1482                 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1483                 INIT_LIST_HEAD(&(tn->hardLinks));
1484                 INIT_LIST_HEAD(&(tn->hashLink));
1485                 INIT_LIST_HEAD(&tn->siblings);
1486                 
1487                 // Add it to the lost and found directory.
1488                 // NB Can't put root or lostNFound in lostNFound so
1489                 // check if lostNFound exists first
1490                 if(dev->lostNFoundDir)
1491                 {
1492                         yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);      
1493                 }
1494         }
1495         
1496
1497         return tn;
1498 }
1499
1500 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1501 {
1502
1503         yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);              
1504         if(obj)
1505         {
1506                 obj->fake = 1;                  // it is fake so it has no NAND presence...
1507                 obj->renameAllowed= 0;  // ... and we're not allowed to rename it...
1508                 obj->unlinkAllowed= 0;  // ... or unlink it
1509                 obj->deleted = 0;
1510                 obj->unlinked = 0;
1511                 obj->st_mode = mode;
1512                 obj->myDev = dev;
1513                 obj->chunkId = 0; // Not a valid chunk.
1514         }
1515         
1516         return obj;
1517         
1518 }
1519
1520
1521 static void yaffs_UnhashObject(yaffs_Object *tn)
1522 {
1523         int bucket;
1524         yaffs_Device *dev = tn->myDev;
1525         
1526         
1527         // If it is still linked into the bucket list, free from the list
1528         if(!list_empty(&tn->hashLink))
1529         {
1530                 list_del_init(&tn->hashLink);
1531                 bucket =  yaffs_HashFunction(tn->objectId);
1532                 dev->objectBucket[bucket].count--;
1533         }
1534         
1535 }
1536
1537
1538 // FreeObject frees up a Object and puts it back on the free list
1539 static void yaffs_FreeObject(yaffs_Object *tn)
1540 {
1541
1542         yaffs_Device *dev = tn->myDev;
1543         
1544 #ifdef  __KERNEL__
1545         if(tn->myInode)
1546         {
1547                 // We're still hooked up to a cached inode.
1548                 // Don't delete now, but mark for later deletion
1549                 tn->deferedFree = 1;
1550                 return;
1551         }
1552 #endif
1553         
1554         yaffs_UnhashObject(tn);
1555         
1556         // Link into the free list.
1557         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1558         dev->freeObjects = tn;
1559         dev->nFreeObjects++;
1560 }
1561
1562
1563
1564 #ifdef __KERNEL__
1565
1566 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1567 {
1568         if(obj->deferedFree)
1569         {
1570            yaffs_FreeObject(obj);
1571         }
1572 }
1573
1574 #endif
1575
1576
1577
1578 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1579 {
1580         // Free the list of allocated Objects
1581         
1582         yaffs_ObjectList *tmp;
1583         
1584         while( dev->allocatedObjectList)
1585         {
1586                 tmp =  dev->allocatedObjectList->next;
1587                 YFREE(dev->allocatedObjectList->objects);
1588                 YFREE(dev->allocatedObjectList);
1589                 
1590                 dev->allocatedObjectList =  tmp;
1591         }
1592         
1593         dev->freeObjects = NULL;
1594         dev->nFreeObjects = 0;
1595 }
1596
1597 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1598 {
1599         int i;
1600         
1601         dev->allocatedObjectList = NULL;
1602         dev->freeObjects = NULL;
1603         dev->nFreeObjects = 0;
1604         
1605         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1606         {
1607                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1608                 dev->objectBucket[i].count = 0; 
1609         }
1610
1611 }
1612
1613
1614
1615
1616
1617
1618 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1619 {
1620         static int x = 0;
1621         int i;
1622         int l = 999;
1623         int lowest = 999999;
1624
1625                 
1626         // First let's see if we can find one that's empty.
1627         
1628         for(i = 0; i < 10 && lowest > 0; i++)
1629          {
1630                 x++;
1631                 x %=  YAFFS_NOBJECT_BUCKETS;
1632                 if(dev->objectBucket[x].count < lowest)
1633                 {
1634                         lowest = dev->objectBucket[x].count;
1635                         l = x;
1636                 }
1637                 
1638         }
1639         
1640         // If we didn't find an empty list, then try
1641         // looking a bit further for a short one
1642         
1643         for(i = 0; i < 10 && lowest > 3; i++)
1644          {
1645                 x++;
1646                 x %=  YAFFS_NOBJECT_BUCKETS;
1647                 if(dev->objectBucket[x].count < lowest)
1648                 {
1649                         lowest = dev->objectBucket[x].count;
1650                         l = x;
1651                 }
1652                 
1653         }
1654         
1655         return l;
1656 }
1657
1658 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1659 {
1660         int bucket = yaffs_FindNiceObjectBucket(dev);
1661         
1662         // Now find an object value that has not already been taken
1663         // by scanning the list.
1664         
1665         int found = 0;
1666         struct list_head *i;
1667         
1668         __u32 n = (__u32)bucket;
1669
1670         //yaffs_CheckObjectHashSanity();        
1671         
1672         while(!found)
1673         {
1674                 found = 1;
1675                 n +=  YAFFS_NOBJECT_BUCKETS;
1676                 if(1 ||dev->objectBucket[bucket].count > 0)
1677                 {
1678                         list_for_each(i,&dev->objectBucket[bucket].list)
1679                         {
1680                                 // If there is already one in the list
1681                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1682                                 {
1683                                         found = 0;
1684                                 }
1685                         }
1686                 }
1687         }
1688         
1689         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1690         
1691         return n;       
1692 }
1693
1694 void yaffs_HashObject(yaffs_Object *in)
1695 {
1696         int bucket = yaffs_HashFunction(in->objectId);
1697         yaffs_Device *dev = in->myDev;
1698         
1699         if(!list_empty(&in->hashLink))
1700         {
1701                 //YINFO("!!!");
1702         }
1703
1704         
1705         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1706         dev->objectBucket[bucket].count++;
1707
1708 }
1709
1710 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1711 {
1712         int bucket = yaffs_HashFunction(number);
1713         struct list_head *i;
1714         yaffs_Object *in;
1715         
1716         list_for_each(i,&dev->objectBucket[bucket].list)
1717         {
1718                 // Look if it is in the list
1719                 if(i)
1720                 {
1721                         in = list_entry(i, yaffs_Object,hashLink);
1722                         if(in->objectId == number)
1723                         {
1724 #ifdef __KERNEL__
1725                                 // Don't tell the VFS about this one if it is defered free
1726                                 if(in->deferedFree)
1727                                   return NULL;
1728 #endif
1729                                   
1730                                 return in;
1731                         }
1732                 }
1733         }
1734         
1735         return NULL;
1736 }
1737
1738
1739
1740 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1741 {
1742                 
1743         yaffs_Object *theObject;
1744
1745         if(number < 0)
1746         {
1747                 number = yaffs_CreateNewObjectNumber(dev);
1748         }
1749         
1750         theObject = yaffs_AllocateEmptyObject(dev);
1751         
1752         if(theObject)
1753         {
1754                 theObject->fake = 0;
1755                 theObject->renameAllowed = 1;
1756                 theObject->unlinkAllowed = 1;
1757                 theObject->objectId = number;
1758                 yaffs_HashObject(theObject);
1759                 theObject->variantType = type;
1760 #ifdef CONFIG_YAFFS_WINCE
1761                 yfsd_WinFileTimeNow(theObject->win_atime);
1762                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1763                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1764
1765 #else
1766
1767                 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = Y_CURRENT_TIME;               
1768 #endif
1769                 switch(type)
1770                 {
1771                         case YAFFS_OBJECT_TYPE_FILE: 
1772                                 theObject->variant.fileVariant.fileSize = 0;
1773                                 theObject->variant.fileVariant.scannedFileSize = 0;
1774                                 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; // max __u32
1775                                 theObject->variant.fileVariant.topLevel = 0;
1776                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1777                                 break;
1778                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1779                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1780                                 break;
1781                         case YAFFS_OBJECT_TYPE_SYMLINK:
1782                                 // No action required
1783                                 break;
1784                         case YAFFS_OBJECT_TYPE_HARDLINK:
1785                                 // No action required
1786                                 break;
1787                         case YAFFS_OBJECT_TYPE_SPECIAL:
1788                                 // No action required
1789                                 break;
1790                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1791                                 // todo this should not happen
1792                                 break;
1793                 }
1794         }
1795         
1796         return theObject;
1797 }
1798
1799 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1800 {
1801         yaffs_Object *theObject = NULL;
1802         
1803         if(number > 0)
1804         {
1805                 theObject = yaffs_FindObjectByNumber(dev,number);
1806         }
1807         
1808         if(!theObject)
1809         {
1810                 theObject = yaffs_CreateNewObject(dev,number,type);
1811         }
1812         
1813         return theObject;
1814
1815 }
1816
1817 YCHAR *yaffs_CloneString(const YCHAR *str)
1818 {
1819         YCHAR *newStr = NULL;
1820         
1821         if(str && *str)
1822         {
1823                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1824                 yaffs_strcpy(newStr,str);
1825         }
1826
1827         return newStr;
1828         
1829 }
1830
1831 //
1832 // Mknod (create) a new object.
1833 // equivalentObject only has meaning for a hard link;
1834 // aliasString only has meaning for a sumlink.
1835 // rdev only has meaning for devices (a subset of special objects)
1836 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1837                                                                  yaffs_Object *parent,
1838                                                                  const YCHAR *name, 
1839                                                                  __u32 mode,
1840                                                                  __u32 uid,
1841                                                                  __u32 gid,
1842                                                                  yaffs_Object *equivalentObject,
1843                                                                  const YCHAR *aliasString,
1844                                                                  __u32 rdev)
1845 {
1846         yaffs_Object *in;
1847
1848         yaffs_Device *dev = parent->myDev;
1849         
1850         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1851         if(yaffs_FindObjectByName(parent,name))
1852         {
1853                 return NULL;
1854         }
1855         
1856         in = yaffs_CreateNewObject(dev,-1,type);
1857         
1858         if(in)
1859         {
1860                 in->chunkId = -1;
1861                 in->valid = 1;
1862                 in->variantType = type;
1863
1864                 in->st_mode  = mode;
1865                 
1866 #ifdef CONFIG_YAFFS_WINCE
1867                 yfsd_WinFileTimeNow(in->win_atime);
1868                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1869                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1870                 
1871 #else
1872                 in->st_atime = in->st_mtime = in->st_ctime = Y_CURRENT_TIME;
1873
1874                 in->st_rdev  = rdev;
1875                 in->st_uid   = uid;
1876                 in->st_gid   = gid;
1877 #endif          
1878                 in->nDataChunks = 0;
1879
1880                 yaffs_SetObjectName(in,name);
1881                 in->dirty = 1;
1882                 
1883                 yaffs_AddObjectToDirectory(parent,in);
1884                 
1885                 in->myDev = parent->myDev;
1886                 
1887                                 
1888                 switch(type)
1889                 {
1890                         case YAFFS_OBJECT_TYPE_SYMLINK:
1891                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1892                                 break;
1893                         case YAFFS_OBJECT_TYPE_HARDLINK:
1894                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1895                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1896                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1897                                 break;
1898                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1899                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1900                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1901                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1902                                 break;
1903                 }
1904
1905                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1906                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1907                 {
1908                         // Could not create the object header, fail the creation
1909                         yaffs_DestroyObject(in);
1910                         in = NULL;
1911                 }
1912
1913         }
1914         
1915         return in;
1916 }
1917
1918 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1919 {
1920         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1921 }
1922
1923 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1924 {
1925         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1926 }
1927
1928 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1929 {
1930         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL,parent,name,mode,uid,gid,NULL,NULL,rdev);
1931 }
1932
1933 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1934 {
1935         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1936 }
1937
1938 // NB yaffs_Link returns the object id of the equivalent object.
1939 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1940 {
1941         // Get the real object in case we were fed a hard link as an equivalent object
1942         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1943         
1944         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1945         {
1946                 return equivalentObject;
1947         }
1948         else
1949         {
1950                 return NULL;
1951         }
1952         
1953 }
1954
1955
1956 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1957 {
1958         int unlinkOp;
1959         int deleteOp;
1960
1961         if(newDir == NULL)
1962         {
1963                 newDir = obj->parent; // use the old directory
1964         }
1965         
1966         if(newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1967         {
1968                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragendy: yaffs_ChangeObjectName: newDir is not a directory"TENDSTR)));
1969                 YBUG();
1970         }
1971
1972         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1973         if(obj->myDev->isYaffs2)
1974         {
1975                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1976         }
1977         else
1978         {
1979                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1980         }
1981
1982         deleteOp = (newDir == obj->myDev->deletedDir);
1983         
1984         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1985         // duplicate names are allowed.
1986         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1987         if( (unlinkOp|| 
1988                  deleteOp ||
1989                  force || 
1990                  !yaffs_FindObjectByName(newDir,newName))  &&
1991              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1992         {
1993                 yaffs_SetObjectName(obj,newName);
1994                 obj->dirty = 1;
1995                 
1996                 yaffs_AddObjectToDirectory(newDir,obj);
1997                 
1998                 if(unlinkOp) obj->unlinked = 1;
1999                 
2000                 // If it is a deletion then we mark it as a shrink for gc purposes.
2001                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
2002                 {
2003                         return YAFFS_OK;
2004                 }
2005         }
2006         
2007         return YAFFS_FAIL;
2008 }
2009
2010
2011
2012 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
2013 {
2014         yaffs_Object *obj;
2015         int force = 0;
2016         
2017 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2018         // Special case for case insemsitive systems (eg. WinCE).
2019         // While look-up is case insensitive, the name isn't.
2020         // THerefore we might want to change x.txt to X.txt
2021         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
2022         {
2023                 force = 1;
2024         }       
2025 #endif
2026         
2027         obj = yaffs_FindObjectByName(oldDir,oldName);
2028         if(obj && obj->renameAllowed)
2029         {
2030                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
2031         }
2032         return YAFFS_FAIL;
2033 }
2034
2035
2036 #if 0
2037
2038 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
2039 {
2040         // Scan the buckets and check that the lists 
2041         // have as many members as the count says there are
2042         int bucket;
2043         int countEm;
2044         struct list_head *j;
2045         int ok = YAFFS_OK;
2046         
2047         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2048         {
2049                 countEm = 0;
2050                 
2051                 list_for_each(j,&dev->objectBucket[bucket].list)
2052                 {
2053                         countEm++;
2054                 }
2055                 
2056                 if(countEm != dev->objectBucket[bucket].count)
2057                 {
2058                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2059                         ok = YAFFS_FAIL;
2060                 }
2061         }
2062
2063         return ok;
2064 }
2065
2066
2067 void yaffs_ObjectTest(yaffs_Device *dev)
2068 {
2069         yaffs_Object *in[1000];
2070         int inNo[1000];
2071         yaffs_Object *inold[1000];
2072         int i;
2073         int j;
2074         
2075         memset(in,0,1000*sizeof(yaffs_Object *));
2076         memset(inold,0,1000*sizeof(yaffs_Object *));
2077         
2078         yaffs_CheckObjectHashSanity(dev);
2079         
2080         for(j = 0; j < 10; j++)
2081         {
2082                 //T(("%d\n",j));
2083                 
2084                 for(i = 0; i < 1000; i++)
2085                 {
2086                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2087                         if(!in[i])
2088                         {
2089                                 YINFO("No more inodes");
2090                         }
2091                         else
2092                         {
2093                                 inNo[i] = in[i]->objectId;
2094                         }
2095                 }
2096                 
2097                 for(i = 0; i < 1000; i++)
2098                 {
2099                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2100                         {
2101                                 //T(("Differnce in look up test\n"));
2102                         }
2103                         else
2104                         {
2105                                 // T(("Look up ok\n"));
2106                         }
2107                 }
2108                 
2109                 yaffs_CheckObjectHashSanity(dev);
2110         
2111                 for(i = 0; i < 1000; i+=3)
2112                 {
2113                         yaffs_FreeObject(in[i]);        
2114                         in[i] = NULL;
2115                 }
2116                 
2117         
2118                 yaffs_CheckObjectHashSanity(dev);
2119         }
2120                 
2121 }
2122
2123 #endif
2124
2125 /////////////////////////// Block Management and Page Allocation ///////////////////
2126
2127
2128 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2129 {
2130         dev->allocationBlock = -1; // force it to get a new one
2131         //Todo we're assuming the malloc will pass.
2132         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2133         // Set up dynamic blockinfo stuff.
2134         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2135         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2136         if(dev->blockInfo && dev->chunkBits)
2137         {
2138                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2139                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2140                 return YAFFS_OK;
2141         }
2142         
2143         return YAFFS_FAIL;
2144         
2145 }
2146
2147 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2148 {
2149         YFREE(dev->blockInfo);
2150         dev->blockInfo = NULL;
2151         YFREE(dev->chunkBits);
2152         dev->chunkBits = NULL;
2153 }
2154
2155
2156 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2157 {
2158         int i;
2159         __u32 seq;
2160         yaffs_BlockInfo *b;
2161         
2162         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2163         
2164         if(!bi->hasShrinkHeader) return 1; // can gc
2165
2166
2167         // Find the oldest dirty sequence number if we don't know it and save it
2168         // so we don't have to keep recomputing it.
2169         if(!dev->oldestDirtySequence)
2170         {
2171                 seq = dev->sequenceNumber;
2172
2173                 for(i = dev->startBlock; i <= dev->endBlock; i++)
2174                 {
2175                         b = yaffs_GetBlockInfo(dev,i);
2176                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2177                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2178                         b->sequenceNumber < seq)
2179                         {
2180                                 seq = b->sequenceNumber;
2181                         }
2182                 }
2183                 dev->oldestDirtySequence = seq;
2184         }
2185
2186
2187         // Can't do gc of this block if there are any blocks older than this one that have
2188         // discarded pages.
2189         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2190         
2191         
2192         return 1;
2193
2194 }
2195
2196 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2197 // for garbage collection.
2198 //
2199
2200
2201
2202 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2203 {
2204
2205         int b = dev->currentDirtyChecker;
2206         
2207         int i;
2208         int iterations;
2209         int dirtiest = -1;
2210         int pagesInUse; 
2211         yaffs_BlockInfo *bi;
2212         static int  nonAggressiveSkip = 0;
2213
2214         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2215         // search harder.
2216         // else (we're doing a leasurely gc), then we only bother to do this if the
2217         // block has only a few pages in use.
2218         
2219
2220         nonAggressiveSkip--;
2221
2222         if(!aggressive &&(nonAggressiveSkip > 0))
2223         {
2224                 return -1;
2225         }
2226
2227         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2228         if(aggressive)
2229         {
2230                 iterations = dev->endBlock - dev->startBlock + 1;
2231         }
2232         else
2233         {
2234                 iterations = dev->endBlock - dev->startBlock + 1;
2235                 iterations = iterations / 16; 
2236                 if(iterations > 200)
2237                 {
2238                         iterations = 200;
2239                 }
2240         }
2241         
2242         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2243         {
2244                 b++;
2245                 if ( b < dev->startBlock || b > dev->endBlock)
2246                 {
2247                         b =  dev->startBlock;
2248                 }
2249
2250                 if(b < dev->startBlock || b > dev->endBlock)
2251                 {
2252                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2253                         YBUG();
2254                 }
2255                 
2256                 bi = yaffs_GetBlockInfo(dev,b);
2257                 
2258                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2259                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2260                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2261                 {
2262                         dirtiest = b;
2263                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2264                 }
2265         }
2266         
2267         dev->currentDirtyChecker = b;
2268         
2269         if(dirtiest > 0)
2270         {
2271                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2272         }
2273         
2274         dev->oldestDirtySequence = 0; // clear this
2275         
2276         if(dirtiest > 0)
2277         {
2278                 nonAggressiveSkip = 4;
2279         }
2280
2281         return dirtiest;
2282 }
2283
2284
2285 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2286 {
2287         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2288         
2289         int erasedOk = 0;
2290         
2291         // If the block is still healthy erase it and mark as clean.
2292         // If the block has had a data failure, then retire it.
2293         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2294
2295         if(!bi->needsRetiring)
2296         {
2297                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2298                 if(!erasedOk)
2299                 {
2300                         dev->nErasureFailures++;
2301                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2302                 }
2303         }
2304
2305         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2306         {
2307                         int i;
2308                         for(i = 0; i < dev->nChunksPerBlock; i++)
2309                         {
2310                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2311                                         {
2312                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2313                                         }
2314                         }
2315         }
2316         
2317         if( erasedOk )
2318         {
2319                 // Clean it up...
2320                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2321                 dev->nErasedBlocks++;
2322                 bi->pagesInUse = 0;
2323                 bi->softDeletions = 0;
2324                 bi->hasShrinkHeader=0;
2325                 yaffs_ClearChunkBits(dev,blockNo);
2326         
2327                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2328         }
2329         else
2330         {
2331                 yaffs_RetireBlock(dev,blockNo);
2332                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2333         }
2334 }
2335
2336 #if 0
2337 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2338 {
2339         int i,j;
2340         yaffs_BlockInfo *bi;
2341         
2342         for(i= dev->startBlock; i <=dev->endBlock; i++)
2343         {
2344                 bi = yaffs_GetBlockInfo(dev,i);
2345                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2346                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2347                 
2348                 for(j = 0; j < dev->nChunksPerBlock; j++)
2349                 {
2350                         if(yaffs_CheckChunkBit(dev,i,j))
2351                         {
2352                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2353
2354                         }
2355                 }
2356                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2357
2358         }
2359 }
2360 #endif
2361
2362
2363 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2364 {
2365         int i;
2366         
2367         yaffs_BlockInfo *bi;
2368         
2369 #if 0
2370         static int j = 0;
2371         j++;
2372         if(j < 0 || j > 100)
2373         {
2374                 j = 0;
2375                 yaffs_DumpBlockStats(dev);
2376         }
2377         
2378 #endif
2379         
2380         if(dev->nErasedBlocks < 1)
2381         {
2382                 // Hoosterman we've got a problem.
2383                 // Can't get space to gc
2384                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2385
2386                 return -1;
2387         }
2388         
2389         // Find an empty block.
2390         
2391         for(i = dev->startBlock; i <= dev->endBlock; i++)
2392         {
2393                 dev->allocationBlockFinder++;
2394                 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock) 
2395                 {
2396                         dev->allocationBlockFinder = dev->startBlock;
2397                 }
2398                 
2399                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2400
2401                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2402                 {
2403                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2404                         dev->sequenceNumber++;
2405                         bi->sequenceNumber = dev->sequenceNumber;
2406                         dev->nErasedBlocks--;           
2407                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d, %d left" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber, dev->nErasedBlocks));      
2408                         return dev->allocationBlockFinder;
2409                 }
2410         }
2411         
2412         
2413         T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been %d" TENDSTR),dev->nErasedBlocks));
2414         
2415         
2416         
2417
2418         
2419         return -1;      
2420 }
2421
2422
2423
2424 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2425 {
2426         int retVal;
2427         yaffs_BlockInfo *bi;
2428         
2429         if(dev->allocationBlock < 0)
2430         {
2431                 // Get next block to allocate off
2432                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2433                 dev->allocationPage = 0;
2434         }
2435         
2436         if(!useReserve &&  dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2437         {
2438                 // Not enough space to allocate unless we're allowed to use the reserve.
2439                 return -1;
2440         }
2441
2442         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2443         {
2444                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2445         }
2446
2447         
2448         // Next page please....
2449         if(dev->allocationBlock >= 0)
2450         {
2451                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2452                 
2453                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2454                                   dev->allocationPage;
2455                 bi->pagesInUse++;
2456                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2457
2458                 dev->allocationPage++;
2459                 
2460                 dev->nFreeChunks--;
2461                 
2462                 // If the block is full set the state to full
2463                 if(dev->allocationPage >= dev->nChunksPerBlock)
2464                 {
2465                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2466                         dev->allocationBlock = -1;
2467                 }
2468
2469
2470                 return retVal;
2471                 
2472         }
2473         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2474
2475         return -1;      
2476 }
2477
2478
2479 // To determine if we have enough space we just look at the 
2480 // number of erased blocks.
2481 // The cache is allowed to use reserved blocks.
2482
2483 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2484 {
2485         return (dev->nErasedBlocks >= dev->nReservedBlocks);
2486 }
2487
2488
2489 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2490 {
2491                 int n;
2492
2493                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2494
2495                 if(dev->allocationBlock> 0)
2496                 {
2497                         n += (dev->nChunksPerBlock - dev->allocationPage);
2498                 }
2499
2500                 return n;
2501
2502 }
2503
2504 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2505 {
2506         int oldChunk;
2507         int newChunk;
2508         int chunkInBlock;
2509         int markNAND;
2510         int retVal = YAFFS_OK;
2511         int cleanups = 0;
2512         int i;
2513
2514         int chunksBefore = yaffs_GetErasedChunks(dev);
2515         int chunksAfter;
2516
2517         yaffs_ExtendedTags  tags;
2518         
2519         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2520         
2521         yaffs_Object *object;
2522         
2523         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2524
2525         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2526         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2527
2528         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2529
2530
2531         if(!yaffs_StillSomeChunkBits(dev,block))
2532         {
2533                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2534                 yaffs_BlockBecameDirty(dev,block);
2535         }
2536         else
2537         {
2538
2539                         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2540
2541         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2542             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2543             chunkInBlock++, oldChunk++ )
2544         {
2545                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2546                 {
2547                         
2548                         // This page is in use and might need to be copied off
2549                         
2550                         markNAND = 1;
2551                         
2552                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2553                         
2554                         yaffs_InitialiseTags(&tags);
2555                         
2556                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2557
2558                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2559                         
2560                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2561                         
2562                         if(!object)
2563                         {
2564                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2565                         }
2566                         
2567                         if(object && object->deleted && tags.chunkId != 0)
2568                         {
2569                                 // Data chunk in a deleted file, throw it away
2570                                 // It's a deleted data chunk,
2571                                 // No need to copy this, just forget about it and fix up the
2572                                 // object.
2573                                 
2574                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2575                                 object->nDataChunks--;
2576                                 
2577                                 if(object->nDataChunks <= 0)
2578                                 {
2579                                         // remeber to clean up the object
2580                                         dev->gcCleanupList[cleanups] = tags.objectId;
2581                                         cleanups++;
2582                                 }
2583                                 markNAND = 0;
2584                         }
2585                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2586                         {
2587                                 // Deleted object header with no data chunks.
2588                                 // Can be discarded and the file deleted.
2589                                 object->chunkId = 0;
2590                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2591                                 object->variant.fileVariant.top = NULL;
2592                                 yaffs_DoGenericObjectDeletion(object);
2593                                 
2594                         }
2595                         else if(object)
2596                         {
2597                                 // It's either a data chunk in a live file or
2598                                 // an ObjectHeader, so we're interested in it.
2599                                 // NB Need to keep the ObjectHeaders of deleted files
2600                                 // until the whole file has been deleted off
2601                                 tags.serialNumber++;
2602
2603                                 dev->nGCCopies++;
2604
2605                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2606                         
2607                                 if(newChunk < 0)
2608                                 {
2609                                         retVal =  YAFFS_FAIL;
2610                                 }
2611                                 else
2612                                 {
2613                         
2614                                         // Ok, now fix up the Tnodes etc.
2615                         
2616                                         if(tags.chunkId == 0)
2617                                         {
2618                                                 // It's a header
2619                                                 object->chunkId = newChunk;
2620                                                 object->serial = tags.serialNumber;
2621                                         }
2622                                         else
2623                                         {
2624                                                 // It's a data chunk
2625                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2626                                         }
2627                                 }
2628                         }
2629                         
2630                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2631                         
2632                 }
2633         }
2634
2635         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2636
2637
2638         // Do any required cleanups
2639         for(i = 0; i < cleanups; i++)
2640         {                                               
2641                 // Time to delete the file too
2642                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2643                 if(object)
2644                 {
2645                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2646                         object->variant.fileVariant.top = NULL;
2647                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2648                         yaffs_DoGenericObjectDeletion(object);
2649                 }
2650
2651         }
2652
2653         }
2654
2655         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2656         {
2657                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2658         }
2659                         
2660         return YAFFS_OK;
2661 }
2662
2663 #if 0
2664 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2665 {
2666         // find a file to delete
2667         struct list_head *i;    
2668         yaffs_Object *l;
2669
2670
2671         //Scan the unlinked files looking for one to delete
2672         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2673         {
2674                 if(i)
2675                 {
2676                         l = list_entry(i, yaffs_Object,siblings);
2677                         if(l->deleted)
2678                         {
2679                                 return l;                       
2680                         }
2681                 }
2682         }       
2683         return NULL;
2684 }
2685
2686
2687 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2688 {
2689         // This does background deletion on unlinked files.. only deleted ones.
2690         // If we don't have a file we're working on then find one
2691         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2692         {
2693                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2694         }
2695         
2696         // OK, we're working on a file...
2697         if(dev->unlinkedDeletion)
2698         {
2699                 yaffs_Object *obj = dev->unlinkedDeletion;
2700                 int delresult;
2701                 int limit; // Number of chunks to delete in a file.
2702                                    // NB this can be exceeded, but not by much.
2703                                    
2704                 limit = -1;
2705
2706                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2707                 
2708                 if(obj->nDataChunks == 0)
2709                 {
2710                         // Done all the deleting of data chunks.
2711                         // Now dump the header and clean up
2712                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2713                         obj->variant.fileVariant.top = NULL;
2714                         yaffs_DoGenericObjectDeletion(obj);
2715                         dev->nDeletedFiles--;
2716                         dev->nUnlinkedFiles--;
2717                         dev->nBackgroundDeletions++;
2718                         dev->unlinkedDeletion = NULL;   
2719                 }
2720         }
2721 }
2722
2723 #endif
2724
2725
2726
2727 // New garbage collector
2728 // If we're very low on erased blocks then we do aggressive garbage collection
2729 // otherwise we do "leasurely" garbage collection.
2730 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2731 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2732 //
2733 // The idea is to help clear out space in a more spread-out manner.
2734 // Dunno if it really does anything useful.
2735 //
2736 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2737 {
2738         int block;
2739         int aggressive; 
2740         int gcOk = YAFFS_OK;
2741         int maxTries = 0;
2742         
2743         //yaffs_DoUnlinkedFileDeletion(dev);
2744
2745         // This loop should pass the first time.
2746         // We'll only see looping here if the erase of the collected block fails.
2747         
2748         do{
2749                 maxTries++;
2750                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2751                 {
2752                         // We need a block soon...
2753                         aggressive = 1;
2754                 }
2755                 else 
2756                 {
2757                         // We're in no hurry
2758                         aggressive = 0;
2759                 }
2760         
2761                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2762         
2763                 if(block > 0)
2764                 {
2765                         dev->garbageCollections++;
2766                         if(!aggressive)
2767                         {
2768                                 dev->passiveGarbageCollections++;
2769                         }
2770
2771                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2772
2773                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2774                 }
2775
2776                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) 
2777                 {
2778                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2779                 }
2780         } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2781
2782         return aggressive ? gcOk: YAFFS_OK;
2783 }
2784
2785
2786 //////////////////////////// TAGS ///////////////////////////////////////
2787
2788
2789
2790 #if 0
2791
2792 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2793 {
2794         // Calculate an ecc
2795         
2796         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2797         unsigned  i,j;
2798         unsigned  ecc = 0;
2799         unsigned bit = 0;
2800
2801         tags->ecc = 0;
2802         
2803         for(i = 0; i < 8; i++)
2804         {
2805                 for(j = 1; j &0xff; j<<=1)
2806                 {
2807                         bit++;
2808                         if(b[i] & j)
2809                         {
2810                                 ecc ^= bit;
2811                         }
2812                 }
2813         }
2814         
2815         tags->ecc = ecc;
2816         
2817         
2818 }
2819
2820 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2821 {
2822         unsigned ecc = tags->ecc;
2823         
2824         yaffs_CalcTagsECC(tags);
2825         
2826         ecc ^= tags->ecc;
2827         
2828         if(ecc && ecc <= 64)
2829         {
2830                 // TODO: Handle the failure better. Retire?
2831                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2832
2833                 ecc--;
2834                                 
2835                 b[ecc / 8] ^= (1 << (ecc & 7));
2836                 
2837                 // Now recvalc the ecc
2838                 yaffs_CalcTagsECC(tags);
2839                 
2840                 return 1; // recovered error
2841         }
2842         else if(ecc)
2843         {
2844                 // Wierd ecc failure value
2845                 // TODO Need to do somethiong here
2846                 return -1; //unrecovered error
2847         }
2848         
2849         return 0;
2850 }
2851
2852 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2853 {
2854         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2855         
2856         yaffs_CalcTagsECC(tagsPtr);
2857         
2858         sparePtr->tagByte0 = tu->asBytes[0];
2859         sparePtr->tagByte1 = tu->asBytes[1];
2860         sparePtr->tagByte2 = tu->asBytes[2];
2861         sparePtr->tagByte3 = tu->asBytes[3];
2862         sparePtr->tagByte4 = tu->asBytes[4];
2863         sparePtr->tagByte5 = tu->asBytes[5];
2864         sparePtr->tagByte6 = tu->asBytes[6];
2865         sparePtr->tagByte7 = tu->asBytes[7];
2866 }
2867
2868 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2869 {
2870         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2871         int result;
2872
2873         tu->asBytes[0]= sparePtr->tagByte0;
2874         tu->asBytes[1]= sparePtr->tagByte1;
2875         tu->asBytes[2]= sparePtr->tagByte2;
2876         tu->asBytes[3]= sparePtr->tagByte3;
2877         tu->asBytes[4]= sparePtr->tagByte4;
2878         tu->asBytes[5]= sparePtr->tagByte5;
2879         tu->asBytes[6]= sparePtr->tagByte6;
2880         tu->asBytes[7]= sparePtr->tagByte7;
2881         
2882         result =  yaffs_CheckECCOnTags(tagsPtr);
2883         if(result> 0)
2884         {
2885                 dev->tagsEccFixed++;
2886         }
2887         else if(result <0)
2888         {
2889                 dev->tagsEccUnfixed++;
2890         }
2891 }
2892
2893 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2894 {
2895         memset(spare,0xFF,sizeof(yaffs_Spare));
2896 }
2897
2898 #endif
2899
2900 #if 0
2901 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2902 {
2903         // NB There must be tags, data is optional
2904         // If there is data, then an ECC is calculated on it.
2905         
2906         yaffs_Spare spare;
2907         
2908         if(!tags)
2909         {
2910                 return YAFFS_FAIL;
2911         }
2912         
2913         //yaffs_SpareInitialise(&spare);
2914         
2915         //if(!dev->useNANDECC && buffer)
2916         //{
2917         //      yaffs_CalcECC(buffer,&spare);
2918         //}
2919         
2920         //yaffs_LoadTagsIntoSpare(&spare,tags);
2921         
2922         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2923         
2924 }
2925 #endif
2926
2927 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2928 {
2929         return  (  tags->chunkId == chunkInObject &&
2930                            tags->objectId == objectId &&
2931                            !tags->chunkDeleted) ? 1 : 0;
2932         
2933 }
2934
2935 /////////////////////////////////////////////////////////////////////////////////////////////////////////
2936
2937
2938 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2939 {
2940         //Get the Tnode, then get the level 0 offset chunk offset
2941     yaffs_Tnode *tn;     
2942     int theChunk = -1;
2943     yaffs_ExtendedTags localTags;
2944     int retVal = -1;
2945     
2946     yaffs_Device *dev = in->myDev;
2947     
2948     
2949     if(!tags)
2950     {
2951         // Passed a NULL, so use our own tags space
2952         tags = &localTags;
2953     }
2954     
2955     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2956     
2957     if(tn)
2958     {
2959                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2960                 
2961                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2962     }
2963     return retVal;
2964 }
2965
2966 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2967 {
2968         //Get the Tnode, then get the level 0 offset chunk offset
2969     yaffs_Tnode *tn;     
2970     int theChunk = -1;
2971     yaffs_ExtendedTags localTags;
2972
2973     yaffs_Device *dev = in->myDev;
2974     int retVal = -1;
2975     
2976     if(!tags)
2977     {
2978         // Passed a NULL, so use our own tags space
2979         tags = &localTags;
2980     }
2981     
2982     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2983     
2984     if(tn)
2985     {
2986     
2987                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2988                 
2989                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2990     
2991                 // Delete the entry in the filestructure (if found)
2992                 if(retVal != -1)
2993                 {
2994                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2995                 }
2996     }
2997     else
2998     {
2999         //T(("No level 0 found for %d\n", chunkInInode));
3000     }
3001     
3002     if(retVal == -1)
3003     {
3004         //T(("Could not find %d to delete\n",chunkInInode));
3005     }
3006     return retVal;
3007 }
3008
3009
3010 #ifdef YAFFS_PARANOID
3011
3012 static int yaffs_CheckFileSanity(yaffs_Object *in)
3013 {
3014         int chunk;
3015         int nChunks;
3016         int fSize;
3017         int failed = 0;
3018         int objId;
3019         yaffs_Tnode *tn;
3020     yaffs_Tags localTags;
3021     yaffs_Tags *tags = &localTags;
3022     int theChunk;
3023     int chunkDeleted;
3024     
3025         
3026         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3027         {
3028                 //T(("Object not a file\n"));
3029                 return YAFFS_FAIL;
3030         }
3031         
3032         objId = in->objectId;
3033         fSize  = in->variant.fileVariant.fileSize;
3034         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3035         
3036         for(chunk = 1; chunk <= nChunks; chunk++)
3037         {
3038                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3039     
3040                 if(tn)
3041                 {
3042     
3043                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3044                         
3045                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3046                         {
3047
3048
3049                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3050                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3051                                 {
3052                                         // found it;
3053                                 
3054                                 }
3055                         }
3056                         else
3057                         {
3058                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
3059                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
3060                                                 
3061                                 failed = 1;
3062                                                         
3063                         }
3064     
3065                 }
3066                 else
3067                 {
3068                         //T(("No level 0 found for %d\n", chunk));
3069                 }
3070         }
3071         
3072         return failed ? YAFFS_FAIL : YAFFS_OK;
3073 }
3074
3075 #endif
3076
3077 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3078 {
3079         // NB inScan is zero unless scanning. For forward scanning, inScan is > 0; for backward scanning inScan is < 0
3080         yaffs_Tnode *tn;
3081         yaffs_Device *dev = in->myDev;
3082         int existingChunk;
3083         yaffs_ExtendedTags existingTags;
3084         yaffs_ExtendedTags newTags;
3085         unsigned existingSerial, newSerial;
3086         
3087         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3088         {
3089                 // Just ignore an attempt at putting a chunk into a non-file during scanning
3090                 // If it is not during Scanning then something went wrong!
3091                 if(!inScan)
3092                 {
3093                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy:attempt to put data chunk into a non-file" TENDSTR)));
3094                         YBUG();
3095                 }
3096                 
3097                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3098                 return YAFFS_OK;
3099         }
3100                 
3101         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3102         if(!tn)
3103         {
3104                 return YAFFS_FAIL;
3105         }
3106
3107         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
3108         
3109         if(inScan != 0)
3110         {
3111                 // If we're scanning then we need to test for duplicates
3112                 // NB This does not need to be efficient since it should only ever 
3113                 // happen when the power fails during a write, then only one
3114                 // chunk should ever be affected.
3115                 //
3116                 // Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3117                 // Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3118                 
3119         
3120                 
3121                 if(existingChunk != 0)
3122                 {
3123                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3124                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3125                         //
3126                         // We have a duplicate now we need to decide which one to use:
3127                         //
3128                         // Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3129                         // Forward scanning YAFFS2: The new one is what we use, dump the old one.
3130                         // YAFFS1: Get both sets of tags and compare serial numbers.
3131                         //
3132                         //
3133                         
3134                         if(inScan > 0)
3135                         {
3136                                 // Only do this for forward scanning
3137                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3138                         
3139                         
3140                                 // Do a proper find
3141                                 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3142                         }
3143
3144                         if(existingChunk <=0)
3145                         {
3146                                 //Hoosterman - how did this happen?
3147                                 
3148                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3149
3150                         }
3151
3152                         
3153                         // NB The deleted flags should be false, otherwise the chunks will 
3154                         // not be loaded during a scan
3155                         
3156                         newSerial = newTags.serialNumber;
3157                         existingSerial = existingTags.serialNumber;
3158                         
3159                         if( (inScan > 0) &&
3160                             ( in->myDev->isYaffs2  ||
3161                               existingChunk <= 0 ||
3162                              ((existingSerial+1) & 3) == newSerial))
3163                         {
3164                                 // Forward scanning.                            
3165                                 // Use new
3166                                 // Delete the old one and drop through to update the tnode
3167                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3168                         }
3169                         else
3170                         {
3171                                 // Backward scanning or we want to use the existing one
3172                                 // Use existing.
3173                                 // Delete the new one and return early so that the tnode isn't changed
3174                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3175                                 return YAFFS_OK;
3176                         }
3177                 }
3178
3179         }
3180                 
3181         if(existingChunk == 0)
3182         {
3183                 in->nDataChunks++;
3184         }
3185         
3186         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3187         
3188         return YAFFS_OK;
3189 }
3190
3191
3192
3193 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3194 {
3195     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3196     
3197     if(chunkInNAND >= 0)
3198     {
3199                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3200         }
3201         else
3202         {
3203                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3204
3205                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3206                 return 0;
3207         }
3208
3209 }
3210
3211
3212 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3213 {
3214         int block;
3215         int page;
3216         yaffs_ExtendedTags tags;
3217         yaffs_BlockInfo *bi;
3218         
3219         if(chunkId <= 0) return;        
3220         
3221         dev->nDeletions++;
3222         block = chunkId / dev->nChunksPerBlock;
3223         page = chunkId % dev->nChunksPerBlock;
3224         
3225         bi = yaffs_GetBlockInfo(dev,block);
3226         
3227         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3228         
3229         if(markNAND && 
3230           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3231           !dev->isYaffs2)
3232         {
3233 //              yaffs_SpareInitialise(&spare);
3234
3235 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3236
3237                 //read data before write, to ensure correct ecc 
3238                 //if we're using MTD verification under Linux
3239                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3240 #endif
3241
3242                 yaffs_InitialiseTags(&tags);
3243
3244                 tags.chunkDeleted = 1;
3245
3246         
3247                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3248                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3249         }
3250         else
3251         {
3252                         dev->nUnmarkedDeletions++;
3253         }       
3254         
3255         
3256         // Pull out of the management area.
3257         // If the whole block became dirty, this will kick off an erasure.
3258         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3259             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3260             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3261             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3262         {
3263                 dev->nFreeChunks++;
3264
3265                 yaffs_ClearChunkBit(dev,block,page);
3266                 bi->pagesInUse--;
3267                 
3268                 if(bi->pagesInUse == 0 &&
3269                    !bi->hasShrinkHeader &&
3270                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3271                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3272             {
3273                 yaffs_BlockBecameDirty(dev,block);
3274             }
3275
3276         }
3277         else
3278         {
3279                 // T(("Bad news deleting chunk %d\n",chunkId));
3280         }
3281         
3282 }
3283
3284
3285
3286
3287 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3288 {
3289         // Find old chunk Need to do this to get serial number
3290         // Write new one and patch into tree.
3291         // Invalidate old tags.
3292
3293     int prevChunkId;
3294     yaffs_ExtendedTags prevTags;
3295     
3296     int newChunkId;
3297     yaffs_ExtendedTags newTags;
3298
3299     yaffs_Device *dev = in->myDev;    
3300
3301         yaffs_CheckGarbageCollection(dev);
3302
3303         // Get the previous chunk at this location in the file if it exists
3304     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3305     
3306     // Set up new tags
3307     yaffs_InitialiseTags(&newTags);
3308     
3309         newTags.chunkId = chunkInInode;
3310         newTags.objectId = in->objectId;
3311         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3312         newTags.byteCount = nBytes;
3313                 
3314 //      yaffs_CalcTagsECC(&newTags);
3315
3316         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3317         
3318         if(newChunkId >= 0)
3319         {
3320                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3321                 
3322                 
3323                 if(prevChunkId >= 0)
3324                 {
3325                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3326         
3327                 }
3328                 
3329                 yaffs_CheckFileSanity(in);
3330         }
3331         return newChunkId;
3332
3333
3334
3335
3336
3337 }
3338
3339
3340 // UpdateObjectHeader updates the header on NAND for an object.
3341 // If name is not NULL, then that new name is used.
3342 //
3343 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3344 {
3345
3346         yaffs_BlockInfo *bi;
3347         
3348         yaffs_Device *dev = in->myDev;
3349         
3350     int prevChunkId;
3351     int retVal = 0;
3352     
3353     int newChunkId;
3354     yaffs_ExtendedTags newTags;
3355     
3356     __u8 *buffer = NULL;
3357     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3358     
3359    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3360     
3361     yaffs_ObjectHeader *oh = NULL;
3362     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3363
3364     
3365     if(!in->fake || force)
3366     {
3367   
3368                 yaffs_CheckGarbageCollection(dev);              
3369     
3370             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3371             oh  = (yaffs_ObjectHeader *)buffer;
3372     
3373                 prevChunkId = in->chunkId;
3374     
3375                 if(prevChunkId >= 0)
3376                 {
3377                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3378                         memcpy(oldName,oh->name,sizeof(oh->name));      
3379                 }
3380
3381                 memset(buffer,0xFF,dev->nBytesPerChunk);
3382
3383                 // Header data
3384                 oh->type = in->variantType;
3385                 
3386                 oh->st_mode = in->st_mode;
3387
3388 #ifdef CONFIG_YAFFS_WINCE
3389                 oh->win_atime[0] = in->win_atime[0];
3390                 oh->win_ctime[0] = in->win_ctime[0];
3391                 oh->win_mtime[0] = in->win_mtime[0];
3392                 oh->win_atime[1] = in->win_atime[1];
3393                 oh->win_ctime[1] = in->win_ctime[1];
3394                 oh->win_mtime[1] = in->win_mtime[1];
3395 #else
3396                 oh->st_uid = in->st_uid;
3397                 oh->st_gid = in->st_gid;
3398                 oh->st_atime = in->st_atime;
3399                 oh->st_mtime = in->st_mtime;
3400                 oh->st_ctime = in->st_ctime;
3401                 oh->st_rdev = in->st_rdev;
3402 #endif  
3403                 if(in->parent)
3404                 {
3405                         oh->parentObjectId = in->parent->objectId;
3406                 }
3407                 else
3408                 {
3409                         oh->parentObjectId = 0;
3410                 }
3411                 
3412                 //oh->sum = in->sum;
3413                 if(name && *name)
3414                 {
3415                         memset(oh->name,0,sizeof(oh->name));
3416                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3417                 }
3418                 else if(prevChunkId)
3419                 {       
3420                         memcpy(oh->name, oldName,sizeof(oh->name));
3421                 }
3422                 else
3423                 {
3424                         memset(oh->name,0,sizeof(oh->name));    
3425                 }
3426                 
3427                 oh->isShrink = isShrink;
3428         
3429                 switch(in->variantType)
3430                 {
3431                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3432                                 // Should not happen
3433                                 break;
3434                         case YAFFS_OBJECT_TYPE_FILE:
3435                                 oh->fileSize = (oh->parentObjectId == YAFFS_OBJECTID_DELETED ||
3436                                                 oh->parentObjectId == YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.fileVariant.fileSize;
3437                                 break;
3438                         case YAFFS_OBJECT_TYPE_HARDLINK:
3439                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3440                                 break;
3441                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3442                                 // Do nothing
3443                                 break;
3444                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3445                                 // Do nothing
3446                                 break;
3447                         case YAFFS_OBJECT_TYPE_SYMLINK:
3448                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3449                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3450                                 break;
3451                 }
3452
3453                 // Tags
3454                 yaffs_InitialiseTags(&newTags);
3455                 in->serial++;
3456                 newTags.chunkId = 0;
3457                 newTags.objectId = in->objectId;
3458                 newTags.serialNumber = in->serial;
3459                 
3460                 // Add extra info for file header
3461                 
3462                 newTags.extraHeaderInfoAvailable = 1;
3463                 newTags.extraParentObjectId = oh->parentObjectId;
3464                 newTags.extraFileLength = oh->fileSize;
3465                 newTags.extraIsShrinkHeader = oh->isShrink;
3466                 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3467                 newTags.extraObjectType  = in->variantType;
3468                 
3469                 // Create new chunk in NAND
3470                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3471     
3472                 if(newChunkId >= 0)
3473                 {
3474                 
3475                         in->chunkId = newChunkId;       
3476                 
3477                         if(prevChunkId >= 0)
3478                         {
3479                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3480                         }
3481                 
3482                         in->dirty = 0;
3483                         
3484                         // If this was a shrink, then mark the block that the chunk lives on
3485                         if(isShrink)
3486                         {
3487                                 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3488                                 bi->hasShrinkHeader = 1;
3489                         }
3490                         
3491                 }
3492                 
3493                 retVal =  newChunkId;
3494
3495     }
3496     
3497         if(buffer) 
3498                 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3499         
3500     return retVal;
3501 }
3502
3503
3504 /////////////////////// Short Operations Cache ////////////////////////////////
3505 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3506 //      reads might be short sequential reads, and a lot of writes may be short 
3507 //  sequential writes. eg. scanning/writing a jpeg file.
3508 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3509 //  with dumb-as-a-rock code.
3510 //  There are a limited number (~10) of cache chunks per device so that we don't
3511 //  need a very intelligent search.
3512
3513
3514
3515
3516
3517 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3518 {
3519         yaffs_Device *dev = obj->myDev;
3520         int lowest;
3521         int i;
3522         yaffs_ChunkCache *cache;
3523         int chunkWritten = 0;
3524         //int nBytes;
3525         int nCaches = obj->myDev->nShortOpCaches;
3526         
3527         if  (nCaches > 0)
3528         {
3529                 do{
3530                         cache = NULL;
3531                 
3532                         // Find the dirty cache for this object with the lowest chunk id.
3533                         for(i = 0; i < nCaches; i++)
3534                         {
3535                                 if(dev->srCache[i].object == obj &&
3536                                    dev->srCache[i].dirty)
3537                                 {
3538                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3539                                         {
3540                                                 cache = &dev->srCache[i];
3541                                                 lowest = cache->chunkId;
3542                                         }
3543                                 }
3544                         }
3545                 
3546                         if(cache && !cache->locked)
3547                         {
3548                                 //Write it out and free it up
3549
3550 #if 0
3551                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3552                         
3553                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3554                                 {
3555                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3556                                 }
3557 #endif                  
3558                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3559                                                                                                                         cache->chunkId,
3560                                                                                                                         cache->data,
3561                                                                                                                         cache->nBytes,1);
3562
3563                                 cache->dirty = 0;
3564                                 cache->object = NULL;
3565                         }
3566                 
3567                 } while(cache && chunkWritten > 0);
3568         
3569                 if(cache)
3570                 {
3571                         //Hoosterman, disk full while writing cache out.
3572                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3573
3574                 }
3575         }       
3576                 
3577 }
3578
3579
3580 // Grab us a chunk for use.
3581 // First look for an empty one. 
3582 // Then look for the least recently used non-dirty one.
3583 // Then look for the least recently used dirty one...., flush and look again.
3584 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3585 {
3586         int i;
3587         int usage;
3588         int theOne;
3589         
3590         if(dev->nShortOpCaches > 0)
3591         {
3592                 for(i = 0; i < dev->nShortOpCaches; i++)
3593                 {
3594                         if(!dev->srCache[i].object)
3595                         {
3596                                 //T(("Grabbing empty %d\n",i));
3597                                 
3598                                 //printf("Grabbing empty %d\n",i);
3599                         
3600                                 return &dev->srCache[i];
3601                         }
3602                 }
3603                 
3604                 return NULL;
3605         
3606                 theOne = -1; 
3607                 usage = 0; // just to stop the compiler grizzling
3608         
3609                 for(i = 0; i < dev->nShortOpCaches; i++)
3610                 {
3611                         if(!dev->srCache[i].dirty &&
3612                         ((dev->srCache[i].lastUse < usage  && theOne >= 0)|| 
3613                                 theOne < 0))
3614                         {
3615                                 usage = dev->srCache[i].lastUse;
3616                                 theOne = i;
3617                         }
3618                 }
3619         
3620                 //T(("Grabbing non-empty %d\n",theOne));
3621                 
3622                 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3623                 
3624                 return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
3625         }
3626         else
3627         {
3628                 return NULL;
3629         }
3630         
3631 }
3632
3633
3634 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3635 {
3636         yaffs_ChunkCache *cache;
3637         yaffs_Object *theObj;
3638         int usage;
3639         int i;
3640         int pushout;
3641         
3642         if(dev->nShortOpCaches > 0)
3643         {
3644                 // Try find a non-dirty one...
3645         
3646                 cache = yaffs_GrabChunkCacheWorker(dev);
3647         
3648                 if(!cache)
3649                 {
3650                         // They were all dirty, find the last recently used object and flush
3651                         // its cache, then  find again.
3652                         // NB what's here is not very accurate, we actually flush the object
3653                         // the last recently used page.
3654                         
3655                         // With locking we can't assume we can use entry zero
3656                         
3657                 
3658                         theObj = NULL;
3659                         usage = -1;
3660                         cache = NULL;
3661                         pushout = -1;
3662         
3663                         for(i = 0; i < dev->nShortOpCaches; i++)
3664                         {
3665                                 if( dev->srCache[i].object && 
3666                                         !dev->srCache[i].locked &&
3667                                         (dev->srCache[i].lastUse < usage || !cache))
3668                                 {
3669                                         usage  = dev->srCache[i].lastUse;
3670                                         theObj = dev->srCache[i].object;
3671                                         cache = &dev->srCache[i];
3672                                         pushout = i;
3673                                 }
3674                         }
3675                 
3676                         if(!cache || cache->dirty)
3677                         {
3678                         
3679                                 //printf("Dirty ");
3680                                 yaffs_FlushFilesChunkCache(theObj);
3681                 
3682                                 // Try again
3683                                 cache = yaffs_GrabChunkCacheWorker(dev);
3684                         }
3685                         else
3686                         {
3687                                 //printf(" pushout %d\n",pushout);
3688                         }
3689                         
3690                 }
3691                 return cache;
3692         }
3693         else
3694                 return NULL;
3695
3696 }
3697
3698
3699 // Find a cached chunk
3700 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3701 {
3702         yaffs_Device *dev = obj->myDev;
3703         int i;
3704         if(dev->nShortOpCaches > 0)
3705         {
3706                 for(i = 0; i < dev->nShortOpCaches; i++)
3707                 {
3708                         if(dev->srCache[i].object == obj && 
3709                         dev->srCache[i].chunkId == chunkId)
3710                         {
3711                                 dev->cacheHits++;
3712                         
3713                                 return &dev->srCache[i];
3714                         }
3715                 }
3716         }
3717         return NULL;
3718 }
3719
3720 // Mark the chunk for the least recently used algorithym
3721 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3722 {
3723
3724         if(dev->nShortOpCaches > 0)
3725         {
3726                 if( dev->srLastUse < 0 || 
3727                         dev->srLastUse > 100000000)
3728                 {
3729                         // Reset the cache usages
3730                         int i;
3731                         for(i = 1; i < dev->nShortOpCaches; i++)
3732                         {
3733                                 dev->srCache[i].lastUse = 0;
3734                         }
3735                         dev->srLastUse = 0;
3736                 }
3737
3738                 dev->srLastUse++;
3739         
3740                 cache->lastUse = dev->srLastUse;
3741
3742                 if(isAWrite)
3743                 {
3744                         cache->dirty = 1;
3745                 }
3746         }
3747 }
3748
3749 // Invalidate a single cache page.
3750 // Do this when a whole page gets written,
3751 // ie the short cache for this page is no longer valid.
3752 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3753 {
3754         if(object->myDev->nShortOpCaches > 0)
3755         {
3756                 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3757
3758                 if(cache)
3759                 {
3760                         cache->object = NULL;
3761                 }
3762         }
3763 }
3764
3765
3766 // Invalidate all the cache pages associated with this object
3767 // Do this whenever ther file is deleted or resized.
3768 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3769 {
3770         int i;
3771         yaffs_Device *dev = in->myDev;
3772         
3773         if(dev->nShortOpCaches > 0)
3774         { 
3775                 // Now invalidate it.
3776                 for(i = 0; i < dev->nShortOpCaches; i++)
3777                 {
3778                         if(dev->srCache[i].object == in)
3779                         {
3780                                 dev->srCache[i].object = NULL;
3781                         }
3782                 }
3783         }
3784 }
3785
3786
3787
3788
3789
3790 ///////////////////////// File read/write ///////////////////////////////
3791 // Read and write have very similar structures.
3792 // In general the read/write has three parts to it
3793 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3794 // * Some complete chunks
3795 // * An incomplete chunk to end off with
3796 //
3797 // Curve-balls: the first chunk might also be the last chunk.
3798
3799 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3800 {
3801         
3802         
3803         int chunk;
3804         int start;
3805         int nToCopy;
3806         int n = nBytes;
3807         int nDone = 0;
3808         yaffs_ChunkCache *cache;
3809         
3810         yaffs_Device *dev;
3811         
3812         dev = in->myDev;
3813         
3814         while(n > 0)
3815         {
3816                 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3817                 start = offset % dev->nBytesPerChunk;
3818
3819                 // OK now check for the curveball where the start and end are in
3820                 // the same chunk.      
3821                 if(     (start + n) < dev->nBytesPerChunk)
3822                 {
3823                         nToCopy = n;
3824                 }
3825                 else
3826                 {
3827                         nToCopy = dev->nBytesPerChunk - start;
3828                 }
3829         
3830                 cache = yaffs_FindChunkCache(in,chunk);
3831                 
3832                 // If the chunk is already in the cache or it is less than a whole chunk
3833                 // then use the cache (if there is caching)
3834                 // else bypass the cache.
3835                 if( cache || nToCopy != dev->nBytesPerChunk)
3836                 {
3837                         if(dev->nShortOpCaches > 0)
3838                         {
3839                                 
3840                                 // If we can't find the data in the cache, then load it up.
3841                                 
3842                                 if(!cache)
3843                                 {
3844                                         cache = yaffs_GrabChunkCache(in->myDev);
3845                                         cache->object = in;
3846                                         cache->chunkId = chunk;
3847                                         cache->dirty = 0;
3848                                         cache->locked = 0;
3849                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3850                                         cache->nBytes = 0;      
3851                                 }
3852                         
3853                                 yaffs_UseChunkCache(dev,cache,0);
3854
3855                                 cache->locked = 1;
3856
3857 #ifdef CONFIG_YAFFS_WINCE
3858                                 yfsd_UnlockYAFFS(TRUE);
3859 #endif
3860                                 memcpy(buffer,&cache->data[start],nToCopy);
3861
3862 #ifdef CONFIG_YAFFS_WINCE
3863                                 yfsd_LockYAFFS(TRUE);
3864 #endif
3865                                 cache->locked = 0;
3866                         }
3867                         else
3868                         {
3869                                 // Read into the local buffer then copy...
3870                                 
3871                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3872                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);            
3873 #ifdef CONFIG_YAFFS_WINCE
3874                                 yfsd_UnlockYAFFS(TRUE);
3875 #endif
3876                                 memcpy(buffer,&localBuffer[start],nToCopy);
3877
3878 #ifdef CONFIG_YAFFS_WINCE
3879                                 yfsd_LockYAFFS(TRUE);
3880 #endif
3881                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3882                         }
3883
3884                 }
3885                 else
3886                 {
3887 #ifdef CONFIG_YAFFS_WINCE
3888                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3889                         
3890                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3891                         // This is because we otherwise screw up WinCE's memory mapper
3892                         yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3893
3894 #ifdef CONFIG_YAFFS_WINCE
3895                                 yfsd_UnlockYAFFS(TRUE);
3896 #endif
3897                         memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3898
3899 #ifdef CONFIG_YAFFS_WINCE
3900                                 yfsd_LockYAFFS(TRUE);
3901                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3902 #endif
3903
3904 #else
3905                         // A full chunk. Read directly into the supplied buffer.
3906                         yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3907 #endif
3908                 }
3909                 
3910                 n -= nToCopy;
3911                 offset += nToCopy;
3912                 buffer += nToCopy;
3913                 nDone += nToCopy;
3914                 
3915         }
3916         
3917         return nDone;
3918 }
3919
3920
3921
3922 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes, int writeThrough)
3923 {       
3924         
3925         int chunk;
3926         int start;
3927         int nToCopy;
3928         int n = nBytes;
3929         int nDone = 0;
3930         int nToWriteBack;
3931         int startOfWrite = offset;
3932         int chunkWritten = 0;
3933         int nBytesRead;
3934         
3935         yaffs_Device *dev;
3936         
3937         dev = in->myDev;
3938         
3939         
3940         while(n > 0 && chunkWritten >= 0)
3941         {
3942                 chunk = offset / dev->nBytesPerChunk + 1;
3943                 start = offset % dev->nBytesPerChunk;
3944                 
3945
3946                 // OK now check for the curveball where the start and end are in
3947                 // the same chunk.
3948                 
3949                 if((start + n) < dev->nBytesPerChunk)
3950                 {
3951                         nToCopy = n;
3952                         
3953                         // Now folks, to calculate how many bytes to write back....
3954                         // If we're overwriting and not writing to then end of file then
3955                         // we need to write back as much as was there before.
3956                         
3957                         nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3958                         
3959                         if(nBytesRead > dev->nBytesPerChunk)
3960                         {
3961                                 nBytesRead = dev->nBytesPerChunk;
3962                         }
3963                         
3964                         nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3965                         
3966                 }
3967                 else
3968                 {
3969                         nToCopy = dev->nBytesPerChunk - start;
3970                         nToWriteBack = dev->nBytesPerChunk;
3971                 }
3972         
3973                 if(nToCopy != dev->nBytesPerChunk)
3974                 {
3975                         // An incomplete start or end chunk (or maybe both start and end chunk)
3976                         if(dev->nShortOpCaches > 0)
3977                            {
3978                                 yaffs_ChunkCache *cache;
3979                                 // If we can't find the data in the cache, then load it up.
3980                                 cache = yaffs_FindChunkCache(in,chunk);
3981                                 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3982                                 {
3983                                         cache = yaffs_GrabChunkCache(in->myDev);
3984                                         cache->object = in;
3985                                         cache->chunkId = chunk;
3986                                         cache->dirty = 0;
3987                                         cache->locked = 0;
3988                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);            
3989                                 }
3990                         
3991                                 if(cache)
3992                                 {       
3993                                         yaffs_UseChunkCache(dev,cache,1);
3994                                         cache->locked = 1;
3995 #ifdef CONFIG_YAFFS_WINCE
3996                                 yfsd_UnlockYAFFS(TRUE);
3997 #endif
3998         
3999                                         memcpy(&cache->data[start],buffer,nToCopy);
4000
4001 #ifdef CONFIG_YAFFS_WINCE
4002                                 yfsd_LockYAFFS(TRUE);
4003 #endif
4004                                         cache->locked = 0;
4005                                         cache->nBytes = nToWriteBack;
4006                                         
4007                                         if(writeThrough)
4008                                         {
4009                                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
4010                                                                                             cache->chunkId,
4011                                                                                             cache->data,
4012                                                                                             cache->nBytes,1);
4013                                                 cache->dirty = 0;
4014                                         }
4015
4016                                 }
4017                                 else
4018                                 {
4019                                         chunkWritten = -1; // fail the write
4020                                 }
4021                         }
4022                         else
4023                         {
4024                                 // An incomplete start or end chunk (or maybe both start and end chunk)
4025                                 // Read into the local buffer then copy, then copy over and write back.
4026                                 
4027                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4028                 
4029                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
4030                                 
4031 #ifdef CONFIG_YAFFS_WINCE
4032                                 yfsd_UnlockYAFFS(TRUE);
4033 #endif
4034                                         
4035                                 memcpy(&localBuffer[start],buffer,nToCopy);
4036                         
4037 #ifdef CONFIG_YAFFS_WINCE
4038                                 yfsd_LockYAFFS(TRUE);
4039 #endif
4040                                 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
4041                                 
4042                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4043                         
4044                                 //T(("Write with readback to chunk %d %d  start %d  copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
4045                         }
4046                         
4047                 }
4048                 else
4049                 {
4050                         
4051 #ifdef CONFIG_YAFFS_WINCE
4052                         // Under WinCE can't do direct transfer. Need to use a local buffer.
4053                         // This is because we otherwise screw up WinCE's memory mapper
4054                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4055 #ifdef CONFIG_YAFFS_WINCE
4056                                 yfsd_UnlockYAFFS(TRUE);
4057 #endif
4058                         memcpy(localBuffer,buffer,dev->nBytesPerChunk);
4059 #ifdef CONFIG_YAFFS_WINCE
4060                                 yfsd_LockYAFFS(TRUE);
4061 #endif
4062                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
4063                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4064 #else
4065                         // A full chunk. Write directly from the supplied buffer.
4066                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
4067 #endif
4068                         // Since we've overwritten the cached data, we better invalidate it.
4069                         yaffs_InvalidateChunkCache(in,chunk);
4070                         //T(("Write to chunk %d %d\n",chunk,chunkWritten));
4071                 }
4072                 
4073                 if(chunkWritten >= 0)
4074                 {
4075                         n -= nToCopy;
4076                         offset += nToCopy;
4077                         buffer += nToCopy;
4078                         nDone += nToCopy;
4079                 }
4080                 
4081         }
4082         
4083         // Update file object
4084         
4085         if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
4086         {
4087                 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4088         }
4089         
4090         in->dirty = 1;
4091         
4092         return nDone;
4093 }
4094
4095 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
4096 {
4097
4098         yaffs_Device *dev = in->myDev;
4099         int oldFileSize = in->variant.fileVariant.fileSize;
4100
4101         
4102         int lastDel = 1 + (oldFileSize-1)/dev->nBytesPerChunk;
4103                 
4104         int startDel = 1 + (newSize + dev->nBytesPerChunk - 1)/
4105                                                 dev->nBytesPerChunk;
4106         int i;
4107         int chunkId;
4108
4109         // Delete backwards so that we don't end up with holes if
4110         // power is lost part-way through the operation.
4111         for(i = lastDel; i >= startDel; i--)
4112         {
4113                 // NB this could be optimised somewhat,
4114                 // eg. could retrieve the tags and write them without
4115                 // using yaffs_DeleteChunk
4116
4117                 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
4118                 if(chunkId > 0)
4119                 {
4120                         if(chunkId < (dev->startBlock * dev->nChunksPerBlock) || 
4121                        chunkId >= ((dev->endBlock+1) * dev->nChunksPerBlock))
4122                         {
4123                                 T(YAFFS_TRACE_ALWAYS,(TSTR("Found daft chunkId %d for %d"TENDSTR),chunkId,i));
4124                         }
4125                         else
4126                         {
4127                                 in->nDataChunks--;
4128                                 yaffs_DeleteChunk(dev,chunkId,1,__LINE__);
4129                         }
4130                 }
4131         }
4132                 
4133 }
4134
4135 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
4136 {
4137
4138         int oldFileSize = in->variant.fileVariant.fileSize;
4139         int sizeOfPartialChunk;
4140         yaffs_Device *dev = in->myDev;
4141         
4142          sizeOfPartialChunk  = newSize % dev->nBytesPerChunk;
4143                 
4144
4145         yaffs_FlushFilesChunkCache(in); 
4146         yaffs_InvalidateWholeChunkCache(in);
4147
4148         yaffs_CheckGarbageCollection(dev);
4149         
4150         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
4151         {
4152                 return yaffs_GetFileSize(in);
4153         }
4154         
4155         if(newSize < oldFileSize)
4156         {
4157
4158                 yaffs_PruneResizedChunks(in,newSize);
4159                 
4160                 if(sizeOfPartialChunk != 0)
4161                 {
4162                         int lastChunk = 1+ newSize/dev->nBytesPerChunk;
4163                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4164                         
4165                         // Got to read and rewrite the last chunk with its new size and zero pad
4166                         yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
4167                         
4168                         memset(localBuffer + sizeOfPartialChunk,0, dev->nBytesPerChunk - sizeOfPartialChunk);
4169                         
4170                         yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
4171                                 
4172                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4173                 }
4174                 
4175                 in->variant.fileVariant.fileSize = newSize;
4176                 
4177                 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
4178                 
4179                 // Write a new object header to show we've shrunk the file
4180                 // Do this only if the file is not in the deleted directories.
4181                 if(in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
4182                    in->parent->objectId != YAFFS_OBJECTID_DELETED
4183                   )
4184                 {
4185                         yaffs_UpdateObjectHeader(in,NULL, 0, 1);
4186                 }
4187
4188                 
4189                 return newSize;
4190                 
4191         }
4192         else
4193         {
4194                 return oldFileSize;
4195         }
4196 }
4197
4198
4199 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4200 {
4201         obj = yaffs_GetEquivalentObject(obj);
4202         
4203         switch(obj->variantType)
4204         {
4205                 case YAFFS_OBJECT_TYPE_FILE: 
4206                         return obj->variant.fileVariant.fileSize;
4207                 case YAFFS_OBJECT_TYPE_SYMLINK:
4208                         return yaffs_strlen(obj->variant.symLinkVariant.alias);
4209                 default:
4210                         return 0;
4211         }
4212 }
4213
4214
4215
4216 // yaffs_FlushFile() updates the file's
4217 // objectId in NAND
4218
4219 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
4220 {
4221         int retVal;
4222         if(in->dirty)
4223         {
4224                 //T(("flushing object header\n"));
4225                 
4226                 yaffs_FlushFilesChunkCache(in);
4227                 if(updateTime)
4228                 {
4229 #ifdef CONFIG_YAFFS_WINCE
4230                         yfsd_WinFileTimeNow(in->win_mtime);
4231 #else
4232
4233                         in->st_mtime = Y_CURRENT_TIME;
4234
4235 #endif
4236                 }
4237
4238                 retVal = (yaffs_UpdateObjectHeader(in,NULL,0,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
4239         }
4240         else
4241         {
4242                 retVal = YAFFS_OK;
4243         }
4244         
4245         return retVal;
4246         
4247 }
4248
4249
4250 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4251 {
4252
4253         // First off, invalidate the file's data in the cache, without flushing.
4254         yaffs_InvalidateWholeChunkCache(in);
4255
4256         if(in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir))
4257         {
4258                 // Move to the unlinked directory so we have a record that it was deleted.
4259                 yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4260
4261         }
4262
4263         
4264         yaffs_RemoveObjectFromDirectory(in);
4265         yaffs_DeleteChunk(in->myDev,in->chunkId,1,__LINE__);
4266         in->chunkId = -1;
4267 #if 0
4268 #ifdef __KERNEL__
4269         if(in->myInode)
4270         {
4271                 in->myInode->u.generic_ip = NULL;
4272                 in->myInode = 0;
4273         }
4274 #endif
4275 #endif
4276
4277         yaffs_FreeObject(in);
4278         return YAFFS_OK;
4279
4280 }
4281
4282 // yaffs_DeleteFile deletes the whole file data
4283 // and the inode associated with the file.
4284 // It does not delete the links associated with the file.
4285 static int yaffs_UnlinkFile(yaffs_Object *in)
4286 {
4287
4288 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
4289
4290         // Delete the file data & tnodes
4291
4292          yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
4293          
4294
4295         yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4296         
4297         return  yaffs_DoGenericObjectDeletion(in);
4298 #else
4299         int retVal;
4300         int immediateDeletion=0;
4301
4302         if(1)
4303         {
4304                 //in->unlinked = 1;
4305                 //in->myDev->nUnlinkedFiles++;
4306                 //in->renameAllowed = 0;
4307 #ifdef __KERNEL__
4308                 if(!in->myInode)
4309                 {
4310                         immediateDeletion = 1;
4311
4312                 }
4313 #else
4314                 if(in->inUse <= 0)
4315                 {
4316                         immediateDeletion = 1;
4317
4318                 }
4319 #endif
4320                 if(immediateDeletion)
4321                 {
4322                         retVal = yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4323                         T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
4324                         in->deleted=1;
4325                         in->myDev->nDeletedFiles++;
4326                         if( 0 && in->myDev->isYaffs2)
4327                         {
4328                                 yaffs_ResizeFile(in,0);
4329                         }
4330                         yaffs_SoftDeleteFile(in);
4331                 }
4332                 else
4333                 {
4334                         retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
4335                 }
4336         
4337         }
4338         return retVal;
4339
4340         
4341 #endif
4342 }
4343
4344 int yaffs_DeleteFile(yaffs_Object *in)
4345 {
4346         int retVal = YAFFS_OK;
4347         
4348         if(in->nDataChunks > 0)
4349         {
4350                 // Use soft deletion
4351                 if(!in->unlinked)
4352                 {
4353                         retVal = yaffs_UnlinkFile(in);
4354                 }
4355                 if(retVal == YAFFS_OK && 
4356                 in->unlinked &&
4357                 !in->deleted)
4358                 {
4359                         in->deleted = 1;
4360                         in->myDev->nDeletedFiles++;
4361                         yaffs_SoftDeleteFile(in);
4362                 }
4363                 return in->deleted ? YAFFS_OK : YAFFS_FAIL;     
4364         }
4365         else
4366         {
4367                 // The file has no data chunks so we toss it immediately
4368                 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4369                 in->variant.fileVariant.top = NULL;
4370                 yaffs_DoGenericObjectDeletion(in);      
4371                 
4372                 return YAFFS_OK;        
4373         }
4374 }
4375
4376 static int yaffs_DeleteDirectory(yaffs_Object *in)
4377 {
4378         //First check that the directory is empty.
4379         if(list_empty(&in->variant.directoryVariant.children))
4380         {
4381                 return  yaffs_DoGenericObjectDeletion(in);
4382         }
4383         
4384         return YAFFS_FAIL;
4385         
4386 }
4387
4388 static int yaffs_DeleteSymLink(yaffs_Object *in)
4389 {
4390         YFREE(in->variant.symLinkVariant.alias);
4391
4392         return  yaffs_DoGenericObjectDeletion(in);
4393 }
4394
4395 static int yaffs_DeleteHardLink(yaffs_Object *in)
4396 {
4397         // remove this hardlink from the list assocaited with the equivalent
4398         // object
4399         list_del(&in->hardLinks);
4400         return  yaffs_DoGenericObjectDeletion(in);      
4401 }
4402
4403
4404 static void yaffs_DestroyObject(yaffs_Object *obj)
4405 {
4406         switch(obj->variantType)
4407         {
4408                 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
4409                 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
4410                 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
4411                 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
4412                 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
4413                 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
4414         }
4415 }
4416
4417
4418 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4419 {
4420
4421         
4422         if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4423         {
4424                 return  yaffs_DeleteHardLink(obj);
4425         }
4426         else if(!list_empty(&obj->hardLinks))
4427         {       
4428                 // Curve ball: We're unlinking an object that has a hardlink.
4429                 //
4430                 //      This problem arises because we are not strictly following
4431                 //  The Linux link/inode model.
4432                 //
4433                 // We can't really delete the object.
4434                 // Instead, we do the following:
4435                 // - Select a hardlink.
4436                 // - Unhook it from the hard links
4437                 // - Unhook it from its parent directory (so that the rename can work)
4438                 // - Rename the object to the hardlink's name.
4439                 // - Delete the hardlink
4440                 
4441                 
4442                 yaffs_Object *hl;
4443                 int retVal;
4444                 YCHAR name[YAFFS_MAX_NAME_LENGTH+1];
4445                 
4446                 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
4447                 
4448                 list_del_init(&hl->hardLinks);
4449                 list_del_init(&hl->siblings);
4450                 
4451                 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
4452                 
4453                 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
4454                 
4455                 if(retVal == YAFFS_OK)
4456                 {
4457                         retVal = yaffs_DoGenericObjectDeletion(hl);
4458                 }
4459                 return retVal;
4460                                 
4461         }
4462         else
4463         {
4464                 switch(obj->variantType)
4465                 {
4466                         case YAFFS_OBJECT_TYPE_FILE:
4467                                 return yaffs_UnlinkFile(obj);
4468                                 break;
4469                         case YAFFS_OBJECT_TYPE_DIRECTORY:
4470                                 return yaffs_DeleteDirectory(obj);
4471                                 break;
4472                         case YAFFS_OBJECT_TYPE_SYMLINK:
4473                                 return yaffs_DeleteSymLink(obj);
4474                                 break;
4475                         case YAFFS_OBJECT_TYPE_SPECIAL:
4476                                 return yaffs_DoGenericObjectDeletion(obj);
4477                                 break;
4478                         case YAFFS_OBJECT_TYPE_HARDLINK:
4479                         case YAFFS_OBJECT_TYPE_UNKNOWN:
4480                         default:
4481                                 return YAFFS_FAIL;
4482                 }
4483         }
4484 }
4485
4486 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4487 {
4488         yaffs_Object *obj;
4489         
4490          obj = yaffs_FindObjectByName(dir,name);
4491          
4492          if(obj && obj->unlinkAllowed)
4493          {
4494                 return yaffs_UnlinkWorker(obj);
4495          }
4496          
4497          return YAFFS_FAIL;
4498         
4499 }
4500
4501 //////////////// Initialisation Scanning /////////////////
4502
4503
4504 #if 0
4505 // For now we use the SmartMedia check.
4506 // We look at the blockStatus byte in the first two chunks
4507 // These must be 0xFF to pass as OK.
4508 // todo: this function needs to be modifyable foir different NAND types
4509 // and different chunk sizes.  Suggest make this into a per-device configurable
4510 // function.
4511 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4512 {
4513         yaffsExtendedTags *tags;
4514         
4515         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&tags,1);
4516 #if 1
4517         if(yaffs_CountBits(spare.blockStatus) < 7)
4518         {
4519                 return 1;
4520         }
4521 #else
4522         if(spare.blockStatus != 0xFF)
4523         {
4524                 return 1;
4525         }
4526 #endif
4527         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4528
4529 #if 1
4530         if(yaffs_CountBits(spare.blockStatus) < 7)
4531         {
4532                 return 1;
4533         }
4534 #else
4535         if(spare.blockStatus != 0xFF)
4536         {
4537                 return 1;
4538         }
4539 #endif
4540         
4541         return 0;
4542         
4543 }
4544
4545 #endif
4546
4547
4548 typedef struct 
4549 {
4550         int seq;
4551         int block;
4552 } yaffs_BlockIndex;
4553
4554
4555
4556 static int yaffs_Scan(yaffs_Device *dev)
4557 {
4558         yaffs_ExtendedTags tags;
4559         int blk;
4560         int blockIterator;
4561         int startIterator;
4562         int endIterator;
4563         int nBlocksToScan = 0;
4564         
4565         int chunk;
4566         int c;
4567         int deleted;
4568         yaffs_BlockState state;
4569         yaffs_Object *hardList = NULL;
4570         yaffs_Object *hl;
4571         yaffs_BlockInfo *bi;
4572         int sequenceNumber;     
4573         yaffs_ObjectHeader *oh;
4574         yaffs_Object *in;
4575         yaffs_Object *parent;
4576         int nBlocks = dev->endBlock - dev->startBlock + 1;
4577         
4578         __u8 *chunkData;
4579
4580         yaffs_BlockIndex *blockIndex = NULL;
4581
4582         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan starts  startblk %d endblk %d..." TENDSTR),dev->startBlock,dev->endBlock));
4583         
4584         chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4585         
4586         
4587         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4588         
4589         if(dev->isYaffs2)
4590         {
4591                 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));               
4592         }
4593         
4594         
4595         // Scan all the blocks to determine their state
4596         for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
4597         {
4598                 bi = yaffs_GetBlockInfo(dev,blk);
4599                 yaffs_ClearChunkBits(dev,blk);
4600                 bi->pagesInUse = 0;
4601                 bi->softDeletions = 0;
4602                                 
4603                 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4604                 
4605                 bi->blockState = state;
4606                 bi->sequenceNumber = sequenceNumber;
4607
4608                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block scanning block %d state %d seq %d" TENDSTR),blk,state,sequenceNumber));
4609                 
4610                 if(state == YAFFS_BLOCK_STATE_DEAD)
4611                 {
4612                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4613                 }
4614                 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4615                 {
4616                         T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block empty " TENDSTR)));
4617                         dev->nErasedBlocks++;
4618                         dev->nFreeChunks += dev->nChunksPerBlock;
4619                 }
4620                 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4621                 {
4622                                         
4623                         // Determine the highest sequence number
4624                         if( dev->isYaffs2 &&
4625                             sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
4626                             sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
4627                          {
4628                                 
4629                                 blockIndex[nBlocksToScan].seq = sequenceNumber;
4630                                 blockIndex[nBlocksToScan].block = blk;
4631                                 
4632                                 nBlocksToScan++;
4633
4634                                 if(sequenceNumber >= dev->sequenceNumber)
4635                                 {
4636                                         dev->sequenceNumber = sequenceNumber;
4637                                 }
4638                         }
4639                         else if(dev->isYaffs2)
4640                         {
4641                                 // TODO: Nasty sequence number!
4642                                 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
4643
4644                         }
4645                 }
4646         }
4647         
4648         // Sort the blocks
4649         // Dungy old bubble sort for now...
4650         if(dev->isYaffs2)
4651         {
4652                 yaffs_BlockIndex temp;
4653                 int i;
4654                 int j;
4655                 
4656                 for(i = 0; i < nBlocksToScan; i++)
4657                         for(j = i+1; j < nBlocksToScan; j++)
4658                          if(blockIndex[i].seq > blockIndex[j].seq)
4659                          {
4660                                 temp = blockIndex[j];
4661                                 blockIndex[j] = blockIndex[i];
4662                                 blockIndex[i] = temp;
4663                          }
4664         }
4665         
4666         
4667         // Now scan the blocks looking at the data.
4668         if(dev->isYaffs2)
4669         {
4670                 startIterator = 0;
4671                 endIterator = nBlocksToScan-1;
4672                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("%d blocks to be scanned" TENDSTR),nBlocksToScan));
4673         }
4674         else
4675         {
4676                 startIterator = dev->startBlock;
4677                 endIterator = dev->endBlock;
4678         }
4679         
4680         // For each block....
4681         for(blockIterator = startIterator; blockIterator <= endIterator; blockIterator++)
4682         {
4683         
4684                 if(dev->isYaffs2)
4685                 {
4686                         // get the block to scan in the correct order
4687                         blk = blockIndex[blockIterator].block;
4688                 }
4689                 else
4690                 {
4691                         blk = blockIterator;
4692                 }
4693
4694
4695                 bi = yaffs_GetBlockInfo(dev,blk);
4696                 state = bi->blockState;
4697                 
4698                 deleted = 0;
4699                 
4700                 // For each chunk in each block that needs scanning....
4701                 for(c = 0; c < dev->nChunksPerBlock && 
4702                                    state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++)
4703                 {
4704                         // Read the tags and decide what to do
4705                         chunk = blk * dev->nChunksPerBlock + c;
4706                         
4707                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
4708
4709                         // Let's have a good look at this chunk...
4710         
4711                         
4712                         if(!dev->isYaffs2 && tags.chunkDeleted)
4713                         {
4714                                 // YAFFS1 only...
4715                                 // A deleted chunk
4716                                 deleted++;
4717                                 dev->nFreeChunks ++;
4718                                 //T((" %d %d deleted\n",blk,c));
4719                         }
4720                         else if(!tags.chunkUsed)
4721                         {
4722                                 // An unassigned chunk in the block
4723                                 // This means that either the block is empty or 
4724                                 // this is the one being allocated from
4725                                 
4726                                 if(c == 0)
4727                                 {
4728                                         // We're looking at the first chunk in the block so the block is unused
4729                                         state = YAFFS_BLOCK_STATE_EMPTY;
4730                                         dev->nErasedBlocks++;
4731                                 }
4732                                 else
4733                                 {
4734                                         // this is the block being allocated from
4735                                         T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4736                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
4737                                         dev->allocationBlock = blk;
4738                                         dev->allocationPage = c;
4739                                         dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
4740                                                                                                           // go forth from here.
4741                                         //Yaffs2 sanity check:
4742                                         // This should be the one with the highest sequence number
4743                                         if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
4744                                         {
4745                                                 T(YAFFS_TRACE_ALWAYS,
4746                                                                 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
4747                                                                 blk,bi->sequenceNumber,dev->sequenceNumber));
4748                                         }
4749                                 }
4750
4751                                 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4752                         }
4753                         else if(tags.chunkId > 0)
4754                         {
4755                                 // chunkId > 0 so it is a data chunk...
4756                                 unsigned int endpos;
4757
4758                                 yaffs_SetChunkBit(dev,blk,c);
4759                                 bi->pagesInUse++;
4760                                                                 
4761                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4762                                 // PutChunkIntoFile checks for a clash (two data chunks with
4763                                 // the same chunkId).
4764                                 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4765                                 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
4766                                 if(in->variantType == YAFFS_OBJECT_TYPE_FILE && in->variant.fileVariant.scannedFileSize <endpos)
4767                                 {
4768                                         in->variant.fileVariant.scannedFileSize = endpos;
4769                                         if(!dev->useHeaderFileSize)
4770                                         {       
4771                                                         in->variant.fileVariant.fileSize = in->variant.fileVariant.scannedFileSize;
4772                                         }
4773
4774                                 }
4775                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
4776                         }
4777                         else
4778                         {
4779                                 // chunkId == 0, so it is an ObjectHeader.
4780                                 // Thus, we read in the object header and make the object
4781                                 yaffs_SetChunkBit(dev,blk,c);
4782                                 bi->pagesInUse++;
4783                                                         
4784                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
4785                                 
4786                                 oh = (yaffs_ObjectHeader *)chunkData;
4787                                 
4788                                 in = yaffs_FindObjectByNumber(dev,tags.objectId);
4789                                 if(in && in->variantType != oh->type)
4790                                 {
4791                                         // This should not happen, but somehow
4792                                         // Wev'e ended up with an objectId that has been reused but not yet 
4793                                         // deleted, and worse still it has changed type. Delete the old object.
4794                                         
4795                                         yaffs_DestroyObject(in);
4796                                         
4797                                         in = 0;
4798                                 }
4799                                 
4800                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4801                                 
4802                                 if(in->valid)
4803                                 {
4804                                         // We have already filled this one. We have a duplicate and need to resolve it.
4805                                         
4806                                         unsigned existingSerial = in->serial;
4807                                         unsigned newSerial = tags.serialNumber;
4808                                         
4809                                         if( dev->isYaffs2 ||
4810                                             ((existingSerial+1) & 3) == newSerial)
4811                                         {
4812                                                 // Use new one - destroy the exisiting one
4813                                                 yaffs_DeleteChunk(dev,in->chunkId,1,__LINE__);
4814                                                 in->valid = 0;
4815                                         }
4816                                         else
4817                                         {
4818                                                 // Use existing - destroy this one.
4819                                                 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
4820                                         }
4821                                 }
4822                                 
4823                                 if(!in->valid &&
4824                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
4825                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4826                                 {
4827                                         // We only load some info, don't fiddle with directory structure
4828                                         in->valid = 1;
4829                                         in->variantType = oh->type;
4830         
4831                                         in->st_mode  = oh->st_mode;
4832 #ifdef CONFIG_YAFFS_WINCE
4833                                         in->win_atime[0] = oh->win_atime[0];
4834                                         in->win_ctime[0] = oh->win_ctime[0];
4835                                         in->win_mtime[0] = oh->win_mtime[0];
4836                                         in->win_atime[1] = oh->win_atime[1];
4837                                         in->win_ctime[1] = oh->win_ctime[1];
4838                                         in->win_mtime[1] = oh->win_mtime[1];
4839 #else
4840                                         in->st_uid   = oh->st_uid;
4841                                         in->st_gid   = oh->st_gid;
4842                                         in->st_atime = oh->st_atime;
4843                                         in->st_mtime = oh->st_mtime;
4844                                         in->st_ctime = oh->st_ctime;
4845                                         in->st_rdev = oh->st_rdev;
4846 #endif
4847                                         in->chunkId  = chunk;
4848
4849                                 }
4850                                 else if(!in->valid)
4851                                 {
4852                                         // we need to load this info
4853                                 
4854                                         in->valid = 1;
4855                                         in->variantType = oh->type;
4856         
4857                                         in->st_mode  = oh->st_mode;
4858 #ifdef CONFIG_YAFFS_WINCE
4859                                         in->win_atime[0] = oh->win_atime[0];
4860                                         in->win_ctime[0] = oh->win_ctime[0];
4861                                         in->win_mtime[0] = oh->win_mtime[0];
4862                                         in->win_atime[1] = oh->win_atime[1];
4863                                         in->win_ctime[1] = oh->win_ctime[1];
4864                                         in->win_mtime[1] = oh->win_mtime[1];
4865 #else
4866                                         in->st_uid   = oh->st_uid;
4867                                         in->st_gid   = oh->st_gid;
4868                                         in->st_atime = oh->st_atime;
4869                                         in->st_mtime = oh->st_mtime;
4870                                         in->st_ctime = oh->st_ctime;
4871                                         in->st_rdev = oh->st_rdev;
4872 #endif
4873                                         in->chunkId  = chunk;
4874
4875                                         yaffs_SetObjectName(in,oh->name);
4876                                         in->dirty = 0;
4877                                                         
4878                                         // directory stuff...
4879                                         // hook up to parent
4880         
4881                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4882                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4883                                         {
4884                                                 // Set up as a directory
4885                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4886                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4887                                         }
4888                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4889                                         {
4890                                                 // Hoosterman, another problem....
4891                                                 // We're trying to use a non-directory as a directory
4892
4893                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4894                                                 parent = dev->lostNFoundDir;
4895                                         }
4896                                 
4897                                         yaffs_AddObjectToDirectory(parent,in);
4898
4899                                         if(0 && (parent == dev->deletedDir ||
4900                                            parent == dev->unlinkedDir))
4901                                         {
4902                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4903                                                 dev->nDeletedFiles++;
4904                                         }
4905                                 
4906                                         // Note re hardlinks.
4907                                         // Since we might scan a hardlink before its equivalent object is scanned
4908                                         // we put them all in a list.
4909                                         // After scanning is complete, we should have all the objects, so we run through this
4910                                         // list and fix up all the chains.              
4911         
4912                                         switch(in->variantType)
4913                                         {
4914                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
4915                                                         break;
4916                                                 case YAFFS_OBJECT_TYPE_FILE:
4917                                                         if(dev->isYaffs2 && oh->isShrink)
4918                                                         {
4919                                                                 // Prune back the shrunken chunks
4920                                                                 yaffs_PruneResizedChunks(in,oh->fileSize);
4921                                                                 // Mark the block as having a shrinkHeader
4922                                                                 bi->hasShrinkHeader = 1;
4923                                                         }
4924                                                         
4925                                                         if(dev->useHeaderFileSize)
4926                                                 
4927                                                                 in->variant.fileVariant.fileSize = oh->fileSize;
4928                                                                 
4929                                                         break;
4930                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
4931                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4932                                                         in->hardLinks.next = (struct list_head *)hardList;
4933                                                         hardList = in;
4934                                                         break;
4935                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
4936                                                         break;
4937                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4938                                                         break;
4939                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
4940                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4941                                                         break;
4942                                         }
4943
4944                                         if(parent == dev->deletedDir)
4945                                         {
4946                                                 yaffs_DestroyObject(in);
4947                                                 bi->hasShrinkHeader = 1;
4948                                         }
4949                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
4950                                 }
4951                         }
4952                 }
4953                 
4954                 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4955                 {
4956                         // If we got this far while scanning, then the block is fully allocated.
4957                         state = YAFFS_BLOCK_STATE_FULL; 
4958                 }
4959                 
4960                 bi->blockState = state;
4961                 
4962                 // Now let's see if it was dirty
4963                 if(     bi->pagesInUse == 0 &&
4964                         !bi->hasShrinkHeader &&
4965                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4966             {
4967                 yaffs_BlockBecameDirty(dev,blk);
4968             }
4969
4970         }
4971         
4972         if(blockIndex)
4973         {
4974                 YFREE(blockIndex);
4975         }
4976         
4977         // Ok, we've done all the scanning.
4978         
4979         // Fix up the hard link chains.
4980         // We should now have scanned all the objects, now it's time to add these 
4981         // hardlinks.
4982         while(hardList)
4983         {
4984                 hl = hardList;
4985                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4986                 
4987                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4988                 
4989                 if(in)
4990                 {
4991                         // Add the hardlink pointers
4992                         hl->variant.hardLinkVariant.equivalentObject=in;
4993                         list_add(&hl->hardLinks,&in->hardLinks);
4994                 }
4995                 else
4996                 {
4997                         //Todo Need to report/handle this better.
4998                         // Got a problem... hardlink to a non-existant object
4999                         hl->variant.hardLinkVariant.equivalentObject=NULL;
5000                         INIT_LIST_HEAD(&hl->hardLinks);
5001                         
5002                 }
5003                 
5004         }
5005         
5006         {
5007                 struct list_head *i;    
5008                 struct list_head *n;
5009                         
5010                 yaffs_Object *l;
5011                 // Soft delete all the unlinked files
5012                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
5013                 {
5014                         if(i)
5015                         {
5016                                 l = list_entry(i, yaffs_Object,siblings);
5017                                 yaffs_DestroyObject(l);         
5018                         }
5019                 }       
5020         }
5021         
5022         yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
5023
5024         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan ends" TENDSTR)));
5025
5026         return YAFFS_OK;
5027 }
5028
5029
5030 static int yaffs_ScanBackwards(yaffs_Device *dev)
5031 {
5032         yaffs_ExtendedTags tags;
5033         int blk;
5034         int blockIterator;
5035         int startIterator;
5036         int endIterator;
5037         int nBlocksToScan = 0;
5038         
5039         int chunk;
5040         int c;
5041         int deleted;
5042         yaffs_BlockState state;
5043         yaffs_Object *hardList = NULL;
5044         yaffs_Object *hl;
5045         yaffs_BlockInfo *bi;
5046         int sequenceNumber;     
5047         yaffs_ObjectHeader *oh;
5048         yaffs_Object *in;
5049         yaffs_Object *parent;
5050         int nBlocks = dev->endBlock - dev->startBlock + 1;
5051         
5052         __u8 *chunkData;
5053
5054         yaffs_BlockIndex *blockIndex = NULL;
5055
5056
5057         if(!dev->isYaffs2)
5058         {
5059                 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
5060                 return YAFFS_FAIL;
5061         }
5062         
5063         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards starts  startblk %d endblk %d..." TENDSTR),dev->startBlock,dev->endBlock));
5064                 
5065         chunkData = yaffs_GetTempBuffer(dev,__LINE__);
5066         
5067         
5068         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
5069         
5070         if(dev->isYaffs2)
5071         {
5072                 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));               
5073         }
5074         
5075         
5076         // Scan all the blocks to determine their state
5077         for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
5078         {
5079                 bi = yaffs_GetBlockInfo(dev,blk);
5080                 yaffs_ClearChunkBits(dev,blk);
5081                 bi->pagesInUse = 0;
5082                 bi->softDeletions = 0;
5083                                 
5084                 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
5085                 
5086                 bi->blockState = state;
5087                 bi->sequenceNumber = sequenceNumber;
5088
5089                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block scanning block %d state %d seq %d" TENDSTR),blk,state,sequenceNumber));
5090                 
5091                 if(state == YAFFS_BLOCK_STATE_DEAD)
5092                 {
5093                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
5094                 }
5095                 else if(state == YAFFS_BLOCK_STATE_EMPTY)
5096                 {
5097                         T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block empty " TENDSTR)));
5098                         dev->nErasedBlocks++;
5099                         dev->nFreeChunks += dev->nChunksPerBlock;
5100                 }
5101                 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5102                 {
5103                                         
5104                         // Determine the highest sequence number
5105                         if( dev->isYaffs2 &&
5106                             sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
5107                             sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
5108                          {
5109                                 
5110                                 blockIndex[nBlocksToScan].seq = sequenceNumber;
5111                                 blockIndex[nBlocksToScan].block = blk;
5112                                 
5113                                 nBlocksToScan++;
5114
5115                                 if(sequenceNumber >= dev->sequenceNumber)
5116                                 {
5117                                         dev->sequenceNumber = sequenceNumber;
5118                                 }
5119                         }
5120                         else if(dev->isYaffs2)
5121                         {
5122                                 // TODO: Nasty sequence number!
5123                                 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
5124
5125                         }
5126                 }
5127         }
5128         
5129         // Sort the blocks
5130         // Dungy old bubble sort for now...
5131         if(dev->isYaffs2)
5132         {
5133                 yaffs_BlockIndex temp;
5134                 int i;
5135                 int j;
5136                 
5137                 for(i = 0; i < nBlocksToScan; i++)
5138                         for(j = i+1; j < nBlocksToScan; j++)
5139                          if(blockIndex[i].seq > blockIndex[j].seq)
5140                          {
5141                                 temp = blockIndex[j];
5142                                 blockIndex[j] = blockIndex[i];
5143                                 blockIndex[i] = temp;
5144                          }
5145         }
5146         
5147         
5148         // Now scan the blocks looking at the data.
5149         if(dev->isYaffs2)
5150         {
5151                 startIterator = 0;
5152                 endIterator = nBlocksToScan-1;
5153                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("%d blocks to be scanned" TENDSTR),nBlocksToScan));
5154         }
5155
5156         
5157         // For each block.... backwards
5158         for(blockIterator = endIterator; blockIterator >= startIterator; blockIterator--)
5159         {
5160         
5161                 // get the block to scan in the correct order
5162                 blk = blockIndex[blockIterator].block;
5163
5164
5165                 bi = yaffs_GetBlockInfo(dev,blk);
5166                 state = bi->blockState;
5167                 
5168                 deleted = 0;
5169                 
5170                 if( 0 && // Disable since this is redundant.
5171                     state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5172                 {
5173                         // Let's look at the first chunk in the block
5174                         chunk = blk * dev->nChunksPerBlock;
5175                         
5176                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
5177
5178                         // Let's have a good look at this chunk...
5179         
5180                         if(!tags.chunkUsed)
5181                         {
5182                                 // An unassigned chunk in the block
5183                                 // This means that either the block is empty or 
5184                                 // this is the one being allocated from
5185                                 
5186                                 // We're looking at the first chunk in the block so the block is unused
5187                                 state = YAFFS_BLOCK_STATE_EMPTY;
5188                                 dev->nErasedBlocks++;
5189                                 dev->nFreeChunks += dev->nChunksPerBlock;
5190                         }
5191                 
5192                 }
5193                 
5194                 // For each chunk in each block that needs scanning....
5195                 for(c = dev->nChunksPerBlock-1; c >= 0 && 
5196                                   (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
5197                                    state == YAFFS_BLOCK_STATE_ALLOCATING); c--)
5198                 {
5199                         // Scan backwards... 
5200                         // Read the tags and decide what to do
5201                         chunk = blk * dev->nChunksPerBlock + c;
5202                         
5203                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
5204
5205                         // Let's have a good look at this chunk...
5206         
5207                         if(!tags.chunkUsed)
5208                         {
5209                                 // An unassigned chunk in the block
5210                                 // This means that either the block is empty or 
5211                                 // this is the one being allocated from
5212                                 
5213                                 if(c == 0)
5214                                 {
5215                                         // We're looking at the first chunk in the block so the block is unused
5216                                         state = YAFFS_BLOCK_STATE_EMPTY;
5217                                         dev->nErasedBlocks++;
5218                                 }
5219                                 else
5220                                 {
5221                                         // this is the block being allocated from
5222                                         if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5223                                         {
5224                                           T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
5225                                         }
5226                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
5227                                         dev->allocationBlock = blk;
5228                                         dev->allocationPage = c;
5229                                         dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
5230                                                                                                           // go forth from here.
5231                                         //Yaffs2 sanity check:
5232                                         // This should be the one with the highest sequence number
5233                                         if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
5234                                         {
5235                                                 T(YAFFS_TRACE_ALWAYS,
5236                                                                 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
5237                                                                 blk,bi->sequenceNumber,dev->sequenceNumber));
5238                                         }
5239                                 }
5240
5241                                 dev->nFreeChunks ++;
5242                         }
5243                         else if(tags.chunkId > 0)
5244                         {
5245                                 // chunkId > 0 so it is a data chunk...
5246                                 unsigned int endpos;
5247                                 
5248                                 __u32 chunkBase = (tags.chunkId - 1)* dev->nBytesPerChunk;
5249
5250                                 yaffs_SetChunkBit(dev,blk,c);
5251                                 bi->pagesInUse++;
5252                                                                 
5253                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
5254                                 if(in->variantType == YAFFS_OBJECT_TYPE_FILE &&
5255                                    chunkBase < in->variant.fileVariant.shrinkSize)
5256                                 {
5257                                         // This has not been invalidated by a resize
5258                                         yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,-1);
5259                                 
5260                                 
5261                                         // File size is calculated by looking at the data chunks if we have not 
5262                                         // seen an object header yet. Stop this practice once we find an object header.
5263                                         endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
5264                                         if(!in->valid && // have not got an object header yet
5265                                            in->variant.fileVariant.scannedFileSize <endpos)
5266                                         {
5267                                                 in->variant.fileVariant.scannedFileSize = endpos;
5268                                                 in->variant.fileVariant.fileSize = in->variant.fileVariant.scannedFileSize;
5269                                         }
5270
5271                                 }
5272                                 else
5273                                 {
5274                                         // This chunk has been invalidated by a resize, so delete
5275                                         yaffs_DeleteChunk(dev,chunk,1,__LINE__);
5276                                         
5277                                         
5278                                 }
5279                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
5280                         }
5281                         else
5282                         {
5283                                 // chunkId == 0, so it is an ObjectHeader.
5284                                 // Thus, we read in the object header and make the object
5285                                 yaffs_SetChunkBit(dev,blk,c);
5286                                 bi->pagesInUse++;
5287                                 
5288                                 oh = NULL;
5289                                 in = NULL;
5290                                 
5291                                 if(tags.extraHeaderInfoAvailable)
5292                                 {
5293                                         in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,tags.extraObjectType);
5294                                 }
5295
5296                                 
5297                                 if(!in || !in->valid)
5298                                 {
5299                                 
5300                                         // If we don't have  valid info then we need to read the chunk
5301                                         // TODO In future we can probably defer reading the chunk and 
5302                                         // living with invalid data until needed.
5303                                                                 
5304                                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
5305                                 
5306                                         oh = (yaffs_ObjectHeader *)chunkData;
5307                                 
5308                                         if(!in)
5309                                            in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
5310                                         
5311                                 }
5312                                 
5313                                 if(!in)
5314                                 {
5315                                         // TODO Hoosterman we have a problem!
5316                                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: Could not make object for object  %d  at chunk %d during scan" TENDSTR),tags.objectId,chunk));
5317
5318                                 }
5319                                 
5320                                 if(in->valid)
5321                                 {
5322                                         // We have already filled this one. We have a duplicate that will be discarded, but 
5323                                         // we first have to suck out resize info if it is a file.
5324                                         
5325                                         if( (in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
5326                                             ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
5327                                             (tags.extraHeaderInfoAvailable && tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
5328                                            )
5329                                         {
5330                                                 __u32 thisSize = (oh) ? oh->fileSize : tags.extraFileLength;
5331                                                 __u32 parentObjectId = (oh) ? oh->parentObjectId : tags.extraParentObjectId;
5332                                                 unsigned isShrink = (oh)  ? oh->isShrink : tags.extraIsShrinkHeader;
5333                                                 
5334                                                 // If it is deleted (unlinked at start also means deleted)
5335                                                 // we treat the file size as being zeroed at this point.
5336                                                 if(parentObjectId == YAFFS_OBJECTID_DELETED ||
5337                                                    parentObjectId == YAFFS_OBJECTID_UNLINKED)
5338                                                 {
5339                                                         thisSize = 0;
5340                                                         isShrink = 1;
5341                                                 }
5342                                                                                                 
5343                                                 if(in->variant.fileVariant.shrinkSize > thisSize)
5344                                                 {
5345                                                         in->variant.fileVariant.shrinkSize = thisSize;
5346                                                 }
5347                                                 
5348                                                 if(isShrink)
5349                                                 {
5350                                                         bi->hasShrinkHeader = 1;
5351                                                 }
5352                                                 
5353                                         }
5354                                         // Use existing - destroy this one.
5355                                         yaffs_DeleteChunk(dev,chunk,1,__LINE__);
5356                 
5357                                 }
5358                                 
5359                                 if(!in->valid &&
5360                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
5361                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
5362                                 {
5363                                         // We only load some info, don't fiddle with directory structure
5364                                         in->valid = 1;
5365                                         in->variantType = oh->type;
5366         
5367                                         in->st_mode  = oh->st_mode;
5368 #ifdef CONFIG_YAFFS_WINCE
5369                                         in->win_atime[0] = oh->win_atime[0];
5370                                         in->win_ctime[0] = oh->win_ctime[0];
5371                                         in->win_mtime[0] = oh->win_mtime[0];
5372                                         in->win_atime[1] = oh->win_atime[1];
5373                                         in->win_ctime[1] = oh->win_ctime[1];
5374                                         in->win_mtime[1] = oh->win_mtime[1];
5375 #else
5376                                         in->st_uid   = oh->st_uid;
5377                                         in->st_gid   = oh->st_gid;
5378                                         in->st_atime = oh->st_atime;
5379                                         in->st_mtime = oh->st_mtime;
5380                                         in->st_ctime = oh->st_ctime;
5381                                         in->st_rdev = oh->st_rdev;
5382 #endif
5383                                         in->chunkId  = chunk;
5384
5385                                 }
5386                                 else if(!in->valid)
5387                                 {
5388                                         // we need to load this info
5389                                 
5390                                         in->valid = 1;
5391                                         in->variantType = oh->type;
5392         
5393                                         in->st_mode  = oh->st_mode;
5394 #ifdef CONFIG_YAFFS_WINCE
5395                                         in->win_atime[0] = oh->win_atime[0];
5396                                         in->win_ctime[0] = oh->win_ctime[0];
5397                                         in->win_mtime[0] = oh->win_mtime[0];
5398                                         in->win_atime[1] = oh->win_atime[1];
5399                                         in->win_ctime[1] = oh->win_ctime[1];
5400                                         in->win_mtime[1] = oh->win_mtime[1];
5401 #else
5402                                         in->st_uid   = oh->st_uid;
5403                                         in->st_gid   = oh->st_gid;
5404                                         in->st_atime = oh->st_atime;
5405                                         in->st_mtime = oh->st_mtime;
5406                                         in->st_ctime = oh->st_ctime;
5407                                         in->st_rdev = oh->st_rdev;
5408 #endif
5409                                         in->chunkId  = chunk;
5410
5411                                         yaffs_SetObjectName(in,oh->name);
5412                                         in->dirty = 0;
5413                                                         
5414                                         // directory stuff...
5415                                         // hook up to parent
5416         
5417                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
5418                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
5419                                         {
5420                                                 // Set up as a directory
5421                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
5422                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
5423                                         }
5424                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5425                                         {
5426                                                 // Hoosterman, another problem....
5427                                                 // We're trying to use a non-directory as a directory
5428
5429                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
5430                                                 parent = dev->lostNFoundDir;
5431                                         }
5432                                 
5433                                         yaffs_AddObjectToDirectory(parent,in);
5434
5435                                         if((parent == dev->deletedDir ||
5436                                            parent == dev->unlinkedDir))
5437                                         {
5438                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
5439                                         }
5440                                         
5441                                         if( oh->isShrink)
5442                                         {
5443                                                 // Mark the block as having a shrinkHeader
5444                                                 bi->hasShrinkHeader = 1;
5445                                         }
5446                                         
5447                                 
5448                                         // Note re hardlinks.
5449                                         // Since we might scan a hardlink before its equivalent object is scanned
5450                                         // we put them all in a list.
5451                                         // After scanning is complete, we should have all the objects, so we run through this
5452                                         // list and fix up all the chains.              
5453         
5454                                         switch(in->variantType)
5455                                         {
5456                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
5457                                                         break;
5458                                                 case YAFFS_OBJECT_TYPE_FILE:
5459
5460                                                         
5461                                                         if(in->variant.fileVariant.scannedFileSize < oh->fileSize)
5462                                                         {
5463                                                                 in->variant.fileVariant.fileSize = oh->fileSize;
5464                                                                 in->variant.fileVariant.scannedFileSize = in->variant.fileVariant.fileSize;
5465                                                         }
5466                                                         
5467                                                                                                         
5468                                                         
5469                                                         if(in->variant.fileVariant.shrinkSize > oh->fileSize)
5470                                                         {
5471                                                                 in->variant.fileVariant.shrinkSize = oh->fileSize;
5472                                                         }
5473                                                                                                         
5474                                                                 
5475                                                         break;
5476                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
5477                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
5478                                                         in->hardLinks.next = (struct list_head *)hardList;
5479                                                         hardList = in;
5480                                                         break;
5481                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
5482                                                         break;
5483                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
5484                                                         break;
5485                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
5486                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
5487                                                         break;
5488                                         }
5489
5490 #if 0
5491                                         if(parent == dev->deletedDir)
5492                                         {
5493                                                 yaffs_DestroyObject(in);
5494                                                 bi->hasShrinkHeader = 1;
5495                                         }
5496 #endif
5497                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
5498                                 }
5499                         }
5500                 }
5501                 
5502                 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5503                 {
5504                         // If we got this far while scanning, then the block is fully allocated.
5505                         state = YAFFS_BLOCK_STATE_FULL; 
5506                 }
5507                 
5508                 bi->blockState = state;
5509                 
5510                 // Now let's see if it was dirty
5511                 if(     bi->pagesInUse == 0 &&
5512                         !bi->hasShrinkHeader &&
5513                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
5514             {
5515                 yaffs_BlockBecameDirty(dev,blk);
5516             }
5517
5518         }
5519         
5520         if(blockIndex)
5521         {
5522                 YFREE(blockIndex);
5523         }
5524         
5525         // Ok, we've done all the scanning.
5526         
5527         // Fix up the hard link chains.
5528         // We should now have scanned all the objects, now it's time to add these 
5529         // hardlinks.
5530         while(hardList)
5531         {
5532                 hl = hardList;
5533                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
5534                 
5535                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
5536                 
5537                 if(in)
5538                 {
5539                         // Add the hardlink pointers
5540                         hl->variant.hardLinkVariant.equivalentObject=in;
5541                         list_add(&hl->hardLinks,&in->hardLinks);
5542                 }
5543                 else
5544                 {
5545                         //Todo Need to report/handle this better.
5546                         // Got a problem... hardlink to a non-existant object
5547                         hl->variant.hardLinkVariant.equivalentObject=NULL;
5548                         INIT_LIST_HEAD(&hl->hardLinks);
5549                         
5550                 }
5551                 
5552         }
5553         
5554         {
5555                 struct list_head *i;    
5556                 struct list_head *n;
5557                         
5558                 yaffs_Object *l;
5559                 
5560                 // Soft delete all the unlinked files
5561                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
5562                 {
5563                         if(i)
5564                         {
5565                                 l = list_entry(i, yaffs_Object,siblings);
5566                                 yaffs_DestroyObject(l);         
5567                         }
5568                 }       
5569                 
5570                 // Soft delete all the deletedDir files
5571                 list_for_each_safe(i,n,&dev->deletedDir->variant.directoryVariant.children)
5572                 {
5573                         if(i)
5574                         {
5575                                 l = list_entry(i, yaffs_Object,siblings);
5576                                 yaffs_DestroyObject(l);         
5577                                 
5578                         }
5579                 }       
5580         }
5581         
5582         yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
5583
5584         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards ends" TENDSTR)));
5585
5586         return YAFFS_OK;
5587 }
5588
5589
5590
5591 ////////////////////////// Directory Functions /////////////////////////
5592
5593
5594 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
5595 {
5596
5597         if(!directory)
5598         {
5599                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: Trying to add an object to a null pointer directory" TENDSTR)));
5600                 YBUG();
5601         }
5602         if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5603         {
5604                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: Trying to add an object to a non-directory" TENDSTR)));
5605                 YBUG();
5606         }
5607
5608                 if(obj->siblings.prev == NULL)
5609         {
5610                 // Not initialised
5611                 INIT_LIST_HEAD(&obj->siblings);
5612                 
5613         }
5614         else if(!list_empty(&obj->siblings))
5615         {
5616                 // If it is holed up somewhere else, un hook it
5617                 list_del_init(&obj->siblings);
5618         }
5619         // Now add it
5620         list_add(&obj->siblings,&directory->variant.directoryVariant.children);
5621         obj->parent = directory;
5622         
5623         if(directory == obj->myDev->unlinkedDir || directory == obj->myDev->deletedDir)
5624         {
5625                 obj->unlinked = 1;
5626                 obj->myDev->nUnlinkedFiles++;
5627                 obj->renameAllowed = 0;
5628         }
5629 }
5630
5631 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
5632 {
5633         list_del_init(&obj->siblings);
5634         obj->parent = NULL;
5635 }
5636
5637 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const YCHAR *name)
5638 {
5639         int sum;
5640         
5641         struct list_head *i;
5642         YCHAR buffer[YAFFS_MAX_NAME_LENGTH+1];
5643         
5644         yaffs_Object *l;
5645         
5646
5647         if(!directory)
5648         {
5649                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: null pointer directory"TENDSTR)));
5650                 YBUG();
5651         }
5652         if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5653         {
5654                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: non-directory"TENDSTR)));
5655                 YBUG();
5656         }
5657
5658         sum = yaffs_CalcNameSum(name);
5659         
5660         list_for_each(i,&directory->variant.directoryVariant.children)
5661         {
5662                 if(i)
5663                 {
5664                         l = list_entry(i, yaffs_Object,siblings);
5665                 
5666                         // Special case for lost-n-found
5667                         if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5668                         {
5669                                 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
5670                                 {
5671                                         return l;
5672                                 }
5673                         }
5674                         else if(yaffs_SumCompare(l->sum, sum)||
5675                                     l->chunkId <= 0) //LostnFound cunk called Objxxx
5676                         {
5677                                 // Do a real check
5678                                 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
5679                                 if(yaffs_strcmp(name,buffer) == 0)
5680                                 {
5681                                         return l;
5682                                 }
5683                         
5684                         }                       
5685                 }
5686         }
5687         
5688         return NULL;
5689 }
5690
5691
5692 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
5693 {
5694         struct list_head *i;    
5695         yaffs_Object *l;
5696         
5697
5698         if(!theDir)
5699         {
5700                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: null pointer directory"TENDSTR)));
5701                 YBUG();
5702         }
5703         if(theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5704         {
5705                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: non-directory"TENDSTR)));
5706                 YBUG();
5707         }
5708         
5709         list_for_each(i,&theDir->variant.directoryVariant.children)
5710         {
5711                 if(i)
5712                 {
5713                         l = list_entry(i, yaffs_Object,siblings);
5714                         if(l && !fn(l))
5715                         {
5716                                 return YAFFS_FAIL;
5717                         }
5718                 }
5719         }
5720         
5721         return YAFFS_OK;
5722
5723 }
5724
5725
5726 // GetEquivalentObject dereferences any hard links to get to the
5727 // actual object.
5728
5729 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5730 {
5731         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
5732         {
5733                 // We want the object id of the equivalent object, not this one
5734                 obj = obj->variant.hardLinkVariant.equivalentObject;
5735         }
5736         return obj;
5737
5738 }
5739
5740 int yaffs_GetObjectName(yaffs_Object *obj,YCHAR *name,int buffSize)
5741 {
5742         memset(name,0,buffSize * sizeof(YCHAR));
5743         
5744         if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5745         {
5746                 yaffs_strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
5747         }
5748         else if(obj->chunkId <= 0)
5749         {
5750                 YCHAR locName[20];
5751                 // make up a name
5752                 yaffs_sprintf(locName,_Y("%s%d"),YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
5753                 yaffs_strncpy(name,locName,buffSize - 1);
5754
5755         }
5756 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5757         else if(obj->shortName[0])
5758         {
5759                 yaffs_strcpy(name,obj->shortName);
5760         }
5761 #endif
5762         else
5763         {
5764                 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev,__LINE__);
5765                 
5766                 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5767
5768                 memset(buffer,0,obj->myDev->nBytesPerChunk);
5769         
5770                 if(obj->chunkId >= 0)
5771                 {
5772                         yaffs_ReadChunkWithTagsFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5773                 }
5774                 yaffs_strncpy(name,oh->name,buffSize - 1);
5775
5776                 yaffs_ReleaseTempBuffer(obj->myDev,buffer,__LINE__);
5777         }
5778         
5779         return yaffs_strlen(name);
5780 }
5781
5782 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5783 {
5784         
5785         // Dereference any hard linking
5786         obj = yaffs_GetEquivalentObject(obj);
5787         
5788         if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5789         {
5790                 return obj->variant.fileVariant.fileSize;
5791         }
5792         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5793         {
5794                 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5795         }
5796         else
5797         {
5798                 // Only a directory should drop through to here
5799                 return obj->myDev->nBytesPerChunk;
5800         }       
5801 }
5802
5803 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5804 {
5805         int count = 0; 
5806         struct list_head *i;
5807         
5808         if(!obj->unlinked)
5809         {
5810                 count++;        // the object itself
5811         }
5812         list_for_each(i,&obj->hardLinks)
5813         {
5814                 count++;        // add the hard links;
5815         }
5816         return count;
5817         
5818 }
5819
5820
5821 int yaffs_GetObjectInode(yaffs_Object *obj)
5822 {
5823         obj = yaffs_GetEquivalentObject(obj);
5824         
5825         return obj->objectId;
5826 }
5827
5828 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5829 {
5830         obj = yaffs_GetEquivalentObject(obj);
5831         
5832         switch(obj->variantType)
5833         {
5834                 case YAFFS_OBJECT_TYPE_FILE:            return DT_REG; break;
5835                 case YAFFS_OBJECT_TYPE_DIRECTORY:       return DT_DIR; break;
5836                 case YAFFS_OBJECT_TYPE_SYMLINK:         return DT_LNK; break;
5837                 case YAFFS_OBJECT_TYPE_HARDLINK:        return DT_REG; break;
5838                 case YAFFS_OBJECT_TYPE_SPECIAL:         
5839                         if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
5840                         if(S_ISCHR(obj->st_mode)) return DT_CHR;
5841                         if(S_ISBLK(obj->st_mode)) return DT_BLK;
5842                         if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
5843                 default: return DT_REG; break;
5844         }
5845 }
5846
5847 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5848 {
5849         obj = yaffs_GetEquivalentObject(obj);
5850         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5851         {
5852                 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5853         }
5854         else
5855         {
5856                 return yaffs_CloneString(_Y(""));
5857         }
5858 }
5859
5860 #ifndef CONFIG_YAFFS_WINCE
5861
5862 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5863 {
5864         unsigned int valid = attr->ia_valid;
5865         
5866         if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
5867         if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
5868         if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
5869         
5870         if(valid & ATTR_ATIME) obj->st_atime = Y_TIME_CONVERT(attr->ia_atime);
5871         if(valid & ATTR_CTIME) obj->st_ctime = Y_TIME_CONVERT(attr->ia_ctime);
5872         if(valid & ATTR_MTIME) obj->st_mtime = Y_TIME_CONVERT(attr->ia_mtime);
5873         
5874         if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
5875         
5876         yaffs_UpdateObjectHeader(obj,NULL,1,0);
5877         
5878         return YAFFS_OK;
5879         
5880 }
5881 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5882 {
5883         unsigned int valid = 0;
5884         
5885         attr->ia_mode = obj->st_mode;   valid |= ATTR_MODE;
5886         attr->ia_uid = obj->st_uid;             valid |= ATTR_UID;
5887         attr->ia_gid = obj->st_gid;             valid |= ATTR_GID;
5888         
5889         Y_TIME_CONVERT(attr->ia_atime)= obj->st_atime;  valid |= ATTR_ATIME;
5890         Y_TIME_CONVERT(attr->ia_ctime) = obj->st_ctime; valid |= ATTR_CTIME;
5891         Y_TIME_CONVERT(attr->ia_mtime) = obj->st_mtime; valid |= ATTR_MTIME;
5892
5893         attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
5894         
5895         attr->ia_valid = valid;
5896         
5897         return YAFFS_OK;
5898         
5899 }
5900
5901 #endif
5902
5903 int yaffs_DumpObject(yaffs_Object *obj)
5904 {
5905 //      __u8 buffer[YAFFS_BYTES_PER_CHUNK];
5906         YCHAR name[257];
5907 //      yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5908
5909 //      memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
5910         
5911 //      if(obj->chunkId >= 0)
5912 //      {
5913 //              yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5914 //      }
5915         
5916         yaffs_GetObjectName(obj,name,256);
5917         
5918         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),
5919                         obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial, 
5920                         obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5921
5922 #if 0
5923         YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
5924                         obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial, 
5925                         obj->sum, obj->chunkId));
5926                 switch(obj->variantType)
5927         {
5928                 case YAFFS_OBJECT_TYPE_FILE: 
5929                         YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
5930                         break;
5931                 case YAFFS_OBJECT_TYPE_DIRECTORY:
5932                         YPRINTF((" DIRECTORY\n"));
5933                         break;
5934                 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
5935                 case YAFFS_OBJECT_TYPE_SYMLINK:
5936                 case YAFFS_OBJECT_TYPE_UNKNOWN:
5937                 default:
5938         }
5939 #endif
5940         
5941         return YAFFS_OK;
5942 }
5943
5944
5945 ///////////////////////// Initialisation code ///////////////////////////
5946
5947
5948 int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5949 {
5950
5951         // Common functions, gotta have
5952         if(!dev->eraseBlockInNAND ||
5953            !dev->initialiseNAND) return 0;
5954
5955 #ifdef CONFIG_YAFFS_YAFFS2
5956
5957         // Can use the "with tags" style interface for yaffs1 or yaffs2
5958         if(dev->writeChunkWithTagsToNAND &&
5959            dev->readChunkWithTagsFromNAND &&
5960            !dev->writeChunkToNAND &&
5961            !dev->readChunkFromNAND &&
5962            dev->markNANDBlockBad &&
5963            dev->queryNANDBlock) return 1;
5964 #endif
5965
5966         // Can use the "spare" style interface for yaffs1
5967         if(!dev->isYaffs2 &&
5968            !dev->writeChunkWithTagsToNAND &&
5969            !dev->readChunkWithTagsFromNAND &&
5970            dev->writeChunkToNAND &&
5971            dev->readChunkFromNAND &&
5972            !dev->markNANDBlockBad &&
5973            !dev->queryNANDBlock) return 1;
5974
5975         return 0; // bad
5976 }
5977
5978
5979 int yaffs_GutsInitialise(yaffs_Device *dev)
5980 {
5981         unsigned x;
5982         int bits;
5983         int extraBits;
5984         int nBlocks;
5985
5986         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
5987         // Check stuff that must be set
5988
5989         if(!dev)
5990         {
5991                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Need a device" TENDSTR)));
5992                 return YAFFS_FAIL;
5993         }
5994
5995         // Check geometry parameters.
5996
5997         if(     (dev->isYaffs2 && dev->nBytesPerChunk <1024)  ||
5998                 (!dev->isYaffs2 && dev->nBytesPerChunk !=512)  ||
5999                 dev->nChunksPerBlock < 2 ||
6000                 dev->nReservedBlocks < 2 ||
6001                 dev->startBlock <= 0 ||
6002                 dev->endBlock <= 0 ||
6003                 dev->endBlock <= (dev->startBlock + dev->nReservedBlocks + 2) // otherwise it is too small
6004           )
6005         {
6006                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s " TENDSTR),
6007                    dev->nBytesPerChunk, dev->isYaffs2 ? "2" : ""));
6008                 return YAFFS_FAIL;
6009         }
6010
6011         if(yaffs_InitialiseNAND(dev) != YAFFS_OK)
6012         {
6013                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
6014                 return YAFFS_FAIL;
6015         }
6016
6017         // Got the right mix of functions?
6018         //
6019         if(!yaffs_CheckDevFunctions(dev))
6020         {
6021                 //Function missing
6022                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device function(s) missing or wrong\n" TENDSTR)));
6023
6024                 return YAFFS_FAIL;
6025         }
6026
6027         // This is really a compilation check.
6028         if(!yaffs_CheckStructures())
6029         {
6030                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
6031                 return YAFFS_FAIL;
6032         }
6033
6034         if(dev->isMounted)
6035         {
6036                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
6037                 return YAFFS_FAIL;
6038         }
6039
6040         //
6041         //
6042         // Finished with most checks. One or two more checks happen later on too.
6043         //
6044
6045         dev->isMounted = 1;
6046
6047
6048         nBlocks = dev->endBlock - dev->startBlock + 1;
6049
6050
6051
6052         // OK now calculate a few things for the device
6053         // Calculate chunkGroupBits.
6054         // We need to find the next power of 2 > than endBlock
6055         
6056         x = dev->nChunksPerBlock * (dev->endBlock+1);
6057         
6058         for(bits = extraBits = 0; x > 1; bits++)
6059         {
6060                 if(x & 1) extraBits++;
6061                 x >>= 1;
6062         }
6063
6064         if(extraBits > 0) bits++;
6065         
6066         
6067         // Level0 Tnodes are 16 bits, so if the bitwidth of the
6068         // chunk range we're using is greater than 16 we need
6069         // to figure out chunk shift and chunkGroupSize
6070         if(bits <= 16) 
6071         {
6072                 dev->chunkGroupBits = 0;
6073         }
6074         else
6075         {
6076                 dev->chunkGroupBits = bits - 16;
6077         }
6078         
6079         dev->chunkGroupSize = 1 << dev->chunkGroupBits;
6080
6081         if(dev->nChunksPerBlock < dev->chunkGroupSize)
6082         {
6083                 // We have a problem because the soft delete won't work if
6084                 // the chunk group size > chunks per block.
6085                 // This can be remedied by using larger "virtual blocks".
6086                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: chunk group too large\n" TENDSTR)));
6087                 
6088                 return YAFFS_FAIL;
6089         }
6090
6091         
6092         // OK, we've finished verifying the device, lets continue with initialisation
6093         
6094         // More device initialisation
6095         dev->garbageCollections = 0;
6096         dev->passiveGarbageCollections = 0;
6097         dev->currentDirtyChecker = 0;
6098         dev->bufferedBlock = -1;
6099         dev->doingBufferedBlockRewrite = 0;
6100         dev->nDeletedFiles = 0;
6101         dev->nBackgroundDeletions=0;
6102         dev->nUnlinkedFiles = 0;
6103         dev->eccFixed=0;
6104         dev->eccUnfixed=0;
6105         dev->tagsEccFixed=0;
6106         dev->tagsEccUnfixed=0;
6107         dev->nErasureFailures = 0;
6108         dev->nErasedBlocks = 0;
6109         
6110         //dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
6111         // Initialise temporary buffers
6112         {
6113                 int i;
6114                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6115                 {
6116                         dev->tempBuffer[i].line = 0; // not in use
6117                         dev->tempBuffer[i].buffer = YMALLOC(dev->nBytesPerChunk);
6118                 }
6119         }
6120         
6121
6122         
6123         yaffs_InitialiseBlocks(dev,nBlocks);
6124         
6125         yaffs_InitialiseTnodes(dev);
6126
6127         yaffs_InitialiseObjects(dev);
6128         
6129         dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
6130         
6131         if(dev->nShortOpCaches > 0)
6132         { 
6133                 int i;
6134                 
6135                 if(dev->nShortOpCaches >  YAFFS_MAX_SHORT_OP_CACHES)
6136                 {
6137                         dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
6138                 }
6139                 
6140                 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
6141                 
6142                 for(i=0; i < dev->nShortOpCaches; i++)
6143                 {
6144                         dev->srCache[i].object = NULL;
6145                         dev->srCache[i].lastUse = 0;
6146                         dev->srCache[i].dirty = 0;
6147                         dev->srCache[i].data = YMALLOC(dev->nBytesPerChunk);
6148                 }
6149                 dev->srLastUse = 0;
6150         }
6151
6152         dev->cacheHits = 0;
6153         
6154         
6155         // Initialise the unlinked, root and lost and found directories
6156         dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = dev->deletedDir = NULL;
6157         
6158         dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
6159         dev->deletedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_DELETED, S_IFDIR);
6160
6161         dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
6162         dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
6163         yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
6164         
6165         if(dev->isYaffs2) 
6166         {
6167                 dev->useHeaderFileSize = 1;
6168         }
6169                 
6170         // Now scan the flash.  
6171         
6172         if(dev->isYaffs2)
6173                 yaffs_ScanBackwards(dev);
6174         else
6175                 yaffs_Scan(dev);
6176         
6177         // Zero out stats
6178         dev->nPageReads = 0;
6179     dev->nPageWrites =  0;
6180         dev->nBlockErasures = 0;
6181         dev->nGCCopies = 0;
6182         dev->nRetriedWrites = 0;
6183
6184         dev->nRetiredBlocks = 0;
6185
6186         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
6187         return YAFFS_OK;
6188                 
6189 }
6190
6191 void yaffs_Deinitialise(yaffs_Device *dev)
6192 {
6193         if(dev->isMounted)
6194         {
6195                 int i;
6196         
6197                 yaffs_DeinitialiseBlocks(dev);
6198                 yaffs_DeinitialiseTnodes(dev);
6199                 yaffs_DeinitialiseObjects(dev);
6200                 if(dev->nShortOpCaches > 0)
6201                 {
6202                                 
6203                         for(i=0; i < dev->nShortOpCaches; i++)
6204                         {
6205                                 YFREE(dev->srCache[i].data);
6206                         }
6207
6208                         YFREE(dev->srCache);
6209                 }
6210
6211                 YFREE(dev->gcCleanupList);
6212
6213                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6214                 {
6215                         YFREE(dev->tempBuffer[i].buffer);
6216                 }
6217                 
6218                 dev->isMounted = 0;
6219         }
6220         
6221 }
6222
6223 #if 0
6224
6225 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
6226 {
6227         int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
6228         
6229         struct list_head *i;    
6230         yaffs_Object *l;
6231         
6232         
6233         // To the free chunks add the chunks that are in the deleted unlinked files.
6234         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
6235         {
6236                 l = list_entry(i, yaffs_Object,siblings);
6237                 if(l->deleted)
6238                 {
6239                         nFree++;
6240                         nFree += l->nDataChunks;
6241                 }
6242         }
6243         
6244         
6245         // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);        
6246
6247         if(nFree < 0) nFree = 0;
6248
6249         return nFree;   
6250         
6251 }
6252
6253 #endif
6254
6255 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
6256 {
6257         int nFree;
6258         int pending;
6259         int b;
6260         int nDirtyCacheChunks=0;
6261         
6262         yaffs_BlockInfo *blk;
6263         
6264         struct list_head *i;    
6265         yaffs_Object *l;
6266         
6267         for(nFree = 0, b = dev->startBlock; b <= dev->endBlock; b++)
6268         {
6269                 blk = yaffs_GetBlockInfo(dev,b);
6270                 
6271                 switch(blk->blockState)
6272                 {
6273                         case YAFFS_BLOCK_STATE_EMPTY:
6274                         case YAFFS_BLOCK_STATE_ALLOCATING: 
6275                         case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break;
6276                         default: break;
6277                 }
6278         }
6279         
6280         
6281         pending = 0;
6282         
6283         // To the free chunks add the chunks that are in the deleted unlinked files.
6284         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
6285         {
6286                 if(i)
6287                 {
6288                         l = list_entry(i, yaffs_Object,siblings);
6289                         if(l->deleted)
6290                         {
6291                                 pending++;
6292                                 pending += l->nDataChunks;
6293                         }
6294                 }
6295         }
6296         
6297         
6298         
6299         //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending);
6300         
6301         if(nFree != dev->nFreeChunks) 
6302         {
6303         //      printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks);
6304         }
6305
6306         nFree += pending;
6307         
6308         // Now count the number of dirty chunks in the cache and subtract those
6309         
6310         {
6311                 int i;
6312                 for(i = 0; i < dev->nShortOpCaches; i++)
6313                 {
6314                         if(dev->srCache[i].dirty) nDirtyCacheChunks++;
6315                 }
6316         }
6317         
6318         nFree -= nDirtyCacheChunks;
6319         
6320         nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
6321         
6322         if(nFree < 0) nFree = 0;
6323
6324         return nFree;   
6325         
6326 }
6327
6328
6329
6330 /////////////////// YAFFS test code //////////////////////////////////
6331
6332 #define yaffs_CheckStruct(structure,syze, name) \
6333            if(sizeof(structure) != syze) \
6334                { \
6335                  T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
6336                  return YAFFS_FAIL; \
6337                    }
6338                  
6339                  
6340 static int yaffs_CheckStructures(void)
6341 {
6342 //      yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
6343 //      yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
6344 //      yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
6345 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
6346         yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
6347 #endif
6348         yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
6349         
6350         
6351         return YAFFS_OK;
6352 }
6353
6354 #if 0
6355 void yaffs_GutsTest(yaffs_Device *dev)
6356 {
6357         
6358         if(yaffs_CheckStructures() != YAFFS_OK)
6359         {
6360                 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
6361                 return;
6362         }
6363         
6364         yaffs_TnodeTest(dev);
6365         yaffs_ObjectTest(dev);  
6366 }
6367 #endif
6368
6369