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"
23 #include "yaffs_nand.h"
25 #include "yaffs_yaffs1.h"
26 #include "yaffs_yaffs2.h"
27 #include "yaffs_bitmap.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 #include "yaffs_ecc.h"
43 /* Robustification (if it ever comes about...) */
44 static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
45 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
47 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
49 const yaffs_ExtendedTags *tags);
50 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
51 const yaffs_ExtendedTags *tags);
53 /* Other local prototypes */
54 static void yaffs_UpdateParent(yaffs_Object *obj);
55 static int yaffs_UnlinkObject(yaffs_Object *obj);
56 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
58 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
60 yaffs_ExtendedTags *tags,
64 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
65 yaffs_ObjectType type);
68 static int yaffs_ApplyXMod(yaffs_Device *dev, char *buffer, yaffs_XAttrMod *xmod);
70 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
71 static int yaffs_CheckStructures(void);
72 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
74 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
77 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
80 static int yaffs_UnlinkWorker(yaffs_Object *obj);
82 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
85 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
86 yaffs_BlockInfo **blockUsedPtr);
88 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
90 static void yaffs_VerifyDirectory(yaffs_Object *directory);
92 static int yaffs_CheckFileSanity(yaffs_Object *in);
94 #define yaffs_CheckFileSanity(in)
97 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
98 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
100 static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
101 yaffs_ExtendedTags *tags);
103 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
105 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
106 yaffs_FileStructure *fStruct,
109 static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
112 yaffs_ExtendedTags *tags);
114 /* Function to calculate chunk and offset */
116 static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
122 chunk = (__u32)(addr >> dev->chunkShift);
124 if (dev->chunkDiv == 1) {
125 /* easy power of 2 case */
126 offset = (__u32)(addr & dev->chunkMask);
128 /* Non power-of-2 case */
132 chunk /= dev->chunkDiv;
134 chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
135 offset = (__u32)(addr - chunkBase);
142 /* Function to return the number of shifts for a power of 2 greater than or
143 * equal to the given number
144 * Note we don't try to cater for all possible numbers and this does not have to
145 * be hellishly efficient.
148 static __u32 ShiftsGE(__u32 x)
153 nShifts = extraBits = 0;
168 /* Function to return the number of shifts to get a 1 in bit 0
171 static __u32 Shifts(__u32 x)
191 * Temporary buffer manipulations.
194 static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
197 __u8 *buf = (__u8 *)1;
199 memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
201 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
202 dev->tempBuffer[i].line = 0; /* not in use */
203 dev->tempBuffer[i].buffer = buf =
204 YMALLOC_DMA(dev->param.totalBytesPerChunk);
207 return buf ? YAFFS_OK : YAFFS_FAIL;
210 __u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
215 if (dev->tempInUse > dev->maxTemp)
216 dev->maxTemp = dev->tempInUse;
218 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
219 if (dev->tempBuffer[i].line == 0) {
220 dev->tempBuffer[i].line = lineNo;
221 if ((i + 1) > dev->maxTemp) {
222 dev->maxTemp = i + 1;
223 for (j = 0; j <= i; j++)
224 dev->tempBuffer[j].maxLine =
225 dev->tempBuffer[j].line;
228 return dev->tempBuffer[i].buffer;
232 T(YAFFS_TRACE_BUFFERS,
233 (TSTR("Out of temp buffers at line %d, other held by lines:"),
235 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
236 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
238 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
241 * If we got here then we have to allocate an unmanaged one
245 dev->unmanagedTempAllocations++;
246 return YMALLOC(dev->nDataBytesPerChunk);
250 void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
257 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
258 if (dev->tempBuffer[i].buffer == buffer) {
259 dev->tempBuffer[i].line = 0;
265 /* assume it is an unmanaged one. */
266 T(YAFFS_TRACE_BUFFERS,
267 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
270 dev->unmanagedTempDeallocations++;
276 * Determine if we have a managed buffer.
278 int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
282 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
283 if (dev->tempBuffer[i].buffer == buffer)
287 for (i = 0; i < dev->param.nShortOpCaches; i++) {
288 if (dev->srCache[i].data == buffer)
292 if (buffer == dev->checkpointBuffer)
295 T(YAFFS_TRACE_ALWAYS,
296 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
304 static int yaffs_SkipVerification(yaffs_Device *dev)
307 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
310 static int yaffs_SkipFullVerification(yaffs_Device *dev)
313 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
316 static int yaffs_SkipNANDVerification(yaffs_Device *dev)
319 return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
322 static const char *blockStateName[] = {
336 static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
341 if (yaffs_SkipVerification(dev))
344 /* Report illegal runtime states */
345 if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
346 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
348 switch (bi->blockState) {
349 case YAFFS_BLOCK_STATE_UNKNOWN:
350 case YAFFS_BLOCK_STATE_SCANNING:
351 case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
352 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
353 n, blockStateName[bi->blockState]));
356 /* Check pages in use and soft deletions are legal */
358 actuallyUsed = bi->pagesInUse - bi->softDeletions;
360 if (bi->pagesInUse < 0 || bi->pagesInUse > dev->param.nChunksPerBlock ||
361 bi->softDeletions < 0 || bi->softDeletions > dev->param.nChunksPerBlock ||
362 actuallyUsed < 0 || actuallyUsed > dev->param.nChunksPerBlock)
363 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
364 n, bi->pagesInUse, bi->softDeletions));
367 /* Check chunk bitmap legal */
368 inUse = yaffs_CountChunkBits(dev, n);
369 if (inUse != bi->pagesInUse)
370 T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
371 n, bi->pagesInUse, inUse));
373 /* Check that the sequence number is valid.
374 * Ten million is legal, but is very unlikely
377 yaffs2_VerifyBlock(dev,bi,n);
382 static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
385 yaffs_VerifyBlock(dev, bi, n);
387 /* After collection the block should be in the erased state */
388 /* This will need to change if we do partial gc */
390 if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
391 bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
392 T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
397 void yaffs_VerifyBlocks(yaffs_Device *dev)
400 int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
401 int nIllegalBlockStates = 0;
403 if (yaffs_SkipVerification(dev))
406 memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
408 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
409 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
410 yaffs_VerifyBlock(dev, bi, i);
412 if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
413 nBlocksPerState[bi->blockState]++;
415 nIllegalBlockStates++;
418 T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
419 T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
421 T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
422 if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
423 T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
425 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
426 T(YAFFS_TRACE_VERIFY,
427 (TSTR("%s %d blocks"TENDSTR),
428 blockStateName[i], nBlocksPerState[i]));
430 if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
431 T(YAFFS_TRACE_VERIFY,
432 (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
433 dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
435 if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
436 T(YAFFS_TRACE_VERIFY,
437 (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
438 dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
440 if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
441 T(YAFFS_TRACE_VERIFY,
442 (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
443 nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
445 T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
450 * Verify the object header. oh must be valid, but obj and tags may be NULL in which
451 * case those tests will not be performed.
453 static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
455 if (obj && yaffs_SkipVerification(obj->myDev))
458 if (!(tags && obj && oh)) {
459 T(YAFFS_TRACE_VERIFY,
460 (TSTR("Verifying object header tags %p obj %p oh %p"TENDSTR),
465 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
466 oh->type > YAFFS_OBJECT_TYPE_MAX)
467 T(YAFFS_TRACE_VERIFY,
468 (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
469 tags->objectId, oh->type));
471 if (tags->objectId != obj->objectId)
472 T(YAFFS_TRACE_VERIFY,
473 (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
474 tags->objectId, obj->objectId));
478 * Check that the object's parent ids match if parentCheck requested.
480 * Tests do not apply to the root object.
483 if (parentCheck && tags->objectId > 1 && !obj->parent)
484 T(YAFFS_TRACE_VERIFY,
485 (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
486 tags->objectId, oh->parentObjectId));
488 if (parentCheck && obj->parent &&
489 oh->parentObjectId != obj->parent->objectId &&
490 (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
491 obj->parent->objectId != YAFFS_OBJECTID_DELETED))
492 T(YAFFS_TRACE_VERIFY,
493 (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
494 tags->objectId, oh->parentObjectId, obj->parent->objectId));
496 if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
497 T(YAFFS_TRACE_VERIFY,
498 (TSTR("Obj %d header name is NULL"TENDSTR),
501 if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
502 T(YAFFS_TRACE_VERIFY,
503 (TSTR("Obj %d header name is 0xFF"TENDSTR),
509 /* Not being used, but don't want to throw away yet */
510 static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
511 __u32 level, int chunkOffset)
514 yaffs_Device *dev = obj->myDev;
520 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
521 if (tn->internal[i]) {
522 ok = yaffs_VerifyTnodeWorker(obj,
525 (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
528 } else if (level == 0) {
529 yaffs_ExtendedTags tags;
530 __u32 objectId = obj->objectId;
532 chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
534 for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
535 __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
538 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
539 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
540 if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
541 T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
542 objectId, chunkOffset, theChunk,
543 tags.objectId, tags.chunkId));
557 static void yaffs_VerifyFile(yaffs_Object *obj)
559 int requiredTallness;
565 yaffs_ExtendedTags tags;
572 if (yaffs_SkipVerification(obj->myDev))
576 objectId = obj->objectId;
578 /* Check file size is consistent with tnode depth */
579 lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
580 x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
581 requiredTallness = 0;
583 x >>= YAFFS_TNODES_INTERNAL_BITS;
587 actualTallness = obj->variant.fileVariant.topLevel;
589 /* Check that the chunks in the tnode tree are all correct.
590 * We do this by scanning through the tnode tree and
591 * checking the tags for every chunk match.
594 if (yaffs_SkipNANDVerification(dev))
597 for (i = 1; i <= lastChunk; i++) {
598 tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
601 __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
603 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
604 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
605 if (tags.objectId != objectId || tags.chunkId != i) {
606 T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
607 objectId, i, theChunk,
608 tags.objectId, tags.chunkId));
616 static void yaffs_VerifyHardLink(yaffs_Object *obj)
618 if (obj && yaffs_SkipVerification(obj->myDev))
621 /* Verify sane equivalent object */
624 static void yaffs_VerifySymlink(yaffs_Object *obj)
626 if (obj && yaffs_SkipVerification(obj->myDev))
629 /* Verify symlink string */
632 static void yaffs_VerifySpecial(yaffs_Object *obj)
634 if (obj && yaffs_SkipVerification(obj->myDev))
638 static void yaffs_VerifyObject(yaffs_Object *obj)
647 __u32 chunkShouldNotBeDeleted;
653 if (obj->beingCreated)
658 if (yaffs_SkipVerification(dev))
661 /* Check sane object header chunk */
663 chunkMin = dev->internalStartBlock * dev->param.nChunksPerBlock;
664 chunkMax = (dev->internalEndBlock+1) * dev->param.nChunksPerBlock - 1;
666 chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
667 chunkIdOk = chunkInRange || (obj->hdrChunk == 0);
668 chunkValid = chunkInRange &&
669 yaffs_CheckChunkBit(dev,
670 obj->hdrChunk / dev->param.nChunksPerBlock,
671 obj->hdrChunk % dev->param.nChunksPerBlock);
672 chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
675 (!chunkIdOk || chunkShouldNotBeDeleted)) {
676 T(YAFFS_TRACE_VERIFY,
677 (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
678 obj->objectId, obj->hdrChunk,
679 chunkIdOk ? "" : ",out of range",
680 chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
683 if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
684 yaffs_ExtendedTags tags;
685 yaffs_ObjectHeader *oh;
686 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
688 oh = (yaffs_ObjectHeader *)buffer;
690 yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
693 yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
695 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
698 /* Verify it has a parent */
699 if (obj && !obj->fake &&
700 (!obj->parent || obj->parent->myDev != dev)) {
701 T(YAFFS_TRACE_VERIFY,
702 (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
703 obj->objectId, obj->parent));
706 /* Verify parent is a directory */
707 if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
708 T(YAFFS_TRACE_VERIFY,
709 (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
710 obj->objectId, obj->parent->variantType));
713 switch (obj->variantType) {
714 case YAFFS_OBJECT_TYPE_FILE:
715 yaffs_VerifyFile(obj);
717 case YAFFS_OBJECT_TYPE_SYMLINK:
718 yaffs_VerifySymlink(obj);
720 case YAFFS_OBJECT_TYPE_DIRECTORY:
721 yaffs_VerifyDirectory(obj);
723 case YAFFS_OBJECT_TYPE_HARDLINK:
724 yaffs_VerifyHardLink(obj);
726 case YAFFS_OBJECT_TYPE_SPECIAL:
727 yaffs_VerifySpecial(obj);
729 case YAFFS_OBJECT_TYPE_UNKNOWN:
731 T(YAFFS_TRACE_VERIFY,
732 (TSTR("Obj %d has illegaltype %d"TENDSTR),
733 obj->objectId, obj->variantType));
738 void yaffs_VerifyObjects(yaffs_Device *dev)
742 struct ylist_head *lh;
744 if (yaffs_SkipVerification(dev))
747 /* Iterate through the objects in each hash entry */
749 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
750 ylist_for_each(lh, &dev->objectBucket[i].list) {
752 obj = ylist_entry(lh, yaffs_Object, hashLink);
753 yaffs_VerifyObject(obj);
761 * Simple hash function. Needs to have a reasonable spread
764 static Y_INLINE int yaffs_HashFunction(int n)
767 return n % YAFFS_NOBJECT_BUCKETS;
771 * Access functions to useful fake objects.
772 * Note that root might have a presence in NAND if permissions are set.
775 yaffs_Object *yaffs_Root(yaffs_Device *dev)
780 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
782 return dev->lostNFoundDir;
787 * Erased NAND checking functions
790 int yaffs_CheckFF(__u8 *buffer, int nBytes)
792 /* Horrible, slow implementation */
801 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
804 int retval = YAFFS_OK;
805 __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
806 yaffs_ExtendedTags tags;
809 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
811 if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
814 if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
815 T(YAFFS_TRACE_NANDACCESS,
816 (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
820 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
827 static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
830 yaffs_ExtendedTags *tags)
832 int retval = YAFFS_OK;
833 yaffs_ExtendedTags tempTags;
834 __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
837 result = yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,&tempTags);
838 if(memcmp(buffer,data,dev->nDataBytesPerChunk) ||
839 tempTags.objectId != tags->objectId ||
840 tempTags.chunkId != tags->chunkId ||
841 tempTags.byteCount != tags->byteCount)
844 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
849 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
851 yaffs_ExtendedTags *tags,
858 yaffs2_InvalidateCheckpoint(dev);
861 yaffs_BlockInfo *bi = 0;
864 chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
870 /* First check this chunk is erased, if it needs
871 * checking. The checking policy (unless forced
872 * always on) is as follows:
874 * Check the first page we try to write in a block.
875 * If the check passes then we don't need to check any
876 * more. If the check fails, we check again...
877 * If the block has been erased, we don't need to check.
879 * However, if the block has been prioritised for gc,
880 * then we think there might be something odd about
881 * this block and stop using it.
883 * Rationale: We should only ever see chunks that have
884 * not been erased if there was a partially written
885 * chunk due to power loss. This checking policy should
886 * catch that case with very few checks and thus save a
887 * lot of checks that are most likely not needed.
890 * If an erase check fails or the write fails we skip the
894 /* let's give it a try */
897 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
898 bi->skipErasedCheck = 0;
900 if (!bi->skipErasedCheck) {
901 erasedOk = yaffs_CheckChunkErased(dev, chunk);
902 if (erasedOk != YAFFS_OK) {
904 (TSTR("**>> yaffs chunk %d was not erased"
907 /* If not erased, delete this one,
908 * skip rest of block and
909 * try another chunk */
910 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
911 yaffs_SkipRestOfBlock(dev);
916 writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
919 if(!bi->skipErasedCheck)
920 writeOk = yaffs_VerifyChunkWritten(dev, chunk, data, tags);
922 if (writeOk != YAFFS_OK) {
923 /* Clean up aborted write, skip to next block and
924 * try another chunk */
925 yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
929 bi->skipErasedCheck = 1;
931 /* Copy the data into the robustification buffer */
932 yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
934 } while (writeOk != YAFFS_OK &&
935 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
942 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
945 dev->nRetriedWrites += (attempts - 1);
954 * Block retiring for handling a broken block.
957 static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
959 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
961 yaffs2_InvalidateCheckpoint(dev);
963 yaffs2_ClearOldestDirtySequence(dev,bi);
965 if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
966 if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
967 T(YAFFS_TRACE_ALWAYS, (TSTR(
968 "yaffs: Failed to mark bad and erase block %d"
969 TENDSTR), blockInNAND));
971 yaffs_ExtendedTags tags;
972 int chunkId = blockInNAND * dev->param.nChunksPerBlock;
974 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
976 memset(buffer, 0xff, dev->nDataBytesPerChunk);
977 yaffs_InitialiseTags(&tags);
978 tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
979 if (dev->param.writeChunkWithTagsToNAND(dev, chunkId -
980 dev->chunkOffset, buffer, &tags) != YAFFS_OK)
981 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
982 TCONT("write bad block marker to block %d")
983 TENDSTR), blockInNAND));
985 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
989 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
990 bi->gcPrioritise = 0;
991 bi->needsRetiring = 0;
993 dev->nRetiredBlocks++;
997 * Functions for robustisizing TODO
1001 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
1003 const yaffs_ExtendedTags *tags)
1006 chunkInNAND=chunkInNAND;
1011 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
1012 const yaffs_ExtendedTags *tags)
1015 chunkInNAND=chunkInNAND;
1019 void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
1021 if (!bi->gcPrioritise) {
1022 bi->gcPrioritise = 1;
1023 dev->hasPendingPrioritisedGCs = 1;
1024 bi->chunkErrorStrikes++;
1026 if (bi->chunkErrorStrikes > 3) {
1027 bi->needsRetiring = 1; /* Too many stikes, so retire this */
1028 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
1034 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
1037 int blockInNAND = chunkInNAND / dev->param.nChunksPerBlock;
1038 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
1040 yaffs_HandleChunkError(dev, bi);
1043 /* Was an actual write failure, so mark the block for retirement */
1044 bi->needsRetiring = 1;
1045 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1046 (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
1049 /* Delete the chunk */
1050 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
1051 yaffs_SkipRestOfBlock(dev);
1055 /*---------------- Name handling functions ------------*/
1057 static __u16 yaffs_CalcNameSum(const YCHAR *name)
1062 const YUCHAR *bname = (const YUCHAR *) name;
1064 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
1066 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1067 sum += yaffs_toupper(*bname) * i;
1069 sum += (*bname) * i;
1078 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
1080 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
1081 memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
1082 if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
1083 yaffs_strcpy(obj->shortName, name);
1085 obj->shortName[0] = _Y('\0');
1087 obj->sum = yaffs_CalcNameSum(name);
1090 /*-------------------- TNODES -------------------
1092 * List of spare tnodes
1093 * The list is hooked together using the first pointer
1098 yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
1100 yaffs_Tnode *tn = yaffs_AllocateRawTnode(dev);
1102 memset(tn, 0, dev->tnodeSize);
1106 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1111 /* FreeTnode frees up a tnode and puts it back on the free list */
1112 static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
1114 yaffs_FreeRawTnode(dev,tn);
1116 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1119 static void yaffs_DeinitialiseTnodesAndObjects(yaffs_Device *dev)
1121 yaffs_DeinitialiseRawTnodesAndObjects(dev);
1127 void yaffs_LoadLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
1130 __u32 *map = (__u32 *)tn;
1136 pos &= YAFFS_TNODES_LEVEL0_MASK;
1137 val >>= dev->chunkGroupBits;
1139 bitInMap = pos * dev->tnodeWidth;
1140 wordInMap = bitInMap / 32;
1141 bitInWord = bitInMap & (32 - 1);
1143 mask = dev->tnodeMask << bitInWord;
1145 map[wordInMap] &= ~mask;
1146 map[wordInMap] |= (mask & (val << bitInWord));
1148 if (dev->tnodeWidth > (32 - bitInWord)) {
1149 bitInWord = (32 - bitInWord);
1151 mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
1152 map[wordInMap] &= ~mask;
1153 map[wordInMap] |= (mask & (val >> bitInWord));
1157 static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
1160 __u32 *map = (__u32 *)tn;
1166 pos &= YAFFS_TNODES_LEVEL0_MASK;
1168 bitInMap = pos * dev->tnodeWidth;
1169 wordInMap = bitInMap / 32;
1170 bitInWord = bitInMap & (32 - 1);
1172 val = map[wordInMap] >> bitInWord;
1174 if (dev->tnodeWidth > (32 - bitInWord)) {
1175 bitInWord = (32 - bitInWord);
1177 val |= (map[wordInMap] << bitInWord);
1180 val &= dev->tnodeMask;
1181 val <<= dev->chunkGroupBits;
1186 /* ------------------- End of individual tnode manipulation -----------------*/
1188 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
1189 * The look up tree is represented by the top tnode and the number of topLevel
1190 * in the tree. 0 means only the level 0 tnode is in the tree.
1193 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
1194 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
1195 yaffs_FileStructure *fStruct,
1198 yaffs_Tnode *tn = fStruct->top;
1200 int requiredTallness;
1201 int level = fStruct->topLevel;
1205 /* Check sane level and chunk Id */
1206 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
1209 if (chunkId > YAFFS_MAX_CHUNK_ID)
1212 /* First check we're tall enough (ie enough topLevel) */
1214 i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1215 requiredTallness = 0;
1217 i >>= YAFFS_TNODES_INTERNAL_BITS;
1221 if (requiredTallness > fStruct->topLevel)
1222 return NULL; /* Not tall enough, so we can't find it */
1224 /* Traverse down to level 0 */
1225 while (level > 0 && tn) {
1226 tn = tn->internal[(chunkId >>
1227 (YAFFS_TNODES_LEVEL0_BITS +
1229 YAFFS_TNODES_INTERNAL_BITS)) &
1230 YAFFS_TNODES_INTERNAL_MASK];
1237 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
1238 * This happens in two steps:
1239 * 1. If the tree isn't tall enough, then make it taller.
1240 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
1242 * Used when modifying the tree.
1244 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
1245 * be plugged into the ttree.
1248 yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
1249 yaffs_FileStructure *fStruct,
1251 yaffs_Tnode *passedTn)
1253 int requiredTallness;
1261 /* Check sane level and page Id */
1262 if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
1265 if (chunkId > YAFFS_MAX_CHUNK_ID)
1268 /* First check we're tall enough (ie enough topLevel) */
1270 x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
1271 requiredTallness = 0;
1273 x >>= YAFFS_TNODES_INTERNAL_BITS;
1278 if (requiredTallness > fStruct->topLevel) {
1279 /* Not tall enough, gotta make the tree taller */
1280 for (i = fStruct->topLevel; i < requiredTallness; i++) {
1282 tn = yaffs_GetTnode(dev);
1285 tn->internal[0] = fStruct->top;
1287 fStruct->topLevel++;
1289 T(YAFFS_TRACE_ERROR,
1290 (TSTR("yaffs: no more tnodes" TENDSTR)));
1296 /* Traverse down to level 0, adding anything we need */
1298 l = fStruct->topLevel;
1302 while (l > 0 && tn) {
1304 (YAFFS_TNODES_LEVEL0_BITS +
1305 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
1306 YAFFS_TNODES_INTERNAL_MASK;
1309 if ((l > 1) && !tn->internal[x]) {
1310 /* Add missing non-level-zero tnode */
1311 tn->internal[x] = yaffs_GetTnode(dev);
1312 if(!tn->internal[x])
1314 } else if (l == 1) {
1315 /* Looking from level 1 at level 0 */
1317 /* If we already have one, then release it.*/
1318 if (tn->internal[x])
1319 yaffs_FreeTnode(dev, tn->internal[x]);
1320 tn->internal[x] = passedTn;
1322 } else if (!tn->internal[x]) {
1323 /* Don't have one, none passed in */
1324 tn->internal[x] = yaffs_GetTnode(dev);
1325 if(!tn->internal[x])
1330 tn = tn->internal[x];
1334 /* top is level 0 */
1336 memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
1337 yaffs_FreeTnode(dev, passedTn);
1344 static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
1345 yaffs_ExtendedTags *tags, int objectId,
1350 for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
1351 if (yaffs_CheckChunkBit(dev, theChunk / dev->param.nChunksPerBlock,
1352 theChunk % dev->param.nChunksPerBlock)) {
1354 if(dev->chunkGroupSize == 1)
1357 yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
1359 if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
1371 /* Experimental code not being used yet. Might speed up file deletion */
1372 /* DeleteWorker scans backwards through the tnode tree and deletes all the
1373 * chunks and tnodes in the file.
1374 * Returns 1 if the tree was deleted.
1375 * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1378 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
1379 int chunkOffset, int *limit)
1384 yaffs_ExtendedTags tags;
1386 yaffs_Device *dev = in->myDev;
1392 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1394 if (tn->internal[i]) {
1395 if (limit && (*limit) < 0) {
1399 yaffs_DeleteWorker(in,
1407 YAFFS_TNODES_INTERNAL_BITS)
1412 yaffs_FreeTnode(dev,
1415 tn->internal[i] = NULL;
1419 return (allDone) ? 1 : 0;
1420 } else if (level == 0) {
1423 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
1425 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
1428 chunkInInode = (chunkOffset <<
1429 YAFFS_TNODES_LEVEL0_BITS) + i;
1432 yaffs_FindChunkInGroup(dev,
1438 if (foundChunk > 0) {
1439 yaffs_DeleteChunk(dev,
1444 *limit = *limit - 1;
1451 yaffs_LoadLevel0Tnode(dev, tn, i, 0);
1455 return (i < 0) ? 1 : 0;
1467 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1469 yaffs_BlockInfo *theBlock;
1472 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1474 blockNo = chunk / dev->param.nChunksPerBlock;
1475 theBlock = yaffs_GetBlockInfo(dev, blockNo);
1477 theBlock->softDeletions++;
1479 yaffs2_UpdateOldestDirtySequence(dev, blockNo, theBlock);
1483 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1484 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1486 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1489 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
1490 __u32 level, int chunkOffset)
1495 yaffs_Device *dev = in->myDev;
1500 for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
1502 if (tn->internal[i]) {
1504 yaffs_SoftDeleteWorker(in,
1510 YAFFS_TNODES_INTERNAL_BITS)
1513 yaffs_FreeTnode(dev,
1516 tn->internal[i] = NULL;
1518 /* Hoosterman... how could this happen? */
1522 return (allDone) ? 1 : 0;
1523 } else if (level == 0) {
1525 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1526 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
1528 /* Note this does not find the real chunk, only the chunk group.
1529 * We make an assumption that a chunk group is not larger than
1532 yaffs_SoftDeleteChunk(dev, theChunk);
1533 yaffs_LoadLevel0Tnode(dev, tn, i, 0);
1547 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1550 obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
1551 if (obj->nDataChunks <= 0) {
1552 /* Empty file with no duplicate object headers, just delete it immediately */
1553 yaffs_FreeTnode(obj->myDev,
1554 obj->variant.fileVariant.top);
1555 obj->variant.fileVariant.top = NULL;
1556 T(YAFFS_TRACE_TRACING,
1557 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1559 yaffs_DoGenericObjectDeletion(obj);
1561 yaffs_SoftDeleteWorker(obj,
1562 obj->variant.fileVariant.top,
1563 obj->variant.fileVariant.
1565 obj->softDeleted = 1;
1570 /* Pruning removes any part of the file structure tree that is beyond the
1571 * bounds of the file (ie that does not point to chunks).
1573 * A file should only get pruned when its size is reduced.
1575 * Before pruning, the chunks must be pulled from the tree and the
1576 * level 0 tnode entries must be zeroed out.
1577 * Could also use this for file deletion, but that's probably better handled
1578 * by a special case.
1580 * This function is recursive. For levels > 0 the function is called again on
1581 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1582 * If there is no data in a subtree then it is pruned.
1585 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
1586 __u32 level, int del0)
1595 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1596 if (tn->internal[i]) {
1598 yaffs_PruneWorker(dev, tn->internal[i],
1600 (i == 0) ? del0 : 1);
1603 if (tn->internal[i])
1607 int tnodeSize_u32 = dev->tnodeSize/sizeof(__u32);
1608 __u32 *map = (__u32 *)tn;
1610 for(i = 0; !hasData && i < tnodeSize_u32; i++){
1616 if (hasData == 0 && del0) {
1617 /* Free and return NULL */
1619 yaffs_FreeTnode(dev, tn);
1629 static int yaffs_PruneFileStructure(yaffs_Device *dev,
1630 yaffs_FileStructure *fStruct)
1637 if (fStruct->topLevel > 0) {
1639 yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
1641 /* Now we have a tree with all the non-zero branches NULL but the height
1642 * is the same as it was.
1643 * Let's see if we can trim internal tnodes to shorten the tree.
1644 * We can do this if only the 0th element in the tnode is in use
1645 * (ie all the non-zero are NULL)
1648 while (fStruct->topLevel && !done) {
1652 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1653 if (tn->internal[i])
1658 fStruct->top = tn->internal[0];
1659 fStruct->topLevel--;
1660 yaffs_FreeTnode(dev, tn);
1670 /*-------------------- End of File Structure functions.-------------------*/
1673 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1674 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1676 yaffs_Object *obj = yaffs_AllocateRawObject(dev);
1681 /* Now sweeten it up... */
1683 memset(obj, 0, sizeof(yaffs_Object));
1684 obj->beingCreated = 1;
1688 obj->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1689 YINIT_LIST_HEAD(&(obj->hardLinks));
1690 YINIT_LIST_HEAD(&(obj->hashLink));
1691 YINIT_LIST_HEAD(&obj->siblings);
1694 /* Now make the directory sane */
1696 obj->parent = dev->rootDir;
1697 ylist_add(&(obj->siblings), &dev->rootDir->variant.directoryVariant.children);
1700 /* Add it to the lost and found directory.
1701 * NB Can't put root or lostNFound in lostNFound so
1702 * check if lostNFound exists first
1704 if (dev->lostNFoundDir)
1705 yaffs_AddObjectToDirectory(dev->lostNFoundDir, obj);
1707 obj->beingCreated = 0;
1710 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1715 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
1720 yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1722 obj->fake = 1; /* it is fake so it might have no NAND presence... */
1723 obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
1724 obj->unlinkAllowed = 0; /* ... or unlink it */
1727 obj->yst_mode = mode;
1729 obj->hdrChunk = 0; /* Not a valid chunk. */
1736 static void yaffs_UnhashObject(yaffs_Object *obj)
1739 yaffs_Device *dev = obj->myDev;
1741 /* If it is still linked into the bucket list, free from the list */
1742 if (!ylist_empty(&obj->hashLink)) {
1743 ylist_del_init(&obj->hashLink);
1744 bucket = yaffs_HashFunction(obj->objectId);
1745 dev->objectBucket[bucket].count--;
1749 /* FreeObject frees up a Object and puts it back on the free list */
1750 static void yaffs_FreeObject(yaffs_Object *obj)
1752 yaffs_Device *dev = obj->myDev;
1754 T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), obj, obj->myInode));
1760 if (!ylist_empty(&obj->siblings))
1765 /* We're still hooked up to a cached inode.
1766 * Don't delete now, but mark for later deletion
1768 obj->deferedFree = 1;
1772 yaffs_UnhashObject(obj);
1774 yaffs_FreeRawObject(dev,obj);
1776 dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
1780 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1782 if (obj->deferedFree)
1783 yaffs_FreeObject(obj);
1786 static void yaffs_InitialiseTnodesAndObjects(yaffs_Device *dev)
1793 yaffs_InitialiseRawTnodesAndObjects(dev);
1795 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1796 YINIT_LIST_HEAD(&dev->objectBucket[i].list);
1797 dev->objectBucket[i].count = 0;
1801 static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1805 int lowest = 999999;
1808 /* Search for the shortest list or one that
1812 for (i = 0; i < 10 && lowest > 4; i++) {
1813 dev->bucketFinder++;
1814 dev->bucketFinder %= YAFFS_NOBJECT_BUCKETS;
1815 if (dev->objectBucket[dev->bucketFinder].count < lowest) {
1816 lowest = dev->objectBucket[dev->bucketFinder].count;
1817 l = dev->bucketFinder;
1825 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1827 int bucket = yaffs_FindNiceObjectBucket(dev);
1829 /* Now find an object value that has not already been taken
1830 * by scanning the list.
1834 struct ylist_head *i;
1836 __u32 n = (__u32) bucket;
1838 /* yaffs_CheckObjectHashSanity(); */
1842 n += YAFFS_NOBJECT_BUCKETS;
1843 if (1 || dev->objectBucket[bucket].count > 0) {
1844 ylist_for_each(i, &dev->objectBucket[bucket].list) {
1845 /* If there is already one in the list */
1846 if (i && ylist_entry(i, yaffs_Object,
1847 hashLink)->objectId == n) {
1857 static void yaffs_HashObject(yaffs_Object *in)
1859 int bucket = yaffs_HashFunction(in->objectId);
1860 yaffs_Device *dev = in->myDev;
1862 ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
1863 dev->objectBucket[bucket].count++;
1866 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
1868 int bucket = yaffs_HashFunction(number);
1869 struct ylist_head *i;
1872 ylist_for_each(i, &dev->objectBucket[bucket].list) {
1873 /* Look if it is in the list */
1875 in = ylist_entry(i, yaffs_Object, hashLink);
1876 if (in->objectId == number) {
1878 /* Don't tell the VFS about this one if it is defered free */
1879 if (in->deferedFree)
1890 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
1891 yaffs_ObjectType type)
1893 yaffs_Object *theObject=NULL;
1894 yaffs_Tnode *tn = NULL;
1897 number = yaffs_CreateNewObjectNumber(dev);
1899 if (type == YAFFS_OBJECT_TYPE_FILE) {
1900 tn = yaffs_GetTnode(dev);
1905 theObject = yaffs_AllocateEmptyObject(dev);
1908 yaffs_FreeTnode(dev,tn);
1914 theObject->fake = 0;
1915 theObject->renameAllowed = 1;
1916 theObject->unlinkAllowed = 1;
1917 theObject->objectId = number;
1918 yaffs_HashObject(theObject);
1919 theObject->variantType = type;
1920 #ifdef CONFIG_YAFFS_WINCE
1921 yfsd_WinFileTimeNow(theObject->win_atime);
1922 theObject->win_ctime[0] = theObject->win_mtime[0] =
1923 theObject->win_atime[0];
1924 theObject->win_ctime[1] = theObject->win_mtime[1] =
1925 theObject->win_atime[1];
1929 theObject->yst_atime = theObject->yst_mtime =
1930 theObject->yst_ctime = Y_CURRENT_TIME;
1933 case YAFFS_OBJECT_TYPE_FILE:
1934 theObject->variant.fileVariant.fileSize = 0;
1935 theObject->variant.fileVariant.scannedFileSize = 0;
1936 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
1937 theObject->variant.fileVariant.topLevel = 0;
1938 theObject->variant.fileVariant.top = tn;
1940 case YAFFS_OBJECT_TYPE_DIRECTORY:
1941 YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
1943 YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
1946 case YAFFS_OBJECT_TYPE_SYMLINK:
1947 case YAFFS_OBJECT_TYPE_HARDLINK:
1948 case YAFFS_OBJECT_TYPE_SPECIAL:
1949 /* No action required */
1951 case YAFFS_OBJECT_TYPE_UNKNOWN:
1952 /* todo this should not happen */
1960 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
1962 yaffs_ObjectType type)
1964 yaffs_Object *theObject = NULL;
1967 theObject = yaffs_FindObjectByNumber(dev, number);
1970 theObject = yaffs_CreateNewObject(dev, number, type);
1977 YCHAR *yaffs_CloneString(const YCHAR *str)
1979 YCHAR *newStr = NULL;
1985 len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
1986 newStr = YMALLOC((len + 1) * sizeof(YCHAR));
1988 yaffs_strncpy(newStr, str,len);
1996 * Mknod (create) a new object.
1997 * equivalentObject only has meaning for a hard link;
1998 * aliasString only has meaning for a symlink.
1999 * rdev only has meaning for devices (a subset of special objects)
2002 static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
2003 yaffs_Object *parent,
2008 yaffs_Object *equivalentObject,
2009 const YCHAR *aliasString, __u32 rdev)
2014 yaffs_Device *dev = parent->myDev;
2016 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
2017 if (yaffs_FindObjectByName(parent, name))
2020 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2021 str = yaffs_CloneString(aliasString);
2026 in = yaffs_CreateNewObject(dev, -1, type);
2041 in->variantType = type;
2043 in->yst_mode = mode;
2045 #ifdef CONFIG_YAFFS_WINCE
2046 yfsd_WinFileTimeNow(in->win_atime);
2047 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
2048 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
2051 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
2053 in->yst_rdev = rdev;
2057 in->nDataChunks = 0;
2059 yaffs_SetObjectName(in, name);
2062 yaffs_AddObjectToDirectory(parent, in);
2064 in->myDev = parent->myDev;
2067 case YAFFS_OBJECT_TYPE_SYMLINK:
2068 in->variant.symLinkVariant.alias = str;
2070 case YAFFS_OBJECT_TYPE_HARDLINK:
2071 in->variant.hardLinkVariant.equivalentObject =
2073 in->variant.hardLinkVariant.equivalentObjectId =
2074 equivalentObject->objectId;
2075 ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
2077 case YAFFS_OBJECT_TYPE_FILE:
2078 case YAFFS_OBJECT_TYPE_DIRECTORY:
2079 case YAFFS_OBJECT_TYPE_SPECIAL:
2080 case YAFFS_OBJECT_TYPE_UNKNOWN:
2085 if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0, NULL) < 0) {
2086 /* Could not create the object header, fail the creation */
2087 yaffs_DeleteObject(in);
2091 yaffs_UpdateParent(parent);
2097 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
2098 __u32 mode, __u32 uid, __u32 gid)
2100 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2101 uid, gid, NULL, NULL, 0);
2104 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
2105 __u32 mode, __u32 uid, __u32 gid)
2107 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2108 mode, uid, gid, NULL, NULL, 0);
2111 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
2112 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
2114 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2115 uid, gid, NULL, NULL, rdev);
2118 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
2119 __u32 mode, __u32 uid, __u32 gid,
2122 return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2123 uid, gid, NULL, alias, 0);
2126 /* yaffs_Link returns the object id of the equivalent object.*/
2127 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
2128 yaffs_Object *equivalentObject)
2130 /* Get the real object in case we were fed a hard link as an equivalent object */
2131 equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
2133 if (yaffs_MknodObject
2134 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2135 equivalentObject, NULL, 0)) {
2136 return equivalentObject;
2143 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
2144 const YCHAR *newName, int force, int shadows)
2149 yaffs_Object *existingTarget;
2152 newDir = obj->parent; /* use the old directory */
2154 if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
2155 T(YAFFS_TRACE_ALWAYS,
2157 ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
2162 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
2163 if (obj->myDev->param.isYaffs2)
2164 unlinkOp = (newDir == obj->myDev->unlinkedDir);
2166 unlinkOp = (newDir == obj->myDev->unlinkedDir
2167 && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
2169 deleteOp = (newDir == obj->myDev->deletedDir);
2171 existingTarget = yaffs_FindObjectByName(newDir, newName);
2173 /* If the object is a file going into the unlinked directory,
2174 * then it is OK to just stuff it in since duplicate names are allowed.
2175 * else only proceed if the new name does not exist and if we're putting
2176 * it into a directory.
2183 newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
2184 yaffs_SetObjectName(obj, newName);
2187 yaffs_AddObjectToDirectory(newDir, obj);
2192 /* If it is a deletion then we mark it as a shrink for gc purposes. */
2193 if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows, NULL) >= 0)
2200 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
2201 yaffs_Object *newDir, const YCHAR *newName)
2203 yaffs_Object *obj = NULL;
2204 yaffs_Object *existingTarget = NULL;
2210 if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2212 if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
2215 dev = oldDir->myDev;
2217 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
2218 /* Special case for case insemsitive systems (eg. WinCE).
2219 * While look-up is case insensitive, the name isn't.
2220 * Therefore we might want to change x.txt to X.txt
2222 if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
2226 if(yaffs_strnlen(newName,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
2230 obj = yaffs_FindObjectByName(oldDir, oldName);
2232 if (obj && obj->renameAllowed) {
2234 /* Now do the handling for an existing target, if there is one */
2236 existingTarget = yaffs_FindObjectByName(newDir, newName);
2237 if (existingTarget &&
2238 existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2239 !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
2240 /* There is a target that is a non-empty directory, so we fail */
2241 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
2242 } else if (existingTarget && existingTarget != obj) {
2243 /* Nuke the target first, using shadowing,
2244 * but only if it isn't the same object.
2246 * Note we must disable gc otherwise it can mess up the shadowing.
2250 yaffs_ChangeObjectName(obj, newDir, newName, force,
2251 existingTarget->objectId);
2252 existingTarget->isShadowed = 1;
2253 yaffs_UnlinkObject(existingTarget);
2257 result = yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
2259 yaffs_UpdateParent(oldDir);
2260 if(newDir != oldDir)
2261 yaffs_UpdateParent(newDir);
2268 /*------------------------- Block Management and Page Allocation ----------------*/
2270 static int yaffs_InitialiseBlocks(yaffs_Device *dev)
2272 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
2274 dev->blockInfo = NULL;
2275 dev->chunkBits = NULL;
2277 dev->allocationBlock = -1; /* force it to get a new one */
2279 /* If the first allocation strategy fails, thry the alternate one */
2280 dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2281 if (!dev->blockInfo) {
2282 dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
2283 dev->blockInfoAlt = 1;
2285 dev->blockInfoAlt = 0;
2287 if (dev->blockInfo) {
2288 /* Set up dynamic blockinfo stuff. */
2289 dev->chunkBitmapStride = (dev->param.nChunksPerBlock + 7) / 8; /* round up bytes */
2290 dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2291 if (!dev->chunkBits) {
2292 dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
2293 dev->chunkBitsAlt = 1;
2295 dev->chunkBitsAlt = 0;
2298 if (dev->blockInfo && dev->chunkBits) {
2299 memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
2300 memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
2307 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2309 if (dev->blockInfoAlt && dev->blockInfo)
2310 YFREE_ALT(dev->blockInfo);
2311 else if (dev->blockInfo)
2312 YFREE(dev->blockInfo);
2314 dev->blockInfoAlt = 0;
2316 dev->blockInfo = NULL;
2318 if (dev->chunkBitsAlt && dev->chunkBits)
2319 YFREE_ALT(dev->chunkBits);
2320 else if (dev->chunkBits)
2321 YFREE(dev->chunkBits);
2322 dev->chunkBitsAlt = 0;
2323 dev->chunkBits = NULL;
2326 void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
2328 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
2332 /* If the block is still healthy erase it and mark as clean.
2333 * If the block has had a data failure, then retire it.
2336 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2337 (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
2338 blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
2340 yaffs2_ClearOldestDirtySequence(dev,bi);
2342 bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2344 /* If this is the block being garbage collected then stop gc'ing this block */
2345 if(blockNo == dev->gcBlock)
2348 /* If this block is currently the best candidate for gc then drop as a candidate */
2349 if(blockNo == dev->gcDirtiest){
2350 dev->gcDirtiest = 0;
2351 dev->gcPagesInUse = 0;
2354 if (!bi->needsRetiring) {
2355 yaffs2_InvalidateCheckpoint(dev);
2356 erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
2358 dev->nErasureFailures++;
2359 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2360 (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
2365 ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
2367 for (i = 0; i < dev->param.nChunksPerBlock; i++) {
2368 if (!yaffs_CheckChunkErased
2369 (dev, blockNo * dev->param.nChunksPerBlock + i)) {
2370 T(YAFFS_TRACE_ERROR,
2372 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2373 TENDSTR), blockNo, i));
2379 /* Clean it up... */
2380 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2381 bi->sequenceNumber = 0;
2382 dev->nErasedBlocks++;
2384 bi->softDeletions = 0;
2385 bi->hasShrinkHeader = 0;
2386 bi->skipErasedCheck = 1; /* This is clean, so no need to check */
2387 bi->gcPrioritise = 0;
2388 yaffs_ClearChunkBits(dev, blockNo);
2390 T(YAFFS_TRACE_ERASE,
2391 (TSTR("Erased block %d" TENDSTR), blockNo));
2393 dev->nFreeChunks -= dev->param.nChunksPerBlock; /* We lost a block of free space */
2395 yaffs_RetireBlock(dev, blockNo);
2396 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2397 (TSTR("**>> Block %d retired" TENDSTR), blockNo));
2401 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2405 yaffs_BlockInfo *bi;
2407 if (dev->nErasedBlocks < 1) {
2408 /* Hoosterman we've got a problem.
2409 * Can't get space to gc
2411 T(YAFFS_TRACE_ERROR,
2412 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
2417 /* Find an empty block. */
2419 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
2420 dev->allocationBlockFinder++;
2421 if (dev->allocationBlockFinder < dev->internalStartBlock
2422 || dev->allocationBlockFinder > dev->internalEndBlock) {
2423 dev->allocationBlockFinder = dev->internalStartBlock;
2426 bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
2428 if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
2429 bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2430 dev->sequenceNumber++;
2431 bi->sequenceNumber = dev->sequenceNumber;
2432 dev->nErasedBlocks--;
2433 T(YAFFS_TRACE_ALLOCATE,
2434 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
2435 dev->allocationBlockFinder, dev->sequenceNumber,
2436 dev->nErasedBlocks));
2437 return dev->allocationBlockFinder;
2441 T(YAFFS_TRACE_ALWAYS,
2443 ("yaffs tragedy: no more erased blocks, but there should have been %d"
2444 TENDSTR), dev->nErasedBlocks));
2451 * Check if there's space to allocate...
2452 * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
2454 int yaffs_CheckSpaceForAllocation(yaffs_Device *dev, int nChunks)
2457 int reservedBlocks = dev->param.nReservedBlocks;
2458 int checkpointBlocks;
2460 checkpointBlocks = yaffs2_CalcCheckpointBlocksRequired(dev);
2462 reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->param.nChunksPerBlock);
2464 return (dev->nFreeChunks > (reservedChunks + nChunks));
2467 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
2468 yaffs_BlockInfo **blockUsedPtr)
2471 yaffs_BlockInfo *bi;
2473 if (dev->allocationBlock < 0) {
2474 /* Get next block to allocate off */
2475 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2476 dev->allocationPage = 0;
2479 if (!useReserve && !yaffs_CheckSpaceForAllocation(dev, 1)) {
2480 /* Not enough space to allocate unless we're allowed to use the reserve. */
2484 if (dev->nErasedBlocks < dev->param.nReservedBlocks
2485 && dev->allocationPage == 0) {
2486 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
2489 /* Next page please.... */
2490 if (dev->allocationBlock >= 0) {
2491 bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
2493 retVal = (dev->allocationBlock * dev->param.nChunksPerBlock) +
2494 dev->allocationPage;
2496 yaffs_SetChunkBit(dev, dev->allocationBlock,
2497 dev->allocationPage);
2499 dev->allocationPage++;
2503 /* If the block is full set the state to full */
2504 if (dev->allocationPage >= dev->param.nChunksPerBlock) {
2505 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2506 dev->allocationBlock = -1;
2515 T(YAFFS_TRACE_ERROR,
2516 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2521 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2525 n = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2527 if (dev->allocationBlock > 0)
2528 n += (dev->param.nChunksPerBlock - dev->allocationPage);
2535 * yaffs_SkipRestOfBlock() skips over the rest of the allocation block
2536 * if we don't want to write to it.
2538 void yaffs_SkipRestOfBlock(yaffs_Device *dev)
2540 if(dev->allocationBlock > 0){
2541 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
2542 if(bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING){
2543 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2544 dev->allocationBlock = -1;
2550 static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
2556 int retVal = YAFFS_OK;
2559 int isCheckpointBlock;
2563 int chunksBefore = yaffs_GetErasedChunks(dev);
2566 yaffs_ExtendedTags tags;
2568 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
2570 yaffs_Object *object;
2572 isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
2575 T(YAFFS_TRACE_TRACING,
2576 (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
2579 bi->hasShrinkHeader,
2582 /*yaffs_VerifyFreeChunks(dev); */
2584 if(bi->blockState == YAFFS_BLOCK_STATE_FULL)
2585 bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2587 bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
2591 if (isCheckpointBlock ||
2592 !yaffs_StillSomeChunkBits(dev, block)) {
2593 T(YAFFS_TRACE_TRACING,
2595 ("Collecting block %d that has no chunks in use" TENDSTR),
2597 yaffs_BlockBecameDirty(dev, block);
2600 __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
2602 yaffs_VerifyBlock(dev, bi, block);
2604 maxCopies = (wholeBlock) ? dev->param.nChunksPerBlock : 5;
2605 oldChunk = block * dev->param.nChunksPerBlock + dev->gcChunk;
2607 for (/* init already done */;
2608 retVal == YAFFS_OK &&
2609 dev->gcChunk < dev->param.nChunksPerBlock &&
2610 (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
2612 dev->gcChunk++, oldChunk++) {
2613 if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
2615 /* This page is in use and might need to be copied off */
2621 yaffs_InitialiseTags(&tags);
2623 yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
2627 yaffs_FindObjectByNumber(dev,
2630 T(YAFFS_TRACE_GC_DETAIL,
2632 ("Collecting chunk in block %d, %d %d %d " TENDSTR),
2633 dev->gcChunk, tags.objectId, tags.chunkId,
2636 if (object && !yaffs_SkipVerification(dev)) {
2637 if (tags.chunkId == 0)
2638 matchingChunk = object->hdrChunk;
2639 else if (object->softDeleted)
2640 matchingChunk = oldChunk; /* Defeat the test */
2642 matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
2644 if (oldChunk != matchingChunk)
2645 T(YAFFS_TRACE_ERROR,
2646 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2647 oldChunk, matchingChunk, tags.objectId, tags.chunkId));
2652 T(YAFFS_TRACE_ERROR,
2654 ("page %d in gc has no object: %d %d %d "
2656 tags.objectId, tags.chunkId, tags.byteCount));
2661 object->softDeleted &&
2662 tags.chunkId != 0) {
2663 /* Data chunk in a soft deleted file, throw it away
2664 * It's a soft deleted data chunk,
2665 * No need to copy this, just forget about it and
2666 * fix up the object.
2669 /* Free chunks already includes softdeleted chunks.
2670 * How ever this chunk is going to soon be really deleted
2671 * which will increment free chunks.
2672 * We have to decrement free chunks so this works out properly.
2676 object->nDataChunks--;
2678 if (object->nDataChunks <= 0) {
2679 /* remeber to clean up the object */
2680 dev->gcCleanupList[cleanups] =
2686 /* Todo object && object->deleted && object->nDataChunks == 0 */
2687 /* Deleted object header with no data chunks.
2688 * Can be discarded and the file deleted.
2690 object->hdrChunk = 0;
2691 yaffs_FreeTnode(object->myDev,
2694 object->variant.fileVariant.top = NULL;
2695 yaffs_DoGenericObjectDeletion(object);
2697 } else if (object) {
2698 /* It's either a data chunk in a live file or
2699 * an ObjectHeader, so we're interested in it.
2700 * NB Need to keep the ObjectHeaders of deleted files
2701 * until the whole file has been deleted off
2703 tags.serialNumber++;
2707 if (tags.chunkId == 0) {
2708 /* It is an object Id,
2709 * We need to nuke the shrinkheader flags first
2710 * Also need to clean up shadowing.
2711 * We no longer want the shrinkHeader flag since its work is done
2712 * and if it is left in place it will mess up scanning.
2715 yaffs_ObjectHeader *oh;
2716 oh = (yaffs_ObjectHeader *)buffer;
2719 tags.extraIsShrinkHeader = 0;
2721 oh->shadowsObject = 0;
2722 oh->inbandShadowsObject = 0;
2723 tags.extraShadows = 0;
2725 /* Update file size */
2726 if(object->variantType == YAFFS_OBJECT_TYPE_FILE){
2727 oh->fileSize = object->variant.fileVariant.fileSize;
2728 tags.extraFileLength = oh->fileSize;
2731 yaffs_VerifyObjectHeader(object, oh, &tags, 1);
2733 yaffs_WriteNewChunkWithTagsToNAND(dev,(__u8 *) oh, &tags, 1);
2736 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
2739 retVal = YAFFS_FAIL;
2742 /* Ok, now fix up the Tnodes etc. */
2744 if (tags.chunkId == 0) {
2746 object->hdrChunk = newChunk;
2747 object->serial = tags.serialNumber;
2749 /* It's a data chunk */
2751 ok = yaffs_PutChunkIntoFile
2759 if (retVal == YAFFS_OK)
2760 yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
2765 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
2768 /* Do any required cleanups */
2769 for (i = 0; i < cleanups; i++) {
2770 /* Time to delete the file too */
2772 yaffs_FindObjectByNumber(dev,
2773 dev->gcCleanupList[i]);
2775 yaffs_FreeTnode(dev,
2776 object->variant.fileVariant.
2778 object->variant.fileVariant.top = NULL;
2781 ("yaffs: About to finally delete object %d"
2782 TENDSTR), object->objectId));
2783 yaffs_DoGenericObjectDeletion(object);
2784 object->myDev->nDeletedFiles--;
2791 yaffs_VerifyCollectedBlock(dev, bi, block);
2795 if (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
2797 * The gc did not complete. Set block state back to FULL
2798 * because checkpointing does not restore gc.
2800 bi->blockState = YAFFS_BLOCK_STATE_FULL;
2802 /* The gc completed. */
2803 chunksAfter = yaffs_GetErasedChunks(dev);
2804 if (chunksBefore >= chunksAfter) {
2807 ("gc did not increase free chunks before %d after %d"
2808 TENDSTR), chunksBefore, chunksAfter));
2820 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2821 * for garbage collection.
2824 static unsigned yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
2830 unsigned selected = 0;
2831 int prioritised = 0;
2832 int prioritisedExists = 0;
2833 yaffs_BlockInfo *bi;
2836 /* First let's see if we need to grab a prioritised block */
2837 if (dev->hasPendingPrioritisedGCs && !aggressive) {
2838 dev->gcDirtiest = 0;
2839 bi = dev->blockInfo;
2840 for (i = dev->internalStartBlock;
2841 i <= dev->internalEndBlock && !selected;
2844 if (bi->gcPrioritise) {
2845 prioritisedExists = 1;
2846 if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2847 yaffs2_BlockNotDisqualifiedFromGC(dev, bi)) {
2856 * If there is a prioritised block and none was selected then
2857 * this happened because there is at least one old dirty block gumming
2858 * up the works. Let's gc the oldest dirty block.
2861 if(prioritisedExists &&
2863 dev->oldestDirtyBlock > 0)
2864 selected = dev->oldestDirtyBlock;
2866 if (!prioritisedExists) /* None found, so we can clear this */
2867 dev->hasPendingPrioritisedGCs = 0;
2870 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2872 * else (we're doing a leasurely gc), then we only bother to do this if the
2873 * block has only a few pages in use.
2878 int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
2880 threshold = dev->param.nChunksPerBlock;
2881 iterations = nBlocks;
2883 int maxThreshold = dev->param.nChunksPerBlock/2;
2884 threshold = background ?
2885 (dev->gcNotDone + 2) * 2 : 0;
2886 if(threshold <YAFFS_GC_PASSIVE_THRESHOLD)
2887 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2888 if(threshold > maxThreshold)
2889 threshold = maxThreshold;
2891 iterations = nBlocks / 16 + 1;
2892 if (iterations > 100)
2898 (dev->gcDirtiest < 1 ||
2899 dev->gcPagesInUse > YAFFS_GC_GOOD_ENOUGH);
2901 dev->gcBlockFinder++;
2902 if (dev->gcBlockFinder < dev->internalStartBlock ||
2903 dev->gcBlockFinder > dev->internalEndBlock)
2904 dev->gcBlockFinder = dev->internalStartBlock;
2906 bi = yaffs_GetBlockInfo(dev, dev->gcBlockFinder);
2908 pagesUsed = bi->pagesInUse - bi->softDeletions;
2910 if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2911 pagesUsed < dev->param.nChunksPerBlock &&
2912 (dev->gcDirtiest < 1 || pagesUsed < dev->gcPagesInUse) &&
2913 yaffs2_BlockNotDisqualifiedFromGC(dev, bi)) {
2914 dev->gcDirtiest = dev->gcBlockFinder;
2915 dev->gcPagesInUse = pagesUsed;
2919 if(dev->gcDirtiest > 0 && dev->gcPagesInUse <= threshold)
2920 selected = dev->gcDirtiest;
2924 * If nothing has been selected for a while, try selecting the oldest dirty
2925 * because that's gumming up the works.
2928 if(!selected && dev->param.isYaffs2 &&
2929 dev->gcNotDone >= ( background ? 10 : 20)){
2930 yaffs2_FindOldestDirtySequence(dev);
2931 if(dev->oldestDirtyBlock > 0) {
2932 selected = dev->oldestDirtyBlock;
2933 dev->gcDirtiest = selected;
2934 dev->oldestDirtyGCs++;
2935 bi = yaffs_GetBlockInfo(dev, selected);
2936 dev->gcPagesInUse = bi->pagesInUse - bi->softDeletions;
2943 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR),
2945 dev->param.nChunksPerBlock - dev->gcPagesInUse,
2949 dev->backgroundGCs++;
2950 dev->gcDirtiest = 0;
2951 dev->gcPagesInUse = 0;
2953 if(dev->refreshSkip > 0)
2958 (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR),
2959 dev->gcBlockFinder, dev->gcNotDone,
2961 dev->gcDirtiest, dev->gcPagesInUse,
2962 dev->oldestDirtyBlock,
2963 background ? " bg" : ""));
2969 /* New garbage collector
2970 * If we're very low on erased blocks then we do aggressive garbage collection
2971 * otherwise we do "leasurely" garbage collection.
2972 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2973 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2975 * The idea is to help clear out space in a more spread-out manner.
2976 * Dunno if it really does anything useful.
2978 static int yaffs_CheckGarbageCollection(yaffs_Device *dev, int background)
2981 int gcOk = YAFFS_OK;
2987 int checkpointBlockAdjust;
2989 if(dev->param.gcControl &&
2990 (dev->param.gcControl(dev) & 1) == 0)
2993 if (dev->gcDisable) {
2994 /* Bail out so we don't get recursive gc */
2998 /* This loop should pass the first time.
2999 * We'll only see looping here if the collection does not increase space.
3005 checkpointBlockAdjust = yaffs2_CalcCheckpointBlocksRequired(dev);
3007 minErased = dev->param.nReservedBlocks + checkpointBlockAdjust + 1;
3008 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3010 /* If we need a block soon then do aggressive gc.*/
3011 if (dev->nErasedBlocks < minErased)
3014 if(dev->gcSkip > 20)
3016 if(erasedChunks < dev->nFreeChunks/2 ||
3028 /* If we don't already have a block being gc'd then see if we should start another */
3030 if (dev->gcBlock < 1 && !aggressive) {
3031 dev->gcBlock = yaffs2_FindRefreshBlock(dev);
3034 if (dev->gcBlock < 1) {
3035 dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive, background);
3039 if (dev->gcBlock > 0) {
3046 ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
3047 dev->nErasedBlocks, aggressive));
3049 gcOk = yaffs_GarbageCollectBlock(dev, dev->gcBlock, aggressive);
3052 if (dev->nErasedBlocks < (dev->param.nReservedBlocks) && dev->gcBlock > 0) {
3055 ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
3056 TENDSTR), dev->nErasedBlocks, maxTries, dev->gcBlock));
3058 } while ((dev->nErasedBlocks < dev->param.nReservedBlocks) &&
3059 (dev->gcBlock > 0) &&
3062 return aggressive ? gcOk : YAFFS_OK;
3066 * yaffs_BackgroundGarbageCollect()
3067 * Garbage collects. Intended to be called from a background thread.
3068 * Returns non-zero if at least half the free chunks are erased.
3070 int yaffs_BackgroundGarbageCollect(yaffs_Device *dev, unsigned urgency)
3072 int erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3074 T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency));
3076 yaffs_CheckGarbageCollection(dev, 1);
3077 return erasedChunks > dev->nFreeChunks/2;
3080 /*------------------------- TAGS --------------------------------*/
3082 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
3085 return (tags->chunkId == chunkInObject &&
3086 tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
3091 /*-------------------- Data file manipulation -----------------*/
3093 static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
3094 yaffs_ExtendedTags *tags)
3096 /*Get the Tnode, then get the level 0 offset chunk offset */
3099 yaffs_ExtendedTags localTags;
3102 yaffs_Device *dev = in->myDev;
3105 /* Passed a NULL, so use our own tags space */
3109 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3112 theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
3115 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3121 static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
3122 yaffs_ExtendedTags *tags)
3124 /* Get the Tnode, then get the level 0 offset chunk offset */
3127 yaffs_ExtendedTags localTags;
3129 yaffs_Device *dev = in->myDev;
3133 /* Passed a NULL, so use our own tags space */
3137 tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
3141 theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
3144 yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
3147 /* Delete the entry in the filestructure (if found) */
3149 yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, 0);
3155 #ifdef YAFFS_PARANOID
3157 static int yaffs_CheckFileSanity(yaffs_Object *in)
3165 yaffs_Tags localTags;
3166 yaffs_Tags *tags = &localTags;
3170 if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
3173 objId = in->objectId;
3174 fSize = in->variant.fileVariant.fileSize;
3176 (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
3178 for (chunk = 1; chunk <= nChunks; chunk++) {
3179 tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
3184 theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
3186 if (yaffs_CheckChunkBits
3187 (dev, theChunk / dev->param.nChunksPerBlock,
3188 theChunk % dev->param.nChunksPerBlock)) {
3190 yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
3194 (tags, in->objectId, chunk, chunkDeleted)) {
3204 /* T(("No level 0 found for %d\n", chunk)); */
3208 return failed ? YAFFS_FAIL : YAFFS_OK;
3213 int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
3214 int chunkInNAND, int inScan)
3216 /* NB inScan is zero unless scanning.
3217 * For forward scanning, inScan is > 0;
3218 * for backward scanning inScan is < 0
3220 * chunkInNAND = 0 is a dummy insert to make sure the tnodes are there.
3224 yaffs_Device *dev = in->myDev;
3226 yaffs_ExtendedTags existingTags;
3227 yaffs_ExtendedTags newTags;
3228 unsigned existingSerial, newSerial;
3230 if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
3231 /* Just ignore an attempt at putting a chunk into a non-file during scanning
3232 * If it is not during Scanning then something went wrong!
3235 T(YAFFS_TRACE_ERROR,
3237 ("yaffs tragedy:attempt to put data chunk into a non-file"
3242 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
3246 tn = yaffs_AddOrFindLevel0Tnode(dev,
3247 &in->variant.fileVariant,
3254 /* Dummy insert, bail now */
3257 existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
3260 /* If we're scanning then we need to test for duplicates
3261 * NB This does not need to be efficient since it should only ever
3262 * happen when the power fails during a write, then only one
3263 * chunk should ever be affected.
3265 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3266 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3269 if (existingChunk > 0) {
3270 /* NB Right now existing chunk will not be real chunkId if the chunk group size > 1
3271 * thus we have to do a FindChunkInFile to get the real chunk id.
3273 * We have a duplicate now we need to decide which one to use:
3275 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3276 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
3277 * YAFFS1: Get both sets of tags and compare serial numbers.
3281 /* Only do this for forward scanning */
3282 yaffs_ReadChunkWithTagsFromNAND(dev,
3286 /* Do a proper find */
3288 yaffs_FindChunkInFile(in, chunkInInode,
3292 if (existingChunk <= 0) {
3293 /*Hoosterman - how did this happen? */
3295 T(YAFFS_TRACE_ERROR,
3297 ("yaffs tragedy: existing chunk < 0 in scan"
3302 /* NB The deleted flags should be false, otherwise the chunks will
3303 * not be loaded during a scan
3307 newSerial = newTags.serialNumber;
3308 existingSerial = existingTags.serialNumber;
3312 (existingChunk <= 0 ||
3313 ((existingSerial + 1) & 3) == newSerial)) {
3314 /* Forward scanning.
3316 * Delete the old one and drop through to update the tnode
3318 yaffs_DeleteChunk(dev, existingChunk, 1,
3321 /* Backward scanning or we want to use the existing one
3323 * Delete the new one and return early so that the tnode isn't changed
3325 yaffs_DeleteChunk(dev, chunkInNAND, 1,
3333 if (existingChunk == 0)
3336 yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
3341 static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
3344 int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
3346 if (chunkInNAND >= 0)
3347 return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
3350 T(YAFFS_TRACE_NANDACCESS,
3351 (TSTR("Chunk %d not found zero instead" TENDSTR),
3353 /* get sane (zero) data if you read a hole */
3354 memset(buffer, 0, in->myDev->nDataBytesPerChunk);
3360 void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
3364 yaffs_ExtendedTags tags;
3365 yaffs_BlockInfo *bi;
3371 block = chunkId / dev->param.nChunksPerBlock;
3372 page = chunkId % dev->param.nChunksPerBlock;
3375 if (!yaffs_CheckChunkBit(dev, block, page))
3376 T(YAFFS_TRACE_VERIFY,
3377 (TSTR("Deleting invalid chunk %d"TENDSTR),
3380 bi = yaffs_GetBlockInfo(dev, block);
3382 yaffs2_UpdateOldestDirtySequence(dev, block, bi);
3384 T(YAFFS_TRACE_DELETION,
3385 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
3387 if (!dev->param.isYaffs2 && markNAND &&
3388 bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) {
3390 yaffs_InitialiseTags(&tags);
3392 tags.chunkDeleted = 1;
3394 yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
3395 yaffs_HandleUpdateChunk(dev, chunkId, &tags);
3397 dev->nUnmarkedDeletions++;
3400 /* Pull out of the management area.
3401 * If the whole block became dirty, this will kick off an erasure.
3403 if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3404 bi->blockState == YAFFS_BLOCK_STATE_FULL ||
3405 bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3406 bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
3409 yaffs_ClearChunkBit(dev, block, page);
3413 if (bi->pagesInUse == 0 &&
3414 !bi->hasShrinkHeader &&
3415 bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3416 bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3417 yaffs_BlockBecameDirty(dev, block);
3424 static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
3425 const __u8 *buffer, int nBytes,
3428 /* Find old chunk Need to do this to get serial number
3429 * Write new one and patch into tree.
3430 * Invalidate old tags.
3434 yaffs_ExtendedTags prevTags;
3437 yaffs_ExtendedTags newTags;
3439 yaffs_Device *dev = in->myDev;
3441 yaffs_CheckGarbageCollection(dev,0);
3443 /* Get the previous chunk at this location in the file if it exists.
3444 * If it does not exist then put a zero into the tree. This creates
3445 * the tnode now, rather than later when it is harder to clean up.
3447 prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
3448 if(prevChunkId < 1 &&
3449 !yaffs_PutChunkIntoFile(in, chunkInInode, 0, 0))
3452 /* Set up new tags */
3453 yaffs_InitialiseTags(&newTags);
3455 newTags.chunkId = chunkInInode;
3456 newTags.objectId = in->objectId;
3457 newTags.serialNumber =
3458 (prevChunkId > 0) ? prevTags.serialNumber + 1 : 1;
3459 newTags.byteCount = nBytes;
3461 if (nBytes < 1 || nBytes > dev->param.totalBytesPerChunk) {
3462 T(YAFFS_TRACE_ERROR,
3463 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
3469 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3472 if (newChunkId > 0) {
3473 yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
3475 if (prevChunkId > 0)
3476 yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
3478 yaffs_CheckFileSanity(in);
3484 /* UpdateObjectHeader updates the header on NAND for an object.
3485 * If name is not NULL, then that new name is used.
3487 int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
3488 int isShrink, int shadows, yaffs_XAttrMod *xmod)
3491 yaffs_BlockInfo *bi;
3493 yaffs_Device *dev = in->myDev;
3500 yaffs_ExtendedTags newTags;
3501 yaffs_ExtendedTags oldTags;
3502 YCHAR *alias = NULL;
3504 __u8 *buffer = NULL;
3505 YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
3507 yaffs_ObjectHeader *oh = NULL;
3509 yaffs_strcpy(oldName, _Y("silly old name"));
3513 in == dev->rootDir || /* The rootDir should also be saved */
3516 yaffs_CheckGarbageCollection(dev,0);
3517 yaffs_CheckObjectDetailsLoaded(in);
3519 buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
3520 oh = (yaffs_ObjectHeader *) buffer;
3522 prevChunkId = in->hdrChunk;
3524 if (prevChunkId > 0) {
3525 result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
3528 yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
3530 memcpy(oldName, oh->name, sizeof(oh->name));
3531 memset(buffer, 0xFF, sizeof(yaffs_ObjectHeader));
3533 memset(buffer, 0xFF, dev->nDataBytesPerChunk);
3535 oh->type = in->variantType;
3536 oh->yst_mode = in->yst_mode;
3537 oh->shadowsObject = oh->inbandShadowsObject = shadows;
3539 #ifdef CONFIG_YAFFS_WINCE
3540 oh->win_atime[0] = in->win_atime[0];
3541 oh->win_ctime[0] = in->win_ctime[0];
3542 oh->win_mtime[0] = in->win_mtime[0];
3543 oh->win_atime[1] = in->win_atime[1];
3544 oh->win_ctime[1] = in->win_ctime[1];
3545 oh->win_mtime[1] = in->win_mtime[1];
3547 oh->yst_uid = in->yst_uid;
3548 oh->yst_gid = in->yst_gid;
3549 oh->yst_atime = in->yst_atime;
3550 oh->yst_mtime = in->yst_mtime;
3551 oh->yst_ctime = in->yst_ctime;
3552 oh->yst_rdev = in->yst_rdev;
3555 oh->parentObjectId = in->parent->objectId;
3557 oh->parentObjectId = 0;
3559 if (name && *name) {
3560 memset(oh->name, 0, sizeof(oh->name));
3561 yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
3562 } else if (prevChunkId > 0)
3563 memcpy(oh->name, oldName, sizeof(oh->name));
3565 memset(oh->name, 0, sizeof(oh->name));
3567 oh->isShrink = isShrink;
3569 switch (in->variantType) {
3570 case YAFFS_OBJECT_TYPE_UNKNOWN:
3571 /* Should not happen */
3573 case YAFFS_OBJECT_TYPE_FILE:
3575 (oh->parentObjectId == YAFFS_OBJECTID_DELETED
3576 || oh->parentObjectId ==
3577 YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
3578 fileVariant.fileSize;
3580 case YAFFS_OBJECT_TYPE_HARDLINK:
3581 oh->equivalentObjectId =
3582 in->variant.hardLinkVariant.equivalentObjectId;
3584 case YAFFS_OBJECT_TYPE_SPECIAL:
3587 case YAFFS_OBJECT_TYPE_DIRECTORY:
3590 case YAFFS_OBJECT_TYPE_SYMLINK:
3591 alias = in->variant.symLinkVariant.alias;
3593 alias = _Y("no alias");
3594 yaffs_strncpy(oh->alias,
3596 YAFFS_MAX_ALIAS_LENGTH);
3597 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3601 /* process any xattrib modifications */
3603 yaffs_ApplyXMod(dev, (char *)buffer, xmod);
3607 yaffs_InitialiseTags(&newTags);
3609 newTags.chunkId = 0;
3610 newTags.objectId = in->objectId;
3611 newTags.serialNumber = in->serial;
3613 /* Add extra info for file header */
3615 newTags.extraHeaderInfoAvailable = 1;
3616 newTags.extraParentObjectId = oh->parentObjectId;
3617 newTags.extraFileLength = oh->fileSize;
3618 newTags.extraIsShrinkHeader = oh->isShrink;
3619 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3620 newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
3621 newTags.extraObjectType = in->variantType;
3623 yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
3625 /* Create new chunk in NAND */
3627 yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
3628 (prevChunkId > 0) ? 1 : 0);
3630 if (newChunkId >= 0) {
3632 in->hdrChunk = newChunkId;
3634 if (prevChunkId > 0) {
3635 yaffs_DeleteChunk(dev, prevChunkId, 1,
3639 if (!yaffs_ObjectHasCachedWriteData(in))
3642 /* If this was a shrink, then mark the block that the chunk lives on */
3644 bi = yaffs_GetBlockInfo(in->myDev,
3645 newChunkId / in->myDev->param.nChunksPerBlock);
3646 bi->hasShrinkHeader = 1;
3651 retVal = newChunkId;
3656 yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
3661 /*------------------------ Short Operations Cache ----------------------------------------
3662 * In many situations where there is no high level buffering (eg WinCE) a lot of
3663 * reads might be short sequential reads, and a lot of writes may be short
3664 * sequential writes. eg. scanning/writing a jpeg file.
3665 * In these cases, a short read/write cache can provide a huge perfomance benefit
3666 * with dumb-as-a-rock code.
3667 * In Linux, the page cache provides read buffering aand the short op cache provides write
3670 * There are a limited number (~10) of cache chunks per device so that we don't
3671 * need a very intelligent search.
3674 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
3676 yaffs_Device *dev = obj->myDev;
3678 yaffs_ChunkCache *cache;
3679 int nCaches = obj->myDev->param.nShortOpCaches;
3681 for (i = 0; i < nCaches; i++) {
3682 cache = &dev->srCache[i];
3683 if (cache->object == obj &&
3692 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3694 yaffs_Device *dev = obj->myDev;
3695 int lowest = -99; /* Stop compiler whining. */
3697 yaffs_ChunkCache *cache;
3698 int chunkWritten = 0;
3699 int nCaches = obj->myDev->param.nShortOpCaches;
3705 /* Find the dirty cache for this object with the lowest chunk id. */
3706 for (i = 0; i < nCaches; i++) {
3707 if (dev->srCache[i].object == obj &&
3708 dev->srCache[i].dirty) {
3710 || dev->srCache[i].chunkId <
3712 cache = &dev->srCache[i];
3713 lowest = cache->chunkId;
3718 if (cache && !cache->locked) {
3719 /* Write it out and free it up */
3722 yaffs_WriteChunkDataToObject(cache->object,
3728 cache->object = NULL;
3731 } while (cache && chunkWritten > 0);
3734 /* Hoosterman, disk full while writing cache out. */
3735 T(YAFFS_TRACE_ERROR,
3736 (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3743 /*yaffs_FlushEntireDeviceCache(dev)
3748 void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
3751 int nCaches = dev->param.nShortOpCaches;
3754 /* Find a dirty object in the cache and flush it...
3755 * until there are no further dirty objects.
3759 for (i = 0; i < nCaches && !obj; i++) {
3760 if (dev->srCache[i].object &&
3761 dev->srCache[i].dirty)
3762 obj = dev->srCache[i].object;
3766 yaffs_FlushFilesChunkCache(obj);
3773 /* Grab us a cache chunk for use.
3774 * First look for an empty one.
3775 * Then look for the least recently used non-dirty one.
3776 * Then look for the least recently used dirty one...., flush and look again.
3778 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3782 if (dev->param.nShortOpCaches > 0) {
3783 for (i = 0; i < dev->param.nShortOpCaches; i++) {
3784 if (!dev->srCache[i].object)
3785 return &dev->srCache[i];
3792 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3794 yaffs_ChunkCache *cache;
3795 yaffs_Object *theObj;
3800 if (dev->param.nShortOpCaches > 0) {
3801 /* Try find a non-dirty one... */
3803 cache = yaffs_GrabChunkCacheWorker(dev);
3806 /* They were all dirty, find the last recently used object and flush
3807 * its cache, then find again.
3808 * NB what's here is not very accurate, we actually flush the object
3809 * the last recently used page.
3812 /* With locking we can't assume we can use entry zero */
3819 for (i = 0; i < dev->param.nShortOpCaches; i++) {
3820 if (dev->srCache[i].object &&
3821 !dev->srCache[i].locked &&
3822 (dev->srCache[i].lastUse < usage || !cache)) {
3823 usage = dev->srCache[i].lastUse;
3824 theObj = dev->srCache[i].object;
3825 cache = &dev->srCache[i];
3830 if (!cache || cache->dirty) {
3831 /* Flush and try again */
3832 yaffs_FlushFilesChunkCache(theObj);
3833 cache = yaffs_GrabChunkCacheWorker(dev);
3843 /* Find a cached chunk */
3844 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
3847 yaffs_Device *dev = obj->myDev;
3849 if (dev->param.nShortOpCaches > 0) {
3850 for (i = 0; i < dev->param.nShortOpCaches; i++) {
3851 if (dev->srCache[i].object == obj &&
3852 dev->srCache[i].chunkId == chunkId) {
3855 return &dev->srCache[i];
3862 /* Mark the chunk for the least recently used algorithym */
3863 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
3867 if (dev->param.nShortOpCaches > 0) {
3868 if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
3869 /* Reset the cache usages */
3871 for (i = 1; i < dev->param.nShortOpCaches; i++)
3872 dev->srCache[i].lastUse = 0;
3879 cache->lastUse = dev->srLastUse;
3886 /* Invalidate a single cache page.
3887 * Do this when a whole page gets written,
3888 * ie the short cache for this page is no longer valid.
3890 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3892 if (object->myDev->param.nShortOpCaches > 0) {
3893 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
3896 cache->object = NULL;
3900 /* Invalidate all the cache pages associated with this object
3901 * Do this whenever ther file is deleted or resized.
3903 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3906 yaffs_Device *dev = in->myDev;
3908 if (dev->param.nShortOpCaches > 0) {
3909 /* Invalidate it. */
3910 for (i = 0; i < dev->param.nShortOpCaches; i++) {
3911 if (dev->srCache[i].object == in)
3912 dev->srCache[i].object = NULL;
3918 /*--------------------- File read/write ------------------------
3919 * Read and write have very similar structures.
3920 * In general the read/write has three parts to it
3921 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3922 * Some complete chunks
3923 * An incomplete chunk to end off with
3925 * Curve-balls: the first chunk might also be the last chunk.
3928 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
3937 yaffs_ChunkCache *cache;
3944 /* chunk = offset / dev->nDataBytesPerChunk + 1; */
3945 /* start = offset % dev->nDataBytesPerChunk; */
3946 yaffs_AddrToChunk(dev, offset, &chunk, &start);
3949 /* OK now check for the curveball where the start and end are in
3952 if ((start + n) < dev->nDataBytesPerChunk)
3955 nToCopy = dev->nDataBytesPerChunk - start;
3957 cache = yaffs_FindChunkCache(in, chunk);
3959 /* If the chunk is already in the cache or it is less than a whole chunk
3960 * or we're using inband tags then use the cache (if there is caching)
3961 * else bypass the cache.
3963 if (cache || nToCopy != dev->nDataBytesPerChunk || dev->param.inbandTags) {
3964 if (dev->param.nShortOpCaches > 0) {
3966 /* If we can't find the data in the cache, then load it up. */
3969 cache = yaffs_GrabChunkCache(in->myDev);
3971 cache->chunkId = chunk;
3974 yaffs_ReadChunkDataFromObject(in, chunk,
3980 yaffs_UseChunkCache(dev, cache, 0);
3985 memcpy(buffer, &cache->data[start], nToCopy);
3989 /* Read into the local buffer then copy..*/
3992 yaffs_GetTempBuffer(dev, __LINE__);
3993 yaffs_ReadChunkDataFromObject(in, chunk,
3996 memcpy(buffer, &localBuffer[start], nToCopy);
3999 yaffs_ReleaseTempBuffer(dev, localBuffer,
4005 /* A full chunk. Read directly into the supplied buffer. */
4006 yaffs_ReadChunkDataFromObject(in, chunk, buffer);
4020 int yaffs_DoWriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
4021 int nBytes, int writeThrough)
4030 int startOfWrite = offset;
4031 int chunkWritten = 0;
4039 while (n > 0 && chunkWritten >= 0) {
4040 yaffs_AddrToChunk(dev, offset, &chunk, &start);
4042 if (chunk * dev->nDataBytesPerChunk + start != offset ||
4043 start >= dev->nDataBytesPerChunk) {
4044 T(YAFFS_TRACE_ERROR, (
4045 TSTR("AddrToChunk of offset %d gives chunk %d start %d"
4047 (int)offset, chunk, start));
4049 chunk++; /* File pos to chunk in file offset */
4051 /* OK now check for the curveball where the start and end are in
4055 if ((start + n) < dev->nDataBytesPerChunk) {
4058 /* Now folks, to calculate how many bytes to write back....
4059 * If we're overwriting and not writing to then end of file then
4060 * we need to write back as much as was there before.
4063 chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
4065 if (chunkStart > in->variant.fileVariant.fileSize)
4066 nBytesRead = 0; /* Past end of file */
4068 nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
4070 if (nBytesRead > dev->nDataBytesPerChunk)
4071 nBytesRead = dev->nDataBytesPerChunk;
4075 (start + n)) ? nBytesRead : (start + n);
4077 if (nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
4081 nToCopy = dev->nDataBytesPerChunk - start;
4082 nToWriteBack = dev->nDataBytesPerChunk;
4085 if (nToCopy != dev->nDataBytesPerChunk || dev->param.inbandTags) {
4086 /* An incomplete start or end chunk (or maybe both start and end chunk),
4087 * or we're using inband tags, so we want to use the cache buffers.
4089 if (dev->param.nShortOpCaches > 0) {
4090 yaffs_ChunkCache *cache;
4091 /* If we can't find the data in the cache, then load the cache */
4092 cache = yaffs_FindChunkCache(in, chunk);
4095 && yaffs_CheckSpaceForAllocation(dev, 1)) {
4096 cache = yaffs_GrabChunkCache(dev);
4098 cache->chunkId = chunk;
4101 yaffs_ReadChunkDataFromObject(in, chunk,
4105 !yaffs_CheckSpaceForAllocation(dev, 1)) {
4106 /* Drop the cache if it was a read cache item and
4107 * no space check has been made for it.
4113 yaffs_UseChunkCache(dev, cache, 1);
4117 memcpy(&cache->data[start], buffer,
4122 cache->nBytes = nToWriteBack;
4126 yaffs_WriteChunkDataToObject
4129 cache->data, cache->nBytes,
4135 chunkWritten = -1; /* fail the write */
4138 /* An incomplete start or end chunk (or maybe both start and end chunk)
4139 * Read into the local buffer then copy, then copy over and write back.
4143 yaffs_GetTempBuffer(dev, __LINE__);
4145 yaffs_ReadChunkDataFromObject(in, chunk,
4150 memcpy(&localBuffer[start], buffer, nToCopy);
4153 yaffs_WriteChunkDataToObject(in, chunk,
4158 yaffs_ReleaseTempBuffer(dev, localBuffer,
4164 /* A full chunk. Write directly from the supplied buffer. */
4169 yaffs_WriteChunkDataToObject(in, chunk, buffer,
4170 dev->nDataBytesPerChunk,
4173 /* Since we've overwritten the cached data, we better invalidate it. */
4174 yaffs_InvalidateChunkCache(in, chunk);
4177 if (chunkWritten >= 0) {
4186 /* Update file object */
4188 if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
4189 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
4196 int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
4197 int nBytes, int writeThrough)
4199 yaffs2_HandleHole(in,offset);
4200 return yaffs_DoWriteDataToFile(in,buffer,offset,nBytes,writeThrough);
4205 /* ---------------------- File resizing stuff ------------------ */
4207 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
4210 yaffs_Device *dev = in->myDev;
4211 int oldFileSize = in->variant.fileVariant.fileSize;
4213 int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
4215 int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
4216 dev->nDataBytesPerChunk;
4220 /* Delete backwards so that we don't end up with holes if
4221 * power is lost part-way through the operation.
4223 for (i = lastDel; i >= startDel; i--) {
4224 /* NB this could be optimised somewhat,
4225 * eg. could retrieve the tags and write them without
4226 * using yaffs_DeleteChunk
4229 chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
4232 (dev->internalStartBlock * dev->param.nChunksPerBlock)
4234 ((dev->internalEndBlock +
4235 1) * dev->param.nChunksPerBlock)) {
4236 T(YAFFS_TRACE_ALWAYS,
4237 (TSTR("Found daft chunkId %d for %d" TENDSTR),
4241 yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
4249 void yaffs_ResizeDown( yaffs_Object *obj, loff_t newSize)
4252 __u32 newSizeOfPartialChunk;
4253 yaffs_Device *dev = obj->myDev;
4255 yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
4257 yaffs_PruneResizedChunks(obj, newSize);
4259 if (newSizeOfPartialChunk != 0) {
4260 int lastChunk = 1 + newFullChunks;
4261 __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
4263 /* Got to read and rewrite the last chunk with its new size and zero pad */
4264 yaffs_ReadChunkDataFromObject(obj, lastChunk, localBuffer);
4265 memset(localBuffer + newSizeOfPartialChunk, 0,
4266 dev->nDataBytesPerChunk - newSizeOfPartialChunk);
4268 yaffs_WriteChunkDataToObject(obj, lastChunk, localBuffer,
4269 newSizeOfPartialChunk, 1);
4271 yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
4274 obj->variant.fileVariant.fileSize = newSize;
4276 yaffs_PruneFileStructure(dev, &obj->variant.fileVariant);
4280 int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
4282 yaffs_Device *dev = in->myDev;
4283 int oldFileSize = in->variant.fileVariant.fileSize;
4285 yaffs_FlushFilesChunkCache(in);
4286 yaffs_InvalidateWholeChunkCache(in);
4288 yaffs_CheckGarbageCollection(dev,0);
4290 if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
4293 if (newSize == oldFileSize)
4296 if(newSize > oldFileSize){
4297 yaffs2_HandleHole(in,newSize);
4298 in->variant.fileVariant.fileSize = newSize;
4300 /* newSize < oldFileSize */
4301 yaffs_ResizeDown(in, newSize);
4304 /* Write a new object header to reflect the resize.
4305 * show we've shrunk the file, if need be
4306 * Do this only if the file is not in the deleted directories
4307 * and is not shadowed.
4311 in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
4312 in->parent->objectId != YAFFS_OBJECTID_DELETED)
4313 yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0, NULL);
4319 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4321 YCHAR *alias = NULL;
4322 obj = yaffs_GetEquivalentObject(obj);
4324 switch (obj->variantType) {
4325 case YAFFS_OBJECT_TYPE_FILE:
4326 return obj->variant.fileVariant.fileSize;
4327 case YAFFS_OBJECT_TYPE_SYMLINK:
4328 alias = obj->variant.symLinkVariant.alias;
4331 return yaffs_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH);
4339 int yaffs_FlushFile(yaffs_Object *in, int updateTime, int dataSync)
4343 yaffs_FlushFilesChunkCache(in);
4344 if(dataSync) /* Only sync data */
4348 #ifdef CONFIG_YAFFS_WINCE
4349 yfsd_WinFileTimeNow(in->win_mtime);
4352 in->yst_mtime = Y_CURRENT_TIME;
4357 retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0, NULL) >=
4358 0) ? YAFFS_OK : YAFFS_FAIL;
4368 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4371 /* First off, invalidate the file's data in the cache, without flushing. */
4372 yaffs_InvalidateWholeChunkCache(in);
4374 if (in->myDev->param.isYaffs2 && (in->parent != in->myDev->deletedDir)) {
4375 /* Move to the unlinked directory so we have a record that it was deleted. */
4376 yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0);
4380 yaffs_RemoveObjectFromDirectory(in);
4381 yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
4384 yaffs_FreeObject(in);
4389 /* yaffs_DeleteFile deletes the whole file data
4390 * and the inode associated with the file.
4391 * It does not delete the links associated with the file.
4393 static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
4397 int immediateDeletion = 0;
4398 yaffs_Device *dev = in->myDev;
4401 immediateDeletion = 1;
4403 if (immediateDeletion) {
4405 yaffs_ChangeObjectName(in, in->myDev->deletedDir,
4406 _Y("deleted"), 0, 0);
4407 T(YAFFS_TRACE_TRACING,
4408 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
4411 in->myDev->nDeletedFiles++;
4412 if (dev->param.disableSoftDelete || dev->param.isYaffs2)
4413 yaffs_ResizeFile(in, 0);
4414 yaffs_SoftDeleteFile(in);
4417 yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
4418 _Y("unlinked"), 0, 0);
4425 int yaffs_DeleteFile(yaffs_Object *in)
4427 int retVal = YAFFS_OK;
4428 int deleted; /* Need to cache value on stack if in is freed */
4429 yaffs_Device *dev = in->myDev;
4431 if (dev->param.disableSoftDelete || dev->param.isYaffs2)
4432 yaffs_ResizeFile(in, 0);
4434 if (in->nDataChunks > 0) {
4435 /* Use soft deletion if there is data in the file.
4436 * That won't be the case if it has been resized to zero.
4439 retVal = yaffs_UnlinkFileIfNeeded(in);
4441 deleted = in->deleted;
4443 if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
4446 in->myDev->nDeletedFiles++;
4447 yaffs_SoftDeleteFile(in);
4449 return deleted ? YAFFS_OK : YAFFS_FAIL;
4451 /* The file has no data chunks so we toss it immediately */
4452 yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
4453 in->variant.fileVariant.top = NULL;
4454 yaffs_DoGenericObjectDeletion(in);
4460 static int yaffs_IsNonEmptyDirectory(yaffs_Object *obj)
4462 return (obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) &&
4463 !(ylist_empty(&obj->variant.directoryVariant.children));
4466 static int yaffs_DeleteDirectory(yaffs_Object *obj)
4468 /* First check that the directory is empty. */
4469 if (yaffs_IsNonEmptyDirectory(obj))
4472 return yaffs_DoGenericObjectDeletion(obj);
4475 static int yaffs_DeleteSymLink(yaffs_Object *in)
4477 if(in->variant.symLinkVariant.alias)
4478 YFREE(in->variant.symLinkVariant.alias);
4479 in->variant.symLinkVariant.alias=NULL;
4481 return yaffs_DoGenericObjectDeletion(in);
4484 static int yaffs_DeleteHardLink(yaffs_Object *in)
4486 /* remove this hardlink from the list assocaited with the equivalent
4489 ylist_del_init(&in->hardLinks);
4490 return yaffs_DoGenericObjectDeletion(in);
4493 int yaffs_DeleteObject(yaffs_Object *obj)
4496 switch (obj->variantType) {
4497 case YAFFS_OBJECT_TYPE_FILE:
4498 retVal = yaffs_DeleteFile(obj);
4500 case YAFFS_OBJECT_TYPE_DIRECTORY:
4501 if(!ylist_empty(&obj->variant.directoryVariant.dirty)){
4502 T(YAFFS_TRACE_BACKGROUND, (TSTR("Remove object %d from dirty directories" TENDSTR),obj->objectId));
4503 ylist_del_init(&obj->variant.directoryVariant.dirty);
4505 return yaffs_DeleteDirectory(obj);
4507 case YAFFS_OBJECT_TYPE_SYMLINK:
4508 retVal = yaffs_DeleteSymLink(obj);
4510 case YAFFS_OBJECT_TYPE_HARDLINK:
4511 retVal = yaffs_DeleteHardLink(obj);
4513 case YAFFS_OBJECT_TYPE_SPECIAL:
4514 retVal = yaffs_DoGenericObjectDeletion(obj);
4516 case YAFFS_OBJECT_TYPE_UNKNOWN:
4518 break; /* should not happen. */
4524 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4527 int immediateDeletion = 0;
4530 immediateDeletion = 1;
4533 yaffs_UpdateParent(obj->parent);
4535 if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
4536 return yaffs_DeleteHardLink(obj);
4537 } else if (!ylist_empty(&obj->hardLinks)) {
4538 /* Curve ball: We're unlinking an object that has a hardlink.
4540 * This problem arises because we are not strictly following
4541 * The Linux link/inode model.
4543 * We can't really delete the object.
4544 * Instead, we do the following:
4545 * - Select a hardlink.
4546 * - Unhook it from the hard links
4547 * - Move it from its parent directory (so that the rename can work)
4548 * - Rename the object to the hardlink's name.
4549 * - Delete the hardlink
4553 yaffs_Object *parent;
4555 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4557 hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
4559 yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4560 parent = hl->parent;
4562 ylist_del_init(&hl->hardLinks);
4564 yaffs_AddObjectToDirectory(obj->myDev->unlinkedDir, hl);
4566 retVal = yaffs_ChangeObjectName(obj,parent, name, 0, 0);
4568 if (retVal == YAFFS_OK)
4569 retVal = yaffs_DoGenericObjectDeletion(hl);
4573 } else if (immediateDeletion) {
4574 switch (obj->variantType) {
4575 case YAFFS_OBJECT_TYPE_FILE:
4576 return yaffs_DeleteFile(obj);
4578 case YAFFS_OBJECT_TYPE_DIRECTORY:
4579 ylist_del_init(&obj->variant.directoryVariant.dirty);
4580 return yaffs_DeleteDirectory(obj);
4582 case YAFFS_OBJECT_TYPE_SYMLINK:
4583 return yaffs_DeleteSymLink(obj);
4585 case YAFFS_OBJECT_TYPE_SPECIAL:
4586 return yaffs_DoGenericObjectDeletion(obj);
4588 case YAFFS_OBJECT_TYPE_HARDLINK:
4589 case YAFFS_OBJECT_TYPE_UNKNOWN:
4593 } else if(yaffs_IsNonEmptyDirectory(obj))
4596 return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
4597 _Y("unlinked"), 0, 0);
4601 static int yaffs_UnlinkObject(yaffs_Object *obj)
4604 if (obj && obj->unlinkAllowed)
4605 return yaffs_UnlinkWorker(obj);
4610 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4614 obj = yaffs_FindObjectByName(dir, name);
4615 return yaffs_UnlinkObject(obj);
4618 /*----------------------- Initialisation Scanning ---------------------- */
4620 void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
4621 int backwardScanning)
4625 if (!backwardScanning) {
4626 /* Handle YAFFS1 forward scanning case
4627 * For YAFFS1 we always do the deletion
4631 /* Handle YAFFS2 case (backward scanning)
4632 * If the shadowed object exists then ignore.
4634 obj = yaffs_FindObjectByNumber(dev, objId);
4639 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4640 * We put it in unlinked dir to be cleaned up after the scanning
4643 yaffs_FindOrCreateObjectByNumber(dev, objId,
4644 YAFFS_OBJECT_TYPE_FILE);
4647 obj->isShadowed = 1;
4648 yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
4649 obj->variant.fileVariant.shrinkSize = 0;
4650 obj->valid = 1; /* So that we don't read any other info for this file */
4655 void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
4662 hardList = (yaffs_Object *) (hardList->hardLinks.next);
4664 in = yaffs_FindObjectByNumber(dev,
4665 hl->variant.hardLinkVariant.
4666 equivalentObjectId);
4669 /* Add the hardlink pointers */
4670 hl->variant.hardLinkVariant.equivalentObject = in;
4671 ylist_add(&hl->hardLinks, &in->hardLinks);
4673 /* Todo Need to report/handle this better.
4674 * Got a problem... hardlink to a non-existant object
4676 hl->variant.hardLinkVariant.equivalentObject = NULL;
4677 YINIT_LIST_HEAD(&hl->hardLinks);
4684 static void yaffs_StripDeletedObjects(yaffs_Device *dev)
4687 * Sort out state of unlinked and deleted objects after scanning.
4689 struct ylist_head *i;
4690 struct ylist_head *n;
4693 /* Soft delete all the unlinked files */
4694 ylist_for_each_safe(i, n,
4695 &dev->unlinkedDir->variant.directoryVariant.children) {
4697 l = ylist_entry(i, yaffs_Object, siblings);
4698 yaffs_DeleteObject(l);
4702 ylist_for_each_safe(i, n,
4703 &dev->deletedDir->variant.directoryVariant.children) {
4705 l = ylist_entry(i, yaffs_Object, siblings);
4706 yaffs_DeleteObject(l);
4713 * This code iterates through all the objects making sure that they are rooted.
4714 * Any unrooted objects are re-rooted in lost+found.
4715 * An object needs to be in one of:
4716 * - Directly under deleted, unlinked
4717 * - Directly or indirectly under root.
4720 * This code assumes that we don't ever change the current relationships between
4722 * rootDir->parent == unlinkedDir->parent == deletedDir->parent == NULL
4723 * lostNfound->parent == rootDir
4725 * This fixes the problem where directories might have inadvertently been deleted
4726 * leaving the object "hanging" without being rooted in the directory tree.
4729 static int yaffs_HasNULLParent(yaffs_Device *dev, yaffs_Object *obj)
4731 return (obj == dev->deletedDir ||
4732 obj == dev->unlinkedDir||
4733 obj == dev->rootDir);
4736 static void yaffs_FixHangingObjects(yaffs_Device *dev)
4739 yaffs_Object *parent;
4741 struct ylist_head *lh;
4742 struct ylist_head *n;
4747 /* Iterate through the objects in each hash entry,
4748 * looking at each object.
4749 * Make sure it is rooted.
4752 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4753 ylist_for_each_safe(lh, n, &dev->objectBucket[i].list) {
4755 obj = ylist_entry(lh, yaffs_Object, hashLink);
4756 parent= obj->parent;
4758 if(yaffs_HasNULLParent(dev,obj)){
4759 /* These directories are not hanging */
4762 else if(!parent || parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4764 else if(yaffs_HasNULLParent(dev,parent))
4768 * Need to follow the parent chain to see if it is hanging.
4773 while(parent != dev->rootDir &&
4775 parent->parent->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
4777 parent = parent->parent;
4780 if(parent != dev->rootDir)
4785 (TSTR("Hanging object %d moved to lost and found" TENDSTR),
4787 yaffs_AddObjectToDirectory(dev->lostNFoundDir,obj);
4796 * Delete directory contents for cleaning up lost and found.
4798 static void yaffs_DeleteDirectoryContents(yaffs_Object *dir)
4801 struct ylist_head *lh;
4802 struct ylist_head *n;
4804 if(dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4807 ylist_for_each_safe(lh, n, &dir->variant.directoryVariant.children) {
4809 obj = ylist_entry(lh, yaffs_Object, siblings);
4810 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
4811 yaffs_DeleteDirectoryContents(obj);
4814 (TSTR("Deleting lost_found object %d" TENDSTR),
4817 /* Need to use UnlinkObject since Delete would not handle
4818 * hardlinked objects correctly.
4820 yaffs_UnlinkObject(obj);
4826 static void yaffs_EmptyLostAndFound(yaffs_Device *dev)
4828 yaffs_DeleteDirectoryContents(dev->lostNFoundDir);
4831 static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
4834 yaffs_ObjectHeader *oh;
4836 yaffs_ExtendedTags tags;
4838 int alloc_failed = 0;
4846 T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
4848 in->lazyLoaded ? "not yet" : "already"));
4851 if (in->lazyLoaded && in->hdrChunk > 0) {
4853 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
4855 result = yaffs_ReadChunkWithTagsFromNAND(dev, in->hdrChunk, chunkData, &tags);
4856 oh = (yaffs_ObjectHeader *) chunkData;
4858 in->yst_mode = oh->yst_mode;
4859 #ifdef CONFIG_YAFFS_WINCE
4860 in->win_atime[0] = oh->win_atime[0];
4861 in->win_ctime[0] = oh->win_ctime[0];
4862 in->win_mtime[0] = oh->win_mtime[0];
4863 in->win_atime[1] = oh->win_atime[1];
4864 in->win_ctime[1] = oh->win_ctime[1];
4865 in->win_mtime[1] = oh->win_mtime[1];
4867 in->yst_uid = oh->yst_uid;
4868 in->yst_gid = oh->yst_gid;
4869 in->yst_atime = oh->yst_atime;
4870 in->yst_mtime = oh->yst_mtime;
4871 in->yst_ctime = oh->yst_ctime;
4872 in->yst_rdev = oh->yst_rdev;
4875 yaffs_SetObjectName(in, oh->name);
4877 if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
4878 in->variant.symLinkVariant.alias =
4879 yaffs_CloneString(oh->alias);
4880 if (!in->variant.symLinkVariant.alias)
4881 alloc_failed = 1; /* Not returned to caller */
4884 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
4888 /*------------------------------ Directory Functions ----------------------------- */
4890 static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
4892 struct ylist_head *lh;
4893 yaffs_Object *listObj;
4898 T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
4903 if (yaffs_SkipVerification(obj->myDev))
4907 T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
4912 if (obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
4913 T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
4917 /* Iterate through the objects in each hash entry */
4919 ylist_for_each(lh, &obj->parent->variant.directoryVariant.children) {
4921 listObj = ylist_entry(lh, yaffs_Object, siblings);
4922 yaffs_VerifyObject(listObj);
4929 T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
4934 static void yaffs_VerifyDirectory(yaffs_Object *directory)
4936 struct ylist_head *lh;
4937 yaffs_Object *listObj;
4944 if (yaffs_SkipFullVerification(directory->myDev))
4947 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
4948 T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variantType));
4952 /* Iterate through the objects in each hash entry */
4954 ylist_for_each(lh, &directory->variant.directoryVariant.children) {
4956 listObj = ylist_entry(lh, yaffs_Object, siblings);
4957 if (listObj->parent != directory) {
4958 T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
4961 yaffs_VerifyObjectInDirectory(listObj);
4967 *yaffs_UpdateParent() handles fixing a directories mtime and ctime when a new
4968 * link (ie. name) is created or deleted in the directory.
4971 * create dir/a : update dir's mtime/ctime
4972 * rm dir/a: update dir's mtime/ctime
4973 * modify dir/a: don't update dir's mtimme/ctime
4975 * This can be handled immediately or defered. Defering helps reduce the number
4976 * of updates when many files in a directory are changed within a brief period.
4978 * If the directory updating is defered then yaffs_UpdateDirtyDirecories must be
4979 * called periodically.
4982 static void yaffs_UpdateParent(yaffs_Object *obj)
4990 obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME;
4991 if(dev->param.deferDirectoryUpdate){
4992 struct ylist_head *link = &obj->variant.directoryVariant.dirty;
4994 if(ylist_empty(link)){
4995 ylist_add(link,&dev->dirtyDirectories);
4996 T(YAFFS_TRACE_BACKGROUND, (TSTR("Added object %d to dirty directories" TENDSTR),obj->objectId));
5000 yaffs_UpdateObjectHeader(obj, NULL, 0, 0, 0, NULL);
5003 void yaffs_UpdateDirtyDirectories(yaffs_Device *dev)
5005 struct ylist_head *link;
5007 yaffs_DirectoryStructure *dS;
5008 yaffs_ObjectVariant *oV;
5010 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
5012 while(!ylist_empty(&dev->dirtyDirectories)){
5013 link = dev->dirtyDirectories.next;
5014 ylist_del_init(link);
5016 dS=ylist_entry(link,yaffs_DirectoryStructure,dirty);
5017 oV = ylist_entry(dS,yaffs_ObjectVariant,directoryVariant);
5018 obj = ylist_entry(oV,yaffs_Object,variant);
5020 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update directory %d" TENDSTR), obj->objectId));
5023 yaffs_UpdateObjectHeader(obj, NULL, 0, 0, 0, NULL);
5027 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
5029 yaffs_Device *dev = obj->myDev;
5030 yaffs_Object *parent;
5032 yaffs_VerifyObjectInDirectory(obj);
5033 parent = obj->parent;
5035 yaffs_VerifyDirectory(parent);
5037 if (dev && dev->param.removeObjectCallback)
5038 dev->param.removeObjectCallback(obj);
5041 ylist_del_init(&obj->siblings);
5044 yaffs_VerifyDirectory(parent);
5047 void yaffs_AddObjectToDirectory(yaffs_Object *directory,
5051 T(YAFFS_TRACE_ALWAYS,
5053 ("tragedy: Trying to add an object to a null pointer directory"
5058 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
5059 T(YAFFS_TRACE_ALWAYS,
5061 ("tragedy: Trying to add an object to a non-directory"
5066 if (obj->siblings.prev == NULL) {
5067 /* Not initialised */
5072 yaffs_VerifyDirectory(directory);
5074 yaffs_RemoveObjectFromDirectory(obj);
5078 ylist_add(&obj->siblings, &directory->variant.directoryVariant.children);
5079 obj->parent = directory;
5081 if (directory == obj->myDev->unlinkedDir
5082 || directory == obj->myDev->deletedDir) {
5084 obj->myDev->nUnlinkedFiles++;
5085 obj->renameAllowed = 0;
5088 yaffs_VerifyDirectory(directory);
5089 yaffs_VerifyObjectInDirectory(obj);
5092 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
5097 struct ylist_head *i;
5098 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
5106 T(YAFFS_TRACE_ALWAYS,
5108 ("tragedy: yaffs_FindObjectByName: null pointer directory"
5113 if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
5114 T(YAFFS_TRACE_ALWAYS,
5116 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
5120 sum = yaffs_CalcNameSum(name);
5122 ylist_for_each(i, &directory->variant.directoryVariant.children) {
5124 l = ylist_entry(i, yaffs_Object, siblings);
5126 if (l->parent != directory)
5129 yaffs_CheckObjectDetailsLoaded(l);
5131 /* Special case for lost-n-found */
5132 if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
5133 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
5135 } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
5136 /* LostnFound chunk called Objxxx
5139 yaffs_GetObjectName(l, buffer,
5140 YAFFS_MAX_NAME_LENGTH + 1);
5141 if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
5152 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
5153 int (*fn) (yaffs_Object *))
5155 struct ylist_head *i;
5159 T(YAFFS_TRACE_ALWAYS,
5161 ("tragedy: yaffs_FindObjectByName: null pointer directory"
5166 if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
5167 T(YAFFS_TRACE_ALWAYS,
5169 ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
5174 ylist_for_each(i, &theDir->variant.directoryVariant.children) {
5176 l = ylist_entry(i, yaffs_Object, siblings);
5187 /* GetEquivalentObject dereferences any hard links to get to the
5191 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5193 if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
5194 /* We want the object id of the equivalent object, not this one */
5195 obj = obj->variant.hardLinkVariant.equivalentObject;
5196 yaffs_CheckObjectDetailsLoaded(obj);
5201 int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
5203 memset(name, 0, buffSize * sizeof(YCHAR));
5205 yaffs_CheckObjectDetailsLoaded(obj);
5207 if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
5208 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
5209 } else if (obj->hdrChunk <= 0) {
5211 YCHAR numString[20];
5212 YCHAR *x = &numString[19];
5213 unsigned v = obj->objectId;
5217 *x = '0' + (v % 10);
5220 /* make up a name */
5221 yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
5222 yaffs_strcat(locName, x);
5223 yaffs_strncpy(name, locName, buffSize - 1);
5226 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5227 else if (obj->shortName[0])
5228 yaffs_strncpy(name, obj->shortName,YAFFS_SHORT_NAME_LENGTH+1);
5232 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
5234 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
5236 memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
5238 if (obj->hdrChunk > 0) {
5239 result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
5240 obj->hdrChunk, buffer,
5243 yaffs_strncpy(name, oh->name, buffSize - 1);
5246 yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
5249 return yaffs_strnlen(name,buffSize-1);
5252 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5254 /* Dereference any hard linking */
5255 obj = yaffs_GetEquivalentObject(obj);
5257 if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5258 return obj->variant.fileVariant.fileSize;
5259 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
5260 if(!obj->variant.symLinkVariant.alias)
5262 return yaffs_strnlen(obj->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
5264 /* Only a directory should drop through to here */
5265 return obj->myDev->nDataBytesPerChunk;
5269 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5272 struct ylist_head *i;
5275 count++; /* the object itself */
5277 ylist_for_each(i, &obj->hardLinks)
5278 count++; /* add the hard links; */
5283 int yaffs_GetObjectInode(yaffs_Object *obj)
5285 obj = yaffs_GetEquivalentObject(obj);
5287 return obj->objectId;
5290 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5292 obj = yaffs_GetEquivalentObject(obj);
5294 switch (obj->variantType) {
5295 case YAFFS_OBJECT_TYPE_FILE:
5298 case YAFFS_OBJECT_TYPE_DIRECTORY:
5301 case YAFFS_OBJECT_TYPE_SYMLINK:
5304 case YAFFS_OBJECT_TYPE_HARDLINK:
5307 case YAFFS_OBJECT_TYPE_SPECIAL:
5308 if (S_ISFIFO(obj->yst_mode))
5310 if (S_ISCHR(obj->yst_mode))
5312 if (S_ISBLK(obj->yst_mode))
5314 if (S_ISSOCK(obj->yst_mode))
5322 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5324 obj = yaffs_GetEquivalentObject(obj);
5325 if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5326 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5328 return yaffs_CloneString(_Y(""));
5331 #ifndef CONFIG_YAFFS_WINCE
5333 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5335 unsigned int valid = attr->ia_valid;
5337 if (valid & ATTR_MODE)
5338 obj->yst_mode = attr->ia_mode;
5339 if (valid & ATTR_UID)
5340 obj->yst_uid = attr->ia_uid;
5341 if (valid & ATTR_GID)
5342 obj->yst_gid = attr->ia_gid;
5344 if (valid & ATTR_ATIME)
5345 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
5346 if (valid & ATTR_CTIME)
5347 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
5348 if (valid & ATTR_MTIME)
5349 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
5351 if (valid & ATTR_SIZE)
5352 yaffs_ResizeFile(obj, attr->ia_size);
5354 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0, NULL);
5359 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5361 unsigned int valid = 0;
5363 attr->ia_mode = obj->yst_mode;
5365 attr->ia_uid = obj->yst_uid;
5367 attr->ia_gid = obj->yst_gid;
5370 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
5371 valid |= ATTR_ATIME;
5372 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
5373 valid |= ATTR_CTIME;
5374 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
5375 valid |= ATTR_MTIME;
5377 attr->ia_size = yaffs_GetFileSize(obj);
5380 attr->ia_valid = valid;
5388 static int yaffs_DoXMod(yaffs_Object *obj, int set, const char *name, const void *value, int size, int flags)
5390 yaffs_XAttrMod xmod;
5399 xmod.result = -ENOSPC;
5401 result = yaffs_UpdateObjectHeader(obj, NULL, 0, 0, 0, &xmod);
5409 static int yaffs_ApplyXMod(yaffs_Device *dev, char *buffer, yaffs_XAttrMod *xmod)
5412 int x_offs = sizeof(yaffs_ObjectHeader);
5413 int x_size = dev->nDataBytesPerChunk - sizeof(yaffs_ObjectHeader);
5415 char * x_buffer = buffer + x_offs;
5418 retval = nval_set(x_buffer, x_size, xmod->name, xmod->data, xmod->size, xmod->flags);
5420 retval = nval_del(x_buffer, x_size, xmod->name);
5422 xmod->result = retval;
5427 static int yaffs_DoXFetch(yaffs_Object *obj, const char *name, void *value, int size)
5429 char *buffer = NULL;
5431 yaffs_ExtendedTags tags;
5432 yaffs_Device *dev = obj->myDev;
5433 int x_offs = sizeof(yaffs_ObjectHeader);
5434 int x_size = dev->nDataBytesPerChunk - sizeof(yaffs_ObjectHeader);
5440 if(obj->hdrChunk < 1)
5443 buffer = yaffs_GetTempBuffer(dev, __LINE__);
5447 result = yaffs_ReadChunkWithTagsFromNAND(dev,obj->hdrChunk, buffer, &tags);
5449 if(result != YAFFS_OK)
5452 x_buffer = buffer + x_offs;
5455 retval = nval_get(x_buffer, x_size, name, value, size);
5457 retval = nval_list(x_buffer, x_size, value,size);
5459 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
5463 int yaffs_SetXAttribute(yaffs_Object *obj, const char *name, const void * value, int size, int flags)
5465 return yaffs_DoXMod(obj, 1, name, value, size, flags);
5468 int yaffs_RemoveXAttribute(yaffs_Object *obj, const char *name)
5470 return yaffs_DoXMod(obj, 0, name, NULL, 0, 0);
5473 int yaffs_GetXAttribute(yaffs_Object *obj, const char *name, void *value, int size)
5475 return yaffs_DoXFetch(obj, name, value, size);
5478 int yaffs_ListXAttributes(yaffs_Object *obj, char *buffer, int size)
5480 return yaffs_DoXFetch(obj, NULL, buffer,size);
5486 int yaffs_DumpObject(yaffs_Object *obj)
5490 yaffs_GetObjectName(obj, name, YAFFS_MAX_NAME_LENGTH + 1);
5492 T(YAFFS_TRACE_ALWAYS,
5494 ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
5495 " chunk %d type %d size %d\n"
5496 TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
5497 obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdrChunk,
5498 yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5504 /*---------------------------- Initialisation code -------------------------------------- */
5506 static int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5509 /* Common functions, gotta have */
5510 if (!dev->param.eraseBlockInNAND || !dev->param.initialiseNAND)
5513 #ifdef CONFIG_YAFFS_YAFFS2
5515 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
5516 if (dev->param.writeChunkWithTagsToNAND &&
5517 dev->param.readChunkWithTagsFromNAND &&
5518 !dev->param.writeChunkToNAND &&
5519 !dev->param.readChunkFromNAND &&
5520 dev->param.markNANDBlockBad &&
5521 dev->param.queryNANDBlock)
5525 /* Can use the "spare" style interface for yaffs1 */
5526 if (!dev->param.isYaffs2 &&
5527 !dev->param.writeChunkWithTagsToNAND &&
5528 !dev->param.readChunkWithTagsFromNAND &&
5529 dev->param.writeChunkToNAND &&
5530 dev->param.readChunkFromNAND &&
5531 !dev->param.markNANDBlockBad &&
5532 !dev->param.queryNANDBlock)
5539 static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
5541 /* Initialise the unlinked, deleted, root and lost and found directories */
5543 dev->lostNFoundDir = dev->rootDir = NULL;
5544 dev->unlinkedDir = dev->deletedDir = NULL;
5547 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
5550 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
5553 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
5554 YAFFS_ROOT_MODE | S_IFDIR);
5555 dev->lostNFoundDir =
5556 yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
5557 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
5559 if (dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir) {
5560 yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
5567 int yaffs_GutsInitialise(yaffs_Device *dev)
5569 int init_failed = 0;
5573 T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
5575 /* Check stuff that must be set */
5578 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
5582 dev->internalStartBlock = dev->param.startBlock;
5583 dev->internalEndBlock = dev->param.endBlock;
5584 dev->blockOffset = 0;
5585 dev->chunkOffset = 0;
5586 dev->nFreeChunks = 0;
5590 if (dev->param.startBlock == 0) {
5591 dev->internalStartBlock = dev->param.startBlock + 1;
5592 dev->internalEndBlock = dev->param.endBlock + 1;
5593 dev->blockOffset = 1;
5594 dev->chunkOffset = dev->param.nChunksPerBlock;
5597 /* Check geometry parameters. */
5599 if ((!dev->param.inbandTags && dev->param.isYaffs2 && dev->param.totalBytesPerChunk < 1024) ||
5600 (!dev->param.isYaffs2 && dev->param.totalBytesPerChunk < 512) ||
5601 (dev->param.inbandTags && !dev->param.isYaffs2) ||
5602 dev->param.nChunksPerBlock < 2 ||
5603 dev->param.nReservedBlocks < 2 ||
5604 dev->internalStartBlock <= 0 ||
5605 dev->internalEndBlock <= 0 ||
5606 dev->internalEndBlock <= (dev->internalStartBlock + dev->param.nReservedBlocks + 2)) { /* otherwise it is too small */
5607 T(YAFFS_TRACE_ALWAYS,
5609 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d "
5610 TENDSTR), dev->param.totalBytesPerChunk, dev->param.isYaffs2 ? "2" : "", dev->param.inbandTags));
5614 if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
5615 T(YAFFS_TRACE_ALWAYS,
5616 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
5620 /* Sort out space for inband tags, if required */
5621 if (dev->param.inbandTags)
5622 dev->nDataBytesPerChunk = dev->param.totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart);
5624 dev->nDataBytesPerChunk = dev->param.totalBytesPerChunk;
5626 /* Got the right mix of functions? */
5627 if (!yaffs_CheckDevFunctions(dev)) {
5628 /* Function missing */
5629 T(YAFFS_TRACE_ALWAYS,
5631 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5636 /* This is really a compilation check. */
5637 if (!yaffs_CheckStructures()) {
5638 T(YAFFS_TRACE_ALWAYS,
5639 (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
5643 if (dev->isMounted) {
5644 T(YAFFS_TRACE_ALWAYS,
5645 (TSTR("yaffs: device already mounted\n" TENDSTR)));
5649 /* Finished with most checks. One or two more checks happen later on too. */
5653 /* OK now calculate a few things for the device */
5656 * Calculate all the chunk size manipulation numbers:
5658 x = dev->nDataBytesPerChunk;
5659 /* We always use dev->chunkShift and dev->chunkDiv */
5660 dev->chunkShift = Shifts(x);
5661 x >>= dev->chunkShift;
5663 /* We only use chunk mask if chunkDiv is 1 */
5664 dev->chunkMask = (1<<dev->chunkShift) - 1;
5667 * Calculate chunkGroupBits.
5668 * We need to find the next power of 2 > than internalEndBlock
5671 x = dev->param.nChunksPerBlock * (dev->internalEndBlock + 1);
5675 /* Set up tnode width if wide tnodes are enabled. */
5676 if (!dev->param.wideTnodesDisabled) {
5677 /* bits must be even so that we end up with 32-bit words */
5681 dev->tnodeWidth = 16;
5683 dev->tnodeWidth = bits;
5685 dev->tnodeWidth = 16;
5687 dev->tnodeMask = (1<<dev->tnodeWidth)-1;
5689 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
5690 * so if the bitwidth of the
5691 * chunk range we're using is greater than 16 we need
5692 * to figure out chunk shift and chunkGroupSize
5695 if (bits <= dev->tnodeWidth)
5696 dev->chunkGroupBits = 0;
5698 dev->chunkGroupBits = bits - dev->tnodeWidth;
5700 dev->tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
5701 if(dev->tnodeSize < sizeof(yaffs_Tnode))
5702 dev->tnodeSize = sizeof(yaffs_Tnode);
5704 dev->chunkGroupSize = 1 << dev->chunkGroupBits;
5706 if (dev->param.nChunksPerBlock < dev->chunkGroupSize) {
5707 /* We have a problem because the soft delete won't work if
5708 * the chunk group size > chunks per block.
5709 * This can be remedied by using larger "virtual blocks".
5711 T(YAFFS_TRACE_ALWAYS,
5712 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
5717 /* OK, we've finished verifying the device, lets continue with initialisation */
5719 /* More device initialisation */
5721 dev->passiveGCs = 0;
5722 dev->oldestDirtyGCs = 0;
5723 dev->backgroundGCs = 0;
5724 dev->gcBlockFinder = 0;
5725 dev->bufferedBlock = -1;
5726 dev->doingBufferedBlockRewrite = 0;
5727 dev->nDeletedFiles = 0;
5728 dev->nBackgroundDeletions = 0;
5729 dev->nUnlinkedFiles = 0;
5731 dev->eccUnfixed = 0;
5732 dev->tagsEccFixed = 0;
5733 dev->tagsEccUnfixed = 0;
5734 dev->nErasureFailures = 0;
5735 dev->nErasedBlocks = 0;
5737 dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
5738 YINIT_LIST_HEAD(&dev->dirtyDirectories);
5739 dev->oldestDirtySequence = 0;
5740 dev->oldestDirtyBlock = 0;
5742 /* Initialise temporary buffers and caches. */
5743 if (!yaffs_InitialiseTempBuffers(dev))
5746 dev->srCache = NULL;
5747 dev->gcCleanupList = NULL;
5751 dev->param.nShortOpCaches > 0) {
5754 int srCacheBytes = dev->param.nShortOpCaches * sizeof(yaffs_ChunkCache);
5756 if (dev->param.nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
5757 dev->param.nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
5759 dev->srCache = YMALLOC(srCacheBytes);
5761 buf = (__u8 *) dev->srCache;
5764 memset(dev->srCache, 0, srCacheBytes);
5766 for (i = 0; i < dev->param.nShortOpCaches && buf; i++) {
5767 dev->srCache[i].object = NULL;
5768 dev->srCache[i].lastUse = 0;
5769 dev->srCache[i].dirty = 0;
5770 dev->srCache[i].data = buf = YMALLOC_DMA(dev->param.totalBytesPerChunk);
5781 dev->gcCleanupList = YMALLOC(dev->param.nChunksPerBlock * sizeof(__u32));
5782 if (!dev->gcCleanupList)
5786 if (dev->param.isYaffs2)
5787 dev->param.useHeaderFileSize = 1;
5789 if (!init_failed && !yaffs_InitialiseBlocks(dev))
5792 yaffs_InitialiseTnodesAndObjects(dev);
5794 if (!init_failed && !yaffs_CreateInitialDirectories(dev))
5799 /* Now scan the flash. */
5800 if (dev->param.isYaffs2) {
5801 if (yaffs2_CheckpointRestore(dev)) {
5802 yaffs_CheckObjectDetailsLoaded(dev->rootDir);
5803 T(YAFFS_TRACE_ALWAYS,
5804 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
5807 /* Clean up the mess caused by an aborted checkpoint load
5808 * and scan backwards.
5810 yaffs_DeinitialiseBlocks(dev);
5812 yaffs_DeinitialiseTnodesAndObjects(dev);
5814 dev->nErasedBlocks = 0;
5815 dev->nFreeChunks = 0;
5816 dev->allocationBlock = -1;
5817 dev->allocationPage = -1;
5818 dev->nDeletedFiles = 0;
5819 dev->nUnlinkedFiles = 0;
5820 dev->nBackgroundDeletions = 0;
5822 if (!init_failed && !yaffs_InitialiseBlocks(dev))
5825 yaffs_InitialiseTnodesAndObjects(dev);
5827 if (!init_failed && !yaffs_CreateInitialDirectories(dev))
5830 if (!init_failed && !yaffs2_ScanBackwards(dev))
5833 } else if (!yaffs1_Scan(dev))
5836 yaffs_StripDeletedObjects(dev);
5837 yaffs_FixHangingObjects(dev);
5838 if(dev->param.emptyLostAndFound)
5839 yaffs_EmptyLostAndFound(dev);
5843 /* Clean up the mess */
5844 T(YAFFS_TRACE_TRACING,
5845 (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
5847 yaffs_Deinitialise(dev);
5851 /* Zero out stats */
5852 dev->nPageReads = 0;
5853 dev->nPageWrites = 0;
5854 dev->nBlockErasures = 0;
5856 dev->nRetriedWrites = 0;
5858 dev->nRetiredBlocks = 0;
5860 yaffs_VerifyFreeChunks(dev);
5861 yaffs_VerifyBlocks(dev);
5863 /* Clean up any aborted checkpoint data */
5864 if(!dev->isCheckpointed && dev->blocksInCheckpoint > 0)
5865 yaffs2_InvalidateCheckpoint(dev);
5867 T(YAFFS_TRACE_TRACING,
5868 (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
5873 void yaffs_Deinitialise(yaffs_Device *dev)
5875 if (dev->isMounted) {
5878 yaffs_DeinitialiseBlocks(dev);
5879 yaffs_DeinitialiseTnodesAndObjects(dev);
5880 if (dev->param.nShortOpCaches > 0 &&
5883 for (i = 0; i < dev->param.nShortOpCaches; i++) {
5884 if (dev->srCache[i].data)
5885 YFREE(dev->srCache[i].data);
5886 dev->srCache[i].data = NULL;
5889 YFREE(dev->srCache);
5890 dev->srCache = NULL;
5893 YFREE(dev->gcCleanupList);
5895 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5896 YFREE(dev->tempBuffer[i].buffer);
5900 if (dev->param.deinitialiseNAND)
5901 dev->param.deinitialiseNAND(dev);
5905 static int yaffs_CountFreeChunks(yaffs_Device *dev)
5910 yaffs_BlockInfo *blk;
5912 blk = dev->blockInfo;
5913 for (b = dev->internalStartBlock; b <= dev->internalEndBlock; b++) {
5914 switch (blk->blockState) {
5915 case YAFFS_BLOCK_STATE_EMPTY:
5916 case YAFFS_BLOCK_STATE_ALLOCATING:
5917 case YAFFS_BLOCK_STATE_COLLECTING:
5918 case YAFFS_BLOCK_STATE_FULL:
5920 (dev->param.nChunksPerBlock - blk->pagesInUse +
5921 blk->softDeletions);
5932 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
5934 /* This is what we report to the outside world */
5937 int nDirtyCacheChunks;
5938 int blocksForCheckpoint;
5942 nFree = dev->nFreeChunks;
5944 nFree = yaffs_CountFreeChunks(dev);
5947 nFree += dev->nDeletedFiles;
5949 /* Now count the number of dirty chunks in the cache and subtract those */
5951 for (nDirtyCacheChunks = 0, i = 0; i < dev->param.nShortOpCaches; i++) {
5952 if (dev->srCache[i].dirty)
5953 nDirtyCacheChunks++;
5956 nFree -= nDirtyCacheChunks;
5958 nFree -= ((dev->param.nReservedBlocks + 1) * dev->param.nChunksPerBlock);
5960 /* Now we figure out how much to reserve for the checkpoint and report that... */
5961 blocksForCheckpoint = yaffs2_CalcCheckpointBlocksRequired(dev);
5963 nFree -= (blocksForCheckpoint * dev->param.nChunksPerBlock);
5972 static int yaffs_freeVerificationFailures;
5974 void yaffs_VerifyFreeChunks(yaffs_Device *dev)
5979 if (yaffs_SkipVerification(dev))
5982 counted = yaffs_CountFreeChunks(dev);
5984 difference = dev->nFreeChunks - counted;
5987 T(YAFFS_TRACE_ALWAYS,
5988 (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
5989 dev->nFreeChunks, counted, difference));
5990 yaffs_freeVerificationFailures++;
5994 /*---------------------------------------- YAFFS test code ----------------------*/
5996 #define yaffs_CheckStruct(structure, syze, name) \
5998 if (sizeof(structure) != syze) { \
5999 T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
6000 name, syze, (int) sizeof(structure))); \
6001 return YAFFS_FAIL; \
6005 static int yaffs_CheckStructures(void)
6007 /* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags"); */
6008 /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */
6009 /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */
6010 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
6011 /* yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode"); */
6013 #ifndef CONFIG_YAFFS_WINCE
6014 yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");