Hard Drive – What is TRIM Exactly?

hard drivessdtrim

From the Wikipedia page of TRIM I get the impression that TRIM was introduced to lower the writes to a SSD, but letter the file system taking case of which blocks are unused.

If that is correct, does that mean, that SSD's that don't use TRIM writes zeros to the blocks, when a file is deleted, to let the SSD know the blocks are free?

Reading this

File systems within a guest tend to
not reuse blocks which means that even
if a file system remains small
relative to the virtual image size,
the actual image size tends to grow
until it reaches the maximum size.

leads me to think, SSD's don't overwrite with zeros? Or are file systems in guests, something completely different?

Best Answer

Flash memory devices (what's used for today's SSDs) can't write arbitrary data at any moment; before writing on a cell (typically 4KB) has to be erased first. Unfortunately, the erase operation is very slow; that's why flash devices were so much slower than magnetic drives, despite having no moving parts.

Modern SSDs hide the erasing time by maintaining a set of pre-erased cells, that means that a write command won't immediately overwrite existing data, instead the drive's controller picks an erased cell, remaps it, and writes with the new data. That (and several write-thought cache strategies) gives the drive a huge speedup, greatly surpassing magnetic drives.

To ensure that there's always a set of preerased cells, any time a cell isn't needed, the drive schedules it for background erasure and adds to the list of fee cells.

Unfortunately, existing filesystems didn't bother to tell the drive when a sector was free. The drive was supposed to be just a dumb repository of bits, after all. Deleting a file or any other operation that marks a sector as free from the filesystem's point of view was only a mark on some metadata structure. The sector itself wasn't touched. Even if the filesystem cleared it by writing zeros over it, the drive couldn't know if that meant the sector was free, or if the user wanted some zeros on a file. After a time, the drive wouldn't have any free cell to erase before writing; and performance degraded tragically.

The TRIM instruction was quickly drafted and adopted by most filesystems currently maintained. It's a simple signal that the filesystem uses to tell the drive that the content of a sector isn't important anymore. As soon as all sectors mapped on a cell are free, the SSD controller unmaps the cell and schedules it for erasure. If the host read those sectors, the SSD wouldn't bother fetching from Flash, it immediately responds with zeros; but the most important effect is to keep the list of preerased cells always replenished.

Still, most SSDs expose a smaller capacity than the physical size of the Flash memory, sometimes as low as 75%. That allows it to keep some unused cells even on a 100% full system, so that (over)writing used sectors is still fast.

Related Question