原文参考: http://www.linux-mtd.infradead.org/doc/ubi.html
Note:
People are often confused about what UBI is, which was the reason for creating this section. Please, realize that:
- UBI is not a Flash Translation Layer (FTL), and it has nothing to do with FTL;
- UBI works with bare flashes, and it does not work with consumer flashes like
MMC
,RS-MMC
,eMMC
,SD
,mini-SD
,micro-SD
,CompactFlash
,MemoryStick
,USB flash drive
, etc; instead, UBI works with raw flash devices, which are mostly found in embedded devices like mobile phones, etc.
人们经常对UBI到底是什么感到迷惑, 这是本说明存在的原因, 你需要知道:
- UBI 不是 FTL (Flash Translation Layer), 跟 FTL 也没任何关系
- UBI 操作的是裸flash设备, 它并不针对用户通常使用的flash设备, 像MMC, RS-MMC, eMMC, SD, mini-SD, micro-SD, CF卡, 存储棒及USB 驱动的flash等, 相反, 它主要操作裸的flash设备, 这大多数是在嵌入式设备中。
1. Overview
UBI (Latin: "where?") stands for "Unsorted Block Images". It is a volume management system for raw flash devices which manages multiple logical volumes on a single physical flash device and spreads the I/O load (i.e, wear-leveling) across whole flash chip.
UBI 代表 "Unsorted Block Images", 它是一个为flash裸设备设计的卷管理系统,该系统管理单个flash设备上的多个逻辑卷, 在整个设备范围内平衡I/O负载。
In a sense, UBI may be compared to the Logical Volume Manager (LVM). Whereas LVM maps logical sectors to physical sectors, UBI maps logical eraseblocks to physical eraseblocks. But besides the mapping, UBI implements global wear-leveling and transparent I/O errors handling.
在某种意义上, UBI类似我们熟悉的 Logical Volume Manager(LVM). LVM 管理硬盘这类块设备, 映射逻辑扇区到物理扇区; 而 UBI 映射逻辑擦除块到物理擦除块。 但是, 由于flash设备自身的特点, UBI 还实现了全局耗损平均功能和对用户透明的 I/O 错误处理功能。
An UBI volume is a set of consecutive logical eraseblocks (LEBs). Each logical eraseblock may be mapped to any physical eraseblock (PEB). This mapping is managed by UBI, it is hidden from users and it is the base mechanism to provide global wear-leveling (along with per-physical eraseblock erase counters and the ability to transparently move data from more worn-out physical eraseblocks to less worn-out ones).
UBI卷是一组连续的逻辑擦除块(LEB), 每一个LEB都可以映射到任何物理擦除块(PEB), 这种映射由 UBI 管理, 这一点是对用户隐藏的, 也是提供耗损平均功能的基本机制。 (耗损平均主要是对物理擦除块的擦除次数计数, 并透明的把数据从过度使用擦除块移动到较少使用的擦除块, 从而使在整个flash全局范围内擦除块的损耗比较接近, 提高寿命)
UBI volume size is specified when the volume is created and may later be changed (volumes are dynamically re-sizable). There are user-space tools which may be used to manipulate UBI volumes.
UBI 卷尺寸在卷创建时指定, 需要时可以重新调整, 有对应的用户空间工具可以用来操作 UBI 卷。
There are 2 types of UBI volumes - dynamic volumes and static volumes. Static volumes are read-only and their contents are protected by CRC-32
checksums, while dynamic volumes are read-write and the upper layers (e.g., a file-system) are responsible for ensuring data integrity.
有两种类型的UBI卷: 动态卷和静态卷。 静态卷是只读的, 存储的内容使用 CRC32 保护; 动态卷是可读写的, 由卷上建立的文件系统保证数据的完整性。
UBI is aware of bad eraseblocks (e.g., NAND flash may have them) and frees the upper layers from any bad block handling. UBI has a pool of reserved physical eraseblocks, and when a physical eraseblock becomes bad, it transparently substitutes it with a good physical eraseblock. UBI moves good data from the newly appeared bad physical eraseblocks to good ones. The result is that users of UBI volumes do not notice I/O errors as UBI takes care of them.
UBI 能够检测坏的擦除块 (例如, Nand flash 可能存在), 可使上层的文件系统免于处理坏块。 UBI 有一个预留的块池, 当发现坏块之后, 就对用户透明的使用好的块替换, 把好的数据从坏块搬运到这些好块中。 这样, UBI 卷的用户不会收到I/O错误, 因为UBI已经处理了。
NAND flashes may have bit-flips which occur on read and write operations. Bit-flips are corrected by ECC checksums, but they may accumulate over time and cause data loss. UBI handles this by moving data from physical eraseblocks which have bit-flips to other physical eraseblocks. This process is called scrubbing. Scrubbing is done transparently in background and is hidden from upper layers.
NAND flash 在进行读写操作时可能出现 bit-flips, 这种错误使用ECC校验进行纠正, 但由于过度累积而引起数据丢失。 针对这个问题, UBI 把出现bit-flips的擦除块中的数据移动到其它物理块中,这种处理称为scrubbing. Scrubbing 是在后台进行的, 对上层用户隐藏。
Here is a short list of the main UBI features:
- UBI provides volumes which may be dynamically created, removed, or re-sized;
- UBI implements wear-leveling across whole flash device (i.e., you may continuously write/erase only one logical eraseblock of an UBI volume, but UBI will spread this to all physical eraseblocks of the flash chip);
- UBI transparently handles bad physical eraseblocks;
- UBI minimizes chances to lose data by means of scrubbing.
下面是UBI 主要特性的列表:
- UBI 提供卷, 卷可以动态创建、删除和调整大小
- UBI 实现了flash全局范围的耗损平均
- UBI 对坏块的处理对用户透明
- UBI 通过Scrubbing方式使丢失数据的可能性降到最低
Here is a comparison of MTD partitions and UBI volumes. They are somewhat because:
- both consist of eraseblocks - logical eraseblocks in case of UBI volumes, and physical eraseblocks in case of MTD partitions;
- both support three basic operations - read, write, and erase.
下面会给出 MTD 分区 与 UBI 卷的对比, 它们有可比性, 是因为:
- 两者都由擦除块组成—— 逻辑擦除块对应UBI卷, 物理擦除块对应MTD分区
- 两者都支持三个基本操作—— 读、写和擦除
But UBI volumes have the following advantages over MTD partitions:
- UBI volumes have no eraseblock wear-leveling constraints, so users do not have to care about this at all, which means the upper level software may be simpler;
- UBI volumes have no bad eraseblocks, which also leads to simpler upper level software;
- UBI volumes are dynamic in a sense that they may be created, removed or re-sized dynamically, while MTD partitions are static;
- UBI handles bit-flips which again makes the upper level software simpler;
- UBI provides a volume update operations which makes it easy to detect interrupted software updates and recover;
- UBI provides an atomic logical eraseblock change operation which allows to change the contents of a logical eraseblock without loosing the data if an unclean reboot happens during the operation; this is might be very useful for the upper-level software (e.g., for a file-system);
- UBI has an un-map operation, which just un-maps a logical eraseblock from the physical eraseblock, schedules the physical eraseblock for erasure and returns; this is very quick and frees upper level software from implementing their own mechanisms to defer erasures (e.g., JFFS2 has to implements such mechanisms).
但是, UBI 比 MTD分区有下列优势:
- UBI 卷没有块耗损平局约束, 用户无需关心, 这意味这上层软件可以更简单
- UBI 卷没有坏块, 也使得上层软件更简单
- UBI 卷在某种意义上说是动态的,可以动态创建、删除及调整尺寸, 而MTD分区是静态的
- UBI 处理bit-flips, 这又使得上层更简单
- UBI 提供卷更新操作, 可以简化对软件更新被中断的检测和恢复更新
- UBI 提供原子性的逻辑块改变操作, 这使得操作被重启中断时不会丢失数据, 这对上层软件也非常有用
- UBI 提供 un-map操作, 这允许接触逻辑块与物理块之间的映射, 灵活调度物理块。 这个操作非常快速, 使得上层软件免于实现它们自己的相应机制(例如, JFFS2 就不得不实现这种机制)
There is an additional driver called gluebi
which emulates MTD devices on top of UBI volumes. This looks a little strange, because UBI works on top of an MTD device, then gluebi
emulates other MTD devices on top, but this actually works and makes it possible for existing software (e.g., JFFS2) to run on top of UBI volumes. However, new software may benefit from the advanced UBI features and let UBI solve many issues which the flash technology imposes.
UBI 还提供一个额外的驱动, 称为gluebi, 用于在UBI卷之上模拟 MTD设备。 这看起来比较奇怪, 因为 UBI 工作在 MTD 设备上, gluebi 却在其上模拟其它MTD设备, 但这对于现有的软件运行在UBI卷上是有用的, 比如 JFFS2。 而新软件则可以从 高级 UBI 特性中受益, 由 UBI 解决flash技术存在问题。
2. Source Code
UBI is in the main-line Linux kernel starting from version 2.6.22
. But it is recommended to use the latest UBI, because we have fixed many bugs since that time, made many improvements and added new features. The UBI git tree may be found at:
git://git.infradead.org/ubi-2.6.git
UBI 从 Linux Kernel 2.6.22开始加入到 linux 主线分支, 但推荐使用最新的UBI, 因为从那时以来我们已经解决了很多bug, 做了很多改善, 添加了新特性。 UBI git 源参见: git://git.infradead.org/ubi-2.6.git
The git tree has 2 branches - the master
branch and linux-next
branches. The master
branch contains the most recent stuff which is often incomplete, buggy, or has not been tested very well. This branch is re-based from time to time. Please, do not use it unless you are an UBI developer. The linux-next
branch contains stable UBI changes which are going to be merged upstream soon. This branch is included to the linux-next git tree. Please, use this branch unless you are an UBI developer.
UBI Git 库中有两个分支: master分支和 linux-next 分支。
master分支含有大部分最近的最新成果, 但经常不完整, 有bug, 甚至没有充分测试; 该分支随时会更改, 所以请不要使用该分支, 除非你是 UBI 开发者。
linux-next 分支含有稳定的代码, 这个是可以合并到linux中的, 推荐使用该分支。