UBI - Unsorted Block Images(未排序的块图像)(8-13)

UBI volume tableTable of contents

  1. Big red note
  2. Overview
  3. Power-cuts tolerance
  4. Kernel source code
  5. Mailing list
  6. User-space tools
  7. UBI headers
  8. UBI volume table
  9. Minimum flash input/output unit
  10. NAND flash sub-pages
  11. UBI headers position
  12. Flash space overhead
  13. Saving erase counters
  14. How UBI flasher should work
  15. Marking eraseblocks as bad
  16. Scalability issues
  17. Reserved blocks for bad block handling (only for NAND chips)
  18. Volume auto-resize
  19. UBI operations
    1. LEB un-map
    2. LEB map
    3. Volume update
    4. Atomic LEB change
  20. Fastmap
  21. R/O block devices on top of UBI volumes
  22. UBI stress testing
  23. More documentation

UBI volume table

UBI卷表

The volume table is an on-flash data structure which contains information about each volume on this UBI device. The volume table is an array of volume table records. Each record contains the following information:

卷表闪存数据结构(意为记录在闪存中),其中包含有关此UBI设备上每个卷的信息。卷表是一个记录卷信息的数组。每条记录包含以下信息:

  • volume size;
    卷的大小;
  • volume name;
    卷的名字;
  • volume type (dynamic or static);
    卷的类型(动态或静态)
  • volume alignment;
    卷的对齐;
  • update marker (set on a volume when an update is initiated and cleared when successfully completed);
    更新标记(启动更新时在卷上设置,成功完成时清除);
  • auto-resize flag;
    自动调整大小标志;
  • CRC-32 checksum for this record.
    此记录的CRC-32校验和。

Each record describes one UBI volume. The record index in the volume table array corresponds to the volume ID it describes. I.e, UBI volume 0 is described by record 0 in the volume table, and so on. The total number of records in the volume table is limited by the LEB size, and cannot be greater than 128. This means that UBI devices cannot have more than 128 volumes.

每个记录描述一个UBI卷。卷表数组中的纪录索引与其描述的卷ID相对应。即,UBI卷0由卷表中的纪录0描述,以此类推。卷表数组中所有纪录的总数受限于LEB的数量,并且不能大于128。也就是说UBI设备不能拥有超过128个卷

Every time an UBI volume is created, removed, re-sized, re-named or updated, the corresponding volume table record is changed. UBI maintains two copies of the volume table for reasons of reliability and power-cut tolerance.

当一个UBI卷被创建,删除,调整大小,重命名或更新时,卷表数组中对应的记录也会被更改。出于可靠性和停电容差的原因,UBI维护两份卷表副本

Implementation details

内部细节

Internally, the volume table resides in a special-purpose UBI volume which is called the layout volume. This volume consists of 2 LEBs - one for each copy of the volume table. The layout volume is an "internal" UBI volume, and users do not see it nor access it. When reading or writing the layout volume, UBI uses the same mechanisms which are used for normal user volumes.

在内部,卷表数组存储在一个叫做布局卷的专用UBI卷中。这个卷由两个LEB块组成 - 每一个块保留一个卷表的副本。这个布局卷是一个“内部”的UBI卷,用户看不到也不能操作它。当读或写卷表数组时,UBI会使用和普通用户卷一样的机制去操作布局卷

UBI uses the following algorithm when updating a volume table record:

升级一个卷表记录时,UBI使用如下法则:

  • Prepare an in-memory buffer with the new volume table contents.
    准备一个包含新卷表内容的内存缓冲区。
  • Un-map LEB0 of the layout volume.
    取消布局卷中 LEB0 的映射关系。
  • Write the new volume table to LEB0.
    把新的卷表数组写到 LEB0。
  • Un-map LEB1 of the layout volume.
    取消布局卷中 LEB1 的映射关系。
  • Write the new volume table to LEB1.
    把新的卷表数组写到 LEB1。
  • Flush the UBI work queue to make sure the PEBs are corresponding to the un-mapped LEBs are erased.
    刷新UBI工作队列,以确保没有和 LEB 形成映射关系的 PEB 被擦除。

When attaching the MTD device, UBI makes sure that the 2 volume table copies are equivalent. If they are not equivalent, which may be caused by an unclean reboot, UBI picks the one from LEB0 and copies it to LEB1 of the layout volume (because, according to the algorithm specified above, LEB0 is the one that is updated first and therefore considered to have the most up-to-date information). If one of the volume table copies is corrupted, UBI restores it from the other volume table copy.

当连接一个MTD设备,UBI确保这两个卷表副本是相同的。如果它们不相同,(这可能是由不干净的重启引起的),UBI就会在布局卷中从LEB0复制一份卷表到LEB1(因为,根据上面的法则说明,LEB0是首先升级的那个块,因此认为它的数据是最新的)。如果有一个卷表副本被破坏了,那么UBI就会根据另一个卷表副本恢复它。

Minimum flash input/output unit

闪存的最小输入/输出单元

UBI uses an abstract model of flash. In short, from UBI's point of view the flash (or MTD device) consists of eraseblocks, which may be good or bad. Each good eraseblock may be read from, written to, or erased. Good eraseblocks may also be marked as bad.

UBI使用闪存的抽象模型。简而言之,从UBI的角度看,flash(或者MTD设备),是由许多擦除块块组成的,这些块或好或坏。每一个好的可擦除块可以读,写,或擦除。好的块也可以被标记为坏块。

Flash reads and writes may only be done in multiples of the minimum input/output unit size, which depends on the flash type.

闪存的读写只能以最小输入/输出单元大小的倍数进行,具体的大小取决于闪存的类型。

  • NOR flashes usually have a minimum I/O unit size of 1 byte, because NOR flashes usually allow reading and writing single bytes (in fact, it is even be possible to change individual bits).
    NOR flash 的最小输入/输出单元通常是1字节,因为NOR flash通常允许读写单个字节(事实上,NOR flash甚至可以单独改变某个位)。
  • Some NOR flashes may have other minimum I/O unit sizes, e.g. 16 or 32 bytes in the case of ECC'd NOR flashes.
    一些NOR flash 可能有其他的最小输入/输出单元,比如,在支持ECC的NOR flash情况下为16字节或32字节。
  • NAND flashes usually have minimum I/O sizes of 512, 2048 or 4096 bytes, which corresponds to their page size. NAND flashes store per-page ECC codes in the OOB area, which means that whole NAND pages have to be written at once to calculate the ECC, and whole NAND pages have to be read at once to check the ECC.
    NAND flash 的最小输入/输出单元通常为512,2048或4096字节,对应着他们的页大小。NAND flash 在OOB区域储存每个页的ECC,这意味着必须一次写入整个NAND页来计算ECC,并且必须一次读整个NAND也来检查ECC

The minimum I/O unit size is a very important characteristic of the MTD device. It affects many things, e.g.:

最小的输入/输出单元是MTD设备一个非常重要的特性。它影响很多事情,例如:

  • the physical position of the VID header depends on the minimum I/O unit size, which means that the LEB size also depends on it; generally, the larger the minimum I/O unit size, the smaller the LEB size, and therefore the greater the UBI flash space overhead;
    VID头部的物理位置取决于最小输入/输出单元,同样LEB的大小也取决于它;通常,最小输入/输出单元越大,LEB的数量就越少,因此UBI闪存空间开销越大
  • all writes to LEBs should be aligned to the minimum I/O unit size, and should be multiples of the minimum I/O unit size; this does not apply to reads, but bear in mind that on the MTD level all reads are done in multiples of the minimum I/O unit size anyway; this is just hidden from users by buffering the read data and copying only the requested amount of bytes to the user buffer.
    所有关于LEB的写操作,都应该和最小的输入/输出单元对齐,并且应该是最小输入/输出单元的整数倍这不适用于读取,但请记住,无论如何,在MTD级别上,所有的读取都是以最小输入/输出单元的倍数完成的;只是在操作中,通过缓冲读取的数据并仅将请求的字节数复制到用户缓冲区,从而对用户隐藏了这个操作。

NAND flash sub-pag

NAND flash 子页

As mentioned earlier, all UBI I/O is be performed in multiples of the minimum I/O unit size, which is equivalent to the NAND device's page size (in the case of NAND flash). However, some SLC NAND flashes allow for smaller I/O units, which are called sub-pages in MTD terminology. Not all NAND devices have sub-pages.

如前所述(Memory Technology Device (MTD) Subsystem for Linux.),所有 UBI 的 I/O 操作都应该为最小I/O单元的整数倍,该最小I/O单元相当于NAND设备的页大小(对于NAND flash)。但是,一些SLC NAND闪存允许更小的I/O单元,在MTD术语中称位子页。但不是所有的NAND设备都支持子页。

  • MLC NANDs do not have sub-pages (at least as of April 2009).
    MLC NAND没有子页(至少到2009年4月还没有)。
  • SLC NANDs usually do have sub-pages. E.g., 512-byte NAND pages usually consist of 2x256-byte sub-pages, and 2048-byte NAND pages usually consist of 4x512-byte sub-pages.
    SLC NAND通常有子页。例如,512字节的NAND页通常由2×256字节的子页组成,而2048字节的NAND页通常由4×512字节的子页组成。
  • SLC OneNAND chips with 2048-byte NAND pages have 4x512-byte sub-pages.
    具有2048字节NAND页的SLC OneNAND芯片具有4x512字节子页。

If the NAND flash supports sub-pages, then ECC codes can be calculated on a per-sub-page basis, instead of a per-page basis. In this case it becomes possible to read and write sub-pages independently.

如果NAND闪存支持子页,则可以按子页计算ECC代码,而不是按页计算ECC代码。在这种情况下,可以独立地读写子页

However, even though the NAND chip may support sub-pages, the NAND controller of your SoC might not. If the flash is managed by a controller which calculates ECC codes only on a per-page basis, then it is impossible to do I/O in sub-page chunks. E.g. this is the case for the OLPC XO-1 laptop) - its NAND chip supports sub-pages, but the NAND controller does not.

但是,虽然NAND芯片支持子页,但是你的Soc的NAND控制器可能不支持。如果这个控制器只能计算整页的ECC,那么就不可能在子页区块中执行输入/输出。​例如,OLPC XO-1笔记本电脑就是这种情况) - 它的NAND芯片支持子页,但NAND控制器不支持(en.wikipedia.org/wiki/OLPC_XO-1)。

Note, the phrase "sub-page" is an MTD term, but this is also referred to as "NOP" which stands for "number of partial programs". NOP1 NAND flashes have no sub-pages - UBI treats them as NANDS with sub-page size equivalent to the NAND page size. NOP2 NAND flashes have 2 sub-pages (half a NAND page each), and NOP4 flashes have 4 sub-pages (a quarter of a NAND page each).

注意,短语“子页”是一个MTD术语,它也被称为“NOP”,表示“部分程序的数量”。NOP1 NAND 闪存没有子页 - UBI把它们当做子页大小和页大小一样的NAND闪存。NOP2 NAND 闪存有两个子页(分别是NAND页大小的一半),而NOP4 闪存有4个子页(每个子页是页大小的四分之一)。

UBI utilizes sub-pages to reduce flash space overhead. This overhead is reduced if sub-pages can be used (see here). Consider a NAND flash with 128KiB eraseblocks and 2048-byte pages. If it does not have sub-pages, UBI puts the VID header at physical offset 2048, so the LEB size becomes 124KiB (128KiB minus one NAND page which stores the EC header and minus another NAND page which stores the VID header). Conversely, if the NAND flash does have sub-pages, UBI puts the VID header at physical offset 512 (the second sub-page), so the LEB size becomes 126KiB (128KiB minus one NAND page which is used for storing both UBI headers). See this section for more information about where the UBI headers are stored.

UBI使用子页来减少闪存空间的消耗。如果可以使用子页,那么闪存空间的损耗就会被减低(详情请点击http://www.linux-mtd.infradead.org/doc/ubi.html#L_overhead)。试想一个NAND闪存,每块大小是128KiB,子页大小2048B。如果不支持子页,那么UBI将VID头部存储在物理偏移2048字节处,因此LEB大小变为124KiB(128KiB减去一个存储EC报头的NAND页,减去一个存储VID报头的NAND页)。相反地,如果NAND闪存支持子页,那么UBI将VID头部放在物理偏移512字节处(第二个子页),因此这个LEB大小就变成126KiB(128KiB减去一个用于存储VID和EC两个头部的NAND页)。

Sub-pages are only used by UBI internally, and only for storing the headers. The UBI API does not allow users to perform I/O to sub-page units. One of the reasons for this is that sub-page writes may be slow. To write a sub-page, the driver may actually write the whole NAND page, but put 0xFF bytes in the sub-pages which are not relevant to this operation. If this is the case, writing 4 sub-pages will be 4 times slower than writing the whole NAND page at once. Thus, UBI does use sub-pages for the headers, but this trick does not extend to the UBI API.

子页只在UBI内部使用,并且只用来储存头部。UBI API不允许用户对子页单元执行I/O操作。这样做的原因之一就是,写子页可能很慢。写一个子页,驱动可能实际上写了整个NAND页,但是会将 0xFF 写到了和这次写操作无关的子页。如果是这种情况,那么写4个子页可能会比一次写一整个页,写四次还要慢。所以,UBI会对头部使用子页,但是在UBI API中不会

UBI headers position

UBI 头部位置

The EC header always resides at offset 0 and takes 64 bytes, the VID header resides at the next available minimum I/O unit or sub-page, and also takes 64 bytes. For example:

EC头部永远保存在偏移位置0的地方,占用64字节,VID头部则保存在下一个有效的最小输入/输出单元或子页,并且也占用64字节。例如:

  • in the case of NOR flash, which has a 1-byte minimum I/O unit, the VID header resides at offset 64;
    对于NOR flash,最小输入/输出单元为1字节,VID头部将在偏移位置64字节处;
  • in the case of a NAND flash which does not have sub-pages, the VID header resides at the second NAND page;
    对于不支持子页的NAND flash,VID头部保存在第二个NAND页;
  • in the case of a NAND flash which has sub-pages, the VID header resides at the second sub-page.
    对于支持子页的NAND flash,VID头部将保存在第二个NAND子页。

Flash space overhead

闪存空间开销

UBI uses some amount of flash space for its own purposes, thus reducing the amount of flash space available for UBI users. Namely:

UBI将一定数量的闪存空间用于UBI自己的用途,这减少了UBI用户可用的闪存空间量。如:

  • 2 PEBs are used to store the volume table;
    2个PEB块会用来存储卷表;
  • 1 PEB is reserved for wear-leveling purposes;
    1个PEB预留用于损耗均衡目的;
  • 1 PEB is reserved for the atomic LEB change operation;
    1个PEB预留用于原子LEB改变操作;
  • some amount of PEBs are reserved for bad PEB handling; this is applicable for NAND flash but not for NOR flash; the amount of reserved PEBs is configurable and is equal to 20 blocks per 1024 blocks by default;
    预留一定数量的PEB用于坏的PEB的处理;这适用于NAND闪存,但不适用于NOR闪存;预留的PEB数量是可配置的,默认每1024个块预留20个块
  • UBI stores the EC and VID headers at the beginning of each PEB; the number of bytes used for these purposes depends on the flash type and is explained below.
    UBI在每个PEB的开头部分储存EC和VID头部;用于这些目的的字节数取决于闪存类型,如下所述。

Let's introduce symbols:

让我们来介绍一下这些符号:

  • W - total number of physical eraseblocks on the flash chip (NB: the entire chip, not the MTD partition);
    W - 整个芯片上的物理可擦除块的总数;(NB:整个芯片上的,不单单是MTD分区)
  • P - total number of physical eraseblocks on the MTD partition;
    P - 整个MTD分区上的物理可擦除块的总数;
  • SP - physical eraseblock size;
    SP - 物理块的大小;
  • SL - logical eraseblock size;
    SL - 逻辑块的大小;
  • BB - number of bad blocks on the MTD partition;
    BB - 整个MTD分区上的坏块数量;
  • BR - number of PEBs reserved for bad PEB handling (it is 20 * W/1024 for NAND by default, and 0 for NOR and other flash types which do not have bad PEBs);
    BR - 用于坏的PEB处理的预留PEB块数量(默认 20 * W / 1024,但对于NOR和其他没有不会产生坏块的Flash,BR为0)
  • B - MAX(BR,BB);
    B - BR和BB相比较大的那个;
  • O - the overhead related to storing EC and VID headers in bytes, i.e. O = SP - SL.
    O - 存储EC和VID标头相关的开销,即 O = SP - SL

The UBI overhead is (B + 4) * SP + O * (P - B - 4) i.e., this amount of bytes will not be accessible for users. O is different for different flashes:

UBI开销为 (B + 4) * SP + O * (P - B - 4),即用户将无法访问使用这些空间。对于不同的闪存,O也是不同的:

  • in the case of NOR flash, which has a 1-byte minimum I/O unit, O is 128 bytes;
    在具有1字节最小I/O单元的NOR闪存的情况下,O是128字节;
  • in the case of a NAND flash which does not have sub-pages (e.g., MLC NAND), O is 2 NAND pages, i.e. 4KiB in the case of 2KiB NAND pages and 1KiB in the case of 512-byte NAND pages;
    对于没有子页的NAND flash(如MLC NAND),O是2个NAND页,如,对于每页2KiB的NAND页,O就是4KiB,对于每页512字节的NAND页,O就是1KiB;
  • in the case of a NAND flash which has sub-pages, UBI optimizes its on-flash layout and puts the EC and VID headers at the same NAND page, but different sub-pages; in this case O is only one NAND page;
    在具有子页的NAND闪存的情况下,UBI优化其闪存布局,并将EC和VID报头放在相同的NAND页,但不同的子页;在这种情况下,O只是一个NAND页;
  • for other flashes the overhead should be 2 minimum I/O units if the minimum I/O unit size is greater or equivalent to 64 bytes, and 2 times 64 bytes aligned to the minimum I/O unit size if the minimum I/O unit size is less than 64 bytes.
    对于其他闪存,如果最小I/O单元大小大于或等于64字节,则开销应为2个最小I/O单元,如果最小I/O单元大小小于64字节,则开销应为与最小I/O单元大小对齐的2乘64字节。

N.B.: the formula above counts bad blocks as a UBI overhead. The real UBI overhead is: (B - BB + 4) * SP + O * (P - B - 4).

注:上面的公式将坏块计为UBI开销。实际的UBI开销是:(B - BB + 4) * SP + O * (P - B - 4)。

Saving erase counters

保存擦除计数器

When working with UBI, it is important to realize that UBI stores erase counters on the flash media. Namely, each physical eraseblock has an EC (erase counter) header which stores the amount of times this physical eraseblock has been erased (see here). It is important not to lose the erase counters, which means the tools you use to erase the flash and to write the UBI images have to be UBI-aware. The mtd-utils repository contains the ubiformat utility which does things properly.
使用UBI时,重要的是要认识到UBI将擦除计数器存储在闪存介质上。即,每个物理擦除块都具有EC(擦除计数器)头部,其存储该物理擦除块已被擦除的次数(参见此处)。一定不要丢失擦除计数器,也就是说用来擦除闪存和写入UBI映像的工具必须是适配UBI的。Mtd-utils存储库提供了可以执行正确操作的ubiformat实用工具。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值