Trim/discard 修剪/丢弃 

Trim or discard is an operation on a storage device based on flash technology (SSD, NVMe or similar), a thin-provisioned device or could be emulated on top of other block device types. On real hardware, there’s a different lifetime span of the memory cells and the driver firmware usually tries to optimize for that. The trim operation issued by user provides hints about what data are unused and allow to reclaim the memory cells. On thin-provisioned or emulated this is could simply free the space.
修剪或丢弃是基于闪存技术(SSD、NVMe 或类似技术)、薄配置设备或可以在其他块设备类型之上模拟的存储设备上的操作。在真实硬件上,存储单元的寿命跨度不同,驱动程序固件通常会尝试优化这一点。用户发出的修剪操作提供了关于未使用数据的提示,并允许回收存储单元。在薄配置或模拟设备上,这可能只是简单地释放空间。

There are three main uses of trim that BTRFS supports:
BTRFS 支持的 trim 的三个主要用途有:

synchronous 同步的

enabled by mounting filesystem with -o discard or -o discard=sync, the trim is done right after the file extents get freed, this however could have severe performance hit and is not recommended as the ranges to be trimmed could be too fragmented
通过使用 -o discard-o discard=sync 挂载文件系统启用,trim 在文件范围被释放后立即执行,但这可能会严重影响性能,不建议使用,因为要修剪的范围可能过于分散

asynchronous 异步

enabled by mounting filesystem with -o discard=async, which is an improved version of the synchronous trim where the freed file extents are first tracked in memory and after a period or enough ranges accumulate the trim is started, expecting the ranges to be much larger and allowing to throttle the number of IO requests which does not interfere with the rest of the filesystem activity
通过挂载带有 -o discard=async 的文件系统启用,这是同步修剪的改进版本,其中释放的文件范围首先在内存中跟踪,然后在一段时间后或足够多的范围积累后开始修剪,期望范围要大得多,并允许限制 IO 请求的数量,这不会干扰文件系统其余部分的活动

manually by fstrim 通过 fstrim 手动

the tool fstrim starts a trim operation on the whole filesystem, no mount options need to be specified, so it’s up to the filesystem to traverse the free space and start the trim, this is suitable for running it as periodic service
工具 fstrim 在整个文件系统上启动修剪操作,不需要指定挂载选项,因此由文件系统遍历空闲空间并启动修剪,适合作为定期服务运行

The trim is considered only a hint to the device, it could ignore it completely, start it only on ranges meeting some criteria, or decide not to do it because of other factors affecting the memory cells. The device itself could internally relocate the data, however this leads to unexpected performance drop. Running trim periodically could prevent that too.
修剪仅被视为对设备的提示,设备完全可以忽略它,仅在满足某些条件的范围内启动,或者由于影响存储单元的其他因素而决定不执行。设备本身可以在内部重新定位数据,但这会导致意外的性能下降。定期运行修剪也可以防止这种情况发生。

When a filesystem is created by mkfs.btrfs(8) and is capable of trim, then it’s by default performed on all devices. Since kernel 6.2 the discard=async mount option is automatically enabled on devices that support that.
当使用 mkfs.btrfs(8) 创建支持修剪的文件系统时,默认情况下会在所有设备上执行修剪。自内核 6.2 起,支持该功能的设备会自动启用 discard=async 挂载选项。