2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 const char *yaffs_guts_c_version
=
15 "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
19 #include "yaffsinterface.h"
20 #include "yaffs_guts.h"
21 #include "yaffs_tagsvalidity.h"
23 #include "yaffs_tagscompat.h"
24 #ifndef CONFIG_YAFFS_USE_OWN_SORT
25 #include "yaffs_qsort.h"
27 #include "yaffs_nand.h"
29 #include "yaffs_checkptrw.h"
31 #include "yaffs_nand.h"
32 #include "yaffs_packedtags2.h"
35 #ifdef CONFIG_YAFFS_WINCE
36 void yfsd_LockYAFFS(BOOL fsLockOnly
);
37 void yfsd_UnlockYAFFS(BOOL fsLockOnly
);
40 #define YAFFS_PASSIVE_GC_CHUNKS 2
42 #include "yaffs_ecc.h"
45 /* Robustification (if it ever comes about...) */
46 static void yaffs_RetireBlock(yaffs_Device
* dev
, int blockInNAND
);
47 static void yaffs_HandleWriteChunkError(yaffs_Device
* dev
, int chunkInNAND
, int erasedOk
);
48 static void yaffs_HandleWriteChunkOk(yaffs_Device
* dev
, int chunkInNAND
,
50 const yaffs_ExtendedTags
* tags
);
51 static void yaffs_HandleUpdateChunk(yaffs_Device
* dev
, int chunkInNAND
,
52 const yaffs_ExtendedTags
* tags
);
54 /* Other local prototypes */
55 static int yaffs_UnlinkObject( yaffs_Object
*obj
);
56 static int yaffs_ObjectHasCachedWriteData(yaffs_Object
*obj
);
58 static void yaffs_HardlinkFixup(yaffs_Device
*dev
, yaffs_Object
*hardList
);
60 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device
* dev
,
62 yaffs_ExtendedTags
* tags
,
64 static int yaffs_PutChunkIntoFile(yaffs_Object
* in
, int chunkInInode
,
65 int chunkInNAND
, int inScan
);
67 static yaffs_Object
*yaffs_CreateNewObject(yaffs_Device
* dev
, int number
,
68 yaffs_ObjectType type
);
69 static void yaffs_AddObjectToDirectory(yaffs_Object
* directory
,
71 static int yaffs_UpdateObjectHeader(yaffs_Object
* in
, const YCHAR
* name
,
72 int force
, int isShrink
, int shadows
);
73 static void yaffs_RemoveObjectFromDirectory(yaffs_Object
* obj
);
74 static int yaffs_CheckStructures(void);
75 static int yaffs_DeleteWorker(yaffs_Object
* in
, yaffs_Tnode
* tn
, __u32 level
,
76 int chunkOffset
, int *limit
);
77 static int yaffs_DoGenericObjectDeletion(yaffs_Object
* in
);
79 static yaffs_BlockInfo
*yaffs_GetBlockInfo(yaffs_Device
* dev
, int blockNo
);
81 static __u8
*yaffs_GetTempBuffer(yaffs_Device
* dev
, int lineNo
);
82 static void yaffs_ReleaseTempBuffer(yaffs_Device
* dev
, __u8
* buffer
,
85 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct
*dev
,
88 static int yaffs_UnlinkWorker(yaffs_Object
* obj
);
89 static void yaffs_DestroyObject(yaffs_Object
* obj
);
91 static int yaffs_TagsMatch(const yaffs_ExtendedTags
* tags
, int objectId
,
94 loff_t
yaffs_GetFileSize(yaffs_Object
* obj
);
96 static int yaffs_AllocateChunk(yaffs_Device
* dev
, int useReserve
, yaffs_BlockInfo
**blockUsedPtr
);
98 static void yaffs_VerifyFreeChunks(yaffs_Device
* dev
);
100 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object
*in
);
102 #ifdef YAFFS_PARANOID
103 static int yaffs_CheckFileSanity(yaffs_Object
* in
);
105 #define yaffs_CheckFileSanity(in)
108 static void yaffs_InvalidateWholeChunkCache(yaffs_Object
* in
);
109 static void yaffs_InvalidateChunkCache(yaffs_Object
* object
, int chunkId
);
111 static void yaffs_InvalidateCheckpoint(yaffs_Device
*dev
);
113 static int yaffs_FindChunkInFile(yaffs_Object
* in
, int chunkInInode
,
114 yaffs_ExtendedTags
* tags
);
116 static __u32
yaffs_GetChunkGroupBase(yaffs_Device
*dev
, yaffs_Tnode
*tn
, unsigned pos
);
117 static yaffs_Tnode
*yaffs_FindLevel0Tnode(yaffs_Device
* dev
,
118 yaffs_FileStructure
* fStruct
,
122 /* Function to calculate chunk and offset */
124 static void yaffs_AddrToChunk(yaffs_Device
*dev
, loff_t addr
, __u32
*chunk
, __u32
*offset
)
127 /* Easy-peasy power of 2 case */
128 *chunk
= (__u32
)(addr
>> dev
->chunkShift
);
129 *offset
= (__u32
)(addr
& dev
->chunkMask
);
131 else if(dev
->crumbsPerChunk
)
133 /* Case where we're using "crumbs" */
134 *offset
= (__u32
)(addr
& dev
->crumbMask
);
135 addr
>>= dev
->crumbShift
;
136 *chunk
= ((__u32
)addr
)/dev
->crumbsPerChunk
;
137 *offset
+= ((addr
- (*chunk
* dev
->crumbsPerChunk
)) << dev
->crumbShift
);
143 /* Function to return the number of shifts for a power of 2 greater than or equal
144 * to the given number
145 * Note we don't try to cater for all possible numbers and this does not have to
146 * be hellishly efficient.
149 static __u32
ShiftsGE(__u32 x
)
154 nShifts
= extraBits
= 0;
157 if(x
& 1) extraBits
++;
168 /* Function to return the number of shifts to get a 1 in bit 0
171 static __u32
ShiftDiv(__u32 x
)
190 * Temporary buffer manipulations.
193 static int yaffs_InitialiseTempBuffers(yaffs_Device
*dev
)
196 __u8
*buf
= (__u8
*)1;
198 memset(dev
->tempBuffer
,0,sizeof(dev
->tempBuffer
));
200 for (i
= 0; buf
&& i
< YAFFS_N_TEMP_BUFFERS
; i
++) {
201 dev
->tempBuffer
[i
].line
= 0; /* not in use */
202 dev
->tempBuffer
[i
].buffer
= buf
=
203 YMALLOC_DMA(dev
->nDataBytesPerChunk
);
206 return buf
? YAFFS_OK
: YAFFS_FAIL
;
210 static __u8
*yaffs_GetTempBuffer(yaffs_Device
* dev
, int lineNo
)
213 for (i
= 0; i
< YAFFS_N_TEMP_BUFFERS
; i
++) {
214 if (dev
->tempBuffer
[i
].line
== 0) {
215 dev
->tempBuffer
[i
].line
= lineNo
;
216 if ((i
+ 1) > dev
->maxTemp
) {
217 dev
->maxTemp
= i
+ 1;
218 for (j
= 0; j
<= i
; j
++)
219 dev
->tempBuffer
[j
].maxLine
=
220 dev
->tempBuffer
[j
].line
;
223 return dev
->tempBuffer
[i
].buffer
;
227 T(YAFFS_TRACE_BUFFERS
,
228 (TSTR("Out of temp buffers at line %d, other held by lines:"),
230 for (i
= 0; i
< YAFFS_N_TEMP_BUFFERS
; i
++) {
231 T(YAFFS_TRACE_BUFFERS
, (TSTR(" %d "), dev
->tempBuffer
[i
].line
));
233 T(YAFFS_TRACE_BUFFERS
, (TSTR(" " TENDSTR
)));
236 * If we got here then we have to allocate an unmanaged one
240 dev
->unmanagedTempAllocations
++;
241 return YMALLOC(dev
->nDataBytesPerChunk
);
245 static void yaffs_ReleaseTempBuffer(yaffs_Device
* dev
, __u8
* buffer
,
249 for (i
= 0; i
< YAFFS_N_TEMP_BUFFERS
; i
++) {
250 if (dev
->tempBuffer
[i
].buffer
== buffer
) {
251 dev
->tempBuffer
[i
].line
= 0;
257 /* assume it is an unmanaged one. */
258 T(YAFFS_TRACE_BUFFERS
,
259 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR
),
262 dev
->unmanagedTempDeallocations
++;
268 * Determine if we have a managed buffer.
270 int yaffs_IsManagedTempBuffer(yaffs_Device
* dev
, const __u8
* buffer
)
273 for (i
= 0; i
< YAFFS_N_TEMP_BUFFERS
; i
++) {
274 if (dev
->tempBuffer
[i
].buffer
== buffer
)
279 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
280 if( dev
->srCache
[i
].data
== buffer
)
285 if (buffer
== dev
->checkpointBuffer
)
288 T(YAFFS_TRACE_ALWAYS
,
289 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR
)));
296 * Chunk bitmap manipulations
299 static Y_INLINE __u8
*yaffs_BlockBits(yaffs_Device
* dev
, int blk
)
301 if (blk
< dev
->internalStartBlock
|| blk
> dev
->internalEndBlock
) {
303 (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR
),
307 return dev
->chunkBits
+
308 (dev
->chunkBitmapStride
* (blk
- dev
->internalStartBlock
));
311 static Y_INLINE
void yaffs_VerifyChunkBitId(yaffs_Device
*dev
, int blk
, int chunk
)
313 if(blk
< dev
->internalStartBlock
|| blk
> dev
->internalEndBlock
||
314 chunk
< 0 || chunk
>= dev
->nChunksPerBlock
) {
316 (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR
),blk
,chunk
));
321 static Y_INLINE
void yaffs_ClearChunkBits(yaffs_Device
* dev
, int blk
)
323 __u8
*blkBits
= yaffs_BlockBits(dev
, blk
);
325 memset(blkBits
, 0, dev
->chunkBitmapStride
);
328 static Y_INLINE
void yaffs_ClearChunkBit(yaffs_Device
* dev
, int blk
, int chunk
)
330 __u8
*blkBits
= yaffs_BlockBits(dev
, blk
);
332 yaffs_VerifyChunkBitId(dev
,blk
,chunk
);
334 blkBits
[chunk
/ 8] &= ~(1 << (chunk
& 7));
337 static Y_INLINE
void yaffs_SetChunkBit(yaffs_Device
* dev
, int blk
, int chunk
)
339 __u8
*blkBits
= yaffs_BlockBits(dev
, blk
);
341 yaffs_VerifyChunkBitId(dev
,blk
,chunk
);
343 blkBits
[chunk
/ 8] |= (1 << (chunk
& 7));
346 static Y_INLINE
int yaffs_CheckChunkBit(yaffs_Device
* dev
, int blk
, int chunk
)
348 __u8
*blkBits
= yaffs_BlockBits(dev
, blk
);
349 yaffs_VerifyChunkBitId(dev
,blk
,chunk
);
351 return (blkBits
[chunk
/ 8] & (1 << (chunk
& 7))) ? 1 : 0;
354 static Y_INLINE
int yaffs_StillSomeChunkBits(yaffs_Device
* dev
, int blk
)
356 __u8
*blkBits
= yaffs_BlockBits(dev
, blk
);
358 for (i
= 0; i
< dev
->chunkBitmapStride
; i
++) {
366 static int yaffs_CountChunkBits(yaffs_Device
* dev
, int blk
)
368 __u8
*blkBits
= yaffs_BlockBits(dev
, blk
);
371 for (i
= 0; i
< dev
->chunkBitmapStride
; i
++) {
388 static int yaffs_SkipVerification(yaffs_Device
*dev
)
390 return !(yaffs_traceMask
& (YAFFS_TRACE_VERIFY
| YAFFS_TRACE_VERIFY_FULL
));
393 static int yaffs_SkipFullVerification(yaffs_Device
*dev
)
395 return !(yaffs_traceMask
& (YAFFS_TRACE_VERIFY_FULL
));
398 static int yaffs_SkipNANDVerification(yaffs_Device
*dev
)
400 return !(yaffs_traceMask
& (YAFFS_TRACE_VERIFY_NAND
));
403 static const char * blockStateName
[] = {
416 static void yaffs_VerifyBlock(yaffs_Device
*dev
,yaffs_BlockInfo
*bi
,int n
)
421 if(yaffs_SkipVerification(dev
))
424 /* Report illegal runtime states */
425 if(bi
->blockState
<0 || bi
->blockState
>= YAFFS_NUMBER_OF_BLOCK_STATES
)
426 T(YAFFS_TRACE_VERIFY
,(TSTR("Block %d has undefined state %d"TENDSTR
),n
,bi
->blockState
));
428 switch(bi
->blockState
){
429 case YAFFS_BLOCK_STATE_UNKNOWN
:
430 case YAFFS_BLOCK_STATE_SCANNING
:
431 case YAFFS_BLOCK_STATE_NEEDS_SCANNING
:
432 T(YAFFS_TRACE_VERIFY
,(TSTR("Block %d has bad run-state %s"TENDSTR
),
433 n
,blockStateName
[bi
->blockState
]));
436 /* Check pages in use and soft deletions are legal */
438 actuallyUsed
= bi
->pagesInUse
- bi
->softDeletions
;
440 if(bi
->pagesInUse
< 0 || bi
->pagesInUse
> dev
->nChunksPerBlock
||
441 bi
->softDeletions
< 0 || bi
->softDeletions
> dev
->nChunksPerBlock
||
442 actuallyUsed
< 0 || actuallyUsed
> dev
->nChunksPerBlock
)
443 T(YAFFS_TRACE_VERIFY
,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR
),
444 n
,bi
->pagesInUse
,bi
->softDeletions
));
447 /* Check chunk bitmap legal */
448 inUse
= yaffs_CountChunkBits(dev
,n
);
449 if(inUse
!= bi
->pagesInUse
)
450 T(YAFFS_TRACE_VERIFY
,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR
),
451 n
,bi
->pagesInUse
,inUse
));
453 /* Check that the sequence number is valid.
454 * Ten million is legal, but is very unlikely
457 (bi
->blockState
== YAFFS_BLOCK_STATE_ALLOCATING
|| bi
->blockState
== YAFFS_BLOCK_STATE_FULL
) &&
458 (bi
->sequenceNumber
< YAFFS_LOWEST_SEQUENCE_NUMBER
|| bi
->sequenceNumber
> 10000000 ))
459 T(YAFFS_TRACE_VERIFY
,(TSTR("Block %d has suspect sequence number of %d"TENDSTR
),
460 n
,bi
->sequenceNumber
));
464 static void yaffs_VerifyCollectedBlock(yaffs_Device
*dev
,yaffs_BlockInfo
*bi
,int n
)
466 yaffs_VerifyBlock(dev
,bi
,n
);
468 /* After collection the block should be in the erased state */
469 /* TODO: This will need to change if we do partial gc */
471 if(bi
->blockState
!= YAFFS_BLOCK_STATE_EMPTY
){
472 T(YAFFS_TRACE_ERROR
,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR
),
477 static void yaffs_VerifyBlocks(yaffs_Device
*dev
)
480 int nBlocksPerState
[YAFFS_NUMBER_OF_BLOCK_STATES
];
481 int nIllegalBlockStates
= 0;
484 if(yaffs_SkipVerification(dev
))
487 memset(nBlocksPerState
,0,sizeof(nBlocksPerState
));
490 for(i
= dev
->internalStartBlock
; i
<= dev
->internalEndBlock
; i
++){
491 yaffs_BlockInfo
*bi
= yaffs_GetBlockInfo(dev
,i
);
492 yaffs_VerifyBlock(dev
,bi
,i
);
494 if(bi
->blockState
>=0 && bi
->blockState
< YAFFS_NUMBER_OF_BLOCK_STATES
)
495 nBlocksPerState
[bi
->blockState
]++;
497 nIllegalBlockStates
++;
501 T(YAFFS_TRACE_VERIFY
,(TSTR(""TENDSTR
)));
502 T(YAFFS_TRACE_VERIFY
,(TSTR("Block summary"TENDSTR
)));
504 T(YAFFS_TRACE_VERIFY
,(TSTR("%d blocks have illegal states"TENDSTR
),nIllegalBlockStates
));
505 if(nBlocksPerState
[YAFFS_BLOCK_STATE_ALLOCATING
] > 1)
506 T(YAFFS_TRACE_VERIFY
,(TSTR("Too many allocating blocks"TENDSTR
)));
508 for(i
= 0; i
< YAFFS_NUMBER_OF_BLOCK_STATES
; i
++)
509 T(YAFFS_TRACE_VERIFY
,
510 (TSTR("%s %d blocks"TENDSTR
),
511 blockStateName
[i
],nBlocksPerState
[i
]));
513 if(dev
->blocksInCheckpoint
!= nBlocksPerState
[YAFFS_BLOCK_STATE_CHECKPOINT
])
514 T(YAFFS_TRACE_VERIFY
,
515 (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR
),
516 dev
->blocksInCheckpoint
, nBlocksPerState
[YAFFS_BLOCK_STATE_CHECKPOINT
]));
518 if(dev
->nErasedBlocks
!= nBlocksPerState
[YAFFS_BLOCK_STATE_EMPTY
])
519 T(YAFFS_TRACE_VERIFY
,
520 (TSTR("Erased block count wrong dev %d count %d"TENDSTR
),
521 dev
->nErasedBlocks
, nBlocksPerState
[YAFFS_BLOCK_STATE_EMPTY
]));
523 if(nBlocksPerState
[YAFFS_BLOCK_STATE_COLLECTING
] > 1)
524 T(YAFFS_TRACE_VERIFY
,
525 (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR
),
526 nBlocksPerState
[YAFFS_BLOCK_STATE_COLLECTING
]));
528 T(YAFFS_TRACE_VERIFY
,(TSTR(""TENDSTR
)));
533 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
534 * case those tests will not be performed.
536 static void yaffs_VerifyObjectHeader(yaffs_Object
*obj
, yaffs_ObjectHeader
*oh
, yaffs_ExtendedTags
*tags
, int parentCheck
)
538 if(yaffs_SkipVerification(obj
->myDev
))
541 if(!(tags
&& obj
&& oh
)){
542 T(YAFFS_TRACE_VERIFY
,
543 (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR
),
544 (__u32
)tags
,(__u32
)obj
,(__u32
)oh
));
548 if(oh
->type
<= YAFFS_OBJECT_TYPE_UNKNOWN
||
549 oh
->type
> YAFFS_OBJECT_TYPE_MAX
)
550 T(YAFFS_TRACE_VERIFY
,
551 (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR
),
552 tags
->objectId
, oh
->type
));
554 if(tags
->objectId
!= obj
->objectId
)
555 T(YAFFS_TRACE_VERIFY
,
556 (TSTR("Obj %d header mismatch objectId %d"TENDSTR
),
557 tags
->objectId
, obj
->objectId
));
561 * Check that the object's parent ids match if parentCheck requested.
563 * Tests do not apply to the root object.
566 if(parentCheck
&& tags
->objectId
> 1 && !obj
->parent
)
567 T(YAFFS_TRACE_VERIFY
,
568 (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR
),
569 tags
->objectId
, oh
->parentObjectId
));
572 if(parentCheck
&& obj
->parent
&&
573 oh
->parentObjectId
!= obj
->parent
->objectId
&&
574 (oh
->parentObjectId
!= YAFFS_OBJECTID_UNLINKED
||
575 obj
->parent
->objectId
!= YAFFS_OBJECTID_DELETED
))
576 T(YAFFS_TRACE_VERIFY
,
577 (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR
),
578 tags
->objectId
, oh
->parentObjectId
, obj
->parent
->objectId
));
581 if(tags
->objectId
> 1 && oh
->name
[0] == 0) /* Null name */
582 T(YAFFS_TRACE_VERIFY
,
583 (TSTR("Obj %d header name is NULL"TENDSTR
),
586 if(tags
->objectId
> 1 && ((__u8
)(oh
->name
[0])) == 0xff) /* Trashed name */
587 T(YAFFS_TRACE_VERIFY
,
588 (TSTR("Obj %d header name is 0xFF"TENDSTR
),
594 static int yaffs_VerifyTnodeWorker(yaffs_Object
* obj
, yaffs_Tnode
* tn
,
595 __u32 level
, int chunkOffset
)
598 yaffs_Device
*dev
= obj
->myDev
;
600 int nTnodeBytes
= (dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8;
605 for (i
= 0; i
< YAFFS_NTNODES_INTERNAL
&& ok
; i
++){
606 if (tn
->internal
[i
]) {
607 ok
= yaffs_VerifyTnodeWorker(obj
,
610 (chunkOffset
<<YAFFS_TNODES_INTERNAL_BITS
) + i
);
613 } else if (level
== 0) {
615 yaffs_ExtendedTags tags
;
616 __u32 objectId
= obj
->objectId
;
618 chunkOffset
<<= YAFFS_TNODES_LEVEL0_BITS
;
620 for(i
= 0; i
< YAFFS_NTNODES_LEVEL0
; i
++){
621 __u32 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,i
);
624 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
625 yaffs_ReadChunkWithTagsFromNAND(dev
,theChunk
,NULL
, &tags
);
626 if(tags
.objectId
!= objectId
|| tags
.chunkId
!= chunkOffset
){
627 T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR
),
628 objectId
, chunkOffset
, theChunk
,
629 tags
.objectId
, tags
.chunkId
));
642 static void yaffs_VerifyFile(yaffs_Object
*obj
)
644 int requiredTallness
;
651 yaffs_ExtendedTags tags
;
655 if(obj
&& yaffs_SkipVerification(obj
->myDev
))
659 objectId
= obj
->objectId
;
661 /* Check file size is consistent with tnode depth */
662 lastChunk
= obj
->variant
.fileVariant
.fileSize
/ dev
->nDataBytesPerChunk
+ 1;
663 x
= lastChunk
>> YAFFS_TNODES_LEVEL0_BITS
;
664 requiredTallness
= 0;
666 x
>>= YAFFS_TNODES_INTERNAL_BITS
;
670 actualTallness
= obj
->variant
.fileVariant
.topLevel
;
672 if(requiredTallness
> actualTallness
)
673 T(YAFFS_TRACE_VERIFY
,
674 (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR
),
675 obj
->objectId
,actualTallness
, requiredTallness
));
678 /* Check that the chunks in the tnode tree are all correct.
679 * We do this by scanning through the tnode tree and
680 * checking the tags for every chunk match.
683 if(yaffs_SkipNANDVerification(dev
))
686 for(i
= 1; i
<= lastChunk
; i
++){
687 tn
= yaffs_FindLevel0Tnode(dev
, &obj
->variant
.fileVariant
,i
);
690 __u32 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,i
);
692 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
693 yaffs_ReadChunkWithTagsFromNAND(dev
,theChunk
,NULL
, &tags
);
694 if(tags
.objectId
!= objectId
|| tags
.chunkId
!= i
){
695 T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR
),
696 objectId
, i
, theChunk
,
697 tags
.objectId
, tags
.chunkId
));
706 static void yaffs_VerifyDirectory(yaffs_Object
*obj
)
708 if(obj
&& yaffs_SkipVerification(obj
->myDev
))
713 static void yaffs_VerifyHardLink(yaffs_Object
*obj
)
715 if(obj
&& yaffs_SkipVerification(obj
->myDev
))
718 /* Verify sane equivalent object */
721 static void yaffs_VerifySymlink(yaffs_Object
*obj
)
723 if(obj
&& yaffs_SkipVerification(obj
->myDev
))
726 /* Verify symlink string */
729 static void yaffs_VerifySpecial(yaffs_Object
*obj
)
731 if(obj
&& yaffs_SkipVerification(obj
->myDev
))
735 static void yaffs_VerifyObject(yaffs_Object
*obj
)
750 if(yaffs_SkipVerification(dev
))
753 /* Check sane object header chunk */
755 chunkMin
= dev
->internalStartBlock
* dev
->nChunksPerBlock
;
756 chunkMax
= (dev
->internalEndBlock
+1) * dev
->nChunksPerBlock
- 1;
758 chunkIdOk
= (obj
->chunkId
>= chunkMin
&& obj
->chunkId
<= chunkMax
);
759 chunkIsLive
= chunkIdOk
&&
760 yaffs_CheckChunkBit(dev
,
761 obj
->chunkId
/ dev
->nChunksPerBlock
,
762 obj
->chunkId
% dev
->nChunksPerBlock
);
764 (!chunkIdOk
|| !chunkIsLive
)) {
765 T(YAFFS_TRACE_VERIFY
,
766 (TSTR("Obj %d has chunkId %d %s %s"TENDSTR
),
767 obj
->objectId
,obj
->chunkId
,
768 chunkIdOk
? "" : ",out of range",
769 chunkIsLive
|| !chunkIdOk
? "" : ",marked as deleted"));
772 if(chunkIdOk
&& chunkIsLive
&&!yaffs_SkipNANDVerification(dev
)) {
773 yaffs_ExtendedTags tags
;
774 yaffs_ObjectHeader
*oh
;
775 __u8
*buffer
= yaffs_GetTempBuffer(dev
,__LINE__
);
777 oh
= (yaffs_ObjectHeader
*)buffer
;
779 yaffs_ReadChunkWithTagsFromNAND(dev
, obj
->chunkId
,buffer
, &tags
);
781 yaffs_VerifyObjectHeader(obj
,oh
,&tags
,1);
783 yaffs_ReleaseTempBuffer(dev
,buffer
,__LINE__
);
786 /* Verify it has a parent */
787 if(obj
&& !obj
->fake
&&
788 (!obj
->parent
|| obj
->parent
->myDev
!= dev
)){
789 T(YAFFS_TRACE_VERIFY
,
790 (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR
),
791 obj
->objectId
,obj
->parent
));
794 /* Verify parent is a directory */
795 if(obj
->parent
&& obj
->parent
->variantType
!= YAFFS_OBJECT_TYPE_DIRECTORY
){
796 T(YAFFS_TRACE_VERIFY
,
797 (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR
),
798 obj
->objectId
,obj
->parent
->variantType
));
801 switch(obj
->variantType
){
802 case YAFFS_OBJECT_TYPE_FILE
:
803 yaffs_VerifyFile(obj
);
805 case YAFFS_OBJECT_TYPE_SYMLINK
:
806 yaffs_VerifySymlink(obj
);
808 case YAFFS_OBJECT_TYPE_DIRECTORY
:
809 yaffs_VerifyDirectory(obj
);
811 case YAFFS_OBJECT_TYPE_HARDLINK
:
812 yaffs_VerifyHardLink(obj
);
814 case YAFFS_OBJECT_TYPE_SPECIAL
:
815 yaffs_VerifySpecial(obj
);
817 case YAFFS_OBJECT_TYPE_UNKNOWN
:
819 T(YAFFS_TRACE_VERIFY
,
820 (TSTR("Obj %d has illegaltype %d"TENDSTR
),
821 obj
->objectId
,obj
->variantType
));
828 static void yaffs_VerifyObjects(yaffs_Device
*dev
)
832 struct list_head
*lh
;
834 if(yaffs_SkipVerification(dev
))
837 /* Iterate through the objects in each hash entry */
839 for(i
= 0; i
< YAFFS_NOBJECT_BUCKETS
; i
++){
840 list_for_each(lh
, &dev
->objectBucket
[i
].list
) {
842 obj
= list_entry(lh
, yaffs_Object
, hashLink
);
843 yaffs_VerifyObject(obj
);
852 * Simple hash function. Needs to have a reasonable spread
855 static Y_INLINE
int yaffs_HashFunction(int n
)
858 return (n
% YAFFS_NOBJECT_BUCKETS
);
862 * Access functions to useful fake objects
865 yaffs_Object
*yaffs_Root(yaffs_Device
* dev
)
870 yaffs_Object
*yaffs_LostNFound(yaffs_Device
* dev
)
872 return dev
->lostNFoundDir
;
877 * Erased NAND checking functions
880 int yaffs_CheckFF(__u8
* buffer
, int nBytes
)
882 /* Horrible, slow implementation */
891 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct
*dev
,
895 int retval
= YAFFS_OK
;
896 __u8
*data
= yaffs_GetTempBuffer(dev
, __LINE__
);
897 yaffs_ExtendedTags tags
;
900 result
= yaffs_ReadChunkWithTagsFromNAND(dev
, chunkInNAND
, data
, &tags
);
902 if(tags
.eccResult
> YAFFS_ECC_RESULT_NO_ERROR
)
906 if (!yaffs_CheckFF(data
, dev
->nDataBytesPerChunk
) || tags
.chunkUsed
) {
907 T(YAFFS_TRACE_NANDACCESS
,
908 (TSTR("Chunk %d not erased" TENDSTR
), chunkInNAND
));
912 yaffs_ReleaseTempBuffer(dev
, data
, __LINE__
);
919 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct
*dev
,
921 yaffs_ExtendedTags
* tags
,
928 yaffs_InvalidateCheckpoint(dev
);
931 yaffs_BlockInfo
*bi
= 0;
934 chunk
= yaffs_AllocateChunk(dev
, useReserve
, &bi
);
940 /* First check this chunk is erased, if it needs
941 * checking. The checking policy (unless forced
942 * always on) is as follows:
944 * Check the first page we try to write in a block.
945 * If the check passes then we don't need to check any
946 * more. If the check fails, we check again...
947 * If the block has been erased, we don't need to check.
949 * However, if the block has been prioritised for gc,
950 * then we think there might be something odd about
951 * this block and stop using it.
953 * Rationale: We should only ever see chunks that have
954 * not been erased if there was a partially written
955 * chunk due to power loss. This checking policy should
956 * catch that case with very few checks and thus save a
957 * lot of checks that are most likely not needed.
959 if (bi
->gcPrioritise
) {
960 yaffs_DeleteChunk(dev
, chunk
, 1, __LINE__
);
961 /* try another chunk */
965 /* let's give it a try */
968 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
969 bi
->skipErasedCheck
= 0;
971 if (!bi
->skipErasedCheck
) {
972 erasedOk
= yaffs_CheckChunkErased(dev
, chunk
);
973 if (erasedOk
!= YAFFS_OK
) {
975 (TSTR ("**>> yaffs chunk %d was not erased"
978 /* try another chunk */
981 bi
->skipErasedCheck
= 1;
984 writeOk
= yaffs_WriteChunkWithTagsToNAND(dev
, chunk
,
986 if (writeOk
!= YAFFS_OK
) {
987 yaffs_HandleWriteChunkError(dev
, chunk
, erasedOk
);
988 /* try another chunk */
992 /* Copy the data into the robustification buffer */
993 yaffs_HandleWriteChunkOk(dev
, chunk
, data
, tags
);
995 } while (writeOk
!= YAFFS_OK
&& attempts
< yaffs_wr_attempts
);
999 (TSTR("**>> yaffs write required %d attempts" TENDSTR
),
1002 dev
->nRetriedWrites
+= (attempts
- 1);
1009 * Block retiring for handling a broken block.
1012 static void yaffs_RetireBlock(yaffs_Device
* dev
, int blockInNAND
)
1014 yaffs_BlockInfo
*bi
= yaffs_GetBlockInfo(dev
, blockInNAND
);
1016 yaffs_InvalidateCheckpoint(dev
);
1018 yaffs_MarkBlockBad(dev
, blockInNAND
);
1020 bi
->blockState
= YAFFS_BLOCK_STATE_DEAD
;
1021 bi
->gcPrioritise
= 0;
1022 bi
->needsRetiring
= 0;
1024 dev
->nRetiredBlocks
++;
1028 * Functions for robustisizing TODO
1032 static void yaffs_HandleWriteChunkOk(yaffs_Device
* dev
, int chunkInNAND
,
1034 const yaffs_ExtendedTags
* tags
)
1038 static void yaffs_HandleUpdateChunk(yaffs_Device
* dev
, int chunkInNAND
,
1039 const yaffs_ExtendedTags
* tags
)
1043 void yaffs_HandleChunkError(yaffs_Device
*dev
, yaffs_BlockInfo
*bi
)
1045 if(!bi
->gcPrioritise
){
1046 bi
->gcPrioritise
= 1;
1047 dev
->hasPendingPrioritisedGCs
= 1;
1048 bi
->chunkErrorStrikes
++;
1050 if(bi
->chunkErrorStrikes
> 3){
1051 bi
->needsRetiring
= 1; /* Too many stikes, so retire this */
1052 T(YAFFS_TRACE_ALWAYS
, (TSTR("yaffs: Block struck out" TENDSTR
)));
1059 static void yaffs_HandleWriteChunkError(yaffs_Device
* dev
, int chunkInNAND
, int erasedOk
)
1062 int blockInNAND
= chunkInNAND
/ dev
->nChunksPerBlock
;
1063 yaffs_BlockInfo
*bi
= yaffs_GetBlockInfo(dev
, blockInNAND
);
1065 yaffs_HandleChunkError(dev
,bi
);
1069 /* Was an actual write failure, so mark the block for retirement */
1070 bi
->needsRetiring
= 1;
1071 T(YAFFS_TRACE_ERROR
| YAFFS_TRACE_BAD_BLOCKS
,
1072 (TSTR("**>> Block %d needs retiring" TENDSTR
), blockInNAND
));
1077 /* Delete the chunk */
1078 yaffs_DeleteChunk(dev
, chunkInNAND
, 1, __LINE__
);
1082 /*---------------- Name handling functions ------------*/
1084 static __u16
yaffs_CalcNameSum(const YCHAR
* name
)
1089 YUCHAR
*bname
= (YUCHAR
*) name
;
1091 while ((*bname
) && (i
< (YAFFS_MAX_NAME_LENGTH
/2))) {
1093 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1094 sum
+= yaffs_toupper(*bname
) * i
;
1096 sum
+= (*bname
) * i
;
1105 static void yaffs_SetObjectName(yaffs_Object
* obj
, const YCHAR
* name
)
1107 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
1108 if (name
&& yaffs_strlen(name
) <= YAFFS_SHORT_NAME_LENGTH
) {
1109 yaffs_strcpy(obj
->shortName
, name
);
1111 obj
->shortName
[0] = _Y('\0');
1114 obj
->sum
= yaffs_CalcNameSum(name
);
1117 /*-------------------- TNODES -------------------
1119 * List of spare tnodes
1120 * The list is hooked together using the first pointer
1124 /* yaffs_CreateTnodes creates a bunch more tnodes and
1125 * adds them to the tnode free list.
1126 * Don't use this function directly
1129 static int yaffs_CreateTnodes(yaffs_Device
* dev
, int nTnodes
)
1133 yaffs_Tnode
*newTnodes
;
1137 yaffs_TnodeList
*tnl
;
1142 /* Calculate the tnode size in bytes for variable width tnode support.
1143 * Must be a multiple of 32-bits */
1144 tnodeSize
= (dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8;
1146 /* make these things */
1148 newTnodes
= YMALLOC(nTnodes
* tnodeSize
);
1149 mem
= (__u8
*)newTnodes
;
1152 T(YAFFS_TRACE_ERROR
,
1153 (TSTR("yaffs: Could not allocate Tnodes" TENDSTR
)));
1157 /* Hook them into the free list */
1159 for (i
= 0; i
< nTnodes
- 1; i
++) {
1160 newTnodes
[i
].internal
[0] = &newTnodes
[i
+ 1];
1161 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1162 newTnodes
[i
].internal
[YAFFS_NTNODES_INTERNAL
] = (void *)1;
1166 newTnodes
[nTnodes
- 1].internal
[0] = dev
->freeTnodes
;
1167 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1168 newTnodes
[nTnodes
- 1].internal
[YAFFS_NTNODES_INTERNAL
] = (void *)1;
1170 dev
->freeTnodes
= newTnodes
;
1172 /* New hookup for wide tnodes */
1173 for(i
= 0; i
< nTnodes
-1; i
++) {
1174 curr
= (yaffs_Tnode
*) &mem
[i
* tnodeSize
];
1175 next
= (yaffs_Tnode
*) &mem
[(i
+1) * tnodeSize
];
1176 curr
->internal
[0] = next
;
1179 curr
= (yaffs_Tnode
*) &mem
[(nTnodes
- 1) * tnodeSize
];
1180 curr
->internal
[0] = dev
->freeTnodes
;
1181 dev
->freeTnodes
= (yaffs_Tnode
*)mem
;
1186 dev
->nFreeTnodes
+= nTnodes
;
1187 dev
->nTnodesCreated
+= nTnodes
;
1189 /* Now add this bunch of tnodes to a list for freeing up.
1190 * NB If we can't add this to the management list it isn't fatal
1191 * but it just means we can't free this bunch of tnodes later.
1194 tnl
= YMALLOC(sizeof(yaffs_TnodeList
));
1196 T(YAFFS_TRACE_ERROR
,
1198 ("yaffs: Could not add tnodes to management list" TENDSTR
)));
1202 tnl
->tnodes
= newTnodes
;
1203 tnl
->next
= dev
->allocatedTnodeList
;
1204 dev
->allocatedTnodeList
= tnl
;
1207 T(YAFFS_TRACE_ALLOCATE
, (TSTR("yaffs: Tnodes added" TENDSTR
)));
1212 /* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
1214 static yaffs_Tnode
*yaffs_GetTnodeRaw(yaffs_Device
* dev
)
1216 yaffs_Tnode
*tn
= NULL
;
1218 /* If there are none left make more */
1219 if (!dev
->freeTnodes
) {
1220 yaffs_CreateTnodes(dev
, YAFFS_ALLOCATION_NTNODES
);
1223 if (dev
->freeTnodes
) {
1224 tn
= dev
->freeTnodes
;
1225 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1226 if (tn
->internal
[YAFFS_NTNODES_INTERNAL
] != (void *)1) {
1227 /* Hoosterman, this thing looks like it isn't in the list */
1228 T(YAFFS_TRACE_ALWAYS
,
1229 (TSTR("yaffs: Tnode list bug 1" TENDSTR
)));
1232 dev
->freeTnodes
= dev
->freeTnodes
->internal
[0];
1239 static yaffs_Tnode
*yaffs_GetTnode(yaffs_Device
* dev
)
1241 yaffs_Tnode
*tn
= yaffs_GetTnodeRaw(dev
);
1244 memset(tn
, 0, (dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8);
1249 /* FreeTnode frees up a tnode and puts it back on the free list */
1250 static void yaffs_FreeTnode(yaffs_Device
* dev
, yaffs_Tnode
* tn
)
1253 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1254 if (tn
->internal
[YAFFS_NTNODES_INTERNAL
] != 0) {
1255 /* Hoosterman, this thing looks like it is already in the list */
1256 T(YAFFS_TRACE_ALWAYS
,
1257 (TSTR("yaffs: Tnode list bug 2" TENDSTR
)));
1259 tn
->internal
[YAFFS_NTNODES_INTERNAL
] = (void *)1;
1261 tn
->internal
[0] = dev
->freeTnodes
;
1262 dev
->freeTnodes
= tn
;
1267 static void yaffs_DeinitialiseTnodes(yaffs_Device
* dev
)
1269 /* Free the list of allocated tnodes */
1270 yaffs_TnodeList
*tmp
;
1272 while (dev
->allocatedTnodeList
) {
1273 tmp
= dev
->allocatedTnodeList
->next
;
1275 YFREE(dev
->allocatedTnodeList
->tnodes
);
1276 YFREE(dev
->allocatedTnodeList
);
1277 dev
->allocatedTnodeList
= tmp
;
1281 dev
->freeTnodes
= NULL
;
1282 dev
->nFreeTnodes
= 0;
1285 static void yaffs_InitialiseTnodes(yaffs_Device
* dev
)
1287 dev
->allocatedTnodeList
= NULL
;
1288 dev
->freeTnodes
= NULL
;
1289 dev
->nFreeTnodes
= 0;
1290 dev
->nTnodesCreated
= 0;
1295 void yaffs_PutLevel0Tnode(yaffs_Device
*dev
, yaffs_Tnode
*tn
, unsigned pos
, unsigned val
)
1297 __u32
*map
= (__u32
*)tn
;
1303 pos
&= YAFFS_TNODES_LEVEL0_MASK
;
1304 val
>>= dev
->chunkGroupBits
;
1306 bitInMap
= pos
* dev
->tnodeWidth
;
1307 wordInMap
= bitInMap
/32;
1308 bitInWord
= bitInMap
& (32 -1);
1310 mask
= dev
->tnodeMask
<< bitInWord
;
1312 map
[wordInMap
] &= ~mask
;
1313 map
[wordInMap
] |= (mask
& (val
<< bitInWord
));
1315 if(dev
->tnodeWidth
> (32-bitInWord
)) {
1316 bitInWord
= (32 - bitInWord
);
1318 mask
= dev
->tnodeMask
>> (/*dev->tnodeWidth -*/ bitInWord
);
1319 map
[wordInMap
] &= ~mask
;
1320 map
[wordInMap
] |= (mask
& (val
>> bitInWord
));
1324 static __u32
yaffs_GetChunkGroupBase(yaffs_Device
*dev
, yaffs_Tnode
*tn
, unsigned pos
)
1326 __u32
*map
= (__u32
*)tn
;
1332 pos
&= YAFFS_TNODES_LEVEL0_MASK
;
1334 bitInMap
= pos
* dev
->tnodeWidth
;
1335 wordInMap
= bitInMap
/32;
1336 bitInWord
= bitInMap
& (32 -1);
1338 val
= map
[wordInMap
] >> bitInWord
;
1340 if(dev
->tnodeWidth
> (32-bitInWord
)) {
1341 bitInWord
= (32 - bitInWord
);
1343 val
|= (map
[wordInMap
] << bitInWord
);
1346 val
&= dev
->tnodeMask
;
1347 val
<<= dev
->chunkGroupBits
;
1352 /* ------------------- End of individual tnode manipulation -----------------*/
1354 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
1355 * The look up tree is represented by the top tnode and the number of topLevel
1356 * in the tree. 0 means only the level 0 tnode is in the tree.
1359 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
1360 static yaffs_Tnode
*yaffs_FindLevel0Tnode(yaffs_Device
* dev
,
1361 yaffs_FileStructure
* fStruct
,
1365 yaffs_Tnode
*tn
= fStruct
->top
;
1367 int requiredTallness
;
1368 int level
= fStruct
->topLevel
;
1370 /* Check sane level and chunk Id */
1371 if (level
< 0 || level
> YAFFS_TNODES_MAX_LEVEL
) {
1375 if (chunkId
> YAFFS_MAX_CHUNK_ID
) {
1379 /* First check we're tall enough (ie enough topLevel) */
1381 i
= chunkId
>> YAFFS_TNODES_LEVEL0_BITS
;
1382 requiredTallness
= 0;
1384 i
>>= YAFFS_TNODES_INTERNAL_BITS
;
1388 if (requiredTallness
> fStruct
->topLevel
) {
1389 /* Not tall enough, so we can't find it, return NULL. */
1393 /* Traverse down to level 0 */
1394 while (level
> 0 && tn
) {
1396 internal
[(chunkId
>>
1397 ( YAFFS_TNODES_LEVEL0_BITS
+
1399 YAFFS_TNODES_INTERNAL_BITS
)
1401 YAFFS_TNODES_INTERNAL_MASK
];
1409 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
1410 * This happens in two steps:
1411 * 1. If the tree isn't tall enough, then make it taller.
1412 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
1414 * Used when modifying the tree.
1416 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
1417 * be plugged into the ttree.
1420 static yaffs_Tnode
*yaffs_AddOrFindLevel0Tnode(yaffs_Device
* dev
,
1421 yaffs_FileStructure
* fStruct
,
1423 yaffs_Tnode
*passedTn
)
1426 int requiredTallness
;
1434 /* Check sane level and page Id */
1435 if (fStruct
->topLevel
< 0 || fStruct
->topLevel
> YAFFS_TNODES_MAX_LEVEL
) {
1439 if (chunkId
> YAFFS_MAX_CHUNK_ID
) {
1443 /* First check we're tall enough (ie enough topLevel) */
1445 x
= chunkId
>> YAFFS_TNODES_LEVEL0_BITS
;
1446 requiredTallness
= 0;
1448 x
>>= YAFFS_TNODES_INTERNAL_BITS
;
1453 if (requiredTallness
> fStruct
->topLevel
) {
1454 /* Not tall enough,gotta make the tree taller */
1455 for (i
= fStruct
->topLevel
; i
< requiredTallness
; i
++) {
1457 tn
= yaffs_GetTnode(dev
);
1460 tn
->internal
[0] = fStruct
->top
;
1463 T(YAFFS_TRACE_ERROR
,
1464 (TSTR("yaffs: no more tnodes" TENDSTR
)));
1468 fStruct
->topLevel
= requiredTallness
;
1471 /* Traverse down to level 0, adding anything we need */
1473 l
= fStruct
->topLevel
;
1477 while (l
> 0 && tn
) {
1479 ( YAFFS_TNODES_LEVEL0_BITS
+
1480 (l
- 1) * YAFFS_TNODES_INTERNAL_BITS
)) &
1481 YAFFS_TNODES_INTERNAL_MASK
;
1484 if((l
>1) && !tn
->internal
[x
]){
1485 /* Add missing non-level-zero tnode */
1486 tn
->internal
[x
] = yaffs_GetTnode(dev
);
1489 /* Looking from level 1 at level 0 */
1491 /* If we already have one, then release it.*/
1493 yaffs_FreeTnode(dev
,tn
->internal
[x
]);
1494 tn
->internal
[x
] = passedTn
;
1496 } else if(!tn
->internal
[x
]) {
1497 /* Don't have one, none passed in */
1498 tn
->internal
[x
] = yaffs_GetTnode(dev
);
1502 tn
= tn
->internal
[x
];
1506 /* top is level 0 */
1508 memcpy(tn
,passedTn
,(dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8);
1509 yaffs_FreeTnode(dev
,passedTn
);
1516 static int yaffs_FindChunkInGroup(yaffs_Device
* dev
, int theChunk
,
1517 yaffs_ExtendedTags
* tags
, int objectId
,
1522 for (j
= 0; theChunk
&& j
< dev
->chunkGroupSize
; j
++) {
1523 if (yaffs_CheckChunkBit
1524 (dev
, theChunk
/ dev
->nChunksPerBlock
,
1525 theChunk
% dev
->nChunksPerBlock
)) {
1526 yaffs_ReadChunkWithTagsFromNAND(dev
, theChunk
, NULL
,
1528 if (yaffs_TagsMatch(tags
, objectId
, chunkInInode
)) {
1540 /* DeleteWorker scans backwards through the tnode tree and deletes all the
1541 * chunks and tnodes in the file
1542 * Returns 1 if the tree was deleted.
1543 * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1546 static int yaffs_DeleteWorker(yaffs_Object
* in
, yaffs_Tnode
* tn
, __u32 level
,
1547 int chunkOffset
, int *limit
)
1552 yaffs_ExtendedTags tags
;
1554 yaffs_Device
*dev
= in
->myDev
;
1561 for (i
= YAFFS_NTNODES_INTERNAL
- 1; allDone
&& i
>= 0;
1563 if (tn
->internal
[i
]) {
1564 if (limit
&& (*limit
) < 0) {
1568 yaffs_DeleteWorker(in
,
1576 YAFFS_TNODES_INTERNAL_BITS
)
1581 yaffs_FreeTnode(dev
,
1584 tn
->internal
[i
] = NULL
;
1589 return (allDone
) ? 1 : 0;
1590 } else if (level
== 0) {
1593 for (i
= YAFFS_NTNODES_LEVEL0
- 1; i
>= 0 && !hitLimit
;
1595 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,i
);
1600 YAFFS_TNODES_LEVEL0_BITS
) + i
;
1603 yaffs_FindChunkInGroup(dev
,
1609 if (foundChunk
> 0) {
1610 yaffs_DeleteChunk(dev
,
1615 *limit
= *limit
- 1;
1623 yaffs_PutLevel0Tnode(dev
,tn
,i
,0);
1627 return (i
< 0) ? 1 : 0;
1637 static void yaffs_SoftDeleteChunk(yaffs_Device
* dev
, int chunk
)
1640 yaffs_BlockInfo
*theBlock
;
1642 T(YAFFS_TRACE_DELETION
, (TSTR("soft delete chunk %d" TENDSTR
), chunk
));
1644 theBlock
= yaffs_GetBlockInfo(dev
, chunk
/ dev
->nChunksPerBlock
);
1646 theBlock
->softDeletions
++;
1651 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1652 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1654 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1657 static int yaffs_SoftDeleteWorker(yaffs_Object
* in
, yaffs_Tnode
* tn
,
1658 __u32 level
, int chunkOffset
)
1663 yaffs_Device
*dev
= in
->myDev
;
1668 for (i
= YAFFS_NTNODES_INTERNAL
- 1; allDone
&& i
>= 0;
1670 if (tn
->internal
[i
]) {
1672 yaffs_SoftDeleteWorker(in
,
1678 YAFFS_TNODES_INTERNAL_BITS
)
1681 yaffs_FreeTnode(dev
,
1684 tn
->internal
[i
] = NULL
;
1686 /* Hoosterman... how could this happen? */
1690 return (allDone
) ? 1 : 0;
1691 } else if (level
== 0) {
1693 for (i
= YAFFS_NTNODES_LEVEL0
- 1; i
>= 0; i
--) {
1694 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,i
);
1696 /* Note this does not find the real chunk, only the chunk group.
1697 * We make an assumption that a chunk group is not larger than
1700 yaffs_SoftDeleteChunk(dev
, theChunk
);
1701 yaffs_PutLevel0Tnode(dev
,tn
,i
,0);
1715 static void yaffs_SoftDeleteFile(yaffs_Object
* obj
)
1718 obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
&& !obj
->softDeleted
) {
1719 if (obj
->nDataChunks
<= 0) {
1720 /* Empty file with no duplicate object headers, just delete it immediately */
1721 yaffs_FreeTnode(obj
->myDev
,
1722 obj
->variant
.fileVariant
.top
);
1723 obj
->variant
.fileVariant
.top
= NULL
;
1724 T(YAFFS_TRACE_TRACING
,
1725 (TSTR("yaffs: Deleting empty file %d" TENDSTR
),
1727 yaffs_DoGenericObjectDeletion(obj
);
1729 yaffs_SoftDeleteWorker(obj
,
1730 obj
->variant
.fileVariant
.top
,
1731 obj
->variant
.fileVariant
.
1733 obj
->softDeleted
= 1;
1738 /* Pruning removes any part of the file structure tree that is beyond the
1739 * bounds of the file (ie that does not point to chunks).
1741 * A file should only get pruned when its size is reduced.
1743 * Before pruning, the chunks must be pulled from the tree and the
1744 * level 0 tnode entries must be zeroed out.
1745 * Could also use this for file deletion, but that's probably better handled
1746 * by a special case.
1749 static yaffs_Tnode
*yaffs_PruneWorker(yaffs_Device
* dev
, yaffs_Tnode
* tn
,
1750 __u32 level
, int del0
)
1758 for (i
= 0; i
< YAFFS_NTNODES_INTERNAL
; i
++) {
1759 if (tn
->internal
[i
] && level
> 0) {
1761 yaffs_PruneWorker(dev
, tn
->internal
[i
],
1763 (i
== 0) ? del0
: 1);
1766 if (tn
->internal
[i
]) {
1771 if (hasData
== 0 && del0
) {
1772 /* Free and return NULL */
1774 yaffs_FreeTnode(dev
, tn
);
1784 static int yaffs_PruneFileStructure(yaffs_Device
* dev
,
1785 yaffs_FileStructure
* fStruct
)
1792 if (fStruct
->topLevel
> 0) {
1794 yaffs_PruneWorker(dev
, fStruct
->top
, fStruct
->topLevel
, 0);
1796 /* Now we have a tree with all the non-zero branches NULL but the height
1797 * is the same as it was.
1798 * Let's see if we can trim internal tnodes to shorten the tree.
1799 * We can do this if only the 0th element in the tnode is in use
1800 * (ie all the non-zero are NULL)
1803 while (fStruct
->topLevel
&& !done
) {
1807 for (i
= 1; i
< YAFFS_NTNODES_INTERNAL
; i
++) {
1808 if (tn
->internal
[i
]) {
1814 fStruct
->top
= tn
->internal
[0];
1815 fStruct
->topLevel
--;
1816 yaffs_FreeTnode(dev
, tn
);
1826 /*-------------------- End of File Structure functions.-------------------*/
1828 /* yaffs_CreateFreeObjects creates a bunch more objects and
1829 * adds them to the object free list.
1831 static int yaffs_CreateFreeObjects(yaffs_Device
* dev
, int nObjects
)
1834 yaffs_Object
*newObjects
;
1835 yaffs_ObjectList
*list
;
1840 /* make these things */
1841 newObjects
= YMALLOC(nObjects
* sizeof(yaffs_Object
));
1842 list
= YMALLOC(sizeof(yaffs_ObjectList
));
1844 if (!newObjects
|| !list
) {
1849 T(YAFFS_TRACE_ALLOCATE
,
1850 (TSTR("yaffs: Could not allocate more objects" TENDSTR
)));
1854 /* Hook them into the free list */
1855 for (i
= 0; i
< nObjects
- 1; i
++) {
1856 newObjects
[i
].siblings
.next
=
1857 (struct list_head
*)(&newObjects
[i
+ 1]);
1860 newObjects
[nObjects
- 1].siblings
.next
= (void *)dev
->freeObjects
;
1861 dev
->freeObjects
= newObjects
;
1862 dev
->nFreeObjects
+= nObjects
;
1863 dev
->nObjectsCreated
+= nObjects
;
1865 /* Now add this bunch of Objects to a list for freeing up. */
1867 list
->objects
= newObjects
;
1868 list
->next
= dev
->allocatedObjectList
;
1869 dev
->allocatedObjectList
= list
;
1875 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1876 static yaffs_Object
*yaffs_AllocateEmptyObject(yaffs_Device
* dev
)
1878 yaffs_Object
*tn
= NULL
;
1880 /* If there are none left make more */
1881 if (!dev
->freeObjects
) {
1882 yaffs_CreateFreeObjects(dev
, YAFFS_ALLOCATION_NOBJECTS
);
1885 if (dev
->freeObjects
) {
1886 tn
= dev
->freeObjects
;
1888 (yaffs_Object
*) (dev
->freeObjects
->siblings
.next
);
1889 dev
->nFreeObjects
--;
1891 /* Now sweeten it up... */
1893 memset(tn
, 0, sizeof(yaffs_Object
));
1896 tn
->variantType
= YAFFS_OBJECT_TYPE_UNKNOWN
;
1897 INIT_LIST_HEAD(&(tn
->hardLinks
));
1898 INIT_LIST_HEAD(&(tn
->hashLink
));
1899 INIT_LIST_HEAD(&tn
->siblings
);
1901 /* Add it to the lost and found directory.
1902 * NB Can't put root or lostNFound in lostNFound so
1903 * check if lostNFound exists first
1905 if (dev
->lostNFoundDir
) {
1906 yaffs_AddObjectToDirectory(dev
->lostNFoundDir
, tn
);
1913 static yaffs_Object
*yaffs_CreateFakeDirectory(yaffs_Device
* dev
, int number
,
1918 yaffs_CreateNewObject(dev
, number
, YAFFS_OBJECT_TYPE_DIRECTORY
);
1920 obj
->fake
= 1; /* it is fake so it has no NAND presence... */
1921 obj
->renameAllowed
= 0; /* ... and we're not allowed to rename it... */
1922 obj
->unlinkAllowed
= 0; /* ... or unlink it */
1925 obj
->yst_mode
= mode
;
1927 obj
->chunkId
= 0; /* Not a valid chunk. */
1934 static void yaffs_UnhashObject(yaffs_Object
* tn
)
1937 yaffs_Device
*dev
= tn
->myDev
;
1939 /* If it is still linked into the bucket list, free from the list */
1940 if (!list_empty(&tn
->hashLink
)) {
1941 list_del_init(&tn
->hashLink
);
1942 bucket
= yaffs_HashFunction(tn
->objectId
);
1943 dev
->objectBucket
[bucket
].count
--;
1948 /* FreeObject frees up a Object and puts it back on the free list */
1949 static void yaffs_FreeObject(yaffs_Object
* tn
)
1952 yaffs_Device
*dev
= tn
->myDev
;
1956 /* We're still hooked up to a cached inode.
1957 * Don't delete now, but mark for later deletion
1959 tn
->deferedFree
= 1;
1964 yaffs_UnhashObject(tn
);
1966 /* Link into the free list. */
1967 tn
->siblings
.next
= (struct list_head
*)(dev
->freeObjects
);
1968 dev
->freeObjects
= tn
;
1969 dev
->nFreeObjects
++;
1974 void yaffs_HandleDeferedFree(yaffs_Object
* obj
)
1976 if (obj
->deferedFree
) {
1977 yaffs_FreeObject(obj
);
1983 static void yaffs_DeinitialiseObjects(yaffs_Device
* dev
)
1985 /* Free the list of allocated Objects */
1987 yaffs_ObjectList
*tmp
;
1989 while (dev
->allocatedObjectList
) {
1990 tmp
= dev
->allocatedObjectList
->next
;
1991 YFREE(dev
->allocatedObjectList
->objects
);
1992 YFREE(dev
->allocatedObjectList
);
1994 dev
->allocatedObjectList
= tmp
;
1997 dev
->freeObjects
= NULL
;
1998 dev
->nFreeObjects
= 0;
2001 static void yaffs_InitialiseObjects(yaffs_Device
* dev
)
2005 dev
->allocatedObjectList
= NULL
;
2006 dev
->freeObjects
= NULL
;
2007 dev
->nFreeObjects
= 0;
2009 for (i
= 0; i
< YAFFS_NOBJECT_BUCKETS
; i
++) {
2010 INIT_LIST_HEAD(&dev
->objectBucket
[i
].list
);
2011 dev
->objectBucket
[i
].count
= 0;
2016 static int yaffs_FindNiceObjectBucket(yaffs_Device
* dev
)
2021 int lowest
= 999999;
2023 /* First let's see if we can find one that's empty. */
2025 for (i
= 0; i
< 10 && lowest
> 0; i
++) {
2027 x
%= YAFFS_NOBJECT_BUCKETS
;
2028 if (dev
->objectBucket
[x
].count
< lowest
) {
2029 lowest
= dev
->objectBucket
[x
].count
;
2035 /* If we didn't find an empty list, then try
2036 * looking a bit further for a short one
2039 for (i
= 0; i
< 10 && lowest
> 3; i
++) {
2041 x
%= YAFFS_NOBJECT_BUCKETS
;
2042 if (dev
->objectBucket
[x
].count
< lowest
) {
2043 lowest
= dev
->objectBucket
[x
].count
;
2052 static int yaffs_CreateNewObjectNumber(yaffs_Device
* dev
)
2054 int bucket
= yaffs_FindNiceObjectBucket(dev
);
2056 /* Now find an object value that has not already been taken
2057 * by scanning the list.
2061 struct list_head
*i
;
2063 __u32 n
= (__u32
) bucket
;
2065 /* yaffs_CheckObjectHashSanity(); */
2069 n
+= YAFFS_NOBJECT_BUCKETS
;
2070 if (1 || dev
->objectBucket
[bucket
].count
> 0) {
2071 list_for_each(i
, &dev
->objectBucket
[bucket
].list
) {
2072 /* If there is already one in the list */
2074 && list_entry(i
, yaffs_Object
,
2075 hashLink
)->objectId
== n
) {
2086 static void yaffs_HashObject(yaffs_Object
* in
)
2088 int bucket
= yaffs_HashFunction(in
->objectId
);
2089 yaffs_Device
*dev
= in
->myDev
;
2091 list_add(&in
->hashLink
, &dev
->objectBucket
[bucket
].list
);
2092 dev
->objectBucket
[bucket
].count
++;
2096 yaffs_Object
*yaffs_FindObjectByNumber(yaffs_Device
* dev
, __u32 number
)
2098 int bucket
= yaffs_HashFunction(number
);
2099 struct list_head
*i
;
2102 list_for_each(i
, &dev
->objectBucket
[bucket
].list
) {
2103 /* Look if it is in the list */
2105 in
= list_entry(i
, yaffs_Object
, hashLink
);
2106 if (in
->objectId
== number
) {
2108 /* Don't tell the VFS about this one if it is defered free */
2109 if (in
->deferedFree
)
2121 yaffs_Object
*yaffs_CreateNewObject(yaffs_Device
* dev
, int number
,
2122 yaffs_ObjectType type
)
2125 yaffs_Object
*theObject
;
2129 number
= yaffs_CreateNewObjectNumber(dev
);
2132 theObject
= yaffs_AllocateEmptyObject(dev
);
2136 if(type
== YAFFS_OBJECT_TYPE_FILE
){
2137 tn
= yaffs_GetTnode(dev
);
2139 yaffs_FreeObject(theObject
);
2147 theObject
->fake
= 0;
2148 theObject
->renameAllowed
= 1;
2149 theObject
->unlinkAllowed
= 1;
2150 theObject
->objectId
= number
;
2151 yaffs_HashObject(theObject
);
2152 theObject
->variantType
= type
;
2153 #ifdef CONFIG_YAFFS_WINCE
2154 yfsd_WinFileTimeNow(theObject
->win_atime
);
2155 theObject
->win_ctime
[0] = theObject
->win_mtime
[0] =
2156 theObject
->win_atime
[0];
2157 theObject
->win_ctime
[1] = theObject
->win_mtime
[1] =
2158 theObject
->win_atime
[1];
2162 theObject
->yst_atime
= theObject
->yst_mtime
=
2163 theObject
->yst_ctime
= Y_CURRENT_TIME
;
2166 case YAFFS_OBJECT_TYPE_FILE
:
2167 theObject
->variant
.fileVariant
.fileSize
= 0;
2168 theObject
->variant
.fileVariant
.scannedFileSize
= 0;
2169 theObject
->variant
.fileVariant
.shrinkSize
= 0xFFFFFFFF; /* max __u32 */
2170 theObject
->variant
.fileVariant
.topLevel
= 0;
2171 theObject
->variant
.fileVariant
.top
= tn
;
2173 case YAFFS_OBJECT_TYPE_DIRECTORY
:
2174 INIT_LIST_HEAD(&theObject
->variant
.directoryVariant
.
2177 case YAFFS_OBJECT_TYPE_SYMLINK
:
2178 case YAFFS_OBJECT_TYPE_HARDLINK
:
2179 case YAFFS_OBJECT_TYPE_SPECIAL
:
2180 /* No action required */
2182 case YAFFS_OBJECT_TYPE_UNKNOWN
:
2183 /* todo this should not happen */
2191 static yaffs_Object
*yaffs_FindOrCreateObjectByNumber(yaffs_Device
* dev
,
2193 yaffs_ObjectType type
)
2195 yaffs_Object
*theObject
= NULL
;
2198 theObject
= yaffs_FindObjectByNumber(dev
, number
);
2202 theObject
= yaffs_CreateNewObject(dev
, number
, type
);
2210 static YCHAR
*yaffs_CloneString(const YCHAR
* str
)
2212 YCHAR
*newStr
= NULL
;
2215 newStr
= YMALLOC((yaffs_strlen(str
) + 1) * sizeof(YCHAR
));
2217 yaffs_strcpy(newStr
, str
);
2225 * Mknod (create) a new object.
2226 * equivalentObject only has meaning for a hard link;
2227 * aliasString only has meaning for a sumlink.
2228 * rdev only has meaning for devices (a subset of special objects)
2231 static yaffs_Object
*yaffs_MknodObject(yaffs_ObjectType type
,
2232 yaffs_Object
* parent
,
2237 yaffs_Object
* equivalentObject
,
2238 const YCHAR
* aliasString
, __u32 rdev
)
2243 yaffs_Device
*dev
= parent
->myDev
;
2245 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
2246 if (yaffs_FindObjectByName(parent
, name
)) {
2250 in
= yaffs_CreateNewObject(dev
, -1, type
);
2252 if(type
== YAFFS_OBJECT_TYPE_SYMLINK
){
2253 str
= yaffs_CloneString(aliasString
);
2255 yaffs_FreeObject(in
);
2265 in
->variantType
= type
;
2267 in
->yst_mode
= mode
;
2269 #ifdef CONFIG_YAFFS_WINCE
2270 yfsd_WinFileTimeNow(in
->win_atime
);
2271 in
->win_ctime
[0] = in
->win_mtime
[0] = in
->win_atime
[0];
2272 in
->win_ctime
[1] = in
->win_mtime
[1] = in
->win_atime
[1];
2275 in
->yst_atime
= in
->yst_mtime
= in
->yst_ctime
= Y_CURRENT_TIME
;
2277 in
->yst_rdev
= rdev
;
2281 in
->nDataChunks
= 0;
2283 yaffs_SetObjectName(in
, name
);
2286 yaffs_AddObjectToDirectory(parent
, in
);
2288 in
->myDev
= parent
->myDev
;
2291 case YAFFS_OBJECT_TYPE_SYMLINK
:
2292 in
->variant
.symLinkVariant
.alias
= str
;
2294 case YAFFS_OBJECT_TYPE_HARDLINK
:
2295 in
->variant
.hardLinkVariant
.equivalentObject
=
2297 in
->variant
.hardLinkVariant
.equivalentObjectId
=
2298 equivalentObject
->objectId
;
2299 list_add(&in
->hardLinks
, &equivalentObject
->hardLinks
);
2301 case YAFFS_OBJECT_TYPE_FILE
:
2302 case YAFFS_OBJECT_TYPE_DIRECTORY
:
2303 case YAFFS_OBJECT_TYPE_SPECIAL
:
2304 case YAFFS_OBJECT_TYPE_UNKNOWN
:
2309 if (yaffs_UpdateObjectHeader(in
, name
, 0, 0, 0) < 0) {
2310 /* Could not create the object header, fail the creation */
2311 yaffs_DestroyObject(in
);
2320 yaffs_Object
*yaffs_MknodFile(yaffs_Object
* parent
, const YCHAR
* name
,
2321 __u32 mode
, __u32 uid
, __u32 gid
)
2323 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE
, parent
, name
, mode
,
2324 uid
, gid
, NULL
, NULL
, 0);
2327 yaffs_Object
*yaffs_MknodDirectory(yaffs_Object
* parent
, const YCHAR
* name
,
2328 __u32 mode
, __u32 uid
, __u32 gid
)
2330 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY
, parent
, name
,
2331 mode
, uid
, gid
, NULL
, NULL
, 0);
2334 yaffs_Object
*yaffs_MknodSpecial(yaffs_Object
* parent
, const YCHAR
* name
,
2335 __u32 mode
, __u32 uid
, __u32 gid
, __u32 rdev
)
2337 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL
, parent
, name
, mode
,
2338 uid
, gid
, NULL
, NULL
, rdev
);
2341 yaffs_Object
*yaffs_MknodSymLink(yaffs_Object
* parent
, const YCHAR
* name
,
2342 __u32 mode
, __u32 uid
, __u32 gid
,
2343 const YCHAR
* alias
)
2345 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK
, parent
, name
, mode
,
2346 uid
, gid
, NULL
, alias
, 0);
2349 /* yaffs_Link returns the object id of the equivalent object.*/
2350 yaffs_Object
*yaffs_Link(yaffs_Object
* parent
, const YCHAR
* name
,
2351 yaffs_Object
* equivalentObject
)
2353 /* Get the real object in case we were fed a hard link as an equivalent object */
2354 equivalentObject
= yaffs_GetEquivalentObject(equivalentObject
);
2356 if (yaffs_MknodObject
2357 (YAFFS_OBJECT_TYPE_HARDLINK
, parent
, name
, 0, 0, 0,
2358 equivalentObject
, NULL
, 0)) {
2359 return equivalentObject
;
2366 static int yaffs_ChangeObjectName(yaffs_Object
* obj
, yaffs_Object
* newDir
,
2367 const YCHAR
* newName
, int force
, int shadows
)
2372 yaffs_Object
*existingTarget
;
2374 if (newDir
== NULL
) {
2375 newDir
= obj
->parent
; /* use the old directory */
2378 if (newDir
->variantType
!= YAFFS_OBJECT_TYPE_DIRECTORY
) {
2379 T(YAFFS_TRACE_ALWAYS
,
2381 ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
2386 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
2387 if (obj
->myDev
->isYaffs2
) {
2388 unlinkOp
= (newDir
== obj
->myDev
->unlinkedDir
);
2390 unlinkOp
= (newDir
== obj
->myDev
->unlinkedDir
2391 && obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
);
2394 deleteOp
= (newDir
== obj
->myDev
->deletedDir
);
2396 existingTarget
= yaffs_FindObjectByName(newDir
, newName
);
2398 /* If the object is a file going into the unlinked directory,
2399 * then it is OK to just stuff it in since duplicate names are allowed.
2400 * else only proceed if the new name does not exist and if we're putting
2401 * it into a directory.
2408 newDir
->variantType
== YAFFS_OBJECT_TYPE_DIRECTORY
) {
2409 yaffs_SetObjectName(obj
, newName
);
2412 yaffs_AddObjectToDirectory(newDir
, obj
);
2417 /* If it is a deletion then we mark it as a shrink for gc purposes. */
2418 if (yaffs_UpdateObjectHeader(obj
, newName
, 0, deleteOp
, shadows
)>= 0)
2425 int yaffs_RenameObject(yaffs_Object
* oldDir
, const YCHAR
* oldName
,
2426 yaffs_Object
* newDir
, const YCHAR
* newName
)
2429 yaffs_Object
*existingTarget
;
2432 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2433 /* Special case for case insemsitive systems (eg. WinCE).
2434 * While look-up is case insensitive, the name isn't.
2435 * Therefore we might want to change x.txt to X.txt
2437 if (oldDir
== newDir
&& yaffs_strcmp(oldName
, newName
) == 0) {
2442 obj
= yaffs_FindObjectByName(oldDir
, oldName
);
2443 /* Check new name to long. */
2444 if (obj
->variantType
== YAFFS_OBJECT_TYPE_SYMLINK
&&
2445 yaffs_strlen(newName
) > YAFFS_MAX_ALIAS_LENGTH
)
2448 else if (obj
->variantType
!= YAFFS_OBJECT_TYPE_SYMLINK
&&
2449 yaffs_strlen(newName
) > YAFFS_MAX_NAME_LENGTH
)
2453 if (obj
&& obj
->renameAllowed
) {
2455 /* Now do the handling for an existing target, if there is one */
2457 existingTarget
= yaffs_FindObjectByName(newDir
, newName
);
2458 if (existingTarget
&&
2459 existingTarget
->variantType
== YAFFS_OBJECT_TYPE_DIRECTORY
&&
2460 !list_empty(&existingTarget
->variant
.directoryVariant
.children
)) {
2461 /* There is a target that is a non-empty directory, so we fail */
2462 return YAFFS_FAIL
; /* EEXIST or ENOTEMPTY */
2463 } else if (existingTarget
&& existingTarget
!= obj
) {
2464 /* Nuke the target first, using shadowing,
2465 * but only if it isn't the same object
2467 yaffs_ChangeObjectName(obj
, newDir
, newName
, force
,
2468 existingTarget
->objectId
);
2469 yaffs_UnlinkObject(existingTarget
);
2472 return yaffs_ChangeObjectName(obj
, newDir
, newName
, 1, 0);
2477 /*------------------------- Block Management and Page Allocation ----------------*/
2479 static int yaffs_InitialiseBlocks(yaffs_Device
* dev
)
2481 int nBlocks
= dev
->internalEndBlock
- dev
->internalStartBlock
+ 1;
2483 dev
->blockInfo
= NULL
;
2484 dev
->chunkBits
= NULL
;
2486 dev
->allocationBlock
= -1; /* force it to get a new one */
2488 /* If the first allocation strategy fails, thry the alternate one */
2489 dev
->blockInfo
= YMALLOC(nBlocks
* sizeof(yaffs_BlockInfo
));
2490 if(!dev
->blockInfo
){
2491 dev
->blockInfo
= YMALLOC_ALT(nBlocks
* sizeof(yaffs_BlockInfo
));
2492 dev
->blockInfoAlt
= 1;
2495 dev
->blockInfoAlt
= 0;
2499 /* Set up dynamic blockinfo stuff. */
2500 dev
->chunkBitmapStride
= (dev
->nChunksPerBlock
+ 7) / 8; /* round up bytes */
2501 dev
->chunkBits
= YMALLOC(dev
->chunkBitmapStride
* nBlocks
);
2502 if(!dev
->chunkBits
){
2503 dev
->chunkBits
= YMALLOC_ALT(dev
->chunkBitmapStride
* nBlocks
);
2504 dev
->chunkBitsAlt
= 1;
2507 dev
->chunkBitsAlt
= 0;
2510 if (dev
->blockInfo
&& dev
->chunkBits
) {
2511 memset(dev
->blockInfo
, 0, nBlocks
* sizeof(yaffs_BlockInfo
));
2512 memset(dev
->chunkBits
, 0, dev
->chunkBitmapStride
* nBlocks
);
2520 static void yaffs_DeinitialiseBlocks(yaffs_Device
* dev
)
2522 if(dev
->blockInfoAlt
&& dev
->blockInfo
)
2523 YFREE_ALT(dev
->blockInfo
);
2524 else if(dev
->blockInfo
)
2525 YFREE(dev
->blockInfo
);
2527 dev
->blockInfoAlt
= 0;
2529 dev
->blockInfo
= NULL
;
2531 if(dev
->chunkBitsAlt
&& dev
->chunkBits
)
2532 YFREE_ALT(dev
->chunkBits
);
2533 else if(dev
->chunkBits
)
2534 YFREE(dev
->chunkBits
);
2535 dev
->chunkBitsAlt
= 0;
2536 dev
->chunkBits
= NULL
;
2539 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device
* dev
,
2540 yaffs_BlockInfo
* bi
)
2547 return 1; /* disqualification only applies to yaffs2. */
2549 if (!bi
->hasShrinkHeader
)
2550 return 1; /* can gc */
2552 /* Find the oldest dirty sequence number if we don't know it and save it
2553 * so we don't have to keep recomputing it.
2555 if (!dev
->oldestDirtySequence
) {
2556 seq
= dev
->sequenceNumber
;
2558 for (i
= dev
->internalStartBlock
; i
<= dev
->internalEndBlock
;
2560 b
= yaffs_GetBlockInfo(dev
, i
);
2561 if (b
->blockState
== YAFFS_BLOCK_STATE_FULL
&&
2562 (b
->pagesInUse
- b
->softDeletions
) <
2563 dev
->nChunksPerBlock
&& b
->sequenceNumber
< seq
) {
2564 seq
= b
->sequenceNumber
;
2567 dev
->oldestDirtySequence
= seq
;
2570 /* Can't do gc of this block if there are any blocks older than this one that have
2573 return (bi
->sequenceNumber
<= dev
->oldestDirtySequence
);
2577 /* FindDiretiestBlock is used to select the dirtiest block (or close enough)
2578 * for garbage collection.
2581 static int yaffs_FindBlockForGarbageCollection(yaffs_Device
* dev
,
2585 int b
= dev
->currentDirtyChecker
;
2592 yaffs_BlockInfo
*bi
;
2593 int pendingPrioritisedExist
= 0;
2595 /* First let's see if we need to grab a prioritised block */
2596 if(dev
->hasPendingPrioritisedGCs
){
2597 for(i
= dev
->internalStartBlock
; i
< dev
->internalEndBlock
&& !prioritised
; i
++){
2599 bi
= yaffs_GetBlockInfo(dev
, i
);
2600 //yaffs_VerifyBlock(dev,bi,i);
2602 if(bi
->gcPrioritise
) {
2603 pendingPrioritisedExist
= 1;
2604 if(bi
->blockState
== YAFFS_BLOCK_STATE_FULL
&&
2605 yaffs_BlockNotDisqualifiedFromGC(dev
, bi
)){
2606 pagesInUse
= (bi
->pagesInUse
- bi
->softDeletions
);
2609 aggressive
= 1; /* Fool the non-aggressive skip logiv below */
2614 if(!pendingPrioritisedExist
) /* None found, so we can clear this */
2615 dev
->hasPendingPrioritisedGCs
= 0;
2618 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2620 * else (we're doing a leasurely gc), then we only bother to do this if the
2621 * block has only a few pages in use.
2624 dev
->nonAggressiveSkip
--;
2626 if (!aggressive
&& (dev
->nonAggressiveSkip
> 0)) {
2632 (aggressive
) ? dev
->nChunksPerBlock
: YAFFS_PASSIVE_GC_CHUNKS
+ 1;
2636 dev
->internalEndBlock
- dev
->internalStartBlock
+ 1;
2639 dev
->internalEndBlock
- dev
->internalStartBlock
+ 1;
2640 iterations
= iterations
/ 16;
2641 if (iterations
> 200) {
2646 for (i
= 0; i
<= iterations
&& pagesInUse
> 0 && !prioritised
; i
++) {
2648 if (b
< dev
->internalStartBlock
|| b
> dev
->internalEndBlock
) {
2649 b
= dev
->internalStartBlock
;
2652 if (b
< dev
->internalStartBlock
|| b
> dev
->internalEndBlock
) {
2653 T(YAFFS_TRACE_ERROR
,
2654 (TSTR("**>> Block %d is not valid" TENDSTR
), b
));
2658 bi
= yaffs_GetBlockInfo(dev
, b
);
2661 if (bi
->blockState
== YAFFS_BLOCK_STATE_CHECKPOINT
) {
2668 if (bi
->blockState
== YAFFS_BLOCK_STATE_FULL
&&
2669 (bi
->pagesInUse
- bi
->softDeletions
) < pagesInUse
&&
2670 yaffs_BlockNotDisqualifiedFromGC(dev
, bi
)) {
2672 pagesInUse
= (bi
->pagesInUse
- bi
->softDeletions
);
2676 dev
->currentDirtyChecker
= b
;
2680 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR
), dirtiest
,
2681 dev
->nChunksPerBlock
- pagesInUse
,prioritised
));
2684 dev
->oldestDirtySequence
= 0;
2687 dev
->nonAggressiveSkip
= 4;
2693 static void yaffs_BlockBecameDirty(yaffs_Device
* dev
, int blockNo
)
2695 yaffs_BlockInfo
*bi
= yaffs_GetBlockInfo(dev
, blockNo
);
2699 /* If the block is still healthy erase it and mark as clean.
2700 * If the block has had a data failure, then retire it.
2703 T(YAFFS_TRACE_GC
| YAFFS_TRACE_ERASE
,
2704 (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR
),
2705 blockNo
, bi
->blockState
, (bi
->needsRetiring
) ? "needs retiring" : ""));
2707 bi
->blockState
= YAFFS_BLOCK_STATE_DIRTY
;
2709 if (!bi
->needsRetiring
) {
2710 yaffs_InvalidateCheckpoint(dev
);
2711 erasedOk
= yaffs_EraseBlockInNAND(dev
, blockNo
);
2713 dev
->nErasureFailures
++;
2714 T(YAFFS_TRACE_ERROR
| YAFFS_TRACE_BAD_BLOCKS
,
2715 (TSTR("**>> Erasure failed %d" TENDSTR
), blockNo
));
2720 ((yaffs_traceMask
& YAFFS_TRACE_ERASE
) || !yaffs_SkipVerification(dev
))) {
2722 for (i
= 0; i
< dev
->nChunksPerBlock
; i
++) {
2723 if (!yaffs_CheckChunkErased
2724 (dev
, blockNo
* dev
->nChunksPerBlock
+ i
)) {
2725 T(YAFFS_TRACE_ERROR
,
2727 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2728 TENDSTR
), blockNo
, i
));
2734 /* Clean it up... */
2735 bi
->blockState
= YAFFS_BLOCK_STATE_EMPTY
;
2736 dev
->nErasedBlocks
++;
2738 bi
->softDeletions
= 0;
2739 bi
->hasShrinkHeader
= 0;
2740 bi
->skipErasedCheck
= 1; /* This is clean, so no need to check */
2741 bi
->gcPrioritise
= 0;
2742 yaffs_ClearChunkBits(dev
, blockNo
);
2744 T(YAFFS_TRACE_ERASE
,
2745 (TSTR("Erased block %d" TENDSTR
), blockNo
));
2747 dev
->nFreeChunks
-= dev
->nChunksPerBlock
; /* We lost a block of free space */
2749 yaffs_RetireBlock(dev
, blockNo
);
2750 T(YAFFS_TRACE_ERROR
| YAFFS_TRACE_BAD_BLOCKS
,
2751 (TSTR("**>> Block %d retired" TENDSTR
), blockNo
));
2755 static int yaffs_FindBlockForAllocation(yaffs_Device
* dev
)
2759 yaffs_BlockInfo
*bi
;
2761 if (dev
->nErasedBlocks
< 1) {
2762 /* Hoosterman we've got a problem.
2763 * Can't get space to gc
2765 T(YAFFS_TRACE_ERROR
,
2766 (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR
)));
2771 /* Find an empty block. */
2773 for (i
= dev
->internalStartBlock
; i
<= dev
->internalEndBlock
; i
++) {
2774 dev
->allocationBlockFinder
++;
2775 if (dev
->allocationBlockFinder
< dev
->internalStartBlock
2776 || dev
->allocationBlockFinder
> dev
->internalEndBlock
) {
2777 dev
->allocationBlockFinder
= dev
->internalStartBlock
;
2780 bi
= yaffs_GetBlockInfo(dev
, dev
->allocationBlockFinder
);
2782 if (bi
->blockState
== YAFFS_BLOCK_STATE_EMPTY
) {
2783 bi
->blockState
= YAFFS_BLOCK_STATE_ALLOCATING
;
2784 dev
->sequenceNumber
++;
2785 bi
->sequenceNumber
= dev
->sequenceNumber
;
2786 dev
->nErasedBlocks
--;
2787 T(YAFFS_TRACE_ALLOCATE
,
2788 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR
),
2789 dev
->allocationBlockFinder
, dev
->sequenceNumber
,
2790 dev
->nErasedBlocks
));
2791 return dev
->allocationBlockFinder
;
2795 T(YAFFS_TRACE_ALWAYS
,
2797 ("yaffs tragedy: no more eraased blocks, but there should have been %d"
2798 TENDSTR
), dev
->nErasedBlocks
));
2804 // Check if there's space to allocate...
2805 // Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
2806 static int yaffs_CheckSpaceForAllocation(yaffs_Device
* dev
)
2809 int reservedBlocks
= dev
->nReservedBlocks
;
2810 int checkpointBlocks
;
2812 checkpointBlocks
= dev
->nCheckpointReservedBlocks
- dev
->blocksInCheckpoint
;
2813 if(checkpointBlocks
< 0)
2814 checkpointBlocks
= 0;
2816 reservedChunks
= ((reservedBlocks
+ checkpointBlocks
) * dev
->nChunksPerBlock
);
2818 return (dev
->nFreeChunks
> reservedChunks
);
2821 static int yaffs_AllocateChunk(yaffs_Device
* dev
, int useReserve
, yaffs_BlockInfo
**blockUsedPtr
)
2824 yaffs_BlockInfo
*bi
;
2826 if (dev
->allocationBlock
< 0) {
2827 /* Get next block to allocate off */
2828 dev
->allocationBlock
= yaffs_FindBlockForAllocation(dev
);
2829 dev
->allocationPage
= 0;
2832 if (!useReserve
&& !yaffs_CheckSpaceForAllocation(dev
)) {
2833 /* Not enough space to allocate unless we're allowed to use the reserve. */
2837 if (dev
->nErasedBlocks
< dev
->nReservedBlocks
2838 && dev
->allocationPage
== 0) {
2839 T(YAFFS_TRACE_ALLOCATE
, (TSTR("Allocating reserve" TENDSTR
)));
2842 /* Next page please.... */
2843 if (dev
->allocationBlock
>= 0) {
2844 bi
= yaffs_GetBlockInfo(dev
, dev
->allocationBlock
);
2846 retVal
= (dev
->allocationBlock
* dev
->nChunksPerBlock
) +
2847 dev
->allocationPage
;
2849 yaffs_SetChunkBit(dev
, dev
->allocationBlock
,
2850 dev
->allocationPage
);
2852 dev
->allocationPage
++;
2856 /* If the block is full set the state to full */
2857 if (dev
->allocationPage
>= dev
->nChunksPerBlock
) {
2858 bi
->blockState
= YAFFS_BLOCK_STATE_FULL
;
2859 dev
->allocationBlock
= -1;
2868 T(YAFFS_TRACE_ERROR
,
2869 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR
)));
2874 static int yaffs_GetErasedChunks(yaffs_Device
* dev
)
2878 n
= dev
->nErasedBlocks
* dev
->nChunksPerBlock
;
2880 if (dev
->allocationBlock
> 0) {
2881 n
+= (dev
->nChunksPerBlock
- dev
->allocationPage
);
2888 static int yaffs_GarbageCollectBlock(yaffs_Device
* dev
, int block
)
2894 int retVal
= YAFFS_OK
;
2897 int isCheckpointBlock
;
2900 int chunksBefore
= yaffs_GetErasedChunks(dev
);
2903 yaffs_ExtendedTags tags
;
2905 yaffs_BlockInfo
*bi
= yaffs_GetBlockInfo(dev
, block
);
2907 yaffs_Object
*object
;
2909 isCheckpointBlock
= (bi
->blockState
== YAFFS_BLOCK_STATE_CHECKPOINT
);
2911 bi
->blockState
= YAFFS_BLOCK_STATE_COLLECTING
;
2913 T(YAFFS_TRACE_TRACING
,
2914 (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR
), block
,
2915 bi
->pagesInUse
, bi
->hasShrinkHeader
));
2917 /*yaffs_VerifyFreeChunks(dev); */
2919 bi
->hasShrinkHeader
= 0; /* clear the flag so that the block can erase */
2921 /* Take off the number of soft deleted entries because
2922 * they're going to get really deleted during GC.
2924 dev
->nFreeChunks
-= bi
->softDeletions
;
2928 if (isCheckpointBlock
||
2929 !yaffs_StillSomeChunkBits(dev
, block
)) {
2930 T(YAFFS_TRACE_TRACING
,
2932 ("Collecting block %d that has no chunks in use" TENDSTR
),
2934 yaffs_BlockBecameDirty(dev
, block
);
2937 __u8
*buffer
= yaffs_GetTempBuffer(dev
, __LINE__
);
2939 yaffs_VerifyBlock(dev
,bi
,block
);
2941 for (chunkInBlock
= 0, oldChunk
= block
* dev
->nChunksPerBlock
;
2942 chunkInBlock
< dev
->nChunksPerBlock
2943 && yaffs_StillSomeChunkBits(dev
, block
);
2944 chunkInBlock
++, oldChunk
++) {
2945 if (yaffs_CheckChunkBit(dev
, block
, chunkInBlock
)) {
2947 /* This page is in use and might need to be copied off */
2951 yaffs_InitialiseTags(&tags
);
2953 yaffs_ReadChunkWithTagsFromNAND(dev
, oldChunk
,
2957 yaffs_FindObjectByNumber(dev
,
2960 T(YAFFS_TRACE_GC_DETAIL
,
2962 ("Collecting page %d, %d %d %d " TENDSTR
),
2963 chunkInBlock
, tags
.objectId
, tags
.chunkId
,
2966 if(object
&& !yaffs_SkipVerification(dev
)){
2967 if(tags
.chunkId
== 0)
2968 matchingChunk
= object
->chunkId
;
2969 else if(object
->softDeleted
)
2970 matchingChunk
= oldChunk
; /* Defeat the test */
2972 matchingChunk
= yaffs_FindChunkInFile(object
,tags
.chunkId
,NULL
);
2974 if(oldChunk
!= matchingChunk
)
2975 T(YAFFS_TRACE_ERROR
,
2976 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR
),
2977 oldChunk
,matchingChunk
,tags
.objectId
, tags
.chunkId
));
2982 T(YAFFS_TRACE_ERROR
,
2984 ("page %d in gc has no object: %d %d %d "
2986 tags
.objectId
, tags
.chunkId
, tags
.byteCount
));
2989 if (object
&& object
->deleted
2990 && tags
.chunkId
!= 0) {
2991 /* Data chunk in a deleted file, throw it away
2992 * It's a soft deleted data chunk,
2993 * No need to copy this, just forget about it and
2994 * fix up the object.
2997 object
->nDataChunks
--;
2999 if (object
->nDataChunks
<= 0) {
3000 /* remeber to clean up the object */
3001 dev
->gcCleanupList
[cleanups
] =
3007 /* Todo object && object->deleted && object->nDataChunks == 0 */
3009 /* Deleted object header with no data chunks.
3010 * Can be discarded and the file deleted.
3012 object
->chunkId
= 0;
3013 yaffs_FreeTnode(object
->myDev
,
3016 object
->variant
.fileVariant
.top
= NULL
;
3017 yaffs_DoGenericObjectDeletion(object
);
3019 } else if (object
) {
3020 /* It's either a data chunk in a live file or
3021 * an ObjectHeader, so we're interested in it.
3022 * NB Need to keep the ObjectHeaders of deleted files
3023 * until the whole file has been deleted off
3025 tags
.serialNumber
++;
3029 if (tags
.chunkId
== 0) {
3030 /* It is an object Id,
3031 * We need to nuke the shrinkheader flags first
3032 * We no longer want the shrinkHeader flag since its work is done
3033 * and if it is left in place it will mess up scanning.
3034 * Also, clear out any shadowing stuff
3037 yaffs_ObjectHeader
*oh
;
3038 oh
= (yaffs_ObjectHeader
*)buffer
;
3040 oh
->shadowsObject
= -1;
3041 tags
.extraShadows
= 0;
3042 tags
.extraIsShrinkHeader
= 0;
3044 yaffs_VerifyObjectHeader(object
,oh
,&tags
,1);
3048 yaffs_WriteNewChunkWithTagsToNAND(dev
, buffer
, &tags
, 1);
3051 retVal
= YAFFS_FAIL
;
3054 /* Ok, now fix up the Tnodes etc. */
3056 if (tags
.chunkId
== 0) {
3058 object
->chunkId
= newChunk
;
3059 object
->serial
= tags
.serialNumber
;
3061 /* It's a data chunk */
3062 yaffs_PutChunkIntoFile
3070 yaffs_DeleteChunk(dev
, oldChunk
, markNAND
, __LINE__
);
3075 yaffs_ReleaseTempBuffer(dev
, buffer
, __LINE__
);
3078 /* Do any required cleanups */
3079 for (i
= 0; i
< cleanups
; i
++) {
3080 /* Time to delete the file too */
3082 yaffs_FindObjectByNumber(dev
,
3083 dev
->gcCleanupList
[i
]);
3085 yaffs_FreeTnode(dev
,
3086 object
->variant
.fileVariant
.
3088 object
->variant
.fileVariant
.top
= NULL
;
3091 ("yaffs: About to finally delete object %d"
3092 TENDSTR
), object
->objectId
));
3093 yaffs_DoGenericObjectDeletion(object
);
3094 object
->myDev
->nDeletedFiles
--;
3101 yaffs_VerifyCollectedBlock(dev
,bi
,block
);
3103 if (chunksBefore
>= (chunksAfter
= yaffs_GetErasedChunks(dev
))) {
3106 ("gc did not increase free chunks before %d after %d"
3107 TENDSTR
), chunksBefore
, chunksAfter
));
3115 /* New garbage collector
3116 * If we're very low on erased blocks then we do aggressive garbage collection
3117 * otherwise we do "leasurely" garbage collection.
3118 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
3119 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
3121 * The idea is to help clear out space in a more spread-out manner.
3122 * Dunno if it really does anything useful.
3124 static int yaffs_CheckGarbageCollection(yaffs_Device
* dev
)
3128 int gcOk
= YAFFS_OK
;
3131 int checkpointBlockAdjust
;
3133 if (dev
->isDoingGC
) {
3134 /* Bail out so we don't get recursive gc */
3138 /* This loop should pass the first time.
3139 * We'll only see looping here if the erase of the collected block fails.
3145 checkpointBlockAdjust
= (dev
->nCheckpointReservedBlocks
- dev
->blocksInCheckpoint
);
3146 if(checkpointBlockAdjust
< 0)
3147 checkpointBlockAdjust
= 0;
3149 if (dev
->nErasedBlocks
< (dev
->nReservedBlocks
+ checkpointBlockAdjust
+ 2)) {
3150 /* We need a block soon...*/
3153 /* We're in no hurry */
3157 block
= yaffs_FindBlockForGarbageCollection(dev
, aggressive
);
3160 dev
->garbageCollections
++;
3162 dev
->passiveGarbageCollections
++;
3167 ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR
),
3168 dev
->nErasedBlocks
, aggressive
));
3170 gcOk
= yaffs_GarbageCollectBlock(dev
, block
);
3173 if (dev
->nErasedBlocks
< (dev
->nReservedBlocks
) && block
> 0) {
3176 ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
3177 TENDSTR
), dev
->nErasedBlocks
, maxTries
, block
));
3179 } while ((dev
->nErasedBlocks
< dev
->nReservedBlocks
) && (block
> 0)
3182 return aggressive
? gcOk
: YAFFS_OK
;
3185 /*------------------------- TAGS --------------------------------*/
3187 static int yaffs_TagsMatch(const yaffs_ExtendedTags
* tags
, int objectId
,
3190 return (tags
->chunkId
== chunkInObject
&&
3191 tags
->objectId
== objectId
&& !tags
->chunkDeleted
) ? 1 : 0;
3196 /*-------------------- Data file manipulation -----------------*/
3198 static int yaffs_FindChunkInFile(yaffs_Object
* in
, int chunkInInode
,
3199 yaffs_ExtendedTags
* tags
)
3201 /*Get the Tnode, then get the level 0 offset chunk offset */
3204 yaffs_ExtendedTags localTags
;
3207 yaffs_Device
*dev
= in
->myDev
;
3210 /* Passed a NULL, so use our own tags space */
3214 tn
= yaffs_FindLevel0Tnode(dev
, &in
->variant
.fileVariant
, chunkInInode
);
3217 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,chunkInInode
);
3220 yaffs_FindChunkInGroup(dev
, theChunk
, tags
, in
->objectId
,
3226 static int yaffs_FindAndDeleteChunkInFile(yaffs_Object
* in
, int chunkInInode
,
3227 yaffs_ExtendedTags
* tags
)
3229 /* Get the Tnode, then get the level 0 offset chunk offset */
3232 yaffs_ExtendedTags localTags
;
3234 yaffs_Device
*dev
= in
->myDev
;
3238 /* Passed a NULL, so use our own tags space */
3242 tn
= yaffs_FindLevel0Tnode(dev
, &in
->variant
.fileVariant
, chunkInInode
);
3246 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,chunkInInode
);
3249 yaffs_FindChunkInGroup(dev
, theChunk
, tags
, in
->objectId
,
3252 /* Delete the entry in the filestructure (if found) */
3254 yaffs_PutLevel0Tnode(dev
,tn
,chunkInInode
,0);
3257 /*T(("No level 0 found for %d\n", chunkInInode)); */
3261 /* T(("Could not find %d to delete\n",chunkInInode)); */
3266 #ifdef YAFFS_PARANOID
3268 static int yaffs_CheckFileSanity(yaffs_Object
* in
)
3276 yaffs_Tags localTags
;
3277 yaffs_Tags
*tags
= &localTags
;
3281 if (in
->variantType
!= YAFFS_OBJECT_TYPE_FILE
) {
3282 /* T(("Object not a file\n")); */
3286 objId
= in
->objectId
;
3287 fSize
= in
->variant
.fileVariant
.fileSize
;
3289 (fSize
+ in
->myDev
->nDataBytesPerChunk
- 1) / in
->myDev
->nDataBytesPerChunk
;
3291 for (chunk
= 1; chunk
<= nChunks
; chunk
++) {
3292 tn
= yaffs_FindLevel0Tnode(in
->myDev
, &in
->variant
.fileVariant
,
3297 theChunk
= yaffs_GetChunkGroupBase(dev
,tn
,chunk
);
3299 if (yaffs_CheckChunkBits
3300 (dev
, theChunk
/ dev
->nChunksPerBlock
,
3301 theChunk
% dev
->nChunksPerBlock
)) {
3303 yaffs_ReadChunkTagsFromNAND(in
->myDev
, theChunk
,
3307 (tags
, in
->objectId
, chunk
, chunkDeleted
)) {
3317 /* T(("No level 0 found for %d\n", chunk)); */
3321 return failed
? YAFFS_FAIL
: YAFFS_OK
;
3326 static int yaffs_PutChunkIntoFile(yaffs_Object
* in
, int chunkInInode
,
3327 int chunkInNAND
, int inScan
)
3329 /* NB inScan is zero unless scanning.
3330 * For forward scanning, inScan is > 0;
3331 * for backward scanning inScan is < 0
3335 yaffs_Device
*dev
= in
->myDev
;
3337 yaffs_ExtendedTags existingTags
;
3338 yaffs_ExtendedTags newTags
;
3339 unsigned existingSerial
, newSerial
;
3341 if (in
->variantType
!= YAFFS_OBJECT_TYPE_FILE
) {
3342 /* Just ignore an attempt at putting a chunk into a non-file during scanning
3343 * If it is not during Scanning then something went wrong!
3346 T(YAFFS_TRACE_ERROR
,
3348 ("yaffs tragedy:attempt to put data chunk into a non-file"
3353 yaffs_DeleteChunk(dev
, chunkInNAND
, 1, __LINE__
);
3357 tn
= yaffs_AddOrFindLevel0Tnode(dev
,
3358 &in
->variant
.fileVariant
,
3365 existingChunk
= yaffs_GetChunkGroupBase(dev
,tn
,chunkInInode
);
3368 /* If we're scanning then we need to test for duplicates
3369 * NB This does not need to be efficient since it should only ever
3370 * happen when the power fails during a write, then only one
3371 * chunk should ever be affected.
3373 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3374 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3377 if (existingChunk
!= 0) {
3378 /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
3379 * thus we have to do a FindChunkInFile to get the real chunk id.
3381 * We have a duplicate now we need to decide which one to use:
3383 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3384 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
3385 * YAFFS1: Get both sets of tags and compare serial numbers.
3389 /* Only do this for forward scanning */
3390 yaffs_ReadChunkWithTagsFromNAND(dev
,
3394 /* Do a proper find */
3396 yaffs_FindChunkInFile(in
, chunkInInode
,
3400 if (existingChunk
<= 0) {
3401 /*Hoosterman - how did this happen? */
3403 T(YAFFS_TRACE_ERROR
,
3405 ("yaffs tragedy: existing chunk < 0 in scan"
3410 /* NB The deleted flags should be false, otherwise the chunks will
3411 * not be loaded during a scan
3414 newSerial
= newTags
.serialNumber
;
3415 existingSerial
= existingTags
.serialNumber
;
3418 (in
->myDev
->isYaffs2
||
3419 existingChunk
<= 0 ||
3420 ((existingSerial
+ 1) & 3) == newSerial
)) {
3421 /* Forward scanning.
3423 * Delete the old one and drop through to update the tnode
3425 yaffs_DeleteChunk(dev
, existingChunk
, 1,
3428 /* Backward scanning or we want to use the existing one
3430 * Delete the new one and return early so that the tnode isn't changed
3432 yaffs_DeleteChunk(dev
, chunkInNAND
, 1,
3440 if (existingChunk
== 0) {
3444 yaffs_PutLevel0Tnode(dev
,tn
,chunkInInode
,chunkInNAND
);
3449 static int yaffs_ReadChunkDataFromObject(yaffs_Object
* in
, int chunkInInode
,
3452 int chunkInNAND
= yaffs_FindChunkInFile(in
, chunkInInode
, NULL
);
3454 if (chunkInNAND
>= 0) {
3455 return yaffs_ReadChunkWithTagsFromNAND(in
->myDev
, chunkInNAND
,
3458 T(YAFFS_TRACE_NANDACCESS
,
3459 (TSTR("Chunk %d not found zero instead" TENDSTR
),
3461 /* get sane (zero) data if you read a hole */
3462 memset(buffer
, 0, in
->myDev
->nDataBytesPerChunk
);
3468 void yaffs_DeleteChunk(yaffs_Device
* dev
, int chunkId
, int markNAND
, int lyn
)
3472 yaffs_ExtendedTags tags
;
3473 yaffs_BlockInfo
*bi
;
3480 block
= chunkId
/ dev
->nChunksPerBlock
;
3481 page
= chunkId
% dev
->nChunksPerBlock
;
3484 if(!yaffs_CheckChunkBit(dev
,block
,page
))
3485 T(YAFFS_TRACE_VERIFY
,
3486 (TSTR("Deleting invalid chunk %d"TENDSTR
),
3489 bi
= yaffs_GetBlockInfo(dev
, block
);
3491 T(YAFFS_TRACE_DELETION
,
3492 (TSTR("line %d delete of chunk %d" TENDSTR
), lyn
, chunkId
));
3495 bi
->blockState
!= YAFFS_BLOCK_STATE_COLLECTING
&& !dev
->isYaffs2
) {
3497 yaffs_InitialiseTags(&tags
);
3499 tags
.chunkDeleted
= 1;
3501 yaffs_WriteChunkWithTagsToNAND(dev
, chunkId
, NULL
, &tags
);
3502 yaffs_HandleUpdateChunk(dev
, chunkId
, &tags
);
3504 dev
->nUnmarkedDeletions
++;
3507 /* Pull out of the management area.
3508 * If the whole block became dirty, this will kick off an erasure.
3510 if (bi
->blockState
== YAFFS_BLOCK_STATE_ALLOCATING
||
3511 bi
->blockState
== YAFFS_BLOCK_STATE_FULL
||
3512 bi
->blockState
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
||
3513 bi
->blockState
== YAFFS_BLOCK_STATE_COLLECTING
) {
3516 yaffs_ClearChunkBit(dev
, block
, page
);
3520 if (bi
->pagesInUse
== 0 &&
3521 !bi
->hasShrinkHeader
&&
3522 bi
->blockState
!= YAFFS_BLOCK_STATE_ALLOCATING
&&
3523 bi
->blockState
!= YAFFS_BLOCK_STATE_NEEDS_SCANNING
) {
3524 yaffs_BlockBecameDirty(dev
, block
);
3528 /* T(("Bad news deleting chunk %d\n",chunkId)); */
3533 static int yaffs_WriteChunkDataToObject(yaffs_Object
* in
, int chunkInInode
,
3534 const __u8
* buffer
, int nBytes
,
3537 /* Find old chunk Need to do this to get serial number
3538 * Write new one and patch into tree.
3539 * Invalidate old tags.
3543 yaffs_ExtendedTags prevTags
;
3546 yaffs_ExtendedTags newTags
;
3548 yaffs_Device
*dev
= in
->myDev
;
3550 yaffs_CheckGarbageCollection(dev
);
3552 /* Get the previous chunk at this location in the file if it exists */
3553 prevChunkId
= yaffs_FindChunkInFile(in
, chunkInInode
, &prevTags
);
3555 /* Set up new tags */
3556 yaffs_InitialiseTags(&newTags
);
3558 newTags
.chunkId
= chunkInInode
;
3559 newTags
.objectId
= in
->objectId
;
3560 newTags
.serialNumber
=
3561 (prevChunkId
>= 0) ? prevTags
.serialNumber
+ 1 : 1;
3562 newTags
.byteCount
= nBytes
;
3565 yaffs_WriteNewChunkWithTagsToNAND(dev
, buffer
, &newTags
,
3568 if (newChunkId
>= 0) {
3569 yaffs_PutChunkIntoFile(in
, chunkInInode
, newChunkId
, 0);
3571 if (prevChunkId
>= 0) {
3572 yaffs_DeleteChunk(dev
, prevChunkId
, 1, __LINE__
);
3576 yaffs_CheckFileSanity(in
);
3582 /* UpdateObjectHeader updates the header on NAND for an object.
3583 * If name is not NULL, then that new name is used.
3585 int yaffs_UpdateObjectHeader(yaffs_Object
* in
, const YCHAR
* name
, int force
,
3586 int isShrink
, int shadows
)
3589 yaffs_BlockInfo
*bi
;
3591 yaffs_Device
*dev
= in
->myDev
;
3598 yaffs_ExtendedTags newTags
;
3599 yaffs_ExtendedTags oldTags
;
3601 __u8
*buffer
= NULL
;
3602 YCHAR oldName
[YAFFS_MAX_NAME_LENGTH
+ 1];
3604 yaffs_ObjectHeader
*oh
= NULL
;
3606 yaffs_strcpy(oldName
,"silly old name");
3608 if (!in
->fake
|| force
) {
3610 yaffs_CheckGarbageCollection(dev
);
3611 yaffs_CheckObjectDetailsLoaded(in
);
3613 buffer
= yaffs_GetTempBuffer(in
->myDev
, __LINE__
);
3614 oh
= (yaffs_ObjectHeader
*) buffer
;
3616 prevChunkId
= in
->chunkId
;
3618 if (prevChunkId
>= 0) {
3619 result
= yaffs_ReadChunkWithTagsFromNAND(dev
, prevChunkId
,
3622 yaffs_VerifyObjectHeader(in
,oh
,&oldTags
,0);
3624 memcpy(oldName
, oh
->name
, sizeof(oh
->name
));
3627 memset(buffer
, 0xFF, dev
->nDataBytesPerChunk
);
3629 oh
->type
= in
->variantType
;
3630 oh
->yst_mode
= in
->yst_mode
;
3631 oh
->shadowsObject
= shadows
;
3633 #ifdef CONFIG_YAFFS_WINCE
3634 oh
->win_atime
[0] = in
->win_atime
[0];
3635 oh
->win_ctime
[0] = in
->win_ctime
[0];
3636 oh
->win_mtime
[0] = in
->win_mtime
[0];
3637 oh
->win_atime
[1] = in
->win_atime
[1];
3638 oh
->win_ctime
[1] = in
->win_ctime
[1];
3639 oh
->win_mtime
[1] = in
->win_mtime
[1];
3641 oh
->yst_uid
= in
->yst_uid
;
3642 oh
->yst_gid
= in
->yst_gid
;
3643 oh
->yst_atime
= in
->yst_atime
;
3644 oh
->yst_mtime
= in
->yst_mtime
;
3645 oh
->yst_ctime
= in
->yst_ctime
;
3646 oh
->yst_rdev
= in
->yst_rdev
;
3649 oh
->parentObjectId
= in
->parent
->objectId
;
3651 oh
->parentObjectId
= 0;
3654 if (name
&& *name
) {
3655 memset(oh
->name
, 0, sizeof(oh
->name
));
3656 yaffs_strncpy(oh
->name
, name
, YAFFS_MAX_NAME_LENGTH
);
3657 } else if (prevChunkId
>=0) {
3658 memcpy(oh
->name
, oldName
, sizeof(oh
->name
));
3660 memset(oh
->name
, 0, sizeof(oh
->name
));
3663 oh
->isShrink
= isShrink
;
3665 switch (in
->variantType
) {
3666 case YAFFS_OBJECT_TYPE_UNKNOWN
:
3667 /* Should not happen */
3669 case YAFFS_OBJECT_TYPE_FILE
:
3671 (oh
->parentObjectId
== YAFFS_OBJECTID_DELETED
3672 || oh
->parentObjectId
==
3673 YAFFS_OBJECTID_UNLINKED
) ? 0 : in
->variant
.
3674 fileVariant
.fileSize
;
3676 case YAFFS_OBJECT_TYPE_HARDLINK
:
3677 oh
->equivalentObjectId
=
3678 in
->variant
.hardLinkVariant
.equivalentObjectId
;
3680 case YAFFS_OBJECT_TYPE_SPECIAL
:
3683 case YAFFS_OBJECT_TYPE_DIRECTORY
:
3686 case YAFFS_OBJECT_TYPE_SYMLINK
:
3687 yaffs_strncpy(oh
->alias
,
3688 in
->variant
.symLinkVariant
.alias
,
3689 YAFFS_MAX_ALIAS_LENGTH
);
3690 oh
->alias
[YAFFS_MAX_ALIAS_LENGTH
] = 0;
3695 yaffs_InitialiseTags(&newTags
);
3697 newTags
.chunkId
= 0;
3698 newTags
.objectId
= in
->objectId
;
3699 newTags
.serialNumber
= in
->serial
;
3701 /* Add extra info for file header */
3703 newTags
.extraHeaderInfoAvailable
= 1;
3704 newTags
.extraParentObjectId
= oh
->parentObjectId
;
3705 newTags
.extraFileLength
= oh
->fileSize
;
3706 newTags
.extraIsShrinkHeader
= oh
->isShrink
;
3707 newTags
.extraEquivalentObjectId
= oh
->equivalentObjectId
;
3708 newTags
.extraShadows
= (oh
->shadowsObject
> 0) ? 1 : 0;
3709 newTags
.extraObjectType
= in
->variantType
;
3711 yaffs_VerifyObjectHeader(in
,oh
,&newTags
,1);
3713 /* Create new chunk in NAND */
3715 yaffs_WriteNewChunkWithTagsToNAND(dev
, buffer
, &newTags
,
3716 (prevChunkId
>= 0) ? 1 : 0);
3718 if (newChunkId
>= 0) {
3720 in
->chunkId
= newChunkId
;
3722 if (prevChunkId
>= 0) {
3723 yaffs_DeleteChunk(dev
, prevChunkId
, 1,
3727 if(!yaffs_ObjectHasCachedWriteData(in
))
3730 /* If this was a shrink, then mark the block that the chunk lives on */
3732 bi
= yaffs_GetBlockInfo(in
->myDev
,
3733 newChunkId
/in
->myDev
-> nChunksPerBlock
);
3734 bi
->hasShrinkHeader
= 1;
3739 retVal
= newChunkId
;
3744 yaffs_ReleaseTempBuffer(dev
, buffer
, __LINE__
);
3749 /*------------------------ Short Operations Cache ----------------------------------------
3750 * In many situations where there is no high level buffering (eg WinCE) a lot of
3751 * reads might be short sequential reads, and a lot of writes may be short
3752 * sequential writes. eg. scanning/writing a jpeg file.
3753 * In these cases, a short read/write cache can provide a huge perfomance benefit
3754 * with dumb-as-a-rock code.
3755 * In Linux, the page cache provides read buffering aand the short op cache provides write
3758 * There are a limited number (~10) of cache chunks per device so that we don't
3759 * need a very intelligent search.
3762 static int yaffs_ObjectHasCachedWriteData(yaffs_Object
*obj
)
3764 yaffs_Device
*dev
= obj
->myDev
;
3766 yaffs_ChunkCache
*cache
;
3767 int nCaches
= obj
->myDev
->nShortOpCaches
;
3769 for(i
= 0; i
< nCaches
; i
++){
3770 cache
= &dev
->srCache
[i
];
3771 if (cache
->object
== obj
&&
3780 static void yaffs_FlushFilesChunkCache(yaffs_Object
* obj
)
3782 yaffs_Device
*dev
= obj
->myDev
;
3783 int lowest
= -99; /* Stop compiler whining. */
3785 yaffs_ChunkCache
*cache
;
3786 int chunkWritten
= 0;
3787 int nCaches
= obj
->myDev
->nShortOpCaches
;
3793 /* Find the dirty cache for this object with the lowest chunk id. */
3794 for (i
= 0; i
< nCaches
; i
++) {
3795 if (dev
->srCache
[i
].object
== obj
&&
3796 dev
->srCache
[i
].dirty
) {
3798 || dev
->srCache
[i
].chunkId
<
3800 cache
= &dev
->srCache
[i
];
3801 lowest
= cache
->chunkId
;
3806 if (cache
&& !cache
->locked
) {
3807 /* Write it out and free it up */
3810 yaffs_WriteChunkDataToObject(cache
->object
,
3816 cache
->object
= NULL
;
3819 } while (cache
&& chunkWritten
> 0);
3822 /* Hoosterman, disk full while writing cache out. */
3823 T(YAFFS_TRACE_ERROR
,
3824 (TSTR("yaffs tragedy: no space during cache write" TENDSTR
)));
3831 /*yaffs_FlushEntireDeviceCache(dev)
3836 void yaffs_FlushEntireDeviceCache(yaffs_Device
*dev
)
3839 int nCaches
= dev
->nShortOpCaches
;
3842 /* Find a dirty object in the cache and flush it...
3843 * until there are no further dirty objects.
3847 for( i
= 0; i
< nCaches
&& !obj
; i
++) {
3848 if (dev
->srCache
[i
].object
&&
3849 dev
->srCache
[i
].dirty
)
3850 obj
= dev
->srCache
[i
].object
;
3854 yaffs_FlushFilesChunkCache(obj
);
3861 /* Grab us a cache chunk for use.
3862 * First look for an empty one.
3863 * Then look for the least recently used non-dirty one.
3864 * Then look for the least recently used dirty one...., flush and look again.
3866 static yaffs_ChunkCache
*yaffs_GrabChunkCacheWorker(yaffs_Device
* dev
)
3872 if (dev
->nShortOpCaches
> 0) {
3873 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
3874 if (!dev
->srCache
[i
].object
)
3875 return &dev
->srCache
[i
];
3881 usage
= 0; /* just to stop the compiler grizzling */
3883 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
3884 if (!dev
->srCache
[i
].dirty
&&
3885 ((dev
->srCache
[i
].lastUse
< usage
&& theOne
>= 0) ||
3887 usage
= dev
->srCache
[i
].lastUse
;
3893 return theOne
>= 0 ? &dev
->srCache
[theOne
] : NULL
;
3900 static yaffs_ChunkCache
*yaffs_GrabChunkCache(yaffs_Device
* dev
)
3902 yaffs_ChunkCache
*cache
;
3903 yaffs_Object
*theObj
;
3908 if (dev
->nShortOpCaches
> 0) {
3909 /* Try find a non-dirty one... */
3911 cache
= yaffs_GrabChunkCacheWorker(dev
);
3914 /* They were all dirty, find the last recently used object and flush
3915 * its cache, then find again.
3916 * NB what's here is not very accurate, we actually flush the object
3917 * the last recently used page.
3920 /* With locking we can't assume we can use entry zero */
3927 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
3928 if (dev
->srCache
[i
].object
&&
3929 !dev
->srCache
[i
].locked
&&
3930 (dev
->srCache
[i
].lastUse
< usage
|| !cache
))
3932 usage
= dev
->srCache
[i
].lastUse
;
3933 theObj
= dev
->srCache
[i
].object
;
3934 cache
= &dev
->srCache
[i
];
3939 if (!cache
|| cache
->dirty
) {
3940 /* Flush and try again */
3941 yaffs_FlushFilesChunkCache(theObj
);
3942 cache
= yaffs_GrabChunkCacheWorker(dev
);
3952 /* Find a cached chunk */
3953 static yaffs_ChunkCache
*yaffs_FindChunkCache(const yaffs_Object
* obj
,
3956 yaffs_Device
*dev
= obj
->myDev
;
3958 if (dev
->nShortOpCaches
> 0) {
3959 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
3960 if (dev
->srCache
[i
].object
== obj
&&
3961 dev
->srCache
[i
].chunkId
== chunkId
) {
3964 return &dev
->srCache
[i
];
3971 /* Mark the chunk for the least recently used algorithym */
3972 static void yaffs_UseChunkCache(yaffs_Device
* dev
, yaffs_ChunkCache
* cache
,
3976 if (dev
->nShortOpCaches
> 0) {
3977 if (dev
->srLastUse
< 0 || dev
->srLastUse
> 100000000) {
3978 /* Reset the cache usages */
3980 for (i
= 1; i
< dev
->nShortOpCaches
; i
++) {
3981 dev
->srCache
[i
].lastUse
= 0;
3988 cache
->lastUse
= dev
->srLastUse
;
3996 /* Invalidate a single cache page.
3997 * Do this when a whole page gets written,
3998 * ie the short cache for this page is no longer valid.
4000 static void yaffs_InvalidateChunkCache(yaffs_Object
* object
, int chunkId
)
4002 if (object
->myDev
->nShortOpCaches
> 0) {
4003 yaffs_ChunkCache
*cache
= yaffs_FindChunkCache(object
, chunkId
);
4006 cache
->object
= NULL
;
4011 /* Invalidate all the cache pages associated with this object
4012 * Do this whenever ther file is deleted or resized.
4014 static void yaffs_InvalidateWholeChunkCache(yaffs_Object
* in
)
4017 yaffs_Device
*dev
= in
->myDev
;
4019 if (dev
->nShortOpCaches
> 0) {
4020 /* Invalidate it. */
4021 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
4022 if (dev
->srCache
[i
].object
== in
) {
4023 dev
->srCache
[i
].object
= NULL
;
4029 /*--------------------- Checkpointing --------------------*/
4032 static int yaffs_WriteCheckpointValidityMarker(yaffs_Device
*dev
,int head
)
4034 yaffs_CheckpointValidity cp
;
4036 memset(&cp
,0,sizeof(cp
));
4038 cp
.structType
= sizeof(cp
);
4039 cp
.magic
= YAFFS_MAGIC
;
4040 cp
.version
= YAFFS_CHECKPOINT_VERSION
;
4041 cp
.head
= (head
) ? 1 : 0;
4043 return (yaffs_CheckpointWrite(dev
,&cp
,sizeof(cp
)) == sizeof(cp
))?
4047 static int yaffs_ReadCheckpointValidityMarker(yaffs_Device
*dev
, int head
)
4049 yaffs_CheckpointValidity cp
;
4052 ok
= (yaffs_CheckpointRead(dev
,&cp
,sizeof(cp
)) == sizeof(cp
));
4055 ok
= (cp
.structType
== sizeof(cp
)) &&
4056 (cp
.magic
== YAFFS_MAGIC
) &&
4057 (cp
.version
== YAFFS_CHECKPOINT_VERSION
) &&
4058 (cp
.head
== ((head
) ? 1 : 0));
4062 static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice
*cp
,
4065 cp
->nErasedBlocks
= dev
->nErasedBlocks
;
4066 cp
->allocationBlock
= dev
->allocationBlock
;
4067 cp
->allocationPage
= dev
->allocationPage
;
4068 cp
->nFreeChunks
= dev
->nFreeChunks
;
4070 cp
->nDeletedFiles
= dev
->nDeletedFiles
;
4071 cp
->nUnlinkedFiles
= dev
->nUnlinkedFiles
;
4072 cp
->nBackgroundDeletions
= dev
->nBackgroundDeletions
;
4073 cp
->sequenceNumber
= dev
->sequenceNumber
;
4074 cp
->oldestDirtySequence
= dev
->oldestDirtySequence
;
4078 static void yaffs_CheckpointDeviceToDevice(yaffs_Device
*dev
,
4079 yaffs_CheckpointDevice
*cp
)
4081 dev
->nErasedBlocks
= cp
->nErasedBlocks
;
4082 dev
->allocationBlock
= cp
->allocationBlock
;
4083 dev
->allocationPage
= cp
->allocationPage
;
4084 dev
->nFreeChunks
= cp
->nFreeChunks
;
4086 dev
->nDeletedFiles
= cp
->nDeletedFiles
;
4087 dev
->nUnlinkedFiles
= cp
->nUnlinkedFiles
;
4088 dev
->nBackgroundDeletions
= cp
->nBackgroundDeletions
;
4089 dev
->sequenceNumber
= cp
->sequenceNumber
;
4090 dev
->oldestDirtySequence
= cp
->oldestDirtySequence
;
4094 static int yaffs_WriteCheckpointDevice(yaffs_Device
*dev
)
4096 yaffs_CheckpointDevice cp
;
4098 __u32 nBlocks
= (dev
->internalEndBlock
- dev
->internalStartBlock
+ 1);
4102 /* Write device runtime values*/
4103 yaffs_DeviceToCheckpointDevice(&cp
,dev
);
4104 cp
.structType
= sizeof(cp
);
4106 ok
= (yaffs_CheckpointWrite(dev
,&cp
,sizeof(cp
)) == sizeof(cp
));
4108 /* Write block info */
4110 nBytes
= nBlocks
* sizeof(yaffs_BlockInfo
);
4111 ok
= (yaffs_CheckpointWrite(dev
,dev
->blockInfo
,nBytes
) == nBytes
);
4114 /* Write chunk bits */
4116 nBytes
= nBlocks
* dev
->chunkBitmapStride
;
4117 ok
= (yaffs_CheckpointWrite(dev
,dev
->chunkBits
,nBytes
) == nBytes
);
4123 static int yaffs_ReadCheckpointDevice(yaffs_Device
*dev
)
4125 yaffs_CheckpointDevice cp
;
4127 __u32 nBlocks
= (dev
->internalEndBlock
- dev
->internalStartBlock
+ 1);
4131 ok
= (yaffs_CheckpointRead(dev
,&cp
,sizeof(cp
)) == sizeof(cp
));
4135 if(cp
.structType
!= sizeof(cp
))
4139 yaffs_CheckpointDeviceToDevice(dev
,&cp
);
4141 nBytes
= nBlocks
* sizeof(yaffs_BlockInfo
);
4143 ok
= (yaffs_CheckpointRead(dev
,dev
->blockInfo
,nBytes
) == nBytes
);
4147 nBytes
= nBlocks
* dev
->chunkBitmapStride
;
4149 ok
= (yaffs_CheckpointRead(dev
,dev
->chunkBits
,nBytes
) == nBytes
);
4154 static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject
*cp
,
4158 cp
->objectId
= obj
->objectId
;
4159 cp
->parentId
= (obj
->parent
) ? obj
->parent
->objectId
: 0;
4160 cp
->chunkId
= obj
->chunkId
;
4161 cp
->variantType
= obj
->variantType
;
4162 cp
->deleted
= obj
->deleted
;
4163 cp
->softDeleted
= obj
->softDeleted
;
4164 cp
->unlinked
= obj
->unlinked
;
4165 cp
->fake
= obj
->fake
;
4166 cp
->renameAllowed
= obj
->renameAllowed
;
4167 cp
->unlinkAllowed
= obj
->unlinkAllowed
;
4168 cp
->serial
= obj
->serial
;
4169 cp
->nDataChunks
= obj
->nDataChunks
;
4171 if(obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
)
4172 cp
->fileSizeOrEquivalentObjectId
= obj
->variant
.fileVariant
.fileSize
;
4173 else if(obj
->variantType
== YAFFS_OBJECT_TYPE_HARDLINK
)
4174 cp
->fileSizeOrEquivalentObjectId
= obj
->variant
.hardLinkVariant
.equivalentObjectId
;
4177 static void yaffs_CheckpointObjectToObject( yaffs_Object
*obj
,yaffs_CheckpointObject
*cp
)
4180 yaffs_Object
*parent
;
4182 obj
->objectId
= cp
->objectId
;
4185 parent
= yaffs_FindOrCreateObjectByNumber(
4188 YAFFS_OBJECT_TYPE_DIRECTORY
);
4193 yaffs_AddObjectToDirectory(parent
, obj
);
4195 obj
->chunkId
= cp
->chunkId
;
4196 obj
->variantType
= cp
->variantType
;
4197 obj
->deleted
= cp
->deleted
;
4198 obj
->softDeleted
= cp
->softDeleted
;
4199 obj
->unlinked
= cp
->unlinked
;
4200 obj
->fake
= cp
->fake
;
4201 obj
->renameAllowed
= cp
->renameAllowed
;
4202 obj
->unlinkAllowed
= cp
->unlinkAllowed
;
4203 obj
->serial
= cp
->serial
;
4204 obj
->nDataChunks
= cp
->nDataChunks
;
4206 if(obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
)
4207 obj
->variant
.fileVariant
.fileSize
= cp
->fileSizeOrEquivalentObjectId
;
4208 else if(obj
->variantType
== YAFFS_OBJECT_TYPE_HARDLINK
)
4209 obj
->variant
.hardLinkVariant
.equivalentObjectId
= cp
->fileSizeOrEquivalentObjectId
;
4211 if(obj
->objectId
>= YAFFS_NOBJECT_BUCKETS
)
4212 obj
->lazyLoaded
= 1;
4217 static int yaffs_CheckpointTnodeWorker(yaffs_Object
* in
, yaffs_Tnode
* tn
,
4218 __u32 level
, int chunkOffset
)
4221 yaffs_Device
*dev
= in
->myDev
;
4223 int nTnodeBytes
= (dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8;
4228 for (i
= 0; i
< YAFFS_NTNODES_INTERNAL
&& ok
; i
++){
4229 if (tn
->internal
[i
]) {
4230 ok
= yaffs_CheckpointTnodeWorker(in
,
4233 (chunkOffset
<<YAFFS_TNODES_INTERNAL_BITS
) + i
);
4236 } else if (level
== 0) {
4237 __u32 baseOffset
= chunkOffset
<< YAFFS_TNODES_LEVEL0_BITS
;
4238 /* printf("write tnode at %d\n",baseOffset); */
4239 ok
= (yaffs_CheckpointWrite(dev
,&baseOffset
,sizeof(baseOffset
)) == sizeof(baseOffset
));
4241 ok
= (yaffs_CheckpointWrite(dev
,tn
,nTnodeBytes
) == nTnodeBytes
);
4249 static int yaffs_WriteCheckpointTnodes(yaffs_Object
*obj
)
4251 __u32 endMarker
= ~0;
4254 if(obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
){
4255 ok
= yaffs_CheckpointTnodeWorker(obj
,
4256 obj
->variant
.fileVariant
.top
,
4257 obj
->variant
.fileVariant
.topLevel
,
4260 ok
= (yaffs_CheckpointWrite(obj
->myDev
,&endMarker
,sizeof(endMarker
)) ==
4267 static int yaffs_ReadCheckpointTnodes(yaffs_Object
*obj
)
4271 yaffs_Device
*dev
= obj
->myDev
;
4272 yaffs_FileStructure
*fileStructPtr
= &obj
->variant
.fileVariant
;
4276 ok
= (yaffs_CheckpointRead(dev
,&baseChunk
,sizeof(baseChunk
)) == sizeof(baseChunk
));
4278 while(ok
&& (~baseChunk
)){
4280 /* Read level 0 tnode */
4283 /* printf("read tnode at %d\n",baseChunk); */
4284 tn
= yaffs_GetTnodeRaw(dev
);
4286 ok
= (yaffs_CheckpointRead(dev
,tn
,(dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8) ==
4287 (dev
->tnodeWidth
* YAFFS_NTNODES_LEVEL0
)/8);
4292 ok
= yaffs_AddOrFindLevel0Tnode(dev
,
4300 ok
= (yaffs_CheckpointRead(dev
,&baseChunk
,sizeof(baseChunk
)) == sizeof(baseChunk
));
4304 T(YAFFS_TRACE_CHECKPOINT
,(
4305 TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR
),
4306 nread
,baseChunk
,ok
));
4312 static int yaffs_WriteCheckpointObjects(yaffs_Device
*dev
)
4315 yaffs_CheckpointObject cp
;
4318 struct list_head
*lh
;
4321 /* Iterate through the objects in each hash entry,
4322 * dumping them to the checkpointing stream.
4325 for(i
= 0; ok
&& i
< YAFFS_NOBJECT_BUCKETS
; i
++){
4326 list_for_each(lh
, &dev
->objectBucket
[i
].list
) {
4328 obj
= list_entry(lh
, yaffs_Object
, hashLink
);
4329 if (!obj
->deferedFree
) {
4330 yaffs_ObjectToCheckpointObject(&cp
,obj
);
4331 cp
.structType
= sizeof(cp
);
4333 T(YAFFS_TRACE_CHECKPOINT
,(
4334 TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR
),
4335 cp
.objectId
,cp
.parentId
,cp
.variantType
,cp
.chunkId
,(unsigned) obj
));
4337 ok
= (yaffs_CheckpointWrite(dev
,&cp
,sizeof(cp
)) == sizeof(cp
));
4339 if(ok
&& obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
){
4340 ok
= yaffs_WriteCheckpointTnodes(obj
);
4347 /* Dump end of list */
4348 memset(&cp
,0xFF,sizeof(yaffs_CheckpointObject
));
4349 cp
.structType
= sizeof(cp
);
4352 ok
= (yaffs_CheckpointWrite(dev
,&cp
,sizeof(cp
)) == sizeof(cp
));
4357 static int yaffs_ReadCheckpointObjects(yaffs_Device
*dev
)
4360 yaffs_CheckpointObject cp
;
4363 yaffs_Object
*hardList
= NULL
;
4365 while(ok
&& !done
) {
4366 ok
= (yaffs_CheckpointRead(dev
,&cp
,sizeof(cp
)) == sizeof(cp
));
4367 if(cp
.structType
!= sizeof(cp
)) {
4368 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("struct size %d instead of %d ok %d"TENDSTR
),
4369 cp
.structType
,sizeof(cp
),ok
));
4373 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR
),
4374 cp
.objectId
,cp
.parentId
,cp
.variantType
,cp
.chunkId
));
4376 if(ok
&& cp
.objectId
== ~0)
4379 obj
= yaffs_FindOrCreateObjectByNumber(dev
,cp
.objectId
, cp
.variantType
);
4381 yaffs_CheckpointObjectToObject(obj
,&cp
);
4382 if(obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
) {
4383 ok
= yaffs_ReadCheckpointTnodes(obj
);
4384 } else if(obj
->variantType
== YAFFS_OBJECT_TYPE_HARDLINK
) {
4385 obj
->hardLinks
.next
=
4386 (struct list_head
*)
4396 yaffs_HardlinkFixup(dev
,hardList
);
4401 static int yaffs_WriteCheckpointSum(yaffs_Device
*dev
)
4403 __u32 checkpointSum
;
4406 yaffs_GetCheckpointSum(dev
,&checkpointSum
);
4408 ok
= (yaffs_CheckpointWrite(dev
,&checkpointSum
,sizeof(checkpointSum
)) == sizeof(checkpointSum
));
4416 static int yaffs_ReadCheckpointSum(yaffs_Device
*dev
)
4418 __u32 checkpointSum0
;
4419 __u32 checkpointSum1
;
4422 yaffs_GetCheckpointSum(dev
,&checkpointSum0
);
4424 ok
= (yaffs_CheckpointRead(dev
,&checkpointSum1
,sizeof(checkpointSum1
)) == sizeof(checkpointSum1
));
4429 if(checkpointSum0
!= checkpointSum1
)
4436 static int yaffs_WriteCheckpointData(yaffs_Device
*dev
)
4441 if(dev
->skipCheckpointWrite
|| !dev
->isYaffs2
){
4442 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("skipping checkpoint write" TENDSTR
)));
4447 ok
= yaffs_CheckpointOpen(dev
,1);
4450 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("write checkpoint validity" TENDSTR
)));
4451 ok
= yaffs_WriteCheckpointValidityMarker(dev
,1);
4454 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("write checkpoint device" TENDSTR
)));
4455 ok
= yaffs_WriteCheckpointDevice(dev
);
4458 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("write checkpoint objects" TENDSTR
)));
4459 ok
= yaffs_WriteCheckpointObjects(dev
);
4462 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("write checkpoint validity" TENDSTR
)));
4463 ok
= yaffs_WriteCheckpointValidityMarker(dev
,0);
4467 ok
= yaffs_WriteCheckpointSum(dev
);
4471 if(!yaffs_CheckpointClose(dev
))
4475 dev
->isCheckpointed
= 1;
4477 dev
->isCheckpointed
= 0;
4479 return dev
->isCheckpointed
;
4482 static int yaffs_ReadCheckpointData(yaffs_Device
*dev
)
4486 if(dev
->skipCheckpointRead
|| !dev
->isYaffs2
){
4487 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("skipping checkpoint read" TENDSTR
)));
4492 ok
= yaffs_CheckpointOpen(dev
,0); /* open for read */
4495 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("read checkpoint validity" TENDSTR
)));
4496 ok
= yaffs_ReadCheckpointValidityMarker(dev
,1);
4499 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("read checkpoint device" TENDSTR
)));
4500 ok
= yaffs_ReadCheckpointDevice(dev
);
4503 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("read checkpoint objects" TENDSTR
)));
4504 ok
= yaffs_ReadCheckpointObjects(dev
);
4507 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("read checkpoint validity" TENDSTR
)));
4508 ok
= yaffs_ReadCheckpointValidityMarker(dev
,0);
4512 ok
= yaffs_ReadCheckpointSum(dev
);
4513 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("read checkpoint checksum %d" TENDSTR
),ok
));
4516 if(!yaffs_CheckpointClose(dev
))
4520 dev
->isCheckpointed
= 1;
4522 dev
->isCheckpointed
= 0;
4528 static void yaffs_InvalidateCheckpoint(yaffs_Device
*dev
)
4530 if(dev
->isCheckpointed
||
4531 dev
->blocksInCheckpoint
> 0){
4532 dev
->isCheckpointed
= 0;
4533 yaffs_CheckpointInvalidateStream(dev
);
4534 if(dev
->superBlock
&& dev
->markSuperBlockDirty
)
4535 dev
->markSuperBlockDirty(dev
->superBlock
);
4540 int yaffs_CheckpointSave(yaffs_Device
*dev
)
4543 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("save entry: isCheckpointed %d"TENDSTR
),dev
->isCheckpointed
));
4545 yaffs_VerifyObjects(dev
);
4546 yaffs_VerifyBlocks(dev
);
4547 yaffs_VerifyFreeChunks(dev
);
4549 if(!dev
->isCheckpointed
) {
4550 yaffs_InvalidateCheckpoint(dev
);
4551 yaffs_WriteCheckpointData(dev
);
4554 T(YAFFS_TRACE_ALWAYS
,(TSTR("save exit: isCheckpointed %d"TENDSTR
),dev
->isCheckpointed
));
4556 return dev
->isCheckpointed
;
4559 int yaffs_CheckpointRestore(yaffs_Device
*dev
)
4562 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("restore entry: isCheckpointed %d"TENDSTR
),dev
->isCheckpointed
));
4564 retval
= yaffs_ReadCheckpointData(dev
);
4566 if(dev
->isCheckpointed
){
4567 yaffs_VerifyObjects(dev
);
4568 yaffs_VerifyBlocks(dev
);
4569 yaffs_VerifyFreeChunks(dev
);
4572 T(YAFFS_TRACE_CHECKPOINT
,(TSTR("restore exit: isCheckpointed %d"TENDSTR
),dev
->isCheckpointed
));
4577 /*--------------------- File read/write ------------------------
4578 * Read and write have very similar structures.
4579 * In general the read/write has three parts to it
4580 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
4581 * Some complete chunks
4582 * An incomplete chunk to end off with
4584 * Curve-balls: the first chunk might also be the last chunk.
4587 int yaffs_ReadDataFromFile(yaffs_Object
* in
, __u8
* buffer
, loff_t offset
,
4596 yaffs_ChunkCache
*cache
;
4603 //chunk = offset / dev->nDataBytesPerChunk + 1;
4604 //start = offset % dev->nDataBytesPerChunk;
4605 yaffs_AddrToChunk(dev
,offset
,&chunk
,&start
);
4608 /* OK now check for the curveball where the start and end are in
4611 if ((start
+ n
) < dev
->nDataBytesPerChunk
) {
4614 nToCopy
= dev
->nDataBytesPerChunk
- start
;
4617 cache
= yaffs_FindChunkCache(in
, chunk
);
4619 /* If the chunk is already in the cache or it is less than a whole chunk
4620 * then use the cache (if there is caching)
4621 * else bypass the cache.
4623 if (cache
|| nToCopy
!= dev
->nDataBytesPerChunk
) {
4624 if (dev
->nShortOpCaches
> 0) {
4626 /* If we can't find the data in the cache, then load it up. */
4629 cache
= yaffs_GrabChunkCache(in
->myDev
);
4631 cache
->chunkId
= chunk
;
4634 yaffs_ReadChunkDataFromObject(in
, chunk
,
4640 yaffs_UseChunkCache(dev
, cache
, 0);
4644 #ifdef CONFIG_YAFFS_WINCE
4645 yfsd_UnlockYAFFS(TRUE
);
4647 memcpy(buffer
, &cache
->data
[start
], nToCopy
);
4649 #ifdef CONFIG_YAFFS_WINCE
4650 yfsd_LockYAFFS(TRUE
);
4654 /* Read into the local buffer then copy..*/
4657 yaffs_GetTempBuffer(dev
, __LINE__
);
4658 yaffs_ReadChunkDataFromObject(in
, chunk
,
4660 #ifdef CONFIG_YAFFS_WINCE
4661 yfsd_UnlockYAFFS(TRUE
);
4663 memcpy(buffer
, &localBuffer
[start
], nToCopy
);
4665 #ifdef CONFIG_YAFFS_WINCE
4666 yfsd_LockYAFFS(TRUE
);
4668 yaffs_ReleaseTempBuffer(dev
, localBuffer
,
4673 #ifdef CONFIG_YAFFS_WINCE
4674 __u8
*localBuffer
= yaffs_GetTempBuffer(dev
, __LINE__
);
4676 /* Under WinCE can't do direct transfer. Need to use a local buffer.
4677 * This is because we otherwise screw up WinCE's memory mapper
4679 yaffs_ReadChunkDataFromObject(in
, chunk
, localBuffer
);
4681 #ifdef CONFIG_YAFFS_WINCE
4682 yfsd_UnlockYAFFS(TRUE
);
4684 memcpy(buffer
, localBuffer
, dev
->nDataBytesPerChunk
);
4686 #ifdef CONFIG_YAFFS_WINCE
4687 yfsd_LockYAFFS(TRUE
);
4688 yaffs_ReleaseTempBuffer(dev
, localBuffer
, __LINE__
);
4692 /* A full chunk. Read directly into the supplied buffer. */
4693 yaffs_ReadChunkDataFromObject(in
, chunk
, buffer
);
4707 int yaffs_WriteDataToFile(yaffs_Object
* in
, const __u8
* buffer
, loff_t offset
,
4708 int nBytes
, int writeThrough
)
4717 int startOfWrite
= offset
;
4718 int chunkWritten
= 0;
4725 while (n
> 0 && chunkWritten
>= 0) {
4726 //chunk = offset / dev->nDataBytesPerChunk + 1;
4727 //start = offset % dev->nDataBytesPerChunk;
4728 yaffs_AddrToChunk(dev
,offset
,&chunk
,&start
);
4731 /* OK now check for the curveball where the start and end are in
4735 if ((start
+ n
) < dev
->nDataBytesPerChunk
) {
4738 /* Now folks, to calculate how many bytes to write back....
4739 * If we're overwriting and not writing to then end of file then
4740 * we need to write back as much as was there before.
4744 in
->variant
.fileVariant
.fileSize
-
4745 ((chunk
- 1) * dev
->nDataBytesPerChunk
);
4747 if (nBytesRead
> dev
->nDataBytesPerChunk
) {
4748 nBytesRead
= dev
->nDataBytesPerChunk
;
4753 (start
+ n
)) ? nBytesRead
: (start
+ n
);
4756 nToCopy
= dev
->nDataBytesPerChunk
- start
;
4757 nToWriteBack
= dev
->nDataBytesPerChunk
;
4760 if (nToCopy
!= dev
->nDataBytesPerChunk
) {
4761 /* An incomplete start or end chunk (or maybe both start and end chunk) */
4762 if (dev
->nShortOpCaches
> 0) {
4763 yaffs_ChunkCache
*cache
;
4764 /* If we can't find the data in the cache, then load the cache */
4765 cache
= yaffs_FindChunkCache(in
, chunk
);
4768 && yaffs_CheckSpaceForAllocation(in
->
4770 cache
= yaffs_GrabChunkCache(in
->myDev
);
4772 cache
->chunkId
= chunk
;
4775 yaffs_ReadChunkDataFromObject(in
, chunk
,
4781 !yaffs_CheckSpaceForAllocation(in
->myDev
)){
4782 /* Drop the cache if it was a read cache item and
4783 * no space check has been made for it.
4789 yaffs_UseChunkCache(dev
, cache
, 1);
4791 #ifdef CONFIG_YAFFS_WINCE
4792 yfsd_UnlockYAFFS(TRUE
);
4795 memcpy(&cache
->data
[start
], buffer
,
4798 #ifdef CONFIG_YAFFS_WINCE
4799 yfsd_LockYAFFS(TRUE
);
4802 cache
->nBytes
= nToWriteBack
;
4806 yaffs_WriteChunkDataToObject
4809 cache
->data
, cache
->nBytes
,
4815 chunkWritten
= -1; /* fail the write */
4818 /* An incomplete start or end chunk (or maybe both start and end chunk)
4819 * Read into the local buffer then copy, then copy over and write back.
4823 yaffs_GetTempBuffer(dev
, __LINE__
);
4825 yaffs_ReadChunkDataFromObject(in
, chunk
,
4828 #ifdef CONFIG_YAFFS_WINCE
4829 yfsd_UnlockYAFFS(TRUE
);
4832 memcpy(&localBuffer
[start
], buffer
, nToCopy
);
4834 #ifdef CONFIG_YAFFS_WINCE
4835 yfsd_LockYAFFS(TRUE
);
4838 yaffs_WriteChunkDataToObject(in
, chunk
,
4843 yaffs_ReleaseTempBuffer(dev
, localBuffer
,
4850 #ifdef CONFIG_YAFFS_WINCE
4851 /* Under WinCE can't do direct transfer. Need to use a local buffer.
4852 * This is because we otherwise screw up WinCE's memory mapper
4854 __u8
*localBuffer
= yaffs_GetTempBuffer(dev
, __LINE__
);
4855 #ifdef CONFIG_YAFFS_WINCE
4856 yfsd_UnlockYAFFS(TRUE
);
4858 memcpy(localBuffer
, buffer
, dev
->nDataBytesPerChunk
);
4859 #ifdef CONFIG_YAFFS_WINCE
4860 yfsd_LockYAFFS(TRUE
);
4863 yaffs_WriteChunkDataToObject(in
, chunk
, localBuffer
,
4864 dev
->nDataBytesPerChunk
,
4866 yaffs_ReleaseTempBuffer(dev
, localBuffer
, __LINE__
);
4868 /* A full chunk. Write directly from the supplied buffer. */
4870 yaffs_WriteChunkDataToObject(in
, chunk
, buffer
,
4871 dev
->nDataBytesPerChunk
,
4874 /* Since we've overwritten the cached data, we better invalidate it. */
4875 yaffs_InvalidateChunkCache(in
, chunk
);
4878 if (chunkWritten
>= 0) {
4887 /* Update file object */
4889 if ((startOfWrite
+ nDone
) > in
->variant
.fileVariant
.fileSize
) {
4890 in
->variant
.fileVariant
.fileSize
= (startOfWrite
+ nDone
);
4899 /* ---------------------- File resizing stuff ------------------ */
4901 static void yaffs_PruneResizedChunks(yaffs_Object
* in
, int newSize
)
4904 yaffs_Device
*dev
= in
->myDev
;
4905 int oldFileSize
= in
->variant
.fileVariant
.fileSize
;
4907 int lastDel
= 1 + (oldFileSize
- 1) / dev
->nDataBytesPerChunk
;
4909 int startDel
= 1 + (newSize
+ dev
->nDataBytesPerChunk
- 1) /
4910 dev
->nDataBytesPerChunk
;
4914 /* Delete backwards so that we don't end up with holes if
4915 * power is lost part-way through the operation.
4917 for (i
= lastDel
; i
>= startDel
; i
--) {
4918 /* NB this could be optimised somewhat,
4919 * eg. could retrieve the tags and write them without
4920 * using yaffs_DeleteChunk
4923 chunkId
= yaffs_FindAndDeleteChunkInFile(in
, i
, NULL
);
4926 (dev
->internalStartBlock
* dev
->nChunksPerBlock
)
4928 ((dev
->internalEndBlock
+
4929 1) * dev
->nChunksPerBlock
)) {
4930 T(YAFFS_TRACE_ALWAYS
,
4931 (TSTR("Found daft chunkId %d for %d" TENDSTR
),
4935 yaffs_DeleteChunk(dev
, chunkId
, 1, __LINE__
);
4942 int yaffs_ResizeFile(yaffs_Object
* in
, loff_t newSize
)
4945 int oldFileSize
= in
->variant
.fileVariant
.fileSize
;
4946 int newSizeOfPartialChunk
;
4949 yaffs_Device
*dev
= in
->myDev
;
4951 yaffs_AddrToChunk(dev
, newSize
, &newFullChunks
, &newSizeOfPartialChunk
);
4953 yaffs_FlushFilesChunkCache(in
);
4954 yaffs_InvalidateWholeChunkCache(in
);
4956 yaffs_CheckGarbageCollection(dev
);
4958 if (in
->variantType
!= YAFFS_OBJECT_TYPE_FILE
) {
4959 return yaffs_GetFileSize(in
);
4962 if (newSize
== oldFileSize
) {
4966 if (newSize
< oldFileSize
) {
4968 yaffs_PruneResizedChunks(in
, newSize
);
4970 if (newSizeOfPartialChunk
!= 0) {
4971 int lastChunk
= 1 + newFullChunks
;
4973 __u8
*localBuffer
= yaffs_GetTempBuffer(dev
, __LINE__
);
4975 /* Got to read and rewrite the last chunk with its new size and zero pad */
4976 yaffs_ReadChunkDataFromObject(in
, lastChunk
,
4979 memset(localBuffer
+ newSizeOfPartialChunk
, 0,
4980 dev
->nDataBytesPerChunk
- newSizeOfPartialChunk
);
4982 yaffs_WriteChunkDataToObject(in
, lastChunk
, localBuffer
,
4983 newSizeOfPartialChunk
, 1);
4985 yaffs_ReleaseTempBuffer(dev
, localBuffer
, __LINE__
);
4988 in
->variant
.fileVariant
.fileSize
= newSize
;
4990 yaffs_PruneFileStructure(dev
, &in
->variant
.fileVariant
);
4992 /* newsSize > oldFileSize */
4993 in
->variant
.fileVariant
.fileSize
= newSize
;
4998 /* Write a new object header.
4999 * show we've shrunk the file, if need be
5000 * Do this only if the file is not in the deleted directories.
5002 if (in
->parent
->objectId
!= YAFFS_OBJECTID_UNLINKED
&&
5003 in
->parent
->objectId
!= YAFFS_OBJECTID_DELETED
) {
5004 yaffs_UpdateObjectHeader(in
, NULL
, 0,
5005 (newSize
< oldFileSize
) ? 1 : 0, 0);
5011 loff_t
yaffs_GetFileSize(yaffs_Object
* obj
)
5013 obj
= yaffs_GetEquivalentObject(obj
);
5015 switch (obj
->variantType
) {
5016 case YAFFS_OBJECT_TYPE_FILE
:
5017 return obj
->variant
.fileVariant
.fileSize
;
5018 case YAFFS_OBJECT_TYPE_SYMLINK
:
5019 return yaffs_strlen(obj
->variant
.symLinkVariant
.alias
);
5027 int yaffs_FlushFile(yaffs_Object
* in
, int updateTime
)
5031 yaffs_FlushFilesChunkCache(in
);
5033 #ifdef CONFIG_YAFFS_WINCE
5034 yfsd_WinFileTimeNow(in
->win_mtime
);
5037 in
->yst_mtime
= Y_CURRENT_TIME
;
5043 (yaffs_UpdateObjectHeader(in
, NULL
, 0, 0, 0) >=
5044 0) ? YAFFS_OK
: YAFFS_FAIL
;
5053 static int yaffs_DoGenericObjectDeletion(yaffs_Object
* in
)
5056 /* First off, invalidate the file's data in the cache, without flushing. */
5057 yaffs_InvalidateWholeChunkCache(in
);
5059 if (in
->myDev
->isYaffs2
&& (in
->parent
!= in
->myDev
->deletedDir
)) {
5060 /* Move to the unlinked directory so we have a record that it was deleted. */
5061 yaffs_ChangeObjectName(in
, in
->myDev
->deletedDir
,"deleted", 0, 0);
5065 yaffs_RemoveObjectFromDirectory(in
);
5066 yaffs_DeleteChunk(in
->myDev
, in
->chunkId
, 1, __LINE__
);
5069 yaffs_FreeObject(in
);
5074 /* yaffs_DeleteFile deletes the whole file data
5075 * and the inode associated with the file.
5076 * It does not delete the links associated with the file.
5078 static int yaffs_UnlinkFile(yaffs_Object
* in
)
5082 int immediateDeletion
= 0;
5087 immediateDeletion
= 1;
5091 if (in
->inUse
<= 0) {
5092 immediateDeletion
= 1;
5096 if (immediateDeletion
) {
5098 yaffs_ChangeObjectName(in
, in
->myDev
->deletedDir
,
5100 T(YAFFS_TRACE_TRACING
,
5101 (TSTR("yaffs: immediate deletion of file %d" TENDSTR
),
5104 in
->myDev
->nDeletedFiles
++;
5105 if (0 && in
->myDev
->isYaffs2
) {
5106 yaffs_ResizeFile(in
, 0);
5108 yaffs_SoftDeleteFile(in
);
5111 yaffs_ChangeObjectName(in
, in
->myDev
->unlinkedDir
,
5119 int yaffs_DeleteFile(yaffs_Object
* in
)
5121 int retVal
= YAFFS_OK
;
5123 if (in
->nDataChunks
> 0) {
5124 /* Use soft deletion if there is data in the file */
5125 if (!in
->unlinked
) {
5126 retVal
= yaffs_UnlinkFile(in
);
5128 if (retVal
== YAFFS_OK
&& in
->unlinked
&& !in
->deleted
) {
5130 in
->myDev
->nDeletedFiles
++;
5131 yaffs_SoftDeleteFile(in
);
5133 return in
->deleted
? YAFFS_OK
: YAFFS_FAIL
;
5135 /* The file has no data chunks so we toss it immediately */
5136 yaffs_FreeTnode(in
->myDev
, in
->variant
.fileVariant
.top
);
5137 in
->variant
.fileVariant
.top
= NULL
;
5138 yaffs_DoGenericObjectDeletion(in
);
5144 static int yaffs_DeleteDirectory(yaffs_Object
* in
)
5146 /* First check that the directory is empty. */
5147 if (list_empty(&in
->variant
.directoryVariant
.children
)) {
5148 return yaffs_DoGenericObjectDeletion(in
);
5155 static int yaffs_DeleteSymLink(yaffs_Object
* in
)
5157 YFREE(in
->variant
.symLinkVariant
.alias
);
5159 return yaffs_DoGenericObjectDeletion(in
);
5162 static int yaffs_DeleteHardLink(yaffs_Object
* in
)
5164 /* remove this hardlink from the list assocaited with the equivalent
5167 list_del(&in
->hardLinks
);
5168 return yaffs_DoGenericObjectDeletion(in
);
5171 static void yaffs_DestroyObject(yaffs_Object
* obj
)
5173 switch (obj
->variantType
) {
5174 case YAFFS_OBJECT_TYPE_FILE
:
5175 yaffs_DeleteFile(obj
);
5177 case YAFFS_OBJECT_TYPE_DIRECTORY
:
5178 yaffs_DeleteDirectory(obj
);
5180 case YAFFS_OBJECT_TYPE_SYMLINK
:
5181 yaffs_DeleteSymLink(obj
);
5183 case YAFFS_OBJECT_TYPE_HARDLINK
:
5184 yaffs_DeleteHardLink(obj
);
5186 case YAFFS_OBJECT_TYPE_SPECIAL
:
5187 yaffs_DoGenericObjectDeletion(obj
);
5189 case YAFFS_OBJECT_TYPE_UNKNOWN
:
5190 break; /* should not happen. */
5194 static int yaffs_UnlinkWorker(yaffs_Object
* obj
)
5197 if (obj
->variantType
== YAFFS_OBJECT_TYPE_HARDLINK
) {
5198 return yaffs_DeleteHardLink(obj
);
5199 } else if (!list_empty(&obj
->hardLinks
)) {
5200 /* Curve ball: We're unlinking an object that has a hardlink.
5202 * This problem arises because we are not strictly following
5203 * The Linux link/inode model.
5205 * We can't really delete the object.
5206 * Instead, we do the following:
5207 * - Select a hardlink.
5208 * - Unhook it from the hard links
5209 * - Unhook it from its parent directory (so that the rename can work)
5210 * - Rename the object to the hardlink's name.
5211 * - Delete the hardlink
5216 YCHAR name
[YAFFS_MAX_NAME_LENGTH
+ 1];
5218 hl
= list_entry(obj
->hardLinks
.next
, yaffs_Object
, hardLinks
);
5220 list_del_init(&hl
->hardLinks
);
5221 list_del_init(&hl
->siblings
);
5223 yaffs_GetObjectName(hl
, name
, YAFFS_MAX_NAME_LENGTH
+ 1);
5225 retVal
= yaffs_ChangeObjectName(obj
, hl
->parent
, name
, 0, 0);
5227 if (retVal
== YAFFS_OK
) {
5228 retVal
= yaffs_DoGenericObjectDeletion(hl
);
5233 switch (obj
->variantType
) {
5234 case YAFFS_OBJECT_TYPE_FILE
:
5235 return yaffs_UnlinkFile(obj
);
5237 case YAFFS_OBJECT_TYPE_DIRECTORY
:
5238 return yaffs_DeleteDirectory(obj
);
5240 case YAFFS_OBJECT_TYPE_SYMLINK
:
5241 return yaffs_DeleteSymLink(obj
);
5243 case YAFFS_OBJECT_TYPE_SPECIAL
:
5244 return yaffs_DoGenericObjectDeletion(obj
);
5246 case YAFFS_OBJECT_TYPE_HARDLINK
:
5247 case YAFFS_OBJECT_TYPE_UNKNOWN
:
5255 static int yaffs_UnlinkObject( yaffs_Object
*obj
)
5258 if (obj
&& obj
->unlinkAllowed
) {
5259 return yaffs_UnlinkWorker(obj
);
5265 int yaffs_Unlink(yaffs_Object
* dir
, const YCHAR
* name
)
5269 obj
= yaffs_FindObjectByName(dir
, name
);
5270 return yaffs_UnlinkObject(obj
);
5273 /*----------------------- Initialisation Scanning ---------------------- */
5275 static void yaffs_HandleShadowedObject(yaffs_Device
* dev
, int objId
,
5276 int backwardScanning
)
5280 if (!backwardScanning
) {
5281 /* Handle YAFFS1 forward scanning case
5282 * For YAFFS1 we always do the deletion
5286 /* Handle YAFFS2 case (backward scanning)
5287 * If the shadowed object exists then ignore.
5289 if (yaffs_FindObjectByNumber(dev
, objId
)) {
5294 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
5295 * We put it in unlinked dir to be cleaned up after the scanning
5298 yaffs_FindOrCreateObjectByNumber(dev
, objId
,
5299 YAFFS_OBJECT_TYPE_FILE
);
5300 yaffs_AddObjectToDirectory(dev
->unlinkedDir
, obj
);
5301 obj
->variant
.fileVariant
.shrinkSize
= 0;
5302 obj
->valid
= 1; /* So that we don't read any other info for this file */
5312 static void yaffs_HardlinkFixup(yaffs_Device
*dev
, yaffs_Object
*hardList
)
5319 hardList
= (yaffs_Object
*) (hardList
->hardLinks
.next
);
5321 in
= yaffs_FindObjectByNumber(dev
,
5322 hl
->variant
.hardLinkVariant
.
5323 equivalentObjectId
);
5326 /* Add the hardlink pointers */
5327 hl
->variant
.hardLinkVariant
.equivalentObject
= in
;
5328 list_add(&hl
->hardLinks
, &in
->hardLinks
);
5330 /* Todo Need to report/handle this better.
5331 * Got a problem... hardlink to a non-existant object
5333 hl
->variant
.hardLinkVariant
.equivalentObject
= NULL
;
5334 INIT_LIST_HEAD(&hl
->hardLinks
);
5346 static int ybicmp(const void *a
, const void *b
){
5347 register int aseq
= ((yaffs_BlockIndex
*)a
)->seq
;
5348 register int bseq
= ((yaffs_BlockIndex
*)b
)->seq
;
5349 register int ablock
= ((yaffs_BlockIndex
*)a
)->block
;
5350 register int bblock
= ((yaffs_BlockIndex
*)b
)->block
;
5352 return ablock
- bblock
;
5358 static int yaffs_Scan(yaffs_Device
* dev
)
5360 yaffs_ExtendedTags tags
;
5365 int nBlocksToScan
= 0;
5371 yaffs_BlockState state
;
5372 yaffs_Object
*hardList
= NULL
;
5373 yaffs_BlockInfo
*bi
;
5375 yaffs_ObjectHeader
*oh
;
5377 yaffs_Object
*parent
;
5378 int nBlocks
= dev
->internalEndBlock
- dev
->internalStartBlock
+ 1;
5380 int alloc_failed
= 0;
5385 yaffs_BlockIndex
*blockIndex
= NULL
;
5387 if (dev
->isYaffs2
) {
5389 (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR
)));
5393 //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
5396 (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR
),
5397 dev
->internalStartBlock
, dev
->internalEndBlock
));
5399 chunkData
= yaffs_GetTempBuffer(dev
, __LINE__
);
5401 dev
->sequenceNumber
= YAFFS_LOWEST_SEQUENCE_NUMBER
;
5403 if (dev
->isYaffs2
) {
5404 blockIndex
= YMALLOC(nBlocks
* sizeof(yaffs_BlockIndex
));
5409 /* Scan all the blocks to determine their state */
5410 for (blk
= dev
->internalStartBlock
; blk
<= dev
->internalEndBlock
; blk
++) {
5411 bi
= yaffs_GetBlockInfo(dev
, blk
);
5412 yaffs_ClearChunkBits(dev
, blk
);
5414 bi
->softDeletions
= 0;
5416 yaffs_QueryInitialBlockState(dev
, blk
, &state
, &sequenceNumber
);
5418 bi
->blockState
= state
;
5419 bi
->sequenceNumber
= sequenceNumber
;
5421 T(YAFFS_TRACE_SCAN_DEBUG
,
5422 (TSTR("Block scanning block %d state %d seq %d" TENDSTR
), blk
,
5423 state
, sequenceNumber
));
5425 if (state
== YAFFS_BLOCK_STATE_DEAD
) {
5426 T(YAFFS_TRACE_BAD_BLOCKS
,
5427 (TSTR("block %d is bad" TENDSTR
), blk
));
5428 } else if (state
== YAFFS_BLOCK_STATE_EMPTY
) {
5429 T(YAFFS_TRACE_SCAN_DEBUG
,
5430 (TSTR("Block empty " TENDSTR
)));
5431 dev
->nErasedBlocks
++;
5432 dev
->nFreeChunks
+= dev
->nChunksPerBlock
;
5433 } else if (state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
) {
5435 /* Determine the highest sequence number */
5436 if (dev
->isYaffs2
&&
5437 sequenceNumber
>= YAFFS_LOWEST_SEQUENCE_NUMBER
&&
5438 sequenceNumber
< YAFFS_HIGHEST_SEQUENCE_NUMBER
) {
5440 blockIndex
[nBlocksToScan
].seq
= sequenceNumber
;
5441 blockIndex
[nBlocksToScan
].block
= blk
;
5445 if (sequenceNumber
>= dev
->sequenceNumber
) {
5446 dev
->sequenceNumber
= sequenceNumber
;
5448 } else if (dev
->isYaffs2
) {
5449 /* TODO: Nasty sequence number! */
5452 ("Block scanning block %d has bad sequence number %d"
5453 TENDSTR
), blk
, sequenceNumber
));
5460 * Dungy old bubble sort for now...
5462 if (dev
->isYaffs2
) {
5463 yaffs_BlockIndex temp
;
5467 for (i
= 0; i
< nBlocksToScan
; i
++)
5468 for (j
= i
+ 1; j
< nBlocksToScan
; j
++)
5469 if (blockIndex
[i
].seq
> blockIndex
[j
].seq
) {
5470 temp
= blockIndex
[j
];
5471 blockIndex
[j
] = blockIndex
[i
];
5472 blockIndex
[i
] = temp
;
5476 /* Now scan the blocks looking at the data. */
5477 if (dev
->isYaffs2
) {
5479 endIterator
= nBlocksToScan
- 1;
5480 T(YAFFS_TRACE_SCAN_DEBUG
,
5481 (TSTR("%d blocks to be scanned" TENDSTR
), nBlocksToScan
));
5483 startIterator
= dev
->internalStartBlock
;
5484 endIterator
= dev
->internalEndBlock
;
5487 /* For each block.... */
5488 for (blockIterator
= startIterator
; !alloc_failed
&& blockIterator
<= endIterator
;
5491 if (dev
->isYaffs2
) {
5492 /* get the block to scan in the correct order */
5493 blk
= blockIndex
[blockIterator
].block
;
5495 blk
= blockIterator
;
5498 bi
= yaffs_GetBlockInfo(dev
, blk
);
5499 state
= bi
->blockState
;
5503 /* For each chunk in each block that needs scanning....*/
5504 for (c
= 0; !alloc_failed
&& c
< dev
->nChunksPerBlock
&&
5505 state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
; c
++) {
5506 /* Read the tags and decide what to do */
5507 chunk
= blk
* dev
->nChunksPerBlock
+ c
;
5509 result
= yaffs_ReadChunkWithTagsFromNAND(dev
, chunk
, NULL
,
5512 /* Let's have a good look at this chunk... */
5514 if (!dev
->isYaffs2
&& tags
.chunkDeleted
) {
5520 /*T((" %d %d deleted\n",blk,c)); */
5521 } else if (!tags
.chunkUsed
) {
5522 /* An unassigned chunk in the block
5523 * This means that either the block is empty or
5524 * this is the one being allocated from
5528 /* We're looking at the first chunk in the block so the block is unused */
5529 state
= YAFFS_BLOCK_STATE_EMPTY
;
5530 dev
->nErasedBlocks
++;
5532 /* this is the block being allocated from */
5535 (" Allocating from %d %d" TENDSTR
),
5537 state
= YAFFS_BLOCK_STATE_ALLOCATING
;
5538 dev
->allocationBlock
= blk
;
5539 dev
->allocationPage
= c
;
5540 dev
->allocationBlockFinder
= blk
;
5541 /* Set it to here to encourage the allocator to go forth from here. */
5543 /* Yaffs2 sanity check:
5544 * This should be the one with the highest sequence number
5547 && (dev
->sequenceNumber
!=
5548 bi
->sequenceNumber
)) {
5549 T(YAFFS_TRACE_ALWAYS
,
5551 ("yaffs: Allocation block %d was not highest sequence id:"
5552 " block seq = %d, dev seq = %d"
5553 TENDSTR
), blk
,bi
->sequenceNumber
,dev
->sequenceNumber
));
5557 dev
->nFreeChunks
+= (dev
->nChunksPerBlock
- c
);
5558 } else if (tags
.chunkId
> 0) {
5559 /* chunkId > 0 so it is a data chunk... */
5560 unsigned int endpos
;
5562 yaffs_SetChunkBit(dev
, blk
, c
);
5565 in
= yaffs_FindOrCreateObjectByNumber(dev
,
5568 YAFFS_OBJECT_TYPE_FILE
);
5569 /* PutChunkIntoFile checks for a clash (two data chunks with
5570 * the same chunkId).
5577 if(!yaffs_PutChunkIntoFile(in
, tags
.chunkId
, chunk
,1))
5582 (tags
.chunkId
- 1) * dev
->nDataBytesPerChunk
+
5585 in
->variantType
== YAFFS_OBJECT_TYPE_FILE
5586 && in
->variant
.fileVariant
.scannedFileSize
<
5588 in
->variant
.fileVariant
.
5589 scannedFileSize
= endpos
;
5590 if (!dev
->useHeaderFileSize
) {
5591 in
->variant
.fileVariant
.
5593 in
->variant
.fileVariant
.
5598 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
5600 /* chunkId == 0, so it is an ObjectHeader.
5601 * Thus, we read in the object header and make the object
5603 yaffs_SetChunkBit(dev
, blk
, c
);
5606 result
= yaffs_ReadChunkWithTagsFromNAND(dev
, chunk
,
5610 oh
= (yaffs_ObjectHeader
*) chunkData
;
5612 in
= yaffs_FindObjectByNumber(dev
,
5614 if (in
&& in
->variantType
!= oh
->type
) {
5615 /* This should not happen, but somehow
5616 * Wev'e ended up with an objectId that has been reused but not yet
5617 * deleted, and worse still it has changed type. Delete the old object.
5620 yaffs_DestroyObject(in
);
5625 in
= yaffs_FindOrCreateObjectByNumber(dev
,
5633 if (in
&& oh
->shadowsObject
> 0) {
5634 yaffs_HandleShadowedObject(dev
,
5640 if (in
&& in
->valid
) {
5641 /* We have already filled this one. We have a duplicate and need to resolve it. */
5643 unsigned existingSerial
= in
->serial
;
5644 unsigned newSerial
= tags
.serialNumber
;
5646 if (dev
->isYaffs2
||
5647 ((existingSerial
+ 1) & 3) ==
5649 /* Use new one - destroy the exisiting one */
5650 yaffs_DeleteChunk(dev
,
5655 /* Use existing - destroy this one. */
5656 yaffs_DeleteChunk(dev
, chunk
, 1,
5661 if (in
&& !in
->valid
&&
5662 (tags
.objectId
== YAFFS_OBJECTID_ROOT
||
5663 tags
.objectId
== YAFFS_OBJECTID_LOSTNFOUND
)) {
5664 /* We only load some info, don't fiddle with directory structure */
5666 in
->variantType
= oh
->type
;
5668 in
->yst_mode
= oh
->yst_mode
;
5669 #ifdef CONFIG_YAFFS_WINCE
5670 in
->win_atime
[0] = oh
->win_atime
[0];
5671 in
->win_ctime
[0] = oh
->win_ctime
[0];
5672 in
->win_mtime
[0] = oh
->win_mtime
[0];
5673 in
->win_atime
[1] = oh
->win_atime
[1];
5674 in
->win_ctime
[1] = oh
->win_ctime
[1];
5675 in
->win_mtime
[1] = oh
->win_mtime
[1];
5677 in
->yst_uid
= oh
->yst_uid
;
5678 in
->yst_gid
= oh
->yst_gid
;
5679 in
->yst_atime
= oh
->yst_atime
;
5680 in
->yst_mtime
= oh
->yst_mtime
;
5681 in
->yst_ctime
= oh
->yst_ctime
;
5682 in
->yst_rdev
= oh
->yst_rdev
;
5684 in
->chunkId
= chunk
;
5686 } else if (in
&& !in
->valid
) {
5687 /* we need to load this info */
5690 in
->variantType
= oh
->type
;
5692 in
->yst_mode
= oh
->yst_mode
;
5693 #ifdef CONFIG_YAFFS_WINCE
5694 in
->win_atime
[0] = oh
->win_atime
[0];
5695 in
->win_ctime
[0] = oh
->win_ctime
[0];
5696 in
->win_mtime
[0] = oh
->win_mtime
[0];
5697 in
->win_atime
[1] = oh
->win_atime
[1];
5698 in
->win_ctime
[1] = oh
->win_ctime
[1];
5699 in
->win_mtime
[1] = oh
->win_mtime
[1];
5701 in
->yst_uid
= oh
->yst_uid
;
5702 in
->yst_gid
= oh
->yst_gid
;
5703 in
->yst_atime
= oh
->yst_atime
;
5704 in
->yst_mtime
= oh
->yst_mtime
;
5705 in
->yst_ctime
= oh
->yst_ctime
;
5706 in
->yst_rdev
= oh
->yst_rdev
;
5708 in
->chunkId
= chunk
;
5710 yaffs_SetObjectName(in
, oh
->name
);
5713 /* directory stuff...
5718 yaffs_FindOrCreateObjectByNumber
5719 (dev
, oh
->parentObjectId
,
5720 YAFFS_OBJECT_TYPE_DIRECTORY
);
5721 if (parent
->variantType
==
5722 YAFFS_OBJECT_TYPE_UNKNOWN
) {
5723 /* Set up as a directory */
5724 parent
->variantType
=
5725 YAFFS_OBJECT_TYPE_DIRECTORY
;
5726 INIT_LIST_HEAD(&parent
->variant
.
5729 } else if (parent
->variantType
!=
5730 YAFFS_OBJECT_TYPE_DIRECTORY
)
5732 /* Hoosterman, another problem....
5733 * We're trying to use a non-directory as a directory
5736 T(YAFFS_TRACE_ERROR
,
5738 ("yaffs tragedy: attempting to use non-directory as"
5739 " a directory in scan. Put in lost+found."
5741 parent
= dev
->lostNFoundDir
;
5744 yaffs_AddObjectToDirectory(parent
, in
);
5746 if (0 && (parent
== dev
->deletedDir
||
5747 parent
== dev
->unlinkedDir
)) {
5748 in
->deleted
= 1; /* If it is unlinked at start up then it wants deleting */
5749 dev
->nDeletedFiles
++;
5751 /* Note re hardlinks.
5752 * Since we might scan a hardlink before its equivalent object is scanned
5753 * we put them all in a list.
5754 * After scanning is complete, we should have all the objects, so we run through this
5755 * list and fix up all the chains.
5758 switch (in
->variantType
) {
5759 case YAFFS_OBJECT_TYPE_UNKNOWN
:
5760 /* Todo got a problem */
5762 case YAFFS_OBJECT_TYPE_FILE
:
5765 /* Prune back the shrunken chunks */
5766 yaffs_PruneResizedChunks
5768 /* Mark the block as having a shrinkHeader */
5769 bi
->hasShrinkHeader
= 1;
5772 if (dev
->useHeaderFileSize
)
5774 in
->variant
.fileVariant
.
5779 case YAFFS_OBJECT_TYPE_HARDLINK
:
5780 in
->variant
.hardLinkVariant
.
5781 equivalentObjectId
=
5782 oh
->equivalentObjectId
;
5783 in
->hardLinks
.next
=
5784 (struct list_head
*)
5788 case YAFFS_OBJECT_TYPE_DIRECTORY
:
5791 case YAFFS_OBJECT_TYPE_SPECIAL
:
5794 case YAFFS_OBJECT_TYPE_SYMLINK
:
5795 in
->variant
.symLinkVariant
.alias
=
5796 yaffs_CloneString(oh
->alias
);
5797 if(!in
->variant
.symLinkVariant
.alias
)
5802 if (parent
== dev
->deletedDir
) {
5803 yaffs_DestroyObject(in
);
5804 bi
->hasShrinkHeader
= 1;
5810 if (state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
) {
5811 /* If we got this far while scanning, then the block is fully allocated.*/
5812 state
= YAFFS_BLOCK_STATE_FULL
;
5815 bi
->blockState
= state
;
5817 /* Now let's see if it was dirty */
5818 if (bi
->pagesInUse
== 0 &&
5819 !bi
->hasShrinkHeader
&&
5820 bi
->blockState
== YAFFS_BLOCK_STATE_FULL
) {
5821 yaffs_BlockBecameDirty(dev
, blk
);
5831 /* Ok, we've done all the scanning.
5832 * Fix up the hard link chains.
5833 * We should now have scanned all the objects, now it's time to add these
5837 yaffs_HardlinkFixup(dev
,hardList
);
5839 /* Handle the unlinked files. Since they were left in an unlinked state we should
5843 struct list_head
*i
;
5844 struct list_head
*n
;
5847 /* Soft delete all the unlinked files */
5848 list_for_each_safe(i
, n
,
5849 &dev
->unlinkedDir
->variant
.directoryVariant
.
5852 l
= list_entry(i
, yaffs_Object
, siblings
);
5853 yaffs_DestroyObject(l
);
5858 yaffs_ReleaseTempBuffer(dev
, chunkData
, __LINE__
);
5864 T(YAFFS_TRACE_SCAN
, (TSTR("yaffs_Scan ends" TENDSTR
)));
5870 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object
*in
)
5873 yaffs_ObjectHeader
*oh
;
5874 yaffs_Device
*dev
= in
->myDev
;
5875 yaffs_ExtendedTags tags
;
5877 int alloc_failed
= 0;
5883 T(YAFFS_TRACE_SCAN
,(TSTR("details for object %d %s loaded" TENDSTR
),
5885 in
->lazyLoaded
? "not yet" : "already"));
5890 chunkData
= yaffs_GetTempBuffer(dev
, __LINE__
);
5892 result
= yaffs_ReadChunkWithTagsFromNAND(dev
,in
->chunkId
,chunkData
,&tags
);
5893 oh
= (yaffs_ObjectHeader
*) chunkData
;
5895 in
->yst_mode
= oh
->yst_mode
;
5896 #ifdef CONFIG_YAFFS_WINCE
5897 in
->win_atime
[0] = oh
->win_atime
[0];
5898 in
->win_ctime
[0] = oh
->win_ctime
[0];
5899 in
->win_mtime
[0] = oh
->win_mtime
[0];
5900 in
->win_atime
[1] = oh
->win_atime
[1];
5901 in
->win_ctime
[1] = oh
->win_ctime
[1];
5902 in
->win_mtime
[1] = oh
->win_mtime
[1];
5904 in
->yst_uid
= oh
->yst_uid
;
5905 in
->yst_gid
= oh
->yst_gid
;
5906 in
->yst_atime
= oh
->yst_atime
;
5907 in
->yst_mtime
= oh
->yst_mtime
;
5908 in
->yst_ctime
= oh
->yst_ctime
;
5909 in
->yst_rdev
= oh
->yst_rdev
;
5912 yaffs_SetObjectName(in
, oh
->name
);
5914 if(in
->variantType
== YAFFS_OBJECT_TYPE_SYMLINK
){
5915 in
->variant
.symLinkVariant
.alias
=
5916 yaffs_CloneString(oh
->alias
);
5917 if(!in
->variant
.symLinkVariant
.alias
)
5918 alloc_failed
= 1; /* Not returned to caller */
5921 yaffs_ReleaseTempBuffer(dev
,chunkData
, __LINE__
);
5925 static int yaffs_ScanBackwards(yaffs_Device
* dev
)
5927 yaffs_ExtendedTags tags
;
5932 int nBlocksToScan
= 0;
5938 yaffs_BlockState state
;
5939 yaffs_Object
*hardList
= NULL
;
5940 yaffs_BlockInfo
*bi
;
5942 yaffs_ObjectHeader
*oh
;
5944 yaffs_Object
*parent
;
5945 int nBlocks
= dev
->internalEndBlock
- dev
->internalStartBlock
+ 1;
5951 int foundChunksInBlock
;
5952 int equivalentObjectId
;
5953 int alloc_failed
= 0;
5956 yaffs_BlockIndex
*blockIndex
= NULL
;
5957 int altBlockIndex
= 0;
5959 if (!dev
->isYaffs2
) {
5961 (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR
)));
5967 ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
5968 TENDSTR
), dev
->internalStartBlock
, dev
->internalEndBlock
));
5971 dev
->sequenceNumber
= YAFFS_LOWEST_SEQUENCE_NUMBER
;
5973 blockIndex
= YMALLOC(nBlocks
* sizeof(yaffs_BlockIndex
));
5976 blockIndex
= YMALLOC_ALT(nBlocks
* sizeof(yaffs_BlockIndex
));
5982 (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR
)));
5986 dev
->blocksInCheckpoint
= 0;
5988 chunkData
= yaffs_GetTempBuffer(dev
, __LINE__
);
5990 /* Scan all the blocks to determine their state */
5991 for (blk
= dev
->internalStartBlock
; blk
<= dev
->internalEndBlock
; blk
++) {
5992 bi
= yaffs_GetBlockInfo(dev
, blk
);
5993 yaffs_ClearChunkBits(dev
, blk
);
5995 bi
->softDeletions
= 0;
5997 yaffs_QueryInitialBlockState(dev
, blk
, &state
, &sequenceNumber
);
5999 bi
->blockState
= state
;
6000 bi
->sequenceNumber
= sequenceNumber
;
6002 if(bi
->sequenceNumber
== YAFFS_SEQUENCE_CHECKPOINT_DATA
)
6003 bi
->blockState
= state
= YAFFS_BLOCK_STATE_CHECKPOINT
;
6005 T(YAFFS_TRACE_SCAN_DEBUG
,
6006 (TSTR("Block scanning block %d state %d seq %d" TENDSTR
), blk
,
6007 state
, sequenceNumber
));
6010 if(state
== YAFFS_BLOCK_STATE_CHECKPOINT
){
6011 dev
->blocksInCheckpoint
++;
6013 } else if (state
== YAFFS_BLOCK_STATE_DEAD
) {
6014 T(YAFFS_TRACE_BAD_BLOCKS
,
6015 (TSTR("block %d is bad" TENDSTR
), blk
));
6016 } else if (state
== YAFFS_BLOCK_STATE_EMPTY
) {
6017 T(YAFFS_TRACE_SCAN_DEBUG
,
6018 (TSTR("Block empty " TENDSTR
)));
6019 dev
->nErasedBlocks
++;
6020 dev
->nFreeChunks
+= dev
->nChunksPerBlock
;
6021 } else if (state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
) {
6023 /* Determine the highest sequence number */
6024 if (dev
->isYaffs2
&&
6025 sequenceNumber
>= YAFFS_LOWEST_SEQUENCE_NUMBER
&&
6026 sequenceNumber
< YAFFS_HIGHEST_SEQUENCE_NUMBER
) {
6028 blockIndex
[nBlocksToScan
].seq
= sequenceNumber
;
6029 blockIndex
[nBlocksToScan
].block
= blk
;
6033 if (sequenceNumber
>= dev
->sequenceNumber
) {
6034 dev
->sequenceNumber
= sequenceNumber
;
6036 } else if (dev
->isYaffs2
) {
6037 /* TODO: Nasty sequence number! */
6040 ("Block scanning block %d has bad sequence number %d"
6041 TENDSTR
), blk
, sequenceNumber
));
6048 (TSTR("%d blocks to be sorted..." TENDSTR
), nBlocksToScan
));
6054 /* Sort the blocks */
6055 #ifndef CONFIG_YAFFS_USE_OWN_SORT
6056 yaffs_qsort(blockIndex
, nBlocksToScan
,
6057 sizeof(yaffs_BlockIndex
), ybicmp
);
6060 /* Dungy old bubble sort... */
6062 yaffs_BlockIndex temp
;
6066 for (i
= 0; i
< nBlocksToScan
; i
++)
6067 for (j
= i
+ 1; j
< nBlocksToScan
; j
++)
6068 if (blockIndex
[i
].seq
> blockIndex
[j
].seq
) {
6069 temp
= blockIndex
[j
];
6070 blockIndex
[j
] = blockIndex
[i
];
6071 blockIndex
[i
] = temp
;
6078 T(YAFFS_TRACE_SCAN
, (TSTR("...done" TENDSTR
)));
6080 /* Now scan the blocks looking at the data. */
6082 endIterator
= nBlocksToScan
- 1;
6083 T(YAFFS_TRACE_SCAN_DEBUG
,
6084 (TSTR("%d blocks to be scanned" TENDSTR
), nBlocksToScan
));
6086 /* For each block.... backwards */
6087 for (blockIterator
= endIterator
; !alloc_failed
&& blockIterator
>= startIterator
;
6089 /* Cooperative multitasking! This loop can run for so
6090 long that watchdog timers expire. */
6093 /* get the block to scan in the correct order */
6094 blk
= blockIndex
[blockIterator
].block
;
6096 bi
= yaffs_GetBlockInfo(dev
, blk
);
6099 state
= bi
->blockState
;
6103 /* For each chunk in each block that needs scanning.... */
6104 foundChunksInBlock
= 0;
6105 for (c
= dev
->nChunksPerBlock
- 1;
6106 !alloc_failed
&& c
>= 0 &&
6107 (state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
||
6108 state
== YAFFS_BLOCK_STATE_ALLOCATING
); c
--) {
6109 /* Scan backwards...
6110 * Read the tags and decide what to do
6113 chunk
= blk
* dev
->nChunksPerBlock
+ c
;
6115 result
= yaffs_ReadChunkWithTagsFromNAND(dev
, chunk
, NULL
,
6118 /* Let's have a good look at this chunk... */
6120 if (!tags
.chunkUsed
) {
6121 /* An unassigned chunk in the block.
6122 * If there are used chunks after this one, then
6123 * it is a chunk that was skipped due to failing the erased
6124 * check. Just skip it so that it can be deleted.
6125 * But, more typically, We get here when this is an unallocated
6126 * chunk and his means that either the block is empty or
6127 * this is the one being allocated from
6130 if(foundChunksInBlock
)
6132 /* This is a chunk that was skipped due to failing the erased check */
6134 } else if (c
== 0) {
6135 /* We're looking at the first chunk in the block so the block is unused */
6136 state
= YAFFS_BLOCK_STATE_EMPTY
;
6137 dev
->nErasedBlocks
++;
6139 if (state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
||
6140 state
== YAFFS_BLOCK_STATE_ALLOCATING
) {
6141 if(dev
->sequenceNumber
== bi
->sequenceNumber
) {
6142 /* this is the block being allocated from */
6146 (" Allocating from %d %d"
6149 state
= YAFFS_BLOCK_STATE_ALLOCATING
;
6150 dev
->allocationBlock
= blk
;
6151 dev
->allocationPage
= c
;
6152 dev
->allocationBlockFinder
= blk
;
6155 /* This is a partially written block that is not
6156 * the current allocation block. This block must have
6157 * had a write failure, so set up for retirement.
6160 bi
->needsRetiring
= 1;
6161 bi
->gcPrioritise
= 1;
6163 T(YAFFS_TRACE_ALWAYS
,
6164 (TSTR("Partially written block %d being set for retirement" TENDSTR
),
6174 } else if (tags
.chunkId
> 0) {
6175 /* chunkId > 0 so it is a data chunk... */
6176 unsigned int endpos
;
6178 (tags
.chunkId
- 1) * dev
->nDataBytesPerChunk
;
6180 foundChunksInBlock
= 1;
6183 yaffs_SetChunkBit(dev
, blk
, c
);
6186 in
= yaffs_FindOrCreateObjectByNumber(dev
,
6189 YAFFS_OBJECT_TYPE_FILE
);
6196 in
->variantType
== YAFFS_OBJECT_TYPE_FILE
6198 in
->variant
.fileVariant
.shrinkSize
) {
6199 /* This has not been invalidated by a resize */
6200 if(!yaffs_PutChunkIntoFile(in
, tags
.chunkId
,
6205 /* File size is calculated by looking at the data chunks if we have not
6206 * seen an object header yet. Stop this practice once we find an object header.
6210 1) * dev
->nDataBytesPerChunk
+
6213 if (!in
->valid
&& /* have not got an object header yet */
6214 in
->variant
.fileVariant
.
6215 scannedFileSize
< endpos
) {
6216 in
->variant
.fileVariant
.
6217 scannedFileSize
= endpos
;
6218 in
->variant
.fileVariant
.
6220 in
->variant
.fileVariant
.
6225 /* This chunk has been invalidated by a resize, so delete */
6226 yaffs_DeleteChunk(dev
, chunk
, 1, __LINE__
);
6230 /* chunkId == 0, so it is an ObjectHeader.
6231 * Thus, we read in the object header and make the object
6233 foundChunksInBlock
= 1;
6235 yaffs_SetChunkBit(dev
, blk
, c
);
6241 if (tags
.extraHeaderInfoAvailable
) {
6242 in
= yaffs_FindOrCreateObjectByNumber
6243 (dev
, tags
.objectId
,
6244 tags
.extraObjectType
);
6248 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
6251 tags
.extraShadows
||
6253 (tags
.objectId
== YAFFS_OBJECTID_ROOT
||
6254 tags
.objectId
== YAFFS_OBJECTID_LOSTNFOUND
))
6257 /* If we don't have valid info then we need to read the chunk
6258 * TODO In future we can probably defer reading the chunk and
6259 * living with invalid data until needed.
6262 result
= yaffs_ReadChunkWithTagsFromNAND(dev
,
6267 oh
= (yaffs_ObjectHeader
*) chunkData
;
6270 in
= yaffs_FindOrCreateObjectByNumber(dev
, tags
.objectId
, oh
->type
);
6275 /* TODO Hoosterman we have a problem! */
6276 T(YAFFS_TRACE_ERROR
,
6278 ("yaffs tragedy: Could not make object for object %d "
6279 "at chunk %d during scan"
6280 TENDSTR
), tags
.objectId
, chunk
));
6285 /* We have already filled this one.
6286 * We have a duplicate that will be discarded, but
6287 * we first have to suck out resize info if it is a file.
6290 if ((in
->variantType
== YAFFS_OBJECT_TYPE_FILE
) &&
6292 oh
-> type
== YAFFS_OBJECT_TYPE_FILE
)||
6293 (tags
.extraHeaderInfoAvailable
&&
6294 tags
.extraObjectType
== YAFFS_OBJECT_TYPE_FILE
))
6297 (oh
) ? oh
->fileSize
: tags
.
6299 __u32 parentObjectId
=
6301 parentObjectId
: tags
.
6302 extraParentObjectId
;
6304 (oh
) ? oh
->isShrink
: tags
.
6305 extraIsShrinkHeader
;
6307 /* If it is deleted (unlinked at start also means deleted)
6308 * we treat the file size as being zeroed at this point.
6310 if (parentObjectId
==
6311 YAFFS_OBJECTID_DELETED
6312 || parentObjectId
==
6313 YAFFS_OBJECTID_UNLINKED
) {
6319 in
->variant
.fileVariant
.
6320 shrinkSize
> thisSize
) {
6321 in
->variant
.fileVariant
.
6327 bi
->hasShrinkHeader
= 1;
6331 /* Use existing - destroy this one. */
6332 yaffs_DeleteChunk(dev
, chunk
, 1, __LINE__
);
6337 (tags
.objectId
== YAFFS_OBJECTID_ROOT
||
6339 YAFFS_OBJECTID_LOSTNFOUND
)) {
6340 /* We only load some info, don't fiddle with directory structure */
6344 in
->variantType
= oh
->type
;
6346 in
->yst_mode
= oh
->yst_mode
;
6347 #ifdef CONFIG_YAFFS_WINCE
6348 in
->win_atime
[0] = oh
->win_atime
[0];
6349 in
->win_ctime
[0] = oh
->win_ctime
[0];
6350 in
->win_mtime
[0] = oh
->win_mtime
[0];
6351 in
->win_atime
[1] = oh
->win_atime
[1];
6352 in
->win_ctime
[1] = oh
->win_ctime
[1];
6353 in
->win_mtime
[1] = oh
->win_mtime
[1];
6355 in
->yst_uid
= oh
->yst_uid
;
6356 in
->yst_gid
= oh
->yst_gid
;
6357 in
->yst_atime
= oh
->yst_atime
;
6358 in
->yst_mtime
= oh
->yst_mtime
;
6359 in
->yst_ctime
= oh
->yst_ctime
;
6360 in
->yst_rdev
= oh
->yst_rdev
;
6364 in
->variantType
= tags
.extraObjectType
;
6368 in
->chunkId
= chunk
;
6370 } else if (!in
->valid
) {
6371 /* we need to load this info */
6374 in
->chunkId
= chunk
;
6377 in
->variantType
= oh
->type
;
6379 in
->yst_mode
= oh
->yst_mode
;
6380 #ifdef CONFIG_YAFFS_WINCE
6381 in
->win_atime
[0] = oh
->win_atime
[0];
6382 in
->win_ctime
[0] = oh
->win_ctime
[0];
6383 in
->win_mtime
[0] = oh
->win_mtime
[0];
6384 in
->win_atime
[1] = oh
->win_atime
[1];
6385 in
->win_ctime
[1] = oh
->win_ctime
[1];
6386 in
->win_mtime
[1] = oh
->win_mtime
[1];
6388 in
->yst_uid
= oh
->yst_uid
;
6389 in
->yst_gid
= oh
->yst_gid
;
6390 in
->yst_atime
= oh
->yst_atime
;
6391 in
->yst_mtime
= oh
->yst_mtime
;
6392 in
->yst_ctime
= oh
->yst_ctime
;
6393 in
->yst_rdev
= oh
->yst_rdev
;
6396 if (oh
->shadowsObject
> 0)
6397 yaffs_HandleShadowedObject(dev
,
6403 yaffs_SetObjectName(in
, oh
->name
);
6405 yaffs_FindOrCreateObjectByNumber
6406 (dev
, oh
->parentObjectId
,
6407 YAFFS_OBJECT_TYPE_DIRECTORY
);
6409 fileSize
= oh
->fileSize
;
6410 isShrink
= oh
->isShrink
;
6411 equivalentObjectId
= oh
->equivalentObjectId
;
6415 in
->variantType
= tags
.extraObjectType
;
6417 yaffs_FindOrCreateObjectByNumber
6418 (dev
, tags
.extraParentObjectId
,
6419 YAFFS_OBJECT_TYPE_DIRECTORY
);
6420 fileSize
= tags
.extraFileLength
;
6421 isShrink
= tags
.extraIsShrinkHeader
;
6422 equivalentObjectId
= tags
.extraEquivalentObjectId
;
6428 /* directory stuff...
6432 if (parent
->variantType
==
6433 YAFFS_OBJECT_TYPE_UNKNOWN
) {
6434 /* Set up as a directory */
6435 parent
->variantType
=
6436 YAFFS_OBJECT_TYPE_DIRECTORY
;
6437 INIT_LIST_HEAD(&parent
->variant
.
6440 } else if (parent
->variantType
!=
6441 YAFFS_OBJECT_TYPE_DIRECTORY
)
6443 /* Hoosterman, another problem....
6444 * We're trying to use a non-directory as a directory
6447 T(YAFFS_TRACE_ERROR
,
6449 ("yaffs tragedy: attempting to use non-directory as"
6450 " a directory in scan. Put in lost+found."
6452 parent
= dev
->lostNFoundDir
;
6455 yaffs_AddObjectToDirectory(parent
, in
);
6457 itsUnlinked
= (parent
== dev
->deletedDir
) ||
6458 (parent
== dev
->unlinkedDir
);
6461 /* Mark the block as having a shrinkHeader */
6462 bi
->hasShrinkHeader
= 1;
6465 /* Note re hardlinks.
6466 * Since we might scan a hardlink before its equivalent object is scanned
6467 * we put them all in a list.
6468 * After scanning is complete, we should have all the objects, so we run
6469 * through this list and fix up all the chains.
6472 switch (in
->variantType
) {
6473 case YAFFS_OBJECT_TYPE_UNKNOWN
:
6474 /* Todo got a problem */
6476 case YAFFS_OBJECT_TYPE_FILE
:
6478 if (in
->variant
.fileVariant
.
6479 scannedFileSize
< fileSize
) {
6480 /* This covers the case where the file size is greater
6481 * than where the data is
6482 * This will happen if the file is resized to be larger
6483 * than its current data extents.
6485 in
->variant
.fileVariant
.fileSize
= fileSize
;
6486 in
->variant
.fileVariant
.scannedFileSize
=
6487 in
->variant
.fileVariant
.fileSize
;
6491 in
->variant
.fileVariant
.shrinkSize
> fileSize
) {
6492 in
->variant
.fileVariant
.shrinkSize
= fileSize
;
6496 case YAFFS_OBJECT_TYPE_HARDLINK
:
6498 in
->variant
.hardLinkVariant
.equivalentObjectId
=
6500 in
->hardLinks
.next
=
6501 (struct list_head
*) hardList
;
6505 case YAFFS_OBJECT_TYPE_DIRECTORY
:
6508 case YAFFS_OBJECT_TYPE_SPECIAL
:
6511 case YAFFS_OBJECT_TYPE_SYMLINK
:
6513 in
->variant
.symLinkVariant
.alias
=
6514 yaffs_CloneString(oh
->
6516 if(!in
->variant
.symLinkVariant
.alias
)
6526 } /* End of scanning for each chunk */
6528 if (state
== YAFFS_BLOCK_STATE_NEEDS_SCANNING
) {
6529 /* If we got this far while scanning, then the block is fully allocated. */
6530 state
= YAFFS_BLOCK_STATE_FULL
;
6533 bi
->blockState
= state
;
6535 /* Now let's see if it was dirty */
6536 if (bi
->pagesInUse
== 0 &&
6537 !bi
->hasShrinkHeader
&&
6538 bi
->blockState
== YAFFS_BLOCK_STATE_FULL
) {
6539 yaffs_BlockBecameDirty(dev
, blk
);
6545 YFREE_ALT(blockIndex
);
6549 /* Ok, we've done all the scanning.
6550 * Fix up the hard link chains.
6551 * We should now have scanned all the objects, now it's time to add these
6554 yaffs_HardlinkFixup(dev
,hardList
);
6558 * Sort out state of unlinked and deleted objects.
6561 struct list_head
*i
;
6562 struct list_head
*n
;
6566 /* Soft delete all the unlinked files */
6567 list_for_each_safe(i
, n
,
6568 &dev
->unlinkedDir
->variant
.directoryVariant
.
6571 l
= list_entry(i
, yaffs_Object
, siblings
);
6572 yaffs_DestroyObject(l
);
6576 /* Soft delete all the deletedDir files */
6577 list_for_each_safe(i
, n
,
6578 &dev
->deletedDir
->variant
.directoryVariant
.
6581 l
= list_entry(i
, yaffs_Object
, siblings
);
6582 yaffs_DestroyObject(l
);
6588 yaffs_ReleaseTempBuffer(dev
, chunkData
, __LINE__
);
6594 T(YAFFS_TRACE_SCAN
, (TSTR("yaffs_ScanBackwards ends" TENDSTR
)));
6599 /*------------------------------ Directory Functions ----------------------------- */
6601 static void yaffs_RemoveObjectFromDirectory(yaffs_Object
* obj
)
6603 yaffs_Device
*dev
= obj
->myDev
;
6605 if(dev
&& dev
->removeObjectCallback
)
6606 dev
->removeObjectCallback(obj
);
6608 list_del_init(&obj
->siblings
);
6613 static void yaffs_AddObjectToDirectory(yaffs_Object
* directory
,
6618 T(YAFFS_TRACE_ALWAYS
,
6620 ("tragedy: Trying to add an object to a null pointer directory"
6624 if (directory
->variantType
!= YAFFS_OBJECT_TYPE_DIRECTORY
) {
6625 T(YAFFS_TRACE_ALWAYS
,
6627 ("tragedy: Trying to add an object to a non-directory"
6632 if (obj
->siblings
.prev
== NULL
) {
6633 /* Not initialised */
6634 INIT_LIST_HEAD(&obj
->siblings
);
6636 } else if (!list_empty(&obj
->siblings
)) {
6637 /* If it is holed up somewhere else, un hook it */
6638 yaffs_RemoveObjectFromDirectory(obj
);
6641 list_add(&obj
->siblings
, &directory
->variant
.directoryVariant
.children
);
6642 obj
->parent
= directory
;
6644 if (directory
== obj
->myDev
->unlinkedDir
6645 || directory
== obj
->myDev
->deletedDir
) {
6647 obj
->myDev
->nUnlinkedFiles
++;
6648 obj
->renameAllowed
= 0;
6652 yaffs_Object
*yaffs_FindObjectByName(yaffs_Object
* directory
,
6657 struct list_head
*i
;
6658 YCHAR buffer
[YAFFS_MAX_NAME_LENGTH
+ 1];
6667 T(YAFFS_TRACE_ALWAYS
,
6669 ("tragedy: yaffs_FindObjectByName: null pointer directory"
6673 if (directory
->variantType
!= YAFFS_OBJECT_TYPE_DIRECTORY
) {
6674 T(YAFFS_TRACE_ALWAYS
,
6676 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR
)));
6680 sum
= yaffs_CalcNameSum(name
);
6682 list_for_each(i
, &directory
->variant
.directoryVariant
.children
) {
6684 l
= list_entry(i
, yaffs_Object
, siblings
);
6686 yaffs_CheckObjectDetailsLoaded(l
);
6688 /* Special case for lost-n-found */
6689 if (l
->objectId
== YAFFS_OBJECTID_LOSTNFOUND
) {
6690 if (yaffs_strcmp(name
, YAFFS_LOSTNFOUND_NAME
) == 0) {
6693 } else if (yaffs_SumCompare(l
->sum
, sum
) || l
->chunkId
<= 0)
6695 /* LostnFound cunk called Objxxx
6698 yaffs_GetObjectName(l
, buffer
,
6699 YAFFS_MAX_NAME_LENGTH
);
6700 if (yaffs_strncmp(name
, buffer
,YAFFS_MAX_NAME_LENGTH
) == 0) {
6713 int yaffs_ApplyToDirectoryChildren(yaffs_Object
* theDir
,
6714 int (*fn
) (yaffs_Object
*))
6716 struct list_head
*i
;
6720 T(YAFFS_TRACE_ALWAYS
,
6722 ("tragedy: yaffs_FindObjectByName: null pointer directory"
6726 if (theDir
->variantType
!= YAFFS_OBJECT_TYPE_DIRECTORY
) {
6727 T(YAFFS_TRACE_ALWAYS
,
6729 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR
)));
6733 list_for_each(i
, &theDir
->variant
.directoryVariant
.children
) {
6735 l
= list_entry(i
, yaffs_Object
, siblings
);
6747 /* GetEquivalentObject dereferences any hard links to get to the
6751 yaffs_Object
*yaffs_GetEquivalentObject(yaffs_Object
* obj
)
6753 if (obj
&& obj
->variantType
== YAFFS_OBJECT_TYPE_HARDLINK
) {
6754 /* We want the object id of the equivalent object, not this one */
6755 obj
= obj
->variant
.hardLinkVariant
.equivalentObject
;
6756 yaffs_CheckObjectDetailsLoaded(obj
);
6762 int yaffs_GetObjectName(yaffs_Object
* obj
, YCHAR
* name
, int buffSize
)
6764 memset(name
, 0, buffSize
* sizeof(YCHAR
));
6766 yaffs_CheckObjectDetailsLoaded(obj
);
6768 if (obj
->objectId
== YAFFS_OBJECTID_LOSTNFOUND
) {
6769 yaffs_strncpy(name
, YAFFS_LOSTNFOUND_NAME
, buffSize
- 1);
6770 } else if (obj
->chunkId
<= 0) {
6772 /* make up a name */
6773 yaffs_sprintf(locName
, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX
,
6775 yaffs_strncpy(name
, locName
, buffSize
- 1);
6778 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
6779 else if (obj
->shortName
[0]) {
6780 yaffs_strcpy(name
, obj
->shortName
);
6785 __u8
*buffer
= yaffs_GetTempBuffer(obj
->myDev
, __LINE__
);
6787 yaffs_ObjectHeader
*oh
= (yaffs_ObjectHeader
*) buffer
;
6789 memset(buffer
, 0, obj
->myDev
->nDataBytesPerChunk
);
6791 if (obj
->chunkId
>= 0) {
6792 result
= yaffs_ReadChunkWithTagsFromNAND(obj
->myDev
,
6793 obj
->chunkId
, buffer
,
6796 yaffs_strncpy(name
, oh
->name
, buffSize
- 1);
6798 yaffs_ReleaseTempBuffer(obj
->myDev
, buffer
, __LINE__
);
6801 return yaffs_strlen(name
);
6804 int yaffs_GetObjectFileLength(yaffs_Object
* obj
)
6807 /* Dereference any hard linking */
6808 obj
= yaffs_GetEquivalentObject(obj
);
6810 if (obj
->variantType
== YAFFS_OBJECT_TYPE_FILE
) {
6811 return obj
->variant
.fileVariant
.fileSize
;
6813 if (obj
->variantType
== YAFFS_OBJECT_TYPE_SYMLINK
) {
6814 return yaffs_strlen(obj
->variant
.symLinkVariant
.alias
);
6816 /* Only a directory should drop through to here */
6817 return obj
->myDev
->nDataBytesPerChunk
;
6821 int yaffs_GetObjectLinkCount(yaffs_Object
* obj
)
6824 struct list_head
*i
;
6826 if (!obj
->unlinked
) {
6827 count
++; /* the object itself */
6829 list_for_each(i
, &obj
->hardLinks
) {
6830 count
++; /* add the hard links; */
6836 int yaffs_GetObjectInode(yaffs_Object
* obj
)
6838 obj
= yaffs_GetEquivalentObject(obj
);
6840 return obj
->objectId
;
6843 unsigned yaffs_GetObjectType(yaffs_Object
* obj
)
6845 obj
= yaffs_GetEquivalentObject(obj
);
6847 switch (obj
->variantType
) {
6848 case YAFFS_OBJECT_TYPE_FILE
:
6851 case YAFFS_OBJECT_TYPE_DIRECTORY
:
6854 case YAFFS_OBJECT_TYPE_SYMLINK
:
6857 case YAFFS_OBJECT_TYPE_HARDLINK
:
6860 case YAFFS_OBJECT_TYPE_SPECIAL
:
6861 if (S_ISFIFO(obj
->yst_mode
))
6863 if (S_ISCHR(obj
->yst_mode
))
6865 if (S_ISBLK(obj
->yst_mode
))
6867 if (S_ISSOCK(obj
->yst_mode
))
6875 YCHAR
*yaffs_GetSymlinkAlias(yaffs_Object
* obj
)
6877 obj
= yaffs_GetEquivalentObject(obj
);
6878 if (obj
->variantType
== YAFFS_OBJECT_TYPE_SYMLINK
) {
6879 return yaffs_CloneString(obj
->variant
.symLinkVariant
.alias
);
6881 return yaffs_CloneString(_Y(""));
6885 #ifndef CONFIG_YAFFS_WINCE
6887 int yaffs_SetAttributes(yaffs_Object
* obj
, struct iattr
*attr
)
6889 unsigned int valid
= attr
->ia_valid
;
6891 if (valid
& ATTR_MODE
)
6892 obj
->yst_mode
= attr
->ia_mode
;
6893 if (valid
& ATTR_UID
)
6894 obj
->yst_uid
= attr
->ia_uid
;
6895 if (valid
& ATTR_GID
)
6896 obj
->yst_gid
= attr
->ia_gid
;
6898 if (valid
& ATTR_ATIME
)
6899 obj
->yst_atime
= Y_TIME_CONVERT(attr
->ia_atime
);
6900 if (valid
& ATTR_CTIME
)
6901 obj
->yst_ctime
= Y_TIME_CONVERT(attr
->ia_ctime
);
6902 if (valid
& ATTR_MTIME
)
6903 obj
->yst_mtime
= Y_TIME_CONVERT(attr
->ia_mtime
);
6905 if (valid
& ATTR_SIZE
)
6906 yaffs_ResizeFile(obj
, attr
->ia_size
);
6908 yaffs_UpdateObjectHeader(obj
, NULL
, 1, 0, 0);
6913 int yaffs_GetAttributes(yaffs_Object
* obj
, struct iattr
*attr
)
6915 unsigned int valid
= 0;
6917 attr
->ia_mode
= obj
->yst_mode
;
6919 attr
->ia_uid
= obj
->yst_uid
;
6921 attr
->ia_gid
= obj
->yst_gid
;
6924 Y_TIME_CONVERT(attr
->ia_atime
) = obj
->yst_atime
;
6925 valid
|= ATTR_ATIME
;
6926 Y_TIME_CONVERT(attr
->ia_ctime
) = obj
->yst_ctime
;
6927 valid
|= ATTR_CTIME
;
6928 Y_TIME_CONVERT(attr
->ia_mtime
) = obj
->yst_mtime
;
6929 valid
|= ATTR_MTIME
;
6931 attr
->ia_size
= yaffs_GetFileSize(obj
);
6934 attr
->ia_valid
= valid
;
6943 int yaffs_DumpObject(yaffs_Object
* obj
)
6947 yaffs_GetObjectName(obj
, name
, 256);
6949 T(YAFFS_TRACE_ALWAYS
,
6951 ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
6952 " chunk %d type %d size %d\n"
6953 TENDSTR
), obj
->objectId
, yaffs_GetObjectInode(obj
), name
,
6954 obj
->dirty
, obj
->valid
, obj
->serial
, obj
->sum
, obj
->chunkId
,
6955 yaffs_GetObjectType(obj
), yaffs_GetObjectFileLength(obj
)));
6961 /*---------------------------- Initialisation code -------------------------------------- */
6963 static int yaffs_CheckDevFunctions(const yaffs_Device
* dev
)
6966 /* Common functions, gotta have */
6967 if (!dev
->eraseBlockInNAND
|| !dev
->initialiseNAND
)
6970 #ifdef CONFIG_YAFFS_YAFFS2
6972 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
6973 if (dev
->writeChunkWithTagsToNAND
&&
6974 dev
->readChunkWithTagsFromNAND
&&
6975 !dev
->writeChunkToNAND
&&
6976 !dev
->readChunkFromNAND
&&
6977 dev
->markNANDBlockBad
&& dev
->queryNANDBlock
)
6981 /* Can use the "spare" style interface for yaffs1 */
6982 if (!dev
->isYaffs2
&&
6983 !dev
->writeChunkWithTagsToNAND
&&
6984 !dev
->readChunkWithTagsFromNAND
&&
6985 dev
->writeChunkToNAND
&&
6986 dev
->readChunkFromNAND
&&
6987 !dev
->markNANDBlockBad
&& !dev
->queryNANDBlock
)
6994 static int yaffs_CreateInitialDirectories(yaffs_Device
*dev
)
6996 /* Initialise the unlinked, deleted, root and lost and found directories */
6998 dev
->lostNFoundDir
= dev
->rootDir
= NULL
;
6999 dev
->unlinkedDir
= dev
->deletedDir
= NULL
;
7002 yaffs_CreateFakeDirectory(dev
, YAFFS_OBJECTID_UNLINKED
, S_IFDIR
);
7005 yaffs_CreateFakeDirectory(dev
, YAFFS_OBJECTID_DELETED
, S_IFDIR
);
7008 yaffs_CreateFakeDirectory(dev
, YAFFS_OBJECTID_ROOT
,
7009 YAFFS_ROOT_MODE
| S_IFDIR
);
7010 dev
->lostNFoundDir
=
7011 yaffs_CreateFakeDirectory(dev
, YAFFS_OBJECTID_LOSTNFOUND
,
7012 YAFFS_LOSTNFOUND_MODE
| S_IFDIR
);
7014 if(dev
->lostNFoundDir
&& dev
->rootDir
&& dev
->unlinkedDir
&& dev
->deletedDir
){
7015 yaffs_AddObjectToDirectory(dev
->rootDir
, dev
->lostNFoundDir
);
7022 int yaffs_GutsInitialise(yaffs_Device
* dev
)
7024 int init_failed
= 0;
7028 T(YAFFS_TRACE_TRACING
, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR
)));
7030 /* Check stuff that must be set */
7033 T(YAFFS_TRACE_ALWAYS
, (TSTR("yaffs: Need a device" TENDSTR
)));
7037 dev
->internalStartBlock
= dev
->startBlock
;
7038 dev
->internalEndBlock
= dev
->endBlock
;
7039 dev
->blockOffset
= 0;
7040 dev
->chunkOffset
= 0;
7041 dev
->nFreeChunks
= 0;
7043 if (dev
->startBlock
== 0) {
7044 dev
->internalStartBlock
= dev
->startBlock
+ 1;
7045 dev
->internalEndBlock
= dev
->endBlock
+ 1;
7046 dev
->blockOffset
= 1;
7047 dev
->chunkOffset
= dev
->nChunksPerBlock
;
7050 /* Check geometry parameters. */
7052 if ((dev
->isYaffs2
&& dev
->nDataBytesPerChunk
< 1024) ||
7053 (!dev
->isYaffs2
&& dev
->nDataBytesPerChunk
!= 512) ||
7054 dev
->nChunksPerBlock
< 2 ||
7055 dev
->nReservedBlocks
< 2 ||
7056 dev
->internalStartBlock
<= 0 ||
7057 dev
->internalEndBlock
<= 0 ||
7058 dev
->internalEndBlock
<= (dev
->internalStartBlock
+ dev
->nReservedBlocks
+ 2) // otherwise it is too small
7060 T(YAFFS_TRACE_ALWAYS
,
7062 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
7063 TENDSTR
), dev
->nDataBytesPerChunk
, dev
->isYaffs2
? "2" : ""));
7067 if (yaffs_InitialiseNAND(dev
) != YAFFS_OK
) {
7068 T(YAFFS_TRACE_ALWAYS
,
7069 (TSTR("yaffs: InitialiseNAND failed" TENDSTR
)));
7073 /* Got the right mix of functions? */
7074 if (!yaffs_CheckDevFunctions(dev
)) {
7075 /* Function missing */
7076 T(YAFFS_TRACE_ALWAYS
,
7078 ("yaffs: device function(s) missing or wrong\n" TENDSTR
)));
7083 /* This is really a compilation check. */
7084 if (!yaffs_CheckStructures()) {
7085 T(YAFFS_TRACE_ALWAYS
,
7086 (TSTR("yaffs_CheckStructures failed\n" TENDSTR
)));
7090 if (dev
->isMounted
) {
7091 T(YAFFS_TRACE_ALWAYS
,
7092 (TSTR("yaffs: device already mounted\n" TENDSTR
)));
7096 /* Finished with most checks. One or two more checks happen later on too. */
7102 /* OK now calculate a few things for the device */
7105 * Calculate all the chunk size manipulation numbers:
7107 /* Start off assuming it is a power of 2 */
7108 dev
->chunkShift
= ShiftDiv(dev
->nDataBytesPerChunk
);
7109 dev
->chunkMask
= (1<<dev
->chunkShift
) - 1;
7111 if(dev
->nDataBytesPerChunk
== (dev
->chunkMask
+ 1)){
7112 /* Yes it is a power of 2, disable crumbs */
7114 dev
->crumbShift
= 0;
7115 dev
->crumbsPerChunk
= 0;
7117 /* Not a power of 2, use crumbs instead */
7118 dev
->crumbShift
= ShiftDiv(sizeof(yaffs_PackedTags2TagsPart
));
7119 dev
->crumbMask
= (1<<dev
->crumbShift
)-1;
7120 dev
->crumbsPerChunk
= dev
->nDataBytesPerChunk
/(1 << dev
->crumbShift
);
7121 dev
->chunkShift
= 0;
7127 * Calculate chunkGroupBits.
7128 * We need to find the next power of 2 > than internalEndBlock
7131 x
= dev
->nChunksPerBlock
* (dev
->internalEndBlock
+ 1);
7135 /* Set up tnode width if wide tnodes are enabled. */
7136 if(!dev
->wideTnodesDisabled
){
7137 /* bits must be even so that we end up with 32-bit words */
7141 dev
->tnodeWidth
= 16;
7143 dev
->tnodeWidth
= bits
;
7146 dev
->tnodeWidth
= 16;
7148 dev
->tnodeMask
= (1<<dev
->tnodeWidth
)-1;
7150 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
7151 * so if the bitwidth of the
7152 * chunk range we're using is greater than 16 we need
7153 * to figure out chunk shift and chunkGroupSize
7156 if (bits
<= dev
->tnodeWidth
)
7157 dev
->chunkGroupBits
= 0;
7159 dev
->chunkGroupBits
= bits
- dev
->tnodeWidth
;
7162 dev
->chunkGroupSize
= 1 << dev
->chunkGroupBits
;
7164 if (dev
->nChunksPerBlock
< dev
->chunkGroupSize
) {
7165 /* We have a problem because the soft delete won't work if
7166 * the chunk group size > chunks per block.
7167 * This can be remedied by using larger "virtual blocks".
7169 T(YAFFS_TRACE_ALWAYS
,
7170 (TSTR("yaffs: chunk group too large\n" TENDSTR
)));
7175 /* OK, we've finished verifying the device, lets continue with initialisation */
7177 /* More device initialisation */
7178 dev
->garbageCollections
= 0;
7179 dev
->passiveGarbageCollections
= 0;
7180 dev
->currentDirtyChecker
= 0;
7181 dev
->bufferedBlock
= -1;
7182 dev
->doingBufferedBlockRewrite
= 0;
7183 dev
->nDeletedFiles
= 0;
7184 dev
->nBackgroundDeletions
= 0;
7185 dev
->nUnlinkedFiles
= 0;
7187 dev
->eccUnfixed
= 0;
7188 dev
->tagsEccFixed
= 0;
7189 dev
->tagsEccUnfixed
= 0;
7190 dev
->nErasureFailures
= 0;
7191 dev
->nErasedBlocks
= 0;
7193 dev
->hasPendingPrioritisedGCs
= 1; /* Assume the worst for now, will get fixed on first GC */
7195 /* Initialise temporary buffers and caches. */
7196 if(!yaffs_InitialiseTempBuffers(dev
))
7199 dev
->srCache
= NULL
;
7200 dev
->gcCleanupList
= NULL
;
7204 dev
->nShortOpCaches
> 0) {
7207 int srCacheBytes
= dev
->nShortOpCaches
* sizeof(yaffs_ChunkCache
);
7209 if (dev
->nShortOpCaches
> YAFFS_MAX_SHORT_OP_CACHES
) {
7210 dev
->nShortOpCaches
= YAFFS_MAX_SHORT_OP_CACHES
;
7213 buf
= dev
->srCache
= YMALLOC(srCacheBytes
);
7216 memset(dev
->srCache
,0,srCacheBytes
);
7218 for (i
= 0; i
< dev
->nShortOpCaches
&& buf
; i
++) {
7219 dev
->srCache
[i
].object
= NULL
;
7220 dev
->srCache
[i
].lastUse
= 0;
7221 dev
->srCache
[i
].dirty
= 0;
7222 dev
->srCache
[i
].data
= buf
= YMALLOC_DMA(dev
->nDataBytesPerChunk
);
7233 dev
->gcCleanupList
= YMALLOC(dev
->nChunksPerBlock
* sizeof(__u32
));
7234 if(!dev
->gcCleanupList
)
7238 if (dev
->isYaffs2
) {
7239 dev
->useHeaderFileSize
= 1;
7241 if(!init_failed
&& !yaffs_InitialiseBlocks(dev
))
7244 yaffs_InitialiseTnodes(dev
);
7245 yaffs_InitialiseObjects(dev
);
7247 if(!init_failed
&& !yaffs_CreateInitialDirectories(dev
))
7252 /* Now scan the flash. */
7253 if (dev
->isYaffs2
) {
7254 if(yaffs_CheckpointRestore(dev
)) {
7255 T(YAFFS_TRACE_ALWAYS
,
7256 (TSTR("yaffs: restored from checkpoint" TENDSTR
)));
7259 /* Clean up the mess caused by an aborted checkpoint load
7260 * and scan backwards.
7262 yaffs_DeinitialiseBlocks(dev
);
7263 yaffs_DeinitialiseTnodes(dev
);
7264 yaffs_DeinitialiseObjects(dev
);
7267 dev
->nErasedBlocks
= 0;
7268 dev
->nFreeChunks
= 0;
7269 dev
->allocationBlock
= -1;
7270 dev
->allocationPage
= -1;
7271 dev
->nDeletedFiles
= 0;
7272 dev
->nUnlinkedFiles
= 0;
7273 dev
->nBackgroundDeletions
= 0;
7274 dev
->oldestDirtySequence
= 0;
7276 if(!init_failed
&& !yaffs_InitialiseBlocks(dev
))
7279 yaffs_InitialiseTnodes(dev
);
7280 yaffs_InitialiseObjects(dev
);
7282 if(!init_failed
&& !yaffs_CreateInitialDirectories(dev
))
7285 if(!init_failed
&& !yaffs_ScanBackwards(dev
))
7289 if(!yaffs_Scan(dev
))
7294 /* Clean up the mess */
7295 T(YAFFS_TRACE_TRACING
,
7296 (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR
)));
7298 yaffs_Deinitialise(dev
);
7302 /* Zero out stats */
7303 dev
->nPageReads
= 0;
7304 dev
->nPageWrites
= 0;
7305 dev
->nBlockErasures
= 0;
7307 dev
->nRetriedWrites
= 0;
7309 dev
->nRetiredBlocks
= 0;
7311 yaffs_VerifyFreeChunks(dev
);
7312 yaffs_VerifyBlocks(dev
);
7315 T(YAFFS_TRACE_TRACING
,
7316 (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR
)));
7321 void yaffs_Deinitialise(yaffs_Device
* dev
)
7323 if (dev
->isMounted
) {
7326 yaffs_DeinitialiseBlocks(dev
);
7327 yaffs_DeinitialiseTnodes(dev
);
7328 yaffs_DeinitialiseObjects(dev
);
7329 if (dev
->nShortOpCaches
> 0 &&
7332 for (i
= 0; i
< dev
->nShortOpCaches
; i
++) {
7333 if(dev
->srCache
[i
].data
)
7334 YFREE(dev
->srCache
[i
].data
);
7335 dev
->srCache
[i
].data
= NULL
;
7338 YFREE(dev
->srCache
);
7339 dev
->srCache
= NULL
;
7342 YFREE(dev
->gcCleanupList
);
7344 for (i
= 0; i
< YAFFS_N_TEMP_BUFFERS
; i
++) {
7345 YFREE(dev
->tempBuffer
[i
].buffer
);
7353 static int yaffs_CountFreeChunks(yaffs_Device
* dev
)
7358 yaffs_BlockInfo
*blk
;
7360 for (nFree
= 0, b
= dev
->internalStartBlock
; b
<= dev
->internalEndBlock
;
7362 blk
= yaffs_GetBlockInfo(dev
, b
);
7364 switch (blk
->blockState
) {
7365 case YAFFS_BLOCK_STATE_EMPTY
:
7366 case YAFFS_BLOCK_STATE_ALLOCATING
:
7367 case YAFFS_BLOCK_STATE_COLLECTING
:
7368 case YAFFS_BLOCK_STATE_FULL
:
7370 (dev
->nChunksPerBlock
- blk
->pagesInUse
+
7371 blk
->softDeletions
);
7382 int yaffs_GetNumberOfFreeChunks(yaffs_Device
* dev
)
7384 /* This is what we report to the outside world */
7387 int nDirtyCacheChunks
;
7388 int blocksForCheckpoint
;
7391 nFree
= dev
->nFreeChunks
;
7393 nFree
= yaffs_CountFreeChunks(dev
);
7396 nFree
+= dev
->nDeletedFiles
;
7398 /* Now count the number of dirty chunks in the cache and subtract those */
7402 for (nDirtyCacheChunks
= 0, i
= 0; i
< dev
->nShortOpCaches
; i
++) {
7403 if (dev
->srCache
[i
].dirty
)
7404 nDirtyCacheChunks
++;
7408 nFree
-= nDirtyCacheChunks
;
7410 nFree
-= ((dev
->nReservedBlocks
+ 1) * dev
->nChunksPerBlock
);
7412 /* Now we figure out how much to reserve for the checkpoint and report that... */
7413 blocksForCheckpoint
= dev
->nCheckpointReservedBlocks
- dev
->blocksInCheckpoint
;
7414 if(blocksForCheckpoint
< 0)
7415 blocksForCheckpoint
= 0;
7417 nFree
-= (blocksForCheckpoint
* dev
->nChunksPerBlock
);
7426 static int yaffs_freeVerificationFailures
;
7428 static void yaffs_VerifyFreeChunks(yaffs_Device
* dev
)
7433 if(yaffs_SkipVerification(dev
))
7436 counted
= yaffs_CountFreeChunks(dev
);
7438 difference
= dev
->nFreeChunks
- counted
;
7441 T(YAFFS_TRACE_ALWAYS
,
7442 (TSTR("Freechunks verification failure %d %d %d" TENDSTR
),
7443 dev
->nFreeChunks
, counted
, difference
));
7444 yaffs_freeVerificationFailures
++;
7448 /*---------------------------------------- YAFFS test code ----------------------*/
7450 #define yaffs_CheckStruct(structure,syze, name) \
7451 if(sizeof(structure) != syze) \
7453 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
7454 name,syze,sizeof(structure))); \
7455 return YAFFS_FAIL; \
7458 static int yaffs_CheckStructures(void)
7460 /* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
7461 /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
7462 /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
7463 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
7464 yaffs_CheckStruct(yaffs_Tnode
, 2 * YAFFS_NTNODES_LEVEL0
, "yaffs_Tnode")
7466 yaffs_CheckStruct(yaffs_ObjectHeader
, 512, "yaffs_ObjectHeader")