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.
17 const char *yaffs_guts_c_version =
18 "$Id: yaffs_guts.c,v 1.52 2007/10/16 00:45:05 charles Exp $";
21 #include "linux/stat.h"
23 #include "yaffsinterface.h"
25 #include "yaffs_guts.h"
26 #include "yaffs_tagsvalidity.h"
28 #include "yaffs_tagscompat.h"
29 #ifndef CONFIG_YAFFS_USE_OWN_SORT
30 #include "yaffs_qsort.h"
32 #include "yaffs_nand.h"
34 #include "yaffs_checkptrw.h"
36 #include "yaffs_nand.h"
37 #include "yaffs_packedtags2.h"
41 #ifdef CONFIG_YAFFS_WINCE
42 void yfsd_LockYAFFS(BOOL fsLockOnly);
43 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
46 #define YAFFS_PASSIVE_GC_CHUNKS 2
48 #include "yaffs_ecc.h"
51 /* Robustification (if it ever comes about...) */
52 static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
53 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
54 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
56 const yaffs_ExtendedTags * tags);
57 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
58 const yaffs_ExtendedTags * tags);
60 /* Other local prototypes */
61 static int yaffs_UnlinkObject( yaffs_Object *obj);
62 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
64 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
66 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
68 yaffs_ExtendedTags * tags,
70 static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
71 int chunkInNAND, int inScan);
73 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
74 yaffs_ObjectType type);
75 static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
77 static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
78 int force, int isShrink, int shadows);
79 static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
80 static int yaffs_CheckStructures(void);
81 static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
82 int chunkOffset, int *limit);
83 static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
85 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
87 static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
88 static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
91 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
94 static int yaffs_UnlinkWorker(yaffs_Object * obj);
95 static void yaffs_DestroyObject(yaffs_Object * obj);
97 static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
100 loff_t yaffs_GetFileSize(yaffs_Object * obj);
102 static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
104 static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
106 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
108 #ifdef YAFFS_PARANOID
109 static int yaffs_CheckFileSanity(yaffs_Object * in);
111 #define yaffs_CheckFileSanity(in)
114 static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
115 static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
117 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
119 static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
120 yaffs_ExtendedTags * tags);
122 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
123 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
124 yaffs_FileStructure * fStruct,
128 /* Function to calculate chunk and offset */
130 static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
133 /* Easy-peasy power of 2 case */
134 *chunk = (__u32)(addr >> dev->chunkShift);
135 *offset = (__u32)(addr & dev->chunkMask);
137 else if(dev->crumbsPerChunk)
139 /* Case where we're using "crumbs" */
140 *offset = (__u32)(addr & dev->crumbMask);
141 addr >>= dev->crumbShift;
142 *chunk = ((__u32)addr)/dev->crumbsPerChunk;
143 *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
149 /* Function to return the number of shifts for a power of 2 greater than or equal
150 * to the given number
151 * Note we don't try to cater for all possible numbers and this does not have to
152 * be hellishly efficient.
155 static __u32 ShiftsGE(__u32 x)
160 nShifts = extraBits = 0;
163 if(x & 1) extraBits++;
174 /* Function to return the number of shifts to get a 1 in bit 0
177 static __u32 ShiftDiv(__u32 x)
196 * Temporary buffer manipulations.
199 static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
202 __u8 *buf = (__u8 *)1;
204 memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
206 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
207 dev->tempBuffer[i].line = 0; /* not in use */
208 dev->tempBuffer[i].buffer = buf =
209 YMALLOC_DMA(dev->nDataBytesPerChunk);
212 return buf ? YAFFS_OK : YAFFS_FAIL;
216 static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
219 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
220 if (dev->tempBuffer[i].line == 0) {
221 dev->tempBuffer[i].line = lineNo;
222 if ((i + 1) > dev->maxTemp) {
223 dev->maxTemp = i + 1;
224 for (j = 0; j <= i; j++)
225 dev->tempBuffer[j].maxLine =
226 dev->tempBuffer[j].line;
229 return dev->tempBuffer[i].buffer;
233 T(YAFFS_TRACE_BUFFERS,
234 (TSTR("Out of temp buffers at line %d, other held by lines:"),
236 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
237 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
239 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
242 * If we got here then we have to allocate an unmanaged one
246 dev->unmanagedTempAllocations++;
247 return YMALLOC(dev->nDataBytesPerChunk);
251 static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
255 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
256 if (dev->tempBuffer[i].buffer == buffer) {
257 dev->tempBuffer[i].line = 0;
263 /* assume it is an unmanaged one. */
264 T(YAFFS_TRACE_BUFFERS,
265 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
268 dev->unmanagedTempDeallocations++;
274 * Determine if we have a managed buffer.
276 int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
279 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
280 if (dev->tempBuffer[i].buffer == buffer)
285 for (i = 0; i < dev->nShortOpCaches; i++) {
286 if( dev->srCache[i].data == buffer )
291 if (buffer == dev->checkpointBuffer)
294 T(YAFFS_TRACE_ALWAYS,
295 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
302 * Chunk bitmap manipulations
305 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
307 if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
309 (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
313 return dev->chunkBits +
314 (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
317 static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
319 if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
320 chunk < 0 || chunk >= dev->nChunksPerBlock) {
322 (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
327 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
329 __u8 *blkBits = yaffs_BlockBits(dev, blk);
331 memset(blkBits, 0, dev->chunkBitmapStride);
334 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
336 __u8 *blkBits = yaffs_BlockBits(dev, blk);
338 yaffs_VerifyChunkBitId(dev,blk,chunk);
340 blkBits[chunk / 8] &= ~(1 << (chunk & 7));
343 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
345 __u8 *blkBits = yaffs_BlockBits(dev, blk);
347 yaffs_VerifyChunkBitId(dev,blk,chunk);
349 blkBits[chunk / 8] |= (1 << (chunk & 7));
352 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
354 __u8 *blkBits = yaffs_BlockBits(dev, blk);
355 yaffs_VerifyChunkBitId(dev,blk,chunk);
357 return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
360 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
362 __u8 *blkBits = yaffs_BlockBits(dev, blk);
364 for (i = 0; i < dev->chunkBitmapStride; i++) {
372 static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
374 __u8 *blkBits = yaffs_BlockBits(dev, blk);
377 for (i = 0; i < dev->chunkBitmapStride; i++) {
394 static int yaffs_SkipVerification(yaffs_Device *dev)
396 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
399 static int yaffs_SkipFullVerification(yaffs_Device *dev)
401 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
404 static int yaffs_SkipNANDVerification(yaffs_Device *dev)
406 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
409 static const char * blockStateName[] = {
422 static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
427 if(yaffs_SkipVerification(dev))
430 /* Report illegal runtime states */
431 if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
432 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
434 switch(bi->blockState){
435 case YAFFS_BLOCK_STATE_UNKNOWN:
436 case YAFFS_BLOCK_STATE_SCANNING:
437 case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
438 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
439 n,blockStateName[bi->blockState]));
442 /* Check pages in use and soft deletions are legal */
444 actuallyUsed = bi->pagesInUse - bi->softDeletions;
446 if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
447 bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
448 actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
449 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
450 n,bi->pagesInUse,bi->softDeletions));
453 /* Check chunk bitmap legal */
454 inUse = yaffs_CountChunkBits(dev,n);
455 if(inUse != bi->pagesInUse)
456 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
457 n,bi->pagesInUse,inUse));
459 /* Check that the sequence number is valid.
460 * Ten million is legal, but is very unlikely
463 (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
464 (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
465 T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
466 n,bi->sequenceNumber));
470 static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
472 yaffs_VerifyBlock(dev,bi,n);
474 /* After collection the block should be in the erased state */
475 /* TODO: This will need to change if we do partial gc */
477 if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
478 T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
483 static void yaffs_VerifyBlocks(yaffs_Device *dev)
486 int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
487 int nIllegalBlockStates = 0;
490 if(yaffs_SkipVerification(dev))
493 memset(nBlocksPerState,0,sizeof(nBlocksPerState));
496 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
497 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
498 yaffs_VerifyBlock(dev,bi,i);
500 if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
501 nBlocksPerState[bi->blockState]++;
503 nIllegalBlockStates++;
507 T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
508 T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
510 T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
511 if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
512 T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
514 for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
515 T(YAFFS_TRACE_VERIFY,
516 (TSTR("%s %d blocks"TENDSTR),
517 blockStateName[i],nBlocksPerState[i]));
519 if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
520 T(YAFFS_TRACE_VERIFY,
521 (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
522 dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
524 if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
525 T(YAFFS_TRACE_VERIFY,
526 (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
527 dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
529 if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
530 T(YAFFS_TRACE_VERIFY,
531 (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
532 nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
534 T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
539 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
540 * case those tests will not be performed.
542 static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
544 if(yaffs_SkipVerification(obj->myDev))
547 if(!(tags && obj && oh)){
548 T(YAFFS_TRACE_VERIFY,
549 (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
550 (__u32)tags,(__u32)obj,(__u32)oh));
554 if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
555 oh->type > YAFFS_OBJECT_TYPE_MAX)
556 T(YAFFS_TRACE_VERIFY,
557 (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
558 tags->objectId, oh->type));
560 if(tags->objectId != obj->objectId)
561 T(YAFFS_TRACE_VERIFY,
562 (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
563 tags->objectId, obj->objectId));
567 * Check that the object's parent ids match if parentCheck requested.
569 * Tests do not apply to the root object.
572 if(parentCheck && tags->objectId > 1 && !obj->parent)
573 T(YAFFS_TRACE_VERIFY,
574 (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
575 tags->objectId, oh->parentObjectId));
578 if(parentCheck && obj->parent &&
579 oh->parentObjectId != obj->parent->objectId &&
580 (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
581 obj->parent->objectId != YAFFS_OBJECTID_DELETED))
582 T(YAFFS_TRACE_VERIFY,
583 (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
584 tags->objectId, oh->parentObjectId, obj->parent->objectId));
587 if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
588 T(YAFFS_TRACE_VERIFY,
589 (TSTR("Obj %d header name is NULL"TENDSTR),
592 if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
593 T(YAFFS_TRACE_VERIFY,
594 (TSTR("Obj %d header name is 0xFF"TENDSTR),
600 static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
601 __u32 level, int chunkOffset)
604 yaffs_Device *dev = obj->myDev;
610 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
611 if (tn->internal[i]) {
612 ok = yaffs_VerifyTnodeWorker(obj,
615 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
618 } else if (level == 0) {
620 yaffs_ExtendedTags tags;
621 __u32 objectId = obj->objectId;
623 chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
625 for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
626 __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
629 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
630 yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
631 if(tags.objectId != objectId || tags.chunkId != chunkOffset){
632 T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
633 objectId, chunkOffset, theChunk,
634 tags.objectId, tags.chunkId));
647 static void yaffs_VerifyFile(yaffs_Object *obj)
649 int requiredTallness;
655 yaffs_ExtendedTags tags;
659 if(obj && yaffs_SkipVerification(obj->myDev))
663 objectId = obj->objectId;
665 /* Check file size is consistent with tnode depth */
666 lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
667 x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
668 requiredTallness = 0;
670 x >>= YAFFS_TNODES_INTERNAL_BITS;
674 actualTallness = obj->variant.fileVariant.topLevel;
676 if(requiredTallness > actualTallness )
677 T(YAFFS_TRACE_VERIFY,
678 (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
679 obj->objectId,actualTallness, requiredTallness));
682 /* Check that the chunks in the tnode tree are all correct.
683 * We do this by scanning through the tnode tree and
684 * checking the tags for every chunk match.
687 if(yaffs_SkipNANDVerification(dev))
690 for(i = 1; i <= lastChunk; i++){
691 tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
694 __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
696 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
697 yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
698 if(tags.objectId != objectId || tags.chunkId != i){
699 T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
700 objectId, i, theChunk,
701 tags.objectId, tags.chunkId));
710 static void yaffs_VerifyDirectory(yaffs_Object *obj)
712 if(obj && yaffs_SkipVerification(obj->myDev))
717 static void yaffs_VerifyHardLink(yaffs_Object *obj)
719 if(obj && yaffs_SkipVerification(obj->myDev))
722 /* Verify sane equivalent object */
725 static void yaffs_VerifySymlink(yaffs_Object *obj)
727 if(obj && yaffs_SkipVerification(obj->myDev))
730 /* Verify symlink string */
733 static void yaffs_VerifySpecial(yaffs_Object *obj)
735 if(obj && yaffs_SkipVerification(obj->myDev))
739 static void yaffs_VerifyObject(yaffs_Object *obj)
754 if(yaffs_SkipVerification(dev))
757 /* Check sane object header chunk */
759 chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
760 chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
762 chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
763 chunkIsLive = chunkIdOk &&
764 yaffs_CheckChunkBit(dev,
765 obj->chunkId / dev->nChunksPerBlock,
766 obj->chunkId % dev->nChunksPerBlock);
768 (!chunkIdOk || !chunkIsLive)) {
769 T(YAFFS_TRACE_VERIFY,
770 (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
771 obj->objectId,obj->chunkId,
772 chunkIdOk ? "" : ",out of range",
773 chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
776 if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
777 yaffs_ExtendedTags tags;
778 yaffs_ObjectHeader *oh;
779 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
781 oh = (yaffs_ObjectHeader *)buffer;
783 yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
785 yaffs_VerifyObjectHeader(obj,oh,&tags,1);
787 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
790 /* Verify it has a parent */
791 if(obj && !obj->fake &&
792 (!obj->parent || obj->parent->myDev != dev)){
793 T(YAFFS_TRACE_VERIFY,
794 (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
795 obj->objectId,obj->parent));
798 /* Verify parent is a directory */
799 if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
800 T(YAFFS_TRACE_VERIFY,
801 (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
802 obj->objectId,obj->parent->variantType));
805 switch(obj->variantType){
806 case YAFFS_OBJECT_TYPE_FILE:
807 yaffs_VerifyFile(obj);
809 case YAFFS_OBJECT_TYPE_SYMLINK:
810 yaffs_VerifySymlink(obj);
812 case YAFFS_OBJECT_TYPE_DIRECTORY:
813 yaffs_VerifyDirectory(obj);
815 case YAFFS_OBJECT_TYPE_HARDLINK:
816 yaffs_VerifyHardLink(obj);
818 case YAFFS_OBJECT_TYPE_SPECIAL:
819 yaffs_VerifySpecial(obj);
821 case YAFFS_OBJECT_TYPE_UNKNOWN:
823 T(YAFFS_TRACE_VERIFY,
824 (TSTR("Obj %d has illegaltype %d"TENDSTR),
825 obj->objectId,obj->variantType));
832 static void yaffs_VerifyObjects(yaffs_Device *dev)
836 struct list_head *lh;
838 if(yaffs_SkipVerification(dev))
841 /* Iterate through the objects in each hash entry */
843 for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
844 list_for_each(lh, &dev->objectBucket[i].list) {
846 obj = list_entry(lh, yaffs_Object, hashLink);
847 yaffs_VerifyObject(obj);
856 * Simple hash function. Needs to have a reasonable spread
859 static Y_INLINE int yaffs_HashFunction(int n)
865 return (n % YAFFS_NOBJECT_BUCKETS);
869 * Access functions to useful fake objects
872 yaffs_Object *yaffs_Root(yaffs_Device * dev)
877 yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
879 return dev->lostNFoundDir;
884 * Erased NAND checking functions
887 int yaffs_CheckFF(__u8 * buffer, int nBytes)
889 /* Horrible, slow implementation */
898 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
902 int retval = YAFFS_OK;
903 __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
904 yaffs_ExtendedTags tags;
907 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
909 if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
913 if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
914 T(YAFFS_TRACE_NANDACCESS,
915 (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
919 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
925 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
927 yaffs_ExtendedTags * tags,
934 yaffs_InvalidateCheckpoint(dev);
937 yaffs_BlockInfo *bi = 0;
940 chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
946 /* First check this chunk is erased, if it needs
947 * checking. The checking policy (unless forced
948 * always on) is as follows:
950 * Check the first page we try to write in a block.
951 * If the check passes then we don't need to check any
952 * more. If the check fails, we check again...
953 * If the block has been erased, we don't need to check.
955 * However, if the block has been prioritised for gc,
956 * then we think there might be something odd about
957 * this block and stop using it.
959 * Rationale: We should only ever see chunks that have
960 * not been erased if there was a partially written
961 * chunk due to power loss. This checking policy should
962 * catch that case with very few checks and thus save a
963 * lot of checks that are most likely not needed.
965 if (bi->gcPrioritise) {
966 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
967 /* try another chunk */
971 /* let's give it a try */
974 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
975 bi->skipErasedCheck = 0;
977 if (!bi->skipErasedCheck) {
978 erasedOk = yaffs_CheckChunkErased(dev, chunk);
979 if (erasedOk != YAFFS_OK) {
981 (TSTR ("**>> yaffs chunk %d was not erased"
984 /* try another chunk */
987 bi->skipErasedCheck = 1;
990 writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
992 if (writeOk != YAFFS_OK) {
993 yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
994 /* try another chunk */
998 /* Copy the data into the robustification buffer */
999 yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
1001 } while (writeOk != YAFFS_OK &&
1002 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
1008 T(YAFFS_TRACE_ERROR,
1009 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
1012 dev->nRetriedWrites += (attempts - 1);
1019 * Block retiring for handling a broken block.
1022 static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
1024 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1026 yaffs_InvalidateCheckpoint(dev);
1028 yaffs_MarkBlockBad(dev, blockInNAND);
1030 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
1031 bi->gcPrioritise = 0;
1032 bi->needsRetiring = 0;
1034 dev->nRetiredBlocks++;
1038 * Functions for robustisizing TODO
1042 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
1044 const yaffs_ExtendedTags * tags)
1048 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
1049 const yaffs_ExtendedTags * tags)
1053 void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
1055 if(!bi->gcPrioritise){
1056 bi->gcPrioritise = 1;
1057 dev->hasPendingPrioritisedGCs = 1;
1058 bi->chunkErrorStrikes ++;
1060 if(bi->chunkErrorStrikes > 3){
1061 bi->needsRetiring = 1; /* Too many stikes, so retire this */
1062 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
1069 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
1072 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
1073 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1075 yaffs_HandleChunkError(dev,bi);
1079 /* Was an actual write failure, so mark the block for retirement */
1080 bi->needsRetiring = 1;
1081 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1082 (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
1087 /* Delete the chunk */
1088 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
1092 /*---------------- Name handling functions ------------*/
1094 static __u16 yaffs_CalcNameSum(const YCHAR * name)
1099 YUCHAR *bname = (YUCHAR *) name;
1101 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
1103 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1104 sum += yaffs_toupper(*bname) * i;
1106 sum += (*bname) * i;
1115 static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
1117 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
1118 if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
1119 yaffs_strcpy(obj->shortName, name);
1121 obj->shortName[0] = _Y('\0');
1124 obj->sum = yaffs_CalcNameSum(name);
1127 /*-------------------- TNODES -------------------
1129 * List of spare tnodes
1130 * The list is hooked together using the first pointer
1134 /* yaffs_CreateTnodes creates a bunch more tnodes and
1135 * adds them to the tnode free list.
1136 * Don't use this function directly
1139 static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
1143 yaffs_Tnode *newTnodes;
1147 yaffs_TnodeList *tnl;
1152 /* Calculate the tnode size in bytes for variable width tnode support.
1153 * Must be a multiple of 32-bits */
1154 tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
1156 /* make these things */
1158 newTnodes = YMALLOC(nTnodes * tnodeSize);
1159 mem = (__u8 *)newTnodes;
1162 T(YAFFS_TRACE_ERROR,
1163 (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
1167 /* Hook them into the free list */
1169 for (i = 0; i < nTnodes - 1; i++) {
1170 newTnodes[i].internal[0] = &newTnodes[i + 1];
1171 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1172 newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1176 newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
1177 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1178 newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1180 dev->freeTnodes = newTnodes;
1182 /* New hookup for wide tnodes */
1183 for(i = 0; i < nTnodes -1; i++) {
1184 curr = (yaffs_Tnode *) &mem[i * tnodeSize];
1185 next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
1186 curr->internal[0] = next;
1189 curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
1190 curr->internal[0] = dev->freeTnodes;
1191 dev->freeTnodes = (yaffs_Tnode *)mem;
1196 dev->nFreeTnodes += nTnodes;
1197 dev->nTnodesCreated += nTnodes;
1199 /* Now add this bunch of tnodes to a list for freeing up.
1200 * NB If we can't add this to the management list it isn't fatal
1201 * but it just means we can't free this bunch of tnodes later.
1204 tnl = YMALLOC(sizeof(yaffs_TnodeList));
1206 T(YAFFS_TRACE_ERROR,
1208 ("yaffs: Could not add tnodes to management list" TENDSTR)));
1212 tnl->tnodes = newTnodes;
1213 tnl->next = dev->allocatedTnodeList;
1214 dev->allocatedTnodeList = tnl;
1217 T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
1222 /* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
1224 static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
1226 yaffs_Tnode *tn = NULL;
1228 /* If there are none left make more */
1229 if (!dev->freeTnodes) {
1230 yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
1233 if (dev->freeTnodes) {
1234 tn = dev->freeTnodes;
1235 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1236 if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
1237 /* Hoosterman, this thing looks like it isn't in the list */
1238 T(YAFFS_TRACE_ALWAYS,
1239 (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
1242 dev->freeTnodes = dev->freeTnodes->internal[0];
1249 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
1251 yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
1254 memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1259 /* FreeTnode frees up a tnode and puts it back on the free list */
1260 static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
1263 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
1264 if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
1265 /* Hoosterman, this thing looks like it is already in the list */
1266 T(YAFFS_TRACE_ALWAYS,
1267 (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
1269 tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
1271 tn->internal[0] = dev->freeTnodes;
1272 dev->freeTnodes = tn;
1277 static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
1279 /* Free the list of allocated tnodes */
1280 yaffs_TnodeList *tmp;
1282 while (dev->allocatedTnodeList) {
1283 tmp = dev->allocatedTnodeList->next;
1285 YFREE(dev->allocatedTnodeList->tnodes);
1286 YFREE(dev->allocatedTnodeList);
1287 dev->allocatedTnodeList = tmp;
1291 dev->freeTnodes = NULL;
1292 dev->nFreeTnodes = 0;
1295 static void yaffs_InitialiseTnodes(yaffs_Device * dev)
1297 dev->allocatedTnodeList = NULL;
1298 dev->freeTnodes = NULL;
1299 dev->nFreeTnodes = 0;
1300 dev->nTnodesCreated = 0;
1305 void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
1307 __u32 *map = (__u32 *)tn;
1313 pos &= YAFFS_TNODES_LEVEL0_MASK;
1314 val >>= dev->chunkGroupBits;
1316 bitInMap = pos * dev->tnodeWidth;
1317 wordInMap = bitInMap /32;
1318 bitInWord = bitInMap & (32 -1);
1320 mask = dev->tnodeMask << bitInWord;
1322 map[wordInMap] &= ~mask;
1323 map[wordInMap] |= (mask & (val << bitInWord));
1325 if(dev->tnodeWidth > (32-bitInWord)) {
1326 bitInWord = (32 - bitInWord);
1328 mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
1329 map[wordInMap] &= ~mask;
1330 map[wordInMap] |= (mask & (val >> bitInWord));
1334 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
1336 __u32 *map = (__u32 *)tn;
1342 pos &= YAFFS_TNODES_LEVEL0_MASK;
1344 bitInMap = pos * dev->tnodeWidth;
1345 wordInMap = bitInMap /32;
1346 bitInWord = bitInMap & (32 -1);
1348 val = map[wordInMap] >> bitInWord;
1350 if(dev->tnodeWidth > (32-bitInWord)) {
1351 bitInWord = (32 - bitInWord);
1353 val |= (map[wordInMap] << bitInWord);
1356 val &= dev->tnodeMask;
1357 val <<= dev->chunkGroupBits;
1362 /* ------------------- End of individual tnode manipulation -----------------*/
1364 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
1365 * The look up tree is represented by the top tnode and the number of topLevel
1366 * in the tree. 0 means only the level 0 tnode is in the tree.
1369 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
1370 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
1371 yaffs_FileStructure * fStruct,
1375 yaffs_Tnode *tn = fStruct->top;
1377 int requiredTallness;
1378 int level = fStruct->topLevel;
1380 /* Check sane level and chunk Id */
1381 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
1385 if (chunkId > YAFFS_MAX_CHUNK_ID) {
1389 /* First check we're tall enough (ie enough topLevel) */
1391 i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1392 requiredTallness = 0;
1394 i >>= YAFFS_TNODES_INTERNAL_BITS;
1398 if (requiredTallness > fStruct->topLevel) {
1399 /* Not tall enough, so we can't find it, return NULL. */
1403 /* Traverse down to level 0 */
1404 while (level > 0 && tn) {
1406 internal[(chunkId >>
1407 ( YAFFS_TNODES_LEVEL0_BITS +
1409 YAFFS_TNODES_INTERNAL_BITS)
1411 YAFFS_TNODES_INTERNAL_MASK];
1419 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
1420 * This happens in two steps:
1421 * 1. If the tree isn't tall enough, then make it taller.
1422 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
1424 * Used when modifying the tree.
1426 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
1427 * be plugged into the ttree.
1430 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
1431 yaffs_FileStructure * fStruct,
1433 yaffs_Tnode *passedTn)
1436 int requiredTallness;
1444 /* Check sane level and page Id */
1445 if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
1449 if (chunkId > YAFFS_MAX_CHUNK_ID) {
1453 /* First check we're tall enough (ie enough topLevel) */
1455 x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1456 requiredTallness = 0;
1458 x >>= YAFFS_TNODES_INTERNAL_BITS;
1463 if (requiredTallness > fStruct->topLevel) {
1464 /* Not tall enough,gotta make the tree taller */
1465 for (i = fStruct->topLevel; i < requiredTallness; i++) {
1467 tn = yaffs_GetTnode(dev);
1470 tn->internal[0] = fStruct->top;
1473 T(YAFFS_TRACE_ERROR,
1474 (TSTR("yaffs: no more tnodes" TENDSTR)));
1478 fStruct->topLevel = requiredTallness;
1481 /* Traverse down to level 0, adding anything we need */
1483 l = fStruct->topLevel;
1487 while (l > 0 && tn) {
1489 ( YAFFS_TNODES_LEVEL0_BITS +
1490 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
1491 YAFFS_TNODES_INTERNAL_MASK;
1494 if((l>1) && !tn->internal[x]){
1495 /* Add missing non-level-zero tnode */
1496 tn->internal[x] = yaffs_GetTnode(dev);
1499 /* Looking from level 1 at level 0 */
1501 /* If we already have one, then release it.*/
1503 yaffs_FreeTnode(dev,tn->internal[x]);
1504 tn->internal[x] = passedTn;
1506 } else if(!tn->internal[x]) {
1507 /* Don't have one, none passed in */
1508 tn->internal[x] = yaffs_GetTnode(dev);
1512 tn = tn->internal[x];
1516 /* top is level 0 */
1518 memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1519 yaffs_FreeTnode(dev,passedTn);
1526 static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
1527 yaffs_ExtendedTags * tags, int objectId,
1532 for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
1533 if (yaffs_CheckChunkBit
1534 (dev, theChunk / dev->nChunksPerBlock,
1535 theChunk % dev->nChunksPerBlock)) {
1536 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
1538 if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
1550 /* DeleteWorker scans backwards through the tnode tree and deletes all the
1551 * chunks and tnodes in the file
1552 * Returns 1 if the tree was deleted.
1553 * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1556 static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
1557 int chunkOffset, int *limit)
1562 yaffs_ExtendedTags tags;
1564 yaffs_Device *dev = in->myDev;
1571 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1573 if (tn->internal[i]) {
1574 if (limit && (*limit) < 0) {
1578 yaffs_DeleteWorker(in,
1586 YAFFS_TNODES_INTERNAL_BITS)
1591 yaffs_FreeTnode(dev,
1594 tn->internal[i] = NULL;
1599 return (allDone) ? 1 : 0;
1600 } else if (level == 0) {
1603 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
1605 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
1610 YAFFS_TNODES_LEVEL0_BITS) + i;
1613 yaffs_FindChunkInGroup(dev,
1619 if (foundChunk > 0) {
1620 yaffs_DeleteChunk(dev,
1625 *limit = *limit - 1;
1633 yaffs_PutLevel0Tnode(dev,tn,i,0);
1637 return (i < 0) ? 1 : 0;
1647 static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
1650 yaffs_BlockInfo *theBlock;
1652 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1654 theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
1656 theBlock->softDeletions++;
1661 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1662 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1664 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1667 static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
1668 __u32 level, int chunkOffset)
1673 yaffs_Device *dev = in->myDev;
1678 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1680 if (tn->internal[i]) {
1682 yaffs_SoftDeleteWorker(in,
1688 YAFFS_TNODES_INTERNAL_BITS)
1691 yaffs_FreeTnode(dev,
1694 tn->internal[i] = NULL;
1696 /* Hoosterman... how could this happen? */
1700 return (allDone) ? 1 : 0;
1701 } else if (level == 0) {
1703 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1704 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
1706 /* Note this does not find the real chunk, only the chunk group.
1707 * We make an assumption that a chunk group is not larger than
1710 yaffs_SoftDeleteChunk(dev, theChunk);
1711 yaffs_PutLevel0Tnode(dev,tn,i,0);
1725 static void yaffs_SoftDeleteFile(yaffs_Object * obj)
1728 obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
1729 if (obj->nDataChunks <= 0) {
1730 /* Empty file with no duplicate object headers, just delete it immediately */
1731 yaffs_FreeTnode(obj->myDev,
1732 obj->variant.fileVariant.top);
1733 obj->variant.fileVariant.top = NULL;
1734 T(YAFFS_TRACE_TRACING,
1735 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1737 yaffs_DoGenericObjectDeletion(obj);
1739 yaffs_SoftDeleteWorker(obj,
1740 obj->variant.fileVariant.top,
1741 obj->variant.fileVariant.
1743 obj->softDeleted = 1;
1748 /* Pruning removes any part of the file structure tree that is beyond the
1749 * bounds of the file (ie that does not point to chunks).
1751 * A file should only get pruned when its size is reduced.
1753 * Before pruning, the chunks must be pulled from the tree and the
1754 * level 0 tnode entries must be zeroed out.
1755 * Could also use this for file deletion, but that's probably better handled
1756 * by a special case.
1759 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
1760 __u32 level, int del0)
1768 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1769 if (tn->internal[i] && level > 0) {
1771 yaffs_PruneWorker(dev, tn->internal[i],
1773 (i == 0) ? del0 : 1);
1776 if (tn->internal[i]) {
1781 if (hasData == 0 && del0) {
1782 /* Free and return NULL */
1784 yaffs_FreeTnode(dev, tn);
1794 static int yaffs_PruneFileStructure(yaffs_Device * dev,
1795 yaffs_FileStructure * fStruct)
1802 if (fStruct->topLevel > 0) {
1804 yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
1806 /* Now we have a tree with all the non-zero branches NULL but the height
1807 * is the same as it was.
1808 * Let's see if we can trim internal tnodes to shorten the tree.
1809 * We can do this if only the 0th element in the tnode is in use
1810 * (ie all the non-zero are NULL)
1813 while (fStruct->topLevel && !done) {
1817 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1818 if (tn->internal[i]) {
1824 fStruct->top = tn->internal[0];
1825 fStruct->topLevel--;
1826 yaffs_FreeTnode(dev, tn);
1836 /*-------------------- End of File Structure functions.-------------------*/
1838 /* yaffs_CreateFreeObjects creates a bunch more objects and
1839 * adds them to the object free list.
1841 static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
1844 yaffs_Object *newObjects;
1845 yaffs_ObjectList *list;
1850 /* make these things */
1851 newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1852 list = YMALLOC(sizeof(yaffs_ObjectList));
1854 if (!newObjects || !list) {
1859 T(YAFFS_TRACE_ALLOCATE,
1860 (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1864 /* Hook them into the free list */
1865 for (i = 0; i < nObjects - 1; i++) {
1866 newObjects[i].siblings.next =
1867 (struct list_head *)(&newObjects[i + 1]);
1870 newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1871 dev->freeObjects = newObjects;
1872 dev->nFreeObjects += nObjects;
1873 dev->nObjectsCreated += nObjects;
1875 /* Now add this bunch of Objects to a list for freeing up. */
1877 list->objects = newObjects;
1878 list->next = dev->allocatedObjectList;
1879 dev->allocatedObjectList = list;
1885 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1886 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
1888 yaffs_Object *tn = NULL;
1890 /* If there are none left make more */
1891 if (!dev->freeObjects) {
1892 yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
1895 if (dev->freeObjects) {
1896 tn = dev->freeObjects;
1898 (yaffs_Object *) (dev->freeObjects->siblings.next);
1899 dev->nFreeObjects--;
1901 /* Now sweeten it up... */
1903 memset(tn, 0, sizeof(yaffs_Object));
1906 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1907 INIT_LIST_HEAD(&(tn->hardLinks));
1908 INIT_LIST_HEAD(&(tn->hashLink));
1909 INIT_LIST_HEAD(&tn->siblings);
1911 /* Add it to the lost and found directory.
1912 * NB Can't put root or lostNFound in lostNFound so
1913 * check if lostNFound exists first
1915 if (dev->lostNFoundDir) {
1916 yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
1923 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
1928 yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1930 obj->fake = 1; /* it is fake so it has no NAND presence... */
1931 obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
1932 obj->unlinkAllowed = 0; /* ... or unlink it */
1935 obj->yst_mode = mode;
1937 obj->chunkId = 0; /* Not a valid chunk. */
1944 static void yaffs_UnhashObject(yaffs_Object * tn)
1947 yaffs_Device *dev = tn->myDev;
1949 /* If it is still linked into the bucket list, free from the list */
1950 if (!list_empty(&tn->hashLink)) {
1951 list_del_init(&tn->hashLink);
1952 bucket = yaffs_HashFunction(tn->objectId);
1953 dev->objectBucket[bucket].count--;
1958 /* FreeObject frees up a Object and puts it back on the free list */
1959 static void yaffs_FreeObject(yaffs_Object * tn)
1962 yaffs_Device *dev = tn->myDev;
1964 /* XXX U-BOOT XXX */
1968 /* We're still hooked up to a cached inode.
1969 * Don't delete now, but mark for later deletion
1971 tn->deferedFree = 1;
1976 yaffs_UnhashObject(tn);
1978 /* Link into the free list. */
1979 tn->siblings.next = (struct list_head *)(dev->freeObjects);
1980 dev->freeObjects = tn;
1981 dev->nFreeObjects++;
1984 /* XXX U-BOOT XXX */
1988 void yaffs_HandleDeferedFree(yaffs_Object * obj)
1990 if (obj->deferedFree) {
1991 yaffs_FreeObject(obj);
1998 static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
2000 /* Free the list of allocated Objects */
2002 yaffs_ObjectList *tmp;
2004 while (dev->allocatedObjectList) {
2005 tmp = dev->allocatedObjectList->next;
2006 YFREE(dev->allocatedObjectList->objects);
2007 YFREE(dev->allocatedObjectList);
2009 dev->allocatedObjectList = tmp;
2012 dev->freeObjects = NULL;
2013 dev->nFreeObjects = 0;
2016 static void yaffs_InitialiseObjects(yaffs_Device * dev)
2020 dev->allocatedObjectList = NULL;
2021 dev->freeObjects = NULL;
2022 dev->nFreeObjects = 0;
2024 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2025 INIT_LIST_HEAD(&dev->objectBucket[i].list);
2026 dev->objectBucket[i].count = 0;
2031 static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
2036 int lowest = 999999;
2038 /* First let's see if we can find one that's empty. */
2040 for (i = 0; i < 10 && lowest > 0; i++) {
2042 x %= YAFFS_NOBJECT_BUCKETS;
2043 if (dev->objectBucket[x].count < lowest) {
2044 lowest = dev->objectBucket[x].count;
2050 /* If we didn't find an empty list, then try
2051 * looking a bit further for a short one
2054 for (i = 0; i < 10 && lowest > 3; i++) {
2056 x %= YAFFS_NOBJECT_BUCKETS;
2057 if (dev->objectBucket[x].count < lowest) {
2058 lowest = dev->objectBucket[x].count;
2067 static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
2069 int bucket = yaffs_FindNiceObjectBucket(dev);
2071 /* Now find an object value that has not already been taken
2072 * by scanning the list.
2076 struct list_head *i;
2078 __u32 n = (__u32) bucket;
2080 /* yaffs_CheckObjectHashSanity(); */
2084 n += YAFFS_NOBJECT_BUCKETS;
2085 if (1 || dev->objectBucket[bucket].count > 0) {
2086 list_for_each(i, &dev->objectBucket[bucket].list) {
2087 /* If there is already one in the list */
2089 && list_entry(i, yaffs_Object,
2090 hashLink)->objectId == n) {
2101 static void yaffs_HashObject(yaffs_Object * in)
2103 int bucket = yaffs_HashFunction(in->objectId);
2104 yaffs_Device *dev = in->myDev;
2106 list_add(&in->hashLink, &dev->objectBucket[bucket].list);
2107 dev->objectBucket[bucket].count++;
2111 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
2113 int bucket = yaffs_HashFunction(number);
2114 struct list_head *i;
2117 list_for_each(i, &dev->objectBucket[bucket].list) {
2118 /* Look if it is in the list */
2120 in = list_entry(i, yaffs_Object, hashLink);
2121 if (in->objectId == number) {
2122 /* XXX U-BOOT XXX */
2125 /* Don't tell the VFS about this one if it is defered free */
2126 if (in->deferedFree)
2138 yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
2139 yaffs_ObjectType type)
2142 yaffs_Object *theObject;
2146 number = yaffs_CreateNewObjectNumber(dev);
2149 theObject = yaffs_AllocateEmptyObject(dev);
2153 if(type == YAFFS_OBJECT_TYPE_FILE){
2154 tn = yaffs_GetTnode(dev);
2156 yaffs_FreeObject(theObject);
2164 theObject->fake = 0;
2165 theObject->renameAllowed = 1;
2166 theObject->unlinkAllowed = 1;
2167 theObject->objectId = number;
2168 yaffs_HashObject(theObject);
2169 theObject->variantType = type;
2170 #ifdef CONFIG_YAFFS_WINCE
2171 yfsd_WinFileTimeNow(theObject->win_atime);
2172 theObject->win_ctime[0] = theObject->win_mtime[0] =
2173 theObject->win_atime[0];
2174 theObject->win_ctime[1] = theObject->win_mtime[1] =
2175 theObject->win_atime[1];
2179 theObject->yst_atime = theObject->yst_mtime =
2180 theObject->yst_ctime = Y_CURRENT_TIME;
2183 case YAFFS_OBJECT_TYPE_FILE:
2184 theObject->variant.fileVariant.fileSize = 0;
2185 theObject->variant.fileVariant.scannedFileSize = 0;
2186 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
2187 theObject->variant.fileVariant.topLevel = 0;
2188 theObject->variant.fileVariant.top = tn;
2190 case YAFFS_OBJECT_TYPE_DIRECTORY:
2191 INIT_LIST_HEAD(&theObject->variant.directoryVariant.
2194 case YAFFS_OBJECT_TYPE_SYMLINK:
2195 case YAFFS_OBJECT_TYPE_HARDLINK:
2196 case YAFFS_OBJECT_TYPE_SPECIAL:
2197 /* No action required */
2199 case YAFFS_OBJECT_TYPE_UNKNOWN:
2200 /* todo this should not happen */
2208 static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
2210 yaffs_ObjectType type)
2212 yaffs_Object *theObject = NULL;
2215 theObject = yaffs_FindObjectByNumber(dev, number);
2219 theObject = yaffs_CreateNewObject(dev, number, type);
2227 static YCHAR *yaffs_CloneString(const YCHAR * str)
2229 YCHAR *newStr = NULL;
2232 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
2234 yaffs_strcpy(newStr, str);
2242 * Mknod (create) a new object.
2243 * equivalentObject only has meaning for a hard link;
2244 * aliasString only has meaning for a sumlink.
2245 * rdev only has meaning for devices (a subset of special objects)
2248 static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
2249 yaffs_Object * parent,
2254 yaffs_Object * equivalentObject,
2255 const YCHAR * aliasString, __u32 rdev)
2260 yaffs_Device *dev = parent->myDev;
2262 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
2263 if (yaffs_FindObjectByName(parent, name)) {
2267 in = yaffs_CreateNewObject(dev, -1, type);
2269 if(type == YAFFS_OBJECT_TYPE_SYMLINK){
2270 str = yaffs_CloneString(aliasString);
2272 yaffs_FreeObject(in);
2282 in->variantType = type;
2284 in->yst_mode = mode;
2286 #ifdef CONFIG_YAFFS_WINCE
2287 yfsd_WinFileTimeNow(in->win_atime);
2288 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
2289 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
2292 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
2294 in->yst_rdev = rdev;
2298 in->nDataChunks = 0;
2300 yaffs_SetObjectName(in, name);
2303 yaffs_AddObjectToDirectory(parent, in);
2305 in->myDev = parent->myDev;
2308 case YAFFS_OBJECT_TYPE_SYMLINK:
2309 in->variant.symLinkVariant.alias = str;
2311 case YAFFS_OBJECT_TYPE_HARDLINK:
2312 in->variant.hardLinkVariant.equivalentObject =
2314 in->variant.hardLinkVariant.equivalentObjectId =
2315 equivalentObject->objectId;
2316 list_add(&in->hardLinks, &equivalentObject->hardLinks);
2318 case YAFFS_OBJECT_TYPE_FILE:
2319 case YAFFS_OBJECT_TYPE_DIRECTORY:
2320 case YAFFS_OBJECT_TYPE_SPECIAL:
2321 case YAFFS_OBJECT_TYPE_UNKNOWN:
2326 if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
2327 /* Could not create the object header, fail the creation */
2328 yaffs_DestroyObject(in);
2337 yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
2338 __u32 mode, __u32 uid, __u32 gid)
2340 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2341 uid, gid, NULL, NULL, 0);
2344 yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
2345 __u32 mode, __u32 uid, __u32 gid)
2347 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2348 mode, uid, gid, NULL, NULL, 0);
2351 yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
2352 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
2354 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2355 uid, gid, NULL, NULL, rdev);
2358 yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
2359 __u32 mode, __u32 uid, __u32 gid,
2360 const YCHAR * alias)
2362 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2363 uid, gid, NULL, alias, 0);
2366 /* yaffs_Link returns the object id of the equivalent object.*/
2367 yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
2368 yaffs_Object * equivalentObject)
2370 /* Get the real object in case we were fed a hard link as an equivalent object */
2371 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
2373 if (yaffs_MknodObject
2374 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2375 equivalentObject, NULL, 0)) {
2376 return equivalentObject;
2383 static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
2384 const YCHAR * newName, int force, int shadows)
2389 yaffs_Object *existingTarget;
2391 if (newDir == NULL) {
2392 newDir = obj->parent; /* use the old directory */
2395 if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
2396 T(YAFFS_TRACE_ALWAYS,
2398 ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
2403 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
2404 if (obj->myDev->isYaffs2) {
2405 unlinkOp = (newDir == obj->myDev->unlinkedDir);
2407 unlinkOp = (newDir == obj->myDev->unlinkedDir
2408 && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
2411 deleteOp = (newDir == obj->myDev->deletedDir);
2413 existingTarget = yaffs_FindObjectByName(newDir, newName);
2415 /* If the object is a file going into the unlinked directory,
2416 * then it is OK to just stuff it in since duplicate names are allowed.
2417 * else only proceed if the new name does not exist and if we're putting
2418 * it into a directory.
2425 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
2426 yaffs_SetObjectName(obj, newName);
2429 yaffs_AddObjectToDirectory(newDir, obj);
2434 /* If it is a deletion then we mark it as a shrink for gc purposes. */
2435 if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
2442 int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
2443 yaffs_Object * newDir, const YCHAR * newName)
2446 yaffs_Object *existingTarget;
2449 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2450 /* Special case for case insemsitive systems (eg. WinCE).
2451 * While look-up is case insensitive, the name isn't.
2452 * Therefore we might want to change x.txt to X.txt
2454 if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
2459 obj = yaffs_FindObjectByName(oldDir, oldName);
2460 /* Check new name to long. */
2461 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
2462 yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
2465 else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
2466 yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
2470 if (obj && obj->renameAllowed) {
2472 /* Now do the handling for an existing target, if there is one */
2474 existingTarget = yaffs_FindObjectByName(newDir, newName);
2475 if (existingTarget &&
2476 existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2477 !list_empty(&existingTarget->variant.directoryVariant.children)) {
2478 /* There is a target that is a non-empty directory, so we fail */
2479 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
2480 } else if (existingTarget && existingTarget != obj) {
2481 /* Nuke the target first, using shadowing,
2482 * but only if it isn't the same object
2484 yaffs_ChangeObjectName(obj, newDir, newName, force,
2485 existingTarget->objectId);
2486 yaffs_UnlinkObject(existingTarget);
2489 return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
2494 /*------------------------- Block Management and Page Allocation ----------------*/
2496 static int yaffs_InitialiseBlocks(yaffs_Device * dev)
2498 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
2500 dev->blockInfo = NULL;
2501 dev->chunkBits = NULL;
2503 dev->allocationBlock = -1; /* force it to get a new one */
2505 /* If the first allocation strategy fails, thry the alternate one */
2506 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2507 if(!dev->blockInfo){
2508 dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
2509 dev->blockInfoAlt = 1;
2512 dev->blockInfoAlt = 0;
2516 /* Set up dynamic blockinfo stuff. */
2517 dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
2518 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2519 if(!dev->chunkBits){
2520 dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
2521 dev->chunkBitsAlt = 1;
2524 dev->chunkBitsAlt = 0;
2527 if (dev->blockInfo && dev->chunkBits) {
2528 memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
2529 memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
2537 static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
2539 if(dev->blockInfoAlt && dev->blockInfo)
2540 YFREE_ALT(dev->blockInfo);
2541 else if(dev->blockInfo)
2542 YFREE(dev->blockInfo);
2544 dev->blockInfoAlt = 0;
2546 dev->blockInfo = NULL;
2548 if(dev->chunkBitsAlt && dev->chunkBits)
2549 YFREE_ALT(dev->chunkBits);
2550 else if(dev->chunkBits)
2551 YFREE(dev->chunkBits);
2552 dev->chunkBitsAlt = 0;
2553 dev->chunkBits = NULL;
2556 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
2557 yaffs_BlockInfo * bi)
2564 return 1; /* disqualification only applies to yaffs2. */
2566 if (!bi->hasShrinkHeader)
2567 return 1; /* can gc */
2569 /* Find the oldest dirty sequence number if we don't know it and save it
2570 * so we don't have to keep recomputing it.
2572 if (!dev->oldestDirtySequence) {
2573 seq = dev->sequenceNumber;
2575 for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
2577 b = yaffs_GetBlockInfo(dev, i);
2578 if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
2579 (b->pagesInUse - b->softDeletions) <
2580 dev->nChunksPerBlock && b->sequenceNumber < seq) {
2581 seq = b->sequenceNumber;
2584 dev->oldestDirtySequence = seq;
2587 /* Can't do gc of this block if there are any blocks older than this one that have
2590 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2594 /* FindDiretiestBlock is used to select the dirtiest block (or close enough)
2595 * for garbage collection.
2598 static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
2602 int b = dev->currentDirtyChecker;
2609 yaffs_BlockInfo *bi;
2610 int pendingPrioritisedExist = 0;
2612 /* First let's see if we need to grab a prioritised block */
2613 if(dev->hasPendingPrioritisedGCs){
2614 for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
2616 bi = yaffs_GetBlockInfo(dev, i);
2617 //yaffs_VerifyBlock(dev,bi,i);
2619 if(bi->gcPrioritise) {
2620 pendingPrioritisedExist = 1;
2621 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2622 yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
2623 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2626 aggressive = 1; /* Fool the non-aggressive skip logiv below */
2631 if(!pendingPrioritisedExist) /* None found, so we can clear this */
2632 dev->hasPendingPrioritisedGCs = 0;
2635 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2637 * else (we're doing a leasurely gc), then we only bother to do this if the
2638 * block has only a few pages in use.
2641 dev->nonAggressiveSkip--;
2643 if (!aggressive && (dev->nonAggressiveSkip > 0)) {
2649 (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2653 dev->internalEndBlock - dev->internalStartBlock + 1;
2656 dev->internalEndBlock - dev->internalStartBlock + 1;
2657 iterations = iterations / 16;
2658 if (iterations > 200) {
2663 for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
2665 if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
2666 b = dev->internalStartBlock;
2669 if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
2670 T(YAFFS_TRACE_ERROR,
2671 (TSTR("**>> Block %d is not valid" TENDSTR), b));
2675 bi = yaffs_GetBlockInfo(dev, b);
2678 if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
2685 if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2686 (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
2687 yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
2689 pagesInUse = (bi->pagesInUse - bi->softDeletions);
2693 dev->currentDirtyChecker = b;
2697 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
2698 dev->nChunksPerBlock - pagesInUse,prioritised));
2701 dev->oldestDirtySequence = 0;
2704 dev->nonAggressiveSkip = 4;
2710 static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
2712 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
2716 /* If the block is still healthy erase it and mark as clean.
2717 * If the block has had a data failure, then retire it.
2720 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2721 (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
2722 blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
2724 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2726 if (!bi->needsRetiring) {
2727 yaffs_InvalidateCheckpoint(dev);
2728 erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
2730 dev->nErasureFailures++;
2731 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2732 (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
2737 ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
2739 for (i = 0; i < dev->nChunksPerBlock; i++) {
2740 if (!yaffs_CheckChunkErased
2741 (dev, blockNo * dev->nChunksPerBlock + i)) {
2742 T(YAFFS_TRACE_ERROR,
2744 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2745 TENDSTR), blockNo, i));
2751 /* Clean it up... */
2752 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2753 dev->nErasedBlocks++;
2755 bi->softDeletions = 0;
2756 bi->hasShrinkHeader = 0;
2757 bi->skipErasedCheck = 1; /* This is clean, so no need to check */
2758 bi->gcPrioritise = 0;
2759 yaffs_ClearChunkBits(dev, blockNo);
2761 T(YAFFS_TRACE_ERASE,
2762 (TSTR("Erased block %d" TENDSTR), blockNo));
2764 dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
2766 yaffs_RetireBlock(dev, blockNo);
2767 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2768 (TSTR("**>> Block %d retired" TENDSTR), blockNo));
2772 static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
2776 yaffs_BlockInfo *bi;
2778 if (dev->nErasedBlocks < 1) {
2779 /* Hoosterman we've got a problem.
2780 * Can't get space to gc
2782 T(YAFFS_TRACE_ERROR,
2783 (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2788 /* Find an empty block. */
2790 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
2791 dev->allocationBlockFinder++;
2792 if (dev->allocationBlockFinder < dev->internalStartBlock
2793 || dev->allocationBlockFinder > dev->internalEndBlock) {
2794 dev->allocationBlockFinder = dev->internalStartBlock;
2797 bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
2799 if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
2800 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2801 dev->sequenceNumber++;
2802 bi->sequenceNumber = dev->sequenceNumber;
2803 dev->nErasedBlocks--;
2804 T(YAFFS_TRACE_ALLOCATE,
2805 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
2806 dev->allocationBlockFinder, dev->sequenceNumber,
2807 dev->nErasedBlocks));
2808 return dev->allocationBlockFinder;
2812 T(YAFFS_TRACE_ALWAYS,
2814 ("yaffs tragedy: no more eraased blocks, but there should have been %d"
2815 TENDSTR), dev->nErasedBlocks));
2821 // Check if there's space to allocate...
2822 // Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
2823 static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
2826 int reservedBlocks = dev->nReservedBlocks;
2827 int checkpointBlocks;
2829 checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
2830 if(checkpointBlocks < 0)
2831 checkpointBlocks = 0;
2833 reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
2835 return (dev->nFreeChunks > reservedChunks);
2838 static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
2841 yaffs_BlockInfo *bi;
2843 if (dev->allocationBlock < 0) {
2844 /* Get next block to allocate off */
2845 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2846 dev->allocationPage = 0;
2849 if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
2850 /* Not enough space to allocate unless we're allowed to use the reserve. */
2854 if (dev->nErasedBlocks < dev->nReservedBlocks
2855 && dev->allocationPage == 0) {
2856 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
2859 /* Next page please.... */
2860 if (dev->allocationBlock >= 0) {
2861 bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
2863 retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
2864 dev->allocationPage;
2866 yaffs_SetChunkBit(dev, dev->allocationBlock,
2867 dev->allocationPage);
2869 dev->allocationPage++;
2873 /* If the block is full set the state to full */
2874 if (dev->allocationPage >= dev->nChunksPerBlock) {
2875 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2876 dev->allocationBlock = -1;
2885 T(YAFFS_TRACE_ERROR,
2886 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2891 static int yaffs_GetErasedChunks(yaffs_Device * dev)
2895 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2897 if (dev->allocationBlock > 0) {
2898 n += (dev->nChunksPerBlock - dev->allocationPage);
2905 static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
2911 int retVal = YAFFS_OK;
2914 int isCheckpointBlock;
2917 int chunksBefore = yaffs_GetErasedChunks(dev);
2920 yaffs_ExtendedTags tags;
2922 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
2924 yaffs_Object *object;
2926 isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
2928 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2930 T(YAFFS_TRACE_TRACING,
2931 (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
2932 bi->pagesInUse, bi->hasShrinkHeader));
2934 /*yaffs_VerifyFreeChunks(dev); */
2936 bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
2938 /* Take off the number of soft deleted entries because
2939 * they're going to get really deleted during GC.
2941 dev->nFreeChunks -= bi->softDeletions;
2945 if (isCheckpointBlock ||
2946 !yaffs_StillSomeChunkBits(dev, block)) {
2947 T(YAFFS_TRACE_TRACING,
2949 ("Collecting block %d that has no chunks in use" TENDSTR),
2951 yaffs_BlockBecameDirty(dev, block);
2954 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
2956 yaffs_VerifyBlock(dev,bi,block);
2958 for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
2959 chunkInBlock < dev->nChunksPerBlock
2960 && yaffs_StillSomeChunkBits(dev, block);
2961 chunkInBlock++, oldChunk++) {
2962 if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
2964 /* This page is in use and might need to be copied off */
2968 yaffs_InitialiseTags(&tags);
2970 yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
2974 yaffs_FindObjectByNumber(dev,
2977 T(YAFFS_TRACE_GC_DETAIL,
2979 ("Collecting page %d, %d %d %d " TENDSTR),
2980 chunkInBlock, tags.objectId, tags.chunkId,
2983 if(object && !yaffs_SkipVerification(dev)){
2984 if(tags.chunkId == 0)
2985 matchingChunk = object->chunkId;
2986 else if(object->softDeleted)
2987 matchingChunk = oldChunk; /* Defeat the test */
2989 matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
2991 if(oldChunk != matchingChunk)
2992 T(YAFFS_TRACE_ERROR,
2993 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2994 oldChunk,matchingChunk,tags.objectId, tags.chunkId));
2999 T(YAFFS_TRACE_ERROR,
3001 ("page %d in gc has no object: %d %d %d "
3003 tags.objectId, tags.chunkId, tags.byteCount));
3006 if (object && object->deleted
3007 && tags.chunkId != 0) {
3008 /* Data chunk in a deleted file, throw it away
3009 * It's a soft deleted data chunk,
3010 * No need to copy this, just forget about it and
3011 * fix up the object.
3014 object->nDataChunks--;
3016 if (object->nDataChunks <= 0) {
3017 /* remeber to clean up the object */
3018 dev->gcCleanupList[cleanups] =
3024 /* Todo object && object->deleted && object->nDataChunks == 0 */
3026 /* Deleted object header with no data chunks.
3027 * Can be discarded and the file deleted.
3029 object->chunkId = 0;
3030 yaffs_FreeTnode(object->myDev,
3033 object->variant.fileVariant.top = NULL;
3034 yaffs_DoGenericObjectDeletion(object);
3036 } else if (object) {
3037 /* It's either a data chunk in a live file or
3038 * an ObjectHeader, so we're interested in it.
3039 * NB Need to keep the ObjectHeaders of deleted files
3040 * until the whole file has been deleted off
3042 tags.serialNumber++;
3046 if (tags.chunkId == 0) {
3047 /* It is an object Id,
3048 * We need to nuke the shrinkheader flags first
3049 * We no longer want the shrinkHeader flag since its work is done
3050 * and if it is left in place it will mess up scanning.
3051 * Also, clear out any shadowing stuff
3054 yaffs_ObjectHeader *oh;
3055 oh = (yaffs_ObjectHeader *)buffer;
3057 oh->shadowsObject = -1;
3058 tags.extraShadows = 0;
3059 tags.extraIsShrinkHeader = 0;
3061 yaffs_VerifyObjectHeader(object,oh,&tags,1);
3065 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
3068 retVal = YAFFS_FAIL;
3071 /* Ok, now fix up the Tnodes etc. */
3073 if (tags.chunkId == 0) {
3075 object->chunkId = newChunk;
3076 object->serial = tags.serialNumber;
3078 /* It's a data chunk */
3079 yaffs_PutChunkIntoFile
3087 yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
3092 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3095 /* Do any required cleanups */
3096 for (i = 0; i < cleanups; i++) {
3097 /* Time to delete the file too */
3099 yaffs_FindObjectByNumber(dev,
3100 dev->gcCleanupList[i]);
3102 yaffs_FreeTnode(dev,
3103 object->variant.fileVariant.
3105 object->variant.fileVariant.top = NULL;
3108 ("yaffs: About to finally delete object %d"
3109 TENDSTR), object->objectId));
3110 yaffs_DoGenericObjectDeletion(object);
3111 object->myDev->nDeletedFiles--;
3118 yaffs_VerifyCollectedBlock(dev,bi,block);
3120 if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
3123 ("gc did not increase free chunks before %d after %d"
3124 TENDSTR), chunksBefore, chunksAfter));
3132 /* New garbage collector
3133 * If we're very low on erased blocks then we do aggressive garbage collection
3134 * otherwise we do "leasurely" garbage collection.
3135 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
3136 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
3138 * The idea is to help clear out space in a more spread-out manner.
3139 * Dunno if it really does anything useful.
3141 static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
3145 int gcOk = YAFFS_OK;
3148 int checkpointBlockAdjust;
3150 if (dev->isDoingGC) {
3151 /* Bail out so we don't get recursive gc */
3155 /* This loop should pass the first time.
3156 * We'll only see looping here if the erase of the collected block fails.
3162 checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
3163 if(checkpointBlockAdjust < 0)
3164 checkpointBlockAdjust = 0;
3166 if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
3167 /* We need a block soon...*/
3170 /* We're in no hurry */
3174 block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
3177 dev->garbageCollections++;
3179 dev->passiveGarbageCollections++;
3184 ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
3185 dev->nErasedBlocks, aggressive));
3187 gcOk = yaffs_GarbageCollectBlock(dev, block);
3190 if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
3193 ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
3194 TENDSTR), dev->nErasedBlocks, maxTries, block));
3196 } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
3199 return aggressive ? gcOk : YAFFS_OK;
3202 /*------------------------- TAGS --------------------------------*/
3204 static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3207 return (tags->chunkId == chunkInObject &&
3208 tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
3213 /*-------------------- Data file manipulation -----------------*/
3215 static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3216 yaffs_ExtendedTags * tags)
3218 /*Get the Tnode, then get the level 0 offset chunk offset */
3221 yaffs_ExtendedTags localTags;
3224 yaffs_Device *dev = in->myDev;
3227 /* Passed a NULL, so use our own tags space */
3231 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3234 theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
3237 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3243 static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
3244 yaffs_ExtendedTags * tags)
3246 /* Get the Tnode, then get the level 0 offset chunk offset */
3249 yaffs_ExtendedTags localTags;
3251 yaffs_Device *dev = in->myDev;
3255 /* Passed a NULL, so use our own tags space */
3259 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3263 theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
3266 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3269 /* Delete the entry in the filestructure (if found) */
3271 yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
3274 /*T(("No level 0 found for %d\n", chunkInInode)); */
3278 /* T(("Could not find %d to delete\n",chunkInInode)); */
3283 #ifdef YAFFS_PARANOID
3285 static int yaffs_CheckFileSanity(yaffs_Object * in)
3293 yaffs_Tags localTags;
3294 yaffs_Tags *tags = &localTags;
3298 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
3299 /* T(("Object not a file\n")); */
3303 objId = in->objectId;
3304 fSize = in->variant.fileVariant.fileSize;
3306 (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
3308 for (chunk = 1; chunk <= nChunks; chunk++) {
3309 tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
3314 theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
3316 if (yaffs_CheckChunkBits
3317 (dev, theChunk / dev->nChunksPerBlock,
3318 theChunk % dev->nChunksPerBlock)) {
3320 yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
3324 (tags, in->objectId, chunk, chunkDeleted)) {
3334 /* T(("No level 0 found for %d\n", chunk)); */
3338 return failed ? YAFFS_FAIL : YAFFS_OK;
3343 static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3344 int chunkInNAND, int inScan)
3346 /* NB inScan is zero unless scanning.
3347 * For forward scanning, inScan is > 0;
3348 * for backward scanning inScan is < 0
3352 yaffs_Device *dev = in->myDev;
3354 yaffs_ExtendedTags existingTags;
3355 yaffs_ExtendedTags newTags;
3356 unsigned existingSerial, newSerial;
3358 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
3359 /* Just ignore an attempt at putting a chunk into a non-file during scanning
3360 * If it is not during Scanning then something went wrong!
3363 T(YAFFS_TRACE_ERROR,
3365 ("yaffs tragedy:attempt to put data chunk into a non-file"
3370 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
3374 tn = yaffs_AddOrFindLevel0Tnode(dev,
3375 &in->variant.fileVariant,
3382 existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
3385 /* If we're scanning then we need to test for duplicates
3386 * NB This does not need to be efficient since it should only ever
3387 * happen when the power fails during a write, then only one
3388 * chunk should ever be affected.
3390 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3391 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3394 if (existingChunk != 0) {
3395 /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
3396 * thus we have to do a FindChunkInFile to get the real chunk id.
3398 * We have a duplicate now we need to decide which one to use:
3400 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3401 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
3402 * YAFFS1: Get both sets of tags and compare serial numbers.
3406 /* Only do this for forward scanning */
3407 yaffs_ReadChunkWithTagsFromNAND(dev,
3411 /* Do a proper find */
3413 yaffs_FindChunkInFile(in, chunkInInode,
3417 if (existingChunk <= 0) {
3418 /*Hoosterman - how did this happen? */
3420 T(YAFFS_TRACE_ERROR,
3422 ("yaffs tragedy: existing chunk < 0 in scan"
3427 /* NB The deleted flags should be false, otherwise the chunks will
3428 * not be loaded during a scan
3431 newSerial = newTags.serialNumber;
3432 existingSerial = existingTags.serialNumber;
3435 (in->myDev->isYaffs2 ||
3436 existingChunk <= 0 ||
3437 ((existingSerial + 1) & 3) == newSerial)) {
3438 /* Forward scanning.
3440 * Delete the old one and drop through to update the tnode
3442 yaffs_DeleteChunk(dev, existingChunk, 1,
3445 /* Backward scanning or we want to use the existing one
3447 * Delete the new one and return early so that the tnode isn't changed
3449 yaffs_DeleteChunk(dev, chunkInNAND, 1,
3457 if (existingChunk == 0) {
3461 yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
3466 static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
3469 int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
3471 if (chunkInNAND >= 0) {
3472 return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
3475 T(YAFFS_TRACE_NANDACCESS,
3476 (TSTR("Chunk %d not found zero instead" TENDSTR),
3478 /* get sane (zero) data if you read a hole */
3479 memset(buffer, 0, in->myDev->nDataBytesPerChunk);
3485 void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
3489 yaffs_ExtendedTags tags;
3490 yaffs_BlockInfo *bi;
3497 block = chunkId / dev->nChunksPerBlock;
3498 page = chunkId % dev->nChunksPerBlock;
3501 if(!yaffs_CheckChunkBit(dev,block,page))
3502 T(YAFFS_TRACE_VERIFY,
3503 (TSTR("Deleting invalid chunk %d"TENDSTR),
3506 bi = yaffs_GetBlockInfo(dev, block);
3508 T(YAFFS_TRACE_DELETION,
3509 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
3512 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
3514 yaffs_InitialiseTags(&tags);
3516 tags.chunkDeleted = 1;
3518 yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
3519 yaffs_HandleUpdateChunk(dev, chunkId, &tags);
3521 dev->nUnmarkedDeletions++;
3524 /* Pull out of the management area.
3525 * If the whole block became dirty, this will kick off an erasure.
3527 if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3528 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3529 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3530 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
3533 yaffs_ClearChunkBit(dev, block, page);
3537 if (bi->pagesInUse == 0 &&
3538 !bi->hasShrinkHeader &&
3539 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3540 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3541 yaffs_BlockBecameDirty(dev, block);
3545 /* T(("Bad news deleting chunk %d\n",chunkId)); */
3550 static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
3551 const __u8 * buffer, int nBytes,
3554 /* Find old chunk Need to do this to get serial number
3555 * Write new one and patch into tree.
3556 * Invalidate old tags.
3560 yaffs_ExtendedTags prevTags;
3563 yaffs_ExtendedTags newTags;
3565 yaffs_Device *dev = in->myDev;
3567 yaffs_CheckGarbageCollection(dev);
3569 /* Get the previous chunk at this location in the file if it exists */
3570 prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
3572 /* Set up new tags */
3573 yaffs_InitialiseTags(&newTags);
3575 newTags.chunkId = chunkInInode;
3576 newTags.objectId = in->objectId;
3577 newTags.serialNumber =
3578 (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3579 newTags.byteCount = nBytes;
3582 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3585 if (newChunkId >= 0) {
3586 yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
3588 if (prevChunkId >= 0) {
3589 yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
3593 yaffs_CheckFileSanity(in);
3599 /* UpdateObjectHeader updates the header on NAND for an object.
3600 * If name is not NULL, then that new name is used.
3602 int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
3603 int isShrink, int shadows)
3606 yaffs_BlockInfo *bi;
3608 yaffs_Device *dev = in->myDev;
3615 yaffs_ExtendedTags newTags;
3616 yaffs_ExtendedTags oldTags;
3618 __u8 *buffer = NULL;
3619 YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
3621 yaffs_ObjectHeader *oh = NULL;
3623 yaffs_strcpy(oldName,"silly old name");
3625 if (!in->fake || force) {
3627 yaffs_CheckGarbageCollection(dev);
3628 yaffs_CheckObjectDetailsLoaded(in);
3630 buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
3631 oh = (yaffs_ObjectHeader *) buffer;
3633 prevChunkId = in->chunkId;
3635 if (prevChunkId >= 0) {
3636 result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
3639 yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
3641 memcpy(oldName, oh->name, sizeof(oh->name));
3644 memset(buffer, 0xFF, dev->nDataBytesPerChunk);
3646 oh->type = in->variantType;
3647 oh->yst_mode = in->yst_mode;
3648 oh->shadowsObject = shadows;
3650 #ifdef CONFIG_YAFFS_WINCE
3651 oh->win_atime[0] = in->win_atime[0];
3652 oh->win_ctime[0] = in->win_ctime[0];
3653 oh->win_mtime[0] = in->win_mtime[0];
3654 oh->win_atime[1] = in->win_atime[1];
3655 oh->win_ctime[1] = in->win_ctime[1];
3656 oh->win_mtime[1] = in->win_mtime[1];
3658 oh->yst_uid = in->yst_uid;
3659 oh->yst_gid = in->yst_gid;
3660 oh->yst_atime = in->yst_atime;
3661 oh->yst_mtime = in->yst_mtime;
3662 oh->yst_ctime = in->yst_ctime;
3663 oh->yst_rdev = in->yst_rdev;
3666 oh->parentObjectId = in->parent->objectId;
3668 oh->parentObjectId = 0;
3671 if (name && *name) {
3672 memset(oh->name, 0, sizeof(oh->name));
3673 yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
3674 } else if (prevChunkId>=0) {
3675 memcpy(oh->name, oldName, sizeof(oh->name));
3677 memset(oh->name, 0, sizeof(oh->name));
3680 oh->isShrink = isShrink;
3682 switch (in->variantType) {
3683 case YAFFS_OBJECT_TYPE_UNKNOWN:
3684 /* Should not happen */
3686 case YAFFS_OBJECT_TYPE_FILE:
3688 (oh->parentObjectId == YAFFS_OBJECTID_DELETED
3689 || oh->parentObjectId ==
3690 YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
3691 fileVariant.fileSize;
3693 case YAFFS_OBJECT_TYPE_HARDLINK:
3694 oh->equivalentObjectId =
3695 in->variant.hardLinkVariant.equivalentObjectId;
3697 case YAFFS_OBJECT_TYPE_SPECIAL:
3700 case YAFFS_OBJECT_TYPE_DIRECTORY:
3703 case YAFFS_OBJECT_TYPE_SYMLINK:
3704 yaffs_strncpy(oh->alias,
3705 in->variant.symLinkVariant.alias,
3706 YAFFS_MAX_ALIAS_LENGTH);
3707 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3712 yaffs_InitialiseTags(&newTags);
3714 newTags.chunkId = 0;
3715 newTags.objectId = in->objectId;
3716 newTags.serialNumber = in->serial;
3718 /* Add extra info for file header */
3720 newTags.extraHeaderInfoAvailable = 1;
3721 newTags.extraParentObjectId = oh->parentObjectId;
3722 newTags.extraFileLength = oh->fileSize;
3723 newTags.extraIsShrinkHeader = oh->isShrink;
3724 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3725 newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
3726 newTags.extraObjectType = in->variantType;
3728 yaffs_VerifyObjectHeader(in,oh,&newTags,1);
3730 /* Create new chunk in NAND */
3732 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3733 (prevChunkId >= 0) ? 1 : 0);
3735 if (newChunkId >= 0) {
3737 in->chunkId = newChunkId;
3739 if (prevChunkId >= 0) {
3740 yaffs_DeleteChunk(dev, prevChunkId, 1,
3744 if(!yaffs_ObjectHasCachedWriteData(in))
3747 /* If this was a shrink, then mark the block that the chunk lives on */
3749 bi = yaffs_GetBlockInfo(in->myDev,
3750 newChunkId /in->myDev-> nChunksPerBlock);
3751 bi->hasShrinkHeader = 1;
3756 retVal = newChunkId;
3761 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3766 /*------------------------ Short Operations Cache ----------------------------------------
3767 * In many situations where there is no high level buffering (eg WinCE) a lot of
3768 * reads might be short sequential reads, and a lot of writes may be short
3769 * sequential writes. eg. scanning/writing a jpeg file.
3770 * In these cases, a short read/write cache can provide a huge perfomance benefit
3771 * with dumb-as-a-rock code.
3772 * In Linux, the page cache provides read buffering aand the short op cache provides write
3775 * There are a limited number (~10) of cache chunks per device so that we don't
3776 * need a very intelligent search.
3779 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
3781 yaffs_Device *dev = obj->myDev;
3783 yaffs_ChunkCache *cache;
3784 int nCaches = obj->myDev->nShortOpCaches;
3786 for(i = 0; i < nCaches; i++){
3787 cache = &dev->srCache[i];
3788 if (cache->object == obj &&
3797 static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
3799 yaffs_Device *dev = obj->myDev;
3800 int lowest = -99; /* Stop compiler whining. */
3802 yaffs_ChunkCache *cache;
3803 int chunkWritten = 0;
3804 int nCaches = obj->myDev->nShortOpCaches;
3810 /* Find the dirty cache for this object with the lowest chunk id. */
3811 for (i = 0; i < nCaches; i++) {
3812 if (dev->srCache[i].object == obj &&
3813 dev->srCache[i].dirty) {
3815 || dev->srCache[i].chunkId <
3817 cache = &dev->srCache[i];
3818 lowest = cache->chunkId;
3823 if (cache && !cache->locked) {
3824 /* Write it out and free it up */
3827 yaffs_WriteChunkDataToObject(cache->object,
3833 cache->object = NULL;
3836 } while (cache && chunkWritten > 0);
3839 /* Hoosterman, disk full while writing cache out. */
3840 T(YAFFS_TRACE_ERROR,
3841 (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3848 /*yaffs_FlushEntireDeviceCache(dev)
3853 void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
3856 int nCaches = dev->nShortOpCaches;
3859 /* Find a dirty object in the cache and flush it...
3860 * until there are no further dirty objects.
3864 for( i = 0; i < nCaches && !obj; i++) {
3865 if (dev->srCache[i].object &&
3866 dev->srCache[i].dirty)
3867 obj = dev->srCache[i].object;
3871 yaffs_FlushFilesChunkCache(obj);
3878 /* Grab us a cache chunk for use.
3879 * First look for an empty one.
3880 * Then look for the least recently used non-dirty one.
3881 * Then look for the least recently used dirty one...., flush and look again.
3883 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
3889 if (dev->nShortOpCaches > 0) {
3890 for (i = 0; i < dev->nShortOpCaches; i++) {
3891 if (!dev->srCache[i].object)
3892 return &dev->srCache[i];
3898 usage = 0; /* just to stop the compiler grizzling */
3900 for (i = 0; i < dev->nShortOpCaches; i++) {
3901 if (!dev->srCache[i].dirty &&
3902 ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
3904 usage = dev->srCache[i].lastUse;
3910 return theOne >= 0 ? &dev->srCache[theOne] : NULL;
3917 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
3919 yaffs_ChunkCache *cache;
3920 yaffs_Object *theObj;
3925 if (dev->nShortOpCaches > 0) {
3926 /* Try find a non-dirty one... */
3928 cache = yaffs_GrabChunkCacheWorker(dev);
3931 /* They were all dirty, find the last recently used object and flush
3932 * its cache, then find again.
3933 * NB what's here is not very accurate, we actually flush the object
3934 * the last recently used page.
3937 /* With locking we can't assume we can use entry zero */
3944 for (i = 0; i < dev->nShortOpCaches; i++) {
3945 if (dev->srCache[i].object &&
3946 !dev->srCache[i].locked &&
3947 (dev->srCache[i].lastUse < usage || !cache))
3949 usage = dev->srCache[i].lastUse;
3950 theObj = dev->srCache[i].object;
3951 cache = &dev->srCache[i];
3956 if (!cache || cache->dirty) {
3957 /* Flush and try again */
3958 yaffs_FlushFilesChunkCache(theObj);
3959 cache = yaffs_GrabChunkCacheWorker(dev);
3969 /* Find a cached chunk */
3970 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
3973 yaffs_Device *dev = obj->myDev;
3975 if (dev->nShortOpCaches > 0) {
3976 for (i = 0; i < dev->nShortOpCaches; i++) {
3977 if (dev->srCache[i].object == obj &&
3978 dev->srCache[i].chunkId == chunkId) {
3981 return &dev->srCache[i];
3988 /* Mark the chunk for the least recently used algorithym */
3989 static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
3993 if (dev->nShortOpCaches > 0) {
3994 if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
3995 /* Reset the cache usages */
3997 for (i = 1; i < dev->nShortOpCaches; i++) {
3998 dev->srCache[i].lastUse = 0;
4005 cache->lastUse = dev->srLastUse;
4013 /* Invalidate a single cache page.
4014 * Do this when a whole page gets written,
4015 * ie the short cache for this page is no longer valid.
4017 static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
4019 if (object->myDev->nShortOpCaches > 0) {
4020 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
4023 cache->object = NULL;
4028 /* Invalidate all the cache pages associated with this object
4029 * Do this whenever ther file is deleted or resized.
4031 static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
4034 yaffs_Device *dev = in->myDev;
4036 if (dev->nShortOpCaches > 0) {
4037 /* Invalidate it. */
4038 for (i = 0; i < dev->nShortOpCaches; i++) {
4039 if (dev->srCache[i].object == in) {
4040 dev->srCache[i].object = NULL;
4046 /*--------------------- Checkpointing --------------------*/
4049 static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
4051 yaffs_CheckpointValidity cp;
4053 memset(&cp,0,sizeof(cp));
4055 cp.structType = sizeof(cp);
4056 cp.magic = YAFFS_MAGIC;
4057 cp.version = YAFFS_CHECKPOINT_VERSION;
4058 cp.head = (head) ? 1 : 0;
4060 return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
4064 static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
4066 yaffs_CheckpointValidity cp;
4069 ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
4072 ok = (cp.structType == sizeof(cp)) &&
4073 (cp.magic == YAFFS_MAGIC) &&
4074 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
4075 (cp.head == ((head) ? 1 : 0));
4079 static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
4082 cp->nErasedBlocks = dev->nErasedBlocks;
4083 cp->allocationBlock = dev->allocationBlock;
4084 cp->allocationPage = dev->allocationPage;
4085 cp->nFreeChunks = dev->nFreeChunks;
4087 cp->nDeletedFiles = dev->nDeletedFiles;
4088 cp->nUnlinkedFiles = dev->nUnlinkedFiles;
4089 cp->nBackgroundDeletions = dev->nBackgroundDeletions;
4090 cp->sequenceNumber = dev->sequenceNumber;
4091 cp->oldestDirtySequence = dev->oldestDirtySequence;
4095 static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
4096 yaffs_CheckpointDevice *cp)
4098 dev->nErasedBlocks = cp->nErasedBlocks;
4099 dev->allocationBlock = cp->allocationBlock;
4100 dev->allocationPage = cp->allocationPage;
4101 dev->nFreeChunks = cp->nFreeChunks;
4103 dev->nDeletedFiles = cp->nDeletedFiles;
4104 dev->nUnlinkedFiles = cp->nUnlinkedFiles;
4105 dev->nBackgroundDeletions = cp->nBackgroundDeletions;
4106 dev->sequenceNumber = cp->sequenceNumber;
4107 dev->oldestDirtySequence = cp->oldestDirtySequence;
4111 static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
4113 yaffs_CheckpointDevice cp;
4115 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
4119 /* Write device runtime values*/
4120 yaffs_DeviceToCheckpointDevice(&cp,dev);
4121 cp.structType = sizeof(cp);
4123 ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
4125 /* Write block info */
4127 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
4128 ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
4131 /* Write chunk bits */
4133 nBytes = nBlocks * dev->chunkBitmapStride;
4134 ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
4140 static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
4142 yaffs_CheckpointDevice cp;
4144 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
4148 ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
4152 if(cp.structType != sizeof(cp))
4156 yaffs_CheckpointDeviceToDevice(dev,&cp);
4158 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
4160 ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
4164 nBytes = nBlocks * dev->chunkBitmapStride;
4166 ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
4171 static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
4175 cp->objectId = obj->objectId;
4176 cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
4177 cp->chunkId = obj->chunkId;
4178 cp->variantType = obj->variantType;
4179 cp->deleted = obj->deleted;
4180 cp->softDeleted = obj->softDeleted;
4181 cp->unlinked = obj->unlinked;
4182 cp->fake = obj->fake;
4183 cp->renameAllowed = obj->renameAllowed;
4184 cp->unlinkAllowed = obj->unlinkAllowed;
4185 cp->serial = obj->serial;
4186 cp->nDataChunks = obj->nDataChunks;
4188 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4189 cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
4190 else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4191 cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
4194 static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
4197 yaffs_Object *parent;
4199 obj->objectId = cp->objectId;
4202 parent = yaffs_FindOrCreateObjectByNumber(
4205 YAFFS_OBJECT_TYPE_DIRECTORY);
4210 yaffs_AddObjectToDirectory(parent, obj);
4212 obj->chunkId = cp->chunkId;
4213 obj->variantType = cp->variantType;
4214 obj->deleted = cp->deleted;
4215 obj->softDeleted = cp->softDeleted;
4216 obj->unlinked = cp->unlinked;
4217 obj->fake = cp->fake;
4218 obj->renameAllowed = cp->renameAllowed;
4219 obj->unlinkAllowed = cp->unlinkAllowed;
4220 obj->serial = cp->serial;
4221 obj->nDataChunks = cp->nDataChunks;
4223 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4224 obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
4225 else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4226 obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
4228 if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
4229 obj->lazyLoaded = 1;
4234 static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
4235 __u32 level, int chunkOffset)
4238 yaffs_Device *dev = in->myDev;
4240 int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4245 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4246 if (tn->internal[i]) {
4247 ok = yaffs_CheckpointTnodeWorker(in,
4250 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4253 } else if (level == 0) {
4254 __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
4255 /* printf("write tnode at %d\n",baseOffset); */
4256 ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
4258 ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
4266 static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
4268 __u32 endMarker = ~0;
4271 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
4272 ok = yaffs_CheckpointTnodeWorker(obj,
4273 obj->variant.fileVariant.top,
4274 obj->variant.fileVariant.topLevel,
4277 ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
4284 static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
4288 yaffs_Device *dev = obj->myDev;
4289 yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
4293 ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
4295 while(ok && (~baseChunk)){
4297 /* Read level 0 tnode */
4300 /* printf("read tnode at %d\n",baseChunk); */
4301 tn = yaffs_GetTnodeRaw(dev);
4303 ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
4304 (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4309 ok = yaffs_AddOrFindLevel0Tnode(dev,
4317 ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
4321 T(YAFFS_TRACE_CHECKPOINT,(
4322 TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
4323 nread,baseChunk,ok));
4329 static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
4332 yaffs_CheckpointObject cp;
4335 struct list_head *lh;
4338 /* Iterate through the objects in each hash entry,
4339 * dumping them to the checkpointing stream.
4342 for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
4343 list_for_each(lh, &dev->objectBucket[i].list) {
4345 obj = list_entry(lh, yaffs_Object, hashLink);
4346 if (!obj->deferedFree) {
4347 yaffs_ObjectToCheckpointObject(&cp,obj);
4348 cp.structType = sizeof(cp);
4350 T(YAFFS_TRACE_CHECKPOINT,(
4351 TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
4352 cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
4354 ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
4356 if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
4357 ok = yaffs_WriteCheckpointTnodes(obj);
4364 /* Dump end of list */
4365 memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
4366 cp.structType = sizeof(cp);
4369 ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
4374 static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
4377 yaffs_CheckpointObject cp;
4380 yaffs_Object *hardList = NULL;
4382 while(ok && !done) {
4383 ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
4384 if(cp.structType != sizeof(cp)) {
4385 T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
4386 cp.structType,sizeof(cp),ok));
4390 T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
4391 cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
4393 if(ok && cp.objectId == ~0)
4396 obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
4398 yaffs_CheckpointObjectToObject(obj,&cp);
4399 if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
4400 ok = yaffs_ReadCheckpointTnodes(obj);
4401 } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
4402 obj->hardLinks.next =
4403 (struct list_head *)
4413 yaffs_HardlinkFixup(dev,hardList);
4418 static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
4420 __u32 checkpointSum;
4423 yaffs_GetCheckpointSum(dev,&checkpointSum);
4425 ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
4433 static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
4435 __u32 checkpointSum0;
4436 __u32 checkpointSum1;
4439 yaffs_GetCheckpointSum(dev,&checkpointSum0);
4441 ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
4446 if(checkpointSum0 != checkpointSum1)
4453 static int yaffs_WriteCheckpointData(yaffs_Device *dev)
4458 if(dev->skipCheckpointWrite || !dev->isYaffs2){
4459 T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
4464 ok = yaffs_CheckpointOpen(dev,1);
4467 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
4468 ok = yaffs_WriteCheckpointValidityMarker(dev,1);
4471 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
4472 ok = yaffs_WriteCheckpointDevice(dev);
4475 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
4476 ok = yaffs_WriteCheckpointObjects(dev);
4479 T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
4480 ok = yaffs_WriteCheckpointValidityMarker(dev,0);
4484 ok = yaffs_WriteCheckpointSum(dev);
4488 if(!yaffs_CheckpointClose(dev))
4492 dev->isCheckpointed = 1;
4494 dev->isCheckpointed = 0;
4496 return dev->isCheckpointed;
4499 static int yaffs_ReadCheckpointData(yaffs_Device *dev)
4503 if(dev->skipCheckpointRead || !dev->isYaffs2){
4504 T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
4509 ok = yaffs_CheckpointOpen(dev,0); /* open for read */
4512 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
4513 ok = yaffs_ReadCheckpointValidityMarker(dev,1);
4516 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
4517 ok = yaffs_ReadCheckpointDevice(dev);
4520 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
4521 ok = yaffs_ReadCheckpointObjects(dev);
4524 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
4525 ok = yaffs_ReadCheckpointValidityMarker(dev,0);
4529 ok = yaffs_ReadCheckpointSum(dev);
4530 T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
4533 if(!yaffs_CheckpointClose(dev))
4537 dev->isCheckpointed = 1;
4539 dev->isCheckpointed = 0;
4545 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
4547 if(dev->isCheckpointed ||
4548 dev->blocksInCheckpoint > 0){
4549 dev->isCheckpointed = 0;
4550 yaffs_CheckpointInvalidateStream(dev);
4551 if(dev->superBlock && dev->markSuperBlockDirty)
4552 dev->markSuperBlockDirty(dev->superBlock);
4557 int yaffs_CheckpointSave(yaffs_Device *dev)
4560 T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4562 yaffs_VerifyObjects(dev);
4563 yaffs_VerifyBlocks(dev);
4564 yaffs_VerifyFreeChunks(dev);
4566 if(!dev->isCheckpointed) {
4567 yaffs_InvalidateCheckpoint(dev);
4568 yaffs_WriteCheckpointData(dev);
4571 T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4573 return dev->isCheckpointed;
4576 int yaffs_CheckpointRestore(yaffs_Device *dev)
4579 T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4581 retval = yaffs_ReadCheckpointData(dev);
4583 if(dev->isCheckpointed){
4584 yaffs_VerifyObjects(dev);
4585 yaffs_VerifyBlocks(dev);
4586 yaffs_VerifyFreeChunks(dev);
4589 T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
4594 /*--------------------- File read/write ------------------------
4595 * Read and write have very similar structures.
4596 * In general the read/write has three parts to it
4597 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
4598 * Some complete chunks
4599 * An incomplete chunk to end off with
4601 * Curve-balls: the first chunk might also be the last chunk.
4604 int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
4613 yaffs_ChunkCache *cache;
4620 //chunk = offset / dev->nDataBytesPerChunk + 1;
4621 //start = offset % dev->nDataBytesPerChunk;
4622 yaffs_AddrToChunk(dev,offset,&chunk,&start);
4625 /* OK now check for the curveball where the start and end are in
4628 if ((start + n) < dev->nDataBytesPerChunk) {
4631 nToCopy = dev->nDataBytesPerChunk - start;
4634 cache = yaffs_FindChunkCache(in, chunk);
4636 /* If the chunk is already in the cache or it is less than a whole chunk
4637 * then use the cache (if there is caching)
4638 * else bypass the cache.
4640 if (cache || nToCopy != dev->nDataBytesPerChunk) {
4641 if (dev->nShortOpCaches > 0) {
4643 /* If we can't find the data in the cache, then load it up. */
4646 cache = yaffs_GrabChunkCache(in->myDev);
4648 cache->chunkId = chunk;
4651 yaffs_ReadChunkDataFromObject(in, chunk,
4657 yaffs_UseChunkCache(dev, cache, 0);
4661 #ifdef CONFIG_YAFFS_WINCE
4662 yfsd_UnlockYAFFS(TRUE);
4664 memcpy(buffer, &cache->data[start], nToCopy);
4666 #ifdef CONFIG_YAFFS_WINCE
4667 yfsd_LockYAFFS(TRUE);
4671 /* Read into the local buffer then copy..*/
4674 yaffs_GetTempBuffer(dev, __LINE__);
4675 yaffs_ReadChunkDataFromObject(in, chunk,
4677 #ifdef CONFIG_YAFFS_WINCE
4678 yfsd_UnlockYAFFS(TRUE);
4680 memcpy(buffer, &localBuffer[start], nToCopy);
4682 #ifdef CONFIG_YAFFS_WINCE
4683 yfsd_LockYAFFS(TRUE);
4685 yaffs_ReleaseTempBuffer(dev, localBuffer,
4690 #ifdef CONFIG_YAFFS_WINCE
4691 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4693 /* Under WinCE can't do direct transfer. Need to use a local buffer.
4694 * This is because we otherwise screw up WinCE's memory mapper
4696 yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
4698 #ifdef CONFIG_YAFFS_WINCE
4699 yfsd_UnlockYAFFS(TRUE);
4701 memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
4703 #ifdef CONFIG_YAFFS_WINCE
4704 yfsd_LockYAFFS(TRUE);
4705 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
4709 /* A full chunk. Read directly into the supplied buffer. */
4710 yaffs_ReadChunkDataFromObject(in, chunk, buffer);
4724 int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
4725 int nBytes, int writeThrough)
4734 int startOfWrite = offset;
4735 int chunkWritten = 0;
4742 while (n > 0 && chunkWritten >= 0) {
4743 //chunk = offset / dev->nDataBytesPerChunk + 1;
4744 //start = offset % dev->nDataBytesPerChunk;
4745 yaffs_AddrToChunk(dev,offset,&chunk,&start);
4748 /* OK now check for the curveball where the start and end are in
4752 if ((start + n) < dev->nDataBytesPerChunk) {
4755 /* Now folks, to calculate how many bytes to write back....
4756 * If we're overwriting and not writing to then end of file then
4757 * we need to write back as much as was there before.
4761 in->variant.fileVariant.fileSize -
4762 ((chunk - 1) * dev->nDataBytesPerChunk);
4764 if (nBytesRead > dev->nDataBytesPerChunk) {
4765 nBytesRead = dev->nDataBytesPerChunk;
4770 (start + n)) ? nBytesRead : (start + n);
4773 nToCopy = dev->nDataBytesPerChunk - start;
4774 nToWriteBack = dev->nDataBytesPerChunk;
4777 if (nToCopy != dev->nDataBytesPerChunk) {
4778 /* An incomplete start or end chunk (or maybe both start and end chunk) */
4779 if (dev->nShortOpCaches > 0) {
4780 yaffs_ChunkCache *cache;
4781 /* If we can't find the data in the cache, then load the cache */
4782 cache = yaffs_FindChunkCache(in, chunk);
4785 && yaffs_CheckSpaceForAllocation(in->
4787 cache = yaffs_GrabChunkCache(in->myDev);
4789 cache->chunkId = chunk;
4792 yaffs_ReadChunkDataFromObject(in, chunk,
4798 !yaffs_CheckSpaceForAllocation(in->myDev)){
4799 /* Drop the cache if it was a read cache item and
4800 * no space check has been made for it.
4806 yaffs_UseChunkCache(dev, cache, 1);
4808 #ifdef CONFIG_YAFFS_WINCE
4809 yfsd_UnlockYAFFS(TRUE);
4812 memcpy(&cache->data[start], buffer,
4815 #ifdef CONFIG_YAFFS_WINCE
4816 yfsd_LockYAFFS(TRUE);
4819 cache->nBytes = nToWriteBack;
4823 yaffs_WriteChunkDataToObject
4826 cache->data, cache->nBytes,
4832 chunkWritten = -1; /* fail the write */
4835 /* An incomplete start or end chunk (or maybe both start and end chunk)
4836 * Read into the local buffer then copy, then copy over and write back.
4840 yaffs_GetTempBuffer(dev, __LINE__);
4842 yaffs_ReadChunkDataFromObject(in, chunk,
4845 #ifdef CONFIG_YAFFS_WINCE
4846 yfsd_UnlockYAFFS(TRUE);
4849 memcpy(&localBuffer[start], buffer, nToCopy);
4851 #ifdef CONFIG_YAFFS_WINCE
4852 yfsd_LockYAFFS(TRUE);
4855 yaffs_WriteChunkDataToObject(in, chunk,
4860 yaffs_ReleaseTempBuffer(dev, localBuffer,
4867 #ifdef CONFIG_YAFFS_WINCE
4868 /* Under WinCE can't do direct transfer. Need to use a local buffer.
4869 * This is because we otherwise screw up WinCE's memory mapper
4871 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4872 #ifdef CONFIG_YAFFS_WINCE
4873 yfsd_UnlockYAFFS(TRUE);
4875 memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
4876 #ifdef CONFIG_YAFFS_WINCE
4877 yfsd_LockYAFFS(TRUE);
4880 yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
4881 dev->nDataBytesPerChunk,
4883 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
4885 /* A full chunk. Write directly from the supplied buffer. */
4887 yaffs_WriteChunkDataToObject(in, chunk, buffer,
4888 dev->nDataBytesPerChunk,
4891 /* Since we've overwritten the cached data, we better invalidate it. */
4892 yaffs_InvalidateChunkCache(in, chunk);
4895 if (chunkWritten >= 0) {
4904 /* Update file object */
4906 if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
4907 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4916 /* ---------------------- File resizing stuff ------------------ */
4918 static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
4921 yaffs_Device *dev = in->myDev;
4922 int oldFileSize = in->variant.fileVariant.fileSize;
4924 int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
4926 int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
4927 dev->nDataBytesPerChunk;
4931 /* Delete backwards so that we don't end up with holes if
4932 * power is lost part-way through the operation.
4934 for (i = lastDel; i >= startDel; i--) {
4935 /* NB this could be optimised somewhat,
4936 * eg. could retrieve the tags and write them without
4937 * using yaffs_DeleteChunk
4940 chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
4943 (dev->internalStartBlock * dev->nChunksPerBlock)
4945 ((dev->internalEndBlock +
4946 1) * dev->nChunksPerBlock)) {
4947 T(YAFFS_TRACE_ALWAYS,
4948 (TSTR("Found daft chunkId %d for %d" TENDSTR),
4952 yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
4959 int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
4962 int oldFileSize = in->variant.fileVariant.fileSize;
4963 int newSizeOfPartialChunk;
4966 yaffs_Device *dev = in->myDev;
4968 yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
4970 yaffs_FlushFilesChunkCache(in);
4971 yaffs_InvalidateWholeChunkCache(in);
4973 yaffs_CheckGarbageCollection(dev);
4975 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
4976 return yaffs_GetFileSize(in);
4979 if (newSize == oldFileSize) {
4983 if (newSize < oldFileSize) {
4985 yaffs_PruneResizedChunks(in, newSize);
4987 if (newSizeOfPartialChunk != 0) {
4988 int lastChunk = 1 + newFullChunks;
4990 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4992 /* Got to read and rewrite the last chunk with its new size and zero pad */
4993 yaffs_ReadChunkDataFromObject(in, lastChunk,
4996 memset(localBuffer + newSizeOfPartialChunk, 0,
4997 dev->nDataBytesPerChunk - newSizeOfPartialChunk);
4999 yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
5000 newSizeOfPartialChunk, 1);
5002 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
5005 in->variant.fileVariant.fileSize = newSize;
5007 yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
5009 /* newsSize > oldFileSize */
5010 in->variant.fileVariant.fileSize = newSize;
5015 /* Write a new object header.
5016 * show we've shrunk the file, if need be
5017 * Do this only if the file is not in the deleted directories.
5019 if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
5020 in->parent->objectId != YAFFS_OBJECTID_DELETED) {
5021 yaffs_UpdateObjectHeader(in, NULL, 0,
5022 (newSize < oldFileSize) ? 1 : 0, 0);
5028 loff_t yaffs_GetFileSize(yaffs_Object * obj)
5030 obj = yaffs_GetEquivalentObject(obj);
5032 switch (obj->variantType) {
5033 case YAFFS_OBJECT_TYPE_FILE:
5034 return obj->variant.fileVariant.fileSize;
5035 case YAFFS_OBJECT_TYPE_SYMLINK:
5036 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5044 int yaffs_FlushFile(yaffs_Object * in, int updateTime)
5048 yaffs_FlushFilesChunkCache(in);
5050 #ifdef CONFIG_YAFFS_WINCE
5051 yfsd_WinFileTimeNow(in->win_mtime);
5054 in->yst_mtime = Y_CURRENT_TIME;
5060 (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
5061 0) ? YAFFS_OK : YAFFS_FAIL;
5070 static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
5073 /* First off, invalidate the file's data in the cache, without flushing. */
5074 yaffs_InvalidateWholeChunkCache(in);
5076 if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
5077 /* Move to the unlinked directory so we have a record that it was deleted. */
5078 yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
5082 yaffs_RemoveObjectFromDirectory(in);
5083 yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
5086 yaffs_FreeObject(in);
5091 /* yaffs_DeleteFile deletes the whole file data
5092 * and the inode associated with the file.
5093 * It does not delete the links associated with the file.
5095 static int yaffs_UnlinkFile(yaffs_Object * in)
5099 int immediateDeletion = 0;
5102 /* XXX U-BOOT XXX */
5106 immediateDeletion = 1;
5111 if (in->inUse <= 0) {
5112 immediateDeletion = 1;
5116 if (immediateDeletion) {
5118 yaffs_ChangeObjectName(in, in->myDev->deletedDir,
5120 T(YAFFS_TRACE_TRACING,
5121 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
5124 in->myDev->nDeletedFiles++;
5125 if (0 && in->myDev->isYaffs2) {
5126 yaffs_ResizeFile(in, 0);
5128 yaffs_SoftDeleteFile(in);
5131 yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
5139 int yaffs_DeleteFile(yaffs_Object * in)
5141 int retVal = YAFFS_OK;
5143 if (in->nDataChunks > 0) {
5144 /* Use soft deletion if there is data in the file */
5145 if (!in->unlinked) {
5146 retVal = yaffs_UnlinkFile(in);
5148 if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
5150 in->myDev->nDeletedFiles++;
5151 yaffs_SoftDeleteFile(in);
5153 return in->deleted ? YAFFS_OK : YAFFS_FAIL;
5155 /* The file has no data chunks so we toss it immediately */
5156 yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
5157 in->variant.fileVariant.top = NULL;
5158 yaffs_DoGenericObjectDeletion(in);
5164 static int yaffs_DeleteDirectory(yaffs_Object * in)
5166 /* First check that the directory is empty. */
5167 if (list_empty(&in->variant.directoryVariant.children)) {
5168 return yaffs_DoGenericObjectDeletion(in);
5175 static int yaffs_DeleteSymLink(yaffs_Object * in)
5177 YFREE(in->variant.symLinkVariant.alias);
5179 return yaffs_DoGenericObjectDeletion(in);
5182 static int yaffs_DeleteHardLink(yaffs_Object * in)
5184 /* remove this hardlink from the list assocaited with the equivalent
5187 list_del(&in->hardLinks);
5188 return yaffs_DoGenericObjectDeletion(in);
5191 static void yaffs_DestroyObject(yaffs_Object * obj)
5193 switch (obj->variantType) {
5194 case YAFFS_OBJECT_TYPE_FILE:
5195 yaffs_DeleteFile(obj);
5197 case YAFFS_OBJECT_TYPE_DIRECTORY:
5198 yaffs_DeleteDirectory(obj);
5200 case YAFFS_OBJECT_TYPE_SYMLINK:
5201 yaffs_DeleteSymLink(obj);
5203 case YAFFS_OBJECT_TYPE_HARDLINK:
5204 yaffs_DeleteHardLink(obj);
5206 case YAFFS_OBJECT_TYPE_SPECIAL:
5207 yaffs_DoGenericObjectDeletion(obj);
5209 case YAFFS_OBJECT_TYPE_UNKNOWN:
5210 break; /* should not happen. */
5214 static int yaffs_UnlinkWorker(yaffs_Object * obj)
5217 if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
5218 return yaffs_DeleteHardLink(obj);
5219 } else if (!list_empty(&obj->hardLinks)) {
5220 /* Curve ball: We're unlinking an object that has a hardlink.
5222 * This problem arises because we are not strictly following
5223 * The Linux link/inode model.
5225 * We can't really delete the object.
5226 * Instead, we do the following:
5227 * - Select a hardlink.
5228 * - Unhook it from the hard links
5229 * - Unhook it from its parent directory (so that the rename can work)
5230 * - Rename the object to the hardlink's name.
5231 * - Delete the hardlink
5236 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
5238 hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
5240 list_del_init(&hl->hardLinks);
5241 list_del_init(&hl->siblings);
5243 yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
5245 retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
5247 if (retVal == YAFFS_OK) {
5248 retVal = yaffs_DoGenericObjectDeletion(hl);
5253 switch (obj->variantType) {
5254 case YAFFS_OBJECT_TYPE_FILE:
5255 return yaffs_UnlinkFile(obj);
5257 case YAFFS_OBJECT_TYPE_DIRECTORY:
5258 return yaffs_DeleteDirectory(obj);
5260 case YAFFS_OBJECT_TYPE_SYMLINK:
5261 return yaffs_DeleteSymLink(obj);
5263 case YAFFS_OBJECT_TYPE_SPECIAL:
5264 return yaffs_DoGenericObjectDeletion(obj);
5266 case YAFFS_OBJECT_TYPE_HARDLINK:
5267 case YAFFS_OBJECT_TYPE_UNKNOWN:
5275 static int yaffs_UnlinkObject( yaffs_Object *obj)
5278 if (obj && obj->unlinkAllowed) {
5279 return yaffs_UnlinkWorker(obj);
5285 int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
5289 obj = yaffs_FindObjectByName(dir, name);
5290 return yaffs_UnlinkObject(obj);
5293 /*----------------------- Initialisation Scanning ---------------------- */
5295 static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
5296 int backwardScanning)
5300 if (!backwardScanning) {
5301 /* Handle YAFFS1 forward scanning case
5302 * For YAFFS1 we always do the deletion
5306 /* Handle YAFFS2 case (backward scanning)
5307 * If the shadowed object exists then ignore.
5309 if (yaffs_FindObjectByNumber(dev, objId)) {
5314 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
5315 * We put it in unlinked dir to be cleaned up after the scanning
5318 yaffs_FindOrCreateObjectByNumber(dev, objId,
5319 YAFFS_OBJECT_TYPE_FILE);
5320 yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
5321 obj->variant.fileVariant.shrinkSize = 0;
5322 obj->valid = 1; /* So that we don't read any other info for this file */
5332 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
5339 hardList = (yaffs_Object *) (hardList->hardLinks.next);
5341 in = yaffs_FindObjectByNumber(dev,
5342 hl->variant.hardLinkVariant.
5343 equivalentObjectId);
5346 /* Add the hardlink pointers */
5347 hl->variant.hardLinkVariant.equivalentObject = in;
5348 list_add(&hl->hardLinks, &in->hardLinks);
5350 /* Todo Need to report/handle this better.
5351 * Got a problem... hardlink to a non-existant object
5353 hl->variant.hardLinkVariant.equivalentObject = NULL;
5354 INIT_LIST_HEAD(&hl->hardLinks);
5366 static int ybicmp(const void *a, const void *b){
5367 register int aseq = ((yaffs_BlockIndex *)a)->seq;
5368 register int bseq = ((yaffs_BlockIndex *)b)->seq;
5369 register int ablock = ((yaffs_BlockIndex *)a)->block;
5370 register int bblock = ((yaffs_BlockIndex *)b)->block;
5372 return ablock - bblock;
5378 static int yaffs_Scan(yaffs_Device * dev)
5380 yaffs_ExtendedTags tags;
5385 int nBlocksToScan = 0;
5391 yaffs_BlockState state;
5392 yaffs_Object *hardList = NULL;
5393 yaffs_BlockInfo *bi;
5395 yaffs_ObjectHeader *oh;
5397 yaffs_Object *parent;
5398 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5400 int alloc_failed = 0;
5405 yaffs_BlockIndex *blockIndex = NULL;
5407 if (dev->isYaffs2) {
5409 (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
5413 //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
5416 (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
5417 dev->internalStartBlock, dev->internalEndBlock));
5419 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
5421 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
5423 if (dev->isYaffs2) {
5424 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
5429 /* Scan all the blocks to determine their state */
5430 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
5431 bi = yaffs_GetBlockInfo(dev, blk);
5432 yaffs_ClearChunkBits(dev, blk);
5434 bi->softDeletions = 0;
5436 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
5438 bi->blockState = state;
5439 bi->sequenceNumber = sequenceNumber;
5441 T(YAFFS_TRACE_SCAN_DEBUG,
5442 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
5443 state, sequenceNumber));
5445 if (state == YAFFS_BLOCK_STATE_DEAD) {
5446 T(YAFFS_TRACE_BAD_BLOCKS,
5447 (TSTR("block %d is bad" TENDSTR), blk));
5448 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
5449 T(YAFFS_TRACE_SCAN_DEBUG,
5450 (TSTR("Block empty " TENDSTR)));
5451 dev->nErasedBlocks++;
5452 dev->nFreeChunks += dev->nChunksPerBlock;
5453 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
5455 /* Determine the highest sequence number */
5456 if (dev->isYaffs2 &&
5457 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
5458 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
5460 blockIndex[nBlocksToScan].seq = sequenceNumber;
5461 blockIndex[nBlocksToScan].block = blk;
5465 if (sequenceNumber >= dev->sequenceNumber) {
5466 dev->sequenceNumber = sequenceNumber;
5468 } else if (dev->isYaffs2) {
5469 /* TODO: Nasty sequence number! */
5472 ("Block scanning block %d has bad sequence number %d"
5473 TENDSTR), blk, sequenceNumber));
5480 * Dungy old bubble sort for now...
5482 if (dev->isYaffs2) {
5483 yaffs_BlockIndex temp;
5487 for (i = 0; i < nBlocksToScan; i++)
5488 for (j = i + 1; j < nBlocksToScan; j++)
5489 if (blockIndex[i].seq > blockIndex[j].seq) {
5490 temp = blockIndex[j];
5491 blockIndex[j] = blockIndex[i];
5492 blockIndex[i] = temp;
5496 /* Now scan the blocks looking at the data. */
5497 if (dev->isYaffs2) {
5499 endIterator = nBlocksToScan - 1;
5500 T(YAFFS_TRACE_SCAN_DEBUG,
5501 (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
5503 startIterator = dev->internalStartBlock;
5504 endIterator = dev->internalEndBlock;
5507 /* For each block.... */
5508 for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
5511 if (dev->isYaffs2) {
5512 /* get the block to scan in the correct order */
5513 blk = blockIndex[blockIterator].block;
5515 blk = blockIterator;
5518 bi = yaffs_GetBlockInfo(dev, blk);
5519 state = bi->blockState;
5523 /* For each chunk in each block that needs scanning....*/
5524 for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
5525 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
5526 /* Read the tags and decide what to do */
5527 chunk = blk * dev->nChunksPerBlock + c;
5529 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
5532 /* Let's have a good look at this chunk... */
5534 if (!dev->isYaffs2 && tags.chunkDeleted) {
5540 /*T((" %d %d deleted\n",blk,c)); */
5541 } else if (!tags.chunkUsed) {
5542 /* An unassigned chunk in the block
5543 * This means that either the block is empty or
5544 * this is the one being allocated from
5548 /* We're looking at the first chunk in the block so the block is unused */
5549 state = YAFFS_BLOCK_STATE_EMPTY;
5550 dev->nErasedBlocks++;
5552 /* this is the block being allocated from */
5555 (" Allocating from %d %d" TENDSTR),
5557 state = YAFFS_BLOCK_STATE_ALLOCATING;
5558 dev->allocationBlock = blk;
5559 dev->allocationPage = c;
5560 dev->allocationBlockFinder = blk;
5561 /* Set it to here to encourage the allocator to go forth from here. */
5563 /* Yaffs2 sanity check:
5564 * This should be the one with the highest sequence number
5567 && (dev->sequenceNumber !=
5568 bi->sequenceNumber)) {
5569 T(YAFFS_TRACE_ALWAYS,
5571 ("yaffs: Allocation block %d was not highest sequence id:"
5572 " block seq = %d, dev seq = %d"
5573 TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
5577 dev->nFreeChunks += (dev->nChunksPerBlock - c);
5578 } else if (tags.chunkId > 0) {
5579 /* chunkId > 0 so it is a data chunk... */
5580 unsigned int endpos;
5582 yaffs_SetChunkBit(dev, blk, c);
5585 in = yaffs_FindOrCreateObjectByNumber(dev,
5588 YAFFS_OBJECT_TYPE_FILE);
5589 /* PutChunkIntoFile checks for a clash (two data chunks with
5590 * the same chunkId).
5597 if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
5602 (tags.chunkId - 1) * dev->nDataBytesPerChunk +
5605 in->variantType == YAFFS_OBJECT_TYPE_FILE
5606 && in->variant.fileVariant.scannedFileSize <
5608 in->variant.fileVariant.
5609 scannedFileSize = endpos;
5610 if (!dev->useHeaderFileSize) {
5611 in->variant.fileVariant.
5613 in->variant.fileVariant.
5618 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
5620 /* chunkId == 0, so it is an ObjectHeader.
5621 * Thus, we read in the object header and make the object
5623 yaffs_SetChunkBit(dev, blk, c);
5626 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
5630 oh = (yaffs_ObjectHeader *) chunkData;
5632 in = yaffs_FindObjectByNumber(dev,
5634 if (in && in->variantType != oh->type) {
5635 /* This should not happen, but somehow
5636 * Wev'e ended up with an objectId that has been reused but not yet
5637 * deleted, and worse still it has changed type. Delete the old object.
5640 yaffs_DestroyObject(in);
5645 in = yaffs_FindOrCreateObjectByNumber(dev,
5653 if (in && oh->shadowsObject > 0) {
5654 yaffs_HandleShadowedObject(dev,
5660 if (in && in->valid) {
5661 /* We have already filled this one. We have a duplicate and need to resolve it. */
5663 unsigned existingSerial = in->serial;
5664 unsigned newSerial = tags.serialNumber;
5666 if (dev->isYaffs2 ||
5667 ((existingSerial + 1) & 3) ==
5669 /* Use new one - destroy the exisiting one */
5670 yaffs_DeleteChunk(dev,
5675 /* Use existing - destroy this one. */
5676 yaffs_DeleteChunk(dev, chunk, 1,
5681 if (in && !in->valid &&
5682 (tags.objectId == YAFFS_OBJECTID_ROOT ||
5683 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
5684 /* We only load some info, don't fiddle with directory structure */
5686 in->variantType = oh->type;
5688 in->yst_mode = oh->yst_mode;
5689 #ifdef CONFIG_YAFFS_WINCE
5690 in->win_atime[0] = oh->win_atime[0];
5691 in->win_ctime[0] = oh->win_ctime[0];
5692 in->win_mtime[0] = oh->win_mtime[0];
5693 in->win_atime[1] = oh->win_atime[1];
5694 in->win_ctime[1] = oh->win_ctime[1];
5695 in->win_mtime[1] = oh->win_mtime[1];
5697 in->yst_uid = oh->yst_uid;
5698 in->yst_gid = oh->yst_gid;
5699 in->yst_atime = oh->yst_atime;
5700 in->yst_mtime = oh->yst_mtime;
5701 in->yst_ctime = oh->yst_ctime;
5702 in->yst_rdev = oh->yst_rdev;
5704 in->chunkId = chunk;
5706 } else if (in && !in->valid) {
5707 /* we need to load this info */
5710 in->variantType = oh->type;
5712 in->yst_mode = oh->yst_mode;
5713 #ifdef CONFIG_YAFFS_WINCE
5714 in->win_atime[0] = oh->win_atime[0];
5715 in->win_ctime[0] = oh->win_ctime[0];
5716 in->win_mtime[0] = oh->win_mtime[0];
5717 in->win_atime[1] = oh->win_atime[1];
5718 in->win_ctime[1] = oh->win_ctime[1];
5719 in->win_mtime[1] = oh->win_mtime[1];
5721 in->yst_uid = oh->yst_uid;
5722 in->yst_gid = oh->yst_gid;
5723 in->yst_atime = oh->yst_atime;
5724 in->yst_mtime = oh->yst_mtime;
5725 in->yst_ctime = oh->yst_ctime;
5726 in->yst_rdev = oh->yst_rdev;
5728 in->chunkId = chunk;
5730 yaffs_SetObjectName(in, oh->name);
5733 /* directory stuff...
5738 yaffs_FindOrCreateObjectByNumber
5739 (dev, oh->parentObjectId,
5740 YAFFS_OBJECT_TYPE_DIRECTORY);
5741 if (parent->variantType ==
5742 YAFFS_OBJECT_TYPE_UNKNOWN) {
5743 /* Set up as a directory */
5744 parent->variantType =
5745 YAFFS_OBJECT_TYPE_DIRECTORY;
5746 INIT_LIST_HEAD(&parent->variant.
5749 } else if (parent->variantType !=
5750 YAFFS_OBJECT_TYPE_DIRECTORY)
5752 /* Hoosterman, another problem....
5753 * We're trying to use a non-directory as a directory
5756 T(YAFFS_TRACE_ERROR,
5758 ("yaffs tragedy: attempting to use non-directory as"
5759 " a directory in scan. Put in lost+found."
5761 parent = dev->lostNFoundDir;
5764 yaffs_AddObjectToDirectory(parent, in);
5766 if (0 && (parent == dev->deletedDir ||
5767 parent == dev->unlinkedDir)) {
5768 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
5769 dev->nDeletedFiles++;
5771 /* Note re hardlinks.
5772 * Since we might scan a hardlink before its equivalent object is scanned
5773 * we put them all in a list.
5774 * After scanning is complete, we should have all the objects, so we run through this
5775 * list and fix up all the chains.
5778 switch (in->variantType) {
5779 case YAFFS_OBJECT_TYPE_UNKNOWN:
5780 /* Todo got a problem */
5782 case YAFFS_OBJECT_TYPE_FILE:
5785 /* Prune back the shrunken chunks */
5786 yaffs_PruneResizedChunks
5788 /* Mark the block as having a shrinkHeader */
5789 bi->hasShrinkHeader = 1;
5792 if (dev->useHeaderFileSize)
5794 in->variant.fileVariant.
5799 case YAFFS_OBJECT_TYPE_HARDLINK:
5800 in->variant.hardLinkVariant.
5801 equivalentObjectId =
5802 oh->equivalentObjectId;
5803 in->hardLinks.next =
5804 (struct list_head *)
5808 case YAFFS_OBJECT_TYPE_DIRECTORY:
5811 case YAFFS_OBJECT_TYPE_SPECIAL:
5814 case YAFFS_OBJECT_TYPE_SYMLINK:
5815 in->variant.symLinkVariant.alias =
5816 yaffs_CloneString(oh->alias);
5817 if(!in->variant.symLinkVariant.alias)
5822 if (parent == dev->deletedDir) {
5823 yaffs_DestroyObject(in);
5824 bi->hasShrinkHeader = 1;
5830 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
5831 /* If we got this far while scanning, then the block is fully allocated.*/
5832 state = YAFFS_BLOCK_STATE_FULL;
5835 bi->blockState = state;
5837 /* Now let's see if it was dirty */
5838 if (bi->pagesInUse == 0 &&
5839 !bi->hasShrinkHeader &&
5840 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
5841 yaffs_BlockBecameDirty(dev, blk);
5851 /* Ok, we've done all the scanning.
5852 * Fix up the hard link chains.
5853 * We should now have scanned all the objects, now it's time to add these
5857 yaffs_HardlinkFixup(dev,hardList);
5859 /* Handle the unlinked files. Since they were left in an unlinked state we should
5863 struct list_head *i;
5864 struct list_head *n;
5867 /* Soft delete all the unlinked files */
5868 list_for_each_safe(i, n,
5869 &dev->unlinkedDir->variant.directoryVariant.
5872 l = list_entry(i, yaffs_Object, siblings);
5873 yaffs_DestroyObject(l);
5878 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
5884 T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
5890 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
5893 yaffs_ObjectHeader *oh;
5894 yaffs_Device *dev = in->myDev;
5895 yaffs_ExtendedTags tags;
5897 int alloc_failed = 0;
5903 T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
5905 in->lazyLoaded ? "not yet" : "already"));
5910 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
5912 result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
5913 oh = (yaffs_ObjectHeader *) chunkData;
5915 in->yst_mode = oh->yst_mode;
5916 #ifdef CONFIG_YAFFS_WINCE
5917 in->win_atime[0] = oh->win_atime[0];
5918 in->win_ctime[0] = oh->win_ctime[0];
5919 in->win_mtime[0] = oh->win_mtime[0];
5920 in->win_atime[1] = oh->win_atime[1];
5921 in->win_ctime[1] = oh->win_ctime[1];
5922 in->win_mtime[1] = oh->win_mtime[1];
5924 in->yst_uid = oh->yst_uid;
5925 in->yst_gid = oh->yst_gid;
5926 in->yst_atime = oh->yst_atime;
5927 in->yst_mtime = oh->yst_mtime;
5928 in->yst_ctime = oh->yst_ctime;
5929 in->yst_rdev = oh->yst_rdev;
5932 yaffs_SetObjectName(in, oh->name);
5934 if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
5935 in->variant.symLinkVariant.alias =
5936 yaffs_CloneString(oh->alias);
5937 if(!in->variant.symLinkVariant.alias)
5938 alloc_failed = 1; /* Not returned to caller */
5941 yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
5945 static int yaffs_ScanBackwards(yaffs_Device * dev)
5947 yaffs_ExtendedTags tags;
5952 int nBlocksToScan = 0;
5958 yaffs_BlockState state;
5959 yaffs_Object *hardList = NULL;
5960 yaffs_BlockInfo *bi;
5962 yaffs_ObjectHeader *oh;
5964 yaffs_Object *parent;
5965 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5971 int foundChunksInBlock;
5972 int equivalentObjectId;
5973 int alloc_failed = 0;
5976 yaffs_BlockIndex *blockIndex = NULL;
5977 int altBlockIndex = 0;
5979 if (!dev->isYaffs2) {
5981 (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
5987 ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
5988 TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
5991 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
5993 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
5996 blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
6002 (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
6006 dev->blocksInCheckpoint = 0;
6008 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
6010 /* Scan all the blocks to determine their state */
6011 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
6012 bi = yaffs_GetBlockInfo(dev, blk);
6013 yaffs_ClearChunkBits(dev, blk);
6015 bi->softDeletions = 0;
6017 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
6019 bi->blockState = state;
6020 bi->sequenceNumber = sequenceNumber;
6022 if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
6023 bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
6025 T(YAFFS_TRACE_SCAN_DEBUG,
6026 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
6027 state, sequenceNumber));
6030 if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
6031 dev->blocksInCheckpoint++;
6033 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
6034 T(YAFFS_TRACE_BAD_BLOCKS,
6035 (TSTR("block %d is bad" TENDSTR), blk));
6036 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
6037 T(YAFFS_TRACE_SCAN_DEBUG,
6038 (TSTR("Block empty " TENDSTR)));
6039 dev->nErasedBlocks++;
6040 dev->nFreeChunks += dev->nChunksPerBlock;
6041 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6043 /* Determine the highest sequence number */
6044 if (dev->isYaffs2 &&
6045 sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
6046 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
6048 blockIndex[nBlocksToScan].seq = sequenceNumber;
6049 blockIndex[nBlocksToScan].block = blk;
6053 if (sequenceNumber >= dev->sequenceNumber) {
6054 dev->sequenceNumber = sequenceNumber;
6056 } else if (dev->isYaffs2) {
6057 /* TODO: Nasty sequence number! */
6060 ("Block scanning block %d has bad sequence number %d"
6061 TENDSTR), blk, sequenceNumber));
6068 (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
6074 /* Sort the blocks */
6075 #ifndef CONFIG_YAFFS_USE_OWN_SORT
6077 /* Use qsort now. */
6078 yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
6082 /* Dungy old bubble sort... */
6084 yaffs_BlockIndex temp;
6088 for (i = 0; i < nBlocksToScan; i++)
6089 for (j = i + 1; j < nBlocksToScan; j++)
6090 if (blockIndex[i].seq > blockIndex[j].seq) {
6091 temp = blockIndex[j];
6092 blockIndex[j] = blockIndex[i];
6093 blockIndex[i] = temp;
6100 T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
6102 /* Now scan the blocks looking at the data. */
6104 endIterator = nBlocksToScan - 1;
6105 T(YAFFS_TRACE_SCAN_DEBUG,
6106 (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
6108 /* For each block.... backwards */
6109 for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
6111 /* Cooperative multitasking! This loop can run for so
6112 long that watchdog timers expire. */
6115 /* get the block to scan in the correct order */
6116 blk = blockIndex[blockIterator].block;
6118 bi = yaffs_GetBlockInfo(dev, blk);
6121 state = bi->blockState;
6125 /* For each chunk in each block that needs scanning.... */
6126 foundChunksInBlock = 0;
6127 for (c = dev->nChunksPerBlock - 1;
6128 !alloc_failed && c >= 0 &&
6129 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6130 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
6131 /* Scan backwards...
6132 * Read the tags and decide what to do
6135 chunk = blk * dev->nChunksPerBlock + c;
6137 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
6140 /* Let's have a good look at this chunk... */
6142 if (!tags.chunkUsed) {
6143 /* An unassigned chunk in the block.
6144 * If there are used chunks after this one, then
6145 * it is a chunk that was skipped due to failing the erased
6146 * check. Just skip it so that it can be deleted.
6147 * But, more typically, We get here when this is an unallocated
6148 * chunk and his means that either the block is empty or
6149 * this is the one being allocated from
6152 if(foundChunksInBlock)
6154 /* This is a chunk that was skipped due to failing the erased check */
6156 } else if (c == 0) {
6157 /* We're looking at the first chunk in the block so the block is unused */
6158 state = YAFFS_BLOCK_STATE_EMPTY;
6159 dev->nErasedBlocks++;
6161 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6162 state == YAFFS_BLOCK_STATE_ALLOCATING) {
6163 if(dev->sequenceNumber == bi->sequenceNumber) {
6164 /* this is the block being allocated from */
6168 (" Allocating from %d %d"
6171 state = YAFFS_BLOCK_STATE_ALLOCATING;
6172 dev->allocationBlock = blk;
6173 dev->allocationPage = c;
6174 dev->allocationBlockFinder = blk;
6177 /* This is a partially written block that is not
6178 * the current allocation block. This block must have
6179 * had a write failure, so set up for retirement.
6182 bi->needsRetiring = 1;
6183 bi->gcPrioritise = 1;
6185 T(YAFFS_TRACE_ALWAYS,
6186 (TSTR("Partially written block %d being set for retirement" TENDSTR),
6196 } else if (tags.chunkId > 0) {
6197 /* chunkId > 0 so it is a data chunk... */
6198 unsigned int endpos;
6200 (tags.chunkId - 1) * dev->nDataBytesPerChunk;
6202 foundChunksInBlock = 1;
6205 yaffs_SetChunkBit(dev, blk, c);
6208 in = yaffs_FindOrCreateObjectByNumber(dev,
6211 YAFFS_OBJECT_TYPE_FILE);
6218 in->variantType == YAFFS_OBJECT_TYPE_FILE
6220 in->variant.fileVariant.shrinkSize) {
6221 /* This has not been invalidated by a resize */
6222 if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
6227 /* File size is calculated by looking at the data chunks if we have not
6228 * seen an object header yet. Stop this practice once we find an object header.
6232 1) * dev->nDataBytesPerChunk +
6235 if (!in->valid && /* have not got an object header yet */
6236 in->variant.fileVariant.
6237 scannedFileSize < endpos) {
6238 in->variant.fileVariant.
6239 scannedFileSize = endpos;
6240 in->variant.fileVariant.
6242 in->variant.fileVariant.
6247 /* This chunk has been invalidated by a resize, so delete */
6248 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
6252 /* chunkId == 0, so it is an ObjectHeader.
6253 * Thus, we read in the object header and make the object
6255 foundChunksInBlock = 1;
6257 yaffs_SetChunkBit(dev, blk, c);
6263 if (tags.extraHeaderInfoAvailable) {
6264 in = yaffs_FindOrCreateObjectByNumber
6265 (dev, tags.objectId,
6266 tags.extraObjectType);
6270 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
6273 tags.extraShadows ||
6275 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6276 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
6279 /* If we don't have valid info then we need to read the chunk
6280 * TODO In future we can probably defer reading the chunk and
6281 * living with invalid data until needed.
6284 result = yaffs_ReadChunkWithTagsFromNAND(dev,
6289 oh = (yaffs_ObjectHeader *) chunkData;
6292 in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
6297 /* TODO Hoosterman we have a problem! */
6298 T(YAFFS_TRACE_ERROR,
6300 ("yaffs tragedy: Could not make object for object %d "
6301 "at chunk %d during scan"
6302 TENDSTR), tags.objectId, chunk));
6307 /* We have already filled this one.
6308 * We have a duplicate that will be discarded, but
6309 * we first have to suck out resize info if it is a file.
6312 if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
6314 oh-> type == YAFFS_OBJECT_TYPE_FILE)||
6315 (tags.extraHeaderInfoAvailable &&
6316 tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
6319 (oh) ? oh->fileSize : tags.
6321 __u32 parentObjectId =
6323 parentObjectId : tags.
6324 extraParentObjectId;
6326 (oh) ? oh->isShrink : tags.
6327 extraIsShrinkHeader;
6329 /* If it is deleted (unlinked at start also means deleted)
6330 * we treat the file size as being zeroed at this point.
6332 if (parentObjectId ==
6333 YAFFS_OBJECTID_DELETED
6334 || parentObjectId ==
6335 YAFFS_OBJECTID_UNLINKED) {
6341 in->variant.fileVariant.
6342 shrinkSize > thisSize) {
6343 in->variant.fileVariant.
6349 bi->hasShrinkHeader = 1;
6353 /* Use existing - destroy this one. */
6354 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
6359 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6361 YAFFS_OBJECTID_LOSTNFOUND)) {
6362 /* We only load some info, don't fiddle with directory structure */
6366 in->variantType = oh->type;
6368 in->yst_mode = oh->yst_mode;
6369 #ifdef CONFIG_YAFFS_WINCE
6370 in->win_atime[0] = oh->win_atime[0];
6371 in->win_ctime[0] = oh->win_ctime[0];
6372 in->win_mtime[0] = oh->win_mtime[0];
6373 in->win_atime[1] = oh->win_atime[1];
6374 in->win_ctime[1] = oh->win_ctime[1];
6375 in->win_mtime[1] = oh->win_mtime[1];
6377 in->yst_uid = oh->yst_uid;
6378 in->yst_gid = oh->yst_gid;
6379 in->yst_atime = oh->yst_atime;
6380 in->yst_mtime = oh->yst_mtime;
6381 in->yst_ctime = oh->yst_ctime;
6382 in->yst_rdev = oh->yst_rdev;
6386 in->variantType = tags.extraObjectType;
6390 in->chunkId = chunk;
6392 } else if (!in->valid) {
6393 /* we need to load this info */
6396 in->chunkId = chunk;
6399 in->variantType = oh->type;
6401 in->yst_mode = oh->yst_mode;
6402 #ifdef CONFIG_YAFFS_WINCE
6403 in->win_atime[0] = oh->win_atime[0];
6404 in->win_ctime[0] = oh->win_ctime[0];
6405 in->win_mtime[0] = oh->win_mtime[0];
6406 in->win_atime[1] = oh->win_atime[1];
6407 in->win_ctime[1] = oh->win_ctime[1];
6408 in->win_mtime[1] = oh->win_mtime[1];
6410 in->yst_uid = oh->yst_uid;
6411 in->yst_gid = oh->yst_gid;
6412 in->yst_atime = oh->yst_atime;
6413 in->yst_mtime = oh->yst_mtime;
6414 in->yst_ctime = oh->yst_ctime;
6415 in->yst_rdev = oh->yst_rdev;
6418 if (oh->shadowsObject > 0)
6419 yaffs_HandleShadowedObject(dev,
6425 yaffs_SetObjectName(in, oh->name);
6427 yaffs_FindOrCreateObjectByNumber
6428 (dev, oh->parentObjectId,
6429 YAFFS_OBJECT_TYPE_DIRECTORY);
6431 fileSize = oh->fileSize;
6432 isShrink = oh->isShrink;
6433 equivalentObjectId = oh->equivalentObjectId;
6437 in->variantType = tags.extraObjectType;
6439 yaffs_FindOrCreateObjectByNumber
6440 (dev, tags.extraParentObjectId,
6441 YAFFS_OBJECT_TYPE_DIRECTORY);
6442 fileSize = tags.extraFileLength;
6443 isShrink = tags.extraIsShrinkHeader;
6444 equivalentObjectId = tags.extraEquivalentObjectId;
6450 /* directory stuff...
6454 if (parent->variantType ==
6455 YAFFS_OBJECT_TYPE_UNKNOWN) {
6456 /* Set up as a directory */
6457 parent->variantType =
6458 YAFFS_OBJECT_TYPE_DIRECTORY;
6459 INIT_LIST_HEAD(&parent->variant.
6462 } else if (parent->variantType !=
6463 YAFFS_OBJECT_TYPE_DIRECTORY)
6465 /* Hoosterman, another problem....
6466 * We're trying to use a non-directory as a directory
6469 T(YAFFS_TRACE_ERROR,
6471 ("yaffs tragedy: attempting to use non-directory as"
6472 " a directory in scan. Put in lost+found."
6474 parent = dev->lostNFoundDir;
6477 yaffs_AddObjectToDirectory(parent, in);
6479 itsUnlinked = (parent == dev->deletedDir) ||
6480 (parent == dev->unlinkedDir);
6483 /* Mark the block as having a shrinkHeader */
6484 bi->hasShrinkHeader = 1;
6487 /* Note re hardlinks.
6488 * Since we might scan a hardlink before its equivalent object is scanned
6489 * we put them all in a list.
6490 * After scanning is complete, we should have all the objects, so we run
6491 * through this list and fix up all the chains.
6494 switch (in->variantType) {
6495 case YAFFS_OBJECT_TYPE_UNKNOWN:
6496 /* Todo got a problem */
6498 case YAFFS_OBJECT_TYPE_FILE:
6500 if (in->variant.fileVariant.
6501 scannedFileSize < fileSize) {
6502 /* This covers the case where the file size is greater
6503 * than where the data is
6504 * This will happen if the file is resized to be larger
6505 * than its current data extents.
6507 in->variant.fileVariant.fileSize = fileSize;
6508 in->variant.fileVariant.scannedFileSize =
6509 in->variant.fileVariant.fileSize;
6513 in->variant.fileVariant.shrinkSize > fileSize) {
6514 in->variant.fileVariant.shrinkSize = fileSize;
6518 case YAFFS_OBJECT_TYPE_HARDLINK:
6520 in->variant.hardLinkVariant.equivalentObjectId =
6522 in->hardLinks.next =
6523 (struct list_head *) hardList;
6527 case YAFFS_OBJECT_TYPE_DIRECTORY:
6530 case YAFFS_OBJECT_TYPE_SPECIAL:
6533 case YAFFS_OBJECT_TYPE_SYMLINK:
6535 in->variant.symLinkVariant.alias =
6536 yaffs_CloneString(oh->
6538 if(!in->variant.symLinkVariant.alias)
6548 } /* End of scanning for each chunk */
6550 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6551 /* If we got this far while scanning, then the block is fully allocated. */
6552 state = YAFFS_BLOCK_STATE_FULL;
6555 bi->blockState = state;
6557 /* Now let's see if it was dirty */
6558 if (bi->pagesInUse == 0 &&
6559 !bi->hasShrinkHeader &&
6560 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
6561 yaffs_BlockBecameDirty(dev, blk);
6567 YFREE_ALT(blockIndex);
6571 /* Ok, we've done all the scanning.
6572 * Fix up the hard link chains.
6573 * We should now have scanned all the objects, now it's time to add these
6576 yaffs_HardlinkFixup(dev,hardList);
6580 * Sort out state of unlinked and deleted objects.
6583 struct list_head *i;
6584 struct list_head *n;
6588 /* Soft delete all the unlinked files */
6589 list_for_each_safe(i, n,
6590 &dev->unlinkedDir->variant.directoryVariant.
6593 l = list_entry(i, yaffs_Object, siblings);
6594 yaffs_DestroyObject(l);
6598 /* Soft delete all the deletedDir files */
6599 list_for_each_safe(i, n,
6600 &dev->deletedDir->variant.directoryVariant.
6603 l = list_entry(i, yaffs_Object, siblings);
6604 yaffs_DestroyObject(l);
6610 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
6616 T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
6621 /*------------------------------ Directory Functions ----------------------------- */
6623 static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
6625 yaffs_Device *dev = obj->myDev;
6627 if(dev && dev->removeObjectCallback)
6628 dev->removeObjectCallback(obj);
6630 list_del_init(&obj->siblings);
6635 static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
6640 T(YAFFS_TRACE_ALWAYS,
6642 ("tragedy: Trying to add an object to a null pointer directory"
6646 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6647 T(YAFFS_TRACE_ALWAYS,
6649 ("tragedy: Trying to add an object to a non-directory"
6654 if (obj->siblings.prev == NULL) {
6655 /* Not initialised */
6656 INIT_LIST_HEAD(&obj->siblings);
6658 } else if (!list_empty(&obj->siblings)) {
6659 /* If it is holed up somewhere else, un hook it */
6660 yaffs_RemoveObjectFromDirectory(obj);
6663 list_add(&obj->siblings, &directory->variant.directoryVariant.children);
6664 obj->parent = directory;
6666 if (directory == obj->myDev->unlinkedDir
6667 || directory == obj->myDev->deletedDir) {
6669 obj->myDev->nUnlinkedFiles++;
6670 obj->renameAllowed = 0;
6674 yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
6679 struct list_head *i;
6680 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
6689 T(YAFFS_TRACE_ALWAYS,
6691 ("tragedy: yaffs_FindObjectByName: null pointer directory"
6695 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6696 T(YAFFS_TRACE_ALWAYS,
6698 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
6702 sum = yaffs_CalcNameSum(name);
6704 list_for_each(i, &directory->variant.directoryVariant.children) {
6706 l = list_entry(i, yaffs_Object, siblings);
6708 yaffs_CheckObjectDetailsLoaded(l);
6710 /* Special case for lost-n-found */
6711 if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
6712 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
6715 } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
6717 /* LostnFound cunk called Objxxx
6720 yaffs_GetObjectName(l, buffer,
6721 YAFFS_MAX_NAME_LENGTH);
6722 if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
6735 int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
6736 int (*fn) (yaffs_Object *))
6738 struct list_head *i;
6742 T(YAFFS_TRACE_ALWAYS,
6744 ("tragedy: yaffs_FindObjectByName: null pointer directory"
6748 if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6749 T(YAFFS_TRACE_ALWAYS,
6751 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
6755 list_for_each(i, &theDir->variant.directoryVariant.children) {
6757 l = list_entry(i, yaffs_Object, siblings);
6769 /* GetEquivalentObject dereferences any hard links to get to the
6773 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
6775 if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
6776 /* We want the object id of the equivalent object, not this one */
6777 obj = obj->variant.hardLinkVariant.equivalentObject;
6778 yaffs_CheckObjectDetailsLoaded(obj);
6784 int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
6786 memset(name, 0, buffSize * sizeof(YCHAR));
6788 yaffs_CheckObjectDetailsLoaded(obj);
6790 if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
6791 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
6792 } else if (obj->chunkId <= 0) {
6794 /* make up a name */
6795 yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
6797 yaffs_strncpy(name, locName, buffSize - 1);
6800 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
6801 else if (obj->shortName[0]) {
6802 yaffs_strcpy(name, obj->shortName);
6807 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
6809 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
6811 memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
6813 if (obj->chunkId >= 0) {
6814 result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
6815 obj->chunkId, buffer,
6818 yaffs_strncpy(name, oh->name, buffSize - 1);
6820 yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
6823 return yaffs_strlen(name);
6826 int yaffs_GetObjectFileLength(yaffs_Object * obj)
6829 /* Dereference any hard linking */
6830 obj = yaffs_GetEquivalentObject(obj);
6832 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
6833 return obj->variant.fileVariant.fileSize;
6835 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
6836 return yaffs_strlen(obj->variant.symLinkVariant.alias);
6838 /* Only a directory should drop through to here */
6839 return obj->myDev->nDataBytesPerChunk;
6843 int yaffs_GetObjectLinkCount(yaffs_Object * obj)
6846 struct list_head *i;
6848 if (!obj->unlinked) {
6849 count++; /* the object itself */
6851 list_for_each(i, &obj->hardLinks) {
6852 count++; /* add the hard links; */
6858 int yaffs_GetObjectInode(yaffs_Object * obj)
6860 obj = yaffs_GetEquivalentObject(obj);
6862 return obj->objectId;
6865 unsigned yaffs_GetObjectType(yaffs_Object * obj)
6867 obj = yaffs_GetEquivalentObject(obj);
6869 switch (obj->variantType) {
6870 case YAFFS_OBJECT_TYPE_FILE:
6873 case YAFFS_OBJECT_TYPE_DIRECTORY:
6876 case YAFFS_OBJECT_TYPE_SYMLINK:
6879 case YAFFS_OBJECT_TYPE_HARDLINK:
6882 case YAFFS_OBJECT_TYPE_SPECIAL:
6883 if (S_ISFIFO(obj->yst_mode))
6885 if (S_ISCHR(obj->yst_mode))
6887 if (S_ISBLK(obj->yst_mode))
6889 if (S_ISSOCK(obj->yst_mode))
6897 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
6899 obj = yaffs_GetEquivalentObject(obj);
6900 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
6901 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
6903 return yaffs_CloneString(_Y(""));
6907 #ifndef CONFIG_YAFFS_WINCE
6909 int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
6911 unsigned int valid = attr->ia_valid;
6913 if (valid & ATTR_MODE)
6914 obj->yst_mode = attr->ia_mode;
6915 if (valid & ATTR_UID)
6916 obj->yst_uid = attr->ia_uid;
6917 if (valid & ATTR_GID)
6918 obj->yst_gid = attr->ia_gid;
6920 if (valid & ATTR_ATIME)
6921 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
6922 if (valid & ATTR_CTIME)
6923 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
6924 if (valid & ATTR_MTIME)
6925 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
6927 if (valid & ATTR_SIZE)
6928 yaffs_ResizeFile(obj, attr->ia_size);
6930 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
6935 int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
6937 unsigned int valid = 0;
6939 attr->ia_mode = obj->yst_mode;
6941 attr->ia_uid = obj->yst_uid;
6943 attr->ia_gid = obj->yst_gid;
6946 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
6947 valid |= ATTR_ATIME;
6948 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
6949 valid |= ATTR_CTIME;
6950 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
6951 valid |= ATTR_MTIME;
6953 attr->ia_size = yaffs_GetFileSize(obj);
6956 attr->ia_valid = valid;
6965 int yaffs_DumpObject(yaffs_Object * obj)
6969 yaffs_GetObjectName(obj, name, 256);
6971 T(YAFFS_TRACE_ALWAYS,
6973 ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
6974 " chunk %d type %d size %d\n"
6975 TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
6976 obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
6977 yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
6983 /*---------------------------- Initialisation code -------------------------------------- */
6985 static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
6988 /* Common functions, gotta have */
6989 if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
6992 #ifdef CONFIG_YAFFS_YAFFS2
6994 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
6995 if (dev->writeChunkWithTagsToNAND &&
6996 dev->readChunkWithTagsFromNAND &&
6997 !dev->writeChunkToNAND &&
6998 !dev->readChunkFromNAND &&
6999 dev->markNANDBlockBad && dev->queryNANDBlock)
7003 /* Can use the "spare" style interface for yaffs1 */
7004 if (!dev->isYaffs2 &&
7005 !dev->writeChunkWithTagsToNAND &&
7006 !dev->readChunkWithTagsFromNAND &&
7007 dev->writeChunkToNAND &&
7008 dev->readChunkFromNAND &&
7009 !dev->markNANDBlockBad && !dev->queryNANDBlock)
7016 static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
7018 /* Initialise the unlinked, deleted, root and lost and found directories */
7020 dev->lostNFoundDir = dev->rootDir = NULL;
7021 dev->unlinkedDir = dev->deletedDir = NULL;
7024 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
7027 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
7030 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
7031 YAFFS_ROOT_MODE | S_IFDIR);
7032 dev->lostNFoundDir =
7033 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
7034 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
7036 if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
7037 yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
7044 int yaffs_GutsInitialise(yaffs_Device * dev)
7046 int init_failed = 0;
7050 T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
7052 /* Check stuff that must be set */
7055 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
7059 dev->internalStartBlock = dev->startBlock;
7060 dev->internalEndBlock = dev->endBlock;
7061 dev->blockOffset = 0;
7062 dev->chunkOffset = 0;
7063 dev->nFreeChunks = 0;
7065 if (dev->startBlock == 0) {
7066 dev->internalStartBlock = dev->startBlock + 1;
7067 dev->internalEndBlock = dev->endBlock + 1;
7068 dev->blockOffset = 1;
7069 dev->chunkOffset = dev->nChunksPerBlock;
7072 /* Check geometry parameters. */
7074 if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
7075 (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
7076 dev->nChunksPerBlock < 2 ||
7077 dev->nReservedBlocks < 2 ||
7078 dev->internalStartBlock <= 0 ||
7079 dev->internalEndBlock <= 0 ||
7080 dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
7082 T(YAFFS_TRACE_ALWAYS,
7084 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
7085 TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
7089 if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
7090 T(YAFFS_TRACE_ALWAYS,
7091 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
7095 /* Got the right mix of functions? */
7096 if (!yaffs_CheckDevFunctions(dev)) {
7097 /* Function missing */
7098 T(YAFFS_TRACE_ALWAYS,
7100 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
7105 /* This is really a compilation check. */
7106 if (!yaffs_CheckStructures()) {
7107 T(YAFFS_TRACE_ALWAYS,
7108 (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
7112 if (dev->isMounted) {
7113 T(YAFFS_TRACE_ALWAYS,
7114 (TSTR("yaffs: device already mounted\n" TENDSTR)));
7118 /* Finished with most checks. One or two more checks happen later on too. */
7124 /* OK now calculate a few things for the device */
7127 * Calculate all the chunk size manipulation numbers:
7129 /* Start off assuming it is a power of 2 */
7130 dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
7131 dev->chunkMask = (1<<dev->chunkShift) - 1;
7133 if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
7134 /* Yes it is a power of 2, disable crumbs */
7136 dev->crumbShift = 0;
7137 dev->crumbsPerChunk = 0;
7139 /* Not a power of 2, use crumbs instead */
7140 dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
7141 dev->crumbMask = (1<<dev->crumbShift)-1;
7142 dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
7143 dev->chunkShift = 0;
7149 * Calculate chunkGroupBits.
7150 * We need to find the next power of 2 > than internalEndBlock
7153 x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
7157 /* Set up tnode width if wide tnodes are enabled. */
7158 if(!dev->wideTnodesDisabled){
7159 /* bits must be even so that we end up with 32-bit words */
7163 dev->tnodeWidth = 16;
7165 dev->tnodeWidth = bits;
7168 dev->tnodeWidth = 16;
7170 dev->tnodeMask = (1<<dev->tnodeWidth)-1;
7172 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
7173 * so if the bitwidth of the
7174 * chunk range we're using is greater than 16 we need
7175 * to figure out chunk shift and chunkGroupSize
7178 if (bits <= dev->tnodeWidth)
7179 dev->chunkGroupBits = 0;
7181 dev->chunkGroupBits = bits - dev->tnodeWidth;
7184 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
7186 if (dev->nChunksPerBlock < dev->chunkGroupSize) {
7187 /* We have a problem because the soft delete won't work if
7188 * the chunk group size > chunks per block.
7189 * This can be remedied by using larger "virtual blocks".
7191 T(YAFFS_TRACE_ALWAYS,
7192 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
7197 /* OK, we've finished verifying the device, lets continue with initialisation */
7199 /* More device initialisation */
7200 dev->garbageCollections = 0;
7201 dev->passiveGarbageCollections = 0;
7202 dev->currentDirtyChecker = 0;
7203 dev->bufferedBlock = -1;
7204 dev->doingBufferedBlockRewrite = 0;
7205 dev->nDeletedFiles = 0;
7206 dev->nBackgroundDeletions = 0;
7207 dev->nUnlinkedFiles = 0;
7209 dev->eccUnfixed = 0;
7210 dev->tagsEccFixed = 0;
7211 dev->tagsEccUnfixed = 0;
7212 dev->nErasureFailures = 0;
7213 dev->nErasedBlocks = 0;
7215 dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
7217 /* Initialise temporary buffers and caches. */
7218 if(!yaffs_InitialiseTempBuffers(dev))
7221 dev->srCache = NULL;
7222 dev->gcCleanupList = NULL;
7226 dev->nShortOpCaches > 0) {
7229 int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
7231 if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
7232 dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
7235 buf = dev->srCache = YMALLOC(srCacheBytes);
7238 memset(dev->srCache,0,srCacheBytes);
7240 for (i = 0; i < dev->nShortOpCaches && buf; i++) {
7241 dev->srCache[i].object = NULL;
7242 dev->srCache[i].lastUse = 0;
7243 dev->srCache[i].dirty = 0;
7244 dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
7255 dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
7256 if(!dev->gcCleanupList)
7260 if (dev->isYaffs2) {
7261 dev->useHeaderFileSize = 1;
7263 if(!init_failed && !yaffs_InitialiseBlocks(dev))
7266 yaffs_InitialiseTnodes(dev);
7267 yaffs_InitialiseObjects(dev);
7269 if(!init_failed && !yaffs_CreateInitialDirectories(dev))
7274 /* Now scan the flash. */
7275 if (dev->isYaffs2) {
7276 if(yaffs_CheckpointRestore(dev)) {
7277 T(YAFFS_TRACE_ALWAYS,
7278 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
7281 /* Clean up the mess caused by an aborted checkpoint load
7282 * and scan backwards.
7284 yaffs_DeinitialiseBlocks(dev);
7285 yaffs_DeinitialiseTnodes(dev);
7286 yaffs_DeinitialiseObjects(dev);
7289 dev->nErasedBlocks = 0;
7290 dev->nFreeChunks = 0;
7291 dev->allocationBlock = -1;
7292 dev->allocationPage = -1;
7293 dev->nDeletedFiles = 0;
7294 dev->nUnlinkedFiles = 0;
7295 dev->nBackgroundDeletions = 0;
7296 dev->oldestDirtySequence = 0;
7298 if(!init_failed && !yaffs_InitialiseBlocks(dev))
7301 yaffs_InitialiseTnodes(dev);
7302 yaffs_InitialiseObjects(dev);
7304 if(!init_failed && !yaffs_CreateInitialDirectories(dev))
7307 if(!init_failed && !yaffs_ScanBackwards(dev))
7311 if(!yaffs_Scan(dev))
7316 /* Clean up the mess */
7317 T(YAFFS_TRACE_TRACING,
7318 (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
7320 yaffs_Deinitialise(dev);
7324 /* Zero out stats */
7325 dev->nPageReads = 0;
7326 dev->nPageWrites = 0;
7327 dev->nBlockErasures = 0;
7329 dev->nRetriedWrites = 0;
7331 dev->nRetiredBlocks = 0;
7333 yaffs_VerifyFreeChunks(dev);
7334 yaffs_VerifyBlocks(dev);
7337 T(YAFFS_TRACE_TRACING,
7338 (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
7343 void yaffs_Deinitialise(yaffs_Device * dev)
7345 if (dev->isMounted) {
7348 yaffs_DeinitialiseBlocks(dev);
7349 yaffs_DeinitialiseTnodes(dev);
7350 yaffs_DeinitialiseObjects(dev);
7351 if (dev->nShortOpCaches > 0 &&
7354 for (i = 0; i < dev->nShortOpCaches; i++) {
7355 if(dev->srCache[i].data)
7356 YFREE(dev->srCache[i].data);
7357 dev->srCache[i].data = NULL;
7360 YFREE(dev->srCache);
7361 dev->srCache = NULL;
7364 YFREE(dev->gcCleanupList);
7366 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
7367 YFREE(dev->tempBuffer[i].buffer);
7375 static int yaffs_CountFreeChunks(yaffs_Device * dev)
7380 yaffs_BlockInfo *blk;
7382 for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
7384 blk = yaffs_GetBlockInfo(dev, b);
7386 switch (blk->blockState) {
7387 case YAFFS_BLOCK_STATE_EMPTY:
7388 case YAFFS_BLOCK_STATE_ALLOCATING:
7389 case YAFFS_BLOCK_STATE_COLLECTING:
7390 case YAFFS_BLOCK_STATE_FULL:
7392 (dev->nChunksPerBlock - blk->pagesInUse +
7393 blk->softDeletions);
7404 int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
7406 /* This is what we report to the outside world */
7409 int nDirtyCacheChunks;
7410 int blocksForCheckpoint;
7413 nFree = dev->nFreeChunks;
7415 nFree = yaffs_CountFreeChunks(dev);
7418 nFree += dev->nDeletedFiles;
7420 /* Now count the number of dirty chunks in the cache and subtract those */
7424 for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
7425 if (dev->srCache[i].dirty)
7426 nDirtyCacheChunks++;
7430 nFree -= nDirtyCacheChunks;
7432 nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
7434 /* Now we figure out how much to reserve for the checkpoint and report that... */
7435 blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
7436 if(blocksForCheckpoint < 0)
7437 blocksForCheckpoint = 0;
7439 nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
7448 static int yaffs_freeVerificationFailures;
7450 static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
7455 if(yaffs_SkipVerification(dev))
7458 counted = yaffs_CountFreeChunks(dev);
7460 difference = dev->nFreeChunks - counted;
7463 T(YAFFS_TRACE_ALWAYS,
7464 (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
7465 dev->nFreeChunks, counted, difference));
7466 yaffs_freeVerificationFailures++;
7470 /*---------------------------------------- YAFFS test code ----------------------*/
7472 #define yaffs_CheckStruct(structure,syze, name) \
7473 if(sizeof(structure) != syze) \
7475 T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
7476 name,syze,sizeof(structure))); \
7477 return YAFFS_FAIL; \
7480 static int yaffs_CheckStructures(void)
7482 /* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
7483 /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
7484 /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
7485 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
7486 yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
7488 yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")