⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yaffs_guts.cpp

📁 文件系统在DSP上的实现
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* * YAFFS: Yet another FFS. A NAND-flash specific file system. * yaffs_guts.c  The main guts of YAFFS * * Copyright (C) 2002 Aleph One Ltd. *   for Toby Churchill Ltd and Brightstar Engineering * * Created by Charles Manning <charles@aleph1.co.uk> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */   //yaffs_guts.cconst char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.33 2003/11/16 07:40:42 charles Exp $";#ifdef _WIN32#include "stdafx.h"#endif#include "yportenv.h"#include "yaffsinterface.h"#include "yaffs_guts.h"#include "sysarch.h"#define YAFFS_PASSIVE_GC_CHUNKS 2#if 0// Use Steven Hill's ECC struff instead// External functions for ECC on datavoid nand_calculate_ecc (const u_char *dat, u_char *ecc_code);int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);#define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)#define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)#else#include "yaffs_ecc.h"#endif// countBits is a quick way of counting the number of bits in a byte.// ie. countBits[n] holds the number of 1 bits in a byte with the value n.static const char yaffs_countBitsTable[256] ={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};static int yaffs_CountBits(__u8 x){	int retVal;	retVal = yaffs_countBitsTable[x];	return retVal;}// Local prototypesstatic int yaffs_CheckObjectHashSanity(yaffs_Device *dev);static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force);static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND);static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);static int yaffs_CheckStructures(void);static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);// Robustificationstatic void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);static int yaffs_UnlinkWorker(yaffs_Object *obj);static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj);static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1);loff_t yaffs_GetFileSize(yaffs_Object *obj);static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);#ifdef YAFFS_PARANOIDstatic int yaffs_CheckFileSanity(yaffs_Object *in);#else#define yaffs_CheckFileSanity(in)#endifstatic void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);__u16 yaffs_CalcSum(const __u8 *data);// Chunk bitmap manipulations//描述:返回该blk的chunkBits的起始地址static __inline __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk){	if(blk < dev->startBlock || blk > dev->endBlock)	{		T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));		YBUG();	}	return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock));	                         //chunkBitmapStride:一个block的chunkBits占的字节数}//描述:一个block的chunkBits都置为0static __inline__ void yaffs_ClearChunkBits(yaffs_Device *dev,int blk){	__u8 *blkBits = yaffs_BlockBits(dev,blk);	 memset(blkBits,0,dev->chunkBitmapStride);  }static __inline__ void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk){	__u8 *blkBits = yaffs_BlockBits(dev,blk);		blkBits[chunk/8] &=  ~ (1<<(chunk & 7));}//描述:设置该blk的chunk的位为1static __inline__ void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk){	__u8 *blkBits = yaffs_BlockBits(dev,blk);	blkBits[chunk/8] |=   (1<<(chunk & 7));}static __inline__ int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk){	__u8 *blkBits = yaffs_BlockBits(dev,blk);	return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;}static __inline__ int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk){	__u8 *blkBits = yaffs_BlockBits(dev,blk);	int i;	for(i = 0; i < dev->chunkBitmapStride; i++)	{		if(*blkBits) return 1;		blkBits++;	}	return 0;}// Function to manipulate block infostatic  __inline__ yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk){	if(blk < dev->startBlock || blk > dev->endBlock)	{		T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));		YBUG();	}	return &dev->blockInfo[blk - dev->startBlock];}static  __inline__ int yaffs_HashFunction(int n){	return (n % YAFFS_NOBJECT_BUCKETS);}yaffs_Object *yaffs_Root(yaffs_Device *dev){	return dev->rootDir;}yaffs_Object *yaffs_LostNFound(yaffs_Device *dev){	return dev->lostNFoundDir;}static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare){	if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)	{		T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));		return YAFFS_FAIL;	}	dev->nPageWrites++;	return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);}/////////////////////////////////////////////////////////////////////////////////////////////////参数:@data 用来返回该Chunk的数据//      @spare 用来返回该Chunk的spare//备注:data 和 spare都有可能为空,表示不用返回相应的数据int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,							int chunkInNAND, 							__u8 *data, 							yaffs_Spare *spare, 							int doErrorCorrection){	int retVal;	yaffs_Spare localSpare;	dev->nPageReads++;		if(!spare && data)	{		// If we don't have a real spare, then we use a local one.		// Need this for the calculation of the ecc		spare = &localSpare;	}		for (int i=0; i<3; i++)	{		retVal  = dev->readChunkFromNAND(dev,chunkInNAND, data, spare);		                 //变成真正的dev读,在这里spare肯定不为空,data有可能为空		if (data && doErrorCorrection)		{            if (yaffs_CalcSum(data) == spare->ecc)  //校验正确			{				return retVal;			}        }        else   //无须校验,直接返回        {        	return retVal;       	}       	       	//Sleep(1);   //躲避刚才的错误状态	}//lcd.print("Rd err\n",0, 3); 	while(1);    yaffs_HandleReadDataError(dev, chunkInNAND);	return retVal;}static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND){	static int init = 0;	static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];	static __u8 data[YAFFS_BYTES_PER_CHUNK];    // Might as well always allocate the larger size for dev->useNANDECC == true;	static __u8 spare[sizeof(struct yaffs_NANDSpare)];	  	dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare);		if(!init)	{		memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);		init = 1;	}		if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return  YAFFS_FAIL;	if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;		return YAFFS_OK;	}int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND){	dev->nBlockErasures++;	return dev->eraseBlockInNAND(dev,blockInNAND);}int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev){	return dev->initialiseNAND(dev);}//描述:把data和spare写到FLASH,经过以下各个过程:分配一个chunk,(校验是否擦除OK),写,(读出来校验),//     不成功则重写,返回写到的chunkInNAMDstatic int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve){	int chunk;		int writeOk = 1;	int attempts = 0;		unsigned char rbData[YAFFS_BYTES_PER_CHUNK];	yaffs_Spare rbSpare;		do{		chunk = yaffs_AllocateChunk(dev,useReserve);  //分配一个chunk,返回chunkInNAMD			if(chunk >= 0)		{			// First check this chunk is erased...#ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK			writeOk = yaffs_CheckChunkErased(dev,chunk);#endif					if(!writeOk)			{				T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));			}			else			{				writeOk =  yaffs_WriteChunkToNAND(dev,chunk,data,spare);  //把data和spare写到chunk			}			attempts++;			if(writeOk)			{				// Readback & verify				// If verify fails, then delete this chunk and try again				// To verify we compare everything except the block and 				// page status bytes.				// NB We check a raw read without ECC correction applied				yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0);				#ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY				if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare))				{					// Didn't verify					T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk));					writeOk = 0;				}	#endif							}							/*if (writeOk == 0)			{				writeOk = dev->repairWriteChunkError(dev, chunk, data, spare);			}*/										if (writeOk)			{				// Copy the data into the write buffer.				// NB We do this at the end to prevent duplicates in the case of a write error.				//Todo				yaffs_HandleWriteChunkOk(dev,chunk,data,spare);  //没做任何事情			}			else			{				//lcd.print("Wr fail 3\n",0, 3); 	while(1);							yaffs_HandleWriteChunkError(dev,chunk);			}		}			} while(chunk >= 0 && ! writeOk);		if(attempts > 1)	{		T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));//	lcd.print("Wr attemp\n",0, 3); 	while(1);					dev->nRetriedWrites+= (attempts - 1);		}	/*	if (writeOk == 0)	{		lcd.print("Wr fail retry\n",0, 3); 	while(1);				}*/		return chunk;}///// Functions for robustisizing////static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND){	// Ding the blockStatus in the first two pages of the block.		yaffs_Spare spare;	memset(&spare, 0xff,sizeof(yaffs_Spare));	spare.blockStatus = 0;		// TODO change this retirement marking for other NAND types	yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);	yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);		yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;	dev->nRetiredBlocks++;}static int yaffs_RewriteBufferedBlock(yaffs_Device *dev){	dev->doingBufferedBlockRewrite = 1;	//	//	Remove erased chunks	//  Rewrite existing chunks to a new block	//	Set current write block to the new block		dev->doingBufferedBlockRewrite = 0;		return 1;}static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND){	int blockInNAND = chunkInNAND/dev->nChunksPerBlock;	// Mark the block for retirement//	yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;	   //BY CEN  不产生坏块//	T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));	T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d read error" TENDSTR),blockInNAND));	//TODO		// Just do a garbage collection on the affected block then retire the block	// NB recursion}static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND){}static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare){}static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare){}static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND){	int blockInNAND = chunkInNAND/dev->nChunksPerBlock;	// Mark the block for retirement//	yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;    //BY CEN  不产生坏块	// Delete the chunk	yaffs_DeleteChunk(dev,chunkInNAND,1);}static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1){	if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||		s0->tagByte0 != s1->tagByte0 ||		s0->tagByte1 != s1->tagByte1 ||		s0->tagByte2 != s1->tagByte2 ||		s0->tagByte3 != s1->tagByte3 ||		s0->tagByte4 != s1->tagByte4 ||		s0->tagByte5 != s1->tagByte5 ||		s0->tagByte6 != s1->tagByte6 ||		s0->tagByte7 != s1->tagByte7 ||		s0->ecc1[0]  != s1->ecc1[0]  ||		s0->ecc1[1]  != s1->ecc1[1]  ||		s0->ecc1[2]  != s1->ecc1[2]  ||		s0->ecc2 != s1->ecc2  ||		s0->ecc != s1->ecc)		{			return 0;		}		return 1;}///////////////////////// Object management //////////////////// List of spare objects// The list is hooked together using the first pointer// in the object// static yaffs_Object *yaffs_freeObjects = NULL;// static int yaffs_nFreeObjects;// static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;// static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];static __u16 yaffs_CalcNameSum(const char *name){	__u16 sum = 0;	__u16 i = 1;		__u8 *bname = (__u8 *)name;	if(bname)	{		while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))		{#ifdef CONFIG_YAFFS_CASE_INSENSITIVE			sum += toupper(*bname) * i;#else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -