3

I just noticed if I do time fstrim /<some ext4>; time fstrim /<some ext4> the 2nd call usually print 0 bytes trimmed and finished immediately. However if I do same thing on XFS, 2 of the calls are taking exactly same time and print exactly same amount of trimmed bytes. This makes me wonder does the trim really worked? If yes, is there any flag I can turn on to make the XFS not re-trim the already trimmed blocks?

I used all default settings to mount xfs.

Wang
  • 292
  • 1
  • 2
  • 11

1 Answers1

5

Ext4 caches extents that have already been trimmed in memory and doesn't retrim them until reboot.

XFS does not cache this information and retrims everything on every fstrim start.

Source:

Fstrim calls FITRIM IOCTL on file system that eventually calls into a FS specific internal function: https://github.com/util-linux/util-linux/blob/master/sys-utils/fstrim.c#L120

I omitted several in-between functions...

Ext4 driver checks memory structures if the range has already been discarded. If not, it tries to discard it and sets it as already discarded, preventing further discards until system is rebooted: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/ext4/mballoc.c?h=v6.0#n6442

XFS internal function has no such check and just discards everything: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/xfs/xfs_discard.c?h=v6.0#n151

Don Zoomik
  • 1,533
  • 9
  • 14