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.

1 Answer 1

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

3
  • if you include some reliable sources I will accept your answer. Thanks a lot!
    – Wang
    Oct 15, 2022 at 21:56
  • 2
    @Wang AFAIK, this is not formally documented anywhere, so the only reliable source is likely to be the source code itself. Oct 15, 2022 at 22:16
  • 1
    Dug through some kernel source and added references
    – Don Zoomik
    Oct 16, 2022 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.