Score:3

fstrim is very slow on xfs and always return same value unlike ext4

de flag

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.

Score:5
dz flag

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

de flag
if you include some reliable sources I will accept your answer. Thanks a lot!
mx flag
@Wang AFAIK, this is not formally documented anywhere, so the only reliable source is likely to be the source code itself.
dz flag
Dug through some kernel source and added references
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.