UBI Volume Table
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:
- volume size;
- volume name;
- volume type (dynamic or static);
- volume alignment;
- update marker (set for volumes which had interrupted updates;
- auto-resize flag;
CRC-32
checksum for this record.
卷表
Volume table 是一个flash数据结构, 该结构含有 UBI 设备上每一个卷的信息, Volume table 是一个数组, 存储volume记录, 每一条记录含有以下信息:
- 卷 size
- 卷 名字
- 卷类型(dynamic 或 static)
- 卷 对齐
- 更新标记 (为被中断的更新设置)
- 自动调整大小标记
- 本记录的 CRC-32校验和
Each record describes one UBI volume and record index in the volume table array corresponds to the volume ID. I.e, UBI volume 0 is described by record 0 in the volume table, and so on. Count of records in the volume table is limited by the LEB size, but cannot be greater than 128. This means that UBI devices cannot have more than 128 volumes.
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 for reliability and power-cut tolerance reasons.
每条记录描述一个 UBI 卷, 卷表中的记录索引与卷 ID 对应。例如, 卷 0 由表中的记录 0 描述。卷表中记录的数目受 LEB 大小限制, 但不能大于128. 这意味着 UBI 设备不会多于128个卷。
每当一个卷被创建、删除、调整大小、重命名或更新, 卷标中相应的记录会变更; 为了提供可靠性和容错性, UBI 为每一个卷创建两个备份。
Implementation details
Internally, the volume table resides in a special-purpose UBI volume which is called 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 the users do not see it and cannot access it. When reading or writing the layout volume, UBI uses the same mechanisms which are used for normal user volumes.
UBI uses the following algorithm when updating a volume table record.
- Prepare in-memory buffer with the new volume table contents.
- Un-map LEB0 of the layout volume.
- Write the new volume table to LEB0.
- Un-map LEB1 of the layout volume.
- Write the new volume table to LEB1.
- Flush the UBI work queue to make sure the PEBs are corresponding to the un-mapped LEBs are erased.
实现细节
在内部, 卷标存储在称为 layout volume 的特殊UBI卷中, 该卷由2个LEB组成, 其中一个存储volume table的副本。 layout volume 是内部的, 对用户不可见, 用户不能访问。当读写layout volume 时, 使用与普通卷一样的机制。
在更新volume table记录时, UBI 采用 如下算法:
- 准备volume table 内容的内存缓存
- un-map layout volume 的 LEB0
- 写新的volume table内容到 LEB0
- un-map layout volume 的 LEB1
- 写新的volume table 内容到 LEB1
- 刷新 UBI 工作队列, 确保与un-mapped 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 it is newer). If one of the volume table copies is corrupted, UBI restores it from the other volume table copy.
当attach到 MTD 设备时, UBI 确保两个卷表一致, 如果不一致,可能是由意外重启导致的, UBI 使用 LEB0 的数据更新 LEB1中的数据, 如果其中一个volume table 数据损坏了, UBI 使用另一个进行恢复。