You are on page 1of 2

MTD

First off, an MTD is a "Memory Technology Device", so it's just "MTD". An


"MTD device" is a pleonasm.
Unix traditionally only knew block devices and character devices. Character
devices were things like keyboards or mice, that you could read current data from,
but couldn't be seek-ed and didn't have a size. Block devices had a fixed size and
could be seek-ed. They also happened to be organized in blocks of multiple bytes,
usually 512.
Flash doesn't match the description of either block or character devices.
They behave similar to block device, but have differences. For example, block
devices don't distinguish between write and erase operations. Therefore, a special
device type to match flash characteristics was created: MTD.So MTD is neither a
block nor a char device.

Block device MTD device


Consists of sectors Consists of eraseblocks
Sectors are small (512, 1024 bytes) Eraseblocks are larger (typically 128KiB)
Maintains 3 main operations: read from
Maintains 2 main operations: read sector and write
eraseblock, write to eraseblock, and erase
sector
eraseblock
Bad sectors are re-mapped and hidden by hardware (at
Bad eraseblocks are not hidden and should
least in modern LBA hard drives); in case of FTL
be dealt with in software
devices it is the responsibility of FTL to provide this
Eraseblocks wear-out and become bad and
Sectors are devoid of the wear-out property (in FTL
unusable after about 103 (for MLC NAND) -
devices it is the responsibility of FTL to provide this)
105 (NOR, SLC NAND) erase cycles

In order to use one of conventional file systems over an MTD device, 
you need a software layer which emulates a block device over the MTD 
device.   These   layers   are   often   called  Flash   Translation   Layers 
(FTLs). 
There is an extremely simple FTL layer in Linux MTD subsystem - mtdblock. It emulates block
devices over MTD devices. There is also an mtdblock_ro module which emulates read-only block
devices. When you load this module, it creates a block device for each MTD device in the system. The
block devices are then accessible via /dev/mtdblockX device nodes.
But in many cases using mtdblock is a very bad idea because what it basically does if you change
any sector of your mtdblockX device, it reads the whole corresponding eraseblock into the memory,
erases the eraseblock, changes the sector in RAM, and writes the whole eraseblock back. This is very
straightforward. If you have a power failure when the eraseblock is being erased, you lose all the block
device sectors in it. The flash will likely decay soon because you will wear few eraseblocks out - most
probably those ones which contain FAT/bitmap/inode table/etc.
Unfortunately it is a rather difficult task to create a good FTL layer and nobody still managed to
implement one for Linux. But now when we have UBI (see here) it is much easier to do it on top of
UBI.
It makes sense to use mtdblock_ro for read-only file systems or read-only mounts. For example,
one may use SquashFS as it compresses data quite well. But think twice before using mtdblock in
read-write mode. And don't try to use it on NAND flash as it is does not handle bad eraseblocks.

You might also like