没有合适的资源?快使用搜索试试~ 我知道了~
首页yaffs源代码分析,比情景分析更详细
资源详情
资源评论
资源推荐
狗拿耗子
YAFFS2
———狗拿耗子第三篇
1、YAFFS2 的背景
YAFFS2 是Charles Manning开发的NAND Flash文件系统。可以从www.yaffs.net获得详细的描
述和最新的版本。关于Nand Flash与Nor Flash的不同之处,网上有很多文章叙述,这里不再
罗嗦。反正是在嵌入式系统中,一般是Nor Flash存放在Boot程序,Nand Flash作为正式的存
储设备使用。
YAFFS (Yet Another Flash File System) was designed and written by Charles Manning, of
Whitecliffs, New Zealand, for the company Aleph One. YAFFS is the first file system that was
designed specifically for NAND flash.
Yaffs1 is the first version of this file system and works on NAND chips that have 512 byte pages +
16 byte spare (OOB;Out-Of-Band) areas. These older chips also generally allow 2 or 3 write
cycles per page, which YAFFS takes advantage of - i.e. dirty pages are marked by writing to a
specific spare area byte.
Newer NAND flash chips have larger pages, 2048 bytes + 64 bytes spare areas, and stricter write
requirements. Each page within a block must be written to in sequential order, and each page must
be written only once. YAFFS2 was designed to accommodate these newer chips. YAFFS2 is based
on the YAFFS1 source code, with the major difference being that internal structures are not fixed
to assume 512 byte sizing, and a block sequence number is placed on each written page. In this
way older pages can be logically overwritten without violating the "write once" rule.
YAFFS is a robust log-structured file system that holds data integrity as a high priority. A
secondary YAFFS goal is high performance. YAFFS will typically outperform most alternatives. It
is also designed to be portable and has been used on Linux, WinCE, pSOS, eCOS, ThreadX and
various special-purpose OSes. A variant 'YAFFS/Direct' is used in situations where there is no OS,
embedded OSes and bootloaders: it has the same core filesystem but simpler interfacing to the OS
and NAND flash hardware.
The filesystem is licensed both under the GPL and under per-product licences available from
Aleph One.
YAFFS1
YAFFS has no inherent formatting, an erased flash chip is formatted. It follows the smart media
scheme of marking the 5th byte of the spare area for bad blocks, and ignores any blocks where the
spare area byte 5 is not 0xFF.
1
To write file data, YAFFS initially writes a whole page (chunk in YAFFS terminology) that
describes the file metadata, such as timestamps, name, path, etc. The new file is assigned a unique
object ID number; every data chunk within the file will contain this unique object ID within the
spare area. YAFFS maintains a tree structure in RAM memory of the physical location of these
狗拿耗子
chunks. When a chunk is no longer valid (the file is deleted, or parts of the file are overwritten),
YAFFS marks a particular byte in the spare area of the chunk as ‘dirty’. When an entire block (32
pages) is marked as dirty, YAFFS can erase the block and reclaim the space. If free space on the
device is low, YAFFS may need to choose a block that has some number of dirty pages and some
number of good pages, move the good pages to a new block, mark the old pages as dirty and erase
the block. The process of moving good pages & erasing blocks is called Garbage Collection.
When a YAFFS system mounts a NAND flash device, it must scan the spare areas of every block
to check for valid data, whereby it can then reconstitute the tree data structures.
YAFFS2
YAFFS2 is similar in concept to YAFFS1, and shares much the same code; and the YAFFS2 code
base supports YAFFS1 data formats through backward compatibility. The main difference is that
YAFFS2 needs to jump through significant hoops to meet the "write once" requirement of modern
NAND flash.
YAFFS2 marks every newly written block with a sequence number that is monotonically
increasing. The sequence of the chunks can be inferred from the block sequence number and the
chunk offset within the block. Thereby when YAFFS2 scans the flash and detects multiple chunks
that have identical ObjectIDs and ChunkNumbers, it can choose which to use by taking the
greatest sequence number. For efficiency reasons YAFFS2 also introduces the concept of shrink
headers. For example when a file is resized to a smaller size, YAFFS1 will mark all of the affected
chunks as dirty - YAFFS2 cannot do this due to the "write once" rule. YAFFS2 instead writes a
"shrink header", which indicates that a certain number of pages before that point are invalid. This
lets YAFFS2 reconstruct the final state of the filesystem when the system reboots.
YAFFS2 uses a more abstract definition of the NAND flash allowing it to be used with a wider
variety of flash parts with different geometries, bad block handling rules etc.
2
YAFFS2 now supports "checkpointing" which bypasses normal mount scanning, allowing very
fast mount times. Mileage will vary, but mount times of c. 3 seconds for 2 GB have been reported.
狗拿耗子
2、chunk 与 block
K9F1208 是 64M 的 Nand Flash,一个 chunk 包含 512byte 的 data area,与 16byte 的 spare area,
32 个 chunk 构成了一个 block。
2.1 spare area 与 tag
在 YAFFS2 中 data area 用来存放数据,而 spare area 用于存放 chunk 的归属等信息。YAFFS2
给出了一个数据结构struct yaffs_ExtendedTags用来存放上述信息。显然在16byte的spare area
中无法存放 struct yaffs_ExtendedTags,所以必须针对 K9F1208 这种 Flash 来适配 tag。
typedef struct {
unsigned validMarker0;
unsigned chunkUsed; /* Status of the chunk: used or unused */
unsigned objectId; /* If 0 then this is not part of an object (unused) */
unsigned chunkId; /* If 0 then this is a header, else a data chunk */
unsigned byteCount; /* Only valid for data chunks */
/* The following stuff only has meaning when we read */
yaffs_ECCResult eccResult;
unsigned blockBad;
/* YAFFS 1 stuff */
unsigned chunkDeleted; /* The chunk is marked deleted */
unsigned serialNumber; /* Yaffs1 2-bit serial number */
/* YAFFS2 stuff */
unsigned sequenceNumber; /* The sequence number of this block */
/* Extra info if this is an object header (YAFFS2 only) */
unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
unsigned extraParentObjectId; /* The parent object */
unsigned extraIsShrinkHeader; /* Is it a shrink header? */
unsigned extraShadows; /* Does this shadow another object? */
yaffs_ObjectType extraObjectType; /* What object type? */
unsigned extraFileLength; /* Length if it is a file */
unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
unsigned validMarker1;
} yaffs_ExtendedTags;
2.1.1 经过适配后的 tag
3
属性名称 长度(bit) 开始于(bit) 结束于(bit)
firstChunkValid 8 0 7
objectId 16 8 23
chunkId 16 24 39
byteCount 11 40 50
serialNumber 2 51 52
sequenceNumber 29 53 81
extraHeaderInfoAvailable 1 82 82
extraParentObjectId 16 83 98
狗拿耗子
extraIsShrinkHeader 1 99 99
extraShadows 1 100 100
extraObjectType 4 101 104
extraFileLength 21 105 125
eccForTagHiBits 2 126 127
secondChunkValid 8 128 135
extraEquivalentObjectId 16 136 151
eccForTagLoBits 16 152 167
eccFor1stHalfPage 22 168 189
eccFor2stHalfPage 22 190 211
eccFor3stHalfPage 22 212 233
eccFor4stHalfPage 22 234 255
typedef struct {
unsigned char data[32];
} YAFFS2_K9F1208_Tag;
在我的适配中不妨将连续的两个 chunk 叫做 d_chunk。d_chunk 包含 1024 byte 数据,32 byte
spare area,此时一个 block 包含 16 个 d_chunk。
z K9F1208 的手册没有提到 flash 在 erase 后,spare area 的值是多少。不过在
Yaffs_tagscompat.c 的函数 yaffs_TagsCompatabilityQueryNANDBlock()中可以知道,判断
block 是否 erase 过的依据是该 block 上的 chunk 的 16 Byte 的 spare area 是否全是 0xff。
所以可以根据 struct YAFFS2_K9F1208_Tag 的值是否全为 0xff ,来确定
yaffs_ExtendedTags.chunkUsed。
z K9F1208 的手册说,可能在出厂的时候,flash 上就存在坏的 block,而鉴别这些坏 block
的方法是读取 block 前两个 chunk 的 spare area,如果它们的第一个字节均为 0xff,则该
block 可用,否则该 block 是坏的。所以在 struct YAFFS2_K9F1208_Tag 中将第一个字节
与第 17 个字节分别定义为 firstChunkValid、secondChunkValid;当读取的 tag 中这两个
字节不为 0xff,则表示相应的 block 是坏的;在写入时,应将这两个字节置为 0xff。
z struct YAFFS2_K9F1208_Tag 提供了
88 bits 用于 data 的 ecc 校验,18 bits 用于 tag 自身
的校验。ECC 的校验算法将在下面给出,这里不再解释。
z yaffs_ExtendedTags.chunkDeleted 始终为 false,因为与 YAFFS2 不同的是,YAFFS2 不
会写 spare area,用于表示该 chunk 被删除。所以 struct YAFFS2_K9F1208_Tag 不再包含
该值,从 YAFFS2_K9F1208_Tag 转换为 yaffs_ExtendedTags 时,chunkDeleted 等于 false。
z chunkID 用 16 bits 表示,可以索引范围为 0 到 0xffff,即可索引 0x10000 个 d_chunk,
而每个 d_chunk 大小为 1k Bytes,所以可以覆盖 64M Bytes,恰好为 K9F1208 可以存储
的数据大小。
z byteCount 用 11 bits 表示,可以表示 0 到 2047,大于 d_chunk 的大小。
4
z sequenceNumber 用 29 bits 表示,在 YAFFS2 的说明中有:
/* Sequence numbers are used in YAFFS2 to determine block allocation order.
* The range is limited slightly to help distinguish bad numbers from good.
* This also allows us to perhaps in the future use special numbers for
* special purposes.
32*8 = 256
单个文件的最大寻址
狗拿耗子
* EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
* and is a larger number than the lifetime of a 2GB device. */
那么按照上述的分配速度,29 bits 的 sequence 可以用于约两年的分配。
2.1.2 转换函数如下:
z void ExtendTagToK9F1208Tag(...)完成从 yaffs_ExtendedTags 到 YAFFS2_K9F1208_Tag
的转换。
z void K9F1208TagToExtendTag (...)完成从 YAFFS2_K9F1208_Tag 到 yaffs_ExtendedTags
的转换。
z class K9F1208Tag 用于设置 YAFFS2_K9F1208_Tag 的各个属性。
class K9F1208Tag
{
public:
void SetFirstChunkValid(bool);
void SetObjectID(unsigned short);
void SetChunkID(unsigned short);
......
unsigned char *Get1stHalfData();
unsigned char *Get2stHalfData();
unsigned char *GetAllData();
unsigned char *GetWholeTag();
unsigned char *Get1stHalfTag();
unsigned char *Get2stHalfTag();
private:
unsigned char tag_data_[32];
};
2.2 驱动函数的适配
如下可知 YAFFS2 共需要 6 个驱动函数(用蓝色标注出来的函数),下面分别介绍其中 4 个
函数的适配。
static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
{
/* Common functions, gotta have */
if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
return 0;
#ifdef CONFIG_YAFFS_YAFFS2
/* Can use the "with tags" style interface for yaffs1 or yaffs2 */
if (dev->writeChunkWithTagsToNAND &&
dev->readChunkWithTagsFromNAND &&
!dev->writeChunkToNAND &&
!dev->readChunkFromNAND &&
dev->markNANDBlockBad && dev->queryNANDBlock)
return 1;
5
#endif
剩余62页未读,继续阅读
zhangshengheng
- 粉丝: 5
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 2023年中国辣条食品行业创新及消费需求洞察报告.pptx
- 2023年半导体行业20强品牌.pptx
- 2023年全球电力行业评论.pptx
- 2023年全球网络安全现状-劳动力资源和网络运营的全球发展新态势.pptx
- 毕业设计-基于单片机的液体密度检测系统设计.doc
- 家用清扫机器人设计.doc
- 基于VB+数据库SQL的教师信息管理系统设计与实现 计算机专业设计范文模板参考资料.pdf
- 官塘驿林场林防火(资源监管)“空天地人”四位一体监测系统方案.doc
- 基于专利语义表征的技术预见方法及其应用.docx
- 浅谈电子商务的现状及发展趋势学习总结.doc
- 基于单片机的智能仓库温湿度控制系统 (2).pdf
- 基于SSM框架知识产权管理系统 (2).pdf
- 9年终工作总结新年计划PPT模板.pptx
- Hytera海能达CH04L01 说明书.pdf
- 数据中心运维操作标准及流程.pdf
- 报告模板 -成本分析与报告培训之三.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论12