2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include "yaffs_trace.h"
16 #include "yaffsinterface.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_tagsvalidity.h"
19 #include "yaffs_getblockinfo.h"
21 #include "yaffs_tagscompat.h"
22 #ifndef CONFIG_YAFFS_USE_OWN_SORT
23 #include "yaffs_qsort.h"
25 #include "yaffs_nand.h"
27 #include "yaffs_checkptrw.h"
29 #include "yaffs_nand.h"
30 #include "yaffs_packedtags2.h"
32 #include "yaffs_nameval.h"
33 #include "yaffs_allocator.h"
35 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
36 #define YAFFS_GC_GOOD_ENOUGH 2
37 #define YAFFS_GC_PASSIVE_THRESHOLD 4
39 #define YAFFS_SMALL_HOLE_THRESHOLD 3
42 * Checkpoints are really no benefit on very small partitions.
44 * To save space on small partitions don't bother with checkpoints unless
45 * the partition is at least this big.
47 #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
49 #include "yaffs_ecc.h"
51 /* Private structure for doing xattr modifications */
53 int set; /* If 0 then this is a deletion */
61 /* Robustification (if it ever comes about...) */
62 static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
63 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
65 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
67 const yaffs_ExtendedTags *tags);
68 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
69 const yaffs_ExtendedTags *tags);
71 /* Other local prototypes */
72 static void yaffs_UpdateParent(yaffs_Object *obj);
73 static int yaffs_UnlinkObject(yaffs_Object *obj);
74 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
76 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
78 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
80 yaffs_ExtendedTags *tags,
82 static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
83 int chunkInNAND, int inScan);
85 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
86 yaffs_ObjectType type);
87 static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
89 static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
90 int force, int isShrink, int shadows, yaffs_XAttrMod *xop);
91 static int yaffs_ApplyXMod(yaffs_Device *dev, char *buffer, yaffs_XAttrMod *xmod);
93 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
94 static int yaffs_CheckStructures(void);
95 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
97 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
100 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
103 static int yaffs_UnlinkWorker(yaffs_Object *obj);
105 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
108 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
109 yaffs_BlockInfo **blockUsedPtr);
111 static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
113 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
115 static void yaffs_VerifyDirectory(yaffs_Object *directory);
116 #ifdef YAFFS_PARANOID
117 static int yaffs_CheckFileSanity(yaffs_Object *in);
119 #define yaffs_CheckFileSanity(in)
122 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
123 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
125 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
127 static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
128 yaffs_ExtendedTags *tags);
130 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
132 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
133 yaffs_FileStructure *fStruct,
136 static int yaffs_HandleHole(yaffs_Object *obj, loff_t newSize);
137 static void yaffs_SkipRestOfBlock(yaffs_Device *dev);
138 static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
141 yaffs_ExtendedTags *tags);
143 /* Function to calculate chunk and offset */
145 static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
151 chunk = (__u32)(addr >> dev->chunkShift);
153 if (dev->chunkDiv == 1) {
154 /* easy power of 2 case */
155 offset = (__u32)(addr & dev->chunkMask);
157 /* Non power-of-2 case */
161 chunk /= dev->chunkDiv;
163 chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
164 offset = (__u32)(addr - chunkBase);
171 /* Function to return the number of shifts for a power of 2 greater than or
172 * equal to the given number
173 * Note we don't try to cater for all possible numbers and this does not have to
174 * be hellishly efficient.
177 static __u32 ShiftsGE(__u32 x)
182 nShifts = extraBits = 0;
197 /* Function to return the number of shifts to get a 1 in bit 0
200 static __u32 Shifts(__u32 x)
220 * Temporary buffer manipulations.
223 static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
226 __u8 *buf = (__u8 *)1;
228 memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
230 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
231 dev->tempBuffer[i].line = 0; /* not in use */
232 dev->tempBuffer[i].buffer = buf =
233 YMALLOC_DMA(dev->param.totalBytesPerChunk);
236 return buf ? YAFFS_OK : YAFFS_FAIL;
239 __u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
244 if (dev->tempInUse > dev->maxTemp)
245 dev->maxTemp = dev->tempInUse;
247 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
248 if (dev->tempBuffer[i].line == 0) {
249 dev->tempBuffer[i].line = lineNo;
250 if ((i + 1) > dev->maxTemp) {
251 dev->maxTemp = i + 1;
252 for (j = 0; j <= i; j++)
253 dev->tempBuffer[j].maxLine =
254 dev->tempBuffer[j].line;
257 return dev->tempBuffer[i].buffer;
261 T(YAFFS_TRACE_BUFFERS,
262 (TSTR("Out of temp buffers at line %d, other held by lines:"),
264 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
265 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
267 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
270 * If we got here then we have to allocate an unmanaged one
274 dev->unmanagedTempAllocations++;
275 return YMALLOC(dev->nDataBytesPerChunk);
279 void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
286 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
287 if (dev->tempBuffer[i].buffer == buffer) {
288 dev->tempBuffer[i].line = 0;
294 /* assume it is an unmanaged one. */
295 T(YAFFS_TRACE_BUFFERS,
296 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
299 dev->unmanagedTempDeallocations++;
305 * Determine if we have a managed buffer.
307 int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
311 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
312 if (dev->tempBuffer[i].buffer == buffer)
316 for (i = 0; i < dev->param.nShortOpCaches; i++) {
317 if (dev->srCache[i].data == buffer)
321 if (buffer == dev->checkpointBuffer)
324 T(YAFFS_TRACE_ALWAYS,
325 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
332 * Chunk bitmap manipulations
335 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
337 if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
339 (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
343 return dev->chunkBits +
344 (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
347 static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
349 if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
350 chunk < 0 || chunk >= dev->param.nChunksPerBlock) {
352 (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
358 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
360 __u8 *blkBits = yaffs_BlockBits(dev, blk);
362 memset(blkBits, 0, dev->chunkBitmapStride);
365 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
367 __u8 *blkBits = yaffs_BlockBits(dev, blk);
369 yaffs_VerifyChunkBitId(dev, blk, chunk);
371 blkBits[chunk / 8] &= ~(1 << (chunk & 7));
374 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
376 __u8 *blkBits = yaffs_BlockBits(dev, blk);
378 yaffs_VerifyChunkBitId(dev, blk, chunk);
380 blkBits[chunk / 8] |= (1 << (chunk & 7));
383 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
385 __u8 *blkBits = yaffs_BlockBits(dev, blk);
386 yaffs_VerifyChunkBitId(dev, blk, chunk);
388 return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
391 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
393 __u8 *blkBits = yaffs_BlockBits(dev, blk);
395 for (i = 0; i < dev->chunkBitmapStride; i++) {
403 static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
405 __u8 *blkBits = yaffs_BlockBits(dev, blk);
408 for (i = 0; i < dev->chunkBitmapStride; i++) {
425 static int yaffs_SkipVerification(yaffs_Device *dev)
428 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
431 static int yaffs_SkipFullVerification(yaffs_Device *dev)
434 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
437 static int yaffs_SkipNANDVerification(yaffs_Device *dev)
440 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
443 static const char *blockStateName[] = {
456 static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
461 if (yaffs_SkipVerification(dev))
464 /* Report illegal runtime states */
465 if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
466 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
468 switch (bi->blockState) {
469 case YAFFS_BLOCK_STATE_UNKNOWN:
470 case YAFFS_BLOCK_STATE_SCANNING:
471 case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
472 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
473 n, blockStateName[bi->blockState]));
476 /* Check pages in use and soft deletions are legal */
478 actuallyUsed = bi->pagesInUse - bi->softDeletions;
480 if (bi->pagesInUse < 0 || bi->pagesInUse > dev->param.nChunksPerBlock ||
481 bi->softDeletions < 0 || bi->softDeletions > dev->param.nChunksPerBlock ||
482 actuallyUsed < 0 || actuallyUsed > dev->param.nChunksPerBlock)
483 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
484 n, bi->pagesInUse, bi->softDeletions));
487 /* Check chunk bitmap legal */
488 inUse = yaffs_CountChunkBits(dev, n);
489 if (inUse != bi->pagesInUse)
490 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
491 n, bi->pagesInUse, inUse));
493 /* Check that the sequence number is valid.
494 * Ten million is legal, but is very unlikely
496 if (dev->param.isYaffs2 &&
497 (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
498 (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
499 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
500 n, bi->sequenceNumber));
503 static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
506 yaffs_VerifyBlock(dev, bi, n);
508 /* After collection the block should be in the erased state */
509 /* This will need to change if we do partial gc */
511 if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
512 bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
513 T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
518 static void yaffs_VerifyBlocks(yaffs_Device *dev)
521 int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
522 int nIllegalBlockStates = 0;
524 if (yaffs_SkipVerification(dev))
527 memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
529 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
530 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
531 yaffs_VerifyBlock(dev, bi, i);
533 if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
534 nBlocksPerState[bi->blockState]++;
536 nIllegalBlockStates++;
539 T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
540 T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
542 T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
543 if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
544 T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
546 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
547 T(YAFFS_TRACE_VERIFY,
548 (TSTR("%s %d blocks"TENDSTR),
549 blockStateName[i], nBlocksPerState[i]));
551 if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
552 T(YAFFS_TRACE_VERIFY,
553 (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
554 dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
556 if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
557 T(YAFFS_TRACE_VERIFY,
558 (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
559 dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
561 if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
562 T(YAFFS_TRACE_VERIFY,
563 (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
564 nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
566 T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
571 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
572 * case those tests will not be performed.
574 static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
576 if (obj && yaffs_SkipVerification(obj->myDev))
579 if (!(tags && obj && oh)) {
580 T(YAFFS_TRACE_VERIFY,
581 (TSTR("Verifying object header tags %p obj %p oh %p"TENDSTR),
586 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
587 oh->type > YAFFS_OBJECT_TYPE_MAX)
588 T(YAFFS_TRACE_VERIFY,
589 (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
590 tags->objectId, oh->type));
592 if (tags->objectId != obj->objectId)
593 T(YAFFS_TRACE_VERIFY,
594 (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
595 tags->objectId, obj->objectId));
599 * Check that the object's parent ids match if parentCheck requested.
601 * Tests do not apply to the root object.
604 if (parentCheck && tags->objectId > 1 && !obj->parent)
605 T(YAFFS_TRACE_VERIFY,
606 (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
607 tags->objectId, oh->parentObjectId));
609 if (parentCheck && obj->parent &&
610 oh->parentObjectId != obj->parent->objectId &&
611 (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
612 obj->parent->objectId != YAFFS_OBJECTID_DELETED))
613 T(YAFFS_TRACE_VERIFY,
614 (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
615 tags->objectId, oh->parentObjectId, obj->parent->objectId));
617 if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
618 T(YAFFS_TRACE_VERIFY,
619 (TSTR("Obj %d header name is NULL"TENDSTR),
622 if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
623 T(YAFFS_TRACE_VERIFY,
624 (TSTR("Obj %d header name is 0xFF"TENDSTR),
630 /* Not being used, but don't want to throw away yet */
631 static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
632 __u32 level, int chunkOffset)
635 yaffs_Device *dev = obj->myDev;
641 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
642 if (tn->internal[i]) {
643 ok = yaffs_VerifyTnodeWorker(obj,
646 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
649 } else if (level == 0) {
650 yaffs_ExtendedTags tags;
651 __u32 objectId = obj->objectId;
653 chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
655 for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
656 __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
659 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
660 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
661 if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
662 T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
663 objectId, chunkOffset, theChunk,
664 tags.objectId, tags.chunkId));
678 static void yaffs_VerifyFile(yaffs_Object *obj)
680 int requiredTallness;
686 yaffs_ExtendedTags tags;
693 if (yaffs_SkipVerification(obj->myDev))
697 objectId = obj->objectId;
699 /* Check file size is consistent with tnode depth */
700 lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
701 x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
702 requiredTallness = 0;
704 x >>= YAFFS_TNODES_INTERNAL_BITS;
708 actualTallness = obj->variant.fileVariant.topLevel;
710 /* Check that the chunks in the tnode tree are all correct.
711 * We do this by scanning through the tnode tree and
712 * checking the tags for every chunk match.
715 if (yaffs_SkipNANDVerification(dev))
718 for (i = 1; i <= lastChunk; i++) {
719 tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
722 __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
724 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
725 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
726 if (tags.objectId != objectId || tags.chunkId != i) {
727 T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
728 objectId, i, theChunk,
729 tags.objectId, tags.chunkId));
737 static void yaffs_VerifyHardLink(yaffs_Object *obj)
739 if (obj && yaffs_SkipVerification(obj->myDev))
742 /* Verify sane equivalent object */
745 static void yaffs_VerifySymlink(yaffs_Object *obj)
747 if (obj && yaffs_SkipVerification(obj->myDev))
750 /* Verify symlink string */
753 static void yaffs_VerifySpecial(yaffs_Object *obj)
755 if (obj && yaffs_SkipVerification(obj->myDev))
759 static void yaffs_VerifyObject(yaffs_Object *obj)
768 __u32 chunkShouldNotBeDeleted;
774 if (obj->beingCreated)
779 if (yaffs_SkipVerification(dev))
782 /* Check sane object header chunk */
784 chunkMin = dev->internalStartBlock * dev->param.nChunksPerBlock;
785 chunkMax = (dev->internalEndBlock+1) * dev->param.nChunksPerBlock - 1;
787 chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
788 chunkIdOk = chunkInRange || (obj->hdrChunk == 0);
789 chunkValid = chunkInRange &&
790 yaffs_CheckChunkBit(dev,
791 obj->hdrChunk / dev->param.nChunksPerBlock,
792 obj->hdrChunk % dev->param.nChunksPerBlock);
793 chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
796 (!chunkIdOk || chunkShouldNotBeDeleted)) {
797 T(YAFFS_TRACE_VERIFY,
798 (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
799 obj->objectId, obj->hdrChunk,
800 chunkIdOk ? "" : ",out of range",
801 chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
804 if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
805 yaffs_ExtendedTags tags;
806 yaffs_ObjectHeader *oh;
807 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
809 oh = (yaffs_ObjectHeader *)buffer;
811 yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
814 yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
816 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
819 /* Verify it has a parent */
820 if (obj && !obj->fake &&
821 (!obj->parent || obj->parent->myDev != dev)) {
822 T(YAFFS_TRACE_VERIFY,
823 (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
824 obj->objectId, obj->parent));
827 /* Verify parent is a directory */
828 if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
829 T(YAFFS_TRACE_VERIFY,
830 (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
831 obj->objectId, obj->parent->variantType));
834 switch (obj->variantType) {
835 case YAFFS_OBJECT_TYPE_FILE:
836 yaffs_VerifyFile(obj);
838 case YAFFS_OBJECT_TYPE_SYMLINK:
839 yaffs_VerifySymlink(obj);
841 case YAFFS_OBJECT_TYPE_DIRECTORY:
842 yaffs_VerifyDirectory(obj);
844 case YAFFS_OBJECT_TYPE_HARDLINK:
845 yaffs_VerifyHardLink(obj);
847 case YAFFS_OBJECT_TYPE_SPECIAL:
848 yaffs_VerifySpecial(obj);
850 case YAFFS_OBJECT_TYPE_UNKNOWN:
852 T(YAFFS_TRACE_VERIFY,
853 (TSTR("Obj %d has illegaltype %d"TENDSTR),
854 obj->objectId, obj->variantType));
859 static void yaffs_VerifyObjects(yaffs_Device *dev)
863 struct ylist_head *lh;
865 if (yaffs_SkipVerification(dev))
868 /* Iterate through the objects in each hash entry */
870 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
871 ylist_for_each(lh, &dev->objectBucket[i].list) {
873 obj = ylist_entry(lh, yaffs_Object, hashLink);
874 yaffs_VerifyObject(obj);
882 * Simple hash function. Needs to have a reasonable spread
885 static Y_INLINE int yaffs_HashFunction(int n)
888 return n % YAFFS_NOBJECT_BUCKETS;
892 * Access functions to useful fake objects.
893 * Note that root might have a presence in NAND if permissions are set.
896 yaffs_Object *yaffs_Root(yaffs_Device *dev)
901 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
903 return dev->lostNFoundDir;
908 * Erased NAND checking functions
911 int yaffs_CheckFF(__u8 *buffer, int nBytes)
913 /* Horrible, slow implementation */
922 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
925 int retval = YAFFS_OK;
926 __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
927 yaffs_ExtendedTags tags;
930 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
932 if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
935 if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
936 T(YAFFS_TRACE_NANDACCESS,
937 (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
941 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
948 static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
951 yaffs_ExtendedTags *tags)
953 int retval = YAFFS_OK;
954 yaffs_ExtendedTags tempTags;
955 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
958 result = yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,&tempTags);
959 if(memcmp(buffer,data,dev->nDataBytesPerChunk) ||
960 tempTags.objectId != tags->objectId ||
961 tempTags.chunkId != tags->chunkId ||
962 tempTags.byteCount != tags->byteCount)
965 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
970 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
972 yaffs_ExtendedTags *tags,
979 yaffs_InvalidateCheckpoint(dev);
982 yaffs_BlockInfo *bi = 0;
985 chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
991 /* First check this chunk is erased, if it needs
992 * checking. The checking policy (unless forced
993 * always on) is as follows:
995 * Check the first page we try to write in a block.
996 * If the check passes then we don't need to check any
997 * more. If the check fails, we check again...
998 * If the block has been erased, we don't need to check.
1000 * However, if the block has been prioritised for gc,
1001 * then we think there might be something odd about
1002 * this block and stop using it.
1004 * Rationale: We should only ever see chunks that have
1005 * not been erased if there was a partially written
1006 * chunk due to power loss. This checking policy should
1007 * catch that case with very few checks and thus save a
1008 * lot of checks that are most likely not needed.
1011 * If an erase check fails or the write fails we skip the
1012 * rest of the block.
1015 /* let's give it a try */
1018 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
1019 bi->skipErasedCheck = 0;
1021 if (!bi->skipErasedCheck) {
1022 erasedOk = yaffs_CheckChunkErased(dev, chunk);
1023 if (erasedOk != YAFFS_OK) {
1024 T(YAFFS_TRACE_ERROR,
1025 (TSTR("**>> yaffs chunk %d was not erased"
1028 /* If not erased, delete this one,
1029 * skip rest of block and
1030 * try another chunk */
1031 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
1032 yaffs_SkipRestOfBlock(dev);
1037 writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
1040 if(!bi->skipErasedCheck)
1041 writeOk = yaffs_VerifyChunkWritten(dev, chunk, data, tags);
1043 if (writeOk != YAFFS_OK) {
1044 /* Clean up aborted write, skip to next block and
1045 * try another chunk */
1046 yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
1050 bi->skipErasedCheck = 1;
1052 /* Copy the data into the robustification buffer */
1053 yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
1055 } while (writeOk != YAFFS_OK &&
1056 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
1062 T(YAFFS_TRACE_ERROR,
1063 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
1066 dev->nRetriedWrites += (attempts - 1);
1074 * Oldest Dirty Sequence Number handling.
1077 /* yaffs_CalcOldestDirtySequence()
1078 * yaffs_FindOldestDirtySequence()
1079 * Calculate the oldest dirty sequence number if we don't know it.
1081 static void yaffs_CalcOldestDirtySequence(yaffs_Device *dev)
1085 unsigned blockNo = 0;
1088 if(!dev->param.isYaffs2)
1091 /* Find the oldest dirty sequence number. */
1092 seq = dev->sequenceNumber + 1;
1094 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
1095 if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
1096 (b->pagesInUse - b->softDeletions) < dev->param.nChunksPerBlock &&
1097 b->sequenceNumber < seq) {
1098 seq = b->sequenceNumber;
1105 dev->oldestDirtySequence = seq;
1106 dev->oldestDirtyBlock = blockNo;
1112 static void yaffs_FindOldestDirtySequence(yaffs_Device *dev)
1114 if(dev->param.isYaffs2 && !dev->oldestDirtySequence)
1115 yaffs_CalcOldestDirtySequence(dev);
1119 * yaffs_ClearOldestDirtySequence()
1120 * Called when a block is erased or marked bad. (ie. when its sequenceNumber
1121 * becomes invalid). If the value matches the oldest then we clear
1122 * dev->oldestDirtySequence to force its recomputation.
1124 static void yaffs_ClearOldestDirtySequence(yaffs_Device *dev, yaffs_BlockInfo *bi)
1127 if(!dev->param.isYaffs2)
1130 if(!bi || bi->sequenceNumber == dev->oldestDirtySequence){
1131 dev->oldestDirtySequence = 0;
1132 dev->oldestDirtyBlock = 0;
1137 * yaffs_UpdateOldestDirtySequence()
1138 * Update the oldest dirty sequence number whenever we dirty a block.
1139 * Only do this if the oldestDirtySequence is actually being tracked.
1141 static void yaffs_UpdateOldestDirtySequence(yaffs_Device *dev, unsigned blockNo, yaffs_BlockInfo *bi)
1143 if(dev->param.isYaffs2 && dev->oldestDirtySequence){
1144 if(dev->oldestDirtySequence > bi->sequenceNumber){
1145 dev->oldestDirtySequence = bi->sequenceNumber;
1146 dev->oldestDirtyBlock = blockNo;
1152 * Block retiring for handling a broken block.
1155 static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
1157 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1159 yaffs_InvalidateCheckpoint(dev);
1161 yaffs_ClearOldestDirtySequence(dev,bi);
1163 if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
1164 if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
1165 T(YAFFS_TRACE_ALWAYS, (TSTR(
1166 "yaffs: Failed to mark bad and erase block %d"
1167 TENDSTR), blockInNAND));
1169 yaffs_ExtendedTags tags;
1170 int chunkId = blockInNAND * dev->param.nChunksPerBlock;
1172 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
1174 memset(buffer, 0xff, dev->nDataBytesPerChunk);
1175 yaffs_InitialiseTags(&tags);
1176 tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
1177 if (dev->param.writeChunkWithTagsToNAND(dev, chunkId -
1178 dev->chunkOffset, buffer, &tags) != YAFFS_OK)
1179 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
1180 TCONT("write bad block marker to block %d")
1181 TENDSTR), blockInNAND));
1183 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
1187 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
1188 bi->gcPrioritise = 0;
1189 bi->needsRetiring = 0;
1191 dev->nRetiredBlocks++;
1195 * Functions for robustisizing TODO
1199 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
1201 const yaffs_ExtendedTags *tags)
1204 chunkInNAND=chunkInNAND;
1209 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
1210 const yaffs_ExtendedTags *tags)
1213 chunkInNAND=chunkInNAND;
1217 void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
1219 if (!bi->gcPrioritise) {
1220 bi->gcPrioritise = 1;
1221 dev->hasPendingPrioritisedGCs = 1;
1222 bi->chunkErrorStrikes++;
1224 if (bi->chunkErrorStrikes > 3) {
1225 bi->needsRetiring = 1; /* Too many stikes, so retire this */
1226 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
1232 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
1235 int blockInNAND = chunkInNAND / dev->param.nChunksPerBlock;
1236 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1238 yaffs_HandleChunkError(dev, bi);
1241 /* Was an actual write failure, so mark the block for retirement */
1242 bi->needsRetiring = 1;
1243 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1244 (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
1247 /* Delete the chunk */
1248 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
1249 yaffs_SkipRestOfBlock(dev);
1253 /*---------------- Name handling functions ------------*/
1255 static __u16 yaffs_CalcNameSum(const YCHAR *name)
1260 const YUCHAR *bname = (const YUCHAR *) name;
1262 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
1264 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1265 sum += yaffs_toupper(*bname) * i;
1267 sum += (*bname) * i;
1276 static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
1278 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
1279 memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
1280 if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
1281 yaffs_strcpy(obj->shortName, name);
1283 obj->shortName[0] = _Y('\0');
1285 obj->sum = yaffs_CalcNameSum(name);
1288 /*-------------------- TNODES -------------------
1290 * List of spare tnodes
1291 * The list is hooked together using the first pointer
1296 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
1298 yaffs_Tnode *tn = yaffs_AllocateRawTnode(dev);
1300 memset(tn, 0, dev->tnodeSize);
1304 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1309 /* FreeTnode frees up a tnode and puts it back on the free list */
1310 static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
1312 yaffs_FreeRawTnode(dev,tn);
1314 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1317 static void yaffs_DeinitialiseTnodesAndObjects(yaffs_Device *dev)
1319 yaffs_DeinitialiseRawTnodesAndObjects(dev);
1325 void yaffs_LoadLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
1328 __u32 *map = (__u32 *)tn;
1334 pos &= YAFFS_TNODES_LEVEL0_MASK;
1335 val >>= dev->chunkGroupBits;
1337 bitInMap = pos * dev->tnodeWidth;
1338 wordInMap = bitInMap / 32;
1339 bitInWord = bitInMap & (32 - 1);
1341 mask = dev->tnodeMask << bitInWord;
1343 map[wordInMap] &= ~mask;
1344 map[wordInMap] |= (mask & (val << bitInWord));
1346 if (dev->tnodeWidth > (32 - bitInWord)) {
1347 bitInWord = (32 - bitInWord);
1349 mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
1350 map[wordInMap] &= ~mask;
1351 map[wordInMap] |= (mask & (val >> bitInWord));
1355 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
1358 __u32 *map = (__u32 *)tn;
1364 pos &= YAFFS_TNODES_LEVEL0_MASK;
1366 bitInMap = pos * dev->tnodeWidth;
1367 wordInMap = bitInMap / 32;
1368 bitInWord = bitInMap & (32 - 1);
1370 val = map[wordInMap] >> bitInWord;
1372 if (dev->tnodeWidth > (32 - bitInWord)) {
1373 bitInWord = (32 - bitInWord);
1375 val |= (map[wordInMap] << bitInWord);
1378 val &= dev->tnodeMask;
1379 val <<= dev->chunkGroupBits;
1384 /* ------------------- End of individual tnode manipulation -----------------*/
1386 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
1387 * The look up tree is represented by the top tnode and the number of topLevel
1388 * in the tree. 0 means only the level 0 tnode is in the tree.
1391 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
1392 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
1393 yaffs_FileStructure *fStruct,
1396 yaffs_Tnode *tn = fStruct->top;
1398 int requiredTallness;
1399 int level = fStruct->topLevel;
1403 /* Check sane level and chunk Id */
1404 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
1407 if (chunkId > YAFFS_MAX_CHUNK_ID)
1410 /* First check we're tall enough (ie enough topLevel) */
1412 i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1413 requiredTallness = 0;
1415 i >>= YAFFS_TNODES_INTERNAL_BITS;
1419 if (requiredTallness > fStruct->topLevel)
1420 return NULL; /* Not tall enough, so we can't find it */
1422 /* Traverse down to level 0 */
1423 while (level > 0 && tn) {
1424 tn = tn->internal[(chunkId >>
1425 (YAFFS_TNODES_LEVEL0_BITS +
1427 YAFFS_TNODES_INTERNAL_BITS)) &
1428 YAFFS_TNODES_INTERNAL_MASK];
1435 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
1436 * This happens in two steps:
1437 * 1. If the tree isn't tall enough, then make it taller.
1438 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
1440 * Used when modifying the tree.
1442 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
1443 * be plugged into the ttree.
1446 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
1447 yaffs_FileStructure *fStruct,
1449 yaffs_Tnode *passedTn)
1451 int requiredTallness;
1459 /* Check sane level and page Id */
1460 if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
1463 if (chunkId > YAFFS_MAX_CHUNK_ID)
1466 /* First check we're tall enough (ie enough topLevel) */
1468 x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1469 requiredTallness = 0;
1471 x >>= YAFFS_TNODES_INTERNAL_BITS;
1476 if (requiredTallness > fStruct->topLevel) {
1477 /* Not tall enough, gotta make the tree taller */
1478 for (i = fStruct->topLevel; i < requiredTallness; i++) {
1480 tn = yaffs_GetTnode(dev);
1483 tn->internal[0] = fStruct->top;
1485 fStruct->topLevel++;
1487 T(YAFFS_TRACE_ERROR,
1488 (TSTR("yaffs: no more tnodes" TENDSTR)));
1494 /* Traverse down to level 0, adding anything we need */
1496 l = fStruct->topLevel;
1500 while (l > 0 && tn) {
1502 (YAFFS_TNODES_LEVEL0_BITS +
1503 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
1504 YAFFS_TNODES_INTERNAL_MASK;
1507 if ((l > 1) && !tn->internal[x]) {
1508 /* Add missing non-level-zero tnode */
1509 tn->internal[x] = yaffs_GetTnode(dev);
1510 if(!tn->internal[x])
1512 } else if (l == 1) {
1513 /* Looking from level 1 at level 0 */
1515 /* If we already have one, then release it.*/
1516 if (tn->internal[x])
1517 yaffs_FreeTnode(dev, tn->internal[x]);
1518 tn->internal[x] = passedTn;
1520 } else if (!tn->internal[x]) {
1521 /* Don't have one, none passed in */
1522 tn->internal[x] = yaffs_GetTnode(dev);
1523 if(!tn->internal[x])
1528 tn = tn->internal[x];
1532 /* top is level 0 */
1534 memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1535 yaffs_FreeTnode(dev, passedTn);
1542 static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
1543 yaffs_ExtendedTags *tags, int objectId,
1548 for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
1549 if (yaffs_CheckChunkBit(dev, theChunk / dev->param.nChunksPerBlock,
1550 theChunk % dev->param.nChunksPerBlock)) {
1552 if(dev->chunkGroupSize == 1)
1555 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
1557 if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
1569 /* Experimental code not being used yet. Might speed up file deletion */
1570 /* DeleteWorker scans backwards through the tnode tree and deletes all the
1571 * chunks and tnodes in the file.
1572 * Returns 1 if the tree was deleted.
1573 * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1576 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
1577 int chunkOffset, int *limit)
1582 yaffs_ExtendedTags tags;
1584 yaffs_Device *dev = in->myDev;
1590 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1592 if (tn->internal[i]) {
1593 if (limit && (*limit) < 0) {
1597 yaffs_DeleteWorker(in,
1605 YAFFS_TNODES_INTERNAL_BITS)
1610 yaffs_FreeTnode(dev,
1613 tn->internal[i] = NULL;
1617 return (allDone) ? 1 : 0;
1618 } else if (level == 0) {
1621 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
1623 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
1626 chunkInInode = (chunkOffset <<
1627 YAFFS_TNODES_LEVEL0_BITS) + i;
1630 yaffs_FindChunkInGroup(dev,
1636 if (foundChunk > 0) {
1637 yaffs_DeleteChunk(dev,
1642 *limit = *limit - 1;
1649 yaffs_LoadLevel0Tnode(dev, tn, i, 0);
1653 return (i < 0) ? 1 : 0;
1665 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1667 yaffs_BlockInfo *theBlock;
1670 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1672 blockNo = chunk / dev->param.nChunksPerBlock;
1673 theBlock = yaffs_GetBlockInfo(dev, blockNo);
1675 theBlock->softDeletions++;
1677 yaffs_UpdateOldestDirtySequence(dev, blockNo, theBlock);
1681 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1682 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1684 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1687 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
1688 __u32 level, int chunkOffset)
1693 yaffs_Device *dev = in->myDev;
1698 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1700 if (tn->internal[i]) {
1702 yaffs_SoftDeleteWorker(in,
1708 YAFFS_TNODES_INTERNAL_BITS)
1711 yaffs_FreeTnode(dev,
1714 tn->internal[i] = NULL;
1716 /* Hoosterman... how could this happen? */
1720 return (allDone) ? 1 : 0;
1721 } else if (level == 0) {
1723 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1724 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
1726 /* Note this does not find the real chunk, only the chunk group.
1727 * We make an assumption that a chunk group is not larger than
1730 yaffs_SoftDeleteChunk(dev, theChunk);
1731 yaffs_LoadLevel0Tnode(dev, tn, i, 0);
1745 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1748 obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
1749 if (obj->nDataChunks <= 0) {
1750 /* Empty file with no duplicate object headers, just delete it immediately */
1751 yaffs_FreeTnode(obj->myDev,
1752 obj->variant.fileVariant.top);
1753 obj->variant.fileVariant.top = NULL;
1754 T(YAFFS_TRACE_TRACING,
1755 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1757 yaffs_DoGenericObjectDeletion(obj);
1759 yaffs_SoftDeleteWorker(obj,
1760 obj->variant.fileVariant.top,
1761 obj->variant.fileVariant.
1763 obj->softDeleted = 1;
1768 /* Pruning removes any part of the file structure tree that is beyond the
1769 * bounds of the file (ie that does not point to chunks).
1771 * A file should only get pruned when its size is reduced.
1773 * Before pruning, the chunks must be pulled from the tree and the
1774 * level 0 tnode entries must be zeroed out.
1775 * Could also use this for file deletion, but that's probably better handled
1776 * by a special case.
1778 * This function is recursive. For levels > 0 the function is called again on
1779 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1780 * If there is no data in a subtree then it is pruned.
1783 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
1784 __u32 level, int del0)
1793 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1794 if (tn->internal[i]) {
1796 yaffs_PruneWorker(dev, tn->internal[i],
1798 (i == 0) ? del0 : 1);
1801 if (tn->internal[i])
1805 int tnodeSize_u32 = dev->tnodeSize/sizeof(__u32);
1806 __u32 *map = (__u32 *)tn;
1808 for(i = 0; !hasData && i < tnodeSize_u32; i++){
1814 if (hasData == 0 && del0) {
1815 /* Free and return NULL */
1817 yaffs_FreeTnode(dev, tn);
1827 static int yaffs_PruneFileStructure(yaffs_Device *dev,
1828 yaffs_FileStructure *fStruct)
1835 if (fStruct->topLevel > 0) {
1837 yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
1839 /* Now we have a tree with all the non-zero branches NULL but the height
1840 * is the same as it was.
1841 * Let's see if we can trim internal tnodes to shorten the tree.
1842 * We can do this if only the 0th element in the tnode is in use
1843 * (ie all the non-zero are NULL)
1846 while (fStruct->topLevel && !done) {
1850 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1851 if (tn->internal[i])
1856 fStruct->top = tn->internal[0];
1857 fStruct->topLevel--;
1858 yaffs_FreeTnode(dev, tn);
1868 /*-------------------- End of File Structure functions.-------------------*/
1871 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1872 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1874 yaffs_Object *obj = yaffs_AllocateRawObject(dev);
1879 /* Now sweeten it up... */
1881 memset(obj, 0, sizeof(yaffs_Object));
1882 obj->beingCreated = 1;
1886 obj->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1887 YINIT_LIST_HEAD(&(obj->hardLinks));
1888 YINIT_LIST_HEAD(&(obj->hashLink));
1889 YINIT_LIST_HEAD(&obj->siblings);
1892 /* Now make the directory sane */
1894 obj->parent = dev->rootDir;
1895 ylist_add(&(obj->siblings), &dev->rootDir->variant.directoryVariant.children);
1898 /* Add it to the lost and found directory.
1899 * NB Can't put root or lostNFound in lostNFound so
1900 * check if lostNFound exists first
1902 if (dev->lostNFoundDir)
1903 yaffs_AddObjectToDirectory(dev->lostNFoundDir, obj);
1905 obj->beingCreated = 0;
1908 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1913 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
1918 yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1920 obj->fake = 1; /* it is fake so it might have no NAND presence... */
1921 obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
1922 obj->unlinkAllowed = 0; /* ... or unlink it */
1925 obj->yst_mode = mode;
1927 obj->hdrChunk = 0; /* Not a valid chunk. */
1934 static void yaffs_UnhashObject(yaffs_Object *obj)
1937 yaffs_Device *dev = obj->myDev;
1939 /* If it is still linked into the bucket list, free from the list */
1940 if (!ylist_empty(&obj->hashLink)) {
1941 ylist_del_init(&obj->hashLink);
1942 bucket = yaffs_HashFunction(obj->objectId);
1943 dev->objectBucket[bucket].count--;
1947 /* FreeObject frees up a Object and puts it back on the free list */
1948 static void yaffs_FreeObject(yaffs_Object *obj)
1950 yaffs_Device *dev = obj->myDev;
1952 T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), obj, obj->myInode));
1958 if (!ylist_empty(&obj->siblings))
1963 /* We're still hooked up to a cached inode.
1964 * Don't delete now, but mark for later deletion
1966 obj->deferedFree = 1;
1970 yaffs_UnhashObject(obj);
1972 yaffs_FreeRawObject(dev,obj);
1974 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1978 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1980 if (obj->deferedFree)
1981 yaffs_FreeObject(obj);
1984 static void yaffs_InitialiseTnodesAndObjects(yaffs_Device *dev)
1991 yaffs_InitialiseRawTnodesAndObjects(dev);
1993 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1994 YINIT_LIST_HEAD(&dev->objectBucket[i].list);
1995 dev->objectBucket[i].count = 0;
1999 static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
2003 int lowest = 999999;
2006 /* Search for the shortest list or one that
2010 for (i = 0; i < 10 && lowest > 4; i++) {
2011 dev->bucketFinder++;
2012 dev->bucketFinder %= YAFFS_NOBJECT_BUCKETS;
2013 if (dev->objectBucket[dev->bucketFinder].count < lowest) {
2014 lowest = dev->objectBucket[dev->bucketFinder].count;
2015 l = dev->bucketFinder;
2023 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
2025 int bucket = yaffs_FindNiceObjectBucket(dev);
2027 /* Now find an object value that has not already been taken
2028 * by scanning the list.
2032 struct ylist_head *i;
2034 __u32 n = (__u32) bucket;
2036 /* yaffs_CheckObjectHashSanity(); */
2040 n += YAFFS_NOBJECT_BUCKETS;
2041 if (1 || dev->objectBucket[bucket].count > 0) {
2042 ylist_for_each(i, &dev->objectBucket[bucket].list) {
2043 /* If there is already one in the list */
2044 if (i && ylist_entry(i, yaffs_Object,
2045 hashLink)->objectId == n) {
2055 static void yaffs_HashObject(yaffs_Object *in)
2057 int bucket = yaffs_HashFunction(in->objectId);
2058 yaffs_Device *dev = in->myDev;
2060 ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
2061 dev->objectBucket[bucket].count++;
2064 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
2066 int bucket = yaffs_HashFunction(number);
2067 struct ylist_head *i;
2070 ylist_for_each(i, &dev->objectBucket[bucket].list) {
2071 /* Look if it is in the list */
2073 in = ylist_entry(i, yaffs_Object, hashLink);
2074 if (in->objectId == number) {
2076 /* Don't tell the VFS about this one if it is defered free */
2077 if (in->deferedFree)
2088 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
2089 yaffs_ObjectType type)
2091 yaffs_Object *theObject=NULL;
2092 yaffs_Tnode *tn = NULL;
2095 number = yaffs_CreateNewObjectNumber(dev);
2097 if (type == YAFFS_OBJECT_TYPE_FILE) {
2098 tn = yaffs_GetTnode(dev);
2103 theObject = yaffs_AllocateEmptyObject(dev);
2106 yaffs_FreeTnode(dev,tn);
2112 theObject->fake = 0;
2113 theObject->renameAllowed = 1;
2114 theObject->unlinkAllowed = 1;
2115 theObject->objectId = number;
2116 yaffs_HashObject(theObject);
2117 theObject->variantType = type;
2118 #ifdef CONFIG_YAFFS_WINCE
2119 yfsd_WinFileTimeNow(theObject->win_atime);
2120 theObject->win_ctime[0] = theObject->win_mtime[0] =
2121 theObject->win_atime[0];
2122 theObject->win_ctime[1] = theObject->win_mtime[1] =
2123 theObject->win_atime[1];
2127 theObject->yst_atime = theObject->yst_mtime =
2128 theObject->yst_ctime = Y_CURRENT_TIME;
2131 case YAFFS_OBJECT_TYPE_FILE:
2132 theObject->variant.fileVariant.fileSize = 0;
2133 theObject->variant.fileVariant.scannedFileSize = 0;
2134 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
2135 theObject->variant.fileVariant.topLevel = 0;
2136 theObject->variant.fileVariant.top = tn;
2138 case YAFFS_OBJECT_TYPE_DIRECTORY:
2139 YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
2141 YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
2144 case YAFFS_OBJECT_TYPE_SYMLINK:
2145 case YAFFS_OBJECT_TYPE_HARDLINK:
2146 case YAFFS_OBJECT_TYPE_SPECIAL:
2147 /* No action required */
2149 case YAFFS_OBJECT_TYPE_UNKNOWN:
2150 /* todo this should not happen */
2158 static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
2160 yaffs_ObjectType type)
2162 yaffs_Object *theObject = NULL;
2165 theObject = yaffs_FindObjectByNumber(dev, number);
2168 theObject = yaffs_CreateNewObject(dev, number, type);
2175 static YCHAR *yaffs_CloneString(const YCHAR *str)
2177 YCHAR *newStr = NULL;
2183 len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
2184 newStr = YMALLOC((len + 1) * sizeof(YCHAR));
2186 yaffs_strncpy(newStr, str,len);
2194 * Mknod (create) a new object.
2195 * equivalentObject only has meaning for a hard link;
2196 * aliasString only has meaning for a symlink.
2197 * rdev only has meaning for devices (a subset of special objects)
2200 static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
2201 yaffs_Object *parent,
2206 yaffs_Object *equivalentObject,
2207 const YCHAR *aliasString, __u32 rdev)
2212 yaffs_Device *dev = parent->myDev;
2214 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
2215 if (yaffs_FindObjectByName(parent, name))
2218 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2219 str = yaffs_CloneString(aliasString);
2224 in = yaffs_CreateNewObject(dev, -1, type);
2239 in->variantType = type;
2241 in->yst_mode = mode;
2243 #ifdef CONFIG_YAFFS_WINCE
2244 yfsd_WinFileTimeNow(in->win_atime);
2245 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
2246 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
2249 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
2251 in->yst_rdev = rdev;
2255 in->nDataChunks = 0;
2257 yaffs_SetObjectName(in, name);
2260 yaffs_AddObjectToDirectory(parent, in);
2262 in->myDev = parent->myDev;
2265 case YAFFS_OBJECT_TYPE_SYMLINK:
2266 in->variant.symLinkVariant.alias = str;
2268 case YAFFS_OBJECT_TYPE_HARDLINK:
2269 in->variant.hardLinkVariant.equivalentObject =
2271 in->variant.hardLinkVariant.equivalentObjectId =
2272 equivalentObject->objectId;
2273 ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
2275 case YAFFS_OBJECT_TYPE_FILE:
2276 case YAFFS_OBJECT_TYPE_DIRECTORY:
2277 case YAFFS_OBJECT_TYPE_SPECIAL:
2278 case YAFFS_OBJECT_TYPE_UNKNOWN:
2283 if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0, NULL) < 0) {
2284 /* Could not create the object header, fail the creation */
2285 yaffs_DeleteObject(in);
2289 yaffs_UpdateParent(parent);
2295 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
2296 __u32 mode, __u32 uid, __u32 gid)
2298 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2299 uid, gid, NULL, NULL, 0);
2302 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
2303 __u32 mode, __u32 uid, __u32 gid)
2305 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2306 mode, uid, gid, NULL, NULL, 0);
2309 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
2310 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
2312 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2313 uid, gid, NULL, NULL, rdev);
2316 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
2317 __u32 mode, __u32 uid, __u32 gid,
2320 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2321 uid, gid, NULL, alias, 0);
2324 /* yaffs_Link returns the object id of the equivalent object.*/
2325 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
2326 yaffs_Object *equivalentObject)
2328 /* Get the real object in case we were fed a hard link as an equivalent object */
2329 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
2331 if (yaffs_MknodObject
2332 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2333 equivalentObject, NULL, 0)) {
2334 return equivalentObject;
2341 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
2342 const YCHAR *newName, int force, int shadows)
2347 yaffs_Object *existingTarget;
2350 newDir = obj->parent; /* use the old directory */
2352 if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
2353 T(YAFFS_TRACE_ALWAYS,
2355 ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
2360 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
2361 if (obj->myDev->param.isYaffs2)
2362 unlinkOp = (newDir == obj->myDev->unlinkedDir);
2364 unlinkOp = (newDir == obj->myDev->unlinkedDir
2365 && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
2367 deleteOp = (newDir == obj->myDev->deletedDir);
2369 existingTarget = yaffs_FindObjectByName(newDir, newName);
2371 /* If the object is a file going into the unlinked directory,
2372 * then it is OK to just stuff it in since duplicate names are allowed.
2373 * else only proceed if the new name does not exist and if we're putting
2374 * it into a directory.
2381 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
2382 yaffs_SetObjectName(obj, newName);
2385 yaffs_AddObjectToDirectory(newDir, obj);
2390 /* If it is a deletion then we mark it as a shrink for gc purposes. */
2391 if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows, NULL) >= 0)
2398 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
2399 yaffs_Object *newDir, const YCHAR *newName)
2401 yaffs_Object *obj = NULL;
2402 yaffs_Object *existingTarget = NULL;
2408 if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2410 if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2413 dev = oldDir->myDev;
2415 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2416 /* Special case for case insemsitive systems (eg. WinCE).
2417 * While look-up is case insensitive, the name isn't.
2418 * Therefore we might want to change x.txt to X.txt
2420 if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
2424 if(yaffs_strnlen(newName,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
2428 obj = yaffs_FindObjectByName(oldDir, oldName);
2430 if (obj && obj->renameAllowed) {
2432 /* Now do the handling for an existing target, if there is one */
2434 existingTarget = yaffs_FindObjectByName(newDir, newName);
2435 if (existingTarget &&
2436 existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2437 !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
2438 /* There is a target that is a non-empty directory, so we fail */
2439 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
2440 } else if (existingTarget && existingTarget != obj) {
2441 /* Nuke the target first, using shadowing,
2442 * but only if it isn't the same object.
2444 * Note we must disable gc otherwise it can mess up the shadowing.
2448 yaffs_ChangeObjectName(obj, newDir, newName, force,
2449 existingTarget->objectId);
2450 existingTarget->isShadowed = 1;
2451 yaffs_UnlinkObject(existingTarget);
2455 result = yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
2457 yaffs_UpdateParent(oldDir);
2458 if(newDir != oldDir)
2459 yaffs_UpdateParent(newDir);
2466 /*------------------------- Block Management and Page Allocation ----------------*/
2468 static int yaffs_InitialiseBlocks(yaffs_Device *dev)
2470 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
2472 dev->blockInfo = NULL;
2473 dev->chunkBits = NULL;
2475 dev->allocationBlock = -1; /* force it to get a new one */
2477 /* If the first allocation strategy fails, thry the alternate one */
2478 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2479 if (!dev->blockInfo) {
2480 dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
2481 dev->blockInfoAlt = 1;
2483 dev->blockInfoAlt = 0;
2485 if (dev->blockInfo) {
2486 /* Set up dynamic blockinfo stuff. */
2487 dev->chunkBitmapStride = (dev->param.nChunksPerBlock + 7) / 8; /* round up bytes */
2488 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2489 if (!dev->chunkBits) {
2490 dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
2491 dev->chunkBitsAlt = 1;
2493 dev->chunkBitsAlt = 0;
2496 if (dev->blockInfo && dev->chunkBits) {
2497 memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
2498 memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
2505 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2507 if (dev->blockInfoAlt && dev->blockInfo)
2508 YFREE_ALT(dev->blockInfo);
2509 else if (dev->blockInfo)
2510 YFREE(dev->blockInfo);
2512 dev->blockInfoAlt = 0;
2514 dev->blockInfo = NULL;
2516 if (dev->chunkBitsAlt && dev->chunkBits)
2517 YFREE_ALT(dev->chunkBits);
2518 else if (dev->chunkBits)
2519 YFREE(dev->chunkBits);
2520 dev->chunkBitsAlt = 0;
2521 dev->chunkBits = NULL;
2524 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
2525 yaffs_BlockInfo *bi)
2528 if (!dev->param.isYaffs2)
2529 return 1; /* disqualification only applies to yaffs2. */
2531 if (!bi->hasShrinkHeader)
2532 return 1; /* can gc */
2534 yaffs_FindOldestDirtySequence(dev);
2536 /* Can't do gc of this block if there are any blocks older than this one that have
2539 return (bi->sequenceNumber <= dev->oldestDirtySequence);
2543 * yaffs_FindRefreshBlock()
2544 * periodically finds the oldest full block by sequence number for refreshing.
2547 static __u32 yaffs_FindRefreshBlock(yaffs_Device *dev)
2552 __u32 oldestSequence = 0;
2554 yaffs_BlockInfo *bi;
2557 * If refresh period < 10 then refreshing is disabled.
2559 if(dev->param.refreshPeriod < 10 ||
2560 !dev->param.isYaffs2)
2564 * Fix broken values.
2566 if(dev->refreshSkip > dev->param.refreshPeriod)
2567 dev->refreshSkip = dev->param.refreshPeriod;
2569 if(dev->refreshSkip > 0)
2573 * Refresh skip is now zero.
2574 * We'll do a refresh this time around....
2575 * Update the refresh skip and find the oldest block.
2577 dev->refreshSkip = dev->param.refreshPeriod;
2578 dev->refreshCount++;
2579 bi = dev->blockInfo;
2580 for (b = dev->internalStartBlock; b <=dev->internalEndBlock; b++){
2582 if (bi->blockState == YAFFS_BLOCK_STATE_FULL){
2585 bi->sequenceNumber < oldestSequence){
2587 oldestSequence = bi->sequenceNumber;
2595 (TSTR("GC refresh count %d selected block %d with sequenceNumber %d" TENDSTR),
2596 dev->refreshCount, oldest, oldestSequence));
2603 static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
2605 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
2609 /* If the block is still healthy erase it and mark as clean.
2610 * If the block has had a data failure, then retire it.
2613 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2614 (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
2615 blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
2617 yaffs_ClearOldestDirtySequence(dev,bi);
2619 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2621 /* If this is the block being garbage collected then stop gc'ing this block */
2622 if(blockNo == dev->gcBlock)
2625 /* If this block is currently the best candidate for gc then drop as a candidate */
2626 if(blockNo == dev->gcDirtiest){
2627 dev->gcDirtiest = 0;
2628 dev->gcPagesInUse = 0;
2631 if (!bi->needsRetiring) {
2632 yaffs_InvalidateCheckpoint(dev);
2633 erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
2635 dev->nErasureFailures++;
2636 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2637 (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
2642 ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
2644 for (i = 0; i < dev->param.nChunksPerBlock; i++) {
2645 if (!yaffs_CheckChunkErased
2646 (dev, blockNo * dev->param.nChunksPerBlock + i)) {
2647 T(YAFFS_TRACE_ERROR,
2649 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2650 TENDSTR), blockNo, i));
2656 /* Clean it up... */
2657 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2658 bi->sequenceNumber = 0;
2659 dev->nErasedBlocks++;
2661 bi->softDeletions = 0;
2662 bi->hasShrinkHeader = 0;
2663 bi->skipErasedCheck = 1; /* This is clean, so no need to check */
2664 bi->gcPrioritise = 0;
2665 yaffs_ClearChunkBits(dev, blockNo);
2667 T(YAFFS_TRACE_ERASE,
2668 (TSTR("Erased block %d" TENDSTR), blockNo));
2670 dev->nFreeChunks -= dev->param.nChunksPerBlock; /* We lost a block of free space */
2672 yaffs_RetireBlock(dev, blockNo);
2673 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2674 (TSTR("**>> Block %d retired" TENDSTR), blockNo));
2678 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2682 yaffs_BlockInfo *bi;
2684 if (dev->nErasedBlocks < 1) {
2685 /* Hoosterman we've got a problem.
2686 * Can't get space to gc
2688 T(YAFFS_TRACE_ERROR,
2689 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
2694 /* Find an empty block. */
2696 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
2697 dev->allocationBlockFinder++;
2698 if (dev->allocationBlockFinder < dev->internalStartBlock
2699 || dev->allocationBlockFinder > dev->internalEndBlock) {
2700 dev->allocationBlockFinder = dev->internalStartBlock;
2703 bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
2705 if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
2706 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2707 dev->sequenceNumber++;
2708 bi->sequenceNumber = dev->sequenceNumber;
2709 dev->nErasedBlocks--;
2710 T(YAFFS_TRACE_ALLOCATE,
2711 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
2712 dev->allocationBlockFinder, dev->sequenceNumber,
2713 dev->nErasedBlocks));
2714 return dev->allocationBlockFinder;
2718 T(YAFFS_TRACE_ALWAYS,
2720 ("yaffs tragedy: no more erased blocks, but there should have been %d"
2721 TENDSTR), dev->nErasedBlocks));
2728 static int yaffs_CheckpointRequired(yaffs_Device *dev)
2730 int nblocks = dev->internalEndBlock - dev->internalStartBlock + 1 ;
2731 return dev->param.isYaffs2 &&
2732 !dev->param.skipCheckpointWrite &&
2733 (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
2735 static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
2737 if (!dev->nCheckpointBlocksRequired &&
2738 yaffs_CheckpointRequired(dev)){
2739 /* Not a valid value so recalculate */
2742 int devBlocks = (dev->param.endBlock - dev->param.startBlock + 1);
2744 nBytes += sizeof(yaffs_CheckpointValidity);
2745 nBytes += sizeof(yaffs_CheckpointDevice);
2746 nBytes += devBlocks * sizeof(yaffs_BlockInfo);
2747 nBytes += devBlocks * dev->chunkBitmapStride;
2748 nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjects);
2749 nBytes += (dev->tnodeSize + sizeof(__u32)) * (dev->nTnodes);
2750 nBytes += sizeof(yaffs_CheckpointValidity);
2751 nBytes += sizeof(__u32); /* checksum*/
2753 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
2755 nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->param.nChunksPerBlock)) + 3;
2757 dev->nCheckpointBlocksRequired = nBlocks;
2760 return dev->nCheckpointBlocksRequired;
2764 * Check if there's space to allocate...
2765 * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
2767 static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev, int nChunks)
2770 int reservedBlocks = dev->param.nReservedBlocks;
2771 int checkpointBlocks;
2773 if (dev->param.isYaffs2) {
2774 checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
2775 dev->blocksInCheckpoint;
2776 if (checkpointBlocks < 0)
2777 checkpointBlocks = 0;
2779 checkpointBlocks = 0;
2782 reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->param.nChunksPerBlock);
2784 return (dev->nFreeChunks > (reservedChunks + nChunks));
2787 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
2788 yaffs_BlockInfo **blockUsedPtr)
2791 yaffs_BlockInfo *bi;
2793 if (dev->allocationBlock < 0) {
2794 /* Get next block to allocate off */
2795 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2796 dev->allocationPage = 0;
2799 if (!useReserve && !yaffs_CheckSpaceForAllocation(dev, 1)) {
2800 /* Not enough space to allocate unless we're allowed to use the reserve. */
2804 if (dev->nErasedBlocks < dev->param.nReservedBlocks
2805 && dev->allocationPage == 0) {
2806 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
2809 /* Next page please.... */
2810 if (dev->allocationBlock >= 0) {
2811 bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
2813 retVal = (dev->allocationBlock * dev->param.nChunksPerBlock) +
2814 dev->allocationPage;
2816 yaffs_SetChunkBit(dev, dev->allocationBlock,
2817 dev->allocationPage);
2819 dev->allocationPage++;
2823 /* If the block is full set the state to full */
2824 if (dev->allocationPage >= dev->param.nChunksPerBlock) {
2825 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2826 dev->allocationBlock = -1;
2835 T(YAFFS_TRACE_ERROR,
2836 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2841 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2845 n = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2847 if (dev->allocationBlock > 0)
2848 n += (dev->param.nChunksPerBlock - dev->allocationPage);
2855 * yaffs_SkipRestOfBlock() skips over the rest of the allocation block
2856 * if we don't want to write to it.
2858 static void yaffs_SkipRestOfBlock(yaffs_Device *dev)
2860 if(dev->allocationBlock > 0){
2861 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
2862 if(bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING){
2863 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2864 dev->allocationBlock = -1;
2870 static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
2876 int retVal = YAFFS_OK;
2879 int isCheckpointBlock;
2883 int chunksBefore = yaffs_GetErasedChunks(dev);
2886 yaffs_ExtendedTags tags;
2888 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
2890 yaffs_Object *object;
2892 isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
2895 T(YAFFS_TRACE_TRACING,
2896 (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
2899 bi->hasShrinkHeader,
2902 /*yaffs_VerifyFreeChunks(dev); */
2904 if(bi->blockState == YAFFS_BLOCK_STATE_FULL)
2905 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2907 bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
2911 if (isCheckpointBlock ||
2912 !yaffs_StillSomeChunkBits(dev, block)) {
2913 T(YAFFS_TRACE_TRACING,
2915 ("Collecting block %d that has no chunks in use" TENDSTR),
2917 yaffs_BlockBecameDirty(dev, block);
2920 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
2922 yaffs_VerifyBlock(dev, bi, block);
2924 maxCopies = (wholeBlock) ? dev->param.nChunksPerBlock : 5;
2925 oldChunk = block * dev->param.nChunksPerBlock + dev->gcChunk;
2927 for (/* init already done */;
2928 retVal == YAFFS_OK &&
2929 dev->gcChunk < dev->param.nChunksPerBlock &&
2930 (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
2932 dev->gcChunk++, oldChunk++) {
2933 if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
2935 /* This page is in use and might need to be copied off */
2941 yaffs_InitialiseTags(&tags);
2943 yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
2947 yaffs_FindObjectByNumber(dev,
2950 T(YAFFS_TRACE_GC_DETAIL,
2952 ("Collecting chunk in block %d, %d %d %d " TENDSTR),
2953 dev->gcChunk, tags.objectId, tags.chunkId,
2956 if (object && !yaffs_SkipVerification(dev)) {
2957 if (tags.chunkId == 0)
2958 matchingChunk = object->hdrChunk;
2959 else if (object->softDeleted)
2960 matchingChunk = oldChunk; /* Defeat the test */
2962 matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
2964 if (oldChunk != matchingChunk)
2965 T(YAFFS_TRACE_ERROR,
2966 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2967 oldChunk, matchingChunk, tags.objectId, tags.chunkId));
2972 T(YAFFS_TRACE_ERROR,
2974 ("page %d in gc has no object: %d %d %d "
2976 tags.objectId, tags.chunkId, tags.byteCount));
2981 object->softDeleted &&
2982 tags.chunkId != 0) {
2983 /* Data chunk in a soft deleted file, throw it away
2984 * It's a soft deleted data chunk,
2985 * No need to copy this, just forget about it and
2986 * fix up the object.
2989 /* Free chunks already includes softdeleted chunks.
2990 * How ever this chunk is going to soon be really deleted
2991 * which will increment free chunks.
2992 * We have to decrement free chunks so this works out properly.
2996 object->nDataChunks--;
2998 if (object->nDataChunks <= 0) {
2999 /* remeber to clean up the object */
3000 dev->gcCleanupList[cleanups] =
3006 /* Todo object && object->deleted && object->nDataChunks == 0 */
3007 /* Deleted object header with no data chunks.
3008 * Can be discarded and the file deleted.
3010 object->hdrChunk = 0;
3011 yaffs_FreeTnode(object->myDev,
3014 object->variant.fileVariant.top = NULL;
3015 yaffs_DoGenericObjectDeletion(object);
3017 } else if (object) {
3018 /* It's either a data chunk in a live file or
3019 * an ObjectHeader, so we're interested in it.
3020 * NB Need to keep the ObjectHeaders of deleted files
3021 * until the whole file has been deleted off
3023 tags.serialNumber++;
3027 if (tags.chunkId == 0) {
3028 /* It is an object Id,
3029 * We need to nuke the shrinkheader flags first
3030 * Also need to clean up shadowing.
3031 * We no longer want the shrinkHeader flag since its work is done
3032 * and if it is left in place it will mess up scanning.
3035 yaffs_ObjectHeader *oh;
3036 oh = (yaffs_ObjectHeader *)buffer;
3039 tags.extraIsShrinkHeader = 0;
3041 oh->shadowsObject = 0;
3042 oh->inbandShadowsObject = 0;
3043 tags.extraShadows = 0;
3045 /* Update file size */
3046 if(object->variantType == YAFFS_OBJECT_TYPE_FILE){
3047 oh->fileSize = object->variant.fileVariant.fileSize;
3048 tags.extraFileLength = oh->fileSize;
3051 yaffs_VerifyObjectHeader(object, oh, &tags, 1);
3053 yaffs_WriteNewChunkWithTagsToNAND(dev,(__u8 *) oh, &tags, 1);
3056 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
3059 retVal = YAFFS_FAIL;
3062 /* Ok, now fix up the Tnodes etc. */
3064 if (tags.chunkId == 0) {
3066 object->hdrChunk = newChunk;
3067 object->serial = tags.serialNumber;
3069 /* It's a data chunk */
3071 ok = yaffs_PutChunkIntoFile
3079 if (retVal == YAFFS_OK)
3080 yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
3085 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3088 /* Do any required cleanups */
3089 for (i = 0; i < cleanups; i++) {
3090 /* Time to delete the file too */
3092 yaffs_FindObjectByNumber(dev,
3093 dev->gcCleanupList[i]);
3095 yaffs_FreeTnode(dev,
3096 object->variant.fileVariant.
3098 object->variant.fileVariant.top = NULL;
3101 ("yaffs: About to finally delete object %d"
3102 TENDSTR), object->objectId));
3103 yaffs_DoGenericObjectDeletion(object);
3104 object->myDev->nDeletedFiles--;
3111 yaffs_VerifyCollectedBlock(dev, bi, block);
3115 if (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
3117 * The gc did not complete. Set block state back to FULL
3118 * because checkpointing does not restore gc.
3120 bi->blockState = YAFFS_BLOCK_STATE_FULL;
3122 /* The gc completed. */
3123 chunksAfter = yaffs_GetErasedChunks(dev);
3124 if (chunksBefore >= chunksAfter) {
3127 ("gc did not increase free chunks before %d after %d"
3128 TENDSTR), chunksBefore, chunksAfter));
3140 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
3141 * for garbage collection.
3144 static unsigned yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
3150 unsigned selected = 0;
3151 int prioritised = 0;
3152 int prioritisedExists = 0;
3153 yaffs_BlockInfo *bi;
3156 /* First let's see if we need to grab a prioritised block */
3157 if (dev->hasPendingPrioritisedGCs && !aggressive) {
3158 dev->gcDirtiest = 0;
3159 bi = dev->blockInfo;
3160 for (i = dev->internalStartBlock;
3161 i <= dev->internalEndBlock && !selected;
3164 if (bi->gcPrioritise) {
3165 prioritisedExists = 1;
3166 if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
3167 yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
3176 * If there is a prioritised block and none was selected then
3177 * this happened because there is at least one old dirty block gumming
3178 * up the works. Let's gc the oldest dirty block.
3181 if(prioritisedExists &&
3183 dev->oldestDirtyBlock > 0)
3184 selected = dev->oldestDirtyBlock;
3186 if (!prioritisedExists) /* None found, so we can clear this */
3187 dev->hasPendingPrioritisedGCs = 0;
3190 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
3192 * else (we're doing a leasurely gc), then we only bother to do this if the
3193 * block has only a few pages in use.
3198 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
3200 threshold = dev->param.nChunksPerBlock;
3201 iterations = nBlocks;
3203 int maxThreshold = dev->param.nChunksPerBlock/2;
3204 threshold = background ?
3205 (dev->gcNotDone + 2) * 2 : 0;
3206 if(threshold <YAFFS_GC_PASSIVE_THRESHOLD)
3207 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
3208 if(threshold > maxThreshold)
3209 threshold = maxThreshold;
3211 iterations = nBlocks / 16 + 1;
3212 if (iterations > 100)
3218 (dev->gcDirtiest < 1 ||
3219 dev->gcPagesInUse > YAFFS_GC_GOOD_ENOUGH);
3221 dev->gcBlockFinder++;
3222 if (dev->gcBlockFinder < dev->internalStartBlock ||
3223 dev->gcBlockFinder > dev->internalEndBlock)
3224 dev->gcBlockFinder = dev->internalStartBlock;
3226 bi = yaffs_GetBlockInfo(dev, dev->gcBlockFinder);
3228 pagesUsed = bi->pagesInUse - bi->softDeletions;
3230 if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
3231 pagesUsed < dev->param.nChunksPerBlock &&
3232 (dev->gcDirtiest < 1 || pagesUsed < dev->gcPagesInUse) &&
3233 yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
3234 dev->gcDirtiest = dev->gcBlockFinder;
3235 dev->gcPagesInUse = pagesUsed;
3239 if(dev->gcDirtiest > 0 && dev->gcPagesInUse <= threshold)
3240 selected = dev->gcDirtiest;
3244 * If nothing has been selected for a while, try selecting the oldest dirty
3245 * because that's gumming up the works.
3248 if(!selected && dev->param.isYaffs2 &&
3249 dev->gcNotDone >= ( background ? 10 : 20)){
3250 yaffs_FindOldestDirtySequence(dev);
3251 if(dev->oldestDirtyBlock > 0) {
3252 selected = dev->oldestDirtyBlock;
3253 dev->gcDirtiest = selected;
3254 dev->oldestDirtyGCs++;
3255 bi = yaffs_GetBlockInfo(dev, selected);
3256 dev->gcPagesInUse = bi->pagesInUse - bi->softDeletions;
3263 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR),
3265 dev->param.nChunksPerBlock - dev->gcPagesInUse,
3269 dev->backgroundGCs++;
3270 dev->gcDirtiest = 0;
3271 dev->gcPagesInUse = 0;
3273 if(dev->refreshSkip > 0)
3278 (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR),
3279 dev->gcBlockFinder, dev->gcNotDone,
3281 dev->gcDirtiest, dev->gcPagesInUse,
3282 dev->oldestDirtyBlock,
3283 background ? " bg" : ""));
3289 /* New garbage collector
3290 * If we're very low on erased blocks then we do aggressive garbage collection
3291 * otherwise we do "leasurely" garbage collection.
3292 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
3293 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
3295 * The idea is to help clear out space in a more spread-out manner.
3296 * Dunno if it really does anything useful.
3298 static int yaffs_CheckGarbageCollection(yaffs_Device *dev, int background)
3301 int gcOk = YAFFS_OK;
3307 int checkpointBlockAdjust;
3309 if(dev->param.gcControl &&
3310 (dev->param.gcControl(dev) & 1) == 0)
3313 if (dev->gcDisable) {
3314 /* Bail out so we don't get recursive gc */
3318 /* This loop should pass the first time.
3319 * We'll only see looping here if the collection does not increase space.
3325 checkpointBlockAdjust = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
3326 if (checkpointBlockAdjust < 0)
3327 checkpointBlockAdjust = 0;
3329 minErased = dev->param.nReservedBlocks + checkpointBlockAdjust + 1;
3330 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3332 /* If we need a block soon then do aggressive gc.*/
3333 if (dev->nErasedBlocks < minErased)
3336 if(dev->gcSkip > 20)
3338 if(erasedChunks < dev->nFreeChunks/2 ||
3350 /* If we don't already have a block being gc'd then see if we should start another */
3352 if (dev->gcBlock < 1 && !aggressive) {
3353 dev->gcBlock = yaffs_FindRefreshBlock(dev);
3356 if (dev->gcBlock < 1) {
3357 dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive, background);
3361 if (dev->gcBlock > 0) {
3368 ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
3369 dev->nErasedBlocks, aggressive));
3371 gcOk = yaffs_GarbageCollectBlock(dev, dev->gcBlock, aggressive);
3374 if (dev->nErasedBlocks < (dev->param.nReservedBlocks) && dev->gcBlock > 0) {
3377 ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
3378 TENDSTR), dev->nErasedBlocks, maxTries, dev->gcBlock));
3380 } while ((dev->nErasedBlocks < dev->param.nReservedBlocks) &&
3381 (dev->gcBlock > 0) &&
3384 return aggressive ? gcOk : YAFFS_OK;
3388 * yaffs_BackgroundGarbageCollect()
3389 * Garbage collects. Intended to be called from a background thread.
3390 * Returns non-zero if at least half the free chunks are erased.
3392 int yaffs_BackgroundGarbageCollect(yaffs_Device *dev, unsigned urgency)
3394 int erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3396 T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency));
3398 yaffs_CheckGarbageCollection(dev, 1);
3399 return erasedChunks > dev->nFreeChunks/2;
3402 /*------------------------- TAGS --------------------------------*/
3404 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
3407 return (tags->chunkId == chunkInObject &&
3408 tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
3413 /*-------------------- Data file manipulation -----------------*/
3415 static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
3416 yaffs_ExtendedTags *tags)
3418 /*Get the Tnode, then get the level 0 offset chunk offset */
3421 yaffs_ExtendedTags localTags;
3424 yaffs_Device *dev = in->myDev;
3427 /* Passed a NULL, so use our own tags space */
3431 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3434 theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
3437 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3443 static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
3444 yaffs_ExtendedTags *tags)
3446 /* Get the Tnode, then get the level 0 offset chunk offset */
3449 yaffs_ExtendedTags localTags;
3451 yaffs_Device *dev = in->myDev;
3455 /* Passed a NULL, so use our own tags space */
3459 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3463 theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
3466 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3469 /* Delete the entry in the filestructure (if found) */
3471 yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, 0);
3477 #ifdef YAFFS_PARANOID
3479 static int yaffs_CheckFileSanity(yaffs_Object *in)
3487 yaffs_Tags localTags;
3488 yaffs_Tags *tags = &localTags;
3492 if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
3495 objId = in->objectId;
3496 fSize = in->variant.fileVariant.fileSize;
3498 (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
3500 for (chunk = 1; chunk <= nChunks; chunk++) {
3501 tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
3506 theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
3508 if (yaffs_CheckChunkBits
3509 (dev, theChunk / dev->param.nChunksPerBlock,
3510 theChunk % dev->param.nChunksPerBlock)) {
3512 yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
3516 (tags, in->objectId, chunk, chunkDeleted)) {
3526 /* T(("No level 0 found for %d\n", chunk)); */
3530 return failed ? YAFFS_FAIL : YAFFS_OK;
3535 static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
3536 int chunkInNAND, int inScan)
3538 /* NB inScan is zero unless scanning.
3539 * For forward scanning, inScan is > 0;
3540 * for backward scanning inScan is < 0
3542 * chunkInNAND = 0 is a dummy insert to make sure the tnodes are there.
3546 yaffs_Device *dev = in->myDev;
3548 yaffs_ExtendedTags existingTags;
3549 yaffs_ExtendedTags newTags;
3550 unsigned existingSerial, newSerial;
3552 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
3553 /* Just ignore an attempt at putting a chunk into a non-file during scanning
3554 * If it is not during Scanning then something went wrong!
3557 T(YAFFS_TRACE_ERROR,
3559 ("yaffs tragedy:attempt to put data chunk into a non-file"
3564 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
3568 tn = yaffs_AddOrFindLevel0Tnode(dev,
3569 &in->variant.fileVariant,
3576 /* Dummy insert, bail now */
3579 existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
3582 /* If we're scanning then we need to test for duplicates
3583 * NB This does not need to be efficient since it should only ever
3584 * happen when the power fails during a write, then only one
3585 * chunk should ever be affected.
3587 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3588 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3591 if (existingChunk > 0) {
3592 /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
3593 * thus we have to do a FindChunkInFile to get the real chunk id.
3595 * We have a duplicate now we need to decide which one to use:
3597 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3598 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
3599 * YAFFS1: Get both sets of tags and compare serial numbers.
3603 /* Only do this for forward scanning */
3604 yaffs_ReadChunkWithTagsFromNAND(dev,
3608 /* Do a proper find */
3610 yaffs_FindChunkInFile(in, chunkInInode,
3614 if (existingChunk <= 0) {
3615 /*Hoosterman - how did this happen? */
3617 T(YAFFS_TRACE_ERROR,
3619 ("yaffs tragedy: existing chunk < 0 in scan"
3624 /* NB The deleted flags should be false, otherwise the chunks will
3625 * not be loaded during a scan
3629 newSerial = newTags.serialNumber;
3630 existingSerial = existingTags.serialNumber;
3634 (in->myDev->param.isYaffs2 ||
3635 existingChunk <= 0 ||
3636 ((existingSerial + 1) & 3) == newSerial)) {
3637 /* Forward scanning.
3639 * Delete the old one and drop through to update the tnode
3641 yaffs_DeleteChunk(dev, existingChunk, 1,
3644 /* Backward scanning or we want to use the existing one
3646 * Delete the new one and return early so that the tnode isn't changed
3648 yaffs_DeleteChunk(dev, chunkInNAND, 1,
3656 if (existingChunk == 0)
3659 yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
3664 static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
3667 int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
3669 if (chunkInNAND >= 0)
3670 return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
3673 T(YAFFS_TRACE_NANDACCESS,
3674 (TSTR("Chunk %d not found zero instead" TENDSTR),
3676 /* get sane (zero) data if you read a hole */
3677 memset(buffer, 0, in->myDev->nDataBytesPerChunk);
3683 void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
3687 yaffs_ExtendedTags tags;
3688 yaffs_BlockInfo *bi;
3694 block = chunkId / dev->param.nChunksPerBlock;
3695 page = chunkId % dev->param.nChunksPerBlock;
3698 if (!yaffs_CheckChunkBit(dev, block, page))
3699 T(YAFFS_TRACE_VERIFY,
3700 (TSTR("Deleting invalid chunk %d"TENDSTR),
3703 bi = yaffs_GetBlockInfo(dev, block);
3705 yaffs_UpdateOldestDirtySequence(dev, block, bi);
3707 T(YAFFS_TRACE_DELETION,
3708 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
3711 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->param.isYaffs2) {
3713 yaffs_InitialiseTags(&tags);
3715 tags.chunkDeleted = 1;
3717 yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
3718 yaffs_HandleUpdateChunk(dev, chunkId, &tags);
3720 dev->nUnmarkedDeletions++;
3723 /* Pull out of the management area.
3724 * If the whole block became dirty, this will kick off an erasure.
3726 if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3727 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3728 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3729 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
3732 yaffs_ClearChunkBit(dev, block, page);
3736 if (bi->pagesInUse == 0 &&
3737 !bi->hasShrinkHeader &&
3738 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3739 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3740 yaffs_BlockBecameDirty(dev, block);
3747 static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
3748 const __u8 *buffer, int nBytes,
3751 /* Find old chunk Need to do this to get serial number
3752 * Write new one and patch into tree.
3753 * Invalidate old tags.
3757 yaffs_ExtendedTags prevTags;
3760 yaffs_ExtendedTags newTags;
3762 yaffs_Device *dev = in->myDev;
3764 yaffs_CheckGarbageCollection(dev,0);
3766 /* Get the previous chunk at this location in the file if it exists.
3767 * If it does not exist then put a zero into the tree. This creates
3768 * the tnode now, rather than later when it is harder to clean up.
3770 prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
3771 if(prevChunkId < 1 &&
3772 !yaffs_PutChunkIntoFile(in, chunkInInode, 0, 0))
3775 /* Set up new tags */
3776 yaffs_InitialiseTags(&newTags);
3778 newTags.chunkId = chunkInInode;
3779 newTags.objectId = in->objectId;
3780 newTags.serialNumber =
3781 (prevChunkId > 0) ? prevTags.serialNumber + 1 : 1;
3782 newTags.byteCount = nBytes;
3784 if (nBytes < 1 || nBytes > dev->param.totalBytesPerChunk) {
3785 T(YAFFS_TRACE_ERROR,
3786 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
3792 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3795 if (newChunkId > 0) {
3796 yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
3798 if (prevChunkId > 0)
3799 yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
3801 yaffs_CheckFileSanity(in);
3807 /* UpdateObjectHeader updates the header on NAND for an object.
3808 * If name is not NULL, then that new name is used.
3810 int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
3811 int isShrink, int shadows, yaffs_XAttrMod *xmod)
3814 yaffs_BlockInfo *bi;
3816 yaffs_Device *dev = in->myDev;
3823 yaffs_ExtendedTags newTags;
3824 yaffs_ExtendedTags oldTags;
3825 YCHAR *alias = NULL;
3827 __u8 *buffer = NULL;
3828 YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
3830 yaffs_ObjectHeader *oh = NULL;
3832 yaffs_strcpy(oldName, _Y("silly old name"));
3836 in == dev->rootDir || /* The rootDir should also be saved */
3839 yaffs_CheckGarbageCollection(dev,0);
3840 yaffs_CheckObjectDetailsLoaded(in);
3842 buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
3843 oh = (yaffs_ObjectHeader *) buffer;
3845 prevChunkId = in->hdrChunk;
3847 if (prevChunkId > 0) {
3848 result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
3851 yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
3853 memcpy(oldName, oh->name, sizeof(oh->name));
3854 memset(buffer, 0xFF, sizeof(yaffs_ObjectHeader));
3856 memset(buffer, 0xFF, dev->nDataBytesPerChunk);
3858 oh->type = in->variantType;
3859 oh->yst_mode = in->yst_mode;
3860 oh->shadowsObject = oh->inbandShadowsObject = shadows;
3862 #ifdef CONFIG_YAFFS_WINCE
3863 oh->win_atime[0] = in->win_atime[0];
3864 oh->win_ctime[0] = in->win_ctime[0];
3865 oh->win_mtime[0] = in->win_mtime[0];
3866 oh->win_atime[1] = in->win_atime[1];
3867 oh->win_ctime[1] = in->win_ctime[1];
3868 oh->win_mtime[1] = in->win_mtime[1];
3870 oh->yst_uid = in->yst_uid;
3871 oh->yst_gid = in->yst_gid;
3872 oh->yst_atime = in->yst_atime;
3873 oh->yst_mtime = in->yst_mtime;
3874 oh->yst_ctime = in->yst_ctime;
3875 oh->yst_rdev = in->yst_rdev;
3878 oh->parentObjectId = in->parent->objectId;
3880 oh->parentObjectId = 0;
3882 if (name && *name) {
3883 memset(oh->name, 0, sizeof(oh->name));
3884 yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
3885 } else if (prevChunkId > 0)
3886 memcpy(oh->name, oldName, sizeof(oh->name));
3888 memset(oh->name, 0, sizeof(oh->name));
3890 oh->isShrink = isShrink;
3892 switch (in->variantType) {
3893 case YAFFS_OBJECT_TYPE_UNKNOWN:
3894 /* Should not happen */
3896 case YAFFS_OBJECT_TYPE_FILE:
3898 (oh->parentObjectId == YAFFS_OBJECTID_DELETED
3899 || oh->parentObjectId ==
3900 YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
3901 fileVariant.fileSize;
3903 case YAFFS_OBJECT_TYPE_HARDLINK:
3904 oh->equivalentObjectId =
3905 in->variant.hardLinkVariant.equivalentObjectId;
3907 case YAFFS_OBJECT_TYPE_SPECIAL:
3910 case YAFFS_OBJECT_TYPE_DIRECTORY:
3913 case YAFFS_OBJECT_TYPE_SYMLINK:
3914 alias = in->variant.symLinkVariant.alias;
3916 alias = _Y("no alias");
3917 yaffs_strncpy(oh->alias,
3919 YAFFS_MAX_ALIAS_LENGTH);
3920 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3924 /* process any xattrib modifications */
3926 yaffs_ApplyXMod(dev, (char *)buffer, xmod);
3930 yaffs_InitialiseTags(&newTags);
3932 newTags.chunkId = 0;
3933 newTags.objectId = in->objectId;
3934 newTags.serialNumber = in->serial;
3936 /* Add extra info for file header */
3938 newTags.extraHeaderInfoAvailable = 1;
3939 newTags.extraParentObjectId = oh->parentObjectId;
3940 newTags.extraFileLength = oh->fileSize;
3941 newTags.extraIsShrinkHeader = oh->isShrink;
3942 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3943 newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
3944 newTags.extraObjectType = in->variantType;
3946 yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
3948 /* Create new chunk in NAND */
3950 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3951 (prevChunkId > 0) ? 1 : 0);
3953 if (newChunkId >= 0) {
3955 in->hdrChunk = newChunkId;
3957 if (prevChunkId > 0) {
3958 yaffs_DeleteChunk(dev, prevChunkId, 1,
3962 if (!yaffs_ObjectHasCachedWriteData(in))
3965 /* If this was a shrink, then mark the block that the chunk lives on */
3967 bi = yaffs_GetBlockInfo(in->myDev,
3968 newChunkId / in->myDev->param.nChunksPerBlock);
3969 bi->hasShrinkHeader = 1;
3974 retVal = newChunkId;
3979 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3984 /*------------------------ Short Operations Cache ----------------------------------------
3985 * In many situations where there is no high level buffering (eg WinCE) a lot of
3986 * reads might be short sequential reads, and a lot of writes may be short
3987 * sequential writes. eg. scanning/writing a jpeg file.
3988 * In these cases, a short read/write cache can provide a huge perfomance benefit
3989 * with dumb-as-a-rock code.
3990 * In Linux, the page cache provides read buffering aand the short op cache provides write
3993 * There are a limited number (~10) of cache chunks per device so that we don't
3994 * need a very intelligent search.
3997 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
3999 yaffs_Device *dev = obj->myDev;
4001 yaffs_ChunkCache *cache;
4002 int nCaches = obj->myDev->param.nShortOpCaches;
4004 for (i = 0; i < nCaches; i++) {
4005 cache = &dev->srCache[i];
4006 if (cache->object == obj &&
4015 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
4017 yaffs_Device *dev = obj->myDev;
4018 int lowest = -99; /* Stop compiler whining. */
4020 yaffs_ChunkCache *cache;
4021 int chunkWritten = 0;
4022 int nCaches = obj->myDev->param.nShortOpCaches;
4028 /* Find the dirty cache for this object with the lowest chunk id. */
4029 for (i = 0; i < nCaches; i++) {
4030 if (dev->srCache[i].object == obj &&
4031 dev->srCache[i].dirty) {
4033 || dev->srCache[i].chunkId <
4035 cache = &dev->srCache[i];
4036 lowest = cache->chunkId;
4041 if (cache && !cache->locked) {
4042 /* Write it out and free it up */
4045 yaffs_WriteChunkDataToObject(cache->object,
4051 cache->object = NULL;
4054 } while (cache && chunkWritten > 0);
4057 /* Hoosterman, disk full while writing cache out. */
4058 T(YAFFS_TRACE_ERROR,
4059 (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
4066 /*yaffs_FlushEntireDeviceCache(dev)
4071 void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
4074 int nCaches = dev->param.nShortOpCaches;
4077 /* Find a dirty object in the cache and flush it...
4078 * until there are no further dirty objects.
4082 for (i = 0; i < nCaches && !obj; i++) {
4083 if (dev->srCache[i].object &&
4084 dev->srCache[i].dirty)
4085 obj = dev->srCache[i].object;
4089 yaffs_FlushFilesChunkCache(obj);
4096 /* Grab us a cache chunk for use.
4097 * First look for an empty one.
4098 * Then look for the least recently used non-dirty one.
4099 * Then look for the least recently used dirty one...., flush and look again.
4101 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
4105 if (dev->param.nShortOpCaches > 0) {
4106 for (i = 0; i < dev->param.nShortOpCaches; i++) {
4107 if (!dev->srCache[i].object)
4108 return &dev->srCache[i];
4115 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
4117 yaffs_ChunkCache *cache;
4118 yaffs_Object *theObj;
4123 if (dev->param.nShortOpCaches > 0) {
4124 /* Try find a non-dirty one... */
4126 cache = yaffs_GrabChunkCacheWorker(dev);
4129 /* They were all dirty, find the last recently used object and flush
4130 * its cache, then find again.
4131 * NB what's here is not very accurate, we actually flush the object
4132 * the last recently used page.
4135 /* With locking we can't assume we can use entry zero */
4142 for (i = 0; i < dev->param.nShortOpCaches; i++) {
4143 if (dev->srCache[i].object &&
4144 !dev->srCache[i].locked &&
4145 (dev->srCache[i].lastUse < usage || !cache)) {
4146 usage = dev->srCache[i].lastUse;
4147 theObj = dev->srCache[i].object;
4148 cache = &dev->srCache[i];
4153 if (!cache || cache->dirty) {
4154 /* Flush and try again */
4155 yaffs_FlushFilesChunkCache(theObj);
4156 cache = yaffs_GrabChunkCacheWorker(dev);
4166 /* Find a cached chunk */
4167 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
4170 yaffs_Device *dev = obj->myDev;
4172 if (dev->param.nShortOpCaches > 0) {
4173 for (i = 0; i < dev->param.nShortOpCaches; i++) {
4174 if (dev->srCache[i].object == obj &&
4175 dev->srCache[i].chunkId == chunkId) {
4178 return &dev->srCache[i];
4185 /* Mark the chunk for the least recently used algorithym */
4186 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
4190 if (dev->param.nShortOpCaches > 0) {
4191 if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
4192 /* Reset the cache usages */
4194 for (i = 1; i < dev->param.nShortOpCaches; i++)
4195 dev->srCache[i].lastUse = 0;
4202 cache->lastUse = dev->srLastUse;
4209 /* Invalidate a single cache page.
4210 * Do this when a whole page gets written,
4211 * ie the short cache for this page is no longer valid.
4213 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
4215 if (object->myDev->param.nShortOpCaches > 0) {
4216 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
4219 cache->object = NULL;
4223 /* Invalidate all the cache pages associated with this object
4224 * Do this whenever ther file is deleted or resized.
4226 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
4229 yaffs_Device *dev = in->myDev;
4231 if (dev->param.nShortOpCaches > 0) {
4232 /* Invalidate it. */
4233 for (i = 0; i < dev->param.nShortOpCaches; i++) {
4234 if (dev->srCache[i].object == in)
4235 dev->srCache[i].object = NULL;
4240 /*--------------------- Checkpointing --------------------*/
4243 static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
4245 yaffs_CheckpointValidity cp;
4247 memset(&cp, 0, sizeof(cp));
4249 cp.structType = sizeof(cp);
4250 cp.magic = YAFFS_MAGIC;
4251 cp.version = YAFFS_CHECKPOINT_VERSION;
4252 cp.head = (head) ? 1 : 0;
4254 return (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
4258 static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
4260 yaffs_CheckpointValidity cp;
4263 ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
4266 ok = (cp.structType == sizeof(cp)) &&
4267 (cp.magic == YAFFS_MAGIC) &&
4268 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
4269 (cp.head == ((head) ? 1 : 0));
4273 static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
4276 cp->nErasedBlocks = dev->nErasedBlocks;
4277 cp->allocationBlock = dev->allocationBlock;
4278 cp->allocationPage = dev->allocationPage;
4279 cp->nFreeChunks = dev->nFreeChunks;
4281 cp->nDeletedFiles = dev->nDeletedFiles;
4282 cp->nUnlinkedFiles = dev->nUnlinkedFiles;
4283 cp->nBackgroundDeletions = dev->nBackgroundDeletions;
4284 cp->sequenceNumber = dev->sequenceNumber;
4288 static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
4289 yaffs_CheckpointDevice *cp)
4291 dev->nErasedBlocks = cp->nErasedBlocks;
4292 dev->allocationBlock = cp->allocationBlock;
4293 dev->allocationPage = cp->allocationPage;
4294 dev->nFreeChunks = cp->nFreeChunks;
4296 dev->nDeletedFiles = cp->nDeletedFiles;
4297 dev->nUnlinkedFiles = cp->nUnlinkedFiles;
4298 dev->nBackgroundDeletions = cp->nBackgroundDeletions;
4299 dev->sequenceNumber = cp->sequenceNumber;
4303 static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
4305 yaffs_CheckpointDevice cp;
4307 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
4311 /* Write device runtime values*/
4312 yaffs_DeviceToCheckpointDevice(&cp, dev);
4313 cp.structType = sizeof(cp);
4315 ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
4317 /* Write block info */
4319 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
4320 ok = (yaffs_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
4323 /* Write chunk bits */
4325 nBytes = nBlocks * dev->chunkBitmapStride;
4326 ok = (yaffs_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
4332 static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
4334 yaffs_CheckpointDevice cp;
4336 __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
4340 ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
4344 if (cp.structType != sizeof(cp))
4348 yaffs_CheckpointDeviceToDevice(dev, &cp);
4350 nBytes = nBlocks * sizeof(yaffs_BlockInfo);
4352 ok = (yaffs_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
4356 nBytes = nBlocks * dev->chunkBitmapStride;
4358 ok = (yaffs_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
4363 static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
4367 cp->objectId = obj->objectId;
4368 cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
4369 cp->hdrChunk = obj->hdrChunk;
4370 cp->variantType = obj->variantType;
4371 cp->deleted = obj->deleted;
4372 cp->softDeleted = obj->softDeleted;
4373 cp->unlinked = obj->unlinked;
4374 cp->fake = obj->fake;
4375 cp->renameAllowed = obj->renameAllowed;
4376 cp->unlinkAllowed = obj->unlinkAllowed;
4377 cp->serial = obj->serial;
4378 cp->nDataChunks = obj->nDataChunks;
4380 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4381 cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
4382 else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4383 cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
4386 static int yaffs_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
4389 yaffs_Object *parent;
4391 if (obj->variantType != cp->variantType) {
4392 T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
4393 TCONT("chunk %d does not match existing object type %d")
4394 TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
4399 obj->objectId = cp->objectId;
4402 parent = yaffs_FindOrCreateObjectByNumber(
4405 YAFFS_OBJECT_TYPE_DIRECTORY);
4410 if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
4411 T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
4412 TCONT(" chunk %d Parent type, %d, not directory")
4414 cp->objectId, cp->parentId, cp->variantType,
4415 cp->hdrChunk, parent->variantType));
4418 yaffs_AddObjectToDirectory(parent, obj);
4421 obj->hdrChunk = cp->hdrChunk;
4422 obj->variantType = cp->variantType;
4423 obj->deleted = cp->deleted;
4424 obj->softDeleted = cp->softDeleted;
4425 obj->unlinked = cp->unlinked;
4426 obj->fake = cp->fake;
4427 obj->renameAllowed = cp->renameAllowed;
4428 obj->unlinkAllowed = cp->unlinkAllowed;
4429 obj->serial = cp->serial;
4430 obj->nDataChunks = cp->nDataChunks;
4432 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4433 obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
4434 else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4435 obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
4437 if (obj->hdrChunk > 0)
4438 obj->lazyLoaded = 1;
4444 static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
4445 __u32 level, int chunkOffset)
4448 yaffs_Device *dev = in->myDev;
4454 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
4455 if (tn->internal[i]) {
4456 ok = yaffs_CheckpointTnodeWorker(in,
4459 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4462 } else if (level == 0) {
4463 __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
4464 ok = (yaffs_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
4466 ok = (yaffs_CheckpointWrite(dev, tn, dev->tnodeSize) == dev->tnodeSize);
4474 static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
4476 __u32 endMarker = ~0;
4479 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
4480 ok = yaffs_CheckpointTnodeWorker(obj,
4481 obj->variant.fileVariant.top,
4482 obj->variant.fileVariant.topLevel,
4485 ok = (yaffs_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
4492 static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
4496 yaffs_Device *dev = obj->myDev;
4497 yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
4501 ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
4503 while (ok && (~baseChunk)) {
4505 /* Read level 0 tnode */
4508 tn = yaffs_GetTnode(dev);
4510 ok = (yaffs_CheckpointRead(dev, tn, dev->tnodeSize) == dev->tnodeSize);
4515 ok = yaffs_AddOrFindLevel0Tnode(dev,
4521 ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
4525 T(YAFFS_TRACE_CHECKPOINT, (
4526 TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
4527 nread, baseChunk, ok));
4533 static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
4536 yaffs_CheckpointObject cp;
4539 struct ylist_head *lh;
4542 /* Iterate through the objects in each hash entry,
4543 * dumping them to the checkpointing stream.
4546 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
4547 ylist_for_each(lh, &dev->objectBucket[i].list) {
4549 obj = ylist_entry(lh, yaffs_Object, hashLink);
4550 if (!obj->deferedFree) {
4551 yaffs_ObjectToCheckpointObject(&cp, obj);
4552 cp.structType = sizeof(cp);
4554 T(YAFFS_TRACE_CHECKPOINT, (
4555 TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" TENDSTR),
4556 cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, obj));
4558 ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
4560 if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4561 ok = yaffs_WriteCheckpointTnodes(obj);
4567 /* Dump end of list */
4568 memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
4569 cp.structType = sizeof(cp);
4572 ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
4577 static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
4580 yaffs_CheckpointObject cp;
4583 yaffs_Object *hardList = NULL;
4585 while (ok && !done) {
4586 ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
4587 if (cp.structType != sizeof(cp)) {
4588 T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
4589 cp.structType, (int)sizeof(cp), ok));
4593 T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
4594 cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
4596 if (ok && cp.objectId == ~0)
4599 obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
4601 ok = yaffs_CheckpointObjectToObject(obj, &cp);
4604 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
4605 ok = yaffs_ReadCheckpointTnodes(obj);
4606 } else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
4607 obj->hardLinks.next =
4608 (struct ylist_head *) hardList;
4617 yaffs_HardlinkFixup(dev, hardList);
4622 static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
4624 __u32 checkpointSum;
4627 yaffs_GetCheckpointSum(dev, &checkpointSum);
4629 ok = (yaffs_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
4637 static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
4639 __u32 checkpointSum0;
4640 __u32 checkpointSum1;
4643 yaffs_GetCheckpointSum(dev, &checkpointSum0);
4645 ok = (yaffs_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
4650 if (checkpointSum0 != checkpointSum1)
4657 static int yaffs_WriteCheckpointData(yaffs_Device *dev)
4661 if (!yaffs_CheckpointRequired(dev)) {
4662 T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
4667 ok = yaffs_CheckpointOpen(dev, 1);
4670 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
4671 ok = yaffs_WriteCheckpointValidityMarker(dev, 1);
4674 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
4675 ok = yaffs_WriteCheckpointDevice(dev);
4678 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
4679 ok = yaffs_WriteCheckpointObjects(dev);
4682 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
4683 ok = yaffs_WriteCheckpointValidityMarker(dev, 0);
4687 ok = yaffs_WriteCheckpointSum(dev);
4689 if (!yaffs_CheckpointClose(dev))
4693 dev->isCheckpointed = 1;
4695 dev->isCheckpointed = 0;
4697 return dev->isCheckpointed;
4700 static int yaffs_ReadCheckpointData(yaffs_Device *dev)
4704 if (dev->param.skipCheckpointRead || !dev->param.isYaffs2) {
4705 T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
4710 ok = yaffs_CheckpointOpen(dev, 0); /* open for read */
4713 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
4714 ok = yaffs_ReadCheckpointValidityMarker(dev, 1);
4717 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
4718 ok = yaffs_ReadCheckpointDevice(dev);
4721 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
4722 ok = yaffs_ReadCheckpointObjects(dev);
4725 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
4726 ok = yaffs_ReadCheckpointValidityMarker(dev, 0);
4730 ok = yaffs_ReadCheckpointSum(dev);
4731 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
4734 if (!yaffs_CheckpointClose(dev))
4738 dev->isCheckpointed = 1;
4740 dev->isCheckpointed = 0;
4746 static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
4748 if (dev->isCheckpointed ||
4749 dev->blocksInCheckpoint > 0) {
4750 dev->isCheckpointed = 0;
4751 yaffs_CheckpointInvalidateStream(dev);
4753 if (dev->param.markSuperBlockDirty)
4754 dev->param.markSuperBlockDirty(dev);
4758 int yaffs_CheckpointSave(yaffs_Device *dev)
4761 T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
4763 yaffs_VerifyObjects(dev);
4764 yaffs_VerifyBlocks(dev);
4765 yaffs_VerifyFreeChunks(dev);
4767 if (!dev->isCheckpointed) {
4768 yaffs_InvalidateCheckpoint(dev);
4769 yaffs_WriteCheckpointData(dev);
4772 T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
4774 return dev->isCheckpointed;
4777 int yaffs_CheckpointRestore(yaffs_Device *dev)
4780 T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
4782 retval = yaffs_ReadCheckpointData(dev);
4784 if (dev->isCheckpointed) {
4785 yaffs_VerifyObjects(dev);
4786 yaffs_VerifyBlocks(dev);
4787 yaffs_VerifyFreeChunks(dev);
4790 T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
4795 /*--------------------- File read/write ------------------------
4796 * Read and write have very similar structures.
4797 * In general the read/write has three parts to it
4798 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
4799 * Some complete chunks
4800 * An incomplete chunk to end off with
4802 * Curve-balls: the first chunk might also be the last chunk.
4805 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
4814 yaffs_ChunkCache *cache;
4821 /* chunk = offset / dev->nDataBytesPerChunk + 1; */
4822 /* start = offset % dev->nDataBytesPerChunk; */
4823 yaffs_AddrToChunk(dev, offset, &chunk, &start);
4826 /* OK now check for the curveball where the start and end are in
4829 if ((start + n) < dev->nDataBytesPerChunk)
4832 nToCopy = dev->nDataBytesPerChunk - start;
4834 cache = yaffs_FindChunkCache(in, chunk);
4836 /* If the chunk is already in the cache or it is less than a whole chunk
4837 * or we're using inband tags then use the cache (if there is caching)
4838 * else bypass the cache.
4840 if (cache || nToCopy != dev->nDataBytesPerChunk || dev->param.inbandTags) {
4841 if (dev->param.nShortOpCaches > 0) {
4843 /* If we can't find the data in the cache, then load it up. */
4846 cache = yaffs_GrabChunkCache(in->myDev);
4848 cache->chunkId = chunk;
4851 yaffs_ReadChunkDataFromObject(in, chunk,
4857 yaffs_UseChunkCache(dev, cache, 0);
4862 memcpy(buffer, &cache->data[start], nToCopy);
4866 /* Read into the local buffer then copy..*/
4869 yaffs_GetTempBuffer(dev, __LINE__);
4870 yaffs_ReadChunkDataFromObject(in, chunk,
4873 memcpy(buffer, &localBuffer[start], nToCopy);
4876 yaffs_ReleaseTempBuffer(dev, localBuffer,
4882 /* A full chunk. Read directly into the supplied buffer. */
4883 yaffs_ReadChunkDataFromObject(in, chunk, buffer);
4897 int yaffs_DoWriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
4898 int nBytes, int writeThrough)
4907 int startOfWrite = offset;
4908 int chunkWritten = 0;
4916 while (n > 0 && chunkWritten >= 0) {
4917 yaffs_AddrToChunk(dev, offset, &chunk, &start);
4919 if (chunk * dev->nDataBytesPerChunk + start != offset ||
4920 start >= dev->nDataBytesPerChunk) {
4921 T(YAFFS_TRACE_ERROR, (
4922 TSTR("AddrToChunk of offset %d gives chunk %d start %d"
4924 (int)offset, chunk, start));
4926 chunk++; /* File pos to chunk in file offset */
4928 /* OK now check for the curveball where the start and end are in
4932 if ((start + n) < dev->nDataBytesPerChunk) {
4935 /* Now folks, to calculate how many bytes to write back....
4936 * If we're overwriting and not writing to then end of file then
4937 * we need to write back as much as was there before.
4940 chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
4942 if (chunkStart > in->variant.fileVariant.fileSize)
4943 nBytesRead = 0; /* Past end of file */
4945 nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
4947 if (nBytesRead > dev->nDataBytesPerChunk)
4948 nBytesRead = dev->nDataBytesPerChunk;
4952 (start + n)) ? nBytesRead : (start + n);
4954 if (nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
4958 nToCopy = dev->nDataBytesPerChunk - start;
4959 nToWriteBack = dev->nDataBytesPerChunk;
4962 if (nToCopy != dev->nDataBytesPerChunk || dev->param.inbandTags) {
4963 /* An incomplete start or end chunk (or maybe both start and end chunk),
4964 * or we're using inband tags, so we want to use the cache buffers.
4966 if (dev->param.nShortOpCaches > 0) {
4967 yaffs_ChunkCache *cache;
4968 /* If we can't find the data in the cache, then load the cache */
4969 cache = yaffs_FindChunkCache(in, chunk);
4972 && yaffs_CheckSpaceForAllocation(dev, 1)) {
4973 cache = yaffs_GrabChunkCache(dev);
4975 cache->chunkId = chunk;
4978 yaffs_ReadChunkDataFromObject(in, chunk,
4982 !yaffs_CheckSpaceForAllocation(dev, 1)) {
4983 /* Drop the cache if it was a read cache item and
4984 * no space check has been made for it.
4990 yaffs_UseChunkCache(dev, cache, 1);
4994 memcpy(&cache->data[start], buffer,
4999 cache->nBytes = nToWriteBack;
5003 yaffs_WriteChunkDataToObject
5006 cache->data, cache->nBytes,
5012 chunkWritten = -1; /* fail the write */
5015 /* An incomplete start or end chunk (or maybe both start and end chunk)
5016 * Read into the local buffer then copy, then copy over and write back.
5020 yaffs_GetTempBuffer(dev, __LINE__);
5022 yaffs_ReadChunkDataFromObject(in, chunk,
5027 memcpy(&localBuffer[start], buffer, nToCopy);
5030 yaffs_WriteChunkDataToObject(in, chunk,
5035 yaffs_ReleaseTempBuffer(dev, localBuffer,
5041 /* A full chunk. Write directly from the supplied buffer. */
5046 yaffs_WriteChunkDataToObject(in, chunk, buffer,
5047 dev->nDataBytesPerChunk,
5050 /* Since we've overwritten the cached data, we better invalidate it. */
5051 yaffs_InvalidateChunkCache(in, chunk);
5054 if (chunkWritten >= 0) {
5063 /* Update file object */
5065 if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
5066 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
5073 int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
5074 int nBytes, int writeThrough)
5076 yaffs_HandleHole(in,offset);
5077 return yaffs_DoWriteDataToFile(in,buffer,offset,nBytes,writeThrough);
5082 /* ---------------------- File resizing stuff ------------------ */
5084 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
5087 yaffs_Device *dev = in->myDev;
5088 int oldFileSize = in->variant.fileVariant.fileSize;
5090 int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
5092 int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
5093 dev->nDataBytesPerChunk;
5097 /* Delete backwards so that we don't end up with holes if
5098 * power is lost part-way through the operation.
5100 for (i = lastDel; i >= startDel; i--) {
5101 /* NB this could be optimised somewhat,
5102 * eg. could retrieve the tags and write them without
5103 * using yaffs_DeleteChunk
5106 chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
5109 (dev->internalStartBlock * dev->param.nChunksPerBlock)
5111 ((dev->internalEndBlock +
5112 1) * dev->param.nChunksPerBlock)) {
5113 T(YAFFS_TRACE_ALWAYS,
5114 (TSTR("Found daft chunkId %d for %d" TENDSTR),
5118 yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
5126 static void yaffs_ResizeDown( yaffs_Object *obj, loff_t newSize)
5129 __u32 newSizeOfPartialChunk;
5130 yaffs_Device *dev = obj->myDev;
5132 yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
5134 yaffs_PruneResizedChunks(obj, newSize);
5136 if (newSizeOfPartialChunk != 0) {
5137 int lastChunk = 1 + newFullChunks;
5138 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
5140 /* Got to read and rewrite the last chunk with its new size and zero pad */
5141 yaffs_ReadChunkDataFromObject(obj, lastChunk, localBuffer);
5142 memset(localBuffer + newSizeOfPartialChunk, 0,
5143 dev->nDataBytesPerChunk - newSizeOfPartialChunk);
5145 yaffs_WriteChunkDataToObject(obj, lastChunk, localBuffer,
5146 newSizeOfPartialChunk, 1);
5148 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
5151 obj->variant.fileVariant.fileSize = newSize;
5153 yaffs_PruneFileStructure(dev, &obj->variant.fileVariant);
5157 static int yaffs_HandleHole(yaffs_Object *obj, loff_t newSize)
5159 /* if newsSize > oldFileSize.
5160 * We're going to be writing a hole.
5161 * If the hole is small then write zeros otherwise write a start of hole marker.
5168 int result = YAFFS_OK;
5169 yaffs_Device *dev = NULL;
5171 __u8 *localBuffer = NULL;
5173 int smallIncreaseOk = 0;
5178 if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
5183 /* Bail out if not yaffs2 mode */
5184 if(!dev->param.isYaffs2)
5187 oldFileSize = obj->variant.fileVariant.fileSize;
5189 if (newSize <= oldFileSize)
5192 increase = newSize - oldFileSize;
5194 if(increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->nDataBytesPerChunk &&
5195 yaffs_CheckSpaceForAllocation(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
5201 localBuffer= yaffs_GetTempBuffer(dev, __LINE__);
5204 /* fill hole with zero bytes */
5205 int pos = oldFileSize;
5208 memset(localBuffer,0,dev->nDataBytesPerChunk);
5209 smallIncreaseOk = 1;
5211 while(increase > 0 && smallIncreaseOk){
5212 thisWrite = increase;
5213 if(thisWrite > dev->nDataBytesPerChunk)
5214 thisWrite = dev->nDataBytesPerChunk;
5215 written = yaffs_DoWriteDataToFile(obj,localBuffer,pos,thisWrite,0);
5216 if(written == thisWrite){
5218 increase -= thisWrite;
5220 smallIncreaseOk = 0;
5223 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
5225 /* If we were out of space then reverse any chunks we've added */
5226 if(!smallIncreaseOk)
5227 yaffs_ResizeDown(obj, oldFileSize);
5230 if (!smallIncreaseOk &&
5232 obj->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
5233 obj->parent->objectId != YAFFS_OBJECTID_DELETED){
5234 /* Write a hole start header with the old file size */
5235 yaffs_UpdateObjectHeader(obj, NULL, 0, 1, 0, NULL);
5242 int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
5244 yaffs_Device *dev = in->myDev;
5245 int oldFileSize = in->variant.fileVariant.fileSize;
5247 yaffs_FlushFilesChunkCache(in);
5248 yaffs_InvalidateWholeChunkCache(in);
5250 yaffs_CheckGarbageCollection(dev,0);
5252 if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
5255 if (newSize == oldFileSize)
5258 if(newSize > oldFileSize){
5259 yaffs_HandleHole(in,newSize);
5260 in->variant.fileVariant.fileSize = newSize;
5262 /* newSize < oldFileSize */
5263 yaffs_ResizeDown(in, newSize);
5266 /* Write a new object header to reflect the resize.
5267 * show we've shrunk the file, if need be
5268 * Do this only if the file is not in the deleted directories
5269 * and is not shadowed.
5273 in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
5274 in->parent->objectId != YAFFS_OBJECTID_DELETED)
5275 yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0, NULL);
5281 loff_t yaffs_GetFileSize(yaffs_Object *obj)
5283 YCHAR *alias = NULL;
5284 obj = yaffs_GetEquivalentObject(obj);
5286 switch (obj->variantType) {
5287 case YAFFS_OBJECT_TYPE_FILE:
5288 return obj->variant.fileVariant.fileSize;
5289 case YAFFS_OBJECT_TYPE_SYMLINK:
5290 alias = obj->variant.symLinkVariant.alias;
5293 return yaffs_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH);
5301 int yaffs_FlushFile(yaffs_Object *in, int updateTime, int dataSync)
5305 yaffs_FlushFilesChunkCache(in);
5306 if(dataSync) /* Only sync data */
5310 #ifdef CONFIG_YAFFS_WINCE
5311 yfsd_WinFileTimeNow(in->win_mtime);
5314 in->yst_mtime = Y_CURRENT_TIME;
5319 retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0, NULL) >=
5320 0) ? YAFFS_OK : YAFFS_FAIL;
5330 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
5333 /* First off, invalidate the file's data in the cache, without flushing. */
5334 yaffs_InvalidateWholeChunkCache(in);
5336 if (in->myDev->param.isYaffs2 && (in->parent != in->myDev->deletedDir)) {
5337 /* Move to the unlinked directory so we have a record that it was deleted. */
5338 yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0);
5342 yaffs_RemoveObjectFromDirectory(in);
5343 yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
5346 yaffs_FreeObject(in);
5351 /* yaffs_DeleteFile deletes the whole file data
5352 * and the inode associated with the file.
5353 * It does not delete the links associated with the file.
5355 static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
5359 int immediateDeletion = 0;
5360 yaffs_Device *dev = in->myDev;
5363 immediateDeletion = 1;
5365 if (immediateDeletion) {
5367 yaffs_ChangeObjectName(in, in->myDev->deletedDir,
5368 _Y("deleted"), 0, 0);
5369 T(YAFFS_TRACE_TRACING,
5370 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
5373 in->myDev->nDeletedFiles++;
5374 if (dev->param.disableSoftDelete || dev->param.isYaffs2)
5375 yaffs_ResizeFile(in, 0);
5376 yaffs_SoftDeleteFile(in);
5379 yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
5380 _Y("unlinked"), 0, 0);
5387 int yaffs_DeleteFile(yaffs_Object *in)
5389 int retVal = YAFFS_OK;
5390 int deleted; /* Need to cache value on stack if in is freed */
5391 yaffs_Device *dev = in->myDev;
5393 if (dev->param.disableSoftDelete || dev->param.isYaffs2)
5394 yaffs_ResizeFile(in, 0);
5396 if (in->nDataChunks > 0) {
5397 /* Use soft deletion if there is data in the file.
5398 * That won't be the case if it has been resized to zero.
5401 retVal = yaffs_UnlinkFileIfNeeded(in);
5403 deleted = in->deleted;
5405 if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
5408 in->myDev->nDeletedFiles++;
5409 yaffs_SoftDeleteFile(in);
5411 return deleted ? YAFFS_OK : YAFFS_FAIL;
5413 /* The file has no data chunks so we toss it immediately */
5414 yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
5415 in->variant.fileVariant.top = NULL;
5416 yaffs_DoGenericObjectDeletion(in);
5422 static int yaffs_IsNonEmptyDirectory(yaffs_Object *obj)
5424 return (obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) &&
5425 !(ylist_empty(&obj->variant.directoryVariant.children));
5428 static int yaffs_DeleteDirectory(yaffs_Object *obj)
5430 /* First check that the directory is empty. */
5431 if (yaffs_IsNonEmptyDirectory(obj))
5434 return yaffs_DoGenericObjectDeletion(obj);
5437 static int yaffs_DeleteSymLink(yaffs_Object *in)
5439 if(in->variant.symLinkVariant.alias)
5440 YFREE(in->variant.symLinkVariant.alias);
5441 in->variant.symLinkVariant.alias=NULL;
5443 return yaffs_DoGenericObjectDeletion(in);
5446 static int yaffs_DeleteHardLink(yaffs_Object *in)
5448 /* remove this hardlink from the list assocaited with the equivalent
5451 ylist_del_init(&in->hardLinks);
5452 return yaffs_DoGenericObjectDeletion(in);
5455 int yaffs_DeleteObject(yaffs_Object *obj)
5458 switch (obj->variantType) {
5459 case YAFFS_OBJECT_TYPE_FILE:
5460 retVal = yaffs_DeleteFile(obj);
5462 case YAFFS_OBJECT_TYPE_DIRECTORY:
5463 if(!ylist_empty(&obj->variant.directoryVariant.dirty)){
5464 T(YAFFS_TRACE_BACKGROUND, (TSTR("Remove object %d from dirty directories" TENDSTR),obj->objectId));
5465 ylist_del_init(&obj->variant.directoryVariant.dirty);
5467 return yaffs_DeleteDirectory(obj);
5469 case YAFFS_OBJECT_TYPE_SYMLINK:
5470 retVal = yaffs_DeleteSymLink(obj);
5472 case YAFFS_OBJECT_TYPE_HARDLINK:
5473 retVal = yaffs_DeleteHardLink(obj);
5475 case YAFFS_OBJECT_TYPE_SPECIAL:
5476 retVal = yaffs_DoGenericObjectDeletion(obj);
5478 case YAFFS_OBJECT_TYPE_UNKNOWN:
5480 break; /* should not happen. */
5486 static int yaffs_UnlinkWorker(yaffs_Object *obj)
5489 int immediateDeletion = 0;
5492 immediateDeletion = 1;
5495 yaffs_UpdateParent(obj->parent);
5497 if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
5498 return yaffs_DeleteHardLink(obj);
5499 } else if (!ylist_empty(&obj->hardLinks)) {
5500 /* Curve ball: We're unlinking an object that has a hardlink.
5502 * This problem arises because we are not strictly following
5503 * The Linux link/inode model.
5505 * We can't really delete the object.
5506 * Instead, we do the following:
5507 * - Select a hardlink.
5508 * - Unhook it from the hard links
5509 * - Move it from its parent directory (so that the rename can work)
5510 * - Rename the object to the hardlink's name.
5511 * - Delete the hardlink
5515 yaffs_Object *parent;
5517 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
5519 hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
5521 yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
5522 parent = hl->parent;
5524 ylist_del_init(&hl->hardLinks);
5526 yaffs_AddObjectToDirectory(obj->myDev->unlinkedDir, hl);
5528 retVal = yaffs_ChangeObjectName(obj,parent, name, 0, 0);
5530 if (retVal == YAFFS_OK)
5531 retVal = yaffs_DoGenericObjectDeletion(hl);
5535 } else if (immediateDeletion) {
5536 switch (obj->variantType) {
5537 case YAFFS_OBJECT_TYPE_FILE:
5538 return yaffs_DeleteFile(obj);
5540 case YAFFS_OBJECT_TYPE_DIRECTORY:
5541 ylist_del_init(&obj->variant.directoryVariant.dirty);
5542 return yaffs_DeleteDirectory(obj);
5544 case YAFFS_OBJECT_TYPE_SYMLINK:
5545 return yaffs_DeleteSymLink(obj);
5547 case YAFFS_OBJECT_TYPE_SPECIAL:
5548 return yaffs_DoGenericObjectDeletion(obj);
5550 case YAFFS_OBJECT_TYPE_HARDLINK:
5551 case YAFFS_OBJECT_TYPE_UNKNOWN:
5555 } else if(yaffs_IsNonEmptyDirectory(obj))
5558 return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
5559 _Y("unlinked"), 0, 0);
5563 static int yaffs_UnlinkObject(yaffs_Object *obj)
5566 if (obj && obj->unlinkAllowed)
5567 return yaffs_UnlinkWorker(obj);
5572 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
5576 obj = yaffs_FindObjectByName(dir, name);
5577 return yaffs_UnlinkObject(obj);
5580 /*----------------------- Initialisation Scanning ---------------------- */
5582 static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
5583 int backwardScanning)
5587 if (!backwardScanning) {
5588 /* Handle YAFFS1 forward scanning case
5589 * For YAFFS1 we always do the deletion
5593 /* Handle YAFFS2 case (backward scanning)
5594 * If the shadowed object exists then ignore.
5596 obj = yaffs_FindObjectByNumber(dev, objId);
5601 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
5602 * We put it in unlinked dir to be cleaned up after the scanning
5605 yaffs_FindOrCreateObjectByNumber(dev, objId,
5606 YAFFS_OBJECT_TYPE_FILE);
5609 obj->isShadowed = 1;
5610 yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
5611 obj->variant.fileVariant.shrinkSize = 0;
5612 obj->valid = 1; /* So that we don't read any other info for this file */
5622 static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
5629 hardList = (yaffs_Object *) (hardList->hardLinks.next);
5631 in = yaffs_FindObjectByNumber(dev,
5632 hl->variant.hardLinkVariant.
5633 equivalentObjectId);
5636 /* Add the hardlink pointers */
5637 hl->variant.hardLinkVariant.equivalentObject = in;
5638 ylist_add(&hl->hardLinks, &in->hardLinks);
5640 /* Todo Need to report/handle this better.
5641 * Got a problem... hardlink to a non-existant object
5643 hl->variant.hardLinkVariant.equivalentObject = NULL;
5644 YINIT_LIST_HEAD(&hl->hardLinks);
5654 static int ybicmp(const void *a, const void *b)
5656 register int aseq = ((yaffs_BlockIndex *)a)->seq;
5657 register int bseq = ((yaffs_BlockIndex *)b)->seq;
5658 register int ablock = ((yaffs_BlockIndex *)a)->block;
5659 register int bblock = ((yaffs_BlockIndex *)b)->block;
5661 return ablock - bblock;
5667 struct yaffs_ShadowFixerStruct {
5670 struct yaffs_ShadowFixerStruct *next;
5674 static void yaffs_StripDeletedObjects(yaffs_Device *dev)
5677 * Sort out state of unlinked and deleted objects after scanning.
5679 struct ylist_head *i;
5680 struct ylist_head *n;
5683 /* Soft delete all the unlinked files */
5684 ylist_for_each_safe(i, n,
5685 &dev->unlinkedDir->variant.directoryVariant.children) {
5687 l = ylist_entry(i, yaffs_Object, siblings);
5688 yaffs_DeleteObject(l);
5692 ylist_for_each_safe(i, n,
5693 &dev->deletedDir->variant.directoryVariant.children) {
5695 l = ylist_entry(i, yaffs_Object, siblings);
5696 yaffs_DeleteObject(l);
5703 * This code iterates through all the objects making sure that they are rooted.
5704 * Any unrooted objects are re-rooted in lost+found.
5705 * An object needs to be in one of:
5706 * - Directly under deleted, unlinked
5707 * - Directly or indirectly under root.
5710 * This code assumes that we don't ever change the current relationships between
5712 * rootDir->parent == unlinkedDir->parent == deletedDir->parent == NULL
5713 * lostNfound->parent == rootDir
5715 * This fixes the problem where directories might have inadvertently been deleted
5716 * leaving the object "hanging" without being rooted in the directory tree.
5719 static int yaffs_HasNULLParent(yaffs_Device *dev, yaffs_Object *obj)
5721 return (obj == dev->deletedDir ||
5722 obj == dev->unlinkedDir||
5723 obj == dev->rootDir);
5726 static void yaffs_FixHangingObjects(yaffs_Device *dev)
5729 yaffs_Object *parent;
5731 struct ylist_head *lh;
5732 struct ylist_head *n;
5737 /* Iterate through the objects in each hash entry,
5738 * looking at each object.
5739 * Make sure it is rooted.
5742 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5743 ylist_for_each_safe(lh, n, &dev->objectBucket[i].list) {
5745 obj = ylist_entry(lh, yaffs_Object, hashLink);
5746 parent= obj->parent;
5748 if(yaffs_HasNULLParent(dev,obj)){
5749 /* These directories are not hanging */
5752 else if(!parent || parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5754 else if(yaffs_HasNULLParent(dev,parent))
5758 * Need to follow the parent chain to see if it is hanging.
5763 while(parent != dev->rootDir &&
5765 parent->parent->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
5767 parent = parent->parent;
5770 if(parent != dev->rootDir)
5775 (TSTR("Hanging object %d moved to lost and found" TENDSTR),
5777 yaffs_AddObjectToDirectory(dev->lostNFoundDir,obj);
5786 * Delete directory contents for cleaning up lost and found.
5788 static void yaffs_DeleteDirectoryContents(yaffs_Object *dir)
5791 struct ylist_head *lh;
5792 struct ylist_head *n;
5794 if(dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5797 ylist_for_each_safe(lh, n, &dir->variant.directoryVariant.children) {
5799 obj = ylist_entry(lh, yaffs_Object, siblings);
5800 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
5801 yaffs_DeleteDirectoryContents(obj);
5804 (TSTR("Deleting lost_found object %d" TENDSTR),
5807 /* Need to use UnlinkObject since Delete would not handle
5808 * hardlinked objects correctly.
5810 yaffs_UnlinkObject(obj);
5816 static void yaffs_EmptyLostAndFound(yaffs_Device *dev)
5818 yaffs_DeleteDirectoryContents(dev->lostNFoundDir);
5821 static int yaffs_Scan(yaffs_Device *dev)
5823 yaffs_ExtendedTags tags;
5833 yaffs_BlockState state;
5834 yaffs_Object *hardList = NULL;
5835 yaffs_BlockInfo *bi;
5836 __u32 sequenceNumber;
5837 yaffs_ObjectHeader *oh;
5839 yaffs_Object *parent;
5841 int alloc_failed = 0;
5843 struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
5851 (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
5852 dev->internalStartBlock, dev->internalEndBlock));
5854 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
5856 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
5858 /* Scan all the blocks to determine their state */
5859 bi = dev->blockInfo;
5860 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
5861 yaffs_ClearChunkBits(dev, blk);
5863 bi->softDeletions = 0;
5865 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
5867 bi->blockState = state;
5868 bi->sequenceNumber = sequenceNumber;
5870 if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
5871 bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
5873 T(YAFFS_TRACE_SCAN_DEBUG,
5874 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
5875 state, sequenceNumber));
5877 if (state == YAFFS_BLOCK_STATE_DEAD) {
5878 T(YAFFS_TRACE_BAD_BLOCKS,
5879 (TSTR("block %d is bad" TENDSTR), blk));
5880 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
5881 T(YAFFS_TRACE_SCAN_DEBUG,
5882 (TSTR("Block empty " TENDSTR)));
5883 dev->nErasedBlocks++;
5884 dev->nFreeChunks += dev->param.nChunksPerBlock;
5889 startIterator = dev->internalStartBlock;
5890 endIterator = dev->internalEndBlock;
5892 /* For each block.... */
5893 for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
5900 blk = blockIterator;
5902 bi = yaffs_GetBlockInfo(dev, blk);
5903 state = bi->blockState;
5907 /* For each chunk in each block that needs scanning....*/
5908 for (c = 0; !alloc_failed && c < dev->param.nChunksPerBlock &&
5909 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
5910 /* Read the tags and decide what to do */
5911 chunk = blk * dev->param.nChunksPerBlock + c;
5913 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
5916 /* Let's have a good look at this chunk... */
5918 if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
5924 /*T((" %d %d deleted\n",blk,c)); */
5925 } else if (!tags.chunkUsed) {
5926 /* An unassigned chunk in the block
5927 * This means that either the block is empty or
5928 * this is the one being allocated from
5932 /* We're looking at the first chunk in the block so the block is unused */
5933 state = YAFFS_BLOCK_STATE_EMPTY;
5934 dev->nErasedBlocks++;
5936 /* this is the block being allocated from */
5939 (" Allocating from %d %d" TENDSTR),
5941 state = YAFFS_BLOCK_STATE_ALLOCATING;
5942 dev->allocationBlock = blk;
5943 dev->allocationPage = c;
5944 dev->allocationBlockFinder = blk;
5945 /* Set block finder here to encourage the allocator to go forth from here. */
5949 dev->nFreeChunks += (dev->param.nChunksPerBlock - c);
5950 } else if (tags.chunkId > 0) {
5951 /* chunkId > 0 so it is a data chunk... */
5952 unsigned int endpos;
5954 yaffs_SetChunkBit(dev, blk, c);
5957 in = yaffs_FindOrCreateObjectByNumber(dev,
5960 YAFFS_OBJECT_TYPE_FILE);
5961 /* PutChunkIntoFile checks for a clash (two data chunks with
5962 * the same chunkId).
5969 if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
5974 (tags.chunkId - 1) * dev->nDataBytesPerChunk +
5977 in->variantType == YAFFS_OBJECT_TYPE_FILE
5978 && in->variant.fileVariant.scannedFileSize <
5980 in->variant.fileVariant.
5981 scannedFileSize = endpos;
5982 if (!dev->param.useHeaderFileSize) {
5983 in->variant.fileVariant.
5985 in->variant.fileVariant.
5990 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
5992 /* chunkId == 0, so it is an ObjectHeader.
5993 * Thus, we read in the object header and make the object
5995 yaffs_SetChunkBit(dev, blk, c);
5998 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
6002 oh = (yaffs_ObjectHeader *) chunkData;
6004 in = yaffs_FindObjectByNumber(dev,
6006 if (in && in->variantType != oh->type) {
6007 /* This should not happen, but somehow
6008 * Wev'e ended up with an objectId that has been reused but not yet
6009 * deleted, and worse still it has changed type. Delete the old object.
6012 yaffs_DeleteObject(in);
6017 in = yaffs_FindOrCreateObjectByNumber(dev,
6025 if (in && oh->shadowsObject > 0) {
6027 struct yaffs_ShadowFixerStruct *fixer;
6028 fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
6030 fixer->next = shadowFixerList;
6031 shadowFixerList = fixer;
6032 fixer->objectId = tags.objectId;
6033 fixer->shadowedId = oh->shadowsObject;
6038 if (in && in->valid) {
6039 /* We have already filled this one. We have a duplicate and need to resolve it. */
6041 unsigned existingSerial = in->serial;
6042 unsigned newSerial = tags.serialNumber;
6044 if (((existingSerial + 1) & 3) == newSerial) {
6045 /* Use new one - destroy the exisiting one */
6046 yaffs_DeleteChunk(dev,
6051 /* Use existing - destroy this one. */
6052 yaffs_DeleteChunk(dev, chunk, 1,
6057 if (in && !in->valid &&
6058 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6059 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
6060 /* We only load some info, don't fiddle with directory structure */
6062 in->variantType = oh->type;
6064 in->yst_mode = oh->yst_mode;
6065 #ifdef CONFIG_YAFFS_WINCE
6066 in->win_atime[0] = oh->win_atime[0];
6067 in->win_ctime[0] = oh->win_ctime[0];
6068 in->win_mtime[0] = oh->win_mtime[0];
6069 in->win_atime[1] = oh->win_atime[1];
6070 in->win_ctime[1] = oh->win_ctime[1];
6071 in->win_mtime[1] = oh->win_mtime[1];
6073 in->yst_uid = oh->yst_uid;
6074 in->yst_gid = oh->yst_gid;
6075 in->yst_atime = oh->yst_atime;
6076 in->yst_mtime = oh->yst_mtime;
6077 in->yst_ctime = oh->yst_ctime;
6078 in->yst_rdev = oh->yst_rdev;
6080 in->hdrChunk = chunk;
6081 in->serial = tags.serialNumber;
6083 } else if (in && !in->valid) {
6084 /* we need to load this info */
6087 in->variantType = oh->type;
6089 in->yst_mode = oh->yst_mode;
6090 #ifdef CONFIG_YAFFS_WINCE
6091 in->win_atime[0] = oh->win_atime[0];
6092 in->win_ctime[0] = oh->win_ctime[0];
6093 in->win_mtime[0] = oh->win_mtime[0];
6094 in->win_atime[1] = oh->win_atime[1];
6095 in->win_ctime[1] = oh->win_ctime[1];
6096 in->win_mtime[1] = oh->win_mtime[1];
6098 in->yst_uid = oh->yst_uid;
6099 in->yst_gid = oh->yst_gid;
6100 in->yst_atime = oh->yst_atime;
6101 in->yst_mtime = oh->yst_mtime;
6102 in->yst_ctime = oh->yst_ctime;
6103 in->yst_rdev = oh->yst_rdev;
6105 in->hdrChunk = chunk;
6106 in->serial = tags.serialNumber;
6108 yaffs_SetObjectName(in, oh->name);
6111 /* directory stuff...
6116 yaffs_FindOrCreateObjectByNumber
6117 (dev, oh->parentObjectId,
6118 YAFFS_OBJECT_TYPE_DIRECTORY);
6121 if (parent && parent->variantType ==
6122 YAFFS_OBJECT_TYPE_UNKNOWN) {
6123 /* Set up as a directory */
6124 parent->variantType =
6125 YAFFS_OBJECT_TYPE_DIRECTORY;
6126 YINIT_LIST_HEAD(&parent->variant.
6129 } else if (!parent || parent->variantType !=
6130 YAFFS_OBJECT_TYPE_DIRECTORY) {
6131 /* Hoosterman, another problem....
6132 * We're trying to use a non-directory as a directory
6135 T(YAFFS_TRACE_ERROR,
6137 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
6139 parent = dev->lostNFoundDir;
6142 yaffs_AddObjectToDirectory(parent, in);
6144 if (0 && (parent == dev->deletedDir ||
6145 parent == dev->unlinkedDir)) {
6146 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
6147 dev->nDeletedFiles++;
6149 /* Note re hardlinks.
6150 * Since we might scan a hardlink before its equivalent object is scanned
6151 * we put them all in a list.
6152 * After scanning is complete, we should have all the objects, so we run through this
6153 * list and fix up all the chains.
6156 switch (in->variantType) {
6157 case YAFFS_OBJECT_TYPE_UNKNOWN:
6158 /* Todo got a problem */
6160 case YAFFS_OBJECT_TYPE_FILE:
6161 if (dev->param.useHeaderFileSize)
6163 in->variant.fileVariant.
6168 case YAFFS_OBJECT_TYPE_HARDLINK:
6169 in->variant.hardLinkVariant.
6170 equivalentObjectId =
6171 oh->equivalentObjectId;
6172 in->hardLinks.next =
6173 (struct ylist_head *)
6177 case YAFFS_OBJECT_TYPE_DIRECTORY:
6180 case YAFFS_OBJECT_TYPE_SPECIAL:
6183 case YAFFS_OBJECT_TYPE_SYMLINK:
6184 in->variant.symLinkVariant.alias =
6185 yaffs_CloneString(oh->alias);
6186 if (!in->variant.symLinkVariant.alias)
6195 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6196 /* If we got this far while scanning, then the block is fully allocated.*/
6197 state = YAFFS_BLOCK_STATE_FULL;
6200 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
6201 /* If the block was partially allocated then treat it as fully allocated.*/
6202 state = YAFFS_BLOCK_STATE_FULL;
6203 dev->allocationBlock = -1;
6206 bi->blockState = state;
6208 /* Now let's see if it was dirty */
6209 if (bi->pagesInUse == 0 &&
6210 !bi->hasShrinkHeader &&
6211 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
6212 yaffs_BlockBecameDirty(dev, blk);
6218 /* Ok, we've done all the scanning.
6219 * Fix up the hard link chains.
6220 * We should now have scanned all the objects, now it's time to add these
6224 yaffs_HardlinkFixup(dev, hardList);
6226 /* Fix up any shadowed objects */
6228 struct yaffs_ShadowFixerStruct *fixer;
6231 while (shadowFixerList) {
6232 fixer = shadowFixerList;
6233 shadowFixerList = fixer->next;
6234 /* Complete the rename transaction by deleting the shadowed object
6235 * then setting the object header to unshadowed.
6237 obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
6239 yaffs_DeleteObject(obj);
6241 obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
6244 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0, NULL);
6250 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
6255 T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
6261 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
6264 yaffs_ObjectHeader *oh;
6266 yaffs_ExtendedTags tags;
6268 int alloc_failed = 0;
6276 T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
6278 in->lazyLoaded ? "not yet" : "already"));
6281 if (in->lazyLoaded && in->hdrChunk > 0) {
6283 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
6285 result = yaffs_ReadChunkWithTagsFromNAND(dev, in->hdrChunk, chunkData, &tags);
6286 oh = (yaffs_ObjectHeader *) chunkData;
6288 in->yst_mode = oh->yst_mode;
6289 #ifdef CONFIG_YAFFS_WINCE
6290 in->win_atime[0] = oh->win_atime[0];
6291 in->win_ctime[0] = oh->win_ctime[0];
6292 in->win_mtime[0] = oh->win_mtime[0];
6293 in->win_atime[1] = oh->win_atime[1];
6294 in->win_ctime[1] = oh->win_ctime[1];
6295 in->win_mtime[1] = oh->win_mtime[1];
6297 in->yst_uid = oh->yst_uid;
6298 in->yst_gid = oh->yst_gid;
6299 in->yst_atime = oh->yst_atime;
6300 in->yst_mtime = oh->yst_mtime;
6301 in->yst_ctime = oh->yst_ctime;
6302 in->yst_rdev = oh->yst_rdev;
6305 yaffs_SetObjectName(in, oh->name);
6307 if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
6308 in->variant.symLinkVariant.alias =
6309 yaffs_CloneString(oh->alias);
6310 if (!in->variant.symLinkVariant.alias)
6311 alloc_failed = 1; /* Not returned to caller */
6314 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
6318 static int yaffs_ScanBackwards(yaffs_Device *dev)
6320 yaffs_ExtendedTags tags;
6325 int nBlocksToScan = 0;
6331 yaffs_BlockState state;
6332 yaffs_Object *hardList = NULL;
6333 yaffs_BlockInfo *bi;
6334 __u32 sequenceNumber;
6335 yaffs_ObjectHeader *oh;
6337 yaffs_Object *parent;
6338 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6344 int foundChunksInBlock;
6345 int equivalentObjectId;
6346 int alloc_failed = 0;
6349 yaffs_BlockIndex *blockIndex = NULL;
6350 int altBlockIndex = 0;
6352 if (!dev->param.isYaffs2) {
6354 (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
6360 ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
6361 TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
6364 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
6366 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
6369 blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
6375 (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
6379 dev->blocksInCheckpoint = 0;
6381 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
6383 /* Scan all the blocks to determine their state */
6384 bi = dev->blockInfo;
6385 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
6386 yaffs_ClearChunkBits(dev, blk);
6388 bi->softDeletions = 0;
6390 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
6392 bi->blockState = state;
6393 bi->sequenceNumber = sequenceNumber;
6395 if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
6396 bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
6397 if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
6398 bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
6400 T(YAFFS_TRACE_SCAN_DEBUG,
6401 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
6402 state, sequenceNumber));
6405 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
6406 dev->blocksInCheckpoint++;
6408 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
6409 T(YAFFS_TRACE_BAD_BLOCKS,
6410 (TSTR("block %d is bad" TENDSTR), blk));
6411 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
6412 T(YAFFS_TRACE_SCAN_DEBUG,
6413 (TSTR("Block empty " TENDSTR)));
6414 dev->nErasedBlocks++;
6415 dev->nFreeChunks += dev->param.nChunksPerBlock;
6416 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6418 /* Determine the highest sequence number */
6419 if (sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
6420 sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
6422 blockIndex[nBlocksToScan].seq = sequenceNumber;
6423 blockIndex[nBlocksToScan].block = blk;
6427 if (sequenceNumber >= dev->sequenceNumber)
6428 dev->sequenceNumber = sequenceNumber;
6430 /* TODO: Nasty sequence number! */
6433 ("Block scanning block %d has bad sequence number %d"
6434 TENDSTR), blk, sequenceNumber));
6442 (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
6448 /* Sort the blocks */
6449 #ifndef CONFIG_YAFFS_USE_OWN_SORT
6451 /* Use qsort now. */
6452 yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
6456 /* Dungy old bubble sort... */
6458 yaffs_BlockIndex temp;
6462 for (i = 0; i < nBlocksToScan; i++)
6463 for (j = i + 1; j < nBlocksToScan; j++)
6464 if (blockIndex[i].seq > blockIndex[j].seq) {
6465 temp = blockIndex[j];
6466 blockIndex[j] = blockIndex[i];
6467 blockIndex[i] = temp;
6474 T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
6476 /* Now scan the blocks looking at the data. */
6478 endIterator = nBlocksToScan - 1;
6479 T(YAFFS_TRACE_SCAN_DEBUG,
6480 (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
6482 /* For each block.... backwards */
6483 for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
6485 /* Cooperative multitasking! This loop can run for so
6486 long that watchdog timers expire. */
6489 /* get the block to scan in the correct order */
6490 blk = blockIndex[blockIterator].block;
6492 bi = yaffs_GetBlockInfo(dev, blk);
6495 state = bi->blockState;
6499 /* For each chunk in each block that needs scanning.... */
6500 foundChunksInBlock = 0;
6501 for (c = dev->param.nChunksPerBlock - 1;
6502 !alloc_failed && c >= 0 &&
6503 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6504 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
6505 /* Scan backwards...
6506 * Read the tags and decide what to do
6509 chunk = blk * dev->param.nChunksPerBlock + c;
6511 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
6514 /* Let's have a good look at this chunk... */
6516 if (!tags.chunkUsed) {
6517 /* An unassigned chunk in the block.
6518 * If there are used chunks after this one, then
6519 * it is a chunk that was skipped due to failing the erased
6520 * check. Just skip it so that it can be deleted.
6521 * But, more typically, We get here when this is an unallocated
6522 * chunk and his means that either the block is empty or
6523 * this is the one being allocated from
6526 if (foundChunksInBlock) {
6527 /* This is a chunk that was skipped due to failing the erased check */
6528 } else if (c == 0) {
6529 /* We're looking at the first chunk in the block so the block is unused */
6530 state = YAFFS_BLOCK_STATE_EMPTY;
6531 dev->nErasedBlocks++;
6533 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6534 state == YAFFS_BLOCK_STATE_ALLOCATING) {
6535 if (dev->sequenceNumber == bi->sequenceNumber) {
6536 /* this is the block being allocated from */
6540 (" Allocating from %d %d"
6543 state = YAFFS_BLOCK_STATE_ALLOCATING;
6544 dev->allocationBlock = blk;
6545 dev->allocationPage = c;
6546 dev->allocationBlockFinder = blk;
6548 /* This is a partially written block that is not
6549 * the current allocation block.
6553 (TSTR("Partially written block %d detected" TENDSTR),
6561 } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED) {
6563 (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
6568 } else if (tags.chunkId > 0) {
6569 /* chunkId > 0 so it is a data chunk... */
6570 unsigned int endpos;
6572 (tags.chunkId - 1) * dev->nDataBytesPerChunk;
6574 foundChunksInBlock = 1;
6577 yaffs_SetChunkBit(dev, blk, c);
6580 in = yaffs_FindOrCreateObjectByNumber(dev,
6583 YAFFS_OBJECT_TYPE_FILE);
6590 in->variantType == YAFFS_OBJECT_TYPE_FILE
6591 && chunkBase < in->variant.fileVariant.shrinkSize) {
6592 /* This has not been invalidated by a resize */
6593 if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, -1)) {
6597 /* File size is calculated by looking at the data chunks if we have not
6598 * seen an object header yet. Stop this practice once we find an object header.
6600 endpos = chunkBase + tags.byteCount;
6602 if (!in->valid && /* have not got an object header yet */
6603 in->variant.fileVariant.scannedFileSize < endpos) {
6604 in->variant.fileVariant.scannedFileSize = endpos;
6605 in->variant.fileVariant.fileSize = endpos;
6609 /* This chunk has been invalidated by a resize, or a past file deletion
6610 * so delete the chunk*/
6611 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
6615 /* chunkId == 0, so it is an ObjectHeader.
6616 * Thus, we read in the object header and make the object
6618 foundChunksInBlock = 1;
6620 yaffs_SetChunkBit(dev, blk, c);
6626 if (tags.extraHeaderInfoAvailable) {
6627 in = yaffs_FindOrCreateObjectByNumber(dev,
6629 tags.extraObjectType);
6635 (!in->valid && dev->param.disableLazyLoad) ||
6636 tags.extraShadows ||
6638 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6639 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))) {
6641 /* If we don't have valid info then we need to read the chunk
6642 * TODO In future we can probably defer reading the chunk and
6643 * living with invalid data until needed.
6646 result = yaffs_ReadChunkWithTagsFromNAND(dev,
6651 oh = (yaffs_ObjectHeader *) chunkData;
6653 if (dev->param.inbandTags) {
6654 /* Fix up the header if they got corrupted by inband tags */
6655 oh->shadowsObject = oh->inbandShadowsObject;
6656 oh->isShrink = oh->inbandIsShrink;
6660 in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
6668 /* TODO Hoosterman we have a problem! */
6669 T(YAFFS_TRACE_ERROR,
6671 ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
6672 TENDSTR), tags.objectId, chunk));
6677 /* We have already filled this one.
6678 * We have a duplicate that will be discarded, but
6679 * we first have to suck out resize info if it is a file.
6682 if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
6684 oh->type == YAFFS_OBJECT_TYPE_FILE) ||
6685 (tags.extraHeaderInfoAvailable &&
6686 tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))) {
6688 (oh) ? oh->fileSize : tags.
6690 __u32 parentObjectId =
6692 parentObjectId : tags.
6693 extraParentObjectId;
6697 (oh) ? oh->isShrink : tags.
6698 extraIsShrinkHeader;
6700 /* If it is deleted (unlinked at start also means deleted)
6701 * we treat the file size as being zeroed at this point.
6703 if (parentObjectId ==
6704 YAFFS_OBJECTID_DELETED
6705 || parentObjectId ==
6706 YAFFS_OBJECTID_UNLINKED) {
6711 if (isShrink && in->variant.fileVariant.shrinkSize > thisSize)
6712 in->variant.fileVariant.shrinkSize = thisSize;
6715 bi->hasShrinkHeader = 1;
6718 /* Use existing - destroy this one. */
6719 yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
6723 if (!in->valid && in->variantType !=
6724 (oh ? oh->type : tags.extraObjectType))
6725 T(YAFFS_TRACE_ERROR, (
6726 TSTR("yaffs tragedy: Bad object type, "
6727 TCONT("%d != %d, for object %d at chunk ")
6728 TCONT("%d during scan")
6730 oh->type : tags.extraObjectType,
6731 in->variantType, tags.objectId,
6735 (tags.objectId == YAFFS_OBJECTID_ROOT ||
6737 YAFFS_OBJECTID_LOSTNFOUND)) {
6738 /* We only load some info, don't fiddle with directory structure */
6742 in->variantType = oh->type;
6744 in->yst_mode = oh->yst_mode;
6745 #ifdef CONFIG_YAFFS_WINCE
6746 in->win_atime[0] = oh->win_atime[0];
6747 in->win_ctime[0] = oh->win_ctime[0];
6748 in->win_mtime[0] = oh->win_mtime[0];
6749 in->win_atime[1] = oh->win_atime[1];
6750 in->win_ctime[1] = oh->win_ctime[1];
6751 in->win_mtime[1] = oh->win_mtime[1];
6753 in->yst_uid = oh->yst_uid;
6754 in->yst_gid = oh->yst_gid;
6755 in->yst_atime = oh->yst_atime;
6756 in->yst_mtime = oh->yst_mtime;
6757 in->yst_ctime = oh->yst_ctime;
6758 in->yst_rdev = oh->yst_rdev;
6762 in->variantType = tags.extraObjectType;
6766 in->hdrChunk = chunk;
6768 } else if (!in->valid) {
6769 /* we need to load this info */
6772 in->hdrChunk = chunk;
6775 in->variantType = oh->type;
6777 in->yst_mode = oh->yst_mode;
6778 #ifdef CONFIG_YAFFS_WINCE
6779 in->win_atime[0] = oh->win_atime[0];
6780 in->win_ctime[0] = oh->win_ctime[0];
6781 in->win_mtime[0] = oh->win_mtime[0];
6782 in->win_atime[1] = oh->win_atime[1];
6783 in->win_ctime[1] = oh->win_ctime[1];
6784 in->win_mtime[1] = oh->win_mtime[1];
6786 in->yst_uid = oh->yst_uid;
6787 in->yst_gid = oh->yst_gid;
6788 in->yst_atime = oh->yst_atime;
6789 in->yst_mtime = oh->yst_mtime;
6790 in->yst_ctime = oh->yst_ctime;
6791 in->yst_rdev = oh->yst_rdev;
6794 if (oh->shadowsObject > 0)
6795 yaffs_HandleShadowedObject(dev,
6802 yaffs_SetObjectName(in, oh->name);
6804 yaffs_FindOrCreateObjectByNumber
6805 (dev, oh->parentObjectId,
6806 YAFFS_OBJECT_TYPE_DIRECTORY);
6808 fileSize = oh->fileSize;
6809 isShrink = oh->isShrink;
6810 equivalentObjectId = oh->equivalentObjectId;
6813 in->variantType = tags.extraObjectType;
6815 yaffs_FindOrCreateObjectByNumber
6816 (dev, tags.extraParentObjectId,
6817 YAFFS_OBJECT_TYPE_DIRECTORY);
6818 fileSize = tags.extraFileLength;
6819 isShrink = tags.extraIsShrinkHeader;
6820 equivalentObjectId = tags.extraEquivalentObjectId;
6829 /* directory stuff...
6833 if (parent && parent->variantType ==
6834 YAFFS_OBJECT_TYPE_UNKNOWN) {
6835 /* Set up as a directory */
6836 parent->variantType =
6837 YAFFS_OBJECT_TYPE_DIRECTORY;
6838 YINIT_LIST_HEAD(&parent->variant.
6841 } else if (!parent || parent->variantType !=
6842 YAFFS_OBJECT_TYPE_DIRECTORY) {
6843 /* Hoosterman, another problem....
6844 * We're trying to use a non-directory as a directory
6847 T(YAFFS_TRACE_ERROR,
6849 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
6851 parent = dev->lostNFoundDir;
6854 yaffs_AddObjectToDirectory(parent, in);
6856 itsUnlinked = (parent == dev->deletedDir) ||
6857 (parent == dev->unlinkedDir);
6860 /* Mark the block as having a shrinkHeader */
6861 bi->hasShrinkHeader = 1;
6864 /* Note re hardlinks.
6865 * Since we might scan a hardlink before its equivalent object is scanned
6866 * we put them all in a list.
6867 * After scanning is complete, we should have all the objects, so we run
6868 * through this list and fix up all the chains.
6871 switch (in->variantType) {
6872 case YAFFS_OBJECT_TYPE_UNKNOWN:
6873 /* Todo got a problem */
6875 case YAFFS_OBJECT_TYPE_FILE:
6877 if (in->variant.fileVariant.
6878 scannedFileSize < fileSize) {
6879 /* This covers the case where the file size is greater
6880 * than where the data is
6881 * This will happen if the file is resized to be larger
6882 * than its current data extents.
6884 in->variant.fileVariant.fileSize = fileSize;
6885 in->variant.fileVariant.scannedFileSize = fileSize;
6888 if (in->variant.fileVariant.shrinkSize > fileSize)
6889 in->variant.fileVariant.shrinkSize = fileSize;
6893 case YAFFS_OBJECT_TYPE_HARDLINK:
6895 in->variant.hardLinkVariant.equivalentObjectId =
6897 in->hardLinks.next =
6898 (struct ylist_head *) hardList;
6902 case YAFFS_OBJECT_TYPE_DIRECTORY:
6905 case YAFFS_OBJECT_TYPE_SPECIAL:
6908 case YAFFS_OBJECT_TYPE_SYMLINK:
6910 in->variant.symLinkVariant.alias =
6911 yaffs_CloneString(oh->alias);
6912 if (!in->variant.symLinkVariant.alias)
6922 } /* End of scanning for each chunk */
6924 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6925 /* If we got this far while scanning, then the block is fully allocated. */
6926 state = YAFFS_BLOCK_STATE_FULL;
6930 bi->blockState = state;
6932 /* Now let's see if it was dirty */
6933 if (bi->pagesInUse == 0 &&
6934 !bi->hasShrinkHeader &&
6935 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
6936 yaffs_BlockBecameDirty(dev, blk);
6941 yaffs_SkipRestOfBlock(dev);
6944 YFREE_ALT(blockIndex);
6948 /* Ok, we've done all the scanning.
6949 * Fix up the hard link chains.
6950 * We should now have scanned all the objects, now it's time to add these
6953 yaffs_HardlinkFixup(dev, hardList);
6956 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
6961 T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
6966 /*------------------------------ Directory Functions ----------------------------- */
6968 static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
6970 struct ylist_head *lh;
6971 yaffs_Object *listObj;
6976 T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
6981 if (yaffs_SkipVerification(obj->myDev))
6985 T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
6990 if (obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6991 T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
6995 /* Iterate through the objects in each hash entry */
6997 ylist_for_each(lh, &obj->parent->variant.directoryVariant.children) {
6999 listObj = ylist_entry(lh, yaffs_Object, siblings);
7000 yaffs_VerifyObject(listObj);
7007 T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
7012 static void yaffs_VerifyDirectory(yaffs_Object *directory)
7014 struct ylist_head *lh;
7015 yaffs_Object *listObj;
7022 if (yaffs_SkipFullVerification(directory->myDev))
7025 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
7026 T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variantType));
7030 /* Iterate through the objects in each hash entry */
7032 ylist_for_each(lh, &directory->variant.directoryVariant.children) {
7034 listObj = ylist_entry(lh, yaffs_Object, siblings);
7035 if (listObj->parent != directory) {
7036 T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
7039 yaffs_VerifyObjectInDirectory(listObj);
7045 *yaffs_UpdateParent() handles fixing a directories mtime and ctime when a new
7046 * link (ie. name) is created or deleted in the directory.
7049 * create dir/a : update dir's mtime/ctime
7050 * rm dir/a: update dir's mtime/ctime
7051 * modify dir/a: don't update dir's mtimme/ctime
7053 * This can be handled immediately or defered. Defering helps reduce the number
7054 * of updates when many files in a directory are changed within a brief period.
7056 * If the directory updating is defered then yaffs_UpdateDirtyDirecories must be
7057 * called periodically.
7060 static void yaffs_UpdateParent(yaffs_Object *obj)
7068 obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME;
7069 if(dev->param.deferDirectoryUpdate){
7070 struct ylist_head *link = &obj->variant.directoryVariant.dirty;
7072 if(ylist_empty(link)){
7073 ylist_add(link,&dev->dirtyDirectories);
7074 T(YAFFS_TRACE_BACKGROUND, (TSTR("Added object %d to dirty directories" TENDSTR),obj->objectId));
7078 yaffs_UpdateObjectHeader(obj, NULL, 0, 0, 0, NULL);
7081 void yaffs_UpdateDirtyDirectories(yaffs_Device *dev)
7083 struct ylist_head *link;
7085 yaffs_DirectoryStructure *dS;
7086 yaffs_ObjectVariant *oV;
7088 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
7090 while(!ylist_empty(&dev->dirtyDirectories)){
7091 link = dev->dirtyDirectories.next;
7092 ylist_del_init(link);
7094 dS=ylist_entry(link,yaffs_DirectoryStructure,dirty);
7095 oV = ylist_entry(dS,yaffs_ObjectVariant,directoryVariant);
7096 obj = ylist_entry(oV,yaffs_Object,variant);
7098 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update directory %d" TENDSTR), obj->objectId));
7101 yaffs_UpdateObjectHeader(obj, NULL, 0, 0, 0, NULL);
7105 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
7107 yaffs_Device *dev = obj->myDev;
7108 yaffs_Object *parent;
7110 yaffs_VerifyObjectInDirectory(obj);
7111 parent = obj->parent;
7113 yaffs_VerifyDirectory(parent);
7115 if (dev && dev->param.removeObjectCallback)
7116 dev->param.removeObjectCallback(obj);
7119 ylist_del_init(&obj->siblings);
7122 yaffs_VerifyDirectory(parent);
7125 static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
7129 T(YAFFS_TRACE_ALWAYS,
7131 ("tragedy: Trying to add an object to a null pointer directory"
7136 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
7137 T(YAFFS_TRACE_ALWAYS,
7139 ("tragedy: Trying to add an object to a non-directory"
7144 if (obj->siblings.prev == NULL) {
7145 /* Not initialised */
7150 yaffs_VerifyDirectory(directory);
7152 yaffs_RemoveObjectFromDirectory(obj);
7156 ylist_add(&obj->siblings, &directory->variant.directoryVariant.children);
7157 obj->parent = directory;
7159 if (directory == obj->myDev->unlinkedDir
7160 || directory == obj->myDev->deletedDir) {
7162 obj->myDev->nUnlinkedFiles++;
7163 obj->renameAllowed = 0;
7166 yaffs_VerifyDirectory(directory);
7167 yaffs_VerifyObjectInDirectory(obj);
7170 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
7175 struct ylist_head *i;
7176 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
7184 T(YAFFS_TRACE_ALWAYS,
7186 ("tragedy: yaffs_FindObjectByName: null pointer directory"
7191 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
7192 T(YAFFS_TRACE_ALWAYS,
7194 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
7198 sum = yaffs_CalcNameSum(name);
7200 ylist_for_each(i, &directory->variant.directoryVariant.children) {
7202 l = ylist_entry(i, yaffs_Object, siblings);
7204 if (l->parent != directory)
7207 yaffs_CheckObjectDetailsLoaded(l);
7209 /* Special case for lost-n-found */
7210 if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
7211 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
7213 } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
7214 /* LostnFound chunk called Objxxx
7217 yaffs_GetObjectName(l, buffer,
7218 YAFFS_MAX_NAME_LENGTH + 1);
7219 if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
7230 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
7231 int (*fn) (yaffs_Object *))
7233 struct ylist_head *i;
7237 T(YAFFS_TRACE_ALWAYS,
7239 ("tragedy: yaffs_FindObjectByName: null pointer directory"
7244 if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
7245 T(YAFFS_TRACE_ALWAYS,
7247 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
7252 ylist_for_each(i, &theDir->variant.directoryVariant.children) {
7254 l = ylist_entry(i, yaffs_Object, siblings);
7265 /* GetEquivalentObject dereferences any hard links to get to the
7269 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
7271 if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
7272 /* We want the object id of the equivalent object, not this one */
7273 obj = obj->variant.hardLinkVariant.equivalentObject;
7274 yaffs_CheckObjectDetailsLoaded(obj);
7279 int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
7281 memset(name, 0, buffSize * sizeof(YCHAR));
7283 yaffs_CheckObjectDetailsLoaded(obj);
7285 if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
7286 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
7287 } else if (obj->hdrChunk <= 0) {
7289 YCHAR numString[20];
7290 YCHAR *x = &numString[19];
7291 unsigned v = obj->objectId;
7295 *x = '0' + (v % 10);
7298 /* make up a name */
7299 yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
7300 yaffs_strcat(locName, x);
7301 yaffs_strncpy(name, locName, buffSize - 1);
7304 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
7305 else if (obj->shortName[0])
7306 yaffs_strncpy(name, obj->shortName,YAFFS_SHORT_NAME_LENGTH+1);
7310 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
7312 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
7314 memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
7316 if (obj->hdrChunk > 0) {
7317 result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
7318 obj->hdrChunk, buffer,
7321 yaffs_strncpy(name, oh->name, buffSize - 1);
7324 yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
7327 return yaffs_strnlen(name,buffSize-1);
7330 int yaffs_GetObjectFileLength(yaffs_Object *obj)
7332 /* Dereference any hard linking */
7333 obj = yaffs_GetEquivalentObject(obj);
7335 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7336 return obj->variant.fileVariant.fileSize;
7337 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
7338 if(!obj->variant.symLinkVariant.alias)
7340 return yaffs_strnlen(obj->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
7342 /* Only a directory should drop through to here */
7343 return obj->myDev->nDataBytesPerChunk;
7347 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
7350 struct ylist_head *i;
7353 count++; /* the object itself */
7355 ylist_for_each(i, &obj->hardLinks)
7356 count++; /* add the hard links; */
7361 int yaffs_GetObjectInode(yaffs_Object *obj)
7363 obj = yaffs_GetEquivalentObject(obj);
7365 return obj->objectId;
7368 unsigned yaffs_GetObjectType(yaffs_Object *obj)
7370 obj = yaffs_GetEquivalentObject(obj);
7372 switch (obj->variantType) {
7373 case YAFFS_OBJECT_TYPE_FILE:
7376 case YAFFS_OBJECT_TYPE_DIRECTORY:
7379 case YAFFS_OBJECT_TYPE_SYMLINK:
7382 case YAFFS_OBJECT_TYPE_HARDLINK:
7385 case YAFFS_OBJECT_TYPE_SPECIAL:
7386 if (S_ISFIFO(obj->yst_mode))
7388 if (S_ISCHR(obj->yst_mode))
7390 if (S_ISBLK(obj->yst_mode))
7392 if (S_ISSOCK(obj->yst_mode))
7400 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
7402 obj = yaffs_GetEquivalentObject(obj);
7403 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
7404 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
7406 return yaffs_CloneString(_Y(""));
7409 #ifndef CONFIG_YAFFS_WINCE
7411 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
7413 unsigned int valid = attr->ia_valid;
7415 if (valid & ATTR_MODE)
7416 obj->yst_mode = attr->ia_mode;
7417 if (valid & ATTR_UID)
7418 obj->yst_uid = attr->ia_uid;
7419 if (valid & ATTR_GID)
7420 obj->yst_gid = attr->ia_gid;
7422 if (valid & ATTR_ATIME)
7423 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
7424 if (valid & ATTR_CTIME)
7425 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
7426 if (valid & ATTR_MTIME)
7427 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
7429 if (valid & ATTR_SIZE)
7430 yaffs_ResizeFile(obj, attr->ia_size);
7432 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0, NULL);
7437 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
7439 unsigned int valid = 0;
7441 attr->ia_mode = obj->yst_mode;
7443 attr->ia_uid = obj->yst_uid;
7445 attr->ia_gid = obj->yst_gid;
7448 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
7449 valid |= ATTR_ATIME;
7450 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
7451 valid |= ATTR_CTIME;
7452 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
7453 valid |= ATTR_MTIME;
7455 attr->ia_size = yaffs_GetFileSize(obj);
7458 attr->ia_valid = valid;
7466 static int yaffs_DoXMod(yaffs_Object *obj, int set, const char *name, const void *value, int size, int flags)
7468 yaffs_XAttrMod xmod;
7477 xmod.result = -ENOSPC;
7479 result = yaffs_UpdateObjectHeader(obj, NULL, 0, 0, 0, &xmod);
7487 static int yaffs_ApplyXMod(yaffs_Device *dev, char *buffer, yaffs_XAttrMod *xmod)
7490 int x_offs = sizeof(yaffs_ObjectHeader);
7491 int x_size = dev->nDataBytesPerChunk - sizeof(yaffs_ObjectHeader);
7493 char * x_buffer = buffer + x_offs;
7496 retval = nval_set(x_buffer, x_size, xmod->name, xmod->data, xmod->size, xmod->flags);
7498 retval = nval_del(x_buffer, x_size, xmod->name);
7500 xmod->result = retval;
7505 static int yaffs_DoXFetch(yaffs_Object *obj, const char *name, void *value, int size)
7507 char *buffer = NULL;
7509 yaffs_ExtendedTags tags;
7510 yaffs_Device *dev = obj->myDev;
7511 int x_offs = sizeof(yaffs_ObjectHeader);
7512 int x_size = dev->nDataBytesPerChunk - sizeof(yaffs_ObjectHeader);
7518 if(obj->hdrChunk < 1)
7521 buffer = yaffs_GetTempBuffer(dev, __LINE__);
7525 result = yaffs_ReadChunkWithTagsFromNAND(dev,obj->hdrChunk, buffer, &tags);
7527 if(result != YAFFS_OK)
7530 x_buffer = buffer + x_offs;
7533 retval = nval_get(x_buffer, x_size, name, value, size);
7535 retval = nval_list(x_buffer, x_size, value,size);
7537 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
7541 int yaffs_SetXAttribute(yaffs_Object *obj, const char *name, const void * value, int size, int flags)
7543 return yaffs_DoXMod(obj, 1, name, value, size, flags);
7546 int yaffs_RemoveXAttribute(yaffs_Object *obj, const char *name)
7548 return yaffs_DoXMod(obj, 0, name, NULL, 0, 0);
7551 int yaffs_GetXAttribute(yaffs_Object *obj, const char *name, void *value, int size)
7553 return yaffs_DoXFetch(obj, name, value, size);
7556 int yaffs_ListXAttributes(yaffs_Object *obj, char *buffer, int size)
7558 return yaffs_DoXFetch(obj, NULL, buffer,size);
7564 int yaffs_DumpObject(yaffs_Object *obj)
7568 yaffs_GetObjectName(obj, name, YAFFS_MAX_NAME_LENGTH + 1);
7570 T(YAFFS_TRACE_ALWAYS,
7572 ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
7573 " chunk %d type %d size %d\n"
7574 TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
7575 obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdrChunk,
7576 yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
7582 /*---------------------------- Initialisation code -------------------------------------- */
7584 static int yaffs_CheckDevFunctions(const yaffs_Device *dev)
7587 /* Common functions, gotta have */
7588 if (!dev->param.eraseBlockInNAND || !dev->param.initialiseNAND)
7591 #ifdef CONFIG_YAFFS_YAFFS2
7593 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
7594 if (dev->param.writeChunkWithTagsToNAND &&
7595 dev->param.readChunkWithTagsFromNAND &&
7596 !dev->param.writeChunkToNAND &&
7597 !dev->param.readChunkFromNAND &&
7598 dev->param.markNANDBlockBad &&
7599 dev->param.queryNANDBlock)
7603 /* Can use the "spare" style interface for yaffs1 */
7604 if (!dev->param.isYaffs2 &&
7605 !dev->param.writeChunkWithTagsToNAND &&
7606 !dev->param.readChunkWithTagsFromNAND &&
7607 dev->param.writeChunkToNAND &&
7608 dev->param.readChunkFromNAND &&
7609 !dev->param.markNANDBlockBad &&
7610 !dev->param.queryNANDBlock)
7617 static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
7619 /* Initialise the unlinked, deleted, root and lost and found directories */
7621 dev->lostNFoundDir = dev->rootDir = NULL;
7622 dev->unlinkedDir = dev->deletedDir = NULL;
7625 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
7628 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
7631 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
7632 YAFFS_ROOT_MODE | S_IFDIR);
7633 dev->lostNFoundDir =
7634 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
7635 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
7637 if (dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir) {
7638 yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
7645 int yaffs_GutsInitialise(yaffs_Device *dev)
7647 int init_failed = 0;
7651 T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
7653 /* Check stuff that must be set */
7656 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
7660 dev->internalStartBlock = dev->param.startBlock;
7661 dev->internalEndBlock = dev->param.endBlock;
7662 dev->blockOffset = 0;
7663 dev->chunkOffset = 0;
7664 dev->nFreeChunks = 0;
7668 if (dev->param.startBlock == 0) {
7669 dev->internalStartBlock = dev->param.startBlock + 1;
7670 dev->internalEndBlock = dev->param.endBlock + 1;
7671 dev->blockOffset = 1;
7672 dev->chunkOffset = dev->param.nChunksPerBlock;
7675 /* Check geometry parameters. */
7677 if ((!dev->param.inbandTags && dev->param.isYaffs2 && dev->param.totalBytesPerChunk < 1024) ||
7678 (!dev->param.isYaffs2 && dev->param.totalBytesPerChunk < 512) ||
7679 (dev->param.inbandTags && !dev->param.isYaffs2) ||
7680 dev->param.nChunksPerBlock < 2 ||
7681 dev->param.nReservedBlocks < 2 ||
7682 dev->internalStartBlock <= 0 ||
7683 dev->internalEndBlock <= 0 ||
7684 dev->internalEndBlock <= (dev->internalStartBlock + dev->param.nReservedBlocks + 2)) { /* otherwise it is too small */
7685 T(YAFFS_TRACE_ALWAYS,
7687 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d "
7688 TENDSTR), dev->param.totalBytesPerChunk, dev->param.isYaffs2 ? "2" : "", dev->param.inbandTags));
7692 if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
7693 T(YAFFS_TRACE_ALWAYS,
7694 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
7698 /* Sort out space for inband tags, if required */
7699 if (dev->param.inbandTags)
7700 dev->nDataBytesPerChunk = dev->param.totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart);
7702 dev->nDataBytesPerChunk = dev->param.totalBytesPerChunk;
7704 /* Got the right mix of functions? */
7705 if (!yaffs_CheckDevFunctions(dev)) {
7706 /* Function missing */
7707 T(YAFFS_TRACE_ALWAYS,
7709 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
7714 /* This is really a compilation check. */
7715 if (!yaffs_CheckStructures()) {
7716 T(YAFFS_TRACE_ALWAYS,
7717 (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
7721 if (dev->isMounted) {
7722 T(YAFFS_TRACE_ALWAYS,
7723 (TSTR("yaffs: device already mounted\n" TENDSTR)));
7727 /* Finished with most checks. One or two more checks happen later on too. */
7731 /* OK now calculate a few things for the device */
7734 * Calculate all the chunk size manipulation numbers:
7736 x = dev->nDataBytesPerChunk;
7737 /* We always use dev->chunkShift and dev->chunkDiv */
7738 dev->chunkShift = Shifts(x);
7739 x >>= dev->chunkShift;
7741 /* We only use chunk mask if chunkDiv is 1 */
7742 dev->chunkMask = (1<<dev->chunkShift) - 1;
7745 * Calculate chunkGroupBits.
7746 * We need to find the next power of 2 > than internalEndBlock
7749 x = dev->param.nChunksPerBlock * (dev->internalEndBlock + 1);
7753 /* Set up tnode width if wide tnodes are enabled. */
7754 if (!dev->param.wideTnodesDisabled) {
7755 /* bits must be even so that we end up with 32-bit words */
7759 dev->tnodeWidth = 16;
7761 dev->tnodeWidth = bits;
7763 dev->tnodeWidth = 16;
7765 dev->tnodeMask = (1<<dev->tnodeWidth)-1;
7767 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
7768 * so if the bitwidth of the
7769 * chunk range we're using is greater than 16 we need
7770 * to figure out chunk shift and chunkGroupSize
7773 if (bits <= dev->tnodeWidth)
7774 dev->chunkGroupBits = 0;
7776 dev->chunkGroupBits = bits - dev->tnodeWidth;
7778 dev->tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7779 if(dev->tnodeSize < sizeof(yaffs_Tnode))
7780 dev->tnodeSize = sizeof(yaffs_Tnode);
7782 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
7784 if (dev->param.nChunksPerBlock < dev->chunkGroupSize) {
7785 /* We have a problem because the soft delete won't work if
7786 * the chunk group size > chunks per block.
7787 * This can be remedied by using larger "virtual blocks".
7789 T(YAFFS_TRACE_ALWAYS,
7790 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
7795 /* OK, we've finished verifying the device, lets continue with initialisation */
7797 /* More device initialisation */
7799 dev->passiveGCs = 0;
7800 dev->oldestDirtyGCs = 0;
7801 dev->backgroundGCs = 0;
7802 dev->gcBlockFinder = 0;
7803 dev->bufferedBlock = -1;
7804 dev->doingBufferedBlockRewrite = 0;
7805 dev->nDeletedFiles = 0;
7806 dev->nBackgroundDeletions = 0;
7807 dev->nUnlinkedFiles = 0;
7809 dev->eccUnfixed = 0;
7810 dev->tagsEccFixed = 0;
7811 dev->tagsEccUnfixed = 0;
7812 dev->nErasureFailures = 0;
7813 dev->nErasedBlocks = 0;
7815 dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
7816 YINIT_LIST_HEAD(&dev->dirtyDirectories);
7817 dev->oldestDirtySequence = 0;
7818 dev->oldestDirtyBlock = 0;
7820 /* Initialise temporary buffers and caches. */
7821 if (!yaffs_InitialiseTempBuffers(dev))
7824 dev->srCache = NULL;
7825 dev->gcCleanupList = NULL;
7829 dev->param.nShortOpCaches > 0) {
7832 int srCacheBytes = dev->param.nShortOpCaches * sizeof(yaffs_ChunkCache);
7834 if (dev->param.nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
7835 dev->param.nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
7837 dev->srCache = YMALLOC(srCacheBytes);
7839 buf = (__u8 *) dev->srCache;
7842 memset(dev->srCache, 0, srCacheBytes);
7844 for (i = 0; i < dev->param.nShortOpCaches && buf; i++) {
7845 dev->srCache[i].object = NULL;
7846 dev->srCache[i].lastUse = 0;
7847 dev->srCache[i].dirty = 0;
7848 dev->srCache[i].data = buf = YMALLOC_DMA(dev->param.totalBytesPerChunk);
7859 dev->gcCleanupList = YMALLOC(dev->param.nChunksPerBlock * sizeof(__u32));
7860 if (!dev->gcCleanupList)
7864 if (dev->param.isYaffs2)
7865 dev->param.useHeaderFileSize = 1;
7867 if (!init_failed && !yaffs_InitialiseBlocks(dev))
7870 yaffs_InitialiseTnodesAndObjects(dev);
7872 if (!init_failed && !yaffs_CreateInitialDirectories(dev))
7877 /* Now scan the flash. */
7878 if (dev->param.isYaffs2) {
7879 if (yaffs_CheckpointRestore(dev)) {
7880 yaffs_CheckObjectDetailsLoaded(dev->rootDir);
7881 T(YAFFS_TRACE_ALWAYS,
7882 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
7885 /* Clean up the mess caused by an aborted checkpoint load
7886 * and scan backwards.
7888 yaffs_DeinitialiseBlocks(dev);
7890 yaffs_DeinitialiseTnodesAndObjects(dev);
7892 dev->nErasedBlocks = 0;
7893 dev->nFreeChunks = 0;
7894 dev->allocationBlock = -1;
7895 dev->allocationPage = -1;
7896 dev->nDeletedFiles = 0;
7897 dev->nUnlinkedFiles = 0;
7898 dev->nBackgroundDeletions = 0;
7900 if (!init_failed && !yaffs_InitialiseBlocks(dev))
7903 yaffs_InitialiseTnodesAndObjects(dev);
7905 if (!init_failed && !yaffs_CreateInitialDirectories(dev))
7908 if (!init_failed && !yaffs_ScanBackwards(dev))
7911 } else if (!yaffs_Scan(dev))
7914 yaffs_StripDeletedObjects(dev);
7915 yaffs_FixHangingObjects(dev);
7916 if(dev->param.emptyLostAndFound)
7917 yaffs_EmptyLostAndFound(dev);
7921 /* Clean up the mess */
7922 T(YAFFS_TRACE_TRACING,
7923 (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
7925 yaffs_Deinitialise(dev);
7929 /* Zero out stats */
7930 dev->nPageReads = 0;
7931 dev->nPageWrites = 0;
7932 dev->nBlockErasures = 0;
7934 dev->nRetriedWrites = 0;
7936 dev->nRetiredBlocks = 0;
7938 yaffs_VerifyFreeChunks(dev);
7939 yaffs_VerifyBlocks(dev);
7941 /* Clean up any aborted checkpoint data */
7942 if(!dev->isCheckpointed && dev->blocksInCheckpoint > 0)
7943 yaffs_InvalidateCheckpoint(dev);
7945 T(YAFFS_TRACE_TRACING,
7946 (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
7951 void yaffs_Deinitialise(yaffs_Device *dev)
7953 if (dev->isMounted) {
7956 yaffs_DeinitialiseBlocks(dev);
7957 yaffs_DeinitialiseTnodesAndObjects(dev);
7958 if (dev->param.nShortOpCaches > 0 &&
7961 for (i = 0; i < dev->param.nShortOpCaches; i++) {
7962 if (dev->srCache[i].data)
7963 YFREE(dev->srCache[i].data);
7964 dev->srCache[i].data = NULL;
7967 YFREE(dev->srCache);
7968 dev->srCache = NULL;
7971 YFREE(dev->gcCleanupList);
7973 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
7974 YFREE(dev->tempBuffer[i].buffer);
7978 if (dev->param.deinitialiseNAND)
7979 dev->param.deinitialiseNAND(dev);
7983 static int yaffs_CountFreeChunks(yaffs_Device *dev)
7988 yaffs_BlockInfo *blk;
7990 blk = dev->blockInfo;
7991 for (b = dev->internalStartBlock; b <= dev->internalEndBlock; b++) {
7992 switch (blk->blockState) {
7993 case YAFFS_BLOCK_STATE_EMPTY:
7994 case YAFFS_BLOCK_STATE_ALLOCATING:
7995 case YAFFS_BLOCK_STATE_COLLECTING:
7996 case YAFFS_BLOCK_STATE_FULL:
7998 (dev->param.nChunksPerBlock - blk->pagesInUse +
7999 blk->softDeletions);
8010 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
8012 /* This is what we report to the outside world */
8015 int nDirtyCacheChunks;
8016 int blocksForCheckpoint;
8020 nFree = dev->nFreeChunks;
8022 nFree = yaffs_CountFreeChunks(dev);
8025 nFree += dev->nDeletedFiles;
8027 /* Now count the number of dirty chunks in the cache and subtract those */
8029 for (nDirtyCacheChunks = 0, i = 0; i < dev->param.nShortOpCaches; i++) {
8030 if (dev->srCache[i].dirty)
8031 nDirtyCacheChunks++;
8034 nFree -= nDirtyCacheChunks;
8036 nFree -= ((dev->param.nReservedBlocks + 1) * dev->param.nChunksPerBlock);
8038 /* Now we figure out how much to reserve for the checkpoint and report that... */
8039 blocksForCheckpoint = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
8040 if (blocksForCheckpoint < 0)
8041 blocksForCheckpoint = 0;
8043 nFree -= (blocksForCheckpoint * dev->param.nChunksPerBlock);
8052 static int yaffs_freeVerificationFailures;
8054 static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
8059 if (yaffs_SkipVerification(dev))
8062 counted = yaffs_CountFreeChunks(dev);
8064 difference = dev->nFreeChunks - counted;
8067 T(YAFFS_TRACE_ALWAYS,
8068 (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
8069 dev->nFreeChunks, counted, difference));
8070 yaffs_freeVerificationFailures++;
8074 /*---------------------------------------- YAFFS test code ----------------------*/
8076 #define yaffs_CheckStruct(structure, syze, name) \
8078 if (sizeof(structure) != syze) { \
8079 T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
8080 name, syze, (int) sizeof(structure))); \
8081 return YAFFS_FAIL; \
8085 static int yaffs_CheckStructures(void)
8087 /* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags"); */
8088 /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */
8089 /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */
8090 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
8091 /* yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode"); */
8093 #ifndef CONFIG_YAFFS_WINCE
8094 yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");